<?php

namespace OfficeBrain\CustomBundle\CmsBundle\Manager\Extended;

use OfficeBrain\CustomBundle\CmsBundle\Manager\AbstractManager\InvoiceRequestAbstract as InvoiceRequestAbstract;
use OfficeBrain\CustomBundle\CmsBundle\Entity\InvoiceRequest;
use OfficeBrain\CustomBundle\CmsBundle\Form\InvoiceRequestType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;

class InvoiceRequestExtended extends InvoiceRequestAbstract
{
    public function __construct($container,$entityManager, $request, $emailManager, $paginatorManager)
    {
        parent::__construct($container,$entityManager, $request, $emailManager, $paginatorManager);
    }

    public function __destruct()
    {
        parent::__destruct();
    }
    
    public function setInvoiceRequest($invoiceRequest) {
    	$invoiceRequest->setUserId($this->projectSetting['user_id']);
    	$invoiceRequest->setCreatedUid($this->projectSetting['user_id']);
    	$invoiceRequest->setInstanceId($this->projectSetting['instance_id']);
    	$invoiceRequest->setInstanceType($this->projectSetting['instance_type']);
    	$invoiceRequest->setCreatedAt(new \DateTime());
    	$invoiceRequest->setUpdatedAt(new \DateTime());
    	$invoiceRequest->setUpdatedUid($this->projectSetting['user_id']);
    	
    	return $this->invoiceRequestRepository->set($invoiceRequest);
    }
    
    /**
     * @author Employee Id: 5460
     *
     * Description : This function is used to get list of all invoice request
     *
     * @param : $file object
     *
     * @return boolean
     *
     * @throws Exception : Null
     *
     **/
    public function getCmsInvoiceRequestList()
    {
    	$data = array();
    
    	//$cmsUploadArtworkCenterList = $this->uploadArtworkCenterRepo->findAll();
    	$cmsInvoiceRequestList = $this->invoiceRequestRepository->getInvoiceList();
    	 
    	if (count($cmsInvoiceRequestList) > 0) {
    		$data['pagination'] = $this->paginatorManager->paginate($cmsInvoiceRequestList, $this->request->get('page'), $this->recordsPerPage, array(), count($cmsInvoiceRequestList));
    	}
    
    	$data['records_per_page'] = $this->recordsPerPage;
    	$data['page'] = $this->request->get('page');
    
    	return $data;
    }
    
    
    /**
     * @author Employee Id: 5460
     *
     * Description : This function is used to get list of all invoice request after searching
     *
     * @param : $file object
     *
     * @return boolean
     *
     * @throws Exception : Null
     *
     **/
    public function searchCmsInvoiceRequestList()
    {
    	$data = array();
    	$option = $this->request->get('option');
    	$keyword = $this->request->get('keyword');
    
    	$cmsInvoiceRequestList = $this->invoiceRequestRepository->findByOption($option, $keyword);
    
    	if (count($cmsInvoiceRequestList) > 0) {
    		$data['pagination'] = $this->paginatorManager->paginate($cmsInvoiceRequestList, $this->request->get('page'), $this->recordsPerPage, array(), count($cmsInvoiceRequestList));
    	}
    
    	$data['records_per_page'] = $this->recordsPerPage;
    	$data['page'] = $this->request->get('page');
    
    	return $data;
    }
    

    /**
     * @author Employee Id: 5069
     *
     * Description : Soft delete requested invoice request
     *
     * @param 1 : invoice Request  id
     *
     * @return type : Null
     *
     * @throws Exception: Null
     */
    public function deleteInvoiceRequestId($id)
    {
    	$inventObj = $this->invoiceRequestRepository->find($id);
    	/* if (!is_object($eventObj->getTemplate())) {
    	 $eventObj->setDeletedAt(new \DateTime());
    	 $this->commit($eventObj);
    	 $this->session->getFlashBag()->add('success_message', $this->translator->trans('email.event.message.delete_success'));
    	 } else {
    	 $this->session->getFlashBag()->add('error_message', $this->translator->trans('email.event.message.delete_error1'));
    	 } */
    
    	$currentDateTime = new \DateTime();
    
    	$inventObj->setDeletedAt($currentDateTime);
    	$inventObj->setDeletedUid($this->deletedUid);
    	$this->commit($inventObj);
    	$this->session->getFlashBag()->add('success_message', $this->translator->trans('cms.admin.list.invoive_request.delete_message'));
    	//unset variable
    	unset($inventObj);
    	unset($currentDateTime);
    }
    
    /**
     * @author Employee Id: 5069
     *
     * Description : This function is used to get detali of  invoice request using id
     *
     * 
     *
     * @return boolean
     *
     * @throws Exception : Null
     *
     **/
    public function getCmsInvoiceRequestDetail($id)
    {  
    	$data = array();
    
    	$cmsInvoiceRequestDetail = $this->invoiceRequestRepository->findById($id);
        
    	$data['data'] = $cmsInvoiceRequestDetail;
    
    	return $data;
    }
    
    public function sendInvoiceRequestEmail($invoiceRequest) {
    	$eventKey = 'invoice_request_email';
    	$user = $this->projectUserSetting['user_data'];

    	$params = array(
    			'sender_name' => $invoiceRequest->getSenderName(),
    			'company_name' => $invoiceRequest->getCompanyName(),
    			'po_number' => $invoiceRequest->getPoNumber(),
    			'fax' => $invoiceRequest->getFax(),
    			'email' => $invoiceRequest->getEmail()
    	);
    	$languageId = $this->projectSetting['culture_id'];
    	
    	$to = $user->getEmail();
//     	$from =  $this->projectSetting['admin_email'];
    	$from =  "";
    	$subject = "";
    	$attachment = array();
    	$cc = array();
    	$bcc = array();
    	
    	$this->emailManager->sendMail($eventKey, $to, $params, $languageId, $from, $subject, $attachment, $cc, $bcc);
    	
    	return true;
    }
}
