Magento 插件StoreCredit 做退货退款插件处理

Magento 插件StoreCredit 做退货退款插件处理

其中的一个功能要求,creditmemo 与新下单做关联。就是将退的钱用做哪个新的订单。

处理方法 遍历creditmemo 与 amasty_storecredit_customer_balance_history

判断 sales_flat_creditmemo表的 base_am_amount_total_refunded,am_amount_total_refunded 字段的值是否为NULL。 为空代表没有退到amasty_storecredit_customer_balance_history

处理方式: 写一个脚本,运行定时任务,

代码如下:

<?php
/**
 * Created by PhpStorm.
 * User: sun_lollicup
 * Date: 2019/7/2
 * Time: 13:52
 */

error_reporting(E_ALL);
ini_set('display_errors', 'On');
set_time_limit(0);

require 'app/Mage.php';
if (!Mage::isInstalled()) {
    echo "Application is not installed yet, please complete install wizard first.";
    exit;
}

Mage::app('admin')->setUseSessionInUrl(false);
umask(0);

Mage::log('start time: ' . date('Y-m-d H:i:s'), null, 'balance.log');

//database read adapter
$read = Mage::getSingleton('core/resource')->getConnection('core_read');

//database write adapter
$write = Mage::getSingleton('core/resource')->getConnection('core_write');

$balanceHistory = Mage::getModel('amstcred/balanceHistory')
    ->getCollection()
    ->addFieldToFilter('is_relation_order', 0)
    ->addFieldToFilter('action', 2)
    ->getData();


if(!empty($balanceHistory)){
    Mage::log('Refund of order count: '.count($balanceHistory), null, 'balance.log');
    foreach ($balanceHistory  as $i => $j) {
        $balance_id    = intval($j['balance_id']);
        $balanceList   = array();
        $old_order_id  = "";

        // $balanceList = $read->fetchAll("select * from amasty_storecredit_customer_balance_history where balance_id = ".$balance_id." and action in (1,2) order by history_id asc" );
        //$balanceList  = $read->fetchAll("select * from amasty_storecredit_customer_balance_history where balance_id = 950  and action in (1,2) order by history_id asc");
        // $balanceList = $read->fetchAll("select * from amasty_storecredit_customer_balance_history where balance_id = 5005  and action in (1,2) order by history_id asc");
        // $balanceList = $read->fetchAll("select * from amasty_storecredit_customer_balance_history where balance_id = 5103 and action in (1,2) order by history_id asc");
        // $balanceList = $read->fetchAll("select * from amasty_storecredit_customer_balance_history where balance_id = 272 and action in (1,2) order by history_id asc");
        // $balanceList = $read->fetchAll("select * from amasty_storecredit_customer_balance_history where balance_id = 4872 and action in (1,2) order by history_id asc");
        $balanceList = $read->fetchAll("select * from amasty_storecredit_customer_balance_history where balance_id = 2337 and action in (1,2) order by history_id asc");
        //$balanceList = $read->fetchAll("select * from amasty_storecredit_customer_balance_history where balance_id = 5139 and action in (1,2) order by history_id asc");
       // $balanceList = $read->fetchAll("select * from amasty_storecredit_customer_balance_history where balance_id = 5148 and action in (1,2) order by history_id asc");
         //$balanceList = $read->fetchAll("select * from amasty_storecredit_customer_balance_history where balance_id = 5156 and action in (1,2) order by history_id asc");



        Mage::log('balance_id: ' . $balance_id, null, 'balance.log');

        foreach ($balanceList as $v => $k) {
            $historyId = $k['history_id'];
            if ($k['action'] == 2 && $k['is_relation_order'] == 0) {    //undo  creditmemo
                $data         = unserialize($k['operation_data']);
                $increment_id = $data[0];
                Mage::log('order for increment_id: ' . $increment_id, null, 'balance.log');
                // $orderInfo = Mage::getModel('sales/order')->loadByIncrementId($increment_id)->getData();
                // $order_id  = $orderInfo['entity_id'];
                // $order_id  = $orderInfo['entity_id'] = 72298;
                // $order_id  = $orderInfo['entity_id'] = 70859;
                // $order_id  = $orderInfo['entity_id'] = 70519;
                // $order_id  = $orderInfo['entity_id'] = 68603;
                // $order_id  = $orderInfo['entity_id'] = 68978;
                $order_id  = $orderInfo['entity_id'] = 69719;
                //$order_id  = $orderInfo['entity_id'] = 71135;
                //$order_id  = $orderInfo['entity_id'] = 74511;
                //$order_id  = $orderInfo['entity_id'] = 74847;

                Mage::log('order for entity_id: ' . $order_id, null, 'balance.log');

                $collection  = Mage::getModel('sales/order_creditmemo')
                    ->getCollection()
                    ->addFieldToFilter('order_id', $order_id)
                   // ->addFieldToFilter('transaction_id', array('null' => 'transaction_id'));
                    ->addFieldToFilter('base_am_amount_total_refunded',  array('notnull' => 'base_am_amount_total_refunded'))
                    ->addFieldToFilter('am_amount_total_refunded',  array('notnull' => 'am_amount_total_refunded'));

                Mage::log('sales_flat_creditmemo  select: ' . (string)$collection ->getSelect(), null, 'balance.log');
                $creditmemoList = $collection->getData();

                if(empty($creditmemoList)) {
                    Mage::log('sales_flat_creditmemo for total: ' . count($creditmemoList), null, 'balance.log');
                }else {
                    Mage::log('sales_flat_creditmemo for total: ' . count($creditmemoList), null, 'balance.log');
                    if(count($creditmemoList) == 1){
                        foreach ($creditmemoList as $value => $key) {
                            $id = $key['entity_id'];
                        }

                        Mage::log('sales_flat_creditmemo for entity_id:'. $id, null, 'balance.log');
                        // check next one data  of action is 1 or 2 or 3,when eq 1
                        $V = $v+1;
                        if ($balanceList[$V]['action'] == 1) {   //new web order
                            $operation_data    = unserialize($balanceList[$V]['operation_data']);
                            $incrementId       = $operation_data[0];
                            $store_credit_used = abs($balanceList[$V]['balance_delta']);
                            $model = Mage::getModel('sales/order_creditmemo')->load($id);
                            $model->setNewOrderId($incrementId);
                            $model->setStoreCreditUsed($store_credit_used);

                            Mage::log('sales_flat_creditmemo  select: ' . (string)$model ->getSelect(), null, 'balance.log');
                            $query = $model->save();
                            Mage::log('new_order_id: ' . $incrementId . '------store_credit_used: ' . $store_credit_used, null, 'balance.log');
                            Mage::log('sql: ' . $query->toString(), null, 'balance.log');

                            $write->update(
                                "amasty_storecredit_customer_balance_history",
                                array("is_relation_order" => 1),
                                "history_id=" . $historyId
                            );
                        }
                    }else {
                        $V = $v+1;
                        //the same order_id not do ,update history
                        if($old_order_id == $order_id) {
                            $write->update(
                                "amasty_storecredit_customer_balance_history",
                                array("is_relation_order" => 1),
                                "history_id=" . $historyId
                            );
                        } else{
                            //the same order_id  ,but update only one  historyId
                            foreach ($creditmemoList as $key => $value) {
                                echo  $id = $value['entity_id'] ."+++++++++++++++".PHP_EOL;
                                Mage::log('sales_flat_creditmemo for entity_id:'. $id, null, 'balance.log');
                                // check next one data  of action is 1 or 2 or 3,when eq 1
                                if ($balanceList[$V]['action'] == 1) {
                                    $operation_data = unserialize($balanceList[$V]['operation_data']);

                                    $incrementId = $operation_data[0];
                                    $store_credit_used = abs($balanceList[$V]['balance_delta']);

                                    $model = Mage::getModel('sales/order_creditmemo')->load($id);
                                    $model->setNewOrderId($incrementId);
                                    $model->setStoreCreditUsed($store_credit_used);

                                    Mage::log('sales_flat_creditmemo  select: ' . (string)$model ->getSelect(), null, 'balance.log');

                                    $query = $model->save();
                                    Mage::log('new_order_id: ' . $incrementId . '------store_credit_used: ' . $store_credit_used, null, 'balance.log');
                                    Mage::log('sql: ' . $query->toString(), null, 'balance.log');


                                    $write->update(
                                        "amasty_storecredit_customer_balance_history",
                                        array("is_relation_order" => 1),
                                        "history_id=" . $historyId
                                    );
                                    $V = $V+2;   // get payment  action =1
                                }

                            }

                            $old_order_id = $order_id;
                        }
                    }
                }

            }
        }
    }
}else{

    Mage::log('Refund of order count: '.count($balanceHistory), null, 'balance.log');
}

Mage::log('end time: '.date('Y-m-d H:i:s'), null, 'balance.log');

Leave a comment

您的电子邮箱地址不会被公开。 必填项已用 * 标注