<?php

class Financial_FeeTransferRecordsController extends Ec_Controller_Action
{
    public function preDispatch()
    {
        $this->tplDirectory = "financial/views/transfer/";
        $this->serviceClass = new Service_FeeTransferRecords();
    }

    public function listAction()
    {

        $feeTransferRecordsStatus = Process_BillConfig::feeTransferRecordsStatus('auto');
        $settlementCode = Process_BillConfig::settlementCode('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"
            );

            $condition['so_code'] = trim($this->getRequest()->getParam("so_code", ""));
            $condition['ftr_status'] = trim($this->getRequest()->getParam("ftr_status", ""));
            $condition['pm_code'] = trim($this->getRequest()->getParam("pm_code", ""));
            $condition['payer_code'] = trim($this->getRequest()->getParam("payer_code", ""));
            $condition['ftr_transaction_no'] = trim($this->getRequest()->getParam("transaction_no", ""));
            $condition['dateFor'] = isset($params['dateFor']) ? $params['dateFor'] . ' 00:00:00' : '';
            $condition['dateTo'] = isset($params['dateTo']) ? $params['dateTo'] . ' 23:59:59' : '';

            $count = $this->serviceClass->getAllByCondition($condition, 'count(*)');
            $return['total'] = $count;

            if ($count) {
                $showFields = array(

                    'payer_code',
                    'so_code',
                    'pm_code',
                    'ftr_transaction_no',
                    'ftr_transaction_date',
                    'ftr_contact_person',
                    'ftr_company',
                    'ftr_status',
                    'ft_code',
                    'ba_id',
                    'ftr_amount',
                    'ftr_amount_real',
                    'ftr_currency_code',
                    'ftr_note',
                    'ftr_payin_time',
                    'ftr_verify_time',
                    'ftr_dishonour_time',
                    'ftr_creator_id',
                    'ftr_verify_id',
                    'ftr_dishonour_id',
                    'ftr_add_time',
                    'fee_transfer_records.ftr_id as new_ftr_id',
                    'ftr_actual_payment_customer',
                );
                $rows = $this->serviceClass->getAllByCondition($condition, $showFields, $pageSize, $page, array('fee_transfer_records.ftr_id desc'));
                $return['data'] = $rows;
                $return['state'] = 1;
                $return['message'] = "";
                $return['settlementCode'] = $settlementCode;
                $return['feeStatus'] = $feeTransferRecordsStatus;
            }
            die(Zend_Json::encode($return));
        }
        $this->view->dateFor = date("Y-m-d", strtotime("-32 days"));
        $this->view->dateTo = date("Y-m-d");
        $this->view->settlementCode = $settlementCode;
        $this->view->feeStatus = $feeTransferRecordsStatus;
        $this->view->paymentMethod = Service_PaymentMethod::getByCondition(array(), array("pm_code", "title"));
        echo Ec::renderTpl($this->tplDirectory . "fee_transfer_records_index.tpl", 'layout');
    }

    /**
     * @desc 审核费用
     */
    public function verifyAction()
    {
        $result = array(
            "state" => 0,
            "message" => "Fail.",
            "data" => array()
        );
        if ($this->_request->isPost()) {
            $param = array(
                //'amount' => $this->_request->getParam("amount", 0),
                'amount_real' => $this->_request->getParam("amount_real", 0),
                'desc' => $this->_request->getParam("desc", 0),
                'ftr_id' => $this->_request->getParam("ftr_id", 0),
                'opType' => $this->_request->getParam("opType", 0),
            );
            $obj = new Process_Financial();
            $result = $obj->transferRecordsVerifyTransaction($param);
        }
        die(Zend_Json::encode($result));
    }

    /**
     * @desc 详细
     */
    public function viewAction()
    {
        $paramId = $this->_request->getParam("paramId", '0');
        if (empty($paramId)) {
            die("no data");
        }
        $row = Service_FeeTransferRecords::getByField($paramId, 'ftr_id');
        if (empty($row)) {
            die("no data");
        }
        $row['short'] = '';
        $row['company'] = '';
        $row['settlementType'] = '';

        $feeTransferRecordsStatus = Process_BillConfig::feeTransferRecordsStatus('auto');

        $row['ftr_status_text'] = $feeTransferRecordsStatus[$row['ftr_status']];

        $pmRow = Service_PaymentMethod::getByField($row['pm_code'], 'pm_code');

        $row['pm_code_text'] = isset($pmRow['title']) ? $pmRow['title'] : '';

        if ($row['so_code'] == 'cm') {
            $customerSettlementType = Process_BillConfig::customerSettlementType('auto');
            $cRow = Service_Customer::getByField($row['payer_code'], 'customer_code', array('cash_type', 'customer_short', 'customer_company_name'));
            $row['short'] = $cRow['customer_short'];
            $row['company'] = $cRow['customer_company_name'];;
            $row['settlementType'] = isset($customerSettlementType[$cRow['cash_type']]) ? $customerSettlementType[$cRow['cash_type']] : '';
        }
        $settlementCode = Process_BillConfig::settlementCode('auto');
        $row['settlementCode'] = isset($settlementCode[$row['so_code']]) ? $settlementCode[$row['so_code']] : '';


        $row['ftra_payer_bank_name'] = '';
        $row['ftra_payer_account_name'] = '';
        $row['ftra_payer_account'] = '';


        $userRow = Service_User::getByField($row['ftr_creator_id'], 'user_id', array('user_name'));
        $userArr[$row['ftr_creator_id']] = isset($userRow['user_name']) ? $userRow['user_name'] : '系统';


        $row['creatorName'] = $userArr[$row['ftr_creator_id']];

        if (!isset($userArr[$row['ftr_verify_id']])) {
            $userRow = Service_User::getByField($row['ftr_verify_id'], 'user_id', array('user_name'));
            $userArr[$row['ftr_verify_id']] = isset($userRow['user_name']) ? $userRow['user_name'] : '系统';
        }

        if (!isset($userArr[$row['ftr_dishonour_id']])) {
            $userRow = Service_User::getByField($row['ftr_dishonour_id'], 'user_id', array('user_name'));
            $userArr[$row['ftr_dishonour_id']] = isset($userRow['user_name']) ? $userRow['user_name'] : '系统';
        }

        $row['verifyName'] = $userArr[$row['ftr_verify_id']];
        $row['dishonourName'] = $userArr[$row['ftr_dishonour_id']];

        $row['ba_code'] = '';
        $row['ba_shortcode'] = '';
        $row['ba_name'] = '';
        $row['ba_bankname'] = '';

        //付款信息
        $attrRow = Service_FeeTransferRecordAttach::getByField($row['ftr_id'], 'ftr_id');
        if (!empty($attrRow)) {
            $row = array_merge($row, $attrRow);
        }

        $bankRow = Service_BankAccount::getByField($row['ba_id'], 'ba_id');
        if (!empty($bankRow)) {
            $row = array_merge($row, $bankRow);
        }
        $this->view->data = $row;
        echo Ec::renderTpl($this->tplDirectory . "detail.tpl", 'layout');
    }


    /**
     * @desc 获取帐单费用明细信息
     */
    public function receiptAction()
    {
        $paramId = $this->_request->getParam('paramId', '0');
        $rpType = Process_BillConfig::rpType('auto');
        $customerSettlementType = Process_BillConfig::customerSettlementType('auto');
        $settlementCode = Process_BillConfig::settlementCode('auto');

        $row = array(
            'bi_id' => '',
            'bb_id' => '',
            'bb_refer_code' => '',
            'customer_code' => '',
            'customer_name' => '',
            'ft_code' => '',
            'bi_amount' => '',
            'currency_code' => '',
            'bi_note' => '',
            'cash_type_text' => '',
            'cb_value' => '',
            'bi_rp_type' => '',
            'rpType_text' => '',
            "cash_type" => '', "customer_short" => '', "customer_currency" => ''
        );
        if (!empty($paramId)) {
            $row = Service_BilIncome::getByField($paramId);
            $bbaRow = Service_BilBusinessAttach::getByField($row['bb_id'], 'bb_id');
            $cRow = Service_Customer::getByField($row['customer_code'], 'customer_code');
            $cbRow = Service_CustomerBalance::getByField($row['customer_code'], 'customer_code');
            $row = array_merge($row, $cRow, $cbRow);
            $row['bb_refer_code'] = $bbaRow['bb_refer_code'];
            $row['customer_name'] = $cRow['customer_firstname'] . " " . $cRow['customer_lastname'];
            $row['cash_type_text'] = isset($customerSettlementType[$row['cash_type']]) ? $customerSettlementType[$row['cash_type']] : '未绑定';
            $row['rpType_text'] = isset($rpType[$row['bi_rp_type']]) ? $rpType[$row['bi_rp_type']] : '异常';
        }

        $this->view->feeType = Service_FeeType::getByCondition(array(), '*', 0);
        $this->view->currency = Service_Currency::getByCondition(array(), '*', 0);

        $this->view->title = !empty($row['bb_refer_code']) ? "编辑费用：" . $row['bb_refer_code'] : '新增费用';

        $this->view->data = $row;
        $this->view->settlementCode = $settlementCode;
        $this->view->paymentMethod = Service_PaymentMethod::getByCondition(array(), array("pm_code", "title"));
        $this->view->bankAccount = Service_BankAccount::getByCondition(array(), "*", 0, 0, array("ba_shortcode", "bk_id"));

        echo Ec::renderTpl($this->tplDirectory . "receipt.tpl", 'layout');
    }


    /**
     * @desc 确认提交收款通知
     */
    public function confirmReceiptAction()
    {
        $result = array(
            "state" => 0,
            "message" => "Fail.",
            "data" => array()
        );

        if ($this->_request->isPost()) {
            $paymentMethod = $this->_request->getParam("paymentMethod", '');
            $params = $this->_request->getParams();
            $attach = $param = array();
            if (strtolower($paymentMethod) == 'cash') {
                $param = array(
                    "payer_code" => $params['payerCode'],
                    "so_code" => $params['payerTypeCode'],
                    "pm_code" => 'CASH',
                    "ftr_transaction_no" => $params['cash_transaction_no'],
                    "ftr_actual_payment_customer" => $params['cash_actual_payment_customer'],
                    "ftr_transaction_date" => $params['cash_payin_time'],
                    "ftr_contact_person" => $params['cash_actual_payment_customer'],
                    "ftr_company" => '',
                    "ftr_status" => '1',
                    "ft_code" => $params['cash_ft_code'],
                    "ba_id" => '',
                    "ftr_amount" => $params['cash_amount'],
                    "ftr_currency_code" => $params['cash_currency_code'],
                    "ftr_note" => $params['cash_note'],
                    "bill_id" => '',
                    "ftr_payin_time" => $params['cash_payin_time'],
                );
            } else {
                $param = array(
                    "payer_code" => $params['payerCode'],
                    "so_code" => $params['payerTypeCode'],
                    "pm_code" => 'BANK',
                    "ftr_transaction_no" => $params['bank_transaction_no'],
                    "ftr_actual_payment_customer" => $params['bank_actual_payment_customer'],
                    "ftr_transaction_date" => $params['bank_payin_time'],
                    "ftr_contact_person" => $params['bank_actual_payment_customer'],
                    "ftr_company" => '',
                    "ftr_status" => '1',
                    "ft_code" => $params['bank_ft_code'],
                    "ba_id" => $params['bank_ba_id'],
                    "ftr_amount" => $params['bank_amount'],
                    "ftr_currency_code" => $params['bank_currency_code'],
                    "ftr_note" => $params['bank_note'],
                    "bill_id" => '',
                    "ftr_payin_time" => $params['bank_payin_time'],
                );
                $attach = array(
                    'fpa_id' => $params['fpa_id'],
                    'save_payment_account' => (isset($params['save_payment_account']) ? 1 : 0),
                    'ftra_payer_bank_name' => $params['ftra_payer_bank_name'],
                    'ftra_payer_account_name' => $params['ftra_payer_account_name'],
                    'ftra_payer_account' => $params['ftra_payer_account'],
                );
            }
            $obj = new Process_Financial();
            $result = $obj->receiptTransaction($param, $attach);
        }
        die(Zend_Json::encode($result));
    }


}