<?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\logic\project;


use app\common\model\build\BuildPlan;
use app\common\model\build\BuildReport;
use app\common\model\build\BuildReportDetail;
use app\common\model\project\Project;
use app\common\model\project\ProjectAttendanceDetail;
use app\common\model\project\ProjectPersonnel;
use app\common\model\project\ProjectSalaryDetail;
use app\common\model\project\ProjectSalaryPayment;
use app\common\logic\BaseLogic;
use think\facade\Db;


/**
 * 工资付款逻辑
 * Class ProjectSalaryPaymentLogic
 * @package app\adminapi\logic\project
 */
class ProjectSalaryPaymentLogic extends BaseLogic
{
	
    /**
     * @notes 添加工资付款
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2023/12/27 16:08
     */
    public static function add(array $params): bool
    {
        Db::startTrans();
        try {
	        $payment_detail = $params['payment_detail'];//json_decode($params['payment_detail'],true);
            $res = ProjectSalaryPayment::create([
                'payment_code' => data_unique_code('项目工资付款单'),
                'project_id' => $params['project_id'],
                'payment_type' => $params['payment_type'],
                'apply_date' => strtotime($params['apply_date']),
                'remark' => $params['remark'] ?? '',
                'annex' => $params['annex']? json_encode($params['annex']) : null,
            ]);
	        foreach($payment_detail as $v){
		        ProjectSalaryDetail::create([
			        'salary_payment_id' => $res->id,
			        'person_id' => $v['person_id'],
			        'apply_date' => strtotime($params['apply_date']),
			        'apply_amount' => $v['apply_amount'],
			        'remark' => $v['remark'] ?? '',
		        ]);
	        }
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }
	

    /**
     * @notes 获取工资付款详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2023/12/27 16:08
     */
    public static function detail($params): array
    {
		$data = ProjectSalaryPayment::field('id,payment_code,project_id,payment_type,apply_date,remark,annex')->findOrEmpty($params['id'])->toArray();
		$project = Project::field('name,project_code')->where('id',$data['project_id'])->findOrEmpty();
		$data['project_name'] = $project['name'];
	    $data['project_code'] = $project['project_code'];
		$data['payment_detail'] = ProjectSalaryDetail::field('person_id,apply_amount,remark')->where('salary_payment_id',$data['id'])
				->select()->each(function($item){
					$person = ProjectPersonnel::field('name,idcard,work_type,deposit_bank,bank_no')->where('id',$item['person_id'])->findOrEmpty();
					$item['person_name'] = $person['name'];
					$item['person_idcard'] = $person['idcard'];
					$item['person_work_type_text'] = $person->work_type_text;
					$item['person_deposit_bank'] = $person['deposit_bank'];
					$item['person_bank_no'] = $person['bank_no'];
					//总工资付款
					$item['total_amount'] = ProjectSalaryDetail::where('salary_payment_id',$item['id'])->sum('apply_amount');
					//总考勤收入
					$daily_income = ProjectAttendanceDetail::where('person_id',$item['person_id'])->sum('daily_income');
					//总施工收入
					$build_report_details = BuildReportDetail::field('report_id,work_num')->where('person_id',$item['person_id'])->select()->each(function($item2){
						$report = BuildReport::field('plan_id')->where('id',$item2['report_id'])->findOrEmpty();
						$plan = BuildPlan::field('price')->where('id',$report['plan_id'])->findOrEmpty();
						$item2['amount'] = $item2['work_num'] * $plan['price'];
					})->toArray();
					$work_income = 0;
					foreach($build_report_details as $v){
						$work_income += $v['amount'];
					}
					$item['total_income'] = $daily_income + $work_income;
					//总支出
					$item['total_pay_out'] = ProjectSalaryDetail::where('person_id',$item['person_id'])->sum('apply_amount');
					$item['balance'] = $item['total_income'] - $item['total_pay_out'];
					return $item;
				})
				->toArray();
        return $data;
    }
}