table = 'stats_logistic'; $this->lang = false; $this->date_begin = date('Y-m-d'); $this->date_end = date('Y-m-d'); $this->date_stock = date('Y-m-d'); $this->date_export_begin = date('Y-m-d'); $this->date_export_end = date('Y-m-d'); if (!empty($_GET['date_begin'])) { $this->date_begin = $_GET['date_begin']; } if (!empty($_GET['date_end'])){ $this->date_end = $_GET['date_end']; } if (!empty($_GET['date_stock'])){ $this->date_stock = $_GET['date_stock']; } parent::__construct(); } public function postProcess() { global $currentIndex, $cookie; if (Tools::getValue('submitExport')) { $this->date_export_begin = Tools::getValue('date_export_begin', $this->date_export_begin); $this->date_export_end = Tools::getValue('date_export_end', $this->date_export_end); $sale_ids = $this->getSales(); if ($sale_ids) { foreach ($sale_ids as $sale) { $ps = new Sale((int)$sale['id_sale']); $product_ids = $ps->getProducts(); $products = $this->getSaleDetails($product_ids); $reasons= array(); if ($products) { $result[(int)$sale['id_sale']] = array( 'ca_ttc' => 0, 'ca_refund' => 0, 'percent_ca_refund' => 0, 'quantity_sold' => 0, 'quantity_refund' => 0, 'percent_refund' => 0 ); foreach($products as $row) { $price = (isset($row['product_quantity_discount']) && $row['product_quantity_discount']!=0)?(float)$row['product_quantity_discount']:(float)$row['product_price']; $result[(int)$sale['id_sale']]['ca_ttc'] += (((int)$row['product_quantity'] - (int)$row['product_quantity_reinjected'])*$price); $result[(int)$sale['id_sale']]['quantity_sold'] += ((int)$row['product_quantity'] - (int)$row['product_quantity_reinjected']); if ($row['product_quantity_refunded']>0) { $result[(int)$sale['id_sale']]['quantity_refund'] += (int)$row['product_quantity_refunded']; $result[(int)$sale['id_sale']]['ca_refund'] += ((int)$row['product_quantity_refunded']*$price); if (!isset($row['id_reason'])) { $row['id_reason'] = 99; } if(!in_array((int)$row['id_reason'], $reasons)) { $reasons[] = (int) $row['id_reason']; } if(!isset($result[(int)$sale['id_sale']][(int)$row['id_reason']])) { $result[(int)$sale['id_sale']][(int)$row['id_reason']] = array( 'ca_refund' => 0, 'percent_ca_refund' => 0, 'quantity_refund' => 0, 'percent_refund' => 0, ); } $result[(int)$sale['id_sale']][(int)$row['id_reason']]['ca_refund'] += ((int)$row['product_quantity_refunded']*$price); $result[(int)$sale['id_sale']][(int)$row['id_reason']]['quantity_refund'] += (int)$row['product_quantity_refunded']; } } $result[(int)$sale['id_sale']]['percent_ca_refund'] = ($result[(int)$sale['id_sale']]['ca_refund'] / $result[(int)$sale['id_sale']]['ca_ttc'])*100; $result[(int)$sale['id_sale']]['percent_refund'] = ($result[(int)$sale['id_sale']]['quantity_refund'] / $result[(int)$sale['id_sale']]['quantity_sold'])*100; foreach ($reasons as $key => $reason_id) { if (isset($result[(int)$sale['id_sale']][$reason_id])) { $result[(int)$sale['id_sale']][$reason_id]['percent_ca_refund'] += ($result[(int)$sale['id_sale']][$reason_id]['ca_refund'] / $result[(int)$sale['id_sale']]['ca_ttc'])*100; $result[(int)$sale['id_sale']][$reason_id]['percent_refund'] += ($result[(int)$sale['id_sale']][$reason_id]['quantity_refund'] / $result[(int)$sale['id_sale']]['quantity_sold'])*100; } } } } echo "
";var_dump($result);echo "
";die(); if ($result) { $this->exportCSV($result); } } else { die('pas de vente trouvée'); } } } public function display() { global $currentIndex, $cookie; $date = Configuration::get('MAJ_STAT_LOGISTIC'); echo ' '; echo '
' .'' .'' .'' .'' .'' .'' .'' .'' .'' .'' .' Dernière mise à jour : '.$date.'' .'
' ; $stats = $this->getStats(); $poles = array(); for ($i=0; $i < 6; $i++) { $poles[] = array( 'recap' => array( 'quantity' => 0, 'nb_package' => 0 ), 'employee' => array(), ); } $poles[0]['name'] = 'Pôle 1 : Petites marques'; $poles[1]['name'] = 'Pôle 2 : Marques moyenne'; $poles[2]['name'] = 'Pôle 3 : Grosses marques'; $poles[3]['name'] = 'Pôle 4 : Postes complementaires'; $poles[4]['name'] = 'Pôle 5 : Multi'; $poles[5]['name'] = 'Pôle 6 : Autre'; foreach ($stats as $key => $stat) { if (!isset($stat['id_employee'])) { continue; } switch ($stat['id_employee']) { case '52': $poles[0]['recap']['quantity'] += $stat['quantity']; $poles[0]['recap']['nb_package'] += $stat['nb_package']; $poles[0]['employee'][] = $stat; break; case '22': case '24': $poles[1]['recap']['quantity'] += $stat['quantity']; $poles[1]['recap']['nb_package'] += $stat['nb_package']; $poles[1]['employee'][] = $stat; break; case '25': case '44': $poles[2]['recap']['quantity'] += $stat['quantity']; $poles[2]['recap']['nb_package'] += $stat['nb_package']; $poles[2]['employee'][] = $stat; break; case '47': case '48': $poles[3]['recap']['quantity'] += $stat['quantity']; $poles[3]['recap']['nb_package'] += $stat['nb_package']; $poles[3]['employee'][] = $stat; break; case '35': case '36': $poles[4]['recap']['quantity'] += $stat['quantity']; $poles[4]['recap']['nb_package'] += $stat['nb_package']; $poles[4]['employee'][] = $stat; break; case '28': default: $poles[5]['recap']['quantity'] += $stat['quantity']; $poles[5]['recap']['nb_package'] += $stat['nb_package']; $poles[5]['employee'][] = $stat; break; } $total_quantity += $stat['quantity']; } $total_colis = $this->getTotalColis(); $total_quantity = 0; echo '

Stats de la logistique

'; foreach ($poles as $k => $pole) { echo ''; foreach ($pole['employee'] as $key => $stat) { echo ''; } } echo '
Id Employee Prénom Nom Email Quantité Nombre de colis % total
'.$pole['name'].' '.$pole['recap']['quantity'].' '.$pole['recap']['nb_package'].' '. number_format(($pole['recap']['nb_package']* 100 / $total_colis), 2) .' %
'.$stat['id_employee'].' '.$stat['firstname'].' '.$stat['lastname'].' '.$stat['email'].' '.$stat['quantity'].' '.$stat['nb_package'].' '. number_format(($stat['nb_package']* 100 / $total_colis), 2) .' %
Total : '.$total_quantity.' '.$total_colis.' 100 %
'; $reports = array( '25' => array( 'name' => 'Nb commandes > 25j', 'payment' => $this->getReport(25,2), 'partial' => $this->getReport(25,17) ), '21' => array( 'name' => 'Nb commandes > 21j', 'payment' => $this->getReport(21,2), 'partial' => $this->getReport(21,17) ), '18' => array( 'name' => 'Nb commandes > 18j', 'payment' => $this->getReport(18,2), 'partial' => $this->getReport(18,17) ), '2' => array( 'name' => 'Nb commandes > 2j', 'payment' => $this->getReport(2,2), 'partial' => $this->getReport(2,17) ) ); echo '

Report Stock

' .'' .'' .'' .'' .'' .'' .'' .'' .'' .'
' ; echo ''; foreach ($reports as $k => $report) { echo ' '; } echo '
Paiement accepté Commande partiellement expédiée
'.$report['name'].' '.date('Y-m-d', strtotime($this->date_stock. ' - '.$k.' days')).' '.$report['payment'].' '.$report['partial'].'
'; /*echo '

Export Remboursemments

' .'' .'' .'' .'' .'' .'' .'' .'' .'' .'' .'' .'
' ;*/ } public function getStats() { return Db::getInstance()->executeS(' SELECT e.`id_employee`, e.`email`, e.`lastname`, e.`firstname`, SUM(s.`nb_product`) AS `nb_product`, SUM(s.`quantity`) AS `quantity`, SUM(s.`nb_package`) AS `nb_package` FROM `'._DB_PREFIX_.'stats_logistic` s LEFT JOIN `'._DB_PREFIX_.'employee` e USING(`id_employee`) WHERE s.`date` BETWEEN "'.pSQL($this->date_begin).'" AND "'.pSQL($this->date_end).'" GROUP BY s.`id_employee` '); } public function getTotalColis() { return Db::getInstance()->getValue(' SELECT SUM(s.`nb_package`) AS `nb_package` FROM `'._DB_PREFIX_.'stats_logistic` s WHERE s.`date` BETWEEN "'.pSQL($this->date_begin).'" AND "'.pSQL($this->date_end).'" '); } public function getReport($day = 25, $state = 2) { return Db::getInstance()->getValue(' SELECT COUNT(o.`id_order`) FROM `'._DB_PREFIX_.'orders` o LEFT JOIN `'._DB_PREFIX_.'order_history` oh ON (oh.`id_order` = o.`id_order`) WHERE o.`date_add` < DATE_SUB("'.pSQL($this->date_stock).'",INTERVAL '.((int)$day+1).' DAY) AND oh.`id_order_state` = '.(int)$state.' AND oh.`id_order_history` = (SELECT MAX(`id_order_history`) FROM `'._DB_PREFIX_.'order_history` moh WHERE moh.`id_order` = o.`id_order` GROUP BY moh.`id_order`) -- AND -- ( -- SELECT `id_order_state` -- FROM '._DB_PREFIX_.'order_history oh -- WHERE o.id_order = oh.id_order -- ORDER BY id_order_history DESC -- LIMIT 1 -- ) = '.(int)$state.' '); } public function getSaleDetails($product_ids) { return Db::getInstance()->ExecuteS(' SELECT od.*, rr.`id_reason` FROM `'._DB_PREFIX_.'order_detail` od LEFT JOIN `'._DB_PREFIX_.'order_slip` os ON (os.`id_order` = od.`id_order`) LEFT JOIN `'._DB_PREFIX_.'refundreason` rr ON (rr.`id_order_slip` = os.`id_order_slip`) WHERE od.`product_id` IN ('.implode(',', $product_ids).') -- AND od.`product_quantity_refunded`>0 ORDER BY rr.`id_reason` '); return Db::getInstance()->ExecuteS(' SELECT od.* FROM `'._DB_PREFIX_.'order_detail` od WHERE od.`product_id` IN ('.implode(',', $product_ids).') '); } public function getSales() { return Db::getInstance()->ExecuteS(' SELECT `id_sale` FROM `'._DB_PREFIX_.'privatesale` WHERE `date_start` BETWEEN "'.pSQL($this->date_export_begin).'" AND "'.pSQL($this->date_export_end).'" '); } public function getProducts($id_sale) { return Db::getInstance()->ExecuteS(' SELECT `id_product` FROM `'._DB_PREFIX_.'product_ps_cache` WHERE `id_sale`='.(int)$id_sale.' '); } public function exportCSV () { $fp = fopen("php://output", 'w+'); $delim = ';'; $row_1 = array("","","","","","","",""); $data=array(); foreach ($row_definition as $col) { $data[] = $col; } fputcsv ($fp,$data,$delim); foreach ($items as $item) { $data = array(); foreach ($row_definition as $key => $col) { $data[] = (isset($item->$key) ? $item->$key : ''); } fputcsv ($fp,array_map('utf8_decode',array_values($data)),$delim); } fclose($fp); } }