1

I am using a Cron job to export orders to a third party warehouse system every two hours via FTP which has been working fine.

The problem is I need to change the order status once the FTP upload has completed to prevent duplicate orders being sent across.

Currently the Cron runs every two hours and checks for orders with status "ready to ship" then adds the orders to a CSV file and uploads it to FTP but for some reason the order status doesn't change so when the Cron runs again it resends the same orders to FTP causing duplicates.

We also PayPal IPN so all new orders get set to "ready to ship" when the funds have been captured.

Here's my Cron.php file

      private static function exportOrdersToCSV()
        {
            Mage::log('exportOrdersToCSV');

            // Make CSV filename
            $path = Mage::getBaseDir('var') . DS . 'orders' . DS;

            $date = new DateTime(null, new DateTimeZone('Europe/London'));
            $filename = $path. $date->format('Y-m-d-H'). '_ilg_feed.csv';
            //$filename = $path. date('Y-m-d-H'). '_ilg_feed.csv';

            // Select orders to export
            $orders = Mage::getModel('sales/order')->getCollection()
                    ->addFieldToFilter('status', 'ready_to_ship');

            $order_str = 'DATE,ORDER REF,CONTACT NAME,COMPANY,ADDRESS1,ADDRESS2,ADDRESS3,TOWN,COUNTY,POSTCODE,COUNTRY,COUNTRY CODE,PRODUCT CODE,QTY,CUSTOMER REFERENCE,TELEPHONE,MOBILENO,COMMENTS,EMAIL,SHIPPERS REF,DELIVERY TYPE,ORDER ORIGIN,CUSTOMS VALUE,DELIVERY INSTRUCTIONS,OPTION1,OPTION2,OPTION3,OPTION4,OPTION5,OPTION6,OPTION7,OPTION8,OPTION9,OPTION10'. "\n";
            $orders_ary = array();
            foreach ($orders as $order) {
                $orders_ary[] = $order->getEntityId();
                $shippingAddress = $order->getShippingAddress();

                $date = explode(' ', $order->getCreatedAtFormated('short'));
                $order_info1 = array(
                    $date[0],
                    $order->getEntityId(),
                    $shippingAddress->getName(),
                    $shippingAddress->getCompany(),
                    $shippingAddress->getStreet(1),
                    $shippingAddress->getStreet(2),
                    $shippingAddress->getStreet(3),
                    $shippingAddress->getCity(),
                    $shippingAddress->getRegion(),
                    $shippingAddress->getPostcode(),
                    $shippingAddress->getCountry(),
                    $shippingAddress->getCountryId()
                );

                // Set delivery type
                $delivery_type = $order->getShippingMethod() === 'flatrate_flatrate' ? 'ND' : 'RMT'; // next day or Royal Mail
                $order_info2 = array(
                    $order->getCustomerId(),
                    (strlen($shippingAddress->getTelephone()) > 1) ? $shippingAddress->getTelephone() : $order->getPhone(),
                    '', // mobile
                    '', // comments
                    $shippingAddress->getEmail(),
                    '', // shippers ref
                    $delivery_type, // delivery type
                    'WEB',  // order origin
                );
                $order_info3 = array(
                    '', // delivery instructions
                    '','','','','','','','','',''   // OPTION1-OPTION10
                );

                $items = $order->getItemsCollection(array(), true);
                foreach ($items AS $item) {
                    //Mage::log('Adding product #'. $item->getSku(). ' to order '. $order->getEntityId());
                    $product = array(
                        $item->getSku(),
                        intval($item->getQtyOrdered())
                    );
                    $customs_value = array('');

                    $ary = array_merge($order_info1, $product, $order_info2, $customs_value, $order_info3);
                    $order_str .= '"'. implode('","', $ary). '"'. "\n";
                }
            }

            if (count($orders_ary) === 0) {
                return null;
            }

            // Save to file
            $ret = array();
            if (file_put_contents($filename, $order_str) !== false) {
                $ret['orders'] = array_unique($orders_ary);
                $ret['filename'] = $filename;
            }

            return $ret;
        } 

 /**
     * FTP orders CSV file to 3PL FTP server
     */
    private static function ftpOrdersTo3PL($orders_ary)
    {
        Mage::log('ftpOrdersTo3PL');
        $filename = $orders_ary['filename'];

        if (!$filename) {
            throw new \Exception('FTP CSV filename not specified');
        }

        if (!file_exists($filename)) {
            throw new \Exception('FTP CSV file '. $filename. ' not found');
        }        

        $ftp_server = self::FTP_SERVER;
        $ftp_user = self::FTP_USER;
        $ftp_password = self::FTP_PASSWORD;

        //if (!($ftp = ftp_ssl_connect($ftp_server))) {
        if (!($ftp = ftp_connect($ftp_server))) {
            throw new \Exception('FTP connection to '. $ftp_server. ' failed');
        }

        if (!ftp_login($ftp, $ftp_user, $ftp_password)) {
            ftp_close($ftp);
            throw new \Exception('FTP login as '. $ftp_user. ' failed');
        }

        if (!ftp_pasv($ftp, true)) {
            ftp_close($ftp);
            throw new \Exception('FTP failed to switch to passive mode');
        }

        $remote_filename = self::FTP_CSV_DIR. DS. basename($filename);
        if (!ftp_put($ftp, $remote_filename, $filename, FTP_BINARY)) {
            ftp_close($ftp);
            throw new \Exception('FTP failed to put '. $filename. ' to '. $remote_filename);
        }

        ftp_close($ftp);

        // If the file upload succeeds then change the status of the orders to indicate they went to the 3PL
        if (is_array($orders_ary['orders'])) {
            $auth_token = self::getBrightpearlAuthToken();        


            foreach ($orders_ary['orders'] as $orderid) {
                Mage::log('Updating order #'. $orderid. ' status to packing');
                $order = Mage::getModel('sales/order')->load($orderid);
                $incrementid = $order->getIncrementId();
                $order->setState('processing', 'packing', 'Order sent to 3PL for packing', false)->save();

                // Update the order status in Brightpearl
                if (($bp_orderid = self::getBrightpearlOrderId($auth_token, $incrementid)) !== null) {
                    Mage::log('Updating status for BP order '. $bp_orderid. ' ('. $incrementid. ')');
                    self::setBrightpearlOrderStatus($auth_token, $bp_orderid);
                }
            }
        }
    }

The line where order status should change isn't firing and I can't figure out what the problem is and could do with some help here please...!

enter image description here

exception.log

Next exception 'Zend_Db_Statement_Exception' with message 'SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction' in /chroot/home/home/testwebsite.com/html/lib/Zend/Db/Statement/Pdo.php:234
Stack trace:
#0 /chroot/home/home/testwebsite.com/html/lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)
#1 /chroot/home/home/testwebsite.com/html/app/code/core/Zend/Db/Statement.php(291): Varien_Db_Statement_Pdo_Mysql->_execute(Array)
#2 /chroot/home/home/testwebsite.com/html/lib/Zend/Db/Adapter/Abstract.php(479): Zend_Db_Statement->execute(Array)
#3 /chroot/home/home/testwebsite.com/html/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('UPDATE `index_p...', Array)
#4 /chroot/home/home/testwebsite.com/html/lib/Varien/Db/Adapter/Pdo/Mysql.php(428): Zend_Db_Adapter_Pdo_Abstract->query('UPDATE `index_p...', Array)
#5 /chroot/home/home/testwebsite.com/html/lib/Zend/Db/Adapter/Abstract.php(632): Varien_Db_Adapter_Pdo_Mysql->query('UPDATE `index_p...', Array)
#6 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Index/Model/Resource/Process.php(137): Zend_Db_Adapter_Abstract->update('index_process', Array, Array)
#7 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Index/Model/Resource/Process.php(150): Mage_Index_Model_Resource_Process->_updateProcessData('8', Array)
#8 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Index/Model/Process.php(277): Mage_Index_Model_Resource_Process->updateProcessStartDate(Object(Mage_Index_Model_Process))
#9 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Index/Model/Process.php(638): Mage_Index_Model_Process->processEvent(Object(Mage_Index_Model_Event))
#10 [internal function]: Mage_Index_Model_Process->safeProcessEvent(Object(Mage_Index_Model_Event))
#11 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Index/Model/Indexer.php(366): call_user_func_array(Array, Array)
#12 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Index/Model/Indexer.php(246): Mage_Index_Model_Indexer->_runAll('safeProcessEven...', Array)
#13 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Index/Model/Indexer.php(317): Mage_Index_Model_Indexer->indexEvent(Object(Mage_Index_Model_Event))
#14 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/CatalogInventory/Model/Stock/Item.php(792): Mage_Index_Model_Indexer->processEntityAction(Object(Plumrocket_Cartreservation_Model_Override_CatalogInventory_Stock_Item), 'cataloginventor...', 'save')
#15 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Core/Model/Abstract.php(319): Mage_CatalogInventory_Model_Stock_Item->_afterSave()
#16 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/CatalogInventory/Model/Observer.php(154): Mage_Core_Model_Abstract->save()
#17 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Core/Model/App.php(1338): Mage_CatalogInventory_Model_Observer->saveInventoryData(Object(Varien_Event_Observer))
#18 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Core/Model/App.php(1317): Mage_Core_Model_App->_callObserverMethod(Object(Mage_CatalogInventory_Model_Observer), 'saveInventoryDa...', Object(Varien_Event_Observer))
#19 /chroot/home/home/testwebsite.com/html/app/Mage.php(447): Mage_Core_Model_App->dispatchEvent('catalog_product...', Array)
#20 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Core/Model/Abstract.php(466): Mage::dispatchEvent('catalog_product...', Array)
#21 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Catalog/Model/Product.php(549): Mage_Core_Model_Abstract->_afterSave()
#22 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Core/Model/Abstract.php(319): Mage_Catalog_Model_Product->_afterSave()
#23 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Catalog/Model/Product/Api/V2.php(194): Mage_Core_Model_Abstract->save()
#24 [internal function]: Mage_Catalog_Model_Product_Api_V2->update('1412', Object(stdClass), NULL, NULL)
#25 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Api/Model/Server/Handler/Abstract.php(296): call_user_func_array(Array, Array)
#26 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Api/Model/Server/V2/Handler.php(57): Mage_Api_Model_Server_Handler_Abstract->call('ae111f981dbfadd...', 'product.update', Array)
#27 [internal function]: Mage_Api_Model_Server_V2_Handler->__call('catalogProductU...', Array)
#28 [internal function]: Mage_Api_Model_Server_V2_Handler->catalogProductUpdate('ae111f981dbfadd...', '1412', Object(stdClass), NULL, NULL)
#29 /chroot/home/home/testwebsite.com/html/lib/Zend/Soap/Server.php(832): SoapServer->handle('<?xml version="...')
#30 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Api/Model/Server/V2/Adapter/Soap.php(70): Zend_Soap_Server->handle()
#31 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Api/Model/Server.php(138): Mage_Api_Model_Server_V2_Adapter_Soap->run()
#32 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Api/controllers/V2/SoapController.php(46): Mage_Api_Model_Server->run()
#33 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Core/Controller/Varien/Action.php(418): Mage_Api_V2_SoapController->indexAction()
#34 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(250): Mage_Core_Controller_Varien_Action->dispatch('index')
#35 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Core/Controller/Varien/Front.php(172): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#36 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Core/Model/App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#37 /chroot/home/home/testwebsite.com/html/app/Mage.php(683): Mage_Core_Model_App->run(Array)
#38 /chroot/home/home/testwebsite.com/html/index.php(87): Mage::run('', 'store')
#39 {main}
2014-06-05T07:31:18+00:00 ERR (3): 
exception 'PDOException' with message 'SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction' in /chroot/home/home/testwebsite.com/html/lib/Zend/Db/Statement/Pdo.php:228
Stack trace:
#0 /chroot/home/home/testwebsite.com/html/lib/Zend/Db/Statement/Pdo.php(228): PDOStatement->execute(Array)
#1 /chroot/home/home/testwebsite.com/html/lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)
#2 /chroot/home/home/testwebsite.com/html/app/code/core/Zend/Db/Statement.php(291): Varien_Db_Statement_Pdo_Mysql->_execute(Array)
#3 /chroot/home/home/testwebsite.com/html/lib/Zend/Db/Adapter/Abstract.php(479): Zend_Db_Statement->execute(Array)
#4 /chroot/home/home/testwebsite.com/html/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('UPDATE `index_e...', Array)
#5 /chroot/home/home/testwebsite.com/html/lib/Varien/Db/Adapter/Pdo/Mysql.php(428): Zend_Db_Adapter_Pdo_Abstract->query('UPDATE `index_e...', Array)
#6 /chroot/home/home/testwebsite.com/html/lib/Zend/Db/Adapter/Abstract.php(632): Varien_Db_Adapter_Pdo_Mysql->query('UPDATE `index_e...', Array)
#7 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Core/Model/Resource/Db/Abstract.php(433): Zend_Db_Adapter_Abstract->update('index_event', Array, 'event_id='5776'')
#8 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Core/Model/Abstract.php(318): Mage_Core_Model_Resource_Db_Abstract->save(Object(Mage_Index_Model_Event))
#9 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Index/Model/Indexer.php(333): Mage_Core_Model_Abstract->save()
#10 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/CatalogInventory/Model/Stock/Item.php(792): Mage_Index_Model_Indexer->processEntityAction(Object(Plumrocket_Cartreservation_Model_Override_CatalogInventory_Stock_Item), 'cataloginventor...', 'save')
#11 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Core/Model/Abstract.php(319): Mage_CatalogInventory_Model_Stock_Item->_afterSave()
#12 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/CatalogInventory/Model/Stock/Item/Api/V2.php(61): Mage_Core_Model_Abstract->save()
#13 [internal function]: Mage_CatalogInventory_Model_Stock_Item_Api_V2->update('2430', Object(stdClass))
#14 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Api/Model/Server/Handler/Abstract.php(296): call_user_func_array(Array, Array)
#15 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Api/Model/Server/V2/Handler.php(57): Mage_Api_Model_Server_Handler_Abstract->call('fa4880704a9bbc9...', 'product_stock.u...', Array)
#16 [internal function]: Mage_Api_Model_Server_V2_Handler->__call('catalogInventor...', Array)
#17 [internal function]: Mage_Api_Model_Server_V2_Handler->catalogInventoryStockItemUpdate('fa4880704a9bbc9...', '2430', Object(stdClass))
#18 /chroot/home/home/testwebsite.com/html/lib/Zend/Soap/Server.php(832): SoapServer->handle('<?xml version="...')
#19 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Api/Model/Server/V2/Adapter/Soap.php(70): Zend_Soap_Server->handle()
#20 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Api/Model/Server.php(138): Mage_Api_Model_Server_V2_Adapter_Soap->run()
#21 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Api/controllers/V2/SoapController.php(46): Mage_Api_Model_Server->run()
#22 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Core/Controller/Varien/Action.php(418): Mage_Api_V2_SoapController->indexAction()
#23 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(250): Mage_Core_Controller_Varien_Action->dispatch('index')
#24 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Core/Controller/Varien/Front.php(172): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#25 /chroot/home/home/testwebsite.com/html/app/code/core/Mage/Core/Model/App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#26 /chroot/home/home/testwebsite.com/html/app/Mage.php(683): Mage_Core_Model_App->run(Array)
#27 /chroot/home/home/testwebsite.com/html/index.php(87): Mage::run('', 'store')
#28 {main}

closed as off-topic by Flyingmana, Marius Jan 28 '15 at 20:46

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced OR seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Flyingmana, Marius
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Have you created the state 'packing' in the custom order statuses? – B00MER Jun 5 '14 at 9:20
  • @B00MER Hi, yes we have 'packing' as an order status see screenshot... – user1704524 Jun 5 '14 at 9:32
  • If you have logging enabled, are there any exceptions or such in var/log exceptions.log or system.log ? – B00MER Jun 5 '14 at 9:35
  • @B00MER the exception.log does show some deadlocks, could that be part of the problem? – user1704524 Jun 5 '14 at 9:57
  • Are any order statuses being set at all? If not then the deadlock is most likely not related. stackoverflow.com/questions/13309341/magento-deadlocks – B00MER Jun 5 '14 at 10:06