name = 'ekstatinvoicelist'; $this->tab = 'export'; $this->version = '1.0.6'; $this->author = 'Ekyss'; $this->need_instance = 0; // $this->ps_versions_compliancy = array('min' => '1.4', 'max' => '1.6'); $this->dependencies = array(); $this->module_key = '3adb3ce6d69b19bdc340546b0b1041ac'; parent::__construct(); $this->page = basename(__FILE__, '.php'); $this->displayName = $this->l('Stat : Invoice List'); $this->description = $this->l('List of invoices over a period.'); $this->confirmUninstall = $this->l('Are you sure you want to uninstall the module ').$this->displayName; } public function install() { if (!parent::install()) { return false; } return true; } public function uninstall() { if (!parent::uninstall()) { return false; } return true; } protected function postProcess() { $this->do_export = false; $is_submit = false; $from = $this->_date_from; $to = $this->_date_to; if (Tools::isSubmit('submitDatePicker')) { if (!Validate::isDate($from = Tools::getValue('datepickerFrom')) || !Validate::isDate($to = Tools::getValue('datepickerTo'))) { $this->_errors[] = Tools::displayError('Date specified is invalid'); } $is_submit = true; } if (Tools::isSubmit('submitDateDay')) { $from = date('Y-m-d'); $to = date('Y-m-d'); $is_submit = true; } if (Tools::isSubmit('submitDateDayPrev')) { $yesterday = time() - 60 * 60 * 24; $from = date('Y-m-d', $yesterday); $to = date('Y-m-d', $yesterday); $is_submit = true; } if (Tools::isSubmit('submitDateMonth')) { $from = date('Y-m-01'); $to = date('Y-m-t'); $is_submit = true; } if (Tools::isSubmit('submitDateMonthPrev')) { $m = (date('m') == 1 ? 12 : date('m') - 1); $y = ($m == 12 ? date('Y') - 1 : date('Y')); $from = $y.'-'.$m.'-01'; $to = $y.'-'.$m.date('-t', mktime(12, 0, 0, $m, 15, $y)); $is_submit = true; } if (Tools::isSubmit('submitDateYear')) { $from = date('Y-01-01'); $to = date('Y-12-31'); $is_submit = true; } if (Tools::isSubmit('submitDateYearPrev')) { $from = (date('Y') - 1).date('-01-01'); $to = (date('Y') - 1).date('-12-31'); $is_submit = true; } if (Tools::isSubmit('submitExportCsv')) { if (!Validate::isDate($from = Tools::getValue('datepickerFrom')) || !Validate::isDate($to = Tools::getValue('datepickerTo'))) { $this->_errors[] = Tools::displayError('Date specified is invalid'); } $this->do_export = true; $is_submit = true; } if (!$is_submit) { if (!Validate::isDate($from = Tools::getValue('datepickerFrom')) || !Validate::isDate($to = Tools::getValue('datepickerTo'))) { $this->_errors[] = Tools::displayError('Date specified is invalid'); } } $this->_date_from = $from; $this->_date_to = $to; if (isset($from) && isset($to) && !count($this->_errors)) { $employee = new Employee($this->context->employee->id); $employee->stats_date_from = $from; $employee->stats_date_to = $to; $employee->update(); } // if (sizeof($this->_errors)) // AdminTab::displayErrors(); } public function getHeaderArray() { $head_tbl = array(); $head_tbl[] = $this->l('N° Cmd.'); $head_tbl[] = $this->l('Statut'); $head_tbl[] = $this->l('N° Facture'); $head_tbl[] = $this->l('Date Facture'); $head_tbl[] = $this->l('Client'); $head_tbl[] = $this->l('N° TVA'); $head_tbl[] = $this->l('Livraison'); $head_tbl[] = $this->l('Mode de paiement'); $head_tbl[] = $this->l('Produits Total HT'); $head_tbl[] = $this->l('Produits Total TVA'); $head_tbl[] = $this->l('Produits Total TTC'); $head_tbl[] = $this->l('Produits Réductions TTC'); $head_tbl[] = $this->l('Livraison Total HT'); $head_tbl[] = $this->l('Livraison Taux TVA'); $head_tbl[] = $this->l('Livraison Total TVA'); $head_tbl[] = $this->l('Livraison Total TTC'); $head_tbl[] = $this->l('Total HT'); $head_tbl[] = $this->l('Total TVA'); $head_tbl[] = $this->l('Total TTC'); $head_tbl[] = $this->l('Total Payé'); return $head_tbl; } public function formatMoney($val_money) { $result = number_format($val_money, 2, ',', ''); // $result = str_replace('.', ',', $val_money); return $result; } public function getDataArray() { // $currency = new Currency((int)(Configuration::get('PS_CURRENCY_DEFAULT'))); $fact_tbl = array(); // $order_ids = Order::getOrdersIdByDate($this->_date_from, $this->_date_to, null, 'invoice'); $order_ids = Order::getOrdersIdInvoiceByDate($this->_date_from, $this->_date_to, null, 'invoice'); foreach ($order_ids as $order_id) { $order = new Order($order_id); $customer = new Customer($order->id_customer); $address = new Address($order->id_address_invoice); $carrier = new Carrier($order->id_carrier); $state = $order->getCurrentStateFull($this->context->language->id); // $trans_ht = round(((100 - $order->carrier_tax_rate) * $order->total_shipping)/100, 2); $trans_ht = round(100 * $order->total_shipping / (100 + $order->carrier_tax_rate), 2); $total_ht = $order->total_products + $trans_ht; // $total_ht = 0; $total_ttc = $order->total_products_wt - $order->total_discounts + $order->total_shipping; // $total_ttc = 0; $total_tva = $total_ttc - $total_ht; $lgn_tbl = array(); // fb::info($order_id , 'order_id'); // fb::info($order->getCurrentStateFull($this->context->language->id) , 'state'); $lgn_tbl[] = $order->id; $lgn_tbl[] = $state['name']; $lgn_tbl[] = $order->invoice_number; $lgn_tbl[] = Tools::displayDate($order->invoice_date, (int)$this->context->language->id, true); $lgn_tbl[] = $customer->firstname.' '.$customer->lastname; $lgn_tbl[] = $address->vat_number; $lgn_tbl[] = $carrier->name; $lgn_tbl[] = $order->payment; $lgn_tbl[] = $this->formatMoney($order->total_products); $lgn_tbl[] = $this->formatMoney($order->total_products_wt - $order->total_products); $lgn_tbl[] = $this->formatMoney($order->total_products_wt); $lgn_tbl[] = $this->formatMoney(-$order->total_discounts); $lgn_tbl[] = $this->formatMoney($trans_ht); $lgn_tbl[] = number_format($order->carrier_tax_rate, 2, ',', ' ').' %'; $lgn_tbl[] = $this->formatMoney($order->total_shipping - $trans_ht); $lgn_tbl[] = $this->formatMoney($order->total_shipping); $lgn_tbl[] = $this->formatMoney($total_ht); $lgn_tbl[] = $this->formatMoney($total_tva); $lgn_tbl[] = $this->formatMoney($total_ttc); $lgn_tbl[] = $this->formatMoney($order->total_paid_real); // ajout de la ligne au tbl $fact_tbl[] = $lgn_tbl; } return $fact_tbl; } public function getContent() { $employee = new Employee($this->context->employee->id); $this->_date_from = $employee->stats_date_from; $this->_date_to = $employee->stats_date_to; $this->_html = ''; $this->_html .= '

'.$this->displayName.'

'; $this->postProcess(); if ($this->do_export) { $this->getExportCsv(); } $this->_html .= '
'.$this->getLeftCol().'
'.$this->getCenterCol().'
'; return $this->_html; } public function getExportCsv() { $head_tbl = $this->getHeaderArray(); // recup data $fact_tbl = $this->getDataArray(); $str_csv = ''; $firstline = 'Liste des factures '; $firstline .= 'du;'.Tools::displayDate($this->_date_from, (int)$this->context->language->id).';'; $firstline .= 'au;'.Tools::displayDate($this->_date_to, (int)$this->context->language->id).';'; $str_csv .= $firstline."\r\n"; $firstline = implode(';', $head_tbl); $str_csv .= $firstline."\r\n"; foreach ($fact_tbl as $lgn_tbl) { $str_csv .= implode(';', $lgn_tbl)."\r\n"; } $str_csv = mb_convert_encoding($str_csv, 'ISO-8859-1', 'UTF-8'); $file = fopen(dirname(__FILE__).'/export.csv', 'w'); fwrite($file, $str_csv); fclose($file); Tools::redirect(_PS_BASE_URL_.__PS_BASE_URI__.'modules/'.$this->name.'/export.csv'); } public function getCenterCol() { $head_tbl = $this->getHeaderArray(); // recup data $fact_tbl = $this->getDataArray(); $html = ''; // $html .= '
';
        // $html .= ''.print_r($this->_errors, true);
        // $html .= 'GET = '.print_r($_GET, true);
        // $html .= 'POST = '.print_r($_POST, true);
        // $html .= '
'; // $html .= 'xx'._PS_BASE_URL_.__PS_BASE_URI__.'xx'; $html .= 'Liste des factures du '.Tools::displayDate($this->_date_from, (int)$this->context->language->id).' au '.Tools::displayDate($this->_date_to, (int)$this->context->language->id).'.'; $html .= ''; $html .= ''; foreach ($head_tbl as $head_cell) { $html .= ''; } $html .= ''; // affiche result foreach ($fact_tbl as $lgn_tbl) { $html .= ''; foreach ($lgn_tbl as $cel_fact) { $html .= ''; } $html .= ''; } $html .= '
'.$head_cell.'
'; $html .= $cel_fact; $html .= '
'; return $html; } public function getLeftCol() { return $this->getCalendar(); } public function getCalendar() { return '
'.$this->displayCalendar(array( 'Calendar' => $this->l('Calendar'), 'Day' => $this->l('Day'), 'Month' => $this->l('Month'), 'Year' => $this->l('Year'), 'From' => $this->l('From:'), 'To' => $this->l('To:'), 'Save' => $this->l('Filtrer'), 'Export' => $this->l('Export CSV'), )).'
 
'; } public function displayCalendar($translations) { $employee = new Employee($this->context->employee->id); return '
'.$translations['Calendar'].'

'.(isset($translations['From']) ? $translations['From'] : 'From:').'

'.(isset($translations['To']) ? $translations['To'] : 'To:').'



'; } }