<?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\FinanceType;
use Dorcas\TabithaBundle\Entity\Finance;
//use Dorcas\TabithaBundle\Form\Type\NotesType;
//use Dorcas\TabithaBundle\Entity\Notes;


class FinanceController extends Controller
{


    public function XnoteformAction($client_id)
    {
        $client = $this->getClient($client_id);
        if (!$client) {
            throw $this->createNotFoundException('Dorcas: Unable to find matching client = ' . $client_id);
        }
        

        $notes  = new Notes();
        $notes->setClient($client);
        $request = $this->getRequest();
        $form = $this->createForm(new NotesType(), $notes);
        $form->bindRequest($request);

        if ($form->isValid()) {
            // TODO: Persist the comment entity
            $em = $this->getDoctrine()
                       ->getEntityManager();
            $em->persist($notes);
            $em->flush();

            return $this->redirect($this->generateUrl('DorcaTabithaBundle_blog_show', array(
                'id' => $notes->getClient()->getId())) .
                '#notes-' . $notes->getId()
            );
        }

        return $this->render('DorcasTabithaBundle:Notes:noteform.html.twig', array(
           // 'notes' => $notes,
            'form'    => $form->createView()
        ));
    }

    protected function getClient($client_id)
    {
        $em = $this->getDoctrine()
                    ->getEntityManager();

        $client = $em->getRepository('DorcasTabithaBundle:Client')->find($client_id);

        return $client;
    }
    
    
    protected function getFinance($client_id)
    {
        $em = $this->getDoctrine()
                    ->getEntityManager();
        // Gets a record with client_id = $client_id
        $finance = $em->getRepository('DorcasTabithaBundle:Finance')->findOneBy(array('client_id' => $client_id));

        return $finance;
    }
    
        public function newAction($client_id)
    {
        $client = $this->getClient($client_id);
        if (!$client) {
            //throw $this->createNotFoundException('Dorcas: Unable to find matching client = ' . $client_id);
            return $this->redirect($this->generateUrl('DorcasTabithaBundle_clientinformation_new')
            );
        }
        
        // Use to check for an existing one-to-one client_id in the Finance table
        $finance = $this->getFinance($client_id);
        if ($finance) {
            //throw $this->createNotFoundException('Dorcas: Finance record already exists, client_id = ' . $client_id);
            return $this->redirect($this->generateUrl('DorcasTabithaBundle_finance_show', array(
                'client_id' => $client_id))
            );
        }

        $finance = new Finance();
        $finance->setClient($client);
        $form   = $this->createForm(new FinanceType(), $finance);

        return $this->render('DorcasTabithaBundle:Finance:financeformnew.html.twig', array(
            'client'      => $client,
            'finance' => $finance,
            'form'   => $form->createView()
        ));
    }
    

    /**
     * Show a finance entry
     */
    public function showAction($client_id)
    {
        $client = $this->getClient($client_id);
        if (!$client) {
            //throw $this->createNotFoundException('Dorcas: Unable to find matching client = ' . $client_id);
            return $this->redirect($this->generateUrl('DorcasTabithaBundle_clientinformation_new')
            );
        }
        
        $finance = $this->getFinance($client_id);
        if (!$finance) {
            //throw $this->createNotFoundException('Dorcas: No finance record exists, client_id = ' . $client_id);
            return $this->redirect($this->generateUrl('DorcasTabithaBundle_finance_new', array(
                'client_id' => $client_id))
            );
        }
        
        $form = $this->createForm(new FinanceType(), $finance);

    return $this->render('DorcasTabithaBundle:Finance:financeformupdate.html.twig', array(
        'client'      => $client,
        'finance'  => $finance,
        'form' => $form->createView(),
        ));
    }
    
    
     public function updateAction($client_id)
{
    $request = $this->get('request');
    
    // don't know what this is doing???
    if (is_null($client_id)) {
        $postData = $request->get('testimonial');
        $client_id = $postData['client_id'];
    }

    // Using this to check for existing client_id
    $client = $this->getClient($client_id);
    if (!$client) {
            throw $this->createNotFoundException('Dorcas: Unable to find matching client = ' . $client_id);
    }
    
    $finance = $this->getFinance($client_id); 
    $form = $this->createForm(new FinanceType(), $finance);
    
    $navnumber = $request->request->get("passnav");

    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);

        if ($form->isValid()) {
            // save the object to the database
            $em = $this->getDoctrine()
                        ->getEntityManager();
            $em->flush();
                       
            switch ($navnumber) {
                        case 9:
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_homepage'));
                            break;
                        case 1:
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_clientinformation_show', array(
                        'client_id' => $client->getClientId())));
                            break;                  
                        case 2:
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_finance_show', array(
                        'client_id' => $client->getClientId())));
                            break;
                        case 3:
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_notes_show', array(
                        'client_id' => $client->getClientId())));
                            break;
                        case 4:
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_assistance_show', array(
                        'client_id' => $client->getClientId())));
                            break;
                        case "5":
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_referrals_show', array(
                        'client_id' => $client->getClientId())));
                            break;
                        case "7":
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_users_show'));
                            break;
                        default:
                           return $this->redirect($this->generateUrl('DorcasTabithaBundle_notes_show', array(
                        'client_id' => $client->getClientId())));
                    } 
          }
        return $this->render('DorcasTabithaBundle:Notes:notelayout.html.twig', array(
            'note' => $finance,
            'form'    => $form->createView()
        ));
    }
    
}
     
        public function createAction($client_id)
    {
   
        $client = $this->getClient($client_id);
        if (!$client) {
            throw $this->createNotFoundException('Dorcas: Unable to find matching client = ' . $client_id);
        }

        $finance  = new Finance();
        $finance->setClient($client);
        $request = $this->getRequest();
        $form    = $this->createForm(new FinanceType(), $finance);
        $form->bindRequest($request);
        
        $navnumber = $request->request->get("passnav");

        if ($form->isValid()) {
            $em = $this->getDoctrine()
                       ->getEntityManager();
            $em->persist($finance);
            $em->flush();
            
             switch ($navnumber) {
                        case 9:
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_homepage'));
                            break;
                        case 1:
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_clientinformation_show', array(
                        'client_id' => $client->getClientId())));
                            break;                  
                        case 2:
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_finance_new', array(
                        'client_id' => $client->getClientId())));
                            break;
                        case 3:
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_notes_show', array(
                        'client_id' => $client->getClientId())));
                            break;
                        case 4:
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_assistance_show', array(
                        'client_id' => $client->getClientId())));
                            break;
                        case "5":
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_referrals_new', array(
                        'client_id' => $client->getClientId())));
                            break;
                        case "7":
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_users_show'));
                            break;
                        default:
                           return $this->redirect($this->generateUrl('DorcasTabithaBundle_notes_show', array(
                        'client_id' => $client->getClientId())));
                    }    
        }

        return $this->render('DorcasTabithaBundle:Notes:notelayout.html.twig', array(
            'note' => $finance,
            'form'    => $form->createView()
        ));
    }
    
}
