180 lines
6.4 KiB
PHP
180 lines
6.4 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\material;
|
||
|
||
|
||
use app\common\model\dept\Dept;
|
||
use app\common\model\dept\Orgs;
|
||
use app\common\model\material\MaterialPurchaseRequest;
|
||
use app\common\logic\BaseLogic;
|
||
use app\common\model\material\MaterialPurchaseRequestDetail;
|
||
use app\common\model\oa\FlowApprove;
|
||
use app\common\model\project\Project;
|
||
use app\common\model\project\ProjectMaterialBudgetDetail;
|
||
use think\facade\Db;
|
||
|
||
|
||
/**
|
||
* 材料采购申请逻辑
|
||
* Class MaterialPurchaseRequestLogic
|
||
* @package app\adminapi\logic\material
|
||
*/
|
||
class MaterialPurchaseRequestLogic extends BaseLogic
|
||
{
|
||
|
||
|
||
/**
|
||
* @notes 添加材料采购申请
|
||
* @param array $params
|
||
* @return bool
|
||
* @author likeadmin
|
||
* @date 2024/01/09 13:47
|
||
*/
|
||
public static function add(array $params,$admin_id): bool
|
||
{
|
||
Db::startTrans();
|
||
try {
|
||
$MaterialPurchaseRequestRes = MaterialPurchaseRequest::create([
|
||
'material_purchase_request_code' => data_unique_code('采购申请'),
|
||
'org_id' => $params['org_id'],
|
||
'dept_id' => $params['dept_id'],
|
||
'project_id' => $params['project_id'],
|
||
'apply_date' => strtotime($params['apply_date']),
|
||
'arrival_date' => strtotime($params['arrival_date']),
|
||
'remark' => $params['remark'] ?? '',
|
||
'annex' => $params['annex']? json_encode($params['annex']) : null,
|
||
]);
|
||
foreach ($params['purchase_request_detail'] as $item)
|
||
{
|
||
MaterialPurchaseRequestDetail::create([
|
||
'project_material_budget_detail_id' => $item['project_material_budget_detail_id'],
|
||
'material_purchase_request_id' => $MaterialPurchaseRequestRes->id,
|
||
'num' => $item['num'],
|
||
'remark' => $item['remark'] ?? '',
|
||
]);
|
||
//预算数量
|
||
$project_material_budget_detail = ProjectMaterialBudgetDetail::field('num')->where('id',$item['project_material_budget_detail_id'])->findOrEmpty();
|
||
//已经申购的数量
|
||
$purchase_request_num = MaterialPurchaseRequestDetail::where('project_material_budget_detail_id',$item['project_material_budget_detail_id'])->sum('num');
|
||
if($project_material_budget_detail['num'] - $purchase_request_num - $item['num'] <=0){
|
||
ProjectMaterialBudgetDetail::where('id',$item['project_material_budget_detail_id'])->update(['is_residual'=>1,'update_time'=>time()]);
|
||
}
|
||
}
|
||
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/09 13:47
|
||
*/
|
||
public static function edit(array $params): bool
|
||
{
|
||
Db::startTrans();
|
||
try {
|
||
MaterialPurchaseRequest::where('id', $params['id'])->update([
|
||
'org_id' => $params['org_id'],
|
||
'dept_id' => $params['dept_id'],
|
||
'project_id' => $params['project_id'],
|
||
'apply_date' => strtotime($params['apply_date']),
|
||
'arrival_date' => strtotime($params['arrival_date']),
|
||
'remark' => $params['remark'] ?? '',
|
||
'annex' => $params['annex']? json_encode($params['annex']) : null,
|
||
]);
|
||
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/09 13:47
|
||
*/
|
||
public static function delete(array $params): bool
|
||
{
|
||
return MaterialPurchaseRequest::destroy($params['id']);
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 获取材料采购申请详情
|
||
* @param $params
|
||
* @return array
|
||
* @author likeadmin
|
||
* @date 2024/01/09 13:47
|
||
*/
|
||
public static function detail($params): array
|
||
{
|
||
$data = MaterialPurchaseRequest::field('id,org_id,dept_id,project_id,material_purchase_request_code,apply_date,arrival_date,remark,annex,approve_id')->findOrEmpty($params['id']);
|
||
$org = Orgs::field('name')->where('id',$data['org_id'])->findOrEmpty();
|
||
$dept = Dept::field('name')->where('id',$data['dept_id'])->findOrEmpty();
|
||
$project = Project::field('name,project_code')->where('id',$data['project_id'])->findOrEmpty();
|
||
$data['org_name'] = $org['name'];
|
||
$data['dept_name'] = $dept['name'];
|
||
$data['project_name'] = $project['name'];
|
||
$data['project_code'] = $project['project_code'];
|
||
$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 = MaterialPurchaseRequest::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\material\MaterialPurchaseRequest',
|
||
$params['path'],
|
||
$params['flow_id'],
|
||
$admin_id
|
||
);
|
||
if($res){
|
||
MaterialPurchaseRequest::where('id',$params['id'])->update([
|
||
'approve_id' => $res,
|
||
]);
|
||
}
|
||
Db::commit();
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
} |