Catalog and history tests

This commit is contained in:
Akolzin Dmitry 2018-10-05 17:50:20 +03:00
parent 6e6177fc71
commit 270b69707d
12 changed files with 626 additions and 203 deletions

View File

@ -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

View File

@ -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);
}
}

View File

@ -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
*

View File

@ -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'];
}
}

View File

@ -2,7 +2,6 @@
class RetailcrmIcml
{
protected $shop;
protected $file;
protected $properties;
@ -145,5 +144,4 @@ class RetailcrmIcml
}
}
}
}

View File

@ -14,7 +14,7 @@
<field id="cumulative_discount" group="customer">cumulativeDiscount</field>
<field id="personal_discount" group="customer">personalDiscount</field>
<field id="discount_card_number" group="customer">discountCardNumber</field>
<field id="address.index" group="customerAddress">index</field>
<field id="address.country" group="customerAddress">country</field>
<field id="address.region" group="customerAddress">region</field>
@ -28,7 +28,7 @@
<field id="address.intercom_code" group="customerAddress">intercomCode</field>
<field id="address.metro" group="customerAddress">metro</field>
<field id="address.notes" group="customerAddress">notes</field>
<field id="contragent.contragent_type" group="customerContragent">contragentType</field>
<field id="contragent.legal_name" group="customerContragent">legalName</field>
<field id="contragent.legal_address" group="customerContragent">legalAddress</field>
@ -38,7 +38,7 @@
<field id="contragent.bank_address" group="customerContragent">bankAddress</field>
<field id="contragent.corr_account" group="customerContragent">corrAccount</field>
<field id="contragent.bank_account" group="customerContragent">bankAccount</field>
<field id="id" group="order">id</field>
<field id="created_at" group="order">createdAt</field>
<field id="order_type" group="order">orderType</field>
@ -51,7 +51,7 @@
<field id="patronymic" group="order">patronymic</field>
<field id="phone" group="order">phone</field>
<field id="additional_phone" group="order">additionalPhone</field>
<field id="email" group="order">email</field>
<field id="email" group="order">email</field>
<field id="payment_type" group="order">paymentType</field>
<field id="payment_status" group="order">paymentStatus</field>
<field id="discount" group="order">discount</field>
@ -63,9 +63,12 @@
<field id="shipment_date" group="order">shipmentDate</field>
<field id="shipped" group="order">shipped</field>
<!--<field id="order_product" group="order">item</field>-->
<field id="payment" group="order">payments</field>
<field id="payments.amount" group="payment">amount</field>
<field id="payments.status" group="payment">status</field>
<field id="payments.type" group="payment">type</field>
<field id="payments.external_id" group="payment">externalId</field>
<field id="payments.amount" group="payment">amount</field>
<field id="payments.comment" group="payment">comment</field>
<field id="order_product.id" group="item">id</field>
<field id="order_product.initial_price" group="item">initialPrice</field>
@ -73,14 +76,14 @@
<field id="order_product.discount_percent" group="item">discountPercent</field>
<field id="order_product.quantity" group="item">quantity</field>
<field id="order_product.status" group="item">status</field>
<field id="delivery_type" group="delivery">code</field>
<field id="delivery_service" group="delivery">service</field>
<field id="delivery_date" group="delivery">date</field>
<field id="delivery_time" group="delivery">time</field>
<field id="delivery_cost" group="delivery">cost</field>
<field id="delivery_net_cost" group="delivery">netCost</field>
<field id="delivery_address.country" group="orderAddress">country</field>
<field id="delivery_address.index" group="orderAddress">index</field>
<field id="delivery_address.region" group="orderAddress">region</field>
@ -94,7 +97,7 @@
<field id="delivery_address.intercom_code" group="orderAddress">intercomCode</field>
<field id="delivery_address.metro" group="orderAddress">metro</field>
<field id="delivery_address.notes" group="orderAddress">notes</field>
<field id="integration_delivery_data.status" group="integrationDelivery">status</field>
<field id="integration_delivery_data.track_number" group="integrationDelivery">trackNumber</field>
<field id="integration_delivery_data.courier" group="integrationDelivery">courier</field>

View File

@ -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);

View File

@ -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`'
);
}
}

View File

@ -0,0 +1,53 @@
<?php
class RetailcrmCatalogTest extends RetailcrmTestCase
{
protected $data;
public function setUp()
{
parent::setUp();
$catalog = new RetailcrmCatalog();
$this->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);
}
}

View File

@ -0,0 +1,329 @@
<?php
class RetailcrmHistoryTest extends RetailcrmTestCase
{
private $apiMock;
private $product;
public function setUp()
{
parent::setUp();
$this->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
)
)
)
);
}
}

View File

@ -92,7 +92,7 @@ class RetailCRMTest extends RetailcrmTestCase
$status = new StdClass();
if ($newOrder === false) {
$status->id = 'completed';
$status->id = 11;
$params = array(
'newOrderStatus' => $status,

View File

@ -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();