engineering/app/adminapi/logic/material/MaterialPurchaseRequestDetailLogic.php
2024-01-25 15:00:49 +08:00

142 lines
6.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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\material\Material;
use app\common\model\material\MaterialClassify;
use app\common\model\material\MaterialPurchaseRequest;
use app\common\model\material\MaterialPurchaseRequestDetail;
use app\common\logic\BaseLogic;
use app\common\model\project\Project;
use app\common\model\project\ProjectMaterialBudgetDetail;
use think\facade\Db;
/**
* 材料采购申请明细逻辑
* Class MaterialPurchaseRequestDetailLogic
* @package app\adminapi\logic\material
*/
class MaterialPurchaseRequestDetailLogic extends BaseLogic
{
/**
* @notes 添加材料采购申请明细
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/01/09 13:47
*/
public static function add(array $params): bool
{
Db::startTrans();
try {
MaterialPurchaseRequestDetail::create([
'num' => $params['num'],
]);
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
{
$data = MaterialPurchaseRequestDetail::where('id',$params['id'])->findOrEmpty();
Db::startTrans();
try {
MaterialPurchaseRequestDetail::where('id', $params['id'])->update([
'num' => $params['num'],
'remark' => $params['remark'] ?? '',
'update_time' => time()
]);
//预算数量
$project_material_budget_detail = ProjectMaterialBudgetDetail::field('num')->where('id',$data['project_material_budget_detail_id'])->findOrEmpty();
//已经申购的数量
$purchase_request_num = MaterialPurchaseRequestDetail::where('project_material_budget_detail_id',$data['project_material_budget_detail_id'])->where('id','<>',$params['id'])->sum('num');
if($project_material_budget_detail['num'] - $purchase_request_num - $params['num'] <=0){
ProjectMaterialBudgetDetail::where('id',$data['project_material_budget_detail_id'])->update(['is_residual'=>1,'update_time'=>time()]);
}else{
ProjectMaterialBudgetDetail::where('id',$data['project_material_budget_detail_id'])->update(['is_residual'=>0,'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 delete(array $params): bool
{
return MaterialPurchaseRequestDetail::destroy($params['id']);
}
/**
* @notes 获取材料采购申请明细详情
* @param $params
* @return array
* @author likeadmin
* @date 2024/01/09 13:47
*/
public static function detail($params): array
{
$data = MaterialPurchaseRequestDetail::field('id,material_purchase_request_id,project_material_budget_detail_id,num')->findOrEmpty($params['id']);
$material_purchase_request = MaterialPurchaseRequest::field('material_purchase_request_code,project_id,apply_date,arrival_date')->where('id',$data['material_purchase_request_id'])->findOrEmpty();
$project = Project::field('name,project_code')->where('id',$material_purchase_request['project_id'])->findOrEmpty();
$project_material_budget_detail = ProjectMaterialBudgetDetail::field('material_id')->where('id',$data['project_material_budget_detail_id'])->findOrEmpty();
$material = Material::field('first_level,second_level,three_level,name,code,specs,brand,parameter_description,unit')->where('id',$project_material_budget_detail['material_id'])->findOrEmpty();
$material_classify = MaterialClassify::where('id','in',[$material['first_level'],$material['second_level'],$material['three_level']])->column('name','id');
$data['material_purchase_request_code'] = $material_purchase_request['material_purchase_request_code'];
$data['project_name'] = $project['name'];
$data['project_code'] = $project['project_code'];
$data['apply_date'] = $material_purchase_request['apply_date'];
$data['arrival_date'] = $material_purchase_request['arrival_date'];
$data['material_first_level'] = !empty($material_classify[$material['first_level']]) ? $material_classify[$material['first_level']] : '';
$data['material_second_level'] = !empty($material_classify[$material['second_level']]) ? $material_classify[$material['second_level']] : '';
$data['material_three_level'] = !empty($material_classify[$material['three_level']]) ? $material_classify[$material['three_level']] : '';
$data['material_name'] = $material['name'];
$data['material_code'] = $material['code'];
$data['material_specs'] = $material['specs'];
$data['material_brand'] = $material['brand'];
$data['material_parameter_description'] = $material['parameter_description'];
$data['material_unit'] = $material['unit'];
return $data->toArray();
}
}