<?php
class Shipment_OutboundBagController extends Ec_Controller_Action
{
    public function preDispatch()
    {
        $this->tplDirectory = "shipment/views/order/";
        $this->serviceClass = new Service_OutboundBag();
    }

    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['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'));
            // }
            //$count = $this->serviceClass->getLeftJoinUserByCondition($condition,'count(*)');
            $count = $this->serviceClass->getJoinOrderByCondition($condition, 'count(*)');
            $return['total'] = $count;

            if ($count) {
                $showFields=array(
                'ob_no',
                'warehouse_id',
                'ob_status',
                'sm_code',
                'ob_weight',
                'creater_id',
                'ob_add_time',
                'ob_update_time',
                'ob_id',
                'ob_num',
                );
                $showFields = $this->serviceClass->getFieldsAlias($showFields);
                //$rows = $this->serviceClass->getLeftJoinUserByCondition($condition,$showFields, $pageSize, $page, array('ob_id desc'));
                $rows = $this->serviceClass->getJoinOrderByCondition($condition,$showFields, $pageSize, $page, array('ob_id desc'));
                foreach($rows as $key=>$value){
                    $user = Service_User::getByField($value['E6'],"user_id","*");
                    $rows[$key]['user_name'] = isset($user['user_code'])?$user['user_code']:"";
                }
                $return['data'] = $rows;
                $return['state'] = 1;
                $return['status'] = Common_Status::bagStatus('auto');
                $return['warehouse'] = Common_DataCache::getWarehouseSimple();
                $return['message'] = "";
            }
            die(Zend_Json::encode($return));
        }
        $this->view->SM=Common_DataCache::getShippingMethodSimple();
        echo Ec::renderTpl($this->tplDirectory . "outbound_bag_index.tpl", 'layout');
    }
    
    public function finishAction() {
    	$response = array('state'=>0, 'message'=>'', 'smCode' => '', 'ob_no' => '', 'ob_num' => '');
    	$ob_no = trim($this->_request->getParam('ob_no'));
    	if(empty($ob_no)) exit(Zend_Json::encode(array('state'=>0, 'message'=>'请输入包袋号')));
    	$aOutboundBag = Service_OutboundBag::getByField($ob_no, 'ob_no');
    	if(empty($aOutboundBag)) exit(Zend_Json::encode(array('state'=>0, 'message'=>'输入的包袋号不存在')));

        $response['smCode'] = $aOutboundBag['sm_code'];
        $response['ob_no'] = $aOutboundBag['ob_no'];
        
    	if(intval($aOutboundBag['ob_status'])!==0) exit(Zend_Json::encode(array('state'=>0, 'message'=>'包袋不是草稿状态，无法完成装袋')));
    	$sm_channel = $this->_request->getParam('sm_channel','0');
    	/*if($sm_channel=='1') {
    		$total_weight = trim($this->_request->getParam('total_weight'));
    		if(empty($total_weight)) exit(Zend_Json::encode(array('state'=>0, 'message'=>'请输入总重量')));
    		if(!is_numeric($total_weight)) exit(Zend_Json::encode(array('state'=>0, 'message'=>'总重量只能是数字')));
    		$total_weight = floatval($total_weight);
    		if($total_weight <= 0) exit(Zend_Json::encode(array('state'=>0, 'message'=>'总重量必须大于0')));
    	} else $total_weight = 0;*/
        $total_weight = 0;
    	//处理
    	$soProcess = new Service_ShipOrderProcess();
    	$result = $soProcess->finishBag($aOutboundBag, $total_weight,$sm_channel);
    	if($result === true) {
            $response['state'] = 1;
            $bOutboundBag = Service_OutboundBag::getByField($ob_no, 'ob_no');
            $response['ob_num'] = $bOutboundBag['ob_num'];
        }else{
            $response['message'] = $result;
        } 
    	exit(Zend_Json::encode($response));
    }

    public function printLabelAction()
    {
        $bagCodes = $this->_request->getParam('bagCodes', "");
        if(empty($bagCodes)){
            die('请选择包袋号');
        }
        $this->view->data = $bagCodes;
        echo Ec::renderTpl($this->tplDirectory . "print_bag_label.tpl", 'layout');
    }

    public function exportAction(){
        set_time_limit(0);
        $exportType = $this->_request->getParam("exportType","");
        $filename = "outboundbag".date('YmdHis').".xls";
        header('Pragma:public');
        header("Content-Type:application/x-msexecl;name={$filename}:charset=utf-8");
        header("Content-Disposition:inline;filename={$filename}");
        //0:草稿;1:已完成;2:已加挂;3:物流完成;4:已出货
        $statusArr = array(
            '0'=>'草稿',
            '1'=>'已完成',
            '2'=>'已加挂',
            '3'=>'物流完成',
            '4'=>'已出货'
        );
        if($exportType=="1"){
            $bagCodes = $this->_request->getParam('bagCodes', "");
            echo "<table border='1'>";
            $title="<tr>";
            $title.="<td>袋子代码</td>";
            $title.="<td>仓库</td>";
            $title.="<td>状态</td>";
            $title.="<td>运输代码</td>";
             $title.="<td>外袋号</td>";
            $title.="<td>袋子重量</td>";
            $title.="<td>创建人</td>";
            $title.="<td>创建时间</td>";
            $title.="<td>更新时间</td>";
            $title.="<td>订单号</td>";
            $title.="<td>服务商单号</td>";
            $title.="<td>实重</td>";
            $title.="<td>体积重</td>";
            $title.="<td>计费重量</td>";
            $title.="</tr>";
           // $title = mb_convert_encoding($title,'GB2312');
            echo $title;
            foreach($bagCodes as $val){
                if($val=="") continue;
                $obRow = Service_OutboundBag::getByField($val,'ob_no',"*");
                $obDetail = Service_OutboundBagDetail::getByCondition(array('ob_no'=>$val),"*");
                 $user = Service_User::getByField($obRow['creater_id'],'user_id');
                 foreach($obDetail as $key=>$obdRow){
                    $content = "<tr>";
                    $content .="<td>{$obRow['ob_no']}</td>";
                    $warehouse = Service_Warehouse::getByField($obRow['warehouse_id'],"warehouse_id","*");
                    $content .="<td>{$warehouse['warehouse_code']}</td>";
                    $content .="<td>{$statusArr[$obRow['ob_status']]}</td>";
                    $content .="<td>{$obRow['sm_code']}</td>";
                    $content .="<td>{$obRow['ob_num']}</td>";
                    $content .="<td>{$obRow['ob_weight']}</td>";
                    $content .="<td>{$user['user_code']}</td>";
                    $content .="<td>{$obRow['ob_add_time']}</td>";
                    $content .="<td>{$obRow['ob_update_time']}</td>";
                    $content.="<td>{$obdRow['order_code']}</td>";
                    $shipOrder = Service_ShipOrder::getByField($obdRow['order_code'],"order_code","*");
                    $content.="<td>{$shipOrder['tracking_number']}</td>";
                    $content.="<td>{$shipOrder['so_weight']}</td>";
                    $content.="<td>{$shipOrder['so_vol_weight']}</td>";
                    //$calWeight =$shipOrder['so_vol_weight']>$shipOrder['so_weight']?$shipOrder['so_vol_weight']:$shipOrder['so_weight'];
                    $calWeight =$shipOrder['so_fee_weight'];
                    $content.="<td>{$calWeight}</td>";
                  $content.="</tr>";
                  //  $content = mb_convert_encoding($content,'GB2312');
                     echo $content;
                }
              
              
            }
               echo "</table>";
        }else{
            $params = $this->_request->getParams();
            $condition = $this->serviceClass->getMatchFields($params);
            $condition['dateFor'] = isset($params['dateFor'])?$params['dateFor']:'';
            $condition['dateTo'] = isset($params['dateTo'])?$params['dateTo']:'';

            $count = $this->serviceClass->getByCondition($condition, 'count(*)');
            $pageSize = 20;
            $totalPage = ceil($count/$pageSize);

              echo "<table border='1'>";
            $title="<tr>";
            $title.="<td>袋子代码</td>";
            $title.="<td>仓库</td>";
            $title.="<td>状态</td>";
            $title.="<td>运输代码</td>";
            $title.="<td>外袋号</td>";
            $title.="<td>袋子重量</td>";
            $title.="<td>创建人</td>";
            $title.="<td>创建时间</td>";
            $title.="<td>更新时间</td>";
            $title.="<td>订单号</td>";
            $title.="<td>服务商单号</td>";
            $title.="<td>实重</td>";
            $title.="<td>体积重</td>";
            $title.="<td>计费重量</td>";
            $title.="</tr>";
            //$title = mb_convert_encoding($title,'GB2312');
            echo $title;

            for($page=1;$page<=$totalPage;$page++){
                $rows = $this->serviceClass->getByCondition($condition,"*", $pageSize, $page, array('ob_id desc'));
                foreach($rows as $key=>$obRow){
                     $user = Service_User::getByField($obRow['creater_id'],'user_id');
                    $obDetail = Service_OutboundBagDetail::getByCondition(array('ob_no'=>$obRow['ob_no']),"*");
                     $warehouse = Service_Warehouse::getByField($obRow['warehouse_id'],"warehouse_id","*");
                    foreach($obDetail as $key=>$obdRow){
                    $content = "<tr>";
                    $content .="<td style='vnd.ms-excel.numberformat:@;'>{$obRow['ob_no']}</td>";
                  
                    $content .="<td>{$warehouse['warehouse_code']}</td>";
                    $content .="<td>{$statusArr[$obRow['ob_status']]}</td>";
                    $content .="<td>{$obRow['sm_code']}</td>";
                    $content .="<td>{$obRow['ob_num']}</td>";
                    $content .="<td>{$obRow['ob_weight']}</td>";
                 
                    $content .="<td>{$user['user_code']}</td>";
                    $content .="<td>{$obRow['ob_add_time']}</td>";
                    $content .="<td>{$obRow['ob_update_time']}</td>";
                    $content.="<td>{$obdRow['order_code']}</td>";
                    $shipOrder = Service_ShipOrder::getByField($obdRow['order_code'],"order_code","*");
                    $content.="<td style='vnd.ms-excel.numberformat:@;'>{$shipOrder['tracking_number']}</td>";
                    $content.="<td>{$shipOrder['so_weight']}</td>";
                    $content.="<td>{$shipOrder['so_vol_weight']}</td>";
                    $calWeight =$shipOrder['so_vol_weight']>$shipOrder['so_weight']?$shipOrder['so_vol_weight']:$shipOrder['so_weight'];
                    $content.="<td>{$calWeight}</td>";
                    $content.="</tr>";
                   // $content = mb_convert_encoding($content,'GB2312');
                    echo $content;
                    }
                    
                }
            }
            echo "</table>";
        }
    }

    public function exportFileAction(){
        $exportType = $this->_request->getParam("downloadType","");
        if($exportType == 1){
            $bagCodes = $this->_request->getParam('bagCodes', "");
            foreach ($bagCodes as $key => $value) {
                $obData = Service_OutboundBag::getByField($value,'ob_no');
                if(!in_array($obData['sm_code'], array('TWPG', 'TWPP')) || !in_array($obData['ob_status'], array(2, 3, 4))){
                    echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
                    echo "<script>alert('此下载只适用于渠道为TWPP、TWPG 且 状态为已加挂之后的袋子');window.close();</script>";exit;
                }
            }
            $condition['ob_no'] = $bagCodes;
            $condition['ob_status'] = array(2, 3, 4);
            $condition['sm_code'] = array('TWPG', 'TWPP');
            $condition['select'] = 'ship';
            $data = Service_OutboundBag::getExportFileJoinByCondition($condition);
            
            $condition['select'] = 'air';
            $airData = Service_OutboundBag::getExportFileJoinByCondition($condition);
        }else{
            $params = $this->_request->getParams();
            $condition = $this->serviceClass->getMatchFields($params);
            $condition['dateFor'] = isset($params['dateFor'])?$params['dateFor']:'';
            $condition['dateTo'] = isset($params['dateTo'])?$params['dateTo']:'';
            $condition['ob_status'] = empty($condition['ob_status']) && is_numeric($condition['ob_status'])===false?array(2, 3, 4):(in_array($condition['ob_status'], array(2, 3, 4))?$condition['ob_status']:'');
            $condition['sm_code'] = empty($condition['sm_code'])?array('TWPG', 'TWPP'):(in_array($condition['sm_code'], array('TWPG', 'TWPP'))?$condition['sm_code']:'');
            // print_r($condition);exit;
            if($condition['ob_status'] && $condition['sm_code']){
                $condition['select'] = 'ship';
                $data = Service_OutboundBag::getExportFileJoinByCondition($condition);
                $condition['select'] = 'air';
                $airData = Service_OutboundBag::getExportFileJoinByCondition($condition);
            }else{
                $data = array();
                $airData = array();
            }
            
        }
        
        set_time_limit(0);
        $downloadType = $this->_request->getParam("downloadType","");
        require_once APPLICATION_PATH . '/../libs/PHPExcel1.8/PHPExcel.php';
        require_once APPLICATION_PATH . '/../libs/PHPExcel1.8/PHPExcel/IOFactory.php';
        $cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
        $cacheSettings = array('memoryCacheSize'=>'16MB');
        PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
        $table = new PHPExcel();
        $table->getDefaultStyle()->getFont()->setName('宋体');
        //Sheet1
        $table->setActiveSheetIndex(0);
        $table->setActiveSheetIndex(0)->setTitle("发货预报");
        $objActSheet=$table->setActiveSheetIndex(0);
        $objActSheet->getColumnDimension('B')->setWidth(20);
        $objActSheet->getColumnDimension('C')->setWidth(20);
        $objActSheet->getColumnDimension('D')->setWidth(20);
        $objActSheet->getColumnDimension('E')->setWidth(15);
        $objActSheet->getColumnDimension('F')->setWidth(15);
        $objActSheet->getColumnDimension('G')->setWidth(15);
        $objActSheet->getColumnDimension('H')->setWidth(15);
        $objActSheet->getColumnDimension('I')->setWidth(15);
        $objActSheet->setCellValue('A1','序号');
        $objActSheet->setCellValue('B1','發货日期');
        $objActSheet->setCellValue('C1','客戶单号');
        $objActSheet->setCellValue('D1','類別');
        $objActSheet->setCellValue('E1','提單號碼');
        $objActSheet->setCellValue('F1','SKU');
        $objActSheet->setCellValue('G1','商品名称');
        $objActSheet->setCellValue('H1','英文名稱');
        $objActSheet->setCellValue('I1','規格型號');
        $objActSheet->setCellValue('J1','海關商編');
        $objActSheet->setCellValue('K1','分类');
        $objActSheet->setCellValue('L1','數量');
        $objActSheet->setCellValue('M1','重量(kg)');
        $objActSheet->setCellValue('N1','單位');
        $objActSheet->setCellValue('O1','單價(RMB)');
        $objActSheet->setCellValue('P1','單價(USD)');
        $objActSheet->setCellValue('Q1','申报价值(RMB)');
        $objActSheet->setCellValue('R1','申报价值(USD)');
        $objActSheet->setCellValue('S1','寄件人');
        $objActSheet->setCellValue('T1','电话');
        $objActSheet->setCellValue('U1','地址');
        $objActSheet->setCellValue('V1','省份');
        $objActSheet->setCellValue('W1','城市');
        $objActSheet->setCellValue('X1','收货人');
        $objActSheet->setCellValue('Y1','电话');
        $objActSheet->setCellValue('Z1','國別');
        $objActSheet->setCellValue('AA1','省/郡/縣/市');
        $objActSheet->setCellValue('AB1','地址');
        $objActSheet->setCellValue('AC1','郵編');

        //导入数据到excel
        $index = 2;
        $index1 = 0;
        $order_code ='';
        foreach ($data as $key => $value) {
            if($value=="") continue;
            //服务商单号
            $tracking_number = $value['tracking_number']?$value['tracking_number']:$value['ref_tracking_number'];
            $convert_fee = Service_Currency::converByCode('USD', 'RMB', $value['op_purpose_declared_value']);

            $sm_code = '0';
            // echo strtoupper(sprintf("%08s", $index2));exit;
            if($value['sm_code'] == 'TWPG') $sm_code = 1;

            //设置小数单元格
            $table->getActiveSheet()->getStyle("M".$index)->getNumberFormat()->setFormatCode('0.00');
            $table->getActiveSheet()->getStyle("O".$index)->getNumberFormat()->setFormatCode('0.00');
            $table->getActiveSheet()->getStyle("P".$index)->getNumberFormat()->setFormatCode('0.00');
            $table->getActiveSheet()->getStyle("Q".$index)->getNumberFormat()->setFormatCode('0.00');
            $table->getActiveSheet()->getStyle("R".$index)->getNumberFormat()->setFormatCode('0.00');
            if($order_code != $value['orders_order_code']){
                $order_code = $value['orders_order_code'];
                $objActSheet->setCellValueExplicit('A'.$index,$index-1-$index1,PHPExcel_Cell_DataType::TYPE_STRING);
                $objActSheet->setCellValue("B".$index,'');
                $objActSheet->setCellValue("C".$index,$value['orders_order_code']);
                $objActSheet->setCellValue("D".$index,$sm_code);
                $objActSheet->setCellValue("E".$index,$tracking_number);
                $objActSheet->setCellValue("S".$index,'林文娟');
                $objActSheet->setCellValueExplicit("T".$index,'075526803796',PHPExcel_Cell_DataType::TYPE_STRING);
                $objActSheet->setCellValue("U".$index,'深圳市南山区临海大道53号前海湾保税港区5号楼5403');
                $objActSheet->setCellValue("V".$index,'广东');
                $objActSheet->setCellValue("W".$index,'深圳');
                $objActSheet->setCellValue("X".$index,$value['add_name']);
                $objActSheet->setCellValue("Y".$index,$value['oab_phone']);
                $objActSheet->setCellValue("Z".$index,$value['country_code']);//国别
                $objActSheet->setCellValue("AA".$index,'');
                $objActSheet->setCellValue("AB".$index,$value['oab_street_address1']);
                $objActSheet->setCellValue("AC".$index,$value['oab_postcode']);
            }else{
                $objActSheet->setCellValueExplicit('A'.$index,'',PHPExcel_Cell_DataType::TYPE_STRING);
                $objActSheet->setCellValue("B".$index,'');
                $objActSheet->setCellValue("C".$index,'');
                $objActSheet->setCellValue("D".$index,'');
                $objActSheet->setCellValue("E".$index,'');
                $objActSheet->setCellValue("S".$index,'');
                $objActSheet->setCellValueExplicit("T".$index,'',PHPExcel_Cell_DataType::TYPE_STRING);
                $objActSheet->setCellValue("U".$index,'');
                $objActSheet->setCellValue("V".$index,'');
                $objActSheet->setCellValue("W".$index,'');
                $objActSheet->setCellValue("X".$index,'');
                $objActSheet->setCellValue("Y".$index,'');
                $objActSheet->setCellValue("Z".$index,'');
                $objActSheet->setCellValue("AA".$index,'');
                $objActSheet->setCellValue("AB".$index,'');
                $objActSheet->setCellValue("AC".$index,'');
                $index1 ++;
            }
            $objActSheet->setCellValue("F".$index,$value['product_sku']);
            $objActSheet->setCellValueExplicit("G".$index,$value['product_title'],PHPExcel_Cell_DataType::TYPE_STRING);
            $objActSheet->setCellValueExplicit("H".$index,$value['op_product_title_en'],PHPExcel_Cell_DataType::TYPE_STRING);
            $objActSheet->setCellValue("I".$index,'');
            $objActSheet->setCellValue("J".$index,'');
            $objActSheet->setCellValue("K".$index,'');
            $objActSheet->setCellValue("L".$index,$value['op_quantity']);
            $objActSheet->setCellValueExplicit("M".$index,$value['product_weight'],PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $objActSheet->setCellValue("N".$index,$value['pu_name']);
            $objActSheet->setCellValue("O".$index,$convert_fee);
            $objActSheet->setCellValue("P".$index,$value['op_purpose_declared_value']);//默认美元
            $objActSheet->setCellValue("Q".$index,$convert_fee*$value['op_quantity']);
            $objActSheet->setCellValue("R".$index,$value['op_purpose_declared_value']*$value['op_quantity']);
            $index ++;
        }

        //Sheet1
        
        //Sheet2
        $msgWorkSheet = new PHPExcel_Worksheet($table, 'card_message');
        $table->addSheet($msgWorkSheet);
        $table->setActiveSheetIndex(1);
        $table->setActiveSheetIndex(1)->setTitle("空运装袋明细");
        $objActSheet1=$table->setActiveSheetIndex(1);
        $objActSheet1->getColumnDimension('A')->setWidth(10);
        $objActSheet1->getColumnDimension('B')->setWidth(20);
        $objActSheet1->getColumnDimension('C')->setWidth(20);
        $objActSheet1->getColumnDimension('D')->setWidth(20);
        $objActSheet1->getColumnDimension('F')->setWidth(15);
        $objActSheet1->getColumnDimension('G')->setWidth(15);
        $objActSheet1->getColumnDimension('H')->setWidth(15);
        $objActSheet1->getColumnDimension('I')->setWidth(15);
        $objActSheet1->setCellValue('A1','序号');
        $objActSheet1->setCellValue('B1','船(航)班日期');
        $objActSheet1->setCellValue('C1','船(航)班編號');
        $objActSheet1->setCellValue('D1','櫃號(集裝箱)');
        $objActSheet1->setCellValue('E1','');
        $objActSheet1->setCellValue('F1','外袋條碼');
        $objActSheet1->setCellValue('G1','交換局代碼');
        $objActSheet1->setCellValue('H1','袋(毛重)');
        $objActSheet1->setCellValue('I1','袋(長)');
        $objActSheet1->setCellValue('J1','袋(寬)');
        $objActSheet1->setCellValue('K1','袋(高)');
        $objActSheet1->setCellValue('L1','提單號碼');
        $objActSheet1->setCellValue('M1','備註');

        $index = 2;
        $index1 = 0;
        $ob_num ='';
        foreach ($airData as $key => $value) {
            if($value=="") continue;
            //服务商单号
            $tracking_number = $value['tracking_number']?$value['tracking_number']:$value['ref_tracking_number'];
            if($value['ob_num']=="")continue;
            //互换局
            $hjtwCode = Service_HjtwApi::getCode($value['oab_country_id'],$value['oab_postcode'],$value['sm_code']);
            $table->getActiveSheet()->getStyle("H".$index)->getNumberFormat()->setFormatCode('0.00');
            if($ob_num != $value['ob_num']){
                $ob_num = $value['ob_num'];
                $objActSheet1->setCellValueExplicit('A'.$index,$index-1-$index1,PHPExcel_Cell_DataType::TYPE_STRING);
                $objActSheet1->setCellValue("F".$index,$value['ob_num']);
                $objActSheet1->setCellValue("G".$index,$hjtwCode);
                $objActSheet1->setCellValue("H".$index,$value['ob_weight']);
                $objActSheet1->setCellValue("I".$index,'120');
                $objActSheet1->setCellValue("J".$index,'100');
                $objActSheet1->setCellValue("K".$index,'0');
                $objActSheet1->setCellValue("L".$index,$tracking_number);
            }else{
                $objActSheet1->setCellValueExplicit('A'.$index,'',PHPExcel_Cell_DataType::TYPE_STRING);
                $objActSheet1->setCellValue("L".$index,$tracking_number);
                $index1 ++;
            }
            $index ++;
        }
        //Sheet2
        
        ///Sheet3
        $msgWorkSheet = new PHPExcel_Worksheet($table, 'card_message'); //创建一个工作表
        $table->addSheet($msgWorkSheet); //插入工作表
        $table->setActiveSheetIndex(2); //切换到新创建的工作表
        $table->setActiveSheetIndex(2)->setTitle("海运装袋明细");
        $objActSheet2=$table->setActiveSheetIndex(2);
        $objActSheet2->getColumnDimension('A')->setWidth(10);
        $objActSheet2->getColumnDimension('B')->setWidth(20);
        $objActSheet2->getColumnDimension('C')->setWidth(20);
        $objActSheet2->getColumnDimension('D')->setWidth(20);
        $objActSheet2->getColumnDimension('E')->setWidth(15);
        $objActSheet2->getColumnDimension('F')->setWidth(15);
        $objActSheet2->getColumnDimension('G')->setWidth(15);
        $objActSheet2->getColumnDimension('H')->setWidth(15);
        $objActSheet2->getColumnDimension('I')->setWidth(15);
        $objActSheet2->setCellValue('A1','序号');
        $objActSheet2->setCellValue('B1','發車日期');
        $objActSheet2->setCellValue('C1','發車時間');
        $objActSheet2->setCellValue('D1','車號');
        $objActSheet2->setCellValue('E1','司機姓名');
        $objActSheet2->setCellValue('F1','外袋條碼');
        $objActSheet2->setCellValue('G1','交換局代碼');
        $objActSheet2->setCellValue('H1','袋(毛重)');
        $objActSheet2->setCellValue('I1','袋(長)');
        $objActSheet2->setCellValue('J1','袋(寬)');
        $objActSheet2->setCellValue('K1','袋(高)');
        $objActSheet2->setCellValue('L1','提單號碼');
        $objActSheet2->setCellValue('M1','備註');
        //Sheet3

        $objWriter = PHPExcel_IOFactory::createWriter($table, 'Excel2007');
        header( "Pragma: public" );
        header( "Expires: 0" );
        header("Accept-Ranges: bytes");
        header('cache-control:must-revalidate');
        header("Content-Disposition: attachment; filename=hjtw_file".date('YmdHis').'.xls');
        header("Content-Type:APPLICATION/OCTET-STREAM;charset=utf-8");
        $objWriter->save('php://output');
    }
    public function cbagAction(){
        if (!empty($_GET)) {
           $bagCodes = $_GET['bagCodes'];
           $outboundbag=Service_OutboundBag::getByField($bagCodes,'ob_no');
            if (!empty($outboundbag)) {
                if ($outboundbag['ob_status']==1) {
                   echo 1;  //袋子状态为已完成
                    $row['ob_status']=0;  //草稿状态
                    $row['ob_weight']=0;
                    $re=Service_OutboundBag::update($row,$bagCodes,'ob_no');
                    if ($re) {    
                        $detail=Service_OutboundBagDetail::getByCondition(array('ob_no'=>$bagCodes),'*'); 
                        $order_weight = 0;           
                        foreach ($detail as $key => $value) {
                          // echo $value['order_code'];
                          // $order_weight += $value['so_weight'];
                           $rows['update_time'] = date('Y-m-d H:i:s');
                           $rows['order_status']=8;
                           $order=Service_Orders::update($rows,$value['order_id'],'order_id');//修改订单状态
                           if($order){
                                $uOrderShip['so_update_time'] = date('Y-m-d H:i:s');
                                $uOrderShip['so_status'] = 1;
                                Service_ShipOrder::update($uOrderShip, $value['so_code'], 'so_code');
                                $uopt = array(
                                    'finishbag_time'=>date("Y-m-d H:i:s"),
                                    'finishbag_user_id'=>Service_User::getUserId(),
                                );
                    /*            $log=array(
                                    'order_id'=>$value['order_id'],
                                    'order_code'=>$value['order_code'],
                                    'ol_type'=>0,
                                    'order_status_from'=>9,
                                    'order_status_to'=>8,
                                    'ol_add_time'=>date("Y-m-d H:i:s"),
                                    'user_id'=>Service_User::getUserId(),
                                    'ol_ip'=>Common_Common::getIP(),
                                    'ol_comments'=>'取消装袋',

                                    );*/
                            Service_OrderLog::log($value['order_id'],$value['order_code'],9,8,0,'取消装袋');
                                Service_OrderOperationTime::update($uopt,$value['order_code'],"order_code");

                           }else{
                             echo 2;//订单不存在
                           }
                        }
                    }else{
                        echo 3;//袋子修改失败
                    }
                }else{
                    echo 0;  //袋子状态不是已完成状态
                }
            }
        }  
        
    }
}