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

302 lines
13 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;
}
$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);