<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 16-11-1
 * Time: 下午4:32
 * To change this template use File | Settings | File Templates.
 */

require_once ('config.php');
$flag = APPLICATION_PATH . '/../data/log/uploadPack.lock';

if (@file_exists($flag)) {
    echo "Another process is running.\r\n";
    echo "{$flag}\r\n";
    exit();
}

$condition = array(
    'sm_code_Arr'=>array('HKDHL','BH-HKDHL'),
    'customer_code'=>'E0693',//环球
    'order_status'=>4,
    'no_empty_ref_tracking_number'=>1,
    'warehouse_id'=>1,
    'order_mode_type'=>0,
);
$total = Service_Orders::getByCondition($condition,"count(*)");
$pageSize = 100;
$totalPage = ceil($total/$pageSize);
for($page=1;$page<=$totalPage;$page++){
    $rows = Service_Orders::getByCondition($condition, "*", $pageSize, $page, 'order_id desc' );
    foreach($rows as $key=>$value){
        $apiDhl = Service_ApiDhl::getByField($value['order_code'],'order_code',"*");
        if($apiDhl['invoice_status']=="0"||$apiDhl['label_status']=="0"){
            $errorText = "未上传发票或面单";
            $errorLog = array(
                'order_code' => $value['order_code'],
                'sm_code' => $value['sm_code'],
                'content' => $errorText,
            );
            Service_TrackingErrorLog::add($errorLog);
            continue;
        }
        $orderCodeArr = array();
        $orderCodeArr[] = $value['order_code'];
        $orderProcess = new Service_OrderProcess();
        $result = $orderProcess->orderArrPickupTransaction($orderCodeArr, 1,"",1);
        if ($result['pick_code']!='' ){
            $pickRow = Service_Picking::getByField($result['pick_code'],"picking_code","*");
            if ($pickRow['picking_type'] != '0') {
                //一票多件确认下架
                $pickCodes = array();
                $pickCodes[] = $result['pick_code'];
                $obj = new Service_PickProcess();
                $return = $obj->confirmPickerTransaction($pickCodes);
                if($return['state']=="1"){
                    $orderCode = $value['order_code'];
                    $packingCode = "";
                    $packObj = new Service_PackProcess();
                    $return = $packObj->packTransaction($orderCode, $packingCode, 1, 1);
                    if($return['state']=="0"){
                        $errorText = "打包贴标失败:".implode(";",$return['error']);
                        $errorLog = array(
                            'order_code' => $value['order_code'],
                            'sm_code' => $value['sm_code'],
                            'content' => $errorText,
                        );
                        Service_TrackingErrorLog::add($errorLog);
                        continue;
                    }
                }else{
                    $errorText = "确认下架失败:".implode(";",$return['message']);
                    $errorLog = array(
                        'order_code' => $value['order_code'],
                        'sm_code' => $value['sm_code'],
                        'content' => $errorText,
                    );
                    Service_TrackingErrorLog::add($errorLog);
                    continue;
                }
                //订单变成已录单状态
                $updateOrder = array(
                    'order_status' => '14',
                    'update_time' => date('Y-m-d H:i:s', time()),
                );
                Service_Orders::update($updateOrder, $value['order_code'], 'order_code');
                //添加日志
                $orderLog = array(
                    'order_id' => $value['order_id'],
                    'order_code' => $value['order_code'],
                    'ol_type' => '0',
                    'order_status_from' => $value['order_status'],
                    'order_status_to' => '14',
                    'ol_add_time' => date("Y-m-d H:i:s"),
                    'user_id' => 0,
                    'ol_ip' => Common_Common::getIP(),
                    'ol_comments' => '环球订单自动下架打包操作',
                );
                Service_OrderLog::add($orderLog);
            }else{
                //单票打包
                $pickingCode = $result['pick_code'];
                $orderProduct = Service_OrderProduct::getByCondition(array('order_code'=>$value['order_code']),"*");
                $productBarcode = $orderProduct[0]['product_barcode'];
                $packageCode = "";
                $packObj = new Service_PackProcess();
                $return = $packObj->assignOrderTransaction($pickingCode,$packageCode,$productBarcode,1);
                if($return['state']=="0"){
                    $errorText = "单票打包失败:".implode(";",$return['error']);
                    $errorLog = array(
                        'order_code' => $value['order_code'],
                        'sm_code' => $value['sm_code'],
                        'content' => $errorText,
                    );
                    Service_TrackingErrorLog::add($errorLog);
                    continue;
                }
                //订单变成已录单状态
                $updateOrder = array(
                    'order_status' => '14',
                    'update_time' => date('Y-m-d H:i:s', time()),
                );
                Service_Orders::update($updateOrder, $value['order_code'], 'order_code');
                //添加日志
                $orderLog = array(
                    'order_id' => $value['order_id'],
                    'order_code' => $value['order_code'],
                    'ol_type' => '0',
                    'order_status_from' => $value['order_status'],
                    'order_status_to' => '14',
                    'ol_add_time' => date("Y-m-d H:i:s"),
                    'user_id' => 0,
                    'ol_ip' => Common_Common::getIP(),
                    'ol_comments' => '环球订单自动下架打包操作',
                );
                Service_OrderLog::add($orderLog);
            }
        }else{
            $errorText = isset($return['errors'][0]) ? $return['errors'][0]['message'] :'没有找到未下架订单';
            $errorLog = array(
                'order_code' => $value['order_code'],
                'sm_code' => $value['sm_code'],
                'content' => "下架失败:".$errorText,
            );
            Service_TrackingErrorLog::add($errorLog);
            continue;
        }
    }
}


echo "[" . date('Y-m-d H:i:s') . "] End run\r\n";
@unlink($flag);
exit;