Search code examples
apimagentointegrationphp-extensioninventory-management

How to fix integration issue Magento store with POS system after site migration?


We migrated our Magento site (Magento 1.8.1.0) from old server to a new server.

But, we can't use the Winepos integration extension any more.

Our site is connected with Winepos system, and this Magento extension had been operated before migrating work.

This is Winepos API manual.

At this time, we think some PHP modules were not installed on our new server.

But, we don't know which PHP modules were not installed. It seems all PHP modules were installed on our new server.

The Magento extension to integrate with Winepos are as follow. This extension is consisted with two files.

Config.xml

<config>
 <global>
   <events>
     <checkout_onepage_controller_success_action>
       <observers>
         <igor_winepos_order_success_observer>
           <type>singleton</type>
           <class>igor_Winepos_Model_Wineposobserver</class>
           <method>checkoutSuccessObserve</method>
         </igor_winepos_order_success_observer>
       </observers>
     </checkout_onepage_controller_success_action>
    </events>
   </global>
  </config>

wineposobserver.php

class igor_Winepos_Model_Wineposobserver extends Varien_Event_Observer {

  function customlog($obj) {
    ob_start();
    var_dump($obj);
    $out1 = ob_get_contents();
    ob_end_clean();
    $f = fopen('/tmp/log.txt', 'ab');
    fwrite($f, $out1);
    fclose($f);
  }

  public function __construct() {
  }

  public function checkoutSuccessObserve($observer) {
    // $event = $observer->getEvent();
    $order_ids = $observer->getData('order_ids');
    if(gettype($order_ids) == 'array' && count($order_ids) == 1) {
       $the_order = Mage::getModel('sales/order')->load($order_ids[0]);
       Mage::helper('globalfunc')->registerOrderWithWinePOSAsynchronousWithTimeout($the_order);
    }
 }
}

I am getting the following in the Apache log:

PHP Warning: PHP Startup: apc.shm_segments setting ignored in MMAP mode in Unknown on line 0 [Sun Apr 30 06:32:30 2017] [notice] Apache/2.2.22 (Ubuntu) mod_ssl/2.2.22 OpenSSL/1.0.1 configured -- resuming normal operations

 285:   function registerOrderWithWinePOSAsynchronousWithTimeout($the_order) {
 286      try {
 287        $items = $the_order->getAllItems();
 ...
 426          $ordered_raw_item = $ordered_products_raw_items[$ordered_product_id];
 428:         $product_winepos_id = trim(strval($product->getResource()->getAttribute('winepos_id')->getFrontend()->getValue($product)));
 429  
 430          $item_element = $doc->createElement('item');
 432          $item_num_element = $doc->createElement('item-num');
 433:         $item_num_element->appendChild($doc->createTextNode(strval($product_winepos_id)));
 434          $item_element->appendChild($item_num_element);
 ...
 466        $the_xml = $doc->saveXML();
 468:       // $post_result = Mage::helper('globalfunc')->post_to_api_winepos('https://wines-in-november.vznlink.com/orders', $the_xml, 'admin276975', '8dc670fb943dc2c0a1415405cdf00e3ec579c4e6', 8, 10);
 470:       return Mage::helper('globalfunc')->delayed_post_to_winepos($the_xml);
 471      } catch(Exception $e) {
 472        $this->customlog($e);
 ...
 475    }

Solution

  • I created a new module to integrate with POS system and implemented inventory synchronization successfully. This work should be done by cron job. To do it, I created three individual script files. Also, POS provider should provide ordered product information including inventory information as txt file via ftp in every time interval.

    lftp -u [username],[password] -e'set ftp:passive-mode false; cd files; put data.txt; quit' [folder name]
    

    The cron job operating blocks are as follow.

    cron_file_mover.php

      $start = microtime(true);
      shell_exec('cp /home/files/data.txt /var/www/vhosts/magento/');
      shell_exec('chown -R www-data:www-data /var/www/vhosts/magento/');
      $end = microtime(true);
      echo 'Run time: '.round($end-$start, 4).'s';
    

    cron_pos_post_script.php

    Utils::initMagento(); $MAX_RETRIES = 5;
    $delayed_jobs = Utils::mageGetRows("select * from delayed_jobs where job_type = 'pos_order' and status = 'todo' order by created_at DESC");
    $current_index = 0;
    foreach($delayed_jobs as $delayed_job) {
      $current_index += 1;
      $retry_count = intval($delayed_job['retry_count']);
      $retry_count += 1;
      $post_result = Mage::helper('globalfunc')->post_to_api_pos('https://vznlink.com/orders', $delayed_job['job_details'], 'admin', 'fd93d2de58ab', 8, 10);  
       Utils::mageSqlExecute("update delayed_jobs set status = 'done' where id = " . $delayed_job['id']);
    
    Utils::mageSqlExecute("update delayed_jobs set status = '" . $new_status . "', retry_count = " . $retry_count . " where id = " . $delayed_job['id']);
    }
    

    cron_pos_update_script.php

    $PATH_TO_FILE = '/var/www/vhosts/magento/data.txt';
    
    function read_pos_file($path_to_file) {
      $min_count_required_for_product = 12;
      $f = fopen($path_to_file, 'rb');
      $text = trim(fread($f, 100000000));
      fclose($f);
      $lines = preg_split('/\r\n|\r|\n/i', $text);
      $products = array();
      foreach($lines as $line) {
      $product = preg_split('/\t/i', trim($line));
      if(count($product) >= $min_count_required_for_product) {
          $product[0] = strval(trim($product[0]));
          $product[1] = strval(trim($product[1]));
          $product[2] = strval(trim($product[2]));
          $product[11] = intval(strval(trim($product[11])));
          $products []= $product;
      }
    }
      return $products;
    }
    
    function update_stock_for_stock_item($product_id, $new_stock) {
       $stock_item = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product_id);
       $stock_item->setData('qty', $new_stock);
       if($new_stock > 0) {
          $stock_item->setData('is_in_stock', 1);
       }
       $stock_item->save();
    }
       $products = read_pos_file($PATH_TO_FILE);
       $total_count = 0; $processed_count = 0;
       foreach($products as $product) {
         $total_count += 1;
         $item_number = $product[0];
         $new_stock = $product[11];
         if($new_stock < 0) {
            $new_stock = 0;
         }
       $products_matching_item_number = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('pos_id')->addFieldToFilter('pos_id', $item_number)->getItems();
       if(count($products_matching_item_number) == 1) {
          $products_matching_item_number = array_values($products_matching_item_number);
          $matching_product = $products_matching_item_number[0];
          $matching_product_id = $matching_product->getId();
          update_stock_for_stock_item($matching_product_id, $new_stock);
          $processed_count += 1;
       }
    }
    

    The cron job settings are as follows.

    10 * * * * /usr/bin/php /var/www/vhosts/magento/pos/cron_winepos_post_script.php &> /dev/null
    10 * * * * /usr/bin/php /var/www/vhosts/magento/pos/cron_winepos_update_script.php &> /dev/null
    20 * * * * /usr/bin/php /var/www/vhosts/magento/pos/cron_pos_file_mover.php &> /dev/null
    

    Keep in mind, checking POS data provided from POS system, cron job setting, checking update script operations.