<?php

class Customer_CustomerWarehouseFeeController extends Ec_Controller_Action{
    public function preDispatch()
    {
        $this->tplDirectory = "customer/views/default/";
        $this->serviceClass = new Service_CustomerWarehouseFee();
    }

    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 = array(
                'customer_code' => $this->_request->getParam('customer_code',""),
                'dateFor' => $this->_request->getParam('dateFor',""),
                'dateTo' => $this->_request->getParam('dateTo',""),
                'fee_id' => $this->_request->getParam("fee_id",""),
            );
            $count = $this->serviceClass->getByCondition($condition, 'count(*)');
            $return['total'] = $count;

            if ($count) {
                $rows = $this->serviceClass->getByCondition($condition,"*", $pageSize, $page," cwf_id desc");
                foreach($rows as $key=>$value){
                    //$warehouse = Service_Warehouse::getByField($value['warehouse_id'],"warehouse_id","*");
                   // $rows[$key]['warehouse_code'] = $warehouse['warehouse_code'];
                    $otherFee = Service_OtherFee::getByField($value['fee_id'],"fee_id","*");
                    $rows[$key]['fee_name'] = $otherFee['fee_name'];
                    if($value['lc_code']!=""){
                        $loction = Service_Location::getByField($value['lc_code'],"lc_code","*");
                        $loctionType = Service_LocationType::getByField($loction['lt_code'],"lt_code","*");
                        $rows[$key]['lwh'] = $loctionType['lt_length']."*".$loctionType['lt_width']."*".$loctionType['lt_height'];
                    }
                }
                $return['data'] = $rows;
                $return['state'] = 1;
                //$return['warehouses'] = $warehouses;
                $return['message'] = "";
            }
            die(Zend_Json::encode($return));
        }
        $warehouses = Service_Warehouse::getAll();
        $this->view->fee = Service_OtherFee::getAll();
        $this->view->warehouses = $warehouses;

        echo Ec::renderTpl($this->tplDirectory . "customer_warehouse_fee_index.tpl", 'layout');
    }
    public function exportFeeAction(){
        $cwfidArr = $this->_request->getParam('cwfidArr','');
        $exportType = $this->_request->getParam("exportType","");
        header('Pragma:public');
        header('Content-Type:application/x-msexecl;name=CustomerWarehouseFee.xls');
        header("Content-Disposition:inline;filename=CustomerWarehouseFee.xls");
        $html = "<table border='1'>";
        $html.="<tr>";
        $html.="<td>客户代码</td>";
        $html.="<td>费用名称</td>";
        $html.="<td>货架号</td>";
        $html.="<td>长*宽*高（CM）</td>";
        $html.="<td>上架时间</td>";
        $html.="<td>免租天数</td>";
        $html.="<td>单价</td>";
        $html.="<td>金额</td>";
        $html.="<td>费用发生时间</td>";
        $html.="</tr>";
        if($exportType=="1"){
            foreach($cwfidArr as $val){
                $customerWarehouseFee = $this->serviceClass->getByField($val,'cwf_id',"*");
                $html.='<tr>';
                $html.="<td>".$customerWarehouseFee['customer_code']."</td>";
                $fee = Service_OtherFee::getByField($customerWarehouseFee['fee_id'],'fee_id',"*");
                if($customerWarehouseFee['fee_id']>0){
                    $html.="<td>".$fee['fee_name']."</td>";
                }else{
                    $html.="<td>物流费</td>";
                }
                $html.="<td>".$customerWarehouseFee['lc_code']."</td>";
                $lwh='';
                if($customerWarehouseFee['lc_code']!=""){
                        $loction = Service_Location::getByField($customerWarehouseFee['lc_code'],"lc_code","*");
                        $loctionType = Service_LocationType::getByField($loction['lt_code'],"lt_code","*");
                        $lwh = $loctionType['lt_length']."*".$loctionType['lt_width']."*".$loctionType['lt_height'];
                        
                    }
                $html.="<td>".$lwh."</td>";
                $html.="<td>".$customerWarehouseFee['cwf_fifo_time']."</td>";
                $html.="<td>".$customerWarehouseFee['limit_time']."</td>";
                $html.="<td>".$customerWarehouseFee['unit_price']."</td>";
                $html.="<td>".$customerWarehouseFee['fee']."</td>";
                $html.="<td>".$customerWarehouseFee['add_time']."</td>";
                $html.="</tr>";
            }
            $html.="</table>";
            echo $html;
            exit;
        }else{
            $condition  = array(
                'customer_code'     => $this->_request->getParam('customer_code',""),
                'dateFor' => $this->_request->getParam('dateFor',""),
                'dateTo' => $this->_request->getParam('dateTo',""),
                'fee_id' => $this->_request->getParam("fee_id",""),
            );
            $count = $this->serviceClass->getByCondition($condition,"count(*)");
            $pageSize = 20;
            $totalPage = ceil($count/$pageSize);
            for($i=1;$i<=$totalPage;$i++){
                $customerWarehouseFeeList = $this->serviceClass->getByCondition($condition,"*",$pageSize,$i,"cwf_id DESC");
                foreach($customerWarehouseFeeList as $customerWarehouseFee){
                    $html.='<tr>';
                    $html.="<td>".$customerWarehouseFee['customer_code']."</td>";
                    $fee = Service_OtherFee::getByField($customerWarehouseFee['fee_id'],'fee_id',"*");
                    if($customerWarehouseFee['fee_id']>0){
                        $html.="<td>".$fee['fee_name']."</td>";
                    }else{
                        $html.="<td>物流费</td>";
                    }
                     $html.="<td>".$customerWarehouseFee['lc_code']."</td>";
                     $lwh='';
                if($customerWarehouseFee['lc_code']!=""){
                        $loction = Service_Location::getByField($customerWarehouseFee['lc_code'],"lc_code","*");
                        $loctionType = Service_LocationType::getByField($loction['lt_code'],"lt_code","*");
                        $lwh = $loctionType['lt_length']."*".$loctionType['lt_width']."*".$loctionType['lt_height'];   
                    }
                $html.="<td>".$lwh."</td>";
                $html.="<td>".$customerWarehouseFee['cwf_fifo_time']."</td>";
                $html.="<td>".$customerWarehouseFee['limit_time']."</td>";
                $html.="<td>".$customerWarehouseFee['unit_price']."</td>";
                $html.="<td>".$customerWarehouseFee['fee']."</td>";
                $html.="<td>".$customerWarehouseFee['add_time']."</td>";
                $html.="</tr>";
                }
            }
            $html.="</table>";
            echo $html;
            exit;
        }
    }
}