<?php
/**
 * Created by JetBrains PhpStorm.
 * User: user
 * Date: 14-3-10
 * Time: 下午2:44
 * To change this template use File | Settings | File Templates.
 */
class Customer_BanlanceLogController extends Ec_Controller_Action
{
    public function preDispatch()
    {
        $this->tplDirectory = "customer/views/default/";
        $this->serviceClass = new Service_Customer();
    }

    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"
            );

            $condition	= array(
                'customer_code'		=> $this->_request->getParam('customer_code',""),
                'cbl_type'            => $this->_request->getParam('cbl_type',""),
                'cbl_refer_code'	=> trim($this->_request->getParam('cbl_refer_code','')),
                'dateFor' => $this->_request->getParam('dateFor',""),
                'dateTo' => $this->_request->getParam('dateTo',""),
                'fee_id' => $this->_request->getParam("fee_id",""),
                'application_name'=> $this->_request->getParam("application_name",""),
                'cbl_note_like' => trim($this->_request->getParam('cbl_note_like',''))
            );
            if($condition['application_name']!=""){
                $application = Service_Application::getByField($condition['application_name'],'application_name',"*");
                $otherFee = Service_OtherFee::getByCondition(array('fee_application_id'=>$application['application_id']),'*');
                $feeIdArr = array();
                foreach($otherFee as $key=>$value){
                    $feeIdArr[] = $value['fee_id'];
                }
                if($condition['application_name']=="订单"){
                    $feeIdArr[] = 0;
                }
                $condition['fee_id_array'] = $feeIdArr;
            }

            $cblType = array(
                //0:冻结; 1:解冻; 2:扣款; 3:入款；4:退款
                '0'=>'冻结',
                '1'=>'解冻',
                '2'=>'扣款',
                '3'=>'入款',
                '4'=>'退款',
            );

            $count = Service_CustomerBalanceLog::getByCondition($condition,"count(*)");
            $return['total'] = $count;
            if ($count){
                $rows = Service_CustomerBalanceLog::getByCondition($condition,"*",$pageSize,$page,'cbl_id desc');
                foreach($rows as $key=>$value){
                    $user = Service_User::getByField($value['user_id'],"user_id","*");
                    $rows[$key]['user_code'] = $user['user_code'];
                    $rows[$key]['cbltypestr'] = $cblType[$value['cbl_type']];
                    if($rows[$key]['application_code'] == ''){$rows[$key]['application_code'] = 'order';}
                    if($value['fee_id']>0){
                        $otherfee=Service_OtherFee::getByField($value['fee_id']);
                        if(!empty($otherfee)){
                            $rows[$key]['fee_name'] = $otherfee['fee_name'];
                            $application = Service_Application::getByField($otherfee['fee_application_id']);
                            $rows[$key]['application_name'] = $application['application_name'];
                            $rows[$key]['application_code'] = $application['application_code'];
                        }else{
                            $feeType = Service_FeeType::getByField($value['fee_id'],'ft_id');
                            if(!empty($feeType)){
                                $rows[$key]['fee_name'] = $feeType['ft_name_cn'];
                            } else {
                                $rows[$key]['fee_name'] = '';
                            }
                            $rows[$key]['application_name'] = '';
                        }
                    }else{
                        $rows[$key]['fee_name'] = '';
                        $rows[$key]['application_name'] = '';
                    }
                    if('readjustment' == $value['application_code']){
                        $rows[$key]['application_name'] = '重新计费';
                        $rows[$key]['application_code'] = 'readjustment';
                    }
                }
                $return['data'] = $rows;
                $return['state'] = 1;
            }
            die(Zend_Json::encode($return));
        }
        $this->view->fee = Service_OtherFee::getAll();
        echo Ec::renderTpl($this->tplDirectory . "banlance_log.tpl", 'layout');
    }

    public function exportAction(){
        $cblidArr = $this->_request->getParam('cblidArr','');
        $exportType = $this->_request->getParam("exportType","");
        header('Pragma:public');
        header('Content-Type:application/x-msexecl;name=CustomerBalanceLog.xls');
        header("Content-Disposition:inline;filename=CustomerBalanceLog.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.="</tr>";
        $cblType = array(
            '0'=>'冻结',
            '1'=>'解冻',
            '2'=>'扣款',
            '3'=>'入款',
            '4'=>'退款',
        );
        if($exportType=="1"){
            foreach($cblidArr as $val){
                $customerBalanceLog = Service_CustomerBalanceLog::getByField($val,'cbl_id',"*");
                $html.='<tr>';
                $html.="<td>".$customerBalanceLog['customer_code']."</td>";
                $fee = Service_OtherFee::getByField($customerBalanceLog['fee_id'],'fee_id',"*");
                if($customerBalanceLog['fee_id']>0){
                    $html.="<td>".$fee['fee_name']."</td>";
                }else{
                    $html.="<td>物流费</td>";
                }
                $html.="<td>".$customerBalanceLog['cbl_refer_code']."</td>";
                if($customerBalanceLog['fee_id']>0){
                    $application = Service_Application::getByField($fee['fee_application_id']);
                    $html.="<td>".$application['application_name']."</td>";
                }else{
                    $html.="<td>订单</td>";
                }
                $customerRow = Service_Customer::getByField($customerBalanceLog['customer_code'],'customer_code',"*");
                $html.="<td>".$cblType[$customerBalanceLog['cbl_type']]."</td>";
                $html.="<td>".$customerBalanceLog['cbl_value']."</td>";
                $html.="<td>".$customerBalanceLog['currency_rate']."</td>";
                $html.="<td>".$customerBalanceLog['currency_code']."</td>";
                $html.="<td>".$customerBalanceLog['cbl_transaction_value']."</td>";
                $html.="<td>".$customerRow['customer_currency']."</td>";
                $html.="<td>".$customerBalanceLog['cbl_current_value']."</td>";
                $html.="<td>".$customerBalanceLog['cbl_current_hold_value']."</td>";
                $html.="<td>".$customerBalanceLog['cbl_add_time']."</td>";
                $html.="<td>".$customerBalanceLog['cbl_note']."</td>";
                $html.="</tr>";
            }
            $html.="</table>";
            echo $html;
            exit;
        }else{
            $condition	= array(
                'customer_code'		=> $this->_request->getParam('customer_code',""),
                'cbl_type'            => $this->_request->getParam('cbl_type',""),
                'cbl_refer_code'	=> trim($this->_request->getParam('cbl_refer_code','')),
                'dateFor' => $this->_request->getParam('dateFor',""),
                'dateTo' => $this->_request->getParam('dateTo',""),
                'fee_id' => $this->_request->getParam("fee_id",""),
                'application_type'=> $this->_request->getParam("application_type",""),
                'cbl_note_like' => trim($this->_request->getParam('cbl_note_like',''))
            );
            if($condition['application_type']=="order"){
                $condition['application_code_arr'] =array('YUNSHU','CAOZUO','CANGCHU','BAOGUAN2','BAOGUAN1');
            }
            if($condition['application_type']=="asn"){
                $condition['application_code_arr'] = array('TIHUO');
            }
            $count = Service_CustomerBalanceLog::getByCondition($condition,"count(*)");
            $pageSize = 20;
            $totalPage = ceil($count/$pageSize);
            for($i=1;$i<=$totalPage;$i++){
                $customerBalanceList = Service_CustomerBalanceLog::getByCondition($condition,"*",$pageSize,$i,"cbl_id DESC");
                foreach($customerBalanceList as $customerBalanceLog){
                    $html.='<tr>';
                    $html.="<td>".$customerBalanceLog['customer_code']."</td>";
                    $fee = Service_OtherFee::getByField($customerBalanceLog['fee_id'],'fee_id',"*");
                    if($customerBalanceLog['fee_id']>0){
                        $html.="<td>".$fee['fee_name']."</td>";
                    }else{
                        $html.="<td>物流费</td>";
                    }
                    $html.="<td>".$customerBalanceLog['cbl_refer_code']."</td>";
                    if($customerBalanceLog['fee_id']>0){
                        $application = Service_Application::getByField($fee['fee_application_id']);
                        $html.="<td>".$application['application_name']."</td>";
                    }else{
                        $html.="<td>订单</td>";
                    }
                    $html.="<td>".$cblType[$customerBalanceLog['cbl_type']]."</td>";
                    $html.="<td>".$customerBalanceLog['cbl_value']."</td>";
                    $html.="<td>".$customerBalanceLog['currency_rate']."</td>";
                    $html.="<td>".$customerBalanceLog['currency_code']."</td>";
                    $html.="<td>".$customerBalanceLog['cbl_transaction_value']."</td>";
                    $customerRow = Service_Customer::getByField($customerBalanceLog['customer_code'],'customer_code',"*");
                    $html.="<td>".$customerRow['customer_currency']."</td>";
                    $html.="<td>".$customerBalanceLog['cbl_current_value']."</td>";
                    $html.="<td>".$customerBalanceLog['cbl_current_hold_value']."</td>";
                    $html.="<td>".$customerBalanceLog['cbl_add_time']."</td>";
                    $html.="<td>".$customerBalanceLog['cbl_note']."</td>";
                    $html.="</tr>";
                }
            }
            $html.="</table>";
            echo $html;
            exit;
        }
    }

}
