246 lines
10 KiB
PHP
246 lines
10 KiB
PHP
<?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\bank\BankAccount;
|
||
use app\common\model\dict\DictData;
|
||
use app\common\model\oa\FlowApprove;
|
||
use app\common\model\project\Project;
|
||
use app\common\model\project\ProjectExpenseReimbursement;
|
||
use app\common\logic\BaseLogic;
|
||
use app\common\model\project\ProjectExpenseReimbursementDetail;
|
||
use app\common\model\project\ProjectExpenseReimbursementInvoiceDetail;
|
||
use app\common\model\project\ProjectLoanApply;
|
||
use think\facade\Db;
|
||
|
||
|
||
/**
|
||
* 费用报销逻辑
|
||
* Class ProjectExpenseReimbursementLogic
|
||
* @package app\adminapi\logic\project
|
||
*/
|
||
class ProjectExpenseReimbursementLogic extends BaseLogic
|
||
{
|
||
|
||
|
||
/**
|
||
* @notes 添加费用报销
|
||
* @param array $params
|
||
* @return bool
|
||
* @author likeadmin
|
||
* @date 2024/01/19 13:44
|
||
*/
|
||
public static function add(array $params,$admin_id): bool
|
||
{
|
||
//获取税率
|
||
$tax_rate = DictData::where('type_value','tax_rate')->column('name','value');
|
||
Db::startTrans();
|
||
try {
|
||
$res = ProjectExpenseReimbursement::create([
|
||
'expense_reimbursement_code' => data_unique_code('项目费用报销'),
|
||
'project_id' => $params['project_id'],
|
||
'apply_user' => $params['apply_user'],
|
||
'apply_date' => strtotime($params['apply_date']),
|
||
'reimbursement_type' => $params['reimbursement_type'],
|
||
'loan_apply_id' => $params['loan_apply_id'] ?? 0,
|
||
'offset_loan_amount' => $params['offset_loan_amount'] ?? 0,
|
||
'payee_name' => $params['payee_name'],
|
||
'payee_bank' => $params['payee_bank'],
|
||
'payee_account' => $params['payee_account'],
|
||
'remark' => $params['remark'] ?? '',
|
||
'annex' => $params['annex'] ? json_encode($params['annex']) : null,
|
||
'bank_account_id' => $params['bank_account_id'],
|
||
]);
|
||
foreach($params['reimbursement_detail'] as $item){
|
||
ProjectExpenseReimbursementDetail::create([
|
||
'expense_reimbursement_id' => $res->id,
|
||
'project_cost_temp_id' => $item['project_cost_temp_id'],
|
||
'amount' => $item['amount'],
|
||
'remark' => $item['remark'] ?? '',
|
||
]);
|
||
}
|
||
foreach($params['invoice_detail'] as $item){
|
||
ProjectExpenseReimbursementInvoiceDetail::create([
|
||
'expense_reimbursement_id' => $res->id,
|
||
'invoice_type' => $item['invoice_type'],
|
||
'invoice_sn' => $item['invoice_sn'],
|
||
'tax_rate' => $item['tax_rate'],
|
||
'invoice_form' => $item['invoice_form'],
|
||
'invoice_amount' => $item['invoice_amount'],
|
||
'tax_amount' => $tax_rate[$item['tax_rate']] / 100 * $item['invoice_amount'],
|
||
'annex' => !empty($item['annex']) ? json_encode($item['annex']) : null,
|
||
'remark' => $item['remark'] ?? '',
|
||
]);
|
||
}
|
||
//添加审批信息
|
||
addApprove(
|
||
'费用报销',
|
||
$res->id,
|
||
'app\common\model\project\ProjectExpenseReimbursement',
|
||
'app\adminapi\logic\project\ProjectExpenseReimbursementLogic',
|
||
$params['approve_detail']['path'],
|
||
$params['approve_detail']['flow_type'],
|
||
$params['approve_detail']['flow_path'],
|
||
$admin_id
|
||
);
|
||
Db::commit();
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 编辑费用报销
|
||
* @param array $params
|
||
* @return bool
|
||
* @author likeadmin
|
||
* @date 2024/01/19 13:44
|
||
*/
|
||
public static function edit(array $params): bool
|
||
{
|
||
//获取税率
|
||
$tax_rate = DictData::where('type_value','tax_rate')->column('name','value');
|
||
Db::startTrans();
|
||
try {
|
||
ProjectExpenseReimbursement::where('id', $params['id'])->update([
|
||
'project_id' => $params['project_id'],
|
||
'apply_user' => $params['apply_user'],
|
||
'apply_date' => strtotime($params['apply_date']),
|
||
'reimbursement_type' => $params['reimbursement_type'],
|
||
'loan_apply_id' => $params['loan_apply_id'] ?? 0,
|
||
'offset_loan_amount' => $params['offset_loan_amount'] ?? 0,
|
||
'payee_name' => $params['payee_name'],
|
||
'payee_bank' => $params['payee_bank'],
|
||
'payee_account' => $params['payee_account'],
|
||
'remark' => $params['remark'] ?? '',
|
||
'annex' => $params['annex'] ? json_encode($params['annex']) : null,
|
||
'bank_account_id' => $params['bank_account_id'],
|
||
'update_time' => time(),
|
||
]);
|
||
foreach($params['reimbursement_detail'] as $item){
|
||
if(isset($item['id']) && $item['id'] != ''){
|
||
ProjectExpenseReimbursementDetail::where('id',$item['id'])->update([
|
||
'expense_reimbursement_id' => $params['id'],
|
||
'project_cost_temp_id' => $item['project_cost_temp_id'],
|
||
'amount' => $item['amount'],
|
||
'remark' => $item['remark'] ?? '',
|
||
'update_time' => time(),
|
||
]);
|
||
}else{
|
||
ProjectExpenseReimbursementDetail::create([
|
||
'expense_reimbursement_id' => $params['id'],
|
||
'project_cost_temp_id' => $item['project_cost_temp_id'],
|
||
'amount' => $item['amount'],
|
||
'remark' => $item['remark'] ?? '',
|
||
]);
|
||
}
|
||
}
|
||
foreach($params['invoice_detail'] as $item){
|
||
if(isset($item['id']) && $item['id'] != ''){
|
||
ProjectExpenseReimbursementInvoiceDetail::where('id',$item['id'])->update([
|
||
'expense_reimbursement_id' => $params['id'],
|
||
'invoice_type' => $item['invoice_type'],
|
||
'invoice_sn' => $item['invoice_sn'],
|
||
'tax_rate' => $item['tax_rate'],
|
||
'invoice_form' => $item['invoice_form'],
|
||
'invoice_amount' => $item['invoice_amount'],
|
||
'tax_amount' => $tax_rate[$item['tax_rate']] / 100 * $item['invoice_amount'],
|
||
'annex' => !empty($item['annex']) ? json_encode($item['annex']) : null,
|
||
'remark' => $item['remark'] ?? '',
|
||
'update_time' => time(),
|
||
]);
|
||
}else{
|
||
ProjectExpenseReimbursementInvoiceDetail::create([
|
||
'expense_reimbursement_id' => $params['id'],
|
||
'invoice_type' => $item['invoice_type'],
|
||
'invoice_sn' => $item['invoice_sn'],
|
||
'tax_rate' => $item['tax_rate'],
|
||
'invoice_form' => $item['invoice_form'],
|
||
'invoice_amount' => $item['invoice_amount'],
|
||
'tax_amount' => $tax_rate[$item['tax_rate']] / 100 * $item['invoice_amount'],
|
||
'annex' => !empty($item['annex']) ? json_encode($item['annex']) : null,
|
||
'remark' => $item['remark'] ?? '',
|
||
]);
|
||
}
|
||
}
|
||
Db::commit();
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 删除费用报销
|
||
* @param array $params
|
||
* @return bool
|
||
* @author likeadmin
|
||
* @date 2024/01/19 13:44
|
||
*/
|
||
public static function delete(array $params): bool
|
||
{
|
||
Db::startTrans();
|
||
try {
|
||
$expense_reimbursement_detail_ids = ProjectExpenseReimbursementDetail::where('expense_reimbursement_id',$params['id'])->column('id');
|
||
$expense_reimbursement_invoice_detail_ids = ProjectExpenseReimbursementInvoiceDetail::where('expense_reimbursement_id',$params['id'])->column('id');
|
||
ProjectExpenseReimbursement::destroy($params['id']);
|
||
ProjectExpenseReimbursementDetail::destroy($expense_reimbursement_detail_ids);
|
||
ProjectExpenseReimbursementInvoiceDetail::destroy($expense_reimbursement_invoice_detail_ids);
|
||
Db::commit();
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 获取费用报销详情
|
||
* @param $params
|
||
* @return array
|
||
* @author likeadmin
|
||
* @date 2024/01/19 13:44
|
||
*/
|
||
public static function detail($params): array
|
||
{
|
||
$data = ProjectExpenseReimbursement::field('id,expense_reimbursement_code,project_id,apply_user,apply_date,reimbursement_type,loan_apply_id,offset_loan_amount,payee_name,payee_bank,payee_account,remark,annex,bank_account_id')
|
||
->findOrEmpty($params['id']);
|
||
$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_text'] = $data->reimbursement_type_text;
|
||
$data['bank_account'] = BankAccount::field('account_sn,deposit_bank,account_name,account')->where('id',$data['bank_account_id'])->findOrEmpty();
|
||
$approve_info = FlowApprove::field('id')->where('content_id',$params['id'])
|
||
->where('content_model','app\common\model\project\ProjectExpenseReimbursement')
|
||
->where('content_logic','app\adminapi\logic\project\ProjectExpenseReimbursementLogic')->findOrEmpty();
|
||
$data['approve_id'] = $approve_info['id'];
|
||
return $data->toArray();
|
||
}
|
||
} |