<?php
class Ship_PriceController extends Ec_Controller_Action
{
    public function preDispatch()
    {
        $this->tplDirectory = "ship/views/";
        $this->serviceClass = new Service_ShipPrice();
        $this->serviceHistoryClass = new Service_ShipPriceHistory();
    }
    /**
     * 列表
     * (non-PHPdoc)
     * @see Ec_Controller_Action::listAction()
     */
    public function listAction()
    {
        $currency=Common_DataCache::getCurrency();
        $type=Common_Type::SMFeeType('auto');
        $warehouse=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();
            $condition = $this->serviceClass->getMatchFields($params);
            unset($params);
            $count = $this->serviceClass->getByCondition($condition, 'count(*)');
            $return['total'] = $count;

            if ($count) {
                $showFields=array(
                    'sm_code',
                    'sp_weight',
                    'sp_cost',
                    'sp_price',
                    'currency_code',
                    'area',
                    'sp_fee_type',
                    'warehouse_id',
                    'sp_id',
                    'sp_ticket_cost',
                    'cost_currency_code',
                    'one_ticket_cbcost',
                    'price_batch_number',
                    'date_start',
                    'date_end'
                );
                $showFields = $this->serviceClass->getFieldsAlias($showFields);
                $rows = $this->serviceClass->getByCondition($condition,$showFields, $pageSize, $page, array('date_start desc', 'sp_id desc'));
                $return['data'] = $rows;
                $return['state'] = 1;
                $return['type'] = $type;
                $return['warehouse'] = $warehouse;
                $return['message'] = "";
            }
            die(Zend_Json::encode($return));
        }
        $this->view->currency=$currency;
        $this->view->type=$type;
        //$this->view->SM=Common_DataCache::getShippingMethodSimple();
        $sm=Service_ShippingMethod::getAll();
        $sm=Common_Common::multisort($sm);
        $this->view->SM =$sm; 
        $this->view->warehouse=$warehouse;
        echo Ec::renderTpl($this->tplDirectory . "price_index.tpl", 'layout');
    }
    /**
     * 编辑
     */
    public function editAction()
    {
        $return = array(
            'state' => 0,
            'message' => '',
            'errorMessage'=>array('Fail.')
        );
        if ($this->_request->isPost()) {
            $params = $this->_request->getParams();
            $row = array(
                'sp_id' => '',
                'sm_code' => '',
                'sp_weight' => '',
                'sm_id' => '',
                'sp_cost' => '',
                'sp_price' => '',
                'currency_code' => '',
                'area' => '',
                'sp_fee_type' => '',
                'warehouse_id' => '',
                'sp_ticket_cost' => '',
                'cost_currency_code' => '', // 成本币种
                'one_ticket_cbcost' => '',
                'date_start' => '',
                'date_end' => ''
            );
            $row = $this->serviceClass->getMatchEditFields($params, $row);
            unset($params);
            $paramId = $row['sp_id'];
            if (!empty($row['sp_id'])) {
                unset($row['sp_id']);
            }
            $smRow = Service_ShippingMethod::getByField($row['sm_id'], 'sm_id');
            $row['sm_id']=$smRow['sm_id'];
            $row['sm_code'] = $smRow['sm_code'];
            unset($smRow);
            $errorArr = $this->serviceClass->validator($row);
            if (!empty($errorArr)) {
                $return = array(
                    'state' => 0,
                    'message'=>'',
                    'errorMessage' => $errorArr
                );
                die(Zend_Json::encode($return));
            }
            $smRow = Service_ShippingMethod::getByField($row['sm_id'], 'sm_id');
            $row['sm_id'] = $smRow['sm_id'];
            unset($smRow);
            if (!empty($paramId)) {
                unset($row['price_batch_number']); /* price_batch_number 不允许修改 */
                $result = $this->serviceClass->update($row, $paramId);
            //  $result1 = $this->serviceHistoryClass->update($row, $paramId);
            } else {
                $result = $this->serviceClass->add($row);
            }
            if ($result) {
                $return['state'] = 1;
                $return['message'] = array('Success.');
            }
        }
        die(Zend_Json::encode($return));
    }
    
    /**
     * 编辑获取数据
     */
    public function getByJsonAction()
    {
        $result = array('state' => 0, 'message' => 'Fail', 'data' => array());
        $paramId = $this->_request->getParam('paramId', '');
        if (!empty($paramId) && $rows = $this->serviceClass->getByField($paramId, 'sp_id')) {
            $rows=$this->serviceClass->getVirtualFields($rows);
            $result = array('state' => 1, 'message' => '', 'data' => $rows);
        }
        die(Zend_Json::encode($result));
    }
    
    /**
     * 删除
     */
    public function deleteAction()
    {
        $result = array(
            "state" => 0,
            "message" => "Fail."
        );
        if ($this->_request->isPost()) {
            $paramId = $this->_request->getPost('paramId');
            if (!empty($paramId)) {
                if ($this->serviceClass->delete($paramId)) {
                    $result['state'] = 1;
                    $result['message'] = 'Success.';
                }
            }
        }
        die(Zend_Json::encode($result));
    }
    
    /**
     * 导出
     */
    public function exportAction(){
        set_time_limit(0);
        $codes = $this->_request->getParam('codes','');     
        $exportformat = 'xls';
        $fileName = 'shipprice'.date('Y-m-d-H-i-s');    
        
        if(!empty($codes)){     //通过选择的订单获取
                    
            if($exportformat=='xls'){
                $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>货币Code</td>';
                $htmlTitle .= '<td>费用类型</td>';
                $htmlTitle .= '<td>运输分区</td>';
                $htmlTitle .= '<td>重量（KG）</td>';
                $htmlTitle .= '<td>成本</td>';
                $htmlTitle .= '<td>费用</td>';
                $htmlTitle .= '<td>单票费用</td>';
                $htmlTitle .= '<td>单票成本</td>';
                $htmlTitle .= '<td>开始时间</td>';
                $htmlTitle .= '<td>结束时间</td>';
                $htmlTitle .= '<td>价格编号</td>';
                $htmlTitle .= '</tr>';                  
                $titlesku = '';
                $orderContent = '';
                $max = 1;               
                foreach($codes as $key=>$id){
                     $shipPrice = Service_ShipPrice::getByField($id, 'sp_id');
                     if(!$shipPrice){continue;}
                     
                     if(!$shipPrice['warehouse_code']){
                            
                        if($shipPrice['warehouse_id']){                     
                             $warehouse = Service_Warehouse::getByField($shipPrice['warehouse_id'],'warehouse_id');
                             if($warehouse){
                                $shipPrice['warehouse_code'] =  $warehouse['warehouse_code'];
                             }else{
                                $shipPrice['warehouse_code'] = '全部';
                             }
                        }else{
                            $shipPrice['warehouse_code'] = '全部';
                        }                           
                            
                     }
                     $shipPrice['sp_fee_type_text'] = Service_ShipPrice::get_sp_fee_type_text($shipPrice['sp_fee_type']); 
                     
                     $orderContent.="<tr>";                 
                     //仓库
                     $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['warehouse_code']}</td>";
                     //运输代码
                     $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['sm_code']}</td>";
                    // 成本币种
                    $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['cost_currency_code']}</td>";
                     //货币Code
                     $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['currency_code']}</td>";                   
                     //费用类型
                     $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['sp_fee_type_text']}</td>";
                     //运输分区
                     $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['area']}</td>";
                     //重量(KG)
                     $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['sp_weight']}</td>";                   
                     //成本
                     $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['sp_cost']}</td>";                    
                     //费用
                     $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['sp_price']}</td>";  
                     $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['sp_ticket_cost']}</td>";
                     $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['one_ticket_cbcost']}</td>"; 
                     $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['date_start']}</td>";
                     $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['date_end']}</td>";
                     $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['price_batch_number']}</td>";
                     $orderContent.="</tr>";
                }       
                $html = $orderHtml.$htmlTitle.$orderContent.'</table>';             
                header('Pragma:public');        
                header('Content-Type:application/x-msexecl;name="' . $fileName . ".{$exportformat}");
                header("Content-Disposition:inline;filename=" . $fileName . ".{$exportformat}");   
                //file_put_contents(APPLICATION_PATH . "/../data/{$fileName}",$html);
                echo $html;
                exit;
                //echo $orderHtml;exit;
                
            }elseif($exportformat=='csv'){
                
            }
                
        }else{      
                $sm_code = $this->_request->getParam('E1','');  
                $warehouse_code = $this->_request->getParam('E9','');   
                $currency_code = trim($this->_request->getParam('E5',''));                                  
                $area = $this->_request->getParam('E6','');  
                    
                $condition  = array(                    
                    'warehouse_code'=>$warehouse_code,
                    'currency_code'=>$currency_code,
                    'sm_code'=>$sm_code,
                    'area'=>$area,                              
                );
            
            $count = Service_ShipPrice::getByCondition($condition, 'count(*)');
            if($count>0){
                $pageSize = 20;
                $pages = ceil($count/20);
                $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>货币Code</td>';
                $htmlTitle .= '<td>费用类型</td>';
                $htmlTitle .= '<td>运输分区</td>';
                $htmlTitle .= '<td>重量（KG）</td>';
                $htmlTitle .= '<td>成本</td>';
                $htmlTitle .= '<td>费用</td>';
                $htmlTitle .= '<td>单票费用</td>';
                $htmlTitle .= '<td>单票成本</td>';    
                $htmlTitle .= '<td>开始时间</td>';
                $htmlTitle .= '<td>结束时间</td>';
                $htmlTitle .= '<td>价格编号</td>';
                $htmlTitle .= '</tr>';  
                $orderContent = '';
                $max = 1;

                for($page=1;$page<=$pages;$page++){
                    $rows = Service_ShipPrice::getByCondition($condition, '*', $pageSize,$page,'sp_id desc');
                    if(!$rows){continue;}
                    
                    foreach($rows as $keyo=>$shipPrice){
                    
                     $shipPrice['sp_fee_type_text'] = Service_ShipPrice::get_sp_fee_type_text($shipPrice['sp_fee_type']); 
                     
                     if(!$shipPrice['warehouse_code']){
                            
                        if($shipPrice['warehouse_id']){                     
                             $warehouse = Service_Warehouse::getByField($shipPrice['warehouse_id'],'warehouse_id');
                             if($warehouse){
                                $shipPrice['warehouse_code'] =  $warehouse['warehouse_code'];
                             }else{
                                $shipPrice['warehouse_code'] = '全部';
                             }
                        }else{
                            $shipPrice['warehouse_code'] = '全部';
                        }                           
                            
                     }
                        $orderContent.="<tr>";                  
                        //仓库
                        $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['warehouse_code']}</td>";
                        //运输代码
                        $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['sm_code']}</td>";
                        // 成本币种
                        $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['cost_currency_code']}</td>";
                        //货币Code
                        $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['currency_code']}</td>";                    
                        //费用类型
                        $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['sp_fee_type_text']}</td>";
                        //运输分区
                        $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['area']}</td>";
                        //重量(KG)
                        $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['sp_weight']}</td>";                    
                        //成本
                        $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['sp_cost']}</td>";                 
                        //费用
                         $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['sp_price']}</td>";  
                         $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['sp_ticket_cost']}</td>";
                         $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['one_ticket_cbcost']}</td>"; 
                         $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['date_start']}</td>";
                         $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['date_end']}</td>";
                         $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['price_batch_number']}</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 batchDeleteAction(){
        $info = "系统错误";     
        $codesArray = $this->_request->getParam('codes','');
        if(empty($codesArray) || !is_array($codesArray)){  echo "没有选择待删除记录!!!";exit; }
        Service_ShipPrice::batchDelete($codesArray,'sp_id');
        $info = "已经删除";
        echo $info;
    }

    /**
     * 批量拷贝到history表
     */
    public function batchCopyToHistoryAction(){
        $info = "系统错误";     
        $codesArray = $this->_request->getParam('codes','');
        if(empty($codesArray) || !is_array($codesArray)){  echo "没有选择待拷贝的记录!!!";exit; }
        Service_ShipPrice::batchCopyToHistory($codesArray,'sp_id');
        $info = "已经拷贝";
        echo $info;
    }

    /**
     * @todo:客户运输价格列表
     *
     */
    public function customerPriceListAction()
    {
        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();
            $obj = new Service_ShipCustomerPrice();
            $condition = $obj->getMatchFields($params);
            unset($params);
            $count = Service_ShipCustomerPrice::getByCondition($condition, 'count(*)');
            $return['total'] = $count;

            if ($count) {
                $showFields=array(
                    'sm_code',
                    'scp_weight',
                    'scp_cost',
                    'scp_price',
                    'currency_code',
                    'area',
                    'scp_fee_type',
                    'warehouse_id',
                    'scp_id',
                    'scp_ticket_cost',
                    'cost_currency_code',
                    'one_ticket_cbcost',
                    'price_batch_number',
                    'date_start',
                    'date_end',
                    'customer_code'
                );
                $showFields = $obj->getFieldsAlias($showFields);
                $rows = Service_ShipCustomerPrice::getByCondition($condition,$showFields, $pageSize, $page, array('date_start desc', 'scp_id desc'));

                $return['data'] = $rows;
                $return['state'] = 1;
                $return['message'] = "";
            }
            die(Zend_Json::encode($return));
        }
        $currency = Common_DataCache::getCurrency();
        $warehouse = Common_DataCache::getWarehouseSimple();
        $type = Common_Type::SMFeeType('auto');
        $this->view->currency = $currency;
        $this->view->type=$type;
        //$this->view->SM=Common_DataCache::getShippingMethodSimple();
        $sm=Service_ShippingMethod::getAll();
        $sm=Common_Common::multisort($sm);
        $this->view->SM = $sm;
        $this->view->warehouse = $warehouse;
        echo Ec::renderTpl($this->tplDirectory . "customer_price.tpl", 'layout');
    }

    /**
     * 编辑获取数据
     */
    public function customerPriceGetByJsonAction()
    {
        $result = array('state' => 0, 'message' => 'Fail', 'data' => array());
        $paramId = $this->_request->getParam('paramId', '');
        if (!empty($paramId) && $rows = Service_ShipCustomerPrice::getByField($paramId, 'scp_id')) {
            $obj = new Service_ShipCustomerPrice();
            $rows = $obj->getVirtualFields($rows);
            $result = array('state' => 1, 'message' => '', 'data' => $rows);
        }
        die(Zend_Json::encode($result));
    }

    /**
     * 编辑
     */
    public function customerPriceEditAction()
    {
        $return = array(
            'state' => 0,
            'message' => '',
            'errorMessage'=>array('Fail.')
        );
        if ($this->_request->isPost()) {
            $params = $this->_request->getParams();
            $row = array(
                'scp_id' => '',
                'warehouse_id' => '',
                'sm_code' => '',
                'area' => '',
                'scp_weight' => '',
                'scp_price' => '',
                'scp_cost' => '',
                'currency_code' => '',
                'scp_fee_type' => '',
                'sm_id' => '',
                'scp_ticket_cost' => '',
                'cost_currency_code' => '',
                'one_ticket_cbcost' => '',
                'price_batch_number' => '',
                'date_start' => '',
                'date_end' => '',
                'customer_code' => '',
            );
            $obj = new Service_ShipCustomerPrice();
            $row = $obj->getMatchEditFields($params, $row);
            unset($params);
            $paramId = $row['scp_id'];
            if (!empty($row['scp_id'])) {
                unset($row['scp_id']);
            }
            $smRow = Service_ShippingMethod::getByField($row['sm_id'], 'sm_id');
            $row['sm_id']=$smRow['sm_id'];
            $row['sm_code'] = $smRow['sm_code'];
            unset($smRow);
            $errorArr = $obj->validator($row);
            if (!empty($errorArr)) {
                $return = array(
                    'state' => 0,
                    'message'=>'',
                    'errorMessage' => $errorArr
                );
                die(Zend_Json::encode($return));
            }
            if (!empty($paramId)) {
                unset($row['price_batch_number']); /* price_batch_number 不允许修改 */
                unset($row['customer_code']); /* customer_code 不允许修改 */
                $result = Service_ShipCustomerPrice::update($row, $paramId);
                //  $result1 = $this->serviceHistoryClass->update($row, $paramId);
            } else {
                $result = Service_ShipCustomerPrice::add($row);
            }
            if ($result) {
                $return['state'] = 1;
                $return['message'] = array('Success.');
            }
        }
        die(Zend_Json::encode($return));
    }

    /**
     * 删除
     */
    public function customerPriceDeleteAction()
    {
        $result = array(
            "state" => 0,
            "message" => "Fail."
        );
        if ($this->_request->isPost()) {
            $paramId = $this->_request->getPost('paramId');
            if (!empty($paramId)) {
                $priceRow = Service_ShipCustomerPrice::getByField( $paramId, 'scp_id');
                if( !$priceRow ) {
                    $result['message'] = '价格不存在';
                } else {
                    $adapter = Common_Common::getAdapter();
                    $adapter->beginTransaction();
                    try{
                        if ( false == Service_ShipCustomerPrice::delete($paramId))
                            throw new Exception( '删除失败' );

                        //检查是否还有数据，没有就删除配置表 这里可能查出有过期的价格
                        $condition = array(
                            'customer_code' => $priceRow['customer_code'],
                            'warehouse_id' => $priceRow['warehouse_id'],
                            'sm_code' => $priceRow['sm_code'],
                        );
                        $priceRows = Service_ShipCustomerWeightPrice::getByCondition( $condition, '*', 1, 1 );
                        $weightRows = Service_ShipCustomerPrice::getByCondition( $condition, '*', 1, 1 );
                        if( empty( $priceRows ) && empty( $weightRows ) ) {
                            $configRows = Service_ShipCustomerPriceConfig::getByCondition( $condition, '*', 1, 1 );
                            if( $configRows ) {
                                $result = Service_ShipCustomerPriceConfig::delete( $configRows[0]['id'], 'id' );
                                if( !$result )
                                    throw new Exception( '删除失败' );
                            }

                        }
                        $result['state'] = 1;
                        $result['message'] = '删除成功';
                        $adapter->commit();
                    }catch ( Exception $e ) {
                        $result['message'] = '删除失败';
                        $adapter->rollBack();
                    }
                }
            }
        }
        die(Zend_Json::encode($result));
    }

    /**
     * 导出
     */
    public function customerPriceExportAction(){
        set_time_limit(0);
        $codes = $this->_request->getParam('codes','');
        $exportformat = 'xls';
        $fileName = 'shipprice'.date('Y-m-d-H-i-s');

        if(!empty($codes)){     //通过选择的订单获取

            if($exportformat=='xls'){
                $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>货币Code</td>';
                $htmlTitle .= '<td>费用类型</td>';
                $htmlTitle .= '<td>运输分区</td>';
                $htmlTitle .= '<td>重量（KG）</td>';
                $htmlTitle .= '<td>成本</td>';
                $htmlTitle .= '<td>费用</td>';
                $htmlTitle .= '<td>单票费用</td>';
                $htmlTitle .= '<td>单票成本</td>';
                $htmlTitle .= '<td>开始时间</td>';
                $htmlTitle .= '<td>结束时间</td>';
                $htmlTitle .= '<td>价格编号</td>';
                $htmlTitle .= '</tr>';
                $titlesku = '';
                $orderContent = '';
                $max = 1;
                foreach($codes as $key=>$id){
                    $shipPrice = Service_ShipCustomerPrice::getByField($id, 'scp_id');
                    if(!$shipPrice){continue;}

                    if(!$shipPrice['warehouse_code']){

                        if($shipPrice['warehouse_id']){
                            $warehouse = Service_Warehouse::getByField($shipPrice['warehouse_id'],'warehouse_id');
                            if($warehouse){
                                $shipPrice['warehouse_code'] =  $warehouse['warehouse_code'];
                            }else{
                                $shipPrice['warehouse_code'] = '全部';
                            }
                        }else{
                            $shipPrice['warehouse_code'] = '全部';
                        }

                    }
                    $shipPrice['sp_fee_type_text'] = Service_ShipPrice::get_sp_fee_type_text($shipPrice['scp_fee_type']);

                    $orderContent.="<tr>";
                    //仓库
                    $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['customer_code']}</td>";
                    //仓库
                    $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['warehouse_code']}</td>";
                    //运输代码
                    $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['sm_code']}</td>";
                    // 成本币种
                    $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['cost_currency_code']}</td>";
                    //货币Code
                    $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['currency_code']}</td>";
                    //费用类型
                    $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['scp_fee_type_text']}</td>";
                    //运输分区
                    $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['area']}</td>";
                    //重量(KG)
                    $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['scp_weight']}</td>";
                    //成本
                    $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['scp_cost']}</td>";
                    //费用
                    $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['scp_price']}</td>";
                    $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['scp_ticket_cost']}</td>";
                    $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['one_ticket_cbcost']}</td>";
                    $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['date_start']}</td>";
                    $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['date_end']}</td>";
                    $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['price_batch_number']}</td>";
                    $orderContent.="</tr>";
                }
                $html = $orderHtml.$htmlTitle.$orderContent.'</table>';
                header('Pragma:public');
                header('Content-Type:application/x-msexecl;name="' . $fileName . ".{$exportformat}");
                header("Content-Disposition:inline;filename=" . $fileName . ".{$exportformat}");
                //file_put_contents(APPLICATION_PATH . "/../data/{$fileName}",$html);
                echo $html;
                exit;
                //echo $orderHtml;exit;

            }elseif($exportformat=='csv'){

            }

        }else{
            $sm_code = $this->_request->getParam('E2','');
            $warehouseId = $this->_request->getParam('E1','');
            $currency_code = trim($this->_request->getParam('E7',''));
            $area = $this->_request->getParam('E3','');

            $condition  = array(
                'warehouse_id'=>$warehouseId,
                'currency_code'=>$currency_code,
                'sm_code'=>$sm_code,
                'area'=>$area,
            );

            $count = Service_ShipCustomerPrice::getByCondition($condition, 'count(*)');
            if($count>0){
                $pageSize = 20;
                $pages = ceil($count/20);
                $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>货币Code</td>';
                $htmlTitle .= '<td>费用类型</td>';
                $htmlTitle .= '<td>运输分区</td>';
                $htmlTitle .= '<td>重量（KG）</td>';
                $htmlTitle .= '<td>成本</td>';
                $htmlTitle .= '<td>费用</td>';
                $htmlTitle .= '<td>单票费用</td>';
                $htmlTitle .= '<td>单票成本</td>';
                $htmlTitle .= '<td>开始时间</td>';
                $htmlTitle .= '<td>结束时间</td>';
                $htmlTitle .= '<td>价格编号</td>';
                $htmlTitle .= '</tr>';
                $orderContent = '';
                $max = 1;

                for($page=1;$page<=$pages;$page++){
                    $rows = Service_ShipCustomerPrice::getByCondition($condition, '*', $pageSize,$page,'scp_id desc');
                    if(!$rows){continue;}

                    foreach($rows as $keyo=>$shipPrice){

                        $shipPrice['sp_fee_type_text'] = Service_ShipPrice::get_sp_fee_type_text($shipPrice['scp_fee_type']);

                        if(!$shipPrice['warehouse_code']){

                            if($shipPrice['warehouse_id']){
                                $warehouse = Service_Warehouse::getByField($shipPrice['warehouse_id'],'warehouse_id');
                                if($warehouse){
                                    $shipPrice['warehouse_code'] =  $warehouse['warehouse_code'];
                                }else{
                                    $shipPrice['warehouse_code'] = '全部';
                                }
                            }else{
                                $shipPrice['warehouse_code'] = '全部';
                            }

                        }
                        $orderContent.="<tr>";
                        //客户代码
                        $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['customer_code']}</td>";
                        //仓库
                        $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['warehouse_code']}</td>";
                        //运输代码
                        $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['sm_code']}</td>";
                        // 成本币种
                        $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['cost_currency_code']}</td>";
                        //货币Code
                        $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['currency_code']}</td>";
                        //费用类型
                        $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['scp_fee_type_text']}</td>";
                        //运输分区
                        $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['area']}</td>";
                        //重量(KG)
                        $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['scp_weight']}</td>";
                        //成本
                        $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['scp_cost']}</td>";
                        //费用
                        $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['scp_price']}</td>";
                        $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['scp_ticket_cost']}</td>";
                        $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['one_ticket_cbcost']}</td>";
                        $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['date_start']}</td>";
                        $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['date_end']}</td>";
                        $orderContent.="<td style='vnd.ms-excel.numberformat:@;'>{$shipPrice['price_batch_number']}</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;
    }

    /**
     * 批量拷贝到history表
     */
    public function copyToHistoryAction(){
        $info = "系统错误";
        $codesArray = $this->_request->getParam('codes','');
        if(empty($codesArray) || !is_array($codesArray)){  echo "没有选择待拷贝的记录!!!";exit; }
        Service_ShipCustomerPrice::copyToHistory($codesArray,'scp_id');
        $info = "已经拷贝";
        echo $info;
    }

    /**
     * 批量删除
     */
    public function batchDeleteCustomerPriceAction(){
        $info = "系统错误";
        $codesArray = $this->_request->getParam('codes','');
        if(empty($codesArray) || !is_array($codesArray)){  echo "没有选择待删除记录!!!";exit; }
        $configs = array();
        $rows = Service_ShipCustomerPrice::getByCondition( array('scp_id_array' => $codesArray ) );
        if( empty( $rows ) ) {
            echo "待删除记录不存在";
            exit;
        }
        $adapter = Common_Common::getAdapter();
        $adapter->beginTransaction();
        try{

            foreach( $rows as $value ) {
                $k = $value['warehouse_id'].$value['customer_code'].$value['sm_code'];
                $configs[$k] = array(
                    'customer_code' => $value['customer_code'],
                    'warehouse_id' => $value['warehouse_id'],
                    'sm_code' => $value['sm_code']
                );
            }

            $result = Service_ShipCustomerPrice::batchDeleteCustomerPrice($codesArray,'scp_id');
            if( !$result )
                throw new Exception('删除成功');

            //检查是否还有数据，没有就删除配置表
            foreach( $configs as $config ) {
                //这里可能查出有过期的价格
                $priceRows = Service_ShipCustomerWeightPrice::getByCondition( $config, '*', 1, 1 );
                $weightRows = Service_ShipCustomerPrice::getByCondition( $config, '*', 1, 1 );
                if( empty( $priceRows ) && empty( $weightRows ) ) {
                    $configRow = Service_ShipCustomerPriceConfig::getByCondition( $config, '*', 1, 1 );
                    if( $configRow ) {
                        $result = Service_ShipCustomerPriceConfig::delete( $configRow[0]['id'], 'id' );
                        if( !$result )
                            throw new Exception( '删除失败' );
                    }

                }
            }
            $info = "删除成功";
            $adapter->commit();
        } catch( Exception $e ) {
            $info = "删除失败";
            $adapter->rollBack();
        }


        echo $info;
    }
    
}