<?php
/**
 * Created by JetBrains PhpStorm.
 * User: user
 * Date: 15-6-19
 * Time: 上午10:02
 * To change this template use File | Settings | File Templates.
 */
require_once ('config.php');

$flagFile = APPLICATION_PATH . '/../data/log/receivingFenxiao.lock';

if (@file_exists($flagFile)) {
    echo 'Another process is running.'."\r\n";
    echo $flagFile."\r\n";
    exit();
}
$link = fopen($flagFile, 'wb');
fclose($link);
define('RUNTIME', '[' . date('Y-m-d H:i:s') . '] ');
echo RUNTIME . "Starting.\r\n\r\n";

$httpURL = "http://api.sinob2b.com/rs/oms/receivingOrder/updateReceivingOrderStatus";
$postHeader = array("Content-Type: application/x-www-form-urlencoded; charset=UTF-8");
$appSercet = "1747dfbf884ac612e050007f01000ac5";
$appKey = "1747dfbf884ac612e050007f01000ac5";
$receivingCondition = array('fmt_code' => 'ASNWCSH-TO-FX', 'fml_status' => 1);
$counts = Service_FtpMessageSendlist::getByCondition($receivingCondition, "count(*)");
$pageSize = 20;
if ($counts > 0){
    $pages = ceil($counts / $pageSize);
    for ($page = 1; $page <= $pages; $page++){
        $sendList = Service_FtpMessageSendlist::getByCondition($receivingCondition, '*', $pageSize, $page, 'fml_id desc');
        foreach($sendList as $row){
            $asnNumber = $row['refercence_no'];
            $receivingRow = Service_Receiving::getByField($asnNumber,'receiving_code',"*");
            if($receivingRow['receiving_status']<7) continue;
            $receivingAttribute = Service_ReceivingAttribute::getByField($asnNumber,'receiving_code',"*");
            if($receivingRow['receive_model_type']=="0"){
                if($receivingAttribute['shelf_time']=="0000-00-00 00:00:00"||$receivingAttribute['shelf_time']=="") continue;
                $receivedTime = $receivingAttribute['shelf_time'];
            }else{
                $receivedTime = $receivingAttribute['receiving_time'];
            }
            $rohStatus = "DONE";

            $param = array(
                'timestamp'=>strtotime($receivedTime),
                'asnNumber'=>$asnNumber,
                'rohStatus'=>'DONE',
                'receivedTime'=>$receivedTime,
                'receivedWeight'=>$receivingRow['netweight']
            );
            $items = array();
            if($receivingRow['receive_model_type']=="1"){
                $receivingOrderDetail = Service_ReceivingOrderDetail::getByCondition(array('receiving_code'=>$asnNumber),"*");
                foreach($receivingOrderDetail as $rod){
                    $orderProduct = Service_OrderProduct::getByCondition(array('order_code'=>$rod['order_code']),"*");
                    foreach($orderProduct as $op){
                        $product = Service_Product::getByField($op['product_id'],"product_id","*");
                        $ite = array(
                            'sku'=>$product['product_sku'],
                            'quantity'=>$op['op_quantity'],
                            'weight'=>$product['product_weight'],
                            'qc'=>'Y'
                        );
                        $items[] = $ite;
                    }
                }
            }else{
                $receivingDetail = Service_ReceivingDetail::getByCondition(array('receiving_code'=>$asnNumber),"*");
                foreach($receivingDetail as $key=>$value){
                    $product = Service_Product::getByField($value['product_id'],"product_id","*");
                    $ite = array(
                        'sku'=>$product['product_sku'],
                        'quantity'=>$value['rd_receiving_qty'],
                        'weight'=>$product['product_weight'],
                        'qc'=>'Y'
                    );
                    $items[] = $ite;
                }
            }

            $shalArr = array();
            $shalArr[] = "appKey".$appKey;
            foreach($param as $key=>$val){
                $shalArr[] = $key.$val;
            }
            foreach($items as $k=>$v){
                foreach($v as $key=>$val){
                    $shalArr[] = "items.".$key.$val;
                }
            }
            sort($shalArr);
            $shalStr = "";
            foreach($shalArr as $val){
                $shalStr.=$val;
            }
            $sign = sha1($shalStr.$appSercet);

//$requestParam = "appKey=".$appKey."&timestamp=".$timestamp."&asnNumber=".$asnNumber."&rohStatus=".$rohStatus."&receivedTime=".urlencode($receivedTime)."&items.sku=TESTPDUCT-01&items.sku=TESTPDUCT-02&items.weight=1.4&items.weight=1.2&items.quantity=1&items.quantity=2&items.qc=N&items.qc=N&sign=".$sign;
            $requestParam = "appKey=".$appKey;
            foreach($param as $key=>$val){
                $requestParam.="&".$key."=".$val;
            }
            foreach($items as $k=>$v){
                foreach($v as $key=>$value){
                    $requestParam.="&items.".$key."=".$value;
                }
            }
            $requestParam.="&sign=".$sign;
            $ch = curl_init($httpURL);
            curl_setopt($ch, CURLOPT_URL, $httpURL);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $postHeader);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS,$requestParam);

            $response = curl_exec($ch);
            if(curl_errno($ch)){
                print curl_error($ch);
            }

            $obj = json_decode($response);

            $result = Common_Common::objectToArray($obj);
            if($result['ack']=="Success"){
                echo $row['refercence_no']."Received Message Success to Fenxiao platform\n";
                $updateftp = array(
                    'fml_status'=>2,
                );
                Service_FtpMessageSendlist::update($updateftp,$row['fml_id'],"fml_id");
            }else{
                echo $row['refercence_no']."Received Message Failure to Fenxiao platform:{$result['message']}\n";
            }
        }
    }
}
echo "[" . date('Y-m-d H:i:s') . "] End run\r\n\r\n";
@unlink($flagFile);