<?php
class EmailingAjax extends EntityRepository {
    
    public $flashmessenger = null;
    
    public function __construct() {
        if(!$this->flashmessenger instanceof FlashMessenger){
            $this->flashmessenger = new FlashMessenger();
        }
    }
    
    public function getResponse($request, $options) {
        return $this->$request($options);
    }
    
    public function _getTranslation($text){
        $translator = new Translator();
        return $translator->_getTranslation($text);
    }
    
    public function getTranslation($options){
        $msj = $options['msj'];
        
        return array(
            'response'=>true,
            'translation'=>$this->_getTranslation($msj)
                );
    }   


/*EMAILING BOL*/
    public function getDataOperation($options){
        $operation = $options['operation_name'];
        $id = $options['operation_id'];
        
        switch($operation){
            case 'purchase_product':
                $manifestRepo = new ManifestRepository();
                $manifestData = $manifestRepo->getById($id);                
                $subject = $this->_getTranslation("Orden de compra # {$manifestData['purchase_number']}");
                $messageMail = $this->_getTranslation("Revisar archivos adjuntos porfavor.");
                
                $vendorRepo = new VendorRepository();
                $vendorData = $vendorRepo->getById($manifestData['id_vendor']);                
                $to = $vendorData['email'];
                
                $defaultAttachments = array();
                $manifestPDF = new ManifestPDF($id,true);
                $manifestFileName= $manifestPDF->getFileName();
                $manifestFilePath = $manifestPDF->getPathFileCreated();                
                $manifestFilePathForLink = $manifestPDF->getPathForLink();
                
                $defaultAttachments[] = array(
                    'fileName'=>$manifestFileName,
                    'filePathForLink'=>$manifestFilePathForLink,
                    'filePath'=>$manifestFilePath
                );
                
                $purchasePDF = new PurchaseProductPDF($id,true);
                $purchaseFileName= $purchasePDF->getFileName();
                $purchaseFilePath = $purchasePDF->getPathFileCreated();
                $purchaseFileForLink = $purchasePDF->getPathForLink();              
                
                
                $defaultAttachments[] = array(
                    'fileName'=>$purchaseFileName,
                    'filePathForLink'=>$purchaseFileForLink,
                    'filePath'=>$purchaseFilePath
                );
                
                $fileManagement = new FileManagement();
                $attachments = $fileManagement->getAttachemntsFilesByOperationAndPrefix('manifest', $id,$defaultAttachments);                
                $attachments = '<strong>'.$this->_getTranslation("Archivos adjuntos").'</strong><br/>'.$attachments;
                
                break;
            
            case 'purchase_good_and_service':
                $purchaseRepo = new PurchaseGoodAndServiceRepository();
                $purchaseData = $purchaseRepo->getById($id);                
                $subject = $this->_getTranslation("Orden de compra # {$purchaseData['purchase_number']}");
                $messageMail = $this->_getTranslation("Revisar archivos adjuntos porfavor.");
                
                $vendorRepo = new VendorRepository();
                $vendorData = $vendorRepo->getById($purchaseData['id_vendor']);                
                $to = $vendorData['email'];
                
                $defaultAttachments = array();                
                $purchasePDF = new PurchaseGoodAndServicePDF($id,true);
                $purchaseFileName= $purchasePDF->getFileName();
                $purchaseFilePath = $purchasePDF->getPathFileCreated();
                $purchaseFileForLink = $purchasePDF->getPathForLink();              
                
                
                $defaultAttachments[] = array(
                    'fileName'=>$purchaseFileName,
                    'filePathForLink'=>$purchaseFileForLink,
                    'filePath'=>$purchaseFilePath
                );
                
                $fileManagement = new FileManagement();
                $attachments = $fileManagement->getAttachemntsFilesByOperationAndPrefix('purchase', $id,$defaultAttachments);                
                $attachments = '<strong>'.$this->_getTranslation("Archivos adjuntos").'</strong><br/>'.$attachments;
                
                break;
            
            case 'receiving':
                $receivingRepo = new ReceivingRepository();
                $receivingata = $receivingRepo->getById($id);                
                $subject = $this->_getTranslation("Recibo de compra # {$receivingata['purchase_number']}");
                $messageMail = $this->_getTranslation("Revisar archivos adjuntos porfavor.");
             
                $to = '';
                
                $defaultAttachments = array();                
                $receivinPDF = new ReceivingPDF($id,true);
                $receivingFileName= $receivinPDF->getFileName();
                $receivingFilePath = $receivinPDF->getPathFileCreated();
                $receivingFileForLink = $receivinPDF->getPathForLink();              
                
                
                $defaultAttachments[] = array(
                    'fileName'=>$receivingFileName,
                    'filePathForLink'=>$receivingFileForLink,
                    'filePath'=>$receivingFilePath
                );
                
                $fileManagement = new FileManagement();
                $attachments = $fileManagement->getAttachemntsFilesByOperationAndPrefix('receiving', $id,$defaultAttachments);                
                $attachments = '<strong>'.$this->_getTranslation("Archivos adjuntos").'</strong><br/>'.$attachments;
                
                break;
            
            case 'sales_order':
                $salesOrderRepo = new ShippingRequestRepository();
                $salesOrderData = $salesOrderRepo->getById($id);                
                $subject = $this->_getTranslation("Orden de venta # {$salesOrderData['sales_order_num']}");
                $messageMail = $this->_getTranslation("Revisar archivos adjuntos porfavor.");
             
                $customerRepo = new CustomerRepository();
                $customerData = $customerRepo->getById($salesOrderData['id_customer']);                
                $to = $customerData['email'];
                
                $defaultAttachments = array();                
                $salesOrderPDF = new SalesOrderPDF($id,true);
                $salesOrderFileName = $salesOrderPDF->getFileName();
                $salesOrderFilePath = $salesOrderPDF->getPathFileCreated();
                $salesOrderFileForLink = $salesOrderPDF->getPathForLink();                
                
                $defaultAttachments[] = array(
                    'fileName'=>$salesOrderFileName,
                    'filePathForLink'=>$salesOrderFileForLink,
                    'filePath'=>$salesOrderFilePath
                );
                
                if($salesOrderData['invoiced']=='1'){
                    $invoicePDF = new InvoicePDF($salesOrderData['invoice_id'],true);
                    $invoiceFileName = $invoicePDF->getFileName();
                    $invoiceFilePath = $invoicePDF->getPathFileCreated();
                    $invoiceFileForLink = $invoicePDF->getPathForLink();                

                    $defaultAttachments[] = array(
                        'fileName'=>$invoiceFileName,
                        'filePathForLink'=>$invoiceFileForLink,
                        'filePath'=>$invoiceFilePath
                    );
                }
                      
                $pickticketPDF = new PickTicketPDF($id,true);
                $pickticketFileName = $pickticketPDF->getFileName();
                $pickticketFilePath = $pickticketPDF->getPathFileCreated();
                $pickticketFileForLink = $pickticketPDF->getPathForLink();                
                
                $defaultAttachments[] = array(
                    'fileName'=>$pickticketFileName,
                    'filePathForLink'=>$pickticketFileForLink,
                    'filePath'=>$pickticketFilePath,
                    'notChecked'=>'notChecked'
                );             
                
                $fileManagement = new FileManagement();
                $attachments = $fileManagement->getAttachemntsFilesByOperationAndPrefix('sales_order', $id,$defaultAttachments);                
                $attachments = '<strong>'.$this->_getTranslation("Archivos adjuntos").'</strong><br/>'.$attachments;
                
                break;
                
            case 'invoice':
                $invoiceRepo = new InvoiceRepository();
                $invoiceData = $invoiceRepo->getById($id);                
                $subject = $this->_getTranslation("Orden de venta # {$invoiceData['invoice_num']}");
                $messageMail = $this->_getTranslation("Revisar archivos adjuntos porfavor.");

                $customerRepo = new CustomerRepository();
                $customerData = $customerRepo->getById($invoiceData['id_customer']);                
                $to = $customerData['email'];

                $defaultAttachments = array();             
                $invoicePDF = new InvoicePDF($id,true);
                $invoiceFileName = $invoicePDF->getFileName();
                $invoiceFilePath = $invoicePDF->getPathFileCreated();
                $invoiceFileForLink = $invoicePDF->getPathForLink();                

                $defaultAttachments[] = array(
                    'fileName'=>$invoiceFileName,
                    'filePathForLink'=>$invoiceFileForLink,
                    'filePath'=>$invoiceFilePath
                );

                $salesOrderPDF = new SalesOrderPDF($invoiceData['sales_order_id'],true);
                $salesOrderFileName = $salesOrderPDF->getFileName();
                $salesOrderFilePath = $salesOrderPDF->getPathFileCreated();
                $salesOrderFileForLink = $salesOrderPDF->getPathForLink();                

                $defaultAttachments[] = array(
                    'fileName'=>$salesOrderFileName,
                    'filePathForLink'=>$salesOrderFileForLink,
                    'filePath'=>$salesOrderFilePath
                );
                
                $shippingRepo = new ShippingRepository();
                $shippingData = $shippingRepo->getByIdShippingRequest($invoiceData['sales_order_id']);   
                
                if($shippingData){
                    $shippingPDF = new BOLPDF($shippingData['id'],true);
                    $shippingFileName= $shippingPDF->getFileName();
                    $shippingFilePath = $shippingPDF->getPathFileCreated();
                    $shippingFileForLink = $shippingPDF->getPathForLink();           

                    $defaultAttachments[] = array(
                        'fileName'=>$shippingFileName,
                        'filePathForLink'=>$shippingFileForLink,
                        'filePath'=>$shippingFilePath,
                        'notChecked'=>'notChecked'
                    );
                }               

                $pickticketPDF = new PickTicketPDF($invoiceData['sales_order_id'],true);
                $pickticketFileName = $pickticketPDF->getFileName();
                $pickticketFilePath = $pickticketPDF->getPathFileCreated();
                $pickticketFileForLink = $pickticketPDF->getPathForLink();                

                $defaultAttachments[] = array(
                    'fileName'=>$pickticketFileName,
                    'filePathForLink'=>$pickticketFileForLink,
                    'filePath'=>$pickticketFilePath,
                    'notChecked'=>'notChecked'
                );

                $fileManagement = new FileManagement();
                $attachments = $fileManagement->getAttachemntsFilesByOperationAndPrefix('sales_order', $id,$defaultAttachments);                
                $attachments = '<strong>'.$this->_getTranslation("Archivos adjuntos").'</strong><br/>'.$attachments;
                
                break;
            
            case 'shipping':
                $shippingRepo = new ShippingRepository();
                $shippingData = $shippingRepo->getById($id);                
                $subject = $this->_getTranslation("BOL # {$shippingData['id_shipping_request']}");
                $messageMail = $this->_getTranslation("Revisar archivos adjuntos porfavor.");
                
                $salesOrderRepo = new ShippingRequestRepository();
                $salesOrderData =  $salesOrderRepo->getById($shippingData['id_shipping_request']);
             
                $customerRepo = new CustomerRepository();
                $customerData = $customerRepo->getById($salesOrderData['id_customer']);                
                $to = $customerData['email'];
                
                $defaultAttachments = array();                
                $shippingPDF = new BOLPDF($id,true);
                $shippingFileName= $shippingPDF->getFileName();
                $shippingFilePath = $shippingPDF->getPathFileCreated();
                $shippingFileForLink = $shippingPDF->getPathForLink();              
                
                
                $defaultAttachments[] = array(
                    'fileName'=>$shippingFileName,
                    'filePathForLink'=>$shippingFileForLink,
                    'filePath'=>$shippingFilePath
                );
                
                $fileManagement = new FileManagement();
                $attachments = $fileManagement->getAttachemntsFilesByOperationAndPrefix('shipping', $id,$defaultAttachments);                
                $attachments = '<strong>'.$this->_getTranslation("Archivos adjuntos").'</strong><br/>'.$attachments;
                
                break;
            
            case 'waste':               
                $subject = $this->_getTranslation("Merma # {$id}");
                $messageMail = $this->_getTranslation("Revisar archivos adjuntos porfavor.");
             
                $to = '';
                
                $defaultAttachments = array();                
                $wastePDF = new WastePDF($id,true);
                $wasteFileName= $wastePDF->getFileName();
                $wasteFilePath = $wastePDF->getPathFileCreated();
                $wasteFileForLink = $wastePDF->getPathForLink();                       
                
                $defaultAttachments[] = array(
                    'fileName'=>$wasteFileName,
                    'filePathForLink'=>$wasteFileForLink,
                    'filePath'=>$wasteFilePath
                );
                
                $fileManagement = new FileManagement();
                $attachments = $fileManagement->getAttachemntsFilesByOperationAndPrefix('waste', $id,$defaultAttachments);                
                $attachments = '<strong>'.$this->_getTranslation("Archivos adjuntos").'</strong><br/>'.$attachments;
                
                break;
            
            case 'manufacturing':               
                $subject = $this->_getTranslation("Reempaque # {$id}");
                $messageMail = $this->_getTranslation("Revisar archivos adjuntos porfavor.");
             
                $to = '';
                
                $defaultAttachments = array();                
                $manufacturingPDF = new ManufacturingPDF($id,true);
                $manufacturingFileName= $manufacturingPDF->getFileName();
                $manufacturingFilePath = $manufacturingPDF->getPathFileCreated();
                $manufacturingFileForLink = $manufacturingPDF->getPathForLink();                       
                
                $defaultAttachments[] = array(
                    'fileName'=>$manufacturingFileName,
                    'filePathForLink'=>$manufacturingFileForLink,
                    'filePath'=>$manufacturingFilePath
                );
                
                $fileManagement = new FileManagement();
                $attachments = $fileManagement->getAttachemntsFilesByOperationAndPrefix('manufacturing', $id,$defaultAttachments);                
                $attachments = '<strong>'.$this->_getTranslation("Archivos adjuntos").'</strong><br/>'.$attachments;
                
                break;
        }      
        
        return array(
            'response'=>true,
            'to'=>$to,
            'subject'=>$subject,
            'messageMail'=>$messageMail,
            'attachments'=>$attachments
        );
        
    }
    
    public function emailing($options){
        $attachments = null;
        if(isset($options['attachmentFiles'])){
            $attachments = array();
            foreach($options['attachmentFiles'] as $row){
                $attachments[] = $row['value'];
            }
        }
        
        $data = array();
        foreach($options['options'] as $row){
            $data[$row['name']] = $row['value'];
        }
       
        try{
            $emailer = new Emailer();
            $emailer->sendEmail(array(
                'to'=>$data['to'],
                'cc'=>$data['cc'],
                'subject'=>$data['subject'],
                'message'=>$data['message'],
                'attachment'=>$attachments
            ));
            
            $this->flashmessenger->addMessage(array('success'=>'Correo electronico enviado correctamente.'));
            return array(
                'response'=>true,
                'msg'=>$this->flashmessenger->getMessageString()
            );
        } catch (Exception $ex) {
            $this->flashmessenger->addMsg(array('danger'=>'Algo salio mal al tratar de enviar el correo electronico. Intenta nuevamente'));
            return array(
                'response'=>null,
                'msg'=>$this->flashmessenger->getMessageString()
            );
        }        
    }  
    /*EN EMAILING BOL*/
}