<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class Pda_ReceivingController extends Ec_Controller_DefaultAction{
    private $warehouseId = 0;
    //初始化
    public function init()
    {
       parent::init();
        $userAuth = new Zend_Session_Namespace('userAuthorization');
        if (!$userAuth->userId || !$userAuth->isLogin) {
            $this->_redirect("/default/index/pda-index");
            exit();
        }     
    }
    
    public function preDispatch()
    {
        $this->tplDirectory = "pda/views/Receiving/";
        $this->serviceClass = new Service_Product();
        $userAuth = new Zend_Session_Namespace('userAuthorization');
        $this->user_id = $userAuth->userId;
        $this->warehouseId  =   $userAuth->warehouseId;
    }
    
    public function submitPutawayAction() {
        //putaway
        $errMsg =   '';
        if($this->_request->isPost()){
            try{
                $operationCode  =   $this->_request->getParam('operationCode', '');
                $lcCode  =   $this->_request->getParam('lcCode', '');
                
                if($operationCode == "" || $lcCode == ""){
                    throw new Exception("质检单号/货架号 不能为空！");                    
                }
                
                $condition  =   array();
                $condition['warehouse_id']  = $this->warehouseId;
                $condition['pd_status']     = '0';
                $condition['qc_code']       =   $operationCode;

                $putawayClass = new Service_PutawayDetail();

                $showFields = $putawayClass->getFieldsAlias(array('pd_id','product_barcode'));

                $data   =   $putawayClass->getByCondition($condition , $showFields);

                if(empty($data)){
                    throw new Exception("质检单号不正确或者该质检单号已上架！");
                }else{
                    $putaway   =  array();
                    foreach ($data as $val) {
                        $putaway[] = array(
                            'pd_id' => $val['E0'],
                            'lc_code' => $lcCode,
                            'product_barcode' => $val['E7'],
                        );
                    }
                    unset($data);
                    $putawayObj = new Service_PutawayProcess();
                    $return = $putawayObj->submit($putaway);   
                    if($return['state'] != 1){
                        $e = '';
                        foreach ($return['error'] as $value) {
                            $e .=   $value['errorMsg']."<br />";
                        }           
                        throw new Exception($e);
                    }else{
                        $errMsg = "质检单号:{$operationCode} 成功上架！";
                    }
                }    
            }catch (Exception $exc) {
                $errMsg =     $exc->getMessage();
            }
        }
        $this->view->errMsg = $errMsg;
        echo $this->view->render($this->tplDirectory . 'putaway.tpl');
    }
    
    
}
