<?php
class Outside_ReceivingBatchController extends Ec_Controller_Action
{
    public function preDispatch()
    {
        $this->tplDirectory = "outside/views/receivingbatch/";
        $this->serviceClass = new Service_ReceivingBatch();
    }

    public function listAction()
    {
        if ($this->_request->isPost()) {
            $page = $this->_request->getParam('page', 1);
            $pageSize = $this->_request->getParam('pageSize', 20);

            $page = $page ? $page : 1;
            $pageSize = $pageSize ? $pageSize : 20;

            $return = array(
                "state" => 0,
                "message" => "No Data"
            );

            $params = $this->_request->getParams();
            $condition = $this->serviceClass->getMatchFields($params);
            $logistics_code = trim($params['logistics_code']);
            $datefrom = $params['dateFor'];
            $dateTo = $params['dateTo'];
            //
            if($datefrom!=''){
            	$condition['add_time_from'] = $datefrom;
            }
            if($dateTo!=''){
            	$condition['add_time_to'] = $dateTo;
            }
            if($params['logistics_code']!=''){
            	//$count = 0;
            	$logistic_order = Service_LogisticsOrder::getByField($logistics_code,'logistics_code');
            	if(empty($logistic_order)){
            		$logistic_order= Service_LogisticsOrder::getByField($logistics_code,'ref_tracking_number');
            		if(!empty($logistic_order)){
            			$scanTracking = '1'; //扫描的是运单号
            		}
            	}
            	$logistics_code = $logistic_order['logistics_code'];
            	if(!empty($logistic_order)){
            		$receivingBatchOrderCondition = array('logistics_code'=>$logistics_code);
            		$receivingBatchOrders = Service_ReceivingBatchOrder::getByCondition($receivingBatchOrderCondition,array('rb_code'));
            		if(!empty($receivingBatchOrders)){
            			$rbcodeArr = array();
            			foreach($receivingBatchOrders as $key=>$value){
            				$rbcodeArr[] = $value['rb_code'];
            			}
            			$condition['rb_code_arr'] = $rbcodeArr;
            		}
            	}
            }
            $count = $this->serviceClass->getByCondition($condition, 'count(*)');
            $return['total'] = $count;
			//echo $count;exit;
            if ($count) {
                $showFields=array(
                   'rb_id', 
	               'rb_code',
                   'customer_code',	
	               'warehouse_id',
	               'status',
	               'add_time',
	               'last_modified_time',
	               'remark',
	               'oper_userid',
                );
                $showFields = $this->serviceClass->getFieldsAlias($showFields);
                $rows = $this->serviceClass->getByCondition($condition,$showFields, $pageSize, $page, array('rb_id desc'));
                foreach($rows as $key=>$value){
                	$user=Service_User::getByField($value['E11']);
                	$rows[$key]['user_name'] = $user['user_code'];
                }
                $return['status'] = $this->serviceClass->getStatus();
                $return['data'] = $rows;
                $return['state'] = 1;
                $return['message'] = "";
            }
            die(Zend_Json::encode($return));
        }
        $this->view->kvType = $this->kvType;
        echo Ec::renderTpl($this->tplDirectory . "receiving_batch_index.tpl", 'layout');
    }

    public function getByJsonAction()
    {
        $result = array('state' => 0, 'message' => 'Fail', 'data' => array());
        $paramId = $this->_request->getParam('paramId', '');
        if (!empty($paramId) && $rows = $this->serviceClass->getByField($paramId, 'rrl_id')) {
            $rows=$this->serviceClass->getVirtualFields($rows);
            $result = array('state' => 1, 'message' => '', 'data' => $rows);
        }
        die(Zend_Json::encode($result));
    }
	/**
	 * @author william-fan
	 * @todo 用于到货总单的创建
	 */
    public function addAction(){
    	$shipMethod = $this->serviceClass->getShipMethod();
    	$this->view->shipMethod = $shipMethod;
    	$country = Service_Country::getAll();
    	$this->view->country = $country;
    	$rb_code = $this->_request->getParam('rb_code','');
    	$info = array();
    	if($rb_code!==''){
    		$receivingBatch = Service_ReceivingBatch::getByField($rb_code,'rb_code');
    		if(!empty($receivingBatch) && $receivingBatch['status']=='0' ){
    			//草稿状态可编辑
    			$info['receivingBatch'] = $receivingBatch;
    			$conditonReceivingBatchDetail = array(
    				'rb_code'=>$rb_code,
    			);
    			$receivingDetail = Service_ReceivingBatchDetail::getByCondition($conditonReceivingBatchDetail,'*');
    			$info['receivingBatchDetail'] = $receivingDetail;
    			$conditionReceivingOrders = array(
    				'rb_code'=>$rb_code
    			);
    			$receivingOrders = Service_ReceivingBatchOrder::getByCondition($conditionReceivingOrders,'*');
    			$info['receivingBatchOrders'] = $receivingOrders;
    			//print_r($info);
    			$this->view->info = $info;
    		}
    	}else{
    		
    	}
    	echo Ec::renderTpl($this->tplDirectory . "receiving_batch_add.tpl", 'layout');
    }
    /**
     * @author william-fan
     * @todo 用于生成到货总单
     */
    public function buildReceivingBatchAction(){
    	if ($this->_request->isPost()){
    		$customer_code = $this->_request->getParam('customer_code');
    		$result = $this->serviceClass->createBatchProcess($customer_code);
    		die(json_encode($result));
    	}
    }
    /**
     * @author william-fan
     * @todo 用于保存运单（提交receiving_batch_order表中）
     */
    public function addToOrderAction(){
    	if ($this->_request->isPost()){
    		$rb_code = trim($this->_request->getParam('rb_code',''));
    		$weight = trim($this->_request->getParam('weight',''));
    		$order_code = trim($this->_request->getParam('order_code',''));
    		$country = trim($this->_request->getParam('country',''));
    		$result = $this->serviceClass->createBatchOrderProcess($rb_code,$weight,$order_code,$country);
    		die(json_encode($result));
    	}
    }
    /**
     * @author william-fan
     * @todo 添加到货单详情表
     */
    public function addToBatchDetailAction(){
    	if ($this->_request->isPost()){
    		$rb_code = $this->_request->getParam('rb_code','');
    		$weight = $this->_request->getParam('weight','');
    		$BagQty = $this->_request->getParam('BagQty','');  //袋数
    		$PackageQty = $this->_request->getParam('PackageQty',''); //件数
    		$smcode = $this->_request->getParam('smcode','');
    		$smDetail = array();
    		/* print_r($weight);
    		print_r($smcode); */
    		if(!empty($smcode)){
    			foreach ($smcode as $key=>$sm){
    				$smWeight = $weight[$key];
    				$smbgQuanty = $BagQty[$key]; 
    				$smpackQty = $PackageQty[$key];
    				$smDate=array(
    					'smCode'=>$sm,	
    					'weight'=>$smWeight,
    					'bagQty'=>$smbgQuanty,
    					'packQty'=>$smpackQty		
    				);
    				$smDetail[] = $smDate;
    			}	
    		}
    		$result = $this->serviceClass->createBatchDetailProcess($smDetail,$rb_code);
    		die(json_encode($result));
    		//print_r($smDetail);
    	}
    }
    /**
     * @author william-fan
     * @todo 检查件数问题
     */
    public function addToBatchDetailCheckAction(){
    	$rb_code = $this->_request->getParam('rb_code','');
    	$weight = $this->_request->getParam('weight','');
    	$BagQty = $this->_request->getParam('BagQty','');  //袋数
    	$PackageQty = $this->_request->getParam('PackageQty',''); //件数
    	$smcode = $this->_request->getParam('smcode','');
    	$smDetail = array();
    	/* print_r($weight);
    	 print_r($smcode); */
    	if(!empty($smcode)){
    		foreach ($smcode as $key=>$sm){
    			$smWeight = $weight[$key];
    			$smbgQuanty = $BagQty[$key];
    			$smpackQty = $PackageQty[$key];
    			$smDate=array(
    					'smCode'=>$sm,
    					'weight'=>$smWeight,
    					'bagQty'=>$smbgQuanty,
    					'packQty'=>$smpackQty
    			);
    			$smDetail[] = $smDate;
    		}
    	}
    	$result = $this->serviceClass->createBatchDetailCheck($smDetail,$rb_code);
    	die(json_encode($result));
    }
    /**
     * @author william-fan
     * @todo 查看运单情况
     */
    public function viewBatchOrderAction(){
    	if ($this->_request->isPost()){
    		$rb_code = $this->_request->getParam('rb_code','');
    		$receivingOrderCondition = array(
    				'rb_code'=>$rb_code,
    		);
    		$result = array(
    			'ask'=>'0',
    			'message'=>'error',	
    			'data'=>'',	
    		);
    		$count = Service_ReceivingBatchOrder::getByCondition($receivingOrderCondition,'count(*)');
    		if($count>0){
    			$receivingBatchOrders = Service_ReceivingBatchOrder::getByCondition($receivingOrderCondition,'*');
    			foreach($receivingBatchOrders as $key=>$value){
    				$logisticOrders = Service_LogisticsOrder::getByField($value['logistics_code'],'logistics_code');
    				$receivingBatchOrders[$key]['tracking_number'] = $logisticOrders['ref_tracking_number'];
    			}
    			$result['data'] = $receivingBatchOrders;
    			$result['ask']='1';
    		}
    		die(json_encode($result));
    	}
    }
    /**
     * @author william-fan
     * @todo 用于删除到货单运单关联表
     */
    public function deleteBatchOrderAction(){
    	if ($this->_request->isPost()){
    		$rbo_id = $this->_request->getParam('rbo_id');
    		$result = $this->serviceClass->deleteBatchOrderProcess($rbo_id);
    		die(json_encode($result));
    	}
    }
    /**
     * @author william-fan
     * @todo 取得扫描件数
     */
    public function getBatchCountAction(){
    	if ($this->_request->isPost()){
    		$rb_code = $this->_request->getParam('rb_code','');
    		$result['count'] = '0';
    		if(!empty($rb_code)){
    			$batchOrderCondition = array(
    					'rb_code'=>$rb_code,
    			);
    			$count = Service_ReceivingBatchOrder::getByCondition($batchOrderCondition,'count(*)');
    			$result['count'] = $count;
    		}
   			die(json_encode($result));
    	}
    }
    /**
     * @author william-fan
     * @todo 用于获取receivingBatch相关数据
     */
    public function getBatchAllAction(){
    	if ($this->_request->isPost()){
    		$rb_code = $this->_request->getParam('rb_code','');
    		$receivingBatch = Service_ReceivingBatch::getByField($rb_code,'rb_code');
    		if(!empty($receivingBatch)){
    			//草稿状态可编辑
    			$info['receivingBatch'] = $receivingBatch;
    			$conditonReceivingBatchDetail = array(
    					'rb_code'=>$rb_code,
    			);
    			$receivingDetail = Service_ReceivingBatchDetail::getByCondition($conditonReceivingBatchDetail,'*');
    			$info['receivingBatchDetail'] = $receivingDetail;
    			$conditionReceivingOrders = array(
    					'rb_code'=>$rb_code
    			);
    			$receivingOrders = Service_ReceivingBatchOrder::getByCondition($conditionReceivingOrders,'*');
    			$info['receivingBatchOrders'] = $receivingOrders;
    			die(json_encode($info));
    		}
    	}
    }
    /**
     * @author william-fan
     * @todo 用于查看出货单
     */
    public function viewBatchAction(){
    	$rb_code = $this->_request->getParam('rb_code','');
    	$receingBatch = Service_ReceivingBatch::getByField($rb_code,'rb_code');
    	//echo $rb_code;
    	//var_dump($receingBatch);
   
    	$user=Service_User::getByField($receingBatch['oper_userid']);
    	$receingBatch['user_name'] = $user['user_code'];
    	$info['receivingDate'] = $receingBatch;
    	
    	$receivingBatchDetailCondition = array(
    		'rb_code'=>$rb_code,
    	);
    	$receivingBatchDetail = Service_ReceivingBatchDetail::getByCondition($receivingBatchDetailCondition,'*');
    	$info['receivingBatchDetail'] = $receivingBatchDetail;
    	
    	$receivingBatchOrdersCondition = array(
    		'rb_code'=>$rb_code,		
    	);
    	$receingBatchOrders=Service_ReceivingBatchOrder::getByCondition($receivingBatchOrdersCondition,'*');
    	if(!empty($receingBatchOrders)){
    		foreach($receingBatchOrders as $key=>$value){
    			$logisticOrder = Service_LogisticsOrder::getByField($value['logistics_code'],'logistics_code');
    			$receingBatchOrders[$key]['tracking_number'] = $logisticOrder['ref_tracking_number'];
                $userRow = Service_User::getByField($value['user_id'],'user_id',"*");
                $receingBatchOrders[$key]['user_code'] = isset($userRow['user_code'])?$userRow['user_code']:"";
    		}
    	}
    	$info['receivingBatchOrders'] = $receingBatchOrders;
    	$receivingBatchLogConditon = array(
    		'rb_code'=>$rb_code,
    	);
    	$receivingBatchLog = Service_ReceivingBatchLog::getByCondition($receivingBatchLogConditon,'*');
    	if(!empty($receivingBatchLog)){
    		foreach($receivingBatchLog as $key=>$value){
    			$user=Service_User::getByField($value['operator']);
    			$receivingBatchLog[$key]['operator'] = $user['user_code'];
    		}
    	}
    	$info['receivingBatchLog'] = $receivingBatchLog;
    	    	
    	//print_r($info);
    	$status = $this->serviceClass->getStatus();
    	$this->view->status = $status;
    	$this->view->info = $info;
    	echo Ec::renderTpl($this->tplDirectory . "receiving_batch_detail_index.tpl", 'layout');
    }
    /**
     * @author william-fan
     * @todo 用于保存运输详情
     */
    public function addSmDetailAction(){
    	if ($this->_request->isPost()){
    		$rb_code = $this->_request->getParam('rb_code','');
    		$weight = $this->_request->getParam('weight','');
    		$BagQty = $this->_request->getParam('BagQty','');  //袋数
    		$PackageQty = $this->_request->getParam('PackageQty',''); //件数
    		$smcode = $this->_request->getParam('smcode','');
    		$smDetail = array();
    		/* print_r($weight);
    		 print_r($smcode); */
    		if(!empty($smcode)){
    			foreach ($smcode as $key=>$sm){
    				$smWeight = $weight[$key];
    				$smbgQuanty = $BagQty[$key];
    				$smpackQty = $PackageQty[$key];
    				$smDate=array(
    						'smCode'=>$sm,
    						'weight'=>$smWeight,
    						'bagQty'=>$smbgQuanty,
    						'packQty'=>$smpackQty
    				);
    				$smDetail[] = $smDate;
    			}
    		}
    		$result = $this->serviceClass->createSMDetailProcess($smDetail,$rb_code);
    		die(json_encode($result));
    		//print_r($smDetail);
    	}
    }
    /**
     * @author william-fan
     * @todo 用于获取国家信息
     */
    public function getCountryAction(){
    	if ($this->_request->isPost()){
    		$result = array(
    			'ask'=>'0',
    			'message'=>'运单无效',
    			'address'=>'',		
    		);
    		$logistics_code = $this->_request->getParam('logistics_code','');
    		$logistic_order = Service_LogisticsOrder::getByField($logistics_code,'logistics_code');
    		if(empty($logistic_order)){
    			$logistic_order= Service_LogisticsOrder::getByField($logistics_code,'ref_tracking_number');
    		}
    		if(!empty($logistic_order)){
    			$address = Service_LogisticsOrderAddress::getByField($logistic_order['logistics_code'],'logistics_code');
    			$result['ask'] = '1';
    			$result['message'] = 'success';
    			$result['address'] = $address;
    		}
    		die(json_encode($result));
    	}
    }
    
}