SyncHistoryOrderInfoToGrid.php

 

SyncHistoryOrderInfoToGrid.php

```

<?php
chdir(dirname(__FILE__));
require_once 'abstract.php';

class Mage_Shell_SyncHistoryOrderInfoToGrid extends Mage_Shell_Abstract
{
    const PAGE_SIZE = 50;
    public function getAllOrderPageCount()
    {
        //$stores = Mage::getModel('core/store')->getCollection();
        $allCount = Mage::getResourceModel('sales/order_collection')
            //->addAttributeToFilter('created_at', array(
            //    'from' => '2017-03-02',
            //    'to' => '2017-04-01',
            //))
            ->addAttributeToSelect('increment_id')
            ->count();
        return ceil($allCount/self::PAGE_SIZE);
        //echo 33;return;
    }

    public function getOrderCollection($current_num)
    {
        $orderCollection = Mage::getResourceModel('sales/order_collection')
            //->addAttributeToFilter('created_at', array(
            //    'from' => '2017-03-02',
            //    'to' => '2017-04-01',
            //))
            ->setPage($current_num, self::PAGE_SIZE)
            ->addAttributeToSelect('increment_id')
            ->addAttributeToSelect('netsuite_internal_id');
        return $orderCollection;
    }


    public function run()
    {
        $params = array_keys($this->_args);
        if (isset($this->_args['getAllOrderPageCount'])) {
            echo $this->getAllOrderPageCount();
            return;

        } else if (isset($this->_args['syncOrderByPageNum'])) {
            $argv = $_SERVER['argv'];
            $current_num = $argv[2];
            //echo $current_num;return;
            $orderCollection = $this->getOrderCollection($current_num);
            $connection       = $this->_getConnection('core_write');
            $tablename = 'sales_flat_order_grid';
            foreach ($orderCollection as $order) {
                $increment_id = $order['increment_id'];
                $netsuite_internal_id = $order['netsuite_internal_id'];
                if ($netsuite_internal_id) {
                    $sql = "UPDATE " . $this->_getTableName($tablename) . " order_grid
                    SET  order_grid.netsuite_internal_id = ?
                     WHERE  order_grid.increment_id = ?
                 ";

                    $connection->query($sql, array($netsuite_internal_id, $increment_id));

                    // update
                    //$orderGrid = Mage::getModel('sales/order_grid')->loadByAttribute($increment_id, 'increment_id');
                    // $orderGrid = Mage::getModel('sales/order_grid')->load($increment_id, 'increment_id');
                    //$orderGrid['netsuite_internal_id'] = $netsuite_internal_id;
                    //$orderGrid->save();
                    echo "update order grid success, increment_id:".$increment_id.", netsuite_internal_id:".$netsuite_internal_id."\n";

                } else {
                    echo "update order grid fail, increment_id:".$increment_id.", netsuite_internal_id:".$netsuite_internal_id."\n";

                }
            }
            return;
        }
    }

    //得到magento的鏈接,用於操作數據庫。
    function _getConnection($type = 'core_read'){
        return Mage::getSingleton('core/resource')->getConnection($type);
    }
    //得到table的name
    function _getTableName($tableName){
        return Mage::getSingleton('core/resource')->getTableName($tableName);
    }

}

$shell = new Mage_Shell_SyncHistoryOrderInfoToGrid();
$shell->run();

```

 

syncHistoryOrderInfoToGrid.sh

 

```

#!/bin/bash
# location of the php binary    /usr/bin/php   /usr/local/php/bin/php

echo "Begin sync hisotry order netsuite_internal_id to order grid table"
variable2=`/usr/bin/php -f SyncHistoryOrderInfoToGrid.php getAllOrderPageCount`
echo "there are $variable2 pages to sync "
#for (( i=1; i<=$variable2; i++ ))  # sudo dpkg-reconfigure dash  -> no
for i in `seq $variable2`  # ubuntu
do
   /usr/bin/php -f SyncHistoryOrderInfoToGrid.php syncOrderByPageNum $i
   echo "Page $i done"
done

```

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章