Files
bio-concept-pharma/web/modules/ekstatinvoicelist/ekstatinvoicelist.php
2019-11-17 19:14:07 +01:00

369 lines
14 KiB
PHP

<?php
/**
* NOTICE OF LICENSE
*
* This source file is subject to a commercial license from SARL EKYSS
* Use, copy, modification or distribution of this source file without written
* license agreement from the SARL EKYSS is strictly forbidden.
* In order to obtain a license, please contact us: prestashop@ekyss.com
* ...........................................................................
* INFORMATION SUR LA LICENCE D'UTILISATION
*
* L'utilisation de ce fichier source est soumise a une licence commerciale
* concedee par la societe EKYSS
* Toute utilisation, reproduction, modification ou distribution du present
* fichier source sans contrat de licence ecrit de la part de la SARL EKYSS est
* expressement interdite.
* Pour obtenir une licence, veuillez contacter la SARL SMC a l'adresse: prestashop@ekyss.com
* ...........................................................................
*
* @author Guillaume D.
* @copyright Copyright (c) 2012-2014 S.A.R.L EKYSS (http://www.ekyss.fr)
* @license Commercial license
* Support by mail : prestashop@ekyss.com
*/
if (!defined('_PS_VERSION_')) {
exit;
}
class EkStatInvoiceList extends Module
{
public function __construct()
{
$this->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 .= '<h2>'.$this->displayName.'</h2>';
$this->postProcess();
if ($this->do_export) {
$this->getExportCsv();
}
$this->_html .= '
<div style="float:left">
'.$this->getLeftCol().'
</div>
<div style="float:left; margin-left:20px;">
'.$this->getCenterCol().'
</div>
<div class="clear"></div>
';
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 .= '<pre>';
// $html .= ''.print_r($this->_errors, true);
// $html .= 'GET = '.print_r($_GET, true);
// $html .= 'POST = '.print_r($_POST, true);
// $html .= '</pre>';
// $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 .= '<table class="table" border="0" cellspacing="0" cellpadding="0">';
$html .= '<tr>';
foreach ($head_tbl as $head_cell) {
$html .= '<th>'.$head_cell.'</th>';
}
$html .= '</tr>';
// affiche result
foreach ($fact_tbl as $lgn_tbl) {
$html .= '<tr style="background-color: #FFFFFF;">';
foreach ($lgn_tbl as $cel_fact) {
$html .= '<td>';
$html .= $cel_fact;
$html .= '</td>';
}
$html .= '</tr>';
}
$html .= '</table>';
return $html;
}
public function getLeftCol()
{
return $this->getCalendar();
}
public function getCalendar()
{
return '<div id="calendar">
'.$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'),
)).'
<div class="clear space">&nbsp;</div></div>';
}
public function displayCalendar($translations)
{
$employee = new Employee($this->context->employee->id);
return '
<fieldset style="width: 200px; font-size:13px;"><legend><img src="../img/admin/date.png" /> '.$translations['Calendar'].'</legend>
<div>
<form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post">
<p>
'.(isset($translations['From']) ? $translations['From'] : 'From:').'
<input type="date" name="datepickerFrom" id="datepickerFrom" value="'.Tools::getValue('datepickerFrom', $employee->stats_date_from).'">
</p>
<p>
'.(isset($translations['To']) ? $translations['To'] : 'To:').'
<input type="date" name="datepickerTo" id="datepickerTo" value="'.Tools::getValue('datepickerTo', $employee->stats_date_to).'">
</p>
<input type="submit" name="submitDatePicker" class="button" value="'.(isset($translations['Save']) ? $translations['Save'] : 'Save').'" />
<input type="submit" name="submitExportCsv" class="button"
value="'.(isset($translations['Export']) ? $translations['Export'] : 'Exporter CSV').'" />
<br/>
<input type="submit" name="submitDateDay" class="button" value="'.$translations['Day'].'">
<input type="submit" name="submitDateMonth" class="button" value="'.$translations['Month'].'">
<input type="submit" name="submitDateYear" class="button" value="'.$translations['Year'].'"><br />
<input type="submit" name="submitDateDayPrev" class="button" value="'.$translations['Day'].'-1" style="margin-top:2px">
<input type="submit" name="submitDateMonthPrev" class="button" value="'.$translations['Month'].'-1" style="margin-top:2px">
<input type="submit" name="submitDateYearPrev" class="button" value="'.$translations['Year'].'-1" style="margin-top:2px">
</form>
</div>
</fieldset>';
}
}