Exemple #1
0
 public static function execute($request = null)
 {
     if (empty($request['label'])) {
         return false;
     }
     App::uses('LilReport', 'Lil.Lib');
     $report = new LilReport(Configure::read('LilCrm.label.' . $request['label']));
     $report->helpers(array('Lil.Lil', 'Lil.LilDate', 'Lil.LilFloat', 'Html'));
     $report->template('LilCrm.' . $request['label']);
     $AdremasContact = ClassRegistry::init('LilCrm.AdremasContact');
     $params = array('conditions' => array('AdremasContact.adrema_id' => empty($request['adrema']) ? null : $request['adrema']));
     $addresses = $AdremasContact->find('all', $params);
     $i = 0;
     foreach ($addresses as $data) {
         $data['i'] = $i;
         $data += $request;
         $report->reset();
         $report->set(compact('data'));
         $report->render(null);
         $i++;
     }
     $report->output('labels', 'I');
     return true;
 }
 /**
  * admin_report method
  *
  * @access public
  * @return void
  */
 public function admin_report()
 {
     //$this->view = 'admin_report_analytics';
     if (!empty($this->request->query['kind'])) {
         $filter = $this->request->query;
         if ($filter['kind'] == 'span') {
             unset($filter['month']);
             $filter['start'] = $this->LilDate->delocalize($filter['start']);
             $filter['end'] = $this->LilDate->delocalize($filter['end']);
         } else {
             unset($filter['start']);
             unset($filter['end']);
             $filter['month'] = $filter['year'] . '-' . str_pad($filter['month'], 2, '0', STR_PAD_LEFT);
         }
         $data = $this->Vat->invoiceAnalytics($filter);
         $vats = $this->Vat->findList();
         $this->response->type('pdf');
         App::uses('LilReport', 'Lil.Lib');
         $report = new LilReport('vats_analytics');
         $report->template('LilInvoices.vats_analytics')->helpers(array('Lil.Lil', 'Lil.LilDate', 'Lil.LilFloat', 'Html'));
         $report->set(compact('data', 'filter', 'vats'))->render('vats');
     }
 }
 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;
 }
 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;
 }
 /**
  * admin_report_unpaid_invoices method
  *
  * @return void
  */
 public function admin_report_unpaid_invoices()
 {
     // need to filter by counters that produce payments
     $Counter = ClassRegistry::init('LilInvoices.InvoicesCounter');
     $counters = $Counter->find('list', array('conditions' => array('InvoicesCounter.expense' => true, 'InvoicesCounter.active' => true)));
     if (!empty($this->request->query)) {
         $filter = $this->request->query;
         App::uses('LilDateEngine', 'Lil.Lib');
         $LilDate = LilDateEngine::getInstance();
         $Invoice = ClassRegistry::init('LilInvoices.Invoice');
         $Invoice->virtualFields['payments_amount'] = 'SELECT COALESCE(SUM(Payment.amount), 0) ' . 'FROM payments_expenses AS PaymentsExpense ' . 'INNER JOIN payments AS Payment ON Payment.id = PaymentsExpense.payment_id ' . 'WHERE PaymentsExpense.expense_id = Expense.id';
         if (!empty($filter['counter'])) {
             $filter['counter'] = array_intersect_key((array) $filter['counter'], array_keys($counters));
         }
         if (!empty($filter['start'])) {
             $filter['start'] = $LilDate->delocalize($filter['start']);
         }
         if (!empty($filter['end'])) {
             $filter['end'] = $LilDate->delocalize($filter['end']);
         }
         $data = $Invoice->find('all', Set::merge($Invoice->filter($filter), array('conditions' => array_merge(!empty($filter['counter']) ? array() : array('Invoice.counter_id' => array_keys($counters)), empty($filter['overdue']) ? array() : array('Invoice.dat_expire <=' => $LilDate->toSql(null, false))), 'contain' => array('Client', 'Expense', 'InvoicesCounter'), 'order' => 'InvoicesCounter.title, Invoice.counter', 'group' => 'Invoice.id HAVING (ABS(ABS(Invoice.total) - ABS(Invoice__payments_amount)) > 0.001)')));
         $this->response->type('pdf');
         App::uses('LilReport', 'Lil.Lib');
         $report = new LilReport('expenses_unpaid_invoices');
         $report->template('LilExpenses.unpaid_invoices')->helpers(array('Lil.Lil', 'Lil.LilDate', 'Lil.LilFloat', 'Html'));
         $report->set(compact('data', 'filter', 'counters'))->render('unpaid_invoices');
     }
     $this->set(compact('counters'));
 }