<?php
/**
 * Created by PhpStorm.
 * User: Gss
 * Date: 2021/3/6 0006
 * Time: 15:00
 */

namespace JinDouYun\Controller\Finance;

use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\StatusCode;

use JinDouYun\Model\Finance\MReceiptRequisition;

use JinDouYun\Controller\BaseController;

class ReceiptRequisition extends BaseController
{
    private $objMReceiptRequisition;

    public function __construct($isCheckAcl = true, $isMustLogin = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $this->objMReceiptRequisition = new MReceiptRequisition($this->onlineEnterpriseId, $this->onlineUserId);
    }

    /**
     * 添加和编辑收款申请单公共字段处理方法
     *
     * @return array
     */
    public function commonFieldFilter()
    {

        $params = $this->request->getRawJson();
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $ReceiptRequisitionData = [
            //'no'                  => getArrayItem($params, 'no', createOrderSn(StatusCode::$orderType['received'],StatusCode::$orderType['received'],1)),
            'customerId'               => getArrayItem($params, 'customerId'),
            'customerName'             => isset($params['customerName']) ? $params['customerName'] : '',
            'receivable'                => isset($params['receivable']) ? $params['receivable'] : '',
            'totalCollectionAmount'     => getArrayItem($params,'totalCollectionAmount',0),
            'totalPreferentialAmount'   => getArrayItem($params,'totalPreferentialAmount',0),
            'totalActualAmount'         => getArrayItem($params,'totalActualAmount',0),
            'operatorId'                => isset($params['operatorId']) ? $params['operatorId'] : '',
            'operatorName'              => isset($params['operatorName']) ? $params['operatorName'] : '',
            'shopId'                    =>getArrayItem($params,'shopId',''),
            'shopName'                  =>getArrayItem($params,'shopName',''),
            'auditStatus'               =>getArrayItem($params,'auditStatus', StatusCode::$auditStatus['auditing']),
            'deleteStatus'              =>getArrayItem($params,'deleteStatus', 5),
            'receiptRequisitionAccountDate'       => getArrayItem($params,'receiptRequisitionAccountDate', [])
        ];

        foreach ($ReceiptRequisitionData as $key => $value) {
            if (empty($value) && $value !== 0) {
                $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }
        //判断expenseSingleAccountDate里面的数据是否为空
        foreach ($ReceiptRequisitionData['receiptRequisitionAccountDate'] as $receiptRequisition) {
            if(!empty($receiptRequisition)) {
                foreach ($receiptRequisition as $k =>$v) {
                    if(in_array($k, ['accountName','accountId','settlementMethod','actualAmount','collectionAmount','actualAmount']) && empty($v)) {
                        $this->sendOutput('请输入账户'.$k, ErrorCode::$paramError);
                    }
                }
            }
        }
        $ReceiptRequisitionData['createTime'] = time();
        $ReceiptRequisitionData['updateTime'] = time();
        return $ReceiptRequisitionData;
    }

    /**
     * 新增收款单
     */
    public function addReceiptRequisition()
    {
        $receiptRequisitionData = $this->commonFieldFilter();

        $result = $this->objMReceiptRequisition->addReceiptRequisition($receiptRequisitionData);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 获取指定的收款单
     */
    public function getReceiptRequisitionInfo()
    {
        $receiptRequisitionId = $this->request->param('request_id');
        if (!$receiptRequisitionId) {
            $this->sendOutput('参数错误', ErrorCode::$paramError);
        }

        $result = $this->objMReceiptRequisition->getReceiptRequisitionInfo($receiptRequisitionId);

        if ($result->isSuccess()) {
            $this->sendOutput($result->getData());
        } else {
            $this->sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 编辑指定的收款单
     */
    public function editReceiptRequisition()
    {
        $receiptRequisitionId  = $this->request->param('request_id');
        if(empty($receiptRequisitionId)){
            $this->sendOutput('参数错误', ErrorCode::$paramError);
        }
        $receiptRequisitionData = $this->commonFieldFilter();
        $receiptRequisitionData['id'] = $receiptRequisitionId;

        $result = $this->objMReceiptRequisition->editReceiptRequisition($receiptRequisitionData);
        if($result->isSuccess()){
            parent::sendOutput($result->getData());
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 删除指定的收款单
     */
    public function delReceiptRequisition()
    {
        $receiptRequisitionId  = $this->request->param('request_id');
        if(!$receiptRequisitionId){
            $this->sendOutput('参数错误', ErrorCode::$paramError);
        }
        $result = $this->objMReceiptRequisition->delReceiptRequisition($receiptRequisitionId);
        if($result->isSuccess()){
            parent::sendOutput($result->getData());
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 获取所有的收款单
     */
    public function getAllReceiptRequisition()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }

        $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
        $params['limit'] = $pageParams['limit'];
        $params['offset'] = $pageParams['offset'];
//        $params['operatorName'] = getArrayItem($params,'operatorName','');
        $params['auditStatus'] = getArrayItem($params,'auditStatus','');
        $params['customerId'] = getArrayItem($params,'customerId','');

        $returnData = $this->objMReceiptRequisition->getAllReceiptRequisition($params);

        if ($returnData->isSuccess()) {
            $returnData = $returnData->getData();
            $pageData = [
                'pageIndex' => $params['page'],
                'pageSize'  => $params['pageSize'],
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        } else {
            parent::sendOutput($returnData->getData(), ErrorCode::$dberror);
        }
    }
    /**
     * 审核申请收款单
     */
    public function updateReceiptRequisitionStatus()
    {
        $params = $this->request->getRawJson();
//        if( empty($params['id']) || empty($params['createTime']) || empty($params['autoType'])){
        if( empty($params['id']) || empty($params['createTime'])){
            $this->sendOutput('参数为空', ErrorCode::$paramError );
        }

        $result = $this->objMReceiptRequisition->updateReceiptRequisitionStatus($params);
        if($result->isSuccess()){
            parent::sendOutput($result->getData());
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }
}