173 lines
5.1 KiB
PHP
Raw Normal View History

<?php
2017-09-01 10:14:13 +02:00
/**
* @author Retail Driver LCC
* @copyright RetailCRM
* @license GPL
* @version 2.2.0
2017-09-01 10:14:13 +02:00
* @link https://retailcrm.ru
*
*/
$_SERVER['HTTPS'] = 1;
require(dirname(__FILE__) . '/../../../config/config.inc.php');
require(dirname(__FILE__) . '/../../../init.php');
require(dirname(__FILE__) . '/../bootstrap.php');
$apiUrl = Configuration::get('RETAILCRM_ADDRESS');
$apiKey = Configuration::get('RETAILCRM_API_TOKEN');
$apiVersion = Configuration::get('RETAILCRM_API_VERSION');
$statusExport = Configuration::get('RETAILCRM_STATUS_EXPORT');
if (!empty($apiUrl) && !empty($apiKey)) {
$api = new RetailcrmProxy($apiUrl, $apiKey, _PS_ROOT_DIR_ . '/retailcrm.log', $apiVersion);
} else {
error_log('orderHistory: set api key & url first', 3, _PS_ROOT_DIR_ . '/retailcrm.log');
exit();
}
$orders = array();
$customers = array();
2016-01-13 18:17:35 +03:00
$customerInstance = new Customer();
$orderInstance = new Order();
2016-01-13 18:17:35 +03:00
$customerRecords = $customerInstance->getCustomers();
$orderRecords = $orderInstance->getOrdersWithInformations();
$delivery = json_decode(Configuration::get('RETAILCRM_API_DELIVERY'), true);
$payment = json_decode(Configuration::get('RETAILCRM_API_PAYMENT'), true);
$status = json_decode(Configuration::get('RETAILCRM_API_STATUS'), true);
foreach ($customerRecords as $record) {
$customers[$record['id_customer']] = array(
'externalId' => $record['id_customer'],
'firstName' => $record['firstname'],
'lastname' => $record['lastname'],
2016-01-13 18:17:35 +03:00
'email' => $record['email']
);
}
unset($customerRecords);
foreach ($orderRecords as $record) {
$object = new Order($record['id_order']);
if (Module::getInstanceByName('advancedcheckout') === false) {
$paymentType = $record['module'];
} else {
$paymentType = $record['payment'];
}
2016-01-13 18:17:35 +03:00
if ($record['current_state'] == 0) {
$order_status = $statusExport;
2016-01-13 18:17:35 +03:00
} else {
$order_status = array_key_exists($record['current_state'], $status)
? $status[$record['current_state']]
: $statusExport
2016-01-13 18:17:35 +03:00
;
}
$cart = new Cart($object->getCartIdStatic($record['id_order']));
$addressCollection = $cart->getAddressCollection();
$address = array_shift($addressCollection);
2016-01-13 18:17:35 +03:00
if ($address instanceof Address) {
$phone = is_null($address->phone)
? is_null($address->phone_mobile) ? '' : $address->phone_mobile
: $address->phone
;
$postcode = $address->postcode;
$city = $address->city;
$addres_line = sprintf("%s %s", $address->address1, $address->address2);
}
$order = array(
'externalId' => $record['id_order'],
'createdAt' => $record['date_add'],
2016-01-13 18:17:35 +03:00
'status' => $order_status,
'firstName' => $record['firstname'],
'lastName' => $record['lastname'],
'email' => $record['email'],
);
2016-01-13 18:17:35 +03:00
if (isset($postcode)) {
$order['delivery']['address']['index'] = $postcode;
2016-01-13 18:17:35 +03:00
}
if (isset($city)) {
$order['delivery']['address']['city'] = $city;
}
if (isset($addres_line)) {
$order['delivery']['address']['text'] = $addres_line;
}
if ($phone) {
$order['phone'] = $phone;
}
if ($apiVersion != 5) {
if (array_key_exists($paymentType, $payment)) {
$order['paymentType'] = $payment[$paymentType];
}
} else {
$order_payment = array(
'externalId' => $record['id_order'] .'#'. $record['reference'],
'amount' => $record['total_paid'],
'type' => $payment[$paymentType] ? $payment[$paymentType] : ''
);
$order['payments'][] = $order_payment;
2016-01-13 18:17:35 +03:00
}
if (array_key_exists($record['id_carrier'], $delivery)) {
$order['delivery']['code'] = $delivery[$record['id_carrier']];
}
if (isset($record['total_shipping_tax_incl']) && (int) $record['total_shipping_tax_incl'] > 0) {
$order['delivery']['cost'] = round($record['total_shipping_tax_incl'], 2);
}
$products = $object->getProducts();
2017-09-01 10:14:13 +02:00
foreach ($products as $product) {
if (isset($product['product_attribute_id']) && $product['product_attribute_id'] > 0) {
2017-06-16 16:13:25 +03:00
$productId = $product['product_id'] . '#' . $product['product_attribute_id'];
} else {
$productId = $product['product_id'];
}
$item = array(
2017-06-16 16:13:25 +03:00
'offer' => array('externalId' => $productId),
'productName' => $product['product_name'],
'quantity' => $product['product_quantity'],
'initialPrice' => round($product['product_price'], 2),
'purchasePrice' => round($product['purchase_supplier_price'], 2)
);
$order['items'][] = $item;
}
if ($record['id_customer']) {
$order['customer']['externalId'] = $record['id_customer'];
2016-01-13 18:17:35 +03:00
}
2016-01-13 18:17:35 +03:00
$orders[$record['id_order']] = $order;
}
2016-01-13 18:17:35 +03:00
unset($orderRecords);
$customers = array_chunk($customers, 50);
2016-01-13 18:17:35 +03:00
foreach ($customers as $chunk) {
$api->customersUpload($chunk);
time_nanosleep(0, 200000000);
}
2016-01-13 18:17:35 +03:00
$orders = array_chunk($orders, 50);
foreach ($orders as $chunk) {
$api->ordersUpload($chunk);
time_nanosleep(0, 200000000);
}