<?php
/**
 * 供应商余额明细管理模块
 * Created by PhpStorm.
 * User: tpl
 * Date: 2019/10/30
 * Time: 13:54
 */

namespace JinDouYun\Controller\Finance;

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

use JinDouYun\Controller\BaseController;
use JinDouYun\Model\Finance\MSupplierBalanceDetail;

class SupplierBalanceDetail extends BaseController
{
    private $objMSupplierBalanceDetail;

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

    /**
     * 后台所有供应商余额明细列表
     */
    public function getAllSupplierBalanceDetail()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }

        $selectParams = [
            'supplierId' => isset($params['supplierId']) ? $params['supplierId'] : '',
            'sort'       => isset($params['sort']) ? $params['sort'] : 'ASC',
        ];

        foreach ($selectParams as $key => $value) {
            if (empty($value) && $value !== 0) {
                $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }

        $selectParams['start'] = !empty($params['start']) ? $params['start'] : 0;
        $selectParams['end'] = !empty($params['end']) ? strtotime(date('Y-m-d', $params['end']) . '23:59:59') : time();

        $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];
        $export = isset($params['export']) ? $params['export'] : 0;
        $result = $this->objMSupplierBalanceDetail->getAllSupplierBalanceDetail($selectParams,$export);

        if ($result->isSuccess()) {
            $returnData = $result->getData();
            $pageData = [
                'pageIndex'      => $params['page'],
                'pageSize'       => $params['pageSize'],
                'pageTotal'      => $returnData['total'],
                'openingBalance' => $returnData['openingBalance'],
                'endingBalance'  => $returnData['endingBalance'],
                'shouldPayTotal' => $returnData['shouldPayTotal'],
                'actualPayTotal' => $returnData['actualPayTotal'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }
}