bebeboutik-scripts/cron_export_sales_monthly.php
Michael RICOIS 49415f3059 change path
2018-03-16 15:33:51 +01:00

957 lines
44 KiB
PHP

<?php
$_SERVER['HTTP_HOST'] = 'www.bebeboutik.com';
require_once realpath(dirname(__FILE__)).'/www/config/config.inc.php';
//require_once realpath(dirname(__FILE__).'/../').'/bebeboutik/config/config.inc.php';
$longopts = array(
'verbose',
'dry-run',
'debug',
'date:',
);
$shortopts = "";
$options = getopt($shortopts, $longopts);
// Options
$optVerbose = false;
if (isset($options['verbose'])) {
$optVerbose = true;
}
$optTest = false;
if (isset($options['dry-run'])) {
$optTest = true;
}
$optDebug = false;
if (isset($options['debug'])) {
$optDebug = true;
}
// Date
$dateSelect = new DateTime();
if (isset($options['date'])) {
$dateSelect = DateTime::createFromFormat('Ymd', $options['date']);
} else {
$dateSelect->modify('first day of previous month');
}
// Function
function getChildren($id=0) {
return Db::getInstance()->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;
}
// Start
echo date('Y-m-d H:i:s')." - START ".$dateSelect->format('Y-m-d')." \n";
$now = $dateSelect->format('Y-m-d').' 00:00:00';
$dateStartSql = $dateSelect->format('Y-m').'-01 00:00:00';
$dateEndSql = $dateSelect->format('Y-m').'-31 23:59:59';
$filename = date('Y-m-d', strtotime($now)).'_new.csv';
if ($optDebug === true) {
$filename = date('Y-m-d', strtotime($now)).'_debug.csv';
}
$f = fopen('extracts/monthly_compta/'.$filename, 'w');
$headers = array(
'date',
'id_order',
'id_order_slip',
'invoice_number',
'multisale',
'id_customer',
'firstname',
'lastname',
'total_product_wo_taxes_220',
'total_taxes_220',
'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 IN (6, 7, 11, 15, 16) ) )
AND o.date_add >= "'.$dateStartSql.'"
AND o.date_add <= "'.$dateEndSql.'"
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;
}
}
// Process orders with details
$orderNb = count($orders);
echo date('Y-m-d H:i:s')." - $orderNb orders \n";
if ($orderNb > 0) {
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_220 = array();
$total_taxes_220 = 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();
$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
');
$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'] == '22.000') {
if((float) $detail['reduction_percent'] != 0.0) {
$total_products_wo_taxes_220[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity']);
$total_taxes_220[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity'] * 0.22);
} else {
$total_products_wo_taxes_220[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity']);
$total_taxes_220[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity'] * 0.22);
}
}
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'] == '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'] == '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'] == '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'] == '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'] == '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);
}
}
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);
}
}
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_220 = 0.0;
foreach($total_products_wo_taxes_220 as $value) {
$_total_products_wo_taxes_220 += $value;
}
$_total_taxes_220 = 0.0;
foreach($total_taxes_220 as $value) {
$_total_taxes_220 += $value;
}
$_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_220,
$_total_taxes_220,
$_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'] / ( 1 + $order['carrier_tax_rate'] / 100) : 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");
}
}
// ORDER REFUNDED
$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',
);
$orderStateNoCredit = array(
16, // Fraude non détecté
);
$slips = Db::getInstance()->ExecuteS('
SELECT *
FROM `'._DB_PREFIX_.'order_slip` os
WHERE os.date_add >= "'.$dateStartSql.'"
AND os.date_add <= "'.$dateEndSql.'"
');
$slipNb = count($slips);
echo date('Y-m-d H:i:s')." - $slipNb orders refund \n";
if($slipNb > 0) {
foreach($slips as $slip) {
$order = Db::getInstance()->getRow('
SELECT o.*, c.*, c.`date_add` as subscribe, v.`version`, osc.`id_order_state`,
(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 o.`id_customer` = c.`id_customer`
LEFT JOIN `'._DB_PREFIX_.'customer_version` v ON c.`id_customer` = v.`id_customer`
LEFT JOIN `'._DB_PREFIX_.'order_state_current` osc ON osc.`id_order` = o.`id_order`
WHERE o.id_order = '.$slip['id_order'].'
');
// Don't write some order
if (in_array($order['id_order_state'], $orderStateNoCredit)) {
continue;
}
$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 from slip
$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'].'
)
');
// Order details from real order
if (count($order_details) == 0) {
if ($optVerbose) {
echo date('Y-m-d H:i:s')." - Order slip with no detail"."\n";
}
$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']
);
// Commande Frauduleuse - generate complete refund
if ($order['id_order_state'] == 15) {
if ($optVerbose) {
echo date('Y-m-d H:i:s')." - Order fraud ".$order['id_order']." - Articles: ".count($order_details)."\n";
}
foreach ($order_details as $k => $detail) {
$order_details[$k]['product_quantity_refunded'] = $detail['product_quantity'];
}
}
// Details refund
else {
$refund_details = Db::getInstance()->executeS('
SELECT r.`id_order_detail`
FROM `'._DB_PREFIX_.'refund_transaction` r
WHERE r.`id_order` = '. (int) $slip['id_order']
);
if (count($refund_details) > 0) {
$realrefund = array();
foreach ($refund_details as $r) {
$realrefund[] = $r['id_order_detail'];
}
// Clean order_details
foreach ($order_details as $k => $od) {
if (in_array($od['id_order_detail'], $realrefund)) {
if ($optVerbose) {
echo date('Y-m-d H:i:s')." - Refund detail"."\n";
}
} else {
unset($order_details[$k]);
}
}
}
// No case, get line mark refunded
else {
if ($optVerbose) {
echo date('Y-m-d H:i:s')." - No transaction"."\n";
}
// Base on real article refunded - clean order details
foreach ($order_details as $k => $detail) {
if ($detail['product_quantity_refunded'] == 0) {
unset($order_details[$k]);
}
}
}
}
}
$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
');
$ldetails = array();
$total_products_wo_taxes_220 = array();
$total_taxes_220 = 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'] == '22.000') {
if((float) $detail['reduction_percent'] != 0.0) {
$total_products_wo_taxes_220[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded']);
$total_taxes_220[] = (float) ($detail['product_price'] * (1 - (float) $detail['reduction_percent'] / 100) * $detail['product_quantity_refunded'] * 0.22);
} else {
$total_products_wo_taxes_220[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded']);
$total_taxes_220[] = (float) (($detail['product_price'] - (float) $detail['reduction_amount']) * $detail['product_quantity_refunded'] * 0.22);
}
}
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'] == '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'] == '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'] == '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'] == '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);
}
}
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);
}
}
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_220 = 0.0;
foreach($total_products_wo_taxes_220 as $value) {
$_total_products_wo_taxes_220 += $value;
}
$_total_taxes_220 = 0.0;
foreach($total_taxes_220 as $value) {
$_total_taxes_220 += $value;
}
$_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_220,
'-'.$_total_taxes_220,
'-'.$_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'] / (1 + $order['carrier_tax_rate'] / 100) : 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_220 + $_total_taxes_220
+ $_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_220 + $_total_taxes_220
+ $_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'],
$order['device']
))."\n");
}
}
fclose($f);