opencart-module/admin/model/retailcrm/history.php

479 lines
22 KiB
PHP
Raw Normal View History

<?php
class ModelRetailcrmHistory extends Model
{
2016-02-09 16:21:45 +03:00
protected $createResult;
public function request()
2016-04-15 12:02:42 +03:00
{
$this->load->model('setting/setting');
$this->load->model('setting/store');
$this->load->model('sale/order');
2016-08-31 17:37:53 +03:00
$this->load->model('sale/customer');
2016-02-09 04:22:06 +03:00
$this->load->model('retailcrm/references');
$this->load->model('catalog/product');
2016-07-25 17:21:15 +03:00
$this->load->model('catalog/option');
$this->load->model('localisation/zone');
$this->load->language('module/retailcrm');
$settings = $this->model_setting_setting->getSetting('retailcrm');
2016-02-09 16:21:45 +03:00
$history = $this->model_setting_setting->getSetting('retailcrm_history');
$settings['domain'] = parse_url(HTTP_SERVER, PHP_URL_HOST);
2016-02-09 16:21:45 +03:00
$url = isset($settings['retailcrm_url']) ? $settings['retailcrm_url'] : null;
$key = isset($settings['retailcrm_apikey']) ? $settings['retailcrm_apikey'] : null;
2016-01-29 17:59:53 +03:00
2016-02-09 16:21:45 +03:00
if (empty($url) || empty($key)) {
$this->log->addNotice('You need to configure retailcrm module first.');
return false;
}
2016-02-09 04:22:06 +03:00
2016-02-09 16:21:45 +03:00
$crm = new RetailcrmProxy(
$settings['retailcrm_url'],
$settings['retailcrm_apikey'],
DIR_SYSTEM . 'logs/retailcrm.log'
);
2016-02-09 16:21:45 +03:00
$lastRun = !empty($history['retailcrm_history'])
? new DateTime($history['retailcrm_history'])
: new DateTime(date('Y-m-d H:i:s', strtotime('-1 days', strtotime(date('Y-m-d H:i:s')))));
2016-02-09 16:21:45 +03:00
$orders = $crm->ordersHistory($lastRun);
$generatedAt = $orders['generatedAt'];
2016-02-09 04:22:06 +03:00
2016-02-09 16:21:45 +03:00
$this->subtotalSettings = $this->model_setting_setting->getSetting('sub_total');
$this->totalSettings = $this->model_setting_setting->getSetting('total');
$this->shippingSettings = $this->model_setting_setting->getSetting('shipping');
2016-02-09 16:21:45 +03:00
$this->delivery = array_flip($settings['retailcrm_delivery']);
$this->payment = array_flip($settings['retailcrm_payment']);
$this->status = array_flip($settings['retailcrm_status']);
2016-02-09 16:21:45 +03:00
$this->ocPayment = $this->model_retailcrm_references
->getOpercartPaymentTypes();
2016-02-09 16:21:45 +03:00
$this->ocDelivery = $this->model_retailcrm_references
->getOpercartDeliveryTypes();
2016-02-09 16:21:45 +03:00
$this->zones = $this->model_localisation_zone->getZones();
2016-02-09 16:21:45 +03:00
$updatedOrders = array();
$newOrders = array();
2016-02-09 16:21:45 +03:00
foreach ($orders['orders'] as $order) {
2016-02-09 16:21:45 +03:00
if (isset($order['deleted'])) continue;
2016-02-09 16:21:45 +03:00
if (isset($order['externalId'])) {
$updatedOrders[] = $order['id'];
} else {
$newOrders[] = $order['id'];
}
}
2016-02-09 16:21:45 +03:00
unset($orders);
2016-02-09 16:21:45 +03:00
if (!empty($newOrders)) {
$orders = $crm->ordersList($filter = array('ids' => $newOrders));
if ($orders) {
$this->createResult = $this->createOrders($orders['orders']);
}
}
2016-02-09 04:22:06 +03:00
2016-02-09 16:21:45 +03:00
if (!empty($updatedOrders)) {
$orders = $crm->ordersList($filter = array('ids' => $updatedOrders));
if ($orders) {
$this->updateOrders($orders['orders']);
}
}
2016-02-09 04:22:06 +03:00
2016-02-09 16:21:45 +03:00
$this->model_setting_setting->editSetting('retailcrm_history', array('retailcrm_history' => $generatedAt));
if (!empty($this->createResult['customers'])) {
$crm->customersFixExternalIds($this->createResult['customers']);
}
2016-02-09 04:22:06 +03:00
2016-03-03 17:16:43 +03:00
if (!empty($this->createResult['orders'])) {
2016-02-09 16:21:45 +03:00
$crm->ordersFixExternalIds($this->createResult['orders']);
}
}
2016-02-09 04:22:06 +03:00
2016-02-09 16:21:45 +03:00
protected function updateOrders($orders)
{
foreach ($orders as $order) {
$store = $this->config->get('config_store_id');
$data = array();
$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'];
$data['lastname'] = (!empty($order['lastName'])) ? $order['lastName'] : ' ';
$data['email'] = $order['email'];
2016-04-14 13:55:11 +03:00
$data['telephone'] = (!empty($order['phone'])) ? $order['phone'] : '';
2016-02-09 16:21:45 +03:00
$data['comment'] = !empty($order['customerComment']) ? $order['customerComment'] : '';
$data['fax'] = '';
$data['payment_address'] = '0';
$data['payment_firstname'] = $order['firstName'];
$data['payment_lastname'] = (!empty($order['lastName'])) ? $order['lastName'] : ' ';
2016-04-08 16:39:51 +03:00
$data['payment_address_1'] = isset($order['customer']['address']) ? $order['customer']['address']['text'] : '';
2016-02-09 16:21:45 +03:00
$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'];
$region = '';
if (is_int($order['delivery']['address']['region'])) {
$region = $order['delivery']['address']['region'];
} else {
foreach ($this->zones as $zone) {
if ($order['delivery']['address']['region'] == $zone['name']) {
$region = $zone['zone_id'];
2016-02-09 04:22:06 +03:00
}
}
2016-02-09 16:21:45 +03:00
}
2016-02-09 16:21:45 +03:00
$data['payment_country_id'] = !empty($order['delivery']['address']['country']) ? $order['delivery']['address']['country'] : 0;
$data['payment_zone_id'] = !empty($order['delivery']['address']['region']) ? $order['delivery']['address']['region'] : $region;
$data['shipping_country_id'] = !empty($order['delivery']['address']['country']) ? $order['delivery']['address']['country'] : 0;
$data['shipping_zone_id'] = $region;
$data['shipping_address'] = '0';
$data['shipping_firstname'] = $order['firstName'];
$data['shipping_lastname'] = (!empty($order['lastName'])) ? $order['lastName'] : ' ';
$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'];
$data['shipping'] = $this->delivery[$order['delivery']['code']];
$data['shipping_method'] = $this->ocDelivery[$data['shipping']];
$data['shipping_code'] = $this->delivery[$order['delivery']['code']];
2016-04-08 16:39:51 +03:00
2016-02-09 16:21:45 +03:00
$data['payment'] = $this->payment[$order['paymentType']];
$data['payment_method'] = $this->ocPayment[$data['payment']];
$data['payment_code'] = $this->payment[$order['paymentType']];
// 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) {
2016-08-31 17:37:53 +03:00
$product = $this->model_catalog_product->getProduct($item['offer']['externalId']);
2016-07-25 17:21:15 +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];
2016-08-31 17:37:53 +03:00
$productOptionValueId = $optionData[1];
2016-07-25 17:21:15 +03:00
$productOptions = $this->model_catalog_product->getProductOptions($productId);
foreach($productOptions as $productOption) {
2016-08-31 17:37:53 +03:00
if($productOption['product_option_id'] == $productOptionId) {
2016-07-25 17:21:15 +03:00
foreach($productOption['product_option_value'] as $productOptionValue) {
2016-08-31 17:37:53 +03:00
if($productOptionValue['option_value_id'] == $productOptionValueId) {
$productOptionValue = $this->model_catalog_option->getOptionValue($productOptionValueId);
$options[] = array(
'order_option_id' => 'default', // default или NULL для автоинкремента http://stackoverflow.com/questions/8753371/how-to-insert-data-to-mysql-having-auto-incremented-primary-key
'product_option_id' => $productOptionId,
'product_option_value_id' => $productOptionValueId,
'name' => $productOption['name'],
'value' => $productOptionValue['name'],
'type' => $productOption['type']
);
2016-07-25 17:21:15 +03:00
}
}
}
}
}
}
2016-08-31 17:37:53 +03:00
2016-02-09 16:21:45 +03:00
$data['order_product'][] = array(
2016-07-25 17:21:15 +03:00
'product_id' => $productId,
2016-08-31 17:37:53 +03:00
'name' => $item['offer']['name'],
2016-02-09 16:21:45 +03:00
'quantity' => $item['quantity'],
2016-08-31 17:37:53 +03:00
'price' => $item['initialPrice'],
'total' => $item['initialPrice'] * $item['quantity'],
'model' => $product['model'],
'order_option' => $options,
// this data will not retrive from crm
'order_product_id' => '',
'tax' => 0,
'reward' => 0
2016-02-09 16:21:45 +03:00
);
}
$deliveryCost = !empty($order['delivery']['cost']) ? $order['delivery']['cost'] : 0;
2016-09-26 15:47:14 +03:00
$order_totals = $this->model_sale_order->getOrderTotals($order['externalId']);
$total_summ = isset($order['totalSumm']) ? $order['totalSumm'] : $order['summ'] + $deliveryCost;
2016-02-09 16:21:45 +03:00
$data['order_total'] = array(
2016-09-26 15:47:14 +03:00
'sub_total' => array(
2016-02-09 16:21:45 +03:00
'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']
),
2016-09-26 15:47:14 +03:00
'shipping' => array(
2016-02-09 16:21:45 +03:00
'order_total_id' => '',
'code' => 'shipping',
'title' => $this->ocDelivery[$data['shipping_code']],
'value' => $deliveryCost,
'text' => $deliveryCost,
'sort_order' => $this->shippingSettings['shipping_sort_order']
),
2016-09-26 15:47:14 +03:00
'total' => array(
2016-02-09 16:21:45 +03:00
'order_total_id' => '',
'code' => 'total',
'title' => $this->language->get('column_total'),
2016-09-26 15:47:14 +03:00
'value' => $total_summ,
'text' => $total_summ,
2016-02-09 16:21:45 +03:00
'sort_order' => $this->totalSettings['total_sort_order']
2016-02-09 04:22:06 +03:00
)
2016-02-09 16:21:45 +03:00
);
2016-02-09 04:22:06 +03:00
2016-09-26 15:47:14 +03:00
foreach ($order_totals as $value) {
if(in_array($value['code'], array_keys($data['order_total']))) {
continue;
}
$data['order_total'][$value['code']] = $value;
$total_summ += $value['value'];
}
$data['order_total']['total']['value'] = $total_summ;
$data['order_total']['total']['text'] = $total_summ;
2016-02-09 16:21:45 +03:00
$data['fromApi'] = true;
2016-02-09 04:22:06 +03:00
2016-02-09 16:21:45 +03:00
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'];
}
2016-02-09 04:22:06 +03:00
2016-08-31 17:37:53 +03:00
$this->model_sale_order->editOrder($order['externalId'], $data);
2016-02-09 16:21:45 +03:00
}
}
protected function createOrders($orders)
{
$customersIdsFix = array();
$ordersIdsFix = array();
foreach ($orders as $order) {
$store = $this->config->get('config_store_id');
$customer_id = (!empty($order['customer']['externalId']))
? $order['customer']['externalId']
: 0;
$data = array();
if ($customer_id == 0) {
$cData = array(
'store_id' => 0,
'customer_group_id' => '1',
'firstname' => $order['firstName'],
'lastname' => (!empty($order['lastName'])) ? $order['lastName'] : ' ',
'email' => $order['email'],
'telephone' => (!empty($order['customer']['phones'][0]['number']) ) ? $order['customer']['phones'][0]['number'] : ' ',
'fax' => '',
'newsletter' => 0,
'password' => 'tmppass',
'status' => 1,
'address' => array(
array(
'firstname' => $order['firstName'],
'lastname' => (!empty($order['lastName'])) ? $order['lastName'] : ' ',
'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' => '',
'zone_id' => '0',
'country_id' => 0
)
2016-02-09 04:22:06 +03:00
),
);
2016-08-31 17:37:53 +03:00
$this->model_sale_customer->addCustomer($cData);
2016-02-09 04:22:06 +03:00
2016-02-09 16:21:45 +03:00
if (!empty($order['email'])) {
2016-08-31 17:37:53 +03:00
$tryToFind = $this->model_sale_customer->getCustomerByEmail($order['email']);
2016-02-09 16:21:45 +03:00
$customer_id = $tryToFind['customer_id'];
2016-02-09 04:22:06 +03:00
} else {
2016-08-31 17:37:53 +03:00
$last = $this->model_sale_customer->getCustomers($data = array('order' => 'DESC', 'limit' => 1));
2016-02-09 16:21:45 +03:00
$customer_id = $last[0]['customer_id'];
}
2016-02-09 16:21:45 +03:00
$customersIdsFix[] = array('id' => $order['customer']['id'], 'externalId' => (int)$customer_id);
}
2016-02-09 16:21:45 +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'];
$data['lastname'] = (!empty($order['lastName'])) ? $order['lastName'] : ' ';
$data['email'] = $order['email'];
$data['telephone'] = (!empty($order['customer']['phones'][0]['number'])) ? $order['customer']['phones'][0]['number'] : ' ';
$data['comment'] = !empty($order['customerComment']) ? $order['customerComment'] : '';
$data['fax'] = '';
$data['payment_address'] = '0';
$data['payment_firstname'] = $order['firstName'];
$data['payment_lastname'] = (!empty($order['lastName'])) ? $order['lastName'] : ' ';
$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'];
$region = '';
if (!empty($order['delivery']['address']['region']) && is_int($order['delivery']['address']['region'])) {
$region = $order['delivery']['address']['region'];
} else {
foreach ($this->zones as $zone) {
if ($order['delivery']['address']['region'] == $zone['name']) {
$region = $zone['zone_id'];
}
}
}
2016-02-09 16:21:45 +03:00
$data['payment_country_id'] = !empty($order['delivery']['address']['country']) ? $order['delivery']['address']['country'] : 0;
$data['payment_zone_id'] = !empty($order['delivery']['address']['region']) ? $order['delivery']['address']['region'] : $region;
$data['shipping_country_id'] = !empty($order['delivery']['address']['country']) ? $order['delivery']['address']['country'] : 0;
$data['shipping_zone_id'] = $region;
$data['shipping_address'] = '0';
$data['shipping_firstname'] = $order['firstName'];
$data['shipping_lastname'] = (!empty($order['lastName'])) ? $order['lastName'] : ' ';
$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'];
$data['shipping'] = $this->delivery[$order['delivery']['code']];
$data['shipping_method'] = $this->ocDelivery[$data['shipping']];
$data['shipping_code'] = $this->delivery[$order['delivery']['code']];
$data['payment'] = $this->payment[$order['paymentType']];
$data['payment_method'] = $this->ocPayment[$data['payment']];
$data['payment_code'] = $this->payment[$order['paymentType']];
// 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) {
$product = $this->model_catalog_product->getProduct($item['offer']['externalId']);
$data['order_product'][] = array(
'product_id' => $item['offer']['externalId'],
'name' => $item['offer']['name'],
'quantity' => $item['quantity'],
'price' => $item['initialPrice'],
'total' => $item['initialPrice'] * $item['quantity'],
'model' => $product['model'],
// this data will not retrive from crm
'order_product_id' => '',
'tax' => 0,
'reward' => 0
);
}
2016-02-09 16:21:45 +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,
'sort_order' => $this->shippingSettings['shipping_sort_order']
),
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,
'sort_order' => $this->totalSettings['total_sort_order']
)
);
$data['fromApi'] = true;
$data['order_status_id'] = 1;
2016-08-31 17:37:53 +03:00
$this->model_sale_order->addOrder($data);
2016-02-09 16:21:45 +03:00
$last = $this->model_sale_order->getOrders($data = array('order' => 'DESC', 'limit' => 1, 'start' => 0));
$ordersIdsFix[] = array('id' => $order['id'], 'externalId' => (int) $last[0]['order_id']);
}
2016-02-09 16:21:45 +03:00
return array('customers' => $customersIdsFix, 'orders' => $ordersIdsFix);
}
2015-07-23 13:14:23 +03:00
}