From f63b3996330dcf71629544abe73c364226fc404d Mon Sep 17 00:00:00 2001 From: Michael RICOIS Date: Mon, 18 Dec 2017 17:14:31 +0100 Subject: [PATCH] Script form server --- .gitignore | 3 + _cron_vouchers.php | 42 + bbb.php | 35 + clean_image.php | 48 + cron_criteo.php | 97 ++ cron_customer_segmentation.php | 21 + cron_emarsys_unsubscribe.php | 37 + cron_export_cohort.php | 263 ++++ cron_export_monthly_recap.php | 841 +++++++++++++ cron_export_sales_daily_new_new_3.php | 804 ++++++++++++ cron_export_sales_monthly.php | 775 ++++++++++++ cron_export_sales_monthly_2.php | 918 ++++++++++++++ cron_export_sales_monthly_new.php | 889 +++++++++++++ cron_loyalty.php | 37 + cron_loyalty_reminder.php | 2 + cron_queue_bdc.php | 1310 ++++++++++++++++++++ cron_sale_cache.php | 28 + cron_sales.php | 129 ++ cron_sales_accepted.php | 122 ++ cron_weekly_stats.php | 203 +++ export_cart.php | 56 + export_laposte.php | 298 +++++ export_multi_monthly.php | 783 ++++++++++++ export_pdf.php | 65 + export_philea_laposte.php | 301 +++++ export_webdav.php | 312 +++++ extracts/monthly_compta/2017-12-01_new.csv | 1 + extracts/monthly_compta/2017-12-18_new.csv | 1 + fichier_test.php | 240 ++++ find_token.php | 14 + reassociation_shipping_number.php | 21 + regenerate_images.php | 83 ++ regeneration_export_laposte.php | 279 +++++ regeneration_monthly.php | 33 + regeneration_order_state_current.php | 31 + regeneration_sale_cache.php | 27 + regeneration_sales_stats.php | 60 + rename_csv_import.php | 49 + sandbox.php | 10 + 39 files changed, 9268 insertions(+) create mode 100644 .gitignore create mode 100644 _cron_vouchers.php create mode 100644 bbb.php create mode 100755 clean_image.php create mode 100755 cron_criteo.php create mode 100755 cron_customer_segmentation.php create mode 100644 cron_emarsys_unsubscribe.php create mode 100644 cron_export_cohort.php create mode 100644 cron_export_monthly_recap.php create mode 100644 cron_export_sales_daily_new_new_3.php create mode 100644 cron_export_sales_monthly.php create mode 100644 cron_export_sales_monthly_2.php create mode 100644 cron_export_sales_monthly_new.php create mode 100644 cron_loyalty.php create mode 100644 cron_loyalty_reminder.php create mode 100644 cron_queue_bdc.php create mode 100644 cron_sale_cache.php create mode 100644 cron_sales.php create mode 100755 cron_sales_accepted.php create mode 100644 cron_weekly_stats.php create mode 100644 export_cart.php create mode 100755 export_laposte.php create mode 100755 export_multi_monthly.php create mode 100644 export_pdf.php create mode 100644 export_philea_laposte.php create mode 100644 export_webdav.php create mode 100644 extracts/monthly_compta/2017-12-01_new.csv create mode 100644 extracts/monthly_compta/2017-12-18_new.csv create mode 100644 fichier_test.php create mode 100644 find_token.php create mode 100644 reassociation_shipping_number.php create mode 100644 regenerate_images.php create mode 100644 regeneration_export_laposte.php create mode 100755 regeneration_monthly.php create mode 100644 regeneration_order_state_current.php create mode 100644 regeneration_sale_cache.php create mode 100644 regeneration_sales_stats.php create mode 100644 rename_csv_import.php create mode 100644 sandbox.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ca21e90 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/.settings/ +/.buildpath +/.project diff --git a/_cron_vouchers.php b/_cron_vouchers.php new file mode 100644 index 0000000..18a13e0 --- /dev/null +++ b/_cron_vouchers.php @@ -0,0 +1,42 @@ +ExecuteS(' + SELECT `id_category` + FROM `'._DB_PREFIX_.'category` + WHERE `active` = 1 +') as $c) { + $categories[] = $c['id_category']; +} + +foreach(Db::getInstance()->ExecuteS(' + SELECT `id_discount` + FROM `'._DB_PREFIX_.'discount` + WHERE `date_from` <= NOW() + AND `date_to` > NOW() + AND `quantity` > 0 + AND `active` = 1 +') as $voucher) { + $voucher_categories = array(); + foreach(Db::getInstance()->ExecuteS(' + SELECT `id_category` + FROM `'._DB_PREFIX_.'discount_category` + WHERE `id_discount` = '.(int) $voucher['id_discount'].' + ') as $vc) { + $voucher_categories[] = $vc['id_category']; + } + + $diff = array_diff($categories, $voucher_categories); + $query = array(); + foreach($diff as $d) { + $query[] = '('.$d.', '.(int) $voucher['id_discount'].')'; + } + Db::getInstance()->Execute(' + INSERT INTO `'._DB_PREFIX_.'discount_category` + VALUES '.implode(', ', $query).' + '); +} diff --git a/bbb.php b/bbb.php new file mode 100644 index 0000000..a57d574 --- /dev/null +++ b/bbb.php @@ -0,0 +1,35 @@ +id_lang){ + @$cookie->id_lang = 2; + } + + $sql = ' + SELECT o.`id_order`, (od.`product_quantity` - ( od.`product_quantity_refunded` + od.`product_quantity_return`)) AS `quantity` + FROM `ps_order_detail` od + LEFT JOIN `ps_orders` o ON (o.`id_order` = od.`id_order`) + WHERE o.`valid` = 1 + AND od.`product_id` IN ( + SELECT cache.`id_product` + FROM `ps_product_ps_cache` cache + WHERE cache.`id_sale` = 3986 + ) + AND o.id_order <> 238884 + '; + $details = Db::getInstance()->executeS($sql); + + // :215569 => Commande test pour >>>> thibault@antadis.com + // test sendMail + ProductVouchers::sendMail(1, 215569, 'TEST'); + + foreach ($details as $key => $detail) { + ProductVouchers::associateCodeToOrder(1, $detail['quantity'], $detail['id_order']); + } diff --git a/clean_image.php b/clean_image.php new file mode 100755 index 0000000..43e9b81 --- /dev/null +++ b/clean_image.php @@ -0,0 +1,48 @@ +ExecuteS(' + SELECT `id_product` + FROM `'._DB_PREFIX_.'product_ps_cache` + WHERE `id_sale` > 2500 + AND `id_sale` < 3500 + -- AND `id_product` < 100000 +') as $row) { + $id_product[] = (int) $row['id_product']; +} + +echo count($id_product)."\n\n"; + +foreach(Db::getInstance()->ExecuteS(' + SELECT * + FROM `'._DB_PREFIX_.'image` + WHERE `id_product` IN ('.implode(', ', $id_product).') + -- AND `id_product` NOT IN (23606, 3522, 23607, 24483, 11752, 35837, 85644, 124519, 269752) +') as $row) { + $folder = Image::getImgFolderStatic((int) $row['id_image']); + + unlink(dirname(__FILE__).'/www/img/p/'.$folder.(int) $row['id_image'].'-thickbox.jpg'); + unlink(dirname(__FILE__).'/www/img/p/'.$folder.(int) $row['id_image'].'-small.jpg'); + unlink(dirname(__FILE__).'/www/img/p/'.$folder.(int) $row['id_image'].'-medium.jpg'); + unlink(dirname(__FILE__).'/www/img/p/'.$folder.(int) $row['id_image'].'-large.jpg'); + unlink(dirname(__FILE__).'/www/img/p/'.$folder.(int) $row['id_image'].'-home.jpg'); + + + // unlink(dirname(__FILE__).'/www/img/p/'.(int) $row['id_product'].'-'.(int) $row['id_image'].'-thickbox.jpg'); + // unlink(dirname(__FILE__).'/www/img/p/'.(int) $row['id_product'].'-'.(int) $row['id_image'].'-small.jpg'); + // unlink(dirname(__FILE__).'/www/img/p/'.(int) $row['id_product'].'-'.(int) $row['id_image'].'-medium.jpg'); + // unlink(dirname(__FILE__).'/www/img/p/'.(int) $row['id_product'].'-'.(int) $row['id_image'].'-large.jpg'); + // unlink(dirname(__FILE__).'/www/img/p/'.(int) $row['id_product'].'-'.(int) $row['id_image'].'-home.jpg'); + + if((int) $row['id_product'] % 200 == 0) { + echo $row['id_product']."\n"; + } +} diff --git a/cron_criteo.php b/cron_criteo.php new file mode 100755 index 0000000..5953288 --- /dev/null +++ b/cron_criteo.php @@ -0,0 +1,97 @@ +addChildCDATA($name, $value); + } else { + return $this->addChild($name, $value); + } + } + + public function addChildCDATA($name, $value=NULL) { + $new_child = $this->addChild($name); + + if ($new_child !== NULL) { + $node = dom_import_simplexml($new_child); + $no = $node->ownerDocument; + $node->appendChild($no->createCDATASection($value)); + } + + return $new_child; + } +} + +setlocale('LC_TIME', 'fr_FR.utf8'); +$id_lang = 2; + +// --> flux catalogue products +$xml = new XML(''); +foreach(Db::getInstance()->ExecuteS(' + SELECT DISTINCT p.`id_product`, p.`price`, p.`quantity`, pl.`name`, pl.`link_rewrite`, cl.`name` as `category_name`, (sp.`reduction`) as `discount`, psl.`description` + FROM `'._DB_PREFIX_.'product` p + LEFT JOIN `'._DB_PREFIX_.'product_ps_cache` psc ON p.`id_product` = psc.`id_product` + LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON pl.`id_product` = p.`id_product` + LEFT JOIN `'._DB_PREFIX_.'specific_price` sp ON p.`id_product` = sp.`id_product` + LEFT JOIN `'._DB_PREFIX_.'privatesale` ps ON psc.`id_sale` = ps.`id_sale` + LEFT JOIN `'._DB_PREFIX_.'privatesale_site_version` psv ON psv.`id_sale` = ps.`id_sale` + LEFT JOIN `'._DB_PREFIX_.'privatesale_lang` psl ON psl.`id_sale` = ps.`id_sale` + LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON ps.`id_category` = cl.`id_category` + WHERE ps.`date_start` < NOW() + AND ps.`date_end` > NOW() + AND ps.`enabled` = 1 + AND psv.`version` = "fr" + AND cl.`id_lang` = '.(int) $id_lang.' + AND pl.`id_lang` = '.(int) $id_lang.' + AND psl.`id_lang` = '.(int) $id_lang.' + AND ps.`id_sale` IS NOT NULL + ORDER BY ps.`id_sale` +') as $row) { + $s = $xml->addChildAuto('product'); + $s->addAttribute("id",$row['id_product']); + + $s->addChildAuto('name', htmlentities($row['name'])); + $s->addChildAuto('producturl', 'http://www.bebeboutik.com/?utm_source=retargeting&utm_medium=cpc&utm_campaign=criteo&lp=criteo'); + + /* Image Cover */ + $cover = Product::getCover((int)$row['id_product']); + $link = new Link(); + $smallimage = $link->getImageLink($row['link_rewrite'], (int)$cover['id_image'], 'small'); + $bigimage = $link->getImageLink($row['link_rewrite'], (int)$cover['id_image'], 'thickbox'); + $s->addChildAuto('smallimage', $smallimage); + $s->addChildAuto('bigimage', $bigimage); + + $s->addChildAuto('description', htmlentities($row['description'])); + + /* Prices */ + $tax_rate = Tax::getProductTaxRate((int)$row['id_product']); + $retailprice = (float)$row['price'] * (1 + ($tax_rate / 100)); + $retailprice = Tools::ps_round($retailprice, 2); + $s->addChildAuto('retailprice', $retailprice); + + /* Price with reduction */ + $price_notax = (float)$row['price'] * (1 - (float)$row['discount']); + $price = $price_notax * (1 + ($tax_rate / 100)); + $price = Tools::ps_round($price, 2); + $s->addChildAuto('price', $price); + + $instock = 0; + if((int)$row['quantity']>0) { + $instock = 1; + } + $s->addChildAuto('instock', $instock); + $s->addChildAuto('category1', htmlentities($row['category_name'])); + + $discount = Tools::ps_round(((float)$row['discount'] * 100), 0); + $s->addChildAuto('discount', $discount); + +} +/*file_put_contents(dirname(__FILE__).'/www/criteo_sales.fr.xml', $xml->asXML());*/ +file_put_contents(dirname(__FILE__).'/www/modules/criteo_bbb/criteo_sales.fr.xml', $xml->asXML()); \ No newline at end of file diff --git a/cron_customer_segmentation.php b/cron_customer_segmentation.php new file mode 100755 index 0000000..97d606f --- /dev/null +++ b/cron_customer_segmentation.php @@ -0,0 +1,21 @@ +getRow(' + SELECT `id_customer` + FROM `'._DB_PREFIX_.'customer` + WHERE `email` = "'.pSQL($line[2]).'" + ')) { + if(strtolower($line[3]) == 'faux') { + $active = 0; + } else { + $active = (int) $line[1]; + } + $db->ExecuteS(' + UPDATE `'._DB_PREFIX_.'customer` + SET `newsletter` = '.(int) $active.', + `newsletter_date_add` = NOW() + WHERE `email` = "'.pSQL($line[2]).'" + LIMIT 1 + '); + } + } + } + fclose($f); + rename($filename, str_replace('/emarsys/', '/emarsys_done/', $filename)); +} \ No newline at end of file diff --git a/cron_export_cohort.php b/cron_export_cohort.php new file mode 100644 index 0000000..0587c5a --- /dev/null +++ b/cron_export_cohort.php @@ -0,0 +1,263 @@ + 0? ' + '.$i: ''); +} + +fputcsv($file, $headers, ';', '"'); + +for($y=2012, $ynow=(int) date('Y'); $y <= $ynow; $y++) { + for($m=1; $m < 13; $m++) { + if(date('Y-m') == $y.'-'.sprintf('%02d', $m)) { + break; + } + + $customers = array(); + foreach(Db::getInstance()->ExecuteS(' + SELECT `id_customer` + FROM `'._DB_PREFIX_.'customer` + WHERE `date_add` BETWEEN "'.$y.'-'.sprintf('%02d', $m).'-01 00:00:00" AND "'.($m + 1 > 12? $y + 1: $y).'-'.($m + 1 > 12? '01': sprintf('%02d', $m + 1)).'-01 00:00:00" + ') as $row) { + $customers[] = (int) $row['id_customer']; + } + + echo $y.'-'.sprintf('%02d', $m); + + $results = array($y.'-'.sprintf('%02d', $m), count($customers)); + + $date = new DateTime($y.'-'.sprintf('%02d', $m).'-01'); + for($i = 1; $i < $monthdiff; $i++) { + $d1 = clone $date; + $d2 = clone $date; + $d1->add(new DateInterval('P'.($i - 1).'M')); + $d2->add(new DateInterval('P'.$i.'M')); + + $total = array(); + + foreach(Db::getInstance()->ExecuteS(' + SELECT DISTINCT `id_customer` + FROM `'._DB_PREFIX_.'orders` + WHERE `id_customer` IN ('.implode(', ', $customers).') + AND (`invoice_date` BETWEEN "'.$d1->format('Y-m-d').' 00:00:00" AND "'.$d2->format('Y-m-d').' 00:00:00") + ') as $row) { + $total[] = (int) $row['id_customer']; + } + + $results[] = count($total); + + $customers = array_diff($customers, $total); + + echo ' '.$i; + } + + fputcsv($file, $results, ';', '"'); + + echo "\n"; + + $monthdiff--; + } +} + +fclose($file); + + +/** End of cohort 1 **/ + +$monthdiff = ((int) date('Y') - 2012 + 1) * 12 - (12 - (int) date('m')); + + +$file2 = fopen(dirname(__FILE__).'/extracts/cohort/'.date('Y-m-d').'_2.csv', 'w'); +$file3 = fopen(dirname(__FILE__).'/extracts/cohort/'.date('Y-m-d').'_3.csv', 'w'); + +$headers = array( + 'date', + 'first_orders', +); + +for($i=0; $i < $monthdiff - 1; $i++) { + $headers[] = 'month'.($i > 0? ' + '.$i: ''); +} + +fputcsv($file2, $headers, ';', '"'); +fputcsv($file3, $headers, ';', '"'); + +for($y=2012, $ynow=(int) date('Y'); $y <= $ynow; $y++) { + for($m=1; $m < 13; $m++) { + if(date('Y-m') == $y.'-'.sprintf('%02d', $m)) { + break; + } + + $first_orders = array(); + + foreach(Db::getInstance()->ExecuteS(' + SELECT DISTINCT o.`id_customer` + FROM `'._DB_PREFIX_.'orders` o + INNER JOIN ( + SELECT `id_customer`, MIN(`invoice_date`) AS `mindate` + FROM `'._DB_PREFIX_.'orders` + GROUP BY `id_customer` + ) o2 + ON o.`id_customer` = o2.`id_customer` + WHERE o2.`mindate` BETWEEN "'.$y.'-'.sprintf('%02d', $m).'-01 00:00:00" AND "'.($m + 1 > 12? $y + 1: $y).'-'.($m + 1 > 12? '01': sprintf('%02d', $m + 1)).'-01 00:00:00" + ') as $row) { + $first_orders[] = (int) $row['id_customer']; + } + + echo $y.'-'.sprintf('%02d', $m); + + $results = array($y.'-'.sprintf('%02d', $m), count($first_orders)); + $results2 = array($y.'-'.sprintf('%02d', $m), count($first_orders)); + + $date = new DateTime($y.'-'.sprintf('%02d', $m).'-01'); + for($i = 1; $i < $monthdiff; $i++) { + $d1 = clone $date; + $d2 = clone $date; + $d1->add(new DateInterval('P'.($i - 1).'M')); + $d2->add(new DateInterval('P'.$i.'M')); + + if(count($first_orders) > 0) { + $total = Db::getInstance()->getValue(' + SELECT COUNT(`id_order`) + FROM `'._DB_PREFIX_.'orders` + WHERE `id_customer` IN ('.implode(', ', $first_orders).') + AND (`invoice_date` BETWEEN "'.$d1->format('Y-m-d').' 00:00:00" AND "'.$d2->format('Y-m-d').' 00:00:00") + '); + + $results[] = $total; + + $total2 = array(); + + foreach(Db::getInstance()->ExecuteS(' + SELECT DISTINCT `id_customer` + FROM `'._DB_PREFIX_.'orders` + WHERE `id_customer` IN ('.implode(', ', $first_orders).') + AND (`invoice_date` BETWEEN "'.$d1->format('Y-m-d').' 00:00:00" AND "'.$d2->format('Y-m-d').' 00:00:00") + ') as $row) { + $total2[] = (int) $row['id_customer']; + } + + $results2[] = count($total2); + } else { + $results[] = 0; + $results2[] = 0; + } + + echo ' '.$i; + } + + fputcsv($file2, $results, ';', '"'); + fputcsv($file3, $results2, ';', '"'); + + echo "\n"; + + $monthdiff--; + } +} + +fclose($file2); +fclose($file3); + + +/** End of cohort 1 **/ + +$monthdiff = ((int) date('Y') - 2012 + 1) * 12 - (12 - (int) date('m')); + + +$file4 = fopen(dirname(__FILE__).'/extracts/cohort/'.date('Y-m-d').'_4.csv', 'w'); + +$headers = array( + 'date', + 'first_orders', +); + +for($i=0; $i < $monthdiff - 1; $i++) { + $headers[] = 'month'.($i > 0? ' + '.$i: ''); +} + +fputcsv($file4, $headers, ';', '"'); + +$double_orders_customers = array(); +foreach(Db::getInstance()->ExecuteS(' + SELECT `id_customer` + FROM `'._DB_PREFIX_.'orders` + GROUP BY `id_customer` + HAVING COUNT(`id_order`) > 1 +') as $row) { + $double_orders_customers[] = (int) $row['id_customer']; +} + +for($y=2012, $ynow=(int) date('Y'); $y <= $ynow; $y++) { + for($m=1; $m < 13; $m++) { + if(date('Y-m') == $y.'-'.sprintf('%02d', $m)) { + break; + } + + $first_orders = array(); + + foreach(Db::getInstance()->ExecuteS(' + SELECT DISTINCT o.`id_customer` + FROM `'._DB_PREFIX_.'orders` o + INNER JOIN ( + SELECT `id_customer`, MIN(`invoice_date`) AS `mindate` + FROM `'._DB_PREFIX_.'orders` + GROUP BY `id_customer` + ) o2 + ON o.`id_customer` = o2.`id_customer` + WHERE o2.`mindate` BETWEEN "'.$y.'-'.sprintf('%02d', $m).'-01 00:00:00" AND "'.($m + 1 > 12? $y + 1: $y).'-'.($m + 1 > 12? '01': sprintf('%02d', $m + 1)).'-01 00:00:00" + ') as $row) { + if(in_array((int) $row['id_customer'], $double_orders_customers)) { + $first_orders[] = (int) $row['id_customer']; + } + } + + echo $y.'-'.sprintf('%02d', $m); + + $results = array($y.'-'.sprintf('%02d', $m), count($first_orders)); + + $date = new DateTime($y.'-'.sprintf('%02d', $m).'-01'); + for($i = 1; $i < $monthdiff; $i++) { + $d1 = clone $date; + $d2 = clone $date; + $d1->add(new DateInterval('P'.($i - 1).'M')); + $d2->add(new DateInterval('P'.$i.'M')); + + if(count($first_orders) > 0) { + $total = Db::getInstance()->getValue(' + SELECT COUNT(`id_order`) + FROM `'._DB_PREFIX_.'orders` + WHERE `id_customer` IN ('.implode(', ', $first_orders).') + AND (`invoice_date` BETWEEN "'.$d1->format('Y-m-d').' 00:00:00" AND "'.$d2->format('Y-m-d').' 00:00:00") + '); + + $results[] = $total; + } else { + $results[] = 0; + } + + echo ' '.$i; + } + + fputcsv($file4, $results, ';', '"'); + + echo "\n"; + + $monthdiff--; + } +} + +fclose($file4); diff --git a/cron_export_monthly_recap.php b/cron_export_monthly_recap.php new file mode 100644 index 0000000..43f7fc9 --- /dev/null +++ b/cron_export_monthly_recap.php @@ -0,0 +1,841 @@ +ExecuteS(' + SELECT o.`id_order` + FROM `'._DB_PREFIX_.'orders` o, + `'._DB_PREFIX_.'order_detail` d, + `'._DB_PREFIX_.'customer` c + WHERE + (o.valid = 1 + OR ( + o.valid = 0 + AND ( + (SELECT h.id_order_state FROM `'._DB_PREFIX_.'order_history` h WHERE h.id_order = o.id_order ORDER BY h.date_add DESC LIMIT 1) = 6 + OR (SELECT h.id_order_state FROM `'._DB_PREFIX_.'order_history` h WHERE h.id_order = o.id_order ORDER BY h.date_add DESC LIMIT 1) = 7 + OR (SELECT h.id_order_state FROM `'._DB_PREFIX_.'order_history` h WHERE h.id_order = o.id_order ORDER BY h.date_add DESC LIMIT 1) = 11 + ) + )) + AND c.id_customer = o.id_customer + AND d.id_order = o.id_order + AND o.date_add >= DATE_FORMAT("'.$now.'" - INTERVAL 2 MONTH, \'%Y-%m-01 00:00:00\') + AND o.date_add <= DATE_FORMAT(LAST_DAY("'.$now.'" - INTERVAL 2 MONTH), \'%Y-%m-%d 23:59:59\') + GROUP BY o.id_order +'); +foreach($orders as $o) { + $order = Db::getInstance()->ExecuteS(' + SELECT c.*, o.* + FROM `'._DB_PREFIX_.'orders` o, + `'._DB_PREFIX_.'customer` c + WHERE + o.id_order = '.$o['id_order'].' + AND c.`id_customer` = o.`id_customer` + '); + $order = $order[0]; + + $address = Db::getInstance()->ExecuteS(' + SELECT a.*, c.`id_zone` + FROM `'._DB_PREFIX_.'address` a, `'._DB_PREFIX_.'country` c + WHERE a.`id_address` = '.$order['id_address_delivery'].' + AND a.`id_country` = c.`id_country` + '); + $address = $address[0]; + + $order_details = Db::getInstance()->ExecuteS(' + SELECT * + FROM `'._DB_PREFIX_.'order_detail` d + WHERE d.id_order = '.$o['id_order'] + ); + + $total_products_wo_taxes_210 = array(); + $total_taxes_210 = array(); + $total_products_wo_taxes_200 = array(); + $total_taxes_200 = array(); + $total_products_wo_taxes_196 = array(); + $total_taxes_196 = array(); + $total_products_wo_taxes_100 = array(); + $total_taxes_100 = array(); + $total_products_wo_taxes_55 = array(); + $total_taxes_55 = array(); + $total_products_wo_taxes_21 = array(); + $total_taxes_21 = array(); + $total_products_wo_taxes_60= array(); + $total_taxes_60= array(); + $total_products_wo_taxes_40 = array(); + $total_taxes_40 = array(); + $total_products_wo_taxes_export = array(); + + // calcul prix d'achat de la commande + $wholesale_price = Db::getInstance()->ExecuteS(' + SELECT `wholesale_price`, p.`id_product` + FROM `'._DB_PREFIX_.'product` p + LEFT JOIN `'._DB_PREFIX_.'order_detail` o ON o.`product_id` = p.`id_product` + WHERE o.`id_order` = '. (int) $order['id_order'] + ); + $prix_achat = array(); + foreach ($wholesale_price as $key => $price) { + $prix_achat[$price['id_product']] = $price['wholesale_price']; + } + $total_achat = 0; + + + $ldetails = array(); + foreach($order_details as $detail) { + + $total_achat += $detail['product_quantity'] * $prix_achat[$detail['product_id']]; + + $ldetails[$detail['id_order_detail']] = $detail; + + if($address['id_country'] != 19) { + if($detail['tax_rate'] == '20.000') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_200[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_200[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.2); + } else { + $total_products_wo_taxes_200[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_200[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.2); + } + } elseif($detail['tax_rate'] == '21.000') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_210[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_210[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.21); + } else { + $total_products_wo_taxes_210[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_210[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.21); + } + } elseif($detail['tax_rate'] == '19.600') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_196[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_196[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.196); + } else { + $total_products_wo_taxes_196[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_196[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.196); + } + } elseif($detail['tax_rate'] == '10.000') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_100[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_100[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.100); + } else { + $total_products_wo_taxes_100[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_100[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.100); + } + } elseif($detail['tax_rate'] == '5.500') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_55[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_55[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.055); + } else { + $total_products_wo_taxes_55[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_55[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.055); + } + } elseif($detail['tax_rate'] == '2.100') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_21[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_21[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.021); + } else { + $total_products_wo_taxes_21[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_21[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.021); + } + } elseif($detail['tax_rate'] == '6.000') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_60[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_60[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.060); + } else { + $total_products_wo_taxes_60[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_60[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.060); + } + } elseif($detail['tax_rate'] == '4.000') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_40[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_40[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.040); + } else { + $total_products_wo_taxes_40[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_40[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.040); + } + } else { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + } else { + $total_products_wo_taxes_export[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + } + } + } else { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + } else { + $total_products_wo_taxes_export[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + } + } + } + + $total_tax_210 = 0.0; + foreach($total_products_wo_taxes_210 as $value) { + $total_tax_210 += $value; + } + $_total_taxes_210 = 0.0; + foreach($total_taxes_210 as $value) { + $_total_taxes_210 += $value; + } + + $total_tax_200 = 0.0; + foreach($total_products_wo_taxes_200 as $value) { + $total_tax_200 += $value; + } + $_total_taxes_200 = 0.0; + foreach($total_taxes_200 as $value) { + $_total_taxes_200 += $value; + } + + $total_tax_196 = 0.0; + foreach($total_products_wo_taxes_196 as $value) { + $total_tax_196 += $value; + } + $_total_taxes_196 = 0.0; + foreach($total_taxes_196 as $value) { + $_total_taxes_196 += $value; + } + + $total_tax_100 = 0.0; + foreach($total_products_wo_taxes_100 as $value) { + $total_tax_100 += $value; + } + $_total_taxes_100 = 0.0; + foreach($total_taxes_100 as $value) { + $_total_taxes_100 += $value; + } + + $total_tax_55 = 0.0; + foreach($total_products_wo_taxes_55 as $value) { + $total_tax_55 += $value; + } + $_total_taxes_55 = 0.0; + foreach($total_taxes_55 as $value) { + $_total_taxes_55 += $value; + } + + $total_tax_21 = 0.0; + foreach($total_products_wo_taxes_21 as $value) { + $total_tax_21 += $value; + } + $_total_taxes_21 = 0.0; + foreach($total_taxes_21 as $value) { + $_total_taxes_21 += $value; + } + + $total_tax_60 = 0.0; + foreach($total_products_wo_taxes_60 as $value) { + $total_tax_60 += $value; + } + $_total_taxes_60 = 0.0; + foreach($total_taxes_60 as $value) { + $_total_taxes_60 += $value; + } + + $total_tax_40 = 0.0; + foreach($total_products_wo_taxes_40 as $value) { + $total_tax_40 += $value; + } + $_total_taxes_40 = 0.0; + foreach($total_taxes_40 as $value) { + $_total_taxes_40 += $value; + } + + $total_tax_export = 0.0; + foreach($total_products_wo_taxes_export as $value) { + $total_tax_export += $value; + } + + $tabs = array( + // fr + 8 => array('tax_200' => 0,'tax_55' => 0,'tax_21' => 0,'port' => 0,'discount' => 0), + // be + 3 => array('tax_210' => 0,'tax_60' => 0,'port' => 0,'discount' => 0), + // es + 6 => array('tax_210' => 0,'tax_40' => 0,'port' => 0,'discount' => 0), + // it + 10 => array('tax_200' => 0,'tax_55' => 0,'tax_21' => 0,'port' => 0,'discount' => 0), + // de + 1 => array('tax_200' => 0,'tax_55' => 0,'tax_21' => 0,'port' => 0,'discount' => 0), + // gb + 17 => array('tax_200' => 0,'tax_55' => 0,'tax_21' => 0,'port' => 0,'discount' => 0), + // lu + 12 => array('tax_200' => 0,'tax_55' => 0,'tax_21' => 0,'port' => 0,'discount' => 0), + // pt + 15 => array('tax_200' => 0,'tax_55' => 0,'tax_21' => 0,'port' => 0,'discount' => 0), + // nl + 13 => array('tax_200' => 0,'tax_55' => 0,'tax_21' => 0,'port' => 0,'discount' => 0), + ); + switch ($address['id_country']) { + case '8': + $tabs[8] = array( + 'tax_200' => $total_tax_200, + 'tax_55' => $total_tax_55, + 'tax_21' => $total_tax_21, + 'port' => (float) $order['total_shipping'] / (strtotime($order['date_add']) >= mktime(0, 0, 0, 1, 1, 2014)? 1.2: 1.196), + 'discount' => $order['total_discounts'] + ); + break; + case '3': + $tabs[3] = array( + 'tax_210' => $total_tax_210, + 'tax_60' => $total_tax_60, + 'port' => (float) $order['total_shipping'] / (strtotime($order['date_add']) >= mktime(0, 0, 0, 1, 1, 2014)? 1.2: 1.196), + 'discount' => $order['total_discounts'] + ); + break; + case '6': + $tabs[6] = array( + 'tax_210' => $total_tax_210, + 'tax_40' => $total_tax_40, + 'port' => (float) $order['total_shipping'] / (strtotime($order['date_add']) >= mktime(0, 0, 0, 1, 1, 2014)? 1.2: 1.196), + 'discount' => $order['total_discounts'] + ); + break; + case '10': + case '1': + case '12': + case '13': + case '15': + case '17': + $tabs[(int)$address['id_country']] = array( + 'tax_200' => $total_tax_200, + 'tax_55' => $total_tax_55, + 'tax_21' => $total_tax_21, + 'port' => (float) $order['total_shipping'] / (strtotime($order['date_add']) >= mktime(0, 0, 0, 1, 1, 2014)? 1.2: 1.196), + 'discount' => $order['total_discounts'] + ); + break; + default: + break; + } + + fwrite($f, implode(';', array( + ($order['payment'] == 'Paybox'?1:0), + ($order['payment'] == 'PayPal'?1:0), + 0, + + $tabs[8]['tax_200'], + $tabs[8]['tax_55'], + $tabs[8]['tax_21'], + $tabs[8]['port'], + $tabs[8]['discount'], + + $tabs[3]['tax_210'], + $tabs[3]['tax_60'], + $tabs[3]['port'], + $tabs[3]['discount'], + + $tabs[6]['tax_210'], + $tabs[6]['tax_40'], + $tabs[6]['port'], + $tabs[6]['discount'], + + $tabs[10]['tax_200'], + $tabs[10]['tax_55'], + $tabs[10]['tax_21'], + $tabs[10]['port'], + $tabs[10]['discount'], + + $tabs[1]['tax_200'], + $tabs[1]['tax_55'], + $tabs[1]['tax_21'], + $tabs[1]['port'], + $tabs[1]['discount'], + + $tabs[17]['tax_200'], + $tabs[17]['tax_55'], + $tabs[17]['tax_21'], + $tabs[17]['port'], + $tabs[17]['discount'], + + $tabs[12]['tax_200'], + $tabs[12]['tax_55'], + $tabs[12]['tax_21'], + $tabs[12]['port'], + $tabs[12]['discount'], + + $tabs[15]['tax_200'], + $tabs[15]['tax_55'], + $tabs[15]['tax_21'], + $tabs[15]['port'], + $tabs[15]['discount'], + + $tabs[13]['tax_200'], + $tabs[13]['tax_55'], + $tabs[13]['tax_21'], + $tabs[13]['port'], + $tabs[13]['discount'], + ))."\n"); +} + +// REFUND +$slips = Db::getInstance()->ExecuteS(' + SELECT * + FROM `'._DB_PREFIX_.'order_slip` os + WHERE os.date_add >= DATE_FORMAT("'.$now.'" - INTERVAL 1 MONTH, \'%Y-%m-01 00:00:00\') + AND os.date_add <= DATE_FORMAT(LAST_DAY("'.$now.'" - INTERVAL 1 MONTH), \'%Y-%m-%d 23:59:59\') +'); +if(count($slips) > 0) { + foreach($slips as $slip) { + $order = Db::getInstance()->getRow(' + SELECT o.*, c.* + FROM `'._DB_PREFIX_.'orders` o + LEFT JOIN `'._DB_PREFIX_.'customer` c + ON o.`id_customer` = c.`id_customer` + WHERE + o.id_order = '.$slip['id_order'].' + '); + + $address = Db::getInstance()->ExecuteS(' + SELECT a.*, c.`id_zone` + FROM `'._DB_PREFIX_.'address` a, `'._DB_PREFIX_.'country` c + WHERE a.`id_address` = '.$order['id_address_delivery'].' + AND a.`id_country` = c.`id_country` + '); + $address = $address[0]; + + $order_details = Db::getInstance()->ExecuteS(' + SELECT * + FROM `'._DB_PREFIX_.'order_detail` d + WHERE d.id_order = '.(int) $slip['id_order'].' + AND d.`id_order_detail` IN ( + SELECT `id_order_detail` + FROM `'._DB_PREFIX_.'order_slip_detail` + WHERE `id_order_slip` = '.(int) $slip['id_order_slip'].' + ) + '); + + $ldetails = array(); + $total_products_wo_taxes_210 = array(); + $total_taxes_210 = array(); + $total_products_wo_taxes_200 = array(); + $total_taxes_200 = array(); + $total_products_wo_taxes_196 = array(); + $total_taxes_196 = array(); + $total_products_wo_taxes_100 = array(); + $total_taxes_100 = array(); + $total_products_wo_taxes_55 = array(); + $total_taxes_55 = array(); + $total_products_wo_taxes_21 = array(); + $total_taxes_21 = array(); + $total_products_wo_taxes_60 = array(); + $total_taxes_60 = array(); + $total_products_wo_taxes_40 = array(); + $total_taxes_40 = array(); + $total_products_wo_taxes_export = array(); + + foreach($order_details as $detail) { + $ldetails[$detail['id_order_detail']] = $detail; + + if($address['id_country'] != 19) { + if($detail['tax_rate'] == '19.600') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_196[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_196[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.196); + } else { + $total_products_wo_taxes_196[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_196[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.196); + } + } elseif($detail['tax_rate'] == '21.000') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_210[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_210[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.21); + } else { + $total_products_wo_taxes_210[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_210[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.21); + } + } elseif($detail['tax_rate'] == '10.000') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_100[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_100[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.100); + } else { + $total_products_wo_taxes_100[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_100[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.100); + } + } elseif($detail['tax_rate'] == '5.500') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_55[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_55[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.055); + } else { + $total_products_wo_taxes_55[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_55[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.055); + } + } elseif($detail['tax_rate'] == '2.100') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_21[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_21[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.021); + } else { + $total_products_wo_taxes_21[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_21[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.021); + } + } elseif($detail['tax_rate'] == '20.000') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_200[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_200[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.2); + } else { + $total_products_wo_taxes_200[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_200[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.2); + } + } elseif($detail['tax_rate'] == '6.000') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_60[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_60[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.06); + } else { + $total_products_wo_taxes_60[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_60[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.06); + } + } elseif($detail['tax_rate'] == '4.000') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_40[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_40[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.04); + } else { + $total_products_wo_taxes_40[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_40[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.04); + } + } else { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + } else { + $total_products_wo_taxes_export[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + } + } + } else { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + } else { + $total_products_wo_taxes_export[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + } + } + } + + $total_tax_210 = 0.0; + foreach($total_products_wo_taxes_210 as $value) { + $total_tax_210 += $value; + } + $_total_taxes_210 = 0.0; + foreach($total_taxes_210 as $value) { + $_total_taxes_210 += $value; + } + + $total_tax_200 = 0.0; + foreach($total_products_wo_taxes_200 as $value) { + $total_tax_200 += $value; + } + $_total_taxes_200 = 0.0; + foreach($total_taxes_200 as $value) { + $_total_taxes_200 += $value; + } + + $total_tax_196 = 0.0; + foreach($total_products_wo_taxes_196 as $value) { + $total_tax_196 += $value; + } + $_total_taxes_196 = 0.0; + foreach($total_taxes_196 as $value) { + $_total_taxes_196 += $value; + } + + $total_tax_100 = 0.0; + foreach($total_products_wo_taxes_100 as $value) { + $total_tax_100 += $value; + } + $_total_taxes_100 = 0.0; + foreach($total_taxes_100 as $value) { + $_total_taxes_100 += $value; + } + + $total_tax_55 = 0.0; + foreach($total_products_wo_taxes_55 as $value) { + $total_tax_55 += $value; + } + $_total_taxes_55 = 0.0; + foreach($total_taxes_55 as $value) { + $_total_taxes_55 += $value; + } + + $total_tax_21 = 0.0; + foreach($total_products_wo_taxes_21 as $value) { + $total_tax_21 += $value; + } + $_total_taxes_21 = 0.0; + foreach($total_taxes_21 as $value) { + $_total_taxes_21 += $value; + } + + $total_tax_60 = 0.0; + foreach($total_products_wo_taxes_60 as $value) { + $total_tax_60 += $value; + } + $_total_taxes_60 = 0.0; + foreach($total_taxes_60 as $value) { + $_total_taxes_60 += $value; + } + + $total_tax_40 = 0.0; + foreach($total_products_wo_taxes_40 as $value) { + $total_tax_40 += $value; + } + $_total_taxes_40 = 0.0; + foreach($total_taxes_40 as $value) { + $_total_taxes_40 += $value; + } + + $total_tax_export = 0.0; + foreach($total_products_wo_taxes_export as $value) { + $total_tax_export += $value; + } + + $tabs = array( + // fr + 8 => array('tax_200' => 0,'tax_55' => 0,'tax_21' => 0,'port' => 0,'discount' => 0), + // be + 3 => array('tax_210' => 0,'tax_60' => 0,'port' => 0,'discount' => 0), + // es + 6 => array('tax_210' => 0,'tax_40' => 0,'port' => 0,'discount' => 0), + // it + 10 => array('tax_200' => 0,'tax_55' => 0,'tax_21' => 0,'port' => 0,'discount' => 0), + // de + 1 => array('tax_200' => 0,'tax_55' => 0,'tax_21' => 0,'port' => 0,'discount' => 0), + // gb + 17 => array('tax_200' => 0,'tax_55' => 0,'tax_21' => 0,'port' => 0,'discount' => 0), + // lu + 12 => array('tax_200' => 0,'tax_55' => 0,'tax_21' => 0,'port' => 0,'discount' => 0), + // pt + 15 => array('tax_200' => 0,'tax_55' => 0,'tax_21' => 0,'port' => 0,'discount' => 0), + // nl + 13 => array('tax_200' => 0,'tax_55' => 0,'tax_21' => 0,'port' => 0,'discount' => 0), + ); + switch ($address['id_country']) { + case '8': + $tabs[8] = array( + 'tax_200' => $total_tax_200, + 'tax_55' => $total_tax_55, + 'tax_21' => $total_tax_21, + 'port' => ($slip['shipping_cost'] == 1? (float) $order['total_shipping'] / (strtotime($order['date_add']) >= mktime(0, 0, 0, 1, 1, 2014)? 1.2: 1.196): 0.0), + 'discount' => 0.0 + ); + break; + case '3': + $tabs[3] = array( + 'tax_210' => $total_tax_210, + 'tax_60' => $total_tax_60, + 'port' => ($slip['shipping_cost'] == 1? (float) $order['total_shipping'] / (strtotime($order['date_add']) >= mktime(0, 0, 0, 1, 1, 2014)? 1.2: 1.196): 0.0), + 'discount' => 0.0 + ); + break; + case '6': + $tabs[6] = array( + 'tax_210' => $total_tax_210, + 'tax_40' => $total_tax_40, + 'port' => ($slip['shipping_cost'] == 1? (float) $order['total_shipping'] / (strtotime($order['date_add']) >= mktime(0, 0, 0, 1, 1, 2014)? 1.2: 1.196): 0.0), + 'discount' => 0.0 + ); + break; + case '10': + case '1': + case '12': + case '13': + case '15': + case '17': + $tabs[(int)$address['id_country']] = array( + 'tax_200' => $total_tax_200, + 'tax_55' => $total_tax_55, + 'tax_21' => $total_tax_21, + 'port' => ($slip['shipping_cost'] == 1? (float) $order['total_shipping'] / (strtotime($order['date_add']) >= mktime(0, 0, 0, 1, 1, 2014)? 1.2: 1.196): 0.0), + 'discount' => 0.0 + ); + break; + default: + break; + } + + fwrite($f, implode(';', array( + ($order['payment'] == 'Paybox'?1:0), + ($order['payment'] == 'PayPal'?1:0), + 0, + + '-'.$tabs[8]['tax_200'], + '-'.$tabs[8]['tax_55'], + '-'.$tabs[8]['tax_21'], + '-'.$tabs[8]['port'], + '-'.$tabs[8]['discount'], + + '-'.$tabs[3]['tax_210'], + '-'.$tabs[3]['tax_60'], + '-'.$tabs[3]['port'], + '-'.$tabs[3]['discount'], + + '-'.$tabs[6]['tax_210'], + '-'.$tabs[6]['tax_40'], + '-'.$tabs[6]['port'], + '-'.$tabs[6]['discount'], + + '-'.$tabs[10]['tax_200'], + '-'.$tabs[10]['tax_55'], + '-'.$tabs[10]['tax_21'], + '-'.$tabs[10]['port'], + '-'.$tabs[10]['discount'], + + '-'.$tabs[1]['tax_200'], + '-'.$tabs[1]['tax_55'], + '-'.$tabs[1]['tax_21'], + '-'.$tabs[1]['port'], + '-'.$tabs[1]['discount'], + + '-'.$tabs[17]['tax_200'], + '-'.$tabs[17]['tax_55'], + '-'.$tabs[17]['tax_21'], + '-'.$tabs[17]['port'], + '-'.$tabs[17]['discount'], + + '-'.$tabs[12]['tax_200'], + '-'.$tabs[12]['tax_55'], + '-'.$tabs[12]['tax_21'], + '-'.$tabs[12]['port'], + '-'.$tabs[12]['discount'], + + '-'.$tabs[15]['tax_200'], + '-'.$tabs[15]['tax_55'], + '-'.$tabs[15]['tax_21'], + '-'.$tabs[15]['port'], + '-'.$tabs[15]['discount'], + + '-'.$tabs[13]['tax_200'], + '-'.$tabs[13]['tax_55'], + '-'.$tabs[13]['tax_21'], + '-'.$tabs[13]['port'], + '-'.$tabs[13]['discount'], + ))."\n"); + } +} + + +fclose($f); + +exit; + diff --git a/cron_export_sales_daily_new_new_3.php b/cron_export_sales_daily_new_new_3.php new file mode 100644 index 0000000..3d27354 --- /dev/null +++ b/cron_export_sales_daily_new_new_3.php @@ -0,0 +1,804 @@ +ExecuteS(' + SELECT DISTINCT `id_order` + FROM `'._DB_PREFIX_.'order_history` + WHERE (`id_order_state` = 2 + OR `id_order_state` = 13) + AND `date_add` >= DATE_FORMAT(DATE_SUB('.$date_to.', INTERVAL 1 DAY), \'%Y-%m-%d 00:00:00\') + AND `date_add` < '.$date_to.' + AND `id_order` NOT IN ( + SELECT `id_order` + FROM `'._DB_PREFIX_.'order_history` + WHERE (`id_order_state` = 2 + OR `id_order_state` = 13) + AND `date_add` < DATE_FORMAT(DATE_SUB('.$date_to.', INTERVAL 1 DAY), \'%Y-%m-%d 00:00:00\') + ) + ORDER BY `id_order` +') as $o) { + $orders_date[] = (int) $o['id_order']; +} + +if(count($orders_date) > 0) { + $query = ' + SELECT + ps.`id_sale`, + psl.`name` AS sale_title, + o.id_order AS id_order, + o.`id_cart` AS `id_cart`, + c.id_customer AS id_customer, + c.email AS email, + c.firstname AS firstname, + c.lastname AS lastname, + a.firstname AS shipping_firstname, + a.lastname AS shipping_lastname, + a.address1 AS shipping_street, + a.address2 AS shipping_street2, + a.postcode AS shipping_postcode, + a.city AS shipping_city, + a.phone AS shipping_phone, + a.phone_mobile AS shipping_phone_mobile, + REPLACE(REPLACE(REPLACE(a.other, "\n", " "), "\r", " "), " ", " ") AS other_info, + l.name AS shipping_country, + d.product_id AS id_product, + d.product_attribute_id AS id_product_attribute, + d.product_name AS order_product_name, + d.product_quantity AS product_quantity, + d.product_price AS product_price_base_wo_taxes, + d.tax_rate AS tax_rate, + p.name AS product_name, + IF(d.product_attribute_id=0,"",REPLACE(d.product_name, CONCAT(p.name, " - "), "")) AS product_combination, + ROUND((d.product_price * (1 - d.reduction_percent / 100) - d.reduction_amount), 6) AS product_price_wo_taxes, + ROUND((d.product_price * (1 - d.reduction_percent / 100) - d.reduction_amount) * (1 + d.tax_rate / 100), 6) AS product_price, + pr.wholesale_price AS wholesale_price, + pa.wholesale_price AS combination_wholesale_price, + d.product_supplier_reference AS supplier_reference, + o.total_shipping AS total_shipping, + CAST(DATE_SUB('.$date_to.', INTERVAL 1 DAY) AS DATE) AS `date`, + o.invoice_number AS invoice_number, + o.payment AS payment_type, + sl.`name` AS `order_state`, + "" AS `refund_reason`, + "" AS `refund_employee`, + IF(ps.`id_employee` = 0, "", CONCAT(e.`firstname`, " ", e.`lastname`)) AS `sale_employee`, + (SELECT pssl.`value` FROM `'._DB_PREFIX_.'privatesale_shipping_lang` pssl LEFT JOIN `'._DB_PREFIX_.'privatesale_shipping_sale` psss ON pssl.`id_shipping` = psss.`id_shipping` WHERE psss.`id_sale` = ps.`id_sale` AND pssl.`id_lang` = 2 LIMIT 1) AS `sale_shipping`, + "" AS `spay_transaction`, + "" AS `paypal_transaction`, + "" AS `picker`, + d.product_ean13 AS product_ean13, + "" AS `vip` + FROM + `'._DB_PREFIX_.'orders` o, + `'._DB_PREFIX_.'customer` c, + `'._DB_PREFIX_.'address` a, + `'._DB_PREFIX_.'privatesale_category` psc, + `'._DB_PREFIX_.'category_lang` psl, + `'._DB_PREFIX_.'country_lang` l, + `'._DB_PREFIX_.'order_state_lang` sl, + `'._DB_PREFIX_.'product` pr, + `'._DB_PREFIX_.'product_lang` p, + `'._DB_PREFIX_.'order_detail` d + LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa + ON pa.id_product_attribute = d.product_attribute_id, + `'._DB_PREFIX_.'privatesale` ps + LEFT JOIN `'._DB_PREFIX_.'employee` e + ON e.`id_employee` = ps.`id_employee` + WHERE c.id_customer = o.id_customer + AND l.id_country = a.id_country + AND l.id_lang = 2 + AND psl.`id_lang` = 2 + AND psc.`id_category` = ( + SELECT `id_category_default` + FROM `ps_product` + WHERE `id_product` = d.product_id LIMIT 1 + ) + AND ps.`id_sale` = psc.`id_sale` + AND psl.`id_category` = ps.`id_category` + AND o.id_address_delivery = a.id_address + AND d.id_order = o.id_order + AND p.id_product = d.product_id + AND pr.id_product = d.product_id + AND p.id_lang = 2 + AND sl.`id_lang` = 2 + AND sl.`id_order_state` = ( + SELECT h.`id_order_state` FROM `'._DB_PREFIX_.'order_history` h + WHERE h.`id_order` = o.`id_order` + ORDER BY h.`date_add` DESC + LIMIT 1 + ) + AND o.`id_order` IN ('.implode(', ', $orders_date).') + '; + $orders = Db::getInstance()->ExecuteS($query); + + $query = ' + SELECT + 0 AS id_sale, + "" AS sale_title, + o.id_order AS id_order, + o.`id_cart` AS `id_cart`, + c.id_customer AS id_customer, + c.email AS email, + c.firstname AS firstname, + c.lastname AS lastname, + a.firstname AS shipping_firstname, + a.lastname AS shipping_lastname, + a.address1 AS shipping_street, + a.address2 AS shipping_street2, + a.postcode AS shipping_postcode, + a.city AS shipping_city, + a.phone AS shipping_phone, + a.phone_mobile AS shipping_phone_mobile, + REPLACE(REPLACE(REPLACE(a.other, "\n", " "), "\r", " "), " ", " ") AS other_info, + l.name AS shipping_country, + od.id_discount AS id_product, + 0 AS id_product_attribute, + od.name AS order_product_name, + 1 AS product_quantity, + 0-od.value AS product_price_base_wo_taxes, + 0 AS tax_rate, + od.name AS product_name, + "" AS product_combination, + 0-od.value AS product_price_wo_taxes, + 0-od.value AS product_price, + 0 AS wholesale_price, + 0 AS combination_wholesale_price, + "DISCOUNT" AS supplier_reference, + 0 AS total_shipping, + o.date_add AS `date`, + o.invoice_number AS invoice_number, + o.payment AS payment_type, + sl.`name` AS `order_state`, + "" AS `refund_reason`, + "" AS `refund_employee`, + "" AS `sale_employee`, + "" AS `sale_shipping`, + "" AS `spay_transaction`, + "" AS `paypal_transaction`, + "" AS `picker`, + "" AS product_ean13, + "" AS `vip` + FROM + `'._DB_PREFIX_.'orders` o, + `'._DB_PREFIX_.'order_discount` od, + `'._DB_PREFIX_.'customer` c, + `'._DB_PREFIX_.'address` a, + `'._DB_PREFIX_.'order_state_lang` sl, + `'._DB_PREFIX_.'country_lang` l + WHERE c.id_customer = o.id_customer + AND o.`id_order` = od.`id_order` + AND l.id_country = a.id_country + AND l.id_lang = 2 + AND o.id_address_delivery = a.id_address + AND sl.`id_lang` = 2 + AND sl.`id_order_state` = ( + SELECT h.`id_order_state` FROM `'._DB_PREFIX_.'order_history` h + WHERE h.`id_order` = o.`id_order` + ORDER BY h.`date_add` DESC + LIMIT 1 + ) + AND o.`id_order` IN ('.implode(', ', $orders_date).') + GROUP BY c.id_customer, od.id_discount + '; + $discounts = Db::getInstance()->ExecuteS($query); + + + $query = ' + SELECT + 0 AS id_sale, + "" AS sale_title, + o.id_order AS id_order, + o.`id_cart` AS `id_cart`, + c.id_customer AS id_customer, + c.email AS email, + c.firstname AS firstname, + c.lastname AS lastname, + a.firstname AS shipping_firstname, + a.lastname AS shipping_lastname, + a.address1 AS shipping_street, + a.address2 AS shipping_street2, + a.postcode AS shipping_postcode, + a.city AS shipping_city, + a.phone AS shipping_phone, + a.phone_mobile AS shipping_phone_mobile, + REPLACE(REPLACE(REPLACE(a.other, "\n", " "), "\r", " "), " ", " ") AS other_info, + l.name AS shipping_country, + ca.id_carrier AS id_product, + 0 AS id_product_attribute, + ca.name AS order_product_name, + 1 AS product_quantity, + o.total_shipping / (1 + o.carrier_tax_rate / 100) AS product_price_base_wo_taxes, + o.carrier_tax_rate AS tax_rate, + ca.name AS product_name, + "" AS product_combination, + o.total_shipping / (1 + o.carrier_tax_rate / 100) AS product_price_wo_taxes, + o.total_shipping AS product_price, + 0 AS wholesale_price, + 0 AS combination_wholesale_price, + "SHIPPING" AS supplier_reference, + 0 AS total_shipping, + o.date_add AS `date`, + o.invoice_number AS invoice_number, + o.payment AS payment_type, + sl.`name` AS `order_state`, + "" AS `refund_reason`, + "" AS `refund_employee`, + "" AS `sale_employee`, + "" AS `sale_shipping`, + "" AS product_ean13, + "" AS `vip` + FROM + `'._DB_PREFIX_.'orders` o, + `'._DB_PREFIX_.'carrier` ca, + `'._DB_PREFIX_.'customer` c, + `'._DB_PREFIX_.'address` a, + `'._DB_PREFIX_.'order_state_lang` sl, + `'._DB_PREFIX_.'country_lang` l + WHERE c.id_customer = o.id_customer + AND l.id_country = a.id_country + AND l.id_lang = 2 + AND o.id_address_delivery = a.id_address + AND o.id_carrier = ca.id_carrier + AND sl.`id_lang` = 2 + AND o.id_address_delivery = a.id_address + AND sl.`id_lang` = 2 + AND sl.`id_order_state` = ( + SELECT h.`id_order_state` FROM `'._DB_PREFIX_.'order_history` h + WHERE h.`id_order` = o.`id_order` + ORDER BY h.`date_add` DESC + LIMIT 1 + ) + AND o.`id_order` IN ('.implode(', ', $orders_date).') + '; + $shipping = Db::getInstance()->ExecuteS($query); +} + +$query = ' + SELECT + ps.`id_sale`, + psl.`name` AS sale_title, + o.id_order AS id_order, + o.`id_cart` AS `id_cart`, + c.id_customer AS id_customer, + c.email AS email, + c.firstname AS firstname, + c.lastname AS lastname, + a.firstname AS shipping_firstname, + a.lastname AS shipping_lastname, + a.address1 AS shipping_street, + a.address2 AS shipping_street2, + a.postcode AS shipping_postcode, + a.city AS shipping_city, + a.phone AS shipping_phone, + a.phone_mobile AS shipping_phone_mobile, + REPLACE(REPLACE(REPLACE(a.other, "\n", " "), "\r", " "), " ", " ") AS other_info, + l.name AS shipping_country, + d.product_id AS id_product, + d.product_attribute_id AS id_product_attribute, + d.product_name AS order_product_name, + -osd.product_quantity AS product_quantity, + d.product_price AS product_price_base_wo_taxes, + d.tax_rate AS tax_rate, + p.name AS product_name, + IF(d.product_attribute_id=0,"",REPLACE(d.product_name, CONCAT(p.name, " - "), "")) AS product_combination, + ROUND((d.product_price * (1 - d.reduction_percent / 100) - d.reduction_amount), 6) AS product_price_wo_taxes, + ROUND((d.product_price * (1 - d.reduction_percent / 100) - d.reduction_amount) * (1 + d.tax_rate / 100), 6) AS product_price, + pr.wholesale_price AS wholesale_price, + pa.wholesale_price AS combination_wholesale_price, + d.product_supplier_reference AS supplier_reference, + o.total_shipping AS total_shipping, + oss.date_add AS `date`, + o.invoice_number AS invoice_number, + o.payment AS payment_type, + sl.`name` AS `order_state`, + IFNULL((SELECT `id_reason` FROM `'._DB_PREFIX_.'refundreason` rr WHERE rr.`id_order_slip` = oss.`id_order_slip` LIMIT 1), "---") AS `refund_reason`, + IFNULL((SELECT CONCAT(`firstname`, " ", `lastname`) FROM `'._DB_PREFIX_.'employee` WHERE `id_employee` = (SELECT rr.`id_employee` FROM `'._DB_PREFIX_.'refundreason` rr WHERE rr.`id_order_slip` = oss.`id_order_slip` LIMIT 1)), "") AS `refund_employee`, + IF(ps.`id_employee` = 0, "", CONCAT(e.`firstname`, " ", e.`lastname`)) AS `sale_employee`, + (SELECT pssl.`value` FROM `'._DB_PREFIX_.'privatesale_shipping_lang` pssl LEFT JOIN `'._DB_PREFIX_.'privatesale_shipping_sale` psss ON pssl.`id_shipping` = psss.`id_shipping` WHERE psss.`id_sale` = ps.`id_sale` AND pssl.`id_lang` = 2 LIMIT 1) AS `sale_shipping`, + "" AS `spay_transaction`, + "" AS `paypal_transaction`, + "" AS `picker`, + d.product_ean13 AS product_ean13, + "" AS `vip` + FROM + `'._DB_PREFIX_.'orders` o, + `'._DB_PREFIX_.'customer` c, + `'._DB_PREFIX_.'address` a, + `'._DB_PREFIX_.'order_state_lang` sl, + `'._DB_PREFIX_.'country_lang` l, + `'._DB_PREFIX_.'product` pr, + `'._DB_PREFIX_.'product_lang` p, + `'._DB_PREFIX_.'privatesale_category` psc, + `'._DB_PREFIX_.'category_lang` psl, + `'._DB_PREFIX_.'order_slip` oss, + `'._DB_PREFIX_.'order_slip_detail` osd, + `'._DB_PREFIX_.'order_detail` d + LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa + ON pa.id_product_attribute = d.product_attribute_id, + `'._DB_PREFIX_.'privatesale` ps + LEFT JOIN `'._DB_PREFIX_.'employee` e + ON e.`id_employee` = ps.`id_employee` + WHERE c.id_customer = o.id_customer + AND l.id_country = a.id_country + AND l.id_lang = 2 + AND ps.`id_sale` = psc.`id_sale` + AND psl.`id_lang` = 2 + AND psc.`id_category` = ( + SELECT `id_category_default` + FROM `ps_product` + WHERE `id_product` = d.product_id LIMIT 1 + ) + AND psl.`id_category` = ps.`id_category` + AND o.id_address_delivery = a.id_address + AND d.id_order = o.id_order + AND p.id_product = d.product_id + AND sl.`id_lang` = 2 + AND sl.`id_order_state` =( + SELECT h.`id_order_state` + FROM `'._DB_PREFIX_.'order_history` h + WHERE h.`id_order` = o.`id_order` + ORDER BY h.`date_add` DESC + LIMIT 1 + ) + AND pr.id_product = d.product_id + AND oss.`id_order_slip` IN ( + SELECT sd.`id_order_slip` + FROM `'._DB_PREFIX_.'order_slip` sd + WHERE sd.date_add >= DATE_FORMAT(DATE_SUB('.$date_to.', INTERVAL 1 DAY), \'%Y-%m-%d 00:00:00\') + AND sd.date_add <= '.$date_to.' + ) + AND osd.`id_order_slip` = oss.`id_order_slip` + AND osd.`id_order_detail` = d.`id_order_detail` + AND p.id_lang = 2 +'; +$slips = Db::getInstance()->ExecuteS($query); + +$query = ' + SELECT + 0 as `id_sale`, + "" as `sale_title`, + o.id_order AS id_order, + o.`id_cart` AS `id_cart`, + c.id_customer AS id_customer, + c.email AS email, + c.firstname AS firstname, + c.lastname AS lastname, + a.firstname AS shipping_firstname, + a.lastname AS shipping_lastname, + a.address1 AS shipping_street, + a.address2 AS shipping_street2, + a.postcode AS shipping_postcode, + a.city AS shipping_city, + a.phone AS shipping_phone, + a.phone_mobile AS shipping_phone_mobile, + REPLACE(REPLACE(REPLACE(a.other, "\n", " "), "\r", " "), " ", " ") AS other_info, + l.name AS shipping_country, + ca.id_carrier AS id_product, + 0 AS id_product_attribute, + ca.name AS order_product_name, + -1 AS product_quantity, + o.total_shipping / (1 + o.carrier_tax_rate / 100) AS product_price_base_wo_taxes, + o.carrier_tax_rate AS tax_rate, + ca.name AS product_name, + "" AS product_combination, + o.total_shipping / (1 + o.carrier_tax_rate / 100) AS product_price_wo_taxes, + o.total_shipping AS product_price, + 0 AS wholesale_price, + 0 AS combination_wholesale_price, + "SHIPPING" AS supplier_reference, + 0 AS total_shipping, + oss.date_add AS `date`, + o.invoice_number AS invoice_number, + o.payment AS payment_type, + sl.`name` AS `order_state`, + IFNULL((SELECT `id_reason` FROM `'._DB_PREFIX_.'refundreason` rr WHERE rr.`id_order_slip` = oss.`id_order_slip` LIMIT 1), "---") AS `refund_reason`, + IFNULL((SELECT CONCAT(`firstname`, " ", `lastname`) FROM `'._DB_PREFIX_.'employee` WHERE `id_employee` = (SELECT rr.`id_employee` FROM `'._DB_PREFIX_.'refundreason` rr WHERE rr.`id_order_slip` = oss.`id_order_slip` LIMIT 1)), "") AS `refund_employee`, + "" AS `sale_employee`, + "" AS `sale_shipping`, + "" AS `spay_transaction`, + "" AS `paypal_transaction`, + "" AS `picker`, + "" AS product_ean13, + "" AS `vip` + FROM + `'._DB_PREFIX_.'orders` o, + `'._DB_PREFIX_.'carrier` ca, + `'._DB_PREFIX_.'customer` c, + `'._DB_PREFIX_.'address` a, + `'._DB_PREFIX_.'order_state_lang` sl, + `'._DB_PREFIX_.'order_slip` oss, + `'._DB_PREFIX_.'country_lang` l + WHERE c.id_customer = o.id_customer + AND l.id_country = a.id_country + AND l.id_lang = 2 + AND o.id_address_delivery = a.id_address + AND o.id_carrier = ca.id_carrier + AND sl.`id_lang` = 2 + AND sl.`id_order_state` = ( + SELECT h.`id_order_state` + FROM `'._DB_PREFIX_.'order_history` h + WHERE h.`id_order` = o.`id_order` + ORDER BY h.`date_add` DESC + LIMIT 1 + ) + AND oss.`id_order_slip` IN ( + SELECT sd.`id_order_slip` + FROM `'._DB_PREFIX_.'order_slip` sd + WHERE sd.date_add >= DATE_FORMAT(DATE_SUB('.$date_to.', INTERVAL 1 DAY), \'%Y-%m-%d 00:00:00\') + AND sd.date_add <= '.$date_to.' + AND `shipping_cost` = 1 + ) + AND oss.`id_order` = o.`id_order` + GROUP BY o.id_order +'; +$slips_shipping = Db::getInstance()->ExecuteS($query); + + +$canceled_orders = array(); +foreach(Db::getInstance()->ExecuteS(' + SELECT `id_order` + FROM `'._DB_PREFIX_.'order_history` + WHERE (`id_order_state` = 6 + OR `id_order_state` = 7) + AND `date_add` >= DATE_FORMAT(DATE_SUB('.$date_to.', INTERVAL 1 DAY), \'%Y-%m-%d 00:00:00\') + AND `date_add` <= '.$date_to.' + GROUP BY `id_order` + ORDER BY `date_add` DESC +') as $o) { + $canceled_orders[] = (int) $o['id_order']; +} + + +if(count($canceled_orders) > 0) { + $query = ' + SELECT + 0 AS id_sale, + "" AS sale_title, + o.id_order AS id_order, + o.`id_cart` AS `id_cart`, + c.id_customer AS id_customer, + c.email AS email, + c.firstname AS firstname, + c.lastname AS lastname, + a.firstname AS shipping_firstname, + a.lastname AS shipping_lastname, + a.address1 AS shipping_street, + a.address2 AS shipping_street2, + a.postcode AS shipping_postcode, + a.city AS shipping_city, + a.phone AS shipping_phone, + a.phone_mobile AS shipping_phone_mobile, + REPLACE(REPLACE(REPLACE(a.other, "\n", " "), "\r", " "), " ", " ") AS other_info, + l.name AS shipping_country, + od.id_discount AS id_product, + 0 AS id_product_attribute, + od.name AS order_product_name, + -1 AS product_quantity, + od.value AS product_price_base_wo_taxes, + 0 AS tax_rate, + od.name AS product_name, + "" AS product_combination, + od.value AS product_price_wo_taxes, + od.value AS product_price, + 0 AS wholesale_price, + 0 AS combination_wholesale_price, + "DISCOUNT" AS supplier_reference, + 0 AS total_shipping, + ( + SELECT h.`date_add` + FROM `'._DB_PREFIX_.'order_history` h + WHERE h.`id_order` = o.`id_order` + AND (h.`id_order_state` = 6 + OR h.`id_order_state` = 7) + ORDER BY h.`date_add` DESC + LIMIT 1 + ) AS `date`, + o.invoice_number AS invoice_number, + o.payment AS payment_type, + sl.`name` AS `order_state`, + "" AS `refund_reason`, + "" AS `refund_employee`, + "" AS `sale_employee`, + "" AS `sale_shipping`, + "" AS `spay_transaction`, + "" AS `paypal_transaction`, + "" AS `picker`, + "" AS product_ean13, + "" AS `vip` + FROM + `'._DB_PREFIX_.'orders` o, + `'._DB_PREFIX_.'order_discount` od, + `'._DB_PREFIX_.'customer` c, + `'._DB_PREFIX_.'address` a, + `'._DB_PREFIX_.'order_state_lang` sl, + `'._DB_PREFIX_.'country_lang` l + WHERE c.id_customer = o.id_customer + AND l.id_country = a.id_country + AND l.id_lang = 2 + AND o.id_address_delivery = a.id_address + AND od.id_order = o.id_order + AND sl.`id_lang` = 2 + AND o.valid = 0 + AND o.`id_order` IN ('.implode(', ', $canceled_orders).') + AND sl.`id_order_state` = ( + SELECT h.`id_order_state` + FROM `'._DB_PREFIX_.'order_history` h + WHERE h.`id_order` = o.`id_order` + ORDER BY h.`date_add` DESC + LIMIT 1 + ) + GROUP BY c.id_customer, od.id_discount + '; + $slips_discounts = Db::getInstance()->ExecuteS($query); +} + + +$partially_canceled_orders = array(); +foreach(Db::getInstance()->ExecuteS(' + SELECT `id_order` + FROM `'._DB_PREFIX_.'order_slip` + WHERE `date_add` >= DATE_FORMAT(DATE_SUB('.$date_to.', INTERVAL 1 DAY), \'%Y-%m-%d 00:00:00\') + AND `date_add` <= '.$date_to.' + GROUP BY `id_order` + ORDER BY `date_add` ASC +') as $o) { + if(!in_array((int) $o['id_order'], $canceled_orders)) { + $o_order = Db::getInstance()->getRow(' + SELECT `total_paid`, `total_shipping` + FROM `'._DB_PREFIX_.'orders` + WHERE `id_order` = '.(int) $o['id_order'].' + '); + $o_discounts = Db::getInstance()->getRow(' + SELECT SUM(`value`) AS `value` + FROM `'._DB_PREFIX_.'discount` + WHERE `id_discount` IN ( + SELECT `id_discount` + FROM `'._DB_PREFIX_.'order_discount` + WHERE `id_order` = '.(int) $o['id_order'].' + ) + '); + + $o_slip = Db::getInstance()->getRow(' + SELECT `shipping_cost` + FROM `'._DB_PREFIX_.'order_slip` + WHERE `id_order` = '.$o['id_order'].' + '); + $o_products = Db::getInstance()->getRow(' + SELECT SUM(ROUND((d.product_price * (1 - d.reduction_percent / 100) - d.reduction_amount) * (1 + d.tax_rate / 100), 6) * (d.`product_quantity` - d.`product_quantity_return`)) AS product_price + FROM `'._DB_PREFIX_.'order_detail` d + WHERE d.`id_order` = '.(int) $o['id_order'].' + AND (d.`product_quantity` - d.`product_quantity_return`) > 0 + '); + + $total = ($o_slip['shipping_cost'] == 1? 0: (float) $o_order['total_shipping']) + (float) $o_products['product_price']; + $total_discounts = (float) $o_discounts['value']; + + if($total < $total_discounts) { + $partially_canceled_orders[(int) $o['id_order']] = $total - $total_discounts; + } + } +} + +// TODO: find a better solution with a single query +foreach($partially_canceled_orders as $porder => $value) { + $query = ' + SELECT + 0 AS id_sale, + "" AS sale_title, + o.id_order AS id_order, + o.`id_cart` AS `id_cart`, + c.id_customer AS id_customer, + c.email AS email, + c.firstname AS firstname, + c.lastname AS lastname, + a.firstname AS shipping_firstname, + a.lastname AS shipping_lastname, + a.address1 AS shipping_street, + a.address2 AS shipping_street2, + a.postcode AS shipping_postcode, + a.city AS shipping_city, + a.phone AS shipping_phone, + a.phone_mobile AS shipping_phone_mobile, + REPLACE(REPLACE(REPLACE(a.other, "\n", " "), "\r", " "), " ", " ") AS other_info, + l.name AS shipping_country, + 0 AS id_product, + 0 AS id_product_attribute, + "DISCOUNT (DIFF)" AS order_product_name, + -1 AS product_quantity, + '.(float) $value.' AS product_price_base_wo_taxes, + 0 AS tax_rate, + "DISCOUNT (DIFF)" AS product_name, + "" AS product_combination, + '.(float) $value.' AS product_price_wo_taxes, + '.(float) $value.' AS product_price, + 0 AS wholesale_price, + 0 AS combination_wholesale_price, + "DISCOUNT (DIFF)" AS supplier_reference, + 0 AS total_shipping, + ( + SELECT h.`date_add` + FROM `'._DB_PREFIX_.'order_history` h + WHERE h.`id_order` = o.`id_order` + ORDER BY h.`date_add` DESC + LIMIT 1 + ) AS `date`, + o.invoice_number AS invoice_number, + o.payment AS payment_type, + sl.`name` AS `order_state`, + "" AS `refund_reason`, + "" AS `refund_employee`, + "" AS `sale_employee`, + "" AS `sale_shipping`, + "" AS `spay_transaction`, + "" AS `paypal_transaction`, + "" AS `picker`, + "" AS product_ean13, + "" AS `vip` + FROM + `'._DB_PREFIX_.'orders` o, + `'._DB_PREFIX_.'customer` c, + `'._DB_PREFIX_.'address` a, + `'._DB_PREFIX_.'order_state_lang` sl, + `'._DB_PREFIX_.'country_lang` l + WHERE c.id_customer = o.id_customer + AND l.id_country = a.id_country + AND l.id_lang = 2 + AND o.id_address_delivery = a.id_address + AND sl.`id_lang` = 2 + AND o.`id_order` = '.(int) $porder.' + AND sl.`id_order_state` = ( + SELECT h.`id_order_state` + FROM `'._DB_PREFIX_.'order_history` h + WHERE h.`id_order` = o.`id_order` + ORDER BY h.`date_add` DESC + LIMIT 1 + ) + '; + $slips_discounts[] = Db::getInstance()->getRow($query); +} + + +$spay_transactions = array(); +$paypal_transactions = array(); +foreach(Db::getInstance()->ExecuteS(' + SELECT `id_order`, `id_transaction` + FROM `'._DB_PREFIX_.'paypal_order` +') as $row) { + $paypal_transactions[(int) $row['id_order']] = $row['id_transaction']; +} + +$paypal_refunds = array(); +foreach(Db::getInstance()->ExecuteS(' + SELECT `id_order`, `message`, `date_add` + FROM `'._DB_PREFIX_.'message` + WHERE `message` LIKE "%Remboursement fini avec PayPal%" +') as $row) { + $m = explode('REFUNDTRANSACTIONID: ', $row['message']); + $m = explode('
', $m[1]); + + $paypal_refunds[$row['id_order']] = array(array($row['date_add'], $m[0])); +} +foreach(Db::getInstance()->ExecuteS(' + SELECT `id_order`, `message`, `date_add` + FROM `'._DB_PREFIX_.'message` + WHERE `message` LIKE "A product%" + AND `message` NOT LIKE "%ACK: Failure%" +') as $row) { + $m = explode('REFUNDTRANSACTIONID: ', $row['message']); + if(count($m) > 1) { + $m = explode('
', $m[1]); + + if(!isset($paypal_refunds[$row['id_order']])) { + $paypal_refunds[$row['id_order']] = array(); + } + + $paypal_refunds[$row['id_order']][] = array($row['date_add'], $m[0]); + } +} + + +$shipping_details = array(); + +function exportCSV($items, $slips=FALSE) { + global $spay_transactions; + global $paypal_transactions; + global $spay_refunds; + global $paypal_refunds; + global $shipping_details; + $float = array('tax_rate', 'product_price_base_wo_taxes', 'product_price_wo_taxes', 'product_price', 'wholesale_price', 'combination_wholesale_price', 'total_shipping'); + if($items && count($items)) { + $data = ''; + foreach($items as $row) { + $line = ''; + foreach($row as $k => $v) { + if(in_array($k, $float)) { + $v = str_replace('.', ',', (string) $v); + } + if($k == 'other_info') { + $line .= ';"'.str_replace(';', ',', str_replace('"', '\"', $v)).'"'; + } else { + $line .= ';'.str_replace(';', ',', str_replace(array("\r", "\n"), '', str_replace('"', '\"', $v))); + } + } + + if(strtolower($row['payment_type']) == 'paypal') { + if($slips) { + $refunds = isset($paypal_refunds[(int) $row['id_order']])? $paypal_refunds[(int) $row['id_order']]: ''; + + if($refunds) { + $ids = array(); + foreach($refunds as $r) { + if(abs(strtotime($r[0]) - strtotime($row['date'])) != 60) { + $ids[] = $r[1]; + } + } + + $line .= ';;'.implode(',', $ids); + } else { + $line .= ';;'; + } + } else { + $line .= ';;'.(isset($paypal_transactions[(int) $row['id_order']])? $paypal_transactions[(int) $row['id_order']]: ''); + } + } else { + if($slips) { + $refunds = isset($spay_refunds[(int) $row['id_cart']])? $spay_refunds[(int) $row['id_cart']]: ''; + + if($refunds) { + $ids = array(); + foreach($refunds as $r) { + if(abs(strtotime($r[0]) - strtotime($row['date'])) != 60) { + $ids[] = $r[1]; + } + } + + $line .= ';'.implode(',', $ids).';'; + } else { + $line .= ';;'; + } + } else { + $line .= ';'.(isset($spay_transactions[(int) $row['id_cart']])? $spay_transactions[(int) $row['id_cart']]: '').';'; + } + } + + $line .= ';'.(isset($shipping_details[(int) $row['id_order']]) && isset($shipping_details[(int) $row['id_order']][(int) $row['id_product'].'_'.(int) $row['id_product_attribute']])? $shipping_details[(int) $row['id_order']][(int) $row['id_product'].'_'.(int) $row['id_product_attribute']]: ''); + + $data .= "\n".substr($line, 1); + } + return $data; + } +} + +//$result = 'id_sale;sale_title;id_order;id_cart;id_customer;email;firstname;lastname;shipping_firstname;shipping_lastname;shipping_street;shipping_street2;shipping_postcode;shipping_city;shipping_phone;shipping_phone_mobile;other_info;shipping_country;id_product;id_product_attribute;order_product_name;product_quantity;product_price_base_wo_taxes;tax_rate;product_name;product_combination;product_price_wo_taxes;product_price;wholesale_price;combination_wholesale_price;supplier_reference;total_shipping;date;invoice_number;payment_type;order_state;refund_reason;refund_employee;sale_employee;spay_transaction;paypal_transaction;picker;product_ean13;vip'; + +$result = ''; +$result .= exportCSV($orders); +$result .= exportCSV($shipping); +$result .= exportCSV($discounts); +$result .= exportCSV($slips, TRUE); +$result .= exportCSV($slips_shipping, TRUE); +$result .= exportCSV($slips_discounts, TRUE); + + +echo $result; + +//file_put_contents('extract/daily-daily/'.date('Y-m-d', mktime()).'-dailydaily2_test.csv', $result); +//~ file_put_contents('extract/daily-daily/2012-08-27-dailydaily2.csv', $result); +exit; diff --git a/cron_export_sales_monthly.php b/cron_export_sales_monthly.php new file mode 100644 index 0000000..8dac2e9 --- /dev/null +++ b/cron_export_sales_monthly.php @@ -0,0 +1,775 @@ +ExecuteS(' + SELECT id_parent, id_category + FROM `'._DB_PREFIX_.'category` + WHERE `id_parent` = '.$id + ); +} + +function getCat($id=0, $title='') { + $products = Db::getInstance()->ExecuteS(' + SELECT id_product, "'.addslashes($title).'" AS `title` + FROM `'._DB_PREFIX_.'product` + WHERE `id_category_default` = '.$id + ); + foreach(getChildren($id) as $child) { + $products = array_merge($products, getCat($child['id_category'])); + } + return $products; +} + + +$f = fopen('extracts/monthly_compta/'.date('Y-m-d', strtotime($now)).'_new.csv', 'w'); + +$headers = array( + 'date', + 'id_order', + 'id_order_slip', + 'invoice_number', + 'multisale', + 'id_customer', + 'firstname', + 'lastname', + 'total_products_wo_taxes_210', + 'total_taxes_210', + 'total_products_wo_taxes_200', + 'total_taxes_200', + 'total_products_wo_taxes_196', + 'total_taxes_196', + 'total_products_wo_taxes_100', + 'total_taxes_100', + 'total_products_wo_taxes_55', + 'total_taxes_55', + 'total_products_wo_taxes_21', + 'total_taxes_21', + 'total_products_wo_taxes_60', + 'total_taxes_60', + 'total_products_wo_taxes_40', + 'total_taxes_40', + 'total_products_wo_taxes_export', + 'shipping_wo_taxes_196-200', + 'shipping_wo_taxes_export', + 'shipping_country', + 'discounts', + 'total_paid', + 'total_paid_real', + 'wholesale_price', + 'payment_type', + 'direct_payment', + 'refund_reason', + 'date_subsribe', + 'pays', + 'device', +); + +fwrite($f, implode(';', $headers)."\n"); + +$query_orders = array(); +$orders = array(); +// ORDERS +foreach(Db::getInstance()->ExecuteS(' + SELECT DISTINCT o.`id_order`, + (CASE o.`appli` + WHEN 0 THEN "DESKTOP" + WHEN 1 THEN "APPLICATION" + WHEN 2 THEN "MOBILE" + END) AS device + FROM `'._DB_PREFIX_.'orders` o + LEFT JOIN `'._DB_PREFIX_.'order_detail` d ON d.id_order = o.id_order + LEFT JOIN `'._DB_PREFIX_.'customer` c ON c.id_customer = o.id_customer + LEFT JOIN `'._DB_PREFIX_.'order_state_current` os ON os.id_order = o.id_order + WHERE + (o.valid = 1 + OR ( + o.valid = 0 + AND ( os.id_order_state = 6 OR os.id_order_state = 7 OR os.id_order_state = 11) + )) + AND o.date_add >= DATE_FORMAT("'.$now.'" - INTERVAL 1 MONTH, \'%Y-%m-01 00:00:00\') + AND o.date_add <= DATE_FORMAT(LAST_DAY("'.$now.'" - INTERVAL 1 MONTH), \'%Y-%m-%d 23:59:59\') + GROUP BY o.id_order +') as $row){ + $orders[(int)$row['id_order']] = array(); + $query_orders[(int)$row['id_order']] = (int)$row['id_order']; +} +// CUSTOMERS +foreach(Db::getInstance()->ExecuteS(' + SELECT c.*, c.`date_add` as subscribe, o.*, v.`version`, + (CASE o.`appli` + WHEN 0 THEN "DESKTOP" + WHEN 1 THEN "APPLICATION" + WHEN 2 THEN "MOBILE" + END) AS device + FROM `'._DB_PREFIX_.'orders` o + LEFT JOIN `'._DB_PREFIX_.'customer` c ON c.`id_customer` = o.`id_customer` + LEFT JOIN `'._DB_PREFIX_.'customer_version` v ON v.`id_customer` = c.`id_customer` + WHERE o.id_order IN ('.implode(',',$query_orders).') +') as $row){ + if(isset($orders[(int)$row['id_order']])){ + $orders[(int)$row['id_order']] = $row; + } +} +// MULTI +foreach(Db::getInstance()->ExecuteS(' + SELECT COUNT(DISTINCT c.`id_sale`) as multi, o.`id_order` + FROM `'._DB_PREFIX_.'product_ps_cache` c + LEFT JOIN `'._DB_PREFIX_.'order_detail` o ON o.product_id = c.`id_product` + WHERE o.`id_order` IN ('.implode(',',$query_orders).') + GROUP BY o.`id_order` +') as $row){ + if(isset($orders[(int)$row['id_order']])){ + $orders[(int)$row['id_order']]['multi'] = (int)$row['multi'] == 1?'S':'M'; + } +} +// ORDER DETAILS +foreach(Db::getInstance()->ExecuteS(' + SELECT d.*, p.`wholesale_price` + FROM `'._DB_PREFIX_.'order_detail` d + LEFT JOIN `'._DB_PREFIX_.'product` p ON (p.`id_product` = d.`product_id`) + WHERE d.id_order IN ('.implode(',',$query_orders).') +') as $row){ + if(isset($orders[(int)$row['id_order']])){ + if(!isset($orders[(int)$row['id_order']]['order_details'])){ + $orders[(int)$row['id_order']]['order_details'] = array(); + } + $orders[(int)$row['id_order']]['order_details'][] = $row; + } +} + +foreach($orders as $order) { + $address = Db::getInstance()->ExecuteS(' + SELECT a.*, c.`id_zone` + FROM `'._DB_PREFIX_.'address` a, `'._DB_PREFIX_.'country` c + WHERE a.`id_address` = '.$order['id_address_delivery'].' + AND a.`id_country` = c.`id_country` + '); + $address = $address[0]; + + $invoice_address = Db::getInstance()->ExecuteS(' + SELECT firstname, lastname + FROM `'._DB_PREFIX_.'address` + WHERE `id_address` = '.$order['id_address_invoice'] + ); + $invoice_address = $invoice_address[0]; + + $total_products_wo_taxes_210 = array(); + $total_taxes_210 = array(); + $total_products_wo_taxes_200 = array(); + $total_taxes_200 = array(); + $total_products_wo_taxes_196 = array(); + $total_taxes_196 = array(); + $total_products_wo_taxes_100 = array(); + $total_taxes_100 = array(); + $total_products_wo_taxes_55 = array(); + $total_taxes_55 = array(); + $total_products_wo_taxes_21 = array(); + $total_taxes_21 = array(); + $total_products_wo_taxes_60 = array(); + $total_taxes_60 = array(); + $total_products_wo_taxes_40 = array(); + $total_taxes_40 = array(); + $total_products_wo_taxes_export = array(); + + $country = Db::getInstance()->getValue(' + SELECT `name` + FROM `'._DB_PREFIX_.'country_lang` + WHERE `id_country` = ( + SELECT `id_country` + FROM `'._DB_PREFIX_.'address` + WHERE `id_address` = '.(int) $order['id_address_delivery'].' + LIMIT 1 + ) + AND `id_lang` = 2 + '); + + $date_send = Db::getInstance()->getValue(' + SELECT `date_add` + FROM `'._DB_PREFIX_.'order_history` + WHERE `id_order_state` = 4 + AND `id_order` = '.(int) $order['id_order'].' + ORDER BY `date_add` ASC + '); + + $ldetails = array(); + $wholesale_price = 0; + foreach($order['order_details'] as $detail) { + $ldetails[$detail['id_order_detail']] = $detail; + $wholesale_price += (float) ($detail['wholesale_price'] * $detail['product_quantity']); + if($address['id_country'] != 19) { + if($detail['tax_rate'] == '20.000') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_200[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_200[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.2); + } else { + $total_products_wo_taxes_200[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_200[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.2); + } + } elseif($detail['tax_rate'] == '21.000') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_210[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_210[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.21); + } else { + $total_products_wo_taxes_210[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_210[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.21); + } + } elseif($detail['tax_rate'] == '19.600') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_196[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_196[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.196); + } else { + $total_products_wo_taxes_196[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_196[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.196); + } + } elseif($detail['tax_rate'] == '10.000') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_100[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_100[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.100); + } else { + $total_products_wo_taxes_100[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_100[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.100); + } + } elseif($detail['tax_rate'] == '5.500') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_55[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_55[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.055); + } else { + $total_products_wo_taxes_55[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_55[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.055); + } + } elseif($detail['tax_rate'] == '2.100') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_21[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_21[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.021); + } else { + $total_products_wo_taxes_21[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_21[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.021); + } + } elseif($detail['tax_rate'] == '6.000') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_60[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_60[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.060); + } else { + $total_products_wo_taxes_60[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_60[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.060); + } + } elseif($detail['tax_rate'] == '4.000') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_40[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_40[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.040); + } else { + $total_products_wo_taxes_40[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_40[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.040); + } + } else { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + } else { + $total_products_wo_taxes_export[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + } + } + } else { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + } else { + $total_products_wo_taxes_export[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + } + } + } + + $_total_products_wo_taxes_210 = 0.0; + foreach($total_products_wo_taxes_210 as $value) { + $_total_products_wo_taxes_210 += $value; + } + $_total_taxes_210 = 0.0; + foreach($total_taxes_210 as $value) { + $_total_taxes_210 += $value; + } + + $_total_products_wo_taxes_200 = 0.0; + foreach($total_products_wo_taxes_200 as $value) { + $_total_products_wo_taxes_200 += $value; + } + $_total_taxes_200 = 0.0; + foreach($total_taxes_200 as $value) { + $_total_taxes_200 += $value; + } + + $_total_products_wo_taxes_196 = 0.0; + foreach($total_products_wo_taxes_196 as $value) { + $_total_products_wo_taxes_196 += $value; + } + $_total_taxes_196 = 0.0; + foreach($total_taxes_196 as $value) { + $_total_taxes_196 += $value; + } + + $_total_products_wo_taxes_100 = 0.0; + foreach($total_products_wo_taxes_100 as $value) { + $_total_products_wo_taxes_100 += $value; + } + $_total_taxes_100 = 0.0; + foreach($total_taxes_100 as $value) { + $_total_taxes_100 += $value; + } + + $_total_products_wo_taxes_55 = 0.0; + foreach($total_products_wo_taxes_55 as $value) { + $_total_products_wo_taxes_55 += $value; + } + $_total_taxes_55 = 0.0; + foreach($total_taxes_55 as $value) { + $_total_taxes_55 += $value; + } + + $_total_products_wo_taxes_21 = 0.0; + foreach($total_products_wo_taxes_21 as $value) { + $_total_products_wo_taxes_21 += $value; + } + $_total_taxes_21 = 0.0; + foreach($total_taxes_21 as $value) { + $_total_taxes_21 += $value; + } + + $_total_products_wo_taxes_60 = 0.0; + foreach($total_products_wo_taxes_60 as $value) { + $_total_products_wo_taxes_60 += $value; + } + $_total_taxes_60 = 0.0; + foreach($total_taxes_60 as $value) { + $_total_taxes_60 += $value; + } + + $_total_products_wo_taxes_40 = 0.0; + foreach($total_products_wo_taxes_40 as $value) { + $_total_products_wo_taxes_40 += $value; + } + $_total_taxes_40 = 0.0; + foreach($total_taxes_40 as $value) { + $_total_taxes_40 += $value; + } + + $_total_products_wo_taxes_export = 0.0; + foreach($total_products_wo_taxes_export as $value) { + $_total_products_wo_taxes_export += $value; + } + + // Find if it's a saved card or account for the payment (Direct payment) + $is_direct_payment = 0; + if($order['payment'] == 'Paybox') { + $is_direct_payment = Db::getInstance()->getValue(' + SELECT `is_saved_card` + FROM `'._DB_PREFIX_.'paybox_transaction` + WHERE `id_cart` = '.(int) $order['id_cart'].' + '); + } elseif ($order['payment'] == 'PayPal') { + $is_direct_payment = Db::getInstance()->getValue(' + SELECT `is_billing` + FROM `'._DB_PREFIX_.'paypal_order` + WHERE `id_order` = '.(int) $order['id_order'].' + '); + } + if((int)$is_direct_payment != 0) { + $order['direct_payment'] = "Oui"; + } else { + $order['direct_payment'] = "Non"; + } + + fwrite($f, implode(';', array( + $order['date_add'], + $order['id_order'], + 0, + $order['invoice_number'], + $order['multi'], + $order['id_customer'], + $invoice_address['firstname'], + $invoice_address['lastname'], + $_total_products_wo_taxes_210, + $_total_taxes_210, + $_total_products_wo_taxes_200, + $_total_taxes_200, + $_total_products_wo_taxes_196, + $_total_taxes_196, + $_total_products_wo_taxes_100, + $_total_taxes_100, + $_total_products_wo_taxes_55, + $_total_taxes_55, + $_total_products_wo_taxes_21, + $_total_taxes_21, + $_total_products_wo_taxes_60, + $_total_taxes_60, + $_total_products_wo_taxes_40, + $_total_taxes_40, + $_total_products_wo_taxes_export, + ($address['id_country'] != 19? (float) $order['total_shipping'] / (strtotime($order['date_add']) >= mktime(0, 0, 0, 1, 1, 2014)? 1.2: 1.196): 0.0), + ($address['id_country'] != 19? 0.0: $order['total_shipping']), + $country, + $order['total_discounts'], + $order['total_paid'], + $order['total_paid_real'], + $wholesale_price, + $order['payment'], + $order['direct_payment'], + '', + $order['subscribe'], + $order['version'], + $order['device'] + ))."\n"); +} + +// This order has been refunded +$slips = Db::getInstance()->ExecuteS(' + SELECT * + FROM `'._DB_PREFIX_.'order_slip` os + WHERE os.date_add >= DATE_FORMAT("'.$now.'" - INTERVAL 1 MONTH, \'%Y-%m-01 00:00:00\') + AND os.date_add <= DATE_FORMAT(LAST_DAY("'.$now.'" - INTERVAL 1 MONTH), \'%Y-%m-%d 23:59:59\') +'); + +$refundreasons = array( + '', + 'CLIENT : Annulation pré-envoi', + 'CLIENT : Rétractation post-envoi', + 'BBB : Erreur Achat / Prod', + 'FEUR : Problème SAV', + 'FEUR : Produit manquant', + 'BBB : Erreur logistique', + 'Autre', + 'BBB : Pbme Site / Paiement', + 'TRANS : Colis détruit', + 'TRANS : Colis perdu', + 'CLIENT : Annulation pour ré-achat', + 'BBB : Suspicion de fraude', +); + +if(count($slips) > 0) { + foreach($slips as $slip) { + $order = Db::getInstance()->getRow(' + SELECT o.*, c.*, c.`date_add` as subscribe, v.`version` + FROM `'._DB_PREFIX_.'orders` o + LEFT JOIN `'._DB_PREFIX_.'customer` c + ON o.`id_customer` = c.`id_customer` + LEFT JOIN `'._DB_PREFIX_.'customer_version` v + ON c.`id_customer` = v.`id_customer` + WHERE + o.id_order = '.$slip['id_order'].' + '); + + $multi = Db::getInstance()->getValue(' + SELECT COUNT(DISTINCT `id_sale`) + FROM `'._DB_PREFIX_.'product_ps_cache` + WHERE `id_product` IN ( + SELECT `product_id` FROM `'._DB_PREFIX_.'order_detail` + WHERE `id_order` = '.(int) $order['id_order'].' + ) + ') == 1? 'S': 'M'; + + $address = Db::getInstance()->ExecuteS(' + SELECT a.*, c.`id_zone` + FROM `'._DB_PREFIX_.'address` a, `'._DB_PREFIX_.'country` c + WHERE a.`id_address` = '.$order['id_address_delivery'].' + AND a.`id_country` = c.`id_country` + '); + $address = $address[0]; + + $invoice_address = Db::getInstance()->ExecuteS(' + SELECT firstname, lastname + FROM `'._DB_PREFIX_.'address` + WHERE `id_address` = '.$order['id_address_invoice'] + ); + $invoice_address = $invoice_address[0]; + + $order_details = Db::getInstance()->ExecuteS(' + SELECT d.*, p.`wholesale_price` + FROM `'._DB_PREFIX_.'order_detail` d + LEFT JOIN `'._DB_PREFIX_.'product` p ON (p.`id_product` = d.`product_id`) + WHERE d.id_order = '.(int) $slip['id_order'].' + AND d.`id_order_detail` IN ( + SELECT `id_order_detail` + FROM `'._DB_PREFIX_.'order_slip_detail` + WHERE `id_order_slip` = '.(int) $slip['id_order_slip'].' + ) + '); + + $country = Db::getInstance()->getValue(' + SELECT `name` + FROM `'._DB_PREFIX_.'country_lang` + WHERE `id_country` = ( + SELECT `id_country` + FROM `'._DB_PREFIX_.'address` + WHERE `id_address` = '.(int) $order['id_address_delivery'].' + LIMIT 1 + ) + AND `id_lang` = 2 + '); + + $date_send = Db::getInstance()->getValue(' + SELECT `date_add` + FROM `'._DB_PREFIX_.'order_history` + WHERE `id_order_state` = 4 + AND `id_order` = '.(int) $order['id_order'].' + ORDER BY `date_add` ASC + '); + + $ldetails = array(); + $total_products_wo_taxes_210 = array(); + $total_taxes_210 = array(); + $total_products_wo_taxes_200 = array(); + $total_taxes_200 = array(); + $total_products_wo_taxes_196 = array(); + $total_taxes_196 = array(); + $total_products_wo_taxes_100 = array(); + $total_taxes_100 = array(); + $total_products_wo_taxes_55 = array(); + $total_taxes_55 = array(); + $total_products_wo_taxes_21 = array(); + $total_taxes_21 = array(); + $total_products_wo_taxes_60 = array(); + $total_taxes_60 = array(); + $total_products_wo_taxes_40 = array(); + $total_taxes_40 = array(); + $total_products_wo_taxes_export = array(); + $wholesale_price = 0; + foreach($order_details as $detail) { + $ldetails[$detail['id_order_detail']] = $detail; + $wholesale_price += (float) ($detail['wholesale_price'] * $detail['product_quantity_refunded']); + + if($address['id_country'] != 19) { + if($detail['tax_rate'] == '19.600') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_196[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_196[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.196); + } else { + $total_products_wo_taxes_196[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_196[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.196); + } + } elseif($detail['tax_rate'] == '21.000') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_210[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_210[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.21); + } else { + $total_products_wo_taxes_210[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_210[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.21); + } + } elseif($detail['tax_rate'] == '10.000') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_100[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_100[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.100); + } else { + $total_products_wo_taxes_100[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_100[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.100); + } + } elseif($detail['tax_rate'] == '5.500') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_55[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_55[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.055); + } else { + $total_products_wo_taxes_55[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_55[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.055); + } + } elseif($detail['tax_rate'] == '2.100') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_21[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_21[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.021); + } else { + $total_products_wo_taxes_21[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_21[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.021); + } + } elseif($detail['tax_rate'] == '20.000') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_200[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_200[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.2); + } else { + $total_products_wo_taxes_200[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_200[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.2); + } + } elseif($detail['tax_rate'] == '6.000') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_60[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_60[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.06); + } else { + $total_products_wo_taxes_60[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_60[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.06); + } + } elseif($detail['tax_rate'] == '4.000') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_40[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_40[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.04); + } else { + $total_products_wo_taxes_40[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_40[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.04); + } + } else { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + } else { + $total_products_wo_taxes_export[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + } + } + } else { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + } else { + $total_products_wo_taxes_export[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + } + } + } + + $_total_products_wo_taxes_210 = 0.0; + foreach($total_products_wo_taxes_210 as $value) { + $_total_products_wo_taxes_210 += $value; + } + $_total_taxes_210 = 0.0; + foreach($total_taxes_210 as $value) { + $_total_taxes_210 += $value; + } + + $_total_products_wo_taxes_200 = 0.0; + foreach($total_products_wo_taxes_200 as $value) { + $_total_products_wo_taxes_200 += $value; + } + $_total_taxes_200 = 0.0; + foreach($total_taxes_200 as $value) { + $_total_taxes_200 += $value; + } + + $_total_products_wo_taxes_196 = 0.0; + foreach($total_products_wo_taxes_196 as $value) { + $_total_products_wo_taxes_196 += $value; + } + $_total_taxes_196 = 0.0; + foreach($total_taxes_196 as $value) { + $_total_taxes_196 += $value; + } + + $_total_products_wo_taxes_100 = 0.0; + foreach($total_products_wo_taxes_100 as $value) { + $_total_products_wo_taxes_100 += $value; + } + $_total_taxes_100 = 0.0; + foreach($total_taxes_100 as $value) { + $_total_taxes_100 += $value; + } + + $_total_products_wo_taxes_55 = 0.0; + foreach($total_products_wo_taxes_55 as $value) { + $_total_products_wo_taxes_55 += $value; + } + $_total_taxes_55 = 0.0; + foreach($total_taxes_55 as $value) { + $_total_taxes_55 += $value; + } + + $_total_products_wo_taxes_21 = 0.0; + foreach($total_products_wo_taxes_21 as $value) { + $_total_products_wo_taxes_21 += $value; + } + $_total_taxes_21 = 0.0; + foreach($total_taxes_21 as $value) { + $_total_taxes_21 += $value; + } + + $_total_products_wo_taxes_60 = 0.0; + foreach($total_products_wo_taxes_60 as $value) { + $_total_products_wo_taxes_60 += $value; + } + $_total_taxes_60 = 0.0; + foreach($total_taxes_60 as $value) { + $_total_taxes_60 += $value; + } + + $_total_products_wo_taxes_40 = 0.0; + foreach($total_products_wo_taxes_40 as $value) { + $_total_products_wo_taxes_40 += $value; + } + $_total_taxes_40 = 0.0; + foreach($total_taxes_40 as $value) { + $_total_taxes_40 += $value; + } + + $_total_products_wo_taxes_export = 0.0; + foreach($total_products_wo_taxes_export as $value) { + $_total_products_wo_taxes_export += $value; + } + + $refundreason = Db::getInstance()->getValue(' + SELECT `id_reason` + FROM `'._DB_PREFIX_.'refundreason` + WHERE `id_order_slip` = '.(int) $slip['id_order_slip'].' + '); + + $refundreason = $refundreasons[(int) $refundreason]; + + // Find if it's a saved card or account for the payment (Direct payment) + $is_direct_payment = 0; + if($order['payment'] == 'Paybox') { + $is_direct_payment = Db::getInstance()->getValue(' + SELECT `is_saved_card` + FROM `'._DB_PREFIX_.'paybox_transaction` + WHERE `id_cart` = '.(int) $order['id_cart'].' + '); + } elseif ($order['payment'] == 'PayPal') { + $is_direct_payment = Db::getInstance()->getValue(' + SELECT `is_billing` + FROM `'._DB_PREFIX_.'paypal_order` + WHERE `id_order` = '.(int) $order['id_order'].' + '); + } + if((int)$is_direct_payment != 0) { + $order['direct_payment'] = "Oui"; + } else { + $order['direct_payment'] = "Non"; + } + + fwrite($f, implode(';', array( + $slip['date_add'], + $order['id_order'], + $slip['id_order_slip'], + $order['invoice_number'], + $multi, + $order['id_customer'], + $invoice_address['firstname'], + $invoice_address['lastname'], + '-'.$_total_products_wo_taxes_210, + '-'.$_total_taxes_210, + '-'.$_total_products_wo_taxes_200, + '-'.$_total_taxes_200, + '-'.$_total_products_wo_taxes_196, + '-'.$_total_taxes_196, + '-'.$_total_products_wo_taxes_100, + '-'.$_total_taxes_100, + '-'.$_total_products_wo_taxes_55, + '-'.$_total_taxes_55, + '-'.$_total_products_wo_taxes_21, + '-'.$_total_taxes_21, + '-'.$_total_products_wo_taxes_60, + '-'.$_total_taxes_60, + '-'.$_total_products_wo_taxes_40, + '-'.$_total_taxes_40, + '-'.$_total_products_wo_taxes_export, + '-'.($slip['shipping_cost'] == 1? ($address['id_country'] != 19? (float) $order['total_shipping'] / (strtotime($order['date_add']) >= mktime(0, 0, 0, 1, 1, 2014)? 1.2: 1.196): 0.0): 0.0), + '-'.($slip['shipping_cost'] == 1? ($address['id_country'] != 19? 0.0: $order['total_shipping']): 0.0), + $country, + '-0.0', + '-'.($_total_products_wo_taxes_210 + $_total_taxes_210 + $_total_products_wo_taxes_200 + $_total_taxes_200 + $_total_products_wo_taxes_196 + $_total_taxes_196 + $_total_products_wo_taxes_100 + $_total_taxes_100 + $_total_products_wo_taxes_55 + $_total_taxes_55 + $_total_products_wo_taxes_21 + $_total_taxes_21 + $_total_products_wo_taxes_60 + $_total_taxes_60 + $_total_products_wo_taxes_40 + $_total_taxes_40 + $_total_products_wo_taxes_export + ($slip['shipping_cost'] == 1? (float) $order['total_shipping']: 0.0)), + '-'.($_total_products_wo_taxes_210 + $_total_taxes_210 + $_total_products_wo_taxes_200 + $_total_taxes_200 + $_total_products_wo_taxes_196 + $_total_taxes_196 + $_total_products_wo_taxes_100 + $_total_taxes_100 + $_total_products_wo_taxes_55 + $_total_taxes_55 + $_total_products_wo_taxes_21 + $_total_taxes_21 + $_total_products_wo_taxes_60 + $_total_taxes_60 + $_total_products_wo_taxes_40 + $_total_taxes_40 + $_total_products_wo_taxes_export + ($slip['shipping_cost'] == 1? (float) $order['total_shipping']: 0.0)), + '-'.$wholesale_price, + 'REFUND '.strtoupper($order['payment']), + $order['direct_payment'], + $refundreason, + $order['subscribe'], + $order['version'], + ))."\n"); + } +} + +fclose($f); + +exit; diff --git a/cron_export_sales_monthly_2.php b/cron_export_sales_monthly_2.php new file mode 100644 index 0000000..bee28ff --- /dev/null +++ b/cron_export_sales_monthly_2.php @@ -0,0 +1,918 @@ +ExecuteS(' + SELECT id_parent, id_category + FROM `'._DB_PREFIX_.'category` + WHERE `id_parent` = '.$id + ); +} + +function getCat($id=0, $title='') { + $products = Db::getInstance()->ExecuteS(' + SELECT id_product, "'.addslashes($title).'" AS `title` + FROM `'._DB_PREFIX_.'product` + WHERE `id_category_default` = '.$id + ); + foreach(getChildren($id) as $child) { + $products = array_merge($products, getCat($child['id_category'])); + } + return $products; +} + + +// $f = fopen('extracts/monthly_compta/decembre2014.csv', 'w'); +$f = fopen('extracts/monthly_compta/'.date('Y-m-d', strtotime($now)).'_last.csv', 'w'); +//$f = fopen('extracts/monthly_compta/2012-01-01_ok2.csv', 'w'); + +$headers = array( + 'date', + //'sale_title', + 'id_order', + 'id_order_slip', + 'invoice_number', + 'multisale', + 'id_customer', + 'firstname', + 'lastname', + 'total_products_wo_taxes_210', + 'total_taxes_210', + 'total_products_wo_taxes_200', + 'total_taxes_200', + 'total_products_wo_taxes_196', + 'total_taxes_196', + 'total_products_wo_taxes_100', + 'total_taxes_100', + 'total_products_wo_taxes_55', + 'total_taxes_55', + 'total_products_wo_taxes_21', + 'total_taxes_21', + 'total_products_wo_taxes_60', + 'total_taxes_60', + 'total_products_wo_taxes_40', + 'total_taxes_40', + 'total_products_wo_taxes_export', + 'shipping_wo_taxes_196-200', + 'shipping_wo_taxes_export', + 'shipping_country', + 'discounts', + 'total_paid', + 'total_paid_real', + 'total_achat', + 'payment_type', + 'direct_payment', + 'date_send', + 'refund_reason', + 'new', +); + +fwrite($f, implode(';', $headers)."\n"); + + +$orders = Db::getInstance()->ExecuteS(' + SELECT o.id_order + FROM `'._DB_PREFIX_.'orders` o, + `'._DB_PREFIX_.'order_detail` d, + `'._DB_PREFIX_.'customer` c + WHERE + (o.valid = 1 + OR ( + o.valid = 0 + AND ( + (SELECT h.id_order_state FROM `'._DB_PREFIX_.'order_history` h WHERE h.id_order = o.id_order ORDER BY h.date_add DESC LIMIT 1) = 6 + OR (SELECT h.id_order_state FROM `'._DB_PREFIX_.'order_history` h WHERE h.id_order = o.id_order ORDER BY h.date_add DESC LIMIT 1) = 7 + OR (SELECT h.id_order_state FROM `'._DB_PREFIX_.'order_history` h WHERE h.id_order = o.id_order ORDER BY h.date_add DESC LIMIT 1) = 11 + ) + )) + AND c.id_customer = o.id_customer + AND d.id_order = o.id_order + AND o.date_add >= DATE_FORMAT("'.$now.'" - INTERVAL 2 MONTH, \'%Y-%m-01 00:00:00\') + AND o.date_add <= DATE_FORMAT(LAST_DAY("'.$now.'" - INTERVAL 2 MONTH), \'%Y-%m-%d 23:59:59\') + GROUP BY o.id_order +'); + +// $orders = Db::getInstance()->ExecuteS(' +// SELECT o.id_order +// FROM `'._DB_PREFIX_.'orders` o, +// `'._DB_PREFIX_.'order_detail` d, +// `'._DB_PREFIX_.'customer` c +// WHERE +// (o.valid = 1 +// OR ( +// o.valid = 0 +// AND ( +// (SELECT h.id_order_state FROM `'._DB_PREFIX_.'order_history` h WHERE h.id_order = o.id_order ORDER BY h.date_add DESC LIMIT 1) = 6 +// OR (SELECT h.id_order_state FROM `'._DB_PREFIX_.'order_history` h WHERE h.id_order = o.id_order ORDER BY h.date_add DESC LIMIT 1) = 7 +// OR (SELECT h.id_order_state FROM `'._DB_PREFIX_.'order_history` h WHERE h.id_order = o.id_order ORDER BY h.date_add DESC LIMIT 1) = 11 +// ) +// )) +// AND c.id_customer = o.id_customer +// AND d.id_order = o.id_order +// AND o.date_add >= "2014-12-01 00:00:00" +// AND o.date_add <= "2014-12-31 23:59:59" +// GROUP BY o.id_order +// '); + + +foreach($orders as $o) { + $order = Db::getInstance()->ExecuteS(' + SELECT c.*, o.* + FROM `'._DB_PREFIX_.'orders` o, + `'._DB_PREFIX_.'customer` c + WHERE + o.id_order = '.$o['id_order'].' + AND c.`id_customer` = o.`id_customer` + '); + $order = $order[0]; + + $multi = Db::getInstance()->getValue(' + SELECT COUNT(DISTINCT `id_sale`) + FROM `'._DB_PREFIX_.'product_ps_cache` + WHERE `id_product` IN ( + SELECT `product_id` FROM `'._DB_PREFIX_.'order_detail` + WHERE `id_order` = '.(int) $order['id_order'].' + ) + ') == 1? 'S': 'M'; + + $address = Db::getInstance()->ExecuteS(' + SELECT a.*, c.`id_zone` + FROM `'._DB_PREFIX_.'address` a, `'._DB_PREFIX_.'country` c + WHERE a.`id_address` = '.$order['id_address_delivery'].' + AND a.`id_country` = c.`id_country` + '); + $address = $address[0]; + + $invoice_address = Db::getInstance()->ExecuteS(' + SELECT firstname, lastname + FROM `'._DB_PREFIX_.'address` + WHERE `id_address` = '.$order['id_address_invoice'] + ); + $invoice_address = $invoice_address[0]; + + $order_details = Db::getInstance()->ExecuteS(' + SELECT * + FROM `'._DB_PREFIX_.'order_detail` d + WHERE d.id_order = '.$o['id_order'] + ); + + $total_products_wo_taxes_210 = array(); + $total_taxes_210 = array(); + $total_products_wo_taxes_200 = array(); + $total_taxes_200 = array(); + $total_products_wo_taxes_196 = array(); + $total_taxes_196 = array(); + $total_products_wo_taxes_100 = array(); + $total_taxes_100 = array(); + $total_products_wo_taxes_55 = array(); + $total_taxes_55 = array(); + $total_products_wo_taxes_21 = array(); + $total_taxes_21 = array(); + $total_products_wo_taxes_60= array(); + $total_taxes_60= array(); + $total_products_wo_taxes_40 = array(); + $total_taxes_40 = array(); + $total_products_wo_taxes_export = array(); + + $country = Db::getInstance()->getValue(' + SELECT `name` + FROM `'._DB_PREFIX_.'country_lang` + WHERE `id_country` = ( + SELECT `id_country` + FROM `'._DB_PREFIX_.'address` + WHERE `id_address` = '.(int) $order['id_address_delivery'].' + LIMIT 1 + ) + AND `id_lang` = 2 + '); + + $date_send = Db::getInstance()->getValue(' + SELECT `date_add` + FROM `'._DB_PREFIX_.'order_history` + WHERE `id_order_state` = 4 + AND `id_order` = '.(int) $order['id_order'].' + ORDER BY `date_add` ASC + '); + + // calcul prix d'achat de la commande + $wholesale_price = Db::getInstance()->ExecuteS(' + SELECT `wholesale_price`, p.`id_product` + FROM `'._DB_PREFIX_.'product` p + LEFT JOIN `'._DB_PREFIX_.'order_detail` o ON o.`product_id` = p.`id_product` + WHERE o.`id_order` = '. (int) $order['id_order'] + ); + $prix_achat = array(); + foreach ($wholesale_price as $key => $price) { + $prix_achat[$price['id_product']] = $price['wholesale_price']; + } + $total_achat = 0; + + + $ldetails = array(); + foreach($order_details as $detail) { + + $total_achat += $detail['product_quantity'] * $prix_achat[$detail['product_id']]; + + $ldetails[$detail['id_order_detail']] = $detail; + + if($address['id_country'] != 19) { + if($detail['tax_rate'] == '20.000') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_200[] = (float) ($detail['product_quantity_discount'] / 1.2); + $total_taxes_200[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.2); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_200[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_200[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.2); + } else { + $total_products_wo_taxes_200[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_200[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.2); + } + /*}*/ + } elseif($detail['tax_rate'] == '21.000') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_196[] = (float) ($detail['product_quantity_discount'] / 1.196); + $total_taxes_196[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.196); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_210[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_210[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.21); + } else { + $total_products_wo_taxes_210[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_210[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.21); + } + /*}*/ + }elseif($detail['tax_rate'] == '19.600') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_196[] = (float) ($detail['product_quantity_discount'] / 1.196); + $total_taxes_196[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.196); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_196[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_196[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.196); + } else { + $total_products_wo_taxes_196[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_196[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.196); + } + /*}*/ + } elseif($detail['tax_rate'] == '10.000') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_100[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_100[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.100); + } else { + $total_products_wo_taxes_100[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_100[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.100); + } + } elseif($detail['tax_rate'] == '5.500') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_55[] = (float) ($detail['product_quantity_discount'] / 1.055); + $total_taxes_55[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.055); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_55[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_55[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.055); + } else { + $total_products_wo_taxes_55[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_55[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.055); + } + /*}*/ + } elseif($detail['tax_rate'] == '2.100') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_21[] = (float) ($detail['product_quantity_discount'] / 1.021); + $total_taxes_21[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.021); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_21[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_21[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.021); + } else { + $total_products_wo_taxes_21[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_21[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.021); + } + /*}*/ + } elseif($detail['tax_rate'] == '6.000') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_21[] = (float) ($detail['product_quantity_discount'] / 1.021); + $total_taxes_21[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.021); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_60[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_60[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.060); + } else { + $total_products_wo_taxes_60[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_60[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.060); + } + /*}*/ + } elseif($detail['tax_rate'] == '4.000') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_21[] = (float) ($detail['product_quantity_discount'] / 1.021); + $total_taxes_21[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.021); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_40[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_40[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.040); + } else { + $total_products_wo_taxes_40[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_40[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.040); + } + /*}*/ + } else { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_quantity_discount']); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + } else { + $total_products_wo_taxes_export[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + } + /*}*/ + } + } else { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_quantity_discount']); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + } else { + $total_products_wo_taxes_export[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + } + /*}*/ + } + } + + $_total_products_wo_taxes_210 = 0.0; + foreach($total_products_wo_taxes_210 as $value) { + $_total_products_wo_taxes_210 += $value; + } + $_total_taxes_210 = 0.0; + foreach($total_taxes_210 as $value) { + $_total_taxes_210 += $value; + } + + $_total_products_wo_taxes_200 = 0.0; + foreach($total_products_wo_taxes_200 as $value) { + $_total_products_wo_taxes_200 += $value; + } + $_total_taxes_200 = 0.0; + foreach($total_taxes_200 as $value) { + $_total_taxes_200 += $value; + } + + $_total_products_wo_taxes_196 = 0.0; + foreach($total_products_wo_taxes_196 as $value) { + $_total_products_wo_taxes_196 += $value; + } + $_total_taxes_196 = 0.0; + foreach($total_taxes_196 as $value) { + $_total_taxes_196 += $value; + } + + $_total_products_wo_taxes_100 = 0.0; + foreach($total_products_wo_taxes_100 as $value) { + $_total_products_wo_taxes_100 += $value; + } + $_total_taxes_100 = 0.0; + foreach($total_taxes_100 as $value) { + $_total_taxes_100 += $value; + } + + $_total_products_wo_taxes_55 = 0.0; + foreach($total_products_wo_taxes_55 as $value) { + $_total_products_wo_taxes_55 += $value; + } + $_total_taxes_55 = 0.0; + foreach($total_taxes_55 as $value) { + $_total_taxes_55 += $value; + } + + $_total_products_wo_taxes_21 = 0.0; + foreach($total_products_wo_taxes_21 as $value) { + $_total_products_wo_taxes_21 += $value; + } + $_total_taxes_21 = 0.0; + foreach($total_taxes_21 as $value) { + $_total_taxes_21 += $value; + } + + $_total_products_wo_taxes_60 = 0.0; + foreach($total_products_wo_taxes_60 as $value) { + $_total_products_wo_taxes_60 += $value; + } + $_total_taxes_60 = 0.0; + foreach($total_taxes_60 as $value) { + $_total_taxes_60 += $value; + } + + $_total_products_wo_taxes_40 = 0.0; + foreach($total_products_wo_taxes_40 as $value) { + $_total_products_wo_taxes_40 += $value; + } + $_total_taxes_40 = 0.0; + foreach($total_taxes_40 as $value) { + $_total_taxes_40 += $value; + } + + $_total_products_wo_taxes_export = 0.0; + foreach($total_products_wo_taxes_export as $value) { + $_total_products_wo_taxes_export += $value; + } + + // Find if it's a saved card or account for the payment (Direct payment) + $is_direct_payment = 0; + if($order['payment'] == 'Paybox') { + $is_direct_payment = Db::getInstance()->getValue(' + SELECT `is_saved_card` + FROM `'._DB_PREFIX_.'paybox_transaction` + WHERE `id_cart` = '.(int) $order['id_cart'].' + '); + } elseif ($order['payment'] == 'PayPal') { + $is_direct_payment = Db::getInstance()->getValue(' + SELECT `is_billing` + FROM `'._DB_PREFIX_.'paypal_order` + WHERE `id_order` = '.(int) $order['id_order'].' + '); + } + if((int)$is_direct_payment != 0) { + $order['direct_payment'] = "Oui"; + } else { + $order['direct_payment'] = "Non"; + } + + fwrite($f, implode(';', array( + $order['date_add'], + //addslashes(implode(' / ', $sale_names)), + $order['id_order'], + 0, + $order['invoice_number'], + $multi, + $order['id_customer'], + $invoice_address['firstname'], + $invoice_address['lastname'], + $_total_products_wo_taxes_210, + $_total_taxes_210, + $_total_products_wo_taxes_200, + $_total_taxes_200, + $_total_products_wo_taxes_196, + $_total_taxes_196, + $_total_products_wo_taxes_100, + $_total_taxes_100, + $_total_products_wo_taxes_55, + $_total_taxes_55, + $_total_products_wo_taxes_21, + $_total_taxes_21, + $_total_products_wo_taxes_60, + $_total_taxes_60, + $_total_products_wo_taxes_40, + $_total_taxes_40, + $_total_products_wo_taxes_export, + ($address['id_country'] != 19? (float) $order['total_shipping'] / (strtotime($order['date_add']) >= mktime(0, 0, 0, 1, 1, 2014)? 1.2: 1.196): 0.0), + ($address['id_country'] != 19? 0.0: $order['total_shipping']), + $country, + $order['total_discounts'], + $order['total_paid'], + $order['total_paid_real'], + $total_achat, + $order['payment'], + $order['direct_payment'], + $date_send, + '', + (int) Db::getInstance()->getValue(' + SELECT `id_order` + FROM `'._DB_PREFIX_.'orders` + WHERE `id_customer` = '.(int) $order['id_customer'].' + AND `id_order` != '.(int) $order['id_order'].' + ') == 0, + ))."\n"); +} + +// This order has been refunded +$slips = Db::getInstance()->ExecuteS(' + SELECT * + FROM `'._DB_PREFIX_.'order_slip` os + WHERE os.date_add >= DATE_FORMAT("'.$now.'" - INTERVAL 1 MONTH, \'%Y-%m-01 00:00:00\') + AND os.date_add <= DATE_FORMAT(LAST_DAY("'.$now.'" - INTERVAL 1 MONTH), \'%Y-%m-%d 23:59:59\') +'); + +// $slips = Db::getInstance()->ExecuteS(' +// SELECT * +// FROM `'._DB_PREFIX_.'order_slip` os +// WHERE os.date_add >= "2014-12-01 00:00:00" +// AND os.date_add <= "2014-12-31 23:59:59" +// '); + +// $slips = Db::getInstance()->ExecuteS(' +// SELECT * +// FROM `'._DB_PREFIX_.'order_slip` os +// AND os.date_add >= "'. date('Y') .'-01-01 00:00:00" +// '); + + + +$refundreasons = array( + '', + 'CLIENT : Annulation pré-envoi', + 'CLIENT : Rétractation post-envoi', + 'BBB : Erreur Achat / Prod', + 'FEUR : Problème SAV', + 'FEUR : Produit manquant', + 'BBB : Erreur logistique', + 'Autre', + 'BBB : Pbme Site / Paiement', + 'TRANS : Colis détruit', + 'TRANS : Colis perdu', + 'CLIENT : Annulation pour ré-achat', + 'BBB : Suspicion de fraude', +); + +if(count($slips) > 0) { + foreach($slips as $slip) { + $order = Db::getInstance()->getRow(' + SELECT o.*, c.* + FROM `'._DB_PREFIX_.'orders` o + LEFT JOIN `'._DB_PREFIX_.'customer` c + ON o.`id_customer` = c.`id_customer` + WHERE + o.id_order = '.$slip['id_order'].' + '); + + $multi = Db::getInstance()->getValue(' + SELECT COUNT(DISTINCT `id_sale`) + FROM `'._DB_PREFIX_.'product_ps_cache` + WHERE `id_product` IN ( + SELECT `product_id` FROM `'._DB_PREFIX_.'order_detail` + WHERE `id_order` = '.(int) $order['id_order'].' + ) + ') == 1? 'S': 'M'; + + $address = Db::getInstance()->ExecuteS(' + SELECT a.*, c.`id_zone` + FROM `'._DB_PREFIX_.'address` a, `'._DB_PREFIX_.'country` c + WHERE a.`id_address` = '.$order['id_address_delivery'].' + AND a.`id_country` = c.`id_country` + '); + $address = $address[0]; + + $invoice_address = Db::getInstance()->ExecuteS(' + SELECT firstname, lastname + FROM `'._DB_PREFIX_.'address` + WHERE `id_address` = '.$order['id_address_invoice'] + ); + $invoice_address = $invoice_address[0]; + + $order_details = Db::getInstance()->ExecuteS(' + SELECT * + FROM `'._DB_PREFIX_.'order_detail` d + WHERE d.id_order = '.(int) $slip['id_order'].' + AND d.`id_order_detail` IN ( + SELECT `id_order_detail` + FROM `'._DB_PREFIX_.'order_slip_detail` + WHERE `id_order_slip` = '.(int) $slip['id_order_slip'].' + ) + '); + + $country = Db::getInstance()->getValue(' + SELECT `name` + FROM `'._DB_PREFIX_.'country_lang` + WHERE `id_country` = ( + SELECT `id_country` + FROM `'._DB_PREFIX_.'address` + WHERE `id_address` = '.(int) $order['id_address_delivery'].' + LIMIT 1 + ) + AND `id_lang` = 2 + '); + + $date_send = Db::getInstance()->getValue(' + SELECT `date_add` + FROM `'._DB_PREFIX_.'order_history` + WHERE `id_order_state` = 4 + AND `id_order` = '.(int) $order['id_order'].' + ORDER BY `date_add` ASC + '); + + // calcul prix d'achat de la commande + $wholesale_price_slip = Db::getInstance()->ExecuteS(' + SELECT `wholesale_price`, p.`id_product` + FROM `'._DB_PREFIX_.'product` p + LEFT JOIN `'._DB_PREFIX_.'order_detail` o ON o.`product_id` = p.`id_product` + WHERE o.`id_order` = '. (int) $slip['id_order'] + ); + + $prix_achat_slip = array(); + foreach ($wholesale_price_slip as $key => $price) { + $prix_achat_slip[$price['id_product']] = $price['wholesale_price']; + } + + $total_achat_slip = 0; + $ldetails = array(); + $total_products_wo_taxes_210 = array(); + $total_taxes_210 = array(); + $total_products_wo_taxes_200 = array(); + $total_taxes_200 = array(); + $total_products_wo_taxes_196 = array(); + $total_taxes_196 = array(); + $total_products_wo_taxes_100 = array(); + $total_taxes_100 = array(); + $total_products_wo_taxes_55 = array(); + $total_taxes_55 = array(); + $total_products_wo_taxes_21 = array(); + $total_taxes_21 = array(); + $total_products_wo_taxes_60 = array(); + $total_taxes_60 = array(); + $total_products_wo_taxes_40 = array(); + $total_taxes_40 = array(); + $total_products_wo_taxes_export = array(); + + foreach($order_details as $detail) { + $total_achat_slip += $detail['product_quantity'] * $prix_achat_slip[$detail['product_id']]; + /*$total_products_wo_taxes_196 = array(); + $total_taxes_196 = array(); + $total_products_wo_taxes_55 = array(); + $total_taxes_55 = array(); + $total_products_wo_taxes_21 = array(); + $total_taxes_21 = array(); + $total_products_wo_taxes_export = array();*/ + + $ldetails[$detail['id_order_detail']] = $detail; + + if($address['id_country'] != 19) { + if($detail['tax_rate'] == '19.600') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_196[] = (float) ($detail['product_quantity_discount'] / 1.196); + $total_taxes_196[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.196); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_196[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_196[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.196); + } else { + $total_products_wo_taxes_196[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_196[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.196); + } + /*}*/ + } elseif($detail['tax_rate'] == '10.000') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_100[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_100[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.100); + } else { + $total_products_wo_taxes_100[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_100[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.100); + } + } elseif($detail['tax_rate'] == '5.500') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_55[] = (float) ($detail['product_quantity_discount'] / 1.055); + $total_taxes_55[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.055); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_55[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_55[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.055); + } else { + $total_products_wo_taxes_55[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_55[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.055); + } + /*}*/ + } elseif($detail['tax_rate'] == '2.100') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_21[] = (float) ($detail['product_quantity_discount'] / 1.021); + $total_taxes_21[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.021); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_21[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_21[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.021); + } else { + $total_products_wo_taxes_21[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_21[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.021); + } + /*}*/ + } elseif($detail['tax_rate'] == '20.000') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_200[] = (float) ($detail['product_quantity_discount'] / 1.2); + $total_taxes_200[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.2); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_200[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_200[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.2); + } else { + $total_products_wo_taxes_200[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_200[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.2); + } + /*}*/ + } elseif($detail['tax_rate'] == '21.000') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_200[] = (float) ($detail['product_quantity_discount'] / 1.2); + $total_taxes_200[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.2); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_210[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_210[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.21); + } else { + $total_products_wo_taxes_210[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_210[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.21); + } + /*}*/ + } elseif($detail['tax_rate'] == '6.000') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_200[] = (float) ($detail['product_quantity_discount'] / 1.2); + $total_taxes_200[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.2); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_60[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_60[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.06); + } else { + $total_products_wo_taxes_60[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_60[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.06); + } + /*}*/ + } elseif($detail['tax_rate'] == '4.000') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_200[] = (float) ($detail['product_quantity_discount'] / 1.2); + $total_taxes_200[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.2); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_40[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_40[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.04); + } else { + $total_products_wo_taxes_40[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_40[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.04); + } + /*}*/ + } else { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_quantity_discount']); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + } else { + $total_products_wo_taxes_export[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + } + /*}*/ + } + } else { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_quantity_discount']); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + } else { + $total_products_wo_taxes_export[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + } + /*}*/ + } + } + + $_total_products_wo_taxes_210 = 0.0; + foreach($total_products_wo_taxes_210 as $value) { + $_total_products_wo_taxes_210 += $value; + } + $_total_taxes_210 = 0.0; + foreach($total_taxes_210 as $value) { + $_total_taxes_210 += $value; + } + + $_total_products_wo_taxes_200 = 0.0; + foreach($total_products_wo_taxes_200 as $value) { + $_total_products_wo_taxes_200 += $value; + } + $_total_taxes_200 = 0.0; + foreach($total_taxes_200 as $value) { + $_total_taxes_200 += $value; + } + + $_total_products_wo_taxes_196 = 0.0; + foreach($total_products_wo_taxes_196 as $value) { + $_total_products_wo_taxes_196 += $value; + } + $_total_taxes_196 = 0.0; + foreach($total_taxes_196 as $value) { + $_total_taxes_196 += $value; + } + + $_total_products_wo_taxes_100 = 0.0; + foreach($total_products_wo_taxes_100 as $value) { + $_total_products_wo_taxes_100 += $value; + } + $_total_taxes_100 = 0.0; + foreach($total_taxes_100 as $value) { + $_total_taxes_100 += $value; + } + + $_total_products_wo_taxes_55 = 0.0; + foreach($total_products_wo_taxes_55 as $value) { + $_total_products_wo_taxes_55 += $value; + } + $_total_taxes_55 = 0.0; + foreach($total_taxes_55 as $value) { + $_total_taxes_55 += $value; + } + + $_total_products_wo_taxes_21 = 0.0; + foreach($total_products_wo_taxes_21 as $value) { + $_total_products_wo_taxes_21 += $value; + } + $_total_taxes_21 = 0.0; + foreach($total_taxes_21 as $value) { + $_total_taxes_21 += $value; + } + + $_total_products_wo_taxes_60 = 0.0; + foreach($total_products_wo_taxes_60 as $value) { + $_total_products_wo_taxes_60 += $value; + } + $_total_taxes_60 = 0.0; + foreach($total_taxes_60 as $value) { + $_total_taxes_60 += $value; + } + + $_total_products_wo_taxes_40 = 0.0; + foreach($total_products_wo_taxes_40 as $value) { + $_total_products_wo_taxes_40 += $value; + } + $_total_taxes_40 = 0.0; + foreach($total_taxes_40 as $value) { + $_total_taxes_40 += $value; + } + + $_total_products_wo_taxes_export = 0.0; + foreach($total_products_wo_taxes_export as $value) { + $_total_products_wo_taxes_export += $value; + } + + // Find if it's a saved card or account for the payment (Direct payment) + $is_direct_payment = 0; + if($order['payment'] == 'Paybox') { + $is_direct_payment = Db::getInstance()->getValue(' + SELECT `is_saved_card` + FROM `'._DB_PREFIX_.'paybox_transaction` + WHERE `id_cart` = '.(int) $order['id_cart'].' + '); + } elseif ($order['payment'] == 'PayPal') { + $is_direct_payment = Db::getInstance()->getValue(' + SELECT `is_billing` + FROM `'._DB_PREFIX_.'paypal_order` + WHERE `id_order` = '.(int) $order['id_order'].' + '); + } + if((int)$is_direct_payment != 0) { + $order['direct_payment'] = "Oui"; + } else { + $order['direct_payment'] = "Non"; + } + + $refundreason = Db::getInstance()->getValue(' + SELECT `id_reason` + FROM `'._DB_PREFIX_.'refundreason` + WHERE `id_order_slip` = '.(int) $slip['id_order_slip'].' + '); + + $refundreason = $refundreasons[(int) $refundreason]; + + fwrite($f, implode(';', array( + $slip['date_add'], + //addslashes(implode(' / ', $sale_names)), + $order['id_order'], + $slip['id_order_slip'], + $order['invoice_number'], + $multi, + $order['id_customer'], + $invoice_address['firstname'], + $invoice_address['lastname'], + '-'.$_total_products_wo_taxes_210, + '-'.$_total_taxes_210, + '-'.$_total_products_wo_taxes_200, + '-'.$_total_taxes_200, + '-'.$_total_products_wo_taxes_196, + '-'.$_total_taxes_196, + '-'.$_total_products_wo_taxes_100, + '-'.$_total_taxes_100, + '-'.$_total_products_wo_taxes_55, + '-'.$_total_taxes_55, + '-'.$_total_products_wo_taxes_21, + '-'.$_total_taxes_21, + '-'.$_total_products_wo_taxes_60, + '-'.$_total_taxes_60, + '-'.$_total_products_wo_taxes_40, + '-'.$_total_taxes_40, + '-'.$_total_products_wo_taxes_export, + '-'.($slip['shipping_cost'] == 1? ($address['id_country'] != 19? (float) $order['total_shipping'] / (strtotime($order['date_add']) >= mktime(0, 0, 0, 1, 1, 2014)? 1.2: 1.196): 0.0): 0.0), + '-'.($slip['shipping_cost'] == 1? ($address['id_country'] != 19? 0.0: $order['total_shipping']): 0.0), + $country, + '-0.0', + '-'.($_total_products_wo_taxes_210 + $_total_taxes_210 + $_total_products_wo_taxes_200 + $_total_taxes_200 + $_total_products_wo_taxes_196 + $_total_taxes_196 + $_total_products_wo_taxes_100 + $_total_taxes_100 + $_total_products_wo_taxes_55 + $_total_taxes_55 + $_total_products_wo_taxes_21 + $_total_taxes_21 + $_total_products_wo_taxes_60 + $_total_taxes_60 + $_total_products_wo_taxes_40 + $_total_taxes_40 + $_total_products_wo_taxes_export + ($slip['shipping_cost'] == 1? (float) $order['total_shipping']: 0.0)), + '-'.($_total_products_wo_taxes_210 + $_total_taxes_210 + $_total_products_wo_taxes_200 + $_total_taxes_200 + $_total_products_wo_taxes_196 + $_total_taxes_196 + $_total_products_wo_taxes_100 + $_total_taxes_100 + $_total_products_wo_taxes_55 + $_total_taxes_55 + $_total_products_wo_taxes_21 + $_total_taxes_21 + $_total_products_wo_taxes_60 + $_total_taxes_60 + $_total_products_wo_taxes_40 + $_total_taxes_40 + $_total_products_wo_taxes_export + ($slip['shipping_cost'] == 1? (float) $order['total_shipping']: 0.0)), + $total_achat_slip, + 'REFUND '.strtoupper($order['payment']), + $order['direct_payment'], + $date_send, + $refundreason, + '', + ))."\n"); + } +} + +fclose($f); + +exit; diff --git a/cron_export_sales_monthly_new.php b/cron_export_sales_monthly_new.php new file mode 100644 index 0000000..058d9e9 --- /dev/null +++ b/cron_export_sales_monthly_new.php @@ -0,0 +1,889 @@ +ExecuteS(' + SELECT id_parent, id_category + FROM `'._DB_PREFIX_.'category` + WHERE `id_parent` = '.$id + ); +} + +function getCat($id=0, $title='') { + $products = Db::getInstance()->ExecuteS(' + SELECT id_product, "'.addslashes($title).'" AS `title` + FROM `'._DB_PREFIX_.'product` + WHERE `id_category_default` = '.$id + ); + foreach(getChildren($id) as $child) { + $products = array_merge($products, getCat($child['id_category'])); + } + return $products; +} + + +$f = fopen('extracts/monthly_compta/'.date('Y-m-d', strtotime($now)).'_new.csv', 'w'); +//$f = fopen('extracts/monthly_compta/2012-01-01_ok2.csv', 'w'); + +$headers = array( + 'date', + //'sale_title', + 'id_order', + 'id_order_slip', + 'invoice_number', + 'multisale', + 'id_customer', + 'firstname', + 'lastname', + 'total_products_wo_taxes_210', + 'total_taxes_210', + 'total_products_wo_taxes_200', + 'total_taxes_200', + 'total_products_wo_taxes_196', + 'total_taxes_196', + 'total_products_wo_taxes_100', + 'total_taxes_100', + 'total_products_wo_taxes_55', + 'total_taxes_55', + 'total_products_wo_taxes_21', + 'total_taxes_21', + 'total_products_wo_taxes_60', + 'total_taxes_60', + 'total_products_wo_taxes_40', + 'total_taxes_40', + 'total_products_wo_taxes_export', + 'shipping_wo_taxes_196-200', + 'shipping_wo_taxes_export', + 'shipping_country', + 'discounts', + 'total_paid', + 'total_paid_real', + 'wholesale_price', + 'payment_type', + 'direct_payment', + //'date_send', + 'refund_reason', + 'date_subsribe', + 'pays', + //'new', +); + +fwrite($f, implode(';', $headers)."\n"); + + +$orders = Db::getInstance()->ExecuteS(' + SELECT o.id_order + FROM `'._DB_PREFIX_.'orders` o, + `'._DB_PREFIX_.'order_detail` d, + `'._DB_PREFIX_.'customer` c + WHERE + (o.valid = 1 + OR ( + o.valid = 0 + AND ( + (SELECT h.id_order_state FROM `'._DB_PREFIX_.'order_history` h WHERE h.id_order = o.id_order ORDER BY h.date_add DESC LIMIT 1) = 6 + OR (SELECT h.id_order_state FROM `'._DB_PREFIX_.'order_history` h WHERE h.id_order = o.id_order ORDER BY h.date_add DESC LIMIT 1) = 7 + OR (SELECT h.id_order_state FROM `'._DB_PREFIX_.'order_history` h WHERE h.id_order = o.id_order ORDER BY h.date_add DESC LIMIT 1) = 11 + ) + )) + AND c.id_customer = o.id_customer + AND d.id_order = o.id_order + AND o.date_add >= DATE_FORMAT("'.$now.'" - INTERVAL 1 MONTH, \'%Y-%m-01 00:00:00\') + AND o.date_add <= DATE_FORMAT(LAST_DAY("'.$now.'" - INTERVAL 1 MONTH), \'%Y-%m-%d 23:59:59\') + GROUP BY o.id_order +'); + +// $orders = Db::getInstance()->ExecuteS(' +// SELECT o.id_order +// FROM `'._DB_PREFIX_.'orders` o, +// `'._DB_PREFIX_.'order_detail` d, +// `'._DB_PREFIX_.'customer` c +// WHERE +// (o.valid = 1 +// OR ( +// o.valid = 0 +// AND ( +// (SELECT h.id_order_state FROM `'._DB_PREFIX_.'order_history` h WHERE h.id_order = o.id_order ORDER BY h.date_add DESC LIMIT 1) = 6 +// OR (SELECT h.id_order_state FROM `'._DB_PREFIX_.'order_history` h WHERE h.id_order = o.id_order ORDER BY h.date_add DESC LIMIT 1) = 7 +// OR (SELECT h.id_order_state FROM `'._DB_PREFIX_.'order_history` h WHERE h.id_order = o.id_order ORDER BY h.date_add DESC LIMIT 1) = 11 +// ) +// )) +// AND c.id_customer = o.id_customer +// AND d.id_order = o.id_order +// AND o.date_add >= "'. date('Y') .'-01-01 00:00:00" +// GROUP BY o.id_order +// '); + + + +foreach($orders as $o) { + $order = Db::getInstance()->ExecuteS(' + SELECT c.*, c.`date_add` as subscribe, o.*, v.`version` + FROM `'._DB_PREFIX_.'orders` o + LEFT JOIN `'._DB_PREFIX_.'customer` c ON c.`id_customer` = o.`id_customer` + LEFT JOIN `'._DB_PREFIX_.'customer_version` v ON v.`id_customer` = c.`id_customer` + WHERE o.id_order = '.$o['id_order'].' + '); + $order = $order[0]; + + $multi = Db::getInstance()->getValue(' + SELECT COUNT(DISTINCT `id_sale`) + FROM `'._DB_PREFIX_.'product_ps_cache` + WHERE `id_product` IN ( + SELECT `product_id` FROM `'._DB_PREFIX_.'order_detail` + WHERE `id_order` = '.(int) $order['id_order'].' + ) + ') == 1? 'S': 'M'; + + $address = Db::getInstance()->ExecuteS(' + SELECT a.*, c.`id_zone` + FROM `'._DB_PREFIX_.'address` a, `'._DB_PREFIX_.'country` c + WHERE a.`id_address` = '.$order['id_address_delivery'].' + AND a.`id_country` = c.`id_country` + '); + $address = $address[0]; + + $invoice_address = Db::getInstance()->ExecuteS(' + SELECT firstname, lastname + FROM `'._DB_PREFIX_.'address` + WHERE `id_address` = '.$order['id_address_invoice'] + ); + $invoice_address = $invoice_address[0]; + + $order_details = Db::getInstance()->ExecuteS(' + SELECT d.*, p.`wholesale_price` + FROM `'._DB_PREFIX_.'order_detail` d + LEFT JOIN `'._DB_PREFIX_.'product` p ON (p.`id_product` = d.`product_id`) + WHERE d.id_order = '.$o['id_order'] + ); + + $total_products_wo_taxes_210 = array(); + $total_taxes_210 = array(); + $total_products_wo_taxes_200 = array(); + $total_taxes_200 = array(); + $total_products_wo_taxes_196 = array(); + $total_taxes_196 = array(); + $total_products_wo_taxes_100 = array(); + $total_taxes_100 = array(); + $total_products_wo_taxes_55 = array(); + $total_taxes_55 = array(); + $total_products_wo_taxes_21 = array(); + $total_taxes_21 = array(); + $total_products_wo_taxes_60 = array(); + $total_taxes_60 = array(); + $total_products_wo_taxes_40 = array(); + $total_taxes_40 = array(); + $total_products_wo_taxes_export = array(); + + $country = Db::getInstance()->getValue(' + SELECT `name` + FROM `'._DB_PREFIX_.'country_lang` + WHERE `id_country` = ( + SELECT `id_country` + FROM `'._DB_PREFIX_.'address` + WHERE `id_address` = '.(int) $order['id_address_delivery'].' + LIMIT 1 + ) + AND `id_lang` = 2 + '); + + $date_send = Db::getInstance()->getValue(' + SELECT `date_add` + FROM `'._DB_PREFIX_.'order_history` + WHERE `id_order_state` = 4 + AND `id_order` = '.(int) $order['id_order'].' + ORDER BY `date_add` ASC + '); + + $ldetails = array(); + $wholesale_price = 0; + foreach($order_details as $detail) { + $ldetails[$detail['id_order_detail']] = $detail; + $wholesale_price += (float) ($detail['wholesale_price'] * $detail['product_quantity']); + if($address['id_country'] != 19) { + if($detail['tax_rate'] == '20.000') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_200[] = (float) ($detail['product_quantity_discount'] / 1.2); + $total_taxes_200[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.2); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_200[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_200[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.2); + } else { + $total_products_wo_taxes_200[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_200[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.2); + } + /*}*/ + } elseif($detail['tax_rate'] == '21.000') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_196[] = (float) ($detail['product_quantity_discount'] / 1.196); + $total_taxes_196[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.196); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_210[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_210[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.21); + } else { + $total_products_wo_taxes_210[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_210[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.21); + } + /*}*/ + } elseif($detail['tax_rate'] == '19.600') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_196[] = (float) ($detail['product_quantity_discount'] / 1.196); + $total_taxes_196[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.196); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_196[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_196[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.196); + } else { + $total_products_wo_taxes_196[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_196[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.196); + } + /*}*/ + } elseif($detail['tax_rate'] == '10.000') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_100[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_100[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.100); + } else { + $total_products_wo_taxes_100[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_100[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.100); + } + } elseif($detail['tax_rate'] == '5.500') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_55[] = (float) ($detail['product_quantity_discount'] / 1.055); + $total_taxes_55[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.055); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_55[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_55[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.055); + } else { + $total_products_wo_taxes_55[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_55[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.055); + } + /*}*/ + } elseif($detail['tax_rate'] == '2.100') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_21[] = (float) ($detail['product_quantity_discount'] / 1.021); + $total_taxes_21[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.021); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_21[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_21[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.021); + } else { + $total_products_wo_taxes_21[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_21[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.021); + } + /*}*/ + } elseif($detail['tax_rate'] == '6.000') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_21[] = (float) ($detail['product_quantity_discount'] / 1.021); + $total_taxes_21[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.021); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_60[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_60[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.060); + } else { + $total_products_wo_taxes_60[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_60[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.060); + } + /*}*/ + } elseif($detail['tax_rate'] == '4.000') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_21[] = (float) ($detail['product_quantity_discount'] / 1.021); + $total_taxes_21[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.021); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_40[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_40[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.040); + } else { + $total_products_wo_taxes_40[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_40[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.040); + } + /*}*/ + } else { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_quantity_discount']); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + } else { + $total_products_wo_taxes_export[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + } + /*}*/ + } + } else { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_quantity_discount']); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + } else { + $total_products_wo_taxes_export[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + } + /*}*/ + } + } + + $_total_products_wo_taxes_210 = 0.0; + foreach($total_products_wo_taxes_210 as $value) { + $_total_products_wo_taxes_210 += $value; + } + $_total_taxes_210 = 0.0; + foreach($total_taxes_210 as $value) { + $_total_taxes_210 += $value; + } + + $_total_products_wo_taxes_200 = 0.0; + foreach($total_products_wo_taxes_200 as $value) { + $_total_products_wo_taxes_200 += $value; + } + $_total_taxes_200 = 0.0; + foreach($total_taxes_200 as $value) { + $_total_taxes_200 += $value; + } + + $_total_products_wo_taxes_196 = 0.0; + foreach($total_products_wo_taxes_196 as $value) { + $_total_products_wo_taxes_196 += $value; + } + $_total_taxes_196 = 0.0; + foreach($total_taxes_196 as $value) { + $_total_taxes_196 += $value; + } + + $_total_products_wo_taxes_100 = 0.0; + foreach($total_products_wo_taxes_100 as $value) { + $_total_products_wo_taxes_100 += $value; + } + $_total_taxes_100 = 0.0; + foreach($total_taxes_100 as $value) { + $_total_taxes_100 += $value; + } + + $_total_products_wo_taxes_55 = 0.0; + foreach($total_products_wo_taxes_55 as $value) { + $_total_products_wo_taxes_55 += $value; + } + $_total_taxes_55 = 0.0; + foreach($total_taxes_55 as $value) { + $_total_taxes_55 += $value; + } + + $_total_products_wo_taxes_21 = 0.0; + foreach($total_products_wo_taxes_21 as $value) { + $_total_products_wo_taxes_21 += $value; + } + $_total_taxes_21 = 0.0; + foreach($total_taxes_21 as $value) { + $_total_taxes_21 += $value; + } + + $_total_products_wo_taxes_60 = 0.0; + foreach($total_products_wo_taxes_60 as $value) { + $_total_products_wo_taxes_60 += $value; + } + $_total_taxes_60 = 0.0; + foreach($total_taxes_60 as $value) { + $_total_taxes_60 += $value; + } + + $_total_products_wo_taxes_40 = 0.0; + foreach($total_products_wo_taxes_40 as $value) { + $_total_products_wo_taxes_40 += $value; + } + $_total_taxes_40 = 0.0; + foreach($total_taxes_40 as $value) { + $_total_taxes_40 += $value; + } + + $_total_products_wo_taxes_export = 0.0; + foreach($total_products_wo_taxes_export as $value) { + $_total_products_wo_taxes_export += $value; + } + + // Find if it's a saved card or account for the payment (Direct payment) + $is_direct_payment = 0; + if($order['payment'] == 'Paybox') { + $is_direct_payment = Db::getInstance()->getValue(' + SELECT `is_saved_card` + FROM `'._DB_PREFIX_.'paybox_transaction` + WHERE `id_cart` = '.(int) $order['id_cart'].' + '); + } elseif ($order['payment'] == 'PayPal') { + $is_direct_payment = Db::getInstance()->getValue(' + SELECT `is_billing` + FROM `'._DB_PREFIX_.'paypal_order` + WHERE `id_order` = '.(int) $order['id_order'].' + '); + } + if((int)$is_direct_payment != 0) { + $order['direct_payment'] = "Oui"; + } else { + $order['direct_payment'] = "Non"; + } + + fwrite($f, implode(';', array( + $order['date_add'], + //addslashes(implode(' / ', $sale_names)), + $order['id_order'], + 0, + $order['invoice_number'], + $multi, + $order['id_customer'], + $invoice_address['firstname'], + $invoice_address['lastname'], + $_total_products_wo_taxes_210, + $_total_taxes_210, + $_total_products_wo_taxes_200, + $_total_taxes_200, + $_total_products_wo_taxes_196, + $_total_taxes_196, + $_total_products_wo_taxes_100, + $_total_taxes_100, + $_total_products_wo_taxes_55, + $_total_taxes_55, + $_total_products_wo_taxes_21, + $_total_taxes_21, + $_total_products_wo_taxes_60, + $_total_taxes_60, + $_total_products_wo_taxes_40, + $_total_taxes_40, + $_total_products_wo_taxes_export, + ($address['id_country'] != 19? (float) $order['total_shipping'] / (strtotime($order['date_add']) >= mktime(0, 0, 0, 1, 1, 2014)? 1.2: 1.196): 0.0), + ($address['id_country'] != 19? 0.0: $order['total_shipping']), + $country, + $order['total_discounts'], + $order['total_paid'], + $order['total_paid_real'], + $wholesale_price, + $order['payment'], + $order['direct_payment'], + //$date_send, + '', + $order['subscribe'], + $order['version'] + // (int) Db::getInstance()->getValue(' + // SELECT `id_order` + // FROM `'._DB_PREFIX_.'orders` + // WHERE `id_customer` = '.(int) $order['id_customer'].' + // AND `id_order` != '.(int) $order['id_order'].' + // ') == 0, + ))."\n"); +} + +// This order has been refunded +$slips = Db::getInstance()->ExecuteS(' + SELECT * + FROM `'._DB_PREFIX_.'order_slip` os + WHERE os.date_add >= DATE_FORMAT("'.$now.'" - INTERVAL 1 MONTH, \'%Y-%m-01 00:00:00\') + AND os.date_add <= DATE_FORMAT(LAST_DAY("'.$now.'" - INTERVAL 1 MONTH), \'%Y-%m-%d 23:59:59\') +'); + +// $slips = Db::getInstance()->ExecuteS(' +// SELECT * +// FROM `'._DB_PREFIX_.'order_slip` os +// AND os.date_add >= "'. date('Y') .'-01-01 00:00:00" +// '); + + + +$refundreasons = array( + '', + 'CLIENT : Annulation pré-envoi', + 'CLIENT : Rétractation post-envoi', + 'BBB : Erreur Achat / Prod', + 'FEUR : Problème SAV', + 'FEUR : Produit manquant', + 'BBB : Erreur logistique', + 'Autre', + 'BBB : Pbme Site / Paiement', + 'TRANS : Colis détruit', + 'TRANS : Colis perdu', + 'CLIENT : Annulation pour ré-achat', + 'BBB : Suspicion de fraude', +); + +if(count($slips) > 0) { + foreach($slips as $slip) { + $order = Db::getInstance()->getRow(' + SELECT o.*, c.*, c.`date_add` as subscribe, v.`version` + FROM `'._DB_PREFIX_.'orders` o + LEFT JOIN `'._DB_PREFIX_.'customer` c + ON o.`id_customer` = c.`id_customer` + LEFT JOIN `'._DB_PREFIX_.'customer_version` v + ON c.`id_customer` = v.`id_customer` + WHERE + o.id_order = '.$slip['id_order'].' + '); + + $multi = Db::getInstance()->getValue(' + SELECT COUNT(DISTINCT `id_sale`) + FROM `'._DB_PREFIX_.'product_ps_cache` + WHERE `id_product` IN ( + SELECT `product_id` FROM `'._DB_PREFIX_.'order_detail` + WHERE `id_order` = '.(int) $order['id_order'].' + ) + ') == 1? 'S': 'M'; + + $address = Db::getInstance()->ExecuteS(' + SELECT a.*, c.`id_zone` + FROM `'._DB_PREFIX_.'address` a, `'._DB_PREFIX_.'country` c + WHERE a.`id_address` = '.$order['id_address_delivery'].' + AND a.`id_country` = c.`id_country` + '); + $address = $address[0]; + + $invoice_address = Db::getInstance()->ExecuteS(' + SELECT firstname, lastname + FROM `'._DB_PREFIX_.'address` + WHERE `id_address` = '.$order['id_address_invoice'] + ); + $invoice_address = $invoice_address[0]; + + $order_details = Db::getInstance()->ExecuteS(' + SELECT d.*, p.`wholesale_price` + FROM `'._DB_PREFIX_.'order_detail` d + LEFT JOIN `'._DB_PREFIX_.'product` p ON (p.`id_product` = d.`product_id`) + WHERE d.id_order = '.(int) $slip['id_order'].' + AND d.`id_order_detail` IN ( + SELECT `id_order_detail` + FROM `'._DB_PREFIX_.'order_slip_detail` + WHERE `id_order_slip` = '.(int) $slip['id_order_slip'].' + ) + '); + + $country = Db::getInstance()->getValue(' + SELECT `name` + FROM `'._DB_PREFIX_.'country_lang` + WHERE `id_country` = ( + SELECT `id_country` + FROM `'._DB_PREFIX_.'address` + WHERE `id_address` = '.(int) $order['id_address_delivery'].' + LIMIT 1 + ) + AND `id_lang` = 2 + '); + + $date_send = Db::getInstance()->getValue(' + SELECT `date_add` + FROM `'._DB_PREFIX_.'order_history` + WHERE `id_order_state` = 4 + AND `id_order` = '.(int) $order['id_order'].' + ORDER BY `date_add` ASC + '); + + $ldetails = array(); + $total_products_wo_taxes_210 = array(); + $total_taxes_210 = array(); + $total_products_wo_taxes_200 = array(); + $total_taxes_200 = array(); + $total_products_wo_taxes_196 = array(); + $total_taxes_196 = array(); + $total_products_wo_taxes_100 = array(); + $total_taxes_100 = array(); + $total_products_wo_taxes_55 = array(); + $total_taxes_55 = array(); + $total_products_wo_taxes_21 = array(); + $total_taxes_21 = array(); + $total_products_wo_taxes_60 = array(); + $total_taxes_60 = array(); + $total_products_wo_taxes_40 = array(); + $total_taxes_40 = array(); + $total_products_wo_taxes_export = array(); + $wholesale_price = 0; + foreach($order_details as $detail) { + /*$total_products_wo_taxes_196 = array(); + $total_taxes_196 = array(); + $total_products_wo_taxes_55 = array(); + $total_taxes_55 = array(); + $total_products_wo_taxes_21 = array(); + $total_taxes_21 = array(); + $total_products_wo_taxes_export = array();*/ + + $ldetails[$detail['id_order_detail']] = $detail; + $wholesale_price += (float) ($detail['wholesale_price'] * $detail['product_quantity_refunded']); + + if($address['id_country'] != 19) { + if($detail['tax_rate'] == '19.600') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_196[] = (float) ($detail['product_quantity_discount'] / 1.196); + $total_taxes_196[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.196); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_196[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_196[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.196); + } else { + $total_products_wo_taxes_196[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_196[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.196); + } + /*}*/ + } elseif($detail['tax_rate'] == '21.000') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_55[] = (float) ($detail['product_quantity_discount'] / 1.055); + $total_taxes_55[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.055); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_210[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_210[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.21); + } else { + $total_products_wo_taxes_210[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_210[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.21); + } + /*}*/ + } elseif($detail['tax_rate'] == '10.000') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_100[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_100[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.100); + } else { + $total_products_wo_taxes_100[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_100[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.100); + } + } elseif($detail['tax_rate'] == '5.500') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_55[] = (float) ($detail['product_quantity_discount'] / 1.055); + $total_taxes_55[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.055); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_55[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_55[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.055); + } else { + $total_products_wo_taxes_55[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_55[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.055); + } + /*}*/ + } elseif($detail['tax_rate'] == '2.100') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_21[] = (float) ($detail['product_quantity_discount'] / 1.021); + $total_taxes_21[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.021); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_21[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_21[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.021); + } else { + $total_products_wo_taxes_21[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_21[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.021); + } + /*}*/ + } elseif($detail['tax_rate'] == '20.000') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_200[] = (float) ($detail['product_quantity_discount'] / 1.2); + $total_taxes_200[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.2); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_200[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_200[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.2); + } else { + $total_products_wo_taxes_200[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_200[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.2); + } + /*}*/ + } elseif($detail['tax_rate'] == '6.000') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_60[] = (float) ($detail['product_quantity_discount'] / 1.2); + $total_taxes_60[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.2); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_60[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_60[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.06); + } else { + $total_products_wo_taxes_60[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_60[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.06); + } + /*}*/ + } elseif($detail['tax_rate'] == '4.000') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_40[] = (float) ($detail['product_quantity_discount'] / 1.2); + $total_taxes_40[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.2); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_40[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_40[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.04); + } else { + $total_products_wo_taxes_40[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_40[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.04); + } + /*}*/ + } else { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_quantity_discount']); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + } else { + $total_products_wo_taxes_export[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + } + /*}*/ + } + } else { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_quantity_discount']); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + } else { + $total_products_wo_taxes_export[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + } + /*}*/ + } + } + + $_total_products_wo_taxes_210 = 0.0; + foreach($total_products_wo_taxes_210 as $value) { + $_total_products_wo_taxes_210 += $value; + } + $_total_taxes_210 = 0.0; + foreach($total_taxes_210 as $value) { + $_total_taxes_210 += $value; + } + + $_total_products_wo_taxes_200 = 0.0; + foreach($total_products_wo_taxes_200 as $value) { + $_total_products_wo_taxes_200 += $value; + } + $_total_taxes_200 = 0.0; + foreach($total_taxes_200 as $value) { + $_total_taxes_200 += $value; + } + + $_total_products_wo_taxes_196 = 0.0; + foreach($total_products_wo_taxes_196 as $value) { + $_total_products_wo_taxes_196 += $value; + } + $_total_taxes_196 = 0.0; + foreach($total_taxes_196 as $value) { + $_total_taxes_196 += $value; + } + + $_total_products_wo_taxes_100 = 0.0; + foreach($total_products_wo_taxes_100 as $value) { + $_total_products_wo_taxes_100 += $value; + } + $_total_taxes_100 = 0.0; + foreach($total_taxes_100 as $value) { + $_total_taxes_100 += $value; + } + + $_total_products_wo_taxes_55 = 0.0; + foreach($total_products_wo_taxes_55 as $value) { + $_total_products_wo_taxes_55 += $value; + } + $_total_taxes_55 = 0.0; + foreach($total_taxes_55 as $value) { + $_total_taxes_55 += $value; + } + + $_total_products_wo_taxes_21 = 0.0; + foreach($total_products_wo_taxes_21 as $value) { + $_total_products_wo_taxes_21 += $value; + } + $_total_taxes_21 = 0.0; + foreach($total_taxes_21 as $value) { + $_total_taxes_21 += $value; + } + + $_total_products_wo_taxes_60 = 0.0; + foreach($total_products_wo_taxes_60 as $value) { + $_total_products_wo_taxes_60 += $value; + } + $_total_taxes_60 = 0.0; + foreach($total_taxes_60 as $value) { + $_total_taxes_60 += $value; + } + + $_total_products_wo_taxes_40 = 0.0; + foreach($total_products_wo_taxes_40 as $value) { + $_total_products_wo_taxes_40 += $value; + } + $_total_taxes_40 = 0.0; + foreach($total_taxes_40 as $value) { + $_total_taxes_40 += $value; + } + + $_total_products_wo_taxes_export = 0.0; + foreach($total_products_wo_taxes_export as $value) { + $_total_products_wo_taxes_export += $value; + } + + $refundreason = Db::getInstance()->getValue(' + SELECT `id_reason` + FROM `'._DB_PREFIX_.'refundreason` + WHERE `id_order_slip` = '.(int) $slip['id_order_slip'].' + '); + + $refundreason = $refundreasons[(int) $refundreason]; + + // Find if it's a saved card or account for the payment (Direct payment) + $is_direct_payment = 0; + if($order['payment'] == 'Paybox') { + $is_direct_payment = Db::getInstance()->getValue(' + SELECT `is_saved_card` + FROM `'._DB_PREFIX_.'paybox_transaction` + WHERE `id_cart` = '.(int) $order['id_cart'].' + '); + } elseif ($order['payment'] == 'PayPal') { + $is_direct_payment = Db::getInstance()->getValue(' + SELECT `is_billing` + FROM `'._DB_PREFIX_.'paypal_order` + WHERE `id_order` = '.(int) $order['id_order'].' + '); + } + if((int)$is_direct_payment != 0) { + $order['direct_payment'] = "Oui"; + } else { + $order['direct_payment'] = "Non"; + } + + fwrite($f, implode(';', array( + $slip['date_add'], + //addslashes(implode(' / ', $sale_names)), + $order['id_order'], + $slip['id_order_slip'], + $order['invoice_number'], + $multi, + $order['id_customer'], + $invoice_address['firstname'], + $invoice_address['lastname'], + '-'.$_total_products_wo_taxes_210, + '-'.$_total_taxes_210, + '-'.$_total_products_wo_taxes_200, + '-'.$_total_taxes_200, + '-'.$_total_products_wo_taxes_196, + '-'.$_total_taxes_196, + '-'.$_total_products_wo_taxes_100, + '-'.$_total_taxes_100, + '-'.$_total_products_wo_taxes_55, + '-'.$_total_taxes_55, + '-'.$_total_products_wo_taxes_21, + '-'.$_total_taxes_21, + '-'.$_total_products_wo_taxes_60, + '-'.$_total_taxes_60, + '-'.$_total_products_wo_taxes_40, + '-'.$_total_taxes_40, + '-'.$_total_products_wo_taxes_export, + '-'.($slip['shipping_cost'] == 1? ($address['id_country'] != 19? (float) $order['total_shipping'] / (strtotime($order['date_add']) >= mktime(0, 0, 0, 1, 1, 2014)? 1.2: 1.196): 0.0): 0.0), + '-'.($slip['shipping_cost'] == 1? ($address['id_country'] != 19? 0.0: $order['total_shipping']): 0.0), + $country, + '-0.0', + '-'.($_total_products_wo_taxes_210 + $_total_taxes_210 + $_total_products_wo_taxes_200 + $_total_taxes_200 + $_total_products_wo_taxes_196 + $_total_taxes_196 + $_total_products_wo_taxes_100 + $_total_taxes_100 + $_total_products_wo_taxes_55 + $_total_taxes_55 + $_total_products_wo_taxes_21 + $_total_taxes_21 + $_total_products_wo_taxes_60 + $_total_taxes_60 + $_total_products_wo_taxes_40 + $_total_taxes_40 + $_total_products_wo_taxes_export + ($slip['shipping_cost'] == 1? (float) $order['total_shipping']: 0.0)), + '-'.($_total_products_wo_taxes_210 + $_total_taxes_210 + $_total_products_wo_taxes_200 + $_total_taxes_200 + $_total_products_wo_taxes_196 + $_total_taxes_196 + $_total_products_wo_taxes_100 + $_total_taxes_100 + $_total_products_wo_taxes_55 + $_total_taxes_55 + $_total_products_wo_taxes_21 + $_total_taxes_21 + $_total_products_wo_taxes_60 + $_total_taxes_60 + $_total_products_wo_taxes_40 + $_total_taxes_40 + $_total_products_wo_taxes_export + ($slip['shipping_cost'] == 1? (float) $order['total_shipping']: 0.0)), + '-'.$wholesale_price, + 'REFUND '.strtoupper($order['payment']), + $order['direct_payment'], + //$date_send, + $refundreason, + $order['subscribe'], + $order['version'], + //'', + ))."\n"); + } +} + +fclose($f); + +exit; diff --git a/cron_loyalty.php b/cron_loyalty.php new file mode 100644 index 0000000..01886b2 --- /dev/null +++ b/cron_loyalty.php @@ -0,0 +1,37 @@ +ExecuteS(' + SELECT lo.`id_order`, oh.`id_order_state` + FROM `'._DB_PREFIX_.'loyalty` lo + LEFT JOIN `'._DB_PREFIX_.'order_history` oh ON (oh.`id_order` = lo.`id_order`) + WHERE oh.`id_order_history` = (SELECT MAX(`id_order_history`) FROM `'._DB_PREFIX_.'order_history` moh WHERE moh.`id_order` = lo.`id_order` GROUP BY moh.`id_order`) + AND oh.`id_order_state` = 4 AND lo.`id_loyalty_state` NOT IN (2,4) +') as $order) { + if (!Validate::isLoadedObject($loyalty = new LoyaltyModule(LoyaltyModule::getByOrderId((int)$order['id_order'])))) { + continue; + } + if ((int)Configuration::get('PS_LOYALTY_NONE_AWARD') AND $loyalty->id_loyalty_state == LoyaltyStateModule::getNoneAwardId()) { + continue; + } + + if ((int)$order['id_order_state'] == $loyaltyStateValidation->id_order_state) + { + $loyalty->id_loyalty_state = LoyaltyStateModule::getValidationId(); + if ((float)($loyalty->discount_value) == 0) { + $loyalty->discount_value = LoyaltyModule::getOrderDiscountValue($order); + } + } + $loyalty->save(); +} \ No newline at end of file diff --git a/cron_loyalty_reminder.php b/cron_loyalty_reminder.php new file mode 100644 index 0000000..a4abe2d --- /dev/null +++ b/cron_loyalty_reminder.php @@ -0,0 +1,2 @@ +getValue(' + SELECT COUNT(`id_queue`) FROM `'._DB_PREFIX_.'ant_queue_logistic` WHERE `status`=1 +'); +if($on_process!=0){ + exit; +} + +$queue = Db::getInstance()->getRow(' + SELECT * FROM `'._DB_PREFIX_.'ant_queue_logistic` + ORDER BY `id_queue` ASC +'); + +if($queue){ + Db::getInstance()->execute(' + UPDATE `'._DB_PREFIX_.'ant_queue_logistic` SET `status`=1 WHERE `id_queue`='.(int)$queue['id_queue'].' + '); + $lang_id = $queue['version']; + $tax = new Tax(1); + $TVA = number_format($tax->rate, 2); + $type = (int)$queue['file_type']; + $id_category = (int)$queue['id_category']; + $date_from = new DateManager("10/11/2017 07:00:00"); + $date_to = new DateManager("17/11/2017 01:59:59"); + $date_of_extract = new DateManager(date('Y-m-d H:i:s')); + $date_of_extract->setLangue(strtoupper($lang_id)); + $date_from->setLangue(strtoupper($lang_id)); + $date_to->setLangue(strtoupper($lang_id)); + + $array_lang = array( + 'en' => array( + 'Product ID' => 'Product ID', + 'EAN' => 'EAN', + 'Reference BB' => 'BB Boutik ID', + 'Product Name' => 'Product Name', + 'Attribute' => 'Size', + 'Quantity' => 'Quantities sold', + 'Total quantity' => 'Booked quantities', + 'Remaining quantity' => 'Remaining quantities', + 'Unit Price' => 'Unit Price', + 'Total Amount' => 'Total Amount', + 'Purchase order' => 'Purchase order', + 'Purchase order logistic' => 'Purchase order logistic', + 'State order' => 'Sales report', + + 'Missing' => 'Missing', + 'Colisage' => 'Packing', + + 'To Sent' => 'To sent', + 'To Stock' => 'To stock', + + 'Shipped Product' => 'Shipped products', + + 'The number below must be present on each document regarding this order, delivery note, invoice :' => 'The number below must be present on each document regarding this order, delivery note, invoice :', + 'PURCHASE ORDER NUMBER' => 'PURCHASE ORDER NUMBER', + 'Delivery adress' => 'Delivery address', + 'PURCHASE ORDER DATE' => 'PURCHASE ORDER DATE', + 'DELIVERY DUTY PAID' => 'DELIVERY DUTY PAID', + 'YES' => 'YES', + ), + 'fr' => array( + 'Product ID' => 'Référence', + 'EAN' => 'EAN', + 'Reference BB' => 'Référence BBB', + 'Product Name' => 'Nom du produit', + 'Attribute' => 'Déclinaison', + 'Quantity' => 'Quantités vendues', + 'Total quantity' => 'Stock initial', + 'Remaining quantity' => 'Stock restant', + 'Unit Price' => 'PU HT', + 'Total Amount' => 'Prix Total HT', + 'Purchase order' => 'Bon de commande', + 'Purchase order logistic' => 'Bon de commande logistique', + 'State order' => 'Etat des ventes', + 'YES' => 'OUI', + + 'Missing' => 'Manquant', + 'Colisage' => 'Colisage/Ferme', + + 'To Sent' => 'À envoyer', + 'To Stock' => 'À mettre en stock', + + 'Shipped Product' => 'Produits livrés', + + 'PURCHASE ORDER NUMBER' => 'NUMERO DE BON DE COMMANDE', + 'Delivery adress' => 'Adresse de livraison', + 'PURCHASE ORDER DATE' => 'DATE DU BON DE COMMANDE', + 'DELIVERY DUTY PAID' => 'FRANCO DE PORT', + 'The number below must be present on each document regarding this order, delivery note, invoice :' => 'Le numéro suivant doit apparaître sur toute la correspondance annexe, les papiers de livraison et les factures :', + ) + ); + $lang = $array_lang[$lang_id]; + + $sale = Sale::getSaleFromCategory($id_category); + $category = new Category($id_category, 2); + $products = MakeStats::getProductsByCat($id_category); + + $lines = array(); + $array_product = array(); + + $arrayProduct = array(); + foreach($products as $product){ + $array_product[$product['id_product']] = $arrayProduct[$product['id_product']] = new Product($product['id_product']); + } + + $order_states = explode(',', Configuration::get('PS_IT_OF_ORDER_STATES')); + if ($type == 2 || $type == 4) { + $lines = MakeStats::getDetailsByIdsProduct(array_keys($arrayProduct), $order_states, $date_from->getDate('SQL'), $date_to->getDate('SQL')); + } else { + $lines = MakeStats::getOrdersByIdsProduct(array_keys($arrayProduct), $order_states, $date_from->getDate('SQL'), $date_to->getDate('SQL')); + } + + // for stats + $lines_tmp = $lines; + if ($type == 4) { + $lines = MakeStats::getArrayLines($lines, false, $sale->id); + } else { + $lines = MakeStats::getArrayLines($lines); + } + + if ($type == 3) { + $stats = new SaleStats($sale, $category); + $stats->loadData(); + + $total_sale_product = array(); + $total_bdc_ht = 0; + + foreach ($lines_tmp as $key => $order_line) { + $id_product = $order_line['product_id']; + $id_product_attribute = $order_line['product_attribute_id']; + + $current_product = $array_product[$id_product]; + if($id_product_attribute != 0) { + $combination = new Combination($id_product_attribute); + if($combination->wholesale_price == 0) { + $wholesale_price = $current_product->wholesale_price; + } else { + $wholesale_price = $combination->wholesale_price; + } + } else { + $wholesale_price = $current_product->wholesale_price; + } + + $quantity_to_cmd = $order_line['product_quantity'] - $order_line['product_quantity_reinjected']; + $total_sale_ht += $quantity_to_cmd * $wholesale_price; + + if (!is_array($total_sale_product[(int)$id_product.'-'.(int) $id_product_attribute]) ){ + $total_sale_product[(int)$id_product.'-'.(int) $id_product_attribute] = array(); + $total_sale_product[(int)$id_product.'-'.(int) $id_product_attribute]['total_ht'] = 0; + $total_sale_product[(int)$id_product.'-'.(int) $id_product_attribute]['quantiy'] = 0; + $total_sale_product[(int)$id_product.'-'.(int) $id_product_attribute]['name'] = $current_product->name[2]; + } + + $price_ht = $current_product->getPrice(false, $id_product_attribute, 2); + $price_ttc = $current_product->getPrice(true, $id_product_attribute, 2); + + $total_sale_product[(int)$id_product.'-'.(int) $id_product_attribute]['total_ht'] += $price_ht; + $total_sale_product[(int)$id_product.'-'.(int) $id_product_attribute]['quantiy'] += $quantity_to_cmd; + } + + $stats->addToStats('total_bdc_ht', $total_sale_ht); + $bestsales = MakeStats::orderMultiArray($total_sale_product, 'total_ht'); + $bestsales_extract = array_slice($bestsales, 0, 3); + + $i = 1; + foreach ($bestsales_extract as $key => $bestsalesProduct) { + $stats->addToStats('bestsale_'.$i.'_product', $bestsalesProduct['name']); + $stats->addToStats('bestsale_'.$i.'_quantity', $bestsalesProduct['quantiy']); + $stats->addToStats('bestsale_'.$i.'_ca', $bestsalesProduct['total_ht']); + $i++; + } + $stats->loadDataForSubCategory(); + + $fp = fopen('extracts/sales_logistique/Stats_vente_'. $stats->sale->title[2].'.csv', 'w+'); + foreach ($stats->data_stats as $key => $stat) { + $text = $stats->lang[$key] . ';' . $stat ."\r\n"; + fwrite($fp, $text); + } + foreach ($stats->data_subcategories as $key => $subcategories) { + fwrite($fp, 'Sous categorie '. ($key+1)."\r\n"); + + foreach ($subcategories as $key_2 => $subcategory) { + $text = $stats->lang[$key_2] . ';' . $subcategory ."\r\n"; + fwrite($fp, $text); + } + } + fclose($fp); + die; + } + + $arrayTmp = array(); + foreach($lines as $id_product => $arrayProduct) + { + foreach($arrayProduct as $id_product_attribute => $values) + { + //echo "
";var_dump($values);echo "
"; + $arrayTmp[$values['line']['product_id']][$values['line']['product_attribute_id']] = $values; + } + } + + // associate position + $arrayTmp2 = array(); + foreach ($products as $key => $product_order) { + if (isset($arrayTmp[$product_order['id_product']])) { + $arrayTmp2[] = $arrayTmp[$product_order['id_product']]; + } + } + + // $lines2 = $arrayTmp; + $lines2 = $arrayTmp2; + + // ksort($lines2); + $have_declinaison = false; + $array = array(); + foreach($lines2 as $key => $lines_attributes) + { + foreach($lines_attributes as $values) + { + $array[$key][] = $values; + if($values['line']['product_attribute_name_base'] != '') { + if (!$have_declinaison) { + $have_declinaison = true; + } + } + } + } + $lines2 = array_replace($lines2, $array); + + switch($type) + { + case '1': + $title = $lang['Purchase order'].' '. $category->name .' - Bébé Boutik '. $date_from->getNomMois() .' '.$date_from->getAnnee(); + break; + case '2': + $extrafields = Category::getSalesInfos(array((int)$category->id_category)); + $title = $lang['State order'] .' '. $category->name . ' - ' . $extrafields[(int)$category->id_category]['sales'][1]; + break; + case '4': + $title = $lang['Purchase order logistic'].' '. $category->name .' - Bébé Boutik '. $date_from->getNomMois() .' '.$date_from->getAnnee(); + break; + } + + + + $firstColumn = 'A'; + if($have_declinaison === true) + { + if ($type == 4) { + $lastColumn = 'M'; + + $letter_product_ean = 'B'; + $letter_product_ref_interne = 'C'; + $letter_product_name = 'D'; + $letter_product_attribute_name = 'E'; + $letter_product_ht = 'F'; + //$letter_product_M1_M2 = 'F'; + $letter_product_M1 = 'G'; + $letter_product_multi = 'H'; + + $letter_product_stock3btk = 'I'; + $letter_product_missing = 'J'; + $letter_product_to_sent = 'K'; // (M1 + Multi - Missing) + $letter_product_to_stock = 'L'; + + $letter_product_shipped = 'M'; // (M1 + Multi - stock3BTK - Missing + to_stock) + + } else { + + if ($type == 1) { + $lastColumn = 'H'; + } else { + $lastColumn = 'J'; + } + + $letter_bdc_title = 'C'; + $letter_end_address = 'B'; + + $letter_date = 'C'; + $letter_franco = 'D'; + $letter_condition = 'E'; + + $letter_product_ean = 'B'; + $letter_product_ref_interne = 'C'; + $letter_product_name = 'D'; + $letter_product_attribute_name = 'E'; + /*$letter_product_qty = 'F';*/ + + if ($type == 1) { + $letter_product_qty = 'F'; + $letter_product_ht = 'G'; + $letter_product_total = 'H'; + } else { + $letter_product_total_qty = 'F'; + $letter_product_qty = 'G'; + $letter_product_remaining_qty = 'H'; + $letter_product_ht = 'I'; + $letter_product_total = 'J'; + } + } + } + else + { + if ($type == 4) { + $lastColumn = 'L'; + + $letter_product_ean = 'B'; + $letter_product_ref_interne = 'C'; + $letter_product_name = 'D'; + //$letter_product_attribute_name = 'E'; + $letter_product_ht = 'E'; + //$letter_product_M1_M2 = 'F'; + $letter_product_M1 = 'F'; + $letter_product_multi = 'G'; + + $letter_product_stock3btk = 'H'; + $letter_product_missing = 'I'; + $letter_product_to_sent = 'J'; // (M1 + Multi - Missing) + $letter_product_to_stock = 'K'; + + $letter_product_shipped = 'L'; // (M1 + Multi - stock3BTK - Missing + to_stock) + + } else { + + if ($type == 1) { + $lastColumn = 'G'; + } else { + $lastColumn = 'I'; + } + + $letter_bdc_title = 'C'; + $letter_end_address = 'B'; + + $letter_date = 'C'; + $letter_franco = 'D'; + $letter_condition = 'E'; + + + $letter_product_ean = 'B'; + $letter_product_ref_interne = 'C'; + $letter_product_name = 'D'; + // $letter_product_attribute_name = 'C'; + /* $letter_product_qty = 'E';*/ + + if ($type == 1) { + $letter_product_qty = 'E'; + $letter_product_ht = 'F'; + $letter_product_total = 'G'; + } else { + $letter_product_total_qty = 'E'; + $letter_product_qty = 'F'; + $letter_product_remaining_qty = 'G'; + $letter_product_ht = 'H'; + $letter_product_total = 'I'; + } + } + + } + + + // if($loaded === true) + // { + // header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); + // header('Content-Disposition: attachment;filename="'.$title.'.xlsx"'); + + // echo file_get_contents($OrderForm->getFile()); + // exit; + // } + $objPHPExcel = new PHPExcel(); + + $objPHPExcel->getProperties()->setCreator("Antadis"); + $objPHPExcel->getProperties()->setTitle( $title ); + $objPHPExcel->getProperties()->setSubject( $title ); + $objPHPExcel->getProperties()->setDescription( $title ); + + $objPHPExcel->getActiveSheet()->getStyle($firstColumn.'1:'.$lastColumn.'300')->getFont()->setSize(10); + $objPHPExcel->getActiveSheet()->getStyle($firstColumn.'1:'.$lastColumn.'300')->getFont()->setName('calibri'); + // $objPHPExcel->getFont()->setSize(10); + // $objPHPExcel->getFont()->setName('Colibri'); + + $arrayBorder = array( + 'borders' => array( + 'allborders' => array( + 'style' => PHPExcel_Style_Border::BORDER_THIN, + 'color' => array( + 'rgb' => '000000' + ) + ) + ) + ); + + $arrayBorderAutour = array( + 'borders' => array( + 'left' => array( + 'style' => PHPExcel_Style_Border::BORDER_THIN, + ), + 'right' => array( + 'style' => PHPExcel_Style_Border::BORDER_THIN, + ), + 'bottom' => array( + 'style' => PHPExcel_Style_Border::BORDER_THIN, + ), + 'top' => array( + 'style' => PHPExcel_Style_Border::BORDER_THIN, + ) + ) + ); + $arrayConfig = array( + 'PS_IT_OF_ORGANISATION_'.$lang_id, + 'PS_IT_OF_ADDRESS_'.$lang_id, + 'PS_IT_OF_ADDRESS_2_'.$lang_id, + 'PS_IT_OF_CP_CITY_'.$lang_id, + 'PS_IT_OF_NUMBER_PHONE_'.$lang_id + ); + + if(!Configuration::get('PS_IT_OF_ADDRESS_2_'.$lang_id)) + unset($arrayConfig[array_search('PS_IT_OF_ADDRESS_2_'.$lang_id, $arrayConfig)]); + $activeSheet = $objPHPExcel->getActiveSheet(); + + + $i = 1; + + if($type == 1) + { + //LOGO + // $activeSheet->getRowDimension($i)->setRowHeight(80); + + // $objDrawing = new PHPExcel_Worksheet_Drawing(); + // $objDrawing->setWorksheet($activeSheet); + // $objDrawing->setName("name"); + // $objDrawing->setDescription("Description"); + // $objDrawing->setPath(_PS_IMG_DIR_.'logo.jpg'); + // $objDrawing->setCoordinates($firstColumn.$i); + // $objDrawing->setOffsetX(1); + // $objDrawing->setOffsetY(5); + + //ADRESSE DE LIVRAISON + + // $activeSheet->SetCellValue($firstColumn.$i, 'Adresse de livraison : '); + // $activeSheet->getStyle($firstColumn.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP); + + // $activeSheet->getStyle($firstColumn.$i)->getFont()->setBold(true); + // $activeSheet->getStyle($firstColumn.$i)->getFont()->setSize(12); + // $activeSheet->getStyle($firstColumn.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); + // $styleArray = array( + // 'font' => array( + // 'underline' => PHPExcel_Style_Font::UNDERLINE_SINGLE + // ) + // ); + // $activeSheet->getStyle($firstColumn.$i)->applyFromArray($styleArray); + // $objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray); + + + $iOri = $i; + + foreach($arrayConfig as $k => $cfg) + { + $activeSheet->SetCellValue($firstColumn.$i, Configuration::get($cfg) ); + $activeSheet->getStyle($firstColumn.$i)->getFont()->setSize(10); + $activeSheet->getStyle($firstColumn.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); + $activeSheet->mergeCells($firstColumn.$i.':'.$letter_end_address.$i ); + if($k == 0) + { + $activeSheet->getStyle($firstColumn.$i)->getFont()->setBold(true); + $i++; + $activeSheet->SetCellValue($firstColumn.$i, Configuration::get('PS_IT_OF_SLOGAN_'.$lang_id) ); + $activeSheet->getStyle($firstColumn.$i)->getFont()->setSize(10); + $activeSheet->getStyle($firstColumn.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); + $activeSheet->mergeCells($firstColumn.$i.':'.$letter_end_address.$i ); + $i++; + } + $i++; + } + + $activeSheet->SetCellValue($firstColumn.$i, Configuration::get('PS_IT_OF_EMAIL_'.$lang_id) ); + $activeSheet->getStyle($firstColumn.$i)->getFont()->setSize(10); + $activeSheet->getStyle($firstColumn.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); + $activeSheet->mergeCells($firstColumn.$i.':'.$letter_end_address.$i ); + $i++; + + $activeSheet->SetCellValue($firstColumn.$i, Configuration::get('PS_IT_OF_TVA_'.$lang_id) ); + $activeSheet->getStyle($firstColumn.$i)->getFont()->setSize(10); + $activeSheet->getStyle($firstColumn.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); + $activeSheet->mergeCells($firstColumn.$i.':'.$letter_end_address.$i ); + $i++; + + //BON DE COMMANDE + $activeSheet->getCell($letter_bdc_title.$iOri)->setValue($lang['Purchase order'].' '.chr(13).' '.$category->name); + $activeSheet->getStyle($letter_bdc_title.$iOri)->getAlignment()->setWrapText(true); + $activeSheet->getStyle($letter_bdc_title.$iOri)->getFont()->setBold(true); + $activeSheet->getStyle($letter_bdc_title.$iOri)->getFont()->setSize(18); + $activeSheet->getStyle($letter_bdc_title.$iOri)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); + $activeSheet->getStyle($letter_bdc_title.$iOri)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); + + + $activeSheet->mergeCells($letter_bdc_title.$iOri.':'.$lastColumn.($i-1)); + // $activeSheet->getStyle($firstColumn.($iOri).':'.$lastColumn.($i-1))->applyFromArray($arrayBorderAutour); + + $i++; + + + + //TEXTE + $activeSheet->SetCellValue($firstColumn.$i, $lang['The number below must be present on each document regarding this order, delivery note, invoice :']); + $activeSheet->mergeCells($firstColumn.$i.':'.$letter_end_address.$i ); + $activeSheet->getStyle($firstColumn.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_JUSTIFY); + $activeSheet->getRowDimension($i)->setRowHeight(30); + $i++; + $activeSheet->SetCellValue($firstColumn.$i, $lang['PURCHASE ORDER NUMBER'] . ' : '. $OrderForm->id); + $activeSheet->getStyle($firstColumn.$i)->getFont()->setSize(10); + // $activeSheet->getStyle($firstColumn.$i)->getFont()->getColor()->setRGB('FF0000'); + $activeSheet->getStyle($firstColumn.$i)->getFont()->setBold(true); + $activeSheet->getStyle($firstColumn.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_JUSTIFY); + $activeSheet->mergeCells($firstColumn.$i.':'.$letter_end_address.$i ); + $i++; + + //ADRESSE DE LIVRAISON BIS + + $letter = 'D'; + $activeSheet->SetCellValue($letter.$i, $lang['Delivery adress'].' :'); + $activeSheet->getStyle($letter.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP); + + $activeSheet->getStyle($letter.$i)->getFont()->setBold(true); + $activeSheet->getStyle($letter.$i)->getFont()->setSize(12); + $activeSheet->getStyle($letter.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); + // $styleArray = array( + // 'font' => array( + // 'underline' => PHPExcel_Style_Font::UNDERLINE_SINGLE + // ) + // ); + // $activeSheet->getStyle($letter.$i)->applyFromArray($styleArray); + $activeSheet->mergeCells($letter.$i.':'.$lastColumn.$i ); + $i++; + $iOri = $i; + + foreach($arrayConfig as $cfg) + { + $activeSheet->SetCellValue($letter.$i, Configuration::get($cfg) ); + $activeSheet->getStyle($letter.$i)->getFont()->setSize(10); + $activeSheet->mergeCells($letter.$i.':'.$lastColumn.$i ); + $activeSheet->getStyle($letter.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); + $i++; + } + + $i++; + + + $activeSheet->SetCellValue($letter_date.$i, $lang['PURCHASE ORDER DATE'] ); + + $activeSheet->SetCellValue($letter_franco.$i, $lang['DELIVERY DUTY PAID'] ); + + $activeSheet->SetCellValue($letter_condition.$i, 'CONDITIONS' ); + + $activeSheet->getStyle($letter_date.$i.':'.$letter_condition.$i)->getFont()->setBold(true); + $activeSheet->getStyle($letter_date.$i.':'.$letter_condition.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_JUSTIFY); + $activeSheet->getStyle($letter_date.$i.':'.$letter_condition.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); + + $i ++; + + $activeSheet->SetCellValue($letter_date.$i, date('d/m/Y')); + + $activeSheet->SetCellValue($letter_franco.$i, $lang['YES'] ); + + $activeSheet->SetCellValue($letter_condition.$i, 'N/A' ); + + $activeSheet->getStyle($letter_date.($i-1).':'.$letter_condition.$i)->applyFromArray($arrayBorderAutour); + $activeSheet->getStyle($letter_date.$i.':'.$letter_condition.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); + $i ++; + + if(isset(Sale::$otherVar)) + { + $iOriSaleVar = $i; + $value = ''; + /*$activeSheet->SetCellValue($firstColumn.$i, 'Fournisseur : '); + $activeSheet->getStyle($firstColumn.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP); + + $activeSheet->getStyle($firstColumn.$i)->getFont()->setBold(true); + $activeSheet->getStyle($firstColumn.$i)->getFont()->setSize(13); + $activeSheet->getStyle($firstColumn.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);*/ + $p = 0; + foreach(Sale::$otherVar as $var) + if(!empty($sale->$var)) + { + $value = $sale->$var; + + + $activeSheet->getCell($firstColumn.$i)->setValue($value); + + if($var == 'supplier_name') + { + $activeSheet->getStyle($firstColumn.$i)->getFont()->setSize(14); + $activeSheet->getStyle($firstColumn.$i)->getFont()->setBold(true); + $activeSheet->getRowDimension($i)->setRowHeight(20); + } + $activeSheet->getStyle($firstColumn.$i)->getAlignment()->setWrapText(true); + $activeSheet->getStyle($firstColumn.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP); + $activeSheet->mergeCells($firstColumn.$i.':F'.$i); + $i++; + $p++; + } + + if($p == 0) + { + $activeSheet->getCell($firstColumn.$i)->setValue('Aucune information fournisseur'); + + $activeSheet->getStyle($firstColumn.$i)->getAlignment()->setWrapText(true); + $activeSheet->getStyle($firstColumn.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP); + $activeSheet->mergeCells($firstColumn.$i.':F'.$i); + $i++; + } + + + $activeSheet->getStyle($firstColumn.($i-1))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); + $activeSheet->getStyle($firstColumn.($i-1).':'.$lastColumn.($i-1))->applyFromArray($arrayBorderAutour); + + if($iOriSaleVar != $i) + $activeSheet->mergeCells($firstColumn.$iOriSaleVar.':'.$firstColumn.($i-1) ); + // if(!empty($value)) + // { + // $activeSheet->getCell('C'.$i)->setValue($value); + // $activeSheet->getStyle('C'.$i)->getFont()->setSize(13); + // $activeSheet->getStyle('C'.$i)->getAlignment()->setWrapText(true); + // $activeSheet->getStyle('C'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP); + // $activeSheet->mergeCells('C'.$i.':F'.$i); + // $activeSheet->getRowDimension($i)->setRowHeight(90); + // $i++; + // } + } + + $i ++; + } + if ($type == 4) { + $activeSheet->SetCellValue($firstColumn.$i, $title); + $i++;$i++; + + $borders_style = array( + 'borders' => array( + 'allborders' => array( + 'style' => PHPExcel_Style_Border::BORDER_THIN + ) + ) + ); + + $activeSheet->SetCellValue($firstColumn.$i, $lang['Product ID']); + $activeSheet->getStyle($firstColumn.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); + $activeSheet->getStyle($firstColumn.$i)->applyFromArray($borders_style); + + $activeSheet->SetCellValue($letter_product_ean.$i, $lang['EAN']); + $activeSheet->getStyle($letter_product_ean.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); + $activeSheet->getStyle($letter_product_ean.$i)->applyFromArray($borders_style); + + $activeSheet->SetCellValue($letter_product_ref_interne.$i, $lang['Reference BB']); + $activeSheet->getStyle($letter_product_ref_interne.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); + $activeSheet->getStyle($letter_product_ref_interne.$i)->applyFromArray($borders_style); + + + $activeSheet->SetCellValue($letter_product_name.$i, $lang['Product Name']); + $activeSheet->getStyle($letter_product_name.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); + $activeSheet->getStyle($letter_product_name.$i)->applyFromArray($borders_style); + if($have_declinaison === true) { + $activeSheet->SetCellValue($letter_product_attribute_name.$i, $lang['Attribute']); + $activeSheet->getStyle($letter_product_attribute_name.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); + $activeSheet->getStyle($letter_product_attribute_name.$i)->applyFromArray($borders_style); + } + + $activeSheet->setCellValue($letter_product_ht.$i, $lang['Unit Price']); + $activeSheet->getStyle($letter_product_ht.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); + $activeSheet->getStyle($letter_product_ht.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); + $activeSheet->getStyle($letter_product_ht.$i)->applyFromArray($borders_style); + + $activeSheet->setCellValue($letter_product_M1.$i, 'M1'); + $activeSheet->getStyle($letter_product_M1.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); + $activeSheet->getStyle($letter_product_M1.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); + $activeSheet->getStyle($letter_product_M1.$i)->applyFromArray($borders_style); + + $activeSheet->setCellValue($letter_product_multi.$i, 'Multi'); + $activeSheet->getStyle($letter_product_multi.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); + $activeSheet->getStyle($letter_product_multi.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); + $activeSheet->getStyle($letter_product_multi.$i)->applyFromArray($borders_style); + + $activeSheet->setCellValue($letter_product_stock3btk.$i, 'Stock 3BTK'); + $activeSheet->getStyle($letter_product_stock3btk.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); + $activeSheet->getStyle($letter_product_stock3btk.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); + $activeSheet->getStyle($letter_product_stock3btk.$i)->applyFromArray($borders_style); + + $activeSheet->setCellValue($letter_product_missing.$i, $lang['Missing']); + $activeSheet->getStyle($letter_product_missing.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); + $activeSheet->getStyle($letter_product_missing.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); + $activeSheet->getStyle($letter_product_missing.$i)->applyFromArray($borders_style); + + $activeSheet->setCellValue($letter_product_to_sent.$i, $lang['To Sent']); + $activeSheet->getStyle($letter_product_to_sent.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); + $activeSheet->getStyle($letter_product_to_sent.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); + $activeSheet->getStyle($letter_product_to_sent.$i)->applyFromArray($borders_style); + + $activeSheet->setCellValue($letter_product_to_stock.$i, $lang['To Stock']); + $activeSheet->getStyle($letter_product_to_stock.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); + $activeSheet->getStyle($letter_product_to_stock.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); + $activeSheet->getStyle($letter_product_to_stock.$i)->applyFromArray($borders_style); + + $activeSheet->setCellValue($letter_product_shipped.$i, $lang['Shipped Product']); + $activeSheet->getStyle($letter_product_shipped.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); + $activeSheet->getStyle($letter_product_shipped.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); + $activeSheet->getStyle($letter_product_shipped.$i)->applyFromArray($borders_style); + + } else { + + if ($type == 2) { + $activeSheet->SetCellValue($firstColumn.$i, $date_of_extract->getJour().' '.$date_of_extract->getNomMois().' '.$date_of_extract->getAnnee()); + $i++;$i++; + } + + $activeSheet->SetCellValue($firstColumn.$i, $lang['Product ID']); + $activeSheet->getStyle($firstColumn.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); + + + $activeSheet->SetCellValue($letter_product_ean.$i, $lang['EAN']); + $activeSheet->getStyle($letter_product_ean.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); + $activeSheet->SetCellValue($letter_product_ref_interne.$i, $lang['Reference BB']); + $activeSheet->getStyle($letter_product_ref_interne.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); + //$activeSheet->getStyle('E'.$i)->getBorders()->setBold(true); + + $activeSheet->SetCellValue($letter_product_name.$i, $lang['Product Name']); + $activeSheet->getStyle($letter_product_name.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); + if($have_declinaison === true) { + $activeSheet->SetCellValue($letter_product_attribute_name.$i, $lang['Attribute']); + $activeSheet->getStyle($letter_product_attribute_name.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); + } + + $activeSheet->SetCellValue($letter_product_qty.$i, $lang['Quantity']); + $activeSheet->getStyle($letter_product_qty.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); + $activeSheet->getStyle($letter_product_qty.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); + $activeSheet->getStyle($letter_product_qty.$i)->getAlignment()->setWrapText(true); + if ($type == 2) { + $activeSheet->getStyle($letter_product_qty.$i)->getFill()->applyFromArray( + array( + 'type' => PHPExcel_Style_Fill::FILL_SOLID, + 'rotation' => 0, + 'startcolor' => array( + 'rgb' => 'D9D9D9' + ), + 'endcolor' => array( + 'argb' => 'D9D9D9D9' + ) + ) + ); + $activeSheet->SetCellValue($letter_product_total_qty.$i, $lang['Total quantity']); + $activeSheet->getStyle($letter_product_total_qty.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); + $activeSheet->getStyle($letter_product_total_qty.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); + + $activeSheet->SetCellValue($letter_product_remaining_qty.$i, $lang['Remaining quantity']); + $activeSheet->getStyle($letter_product_remaining_qty.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); + $activeSheet->getStyle($letter_product_remaining_qty.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); + } + $activeSheet->setCellValue($letter_product_ht.$i, $lang['Unit Price']); + $activeSheet->getStyle($letter_product_ht.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); + $activeSheet->getStyle($letter_product_ht.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); + + $activeSheet->setCellValue($letter_product_total.$i, $lang['Total Amount']); + $activeSheet->getStyle($letter_product_total.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); + $activeSheet->getStyle($letter_product_total.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); + } + + $i++; + $iOri = $i; + + //p(count($lines)); + $count = 0; + if($type == 1 || $type == 2) + foreach($lines as $line) + foreach($line as $l) + $count += $l['total']; + + switch($type) + { + case '4': + foreach($lines2 as $k => $lines_attributes) + { + $ref = array(); + $decli = array(); + foreach ($lines_attributes as $key => $row) { + // si le produit n'apparait pas dans une commande, on affiche pas la ligne + if (!isset($row['total_m']) || $row['total_m'] == 0) { + unset($lines_attributes[$key]); + continue; + } + $ref[$key] = $row['line']['product_reference']; + $decli[$key] = $row['line']['product_attribute_name_base']; + } + array_multisort($ref, SORT_ASC, $decli, SORT_ASC, $lines_attributes); + foreach($lines_attributes as $key => $values) + { + + if (is_array($lines_attributes[$key+1]) + || $lines2[$k+1][0]['line']['product_reference'] == $values['line']['product_reference']) { + $borders_style = array( + 'borders' => array( + 'right' => array( + 'style' => PHPExcel_Style_Border::BORDER_THIN + ), + 'left' => array( + 'style' => PHPExcel_Style_Border::BORDER_THIN + ) + ) + ); + } else { + $borders_style = array( + 'borders' => array( + 'right' => array( + 'style' => PHPExcel_Style_Border::BORDER_THIN + ), + 'left' => array( + 'style' => PHPExcel_Style_Border::BORDER_THIN + ), + 'bottom' => array( + 'style' => PHPExcel_Style_Border::BORDER_THIN + ) + ) + ); + } + + $id_product_attribute = $values['line']['product_attribute_id']; + $id_product = $values['line']['product_id']; + $current_product = $array_product[$id_product]; + if($id_product_attribute != 0) + { + $combination = new Combination($id_product_attribute); + if($combination->wholesale_price == 0) + $wholesale_price = $current_product->wholesale_price; + else + $wholesale_price = $combination->wholesale_price; + } + else + { + $wholesale_price = $current_product->wholesale_price; + } + + $activeSheet->SetCellValue($firstColumn.$i, $values['line']['product_supplier_reference']); + $activeSheet->getStyle($firstColumn.$i)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_TEXT ); + $activeSheet->getStyle($firstColumn.$i)->applyFromArray($borders_style); + + $activeSheet->SetCellValue($letter_product_ean.$i, $values['line']['product_ean13']); + $activeSheet->getStyle($letter_product_ean.$i)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_NUMBER ); + $activeSheet->getStyle($letter_product_ean.$i)->applyFromArray($borders_style); + + $activeSheet->SetCellValue($letter_product_ref_interne.$i, $values['line']['product_reference']); + $activeSheet->getStyle($letter_product_ref_interne.$i)->applyFromArray($borders_style); + + + $activeSheet->SetCellValue($letter_product_name.$i, $values['line']['product_name_base']); + $activeSheet->getStyle($letter_product_name.$i)->applyFromArray($borders_style); + if($have_declinaison === true) { + $activeSheet->SetCellValue($letter_product_attribute_name.$i, $values['line']['product_attribute_name_base']); + $activeSheet->getStyle($letter_product_attribute_name.$i)->applyFromArray($borders_style); + } + + $activeSheet->setCellValue($letter_product_ht.$i, (float)$wholesale_price ); + $activeSheet->getStyle($letter_product_ht.$i)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE ); + $activeSheet->getStyle($letter_product_ht.$i)->applyFromArray($borders_style); + + $activeSheet->setCellValue($letter_product_multi.$i, (isset($values['multi']))?$values['multi']:0); + $activeSheet->getStyle($letter_product_multi.$i)->applyFromArray($borders_style); + $activeSheet->setCellValue($letter_product_M1.$i, (isset($values['m1']))?$values['m1']:0); + $activeSheet->getStyle($letter_product_M1.$i)->applyFromArray($borders_style); + + // colonnes vide - a remplir par 3BTK + $activeSheet->getStyle($letter_product_stock3btk.$i)->applyFromArray($borders_style); + $activeSheet->getStyle($letter_product_missing.$i)->applyFromArray($borders_style); + + $activeSheet->setCellValue($letter_product_to_sent.$i, '='.$letter_product_M1.$i.'+'.$letter_product_multi.$i.'-'.$letter_product_missing.$i); + $activeSheet->getStyle($letter_product_to_sent.$i)->applyFromArray($borders_style); + + // colonnes vide - a remplir par 3BTK + $activeSheet->getStyle($letter_product_to_stock.$i)->applyFromArray($borders_style); + + $activeSheet->setCellValue($letter_product_shipped.$i, '='.$letter_product_M1.$i.'+'.$letter_product_multi.$i.'-'.$letter_product_stock3btk.$i.'-'.$letter_product_missing.$i.'+'.$letter_product_to_stock.$i); + $activeSheet->getStyle($letter_product_shipped.$i)->applyFromArray($borders_style); + + $i++; + } + } + break; + case '2': + foreach($lines2 as $k => $lines_attributes) + { + foreach($lines_attributes as $key => $values) + { + if ( + (isset($lines_attributes[$key+1]) && is_array($lines_attributes[$key+1])) + || (isset($lines2[$k+1]) && $lines2[$k+1][0]['line']['product_reference'] == $values['line']['product_reference']) + ) { + $borders_style = array( + 'borders' => array( + 'right' => array( + 'style' => PHPExcel_Style_Border::BORDER_THIN + ), + 'left' => array( + 'style' => PHPExcel_Style_Border::BORDER_THIN + ) + ) + ); + } else { + $borders_style = array( + 'borders' => array( + 'right' => array( + 'style' => PHPExcel_Style_Border::BORDER_THIN + ), + 'left' => array( + 'style' => PHPExcel_Style_Border::BORDER_THIN + ), + 'bottom' => array( + 'style' => PHPExcel_Style_Border::BORDER_THIN + ) + ) + ); + } + + $id_product_attribute = $values['line']['product_attribute_id']; + $id_product = $values['line']['product_id']; + $current_product = $array_product[$id_product]; + if($id_product_attribute != 0) + { + $combination = new Combination($id_product_attribute); + if($combination->wholesale_price == 0) + $wholesale_price = $current_product->wholesale_price; + else + $wholesale_price = $combination->wholesale_price; + } + else + { + $wholesale_price = $current_product->wholesale_price; + } + + $activeSheet->SetCellValue($firstColumn.$i, $values['line']['product_supplier_reference']); + $activeSheet->getStyle($firstColumn.$i)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_TEXT ); + $activeSheet->getStyle($firstColumn.$i)->applyFromArray($borders_style); + + $activeSheet->SetCellValue($letter_product_ean.$i, $values['line']['product_ean13']); + $activeSheet->getStyle($letter_product_ean)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER); + $activeSheet->getStyle($letter_product_ean.$i)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER); + $activeSheet->getStyle($letter_product_ean.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); + $activeSheet->getStyle($letter_product_ean.$i)->applyFromArray($borders_style); + + $activeSheet->SetCellValue($letter_product_ref_interne.$i, $values['line']['product_reference']); + $activeSheet->getStyle($letter_product_ref_interne.$i)->applyFromArray($borders_style); + + $activeSheet->SetCellValue($letter_product_name.$i, $values['line']['product_name_base']); + $activeSheet->getStyle($letter_product_name.$i)->applyFromArray($borders_style); + if($have_declinaison === true) { + $activeSheet->SetCellValue($letter_product_attribute_name.$i, $values['line']['product_attribute_name_base']); + $activeSheet->getStyle($letter_product_attribute_name.$i)->applyFromArray($borders_style); + } + + $activeSheet->SetCellValue($letter_product_qty.$i, $values['total']); + $activeSheet->getStyle($letter_product_qty.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); + $activeSheet->getStyle($letter_product_qty.$i)->applyFromArray($borders_style); + $activeSheet->getStyle($letter_product_qty.$i)->getFill()->applyFromArray( + array( + 'type' => PHPExcel_Style_Fill::FILL_SOLID, + 'rotation' => 0, + 'startcolor' => array( + 'rgb' => 'D9D9D9' + ), + 'endcolor' => array( + 'argb' => 'D9D9D9D9' + ) + ) + ); + + $activeSheet->SetCellValue($letter_product_total_qty.$i, ($values['stock']+$values['total'])); + $activeSheet->getStyle($letter_product_total_qty.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); + $activeSheet->getStyle($letter_product_total_qty.$i)->applyFromArray($borders_style); + + $activeSheet->SetCellValue($letter_product_remaining_qty.$i, $values['stock']); + $activeSheet->getStyle($letter_product_remaining_qty.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); + $activeSheet->getStyle($letter_product_remaining_qty.$i)->applyFromArray($borders_style); + + $activeSheet->setCellValue($letter_product_ht.$i, (float)$wholesale_price ); + $activeSheet->setCellValue($letter_product_total.$i, '='.$letter_product_qty.$i.'*'.$letter_product_ht.$i); + + $activeSheet->getStyle($letter_product_ht.$i)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE ); + $activeSheet->getStyle($letter_product_ht.$i)->applyFromArray($borders_style); + $activeSheet->getStyle($letter_product_total.$i)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE ); + $activeSheet->getStyle($letter_product_total.$i)->applyFromArray($borders_style); + + $i++; + } + } + break; + case '1': + //echo "
";var_dump($lines2);echo "
";die(); + foreach($lines2 as $k => $lines_attributes) + { + foreach($lines_attributes as $key => $values) + { + if (is_array($lines_attributes[$key+1]) + || $lines2[$k+1][0]['line']['product_reference'] == $values['line']['product_reference']) { + $borders_style = array( + 'borders' => array( + 'right' => array( + 'style' => PHPExcel_Style_Border::BORDER_THIN + ), + 'left' => array( + 'style' => PHPExcel_Style_Border::BORDER_THIN + ) + ) + ); + } else { + $borders_style = array( + 'borders' => array( + 'right' => array( + 'style' => PHPExcel_Style_Border::BORDER_THIN + ), + 'left' => array( + 'style' => PHPExcel_Style_Border::BORDER_THIN + ), + 'bottom' => array( + 'style' => PHPExcel_Style_Border::BORDER_THIN + ) + ) + ); + } + + $id_product_attribute = $values['line']['product_attribute_id']; + $id_product = $values['line']['product_id']; + $current_product = $array_product[$id_product]; + if($id_product_attribute != 0) + { + $combination = new Combination($id_product_attribute); + if($combination->wholesale_price == 0) + $wholesale_price = $current_product->wholesale_price; + else + $wholesale_price = $combination->wholesale_price; + } + else + { + $wholesale_price = $current_product->wholesale_price; + } + + $activeSheet->SetCellValue($firstColumn.$i, $values['line']['product_supplier_reference']); + $activeSheet->getStyle($firstColumn.$i)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_TEXT ); + $activeSheet->getStyle($firstColumn.$i)->applyFromArray($borders_style); + + $activeSheet->SetCellValue($letter_product_ean.$i, $values['line']['product_ean13']); + $activeSheet->getStyle($letter_product_ean)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER); + $activeSheet->getStyle($letter_product_ean.$i)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER); + $activeSheet->getStyle($letter_product_ean.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); + $activeSheet->getStyle($letter_product_ean.$i)->applyFromArray($borders_style); + + $activeSheet->SetCellValue($letter_product_ref_interne.$i, $values['line']['product_reference']); + $activeSheet->getStyle($letter_product_ref_interne.$i)->applyFromArray($borders_style); + + $activeSheet->SetCellValue($letter_product_name.$i, $values['line']['product_name_base']); + $activeSheet->getStyle($letter_product_name.$i)->applyFromArray($borders_style); + if($have_declinaison === true) { + $activeSheet->SetCellValue($letter_product_attribute_name.$i, $values['line']['product_attribute_name_base']); + $activeSheet->getStyle($letter_product_attribute_name.$i)->applyFromArray($borders_style); + } + + $activeSheet->SetCellValue($letter_product_qty.$i, $values['total']); + $activeSheet->getStyle($letter_product_qty.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); + $activeSheet->getStyle($letter_product_qty.$i)->applyFromArray($borders_style); + + $activeSheet->setCellValue($letter_product_ht.$i, (float)$wholesale_price ); + $activeSheet->setCellValue($letter_product_total.$i, '='.$letter_product_qty.$i.'*'.$letter_product_ht.$i); + + $activeSheet->getStyle($letter_product_ht.$i)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE ); + $activeSheet->getStyle($letter_product_ht.$i)->applyFromArray($borders_style); + $activeSheet->getStyle($letter_product_total.$i)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE ); + $activeSheet->getStyle($letter_product_total.$i)->applyFromArray($borders_style); + + $i++; + } + } + break; + } + + if ($type == 4) { + + $borders_style = array( + 'borders' => array( + 'allborders' => array( + 'style' => PHPExcel_Style_Border::BORDER_THIN + ) + ) + ); + + if($have_declinaison === true){ + $activeSheet->SetCellValue($letter_product_attribute_name.$i, 'Total'); + $activeSheet->getStyle($letter_product_attribute_name.$i)->getFont()->setBold(true); + $activeSheet->getStyle($letter_product_attribute_name.$i)->applyFromArray($borders_style); + } else { + $activeSheet->SetCellValue($letter_product_name.$i, 'Total'); + $activeSheet->getStyle($letter_product_name.$i)->getFont()->setBold(true); + $activeSheet->getStyle($letter_product_name.$i)->applyFromArray($borders_style); + } + + //$activeSheet->SetCellValue($letter_product_ht.$i, '=SUM('.$letter_product_ht.($iOri).':'.$letter_product_ht.($i-1).')'); + //$activeSheet->getStyle($letter_product_ht.$i)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE ); + //$activeSheet->getStyle($letter_product_ht.$i)->getFont()->setBold(true); + $activeSheet->getStyle($letter_product_ht.$i)->applyFromArray($borders_style); + + $activeSheet->SetCellValue($letter_product_multi.$i, '=SUM('.$letter_product_multi.($iOri).':'.$letter_product_multi.($i-1).')'); + $activeSheet->getStyle($letter_product_multi.$i)->getFont()->setBold(true); + $activeSheet->getStyle($letter_product_multi.$i)->applyFromArray($borders_style); + + $activeSheet->SetCellValue($letter_product_M1.$i, '=SUM('.$letter_product_M1.($iOri).':'.$letter_product_M1.($i-1).')'); + $activeSheet->getStyle($letter_product_M1.$i)->getFont()->setBold(true); + $activeSheet->getStyle($letter_product_M1.$i)->applyFromArray($borders_style); + + $activeSheet->SetCellValue($letter_product_stock3btk.$i, '=SUM('.$letter_product_stock3btk.($iOri).':'.$letter_product_stock3btk.($i-1).')'); + $activeSheet->getStyle($letter_product_stock3btk.$i)->getFont()->setBold(true); + $activeSheet->getStyle($letter_product_stock3btk.$i)->applyFromArray($borders_style); + + $activeSheet->SetCellValue($letter_product_missing.$i, '=SUM('.$letter_product_missing.($iOri).':'.$letter_product_missing.($i-1).')'); + $activeSheet->getStyle($letter_product_missing.$i)->getFont()->setBold(true); + $activeSheet->getStyle($letter_product_missing.$i)->applyFromArray($borders_style); + + $activeSheet->SetCellValue($letter_product_to_sent.$i, '=SUM('.$letter_product_to_sent.($iOri).':'.$letter_product_to_sent.($i-1).')'); + $activeSheet->getStyle($letter_product_to_sent.$i)->getFont()->setBold(true); + $activeSheet->getStyle($letter_product_to_sent.$i)->applyFromArray($borders_style); + + $activeSheet->SetCellValue($letter_product_to_stock.$i, '=SUM('.$letter_product_to_stock.($iOri).':'.$letter_product_to_stock.($i-1).')'); + $activeSheet->getStyle($letter_product_to_stock.$i)->getFont()->setBold(true); + $activeSheet->getStyle($letter_product_to_stock.$i)->applyFromArray($borders_style); + + $activeSheet->SetCellValue($letter_product_shipped.$i, '=SUM('.$letter_product_shipped.($iOri).':'.$letter_product_shipped.($i-1).')'); + $activeSheet->getStyle($letter_product_shipped.$i)->getFont()->setBold(true); + $activeSheet->getStyle($letter_product_shipped.$i)->applyFromArray($borders_style); + + $activeSheet->getColumnDimension($firstColumn)->setWidth(25); + $activeSheet->getColumnDimension($letter_product_ean)->setWidth(25); + $activeSheet->getColumnDimension($letter_product_ref_interne)->setWidth(25); + $activeSheet->getColumnDimension($letter_product_name)->setWidth(40); + if ($have_declinaison === true) { + $activeSheet->getColumnDimension($letter_product_attribute_name)->setWidth(20); + } + $activeSheet->getColumnDimension($letter_product_ht)->setWidth(13); + $activeSheet->getColumnDimension($letter_product_multi)->setWidth(13); + $activeSheet->getColumnDimension($letter_product_M1)->setWidth(13); + $activeSheet->getColumnDimension($letter_product_stock3btk)->setWidth(13); + $activeSheet->getColumnDimension($letter_product_missing)->setWidth(13); + $activeSheet->getColumnDimension($letter_product_to_sent)->setWidth(13); + $activeSheet->getColumnDimension($letter_product_to_stock)->setWidth(13); + $activeSheet->getColumnDimension($letter_product_shipped)->setWidth(13); + + $i++; + + $activeSheet->setCellValue($firstColumn.$i, ''); + + } else { + + $activeSheet->SetCellValue($firstColumn.$i, 'Total'); + $activeSheet->getStyle($firstColumn.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); + $activeSheet->getStyle($firstColumn.$i)->getFont()->setBold(true); + + $activeSheet->SetCellValue($letter_product_qty.$i, '=SUM('.$letter_product_qty.($iOri).':'.$letter_product_qty.($i-1).')'); + $activeSheet->getStyle($letter_product_qty.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); + $activeSheet->getStyle($letter_product_qty.$i)->getFont()->setBold(true); + if ($type == 2) { + $activeSheet->getStyle($letter_product_qty.$i)->getFill()->applyFromArray( + array( + 'type' => PHPExcel_Style_Fill::FILL_SOLID, + 'rotation' => 0, + 'startcolor' => array( + 'rgb' => 'D9D9D9' + ), + 'endcolor' => array( + 'argb' => 'D9D9D9D9' + ) + ) + ); + $activeSheet->SetCellValue($letter_product_total_qty.$i, '=SUM('.$letter_product_total_qty.($iOri).':'.$letter_product_total_qty.($i-1).')'); + $activeSheet->getStyle($letter_product_total_qty.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); + $activeSheet->getStyle($letter_product_total_qty.$i)->getFont()->setBold(true); + + $activeSheet->SetCellValue($letter_product_remaining_qty.$i, '=SUM('.$letter_product_remaining_qty.($iOri).':'.$letter_product_remaining_qty.($i-1).')'); + $activeSheet->getStyle($letter_product_remaining_qty.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); + $activeSheet->getStyle($letter_product_remaining_qty.$i)->getFont()->setBold(true); + } + + $activeSheet->getStyle($firstColumn.($iOri-1).':'.$lastColumn.($iOri-1))->applyFromArray($arrayBorder); + $activeSheet->getStyle($firstColumn.$i.':'.$lastColumn.$i)->applyFromArray($arrayBorder); + + $i++; + + $activeSheet->setCellValue($letter_product_ht.$i, $lang['Total Amount']); + $activeSheet->getStyle($letter_product_ht.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); + if($type == 1) + $activeSheet->getStyle($letter_product_ht.$i)->getFont()->setBold(true); + + $activeSheet->setCellValue($letter_product_total.$i, '=SUM('.$letter_product_total.($iOri).':'.$letter_product_total.($i-2).')'); + $activeSheet->getStyle($letter_product_total.$i)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE ); + + if($lang_id != 'en') + { + $i++; + + $activeSheet->setCellValue($letter_product_ht.$i, 'TVA '.$TVA); + $activeSheet->getStyle($letter_product_ht.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); + if($type == 1) + $activeSheet->getStyle($letter_product_ht.$i)->getFont()->setBold(true); + + $activeSheet->setCellValue($letter_product_total.$i, '='.$letter_product_total.($i-1).'*'.($TVA/100) ); + $activeSheet->getStyle($letter_product_total.$i)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE ); + + $i++; + + $activeSheet->setCellValue($letter_product_ht.$i, 'TOTAL TTC'); + $activeSheet->getStyle($letter_product_ht.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); + $activeSheet->getStyle($letter_product_ht.$i)->getFont()->setBold(true); + + $activeSheet->setCellValue($letter_product_total.$i, '='.$letter_product_total.($i-1).'+'.$letter_product_total.($i-2) ); + $activeSheet->getStyle($letter_product_total.$i)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE ); + $activeSheet->getStyle($letter_product_total.$i)->getFont()->setBold(true); + + $activeSheet->getStyle($letter_product_ht.($i-3).':'.$lastColumn.$i)->applyFromArray($arrayBorder); + } + else + { + $activeSheet->getStyle($letter_product_ht.($i-1).':'.$lastColumn.$i)->applyFromArray($arrayBorder); + } + if($type == 1) + { + $value = Configuration::get('PS_IT_OF_BAS_PAGE_'.$lang_id); + if($value != '') + { + $i++; + $value = str_replace('
', ' ' , nl2br($value)); + + $activeSheet->setCellValue($firstColumn.$i, $value); + $activeSheet->mergeCells($firstColumn.$i.':'.$lastColumn.$i ); + $activeSheet->getRowDimension($i)->setRowHeight(80); + $activeSheet->getStyle($firstColumn.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP); + $activeSheet->getStyle($firstColumn.$i)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_TEXT ); + $activeSheet->getStyle($firstColumn.$i)->getAlignment()->setWrapText(true); + } + + $value = Configuration::get('PS_IT_OF_BAS_PAGE_BIS_'.$lang_id); + if($value != '') + { + $i++; + $value = str_replace('
', ' ' , nl2br($value)); + + $activeSheet->setCellValue($firstColumn.$i, $value); + $activeSheet->mergeCells($firstColumn.$i.':'.$lastColumn.$i ); + $activeSheet->getRowDimension($i)->setRowHeight(80); + $activeSheet->getStyle($firstColumn.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP); + $activeSheet->getStyle($firstColumn.$i)->getAlignment()->setWrapText(true); + $activeSheet->getStyle($firstColumn.$i)->getFont()->getColor()->setRGB('FF0000'); + $activeSheet->getStyle($firstColumn.$i)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_TEXT ); + } + } + + $activeSheet->getColumnDimension($firstColumn)->setWidth(25); + $activeSheet->getColumnDimension($letter_product_ean)->setWidth(25); + $activeSheet->getColumnDimension($letter_product_ref_interne)->setWidth(25); + $activeSheet->getColumnDimension($letter_product_qty)->setWidth(18); + $activeSheet->getColumnDimension($letter_product_name)->setWidth(40); + if($have_declinaison === true) + $activeSheet->getColumnDimension($letter_product_attribute_name)->setWidth(20); + + $activeSheet->getColumnDimension($letter_product_qty)->setWidth(13); + if ($type == 2) { + $activeSheet->getColumnDimension($letter_product_total_qty)->setWidth(13); + $activeSheet->getColumnDimension($letter_product_remaining_qty)->setWidth(13); + } + $activeSheet->getColumnDimension($letter_product_ht)->setWidth(13); + $activeSheet->getColumnDimension($letter_product_total)->setWidth(13); + + $i++; + + $activeSheet->setCellValue($firstColumn.$i, ''); + } + + $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); + if($type == 1 && Validate::isLoadedObject($OrderForm)) + { + $objWriter->save($OrderForm->getFile()); + } + else + { + $objWriter->save(dirname(__FILE__).'/extracts/sales_logistique/'.$title.'.xlsx'); + } + exit; +} \ No newline at end of file diff --git a/cron_sale_cache.php b/cron_sale_cache.php new file mode 100644 index 0000000..ce32afd --- /dev/null +++ b/cron_sale_cache.php @@ -0,0 +1,28 @@ +getValue(' + SELECT MIN(`id_product`) FROM `'._DB_PREFIX_.'product` WHERE `date_add` > DATE_SUB(NOW(), INTERVAL 10 DAY) +'); +Db::getInstance()->ExecuteS(' + DELETE FROM `'._DB_PREFIX_.'product_ps_cache` + WHERE `id_product` >= '.$min_id_product.' +'); + +Db::getInstance()->ExecuteS(' + INSERT IGNORE INTO `'._DB_PREFIX_.'product_ps_cache` ( + SELECT p.id_product, IFNULL( + ( + SELECT s.id_sale + FROM `'._DB_PREFIX_.'privatesale_category` s + WHERE s.`id_category` = p.`id_category_default` + LIMIT 1) + , 0 + ) + FROM `'._DB_PREFIX_.'product` p + WHERE p.`id_product` >= '.$min_id_product.' + ) +'); diff --git a/cron_sales.php b/cron_sales.php new file mode 100644 index 0000000..dae8254 --- /dev/null +++ b/cron_sales.php @@ -0,0 +1,129 @@ +ExecuteS(' + SELECT `id_sale` + FROM `'._DB_PREFIX_.'privatesale` + WHERE `date_start` < "'.pSQL($argv[1]).' 00:00:00" + AND `id_sale` > 3000 +') as $row) { + $sale = new Sale($row['id_sale']); + $e = new CronExport('privatesales', array( + 'headers' => array( + 'id_sale', + 'sale_title', + 'id_order', + 'id_customer', + 'gender', + 'email', + 'firstname', + 'lastname', + 'shipping_firstname', + 'shipping_lastname', + 'shipping_street', + 'shipping_street2', + 'shipping_postcode', + 'shipping_city', + 'shipping_phone', + 'shipping_phone_mobile', + 'other_info', + 'shipping_country', + 'id_product', + 'id_product_attribute', + 'order_product_name', + 'product_quantity', + 'product_price_base_wo_taxes', + 'tax_rate', + 'product_name', + 'product_combination', + 'product_price_wo_taxes', + 'product_price', + 'wholesale_price', + 'combination_wholesale_price', + 'supplier_reference', + 'quantity_refunded', + 'quantity_returned', + 'total_shipping', + 'single', + 'date', + 'invoice_number', + 'payment_type', + 'order_state', + 'delivery_mode', + 'version', + 'family', + 'domaine', + ), + 'postfix' => '_'.$id_sale.'_manual', + 'dest_path' => '/../extracts/sales', + 'params' => array( + 'sale' => $sale, + 'date_start' => date('Y-m-d 00:00:00', strtotime($argv[1].' 00:00:00') - 7*86400), + 'date_end' => date('Y-m-d 23:59:59', strtotime($argv[1].' 00:00:00') - 1), + ), + )); + $e->run(); + + $export = fopen($e->filename, 'r'); + fgets($export); + while(!feof($export)) { + fwrite($file, fgets($export)); + } + fclose($export); + unlink($e->filename); +} + +fclose($file); diff --git a/cron_sales_accepted.php b/cron_sales_accepted.php new file mode 100755 index 0000000..a7abc53 --- /dev/null +++ b/cron_sales_accepted.php @@ -0,0 +1,122 @@ +ExecuteS(' + SELECT `id_sale` + FROM `'._DB_PREFIX_.'privatesale` + WHERE `date_start` < "'.pSQL($argv[1]).' 00:00:00" +') as $row) { + $sale = new Sale($row['id_sale']); + $e = new CronExport('privatesales', array( + 'headers' => array( + 'id_sale', + 'sale_title', + 'id_order', + 'id_customer', + 'gender', + 'email', + 'firstname', + 'lastname', + 'shipping_firstname', + 'shipping_lastname', + 'shipping_street', + 'shipping_street2', + 'shipping_postcode', + 'shipping_city', + 'shipping_phone', + 'shipping_phone_mobile', + 'other_info', + 'shipping_country', + 'id_product', + 'id_product_attribute', + 'order_product_name', + 'product_quantity', + 'product_price_base_wo_taxes', + 'tax_rate', + 'product_name', + 'product_combination', + 'product_price_wo_taxes', + 'product_price', + 'wholesale_price', + 'combination_wholesale_price', + 'supplier_reference', + 'quantity_refunded', + 'quantity_returned', + 'total_shipping', + 'single', + 'date', + 'invoice_number', + 'payment_type', + 'order_state', + 'version', + ), + 'postfix' => '_'.$id_sale.'_manual_accepted', + 'dest_path' => '/../extracts/sales', + 'params' => array( + 'sale' => $sale, + 'date_start' => date('Y-m-d 00:00:00', strtotime($argv[1].' 00:00:00') - 60*86400), + 'date_end' => date('Y-m-d 23:59:59', strtotime($argv[1].' 00:00:00') - 1), + ), + )); + $e->run(); + + $export = fopen($e->filename, 'r'); + fgets($export); + while(!feof($export)) { + fwrite($file, fgets($export)); + } + fclose($export); + unlink($e->filename); +} + +fclose($file); diff --git a/cron_weekly_stats.php b/cron_weekly_stats.php new file mode 100644 index 0000000..cdc223c --- /dev/null +++ b/cron_weekly_stats.php @@ -0,0 +1,203 @@ +ExecuteS(' + SELECT `id_sale`, `id_category` + FROM `'._DB_PREFIX_.'privatesale` + WHERE `date_end` >= DATE_FORMAT("'.$now.'" - INTERVAL 7 DAY, \'%Y-%m-%d 00:00:00\') + AND `date_end` < DATE_FORMAT("'.$now.'", \'%Y-%m-%d 00:00:00\') + ORDER BY `date_end` +'); +//echo "
";var_dump($sales);echo "
";die(); +foreach($sales as $row) { + + //echo $row['id_sale']." - ".$row['id_category']."\n"; + + $id_category = (int) $row['id_category']; + $category = new Category($id_category, $id_lang); + $sale = new Sale((int)$row['id_sale']); + $products = $category->getProductsStats($id_lang , "1", "1000000000000000000", "reference",NULL , FALSE, FALSE , FALSE ); + + $arrayProduct = array(); + foreach($products as $product) + $array_product[$product['id_product']] = $arrayProduct[$product['id_product']] = new Product($product['id_product']); + + $order_states = explode(',', Configuration::get('PS_IT_OF_ORDER_STATES')); + $lines = MakeStats::getOrdersByIdsProduct(array_keys($arrayProduct), $order_states); + // for stats + $lines_tmp = $lines; + $lines = MakeStats::getArrayLines($lines); + + $stats = new SaleStats($sale, $category); + $stats->loadData(); + $total_sale_product = array(); + $total_sale_ht = 0; + + foreach ($lines_tmp as $key => $order_line) { + $id_product = $order_line['product_id']; + $id_product_attribute = $order_line['product_attribute_id']; + + $current_product = $array_product[$id_product]; + if($id_product_attribute != 0) { + $combination = new Combination($id_product_attribute); + if($combination->wholesale_price == 0) { + $wholesale_price = $current_product->wholesale_price; + } else { + $wholesale_price = $combination->wholesale_price; + } + } else { + $wholesale_price = $current_product->wholesale_price; + } + + $quantity_to_cmd = $order_line['product_quantity'] - $order_line['product_quantity_reinjected']; + $total_sale_ht += $quantity_to_cmd * $wholesale_price; + if (!is_array($total_sale_product[(int)$id_product]) ){ + $total_sale_product[(int)$id_product] = array(); + $total_sale_product[(int)$id_product]['total_ht'] = 0; + $total_sale_product[(int)$id_product]['quantiy'] = 0; + $total_sale_product[(int)$id_product]['name'] = $current_product->name[2]; + } + + $total_sale_product[(int)$id_product]['total_ht'] += ($wholesale_price * $quantity_to_cmd); + $total_sale_product[(int)$id_product]['quantiy'] += $quantity_to_cmd; + } + + $stats->addToStats('total_bdc_ht', $total_sale_ht); + $bestsales = MakeStats::orderMultiArray($total_sale_product, 'total_ht'); + $bestsales_extract = array_slice($bestsales, 0, 3); + + $i = 1; + foreach ($bestsales_extract as $key => $bestsalesProduct) { + $stats->addToStats('bestsale_'.$i.'_product', $bestsalesProduct['name']); + $stats->addToStats('bestsale_'.$i.'_quantity', $bestsalesProduct['quantiy']); + $stats->addToStats('bestsale_'.$i.'_ca', $bestsalesProduct['total_ht']); + $i++; + } + $stats->loadDataForSubCategory(); + + $csv_row = array( + $row['id_sale'], + $stats->data_stats['name'], + $stats->data_stats['date_start'], + $stats->data_stats['date_end'], + $stats->data_stats['duration'], + $stats->data_stats['nb_ref'], + $stats->data_stats['nb_quantity_start'], + $stats->data_stats['nb_order_simple'], + $stats->data_stats['nb_order_multi'], + $stats->data_stats['nb_order_total'], + $stats->data_stats['taux_multi'], + $stats->data_stats['quantity_sale'], + $stats->data_stats['percent_sale'], + $stats->data_stats['percent_out_of_stock'], + Tools::displayPrice($stats->data_stats['ca_ttc']), + Tools::displayPrice($stats->data_stats['total_bdc_ht']), + $stats->data_stats['marge'], + (isset($stats->data_stats['bestsale_1_product'])?$stats->data_stats['bestsale_1_product']:''), + (isset($stats->data_stats['bestsale_1_quantity'])?$stats->data_stats['bestsale_1_quantity']:''), + (isset($stats->data_stats['bestsale_1_ca'])?Tools::displayPrice($stats->data_stats['bestsale_1_ca']):0), + (isset($stats->data_stats['bestsale_2_product'])?$stats->data_stats['bestsale_2_product']:''), + (isset($stats->data_stats['bestsale_2_quantity'])?$stats->data_stats['bestsale_2_quantity']:''), + (isset($stats->data_stats['bestsale_2_ca'])?Tools::displayPrice($stats->data_stats['bestsale_2_ca']):0), + (isset($stats->data_stats['bestsale_3_product'])?$stats->data_stats['bestsale_3_product']:''), + (isset($stats->data_stats['bestsale_3_quantity'])?$stats->data_stats['bestsale_3_quantity']:''), + (isset($stats->data_stats['bestsale_3_ca'])?Tools::displayPrice($stats->data_stats['bestsale_3_ca']):0), + ); + + foreach ($stats->data_subcategories as $key => $subcategories) { + foreach ($subcategories as $key_2 => $subcategory) { + if ($key_2 == 'total_amount_wholesale_price') { + $csv_row[] = Tools::displayPrice($subcategory); + } else { + $csv_row[]= $subcategory; + } + $i++; + } + } + + fwrite($f, implode(';', $csv_row)."\n"); +} + +fclose($f); + +exit; \ No newline at end of file diff --git a/export_cart.php b/export_cart.php new file mode 100644 index 0000000..3b2d6d9 --- /dev/null +++ b/export_cart.php @@ -0,0 +1,56 @@ +format('Y-m-d').'.csv', 'w'); + $batch = 50; + + $i = 0; + $data = ''; + foreach ( Db::getInstance()->executeS(' + SELECT CONCAT(c.lastname," ", c.firstname) as name, ca.name carrier, a.id_cart, a.id_customer, a.id_currency, a.date_add + FROM ps_cart a + LEFT JOIN `ps_customer` c ON (c.`id_customer` = a.`id_customer`) + LEFT JOIN `ps_carrier` ca ON (ca.`id_carrier` = a.`id_carrier`) + WHERE a.date_add >= "'.$date_current->format('Y-m-d').' 00:00:00" + AND a.date_add <= "'.$date_current->format('Y-m-d').' 23:59:59" + ') as $key => $cart) { + + $cartObj = new Cart($cart['id_cart']); + $data.= $cart['id_cart'].';'.$cart['name'].';'.$cartObj->getOrderTotal().';'.$cart['carrier'].';'.$cart['date_add']."\n"; + $i++; + if($i % $batch == 0) { + writeFile($file, $data); + $data = ''; + $i = 0; + } + } + writeFile($file, $data); + + fwrite($export_total, file_get_contents('tmp/export-'.$date_current->format('Y-m-d').'.csv') ); + fclose($file); + unlink('tmp/export-'.$date_current->format('Y-m-d').'.csv'); + $date_current->add(new DateInterval('P1D')); +} + +fclose($export_total); + +function writeFile($file, $data) { + fwrite($file, $data); +} diff --git a/export_laposte.php b/export_laposte.php new file mode 100755 index 0000000..341a272 --- /dev/null +++ b/export_laposte.php @@ -0,0 +1,298 @@ +format('Y'))); + + // Set Visitor / Customer TimeZone (If server not configured or using e-commerce website for ex.) + $easterDate->setTimezone(new DateTimeZone('Europe/Paris')); + $date->setTimezone(new DateTimeZone('Europe/Paris')); + + // Fixed date list + $publicHolidaysList = array( + new DateTime($date->format('Y-1-1')), // New Year + new DateTime($date->format('Y-5-1')), // Labor day + new DateTime($date->format('Y-5-8')), // 1945 + new DateTime($date->format('Y-7-14')), // National Holiday + new DateTime($date->format('Y-8-15')), // Assumption + new DateTime($date->format('Y-11-1')), // All Saints Day + new DateTime($date->format('Y-11-11')), // Armistice + new DateTime($date->format('Y-12-25')), // Christmas + ); + + $easterDate1 = new DateTime($easterDate->format('Y-m-d')); + $easterDate1->modify('+1 day'); + + $easterDate2 = new DateTime($easterDate->format('Y-m-d')); + $easterDate2->modify('+39 day'); + + $easterDate3 = new DateTime($easterDate->format('Y-m-d')); + $easterDate3->modify('+50 day'); + + $publicHolidaysList[] = $easterDate1; + $publicHolidaysList[] = $easterDate2; + $publicHolidaysList[] = $easterDate3; + + // Sort DateTime + usort($publicHolidaysList, function($a, $b) { + $interval = $a->diff($b); + return $interval->invert? 1: -1; + }); + return $publicHolidaysList; +} + +$holidays = getPublicHolidays(); + +$contract = Configuration::get('LAPOSTEWS_API_CONTRACT'); +$contract2 = Configuration::get('LAPOSTEWS_API_CONTRACT2'); + +$now = new Datetime(); +$now->setTimezone(new DateTimeZone('Europe/Paris')); +//$now->modify('-1 day'); + +$id_orders = array(); +$orders_to_print = array(); + +$f = fopen(dirname(__FILE__).'/extracts/itinsell/'.$now->format('Y-m-d').'_laposte.csv', 'w'); +//$f = fopen(dirname(__FILE__).'/extracts/itinsell/'.$now->format('Y-m-d').'_philea_laposte.csv', 'w'); +csv($f, array( + 'shipping_number', + 'product_code', + 'reference', + 'recipient', + 'address1', + 'address2', + 'address3', + 'address4', + 'postcode', + 'city', + 'country', + 'phone', + 'email', + 'weight', + 'parcel_quantity', + 'saturday_delivery', + 'oversized', + 'instructions', + 'contract', + 'summary_number', + 'summary_date', + 'description', + 'value', + 'order_value', + 'customer_shipping_value', + 'pod_value', + 'insurance_value', + 'shipping_value', + 'delivery_date', + 'id_delivery_point', +)); +// $orders_laposte = array(); +$orders_laposte = Db::getInstance()->ExecuteS(' + SELECT l.`shipping_number`, o.`id_order`, o.`id_cart`, o.`id_address_delivery`, l.`date_add` + FROM `'._DB_PREFIX_.'lapostews` l + LEFT JOIN `'._DB_PREFIX_.'order_detail` d + ON l.`id_order_detail` = d.`id_order_detail` + LEFT JOIN `'._DB_PREFIX_.'orders` o + ON d.`id_order` = o.`id_order` + WHERE l.`date_add` >= "'.pSQL($now->format('Y-m-d 00:00:00')).'" and l.`date_add` <= "'.pSQL($now->format('Y-m-d 23:59:59')).'" + GROUP BY l.`shipping_number` +'); + +// $orders_philea = Db::getInstance()->ExecuteS(' +// SELECT pp.`shipping_number`, o.`id_order`, o.`id_cart`, o.`id_address_delivery`, pp.`date_add` +// FROM `'._DB_PREFIX_.'philea_parcel` pp +// LEFT JOIN `'._DB_PREFIX_.'order_detail` d +// ON pp.`id_order_detail` = d.`id_order_detail` +// LEFT JOIN `'._DB_PREFIX_.'orders` o +// ON d.`id_order` = o.`id_order` +// WHERE pp.`date_add` >= "'.pSQL($now->format('Y-m-d 00:00:00')).'" and pp.`date_add` <= "'.pSQL($now->format('Y-m-d 23:59:59')).'" +// GROUP BY pp.`shipping_number` +// '); +// $orders = array_merge($orders_laposte, $orders_philea); + +foreach($orders_laposte as $row) { + $orders_to_print[] = (int) $row['id_order']; + + $delivery_so = Db::getInstance()->getRow(' + SELECT * + FROM `'._DB_PREFIX_.'socolissimo_delivery_info` + WHERE `id_cart` = '.(int) $row['id_cart'].' + '); + + $delivery = Db::getInstance()->getRow(' + SELECT * + FROM `'._DB_PREFIX_.'address` + WHERE `id_address` = '.(int) $row['id_address_delivery'].' + '); + + $delivery_date = FALSE; + + $hour = explode(' ', $row['date_add']); + $hour = explode(':', $hour[1]); + + if($delivery_so && $delivery_so['cecountry'] == 'FR' || $delivery['id_country'] == 8) { + $delivery_date = new DateTime($now->format('Y-m-d')); + $delivery_date->setTimezone(new DateTimeZone('Europe/Paris')); + $delivery_date->modify('+1 day'); + + if((int) $hour[0] > 16 || ((int) $hour[0] == 16 && (int) $hour[1] >= 30)) { + $delivery_date->modify('+1 day'); + } + + $i = 1; + while($i > 0) { + $is_holiday = FALSE; + foreach($holidays as $holiday) { + if($holiday->format('Y-m-d') == $delivery_date->format('Y-m-d')) { + $is_holiday = TRUE; + break; + } + } + + if((int) $delivery_date->format('w') == 0) { + $is_holiday = TRUE; + } + + $delivery_date->modify('+1 day'); + + if(!$is_holiday) { + $i--; + } + } + } + + if(!$delivery_so) { + csv($f, array( + $row['shipping_number'], + '', + $row['id_order'], + ($delivery['company'] != ''? $delivery['company'].' - ': '').$delivery['firstname'].' '.$delivery['lastname'], + trim(str_replace(array("\n", "\r"), ' ', $delivery['address1'])), + trim(str_replace(array("\n", "\r"), ' ', $delivery['address2'])), + '', + '', + (string) $delivery['postcode'], + $delivery['city'], + Country::getIsoById((int) $delivery['id_country']), + str_replace(' ', '', (string) ($delivery['phone_mobile'] != ''? $delivery['phone_mobile']: $delivery['phone'])), + Db::getInstance()->getValue('SELECT `email` FROM `'._DB_PREFIX_.'customer` WHERE `id_customer` = '.(int) $delivery['id_customer']), + 0.24, + 1, + 1, + 'N', + trim(str_replace(array("\n", "\r"), ' ', $delivery['other'])), + (int) $delivery['id_country'] == 8? $contract: $contract2, + $now->format('Ymd'), + $now->format('d/m/Y'), + '', + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + $delivery_date? $delivery_date->format('d/m/Y'): '', + '', + )); + } else { + csv($f, array( + $row['shipping_number'], + '', + $row['id_order'], + str_replace(array("\n", "\r"), ' ', ($delivery_so['prcompladress'] != ''? $delivery_so['prcompladress'].' - ': '').($delivery_so['cecompanyname'] != ''? $delivery_so['cecompanyname'].' - ': '').$delivery_so['prfirstname'].' '.$delivery_so['prname']), + trim(str_replace(array("\n", "\r"), ' ', $delivery_so['pradress3'])), + trim(str_replace(array("\n", "\r"), ' ', $delivery_so['pradress4'])), + trim(str_replace(array("\n", "\r"), ' ', $delivery_so['pradress1'])), + trim(str_replace(array("\n", "\r"), ' ', $delivery_so['pradress2'])), + (string) $delivery_so['przipcode'], + $delivery_so['prtown'], + $delivery_so['cecountry'], + str_replace(' ', '', $delivery_so['cephonenumber'] != ''? (string) $delivery_so['cephonenumber']: ((string) ($delivery['phone_mobile'] != ''? $delivery['phone_mobile']: $delivery['phone']))), + $delivery_so['ceemail'], + 0.24, + 1, + 1, + 'N', + trim(str_replace(array("\n", "\r"), ' ', $delivery_so['cedeliveryinformation'])), + $delivery_so['cecountry'] == 'FR'? $contract: $contract2, + $now->format('Ymd'), + $now->format('d/m/Y'), + '', + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + $delivery_date? $delivery_date->format('d/m/Y'): '', + $delivery_so['prid'], + )); + } + + $pdf = new PDF('P', 'mm', 'A4'); + $order = new Order((int) $row['id_order']); + if(Validate::isLoadedObject($order)) { + $id_orders[] = (int) $order->id; + PDF::invoice($order, 'F', TRUE, $pdf); + $pdf->Output(dirname(__FILE__).'/extracts/invoices/'.(int) $order->id.'.pdf', 'F'); + } + +} + +fclose($f); + +/** + * Regeneration invoices for specific orders + * using this includes to comment all about csv + */ +// $orders_to_reprint = array(350385,302554,300088,286495,290190,296685,290539,291935,287612,284108,285930,285284,288626,290659,295252,292214,287915,278572,282846,281120,281324,292999,279299,278681,276840,272366,274417,276233,272552,267126,273451,267563,266108,267186,269235,258571,257172,258306,259788,259000,259547,254186,225736,334010,332694,325773,325773,323595,323595,306923,306923,304776,304776,302960,302960,305645,305645,285700,274391,272544,271819,265852,260292,259059,265544,257100,234423,240829,240829,233767,217497,205633,203823,201126,187387,185462,179854,166753,176233,183491,177402,369986,367072,367383,367970,368338,339107,339107,338759,334924,327402,320364,320364,332667,332667,320441,320441,311542,311542,308895,308895,307577,307577,266573,277760,274366,274318,273079,257063,256322,250723,245678,238412,237659,196253,192709,180700,180046,175868,381283,369164,367304,367982,367104,370224,366966,370107,366177,369315,362975,367252,361380,365123,370909,359931,360698,363246,358396,361919,361608,357811,357270,367977,357509,356946,357199,357199,351217,351217,335669,335669,339127,336501,334378,334378,348116,348116,335330,336653,336653,331109,330480,330480,332355,332355,328706,328706,338370,338370,330071,330071,328154,328154,327939,327939,327736,327736,328201,328201,324345,324345,324978,324978,323388,323388,324598,324598,320183,320183,320935,320935,319958,319958,302878,292803,292803,298027,297052,298746,291544,292090,290810,290168,287607,287277,285811,289946,289048,285813,287451,285987,291066,289082,288720,286209,283027,289347,284717,295020,283355,280889,284988,278545,284070,282825,280327,279775,277188,278137,278137,281455,277836,271812,275407,274469,279319,271066,270306,271764,273388,267492,272076,277800,284796,284462,275265,270320,270486,270171,269886,266411,266333,268554,264148,265590,262954,262851,260846,262689,261596,262103,261976,261521,262447,260983,259890,262905,260747,261067,258471,243813,225221,229011,222430,203373,204814,206033,204894,200585,200839,200125,198425,188263,197940,194892,187277,188266,176280,372718,356628,357331,357178,357326,353230,353230,356461,356461,353210,353210,363733,363733,351215,353296,349932,350221,350221,349221,349358,349221,349894,348980,349745,345763,347674,347674,356852,346516,346516,345297,345297,347205,347205,343485,343485,345911,345911,344243,344243,354164,354164,342709,340429,340429,342041,342041,314782,314782,317374,317374,315947,315947,312628,312628,313425,313425,312877,312877,311916,311916,313144,311534,311534,310128,310128,309820,309820,310808,310808,309611,309611,308859,308859,307387,307387,306365,306365,305878,305878,315960,315960,304861,304861,306712,304406,304406,307734,307734,303634,303634,301336,301336,301295,301295,302048,302048,301446,301446,301446,301446,309768,309768,307734,301235,305236,304360,294794,256386,267588,259733,255594,250994,255479,253251,252393,252441,251929,252987,252734,249873,250484,249240,248519,248202,256012,250175,248340,250448,248500,246880,249146,247554,247139,246179,246930,245615,246352,241094,244392,241484,241314,240026,240797,240427,239982,240330,237796,237127,238402,239833,239833,245077); +// foreach ($orders_to_reprint as $key => $order_id) { +// $pdf = new PDF('P', 'mm', 'A4'); +// $order = new Order((int) $order_id); +// if(Validate::isLoadedObject($order)) { +// $id_orders[] = (int) $order->id; +// PDF::invoice($order, 'F', TRUE, $pdf); +// $pdf->Output(dirname(__FILE__).'/extracts/invoices/'.(int) $order->id.'.pdf', 'F'); +// } +// } + +$ftp = ftp_connect('ftp.itrack.itinsell.com'); +ftp_login($ftp, '800914', 'xt954hVUbg'); +ftp_put($ftp, 'CSV/'.$now->format('Y-m-d').'_bbb_laposte.csv', dirname(__FILE__).'/extracts/itinsell/'.$now->format('Y-m-d').'_laposte.csv', FTP_BINARY); +foreach($id_orders as $id_order) { + ftp_put($ftp, 'FacturesCommerciales/'.(int) $id_order.'.pdf', dirname(__FILE__).'/extracts/invoices/'.(int) $id_order.'.pdf', FTP_BINARY); + unlink(dirname(__FILE__).'/extracts/invoices/'.(int) $id_order.'.pdf'); +} +ftp_close($ftp); diff --git a/export_multi_monthly.php b/export_multi_monthly.php new file mode 100755 index 0000000..9b4b3a5 --- /dev/null +++ b/export_multi_monthly.php @@ -0,0 +1,783 @@ +ExecuteS(' + SELECT id_parent, id_category + FROM `'._DB_PREFIX_.'category` + WHERE `id_parent` = '.$id + ); +} + +function getCat($id=0, $title='') { + $products = Db::getInstance()->ExecuteS(' + SELECT id_product, "'.addslashes($title).'" AS `title` + FROM `'._DB_PREFIX_.'product` + WHERE `id_category_default` = '.$id + ); + foreach(getChildren($id) as $child) { + $products = array_merge($products, getCat($child['id_category'])); + } + return $products; +} + +$f = fopen('extracts/monthly_compta/'.date('Y-m-d', strtotime($now)).'_big.csv', 'w'); + +$headers = array( + 'date', + //'sale_title', + 'id_order', + 'id_order_slip', + 'invoice_number', + 'multisale', + 'id_customer', + 'firstname', + 'lastname', + 'total_products_wo_taxes_210', + 'total_taxes_210', + 'total_products_wo_taxes_200', + 'total_taxes_200', + 'total_products_wo_taxes_196', + 'total_taxes_196', + 'total_products_wo_taxes_100', + 'total_taxes_100', + 'total_products_wo_taxes_55', + 'total_taxes_55', + 'total_products_wo_taxes_21', + 'total_taxes_21', + 'total_products_wo_taxes_export', + 'shipping_wo_taxes_196-200', + 'shipping_wo_taxes_export', + 'shipping_country', + 'discounts', + 'total_paid', + 'total_paid_real', + 'total_achat', + 'payment_type', + 'direct_payment', + 'date_send', + 'refund_reason', + 'new', +); + +fwrite($f, implode(';', $headers)."\n"); + + +$orders = Db::getInstance()->ExecuteS(' + SELECT o.id_order + FROM `'._DB_PREFIX_.'orders` o, + `'._DB_PREFIX_.'order_detail` d, + `'._DB_PREFIX_.'customer` c + WHERE + (o.valid = 1 + OR ( + o.valid = 0 + AND ( + (SELECT h.id_order_state FROM `'._DB_PREFIX_.'order_history` h WHERE h.id_order = o.id_order ORDER BY h.date_add DESC LIMIT 1) = 6 + OR (SELECT h.id_order_state FROM `'._DB_PREFIX_.'order_history` h WHERE h.id_order = o.id_order ORDER BY h.date_add DESC LIMIT 1) = 7 + OR (SELECT h.id_order_state FROM `'._DB_PREFIX_.'order_history` h WHERE h.id_order = o.id_order ORDER BY h.date_add DESC LIMIT 1) = 11 + ) + )) + AND c.id_customer = o.id_customer + AND d.id_order = o.id_order + AND o.date_add >= DATE_FORMAT("'.$now.'" - INTERVAL 4 MONTH, \'%Y-%m-01 00:00:00\') + AND o.date_add <= "'.$now.'" + GROUP BY o.id_order +'); + +foreach($orders as $o) { + $order = Db::getInstance()->ExecuteS(' + SELECT c.*, o.* + FROM `'._DB_PREFIX_.'orders` o, + `'._DB_PREFIX_.'customer` c + WHERE + o.id_order = '.$o['id_order'].' + AND c.`id_customer` = o.`id_customer` + '); + $order = $order[0]; + + $multi = Db::getInstance()->getValue(' + SELECT COUNT(DISTINCT `id_sale`) + FROM `'._DB_PREFIX_.'product_ps_cache` + WHERE `id_product` IN ( + SELECT `product_id` FROM `'._DB_PREFIX_.'order_detail` + WHERE `id_order` = '.(int) $order['id_order'].' + ) + ') == 1? 'S': 'M'; + + $address = Db::getInstance()->ExecuteS(' + SELECT a.*, c.`id_zone` + FROM `'._DB_PREFIX_.'address` a, `'._DB_PREFIX_.'country` c + WHERE a.`id_address` = '.$order['id_address_delivery'].' + AND a.`id_country` = c.`id_country` + '); + $address = $address[0]; + + $invoice_address = Db::getInstance()->ExecuteS(' + SELECT firstname, lastname + FROM `'._DB_PREFIX_.'address` + WHERE `id_address` = '.$order['id_address_invoice'] + ); + $invoice_address = $invoice_address[0]; + + $order_details = Db::getInstance()->ExecuteS(' + SELECT * + FROM `'._DB_PREFIX_.'order_detail` d + WHERE d.id_order = '.$o['id_order'] + ); + + $total_products_wo_taxes_210 = array(); + $total_taxes_210 = array(); + $total_products_wo_taxes_200 = array(); + $total_taxes_200 = array(); + $total_products_wo_taxes_196 = array(); + $total_taxes_196 = array(); + $total_products_wo_taxes_100 = array(); + $total_taxes_100 = array(); + $total_products_wo_taxes_55 = array(); + $total_taxes_55 = array(); + $total_products_wo_taxes_21 = array(); + $total_taxes_21 = array(); + $total_products_wo_taxes_export = array(); + + $country = Db::getInstance()->getValue(' + SELECT `name` + FROM `'._DB_PREFIX_.'country_lang` + WHERE `id_country` = ( + SELECT `id_country` + FROM `'._DB_PREFIX_.'address` + WHERE `id_address` = '.(int) $order['id_address_delivery'].' + LIMIT 1 + ) + AND `id_lang` = 2 + '); + + $date_send = Db::getInstance()->getValue(' + SELECT `date_add` + FROM `'._DB_PREFIX_.'order_history` + WHERE `id_order_state` = 4 + AND `id_order` = '.(int) $order['id_order'].' + ORDER BY `date_add` ASC + '); + + // calcul prix d'achat de la commande + $wholesale_price = Db::getInstance()->ExecuteS(' + SELECT `wholesale_price`, p.`id_product` + FROM `'._DB_PREFIX_.'product` p + LEFT JOIN `'._DB_PREFIX_.'order_detail` o ON o.`product_id` = p.`id_product` + WHERE o.`id_order` = '. (int) $order['id_order'] + ); + $prix_achat = array(); + foreach ($wholesale_price as $key => $price) { + $prix_achat[$price['id_product']] = $price['wholesale_price']; + } + $total_achat = 0; + + + $ldetails = array(); + foreach($order_details as $detail) { + + $total_achat += $detail['product_quantity'] * $prix_achat[$detail['product_id']]; + + $ldetails[$detail['id_order_detail']] = $detail; + + if($address['id_country'] != 19) { + if($detail['tax_rate'] == '20.000') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_200[] = (float) ($detail['product_quantity_discount'] / 1.2); + $total_taxes_200[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.2); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_200[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_200[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.2); + } else { + $total_products_wo_taxes_200[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_200[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.2); + } + /*}*/ + } elseif($detail['tax_rate'] == '21.000') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_196[] = (float) ($detail['product_quantity_discount'] / 1.196); + $total_taxes_196[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.196); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_210[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_210[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.21); + } else { + $total_products_wo_taxes_210[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_210[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.21); + } + /*}*/ + }elseif($detail['tax_rate'] == '19.600') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_196[] = (float) ($detail['product_quantity_discount'] / 1.196); + $total_taxes_196[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.196); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_196[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_196[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.196); + } else { + $total_products_wo_taxes_196[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_196[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.196); + } + /*}*/ + } elseif($detail['tax_rate'] == '10.000') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_100[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_100[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.100); + } else { + $total_products_wo_taxes_100[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_100[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.100); + } + } elseif($detail['tax_rate'] == '5.500') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_55[] = (float) ($detail['product_quantity_discount'] / 1.055); + $total_taxes_55[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.055); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_55[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_55[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.055); + } else { + $total_products_wo_taxes_55[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_55[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.055); + } + /*}*/ + } elseif($detail['tax_rate'] == '2.100') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_21[] = (float) ($detail['product_quantity_discount'] / 1.021); + $total_taxes_21[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.021); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_21[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + $total_taxes_21[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.021); + } else { + $total_products_wo_taxes_21[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + $total_taxes_21[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.021); + } + /*}*/ + } else { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_quantity_discount']); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + } else { + $total_products_wo_taxes_export[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + } + /*}*/ + } + } else { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_quantity_discount']); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']); + } else { + $total_products_wo_taxes_export[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']); + } + /*}*/ + } + } + + $_total_products_wo_taxes_210 = 0.0; + foreach($total_products_wo_taxes_210 as $value) { + $_total_products_wo_taxes_210 += $value; + } + $_total_taxes_210 = 0.0; + foreach($total_taxes_210 as $value) { + $_total_taxes_210 += $value; + } + + $_total_products_wo_taxes_200 = 0.0; + foreach($total_products_wo_taxes_200 as $value) { + $_total_products_wo_taxes_200 += $value; + } + $_total_taxes_200 = 0.0; + foreach($total_taxes_200 as $value) { + $_total_taxes_200 += $value; + } + + $_total_products_wo_taxes_196 = 0.0; + foreach($total_products_wo_taxes_196 as $value) { + $_total_products_wo_taxes_196 += $value; + } + $_total_taxes_196 = 0.0; + foreach($total_taxes_196 as $value) { + $_total_taxes_196 += $value; + } + + $_total_products_wo_taxes_100 = 0.0; + foreach($total_products_wo_taxes_100 as $value) { + $_total_products_wo_taxes_100 += $value; + } + $_total_taxes_100 = 0.0; + foreach($total_taxes_100 as $value) { + $_total_taxes_100 += $value; + } + + $_total_products_wo_taxes_55 = 0.0; + foreach($total_products_wo_taxes_55 as $value) { + $_total_products_wo_taxes_55 += $value; + } + $_total_taxes_55 = 0.0; + foreach($total_taxes_55 as $value) { + $_total_taxes_55 += $value; + } + + $_total_products_wo_taxes_21 = 0.0; + foreach($total_products_wo_taxes_21 as $value) { + $_total_products_wo_taxes_21 += $value; + } + $_total_taxes_21 = 0.0; + foreach($total_taxes_21 as $value) { + $_total_taxes_21 += $value; + } + + $_total_products_wo_taxes_export = 0.0; + foreach($total_products_wo_taxes_export as $value) { + $_total_products_wo_taxes_export += $value; + } + + // Find if it's a saved card or account for the payment (Direct payment) + $is_direct_payment = 0; + if($order['payment'] == 'Paybox') { + $is_direct_payment = Db::getInstance()->getValue(' + SELECT `is_saved_card` + FROM `'._DB_PREFIX_.'paybox_transaction` + WHERE `id_cart` = '.(int) $order['id_cart'].' + '); + } elseif ($order['payment'] == 'PayPal') { + $is_direct_payment = Db::getInstance()->getValue(' + SELECT `is_billing` + FROM `'._DB_PREFIX_.'paypal_order` + WHERE `id_order` = '.(int) $order['id_order'].' + '); + } + if((int)$is_direct_payment != 0) { + $order['direct_payment'] = "Oui"; + } else { + $order['direct_payment'] = "Non"; + } + + fwrite($f, implode(';', array( + $order['date_add'], + //addslashes(implode(' / ', $sale_names)), + $order['id_order'], + 0, + $order['invoice_number'], + $multi, + $order['id_customer'], + $invoice_address['firstname'], + $invoice_address['lastname'], + $_total_products_wo_taxes_210, + $_total_taxes_210, + $_total_products_wo_taxes_200, + $_total_taxes_200, + $_total_products_wo_taxes_196, + $_total_taxes_196, + $_total_products_wo_taxes_100, + $_total_taxes_100, + $_total_products_wo_taxes_55, + $_total_taxes_55, + $_total_products_wo_taxes_21, + $_total_taxes_21, + $_total_products_wo_taxes_export, + ($address['id_country'] != 19? (float) $order['total_shipping'] / (strtotime($order['date_add']) >= mktime(0, 0, 0, 1, 1, 2014)? 1.2: 1.196): 0.0), + ($address['id_country'] != 19? 0.0: $order['total_shipping']), + $country, + $order['total_discounts'], + $order['total_paid'], + $order['total_paid_real'], + $total_achat, + $order['payment'], + $order['direct_payment'], + $date_send, + '', + (int) Db::getInstance()->getValue(' + SELECT `id_order` + FROM `'._DB_PREFIX_.'orders` + WHERE `id_customer` = '.(int) $order['id_customer'].' + AND `id_order` != '.(int) $order['id_order'].' + ') == 0, + ))."\n"); +} + +// This order has been refunded +$slips = Db::getInstance()->ExecuteS(' + SELECT * + FROM `'._DB_PREFIX_.'order_slip` os + WHERE os.date_add >= DATE_FORMAT("'.$now.'" - INTERVAL 4 MONTH, \'%Y-%m-01 00:00:00\') + AND os.date_add <= "'.$now.'" +'); + +// $slips = Db::getInstance()->ExecuteS(' +// SELECT * +// FROM `'._DB_PREFIX_.'order_slip` os +// WHERE os.date_add >= "2014-12-01 00:00:00" +// AND os.date_add <= "2014-12-31 23:59:59" +// '); + +// $slips = Db::getInstance()->ExecuteS(' +// SELECT * +// FROM `'._DB_PREFIX_.'order_slip` os +// AND os.date_add >= "'. date('Y') .'-01-01 00:00:00" +// '); + + + +$refundreasons = array( + '', + 'CLIENT : Annulation pré-envoi', + 'CLIENT : Rétractation post-envoi', + 'BBB : Erreur Achat / Prod', + 'FEUR : Problème SAV', + 'FEUR : Produit manquant', + 'BBB : Erreur logistique', + 'Autre', + 'BBB : Pbme Site / Paiement', + 'TRANS : Colis détruit', + 'TRANS : Colis perdu', + 'CLIENT : Annulation pour ré-achat', + 'BBB : Suspicion de fraude', +); + +if(count($slips) > 0) { + foreach($slips as $slip) { + $order = Db::getInstance()->getRow(' + SELECT o.*, c.* + FROM `'._DB_PREFIX_.'orders` o + LEFT JOIN `'._DB_PREFIX_.'customer` c + ON o.`id_customer` = c.`id_customer` + WHERE + o.id_order = '.$slip['id_order'].' + '); + + $multi = Db::getInstance()->getValue(' + SELECT COUNT(DISTINCT `id_sale`) + FROM `'._DB_PREFIX_.'product_ps_cache` + WHERE `id_product` IN ( + SELECT `product_id` FROM `'._DB_PREFIX_.'order_detail` + WHERE `id_order` = '.(int) $order['id_order'].' + ) + ') == 1? 'S': 'M'; + + $address = Db::getInstance()->ExecuteS(' + SELECT a.*, c.`id_zone` + FROM `'._DB_PREFIX_.'address` a, `'._DB_PREFIX_.'country` c + WHERE a.`id_address` = '.$order['id_address_delivery'].' + AND a.`id_country` = c.`id_country` + '); + $address = $address[0]; + + $invoice_address = Db::getInstance()->ExecuteS(' + SELECT firstname, lastname + FROM `'._DB_PREFIX_.'address` + WHERE `id_address` = '.$order['id_address_invoice'] + ); + $invoice_address = $invoice_address[0]; + + $order_details = Db::getInstance()->ExecuteS(' + SELECT * + FROM `'._DB_PREFIX_.'order_detail` d + WHERE d.id_order = '.(int) $slip['id_order'].' + AND d.`id_order_detail` IN ( + SELECT `id_order_detail` + FROM `'._DB_PREFIX_.'order_slip_detail` + WHERE `id_order_slip` = '.(int) $slip['id_order_slip'].' + ) + '); + + $country = Db::getInstance()->getValue(' + SELECT `name` + FROM `'._DB_PREFIX_.'country_lang` + WHERE `id_country` = ( + SELECT `id_country` + FROM `'._DB_PREFIX_.'address` + WHERE `id_address` = '.(int) $order['id_address_delivery'].' + LIMIT 1 + ) + AND `id_lang` = 2 + '); + + $date_send = Db::getInstance()->getValue(' + SELECT `date_add` + FROM `'._DB_PREFIX_.'order_history` + WHERE `id_order_state` = 4 + AND `id_order` = '.(int) $order['id_order'].' + ORDER BY `date_add` ASC + '); + + // calcul prix d'achat de la commande + $wholesale_price_slip = Db::getInstance()->ExecuteS(' + SELECT `wholesale_price`, p.`id_product` + FROM `'._DB_PREFIX_.'product` p + LEFT JOIN `'._DB_PREFIX_.'order_detail` o ON o.`product_id` = p.`id_product` + WHERE o.`id_order` = '. (int) $slip['id_order'] + ); + + $prix_achat_slip = array(); + foreach ($wholesale_price_slip as $key => $price) { + $prix_achat_slip[$price['id_product']] = $price['wholesale_price']; + } + + $total_achat_slip = 0; + $ldetails = array(); + $total_products_wo_taxes_210 = array(); + $total_taxes_210 = array(); + $total_products_wo_taxes_200 = array(); + $total_taxes_200 = array(); + $total_products_wo_taxes_196 = array(); + $total_taxes_196 = array(); + $total_products_wo_taxes_100 = array(); + $total_taxes_100 = array(); + $total_products_wo_taxes_55 = array(); + $total_taxes_55 = array(); + $total_products_wo_taxes_21 = array(); + $total_taxes_21 = array(); + $total_products_wo_taxes_export = array(); + + foreach($order_details as $detail) { + $total_achat_slip += $detail['product_quantity'] * $prix_achat_slip[$detail['product_id']]; + /*$total_products_wo_taxes_196 = array(); + $total_taxes_196 = array(); + $total_products_wo_taxes_55 = array(); + $total_taxes_55 = array(); + $total_products_wo_taxes_21 = array(); + $total_taxes_21 = array(); + $total_products_wo_taxes_export = array();*/ + + $ldetails[$detail['id_order_detail']] = $detail; + + if($address['id_country'] != 19) { + if($detail['tax_rate'] == '19.600') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_196[] = (float) ($detail['product_quantity_discount'] / 1.196); + $total_taxes_196[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.196); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_196[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_196[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.196); + } else { + $total_products_wo_taxes_196[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_196[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.196); + } + /*}*/ + } elseif($detail['tax_rate'] == '10.000') { + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_100[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_100[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.100); + } else { + $total_products_wo_taxes_100[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_100[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.100); + } + } elseif($detail['tax_rate'] == '5.500') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_55[] = (float) ($detail['product_quantity_discount'] / 1.055); + $total_taxes_55[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.055); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_55[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_55[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.055); + } else { + $total_products_wo_taxes_55[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_55[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.055); + } + /*}*/ + } elseif($detail['tax_rate'] == '2.100') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_21[] = (float) ($detail['product_quantity_discount'] / 1.021); + $total_taxes_21[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.021); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_21[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_21[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.021); + } else { + $total_products_wo_taxes_21[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_21[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.021); + } + /*}*/ + } elseif($detail['tax_rate'] == '20.000') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_200[] = (float) ($detail['product_quantity_discount'] / 1.2); + $total_taxes_200[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.2); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_200[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_200[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.2); + } else { + $total_products_wo_taxes_200[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_200[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.2); + } + /*}*/ + } elseif($detail['tax_rate'] == '21.000') { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_200[] = (float) ($detail['product_quantity_discount'] / 1.2); + $total_taxes_200[] = (float) ($detail['product_quantity_discount'] - $detail['product_quantity_discount'] / 1.2); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_210[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + $total_taxes_210[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.21); + } else { + $total_products_wo_taxes_210[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + $total_taxes_210[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.21); + } + /*}*/ + } else { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_quantity_discount']); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + } else { + $total_products_wo_taxes_export[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + } + /*}*/ + } + } else { + /*if((float) $detail['product_quantity_discount'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_quantity_discount']); + } else {*/ + if((float) $detail['reduction_percent'] != 0.0) { + $total_products_wo_taxes_export[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']); + } else { + $total_products_wo_taxes_export[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']); + } + /*}*/ + } + } + + $_total_products_wo_taxes_210 = 0.0; + foreach($total_products_wo_taxes_210 as $value) { + $_total_products_wo_taxes_210 += $value; + } + $_total_taxes_210 = 0.0; + foreach($total_taxes_210 as $value) { + $_total_taxes_210 += $value; + } + + $_total_products_wo_taxes_200 = 0.0; + foreach($total_products_wo_taxes_200 as $value) { + $_total_products_wo_taxes_200 += $value; + } + $_total_taxes_200 = 0.0; + foreach($total_taxes_200 as $value) { + $_total_taxes_200 += $value; + } + + $_total_products_wo_taxes_196 = 0.0; + foreach($total_products_wo_taxes_196 as $value) { + $_total_products_wo_taxes_196 += $value; + } + $_total_taxes_196 = 0.0; + foreach($total_taxes_196 as $value) { + $_total_taxes_196 += $value; + } + + $_total_products_wo_taxes_100 = 0.0; + foreach($total_products_wo_taxes_100 as $value) { + $_total_products_wo_taxes_100 += $value; + } + $_total_taxes_100 = 0.0; + foreach($total_taxes_100 as $value) { + $_total_taxes_100 += $value; + } + + $_total_products_wo_taxes_55 = 0.0; + foreach($total_products_wo_taxes_55 as $value) { + $_total_products_wo_taxes_55 += $value; + } + $_total_taxes_55 = 0.0; + foreach($total_taxes_55 as $value) { + $_total_taxes_55 += $value; + } + + $_total_products_wo_taxes_21 = 0.0; + foreach($total_products_wo_taxes_21 as $value) { + $_total_products_wo_taxes_21 += $value; + } + $_total_taxes_21 = 0.0; + foreach($total_taxes_21 as $value) { + $_total_taxes_21 += $value; + } + + $_total_products_wo_taxes_export = 0.0; + foreach($total_products_wo_taxes_export as $value) { + $_total_products_wo_taxes_export += $value; + } + + // Find if it's a saved card or account for the payment (Direct payment) + $is_direct_payment = 0; + if($order['payment'] == 'Paybox') { + $is_direct_payment = Db::getInstance()->getValue(' + SELECT `is_saved_card` + FROM `'._DB_PREFIX_.'paybox_transaction` + WHERE `id_cart` = '.(int) $order['id_cart'].' + '); + } elseif ($order['payment'] == 'PayPal') { + $is_direct_payment = Db::getInstance()->getValue(' + SELECT `is_billing` + FROM `'._DB_PREFIX_.'paypal_order` + WHERE `id_order` = '.(int) $order['id_order'].' + '); + } + if((int)$is_direct_payment != 0) { + $order['direct_payment'] = "Oui"; + } else { + $order['direct_payment'] = "Non"; + } + + $refundreason = Db::getInstance()->getValue(' + SELECT `id_reason` + FROM `'._DB_PREFIX_.'refundreason` + WHERE `id_order_slip` = '.(int) $slip['id_order_slip'].' + '); + + $refundreason = $refundreasons[(int) $refundreason]; + + fwrite($f, implode(';', array( + $slip['date_add'], + //addslashes(implode(' / ', $sale_names)), + $order['id_order'], + $slip['id_order_slip'], + $order['invoice_number'], + $multi, + $order['id_customer'], + $invoice_address['firstname'], + $invoice_address['lastname'], + '-'.$_total_products_wo_taxes_210, + '-'.$_total_taxes_210, + '-'.$_total_products_wo_taxes_200, + '-'.$_total_taxes_200, + '-'.$_total_products_wo_taxes_196, + '-'.$_total_taxes_196, + '-'.$_total_products_wo_taxes_100, + '-'.$_total_taxes_100, + '-'.$_total_products_wo_taxes_55, + '-'.$_total_taxes_55, + '-'.$_total_products_wo_taxes_21, + '-'.$_total_taxes_21, + '-'.$_total_products_wo_taxes_export, + '-'.($slip['shipping_cost'] == 1? ($address['id_country'] != 19? (float) $order['total_shipping'] / (strtotime($order['date_add']) >= mktime(0, 0, 0, 1, 1, 2014)? 1.2: 1.196): 0.0): 0.0), + '-'.($slip['shipping_cost'] == 1? ($address['id_country'] != 19? 0.0: $order['total_shipping']): 0.0), + $country, + '-0.0', + '-'.($_total_products_wo_taxes_210 + $_total_taxes_210 + $_total_products_wo_taxes_200 + $_total_taxes_200 + $_total_products_wo_taxes_196 + $_total_taxes_196 + $_total_products_wo_taxes_55 + $_total_taxes_55 + $_total_products_wo_taxes_21 + $_total_taxes_21 + $_total_products_wo_taxes_export + ($slip['shipping_cost'] == 1? (float) $order['total_shipping']: 0.0)), + '-'.($_total_products_wo_taxes_210 + $_total_taxes_210 + $_total_products_wo_taxes_200 + $_total_taxes_200 + $_total_products_wo_taxes_196 + $_total_taxes_196 + $_total_products_wo_taxes_55 + $_total_taxes_55 + $_total_products_wo_taxes_21 + $_total_taxes_21 + $_total_products_wo_taxes_export + ($slip['shipping_cost'] == 1? (float) $order['total_shipping']: 0.0)), + $total_achat_slip, + 'REFUND '.strtoupper($order['payment']), + $order['direct_payment'], + $date_send, + $refundreason, + '', + ))."\n"); + } +} + +fclose($f); + +exit; diff --git a/export_pdf.php b/export_pdf.php new file mode 100644 index 0000000..3c2b19a --- /dev/null +++ b/export_pdf.php @@ -0,0 +1,65 @@ +Output(dirname(__FILE__).'/pdf/'.(int) $id_order.'.pdf', 'F'); +// } else { +// echo $id_order."\n"; +// } +// } +// + +foreach( array('8R27056095774' '7R00015822420' '7R00015822253' '6A10909980655' '6A09248520172') as $id_order) { + $pdf = new PDF('P', 'mm', 'A4'); + $order = new Order((int) $id_order); + if(Validate::isLoadedObject($order)) { + PDF::invoice($order, 'F', TRUE, $pdf); + $pdf->Output(dirname(__FILE__).'/pdf/'.(int) $id_order.'.pdf', 'F'); + } else { + echo $id_order."\n"; + } +} \ No newline at end of file diff --git a/export_philea_laposte.php b/export_philea_laposte.php new file mode 100644 index 0000000..e417378 --- /dev/null +++ b/export_philea_laposte.php @@ -0,0 +1,301 @@ +format('Y'))); + + // Set Visitor / Customer TimeZone (If server not configured or using e-commerce website for ex.) + $easterDate->setTimezone(new DateTimeZone('Europe/Paris')); + $date->setTimezone(new DateTimeZone('Europe/Paris')); + + // Fixed date list + $publicHolidaysList = array( + new DateTime($date->format('Y-1-1')), // New Year + new DateTime($date->format('Y-5-1')), // Labor day + new DateTime($date->format('Y-5-8')), // 1945 + new DateTime($date->format('Y-7-14')), // National Holiday + new DateTime($date->format('Y-8-15')), // Assumption + new DateTime($date->format('Y-11-1')), // All Saints Day + new DateTime($date->format('Y-11-11')), // Armistice + new DateTime($date->format('Y-12-25')), // Christmas + ); + + $easterDate1 = new DateTime($easterDate->format('Y-m-d')); + $easterDate1->modify('+1 day'); + + $easterDate2 = new DateTime($easterDate->format('Y-m-d')); + $easterDate2->modify('+39 day'); + + $easterDate3 = new DateTime($easterDate->format('Y-m-d')); + $easterDate3->modify('+50 day'); + + $publicHolidaysList[] = $easterDate1; + $publicHolidaysList[] = $easterDate2; + $publicHolidaysList[] = $easterDate3; + + // Sort DateTime + usort($publicHolidaysList, function($a, $b) { + $interval = $a->diff($b); + return $interval->invert? 1: -1; + }); + return $publicHolidaysList; +} + +$holidays = getPublicHolidays(); + +$contract = Configuration::get('LAPOSTEWS_API_CONTRACT'); +$contract2 = Configuration::get('LAPOSTEWS_API_CONTRACT2'); + +$now = new Datetime(); +$now->setTimezone(new DateTimeZone('Europe/Paris')); +$now->modify('-1 day'); + +$id_orders = array(); +$orders_to_print = array(); + +// $f = fopen(dirname(__FILE__).'/extracts/itinsell/'.$now->format('Y-m-d').'_laposte.csv', 'w'); +$f = fopen(dirname(__FILE__).'/extracts/itinsell/'.$now->format('Y-m-d').'_philea_laposte.csv', 'w'); +csv($f, array( + 'shipping_number', + 'product_code', + 'reference', + 'recipient', + 'address1', + 'address2', + 'address3', + 'address4', + 'postcode', + 'city', + 'country', + 'phone', + 'email', + 'weight', + 'parcel_quantity', + 'saturday_delivery', + 'oversized', + 'instructions', + 'contract', + 'summary_number', + 'summary_date', + 'description', + 'value', + 'order_value', + 'customer_shipping_value', + 'pod_value', + 'insurance_value', + 'shipping_value', + 'delivery_date', + 'id_delivery_point', +)); +// $orders_laposte = array(); +// $orders_laposte = Db::getInstance()->ExecuteS(' +// SELECT l.`shipping_number`, o.`id_order`, o.`id_cart`, o.`id_address_delivery`, l.`date_add` +// FROM `'._DB_PREFIX_.'lapostews` l +// LEFT JOIN `'._DB_PREFIX_.'order_detail` d +// ON l.`id_order_detail` = d.`id_order_detail` +// LEFT JOIN `'._DB_PREFIX_.'orders` o +// ON d.`id_order` = o.`id_order` +// WHERE l.`date_add` >= "'.pSQL($now->format('Y-m-d 00:00:00')).'" and l.`date_add` <= "'.pSQL($now->format('Y-m-d 23:59:59')).'" +// GROUP BY l.`shipping_number` +// '); + +$orders_philea = Db::getInstance()->ExecuteS(' + SELECT pp.`shipping_number`, o.`id_order`, o.`id_cart`, o.`id_address_delivery`, pp.`date_add` + FROM `'._DB_PREFIX_.'philea_parcel` pp + LEFT JOIN `'._DB_PREFIX_.'order_detail` d + ON pp.`id_order_detail` = d.`id_order_detail` + LEFT JOIN `'._DB_PREFIX_.'orders` o + ON d.`id_order` = o.`id_order` + WHERE pp.`date_add` >= "'.pSQL($now->format('Y-m-d 00:00:00')).'" and pp.`date_add` <= "'.pSQL($now->format('Y-m-d 23:59:59')).'" + GROUP BY pp.`shipping_number` +'); + +foreach($orders_philea as $row) { + $orders_to_print[] = (int) $row['id_order']; + + $delivery_so = Db::getInstance()->getRow(' + SELECT * + FROM `'._DB_PREFIX_.'socolissimo_delivery_info` + WHERE `id_cart` = '.(int) $row['id_cart'].' + '); + + $delivery = Db::getInstance()->getRow(' + SELECT * + FROM `'._DB_PREFIX_.'address` + WHERE `id_address` = '.(int) $row['id_address_delivery'].' + '); + + $delivery_date = FALSE; + + $hour = explode(' ', $row['date_add']); + $hour = explode(':', $hour[1]); + + if($delivery_so && $delivery_so['cecountry'] == 'FR' || $delivery['id_country'] == 8) { + $delivery_date = new DateTime($now->format('Y-m-d')); + $delivery_date->setTimezone(new DateTimeZone('Europe/Paris')); + $delivery_date->modify('+1 day'); + + if((int) $hour[0] > 16 || ((int) $hour[0] == 16 && (int) $hour[1] >= 30)) { + $delivery_date->modify('+1 day'); + } + + $i = 1; + while($i > 0) { + $is_holiday = FALSE; + foreach($holidays as $holiday) { + if($holiday->format('Y-m-d') == $delivery_date->format('Y-m-d')) { + $is_holiday = TRUE; + break; + } + } + + if((int) $delivery_date->format('w') == 0) { + $is_holiday = TRUE; + } + + $delivery_date->modify('+1 day'); + + if(!$is_holiday) { + $i--; + } + } + } + + if(!$delivery_so) { + csv($f, array( + $row['shipping_number'], + '', + $row['id_order'], + ($delivery['company'] != ''? $delivery['company'].' - ': '').$delivery['firstname'].' '.$delivery['lastname'], + trim(str_replace(array("\n", "\r"), ' ', $delivery['address1'])), + trim(str_replace(array("\n", "\r"), ' ', $delivery['address2'])), + '', + '', + (string) $delivery['postcode'], + $delivery['city'], + Country::getIsoById((int) $delivery['id_country']), + str_replace(' ', '', (string) ($delivery['phone_mobile'] != ''? $delivery['phone_mobile']: $delivery['phone'])), + Db::getInstance()->getValue('SELECT `email` FROM `'._DB_PREFIX_.'customer` WHERE `id_customer` = '.(int) $delivery['id_customer']), + 0.24, + 1, + 1, + 'N', + trim(str_replace(array("\n", "\r"), ' ', $delivery['other'])), + (int) $delivery['id_country'] == 8? $contract: $contract2, + $now->format('Ymd'), + $now->format('d/m/Y'), + '', + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + $delivery_date? $delivery_date->format('d/m/Y'): '', + '', + )); + } else { + csv($f, array( + $row['shipping_number'], + '', + $row['id_order'], + str_replace(array("\n", "\r"), ' ', ($delivery_so['prcompladress'] != ''? $delivery_so['prcompladress'].' - ': '').($delivery_so['cecompanyname'] != ''? $delivery_so['cecompanyname'].' - ': '').$delivery_so['prfirstname'].' '.$delivery_so['prname']), + trim(str_replace(array("\n", "\r"), ' ', $delivery_so['pradress3'])), + trim(str_replace(array("\n", "\r"), ' ', $delivery_so['pradress4'])), + trim(str_replace(array("\n", "\r"), ' ', $delivery_so['pradress1'])), + trim(str_replace(array("\n", "\r"), ' ', $delivery_so['pradress2'])), + (string) $delivery_so['przipcode'], + $delivery_so['prtown'], + $delivery_so['cecountry'], + str_replace(' ', '', $delivery_so['cephonenumber'] != ''? (string) $delivery_so['cephonenumber']: ((string) ($delivery['phone_mobile'] != ''? $delivery['phone_mobile']: $delivery['phone']))), + $delivery_so['ceemail'], + 0.24, + 1, + 1, + 'N', + trim(str_replace(array("\n", "\r"), ' ', $delivery_so['cedeliveryinformation'])), + $delivery_so['cecountry'] == 'FR'? $contract: $contract2, + $now->format('Ymd'), + $now->format('d/m/Y'), + '', + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + $delivery_date? $delivery_date->format('d/m/Y'): '', + $delivery_so['prid'], + )); + } + + $pdf = new PDF('P', 'mm', 'A4'); + $order = new Order((int) $row['id_order']); + if(Validate::isLoadedObject($order)) { + $id_orders[] = (int) $order->id; + PDF::invoice($order, 'F', TRUE, $pdf); + $pdf->Output(dirname(__FILE__).'/extracts/invoices/'.(int) $order->id.'.pdf', 'F'); + } + +} + +fclose($f); + +/** + * Regeneration invoices for specific orders + * using this includes to comment all about csv + */ +// $orders_to_reprint = array(350385,302554,300088,286495,290190,296685,290539,291935,287612,284108,285930,285284,288626,290659,295252,292214,287915,278572,282846,281120,281324,292999,279299,278681,276840,272366,274417,276233,272552,267126,273451,267563,266108,267186,269235,258571,257172,258306,259788,259000,259547,254186,225736,334010,332694,325773,325773,323595,323595,306923,306923,304776,304776,302960,302960,305645,305645,285700,274391,272544,271819,265852,260292,259059,265544,257100,234423,240829,240829,233767,217497,205633,203823,201126,187387,185462,179854,166753,176233,183491,177402,369986,367072,367383,367970,368338,339107,339107,338759,334924,327402,320364,320364,332667,332667,320441,320441,311542,311542,308895,308895,307577,307577,266573,277760,274366,274318,273079,257063,256322,250723,245678,238412,237659,196253,192709,180700,180046,175868,381283,369164,367304,367982,367104,370224,366966,370107,366177,369315,362975,367252,361380,365123,370909,359931,360698,363246,358396,361919,361608,357811,357270,367977,357509,356946,357199,357199,351217,351217,335669,335669,339127,336501,334378,334378,348116,348116,335330,336653,336653,331109,330480,330480,332355,332355,328706,328706,338370,338370,330071,330071,328154,328154,327939,327939,327736,327736,328201,328201,324345,324345,324978,324978,323388,323388,324598,324598,320183,320183,320935,320935,319958,319958,302878,292803,292803,298027,297052,298746,291544,292090,290810,290168,287607,287277,285811,289946,289048,285813,287451,285987,291066,289082,288720,286209,283027,289347,284717,295020,283355,280889,284988,278545,284070,282825,280327,279775,277188,278137,278137,281455,277836,271812,275407,274469,279319,271066,270306,271764,273388,267492,272076,277800,284796,284462,275265,270320,270486,270171,269886,266411,266333,268554,264148,265590,262954,262851,260846,262689,261596,262103,261976,261521,262447,260983,259890,262905,260747,261067,258471,243813,225221,229011,222430,203373,204814,206033,204894,200585,200839,200125,198425,188263,197940,194892,187277,188266,176280,372718,356628,357331,357178,357326,353230,353230,356461,356461,353210,353210,363733,363733,351215,353296,349932,350221,350221,349221,349358,349221,349894,348980,349745,345763,347674,347674,356852,346516,346516,345297,345297,347205,347205,343485,343485,345911,345911,344243,344243,354164,354164,342709,340429,340429,342041,342041,314782,314782,317374,317374,315947,315947,312628,312628,313425,313425,312877,312877,311916,311916,313144,311534,311534,310128,310128,309820,309820,310808,310808,309611,309611,308859,308859,307387,307387,306365,306365,305878,305878,315960,315960,304861,304861,306712,304406,304406,307734,307734,303634,303634,301336,301336,301295,301295,302048,302048,301446,301446,301446,301446,309768,309768,307734,301235,305236,304360,294794,256386,267588,259733,255594,250994,255479,253251,252393,252441,251929,252987,252734,249873,250484,249240,248519,248202,256012,250175,248340,250448,248500,246880,249146,247554,247139,246179,246930,245615,246352,241094,244392,241484,241314,240026,240797,240427,239982,240330,237796,237127,238402,239833,239833,245077); +// foreach ($orders_to_reprint as $key => $order_id) { +// $pdf = new PDF('P', 'mm', 'A4'); +// $order = new Order((int) $order_id); +// if(Validate::isLoadedObject($order)) { +// $id_orders[] = (int) $order->id; +// PDF::invoice($order, 'F', TRUE, $pdf); +// $pdf->Output(dirname(__FILE__).'/extracts/invoices/'.(int) $order->id.'.pdf', 'F'); +// } +// } + +$ftp = ftp_connect('ftp.itinsell.com'); +ftp_login($ftp, 'CPTXSC', 'c8PssQoLQUYd'); +ftp_put($ftp, 'Colissimo/831400/CSV/'.$now->format('Y-m-d').'_bbb_laposte.csv', dirname(__FILE__).'/extracts/itinsell/'.$now->format('Y-m-d').'_philea_laposte.csv', FTP_BINARY); +ftp_close($ftp); + +$ftp = ftp_connect('ftp.itrack.itinsell.com'); +ftp_login($ftp, '800914', 'xt954hVUbg'); +foreach($id_orders as $id_order) { + ftp_put($ftp, 'FacturesCommerciales/'.(int) $id_order.'.pdf', dirname(__FILE__).'/extracts/invoices/'.(int) $id_order.'.pdf', FTP_BINARY); + unlink(dirname(__FILE__).'/extracts/invoices/'.(int) $id_order.'.pdf'); +} +ftp_close($ftp); diff --git a/export_webdav.php b/export_webdav.php new file mode 100644 index 0000000..eb7fd4d --- /dev/null +++ b/export_webdav.php @@ -0,0 +1,312 @@ +host = $host; + $this->user = $user; + $this->pass = $pass; + $this->connected = false; + } + + + function connect() { + $this->fp = fsockopen($this->host, 80, $errno, $errstr, 5); + if(!$this->fp) { + $this->connected = false; + return false; + } else { + $this->connected = true; + return true; + } + } + + /** + * check if the connection was established + * returns true if connected otherwise false + */ + function isConnected() { + return $this->connected; + } + + /* + * get the file listing from webdav + */ + function getListing($directory='/') { + $listing = array(); + if ($this->isConnected() == false) { + $this->connect(); + } + if ($this->isConnected() == false) { + return $listing; + } else { + $data = 'PROPFIND '.$directory.' HTTP/1.0 +Host: '.$this->host.' +Connection: close +Authorization: Basic '.base64_encode($this->user.':'.$this->pass).' +Depth: 1 +Content-Type: text/xml; charset="utf-8" +Content-Length: 93 + + + +'; + + fwrite($this->fp,$data); + + $xml = ''; + while (!feof($this->fp)) { + $xml .= fgets($this->fp, 8192); + } + + $responses = $this->getContentInTag('d:response',$xml,true); + foreach ($responses as $response) { + $hrefs = $this->getContentInTag('d:href',$response,true); + $rtype = $this->getContentInTag('d:resourcetype',$response,true); + if (trim($rtype[0]) == '') { + $listing[$hrefs[0]] = 'file'; + } else { + $listing[$hrefs[0]] = 'dir'; + } + } + + $this->disconnect(); + return $listing; + } + } + + + + + function storeFile($filename, $destFile) { + if (!file_exists($filename)) { + print "No such file for upload: $filename\n"; + return false; + } + + if ($this->isConnected() == false) { + $this->connect(); + } + if ($this->isConnected() == false) { + return false; + } + + $data = 'PUT '.$destFile.' HTTP/1.0 +Host: '.$this->host.' +Connection: close +Authorization: Basic '.base64_encode($this->user.':'.$this->pass).' +Content-Type: application/octet-stream +Content-Length: '.filesize($filename).' + +'; + fwrite($this->fp,$data); + // print $data; + + $fh = fopen($filename,'rb'); + while (!feof($fh)) { + $data = fread($fh,8192); + fwrite($this->fp,$data); + // print $data; + } + fclose($fh); + $this->disconnect(); + } + + + function disconnect() { + if ($this->isConnected() == true) { + fclose($this->fp); + $this->connected = false; + } + } + + + function getContentInTag($tag,&$xml,$stripcdata=false) { + $ret = array(); + if ($xml == '') { + print "No XML for tag ".$tag."\n"; + } + $matches = array(); + if (preg_match_all('|<'.$tag.'[^>]*>(.*?)|is',$xml,$matches)) { + if ($stripcdata == true) { + foreach ($matches[1] as $match) { + if (strpos($match,'<![CDATA[')!==false) { + $match = preg_replace('/\s*<!\[CDATA\[/','',$match); + $match = preg_replace('/\]\]>\s*/','',$match); + $ret[] = $match; + } else { + $ret[] = $match; + } + } + } else { + $ret = array(); + foreach ($matches[1] as $match) { + $ret[] = html_entity_decode ($match); + } + return $ret; + } + } + return $ret; + } + + +} + +$customers_to_import = array(); + +// Customer informations +foreach (Db::getInstance()->ExecuteS(' + SELECT + c.`id_customer`, + c.`firstname`, + c.`lastname`, + c.`email`, + c.`ip_registration_newsletter`, + IF(c.`deleted` = 1, 0, IF(c.`active` = 0, 0, c.`newsletter`)) AS `newsletter`, + IFNULL(v.`version`, "fr") AS `version` + FROM `ps_customer` c + LEFT JOIN `ps_customer_version` v ON v.`id_customer` = c.`id_customer` + ORDER BY `id_customer` ASC +') as $row) { + $customers_to_import[(int)$row['id_customer']] = array( + 'id_customer' => $row['id_customer'], + 'firstname' => $row['firstname'], + 'lastname' => $row['lastname'], + 'email' => $row['email'], + 'ip_registration_newsletter' => $row['ip_registration_newsletter'], + 'newsletter' => (int)$row['newsletter'], + 'version' => $row['version'], + 'date_first_order' => "", + 'date_last_order' => "", + 'discount' => "" + ); +} +$id_customers = array_keys($customers_to_import); + +$filename = date('Y-m-d', mktime()).'.csv'; + +$f = fopen('extracts/webdav/'.$filename, 'w'); + +fputcsv($f, array('id_customer', 'firstname', 'lastname', 'email', 'ip_registration_newsletter', 'newsletter', 'langue', 'date de premier achat', 'Date de dernier achat', 'discount'), ';', '"'); + +for($i=0, $l=count($id_customers); $i < $l; $i+=5000) { + // first order + foreach (Db::getInstance()->ExecuteS(' + SELECT o.`id_customer`, DATE_FORMAT(o.`date_add`,\'%d/%m/%Y\') AS `date_first_order` + FROM `'._DB_PREFIX_.'orders` o + LEFT JOIN `'._DB_PREFIX_.'order_history` oh ON o.`id_order` = oh.`id_order` + WHERE 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 oh.`id_order_state` NOT IN (1,14,15,18,6,8,10,11) + AND o.`id_customer` IN ('.implode(', ', array_slice($id_customers, $i, 5000)).') + GROUP BY o.`id_customer` + ORDER BY o.`id_order` DESC + ') as $row) { + $customers_to_import[(int)$row['id_customer']]['date_first_order'] = $row['date_first_order']; + } + + // last order + foreach (Db::getInstance()->ExecuteS(' + SELECT o.`id_customer`, DATE_FORMAT(MAX(h.`date_add`), \'%d/%m/%Y\') AS `date_last_order` + FROM `'._DB_PREFIX_.'order_history` h + LEFT JOIN `'._DB_PREFIX_.'orders` o ON h.`id_order` = o.`id_order` + WHERE h.`id_order_state` NOT IN (1,14,15,18,6,8,10,11) + AND o.`id_customer` IN ('.implode(', ', array_slice($id_customers, $i, 5000)).') + GROUP BY o.`id_customer` + ORDER BY h.`id_order` DESC + ') as $row) { + $customers_to_import[(int)$row['id_customer']]['date_last_order'] = $row['date_last_order']; + } + + // Discount + foreach (Db::getInstance()->ExecuteS(' + SELECT dh.`id_customer`, dh.`code` as `discount` + FROM `'._DB_PREFIX_.'ant_discount_history` dh + WHERE dh.`id_customer` IN ('.implode(', ', array_slice($id_customers, $i, 5000)).') + AND dh.`used` = 0 + GROUP BY dh.`id_customer` + ORDER BY dh.`date_add` DESC + ') as $row) { + $customers_to_import[(int)$row['id_customer']]['discount'] = $row['discount']; + } +} +foreach ($customers_to_import as $customer) { + fwrite($f, implode(';', array_values($customer))."\n"); +} +fclose($f); +// Ancienne requete trop longue +// foreach(Db::getInstance()->ExecuteS(' +// SELECT +// c.`id_customer`, +// c.`firstname`, +// c.`lastname`, +// c.`email`, +// c.`ip_registration_newsletter`, +// IF(c.`deleted` = 1, 0, IF(c.`active` = 0, 0, c.`newsletter`)) AS `newsletter`, +// IFNULL(v.`version`, "fr") AS `version`, +// IFNULL( +// ( +// SELECT DATE_FORMAT(o.`date_add`,\'%m/%d/%Y\') +// FROM `'._DB_PREFIX_.'orders` o +// LEFT JOIN `'._DB_PREFIX_.'order_history` oh ON (oh.`id_order` = o.`id_order`) +// WHERE o.`id_customer` = c.`id_customer` +// 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 oh.id_order_state NOT IN (1,14,15,18,6,8,10,11) +// ORDER BY o.`date_add` ASC LIMIT 1 +// ) +// ,"") AS `date_first_order`, +// IFNULL( +// ( +// SELECT DATE_FORMAT(o.`date_add`,\'%m/%d/%Y\') +// FROM `'._DB_PREFIX_.'orders` o +// LEFT JOIN `'._DB_PREFIX_.'order_history` oh ON (oh.`id_order` = o.`id_order`) +// WHERE o.`id_customer` = c.`id_customer` +// 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 oh.id_order_state NOT IN (1,14,15,18,6,8,10,11) +// ORDER BY o.`date_add` DESC LIMIT 1 +// ) +// ,"") AS `date_last_order`, +// IFNULL( +// ( +// SELECT dh.`code` +// FROM `'._DB_PREFIX_.'ant_discount_history` dh +// WHERE dh.`id_customer` = c.`id_customer` +// AND dh.`used` = 0 +// ORDER BY dh.`date_add` DESC LIMIT 1 +// ) +// ,"") AS `discount` +// FROM `'._DB_PREFIX_.'customer` c +// LEFT JOIN `'._DB_PREFIX_.'customer_version` v ON v.`id_customer` = c.`id_customer` +// ORDER BY `id_customer` ASC LIMIT '.$i.','.($i+10000).' +// ') as $customer) { +// fputcsv($f, $customer, ';', '"'); +// } + + +/* +$user = 'admin'; +$pw = 'v6S385Vf'; +$account = 'bebeboutik'; +$host = 'suite.emarsys.net'; + +$w = new WebDAV($host, $user, $pw); +$w->connect(); +$w->storeFile('extracts/webdav/'.$filename, '/storage/'.$account.'/newsletter.csv'); +*/ + +$ftp = ftp_connect('ftp.emarsys.fr'); +ftp_login($ftp, 'emarsys-bbb', 'XuCVuK64'); +ftp_put($ftp, $filename, 'extracts/webdav/'.$filename, FTP_BINARY); +ftp_close($ftp); diff --git a/extracts/monthly_compta/2017-12-01_new.csv b/extracts/monthly_compta/2017-12-01_new.csv new file mode 100644 index 0000000..d95e9ac --- /dev/null +++ b/extracts/monthly_compta/2017-12-01_new.csv @@ -0,0 +1 @@ +date;id_order;id_order_slip;invoice_number;multisale;id_customer;firstname;lastname;total_products_wo_taxes_210;total_taxes_210;total_products_wo_taxes_200;total_taxes_200;total_products_wo_taxes_196;total_taxes_196;total_products_wo_taxes_100;total_taxes_100;total_products_wo_taxes_55;total_taxes_55;total_products_wo_taxes_21;total_taxes_21;total_products_wo_taxes_60;total_taxes_60;total_products_wo_taxes_40;total_taxes_40;total_products_wo_taxes_export;shipping_wo_taxes_196-200;shipping_wo_taxes_export;shipping_country;discounts;total_paid;total_paid_real;wholesale_price;payment_type;direct_payment;refund_reason;date_subsribe;pays;device diff --git a/extracts/monthly_compta/2017-12-18_new.csv b/extracts/monthly_compta/2017-12-18_new.csv new file mode 100644 index 0000000..d95e9ac --- /dev/null +++ b/extracts/monthly_compta/2017-12-18_new.csv @@ -0,0 +1 @@ +date;id_order;id_order_slip;invoice_number;multisale;id_customer;firstname;lastname;total_products_wo_taxes_210;total_taxes_210;total_products_wo_taxes_200;total_taxes_200;total_products_wo_taxes_196;total_taxes_196;total_products_wo_taxes_100;total_taxes_100;total_products_wo_taxes_55;total_taxes_55;total_products_wo_taxes_21;total_taxes_21;total_products_wo_taxes_60;total_taxes_60;total_products_wo_taxes_40;total_taxes_40;total_products_wo_taxes_export;shipping_wo_taxes_196-200;shipping_wo_taxes_export;shipping_country;discounts;total_paid;total_paid_real;wholesale_price;payment_type;direct_payment;refund_reason;date_subsribe;pays;device diff --git a/fichier_test.php b/fichier_test.php new file mode 100644 index 0000000..85caa91 --- /dev/null +++ b/fichier_test.php @@ -0,0 +1,240 @@ +id_customer); + $dst_address = new Address($order->id_address_delivery); + $dst_country = new Country($dst_address->id_country); + + $langs = array( + 1 => 'EN', + 2 => 'FR', + 3 => 'ES', + 5 => 'IT', + 6 => 'EN', + ); + + $dst_address = array( + 'companyName' => '', + 'lastName' => '', + 'firstName' => '', + 'line0' => '', + 'line1' => '', + 'line2' => '', + 'line3' => '', + 'countryCode' => '', + 'city' => '', + 'zipCode' => '', + 'phoneNumber' => '', + 'mobileNumber' => '', + 'doorCode1' => '', + 'doorCode2' => '', + 'email' => '', + 'intercom' => '', + 'language' => $langs[(int) $order->id_lang], + ); + + $prid = ''; + $instructions = ''; + + $unicode_ack = json_decode('"\u0006"'); + $unicode_ack2 = json_decode('"\u00ad"'); + + if(Module::isInstalled('socolissimo') + && ($so_data = Db::getInstance()->getRow(' + SELECT * + FROM `'._DB_PREFIX_.'socolissimo_delivery_info` + WHERE `id_cart` = '.(int) $order->id_cart.' + AND `id_customer` = '.(int) $order->id_customer.' + ')) + && ( + (strpos(Db::getInstance()->getValue(' + SELECT `name` + FROM `'._DB_PREFIX_.'carrier` + WHERE `id_carrier` = '.(int) $order->id_carrier.' + '), 'La Poste') !== FALSE) + || (strpos(Db::getInstance()->getValue(' + SELECT `name` + FROM `'._DB_PREFIX_.'carrier` + WHERE `id_carrier` = '.(int) $order->id_carrier.' + '), 'Colissimo') !== FALSE) + )) { + $product_type = $so_data['delivery_mode']; + if($product_type == 'BOM') { + $product_type = 'DOM'; + } + if($so_data['cecountry'] == 'ES' && $product_type == 'DOM') { + $product_type = 'DOS'; + } + if(!in_array($so_data['cecountry'], array('ES', 'BE', 'AD', 'MC', 'FR', 'GP', 'RE', 'MQ', 'YT', 'NC', 'PM', 'GF'))) { + $product_type = 'DOS'; + } + if($so_data['cecountry'] == 'IT'){ + $product_type = 'COLI'; + } + + $dst_address['email'] = $so_data['ceemail']; + if(empty($dst_address['email'])) { + $dst_address['email'] = $dst_customer->email; + } + + if(!empty($so_data['cename'])) { + $dst_address['lastName'] = mb_substr(str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª','Á','ł','İ'), array(' ', '', '', '', 'a', 'a','a','A','l','i'), $so_data['cename']), 0, 35); + } else { + $dst_address['lastName'] = mb_substr(str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª','Á','ł','İ'), array(' ', '', '', '', 'a', 'a','a','A','l','i'), $so_data['prname']), 0, 35); + } + + if(!empty($so_data['cefirstname'])) { + $dst_address['firstName'] = mb_substr(str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª','Á','ł','','İ'), array(' ', '', '', '', 'a', 'a','a','A','l',' ','i'), $so_data['cefirstname']), 0, 29); + } else { + $dst_address['firstName'] = mb_substr(str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª','Á','ł','','İ'), array(' ', '', '', '', 'a', 'a','a','A','l',' ','i'), $so_data['prfirstname']), 0, 29); + } + + if(!empty($so_data['cecompanyname'])) { + $dst_address['companyName'] = mb_substr(str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª','Á','ł','','İ'), array(' ', '', '', '', 'a', 'a','a','A','l',' ','i'), $so_data['cecompanyname']), 0, 35); + } + + $address_lines = array(); + for($i = 1; $i < 5; $i++) { + if(($line = (string) mb_substr($so_data['pradress'.$i], 0, 32)) != '') { + $address_lines[] = trim($line); + } + } + + if(count($address_lines) == 1) { + $dst_address['line2'] = str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª'), array(' ', '', '', '', 'a', 'a','a'), $address_lines[0]); + } elseif(count($address_lines) == 2) { + $dst_address['line2'] = str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª'), array(' ', '', '', '', 'a', 'a','a'), $address_lines[0]); + $dst_address['line3'] = str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª'), array(' ', '', '', '', 'a', 'a','a'), $address_lines[1]); + } elseif(count($address_lines) == 3) { + $dst_address['line0'] = str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª'), array(' ', '', '', '', 'a', 'a','a'), $address_lines[0]); + $dst_address['line2'] = str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª'), array(' ', '', '', '', 'a', 'a','a'), $address_lines[1]); + $dst_address['line3'] = str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª'), array(' ', '', '', '', 'a', 'a','a'), $address_lines[2]); + } else { + $dst_address['line0'] = str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª'), array(' ', '', '', '', 'a', 'a','a'), $address_lines[0]); + $dst_address['line1'] = str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª'), array(' ', '', '', '', 'a', 'a','a'), $address_lines[1]); + $dst_address['line2'] = str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª'), array(' ', '', '', '', 'a', 'a','a'), $address_lines[2]); + $dst_address['line3'] = str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª'), array(' ', '', '', '', 'a', 'a','a'), $address_lines[3]); + } + + $dst_address['countryCode'] = $so_data['cecountry']; + + if(strtolower($so_data['cecountry']) == 'sl') { + $dst_address['zipCode'] = (string) str_ireplace(array('SI-', 'SL-'), '', mb_substr($so_data['przipcode'], 0, 5)); + } elseif(strtolower($so_data['cecountry']) == 'pt') { + $dst_address['zipCode'] = (string) $so_data['przipcode']; + } else { + $dst_address['zipCode'] = (string) mb_substr($so_data['przipcode'], 0, 5); + } + $dst_address['city'] = (string) mb_substr($so_data['prtown'], 0, 35); + + $dst_address['mobileNumber'] = (string) mb_substr(str_replace(array('.','°', '|', ' ', '’',"/"),'',$so_data['cephonenumber']), 0, 32); + if(strlen($dst_address['mobileNumber']) > 10) { + if(preg_match('/^(0033|\+33|33)[0-9]{9,10}$/', $dst_address['mobileNumber'])) { + $dst_address['mobileNumber'] = '0'.mb_substr(mb_substr($dst_address['mobileNumber'], -10), 1); + } + if(strtolower($so_data['cecountry']) == 'be' && preg_match('/^(0032|32)[0-9]{9,10}$/', $dst_address['mobileNumber'])) { + $dst_address['mobileNumber'] = '+32'.mb_substr(mb_substr($dst_address['mobileNumber'], -10), 1); + } + } elseif(strtolower($so_data['cecountry']) == 'fr' && preg_match('/^[0-9]{9,10}$/', $dst_address['mobileNumber'])) { + if(strlen($dst_address['mobileNumber']) == 9) { + $dst_address['mobileNumber'] = '0'.$dst_address['mobileNumber']; + } elseif(strlen($dst_address['mobileNumber']) == 10) { + $dst_address['mobileNumber'] = '0'.mb_substr(mb_substr($dst_address['mobileNumber'], -10), 1); + } + } elseif(strtolower($so_data['cecountry']) == 'be' && preg_match('/^[0-9]{9,10}$/', $dst_address['mobileNumber'])) { + if(strlen($dst_address['mobileNumber']) == 9) { + $dst_address['mobileNumber'] = '+32'.$dst_address['mobileNumber']; + } elseif(strlen($dst_address['mobileNumber']) == 10) { + $dst_address['mobileNumber'] = '+32'.mb_substr(mb_substr($dst_address['mobileNumber'], -10), 1); + } + } + + if(strtolower($so_data['cecountry']) == 'fr' && mb_substr($dst_address['mobileNumber'], 0, 2) != '06' && mb_substr($dst_address['mobileNumber'], 0, 2) != '07') { + $dst_address['mobileNumber'] = '0600000000'; + } + + if(($doorcode = (string) mb_substr($so_data['cedoorcode1'], 0, 8)) != '') { + $dst_address['doorCode1'] = (string) mb_substr($so_data['cedoorcode1'], 0, 8); + } + if(($doorcode = (string) mb_substr($so_data['cedoorcode2'], 8, 8)) != '') { + $dst_address['doorCode2'] = (string) mb_substr($so_data['cedoorcode1'], 8, 8); + } + + $instructions = trim(str_replace(array('°', '|', ' ', '’', "\n", "\r"), ' ', (string) $so_data['cedeliveryinformation'])); + $prid = $so_data['prid']; + } else { + $dst_address['companyName'] = (string) mb_substr(str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª','Á','ł','','İ', $unicode_ack, $unicode_ack2), array(' ', '', '', '', 'a', 'a','a','A','l',' ','i',' ',' '), $dst_address->company), 0, 35); + $dst_address['lastName'] = (string) mb_substr(str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª','Á','ł','','İ', $unicode_ack, $unicode_ack2), array(' ', '', '', '', 'a', 'a','a','A','l',' ','i',' ',' '), $dst_address->firstname), 0, 35); + $dst_address['firstName'] = (string) mb_substr(strtoupper(str_replace(array('°', 'º', 'º', 'º', 'ª', 'ā','ª','Á','ł','','İ', $unicode_ack, $unicode_ack2), array(' ', '', '', '', 'a', 'a','a','A','l',' ','i',' ',' '), $dst_address->lastname)), 0, 29); + $dst_address['email'] = (string) $dst_customer->email; + if($dst_address->id_country == 193) { + $dst_address['zipCode'] = mb_substr((string) str_ireplace(array('SI-', 'SL-'), '', $dst_address->postcode), 0, 5); + } else { + $dst_address['zipCode'] = mb_substr((string) str_replace(array(' ', '-'), '', $dst_address->postcode), 0, 5); + } + $dst_address['city'] = (string) mb_substr(strtoupper($dst_address->city), 0, 35); + if(in_array($dst_country->iso_code, array('GP', 'RE', 'MQ', 'YT', 'NC', 'PM', 'GF'))) { + $dst_address['countryCode'] = 'FR'; + } else { + $dst_address['countryCode'] = (string) $dst_country->iso_code; + } + $dst_address['line2'] = str_replace(array('º', 'º', 'º', 'ª',), array('', '', '', 'a'), (string) $dst_address->address1); + $dst_address['line3'] = str_replace(array('º', 'º', 'º', 'ª',), array('', '', '', 'a'), (string) $dst_address->address2); + $instructions = trim(str_replace(array('°', '|', ' ', '’', "\n", "\r"), ' ', (string) $dst_address->other)); + + if(in_array($dst_country->iso_code, array('FR', 'AD', 'MC', 'GP', 'RE', 'MQ', 'YT', 'NC', 'PM', 'GF'))) { + + $dst_address->phone = ($dst_address->phone != ''? (string) str_replace(array('.','°', '|', ' ', '’',"/"),'',$dst_address->phone) : ''); + $dst_address->phone_mobile = ($dst_address->phone_mobile != ''? (string) str_replace(array('.','°', '|', ' ', '’',"/"),'',$dst_address->phone_mobile) : ''); + if($dst_address->phone != '' && preg_match('/^0[67][0-9]+$/', $dst_address->phone)) { + $dst_address['phoneNumber'] = (string) $dst_address->phone; + } elseif($dst_address->phone_mobile != '' && preg_match('/^0[67][0-9]+$/', $dst_address->phone_mobile)) { + $dst_address['phoneNumber'] = (string) $dst_address->phone_mobile; + } + + if(strlen($dst_address['phoneNumber']) > 10) { + if(preg_match('/^(0033|\+33|33)[0-9]{9,10}$/', $dst_address['phoneNumber'])) { + $dst_address['phoneNumber'] = '0'.mb_substr(mb_substr($dst_address['phoneNumber'], -10), 1); + } + } + if(strlen($dst_address['mobileNumber']) > 10) { + if(preg_match('/^(0033|\+33|33)[0-9]{9,10}$/', $dst_address['mobileNumber'])) { + $dst_address['mobileNumber'] = '0'.mb_substr(mb_substr($dst_address['mobileNumber'], -10), 1); + } + } + } else { + if($dst_country->iso_code == 'BE') { + if($dst_address->phone != '') { + $dst_address['phoneNumber'] = (string) $dst_address->phone; + } else { + $dst_address['phoneNumber'] = (string) $dst_address->phone_mobile; + } + $dst_address['mobileNumber'] = ''; + } else { + if($dst_address->phone != '') { + $dst_address['phoneNumber'] = (string) $dst_address->phone; + } + if($dst_address->phone_mobile != '') { + $dst_address['mobileNumber'] = (string) $dst_address->phone_mobile; + } + } + } + + if($dst_country->iso_code == 'FR' || $dst_country->iso_code == 'MC' || $dst_country->iso_code == 'BE') { + //$product_type = 'COLD'; // Amené a disparaitre + $product_type = 'DOM'; + } else { + $product_type = 'COLI'; + } + } +} +echo '
';var_dump($product_type, $dst_address);echo '
';die(); \ No newline at end of file diff --git a/find_token.php b/find_token.php new file mode 100644 index 0000000..8e73f99 --- /dev/null +++ b/find_token.php @@ -0,0 +1,14 @@ +ExecuteS(' + SELECT `id_employee` + FROM `'._DB_PREFIX_.'employee` +') as $row) { + echo Tools::getAdminToken('AdminImport'.(int) Tab::getIdFromClassName('AdminImport').(int) $row['id_employee'])."\t".$row['id_employee']; + echo "\n"; +} + diff --git a/reassociation_shipping_number.php b/reassociation_shipping_number.php new file mode 100644 index 0000000..d72eae4 --- /dev/null +++ b/reassociation_shipping_number.php @@ -0,0 +1,21 @@ +ExecuteS(' + SELECT `id_order`, `shipping_number` + FROM `'._DB_PREFIX_.'shipping_history` + WHERE `id_order` IN + (449596,449617,449642,449643,449645,449664,449668,449669,449670,449674,449681,449688,449692,449714,449728,449736,449771,449798,449805,449831,449832,449849,449855,449858,449883,449885,449886,449899,449903,449915,449952,449962,449969,449981,449983,450001,450004,450013,450014,450016,450024,450041,450082,450084,450110,450116,450126,450129,450154,450163,450199,450214,450215,450218,450262,450268,450272,450278,450289,450332,450345,450415,450457,450555,450568,450573,450576,450597,450672,450680,450681,450700,450759,450788,450831,450844,450883,450923,450994,451022,451065,451081,451106,451162,451209,451223,451236,451242,451359,451459,451499,451555,451573,451605,451615,451620,451625,451814,451826,451828,451861,451878,451928,451935,451950,452064,452146) +'); + +//echo '
';var_dump($orders);echo "
\n";die(); + +foreach ($orders as $key => $order) { + Db::getInstance()->ExecuteS(' + UPDATE `'._DB_PREFIX_.'orders` + SET `shipping_number` = "'.pSQL($order['shipping_number']).'" + WHERE `id_order` = '.(int) $order['id_order'].' + LIMIT 1 + '); +} \ No newline at end of file diff --git a/regenerate_images.php b/regenerate_images.php new file mode 100644 index 0000000..fadc2b2 --- /dev/null +++ b/regenerate_images.php @@ -0,0 +1,83 @@ +getProducts(); + +//echo "
";var_dump($products);echo "
";die(); + +$productsImages = Db::getInstance()->ExecuteS(' + SELECT `id_image`, `id_product` + FROM `'._DB_PREFIX_.'image` + WHERE `id_product` IN ('.implode(',',$products).') + ORDER BY `id_image` ASC'); + + +$dir = _PS_PROD_IMG_DIR_; +$type = ImageType::getImagesTypes('products'); +/*$type = array(); +$type[] = array('name' => 'thickbox'); +$type[] = array('name' => 'small'); +$type[] = array('name' => 'medium'); +$type[] = array('name' => 'large'); +$type[] = array('name' => 'home');*/ +//echo "
";var_dump($productsImages,$type);echo "
";die(); +$start_time = time(); +$max_execution_time = 7200; +foreach ($productsImages AS $k => $image) +{ + // delete old rezise image + if (!is_dir($dir)) { + continue; + } + $toDel = scandir($dir); + foreach ($toDel AS $d) { + foreach ($type AS $imageType) { + if (preg_match('/^[0-9]+\-[0-9]+\-'.$imageType['name'].'\.jpg$/', $d) OR preg_match('/^([[:lower:]]{2})\-default\-(.*)\.jpg$/', $d)) { + if (file_exists($dir.$d)) { + unlink($dir.$d); + } + } + } + } + $imageObj = new Image($image['id_image']); + $imageObj->id_product = $image['id_product']; + if (file_exists($dir.$imageObj->getImgFolder())) + { + $toDel = scandir($dir.$imageObj->getImgFolder()); + foreach ($toDel AS $d) { + foreach ($type AS $imageType) { + if (preg_match('/^[0-9]+\-'.$imageType['name'].'\.jpg$/', $d)) { + if (file_exists($dir.$imageObj->getImgFolder().$d)) { + unlink($dir.$imageObj->getImgFolder().$d); + } + } + } + } + } + + // regenerate new + if (file_exists($dir.$imageObj->getExistingImgPath().'.jpg')) { + foreach ($type AS $k => $imageType) + { + if (!file_exists($dir.$imageObj->getExistingImgPath().'-'.stripslashes($imageType['name']).'.jpg')) { + if (!imageResize($dir.$imageObj->getExistingImgPath().'.jpg', $dir.$imageObj->getExistingImgPath().'-'.stripslashes($imageType['name']).'.jpg', (int)($imageType['width']), (int)($imageType['height']))) + $errors = true; + } + if (time() - $start_time > $max_execution_time - 4) { // stop 4 seconds before the tiemout, just enough time to process the end of the page on a slow server + return 'timeout'; + } + } + } + if((int) $image['id_product'] % 100 == 0) { + echo $image['id_product']."\n"; + } +} \ No newline at end of file diff --git a/regeneration_export_laposte.php b/regeneration_export_laposte.php new file mode 100644 index 0000000..38fdcb3 --- /dev/null +++ b/regeneration_export_laposte.php @@ -0,0 +1,279 @@ +format('Y'))); + + // Set Visitor / Customer TimeZone (If server not configured or using e-commerce website for ex.) + $easterDate->setTimezone(new DateTimeZone('Europe/Paris')); + $date->setTimezone(new DateTimeZone('Europe/Paris')); + + // Fixed date list + $publicHolidaysList = array( + new DateTime($date->format('Y-1-1')), // New Year + new DateTime($date->format('Y-5-1')), // Labor day + new DateTime($date->format('Y-5-8')), // 1945 + new DateTime($date->format('Y-7-14')), // National Holiday + new DateTime($date->format('Y-8-15')), // Assumption + new DateTime($date->format('Y-11-1')), // All Saints Day + new DateTime($date->format('Y-11-11')), // Armistice + new DateTime($date->format('Y-12-25')), // Christmas + ); + + $easterDate1 = new DateTime($easterDate->format('Y-m-d')); + $easterDate1->modify('+1 day'); + + $easterDate2 = new DateTime($easterDate->format('Y-m-d')); + $easterDate2->modify('+39 day'); + + $easterDate3 = new DateTime($easterDate->format('Y-m-d')); + $easterDate3->modify('+50 day'); + + $publicHolidaysList[] = $easterDate1; + $publicHolidaysList[] = $easterDate2; + $publicHolidaysList[] = $easterDate3; + + // Sort DateTime + usort($publicHolidaysList, function($a, $b) { + $interval = $a->diff($b); + return $interval->invert? 1: -1; + }); + return $publicHolidaysList; +} + +function generateCsv($now,$holidays,$contract,$contract2) { + + $now->setTimezone(new DateTimeZone('Europe/Paris')); + + $id_orders = array(); + $orders_to_print = array(); + + // $f = fopen(dirname(__FILE__).'/extracts/itinsell/'.$now->format('Y-m-d').'_laposte.csv', 'w'); + $f = fopen(dirname(__FILE__).'/extracts/itinsell/philea/'.$now->format('Y-m-d').'_philea_laposte.csv', 'w'); + csv($f, array( + 'shipping_number', + 'product_code', + 'reference', + 'recipient', + 'address1', + 'address2', + 'address3', + 'address4', + 'postcode', + 'city', + 'country', + 'phone', + 'email', + 'weight', + 'parcel_quantity', + 'saturday_delivery', + 'oversized', + 'instructions', + 'contract', + 'summary_number', + 'summary_date', + 'description', + 'value', + 'order_value', + 'customer_shipping_value', + 'pod_value', + 'insurance_value', + 'shipping_value', + 'delivery_date', + 'id_delivery_point', + )); + // $orders_laposte = array(); + // $orders_laposte = Db::getInstance()->ExecuteS(' + // SELECT l.`shipping_number`, o.`id_order`, o.`id_cart`, o.`id_address_delivery`, l.`date_add` + // FROM `'._DB_PREFIX_.'lapostews` l + // LEFT JOIN `'._DB_PREFIX_.'order_detail` d + // ON l.`id_order_detail` = d.`id_order_detail` + // LEFT JOIN `'._DB_PREFIX_.'orders` o + // ON d.`id_order` = o.`id_order` + // WHERE l.`date_add` >= "'.pSQL($now->format('Y-m-d 00:00:00')).'" and l.`date_add` <= "'.pSQL($now->format('Y-m-d 23:59:59')).'" + // GROUP BY l.`shipping_number` + // '); + + $orders_philea = Db::getInstance()->ExecuteS(' + SELECT pp.`shipping_number`, o.`id_order`, o.`id_cart`, o.`id_address_delivery`, pp.`date_add` + FROM `'._DB_PREFIX_.'philea_parcel` pp + LEFT JOIN `'._DB_PREFIX_.'order_detail` d + ON pp.`id_order_detail` = d.`id_order_detail` + LEFT JOIN `'._DB_PREFIX_.'orders` o + ON d.`id_order` = o.`id_order` + WHERE pp.`date_add` >= "'.pSQL($now->format('Y-m-d 00:00:00')).'" and pp.`date_add` <= "'.pSQL($now->format('Y-m-d 23:59:59')).'" + GROUP BY pp.`shipping_number` + '); + + foreach($orders_philea as $row) { + $orders_to_print[] = (int) $row['id_order']; + + $delivery_so = Db::getInstance()->getRow(' + SELECT * + FROM `'._DB_PREFIX_.'socolissimo_delivery_info` + WHERE `id_cart` = '.(int) $row['id_cart'].' + '); + + $delivery = Db::getInstance()->getRow(' + SELECT * + FROM `'._DB_PREFIX_.'address` + WHERE `id_address` = '.(int) $row['id_address_delivery'].' + '); + + $delivery_date = FALSE; + + $hour = explode(' ', $row['date_add']); + $hour = explode(':', $hour[1]); + + if($delivery_so && $delivery_so['cecountry'] == 'FR' || $delivery['id_country'] == 8) { + $delivery_date = new DateTime($now->format('Y-m-d')); + $delivery_date->setTimezone(new DateTimeZone('Europe/Paris')); + $delivery_date->modify('+1 day'); + + if((int) $hour[0] > 16 || ((int) $hour[0] == 16 && (int) $hour[1] >= 30)) { + $delivery_date->modify('+1 day'); + } + + $i = 1; + while($i > 0) { + $is_holiday = FALSE; + foreach($holidays as $holiday) { + if($holiday->format('Y-m-d') == $delivery_date->format('Y-m-d')) { + $is_holiday = TRUE; + break; + } + } + + if((int) $delivery_date->format('w') == 0) { + $is_holiday = TRUE; + } + + $delivery_date->modify('+1 day'); + + if(!$is_holiday) { + $i--; + } + } + } + + if(!$delivery_so) { + csv($f, array( + $row['shipping_number'], + '', + $row['id_order'], + ($delivery['company'] != ''? $delivery['company'].' - ': '').$delivery['firstname'].' '.$delivery['lastname'], + trim(str_replace(array("\n", "\r"), ' ', $delivery['address1'])), + trim(str_replace(array("\n", "\r"), ' ', $delivery['address2'])), + '', + '', + (string) $delivery['postcode'], + $delivery['city'], + Country::getIsoById((int) $delivery['id_country']), + str_replace(' ', '', (string) ($delivery['phone_mobile'] != ''? $delivery['phone_mobile']: $delivery['phone'])), + Db::getInstance()->getValue('SELECT `email` FROM `'._DB_PREFIX_.'customer` WHERE `id_customer` = '.(int) $delivery['id_customer']), + 0.24, + 1, + 1, + 'N', + trim(str_replace(array("\n", "\r"), ' ', $delivery['other'])), + (int) $delivery['id_country'] == 8? $contract: $contract2, + $now->format('Ymd'), + $now->format('d/m/Y'), + '', + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + $delivery_date? $delivery_date->format('d/m/Y'): '', + '', + )); + } else { + csv($f, array( + $row['shipping_number'], + '', + $row['id_order'], + str_replace(array("\n", "\r"), ' ', ($delivery_so['prcompladress'] != ''? $delivery_so['prcompladress'].' - ': '').($delivery_so['cecompanyname'] != ''? $delivery_so['cecompanyname'].' - ': '').$delivery_so['prfirstname'].' '.$delivery_so['prname']), + trim(str_replace(array("\n", "\r"), ' ', $delivery_so['pradress3'])), + trim(str_replace(array("\n", "\r"), ' ', $delivery_so['pradress4'])), + trim(str_replace(array("\n", "\r"), ' ', $delivery_so['pradress1'])), + trim(str_replace(array("\n", "\r"), ' ', $delivery_so['pradress2'])), + (string) $delivery_so['przipcode'], + $delivery_so['prtown'], + $delivery_so['cecountry'], + str_replace(' ', '', $delivery_so['cephonenumber'] != ''? (string) $delivery_so['cephonenumber']: ((string) ($delivery['phone_mobile'] != ''? $delivery['phone_mobile']: $delivery['phone']))), + $delivery_so['ceemail'], + 0.24, + 1, + 1, + 'N', + trim(str_replace(array("\n", "\r"), ' ', $delivery_so['cedeliveryinformation'])), + $delivery_so['cecountry'] == 'FR'? $contract: $contract2, + $now->format('Ymd'), + $now->format('d/m/Y'), + '', + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + $delivery_date? $delivery_date->format('d/m/Y'): '', + $delivery_so['prid'], + )); + } + + // $pdf = new PDF('P', 'mm', 'A4'); + // $order = new Order((int) $row['id_order']); + // if(Validate::isLoadedObject($order)) { + // $id_orders[] = (int) $order->id; + // PDF::invoice($order, 'F', TRUE, $pdf); + // $pdf->Output(dirname(__FILE__).'/extracts/invoices/'.(int) $order->id.'.pdf', 'F'); + // } + + } + fclose($f); +} + +$date_from = new DateTime('2016-09-30'); +$date_to = new DateTime('2016-12-15'); +$holidays = getPublicHolidays(); +$contract = Configuration::get('LAPOSTEWS_API_CONTRACT'); +$contract2 = Configuration::get('LAPOSTEWS_API_CONTRACT2'); +$date = $date_from; +while ($date<=$date_to) { + generateCsv($date,$holidays,$contract,$contract2); + $date->modify('+1 day'); +} + diff --git a/regeneration_monthly.php b/regeneration_monthly.php new file mode 100755 index 0000000..3f1a35a --- /dev/null +++ b/regeneration_monthly.php @@ -0,0 +1,33 @@ + /dev/null'; +//echo exec($cmd); + +// Historique: +// 22/08/2016 - ($y=2; $y < 4; $y++) +// 23/08/2016 - ($y=4; $y < 5; $y++) +// 24/08/2016 - ($y=5; $y < 6; $y++) +// 25/08/2016 - ($y=6; $y < 7; $y++) +// 17/11/2017 - ($y=7; $y < 8; $y++) + +// new +for ($y=7; $y < 8; $y++) { + $year = '201'.$y; + $start = ($y==2)?4:1; + $end = ($y==7)?11:13; + for ($m=$start; $m < $end; $m++) { + if ($m == 10 || $m == 11 | $m == 12) { + $month = $m; + } else { + $month = '0'.$m; + } + //echo $year."-".$month."-01\n"; + echo exec('php cron_export_sales_monthly.php '.$year.'-'.$month.'-01 2> /dev/null'); + } +} + +exit; \ No newline at end of file diff --git a/regeneration_order_state_current.php b/regeneration_order_state_current.php new file mode 100644 index 0000000..1736fba --- /dev/null +++ b/regeneration_order_state_current.php @@ -0,0 +1,31 @@ +ExecuteS(' +// SELECT DISTINCT o.`id_order`, oh.`id_order_state` +// FROM `ps_orders` o +// LEFT JOIN `ps_order_history` oh ON (oh.`id_order` = o.`id_order`) +// LEFT JOIN `ps_order_detail` d ON o.`id_order` = d.`id_order` +// LEFT JOIN `ps_product_ps_cache` c ON d.`product_id` = c.`id_product` +// LEFT JOIN `ps_philea_sent` pms ON (pms.`id_sale` = c.`id_sale` AND pms.`id_order` = o.`id_order`) +// WHERE c.`id_sale` = '.(int) $id_sale.' +// AND oh.`id_order_history` = (SELECT MAX(`id_order_history`) FROM `ps_order_history` moh WHERE moh.`id_order` = o.`id_order` GROUP BY moh.`id_order`) +// -- AND oh.`id_order_state` IN (2, 3, 4, 13, 17, 9, 18, 19) +// AND pms.`id_order` IS NULL +// -- AND d.`product_quantity` - d.`product_quantity_refunded` > 0 +// ') as $row){ +// Db::getInstance()->ExecuteS(' +// INSERT INTO `'._DB_PREFIX_.'order_state_current` +// VALUES ( +// '.(int) $row['id_order'].', +// '.(int) $row['id_order_state'].', +// NOW() +// ) +// ON DUPLICATE KEY UPDATE +// `id_order_state` = '.(int) $row['id_order_state'].', +// `date_upd` = NOW() +// '); +// } diff --git a/regeneration_sale_cache.php b/regeneration_sale_cache.php new file mode 100644 index 0000000..fd06524 --- /dev/null +++ b/regeneration_sale_cache.php @@ -0,0 +1,27 @@ +ExecuteS(' + DELETE FROM `'._DB_PREFIX_.'product_ps_cache` + WHERE `id_product` < 5081878 + AND `id_product` > 5063468 +'); + +Db::getInstance()->ExecuteS(' + INSERT IGNORE INTO `'._DB_PREFIX_.'product_ps_cache` ( + SELECT p.id_product, IFNULL( + ( + SELECT s.id_sale + FROM `'._DB_PREFIX_.'privatesale_category` s + WHERE s.`id_category` = p.`id_category_default` + LIMIT 1) + , 0 + ) + FROM `'._DB_PREFIX_.'product` p + WHERE `id_product` < 5081878 + AND `id_product` > 5063468 + ) +'); \ No newline at end of file diff --git a/regeneration_sales_stats.php b/regeneration_sales_stats.php new file mode 100644 index 0000000..3f691f7 --- /dev/null +++ b/regeneration_sales_stats.php @@ -0,0 +1,60 @@ + $dateTo) { + return $dates; + } + + if (1 != $dateFrom->format('N')) { + $dateFrom->modify('next monday'); + } + + while ($dateFrom <= $dateTo) { + $dates[] = $dateFrom->format('Y-m-d'); + $dateFrom->modify('+1 week'); + } + + return $dates; +} + +// Historique: +// 23/08/2016 - $date_from = 2015-01-01; $date_to = 2015-02-28; +// 24/08/2016 - $date_from = 2015-02-28; $date_to = 2015-04-30; +// 25/08/2016 - $date_from = 2015-04-30; $date_to = 2015-06-30; +// 25/08/2016 - $date_from = 2015-06-30; $date_to = 2015-08-31; +// 29/08/2016 - $date_from = 2015-08-31; $date_to = 2015-10-31; +// 30/08/2016 - $date_from = 2015-10-31; $date_to = 2015-12-31; +// 31/08/2016 - $date_from = 2015-12-31; $date_to = 2016-02-29; +// 01/09/2016 - $date_from = 2016-02-29; $date_to = 2016-04-30; +// 02/09/2016 - $date_from = 2016-04-30; $date_to = 2016-06-30; +// 02/09/2016 - $date_from = 2016-06-30; $date_to = 2016-08-14; +// 08/09/2016 - $date_from = 2016-05-31; $date_to = 2016-07-31; +// 08/09/2016 - $date_from = 2016-03-31; $date_to = 2016-05-31; +// 08/09/2016 - $date_from = 2016-01-31; $date_to = 2016-03-31; +// 09/09/2016 - $date_from = 2015-12-31; $date_to = 2016-01-31; +// 09/09/2016 - $date_from = 2015-10-31; $date_to = 2015-12-31; +// 09/09/2016 - $date_from = 2015-08-31; $date_to = 2015-10-31; +// 09/09/2016 - $date_from = 2015-06-30; $date_to = 2015-08-30; +// 15/09/2016 - $date_from = 2015-03-01; $date_to = 2015-04-30; +// 15/09/2016 - $date_from = 2015-01-01; $date_to = 2015-02-24; + +$date_from = '2015-01-01'; +$date_to = '2015-02-24'; + +$mondays = getMondaysInRange($date_from, $date_to); + +foreach ($mondays as $key => $monday) { + echo $monday."\n"; + echo exec('php cron_weekly_stats.php '.$monday.' 2> /dev/null'); +} + +exit; \ No newline at end of file diff --git a/rename_csv_import.php b/rename_csv_import.php new file mode 100644 index 0000000..2aa5034 --- /dev/null +++ b/rename_csv_import.php @@ -0,0 +1,49 @@ + &$filename) { + //exclude . .. .svn and index.php and all hidden files + if (preg_match('/^\..*|index.php/Ui', $filename)) { + unset($filesToImport[$k]); + } + // ANTADIS - evite d'importer plusieurs fois le même fichier + if (substr($filename, 0, 3) == 'old') { + unset($filesToImport[$k]); + } + if (isset($filesToImport[$k])) { + $date = strrev(substr(strrev($filesToImport[$k]), 0, 14)); + $years = substr($date, 0, 4); + $month = substr($date, 4, 2); + $day = substr($date, 6, 2); + + $now = new DateTime("now"); + $date2 = new DateTime($years.'-'.$month.'-'.$day); + $interval = $date2->diff($now); + + if ((int)$interval->format('%a')<7) { + $fileTokeep[] = $filesToImport[$k]; + unset($filesToImport[$k]); + } + } +} +foreach ($filesToImport AS $k => &$filename) { + rename(dirname(__FILE__).'/www/adm/import/'.$filename, dirname(__FILE__).'/www/adm/import/old_'.$filename); +} + +function _usortFiles($a, $b) +{ + $a = strrev(substr(strrev($a), 0, 14)); + $b = strrev(substr(strrev($b), 0, 14)); + + if ($a == $b) + return 0; + + return ($a < $b) ? 1 : -1; +} \ No newline at end of file diff --git a/sandbox.php b/sandbox.php new file mode 100644 index 0000000..af03244 --- /dev/null +++ b/sandbox.php @@ -0,0 +1,10 @@ +getOrdersFromSaleMulti($statuts, 2); + +p($orders);