diff --git a/Makefile b/Makefile index 911df7c..24d104c 100644 --- a/Makefile +++ b/Makefile @@ -6,9 +6,12 @@ all: build_archive send_to_ftp delete_archive build_archive: zip -r $(ARCHIVE_NAME) ./retailcrm/* + zip -r /tmp/retailcrm.zip ./retailcrm/* send_to_ftp: curl -T $(ARCHIVE_NAME) -u $(FTP_USER):$(FTP_PASSWORD) ftp://$(FTP_HOST) + curl -T /tmp/retailcrm.zip -u $(FTP_USER):$(FTP_PASSWORD) ftp://$(FTP_HOST) delete_archive: rm -f $(ARCHIVE_NAME) + rm -f /tmp/retailcrm.zip diff --git a/retailcrm/lib/RetailcrmCatalog.php b/retailcrm/lib/RetailcrmCatalog.php index 691bd1b..15b9bbd 100644 --- a/retailcrm/lib/RetailcrmCatalog.php +++ b/retailcrm/lib/RetailcrmCatalog.php @@ -109,9 +109,9 @@ class RetailcrmCatalog } else { $size = null; } - - $productForCombination = new Product($product['id_product']); - + + $productForCombination = new Product($product['id_product']); + $offers = Product::getProductAttributesIds($product['id_product']); if(!empty($offers)) { @@ -119,7 +119,7 @@ class RetailcrmCatalog $combinations = $productForCombination->getAttributeCombinationsById($offer['id_product_attribute' ], $id_lang); if (!empty($combinations)) { - + foreach ($combinations as $combination) { $arSet = array( 'group_name' => $combination['group_name'], @@ -168,7 +168,7 @@ class RetailcrmCatalog $item[mb_strtolower($itemComb['group_name'])] = htmlspecialchars($itemComb['attribute']); } } - + $items[] = $item; } } else { @@ -205,10 +205,8 @@ class RetailcrmCatalog $items[] = $item; } - } return array($categories, $items); } - } diff --git a/retailcrm/lib/RetailcrmHistory.php b/retailcrm/lib/RetailcrmHistory.php index 2f574db..c334c2e 100644 --- a/retailcrm/lib/RetailcrmHistory.php +++ b/retailcrm/lib/RetailcrmHistory.php @@ -154,7 +154,12 @@ class RetailcrmHistory $references = new RetailcrmReferences(self::$api); if ($lastSync === false && $lastDate === false) { - $filter = array('startDate' => date('Y-m-d H:i:s', strtotime('-1 days', strtotime(date('Y-m-d H:i:s'))))); + $filter = array( + 'startDate' => date( + 'Y-m-d H:i:s', + strtotime('-1 days', strtotime(date('Y-m-d H:i:s'))) + ) + ); } elseif ($lastSync === false && $lastDate !== false) { $filter = array('startDate' => $lastDate); } elseif ($lastSync !== false) { @@ -537,6 +542,7 @@ class RetailcrmHistory ) { $ptype = $payment['type']; $ptypes = $references->getSystemPaymentModules(); + if ($payments[$ptype] != null) { foreach ($ptypes as $pay) { if ($pay['code'] == $payments[$ptype]) { @@ -564,183 +570,8 @@ class RetailcrmHistory } } - /* - * Clean deleted items - */ - foreach ($order['items'] as $key => $item) { - if (isset($item['delete']) && $item['delete'] == true) { - if (strpos($item['offer']['externalId'], '#') !== false) { - $itemId = explode('#', $item['offer']['externalId']); - $product_id = $itemId[0]; - $product_attribute_id = $itemId[1]; - } else { - $product_id = $item['offer']['externalId']; - $product_attribute_id = 0; - } - - self::deleteOrderDetailByProduct($orderToUpdate->id, $product_id, $product_attribute_id); - - unset($order['items'][$key]); - $ItemDiscount = true; - } - } - - /* - * Check items quantity and discount - */ - foreach ($orderToUpdate->getProductsDetail() as $orderItem) { - foreach ($order['items'] as $key => $item) { - if (strpos($item['offer']['externalId'], '#') !== false) { - $itemId = explode('#', $item['offer']['externalId']); - $product_id = $itemId[0]; - $product_attribute_id = $itemId[1]; - } else { - $product_id = $item['offer']['externalId']; - $product_attribute_id = 0; - } - - if ($product_id == $orderItem['product_id'] && - $product_attribute_id == $orderItem['product_attribute_id']) { - - $product = new Product((int) $product_id, false, self::$default_lang); - $tax = new TaxCore($product->id_tax_rules_group); - - if ($product_attribute_id != 0) { - $prodPrice = Combination::getPrice($product_attribute_id); - $prodPrice = $prodPrice > 0 ? $prodPrice : $product->price; - } else { - $prodPrice = $product->price; - } - - $prodPrice = $prodPrice + $prodPrice / 100 * $tax->rate; - - // discount - if (self::$apiVersion == 5) { - $productPrice = $prodPrice - $item['discountTotal']; - } else { - $productPrice = $prodPrice - $item['discount']; - - if ($item['discountPercent'] > 0) { - $productPrice = $productPrice - ($prodPrice / 100 * $item['discountPercent']); - } - } - - $productPrice = round($productPrice, 2); - - $orderDetail = new OrderDetail($orderItem['id_order_detail']); - $orderDetail->unit_price_tax_incl = $productPrice; - - // quantity - if (isset($item['quantity']) && $item['quantity'] != $orderItem['product_quantity']) { - $orderDetail->product_quantity = $item['quantity']; - $orderDetail->product_quantity_in_stock = $item['quantity']; - } - - $orderDetail->update(); - $ItemDiscount = true; - unset($order['items'][$key]); - } - } - } - - /* - * Check new items - */ - if (!empty($order['items'])) { - foreach ($order['items'] as $key => $newItem) { - $product_id = $newItem['offer']['externalId']; - $product_attribute_id = 0; - if (strpos($product_id, '#') !== false) { - $product_id = explode('#', $product_id); - - $product_attribute_id = $product_id[1]; - $product_id = $product_id[0]; - } - - $product = new Product((int) $product_id, false, self::$default_lang); - $tax = new TaxCore($product->id_tax_rules_group); - - if ($product_attribute_id != 0) { - $productName = htmlspecialchars( - strip_tags(Product::getProductName($product_id, $product_attribute_id)) - ); - $productPrice = Combination::getPrice($product_attribute_id); - $productPrice = $productPrice > 0 ? $productPrice : $product->price; - } else { - $productName = htmlspecialchars(strip_tags($product->name)); - $productPrice = $product->price; - } - - // discount - 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; - } - - $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]); - } - } - - /* - * Fix prices & discounts - * Discounts only for whole order - */ - 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']; - $totalPaid = $infoOrder['totalSumm']; - $deliveryCost = $infoOrder['delivery']['cost']; - $totalDiscount = $deliveryCost + $orderTotalProducts - $totalPaid; - - $orderCartRules = $orderToUpdate->getCartRules(); - foreach ($orderCartRules as $valCartRules) { - $order_cart_rule = new OrderCartRule($valCartRules['id_order_cart_rule']); - $order_cart_rule->delete(); - } - - $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); + if (isset($order['items'])) { + self::updateItems($order, $orderToUpdate); } /** @@ -777,6 +608,188 @@ class RetailcrmHistory } } + private static function updateItems($order, $orderToUpdate) + { + /* + * Clean deleted items + */ + foreach ($order['items'] as $key => $item) { + if (isset($item['delete']) && $item['delete'] == true) { + if (strpos($item['offer']['externalId'], '#') !== false) { + $itemId = explode('#', $item['offer']['externalId']); + $product_id = $itemId[0]; + $product_attribute_id = $itemId[1]; + } else { + $product_id = $item['offer']['externalId']; + $product_attribute_id = 0; + } + + self::deleteOrderDetailByProduct($orderToUpdate->id, $product_id, $product_attribute_id); + + unset($order['items'][$key]); + $ItemDiscount = true; + } + } + + /* + * Check items quantity and discount + */ + foreach ($orderToUpdate->getProductsDetail() as $orderItem) { + foreach ($order['items'] as $key => $item) { + if (strpos($item['offer']['externalId'], '#') !== false) { + $itemId = explode('#', $item['offer']['externalId']); + $product_id = $itemId[0]; + $product_attribute_id = $itemId[1]; + } else { + $product_id = $item['offer']['externalId']; + $product_attribute_id = 0; + } + + if ($product_id == $orderItem['product_id'] && + $product_attribute_id == $orderItem['product_attribute_id']) { + + $product = new Product((int) $product_id, false, self::$default_lang); + $tax = new TaxCore($product->id_tax_rules_group); + + if ($product_attribute_id != 0) { + $prodPrice = Combination::getPrice($product_attribute_id); + $prodPrice = $prodPrice > 0 ? $prodPrice : $product->price; + } else { + $prodPrice = $product->price; + } + + $prodPrice = $prodPrice + $prodPrice / 100 * $tax->rate; + + // discount + if (self::$apiVersion == 5) { + $productPrice = $prodPrice - $item['discountTotal']; + } else { + $productPrice = $prodPrice - $item['discount']; + + if ($item['discountPercent'] > 0) { + $productPrice = $productPrice - ($prodPrice / 100 * $item['discountPercent']); + } + } + + $productPrice = round($productPrice, 2); + + $orderDetail = new OrderDetail($orderItem['id_order_detail']); + $orderDetail->unit_price_tax_incl = $productPrice; + + // quantity + if (isset($item['quantity']) && $item['quantity'] != $orderItem['product_quantity']) { + $orderDetail->product_quantity = $item['quantity']; + $orderDetail->product_quantity_in_stock = $item['quantity']; + } + + $orderDetail->update(); + $ItemDiscount = true; + unset($order['items'][$key]); + } + } + } + + /* + * Check new items + */ + if (!empty($order['items'])) { + foreach ($order['items'] as $key => $newItem) { + $product_id = $newItem['offer']['externalId']; + $product_attribute_id = 0; + if (strpos($product_id, '#') !== false) { + $product_id = explode('#', $product_id); + + $product_attribute_id = $product_id[1]; + $product_id = $product_id[0]; + } + + $product = new Product((int) $product_id, false, self::$default_lang); + $tax = new TaxCore($product->id_tax_rules_group); + + if ($product_attribute_id != 0) { + $productName = htmlspecialchars( + strip_tags(Product::getProductName($product_id, $product_attribute_id)) + ); + $productPrice = Combination::getPrice($product_attribute_id); + $productPrice = $productPrice > 0 ? $productPrice : $product->price; + } else { + $productName = htmlspecialchars(strip_tags($product->name)); + $productPrice = $product->price; + } + + // discount + 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; + } + + $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]); + } + } + + /* + * Fix prices & discounts + * Discounts only for whole order + */ + 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']; + $totalPaid = $infoOrder['totalSumm']; + $deliveryCost = $infoOrder['delivery']['cost']; + $totalDiscount = $deliveryCost + $orderTotalProducts - $totalPaid; + + $orderCartRules = $orderToUpdate->getCartRules(); + foreach ($orderCartRules as $valCartRules) { + $order_cart_rule = new OrderCartRule($valCartRules['id_order_cart_rule']); + $order_cart_rule->delete(); + } + + $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); + } + } + /** * Delete order product from order by product * diff --git a/retailcrm/lib/RetailcrmHistoryHelper.php b/retailcrm/lib/RetailcrmHistoryHelper.php index c0dd89e..1e0d203 100644 --- a/retailcrm/lib/RetailcrmHistoryHelper.php +++ b/retailcrm/lib/RetailcrmHistoryHelper.php @@ -3,8 +3,8 @@ class RetailcrmHistoryHelper { public static function assemblyOrder($orderHistory) { - if (file_exists(_PS_ROOT_DIR_ . '/modules/retailcrm/objects.xml')) { - $objects = simplexml_load_file(_PS_ROOT_DIR_ . '/modules/retailcrm/objects.xml'); + if (file_exists( __DIR__ . '/../objects.xml')) { + $objects = simplexml_load_file(__DIR__ . '/../objects.xml'); foreach($objects->fields->field as $object) { $fields[(string)$object["group"]][(string)$object["id"]] = (string)$object; } @@ -46,7 +46,7 @@ class RetailcrmHistoryHelper { if ($change['newValue'] == null && $change['field'] == 'payments') { $orders[$change['order']['id']]['payments'][$change['payment']['id']]['delete'] = true; } - if (!$orders[$change['order']['id']]['payments'][$change['payment']['id']]['create'] && $fields['payment'][$change['field']]) { + if (!$orders[$change['order']['id']]['payments'][$change['payment']['id']] && $fields['payment'][$change['field']]) { $orders[$change['order']['id']]['payments'][$change['payment']['id']][$fields['payment'][$change['field']]] = $change['newValue']; } } diff --git a/retailcrm/lib/RetailcrmIcml.php b/retailcrm/lib/RetailcrmIcml.php index 8c72447..930ab44 100644 --- a/retailcrm/lib/RetailcrmIcml.php +++ b/retailcrm/lib/RetailcrmIcml.php @@ -2,7 +2,6 @@ class RetailcrmIcml { - protected $shop; protected $file; protected $properties; @@ -145,5 +144,4 @@ class RetailcrmIcml } } } - } diff --git a/retailcrm/objects.xml b/retailcrm/objects.xml index 5b98ab5..9a5c148 100644 --- a/retailcrm/objects.xml +++ b/retailcrm/objects.xml @@ -14,7 +14,7 @@ cumulativeDiscount personalDiscount discountCardNumber - + index country region @@ -28,7 +28,7 @@ intercomCode metro notes - + contragentType legalName legalAddress @@ -38,7 +38,7 @@ bankAddress corrAccount bankAccount - + id createdAt orderType @@ -51,7 +51,7 @@ patronymic phone additionalPhone - email + email paymentType paymentStatus discount @@ -63,9 +63,12 @@ shipmentDate shipped - payments - amount + status + type + externalId + amount + comment id initialPrice @@ -73,14 +76,14 @@ discountPercent quantity status - + code service date time cost netCost - + country index region @@ -94,7 +97,7 @@ intercomCode metro notes - + status trackNumber courier diff --git a/tests/helpers/RetailcrmTestCase.php b/tests/helpers/RetailcrmTestCase.php index 10a33e6..b244ad2 100644 --- a/tests/helpers/RetailcrmTestCase.php +++ b/tests/helpers/RetailcrmTestCase.php @@ -16,9 +16,27 @@ abstract class RetailcrmTestCase extends \PHPUnit\Framework\TestCase protected function setConfig() { - $delivery = json_encode(array('delivery' => 'delivery')); - $status = json_encode(array('status' => 'status', 'new' => 'new', 'completed'=> 'completed')); - $payment = json_encode(array('ps_checkpayment' => 'ps_checkpayment', 'bankwire' => 'bankwire', 'cheque' => 'cheque')); + $delivery = json_encode( + array( + 1 => 'delivery' + ) + ); + + $status = json_encode( + array( + 9 => 'status', + 10 => 'new', + 11 => 'completed' + ) + ); + + $payment = json_encode( + array( + 'ps_checkpayment' => 'ps_checkpayment', + 'bankwire' => 'bankwire', + 'cheque' => 'cheque' + ) + ); Configuration::updateValue('RETAILCRM_API_DELIVERY', $delivery); Configuration::updateValue('RETAILCRM_API_STATUS', $status); diff --git a/tests/helpers/RetailcrmTestHelper.php b/tests/helpers/RetailcrmTestHelper.php index 58e2172..d24cbe5 100644 --- a/tests/helpers/RetailcrmTestHelper.php +++ b/tests/helpers/RetailcrmTestHelper.php @@ -23,4 +23,11 @@ class RetailcrmTestHelper return $orderPayment->delete(); } + + public static function getMaxOrderId() + { + return Db::getInstance()->getValue( + 'SELECT MAX(id_order) FROM `' . _DB_PREFIX_ . 'orders`' + ); + } } diff --git a/tests/phpunit/RetailcrmCatalogTest.php b/tests/phpunit/RetailcrmCatalogTest.php new file mode 100644 index 0000000..8773fd6 --- /dev/null +++ b/tests/phpunit/RetailcrmCatalogTest.php @@ -0,0 +1,53 @@ +data = $catalog->getData(); + } + + public function testCatalog() + { + $this->assertInternalType('array', $this->data); + $this->assertCount(2, $this->data); + + $categories = $this->data[0]; + $products = $this->data[1]; + + $this->assertNotEmpty($categories); + $this->assertNotEmpty($products); + + foreach ($categories as $category) { + $this->assertNotEmpty($category); + $this->assertArrayHasKey('id', $category); + $this->assertArrayHasKey('parentId', $category); + $this->assertArrayHasKey('name', $category); + } + + foreach ($products as $product) { + $this->assertNotEmpty($product); + $this->assertArrayHasKey('id', $product); + $this->assertArrayHasKey('productId', $product); + $this->assertArrayHasKey('name', $product); + $this->assertArrayHasKey('productName', $product); + $this->assertArrayHasKey('url', $product); + $this->assertRegExp('/http/', $product['url']); + $this->assertArrayHasKey('price', $product); + } + } + + public function testIcmlGenerate() + { + $icml = new RetailcrmIcml(Configuration::get('PS_SHOP_NAME'), _PS_ROOT_DIR_ . '/retailcrm.xml'); + $icml->generate($this->data[0], $this->data[1]); + $this->assertFileExists(_PS_ROOT_DIR_ . '/retailcrm.xml'); + $xml = simplexml_load_file(_PS_ROOT_DIR_ . '/retailcrm.xml'); + $this->assertNotEquals(false, $xml); + } +} diff --git a/tests/phpunit/RetailcrmHistoryTest.php b/tests/phpunit/RetailcrmHistoryTest.php new file mode 100644 index 0000000..5caf989 --- /dev/null +++ b/tests/phpunit/RetailcrmHistoryTest.php @@ -0,0 +1,329 @@ +apiMock = $this->getMockBuilder('RetailcrmProxy') + ->disableOriginalConstructor() + ->setMethods( + array( + 'customersHistory', + 'ordersHistory', + 'ordersGet', + 'customersFixExternalIds', + 'ordersFixExternalIds' + ) + ) + ->getMock(); + + $catalog = new RetailcrmCatalog(); + $data = $catalog->getData(); + $this->product = $data[1][0]; + + Configuration::updateValue('RETAILCRM_API_DELIVERY_DEFAULT', 2); + Configuration::updateValue('RETAILCRM_API_PAYMENT_DEFAULT', 'bankwire'); + + $this->setConfig(); + } + + /** + * @param $api_version + * + * @dataProvider dataProvider + */ + public function testOrderCreate($api_version) + { + $this->apiMock->expects($this->any()) + ->method('ordersHistory') + ->willReturn( + new RetailcrmApiResponse( + '200', + json_encode( + $this->getHistoryDataNewOrder($api_version) + ) + ) + ); + $this->apiMock->expects($this->any()) + ->method('ordersGet') + ->willReturn( + new RetailcrmApiResponse( + '200', + json_encode( + array( + 'order' => $this->getApiOrder($api_version) + ) + ) + ) + ); + + RetailcrmHistory::$default_lang = (int)Configuration::get('PS_LANG_DEFAULT'); + RetailcrmHistory::$apiVersion = $api_version; + RetailcrmHistory::$api = $this->apiMock; + + $oldLastId = RetailcrmTestHelper::getMaxOrderId(); + RetailcrmHistory::ordersHistory(); + $newLastId = RetailcrmTestHelper::getMaxOrderId(); + + $this->assertNotEquals($oldLastId, $newLastId); + $this->assertTrue($newLastId > $oldLastId); + + $order = new Order($newLastId); + + $this->assertInstanceOf('Order', $order); + } + + public function testPaymentStatusUpdate() + { + $lastId = RetailcrmTestHelper::getMaxOrderId(); + + $this->apiMock->expects($this->any()) + ->method('ordersHistory') + ->willReturn( + new RetailcrmApiResponse( + '200', + json_encode( + $this->getUpdatePaymentStatus($lastId) + ) + ) + ); + + RetailcrmHistory::$default_lang = (int)Configuration::get('PS_LANG_DEFAULT'); + RetailcrmHistory::$apiVersion = 5; + RetailcrmHistory::$api = $this->apiMock; + + RetailcrmHistory::ordersHistory(); + } + + public function dataProvider() + { + return array( + array( + 'api_version' => '4' + ), + array( + 'api_version' => '5' + ) + ); + } + + private function getHistoryDataNewOrder($api_version) + { + return array( + 'success' => true, + 'history' => array( + array( + 'id' => 1, + 'createdAt' => '2018-01-01 00:00:00', + 'created' => true, + 'source' => 'user', + 'user' => array( + 'id' => 1 + ), + 'field' => 'status', + 'oldValue' => null, + 'newValue' => array( + 'code' => 'new' + ), + 'order' => $this->getApiOrder($api_version) + ) + ) + ); + } + + private function getApiOrder($api_version) + { + $order = array( + 'slug' => 1, + 'id' => 1, + 'number' => '1C', + 'orderType' => 'eshop-individual', + 'orderMethod' => 'phone', + 'countryIso' => 'RU', + 'createdAt' => '2018-01-01 00:00:00', + 'statusUpdatedAt' => '2018-01-01 00:00:00', + 'summ' => 100, + 'totalSumm' => 100, + 'prepaySum' => 0, + 'purchaseSumm' => 50, + 'markDatetime' => '2018-01-01 00:00:00', + 'firstName' => 'Test', + 'lastName' => 'Test', + 'phone' => '80000000000', + 'call' => false, + 'expired' => false, + 'customer' => array( + 'segments' => array(), + 'id' => 1, + 'firstName' => 'Test', + 'lastName' => 'Test', + 'email' => 'email@test.ru', + 'phones' => array( + array( + 'number' => '111111111111111' + ), + array( + 'number' => '+7111111111' + ) + ), + 'address' => array( + 'index' => '111111', + 'countryIso' => 'RU', + 'region' => 'Test region', + 'city' => 'Test', + 'text' => 'Test text address' + ), + 'createdAt' => '2018-01-01 00:00:00', + 'managerId' => 1, + 'vip' => false, + 'bad' => false, + 'site' => 'test-com', + 'contragent' => array( + 'contragentType' => 'individual' + ), + 'personalDiscount' => 0, + 'cumulativeDiscount' => 0, + 'marginSumm' => 58654, + 'totalSumm' => 61549, + 'averageSumm' => 15387.25, + 'ordersCount' => 4, + 'costSumm' => 101, + 'customFields' => array( + 'custom' => 'test' + ) + ), + 'contragent' => array(), + 'delivery' => array( + 'code' => 'delivery', + 'cost' => 100, + 'netCost' => 0, + 'address' => array( + 'index' => '111111', + 'countryIso' => 'RU', + 'region' => 'Test region', + 'city' => 'Test', + 'text' => 'Test text address' + ) + ), + 'site' => 'test-com', + 'status' => 'new', + 'items' => array( + array( + 'id' => 160, + 'initialPrice' => 100, + 'createdAt' => '2018-01-01 00:00:00', + 'quantity' => 1, + 'status' => 'new', + 'offer' => array( + 'id' => 1, + 'externalId' => $this->product['id'], + 'xmlId' => '1', + 'name' => 'Test name', + 'vatRate' => 'none' + ), + 'properties' => array(), + 'purchasePrice' => 50 + ) + ), + 'fromApi' => false, + 'length' => 0, + 'width' => 0, + 'height' => 0, + 'shipmentStore' => 'main', + 'shipped' => false, + 'customFields' => array(), + 'uploadedToExternalStoreSystem' => false + ); + + if ($api_version == 5) { + $order['payments'][] = array( + 'id' => 97, + 'type' => 'cheque', + 'amount' => 200 + ); + } else { + $order['paymentType'] = 'cheque'; + } + + return $order; + } + + private function getUpdatePaymentStatus($orderId) + { + return array( + 'success' => true, + 'history' => array( + array( + 'id' => 654, + 'createdAt' => '2018-01-01 00:00:00', + 'source' => 'user', + 'user' => array( + 'id' => 1 + ), + 'field' => 'full_paid_at', + 'oldValue' => null, + 'newValue' => '2018-01-01 00:00:00', + 'order' => array( + 'id' => 1, + 'externalId' => $orderId, + 'site' => 'test-com', + 'status' => 'new' + ) + ), + array( + 'id'=> 655, + 'createdAt' => '2018-01-01 00:00:00', + 'source' => 'user', + 'user' => array( + 'id' => 1 + ), + 'field' => 'payments.paid_at', + 'oldValue' => null, + 'newValue' => '2018-01-01 00:00:00', + 'order' => array( + 'id' => 1, + 'externalId' => $orderId, + 'site' => 'test-com', + 'status' => 'new' + ), + 'payment'=> array( + 'id'=> 102, + 'type'=> 'cheque', + 'externalId' => 1 + ) + ), + array( + 'id' => 656, + 'createdAt' => '2018-01-01 00:00:00', + 'source' => 'user', + 'user' => array( + 'id' => 1 + ), + 'field' => 'payments.status', + 'oldValue' => array( + 'code' => 'not-paid' + ), + 'newValue' => array( + 'code' => 'paid' + ), + 'order' => array( + 'id' => 1, + 'externalId' => $orderId, + 'site' => 'test-com', + 'status' => 'new' + ), + 'payment' => array( + 'id' => 102, + 'type' => 'cheque', + 'externalId' => 1 + ) + ) + ) + ); + } +} diff --git a/tests/phpunit/RetailcrmTest.php b/tests/phpunit/RetailcrmTest.php index 8bd3dba..9b83f86 100644 --- a/tests/phpunit/RetailcrmTest.php +++ b/tests/phpunit/RetailcrmTest.php @@ -92,7 +92,7 @@ class RetailCRMTest extends RetailcrmTestCase $status = new StdClass(); if ($newOrder === false) { - $status->id = 'completed'; + $status->id = 11; $params = array( 'newOrderStatus' => $status, diff --git a/tests/phpunit/bootstrap.php b/tests/phpunit/bootstrap.php index 8cf5da5..56326c3 100644 --- a/tests/phpunit/bootstrap.php +++ b/tests/phpunit/bootstrap.php @@ -7,6 +7,7 @@ require_once dirname(__DIR__) . '../../retailcrm/bootstrap.php'; require_once __DIR__ . '/../../retailcrm/retailcrm.php'; require_once __DIR__ . '/../helpers/RetailcrmTestCase.php'; require_once __DIR__ . '/../helpers/RetailcrmTestHelper.php'; +require_once dirname(__DIR__) . '../../../PrestaShop/init.php'; $module = new RetailCRM(); $module->install();