diff --git a/admin/model/retailcrm/history.php b/admin/model/retailcrm/history.php index 2519fb0..9797cc5 100644 --- a/admin/model/retailcrm/history.php +++ b/admin/model/retailcrm/history.php @@ -4,8 +4,6 @@ class ModelRetailcrmHistory extends Model { protected $createResult; - private $opencartApiClient; - public function request() { $this->load->model('setting/setting'); @@ -14,6 +12,7 @@ class ModelRetailcrmHistory extends Model $this->load->model('sale/customer'); $this->load->model('retailcrm/references'); $this->load->model('catalog/product'); + $this->load->model('catalog/option'); $this->load->model('localisation/zone'); $this->load->language('module/retailcrm'); @@ -183,13 +182,50 @@ class ModelRetailcrmHistory extends Model foreach ($order['items'] as $item) { $product = $this->model_catalog_product->getProduct($item['offer']['externalId']); + + $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]; + $productOptionValueId = $optionData[1]; + + $productOptions = $this->model_catalog_product->getProductOptions($productId); + + foreach($productOptions as $productOption) { + if($productOption['product_option_id'] == $productOptionId) { + foreach($productOption['product_option_value'] as $productOptionValue) { + 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'] + ); + } + } + } + } + } + } + $data['order_product'][] = array( - 'product_id' => $item['offer']['externalId'], + 'product_id' => $productId, 'name' => $item['offer']['name'], 'quantity' => $item['quantity'], 'price' => $item['initialPrice'], 'total' => $item['initialPrice'] * $item['quantity'], 'model' => $product['model'], + 'order_option' => $options, // this data will not retrive from crm 'order_product_id' => '', diff --git a/admin/model/retailcrm/icml.php b/admin/model/retailcrm/icml.php index f60d378..ca7883e 100644 --- a/admin/model/retailcrm/icml.php +++ b/admin/model/retailcrm/icml.php @@ -10,12 +10,16 @@ class ModelRetailcrmIcml extends Model protected $eCategories; protected $eOffers; + private $options; + private $optionValues; + public function generateICML() { $this->load->language('module/retailcrm'); $this->load->model('catalog/category'); $this->load->model('catalog/product'); $this->load->model('catalog/manufacturer'); + $this->load->model('catalog/option'); $string = ' @@ -95,107 +99,218 @@ class ModelRetailcrmIcml extends Model $products = $this->model_catalog_product->getProducts(array()); - foreach ($products as $offer) { + foreach ($products as $product) { + // Формируем офферы отнсительно доступных опций + $options = $this->model_catalog_product->getProductOptions($product['product_id']); - $e = $this->eOffers->appendChild($this->dd->createElement('offer')); - $e->setAttribute('id', $offer['product_id']); - $e->setAttribute('productId', $offer['product_id']); - $e->setAttribute('quantity', $offer['quantity']); + $offerOptions = array('select', 'radio'); - /** - * Offer activity - */ - $activity = $offer['status'] == 1 ? 'Y' : 'N'; - $e->appendChild( - $this->dd->createElement('productActivity') - )->appendChild( - $this->dd->createTextNode($activity) - ); + $requiredOptions = array(); + $notRequiredOptions = array(); - /** - * Offer categories - */ - $categories = $this->model_catalog_product - ->getProductCategories($offer['product_id']); - - if (!empty($categories)) { - foreach ($categories as $category) { - $e->appendChild($this->dd->createElement('categoryId')) - ->appendChild( - $this->dd->createTextNode($category) - ); + // Оставляем опции связанные с вариациями товаров, сортируем по параметру обязательный или нет + foreach($options as $option) { + if(in_array($option['type'], $offerOptions)) { + if($option['required']) { + $requiredOptions[] = $option; + } else { + $notRequiredOptions[] = $option; + } } } - /** - * Name & price - */ - $e->appendChild($this->dd->createElement('name')) - ->appendChild($this->dd->createTextNode($offer['name'])); - $e->appendChild($this->dd->createElement('productName')) - ->appendChild($this->dd->createTextNode($offer['name'])); - $e->appendChild($this->dd->createElement('price')) - ->appendChild($this->dd->createTextNode($offer['price'])); + $offers = array(); - /** - * Vendor - */ - if ($offer['manufacturer_id'] != 0) { - $e->appendChild($this->dd->createElement('vendor')) + // Сначала совмещаем все обязательные опции + foreach($requiredOptions as $requiredOption) { + // Если первая итерация + if(empty($offers)) { + foreach($requiredOption['product_option_value'] as $optionValue) { + $offers[$requiredOption['product_option_id'].':'.$requiredOption['option_id'].'-'.$optionValue['option_value_id']] = (float)$optionValue['price']; + } + } else { + foreach($offers as $optionKey => $optionCost) { + unset($offers[$optionKey]); // Работая в контексте обязательных опций не забываем удалять прошлые обязательные опции, т.к. они должны быть скомбинированы с другими обязательными опциями + foreach($requiredOption['product_option_value'] as $optionValue) { + $offers[$optionKey.'_'.$requiredOption['product_option_id'].':'.$requiredOption['option_id'].'-'.$optionValue['option_value_id']] = $optionCost + (float)$optionValue['price']; + } + } + } + } + + // Совмещаем или добавляем необязательные опции, учитывая тот факт что обязательных опций может и не быть. + foreach($notRequiredOptions as $notRequiredOption) { + // Если обязательных опцией не оказалось и первая итерация + if(empty($offers)) { + $offers['0:0-0'] = 0; // В случае работы с необязательными опциями мы должны учитывать товарное предложение без опций, поэтому создадим "пустую" опцию + foreach($notRequiredOption['product_option_value'] as $optionValue) { + $offers[$notRequiredOption['product_option_id'].':'.$notRequiredOption['option_id'].'-'.$optionValue['option_value_id']] = (float)$optionValue['price']; + } + } else { + foreach($offers as $optionKey => $optionCost) { + foreach($notRequiredOption['product_option_value'] as $optionValue) { + $offers[$optionKey.'_'.$notRequiredOption['product_option_id'].':'.$notRequiredOption['option_id'].'-'.$optionValue['option_value_id']] = $optionCost + (float)$optionValue['price']; + } + } + } + } + + if(empty($offers)) { + $offers = array('0:0-0' => '0'); + } + + foreach($offers as $optionsString => $optionsTotalCost) { + $optionsString = explode('_', $optionsString); + $options = array(); + foreach($optionsString as $optionString) { + $option = explode('-', $optionString); + $optionIds = explode(':', $option[0]); + + if($optionString != '0:0-0') { + $optionData = $this->getOptionData($optionIds[1], $option[1]); + + $options[$optionIds[0]] = array( + 'name' => $optionData['optionName'], + 'value' => $optionData['optionValue'], + 'value_id' => $option[1] + ); + } + + } + ksort($options); + + $offerId = array(); + foreach($options as $optionKey => $optionData) { + $offerId[] = $optionKey.'-'.$optionData['value_id']; + } + $offerId = implode('_', $offerId); + + $e = $this->eOffers->appendChild($this->dd->createElement('offer')); + if(!empty($offerId)) + $e->setAttribute('id', $product['product_id'].'#'.$offerId); + else + $e->setAttribute('id', $product['product_id']); + $e->setAttribute('productId', $product['product_id']); + $e->setAttribute('quantity', $product['quantity']); + + /** + * Offer activity + */ + $activity = $product['status'] == 1 ? 'Y' : 'N'; + $e->appendChild( + $this->dd->createElement('productActivity') + )->appendChild( + $this->dd->createTextNode($activity) + ); + + /** + * Offer categories + */ + $categories = $this->model_catalog_product + ->getProductCategories($product['product_id']); + + if (!empty($categories)) { + foreach ($categories as $category) { + $e->appendChild($this->dd->createElement('categoryId')) + ->appendChild( + $this->dd->createTextNode($category) + ); + } + } + + /** + * Name & price + */ + $e->appendChild($this->dd->createElement('productName')) + ->appendChild($this->dd->createTextNode($product['name'])); + if(!empty($options)) { + $optionsString = array(); + foreach($options as $option) { + $optionsString[] = $option['name'].': '.$option['value']; + } + $optionsString = ' ('.implode(', ', $optionsString).')'; + + $e->appendChild($this->dd->createElement('name')) + ->appendChild($this->dd->createTextNode($product['name'].$optionsString)); + } else { + $e->appendChild($this->dd->createElement('name')) + ->appendChild($this->dd->createTextNode($product['name'])); + } + + $e->appendChild($this->dd->createElement('price')) + ->appendChild($this->dd->createTextNode($product['price'] + $optionsTotalCost)); + + /** + * Vendor + */ + if ($product['manufacturer_id'] != 0) { + $e->appendChild($this->dd->createElement('vendor')) + ->appendChild( + $this->dd->createTextNode( + $offerManufacturers[$product['manufacturer_id']] + ) + ); + } + + /** + * Image + */ + if ($product['image']) { + $image = $this->generateImage($product['image']); + $e->appendChild($this->dd->createElement('picture')) + ->appendChild($this->dd->createTextNode($image)); + } + + /** + * Url + */ + $this->url = new Url( + HTTP_CATALOG, + $this->config->get('config_secure') + ? HTTP_CATALOG + : HTTPS_CATALOG + ); + + $e->appendChild($this->dd->createElement('url')) ->appendChild( $this->dd->createTextNode( - $offerManufacturers[$offer['manufacturer_id']] + $this->url->link( + 'product/product&product_id=' . $product['product_id'] + ) ) ); - } - /** - * Image - */ - if ($offer['image']) { - $image = $this->generateImage($offer['image']); - $e->appendChild($this->dd->createElement('picture')) - ->appendChild($this->dd->createTextNode($image)); - } - - /** - * Url - */ - $this->url = new Url( - HTTP_CATALOG, - $this->config->get('config_secure') - ? HTTP_CATALOG - : HTTPS_CATALOG - ); - - $e->appendChild($this->dd->createElement('url')) - ->appendChild( - $this->dd->createTextNode( - $this->url->link( - 'product/product&product_id=' . $offer['product_id'] - ) - ) - ); + // Options + if(!empty($options)) { + foreach($options as $optionKey => $optionData) { + $param = $this->dd->createElement('param'); + $param->setAttribute('code', $optionKey); + $param->setAttribute('name', $optionData['name']); + $param->appendChild($this->dd->createTextNode($optionData['value'])); + $e->appendChild($param); + } + } - if ($offer['sku']) { - $sku = $this->dd->createElement('param'); - $sku->setAttribute('code', 'article'); - $sku->setAttribute('name', $this->language->get('article')); - $sku->appendChild($this->dd->createTextNode($offer['sku'])); - $e->appendChild($sku); - } + if ($product['sku']) { + $sku = $this->dd->createElement('param'); + $sku->setAttribute('code', 'article'); + $sku->setAttribute('name', $this->language->get('article')); + $sku->appendChild($this->dd->createTextNode($product['sku'])); + $e->appendChild($sku); + } - if ($offer['weight'] != '') { - $weight = $this->dd->createElement('param'); - $weight->setAttribute('code', 'weight'); - $weight->setAttribute('name', $this->language->get('weight')); - $weightValue = (isset($offer['weight_class'])) - ? round($offer['weight'], 3) . ' ' . $offer['weight_class'] - : round($offer['weight'], 3) - ; - $weight->appendChild($this->dd->createTextNode($weightValue)); - $e->appendChild($weight); + if ($product['weight'] != '') { + $weight = $this->dd->createElement('param'); + $weight->setAttribute('code', 'weight'); + $weight->setAttribute('name', $this->language->get('weight')); + $weightValue = (isset($offer['weight_class'])) + ? round($product['weight'], 3) . ' ' . $product['weight_class'] + : round($product['weight'], 3) + ; + $weight->appendChild($this->dd->createTextNode($weightValue)); + $e->appendChild($weight); + } } } } @@ -214,4 +329,25 @@ class ModelRetailcrmIcml extends Model $this->config->get('config_image_product_height') ); } + + private function getOptionData($optionId, $optionValueId) { + if(!empty($this->options[$optionId])) { + $option = $this->options[$optionId]; + } else { + $option = $this->model_catalog_option->getOption($optionId); + $this->options[$optionId] = $option; + } + + if(!empty($this->optionValues[$optionValueId])) { + $optionValue = $this->optionValues[$optionValueId]; + } else { + $optionValue = $this->model_catalog_option->getOptionValue($optionValueId); + $this->optionValues[$optionValueId] = $optionValue; + } + + return array( + 'optionName' => $option['name'], + 'optionValue' => $optionValue['name'] + ); + } } diff --git a/admin/model/retailcrm/order.php b/admin/model/retailcrm/order.php index 8f40478..628584c 100644 --- a/admin/model/retailcrm/order.php +++ b/admin/model/retailcrm/order.php @@ -77,10 +77,30 @@ class ModelRetailcrmOrder extends Model { ); $orderProducts = isset($order_data['products']) ? $order_data['products'] : $order_data['order_product']; + $offerOptions = array('select', 'radio'); foreach ($orderProducts as $product) { + $offerId = ''; + if(!empty($product['option'])) { + $options = array(); + + foreach($product['option'] as $option) { + if(!in_array($option['type'], $offerOptions)) continue; + $options[$option['product_option_id']] = $option['option_value_id']; + } + + ksort($options); + + $offerId = array(); + foreach($options as $optionKey => $optionValue) { + $offerId[] = $optionKey.'-'.$optionValue; + } + $offerId = implode('_', $offerId); + } + + $order['items'][] = array( - 'productId' => $product['product_id'], + 'productId' => !empty($offerId) ? $product['product_id'].'#'.$offerId : $product['product_id'], 'productName' => $product['name'], 'initialPrice' => $product['price'], 'quantity' => $product['quantity'], @@ -95,6 +115,10 @@ class ModelRetailcrmOrder extends Model { } } + /** + * @param $order_data + * @param $order_id + */ public function changeInCrm($order_data, $order_id) { $this->load->model('setting/setting'); @@ -154,16 +178,46 @@ class ModelRetailcrmOrder extends Model { ); $orderProducts = isset($order_data['products']) ? $order_data['products'] : $order_data['order_product']; + $offerOptions = array('select', 'radio'); foreach ($orderProducts as $product) { + if(!empty($product['order_option'])) { + $options = array(); + + $productOptions = $this->model_catalog_product->getProductOptions($product['product_id']); + foreach($productOptions as $key=>$productOption) { + $productOptionValues[$productOption['product_option_id']] = array(); + + foreach($productOption['product_option_value'] as $productOptionValue) { + $productOptionValues[$productOption['product_option_id']][$productOptionValue['product_option_value_id']] = $productOptionValue['option_value_id']; + } + } + + foreach($product['order_option'] as $option) { + if(!in_array($option['type'], $offerOptions)) continue; + $options[$option['product_option_id']] = $productOptionValues[$option['product_option_id']][$option['product_option_value_id']]; + } + + ksort($options); + + $offerId = array(); + foreach($options as $optionKey => $optionValue) { + $offerId[] = $optionKey.'-'.$optionValue; + } + $offerId = implode('_', $offerId); + } + $order['items'][] = array( - 'productId' => $product['product_id'], + 'productId' => !empty($offerId) ? $product['product_id'].'#'.$offerId : $product['product_id'], 'productName' => $product['name'], 'initialPrice' => $product['price'], 'quantity' => $product['quantity'], ); } + // Сделаем порядок товраных позиций в срм как и в магазине + $order['items'] = array_reverse($order['items']); + if (isset($order_data['order_status_id']) && $order_data['order_status_id'] > 0) { $order['status'] = $settings['retailcrm_status'][$order_data['order_status_id']]; } diff --git a/catalog/model/retailcrm/order.php b/catalog/model/retailcrm/order.php index e3fed74..d37acae 100644 --- a/catalog/model/retailcrm/order.php +++ b/catalog/model/retailcrm/order.php @@ -29,8 +29,10 @@ class ModelRetailcrmOrder extends Model { 100 ); - foreach($customers['customers'] as $customer) { - $order['customer']['id'] = $customer['id']; + if($customers) { + foreach ($customers['customers'] as $customer) { + $order['customer']['id'] = $customer['id']; + } } unset($customers); @@ -59,14 +61,12 @@ class ModelRetailcrmOrder extends Model { $payment_code = $order_data['payment_code']; $order['paymentType'] = $settings['retailcrm_payment'][$payment_code]; - // Совместимость с 1.5.5, когда этот метод вызывается из model/checkout/order->createOrder(), а не из controller/module/retailcrm->order_create() if(!isset($order_data['shipping_iso_code_2']) && isset($order_data['shipping_country_id'])) { $this->load->model('localisation/country'); $shipping_country = $this->model_localisation_country->getCountry($order_data['shipping_country_id']); $order_data['shipping_iso_code_2'] = $shipping_country['iso_code_2']; } - $delivery_code = $order_data['shipping_code']; $order['delivery'] = array( 'code' => !empty($delivery_code) ? $settings['retailcrm_delivery'][$delivery_code] : '', @@ -87,10 +87,30 @@ class ModelRetailcrmOrder extends Model { ); $orderProducts = isset($order_data['products']) ? $order_data['products'] : $order_data['order_product']; + $offerOptions = array('select', 'radio'); foreach ($orderProducts as $product) { + $offerId = ''; + if(!empty($product['option'])) { + $options = array(); + + foreach($product['option'] as $option) { + if(!in_array($option['type'], $offerOptions)) continue; + $options[$option['product_option_id']] = $option['option_value_id']; + } + + ksort($options); + + $offerId = array(); + foreach($options as $optionKey => $optionValue) { + $offerId[] = $optionKey.'-'.$optionValue; + } + $offerId = implode('_', $offerId); + } + + $order['items'][] = array( - 'productId' => $product['product_id'], + 'productId' => !empty($offerId) ? $product['product_id'].'#'.$offerId : $product['product_id'], 'productName' => $product['name'], 'initialPrice' => $product['price'], 'quantity' => $product['quantity'], @@ -166,10 +186,30 @@ class ModelRetailcrmOrder extends Model { ); $orderProducts = isset($order_data['products']) ? $order_data['products'] : $order_data['order_product']; + $offerOptions = array('select', 'radio'); foreach ($orderProducts as $product) { + $offerId = ''; + if(!empty($product['option'])) { + $options = array(); + + foreach($product['option'] as $option) { + if(!in_array($option['type'], $offerOptions)) continue; + $options[$option['product_option_id']] = $option['option_value_id']; + } + + ksort($options); + + $offerId = array(); + foreach($options as $optionKey => $optionValue) { + $offerId[] = $optionKey.'-'.$optionValue; + } + $offerId = implode('_', $offerId); + } + + $order['items'][] = array( - 'productId' => $product['product_id'], + 'productId' => !empty($offerId) ? $product['product_id'].'#'.$offerId : $product['product_id'], 'productName' => $product['name'], 'initialPrice' => $product['price'], 'quantity' => $product['quantity'],