296 lines
12 KiB
PHP
296 lines
12 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\auth\Admin;
|
||
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\ProjectLoanApply;
|
||
use app\common\model\project\ProjectManagerAppointment;
|
||
use app\common\model\project\ProjectTravelReimbursement;
|
||
use app\common\logic\BaseLogic;
|
||
use app\common\model\project\ProjectTravelReimbursementDetail;
|
||
use app\common\model\project\ProjectTravelReimbursementInvoiceDetail;
|
||
use app\common\model\project\ProjectTripApply;
|
||
use think\facade\Db;
|
||
|
||
|
||
/**
|
||
* 差旅报销逻辑
|
||
* Class ProjectTravelReimbursementLogic
|
||
* @package app\adminapi\logic\project
|
||
*/
|
||
class ProjectTravelReimbursementLogic extends BaseLogic
|
||
{
|
||
|
||
|
||
/**
|
||
* @notes 添加差旅报销
|
||
* @param array $params
|
||
* @return bool
|
||
* @author likeadmin
|
||
* @date 2024/01/18 13:57
|
||
*/
|
||
public static function add(array $params,$admin_id): bool
|
||
{
|
||
$trip_apply = ProjectTripApply::field('project_id')->where('id',$params['trip_apply_id'])->findOrEmpty();
|
||
//获取税率
|
||
$tax_rate = DictData::where('type_value','tax_rate')->column('name','value');
|
||
Db::startTrans();
|
||
try {
|
||
$res = ProjectTravelReimbursement::create([
|
||
'trip_reimbursement_code' => data_unique_code('项目差旅报销'),
|
||
'trip_apply_id' => $params['trip_apply_id'],
|
||
'project_id' => $trip_apply['project_id'],
|
||
'reimbursement_type' => $params['reimbursement_type'],
|
||
'loan_apply_id' => $params['loan_apply_id'] ?? 0,
|
||
'offset_loan_amount' => $params['offset_loan_amount'] ?? 0,//冲抵借款金额
|
||
'apply_user' => $params['apply_user'],
|
||
'apply_date' => strtotime($params['apply_date']),
|
||
'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){
|
||
ProjectTravelReimbursementDetail::create([
|
||
'travel_reimbursement_id' => $res->id,
|
||
'project_cost_temp_id' => $item['project_cost_temp_id'],
|
||
'traffic_fee' => $item['traffic_fee'] ?? 0,
|
||
'stay_fee' => $item['stay_fee'] ?? 0,
|
||
'restaurant_fee' => $item['restaurant_fee'] ?? 0,
|
||
'subsidy_fee' => $item['subsidy_fee'] ?? 0,
|
||
'other_fee' => $item['other_fee'] ?? 0,
|
||
'total_amount' => $item['traffic_fee'] + $item['stay_fee'] + $item['restaurant_fee'] + $item['subsidy_fee'] + $item['other_fee'],
|
||
'remark' => $item['remark'] ?? '',
|
||
]);
|
||
}
|
||
foreach($params['invoice_detail'] as $item){
|
||
ProjectTravelReimbursementInvoiceDetail::create([
|
||
'travel_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'] ?? '',
|
||
]);
|
||
}
|
||
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/18 13:57
|
||
*/
|
||
public static function edit(array $params): bool
|
||
{
|
||
$trip_apply = ProjectTripApply::field('project_id')->where('id',$params['trip_apply_id'])->findOrEmpty();
|
||
//获取税率
|
||
$tax_rate = DictData::where('type_value','tax_rate')->column('name','value');
|
||
Db::startTrans();
|
||
try {
|
||
ProjectTravelReimbursement::where('id', $params['id'])->update([
|
||
'trip_apply_id' => $params['trip_apply_id'],
|
||
'project_id' => $trip_apply['project_id'],
|
||
'reimbursement_type' => $params['reimbursement_type'],
|
||
'loan_apply_id' => $params['loan_apply_id'] ?? 0,
|
||
'offset_loan_amount' => $params['offset_loan_amount'] ?? 0,//冲抵借款金额
|
||
'apply_user' => $params['apply_user'],
|
||
'apply_date' => strtotime($params['apply_date']),
|
||
'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'] != ''){
|
||
ProjectTravelReimbursementDetail::where('id',$item['id'])->update([
|
||
'travel_reimbursement_id' => $params['id'],
|
||
'project_cost_temp_id' => $item['project_cost_temp_id'],
|
||
'traffic_fee' => $item['traffic_fee'] ?? 0,
|
||
'stay_fee' => $item['stay_fee'] ?? 0,
|
||
'restaurant_fee' => $item['restaurant_fee'] ?? 0,
|
||
'subsidy_fee' => $item['subsidy_fee'] ?? 0,
|
||
'other_fee' => $item['other_fee'] ?? 0,
|
||
'total_amount' => $item['traffic_fee'] + $item['stay_fee'] + $item['restaurant_fee'] + $item['subsidy_fee'] + $item['other_fee'],
|
||
'remark' => $item['remark'] ?? '',
|
||
'update_time' => time(),
|
||
]);
|
||
}else{
|
||
ProjectTravelReimbursementDetail::create([
|
||
'travel_reimbursement_id' => $params['id'],
|
||
'project_cost_temp_id' => $item['project_cost_temp_id'],
|
||
'traffic_fee' => $item['traffic_fee'] ?? 0,
|
||
'stay_fee' => $item['stay_fee'] ?? 0,
|
||
'restaurant_fee' => $item['restaurant_fee'] ?? 0,
|
||
'subsidy_fee' => $item['subsidy_fee'] ?? 0,
|
||
'other_fee' => $item['other_fee'] ?? 0,
|
||
'total_amount' => $item['traffic_fee'] + $item['stay_fee'] + $item['restaurant_fee'] + $item['subsidy_fee'] + $item['other_fee'],
|
||
'remark' => $item['remark'] ?? '',
|
||
]);
|
||
}
|
||
}
|
||
foreach($params['invoice_detail'] as $item){
|
||
if(isset($item['id']) && $item['id'] != ''){
|
||
ProjectTravelReimbursementInvoiceDetail::where('id',$item['id'])->update([
|
||
'travel_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{
|
||
ProjectTravelReimbursementInvoiceDetail::create([
|
||
'travel_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/18 13:57
|
||
*/
|
||
public static function delete(array $params): bool
|
||
{
|
||
Db::startTrans();
|
||
try {
|
||
$travel_reimbursement_detail_ids = ProjectTravelReimbursementDetail::where('travel_reimbursement_id',$params['id'])->column('id');
|
||
$travel_reimbursement_invoice_detail_ids = ProjectTravelReimbursementInvoiceDetail::where('travel_reimbursement_id',$params['id'])->column('id');
|
||
ProjectTravelReimbursement::destroy($params['id']);
|
||
ProjectTravelReimbursementDetail::destroy($travel_reimbursement_detail_ids);
|
||
ProjectTravelReimbursementInvoiceDetail::destroy($travel_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/18 13:57
|
||
*/
|
||
public static function detail($params): array
|
||
{
|
||
$field = 'id,trip_apply_id,project_id,trip_reimbursement_code,reimbursement_type,loan_apply_id,offset_loan_amount,apply_user,apply_date,payee_name,payee_bank,payee_account,remark,annex,bank_account_id,approve_id';
|
||
$data = ProjectTravelReimbursement::field($field)
|
||
->findOrEmpty($params['id']);
|
||
$trip_apply = ProjectTripApply::field('trip_apply_code')->where('id',$data['trip_apply_id'])->findOrEmpty();
|
||
$project = Project::field('name,project_code')->where('id',$data['project_id'])->findOrEmpty();
|
||
$manager = ProjectManagerAppointment::field('project_manager')->where('project_id',$data['project_id'])->findOrEmpty();
|
||
$loan_apply = ProjectLoanApply::field('loan_apply_code,loan_amount')->where('id',$data['loan_apply_id'])->findOrEmpty();
|
||
$data['trip_apply_code'] = $trip_apply['trip_apply_code'];
|
||
$data['project_name'] = $project['name'];
|
||
$data['project_code'] = $project['project_code'];
|
||
if($manager->isEmpty()){
|
||
$data['project_manager'] = '';
|
||
}else{
|
||
$admin = Admin::field('name')->where('id',$manager['project_manager'])->findOrEmpty();
|
||
$data['project_manager'] = $admin['name'];
|
||
}
|
||
$data['loan_apply_code'] = !$loan_apply->isEmpty() ? $loan_apply['loan_apply_code'] : '---';
|
||
$data['loan_amount'] = !$loan_apply->isEmpty() ? $loan_apply['loan_amount'] : '---';
|
||
$data['reimbursement_type_text'] = $data->reimbursement_type_text;
|
||
$data['total_amount'] = ProjectTravelReimbursementDetail::where('travel_reimbursement_id',$data['id'])->sum('total_amount');
|
||
$data['pay_amount'] = $data['total_amount'] - $data['offset_loan_amount'];
|
||
$data['bank_account'] = BankAccount::field('account_sn,deposit_bank,account_name,account')->where('id',$data['bank_account_id'])->findOrEmpty();
|
||
$approve_data = FlowApprove::where('id',$data['approve_id'])->findOrEmpty();
|
||
$data['approve_check_status'] = $approve_data['check_status'];
|
||
return $data->toArray();
|
||
}
|
||
|
||
public static function approve($params,$admin_id): bool{
|
||
$data = ProjectTravelReimbursement::where('id',$params['id'])->findOrEmpty();
|
||
$approve_data = FlowApprove::where('id',$data['approve_id'])->findOrEmpty();
|
||
if(!empty($data['approve_id']) && $approve_data['check_status'] != 3){
|
||
self::setError('当前内容存在审核信息,请勿重复提交');
|
||
return false;
|
||
}
|
||
Db::startTrans();
|
||
try {
|
||
$res = addApprove(
|
||
'差旅报销',
|
||
$params['id'],
|
||
'app\common\model\project\ProjectTravelReimbursement',
|
||
$params['path'],
|
||
$params['flow_id'],
|
||
$admin_id
|
||
);
|
||
if($res){
|
||
ProjectTravelReimbursement::where('id',$params['id'])->update([
|
||
'approve_id' => $res,
|
||
]);
|
||
}
|
||
Db::commit();
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
} |