<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台(PHP版)
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用,可去除界面版权logo
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
// | github下载:https://github.com/likeshop-github/likeadmin
// | 访问官网:https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\lists\project;


use app\adminapi\lists\BaseAdminDataLists;
use app\common\model\project\Project;
use app\common\model\project\ProjectExpenseReimbursement;
use app\common\lists\ListsSearchInterface;
use app\common\model\project\ProjectExpenseReimbursementDetail;
use app\common\model\project\ProjectLoanApply;


/**
 * 费用报销列表
 * Class ProjectExpenseReimbursementLists
 * @package app\adminapi\listsproject
 */
class ProjectExpenseReimbursementLists extends BaseAdminDataLists implements ListsSearchInterface
{


    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2024/01/19 13:44
     */
    public function setSearch(): array
    {
        return [
            '=' => ['project_id', 'reimbursement_type'],
            '%like%' => ['expense_reimbursement_code', 'apply_user', 'payee_name'],
        ];
    }


    /**
     * @notes 获取费用报销列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2024/01/19 13:44
     */
    public function lists(): array
    {
        return ProjectExpenseReimbursement::where($this->searchWhere)
            ->field('id,expense_reimbursement_code,project_id,apply_user,apply_date,reimbursement_type,loan_apply_id,offset_loan_amount,remark')
            ->limit($this->limitOffset, $this->limitLength)
            ->order(['id' => 'desc'])
            ->select()->each(function($data){
		        $project = Project::field('name,project_code')->where('id',$data['project_id'])->findOrEmpty();
		        $loan_apply = ProjectLoanApply::field('loan_apply_code,loan_amount')->where('id',$data['loan_apply_id'])->findOrEmpty();
		        $data['project_name'] = $project['name'];
		        $data['project_code'] = $project['project_code'];
		        $data['loan_apply_code'] = !$loan_apply->isEmpty() ? $loan_apply['loan_apply_code'] : '---';
		        $data['loan_amount'] = !$loan_apply->isEmpty() ? $loan_apply['loan_amount'] : '---';
		        $data['total_amount'] = ProjectExpenseReimbursementDetail::where('expense_reimbursement_id',$data['id'])->sum('amount');
		        $data['pay_amount'] = $data['total_amount'] - $data['offset_loan_amount'];
		        $data['reimbursement_type'] = $data->reimbursement_type_text;
				unset($data['project_id'],$data['loan_apply_id']);
				return $data;
	        })
            ->toArray();
    }


    /**
     * @notes 获取费用报销数量
     * @return int
     * @author likeadmin
     * @date 2024/01/19 13:44
     */
    public function count(): int
    {
        return ProjectExpenseReimbursement::where($this->searchWhere)->count();
    }

}