<?php
/**
 * Created by JetBrains PhpStorm.
 * User: user
 * Date: 14-2-20
 * Time: 下午3:00
 * To change this template use File | Settings | File Templates.
 */
class Ship_LogisticsCostController extends Ec_Controller_Action
{
    public function preDispatch()
    {
        $this->tplDirectory = "ship/views/";
        $this->serviceClass = new Service_LogisticsCost();
    }

    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","");
            $country_id = $this->_request->getParam("country_id","");
            $order_code = $this->_request->getParam("order_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","");
            $platform = $this->_request->getParam("platform","");
            $condition = array(
                'sm_code'=>$sm_code,
                'country_id'=>$country_id,
                'customer_code'=>$customer_code,
                'order_code'=>$order_code,
                'reference_no'=>$reference_no,
                'tracking_number'=>$tracking_number,
                'add_time_start'=>$add_time_start,
                'add_time_end'=>$add_time_end,
                'platform'=>$platform
            );
            $count = Service_LogisticsCost::getByCondition($condition,"count(*)");
            $return['total'] = $count;
            if ($count){
                $rows = Service_LogisticsCost::getByCondition($condition,"*",$pageSize, $page,"lc_id desc");
                foreach($rows as $key=>$value){
                    $country = Service_Country::getByField($value['country_id'],"country_id","*");
                    $rows[$key]['country_name'] = $country['country_name'];
                    $shipOrder=Service_ShipOrder::getByField($value['order_code'],'order_code');
                    if(!empty($shipOrder) && $shipOrder['so_fee_weight']>0){
                        $rows[$key]['weight'] = $shipOrder['so_fee_weight'];
                    }
                }
                $return['data'] = $rows;
                $return['state'] = 1;
                $return['message'] = "";
            }
            die(Zend_Json::encode($return));
        }
        $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 . "logistics-cost.tpl", 'layout');
    }

    public function exportAction(){
        $lcIdArr = $this->_request->getParam('lcid','');
        $exportType = $this->_request->getParam("exportType","");
        if($exportType=="1"){
            header('Pragma:public');
            header('Content-Type:application/x-msexecl;name=logisticsCost.xls');
            header("Content-Disposition:inline;filename=logisticsCost.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>重量（KG）</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($lcIdArr as $val){
                if($val=="") continue;
                $value = Service_LogisticsCost::getByField($val,'lc_id',"*");
                $country = Service_Country::getByField($value['country_id'],"country_id","*");
                $shipOrder=Service_ShipOrder::getByField($value['order_code'],'order_code');
                $order=Service_Orders::getByField($value['order_code'],'order_code');
                empty($order) ? $order = Service_LogisticsOrder::getByField($value['order_code'],'logistics_code') : NULL;
                $smCondition = array(
                    'warehouse_id_arr' => array(0, (int) $order['warehouse_id']),
                    'sm_code'          => $value['sm_code'],
                    'country_id'       => (int) $value['country_id'],
                );
                //dhl有城市区别
                if(in_array(strtoupper($value['sm_code']), array('HKDHL','DGM-US','BH-DHL-HKDELIVERY'))){
                    $addressRow = Service_OrderAddressBook::getByField($value['order_code'], 'order_code');
                    if($addressRow['oab_country_id']=='253'){
                        //美国的要加入城市
                        $citys = Service_City::getByCondition(array('country_id' =>$addressRow['oab_country_id'],'city_name'=>$addressRow['oab_city'],'sm_code' => $order['sm_code']));
                        $is_exist = empty($citys) && Service_City::getByWhere(array('country_id' => $addressRow['oab_country_id'],'sm_code' => $order['sm_code']));
                        if(empty($citys) && empty($is_exist)){
                            $conditionCity = array(
                                'country_id'=>$addressRow['oab_country_id'],
                                'city_name'=>$addressRow['oab_city'],
                                'sm_code'=>'0'
                            );
                            $citys = Service_City::getByCondition($conditionCity,'*',1,1);
                        }
                        if(!empty($citys)){
                            $smCondition['city_id_arr'] = array(0, $citys[0]['city_id']);
                        }
                    }
                }
                $smArea = Service_SmCountryMap::getByCondition($smCondition,'*',1,1,array('warehouse_id desc', 'country_id desc', 'province_id desc', 'city_id desc'));
                if(!empty($shipOrder) && $shipOrder['so_fee_weight']>0){
                  $value['weight'] = $shipOrder['so_fee_weight'];
                }
                $html.="<tr>";
                $html.="<td>".$value['customer_code']."</td>";
                $html.="<td>".$value['order_code']."</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['weight']."</td>";
                $html.="<td>".$value['transport_fee']."</td>";
                $html.="<td>".$value['ticket_fare']."</td>";
                $html.="<td>".$value['fuel_addons']."</td>";
                $html.="<td>".$value['total_fee']."</td>";
                $html.="<td>".$value['currency_code']."</td>";
                $html.="<td>".$value['add_time']."</td>";
                $html.="<td style='vnd.ms-excel.numberformat:@;'>".($smArea ? $smArea[0]['area'] : '')."</td>";
                $html.="</tr>";
            }
            $html.= "</table>";
            echo $html;
        }else{
            header('Pragma:public');
            header('Content-Type:application/x-msexecl;name=logisticsCost.xls');
            header("Content-Disposition:inline;filename=logisticsCost.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>重量（KG）</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_id = $this->_request->getParam("country_id","");
            $order_code = $this->_request->getParam("order_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","");
            $platform = $this->_request->getParam("platform","");
            $condition = array(
                'sm_code'=>$sm_code,
                'customer_code'=>$customer_code,
                'country_id'=>$country_id,
                'order_code'=>$order_code,
                'reference_no'=>$reference_no,
                'tracking_number'=>$tracking_number,
                'currency_code'=>$tracking_number,
                'add_time_start'=>$add_time_start,
                'add_time_end'=>$add_time_end,
                'platform'=>$platform
            );
            $count=Service_LogisticsCost::getByCondition($condition,"count(*)");
            if($count>0){
            $pageSize=1000;
            $totalPage = ceil($count/$pageSize);
            for($page=1;$page<=$totalPage;$page++){
            $rows = Service_LogisticsCost::getByCondition($condition,"*",$pageSize, $page,"lc_id desc");
            if(!empty($rows)){
                foreach($rows as $key=>$value){
                    $country = Service_Country::getByField($value['country_id'],"country_id","*");
                    $shipOrder=Service_ShipOrder::getByField($value['order_code'],'order_code');
                    $order=Service_Orders::getByField($value['order_code'],'order_code');
                    empty($order) ? $order = Service_LogisticsOrder::getByField($value['order_code'],'logistics_code') : NULL;
                    $smCondition = array(
                        'warehouse_id_arr' => array(0, (int) $order['warehouse_id']),
                        'sm_code'          => $value['sm_code'],
                        'country_id'       => (int) $value['country_id'],
                    );
                    //dhl有城市区别
                    if(in_array(strtoupper($value['sm_code']), array('HKDHL','DGM-US','BH-DHL-HKDELIVERY'))){
                        $addressRow = Service_OrderAddressBook::getByField($value['order_code'], 'order_code');
                        if($addressRow['oab_country_id']=='253'){
                            //美国的要加入城市
                            $citys = Service_City::getByCondition(array('country_id' =>$addressRow['oab_country_id'],'city_name'=>$addressRow['oab_city'],'sm_code' => $order['sm_code']));
                            $is_exist = empty($citys) && Service_City::getByWhere(array('country_id' => $addressRow['oab_country_id'],'sm_code' => $order['sm_code']));
                            if(empty($citys) && empty($is_exist)){
                                $conditionCity = array(
                                    'country_id'=>$addressRow['oab_country_id'],
                                    'city_name'=>$addressRow['oab_city'],
                                    'sm_code'=>'0'
                                );
                                $citys = Service_City::getByCondition($conditionCity,'*',1,1);
                            }
                            if(!empty($citys)){
                                $smCondition['city_id_arr'] = array(0, $citys[0]['city_id']);
                            }
                        }
                    }
                    $smArea = Service_SmCountryMap::getByCondition($smCondition,'*',1,1,array('warehouse_id desc', 'country_id desc', 'province_id desc', 'city_id desc'));
                    if(!empty($shipOrder) && $shipOrder['so_fee_weight']>0){
                      $value['weight'] = $shipOrder['so_fee_weight'];
                    }
                    $rows[$key]['country_name'] = $country['country_name'];
                    $html.="<tr>";
                    $html.="<td>".$value['customer_code']."</td>";
                    $html.="<td>".$value['order_code']."</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['weight']."</td>";
                    $html.="<td>".$value['transport_fee']."</td>";
                    $html.="<td>".$value['ticket_fare']."</td>";
                    $html.="<td>".$value['fuel_addons']."</td>";
                    $html.="<td>".$value['total_fee']."</td>";
                    $html.="<td>".$value['currency_code']."</td>";
                    $html.="<td>".$value['add_time']."</td>";
                    $html.="<td style='vnd.ms-excel.numberformat:@;'>".($smArea ? $smArea[0]['area'] : '')."</td>";
                    $html.="</tr>";
                }
            }
           }
          }
            $html.= "</table>";
            echo $html;
        }
    }

}
