2017-03-22 11:52:11 +03:00
|
|
|
<?php
|
|
|
|
|
2017-07-11 17:39:48 +03:00
|
|
|
class ModelExtensionRetailcrmHistoryV45 extends Model
|
2017-03-22 11:52:11 +03:00
|
|
|
{
|
|
|
|
protected $createResult;
|
|
|
|
|
|
|
|
private $opencartApiClient;
|
2017-09-21 10:09:26 +03:00
|
|
|
private $customFieldSetting;
|
2017-03-22 11:52:11 +03:00
|
|
|
|
|
|
|
public function request()
|
|
|
|
{
|
2017-09-15 15:47:38 +03:00
|
|
|
$this->moduleTitle = $this->getModuleTitle();
|
2017-03-22 11:52:11 +03:00
|
|
|
$this->load->model('setting/setting');
|
|
|
|
$this->load->model('setting/store');
|
|
|
|
$this->load->model('user/api');
|
|
|
|
$this->load->model('sale/order');
|
|
|
|
$this->load->model('customer/customer');
|
|
|
|
$this->load->model('extension/retailcrm/references');
|
|
|
|
$this->load->model('catalog/product');
|
|
|
|
$this->load->model('catalog/option');
|
|
|
|
$this->load->model('localisation/zone');
|
|
|
|
|
|
|
|
$this->load->language('extension/module/retailcrm');
|
|
|
|
|
2017-09-15 15:47:38 +03:00
|
|
|
$settings = $this->model_setting_setting->getSetting($this->moduleTitle);
|
2017-03-22 11:52:11 +03:00
|
|
|
$history = $this->model_setting_setting->getSetting('retailcrm_history');
|
|
|
|
$settings['domain'] = parse_url(HTTP_SERVER, PHP_URL_HOST);
|
|
|
|
|
2017-09-15 15:47:38 +03:00
|
|
|
$url = isset($settings[$this->moduleTitle . '_url']) ? $settings[$this->moduleTitle . '_url'] : null;
|
|
|
|
$key = isset($settings[$this->moduleTitle . '_apikey']) ? $settings[$this->moduleTitle . '_apikey'] : null;
|
2017-03-22 11:52:11 +03:00
|
|
|
|
|
|
|
if (empty($url) || empty($key)) {
|
|
|
|
$this->log->addNotice('You need to configure retailcrm module first.');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->opencartApiClient = new OpencartApiClient($this->registry);
|
|
|
|
|
|
|
|
$crm = new RetailcrmProxy(
|
2017-09-15 15:47:38 +03:00
|
|
|
$settings[$this->moduleTitle . '_url'],
|
|
|
|
$settings[$this->moduleTitle . '_apikey'],
|
2017-07-11 17:39:48 +03:00
|
|
|
DIR_SYSTEM . 'storage/logs/retailcrm.log',
|
2017-09-15 15:47:38 +03:00
|
|
|
$settings[$this->moduleTitle . '_apiversion']
|
2017-03-22 11:52:11 +03:00
|
|
|
);
|
|
|
|
|
2017-09-15 15:47:38 +03:00
|
|
|
$sinceIdOrders = $history['retailcrm_history_orders'] ? $history['retailcrm_history_orders'] : null;
|
|
|
|
$sinceIdCustomers = $history['retailcrm_history_customers'] ? $history['retailcrm_history_customers'] : null;
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2017-05-18 09:11:53 +03:00
|
|
|
$packsOrders = $crm->ordersHistory(array(
|
2017-09-15 15:47:38 +03:00
|
|
|
'sinceId' => $sinceIdOrders ? $sinceIdOrders : 0
|
2017-03-22 11:52:11 +03:00
|
|
|
), 1, 100);
|
2017-05-17 17:48:50 +03:00
|
|
|
$packsCustomers = $crm->customersHistory(array(
|
2017-09-15 15:47:38 +03:00
|
|
|
'sinceId' => $sinceIdCustomers ? $sinceIdCustomers : 0
|
2017-05-17 17:48:50 +03:00
|
|
|
), 1, 100);
|
2017-09-15 15:47:38 +03:00
|
|
|
|
|
|
|
if(!$packsOrders->isSuccessful() && count($packsOrders->history) <= 0 && !$packsCustomers->isSuccessful() && count($packsCustomers->history) <= 0) {
|
2017-03-22 11:52:11 +03:00
|
|
|
return false;
|
2017-09-15 15:47:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$generatedAt = $packsOrders['generatedAt'];
|
2017-05-18 09:11:53 +03:00
|
|
|
$orders = RetailcrmHistoryHelper::assemblyOrder($packsOrders->history);
|
2017-05-17 17:48:50 +03:00
|
|
|
$customers = RetailcrmHistoryHelper::assemblyCustomer($packsCustomers->history);
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2017-09-15 15:47:38 +03:00
|
|
|
$ordersHistory = $packsOrders->history;
|
|
|
|
$customersHistory = $packsCustomers->history;
|
|
|
|
|
|
|
|
$lastChangeOrders = $ordersHistory ? end($ordersHistory) : null;
|
|
|
|
$lastChangeCustomers = $customersHistory ? end($customersHistory) : null;
|
|
|
|
|
|
|
|
if ($lastChangeOrders !== null) {
|
|
|
|
$sinceIdOrders = $lastChangeOrders['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($lastChangeCustomers !== null) {
|
|
|
|
$sinceIdCustomers = $lastChangeCustomers['id'];
|
|
|
|
}
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2017-07-21 10:27:08 +03:00
|
|
|
$this->totalTitle = $this->totalTitles();
|
|
|
|
$this->subtotalSettings = $this->model_setting_setting->getSetting($this->totalTitle . 'sub_total');
|
|
|
|
$this->totalSettings = $this->model_setting_setting->getSetting($this->totalTitle . 'total');
|
|
|
|
$this->shippingSettings = $this->model_setting_setting->getSetting($this->totalTitle . 'shipping');
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2017-09-15 15:47:38 +03:00
|
|
|
$this->delivery = array_flip($settings[$this->moduleTitle . '_delivery']);
|
|
|
|
$this->payment = array_flip($settings[$this->moduleTitle . '_payment']);
|
|
|
|
$this->status = array_flip($settings[$this->moduleTitle . '_status']);
|
2017-09-21 10:09:26 +03:00
|
|
|
$this->delivery_default = $settings[$this->moduleTitle . '_default_shipping'];
|
|
|
|
$this->payment_default = $settings[$this->moduleTitle . '_default_payment'];
|
2017-03-22 11:52:11 +03:00
|
|
|
$this->ocPayment = $this->model_extension_retailcrm_references
|
|
|
|
->getOpercartPaymentTypes();
|
|
|
|
|
2017-09-15 15:47:38 +03:00
|
|
|
$this->ocDelivery = $settings[$this->moduleTitle . '_delivery'];
|
2017-05-15 15:41:52 +03:00
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
$this->zones = $this->model_localisation_zone->getZones();
|
|
|
|
|
2017-09-21 10:09:26 +03:00
|
|
|
if (isset($settings[$this->moduleTitle . '_custom_field'])) {
|
|
|
|
$this->customFieldSetting = array_flip($settings[$this->moduleTitle . '_custom_field']);
|
|
|
|
}
|
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
$updatedOrders = array();
|
|
|
|
$newOrders = array();
|
|
|
|
|
|
|
|
foreach ($orders as $order) {
|
|
|
|
|
|
|
|
if (isset($order['deleted'])) continue;
|
|
|
|
|
|
|
|
if (isset($order['externalId'])) {
|
|
|
|
$updatedOrders[] = $order['id'];
|
|
|
|
} else {
|
|
|
|
$newOrders[] = $order['id'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($orders);
|
|
|
|
|
2017-05-17 17:48:50 +03:00
|
|
|
$updateCustomers = array();
|
|
|
|
|
|
|
|
foreach ($customers as $customer) {
|
|
|
|
|
|
|
|
if (isset($customer['deleted'])) continue;
|
|
|
|
|
|
|
|
if (isset($customer['externalId'])) {
|
|
|
|
$updateCustomers[] = $customer['id'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($customers);
|
|
|
|
|
2017-09-15 15:47:38 +03:00
|
|
|
if (!empty($updateCustomers)) {
|
|
|
|
$customers = $crm->customersList($filter = array('ids' => $updateCustomers));
|
|
|
|
if ($customers) {
|
|
|
|
$this->updateCustomers($customers['customers']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
if (!empty($newOrders)) {
|
|
|
|
$orders = $crm->ordersList($filter = array('ids' => $newOrders));
|
|
|
|
if ($orders) {
|
|
|
|
$this->createResult = $this->createOrders($orders['orders']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($updatedOrders)) {
|
|
|
|
$orders = $crm->ordersList($filter = array('ids' => $updatedOrders));
|
|
|
|
if ($orders) {
|
|
|
|
$this->updateOrders($orders['orders']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-15 15:47:38 +03:00
|
|
|
$this->model_setting_setting->editSetting(
|
|
|
|
'retailcrm_history',
|
|
|
|
array(
|
|
|
|
'retailcrm_history_orders' => $sinceIdOrders,
|
|
|
|
'retailcrm_history_customers' => $sinceIdCustomers,
|
|
|
|
'retailcrm_history_datetime' => $generatedAt
|
|
|
|
)
|
|
|
|
);
|
2017-03-22 11:52:11 +03:00
|
|
|
|
|
|
|
if (!empty($this->createResult['customers'])) {
|
|
|
|
$crm->customersFixExternalIds($this->createResult['customers']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->createResult['orders'])) {
|
|
|
|
$crm->ordersFixExternalIds($this->createResult['orders']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function updateOrders($orders)
|
2017-07-11 17:39:48 +03:00
|
|
|
{
|
2017-03-22 11:52:11 +03:00
|
|
|
foreach ($orders as $order) {
|
|
|
|
$store = $this->config->get('config_store_id');
|
|
|
|
|
2017-09-21 10:09:26 +03:00
|
|
|
if ($order['payments']) {
|
2017-07-11 17:39:48 +03:00
|
|
|
foreach ($order['payments'] as $orderPayment) {
|
|
|
|
if (isset($orderPayment['externalId'])) {
|
|
|
|
$payment = $orderPayment;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($payment) && count($order['payments']) == 1) {
|
|
|
|
$payment = end($order['payments']);
|
|
|
|
}
|
2017-07-21 10:27:08 +03:00
|
|
|
} elseif (isset($order['paymentType'])) {
|
2017-09-15 15:47:38 +03:00
|
|
|
$payment['type'] = $order['paymentType'];
|
2017-07-11 17:39:48 +03:00
|
|
|
}
|
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
$data = array();
|
|
|
|
|
2017-09-15 15:47:38 +03:00
|
|
|
$mail = isset($order['email']) ? $order['email'] : $order['customer']['email'];
|
|
|
|
$phone = isset($order['phone']) ? $order['phone'] : '';
|
|
|
|
|
|
|
|
if (!$phone) {
|
|
|
|
$data['telephone'] = $order['customer']['phones'] ? $order['customer']['phones'][0]['number'] : '80000000000';
|
|
|
|
} else {
|
|
|
|
$data['telephone'] = $phone;
|
|
|
|
}
|
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
$data['store_id'] = $store == null ? 0 : $store;
|
|
|
|
$data['customer'] = $order['firstName'];
|
|
|
|
$data['customer_id'] = (!empty($order['customer']['externalId'])) ? $order['customer']['externalId'] : 0;
|
|
|
|
$data['customer_group_id'] = 1;
|
|
|
|
$data['firstname'] = $order['firstName'];
|
2017-09-15 15:47:38 +03:00
|
|
|
$data['lastname'] = isset($order['lastName']) ? $order['lastName'] : $order['firstName'];
|
|
|
|
$data['email'] = $mail ? $mail : uniqid() . '@retailrcm.ru';
|
2017-03-22 11:52:11 +03:00
|
|
|
$data['comment'] = !empty($order['customerComment']) ? $order['customerComment'] : '';
|
|
|
|
$data['fax'] = '';
|
|
|
|
|
|
|
|
$data['payment_address'] = '0';
|
|
|
|
$data['payment_firstname'] = $order['firstName'];
|
2017-09-15 15:47:38 +03:00
|
|
|
$data['payment_lastname'] = isset($order['lastName']) ? $order['lastName'] : $order['firstName'];
|
2017-03-22 11:52:11 +03:00
|
|
|
$data['payment_address_1'] = isset($order['customer']['address']) ? $order['customer']['address']['text'] : '';
|
|
|
|
$data['payment_address_2'] = '';
|
|
|
|
$data['payment_company'] = '';
|
|
|
|
$data['payment_company_id'] = '';
|
|
|
|
$data['payment_city'] = !empty($order['customer']['address']['city']) ? $order['customer']['address']['city'] : $order['delivery']['address']['city'];
|
|
|
|
$data['payment_postcode'] = !empty( $order['customer']['address']['index'] ) ? $order['customer']['address']['index'] : $order['delivery']['address']['index'];
|
|
|
|
|
2017-09-15 15:47:38 +03:00
|
|
|
$shippingZone = '';
|
2017-03-22 11:52:11 +03:00
|
|
|
|
|
|
|
if (is_int($order['delivery']['address']['region'])) {
|
2017-09-15 15:47:38 +03:00
|
|
|
$shippingZone = $order['delivery']['address']['region'];
|
2017-03-22 11:52:11 +03:00
|
|
|
} else {
|
2017-09-15 15:47:38 +03:00
|
|
|
$shippingZone = $this->getZoneByName($order['delivery']['address']['region']);
|
|
|
|
|
|
|
|
if ($shippingZone) {
|
|
|
|
$shipping_zone_id = $shippingZone['zone_id'];
|
|
|
|
} else {
|
|
|
|
$shipping_zone_id = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($order['customer']['address']['region'])) {
|
|
|
|
$paymentZone = $this->getZoneByName($order['customer']['address']['region']);
|
|
|
|
|
|
|
|
if ($paymentZone) {
|
|
|
|
$payment_zone_id = $paymentZone['zone_id'];
|
|
|
|
} else {
|
|
|
|
$payment_zone_id = 0;
|
2017-03-22 11:52:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-15 15:47:38 +03:00
|
|
|
if (isset($order['delivery']['address']['countryIso'])) {
|
|
|
|
$shippingCountry = $this->getCountryByIsoCode($order['delivery']['address']['countryIso']);
|
|
|
|
}
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2017-09-15 15:47:38 +03:00
|
|
|
if (isset($order['customer']['address']['countryIso'])) {
|
|
|
|
$paymentCountry = $this->getCountryByIsoCode($order['customer']['address']['countryIso']);
|
|
|
|
}
|
2017-09-21 10:09:26 +03:00
|
|
|
|
|
|
|
$delivery = isset($order['delivery']['code']) ? $order['delivery']['code'] : null;
|
2017-09-15 15:47:38 +03:00
|
|
|
$data['payment_country_id'] = $paymentCountry ? $paymentCountry['country_id'] : 0;
|
|
|
|
$data['payment_zone_id'] = $payment_zone_id;
|
|
|
|
$data['shipping_country_id'] = $shippingCountry ? $shippingCountry['country_id'] : 0;
|
|
|
|
$data['shipping_zone_id'] = $shipping_zone_id;
|
2017-03-22 11:52:11 +03:00
|
|
|
$data['shipping_address'] = '0';
|
|
|
|
$data['shipping_firstname'] = $order['firstName'];
|
2017-09-15 15:47:38 +03:00
|
|
|
$data['shipping_lastname'] = isset($order['lastName']) ? $order['lastName'] : $order['firstName'];
|
2017-03-22 11:52:11 +03:00
|
|
|
$data['shipping_address_1'] = $order['delivery']['address']['text'];
|
|
|
|
$data['shipping_address_2'] = '';
|
|
|
|
$data['shipping_company'] = '';
|
|
|
|
$data['shipping_company_id'] = '';
|
|
|
|
$data['shipping_city'] = $order['delivery']['address']['city'];
|
|
|
|
$data['shipping_postcode'] = $order['delivery']['address']['index'];
|
2017-09-21 10:09:26 +03:00
|
|
|
$data['shipping'] = $delivery != null ? $this->delivery[$delivery] : $this->delivery_default;
|
2017-03-22 11:52:11 +03:00
|
|
|
$data['shipping_method'] = $this->ocDelivery[$data['shipping']];
|
2017-09-21 10:09:26 +03:00
|
|
|
$data['shipping_code'] = $delivery != null ? $this->delivery[$delivery] : $this->delivery_default;
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2017-07-11 17:39:48 +03:00
|
|
|
if (isset($payment)) {
|
|
|
|
$data['payment'] = $this->payment[$payment['type']];
|
|
|
|
$data['payment_method'] = $this->ocPayment[$data['payment']];
|
|
|
|
$data['payment_code'] = $this->payment[$payment['type']];
|
|
|
|
} else {
|
|
|
|
$this->load->model('sale/order');
|
|
|
|
$order_data = $this->model_sale_order->getOrder($order['externalId']);
|
|
|
|
$data['payment_method'] = $order_data['payment_method'];
|
|
|
|
$data['payment_code'] = $order_data['payment_code'];
|
|
|
|
}
|
2017-03-22 11:52:11 +03:00
|
|
|
|
|
|
|
// this data will not retrive from crm for now
|
|
|
|
$data['tax'] = '';
|
|
|
|
$data['tax_id'] = '';
|
|
|
|
$data['product'] = '';
|
|
|
|
$data['product_id'] = '';
|
|
|
|
$data['reward'] = '';
|
|
|
|
$data['affiliate'] = '';
|
|
|
|
$data['affiliate_id'] = '';
|
|
|
|
$data['payment_tax_id'] = '';
|
|
|
|
$data['order_product_id'] = '';
|
|
|
|
$data['payment_company'] = '';
|
|
|
|
$data['payment_company_id'] = '';
|
|
|
|
$data['company'] = '';
|
|
|
|
$data['company_id'] = '';
|
|
|
|
|
|
|
|
$data['order_product'] = array();
|
|
|
|
|
|
|
|
foreach ($order['items'] as $item) {
|
|
|
|
$productId = $item['offer']['externalId'];
|
|
|
|
$options = array();
|
|
|
|
if(mb_strpos($item['offer']['externalId'], '#') > 1) {
|
|
|
|
$offer = explode('#', $item['offer']['externalId']);
|
|
|
|
$productId = $offer[0];
|
|
|
|
$optionsFromCRM = explode('_', $offer[1]);
|
|
|
|
|
2017-09-15 15:47:38 +03:00
|
|
|
foreach ($optionsFromCRM as $optionFromCRM) {
|
2017-03-22 11:52:11 +03:00
|
|
|
$optionData = explode('-', $optionFromCRM);
|
|
|
|
$productOptionId = $optionData[0];
|
|
|
|
$optionValueId = $optionData[1];
|
|
|
|
|
|
|
|
$productOptions = $this->model_catalog_product->getProductOptions($productId);
|
|
|
|
|
|
|
|
foreach($productOptions as $productOption) {
|
|
|
|
if($productOptionId == $productOption['product_option_id']) {
|
|
|
|
foreach($productOption['product_option_value'] as $productOptionValue) {
|
|
|
|
if($productOptionValue['option_value_id'] == $optionValueId) {
|
|
|
|
$options[$productOptionId] = $productOptionValue['product_option_value_id'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$data['order_product'][] = array(
|
|
|
|
'product_id' => $productId,
|
|
|
|
'quantity' => $item['quantity'],
|
|
|
|
'option' => $options
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-09-21 10:09:26 +03:00
|
|
|
if (isset($this->customFieldSetting) && $order['customFields']) {
|
|
|
|
foreach ($order['customFields'] as $code => $value) {
|
|
|
|
if (array_key_exists($code, $this->customFieldSetting)) {
|
|
|
|
$fieldCode = str_replace('o_', '', $this->customFieldSetting[$code]);
|
|
|
|
$customFields[$fieldCode] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$data['custom_field'] = isset($customFields) ? $customFields : '';
|
|
|
|
}
|
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
$deliveryCost = !empty($order['delivery']['cost']) ? $order['delivery']['cost'] : 0;
|
|
|
|
|
2017-04-05 17:08:53 +03:00
|
|
|
if(isset($order['discount']) && $order['discount'] > 0) {
|
|
|
|
$orderTotals = $this->model_sale_order->getOrderTotals($order['externalId']);
|
|
|
|
foreach($orderTotals as $orderTotal) {
|
|
|
|
if($orderTotal['code'] == 'coupon') {
|
|
|
|
$data['order_total'][] = $orderTotal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-15 15:47:38 +03:00
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
$data['order_total'] = array(
|
|
|
|
array(
|
|
|
|
'order_total_id' => '',
|
|
|
|
'code' => 'sub_total',
|
|
|
|
'title' => $this->language->get('product_summ'),
|
|
|
|
'value' => $order['summ'],
|
|
|
|
'text' => $order['summ'],
|
|
|
|
'sort_order' => $this->subtotalSettings['sub_total_sort_order']
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'order_total_id' => '',
|
|
|
|
'code' => 'shipping',
|
|
|
|
'title' => $this->ocDelivery[$data['shipping_code']],
|
|
|
|
'value' => $deliveryCost,
|
|
|
|
'text' => $deliveryCost,
|
2017-07-21 10:27:08 +03:00
|
|
|
'sort_order' => $this->shippingSettings[$this->totalTitle . 'shipping_sort_order']
|
2017-03-22 11:52:11 +03:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'order_total_id' => '',
|
|
|
|
'code' => 'total',
|
|
|
|
'title' => $this->language->get('column_total'),
|
|
|
|
'value' => isset($order['totalSumm']) ? $order['totalSumm'] : $order['summ'] + $deliveryCost,
|
|
|
|
'text' => isset($order['totalSumm']) ? $order['totalSumm'] : $order['summ'] + $deliveryCost,
|
2017-07-21 10:27:08 +03:00
|
|
|
'sort_order' => $this->totalSettings[$this->totalTitle . 'total_sort_order']
|
2017-03-22 11:52:11 +03:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$data['fromApi'] = true;
|
|
|
|
|
|
|
|
if (array_key_exists($order['status'], $this->status)) {
|
|
|
|
$data['order_status_id'] = $this->status[$order['status']];
|
|
|
|
} else {
|
|
|
|
$tmpOrder = $this->model_sale_order->getOrder($order['externalId']);
|
|
|
|
$data['order_status_id'] = $tmpOrder['order_status_id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->opencartApiClient->editOrder($order['externalId'], $data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function createOrders($orders)
|
2017-07-11 17:39:48 +03:00
|
|
|
{
|
2017-03-22 11:52:11 +03:00
|
|
|
$customersIdsFix = array();
|
|
|
|
$ordersIdsFix = array();
|
|
|
|
|
|
|
|
foreach ($orders as $order) {
|
|
|
|
$store = $this->config->get('config_store_id');
|
|
|
|
|
2017-09-21 10:09:26 +03:00
|
|
|
if ($order['payments']) {
|
2017-07-11 17:39:48 +03:00
|
|
|
$payment = end($order['payments']);
|
2017-07-21 10:27:08 +03:00
|
|
|
} elseif (isset($order['paymentType'])) {
|
2017-09-15 15:47:38 +03:00
|
|
|
$payment['type'] = $order['paymentType'];
|
2017-07-11 17:39:48 +03:00
|
|
|
}
|
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
$customer_id = (!empty($order['customer']['externalId']))
|
|
|
|
? $order['customer']['externalId']
|
|
|
|
: 0;
|
|
|
|
|
|
|
|
$data = array();
|
|
|
|
|
|
|
|
if ($customer_id == 0) {
|
2017-09-15 15:47:38 +03:00
|
|
|
if (isset($order['customer']['address']['countryIso'])) {
|
|
|
|
$customerCountry = $this->getCountryByIsoCode($order['customer']['address']['countryIso']);
|
|
|
|
} else {
|
|
|
|
$customerCountry = $this->getCountryByIsoCode($order['delivery']['address']['countryIso']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($order['customer']['address']['region'])) {
|
|
|
|
$customerZone = $this->getZoneByName($order['customer']['address']['region']);
|
|
|
|
} else {
|
|
|
|
$customerZone = $this->getZoneByName($order['delivery']['address']['region']);
|
|
|
|
}
|
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
$cData = array(
|
|
|
|
'store_id' => 0,
|
|
|
|
'customer_group_id' => '1',
|
2017-09-15 15:47:38 +03:00
|
|
|
'firstname' => isset($order['patronymic']) ? $order['firstName'] . ' ' . $order['patronymic'] : $order['firstName'],
|
|
|
|
'lastname' => (!empty($order['customer']['lastName'])) ? $order['customer']['lastName'] : ' ',
|
|
|
|
'email' => $order['customer']['email'],
|
|
|
|
'telephone' => $order['customer']['phones'] ? $order['customer']['phones'][0]['number'] : ' ',
|
2017-03-22 11:52:11 +03:00
|
|
|
'fax' => '',
|
|
|
|
'newsletter' => 0,
|
|
|
|
'password' => 'tmppass',
|
|
|
|
'status' => 1,
|
2017-09-15 15:47:38 +03:00
|
|
|
'approved' => 1,
|
|
|
|
'safe' => 0,
|
2017-03-22 11:52:11 +03:00
|
|
|
'address' => array(
|
|
|
|
array(
|
2017-09-15 15:47:38 +03:00
|
|
|
'firstname' => isset($order['patronymic']) ? $order['firstName'] . ' ' . $order['patronymic'] : $order['firstName'],
|
|
|
|
'lastname' => (!empty($order['customer']['lastName'])) ? $order['customer']['lastName'] : ' ',
|
2017-03-22 11:52:11 +03:00
|
|
|
'address_1' => $order['customer']['address']['text'],
|
|
|
|
'address_2' => ' ',
|
|
|
|
'city' => !empty($order['customer']['address']['city']) ? $order['customer']['address']['city'] : $order['delivery']['address']['city'],
|
|
|
|
'postcode' => isset($order['customer']['address']['index']) ? $order['customer']['address']['index'] : $order['delivery']['address']['index'],
|
|
|
|
'tax_id' => '1',
|
|
|
|
'company' => '',
|
|
|
|
'company_id' => '',
|
2017-09-15 15:47:38 +03:00
|
|
|
'zone_id' => $customerZone ? $customerZone['zone_id'] : 0,
|
|
|
|
'country_id' => $customerCountry ? $customerCountry['country_id'] : 0,
|
|
|
|
'default' => '1'
|
2017-03-22 11:52:11 +03:00
|
|
|
)
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2017-09-15 15:47:38 +03:00
|
|
|
$customer_id = $this->model_customer_customer->addCustomer($cData);
|
2017-03-22 11:52:11 +03:00
|
|
|
|
|
|
|
$customersIdsFix[] = array('id' => $order['customer']['id'], 'externalId' => (int)$customer_id);
|
|
|
|
}
|
|
|
|
|
2017-09-15 15:47:38 +03:00
|
|
|
$mail = isset($order['email']) ? $order['email'] : $order['customer']['email'];
|
|
|
|
$phone = isset($order['phone']) ? $order['phone'] : '';
|
|
|
|
|
|
|
|
if (!$phone) {
|
|
|
|
$data['telephone'] = $order['customer']['phones'] ? $order['customer']['phones'][0]['number'] : '80000000000';
|
|
|
|
} else {
|
|
|
|
$data['telephone'] = $phone;
|
|
|
|
}
|
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
$data['store_id'] = $store == null ? 0 : $store;
|
|
|
|
$data['customer'] = $order['firstName'];
|
|
|
|
$data['customer_id'] = $customer_id;
|
|
|
|
$data['customer_group_id'] = 1;
|
|
|
|
$data['firstname'] = $order['firstName'];
|
2017-09-15 15:47:38 +03:00
|
|
|
$data['lastname'] = (isset($order['lastName'])) ? $order['lastName'] : $order['firstName'];
|
|
|
|
$data['email'] = $mail ? $mail : uniqid() . '@retailrcm.ru';
|
2017-03-22 11:52:11 +03:00
|
|
|
$data['comment'] = !empty($order['customerComment']) ? $order['customerComment'] : '';
|
|
|
|
$data['fax'] = '';
|
|
|
|
$data['payment_address'] = '0';
|
|
|
|
$data['payment_firstname'] = $order['firstName'];
|
2017-09-15 15:47:38 +03:00
|
|
|
$data['payment_lastname'] = (isset($order['lastName'])) ? $order['lastName'] : $order['firstName'];
|
2017-03-22 11:52:11 +03:00
|
|
|
$data['payment_address_1'] = $order['customer']['address']['text'];
|
|
|
|
$data['payment_address_2'] = '';
|
|
|
|
$data['payment_company'] = '';
|
|
|
|
$data['payment_company_id'] = '';
|
|
|
|
$data['payment_city'] = !empty($order['customer']['address']['city']) ? $order['customer']['address']['city'] : $order['delivery']['address']['city'];
|
|
|
|
$data['payment_postcode'] = !empty($order['customer']['address']['index']) ? $order['customer']['address']['index'] : $order['delivery']['address']['index'];
|
|
|
|
|
2017-09-15 15:47:38 +03:00
|
|
|
$shippingZone = '';
|
2017-03-22 11:52:11 +03:00
|
|
|
|
|
|
|
if (!empty($order['delivery']['address']['region']) && is_int($order['delivery']['address']['region'])) {
|
2017-09-15 15:47:38 +03:00
|
|
|
$shippingZone = $order['delivery']['address']['region'];
|
2017-03-22 11:52:11 +03:00
|
|
|
} else {
|
2017-09-15 15:47:38 +03:00
|
|
|
$shippingZone = $this->getZoneByName($order['delivery']['address']['region']);
|
|
|
|
|
|
|
|
if ($shippingZone) {
|
|
|
|
$shipping_zone_id = $shippingZone['zone_id'];
|
|
|
|
} else {
|
|
|
|
$shipping_zone_id = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($order['customer']['address']['region'])) {
|
|
|
|
$paymentZone = $this->getZoneByName($order['customer']['address']['region']);
|
|
|
|
|
|
|
|
if ($paymentZone) {
|
|
|
|
$payment_zone_id = $paymentZone['zone_id'];
|
|
|
|
} else {
|
|
|
|
$payment_zone_id = 0;
|
2017-03-22 11:52:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-15 15:47:38 +03:00
|
|
|
if (isset($order['delivery']['address']['countryIso'])) {
|
|
|
|
$shippingCountry = $this->getCountryByIsoCode($order['delivery']['address']['countryIso']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($order['customer']['address']['countryIso'])) {
|
|
|
|
$paymentCountry = $this->getCountryByIsoCode($order['customer']['address']['countryIso']);
|
|
|
|
}
|
|
|
|
|
2017-09-21 10:09:26 +03:00
|
|
|
$delivery = isset($order['delivery']['code']) ? $order['delivery']['code'] : null;
|
2017-09-15 15:47:38 +03:00
|
|
|
$data['payment_country_id'] = $paymentCountry ? $paymentCountry['country_id'] : 0;
|
|
|
|
$data['payment_zone_id'] = $payment_zone_id;
|
|
|
|
$data['shipping_country_id'] = $shippingCountry ? $shippingCountry['country_id'] : 0;
|
|
|
|
$data['shipping_zone_id'] = $shipping_zone_id;
|
2017-03-22 11:52:11 +03:00
|
|
|
$data['shipping_address'] = '0';
|
|
|
|
$data['shipping_firstname'] = $order['firstName'];
|
2017-09-15 15:47:38 +03:00
|
|
|
$data['shipping_lastname'] = (isset($order['lastName'])) ? $order['lastName'] : $order['firstName'];
|
2017-03-22 11:52:11 +03:00
|
|
|
$data['shipping_address_1'] = $order['delivery']['address']['text'];
|
|
|
|
$data['shipping_address_2'] = '';
|
|
|
|
$data['shipping_company'] = '';
|
|
|
|
$data['shipping_company_id'] = '';
|
|
|
|
$data['shipping_city'] = $order['delivery']['address']['city'];
|
|
|
|
$data['shipping_postcode'] = $order['delivery']['address']['index'];
|
2017-09-21 10:09:26 +03:00
|
|
|
$data['shipping'] = $delivery != null ? $this->delivery[$delivery] : $this->delivery_default;
|
2017-03-22 11:52:11 +03:00
|
|
|
$data['shipping_method'] = $this->ocDelivery[$data['shipping']];
|
2017-09-21 10:09:26 +03:00
|
|
|
$data['shipping_code'] = $delivery != null ? $this->delivery[$delivery] : $this->delivery_default;
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2017-07-11 17:39:48 +03:00
|
|
|
if (isset($payment)) {
|
|
|
|
$data['payment'] = $this->payment[$payment['type']];
|
|
|
|
$data['payment_method'] = $this->ocPayment[$data['payment']];
|
|
|
|
$data['payment_code'] = $this->payment[$payment['type']];
|
2017-09-21 10:09:26 +03:00
|
|
|
} else {
|
|
|
|
$data['payment'] = $this->payment_default;
|
|
|
|
$data['payment_method'] = $this->ocPayment[$data['payment']];
|
|
|
|
$data['payment_code'] = $this->payment_default;
|
2017-07-11 17:39:48 +03:00
|
|
|
}
|
2017-09-15 15:47:38 +03:00
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
// this data will not retrive from crm for now
|
|
|
|
$data['tax'] = '';
|
|
|
|
$data['tax_id'] = '';
|
|
|
|
$data['product'] = '';
|
|
|
|
$data['product_id'] = '';
|
|
|
|
$data['reward'] = '';
|
|
|
|
$data['affiliate'] = '';
|
|
|
|
$data['affiliate_id'] = '';
|
|
|
|
$data['payment_tax_id'] = '';
|
|
|
|
$data['order_product_id'] = '';
|
|
|
|
$data['payment_company'] = '';
|
|
|
|
$data['payment_company_id'] = '';
|
|
|
|
$data['company'] = '';
|
|
|
|
$data['company_id'] = '';
|
|
|
|
|
|
|
|
$data['order_product'] = array();
|
|
|
|
|
|
|
|
foreach ($order['items'] as $item) {
|
2017-09-15 15:47:38 +03:00
|
|
|
$productId = $item['offer']['externalId'];
|
|
|
|
$options = array();
|
|
|
|
if(mb_strpos($item['offer']['externalId'], '#') > 1) {
|
|
|
|
$offer = explode('#', $item['offer']['externalId']);
|
|
|
|
$productId = $offer[0];
|
|
|
|
$optionsFromCRM = explode('_', $offer[1]);
|
|
|
|
|
|
|
|
foreach ($optionsFromCRM as $optionFromCRM) {
|
|
|
|
$optionData = explode('-', $optionFromCRM);
|
|
|
|
$productOptionId = $optionData[0];
|
|
|
|
$optionValueId = $optionData[1];
|
|
|
|
|
|
|
|
$productOptions = $this->model_catalog_product->getProductOptions($productId);
|
|
|
|
|
|
|
|
foreach($productOptions as $productOption) {
|
|
|
|
if($productOptionId == $productOption['product_option_id']) {
|
|
|
|
foreach($productOption['product_option_value'] as $productOptionValue) {
|
|
|
|
if($productOptionValue['option_value_id'] == $optionValueId) {
|
|
|
|
$options[$productOptionId] = $productOptionValue['product_option_value_id'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-22 11:52:11 +03:00
|
|
|
$data['order_product'][] = array(
|
2017-09-15 15:47:38 +03:00
|
|
|
'product_id' => $productId,
|
2017-03-22 11:52:11 +03:00
|
|
|
'quantity' => $item['quantity'],
|
2017-09-15 15:47:38 +03:00
|
|
|
'option' => $options
|
2017-03-22 11:52:11 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-09-21 10:09:26 +03:00
|
|
|
if (isset($this->customFieldSetting) && $order['customFields']) {
|
|
|
|
foreach ($order['customFields'] as $code => $value) {
|
|
|
|
if (array_key_exists($code, $this->customFieldSetting)) {
|
|
|
|
$fieldCode = str_replace('o_', '', $this->customFieldSetting[$code]);
|
|
|
|
$customFields[$fieldCode] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$data['custom_field'] = isset($customFields) ? $customFields : '';
|
|
|
|
}
|
|
|
|
|
2017-03-22 11:52:11 +03:00
|
|
|
$deliveryCost = !empty($order['delivery']['cost']) ? $order['delivery']['cost'] : 0;
|
|
|
|
|
|
|
|
$data['order_total'] = array(
|
|
|
|
array(
|
|
|
|
'order_total_id' => '',
|
|
|
|
'code' => 'sub_total',
|
|
|
|
'title' => $this->language->get('product_summ'),
|
|
|
|
'value' => $order['summ'],
|
|
|
|
'text' => $order['summ'],
|
|
|
|
'sort_order' => $this->subtotalSettings['sub_total_sort_order']
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'order_total_id' => '',
|
|
|
|
'code' => 'shipping',
|
|
|
|
'title' => $this->ocDelivery[$data['shipping_code']],
|
|
|
|
'value' => $deliveryCost,
|
|
|
|
'text' => $deliveryCost,
|
2017-07-21 10:27:08 +03:00
|
|
|
'sort_order' => $this->shippingSettings[$this->totalTitle . 'shipping_sort_order']
|
2017-03-22 11:52:11 +03:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'order_total_id' => '',
|
|
|
|
'code' => 'total',
|
|
|
|
'title' => $this->language->get('column_total'),
|
|
|
|
'value' => !empty($order['totalSumm']) ? $order['totalSumm'] : $order['summ'] + $deliveryCost,
|
|
|
|
'text' => isset($order['totalSumm']) ? $order['totalSumm'] : $order['summ'] + $deliveryCost,
|
2017-07-21 10:27:08 +03:00
|
|
|
'sort_order' => $this->totalSettings[$this->totalTitle . 'total_sort_order']
|
2017-03-22 11:52:11 +03:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$data['fromApi'] = true;
|
|
|
|
$data['order_status_id'] = 1;
|
|
|
|
|
2017-09-15 15:47:38 +03:00
|
|
|
$order_id = $this->opencartApiClient->addOrder($data);
|
2017-03-22 11:52:11 +03:00
|
|
|
|
2017-09-15 15:47:38 +03:00
|
|
|
$ordersIdsFix[] = array('id' => $order['id'], 'externalId' => (int) $order_id);
|
2017-03-22 11:52:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return array('customers' => $customersIdsFix, 'orders' => $ordersIdsFix);
|
|
|
|
}
|
2017-05-17 17:48:50 +03:00
|
|
|
|
|
|
|
protected function updateCustomers($customers)
|
2017-09-21 10:09:26 +03:00
|
|
|
{
|
2017-05-17 17:48:50 +03:00
|
|
|
foreach ($customers as $customer) {
|
|
|
|
|
|
|
|
$customer_id = $customer['externalId'];
|
|
|
|
$customerData = $this->model_customer_customer->getCustomer($customer_id);
|
2017-09-15 15:47:38 +03:00
|
|
|
|
2017-05-17 17:48:50 +03:00
|
|
|
$customerData['firstname'] = $customer['firstName'];
|
2017-09-15 15:47:38 +03:00
|
|
|
$customerData['lastname'] = isset($customer['lastName']) ? $customer['lastName'] : '';
|
2017-05-17 17:48:50 +03:00
|
|
|
$customerData['email'] = $customer['email'];
|
2017-09-15 15:47:38 +03:00
|
|
|
$customerData['telephone'] = $customer['phones'] ? $customer['phones'][0]['number'] : '';
|
|
|
|
|
2017-05-17 17:48:50 +03:00
|
|
|
$customerAddress = $this->model_customer_customer->getAddress($customerData['address_id']);
|
2017-09-15 15:47:38 +03:00
|
|
|
|
|
|
|
if (isset($customer['address']['countryIso'])) {
|
|
|
|
$customerCountry = $this->getCountryByIsoCode($customer['address']['countryIso']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($customer['address']['region'])) {
|
|
|
|
$customerZone = $this->getZoneByName($customer['address']['region']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$customerAddress['firstname'] = isset($customer['patronymic']) ? $customer['firstName'] . ' ' . $customer['patronymic'] : $customer['firstName'];
|
|
|
|
$customerAddress['lastname'] = isset($customer['lastName']) ? $customer['lastName'] : '';
|
2017-05-17 17:48:50 +03:00
|
|
|
$customerAddress['address_1'] = $customer['address']['text'];
|
|
|
|
$customerAddress['city'] = $customer['address']['city'];
|
2017-09-15 15:47:38 +03:00
|
|
|
$customerAddress['postcode'] = $customer['address']['index'] ? $customer['address']['index'] : '';
|
|
|
|
|
|
|
|
if (isset($customerCountry)) {
|
|
|
|
$customerAddress['country_id'] = $customerCountry['country_id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($customerZone)) {
|
|
|
|
$customerAddress['zone_id'] = $customerZone['zone_id'];
|
|
|
|
}
|
|
|
|
|
2017-05-17 17:48:50 +03:00
|
|
|
$customerData['address'] = array($customerAddress);
|
|
|
|
|
2017-09-21 10:09:26 +03:00
|
|
|
if (isset($this->customFieldSetting) && $customer['customFields']) {
|
2017-09-15 15:47:38 +03:00
|
|
|
foreach ($customer['customFields'] as $code => $value) {
|
2017-09-21 10:09:26 +03:00
|
|
|
if (array_key_exists($code, $this->customFieldSetting)) {
|
|
|
|
$fieldCode = str_replace('c_', '', $this->customFieldSetting[$code]);
|
|
|
|
$customFields[$fieldCode] = $value;
|
2017-09-15 15:47:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$customerData['custom_field'] = isset($customFields) ? $customFields : '';
|
|
|
|
}
|
|
|
|
|
2017-05-17 17:48:50 +03:00
|
|
|
$this->model_customer_customer->editCustomer($customer_id, $customerData);
|
|
|
|
}
|
|
|
|
}
|
2017-07-11 17:39:48 +03:00
|
|
|
|
|
|
|
private function getModuleTitle()
|
|
|
|
{
|
|
|
|
if (version_compare(VERSION, '3.0', '<')) {
|
|
|
|
$title = 'retailcrm';
|
|
|
|
} else {
|
|
|
|
$title = 'module_retailcrm';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $title;
|
|
|
|
}
|
2017-07-21 10:27:08 +03:00
|
|
|
|
|
|
|
private function totalTitles()
|
|
|
|
{
|
|
|
|
if (version_compare(VERSION, '3.0', '<')) {
|
|
|
|
$title = '';
|
|
|
|
} else {
|
|
|
|
$title = 'total_';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $title;
|
|
|
|
}
|
2017-09-15 15:47:38 +03:00
|
|
|
|
|
|
|
public function getCountryByIsoCode($isoCode)
|
|
|
|
{
|
|
|
|
$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "country` WHERE iso_code_2 = '" . $isoCode . "'");
|
|
|
|
|
|
|
|
return $query->row;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getZoneByName($name)
|
|
|
|
{
|
|
|
|
$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "zone` WHERE name = '" . $name . "'");
|
|
|
|
|
|
|
|
return $query->row;
|
|
|
|
}
|
2017-03-22 11:52:11 +03:00
|
|
|
}
|