bebeboutik-scripts/cron_export_sales_monthly_new.php
2017-12-18 17:14:31 +01:00

890 lines
37 KiB
PHP

<?php
ini_set('memory_limit', '4096M');
ini_set('max_execution_time', 0);
$_SERVER['HTTP_HOST'] = 'www.bebeboutik.com';
include('www/config/config.inc.php');
$now = $argv[1].' 00:00:00';
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;
}
$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;