Advertisement
Guest User

Untitled

a guest
Sep 28th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. public function stockUpdate(Varien_Event_Observer $observer)
  2. {
  3. # Produto
  4. $event = $observer->getEvent()->getItem()->getProduct();
  5.  
  6. if ($event->getAllnationsSync() == 1) {
  7. # Verifica qual o atributo que representa o ID da All Nations
  8. $control = Mage::getStoreConfig('allnations/general/id_allnations', Mage::app()->getStore());
  9.  
  10. $id = $event->getData($control);
  11. Mage::helper('novapc_allnations')->updateStock($id);
  12. }
  13. }
  14.  
  15. public static function updateStock($codigo = null)
  16. {
  17. try {
  18. # Força o load da página em modo ADMIN
  19. Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
  20.  
  21. # Pega usuario e senha colocados na config
  22. $apiUser = Mage::getStoreConfig('allnations/general/api_user', Mage::app()->getStore());
  23. $apiPassword = Mage::getStoreConfig('allnations/general/api_password', Mage::app()->getStore());
  24.  
  25. # Model da integração, para pegar a data da ultima atualização
  26. $integration = Mage::getModel('novapc_allnations/integration');
  27.  
  28. $productIntegration = $integration->load(2);
  29. $integration = $integration->load(3);
  30.  
  31. # Checa qual é maior, se é a data da integração dos produtos ou da atualização
  32. if ($integration->getUpdatedAt() < $productIntegration->getUpdatedAt()) {
  33. $updatedAt = substr($integration->getUpdatedAt(), 0, 9);
  34. } else {
  35. $updatedAt = substr($productIntegration->getUpdatedAt(), 0, 9);
  36. }
  37.  
  38. # Verifica qual o atributo que representa o ID da All Nations
  39. $control = Mage::getStoreConfig('allnations/general/id_allnations', Mage::app()->getStore());
  40.  
  41. # Instancia o model de estoque
  42. $stockItem = Mage::getModel('cataloginventory/stock_item');
  43.  
  44. # Variavel de controle
  45. $success = 0;
  46.  
  47. # Se for chamado sem o parametro $codigo, atualiza todos os produtos
  48. if ($codigo == null) {
  49. $ANProducts = Mage::getModel('catalog/product')
  50. ->getCollection()
  51. ->addAttributeToSelect('*')
  52. ->addAttributeToFilter('allnations_sync', ['eq' => 1]);
  53.  
  54. foreach ($ANProducts as $single) {
  55. $id = $single->getData($control); // Pega o ID da All Nations
  56. $url = 'http://wspub.allnations.com.br/wsIntEstoqueClientesV2/ServicoReservasPedidosExt.asmx/ConsultaEstoqueProdutos' .
  57. '?CodigoCliente=' . $apiUser .
  58. '&Senha=' . $apiPassword .
  59. '&CodigoProduto=' . $id .
  60. '&Data=' . $updatedAt;
  61.  
  62. $return = Mage::helper('novapc_allnations')->callCurl('GET', $url);
  63. $httpCode = $return['httpCode']['httpCode'];
  64.  
  65. $return = $return['response']['diffgrdiffgram']['NewDataSet']['Estoques'];
  66.  
  67. if ($httpCode == '200') {
  68.  
  69. if ($return['CODIGO'] == $single->getData($control)) {
  70.  
  71. if ($single->getData('allnations_promo') == 0) {
  72. $single->setPrice
  73. ($return['PRECOREVENDA']);
  74. } else {
  75. $single->setSpecialPrice
  76. ($return['PRECOREVENDA']);
  77. }
  78.  
  79. $single->save();
  80.  
  81. $stock = $stockItem->loadByProduct($single->getId());
  82. $stock->setQty($return['ESTOQUEDISPONIVEL']);
  83. $stock->setIsInStock($return['DISPONIVEL']);
  84.  
  85. $stock->save();
  86.  
  87. if ($success != 1) {
  88. $success = 1;
  89. };
  90. }
  91. } else {
  92. return $httpCode;
  93. }
  94. }
  95.  
  96. if ($success == 1) {
  97. $firstUpdate = $integration->getData('first_update');
  98.  
  99. # Checa se ja foi atualizado antes
  100. if (!$firstUpdate) {
  101. $integration->setFirstUpdate(Mage::getModel('core/date')->gmtDate('Y-m-d H:i:s'));
  102. $integration->setUpdatedAt(Mage::getModel('core/date')->gmtDate('Y-m-d H:i:s'));
  103. $integration->save();
  104. } else {
  105. $integration->setUpdatedAt(Mage::getModel('core/date')->gmtDate('Y-m-d H:i:s'));
  106. $integration->save();
  107. }
  108.  
  109. return $httpCode;
  110. }
  111. } else {
  112.  
  113. $url = 'http://wspub.allnations.com.br/wsIntEstoqueClientesV2/ServicoReservasPedidosExt.asmx/ConsultaEstoqueProdutos' .
  114. '?CodigoCliente=' . $apiUser .
  115. '&Senha=' . $apiPassword .
  116. '&CodigoProduto=' . $codigo .
  117. '&Data=' . $updatedAt;
  118.  
  119. $return = Mage::helper('novapc_allnations')->callCurl('GET', $url);
  120. $httpCode = $return['httpCode']['httpCode'];
  121.  
  122. if ($httpCode == '200') {
  123.  
  124. $product = Mage::getModel('catalog/product')
  125. ->loadByAttribute($control, $codigo);
  126.  
  127.  
  128. if ($return['CODIGO'] == $product->getData($control)) {
  129.  
  130. if ($product->getData('allnations_promo') == 0) {
  131. $product->setPrice
  132. ($return['PRECOREVENDA']);
  133. } else {
  134. $product->setSpecialPrice
  135. ($return['PRECOREVENDA']);
  136. }
  137.  
  138. $product->save();
  139.  
  140. $stock = $stockItem->loadByProduct($product->getId());
  141. $stock->setQty($return['ESTOQUEDISPONIVEL']);
  142. $stock->setIsInStock($return['DISPONIVEL']);
  143.  
  144. $stock->save();
  145.  
  146. return $httpCode;
  147. }
  148. }
  149. }
  150. } catch (Exception $e) {
  151. return $e->getMessage();
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement