<?php
class Shipment_OrdersPackController extends Ec_Controller_Action
{
    public function preDispatch()
    {
        $this->tplDirectory = "shipment/views/order/";
        $userAuth = new Zend_Session_Namespace('userAuthorization');
        $this->user_id = $userAuth->userId;
    }

    /**
     * @desc 一票多件
     * @ 订单打包贴标页面
     */
    public function listAction()
    {
        $userRight = Service_UserRight::getByField("/shipment/orders-pack/submit-pack","ur_url","*");
        $urmCondition = array(
            'ur_id'=>$userRight['ur_id'],
            'user_id'=>$this->user_id
        );
        $userRightMap = Service_UserRightMap::getByCondition($urmCondition,"count(*)");
        if($userRightMap){
            $dabaoAuth = 1;
        }else{
            $dabaoAuth = 0;
        }
        $this->view->url = 'http://'.$_SERVER['HTTP_HOST'].'/pdf-plugin/web/viewer.html?file=http://'.$_SERVER['HTTP_HOST'].'/shipment/print/get-pdf-label/orderCode/';
        $this->view->dabaoAuth = $dabaoAuth;
        $this->view->userId = $this->user_id;
        echo Ec::renderTpl($this->tplDirectory . "pack.tpl", 'layout');
    }

    /**
     * @获取订单信息
     * @一票多件
     */
    public function getOrdersAction()
    {
        $return = array(
            'state' => 0,
            'message' => '',
            'error' => array(),
            'data' => array()
        );
        if ($this->_request->isPost()) {
            $orderCode = trim($this->_request->getParam('orderCode', ''));
            $order = ('' !== $orderCode) ? Service_Orders::getByField($orderCode, 'order_code') : FALSE;

            //冻结判断
            if(!empty($order)){
              $customerData = Service_Customer::getByField($order['customer_code'], 'customer_code', array('is_frozen', 'frozen_reason'));
              if($customerData['is_frozen']){
                $return['error'][] = '订单['. $orderCode .']-客户['.$order['customer_code'].']已冻结，冻结原因：'.$customerData['frozen_reason'];
                die(Zend_Json::encode($return));
              }
            }
            
            if (!empty($order) && 6 === (int) $order['order_status']) {
              $packObj = new Service_PackProcess();
              $return = $packObj->getOrderDetailByOrderCode($orderCode, null, 1);
            }
            else {
              ('' === $orderCode) ? $return['error'][] = '订单号不能为空' : NULL;
              ('' !== $orderCode && empty($order)) ? $return['error'][] = '订单号【' . $orderCode . '】不存在' : NULL;
              (!empty($order) && 6 !== (int) $order['order_status']) ? $return['error'][] = '订单【' . $orderCode . '】不是已下架状态' : NULL;
            }
        }
        die(Zend_Json::encode($return));
    }

    /**
     * @desc 订单打包贴标
     * @一票多件
     */
    public function submitPackAction()
    {
        $return = array(
            'state' => 0,
            'message' => '',
            'error' => array(),
            'orderCode' => ''
        );
        if ($this->_request->isPost()) {
            $orderCode = $this->_request->getParam('orderCode', '');
            $packingCode = $this->_request->getParam('packingCode', '');
            $packObj = new Service_PackProcess();
            $return = $packObj->packTransaction($orderCode, $packingCode, 1, null);
        }
        die(Zend_Json::encode($return));
    }
    public function getBarcodeAction()
    {
        $return = array(
            'product_sku' => ''
        );
        if ($this->_request->isPost()) {
            $sku = $this->_request->getParam('psku', '');
            $orderCode = $this->_request->getParam('orderCode', '');      
            $pd=Service_PickingDetail::getByCondition(array('order_code'=>$orderCode),"*");
            if (!empty($pd)) {
              foreach ($pd as $key => $value) {
                $product=Service_Product::getByField($value['product_id'],'product_id',"*");
               if ($product['product_sku']==$sku) {
                 $return=$product;
                  $return['state']=1;
                 //print_r($return);exit;
                 die(Zend_Json::encode($return));
               }else{
                $return['state']=0;
                $return['product_sku']=$sku;
               }
              }
                //print_r($return);exit;
            }
        }
        die(Zend_Json::encode($return));
    }

    public function sanImeiAction(){
        $return = array(
            'state' => 0,
            'message' => '',
        );
        $product_barcode = $this->_request->getParam('product_barcode', '');
        $order_code = $this->_request->getParam('order_code', '');
        $imei_serial = $this->_request->getParam("imei_serial","");
        $op = Service_OrderProduct::getByCondition(array('order_code'=>$order_code,'product_barcode'=>$product_barcode),"*");
        //var_dump($op);
        //exit;
        $productSku = "";
        if(!empty($op)){
            $productId = $op[0]['product_id'];
            $productRow = Service_Product::getByField($productId,"product_id","*");
            $productSku = $productRow['product_sku'];
        }
        $oiRow = array(
            'order_code'=>$order_code,
            'product_sku'=>$productSku,
            'product_barcode'=>$product_barcode,
            'imei_serial'=>$imei_serial
        );
        $existImei = Service_OrderImei::getByField($imei_serial,"imei_serial","*");
        if(!empty($existImei)){
            $return['state'] = 0;
            $return['message'] = "失败:IMEI号[".$imei_serial."]已经被扫描过了!";
            die(Zend_Json::encode($return));
        }
        Service_OrderImei::add($oiRow);
        $return['state'] = 1;
        $return['message'] = "IMEI号[".$imei_serial."]扫描成功!";
        die(Zend_Json::encode($return));
    }
}