<?php
/*
 * Developer : John D'Alto (jdalto@yahoo.com)
 * Date : 9/1/2012
 * All code (c)2012 JLS Consulting, all rights reserved
 */

namespace Dorcas\TabithaBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Dorcas\TabithaBundle\Entity\Client;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Dorcas\TabithaBundle\Form\Type\ClientInformationType;
use Dorcas\TabithaBundle\Form\Type\PledgeType;
use Dorcas\TabithaBundle\Entity\Pledge;
use Dorcas\TabithaBundle\Form\Type\PromissoryType;
use Dorcas\TabithaBundle\Entity\Promissory;
use Dorcas\TabithaBundle\Entity\Referrals;

class PrintController extends Controller
{
    public function referredToAction()
    {
        $request = Request::createFromGlobals();
       
        $data = $request->query->all();
//            var_dump($data);
        $start = $data['start'];
        $end = $data['end'];
        return $this->render('DorcasTabithaBundle:Print:referredtoform.html.twig', array (
            'start' => $start,
            'end' => $end,
        ));
    }   
    
     public function referredToSelectAction($agencyNumber, $agencyName)
    {
//            $request = $this->getRequest();
            $request = Request::createFromGlobals();
       
            $data = $request->query->all();
//            var_dump($data);
            $start = $data['start'];
            $end = $data['end'];
            
            // Convert to YYYY-MM-DD
            $date_start = date('Y-m-d', strtotime($start));
            // Add a day to compensate for 00:00 AM saved time
            $date_end =  date('Y-m-d', strtotime($end) + 86400);
     
         
         $agency = array ( 'dorcasprograms_benefitbank', 'dorcasprograms_budgetcounseling', 'dorcasprograms_childcare', 
            'dorcasprograms_jobsforlifeatdorcas', 'dorcasprograms_joblink', 'dorcasprograms_margaretkeller', 
            'dorcasprograms_resourceoffice', 'dorcasprograms_other',
             
            'crisis_catholicparishoutreach', 'crisis_resourcesforseniors', 'crisis_salvationarmy', 'crisis_trianglefamilyservices',
            'crisis_wakecountyhumanservices', 'crisis_westernwake', 'crisis_withlovefromjesus', 'crisis_other',
             
            'housing_cornerstone', 'housing_raleighrescuemission', 'housing_salvationarmy', 
            'housing_southwilmingtonstreetshelter',
            'housing_wakeinterfaithhospitalitynetwork', 'housing_interact', 
            'housing_caryingplace', 'housing_other', 
             
            'employment_jobsforlife', 'employment_joblink', 'employment_stepupministry', 'employment_otheremploymentprogram',         

            'education_english', 'education_gedclasses', 'education_waketechclasses', 'education_parentingclasses', 
            'education_othertraining', 
             
            'medical_alliancemedicalministry', 'medical_figs', 'medical_ncdentalbus', 
            'medical_theopendoorclinic', 'medical_uncschoolofdentistry', 'medical_wakehealthservices',
            'medical_othermedicalprogram', 
                       
            'personalcounseling_centerforhopeandhealing',
            'personalcounseling_trianglefamilyservices', 'personalcounseling_wakementalhealth', 'personalcounseling_othercounselingprogram',      
             
            'substanceabuseprograms_havenhouseinc', 'substanceabuseprograms_oxfordhouse', 'substanceabuseprograms_southlight', 'substanceabuseprograms_thehealingplace',
            'substanceabuseprograms_trianglealanoclub', 'substanceabuseprograms_othersubstanceabuseprogram', 
             
            'legalservices_legalaidofnorthcarolina',
            'legalservices_northcarolinalegalservicesinc', 'legalservices_otherlegalservices', 
             
            'specialprograms_christmas', 'specialprograms_thanksgiving', 'specialprograms_emaillist', 'specialprograms_other' );
         
         
         $where = $agency[$agencyNumber];
       // Get the Referral Records       
        $em = $this->getDoctrine()->getEntityManager();
        $query = $em->createQuery(
        "SELECT m FROM DorcasTabithaBundle:Referrals m WHERE m.$where = 1 
                AND m.created_dt BETWEEN '$date_start' AND '$date_end' ORDER BY m.created_dt");
        $referrals = $query->getResult();
        if (count($referrals) == 0) 
        {
            unset ($referrals);
            return $this->render('DorcasTabithaBundle:Print:referredselectform.html.twig');
        }
        else
        {
            return $this->render('DorcasTabithaBundle:Print:referredselectform.html.twig', array(
            'agencyName' => $agencyName,
            'referrals' => $referrals,  
            'agencyNumber' => $agencyNumber,
        ));
        }
    }
    
    
    public function pledgeShowAction($client_id)
    {
        // Get Date (from Twig), Client Name and Address
        $em = $this->getDoctrine()->getEntityManager();
        // Get associated Client record
        $client = $em->getRepository('DorcasTabithaBundle:Client')->find($client_id);

        $request = $this->get('request');
    
        // Get Assistance ID from URI 
        //$data = $request->query->all();      
//            var_dump($data);          
        //$assistance_id = $data['assistance_id'];
        
        // Get associated Assistance record
        //$assistance = $em->getRepository('DorcasTabithaBundle:Assistance')->find($assistance_id);
        
        // Stuff Client and Assistance info into Pledge class
        $pledge = New Pledge();
        $pledge->setClientId($client_id);
        $pledge->setClientName($client->getFullName());
        //$pledge->setClientAddress($client->getCurrentAddress());
		$pledge->setClientAddress($client->getFullAddress());
        //$pledge->setAssistanceId($assistance->getAssistanceId());
        // $pledge->setPledgeDorcas($assistance->getCheckDorcas());
        // $pledge->setPledgeSfumc($assistance->getCheckSfumc());
        // $pledge->setPledgeOasis($assistance->getCheckOasis());
        // $pledge->setPledgeOther($assistance->getCheckOther());
        // $pledge->setAssistanceType($assistance->getCheckPurpose());
        // $pledge->setVendorName($assistance->getChecksIssuedPayee());
		$usr= $this->get('security.context')->getToken()->getUser();
		$pledge->setCounselor($usr->getFirstName() . ' ' . $usr->getLastName());
        
        // Stuff Assistance info into Pledge class
        //$pledge->setAssistanceId($assistance_id);
        
        $form = $this->createForm(new PledgeType(), $pledge);
        if ($request->getMethod() == 'POST') {
            $form->bindRequest($request);
                if ($form->isValid()) {
                    $em = $this->getDoctrine()
                       ->getEntityManager();
                    $pledge->setPledgeTotal($pledge->getPledgeDorcas() 
                            + $pledge->getPledgeOther() + $pledge->getPledgeOther2());
                    $em->persist($pledge);
                    $em->flush();
                    // Add render of printable page here
                    return $this->render('DorcasTabithaBundle:Print:pledgeformprintable.html.twig', array(
                        'pledge' => $pledge,
                        ));        
                }
        }

        return $this->render('DorcasTabithaBundle:Print:pledgeform.html.twig', array(
            'form' => $form->createView(),
            'client' => $client,
            'pledge' => $pledge,
            ));
    }   
    
    public function promissoryShowAction($client_id)
    {
        // Get Date (from Twig), Client Name and Address
        $em = $this->getDoctrine()->getEntityManager();
        // Get associated Client record
        $client = $em->getRepository('DorcasTabithaBundle:Client')->find($client_id);

        $request = $this->get('request');
    
        // Get Assistance ID from URI 
        //$data = $request->query->all();      
//            var_dump($data);          
        //$assistance_id = $data['assistance_id'];
        
        // Get associated Assistance record
        //$assistance = $em->getRepository('DorcasTabithaBundle:Assistance')->find($assistance_id);
        
        // Stuff Client and Assistance info into Promissory class
        $promissory = New Promissory();
        $promissory->setClientId($client_id);
        $promissory->setClientName($client->getFullName());
        //$promissory->setClientAddress($client->getCurrentAddress());
		$promissory->setClientAddress($client->getFullAddress());
        //$promissory->setAssistanceId($assistance->getAssistanceId());
        //$promissory->setPledgeDorcas($assistance->getCheckDorcas());
        //$promissory->setPledgeSfumc($assistance->getCheckSfumc());
        //$promissory->setPledgeOasis($assistance->getCheckOasis());
        //$promissory->setPledgeOther($assistance->getCheckOther());
        //$promissory->setAssistanceType($assistance->getCheckPurpose());
        //$promissory->setVendorName($assistance->getChecksIssuedPayee());
        $usr= $this->get('security.context')->getToken()->getUser();
        $promissory->setCounselor($usr->getFirstName() . ' ' . $usr->getLastName());
        
        // Stuff Assistance info into Pledge class
        //$promissory->setAssistanceId($assistance_id);
        
        $form = $this->createForm(new PromissoryType(), $promissory);
        if ($request->getMethod() == 'POST') {
            $form->bindRequest($request);
                if ($form->isValid()) {
                    $em = $this->getDoctrine()
                       ->getEntityManager();
                    $em->persist($promissory);
                    $em->flush();
                    // Add render of printable page here
                    return $this->render('DorcasTabithaBundle:Print:promissoryformprintable.html.twig', array(
                        'promissory' => $promissory,
                        ));        
                }
        }

        return $this->render('DorcasTabithaBundle:Print:promissoryform.html.twig', array(
            'form' => $form->createView(),
            'client' => $client,
            //'assistance' => $assistance,
            'promissory' => $promissory,
            ));
    }   
    
    
}
