<?php
/**
 * 费用及成本明细（杂费）
 * 
 */
class Ship_FeeCostDetailController extends Ec_Controller_Action
{
    public function preDispatch()
    {
        $this->tplDirectory = "ship/views/";
        $this->serviceClass = new Service_FeeCostDetail();
    }

    public function listAction(){
        $currency           = Common_DataCache::getCurrency();
        $type               = Common_Type::SMFeeType('auto');
        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"
            );
            $sm_code            = $this->_request->getParam("sm_code","");
            $fc_id              = $this->_request->getParam("fc_id","");
            $code_number        = $this->_request->getParam("code_number","");
            $fee_code           = $this->_request->getParam("fee_code","");                
            $country_code       = $this->_request->getParam("country_code","");
            $reference_no       = $this->_request->getParam('reference_no',"");
            $tracking_number    = $this->_request->getParam("tracking_number","");
            $add_time_start     = $this->_request->getParam("add_time_start","");
            $add_time_end       = $this->_request->getParam("add_time_end","");
            $customer_code      = $this->_request->getParam("customer_code","");
            $sp_code            = $this->_request->getParam("sp_code","");
            $condition          = array(
                'sm_code'           => $sm_code,
                'customer_code'     => $customer_code,
                'sp_code'           => $sp_code,
                'fc_id'             => $fc_id,
                'fee_code'          => $fee_code,
                'code_number'       => $code_number,
                'country_code'      => $country_code,
                'reference_no'      => $reference_no,
                'tracking_number'   => $tracking_number,
                'add_time_start'    => $add_time_start,
                'add_time_end'      => $add_time_end,
            );
            //删选掉成本为0的
            $condition['cost_amount_sp'] = 0;
            $count              = Service_FeeCostDetail::getByCondition($condition,"count(*)");
            $return['total']    = $count;
            if ($count){
                $rows = Service_FeeCostDetail::getByCondition($condition,"*",$pageSize, $page,"fc_id desc");
                foreach($rows as $key=>$value){
                    $country                    = Service_Country::getByField($value['country_code'],"country_code","*");
                    $rows[$key]['country_name'] = $country['country_name'];
                }
                $return['data']     = $rows;
                $return['state']    = 1;
                $return['message']  = "";
            }
            die(Zend_Json::encode($return));
        }
        $this->view->fee        = Service_OtherFee::getAll();
        $this->view->currency   = $currency;
        $this->view->type       = $type;
        $this->view->SM         = Common_DataCache::getShippingMethodSimple();
        $this->view->countryArr = Common_DataCache::getCountry();
        echo Ec::renderTpl($this->tplDirectory . "fee-cost-detail.tpl", 'layout');
    }

    public function exportAction(){
        $crIdArr    = $this->_request->getParam('fcid','');
        $exportType = $this->_request->getParam("exportType","");
        if($exportType=="1"){
            header('Pragma:public');
            header('Content-Type:application/x-msexecl;name=costRecording.xls');
            header("Content-Disposition:inline;filename=costRecording.xls");
            $html   = "<table border='1'>";
            $html  .= "<tr>";
            $html  .= "<td>订单号</td>";
            $html  .= "<td>交易订单号</td>";
            $html  .= "<td>跟踪号</td>";
            $html  .= "<td>运输方式</td>";
            $html  .= "<td>国家</td>";
            $html  .= "<td>杂费代码</td>";
            $html  .= "<td>杂费名称</td>";
            $html  .= "<td>客户代码</td>";
            //$html  .= "<td>费用金额</td>";
            //$html  .= "<td>费用币种</td>";
            $html  .= "<td>服务商代码</td>";
            $html  .= "<td>成本金额</td>";
            $html  .= "<td>成本币种</td>";
            $html  .= "<td>操作人</td>";
            $html  .= "<td>费用发生时间</td>";
            $html  .= "</tr>";

            foreach($crIdArr as $val){
                if($val=="") continue;
                $value = Service_FeeCostDetail::getByField($val,'fc_id',"*");
                $country = Service_Country::getByField($value['country_code'],"country_code","*");
                $html.="<tr>";
                $html.="<td>".$value['code_number']."</td>";
                $html.="<td style='vnd.ms-excel.numberformat:@;'>".$value['reference_no']."</td>";
                $html.="<td style='vnd.ms-excel.numberformat:@;'>".$value['tracking_number']."</td>";
                $html.="<td>".$value['sm_code']."</td>";
                $html.="<td>".$country['country_name']."</td>";
                $html.="<td>".$value['fee_code']."</td>";
                $html.="<td>".$value['fee_name']."</td>";
                $html.="<td>".$value['customer_code']."</td>";
                //$html.="<td>".$value['fee_amount_customer']."</td>";
                //$html.="<td>".$value['fee_currency_code_customer']."</td>";
                $html.="<td>".$value['sp_code']."</td>";
                $html.="<td>".$value['cost_amount_sp']."</td>";
                $html.="<td>".$value['cost_currency_code_sp']."</td>";
                $html.="<td>".$value['operator']."</td>";
                $html.="<td>".$value['fee_time']."</td>";
                $html.="</tr>";
            }
            $html.= "</table>";
            echo $html;
        }else{
            header('Pragma:public');
            header('Content-Type:application/x-msexecl;name=costRecording.xls');
            header("Content-Disposition:inline;filename=costRecording.xls");
            $html  = "<table border='1'>";
            $html  .= "<tr>";
            $html  .= "<td>订单号</td>";
            $html  .= "<td>交易订单号</td>";
            $html  .= "<td>跟踪号</td>";
            $html  .= "<td>运输方式</td>";
            $html  .= "<td>国家</td>";
            $html  .= "<td>杂费代码</td>";
            $html  .= "<td>杂费名称</td>";
            $html  .= "<td>客户代码</td>";
            //$html  .= "<td>费用金额</td>";
            //$html  .= "<td>费用币种</td>";
            $html  .= "<td>服务商代码</td>";
            $html  .= "<td>成本金额</td>";
            $html  .= "<td>成本币种</td>";
            $html  .= "<td>操作人</td>";
            $html  .= "<td>费用发生时间</td>";
            $html  .= "</tr>";

            $sm_code            = $this->_request->getParam("sm_code","");
            $country_code       = $this->_request->getParam("country_code","");
            $code_number        = $this->_request->getParam("code_number","");
            $reference_no       = $this->_request->getParam('reference_no',"");
            $tracking_number    = $this->_request->getParam("tracking_number","");
            $add_time_start     = $this->_request->getParam("add_time_start","");
            $add_time_end       = $this->_request->getParam("add_time_end","");
            $customer_code      = $this->_request->getParam("customer_code","");
            $fee_code           = $this->_request->getParam("fee_code",""); 
            $condition          = array(
                'sm_code'           => $sm_code,
                'customer_code'     => $customer_code,
                'sp_code'           => $sp_code,
                'fee_code'          => $fee_code,
                'country_code'      => $country_code,
                'code_number'       => $code_number,
                'reference_no'      => $reference_no,
                'tracking_number'   => $tracking_number,
                'currency_code'     => $tracking_number,
                'add_time_start'    => $add_time_start,
                'add_time_end'      => $add_time_end,
            );
            $condition['cost_amount_sp'] = 0;
            $rows = Service_FeeCostDetail::getByCondition($condition,"*",0, 0,"country_code desc");
            if(!empty($rows)){
                foreach($rows as $key=>$value){
                    $country = Service_Country::getByField($value['country_code'],"country_code","*");
                    $rows[$key]['country_name'] = $country['country_name'];

                    $html   .= "<tr>";
                    //$html   .= "<td>".$value['customer_code']."</td>";
                    $html   .= "<td>".$value['code_number']."</td>";
                    $html   .= "<td style='vnd.ms-excel.numberformat:@;'>".$value['reference_no']."</td>";
                    $html   .= "<td style='vnd.ms-excel.numberformat:@;'>".$value['tracking_number']."</td>";
                    $html   .= "<td>".$value['sm_code']."</td>";
                    $html   .= "<td>".$country['country_name']."</td>";
                    $html   .= "<td>".$value['fee_code']."</td>";
                    $html   .= "<td>".$value['fee_name']."</td>";
                    $html   .= "<td>".$value['customer_code']."</td>";
                    //$html   .= "<td>".$value['fee_amount_customer']."</td>";
                    //$html   .= "<td>".$value['fee_currency_code_customer']."</td>";
                    $html   .= "<td>".$value['sp_code']."</td>";
                    $html   .= "<td>".$value['cost_amount_sp']."</td>";
                    $html   .= "<td>".$value['cost_currency_code_sp']."</td>";
                    $html   .= "<td>".$value['operator']."</td>";
                    $html   .= "<td>".$value['fee_time']."</td>";
                    $html   .= "</tr>";
                }
            }
            $html.= "</table>";
            echo $html;
        }
    }
}
