<?php

namespace OfficeBrain\CustomBundle\CmsBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use OfficeBrain\CustomBundle\CmsBundle\Entity\InvoiceRequest;
use OfficeBrain\CustomBundle\CmsBundle\Form\InvoiceRequestType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

/**
 *
 * @author Employee Id: 5412
 *
 * Description : Controller class for invoice request
 *
 * @Route("/{_locale}")
 */
class InvoiceRequestController extends Controller
{
	/**
	 * @author Employee Id: 5412
	 *
	 * Description : This function is used to set member variables default value
	 *
	 * @param : None
	 *
	 * @return: NULL
	 *
	 * @throws Exception: Null
	 */
	public function __construct()
	{
		$this->responseArray = null;
		$this->request = null;
		$this->translator = null;
		$this->projectSetting = null;
		$this->user_id = null;
		$this->session = null;
	}
	
	/**
	 * @author Employee Id: 5412
	 *
	 * Description : This function is used to unset member variables
	 *
	 * @param : None
	 *
	 * @return NULL
	 *
	 * @throws Exception: Null
	 */
	public function __destruct()
	{
		unset($this->responseArray);
		unset($this->request);
		unset($this->translator);
		unset($this->projectSetting);
		unset($this->user_id);
		unset($this->session);
	}
	
	/**
	 * @author Employee Id: 5412
	 *
	 * Description : This function is used to initialize the variables
	 *
	 * @param : None
	 *
	 * @return NULL
	 *
	 * @throws Exception: Null
	 */
	public function initAction()
	{
		$this->request = $this->getRequest();

		$this->responseArray = array();
		$this->translator = $this->get('translator');
		$this->projectSetting = $this->request->get('project_setting');
		$this->user_id = $this->projectSetting['user_id'];
		$this->em = $this->container->get('doctrine')->getEntityManager();
		$this->userId = $this->projectSetting['user_id'];
		$this->session = $this->get('session');

		$this->invoiceRequestCustomManager = $this->get('office_brain_cms_invoice_request_manager_custom');
	}
	
	/**
	 * @author Employee Id: 4494
	 *
	 * Description : This function is used to add new invoice request
	 *
	 * @param : None
	 *
	 * @return NULL
	 *
	 * @throws Exception: Null
	 *
	 * @Route("/invoice-request", name="office_brain_custombundle_cmsbundle_invoice_request_add")
	 * @Method("GET")
	 * @Template("OfficeBrainCustomBundleCmsBundle:InvoiceRequest:add.html.twig")
	 */
	public function addAction()
	{
		$this->initAction();

		if ($this->userId == 0) {
			$this->get('session')->set('_security.main.target_path', $this->generateUrl($this->request->get('_route')));
			return $this->redirect($this->generateUrl('office_brain_userbundle_login_login'));
		}

		$this->responseArray['form'] = $this->createForm(new InvoiceRequestType())->createView();
		
		return $this->responseArray;
	}
	
	/**
	 * @author Employee Id: 4494
	 *
	 * Description : This function is used to add new invoice request
	 *
	 * @param : None
	 *
	 * @return NULL
	 *
	 * @throws Exception: Null
	 *
	 * @Route("/invoice-request", name="office_brain_custombundle_cmsbundle_invoice_request_process_add")
	 * @Method("POST")
	 * @Template("OfficeBrainCustomBundleCmsBundle:InvoiceRequest:add.html.twig")
	 */
	public function processAddAction()
	{
		$this->initAction();
	
		if ($this->userId == 0) {
			$this->get('session')->set('_security.main.target_path', $this->generateUrl($this->request->get('_route')));
			return $this->redirect($this->generateUrl('office_brain_userbundle_login_login'));
		}
	
		$invoiceRequest = new InvoiceRequest();
		$form = $this->createForm(new InvoiceRequestType(), $invoiceRequest);
	
		$form->handleRequest($this->request);
		
		if ($form->isValid()) {
			$this->invoiceRequestCustomManager->setInvoiceRequest($form->getData());
			$this->invoiceRequestCustomManager->sendInvoiceRequestEmail($form->getData());

			$this->session->getFlashBag()->set('success_message', $this->translator->trans('cms.front.invoice_request.action.add_success'));
			
			return $this->redirect($this->generateUrl('office_brain_custombundle_cmsbundle_invoice_request_add'));
		}
		
		$this->responseArray['form'] = $form->createView();
		return $this->responseArray;
	}
}
