From 3767445f6871cfe5adc79ed9736d74069796ec9a Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Fri, 31 Aug 2018 12:22:20 +0300 Subject: [PATCH 1/4] Replased SQL requests on object model --- retailcrm/lib/RetailcrmHistory.php | 310 ++++++++++------------- retailcrm/lib/RetailcrmHistoryHelper.php | 10 +- 2 files changed, 142 insertions(+), 178 deletions(-) diff --git a/retailcrm/lib/RetailcrmHistory.php b/retailcrm/lib/RetailcrmHistory.php index 4b640fa..8eaf410 100644 --- a/retailcrm/lib/RetailcrmHistory.php +++ b/retailcrm/lib/RetailcrmHistory.php @@ -8,8 +8,8 @@ class RetailcrmHistory /** * Get customers history - * - * @return string + * + * @return mixed */ public static function customersHistory() { @@ -122,6 +122,8 @@ class RetailcrmHistory * Update last sync id */ Configuration::updateValue('RETAILCRM_LAST_CUSTOMERS_SYNC', $sinceid); + + return true; } else { return 'Nothing to sync'; } @@ -129,8 +131,8 @@ class RetailcrmHistory /** * Get orders history - * - * @return string + * + * @return mixed */ public static function ordersHistory() { @@ -193,14 +195,14 @@ class RetailcrmHistory $payment = $paymentCRM['type']; } elseif (isset($order['payments']) && count($order['payments']) > 1) { foreach ($order['payments'] as $paymentCRM) { - if ($payment['status'] != 'paid') { + if ($paymentCRM['status'] != 'paid') { $payment = $paymentCRM['type']; } } } } - if (array_key_exists($payment, $payments) && $payments[$payment] != '') { + if (array_key_exists($payment, $payments) && !empty($payments[$payment])) { if (Module::getInstanceByName($payments[$payment])) { $paymentType = Module::getModuleName($payments[$payment]); } else { @@ -236,7 +238,7 @@ class RetailcrmHistory } } - if (!$deliveryType) { + if (!isset($deliveryType) || !$deliveryType) { if ($deliveryDefault) { $deliveryType = $deliveryDefault; } else { @@ -384,19 +386,29 @@ class RetailcrmHistory ); $combinationPrice = Combination::getPrice($product_attribute_id); - $productPrice = $combinationPrice > 0 ? $product->getPrice()+ $combinationPrice : $product->getPrice(); + $productPrice = $combinationPrice > 0 ? $product->getPrice() + $combinationPrice : $product->getPrice(); } else { $productName = htmlspecialchars(strip_tags($product->name)); $productPrice = $product->getPrice(); } $product_list[] = array( - 'product' => $product, - 'product_attribute_id' => $product_attribute_id, - 'product_price' => $product->price, - 'product_price_inc_tax' => $productPrice, - 'product_name' => $productName, - 'quantity' => $item['quantity'] + 'id_product' => $product->id, + 'id_product_attribute' => $product_attribute_id, + 'price' => $product->price, + 'price_wt' => $productPrice, + 'name' => $productName, + 'cart_quantity' => $item['quantity'], + 'weight' => 0, + 'weight_attribute' => 0, + 'stock_quantity' => $item['quantity'], + 'ecotax' => 0, + 'id_shop' => Context::getContext()->shop->id, + 'additional_shipping_cost' => 0, + 'total_wt' => $productPrice * $item['quantity'], + 'total' => $productPrice * $item['quantity'], + 'wholesale_price' => $product->wholesale_price, + 'id_supplier' => $product->id_supplier ); if (isset($item['discountTotal']) && self::$apiVersion == 5) { @@ -407,8 +419,11 @@ class RetailcrmHistory $newOrder->add(false, false); if (isset($order['payments']) && !empty($order['payments'])) { - foreach ($order['payments'] as $pay) { - if (!isset($pay['externalId']) && $pay['status'] == 'paid') { + foreach ($order['payments'] as $payment) { + if (!isset($payment['externalId']) + && isset($payment['status']) + && $payment['status'] == 'paid' + ) { $ptype = $payment['type']; $ptypes = $references->getSystemPaymentModules(); if ($payments[$ptype] != null) { @@ -417,14 +432,13 @@ class RetailcrmHistory $payType = $pay['name']; } } - $paymentType = Module::getModuleName($payments[$ptype]); - Db::getInstance()->execute('INSERT INTO `' . _DB_PREFIX_ . 'order_payment` - (`payment_method`, `order_reference` , `amount`, `date_add`) - VALUES - (\'' . $payType . '\', - \'' . $newOrder->reference . '\', - \'' . $payment['amount'] . '\', - \'' . $payment['paidAt'] . '\')'); + + $orderPayment = new OrderPayment(); + $orderPayment->payment_method = $payType; + $orderPayment->order_reference = $newOrder->reference; + $orderPayment->amount = $payment['amount']; + $orderPayment->date_add = $payment['paidAt']; + $orderPayment->save(); } } } @@ -446,38 +460,9 @@ class RetailcrmHistory /* * Create order details */ - $query = 'INSERT `'._DB_PREFIX_.'order_detail` - ( - `id_order`, `id_order_invoice`, `id_shop`, `product_id`, `product_attribute_id`, - `product_name`, `product_quantity`, `product_quantity_in_stock`, `product_price`, - `product_reference`, `total_price_tax_excl`, `total_price_tax_incl`, - `unit_price_tax_excl`, `unit_price_tax_incl`, `original_product_price` - ) - - VALUES'; - - $context = new Context(); - foreach ($product_list as $product) { - $query .= '(' - .(int) $newOrder->id.', - 0, - '. Context::getContext()->shop->id.', - '.(int) $product['product']->id.', - '.$product['product_attribute_id'].', - '.implode('', array('\'', $product['product_name'], '\'')).', - '.(int) $product['quantity'].', - '.(int) $product['quantity'].', - '.$product['product_price'].', - '.implode('', array('\'', $product['product']->reference, '\'')).', - '.$product['product_price'].', - '.$product['product_price_inc_tax'].', - '.$product['product_price'].', - '.$product['product_price_inc_tax'].', - '.$product['product_price'].' - ),'; - } - - Db::getInstance()->execute(rtrim($query, ',')); + $orderDetail = new OrderDetail(); + $orderDetail->createList($newOrder, $cart, $newOrder->current_state, $product_list); + $orderDetail->save(); if (!empty($customerFix)) { self::$api->customersFixExternalIds($customerFix); @@ -496,55 +481,39 @@ class RetailcrmHistory $dtype = $order['delivery']['code']; $dcost = !empty($order['delivery']['cost']) ? $order['delivery']['cost'] : null; - if ($deliveries[$dtype] != null) { + if (isset($deliveries[$dtype]) && $deliveries[$dtype] != null) { if ($deliveries[$dtype] != $orderToUpdate->id_carrier or $dcost != null) { if ($dtype != null) { - Db::getInstance()->execute('UPDATE `' . _DB_PREFIX_ . 'orders` - SET - `id_carrier` = \'' . $deliveries[$dtype] . '\' - WHERE - `id_order` = ' . (int)$order['externalId']); + $orderCarrier = new OrderCarrier($orderToUpdate->id_order_carrier); + $orderCarrier->id_carrier = $deliveries[$dtype]; } - $updateCarrierFields = array(); if ($dtype != null) { - $updateCarrierFields[] = '`id_carrier` = \'' . $deliveries[$dtype] . '\' '; + $orderCarrier->id_carrier = $deliveries[$dtype]; } if ($dcost != null) { - $updateCarrierFields[] = '`shipping_cost_tax_incl` = \'' . $dcost . '\' '; - $updateCarrierFields[] = '`shipping_cost_tax_excl` = \'' . $dcost . '\' '; + $orderCarrier->shipping_cost_tax_incl = $dcost; + $orderCarrier->shipping_cost_tax_excl = $dcost; } - $updateCarrierFields = implode(', ', $updateCarrierFields); - Db::getInstance()->execute('UPDATE `' . _DB_PREFIX_ . 'order_carrier` - SET - '.$updateCarrierFields.' - WHERE - `id_order` = \'' . $orderToUpdate->id . '\''); + $orderCarrier->update(); } } } - /* + /** * check payment type */ - if (!empty($order['paymentType']) && self::$apiVersion != 5) { $ptype = $order['paymentType']; if ($payments[$ptype] != null) { $paymentType = Module::getModuleName($payments[$ptype]); if ($payments[$ptype] != $orderToUpdate->payment) { - Db::getInstance()->execute('UPDATE `' . _DB_PREFIX_ . 'orders` - SET - `payment` = \'' . ($paymentType != null ? $paymentType : $payments[$ptype]). '\' - WHERE - `id_order` = ' . (int)$order['externalId']); - Db::getInstance()->execute('UPDATE `' . _DB_PREFIX_ . 'order_payment` - SET - `payment_method` = \'' . $payments[$ptype] . '\' - WHERE - `order_reference` = \'' . $orderToUpdate->reference . '\''); + $orderToUpdate->payment = $paymentType != null ? $paymentType : $payments[$ptype]; + $orderPayment = OrderPayment::getByOrderId($orderToUpdate->id); + $orderPayment->payment_method = $payments[$ptype]; + $orderPayment->update(); } } } elseif (!empty($order['payments']) && self::$apiVersion == 5) { @@ -562,19 +531,13 @@ class RetailcrmHistory } } $paymentType = Module::getModuleName($payments[$ptype]); - Db::getInstance()->execute('UPDATE `' . _DB_PREFIX_ . 'orders` - SET - `payment` = \'' . ($paymentType != null ? $paymentType : $payments[$ptype]). '\' - WHERE - `id_order` = ' . (int)$order['externalId']); - - Db::getInstance()->execute('INSERT INTO `' . _DB_PREFIX_ . 'order_payment` - (`payment_method`, `order_reference` , `amount`, `date_add`) - VALUES - (\'' . $payType . '\', - \'' . $orderToUpdate->reference . '\', - \'' . $payment['amount'] . '\', - \'' . $payment['paidAt'] . '\')'); + $orderToUpdate->payment = $paymentType != null ? $paymentType : $payments[$ptype]; + $orderPayment = new OrderPayment(); + $orderPayment->payment_method = $payType; + $orderPayment->order_reference = $orderToUpdate->reference; + $orderPayment->amount = $payment['amount']; + $orderPayment->date_add = $payment['paidAt']; + $orderPayment->save(); } } } @@ -594,11 +557,7 @@ class RetailcrmHistory $product_attribute_id = 0; } - Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'order_detail` - WHERE - `id_order` = '. $orderToUpdate->id .' - AND - `product_id` = '.$product_id. ' AND `product_attribute_id` = '.$product_attribute_id); + self::deleteOrderDetailByProduct($orderToUpdate->id, $product_id, $product_attribute_id); unset($order['items'][$key]); $ItemDiscount = true; @@ -647,22 +606,16 @@ class RetailcrmHistory $productPrice = round($productPrice, 2); - Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'order_detail` - SET - `unit_price_tax_incl` = '.$productPrice.' - WHERE - `id_order_detail` = '.$orderItem['id_order_detail']); + $orderDetail = new OrderDetail($orderItem['id_order_detail']); + $orderDetail->unit_price_tax_incl = $productPrice; // quantity if (isset($item['quantity']) && $item['quantity'] != $orderItem['product_quantity']) { - Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'order_detail` - SET - `product_quantity` = '.$item['quantity'].', - `product_quantity_in_stock` = '.$item['quantity'].' - WHERE - `id_order_detail` = '.$orderItem['id_order_detail']); + $orderDetail->product_quantity = $item['quantity']; + $orderDetail->product_quantity_in_stock = $item['quantity']; } + $orderDetail->update(); $ItemDiscount = true; unset($order['items'][$key]); } @@ -673,16 +626,6 @@ class RetailcrmHistory * Check new items */ if (!empty($order['items'])) { - $query = 'INSERT `'._DB_PREFIX_.'order_detail` - ( - `id_order`, `id_order_invoice`, `id_shop`, `product_id`, `product_attribute_id`, - `product_name`, `product_quantity`, `product_quantity_in_stock`, `product_price`, - `product_reference`, `total_price_tax_excl`, `total_price_tax_incl`, - `unit_price_tax_excl`, `unit_price_tax_incl`, `original_product_price` - ) - - VALUES'; - foreach ($order['items'] as $key => $newItem) { $product_id = $newItem['offer']['externalId']; $product_attribute_id = 0; @@ -708,46 +651,49 @@ class RetailcrmHistory } // discount - if ($newItem['discount'] || $newItem['discountPercent']|| $newItem['discountTotal']) { + if ((isset($newItem['discount']) && $newItem['discount']) + || (isset($newItem['discountPercent']) && $newItem['discountPercent']) + || (isset($newItem['discountTotal']) && $newItem['discountTotal']) + ) { $productPrice = $productPrice - $newItem['discount']; $productPrice = $productPrice - $newItem['discountTotal']; $productPrice = $productPrice - ($prodPrice / 100 * $newItem['discountPercent']); $ItemDiscount = true; } - $query .= '(' - .(int) $orderToUpdate->id.', - 0, - '. Context::getContext()->shop->id.', - '.(int) $product_id.', - '.(int) $product_attribute_id.', - '.implode('', array('\'', $productName, '\'')).', - '.(int) $newItem['quantity'].', - '.(int) $newItem['quantity'].', - '.$productPrice.', - '.implode('', array('\'', $product->reference, '\'')).', - '.$productPrice * $newItem['quantity'].', - '.($productPrice + $productPrice / 100 * $tax->rate) * $newItem['quantity'].', - '.$productPrice.', - '.($productPrice + $productPrice / 100 * $tax->rate).', - '.$productPrice.' - ),'; + $orderDetail = new OrderDetail(); + $orderDetail->id_order = $orderToUpdate->id; + $orderDetail->id_order_invoice = $orderToUpdate->invoice_number; + $orderDetail->id_shop = Context::getContext()->shop->id; + $orderDetail->product_id = (int) $product_id; + $orderDetail->product_attribute_id = (int) $product_attribute_id; + $orderDetail->product_name = implode('', array('\'', $productName, '\'')); + $orderDetail->product_quantity = (int) $newItem['quantity']; + $orderDetail->product_quantity_in_stock = (int) $newItem['quantity']; + $orderDetail->product_price = $productPrice; + $orderDetail->product_reference = implode('', array('\'', $product->reference, '\'')); + $orderDetail->total_price_tax_excl = $productPrice * $newItem['quantity']; + $orderDetail->total_price_tax_incl = ($productPrice + $productPrice / 100 * $tax->rate) * $newItem['quantity']; + $orderDetail->unit_price_tax_excl = $productPrice; + $orderDetail->unit_price_tax_incl = ($productPrice + $productPrice / 100 * $tax->rate); + $orderDetail->original_product_price = $productPrice; + $orderDetail->save(); + unset($orderDetail); unset($order['items'][$key]); } - - Db::getInstance()->execute(rtrim($query, ',')); } /* * Fix prices & discounts * Discounts only for whole order */ - if (isset($order['discount']) || - isset($order['discountPercent']) || - isset($order['delivery']['cost']) || - isset($order['discountTotal']) || - $ItemDiscount) { + if (isset($order['discount']) + || isset($order['discountPercent']) + || isset($order['delivery']['cost']) + || isset($order['discountTotal']) + || $ItemDiscount + ) { $infoOrd = self::$api->ordersGet($order['externalId']); $infoOrder = $infoOrd->order; $orderTotalProducts = $infoOrder['summ']; @@ -760,21 +706,18 @@ class RetailcrmHistory $order_cart_rule = new OrderCartRule($valCartRules['id_order_cart_rule']); $order_cart_rule->delete(); } - $orderToUpdate->update(); - Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'orders` - SET - `total_discounts` = '.$totalDiscount.', - `total_discounts_tax_incl` = '.$totalDiscount.', - `total_discounts_tax_excl` = '.$totalDiscount.', - `total_shipping` = '.$deliveryCost.', - `total_shipping_tax_incl` = '.$deliveryCost.', - `total_shipping_tax_excl` = '.$deliveryCost.', - `total_paid` = '.$totalPaid.', - `total_paid_tax_incl` = '.$totalPaid.', - `total_paid_tax_excl` = '.$totalPaid.', - `total_products_wt` = '.$orderTotalProducts.' - WHERE `id_order` = '.(int) $order['externalId']); + $orderToUpdate->total_discounts = $totalDiscount; + $orderToUpdate->total_discounts_tax_incl = $totalDiscount; + $orderToUpdate->total_discounts_tax_excl = $totalDiscount; + $orderToUpdate->total_shipping = $deliveryCost; + $orderToUpdate->total_shipping_tax_incl = $deliveryCost; + $orderToUpdate->total_shipping_tax_excl = $deliveryCost; + $orderToUpdate->total_paid = $totalPaid; + $orderToUpdate->total_paid_tax_incl = $totalPaid; + $orderToUpdate->total_paid_tax_excl = $totalPaid; + $orderToUpdate->total_products_wt = $orderTotalProducts; + $orderToUpdate->update(); unset($ItemDiscount); } @@ -787,29 +730,48 @@ class RetailcrmHistory if ($statuses[$stype] != null) { if ($statuses[$stype] != $orderToUpdate->current_state) { - Db::getInstance()->execute('INSERT INTO `' . _DB_PREFIX_ . 'order_history` - (`id_employee`, `id_order`, `id_order_state`, `date_add`) - VALUES - (0, ' . $orderToUpdate->id . ', - ' . $statuses[$stype] . ', - "' . date('Y-m-d H:i:s') . '")'); + $orderHistory = new OrderHistory(); + $orderHistory->id_employee = 0; + $orderHistory->id_order = $orderToUpdate->id; + $orderHistory->id_order_state = $statuses[$stype]; + $orderHistory->date_add = date('Y-m-d H:i:s'); + $orderHistory->save(); - Db::getInstance()->execute('UPDATE `' . _DB_PREFIX_ . 'orders` - SET - `current_state` = \'' . $statuses[$stype] . '\' - WHERE - `id_order` = ' . (int)$order['externalId']); + $orderToUpdate->current_state = $statuses[$stype]; + $orderToUpdate->update(); } } } } } + /* * Update last sync timestamp */ Configuration::updateValue('RETAILCRM_LAST_ORDERS_SYNC', $sinceId); + + return true; } else { return 'Nothing to sync'; } } -} \ No newline at end of file + + /** + * Delete order product from order by product + * + * @param $order_id + * @param $product_id + * @param $product_attribute_id + * + * @return void + */ + private static function deleteOrderDetailByProduct($order_id, $product_id, $product_attribute_id) + { + Db::getInstance()->execute(' + DELETE FROM ' . _DB_PREFIX_ . 'order_detail + WHERE id_order = ' . $order_id . ' + AND product_id = ' . $product_id . ' + AND product_attribute_id = ' . $product_attribute_id + ); + } +} diff --git a/retailcrm/lib/RetailcrmHistoryHelper.php b/retailcrm/lib/RetailcrmHistoryHelper.php index b31a958..c0dd89e 100644 --- a/retailcrm/lib/RetailcrmHistoryHelper.php +++ b/retailcrm/lib/RetailcrmHistoryHelper.php @@ -33,10 +33,10 @@ class RetailcrmHistoryHelper { } else { $orders[$change['order']['id']] = $change['order']; } - + if (isset($change['payment'])) { if (isset($orders[$change['order']['id']]['payments'][$change['payment']['id']])) { - $orders[$change['order']['id']]['payments'][$change['payment']['id']] = array_merge($orders[$change['order']['id']]['payments'][$change['payment']['id']], $change['payment']); + $orders[$change['order']['id']]['payments'][$change['payment']['id']] = array_merge($orders[$change['order']['id']]['payments'][$change['payment']['id']], $change['payment']); } else { $orders[$change['order']['id']]['payments'][$change['payment']['id']] = $change['payment']; } @@ -50,7 +50,7 @@ class RetailcrmHistoryHelper { $orders[$change['order']['id']]['payments'][$change['payment']['id']][$fields['payment'][$change['field']]] = $change['newValue']; } } - + if (isset($change['item'])) { if (isset($orders[$change['order']['id']]['items'][$change['item']['id']])) { $orders[$change['order']['id']]['items'][$change['item']['id']] = array_merge($orders[$change['order']['id']]['items'][$change['item']['id']], $change['item']); @@ -64,7 +64,9 @@ class RetailcrmHistoryHelper { if (empty($change['newValue']) && $change['field'] == 'order_product') { $orders[$change['order']['id']]['items'][$change['item']['id']]['delete'] = true; } - if (!$orders[$change['order']['id']]['items'][$change['item']['id']]['create'] && $fields['item'][$change['field']]) { + if (!isset($orders[$change['order']['id']]['items'][$change['item']['id']]['create']) + && isset($fields['item'][$change['field']]) && $fields['item'][$change['field']] + ) { $orders[$change['order']['id']]['items'][$change['item']['id']][$fields['item'][$change['field']]] = $change['newValue']; } } else { From 12b133eaff9f7fef8cde5b016f118e7e83a61601 Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Mon, 3 Sep 2018 16:15:30 +0300 Subject: [PATCH 2/4] Send module configuration in marketplace CRM --- retailcrm/lib/RetailcrmApiClientV4.php | 28 +++++++++ retailcrm/lib/RetailcrmApiClientV5.php | 34 ++++++++++- retailcrm/retailcrm.php | 83 ++++++++++++++++++++++++-- 3 files changed, 138 insertions(+), 7 deletions(-) diff --git a/retailcrm/lib/RetailcrmApiClientV4.php b/retailcrm/lib/RetailcrmApiClientV4.php index 8009e97..c2f2b42 100644 --- a/retailcrm/lib/RetailcrmApiClientV4.php +++ b/retailcrm/lib/RetailcrmApiClientV4.php @@ -1650,6 +1650,34 @@ class RetailcrmApiClientV4 ); } + /** + * Edit marketplace configuration + * + * @param array $configuration + * + * @throws \RetailCrm\Exception\InvalidJsonException + * @throws \RetailCrm\Exception\CurlException + * @throws \InvalidArgumentException + * + * @return ApiResponse + */ + public function marketplaceSettingsEdit(array $configuration) + { + if (!count($configuration) || empty($configuration['code'])) { + throw new \InvalidArgumentException( + 'Parameter `configuration` must contains a data & configuration `code` must be set' + ); + } + + $code = $configuration['code']; + + return $this->client->makeRequest( + "/marketplace/external/setting/$code/edit", + RetailcrmHttpClient::METHOD_POST, + array('configuration' => json_encode($configuration)) + ); + } + /** * Return current site * diff --git a/retailcrm/lib/RetailcrmApiClientV5.php b/retailcrm/lib/RetailcrmApiClientV5.php index 12c114c..4d842f6 100644 --- a/retailcrm/lib/RetailcrmApiClientV5.php +++ b/retailcrm/lib/RetailcrmApiClientV5.php @@ -178,7 +178,7 @@ class RetailcrmApiClientV5 $parameters ); } - + /** * Create custom field * @@ -211,7 +211,7 @@ class RetailcrmApiClientV5 array('customField' => json_encode($customField)) ); } - + /** * Edit custom field * @@ -718,7 +718,7 @@ class RetailcrmApiClientV5 'Parameter `id` must be set' ); } - + return $this->client->makeRequest( sprintf('/orders/payments/%s/delete', $id), RetailcrmHttpClient::METHOD_POST @@ -2292,6 +2292,34 @@ class RetailcrmApiClientV5 ); } + /** + * Edit module configuration + * + * @param array $configuration + * + * @throws \RetailCrm\Exception\InvalidJsonException + * @throws \RetailCrm\Exception\CurlException + * @throws \InvalidArgumentException + * + * @return ApiResponse + */ + public function integrationModulesEdit(array $configuration) + { + if (!count($configuration) || empty($configuration['code'])) { + throw new \InvalidArgumentException( + 'Parameter `configuration` must contains a data & configuration `code` must be set' + ); + } + + $code = $configuration['code']; + + return $this->client->makeRequest( + "/integration-modules/$code/edit", + RetailcrmHttpClient::METHOD_POST, + array('integrationModule' => json_encode($configuration)) + ); + } + /** * Return current site * diff --git a/retailcrm/retailcrm.php b/retailcrm/retailcrm.php index 3e2ec69..79ed058 100644 --- a/retailcrm/retailcrm.php +++ b/retailcrm/retailcrm.php @@ -82,6 +82,16 @@ class RetailCRM extends Module public function uninstall() { + $api = new RetailcrmProxy( + Configuration::get('RETAILCRM_ADDRESS'), + Configuration::get('RETAILCRM_API_TOKEN'), + _PS_ROOT_DIR_ . '/retailcrm.log', + Configuration::get('RETAILCRM_API_VERSION') + ); + + $clientId = Configuration::get('RETAILCRM_CLIENT_ID'); + $this->integrationModule($api, $clientId, Configuration::get('RETAILCRM_API_VERSION'), false); + return parent::uninstall() && Configuration::deleteByName('RETAILCRM_ADDRESS') && Configuration::deleteByName('RETAILCRM_API_TOKEN') && @@ -110,14 +120,16 @@ class RetailCRM extends Module $deliveryDefault = json_encode(Tools::getValue('RETAILCRM_API_DELIVERY_DEFAULT')); $paymentDefault = json_encode(Tools::getValue('RETAILCRM_API_PAYMENT_DEFAULT')); $statusExport = (string)(Tools::getValue('RETAILCRM_STATUS_EXPORT')); + $clientId = Configuration::get('RETAILCRM_CLIENT_ID'); $settings = array( 'address' => $address, 'token' => $token, - 'version' => $version + 'version' => $version, + 'clientId' => $clientId ); $output .= $this->validateForm($settings, $output); - + if ($output === '') { Configuration::updateValue('RETAILCRM_ADDRESS', $address); Configuration::updateValue('RETAILCRM_API_TOKEN', $token); @@ -437,7 +449,7 @@ class RetailCRM extends Module if ($comment !== false) { $order['customerComment'] = $comment; } - + unset($comment); foreach ($orderdb->getProducts() as $item) { @@ -612,7 +624,7 @@ class RetailCRM extends Module } $order['customer']['externalId'] = $customer['externalId']; - + $this->api->ordersCreate($order); return $order; @@ -715,9 +727,19 @@ class RetailCRM extends Module _PS_ROOT_DIR_ . '/retailcrm.log', $settings['version'] ); + $response = $api->deliveryTypesList(); if ($response !== false) { + if (!$settings['clientId']) { + $clientId = hash('md5', date('Y-m-d H:i:s')); + $result = $this->integrationModule($api, $clientId, $settings['version']); + + if ($result) { + Configuration::updateValue('RETAILCRM_CLIENT_ID', $clientId); + } + } + return true; } @@ -736,4 +758,57 @@ class RetailCRM extends Module return $output; } + + /** + * Activate/deactivate module in marketplace retailCRM + * + * @param \RetailcrmProxy $apiClient + * @param string $clientId + * @param string $apiVersion + * @param boolean $active + * + * @return boolean + */ + private function integrationModule($apiClient, $clientId, $apiVersion, $active = true) + { + $scheme = isset($_SERVER['HTTPS']) ? 'https://' : 'http://'; + $logo = 'https://s3.eu-central-1.amazonaws.com/retailcrm-billing/images/5b845ce986911-prestashop2.svg'; + $code = 'prestashop'; + $name = 'PrestaShop'; + $accountUrl = $scheme . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; + + if ($apiVersion == 'v4') { + $configuration = array( + 'name' => $name, + 'code' => $code, + 'logo' => $logo, + 'configurationUrl' => $accountUrl, + 'active' => $active + ); + + $response = $apiClient->marketplaceSettingsEdit($configuration); + } else { + $configuration = array( + 'clientId' => $clientId, + 'code' => $code, + 'integrationCode' => $code, + 'active' => $active, + 'name' => $name, + 'logo' => $logo, + 'accountUrl' => $accountUrl + ); + + $response = $apiClient->integrationModulesEdit($configuration); + } + + if (!$response) { + return false; + } + + if ($response->isSuccessful()) { + return true; + } + + return false; + } } From 83360e4c6929c2861932d294e7e21fb7e21245f6 Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Wed, 24 Oct 2018 16:35:07 +0300 Subject: [PATCH 3/4] Fix for marketplace --- retailcrm/retailcrm.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/retailcrm/retailcrm.php b/retailcrm/retailcrm.php index 79ed058..038b84b 100644 --- a/retailcrm/retailcrm.php +++ b/retailcrm/retailcrm.php @@ -732,7 +732,7 @@ class RetailCRM extends Module if ($response !== false) { if (!$settings['clientId']) { - $clientId = hash('md5', date('Y-m-d H:i:s')); + $clientId = uniqid(); $result = $this->integrationModule($api, $clientId, $settings['version']); if ($result) { @@ -773,14 +773,14 @@ class RetailCRM extends Module { $scheme = isset($_SERVER['HTTPS']) ? 'https://' : 'http://'; $logo = 'https://s3.eu-central-1.amazonaws.com/retailcrm-billing/images/5b845ce986911-prestashop2.svg'; - $code = 'prestashop'; + $integrationCode = 'prestashop'; $name = 'PrestaShop'; $accountUrl = $scheme . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; if ($apiVersion == 'v4') { $configuration = array( 'name' => $name, - 'code' => $code, + 'code' => $integrationCode . '-' . $clientId, 'logo' => $logo, 'configurationUrl' => $accountUrl, 'active' => $active @@ -790,8 +790,8 @@ class RetailCRM extends Module } else { $configuration = array( 'clientId' => $clientId, - 'code' => $code, - 'integrationCode' => $code, + 'code' => $integrationCode . '-' . $clientId, + 'integrationCode' => $integrationCode, 'active' => $active, 'name' => $name, 'logo' => $logo, From f61d14d97dabd64d9ebb6301f41f2a2629da8872 Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Thu, 25 Oct 2018 12:20:52 +0300 Subject: [PATCH 4/4] v2.2.6 --- CHANGELOG.md | 3 +++ VERSION | 2 +- retailcrm/retailcrm.php | 4 ++-- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9105c58..978528e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## v.2.2.6 +* Добавлена активация модуля в маркетплейсе retailCRM + ## v.2.2.5 * Добавлена передача страны при создании заказа для пользователя и заказа * Добавлен метод сохранения сущностей с обработкой исключений diff --git a/VERSION b/VERSION index 21bb5e1..bda8fbe 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.2.5 +2.2.6 diff --git a/retailcrm/retailcrm.php b/retailcrm/retailcrm.php index f033aac..261707c 100644 --- a/retailcrm/retailcrm.php +++ b/retailcrm/retailcrm.php @@ -3,7 +3,7 @@ * @author Retail Driver LCC * @copyright RetailCRM * @license GPL - * @version 2.2.4 + * @version 2.2.6 * @link https://retailcrm.ru * */ @@ -38,7 +38,7 @@ class RetailCRM extends Module { $this->name = 'retailcrm'; $this->tab = 'export'; - $this->version = '2.2.4'; + $this->version = '2.2.6'; $this->author = 'Retail Driver LCC'; $this->displayName = $this->l('RetailCRM'); $this->description = $this->l('Integration module for RetailCRM');