<?php

class Merchant_TrackingController extends Ec_Controller_Action {

  public function preDispatch() {
    $this->tplDir = 'merchant/views/tracking/';
    $this->user = $this->_customerAuth;

  }

  /**
   * [indexAction description]
   *
   * @return [type] [description]
   */
  public function indexAction(){
    $code = $this->_request->getParam('code', '');
    $operationCustomerCode = $this->_request->getParam('operationCustomerCode','');
    $status = $this->_request->getParam('status','12');
    if(empty($code)){
      die('Parameter error');
    }
    $customer_code = $this->user['code'];
    if(!empty($operationCustomerCode)){
        $opCustomer=Service_Customer::getByField($operationCustomerCode, 'customer_code');
        if(empty($opCustomer)){
          die('Customer [' . $operationCustomerCode . '] does not exist .');
        }
        if(Service_CustomerRegister::getByCondition(array('regis_customer_code' => $customer_code, 'customer_code' => $operationCustomerCode)) == false){
          die('No authorization operation ' . $this->_operationCustomerCode . '.');
        }
        $customer_code = $opCustomer['customer_code']; //操作的哪个客户的代码
    }
    $condition = array(
       'customer_code'=>$customer_code,
       'new_reference_no' =>$code
    );
    $orderCodeRows = Service_Orders::getByCondition($condition,'*');
    //$orderCodeRow = Service_Orders::getByField($code, 'reference_no', array('order_code'));
    if(empty($orderCodeRows) || !is_array($orderCodeRows)){
      $orderCodeRow = Service_ShipOrder::getByField($code, 'tracking_number', array('order_code'));
    }else{
      $orderCodeRow = $orderCodeRows[0];
    }
    $shipOrderTracking = array();
    $tripStatus = array(
        '草稿',
        '确认',
        '已提交',
        '已打印',
        '已打包',
        '已装袋',
        '已加挂'
    );
    if(!empty($orderCodeRow) && is_array($orderCodeRow)){
      $shipOrderTracking = Service_ShipOrderTracking::getByCondition(array('order_code'=>$orderCodeRow['order_code']),"*",0,0,"sot_add_time desc");
      if(!empty($shipOrderTracking) && is_array($shipOrderTracking)){
        foreach($shipOrderTracking as $key=>$value){
            $warehouse = Service_Warehouse::getByField($value['warehouse_id'],"warehouse_id","*");
            $shipOrderTracking[$key]['warehouse_code'] = $warehouse['warehouse_code'];
            $shipOrderTracking[$key]['order_code'] = $code;
            if($status=='12'){
               if(in_array($value['status_desc'],$tripStatus)){
                  unset($shipOrderTracking[$key]); //GS不要的操作记录
               }else{
                   if($value['user_id']!='0'){
                      unset($shipOrderTracking[$key]); //GS不要的操作记录
                   }
               }
            }
        }
      }
    }
    /*
    if(empty($shipOrderTracking) || !is_array($shipOrderTracking)){
    $shipOrderTracking = Service_ShipOrderTracking::getByCondition(array('tracking_number'=>$code),"*",0,0,"sot_add_time desc");
    }
    if(empty($shipOrderTracking) || !is_array($shipOrderTracking)){
    $shipOrderTracking = Service_ShipOrderTracking::getByCondition(array('ref_tracking_number'=>$code),"*",0,0,"sot_add_time desc");
    }
    */
    $this->view->shipOrderTracking = $shipOrderTracking;
    echo Ec::renderTpl($this->tplDir . 'index.tpl', 'noleftlayout');
  }
}
