public static function execute($type, $filter, $inline)
 {
     $Invoice = ClassRegistry::init('LilInvoices.Invoice');
     App::uses('LilDateEngine', 'Lil.Lib');
     $LilDate = LilDateEngine::getInstance();
     $default_params = array('contain' => array('InvoicesItem', 'InvoicesCounter', 'InvoicesTax', 'Client' => array('PrimaryAddress', 'PrimaryAccount', 'PrimaryEmail'), 'InvoicesAttachment'));
     $ret = true;
     $params = array_merge_recursive($default_params, $Invoice->filter($filter));
     if ($invoices = $Invoice->find('all', $params)) {
         switch ($type) {
             case 'pdf':
                 App::uses('LilReport', 'Lil.Lib');
                 $report = new LilReport('Invoice');
                 $report->helpers(array('Lil.Lil', 'Lil.LilDate', 'Lil.LilFloat', 'Html'));
                 App::uses('Sanitize', 'Utility');
                 // define filename
                 if (sizeof($invoices) == 1) {
                     $filename = Sanitize::paranoid(__d('lil_invoices', 'invoice') . '_' . $invoices[0]['Invoice']['no'], array('-', '_'));
                 } else {
                     $filename = Sanitize::paranoid(__d('lil_invoices', 'invoices') . '_' . $LilDate->toSql(null, true), array('_'));
                 }
                 $Vat = ClassRegistry::init('LilInvoices.Vat');
                 $vats = $Vat->findList();
                 $i = 1;
                 foreach ($invoices as $data) {
                     $report->template('LilInvoices.' . $data['InvoicesCounter']['layout'], 'LilInvoices.lil_invoices_pdf');
                     $report->reset();
                     $report->set(compact('data', 'vats'));
                     $report->render(null);
                     if ($i < sizeof($invoices)) {
                         $report->addPage();
                     }
                     $i++;
                 }
                 $report->output($filename, $inline ? 'I' : 'D');
                 break;
             case 'envelope':
             case 'eslog':
             case 'sepaxml':
                 App::uses('LilCurrentUser', 'Lil.Lib');
                 $currentUser = LilCurrentUser::getInstance();
                 if (($company_id = $currentUser->get('company_id')) && ($Contact = ClassRegistry::init('LilCrm.Contact'))) {
                     if (!($Company = $Contact->find('first', array('conditions' => array('Contact.id' => $company_id), 'contain' => array('PrimaryAccount', 'PrimaryAddress'))))) {
                         $errors[] = __d('lil_invoices', 'Cannot fetch your employer!');
                     }
                 } else {
                     $errors[] = __d('lil_invoices', 'Please provide your employer!');
                 }
                 $Vat = ClassRegistry::init('LilInvoices.Vat');
                 $vats = $Vat->findList();
                 $errors = array();
                 if (empty($Company['PrimaryAccount'])) {
                     $errors[] = __d('lil_invoices', 'Please provide your default account!');
                 }
                 if (empty($Company['PrimaryAddress'])) {
                     $errors[] = __d('lil_invoices', 'Please provide your address!');
                 }
                 if (empty($Company['Contact']['tax_no'])) {
                     $errors[] = __d('lil_invoices', 'Please provide your tax no!');
                 }
                 foreach ($invoices as $data) {
                     if ($type == 'sepaxml' && empty($data['Client']['PrimaryAccount'])) {
                         $errors[] = __d('lil_invoices', 'Please provide client\'s "%s" default account!', $data['Client']['title']);
                     }
                     if (empty($data['Client']['PrimaryAddress'])) {
                         $errors[] = __d('lil_invoices', 'Please provide client\'s "%s" default address!', $data['Client']['title']);
                     }
                     if ($data['Client']['kind'] == 'C' && empty($data['Client']['tax_no'])) {
                         $errors[] = __d('lil_invoices', 'Please provide client\'s "%s" tax no!', $data['Client']['title']);
                     }
                     if ($type == 'sepaxml' && (empty($data['Invoice']['pmt_type']) || empty($data['Invoice']['pmt_module']) || empty($data['Invoice']['pmt_ref']))) {
                         $errors[] = __d('lil_invoices', 'Please provide payment details for invoice "%1$s - %2$s"', $data['Invoice']['no'], $data['Invoice']['title']);
                     }
                 }
                 if (empty($errors)) {
                     $View = new View(null);
                     $View->viewPath = 'Invoices';
                     $View->plugin = 'LilInvoices';
                     $View->helpers = array('Session', 'Html');
                     $View->set('invoices', $invoices);
                     $View->set('Company', $Company);
                     $View->set('vats', $vats);
                     switch ($type) {
                         case 'eslog':
                             $template = 'admin_export_eslog';
                             break;
                         case 'envelope':
                             $template = 'admin_export_envelope';
                             break;
                         default:
                             $template = 'admin_export_sepaxml';
                     }
                     echo $View->render($template, 'xml');
                 } else {
                     $ret = __d('lil_invoices', 'EXPORT ERROR(s):') . '<br />' . implode('<br />', $errors);
                 }
                 break;
         }
     } else {
         return __d('lil_invoices', 'Export error: No invoices found.');
     }
     return $ret;
 }
Example #2
0
 public function send($data)
 {
     $this->create(null);
     $this->set($data);
     $Invoice = ClassRegistry::init('LilInvoices.Invoice');
     $Vat = ClassRegistry::init('LilInvoices.Vat');
     $vats = $Vat->findList();
     if ($this->validates() && ($invoices = $Invoice->find('all', array('conditions' => array('Invoice.id' => $data['InvoiceEmail']['invoices']), 'contain' => array('InvoicesItem', 'InvoicesCounter', 'Client' => 'PrimaryAddress', 'InvoicesAttachment', 'InvoicesTax' => 'Vat'))))) {
         // generate PDF
         App::uses('LilReport', 'Lil.Lib');
         App::uses('Sanitize', 'Utility');
         $report = new LilReport();
         $report->helpers(array('Lil.Lil', 'Lil.LilDate', 'Lil.LilFloat', 'Html'));
         $report->template('', 'LilInvoices.lil_invoices_pdf');
         $report->set(array('vats' => $vats));
         $page = 0;
         foreach ($invoices as $i) {
             if ($page > 0) {
                 $report->addPage();
             }
             $report->set(array('data' => $i));
             $report->render(null, array('template' => $i['InvoicesCounter']['layout']));
             $page++;
         }
         $filename = 'invoices_' . strftime('%Y%m%d%H%M%S');
         $report->output(TMP . $filename, 'F');
         // init and send email
         App::uses('CakeEmail', 'Network/Email');
         $email = new CakeEmail();
         $email->template('LilInvoices.send_invoice');
         $email->from(Configure::read('Lil.from.email'), Configure::read('Lil.from.name'));
         $email->returnPath(Configure::read('Lil.from.email'));
         $email->subject($data['InvoiceEmail']['subject']);
         $email->to($data['InvoiceEmail']['to']);
         if (!empty($data['InvoiceEmail']['cc']) && $data['InvoiceEmail']['cc'] != $data['InvoiceEmail']['to']) {
             $email->cc($data['InvoiceEmail']['cc']);
         }
         Cache::write('LilInvoices.emailCacheSubject', $data['InvoiceEmail']['subject'], 'Lil');
         Cache::write('LilInvoices.emailCacheBody', $data['InvoiceEmail']['body'], 'Lil');
         Cache::write('LilInvoices.emailCacheCC', $data['InvoiceEmail']['cc'], 'Lil');
         if (isset($data['InvoiceEmail']['cc_me'])) {
             Cache::write('LilInvoices.emailCacheCCMe', $data['InvoiceEmail']['cc_me'], 'Lil');
         }
         Cache::write('LilInvoices.emailCacheTo', $data['InvoiceEmail']['to'], 'Lil');
         $email->viewVars(array('body' => $data['InvoiceEmail']['body'], 'invoices' => $invoices));
         $ats = array($filename . '.pdf' => array('file' => TMP . $filename . '.pdf', 'mimetype' => 'application/pdf'));
         App::uses('Sanitize', 'Utility');
         foreach ($invoices as $i) {
             foreach ($i['InvoicesAttachment'] as $atch) {
                 $atch_name = 'at_' . $i['Invoice']['no'] . '_' . $atch['original'];
                 $atch_file = APP . 'uploads' . DS . 'Invoice' . DS . $atch['filename'];
                 if (file_exists($atch_file)) {
                     $ats[Sanitize::paranoid($atch_name, array('-', '_', '.'))] = array('file' => $atch_file, 'mimetype' => $atch['mimetype']);
                 }
             }
         }
         $email->attachments($ats);
         $result = $email->send();
         unlink(TMP . $filename . '.pdf');
         return $result;
     }
     return false;
 }