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

280 lines
10 KiB
PHP

<?php
if(isset($_SERVER['REMOTE_ADDR'])) {
exit;
}
$_SERVER['HTTP_HOST'] = 'www.bebeboutik.com';
$_SERVER['SERVER_NAME'] = 'www.bebeboutik.com';
$_SERVER['HTTP_PORT'] = 80;
$_SERVER['SERVER_ADDR'] = '37.187.137.153';
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
include dirname(__FILE__).'/www/config/config.inc.php';
function csv($file_handle, $data, $field_separator=';', $enclosure='"', $record_separator="\n") {
escape(FALSE, $enclosure);
$data = array_map('escape', $data);
fwrite($file_handle, $enclosure.implode($enclosure.$field_separator.$enclosure, $data).$enclosure.$record_separator);
}
function escape($data, $enclosure=FALSE) {
static $enc;
if($enclosure === FALSE) {
return str_replace($enc, '\\'.$enc, $data);
}
$enc = $enclosure;
}
function getPublicHolidays(DateTime $date=NULL) {
if($date === NULL) {
$date = new DateTime();
}
$easterDate = new DateTime('@'.easter_date($date->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');
}