<?php
/**
 * Created by JetBrains PhpStorm.
 * User: user
 * Date: 15-6-4
 * Time: 下午8:12
 * To change this template use File | Settings | File Templates.
 */
class Order_SwapOrderController extends Ec_Controller_Action
{
    public function preDispatch()
    {
        $this->tplDirectory = "order/views/default/";
        $this->serviceClass = new Service_SwapOrder();
    }

    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"
            );
            $swo_code = trim($this->_request->getParam("swo_code",""));
            $serial_code = trim($this->_request->getParam("serial_code",""));
            $customer_code = trim($this->_request->getParam("customer_code",""));
            $swo_status = trim($this->_request->getParam("swo_status",""));
            $from_customer_code = trim($this->_request->getParam("from_customer_code",""));
            $product_id = trim($this->_request->getParam("product_id",""));
            $product=Service_Product::getByField($product_id,'product_barcode');
            $to_product_barcode=trim($this->_request->getParam("to_product_barcode",""));
            $searchDataType=$this->_request->getParam('searchDateType','1');
            $timeStart=$this->_request->getParam('timeStart');
            $timeEnd=$this->_request->getParam('timeEnd');
            $hasCustomerQc=$this->_request->getParam('E12');
            if (!empty($product_id)) {
                if (empty($product)) {
                    $p='-1';    //产品不存在时搜不到易货单
                }else{
                    $p=$product['product_id'];
                }
            }
            $condition = array(
                'swo_code'=>$swo_code,
                'serial_code'=>$serial_code,
                'customer_code'=>$customer_code,
                'swo_status'=>$swo_status,
                'product_id'=>$p,
                'from_customer_code'=>$from_customer_code,
                'to_product_barcode'=>$to_product_barcode,
                'has_customer_qc'=>$hasCustomerQc,
            );
             if($searchDataType==1){
                $condition['add_time_start']=$timeStart;
                $condition['add_time_end']=$timeEnd;
            }else{
                 $condition['putway_time_start']=$timeStart;
                $condition['putway_time_end']=$timeEnd;
            }
            $count = Service_SwapOrder::getJoinDetailByCondition($condition,"count(*)");
            $return['total'] = $count;
            if($count){
                $showFields=array(
                    'swo_id',
                    'swo_code',
                    'serial_code',
                    'customer_id',
                    'customer_code',
                    'swo_status',
                    'add_time',
                    'last_update_time',
                    'remark',
                    'putway_time',
                    'is_abnormal'
                );
                $showFields = $this->serviceClass->getFieldsAlias($showFields);
                $row = Service_SwapOrder::getJoinDetailByCondition($condition,$showFields,$pageSize,$page,array('swo_id desc'));
                foreach($row as $key=>$value){
                    $customerRow = Service_Customer::getByField($value['E4'],"customer_code","*");
                    $row[$key]['customer_company_name'] = $customerRow['customer_company_name'];
                }
                $return['data'] = $row;
                $return['state'] = 1;
            }
            die(Zend_Json::encode($return));
        }
        echo Ec::renderTpl($this->tplDirectory . "swap_order_index.tpl", 'layout');
    }

    public function offshelfAction(){
        $swo_id = $this->_request->getParam('swo_id',"");
        $picker_id = $this->_request->getParam("pickerId","");
        //echo $picker_id."=pickerId&swo_id=".$swo_id;
        //exit;
        $sop = new Service_SwapOrderProcess();
        $result = $sop->offShelfTransaction($swo_id,$picker_id);
        die(Zend_Json::encode($result));
    }

    public function viewAction(){
        $swapCode = $this->_request->getParam('swo_code',"");
        $swapRow = Service_SwapOrder::getByField($swapCode,"swo_code","*");
        $this->view->swapRow = $swapRow;
        $this->view->swapDetail = Service_SwapOrderDetail::getLeftJoinProductByCondition(array('swo_code'=>$swapCode),"*");
        $this->view->statusArr = array(
            '0'=>'草稿',
            '1'=>'异常',
            '2'=>'确认',
            '3'=>'下架',
            '4'=>'质检',
            '5'=>'上架',
        );
        echo Ec::renderTpl($this->tplDirectory . "view-swap-order.tpl", 'layout');
    }

    public function qcListAction(){
        $qcStatus=Service_QualityControlProcess::getStatus();
        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();

            $qcObj = new Service_QualityControl();

            $operationType = isset($params['operationType']) ? $params['operationType'] : '';
            $operationCode = isset($params['operationCode']) ? $params['operationCode'] : '';
            if (!empty($operationCode)) {
                $params[$operationType] = $operationCode;
            }
            $condition = $qcObj->getMatchFields($params);
            $condition['dateFor'] = isset($params['dateFor'])?$params['dateFor']:'';
            $condition['dateTo'] = isset($params['dateTo'])?$params['dateTo']:'';
            if(!empty($condition['dateFor'])){
                $condition['dateFor']=date('Y-m-d H:i:s',strtotime($condition['dateFor'].' 00:00:00'));
            }
            if(!empty($condition['dateTo'])){
                $condition['dateTo']=date('Y-m-d H:i:s',strtotime($condition['dateTo'].' 23:59:59'));
            }
            $condition['qc_model'] = "1";

            $count = $qcObj->getByCondition($condition, 'count(*)');
            $return['total'] = $count;

            if ($count) {
                $showFields = array(

                    'qc_code',
                    'receiving_code',
                    'product_barcode',
                    'customer_code',
                    'qc_quantity',
                    'qc_received_quantity',
                    'qc_quantity_sellable',
                    'qc_quantity_unsellable',
                    'qc_status',
                    'qc_add_time',
                    'qc_id',
                    'qc_type',
                    'rd_id',
                    'qc_finish_time',
                    'product_id',
                );
                $showFields = $qcObj->getFieldsAlias($showFields);
                $rows = $qcObj->getByCondition($condition, $showFields, $pageSize, $page, array('qc_id desc'));
                $return['data'] = $rows;
                $return['state'] = 1;
                $return['status'] = $qcStatus;
                $return['message'] = "";
            }
            die(Zend_Json::encode($return));
        }
        echo Ec::renderTpl($this->tplDirectory . "swap-order-qclist.tpl", 'layout');
    }

    public function pickingAction(){
        $wh = Common_DataCache::getWarehouseSimple();
        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();
            $pickingClass = new Service_Picking();
            $condition =$pickingClass->getMatchFields($params);

            if('' !== trim($params['order_code'])){
                //凭订单号去picking_detail表匹配配货单号
                $pickingDetail = Service_PickingDetail::getByField(trim($params['order_code']),'order_code');

                //进入到$pickingDetail
                if($pickingDetail){
                    //配货单空，用订单号搜索
                    if('' === trim($condition['picking_code'])) {
                        $condition['picking_code'] = $pickingDetail['picking_code'];
                    }
                    elseif (trim($condition['picking_code']) !== $pickingDetail['picking_code']){
                        //配货单不匹配订单号,__NONE_PICKING_CODE_FIND__
                        $condition['picking_code'] = '__NONE_PICKING_CODE_FIND__';
                    }
                }
                else {
                    //空订单号，无配货单匹配
                    $condition['picking_code'] = '__NONE_PICKING_CODE_FIND__';
                }
            }

            $condition['dateFor'] = isset($params['dateFor'])?$params['dateFor']:'';
            $condition['dateTo'] = isset($params['dateTo'])?$params['dateTo']:'';
            // if(!empty($condition['dateFor'])){
                // $condition['dateFor']=date('Y-m-d H:i:s',strtotime($condition['dateFor'].' 00:00:00'));
            // }
            // if(!empty($condition['dateTo'])){
                // $condition['dateTo']=date('Y-m-d H:i:s',strtotime($condition['dateTo'].' 23:59:59'));
            // }
            $condition['customer_code']=trim($this->_request->getParam('customer_code',""));
            $condition['picking_model'] = "1";
            $count = $pickingClass->getLeftJoinUserPickByCondition($condition, 'count(distinct picking.picking_code)');
           //print_r($count);exit;
            $return['total'] =$count;

            if ($count) {
                $showFields = array(

                    'warehouse_id',
                    'picking_code',
                    'picker_id',
                    'creater_id',
                    'picking_order_cnt',
                    'picking_lc_cnt',
                    'picking_item_cnt',
                    'picking_status',
                    'is_assign',
                    'picking_type',
                    'picking_add_time',
                    'picking_update_time',
                    'picking_id',
                );
                $showFields = $pickingClass->getFieldsAlias($showFields);
                $rows = $pickingClass->getLeftJoinUserPickByCondition($condition, $showFields, $pageSize, $page, array('picking_id desc'));
                foreach($rows as $key=>$value){
                    $creater = Service_User::getByField($value['E3'],'user_id',"*");
                    $rows[$key]['createName'] = $creater['user_name'];
                    $picker = Service_User::getByField($value['E4'],'user_id',"*");
                    $rows[$key]['pickName'] = $picker['user_name'];
                }
                $return['data'] = $rows;
                $return['state'] = 1;
                $return['warehouse'] = $wh;
                $return['pickStatus'] = Common_Status::pickStatus('auto');
                $return['pickType'] = Common_Type::pickType('auto');
                $return['pickAssign'] = Common_Type::pickAssign('auto');
                $return['message'] = "";
            }
            die(Zend_Json::encode($return));
        }
        echo Ec::renderTpl($this->tplDirectory . "swap-order-picking.tpl", 'layout');
    }

    public function cancelAction(){
        $swo_id = $this->_request->getParam("swo_id","");
        $return = array(
            "state" => 0,
            "message" => ""
        );
        if($swo_id==""){
            $return['message'] = "请选择一个易货单";
            die(Zend_Json::encode($return));
        }
        $swoOrderRow = Service_SwapOrder::getByField($swo_id,'swo_id',"*");
        if(empty($swoOrderRow)){
            $return['message'] = "易货单不存在";
            die(Zend_Json::encode($return));
        }
        if($swoOrderRow['swo_status']!="2"){
            $return['message'] = "易货单不是确认状态";
            die(Zend_Json::encode($return));
        }
        $DB = Common_Common::getAdapter();
        $DB->beginTransaction();
        try{
            $updateSwa = array(
                'swo_status'=>6,
                'last_update_time'=>date("Y-m-d H:i:s"),
            );
            Service_SwapOrder::update($updateSwa,$swo_id,'swo_id');
            $swapOrderDetail = Service_SwapOrderDetail::getByCondition(array('swo_id'=>$swo_id),"*");
            foreach($swapOrderDetail as $value){
                $fromCustomer = Service_Customer::getByField($value['from_customer_code'],"customer_code","*");
                $conInventory = array(
                    'customer_id'=>$fromCustomer['customer_id'],
                    'product_id'=>$value['product_id'],
                    'warehouse_id'=>$swoOrderRow['warehouse_id']
                );
                $productInventory = Service_ProductInventory::getByCondition($conInventory,"*",0,1,'',"for update");
                $inventory = $productInventory[0];
                //修改product_inventory 记录
                $productInventoryRow = array(
                    'pi_sellable'=>$inventory['pi_sellable']+$value['quantity'],
                    'pi_reserved'=>$inventory['pi_reserved']-$value['quantity'],
                    'pi_update_time'=>date('Y-m-d H:i:s',time()),
                );
                Service_ProductInventory::update($productInventoryRow, $inventory['pi_id']);
                $productInventoryLogRow = array(
                    'product_id'=>$inventory['product_id'],
                    'product_barcode'=>$inventory['product_barcode'],
                    'warehouse_id'=>$swoOrderRow['warehouse_id'],
                    'user_id'=>'-1',
                    'pil_sellable'=>$inventory['pi_sellable']+$value['quantity'],
                    'pil_reserved'=>$inventory['pi_reserved']-$value['quantity'],
                    'pil_onway'=>$inventory['pi_onway'],
                    'pil_pending'=>$inventory['pi_pending'],
                    'pil_unsellable'=>$inventory['pi_unsellable'],
                    'pil_shipped'=>$inventory['pi_shipped'],
                    'pil_ip'=>Common_Common::getIP(),
                    'pil_add_time'=>date('Y-m-d H:i:s',time()),
                    'pil_note'=>"易货单[".$swoOrderRow['swo_code']."]截单:[{$inventory['product_barcode']}]可用库存[{$inventory['pi_sellable']}]+".$value['quantity'].",冻结库存[{$inventory['pi_reserved']}]-".$value['quantity'],
                    'from_it_code'=>'Confirm',
                    'to_it_code'=>'Draft',
                    'reference_code'=>$swoOrderRow['swo_code'],
                    'pil_quantity'=>$value['quantity'],
                );
                Service_ProductInventoryLog::add($productInventoryLogRow);
                $updateSwapDetail = array(
                    'swod_status'=>6,
                    'last_update_time'=>date("Y-m-d H:i:s"),
                );
                Service_SwapOrderDetail::update($updateSwapDetail,$value['swod_id'],"swod_id");
            }
            Service_SwapOrderProcess::cancelToFenxiao($swoOrderRow['swo_code']);
            $DB->commit();
            $return['state'] = "1";
            $return['message'] = "易货单[".$swoOrderRow['swo_code']."]已成功截单!";
        }catch (Exception $e){
            $DB->rollBack();
            $return['message'] = $e->getMessage();
        }
        die(Zend_Json::encode($return));

    }
     /**
     * 导出
     */
    public function exportAction(){
        set_time_limit(0);
        //$codes = $this->_request->getParam('swoIds','');     
        $exportformat = 'xls';
        $fileName = 'swaporder' . date('Y-m-d-H-i-s');
        //print_r($codes);exit;
        $status = array('草稿', '异常', '确认', '下架', '质检', '上架', '删除');

        $swo_code = trim($this->_request->getParam('swo_code', ''));
        $serial_code = trim($this->_request->getParam('serial_code', ''));
        $customer_code = trim($this->_request->getParam('customer_code', ''));
        $swo_status = $this->_request->getParam('swo_status', '');
        $from_customer_code = trim($this->_request->getParam('from_customer_code', ''));
        $from_customer_code = strtoupper($from_customer_code);        
        $product_id = trim($this->_request->getParam('product_id', ''));
        $to_product_barcode = trim($this->_request->getParam("to_product_barcode", ""));
        $searchDataType = $this->_request->getParam('searchDateType', '1');
        $timeStart = $this->_request->getParam('timeStart');
        $timeEnd = $this->_request->getParam('timeEnd');
        $isQc = $this->_request->getParam('E12');
        $condition = array(
            'swo_code' => $swo_code,
            'serial_code' => $serial_code,
            'customer_code' => $customer_code,
            'swo_status' => $swo_status,
            'from_customer_code' => $from_customer_code,
            'to_product_barcode' => $to_product_barcode,
            'has_customer_qc' => $isQc,
        );
        if ($searchDataType == 1) {
            $condition['add_time_start'] = $timeStart;
            $condition['add_time_end'] = $timeEnd;
        } else {
            $condition['putway_time_start'] = $timeStart;
            $condition['putway_time_end'] = $timeEnd;
        }
 
        $product = Service_Product::getByField($product_id, 'product_barcode');
        if (!empty($product_id)) {
            if (!empty($product)) {
                $condition['product_id'] = $product['product_id'];
            }
        }
        $orderHtml = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><table border="1"><tr>';
        $htmlTitle = '<td>易货单号</td>';
        $htmlTitle .= '<td>自定义单号</td>';
        $htmlTitle .= '<td>客户代码</td>';
        $htmlTitle .= '<td>公司名称</td>';
        $htmlTitle .= '<td>易货单状态</td>';
        $htmlTitle .= '<td>创建时间</td>';
        $htmlTitle .= '<td>备注</td>';
        $htmlTitle .= '<td>From客户代码</td>';
        $htmlTitle .= '<td>FromSKU</td>';
        $htmlTitle .= '<td>From产品代码</td>';
        $htmlTitle .= '<td>From产品名称</td>';
        $htmlTitle .= '<td>FromSKU数量</td>';
        $htmlTitle .= '<td>To客户代码</td>';
        $htmlTitle .= '<td>ToSKU</td>';
        $htmlTitle .= '<td>To产品代码</td>';
        $htmlTitle .= '</tr>';
        $orderContent = '';

        $rows = Service_SwapOrder::getJoinDetailByCondition($condition, '*');
        if (empty($rows)) {
            $rows = Service_SwapOrder::getAll();
        }    //print_r($rows);exit;
        foreach ($rows as $keyo => $swapOrder) {
            $swapOrderD = Service_SwapOrderDetail::getByCondition(array('swo_id' => $swapOrder['swo_id']));
            if (!empty($swapOrderD)) {
                //$swapOrderDetail=$swapOrderDetail[0];
                foreach ($swapOrderD as $key => $swapOrderDetail) {
                    if (!empty($condition['product_id'])) {
                        if ($condition['product_id'] != $swapOrderDetail['product_id']) {
                            continue;
                        }
                    }
                    if (!empty($condition['from_customer_code'])) {
                        if ($condition['from_customer_code'] != $swapOrderDetail['from_customer_code']) {
                            continue;
                        }
                    }
                    if (!empty($condition['to_product_barcode'])) {
                        if ($condition['to_product_barcode'] != $swapOrderDetail['to_product_barcode']) {
                            continue;
                        }
                    }
                    $product = Service_Product::getByField($swapOrderDetail['product_id'], 'product_id');

                    $to_product = Service_Product::getByField($swapOrderDetail['to_product_id'], 'product_id');
                    $customer_code = Service_Customer::getByField($swapOrder['customer_code'], 'customer_code');
                    $orderContent.="<tr>";
                    //易货单号
                    $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$swapOrder['swo_code']}</td>";
                    //自定义单号
                    $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$swapOrder['serial_code']}</td>";
                    // 客户代码
                    $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$swapOrder['customer_code']}</td>";
                    //公司名称
                    $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$customer_code['customer_company_name']}</td>";
                    //易货单状态
                    $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>" . $status[$swapOrder['swo_status']] . "</td>";
                    //创建时间
                    $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$swapOrder['add_time']}</td>";
                    //备注
                    $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$swapOrder['remark']}</td>";
                    //From客户代码
                    $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$swapOrderDetail['from_customer_code']}</td>";
                    //FromSKU
                    $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$swapOrderDetail['from_sku']}</td>";
                    //From产品代码
                    $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$product['product_barcode']}</td>";
                    $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$product['product_title']}</td>";
                    $orderContent.="<td>{$swapOrderDetail['quantity']}</td>";
                    $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$to_product['customer_code']}</td>";
                    $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$swapOrderDetail['to_sku']}</td>";
                    $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$swapOrderDetail['to_product_barcode']}</td>";

                    $orderContent.="</tr>";
                }
            }
        }
        echo $orderHtml . $htmlTitle . $orderContent . '</table>';
        header('Pragma:public');
        header('Content-Type:application/x-msexecl;name="' . $fileName . ".{$exportformat}");
        header("Content-Disposition:inline;filename=" . $fileName . ".{$exportformat}");

        exit;
    }

    public function autoCompleteAction(){
        $picking_code = $this->_request->getParam("picking_code","");
        $result = array('state' => 0, 'message' => array());
        $pickRow = Service_Picking::getByField($picking_code, 'picking_code');
        if(empty($pickRow)){
            $result['message'] = '配货单号[' . $picking_code . ']不存在';
            //continue;
            die(Zend_Json::encode($result));
        }
        $customerLccodeArr = array(
            'E1033'=>'CH-A001',
            'E0851'=>'SH-A001',
            'E0909'=>'MC-A001',
            'E0693'=>'HQ-A001',
            'E0750'=>'TT-A001',
            //'E0174'=>'QWE003',
        );
        $controArr = array();
        if ($pickRow['picking_type'] != '0') {
            //$result['message'][] = '单号:' . $picking_code . ':配货单类型为一票一件,不须要下架确认.';
            //continue;
            if ($pickRow['picking_status'] != '0') {
                $result['message'] = '单号:' . $picking_code . ':配货单状态已完成,不允许操作.';
                //continue;
                die(Zend_Json::encode($result));
            }
            if ($pickRow['is_assign'] == '0') {
                $result['message'] = '单号:' . $picking_code . ':配货员未分配,不允许操作.';
                die(Zend_Json::encode($result));
                //continue;
            }
            $pickCodes = array();
            $pickCodes[] = $picking_code;
            $obj = new Service_PickProcess();
            $return = $obj->confirmPickerTransaction($pickCodes);
            if($return['state']!="1"){
                $result['message'] = $return['message'][0];
                die(Zend_Json::encode($result));
            }else{
                $controArr[] = "配货单[".$picking_code."]下架成功";
            }
        }
        $pickDetail = Service_PickingDetail::getByCondition(array('picking_code'=>$picking_code),"*");
        $qcList = Service_QualityControl::getByCondition(array('receiving_code'=>$pickDetail[0]['order_code']),"*");
        //var_dump($qcList);
        //exit;
        $putaway = array();
        foreach($qcList as $qc){
            $product = Service_Product::getByField($qc['product_id'],"product_id","*");
            $swd = Service_SwapOrderDetail::getByField($qc['rd_id'],"swod_id","*");
            $params = array(
                'userId'=>Service_User::getUserId(),
                'qcId'=>$qc['qc_id'],
                'productId'=>$qc['product_id'],
                'productBarcode'=>$qc['product_barcode'],
                'customerCode'=>$qc['customer_code'],
                'productCategoryId'=>0,
                'firstReceived'=>0,
                'productBarcodeType'=>$product['product_barcode_type'],
                'productWeight'=>$product['product_weight'],
                'receivedQty'=>$qc['qc_quantity'],
                'problemQty'=>0,
                'noLabelQty'=>0,
                'shelf'=>'',
                'product'=>array(
                    'category'=>'1',
                    'package'=>'1',
                    'declaredValue'=>$product['product_declared_value'],
                    'length'=>$product['product_length'],
                    'width'=>$product['product_width'],
                    'height'=>$product['product_height'],
                ),
                'is_qc'=>$swd['is_qc'],
                'qc_subject'=>$swd['qc_subject'],
                'qc_category'=>$swd['qc_category'],
                'qc_quantity'=>$swd['qc_quantity'],
                'real_qc_category'=>$swd['real_qc_category'],
                'real_qc_quantity'=>$swd['real_qc_quantity'],
                'qualified_rate'=>$swd['qualified_rate'],
                'qc_remark'=>$swd['qc_remark'],
            );
            $qcObj = new Service_SwapOrderProcess();
            $return2 = $qcObj->qcSubmit($params);
            //var_dump($return2);
            if(isset($customerLccodeArr[$qc['customer_code']])){
                $lc_code = $customerLccodeArr[$qc['customer_code']];
            }else{
                $lc_code = "A88";
            }
            if($return2['state']!="1"){
                $result['ask'] = 0;
                $message = "";
                foreach($return2['error'] as $k=>$v){
                    $message.=$v['errorMsg'].";";
                }
                $controArr[] = "质检单号[".$qc['qc_code']."]质检失败:".$message."\n";
                $result['error'] = $controArr;
                exit(Zend_Json::encode($result));
            }else{
                $pd = Service_PutawayDetail::getByField($qc['qc_code'],"qc_code","*");
                $pdd = array(
                    'pd_id'=>$pd['pd_id'],
                    'lc_code'=>$lc_code,
                    'product_barcode'=>$pd['product_barcode'],
                    'qc_code'=>$qc['qc_code'],
                );
                $putaway[] = $pdd;
                $controArr[] = "质检单号[".$qc['qc_code']."]质检成功!\n";
            }
        }

        if(!empty($putaway)){
            foreach($putaway as $pta){
                $param = array();
                $param[] = $pta;
                $sum=0;
                $putawayObj = new Service_PutawayProcess();
                $ret = $putawayObj->submit($param);
                if ($ret['state']==1) {
                    foreach ($param as $key => $value) {
                        $putd=Service_PutawayDetail::getByField($value['pd_id'],'pd_id');
                        $pd=Service_PutawayDetail::getByCondition(array('qc_code'=>$putd['qc_code'],'pd_type'=>2),'*');
                        $qc_code=$putd['qc_code'];
                        //print_r($pd);exit;
                        Service_Box::update(array('box_status'=>0,'product_barcode'=>'','product_id'=>'','box_quantity'=>'','receiving_code'=>''),$putd['box_code'],'box_code');
                        Service_PutawayDetail::update(array('box_code'=>''),$putd['pd_id'],'pd_id');
                        foreach ($pd as $k => $v) {
                            if ($v['pd_status']=='0') {$sum=1;}
                        }
                    }
                    // print_r($sum);exit;
                    if ($sum==0) {
                        Service_QualityControl::update(array('qc_status'=>2),$pta['qc_code'],'qc_code');
                    }
                    $controArr[] = "质检单号[".$pta['qc_code']."]上架成功!\n";
                }else{
                    $result['ask'] = 0;
                    $message = "";
                    foreach($ret['error'] as $k=>$v){
                        $message.=$v['errorMsg'].";";
                    }
                    $controArr[] = "质检单号[".$pta['qc_code']."]上架失败:".$message."\n";
                    $result['error'] = $controArr;
                    exit(Zend_Json::encode($result));
                }
            }
        }
        $result['ask'] = 1;
        $result['message'] = "操作成功!\n";
        $result['error'] = $controArr;
        exit(Zend_Json::encode($result));
    }

}
