<?php
class Warehouse_PackedRecordController extends Ec_Controller_Action{
    
    public function preDispatch(){
        $this->tplDirectory = "warehouse/views/default/";
        $this->serviceClass = new Service_PackedRecord();
    }
    
    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);
            $condition['date_for'] = $params['dateFor'];
            $condition['date_to'] = $params['dateTo'];
            //var_dump($condition);exit;
            unset($params);
            $otherFeeInfo = Service_OtherFee::getByField('YHDBF', 'fee_code');//根据杂费代码查询配置
            $count = $this->serviceClass->getByCondition($condition, 'count(*)');
            $return['total'] = $count;
            if ($count) {
                $showFields=array(
                    'pr_id',
                    'customer_code',
                    'pr_refer_code',
                    'pr_packing_quantity',
                    'pr_packing_time',
                    'pr_add_time',
                    'user_id'
                );
                $showFields = $this->serviceClass->getFieldsAlias($showFields);
                $rows = $this->serviceClass->getByCondition($condition,$showFields, $pageSize, $page, array('pr_id desc'));
                foreach($rows as $key=>$row){
                    $user = Service_User::getByField($row['E6'],"user_id","*");
                    $rows[$key]['E6'] = $user['user_name'];
                }
                $return['data'] = $rows;
                $return['state'] = 1;
            
                $return['message'] = "";
            }
            die(Zend_Json::encode($return));
        }
        echo Ec::renderTpl($this->tplDirectory . "packed-record.tpl", 'layout');
    }
    
    public function addAction(){
        $response = array('status'=>'0','message'=>'操作失败!');
        $params = $this->_request->getParams();
        $condition = $this->serviceClass->getMatchFields($params);
        $customerCode  = $condition['customer_code'];
        $quantity      = $condition['pr_packing_quantity'];
        $packingTime   = $condition['pr_packing_time'];
        $customerData  = Service_Customer::getByField($customerCode, 'customer_code', array('customer_id'));
        if(empty($customerData)){
            $response['message'] = '客户代码不存在!';
            die(Zend_Json::encode($response));
        }
        $code ='YH'.Common_GetNumber::getCode();//随机生成唯一的交易记录的参考号/订单号 
        $coRow['code']           = $code; //单号，生成对应订单号
        $coRow['customer_code']  = $customerCode; //对应单，客户代码
        $coRow['quantity']       = $quantity; // 打包数量
        $coRow['fee_generate_time'] = $packingTime;//打包时间
        $coRow['reference_no']   = $code; //客户参考号
        $coRow['total_quantity'] = $quantity; //总件数
        
        $otherFeeInfo = Service_OtherFee::getByField('YHDBF', 'fee_code');
        if(!empty($otherFeeInfo)){
            $result = Process_DsfhfProcess::deductChangOrderFee($otherFeeInfo,$coRow);
            if($result['status'] == '1'){
                //记录客户打包录入
                $packedRecordLog = array(
                    'customer_code'       => $customerCode,
                    'pr_refer_code'       => $code,
                    'pr_packing_quantity' => $quantity,
                    'pr_packing_time'     => $packingTime,
                    'pr_add_time'         => date('Y-m-d H:i:s'),
                    'user_id'             => Service_User::getUserId(),
                    'fee_id'              => $otherFeeInfo['fee_id']
                );
                $insertResult = Service_PackedRecord::add($packedRecordLog);
                if($insertResult){
                    $response['status'] = '1';
                }else{
                    $response['message'] = '易货打包费扣除成功，客户打包记录录入失败!';
                }
            }else{
                $response['message'] = array($result['message']);
            }
        }else{
            $response['message'] = '易货打包费杂费未配置!';
        }
        die(Zend_Json::encode($response));
    }
}