1
0
mirror of synced 2024-11-22 13:06:07 +03:00

Merge pull request #15 from iyzoer/v1.9

v1.1.1
This commit is contained in:
Alex Lushpai 2018-09-04 09:58:28 +03:00 committed by GitHub
commit 8def052f47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 134 additions and 133 deletions

View File

@ -21,7 +21,7 @@ class Retailcrm_Retailcrm_Model_Exchange
); );
} }
} }
/** /**
* Get orders history & modify orders into shop * Get orders history & modify orders into shop
* *
@ -31,12 +31,12 @@ class Retailcrm_Retailcrm_Model_Exchange
$runTime = $this->getExchangeTime($this->_config['general']['history']); $runTime = $this->getExchangeTime($this->_config['general']['history']);
$historyFilter = array(); $historyFilter = array();
$historiOrder = array(); $historiOrder = array();
$historyStart = Mage::getStoreConfig('retailcrm/general/fhistory'); $historyStart = Mage::getStoreConfig('retailcrm/general/fhistory');
if($historyStart && $historyStart > 0) { if($historyStart && $historyStart > 0) {
$historyFilter['sinceId'] = $historyStart; $historyFilter['sinceId'] = $historyStart;
} }
while(true) { while(true) {
try { try {
$response = $this->_api->ordersHistory($historyFilter); $response = $this->_api->ordersHistory($historyFilter);
@ -46,38 +46,38 @@ class Retailcrm_Retailcrm_Model_Exchange
Mage::log( Mage::log(
sprintf("Orders history error: [HTTP status %s] %s", $response->getStatusCode(), $response->getErrorMsg()) sprintf("Orders history error: [HTTP status %s] %s", $response->getStatusCode(), $response->getErrorMsg())
); );
if (isset($response['errors'])) { if (isset($response['errors'])) {
Mage::log(implode(' :: ', $response['errors'])); Mage::log(implode(' :: ', $response['errors']));
} }
return false; return false;
} }
} catch (Retailcrm_Retailcrm_Model_Exception_CurlException $e) { } catch (Retailcrm_Retailcrm_Model_Exception_CurlException $e) {
Mage::log($e->getMessage()); Mage::log($e->getMessage());
return false; return false;
} }
$orderH = isset($response['history']) ? $response['history'] : array(); $orderH = isset($response['history']) ? $response['history'] : array();
if(count($orderH) == 0) { if (count($orderH) == 0) {
return true; return true;
} }
$historiOrder = array_merge($historiOrder, $orderH); $historiOrder = array_merge($historiOrder, $orderH);
$end = array_pop($response->history); $end = array_pop($response->history);
$historyFilter['sinceId'] = $end['id']; $historyFilter['sinceId'] = $end['id'];
if($response['pagination']['totalPageCount'] == 1) { if($response['pagination']['totalPageCount'] == 1) {
Mage::getModel('core/config')->saveConfig('retailcrm/general/fhistory', $historyFilter['sinceId']); Mage::getModel('core/config')->saveConfig('retailcrm/general/fhistory', $historyFilter['sinceId']);
$orders = self::assemblyOrder($historiOrder); $orders = self::assemblyOrder($historiOrder);
$this->processOrders($orders, $nowTime); $this->processOrders($orders, $nowTime);
return true; return true;
} }
}//endwhile }//endwhile
} }
/** /**
* @param array $orders * @param array $orders
*/ */
@ -89,13 +89,13 @@ class Retailcrm_Retailcrm_Model_Exchange
); );
foreach ($orders as $order) { foreach ($orders as $order) {
if(!empty($order['externalId'])) { if(!empty($order['externalId'])) {
$this->doUpdate($order); $this->doUpdate($order);
} else { } else {
$this->doCreate($order); $this->doCreate($order);
} }
} }
die(); die();
} }
} }
@ -154,11 +154,12 @@ class Retailcrm_Retailcrm_Model_Exchange
$customer->setWebsiteId($siteid); $customer->setWebsiteId($siteid);
$customer->loadByEmail($order['email']); $customer->loadByEmail($order['email']);
if (!is_numeric($customer->getId())) {
if (!is_numeric($customer->getId())) {
$customer $customer
->setGroupId(1) ->setGroupId(1)
->setWebsiteId($siteid) ->setWebsiteId($siteid)
->setStore($storeId) ->setStoreId($storeId)
->setEmail($order['email']) ->setEmail($order['email'])
->setFirstname($order['firstName']) ->setFirstname($order['firstName'])
->setLastname($order['lastName']) ->setLastname($order['lastName'])
@ -224,7 +225,7 @@ class Retailcrm_Retailcrm_Model_Exchange
foreach ($order['items'] as $item) { foreach ($order['items'] as $item) {
$products[$item['offer']['externalId']] = array('qty' => $item['quantity']); $products[$item['offer']['externalId']] = array('qty' => $item['quantity']);
} }
$orderData = array( $orderData = array(
'session' => array( 'session' => array(
'customer_id' => $customer->getId(), 'customer_id' => $customer->getId(),
@ -275,45 +276,45 @@ class Retailcrm_Retailcrm_Model_Exchange
$quote = Mage::getModel('sales/quote')->setStoreId($storeId); $quote = Mage::getModel('sales/quote')->setStoreId($storeId);
$quote->assignCustomer($customer); $quote->assignCustomer($customer);
$quote->setSendCconfirmation($_sendConfirmation); $quote->setSendCconfirmation($_sendConfirmation);
foreach($products as $idx => $val) { foreach($products as $idx => $val) {
$product = Mage::getModel('catalog/product')->load($idx); $product = Mage::getModel('catalog/product')->load($idx);
$quote->addProduct($product, new Varien_Object($val)); $quote->addProduct($product, new Varien_Object($val));
} }
$shipping_method = self::getAllShippingMethodsCode($orderData['order']['shipping_method']); $shipping_method = self::getAllShippingMethodsCode($orderData['order']['shipping_method']);
$billingAddress = $quote->getBillingAddress()->addData($orderData['order']['billing_address']); $billingAddress = $quote->getBillingAddress()->addData($orderData['order']['billing_address']);
$shippingAddress = $quote->getShippingAddress()->addData($orderData['order']['shipping_address']); $shippingAddress = $quote->getShippingAddress()->addData($orderData['order']['shipping_address']);
$shippingAddress->setCollectShippingRates(true) $shippingAddress->setCollectShippingRates(true)
->collectShippingRates() ->collectShippingRates()
->setShippingMethod($shipping_method) ->setShippingMethod($shipping_method)
->setPaymentMethod($orderData['payment']['method']); ->setPaymentMethod($orderData['payment']['method']);
$quote->getPayment()->importData($orderData['payment']); $quote->getPayment()->importData($orderData['payment']);
$quote->collectTotals(); $quote->collectTotals();
$quote->reserveOrderId(); $quote->reserveOrderId();
$quote->save(); $quote->save();
$service = Mage::getModel('sales/service_quote', $quote); $service = Mage::getModel('sales/service_quote', $quote);
try{ try{
$service->submitAll(); $service->submitAll();
} }
catch (Exception $e) { catch (Exception $e) {
Mage::log($e->getMessage()); Mage::log($e->getMessage());
} }
try { try {
$response = $this->_api->ordersFixExternalIds( $response = $this->_api->ordersFixExternalIds(
array( array(
array( array(
'id' => $order['id'], 'id' => $order['id'],
'externalId' =>$service->getOrder()->getRealOrderId() 'externalId' =>$service->getOrder()->getRealOrderId()
) )
) )
); );
if (!$response->isSuccessful() || 200 !== $response->getStatusCode()) { if (!$response->isSuccessful() || 200 !== $response->getStatusCode()) {
Mage::log( Mage::log(
sprintf( sprintf(
@ -329,7 +330,7 @@ class Retailcrm_Retailcrm_Model_Exchange
} }
} catch (Retailcrm_Retailcrm_Model_Exception_CurlException $e) { } catch (Retailcrm_Retailcrm_Model_Exception_CurlException $e) {
Mage::log($e->getMessage()); Mage::log($e->getMessage());
} }
} }
/** /**
@ -338,10 +339,10 @@ class Retailcrm_Retailcrm_Model_Exchange
private function doCreateUp($order) private function doCreateUp($order)
{ {
Mage::log($order, null, 'retailcrmHistoriCreateUp.log', true); Mage::log($order, null, 'retailcrmHistoriCreateUp.log', true);
try { try {
$response = $this->_api->ordersGet($order['id'], $by = 'id'); $response = $this->_api->ordersGet($order['id'], $by = 'id');
if ($response->isSuccessful() && 200 === $response->getStatusCode()) { if ($response->isSuccessful() && 200 === $response->getStatusCode()) {
$order = $response->order; $order = $response->order;
} else { } else {
@ -352,7 +353,7 @@ class Retailcrm_Retailcrm_Model_Exchange
$response->getErrorMsg() $response->getErrorMsg()
) )
); );
if (isset($response['errors'])) { if (isset($response['errors'])) {
Mage::log(implode(' :: ', $response['errors'])); Mage::log(implode(' :: ', $response['errors']));
} }
@ -365,7 +366,7 @@ class Retailcrm_Retailcrm_Model_Exchange
$this->_config = Mage::getStoreConfig('retailcrm'); $this->_config = Mage::getStoreConfig('retailcrm');
$payments = array_flip(array_filter($this->_config['payment'])); $payments = array_flip(array_filter($this->_config['payment']));
$shippings = array_flip(array_filter($this->_config['shipping'])); $shippings = array_flip(array_filter($this->_config['shipping']));
// get store // get store
$_sendConfirmation = '0'; $_sendConfirmation = '0';
$sitesConfig = $this->_helper->getMappingSites(); $sitesConfig = $this->_helper->getMappingSites();
@ -380,23 +381,23 @@ class Retailcrm_Retailcrm_Model_Exchange
} }
$siteid = Mage::getModel('core/store')->load($storeId)->getWebsiteId(); $siteid = Mage::getModel('core/store')->load($storeId)->getWebsiteId();
// search or create customer // search or create customer
$customer = Mage::getSingleton('customer/customer'); $customer = Mage::getSingleton('customer/customer');
$customer->setWebsiteId($siteid); $customer->setWebsiteId($siteid);
$customer->loadByEmail($order['email']); $customer->loadByEmail($order['email']);
if (!is_numeric($customer->getId())) { if (!is_numeric($customer->getId())) {
$customer $customer
->setGropuId(1) ->setGropuId(1)
->setWebsiteId($siteid) ->setWebsiteId($siteid)
->setStore($storeId) ->setStoreId($storeId)
->setEmail($order['email']) ->setEmail($order['email'])
->setFirstname($order['firstName']) ->setFirstname($order['firstName'])
->setLastname($order['lastName']) ->setLastname($order['lastName'])
->setMiddleName($order['patronymic']) ->setMiddleName($order['patronymic'])
->setPassword(uniqid()); ->setPassword(uniqid());
try { try {
$customer->save(); $customer->save();
$customer->setConfirmation(null); $customer->setConfirmation(null);
@ -404,7 +405,7 @@ class Retailcrm_Retailcrm_Model_Exchange
} catch (Exception $e) { } catch (Exception $e) {
Mage::log($e->getMessage()); Mage::log($e->getMessage());
} }
$address = Mage::getModel("customer/address"); $address = Mage::getModel("customer/address");
$address->setCustomerId($customer->getId()) $address->setCustomerId($customer->getId())
->setFirstname($customer->getFirstname()) ->setFirstname($customer->getFirstname())
@ -419,14 +420,14 @@ class Retailcrm_Retailcrm_Model_Exchange
->setIsDefaultBilling('1') ->setIsDefaultBilling('1')
->setIsDefaultShipping('1') ->setIsDefaultShipping('1')
->setSaveInAddressBook('1'); ->setSaveInAddressBook('1');
try{ try{
$address->save(); $address->save();
} }
catch (Exception $e) { catch (Exception $e) {
Mage::log($e->getMessage()); Mage::log($e->getMessage());
} }
try { try {
$response = $this->_api->customersFixExternalIds( $response = $this->_api->customersFixExternalIds(
array( array(
@ -442,7 +443,7 @@ class Retailcrm_Retailcrm_Model_Exchange
$response->getErrorMsg() $response->getErrorMsg()
) )
); );
if (isset($response['errors'])) { if (isset($response['errors'])) {
Mage::log(implode(' :: ', $response['errors'])); Mage::log(implode(' :: ', $response['errors']));
} }
@ -451,12 +452,12 @@ class Retailcrm_Retailcrm_Model_Exchange
Mage::log($e->getMessage()); Mage::log($e->getMessage());
} }
} }
$products = array(); $products = array();
foreach ($order['items'] as $item) { foreach ($order['items'] as $item) {
$products[$item['offer']['externalId']] = array('qty' => $item['quantity']); $products[$item['offer']['externalId']] = array('qty' => $item['quantity']);
} }
$orderData = array( $orderData = array(
'session' => array( 'session' => array(
'customer_id' => $customer->getId(), 'customer_id' => $customer->getId(),
@ -500,11 +501,11 @@ class Retailcrm_Retailcrm_Model_Exchange
'send_confirmation' => $_sendConfirmation 'send_confirmation' => $_sendConfirmation
) )
); );
$quote = Mage::getModel('sales/quote')->setStoreId($storeId); $quote = Mage::getModel('sales/quote')->setStoreId($storeId);
$quote->assignCustomer($customer); $quote->assignCustomer($customer);
$quote->setSendCconfirmation($_sendConfirmation); $quote->setSendCconfirmation($_sendConfirmation);
foreach($products as $idx => $val) { foreach($products as $idx => $val) {
$product = Mage::getModel('catalog/product')->load($idx); $product = Mage::getModel('catalog/product')->load($idx);
$quote->addProduct($product, new Varien_Object($val)); $quote->addProduct($product, new Varien_Object($val));
@ -513,23 +514,23 @@ class Retailcrm_Retailcrm_Model_Exchange
$shipping_method = self::getAllShippingMethodsCode($orderData['order']['shipping_method']); $shipping_method = self::getAllShippingMethodsCode($orderData['order']['shipping_method']);
$billingAddress = $quote->getBillingAddress()->addData($orderData['order']['billing_address']); $billingAddress = $quote->getBillingAddress()->addData($orderData['order']['billing_address']);
$shippingAddress = $quote->getShippingAddress()->addData($orderData['order']['shipping_address']); $shippingAddress = $quote->getShippingAddress()->addData($orderData['order']['shipping_address']);
$shippingAddress->setCollectShippingRates(true) $shippingAddress->setCollectShippingRates(true)
->collectShippingRates() ->collectShippingRates()
->setShippingMethod($shipping_method) ->setShippingMethod($shipping_method)
->setPaymentMethod($orderData['payment']['method']); ->setPaymentMethod($orderData['payment']['method']);
$quote->getPayment()->importData($orderData['payment']); $quote->getPayment()->importData($orderData['payment']);
$quote->collectTotals(); $quote->collectTotals();
$originalId = $order['externalId']; $originalId = $order['externalId'];
$oldOrder = Mage::getModel('sales/order')->loadByIncrementId($originalId); $oldOrder = Mage::getModel('sales/order')->loadByIncrementId($originalId);
$oldOrderArr = $oldOrder->getData(); $oldOrderArr = $oldOrder->getData();
if (!empty($oldOrderArr['original_increment_id'])) { if (!empty($oldOrderArr['original_increment_id'])) {
$originalId = $oldOrderArr['original_increment_id']; $originalId = $oldOrderArr['original_increment_id'];
} }
$orderDataUp = array( $orderDataUp = array(
'original_increment_id' => $originalId, 'original_increment_id' => $originalId,
'relation_parent_id' => $oldOrder->getId(), 'relation_parent_id' => $oldOrder->getId(),
@ -537,23 +538,23 @@ class Retailcrm_Retailcrm_Model_Exchange
'edit_increment' => $oldOrder->getEditIncrement()+1, 'edit_increment' => $oldOrder->getEditIncrement()+1,
'increment_id' => $originalId.'-'.($oldOrder->getEditIncrement()+1) 'increment_id' => $originalId.'-'.($oldOrder->getEditIncrement()+1)
); );
$quote->setReservedOrderId($orderDataUp['increment_id']); $quote->setReservedOrderId($orderDataUp['increment_id']);
$quote->save(); $quote->save();
$service = Mage::getModel('sales/service_quote', $quote); $service = Mage::getModel('sales/service_quote', $quote);
$service->setOrderData($orderDataUp); $service->setOrderData($orderDataUp);
try{ try{
$service->submitAll(); $service->submitAll();
} }
catch (Exception $e) { catch (Exception $e) {
Mage::log($e->getMessage()); Mage::log($e->getMessage());
} }
$magentoOrder = Mage::getModel('sales/order')->loadByIncrementId($orderDataUp['relation_parent_real_id']); $magentoOrder = Mage::getModel('sales/order')->loadByIncrementId($orderDataUp['relation_parent_real_id']);
$magentoOrder->setState(Mage_Sales_Model_Order::STATE_CANCELED, true)->save(); $magentoOrder->setState(Mage_Sales_Model_Order::STATE_CANCELED, true)->save();
try { try {
$response = $this->_api->ordersFixExternalIds( $response = $this->_api->ordersFixExternalIds(
array( array(
@ -563,7 +564,7 @@ class Retailcrm_Retailcrm_Model_Exchange
) )
) )
); );
if (!$response->isSuccessful() || 200 !== $response->getStatusCode()) { if (!$response->isSuccessful() || 200 !== $response->getStatusCode()) {
Mage::log( Mage::log(
sprintf( sprintf(
@ -572,7 +573,7 @@ class Retailcrm_Retailcrm_Model_Exchange
$response->getErrorMsg() $response->getErrorMsg()
) )
); );
if (isset($response['errors'])) { if (isset($response['errors'])) {
Mage::log(implode(' :: ', $response['errors'])); Mage::log(implode(' :: ', $response['errors']));
} }
@ -580,58 +581,58 @@ class Retailcrm_Retailcrm_Model_Exchange
} catch (Retailcrm_Retailcrm_Model_Exception_CurlException $e) { } catch (Retailcrm_Retailcrm_Model_Exception_CurlException $e) {
Mage::log($e->getMessage()); Mage::log($e->getMessage());
} }
} }
/** /**
* @param array $order * @param array $order
*/ */
private function doUpdate($order) private function doUpdate($order)
{ {
$magentoOrder = Mage::getModel('sales/order')->loadByIncrementId($order['externalId']); $magentoOrder = Mage::getModel('sales/order')->loadByIncrementId($order['externalId']);
$magentoOrderArr = $magentoOrder->getData(); $magentoOrderArr = $magentoOrder->getData();
$config = Mage::getStoreConfig('retailcrm'); $config = Mage::getStoreConfig('retailcrm');
Mage::log($order, null, 'retailcrmHistoriUpdate.log', true); Mage::log($order, null, 'retailcrmHistoriUpdate.log', true);
if((!empty($order['order_edit']))&&($order['order_edit'] == 1)) { if((!empty($order['order_edit']))&&($order['order_edit'] == 1)) {
$this->doCreateUp($order); $this->doCreateUp($order);
} }
if (!empty($order['status'])) { if (!empty($order['status'])) {
try { try {
$response = $this->_api->statusesList(); $response = $this->_api->statusesList();
if ($response->isSuccessful() && 200 === $response->getStatusCode()) { if ($response->isSuccessful() && 200 === $response->getStatusCode()) {
$code = $order['status']; $code = $order['status'];
$group = $response->statuses[$code]['group']; $group = $response->statuses[$code]['group'];
if ($magentoOrder->hasInvoices()) { if ($magentoOrder->hasInvoices()) {
$invIncrementIDs = array(); $invIncrementIDs = array();
foreach ($magentoOrder->getInvoiceCollection() as $inv) { foreach ($magentoOrder->getInvoiceCollection() as $inv) {
$invIncrementIDs[] = $inv->getIncrementId(); $invIncrementIDs[] = $inv->getIncrementId();
} }
} }
if (in_array($group, array('approval', 'assembling', 'delivery'))) { if (in_array($group, array('approval', 'assembling', 'delivery'))) {
if(empty($invIncrementIDs)) { if(empty($invIncrementIDs)) {
$magentoOrder->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true); $magentoOrder->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true);
$magentoOrder->save(); $magentoOrder->save();
$invoice = $magentoOrder->prepareInvoice() $invoice = $magentoOrder->prepareInvoice()
->setTransactionId($magentoOrder->getRealOrderId()) ->setTransactionId($magentoOrder->getRealOrderId())
->addComment("Add status on CRM") ->addComment("Add status on CRM")
->register() ->register()
->pay(); ->pay();
$transaction_save = Mage::getModel('core/resource_transaction') $transaction_save = Mage::getModel('core/resource_transaction')
->addObject($invoice) ->addObject($invoice)
->addObject($invoice->getOrder()); ->addObject($invoice->getOrder());
$transaction_save->save(); $transaction_save->save();
} }
} }
if (in_array($group, array('complete'))) { if (in_array($group, array('complete'))) {
if(empty($invIncrementIDs)){ if(empty($invIncrementIDs)){
$invoice = $magentoOrder->prepareInvoice() $invoice = $magentoOrder->prepareInvoice()
@ -639,15 +640,15 @@ class Retailcrm_Retailcrm_Model_Exchange
->addComment("Add status on CRM") ->addComment("Add status on CRM")
->register() ->register()
->pay(); ->pay();
$transaction_save = Mage::getModel('core/resource_transaction') $transaction_save = Mage::getModel('core/resource_transaction')
->addObject($invoice) ->addObject($invoice)
->addObject($invoice->getOrder()); ->addObject($invoice->getOrder());
$transaction_save->save(); $transaction_save->save();
$magentoOrder->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true)->save(); $magentoOrder->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true)->save();
} }
if($magentoOrder->canShip()) { if($magentoOrder->canShip()) {
$itemQty = $magentoOrder->getItemsCollection()->count(); $itemQty = $magentoOrder->getItemsCollection()->count();
$shipment = Mage::getModel('sales/service_order', $magentoOrder)->prepareShipment($itemQty); $shipment = Mage::getModel('sales/service_order', $magentoOrder)->prepareShipment($itemQty);
@ -656,36 +657,36 @@ class Retailcrm_Retailcrm_Model_Exchange
} }
} }
if($code == $config['status']['canceled']) { if($code == $config['status']['canceled']) {
$magentoOrder->setState(Mage_Sales_Model_Order::STATE_CANCELED, true)->save(); $magentoOrder->setState(Mage_Sales_Model_Order::STATE_CANCELED, true)->save();
} }
if($code == $config['status']['holded']) { if($code == $config['status']['holded']) {
if($magentoOrder->canHold()){ if($magentoOrder->canHold()){
$magentoOrder->hold()->save(); $magentoOrder->hold()->save();
} }
} }
if($code == $config['status']['unhold']) { if($code == $config['status']['unhold']) {
if($magentoOrder->canUnhold()) { if($magentoOrder->canUnhold()) {
$magentoOrder->unhold()->save(); $magentoOrder->unhold()->save();
} }
} }
if($code == $config['status']['closed']) { if($code == $config['status']['closed']) {
if($magentoOrder->canCreditmemo()) { if($magentoOrder->canCreditmemo()) {
$orderItem = $magentoOrder->getItemsCollection(); $orderItem = $magentoOrder->getItemsCollection();
foreach ($orderItem as $item) { foreach ($orderItem as $item) {
$data['qtys'][$item->getid()] = $item->getQtyOrdered(); $data['qtys'][$item->getid()] = $item->getQtyOrdered();
} }
$service = Mage::getModel('sales/service_order', $magentoOrder); $service = Mage::getModel('sales/service_order', $magentoOrder);
$creditMemo = $service->prepareCreditmemo($data)->register()->save(); $creditMemo = $service->prepareCreditmemo($data)->register()->save();
$magentoOrder->addStatusToHistory(Mage_Sales_Model_Order::STATE_CLOSED, 'Add status on CRM', false); $magentoOrder->addStatusToHistory(Mage_Sales_Model_Order::STATE_CLOSED, 'Add status on CRM', false);
$magentoOrder->save(); $magentoOrder->save();
} }
} }
Mage::log("Update: " . $order['externalId'], null, 'history.log'); Mage::log("Update: " . $order['externalId'], null, 'history.log');
} else { } else {
Mage::log( Mage::log(
@ -702,14 +703,14 @@ class Retailcrm_Retailcrm_Model_Exchange
} }
} catch (Retailcrm_Retailcrm_Model_Exception_CurlException $e) { } catch (Retailcrm_Retailcrm_Model_Exception_CurlException $e) {
Mage::log($e->getMessage()); Mage::log($e->getMessage());
} }
} }
if(!empty($order['manager_comment'])) { if(!empty($order['manager_comment'])) {
$magentoOrder->addStatusHistoryComment($order['manager_comment']); $magentoOrder->addStatusHistoryComment($order['manager_comment']);
$magentoOrder->save(); $magentoOrder->save();
} }
} }
/** /**
@ -842,8 +843,8 @@ class Retailcrm_Retailcrm_Model_Exchange
return (string) $country; return (string) $country;
} }
public static function assemblyOrder($orderHistory) public static function assemblyOrder($orderHistory)
{ {
$orders = array(); $orders = array();
@ -855,32 +856,32 @@ class Retailcrm_Retailcrm_Model_Exchange
if(isset($change['created'])) { if(isset($change['created'])) {
$item['create'] = 1; $item['create'] = 1;
} }
$items[$item['id']] = $item; $items[$item['id']] = $item;
} }
$change['order']['items'] = $items; $change['order']['items'] = $items;
} }
Mage::log($change, null, 'retailcrmHistoryAssemblyOrder.log', true); Mage::log($change, null, 'retailcrmHistoryAssemblyOrder.log', true);
if($change['order']['contragent']['contragentType']) { if($change['order']['contragent']['contragentType']) {
$change['order']['contragentType'] = self::newValue($change['order']['contragent']['contragentType']); $change['order']['contragentType'] = self::newValue($change['order']['contragent']['contragentType']);
unset($change['order']['contragent']); unset($change['order']['contragent']);
} }
if($orders[$change['order']['id']]) { if($orders[$change['order']['id']]) {
$orders[$change['order']['id']] = array_merge($orders[$change['order']['id']], $change['order']); $orders[$change['order']['id']] = array_merge($orders[$change['order']['id']], $change['order']);
} }
else { else {
$orders[$change['order']['id']] = $change['order']; $orders[$change['order']['id']] = $change['order'];
} }
if($change['field'] == 'manager_comment'){ if($change['field'] == 'manager_comment'){
$orders[$change['order']['id']][$change['field']] = $change['newValue']; $orders[$change['order']['id']][$change['field']] = $change['newValue'];
} }
if(($change['field'] != 'status')&& if(($change['field'] != 'status')&&
($change['field'] != 'country')&& ($change['field'] != 'country')&&
($change['field'] != 'manager_comment')&& ($change['field'] != 'manager_comment')&&
@ -890,31 +891,31 @@ class Retailcrm_Retailcrm_Model_Exchange
) { ) {
$orders[$change['order']['id']]['order_edit'] = 1; $orders[$change['order']['id']]['order_edit'] = 1;
} }
if($change['item']) { if($change['item']) {
if($orders[$change['order']['id']]['items'][$change['item']['id']]) { if($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']); $orders[$change['order']['id']]['items'][$change['item']['id']] = array_merge($orders[$change['order']['id']]['items'][$change['item']['id']], $change['item']);
} }
else{ else{
$orders[$change['order']['id']]['items'][$change['item']['id']] = $change['item']; $orders[$change['order']['id']]['items'][$change['item']['id']] = $change['item'];
} }
if(empty($change['oldValue']) && $change['field'] == 'order_product') { if(empty($change['oldValue']) && $change['field'] == 'order_product') {
$orders[$change['order']['id']]['items'][$change['item']['id']]['create'] = 1; $orders[$change['order']['id']]['items'][$change['item']['id']]['create'] = 1;
$orders[$change['order']['id']]['order_edit'] = 1; $orders[$change['order']['id']]['order_edit'] = 1;
unset($orders[$change['order']['id']]['items'][$change['item']['id']]['delete']); unset($orders[$change['order']['id']]['items'][$change['item']['id']]['delete']);
} }
if(empty($change['newValue']) && $change['field'] == 'order_product') { if(empty($change['newValue']) && $change['field'] == 'order_product') {
$orders[$change['order']['id']]['items'][$change['item']['id']]['delete'] = 1; $orders[$change['order']['id']]['items'][$change['item']['id']]['delete'] = 1;
$orders[$change['order']['id']]['order_edit'] = 1; $orders[$change['order']['id']]['order_edit'] = 1;
} }
if(!empty($change['newValue']) && $change['field'] == 'order_product.quantity') { if(!empty($change['newValue']) && $change['field'] == 'order_product.quantity') {
$orders[$change['order']['id']]['order_edit'] = 1; $orders[$change['order']['id']]['order_edit'] = 1;
} }
if(!$orders[$change['order']['id']]['items'][$change['item']['id']]['create'] && $fields['item'][$change['field']]) { if(!$orders[$change['order']['id']]['items'][$change['item']['id']]['create'] && $fields['item'][$change['field']]) {
$orders[$change['order']['id']]['items'][$change['item']['id']][$fields['item'][$change['field']]] = $change['newValue']; $orders[$change['order']['id']]['items'][$change['item']['id']][$fields['item'][$change['field']]] = $change['newValue'];
} }
@ -941,11 +942,11 @@ class Retailcrm_Retailcrm_Model_Exchange
elseif($fields['order'][$change['field']]) { elseif($fields['order'][$change['field']]) {
$orders[$change['order']['id']][$fields['order'][$change['field']]] = self::newValue($change['newValue']); $orders[$change['order']['id']][$fields['order'][$change['field']]] = self::newValue($change['newValue']);
} }
if(isset($change['created'])) { if(isset($change['created'])) {
$orders[$change['order']['id']]['create'] = 1; $orders[$change['order']['id']]['create'] = 1;
} }
if(isset($change['deleted'])) { if(isset($change['deleted'])) {
$orders[$change['order']['id']]['deleted'] = 1; $orders[$change['order']['id']]['deleted'] = 1;
} }
@ -954,7 +955,7 @@ class Retailcrm_Retailcrm_Model_Exchange
return $orders; return $orders;
} }
public static function removeEmpty($inputArray) public static function removeEmpty($inputArray)
{ {
$outputArray = array(); $outputArray = array();
@ -964,15 +965,15 @@ class Retailcrm_Retailcrm_Model_Exchange
if (is_array($element)) { if (is_array($element)) {
$element = self::removeEmpty($element); $element = self::removeEmpty($element);
} }
$outputArray[$key] = $element; $outputArray[$key] = $element;
} }
} }
} }
return $outputArray; return $outputArray;
} }
public static function newValue($value) public static function newValue($value)
{ {
if(isset($value['code'])) { if(isset($value['code'])) {
@ -981,7 +982,7 @@ class Retailcrm_Retailcrm_Model_Exchange
return $value; return $value;
} }
} }
public static function getAllShippingMethodsCode($code) public static function getAllShippingMethodsCode($code)
{ {
$methods = Mage::getSingleton('shipping/config')->getActiveCarriers(); $methods = Mage::getSingleton('shipping/config')->getActiveCarriers();
@ -992,12 +993,12 @@ class Retailcrm_Retailcrm_Model_Exchange
$_code = $_ccode . '_' . $_mcode; $_code = $_ccode . '_' . $_mcode;
$options[$_ccode] = $_code; $options[$_ccode] = $_code;
} }
} }
} }
return $options[$code]; return $options[$code];
} }
} }

View File

@ -103,25 +103,23 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange
} }
$product = array( $product = array(
'productId' => $item->getProductId(),
'productName' => !isset($parent) ? $item->getName() : $parent->getName(), 'productName' => !isset($parent) ? $item->getName() : $parent->getName(),
'quantity' => !isset($parent) ? intval($item->getQtyOrdered()) : intval($parent->getQtyOrdered()), 'quantity' => !isset($parent) ? intval($item->getQtyOrdered()) : intval($parent->getQtyOrdered()),
'initialPrice' => !isset($parent) ? $item->getPrice() : $parent->getPrice(), 'initialPrice' => !isset($parent) ? $item->getPrice() : $parent->getPrice(),
'offer'=>array( 'offer' => array(
'externalId'=>$item->getProductId() 'externalId' => $item->getProductId()
) )
); );
unset($parent); unset($parent);
$items[] = $product; $items[] = $product;
} elseif($item->getProductType() == "grouped") { } elseif ($item->getProductType() == "grouped") {
$product = array( $product = array(
'productId' => $item->getProductId(),
'productName' => $item->getName(), 'productName' => $item->getName(),
'quantity' => $item->getQtyOrdered(), 'quantity' => $item->getQtyOrdered(),
'initialPrice' => $item->getPrice(), 'initialPrice' => $item->getPrice(),
'offer'=>array( 'offer' => array(
'externalId'=>$item->getProductId() 'externalId '=> $item->getProductId()
) )
); );
@ -172,15 +170,15 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange
if (trim($preparedOrder['delivery']['code']) == ''){ if (trim($preparedOrder['delivery']['code']) == ''){
unset($preparedOrder['delivery']['code']); unset($preparedOrder['delivery']['code']);
} }
if (trim($preparedOrder['paymentType']) == ''){ if (trim($preparedOrder['paymentType']) == ''){
unset($preparedOrder['paymentType']); unset($preparedOrder['paymentType']);
} }
if (trim($preparedOrder['status']) == ''){ if (trim($preparedOrder['status']) == ''){
unset($preparedOrder['status']); unset($preparedOrder['status']);
} }
if ($order->getCustomerIsGuest() == 0) { if ($order->getCustomerIsGuest() == 0) {
$preparedCustomer = array( $preparedCustomer = array(
'externalId' => $order->getCustomerId() 'externalId' => $order->getCustomerId()
@ -267,9 +265,9 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange
} }
return true; return true;
} }
/** /**
* Orders export * Orders export
* *
@ -341,18 +339,20 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange
if ($item->getParentItemId()) { if ($item->getParentItemId()) {
$parent = Mage::getModel('sales/order_item')->load($item->getParentItemId()); $parent = Mage::getModel('sales/order_item')->load($item->getParentItemId());
} }
$product = array( $product = array(
'productId' => $item->getProductId(),
'productName' => !isset($parent) ? $item->getName() : $parent->getName(), 'productName' => !isset($parent) ? $item->getName() : $parent->getName(),
'quantity' => !isset($parent) ? intval($item->getQtyOrdered()) : intval($parent->getQtyOrdered()), 'quantity' => !isset($parent) ? intval($item->getQtyOrdered()) : intval($parent->getQtyOrdered()),
'initialPrice' => !isset($parent) ? $item->getPrice() : $parent->getPrice() 'initialPrice' => !isset($parent) ? $item->getPrice() : $parent->getPrice(),
'offer' => array(
'externalId' => $item->getProductId()
)
); );
unset($parent); unset($parent);
$items[] = $product; $items[] = $product;
} }
} }
$shipping = $this->getShippingCode($order->getShippingMethod()); $shipping = $this->getShippingCode($order->getShippingMethod());
$preparedOrder = array( $preparedOrder = array(
'externalId' => $order->getRealOrderId(), 'externalId' => $order->getRealOrderId(),
@ -391,23 +391,23 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange
), ),
) )
); );
if(trim($preparedOrder['delivery']['code']) == ''){ if(trim($preparedOrder['delivery']['code']) == ''){
unset($preparedOrder['delivery']['code']); unset($preparedOrder['delivery']['code']);
} }
if(trim($preparedOrder['paymentType']) == ''){ if(trim($preparedOrder['paymentType']) == ''){
unset($preparedOrder['paymentType']); unset($preparedOrder['paymentType']);
} }
if(trim($preparedOrder['status']) == ''){ if(trim($preparedOrder['status']) == ''){
unset($preparedOrder['status']); unset($preparedOrder['status']);
} }
if ($order->getCustomerIsGuest() != 0) { if ($order->getCustomerIsGuest() != 0) {
$preparedOrder['customer']['externalId'] = $order->getCustomerId(); $preparedOrder['customer']['externalId'] = $order->getCustomerId();
} }
return Mage::helper('retailcrm')->filterRecursive($preparedOrder); return Mage::helper('retailcrm')->filterRecursive($preparedOrder);
} }