<?php
class Pda_PdaController extends Ec_Controller_Action
{
    public function preDispatch()
    {
        $this->tplDirectory = "pda/views/pda/";
        //$this->serviceClass = new Service_Orders();
		$userAuth = new Zend_Session_Namespace('userAuthorization');
        $this->warehouse_id = $userAuth->warehouseId;
    }

    public function indexAction() {
        $userAuth = new Zend_Session_Namespace('userAuthorization');
        if ($userAuth->userId && $userAuth->isLogin){
            //获取pda菜单列表
            $menu = Service_Pda::getMenu();
            $this->view->menu = $menu;
            echo Ec::renderTpl($this->tplDirectory . "index.tpl", 'pda_layout');
            exit();
        }
        $this->view->errMsg = '';
        echo $this->view->render($this->tplDirectory . 'login.tpl');

    }

    /*@desc pda确认QC
     *@author Gilbert
     *@date 2015-08-25
     */
    public function pdaQcAction() {
        if($this->_request->isPost()){
            $return = array('state'=>0,'message'=>'');
            $obj = new Service_QualityControlProcess();
            $param = $this->_request->getParams();
            $qcRow = Service_QualityControl::getByField($param['qc_code'],"qc_code","*");
            if($qcRow['qc_model']=="1"){
                $qcParams = array(
                    'qcId'=>$qcRow['qc_id'],
                    'productId'=>$qcRow['product_id'],
                    'productBarcode'=>$qcRow['product_barcode'],
                    'customerCode'=>$qcRow['customer_code'],
                    'productCategoryId'=>isset($param['category'])?$param['category']:"",
                    'productBarcodeType'=>isset($param['productBarcodeType'])?$param['productBarcodeType']:"0",
                    'productWeight'=>$param['product_weight'],
                    'receivedQty'=>$param['received_number'],
                    'problemQty'=>$param['defective_number'],
                    'noLabelQty'=>isset($param['noLabelQty'])?$param['noLabelQty']:"",
                    'shelf'=>isset($param['shelf'])?$param['shelf']:"",
                    'remark'=>isset($param['remark'])?$param['remark']:"",
                    'is_qc'=>isset($param['is_qc'])?$param['is_qc']:"",
                    'qc_subject'=>isset($param['qc_subject'])?$param['qc_subject']:"",
                    'qc_category'=>isset($param['qc_category'])?$param['qc_category']:"",
                    'real_qc_category'=>isset($param['real_qc_category'])?$param['real_qc_category']:"",
                    'real_qc_quantity'=>isset($param['real_qc_quantity'])?$param['real_qc_quantity']:"",
                    'qc_remark'=>isset($param['qc_remark'])?$param['qc_remark']:"",
                    'qualified_rate'=>isset($param['qualified_rate'])?$param['qualified_rate']:"",
                );
                $qcObj = new Service_SwapOrderProcess();
                $result = $qcObj->qcSubmit($qcParams);
                $return['state'] = $result['state'];
                if($return['state']=="0"){
                    $message = "";
                    foreach($result['error'] as $er){
                        $message.=$er['errorMsg']."</br>";
                    }
                    $return['message'] = $message;
                }else{
                    $return['message'] = "质检成功";
                }
                die(json_encode($return));
            }else{
                $return = $obj->pdaSubmit($this->_request->getParams());
                die(json_encode($return));
            }
            //$return = $obj->pdaSubmit($this->_request->getParams());
            //die(json_encode($return));
        }
        $this->view->qcCategory = Service_QcCategory::getAll();
        $layout = Zend_Registry::get('layout');
        $layout->bread = ' > 质检';
        echo Ec::renderTpl($this->tplDirectory . "pda_qc.tpl", 'pda_layout');
        exit;
    }

    /*@desc pda分箱
     * @author Gilbert
     * @date 2015-08-27
     */
    public function pdaDivisionAction(){
        if($this->_request->isPost()){
            $return = array('state'=>0,'message'=>'');
            $obj = new Service_BoxUpShelf();
            $return = $obj->pdaDivision($this->_request->getParams());
            die(json_encode($return));
        }
        $layout = Zend_Registry::get('layout');
        $layout->bread = ' > 分箱';
        echo Ec::renderTpl($this->tplDirectory . "pda_division.tpl", 'pda_layout');
        exit;
    }
    /*@desc pda上架
     * @author Gilbert
     * @date 2015-08-28
     */
    public function pdaShelveAction(){
        if($this->_request->isPost()){
            $return = array('state'=>0,'message'=>'');
            $params = $this->_request->getParams();
            $params['shelveType'] = 1;
            $obj = new Service_Shelve($params);
            if( !$obj->pdaShelve()){
                $return['message'] = implode(',',$obj->getError());
            }else{
                $return['state'] = 1;
                $return['message'] = '上架成功';
            }
            die(json_encode($return));
        }
        $layout = Zend_Registry::get('layout');
        $layout->bread = ' > 上架';
        echo Ec::renderTpl($this->tplDirectory . "pda_shelve.tpl", 'pda_layout');
        exit;
    }

    /*检查上架信息
     *
     */
    public function checkAction(){
        $code = trim($this->_request->getParam('code',''));
        $return = array('state'=>0,'message'=>'');
        if(!$code){
            $return['message'] = '单号不能为空';
            die(json_encode($return));
        }
        $obj = new Service_Shelve(array('code'=>$code));
        try{
            $codeType = 'qc';
            if(!$obj->checkQc($code)){
                $codeType = 'box';
                $obj->checkBox($code);
            }else{
                $detailRows = Service_PutawayDetail::getByCondition(array('qc_code'=>$code));
                if($detailRows){
                    foreach($detailRows as $value){
                        if($value['box_code']){
                            throw new Exception( '质检单已分箱，请扫箱号上架');
                        }
                    }
                }
            }
            $error = $obj->getError();
            if(!empty($error)){
                throw new Exception(implode(',',$error));
            }
            $qty = $obj->getQtyPendedToShelve($codeType);
            $return['state'] = 1;
            $return['message'] = 'Success';
            $return = array_merge($return,$qty);
            die(json_encode($return));
        }catch(Exception $e){
            $return['message'] = $e->getMessage();
            die(json_encode($return));
        }
    }


}