From 24a4e1f3b19f9122968a0906a39d1fe872e3a897 Mon Sep 17 00:00:00 2001 From: weiz Date: Tue, 9 Jan 2024 11:24:53 +0800 Subject: [PATCH] update --- .../ProjectMaterialBudgetDetailLists.php | 179 +++++++------ .../project/ProjectMaterialBudgetLists.php | 168 ++++++------ .../ProjectMaterialBudgetDetailLogic.php | 48 +++- .../project/ProjectMaterialBudgetLogic.php | 58 ++++- .../ProjectMaterialBudgetDetailValidate.php | 240 ++++++++++-------- .../project/ProjectMaterialBudgetValidate.php | 9 + .../project/ProjectMaterialBudgetDetail.php | 6 +- 7 files changed, 435 insertions(+), 273 deletions(-) diff --git a/app/adminapi/lists/project/ProjectMaterialBudgetDetailLists.php b/app/adminapi/lists/project/ProjectMaterialBudgetDetailLists.php index e4fb0fce9..0fd28d1c6 100644 --- a/app/adminapi/lists/project/ProjectMaterialBudgetDetailLists.php +++ b/app/adminapi/lists/project/ProjectMaterialBudgetDetailLists.php @@ -1,77 +1,104 @@ - ['material_budget_id', 'material_id', 'budget_type'], - ]; - } - - - /** - * @notes 获取材料预算明细列表 - * @return array - * @throws \think\db\exception\DataNotFoundException - * @throws \think\db\exception\DbException - * @throws \think\db\exception\ModelNotFoundException - * @author likeadmin - * @date 2024/01/08 21:52 - */ - public function lists(): array - { - return ProjectMaterialBudgetDetail::where($this->searchWhere) - ->field(['id', 'material_budget_id', 'material_id', 'budget_type', 'price', 'num', 'amount', 'remark']) - ->limit($this->limitOffset, $this->limitLength) - ->order(['id' => 'desc']) - ->select() - ->toArray(); - } - - - /** - * @notes 获取材料预算明细数量 - * @return int - * @author likeadmin - * @date 2024/01/08 21:52 - */ - public function count(): int - { - return ProjectMaterialBudgetDetail::where($this->searchWhere)->count(); - } - + ['material_budget_id', 'project_id', 'material_id', 'budget_type'], + ]; + } + + + /** + * @notes 获取材料预算明细列表 + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @author likeadmin + * @date 2024/01/08 21:52 + */ + public function lists(): array + { + return ProjectMaterialBudgetDetail::where($this->searchWhere) + ->field('id,project_id,material_budget_id,material_id,budget_type,price,num,amount,remark') + ->limit($this->limitOffset, $this->limitLength) + ->order(['id' => 'desc']) + ->select()->each(function($data){ + $material_budget = ProjectMaterialBudget::field('material_budget_code')->where('id',$data['material_budget_id'])->findOrEmpty(); + $project = Project::field('name,project_code')->where('id',$data['project_id'])->findOrEmpty(); + $material = Material::field('first_level,second_level,three_level,name,code,specs,brand,parameter_description,unit')->where('id',$data['material_id'])->findOrEmpty(); + $material_classify = MaterialClassify::where('id','in',[$material['first_level'],$material['second_level'],$material['three_level']])->column('name','id'); + $data['budget_type'] = $data->budget_type_text; + $data['material_budget_code'] = $material_budget['material_budget_code']; + $data['project_name'] = $project['name']; + $data['project_code'] = $project['project_code']; + $data['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']; + //申购数量 + $data['apply_num'] = 0; + //剩余预算数量 + $data['residual_num'] = $data['num'] - $data['apply_num']; + return $data; + }) + ->toArray(); + } + + + /** + * @notes 获取材料预算明细数量 + * @return int + * @author likeadmin + * @date 2024/01/08 21:52 + */ + public function count(): int + { + return ProjectMaterialBudgetDetail::where($this->searchWhere)->count(); + } + } \ No newline at end of file diff --git a/app/adminapi/lists/project/ProjectMaterialBudgetLists.php b/app/adminapi/lists/project/ProjectMaterialBudgetLists.php index 0408f6beb..add1c3b6d 100644 --- a/app/adminapi/lists/project/ProjectMaterialBudgetLists.php +++ b/app/adminapi/lists/project/ProjectMaterialBudgetLists.php @@ -1,77 +1,93 @@ - ['project_id', 'material_id', 'budget_type'], - ]; - } - - - /** - * @notes 获取材料预算列表 - * @return array - * @throws \think\db\exception\DataNotFoundException - * @throws \think\db\exception\DbException - * @throws \think\db\exception\ModelNotFoundException - * @author likeadmin - * @date 2024/01/08 16:30 - */ - public function lists(): array - { - return ProjectMaterialBudget::where($this->searchWhere) - ->field(['id', 'project_id', 'material_id', 'budget_type', 'num', 'price', 'amount', 'remark']) - ->limit($this->limitOffset, $this->limitLength) - ->order(['id' => 'desc']) - ->select() - ->toArray(); - } - - - /** - * @notes 获取材料预算数量 - * @return int - * @author likeadmin - * @date 2024/01/08 16:30 - */ - public function count(): int - { - return ProjectMaterialBudget::where($this->searchWhere)->count(); - } - + ['project_id', 'material_id', 'budget_type'], + + ]; + } + + + /** + * @notes 获取材料预算列表 + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @author likeadmin + * @date 2024/01/08 16:30 + */ + public function lists(): array + { + return ProjectMaterialBudget::where($this->searchWhere) + ->field('id,project_id,material_budget_code,remark,annex') + ->limit($this->limitOffset, $this->limitLength) + ->order(['id' => 'desc']) + ->select()->each(function($data){ + $project = Project::field('name,project_code')->where('id',$data['project_id'])->findOrEmpty(); + $data['project_name'] = $project['name']; + $data['project_code'] = $project['project_code']; + //预算总数量 + $data['total_num'] = ProjectMaterialBudgetDetail::where('material_budget_id',$data['id'])->sum('num'); + //预算总金额 + $data['total_amount'] = ProjectMaterialBudgetDetail::where('material_budget_id',$data['id'])->sum('amount'); + //申购总数量 + $data['total_apply_num'] = 0; + //剩余预算总数量 + $data['total_residual_num'] = $data['total_num'] - $data['total_apply_num']; + return $data; + }) + ->toArray(); + } + + + /** + * @notes 获取材料预算数量 + * @return int + * @author likeadmin + * @date 2024/01/08 16:30 + */ + public function count(): int + { + return ProjectMaterialBudget::where($this->searchWhere)->count(); + } + } \ No newline at end of file diff --git a/app/adminapi/logic/project/ProjectMaterialBudgetDetailLogic.php b/app/adminapi/logic/project/ProjectMaterialBudgetDetailLogic.php index 8b4b79bb5..6c3c07323 100644 --- a/app/adminapi/logic/project/ProjectMaterialBudgetDetailLogic.php +++ b/app/adminapi/logic/project/ProjectMaterialBudgetDetailLogic.php @@ -15,6 +15,10 @@ namespace app\adminapi\logic\project; +use app\common\model\material\Material; +use app\common\model\material\MaterialClassify; +use app\common\model\project\Project; +use app\common\model\project\ProjectMaterialBudget; use app\common\model\project\ProjectMaterialBudgetDetail; use app\common\logic\BaseLogic; use think\facade\Db; @@ -38,18 +42,19 @@ class ProjectMaterialBudgetDetailLogic extends BaseLogic */ public static function add(array $params): bool { + $material_budget = ProjectMaterialBudget::field('project_id')->where('id',$params['material_budget_id'])->findOrEmpty(); Db::startTrans(); try { ProjectMaterialBudgetDetail::create([ + 'project_id' => $material_budget['project_id'], 'material_budget_id' => $params['material_budget_id'], 'material_id' => $params['material_id'], 'budget_type' => $params['budget_type'], 'price' => $params['price'], 'num' => $params['num'], - 'amount' => $params['amount'], - 'remark' => $params['remark'], + 'amount' => $params['price'] * $params['num'], + 'remark' => $params['remark'] ?? '', ]); - Db::commit(); return true; } catch (\Exception $e) { @@ -69,18 +74,19 @@ class ProjectMaterialBudgetDetailLogic extends BaseLogic */ public static function edit(array $params): bool { + $material_budget = ProjectMaterialBudget::field('project_id')->where('id',$params['material_budget_id'])->findOrEmpty(); Db::startTrans(); try { ProjectMaterialBudgetDetail::where('id', $params['id'])->update([ - 'material_budget_id' => $params['material_budget_id'], - 'material_id' => $params['material_id'], - 'budget_type' => $params['budget_type'], - 'price' => $params['price'], - 'num' => $params['num'], - 'amount' => $params['amount'], - 'remark' => $params['remark'], + 'project_id' => $material_budget['project_id'], + 'material_budget_id' => $params['material_budget_id'], + 'material_id' => $params['material_id'], + 'budget_type' => $params['budget_type'], + 'price' => $params['price'], + 'num' => $params['num'], + 'amount' => $params['price'] * $params['num'], + 'remark' => $params['remark'] ?? '', ]); - Db::commit(); return true; } catch (\Exception $e) { @@ -113,6 +119,24 @@ class ProjectMaterialBudgetDetailLogic extends BaseLogic */ public static function detail($params): array { - return ProjectMaterialBudgetDetail::findOrEmpty($params['id'])->toArray(); + $data = ProjectMaterialBudgetDetail::field('id,project_id,material_budget_id,material_id,budget_type,price,num,amount,remark')->findOrEmpty($params['id']); + $material_budget = ProjectMaterialBudget::field('material_budget_code')->where('id',$data['material_budget_id'])->findOrEmpty(); + $project = Project::field('name,project_code')->where('id',$data['project_id'])->findOrEmpty(); + $material = Material::field('first_level,second_level,three_level,name,code,specs,brand,parameter_description,unit')->where('id',$data['material_id'])->findOrEmpty(); + $material_classify = MaterialClassify::where('id','in',[$material['first_level'],$material['second_level'],$material['three_level']])->column('name','id'); + $data['budget_type_text'] = $data->budget_type_text; + $data['material_budget_code'] = $material_budget['material_budget_code']; + $data['project_name'] = $project['name']; + $data['project_code'] = $project['project_code']; + $data['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(); } } \ No newline at end of file diff --git a/app/adminapi/logic/project/ProjectMaterialBudgetLogic.php b/app/adminapi/logic/project/ProjectMaterialBudgetLogic.php index ffffb82f5..78663cf29 100644 --- a/app/adminapi/logic/project/ProjectMaterialBudgetLogic.php +++ b/app/adminapi/logic/project/ProjectMaterialBudgetLogic.php @@ -15,8 +15,12 @@ namespace app\adminapi\logic\project; +use app\common\model\dept\Dept; +use app\common\model\dept\Orgs; +use app\common\model\project\Project; use app\common\model\project\ProjectMaterialBudget; use app\common\logic\BaseLogic; +use app\common\model\project\ProjectMaterialBudgetDetail; use think\facade\Db; @@ -38,9 +42,10 @@ class ProjectMaterialBudgetLogic extends BaseLogic */ public static function add(array $params): bool { + $detail = json_decode($params['material_budget_detail'],true); Db::startTrans(); try { - ProjectMaterialBudget::create([ + $project_material_budget = ProjectMaterialBudget::create([ 'org_id' => $params['org_id'], 'dept_id' => $params['dept_id'], 'project_id' => $params['project_id'], @@ -48,6 +53,19 @@ class ProjectMaterialBudgetLogic extends BaseLogic 'remark' => $params['remark'] ?? '', 'annex' => !empty($params['annex']) ? $params['annex'] : null, ]); + foreach ($detail as $item) + { + ProjectMaterialBudgetDetail::create([ + 'project_id' => $params['project_id'], + 'material_budget_id' => $project_material_budget->id, + 'material_id' => $item['material_id'], + 'budget_type' => 0, + 'price' => $item['price'], + 'num' => $item['num'], + 'amount' => $item['price'] * $item['num'], + 'remark' => $item['remark'] ?? '', + ]); + } Db::commit(); return true; } catch (\Exception $e) { @@ -67,15 +85,41 @@ class ProjectMaterialBudgetLogic extends BaseLogic */ public static function edit(array $params): bool { + $detail = json_decode($params['material_budget_detail'],true); Db::startTrans(); try { - ProjectMaterialBudget::where('id', $params['id'])->update([ + ProjectMaterialBudget::where('id', $params['id'])->update([ 'org_id' => $params['org_id'], 'dept_id' => $params['dept_id'], 'project_id' => $params['project_id'], 'remark' => $params['remark'] ?? '', 'annex' => !empty($params['annex']) ? $params['annex'] : null, ]); + foreach ($detail as $item) + { + if(isset($item['id']) && $item['id'] != ''){ + ProjectMaterialBudgetDetail::where('id',$item['id'])->update([ + 'project_id' => $params['project_id'], + 'material_budget_id' => $params['id'], + 'material_id' => $item['material_id'], + 'price' => $item['price'], + 'num' => $item['num'], + 'amount' => $item['price'] * $item['num'], + 'remark' => $item['remark'] ?? '', + ]); + }else{ + ProjectMaterialBudgetDetail::create([ + 'project_id' => $params['project_id'], + 'material_budget_id' => $params['id'], + 'material_id' => $item['material_id'], + 'budget_type' => 0, + 'price' => $item['price'], + 'num' => $item['num'], + 'amount' => $item['price'] * $item['num'], + 'remark' => $item['remark'] ?? '', + ]); + } + } Db::commit(); return true; } catch (\Exception $e) { @@ -108,6 +152,14 @@ class ProjectMaterialBudgetLogic extends BaseLogic */ public static function detail($params): array { - return ProjectMaterialBudget::findOrEmpty($params['id'])->toArray(); + $data = ProjectMaterialBudget::field('id,org_id,dept_id,project_id,material_budget_code,remark,annex')->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']; + return $data->toArray(); } } \ No newline at end of file diff --git a/app/adminapi/validate/project/ProjectMaterialBudgetDetailValidate.php b/app/adminapi/validate/project/ProjectMaterialBudgetDetailValidate.php index 68c8cb9c6..312b00263 100644 --- a/app/adminapi/validate/project/ProjectMaterialBudgetDetailValidate.php +++ b/app/adminapi/validate/project/ProjectMaterialBudgetDetailValidate.php @@ -1,106 +1,136 @@ - 'require', - 'material_budget_id' => 'require', - 'material_id' => 'require', - 'budget_type' => 'require', - 'price' => 'require', - 'num' => 'require', - 'amount' => 'require', - ]; - - - /** - * 参数描述 - * @var string[] - */ - protected $field = [ - 'id' => 'id', - 'material_budget_id' => '材料预算id', - 'material_id' => '材料id', - 'budget_type' => '预算类型', - 'price' => '材料单价', - 'num' => '材料数量', - 'amount' => '金额', - ]; - - - /** - * @notes 添加场景 - * @return ProjectMaterialBudgetDetailValidate - * @author likeadmin - * @date 2024/01/08 21:52 - */ - public function sceneAdd() - { - return $this->only(['material_budget_id','material_id','budget_type','price','num','amount']); - } - - - /** - * @notes 编辑场景 - * @return ProjectMaterialBudgetDetailValidate - * @author likeadmin - * @date 2024/01/08 21:52 - */ - public function sceneEdit() - { - return $this->only(['id','material_budget_id','material_id','budget_type','price','num','amount']); - } - - - /** - * @notes 删除场景 - * @return ProjectMaterialBudgetDetailValidate - * @author likeadmin - * @date 2024/01/08 21:52 - */ - public function sceneDelete() - { - return $this->only(['id']); - } - - - /** - * @notes 详情场景 - * @return ProjectMaterialBudgetDetailValidate - * @author likeadmin - * @date 2024/01/08 21:52 - */ - public function sceneDetail() - { - return $this->only(['id']); - } - + 'require', + 'material_budget_id' => 'require|checkMaterialBudget', + 'material_id' => 'require|checkMaterial', + 'budget_type' => 'require|checkBudgetType', + 'price' => 'require|float|egt:0', + 'num' => 'require|integer|gt:0', + ]; + + + /** + * 参数描述 + * @var string[] + */ + protected $message = [ + 'id.require' => '缺少必要参数', + 'material_budget_id.require' => '请选择材料预算', + 'material_id.require' => '请选择材料', + 'budget_type.require' => '请选择类型', + 'price.require' => '请填写单价', + 'price.float' => '单价值必须是数字', + 'price.egt' => '单价值必须大于等于0', + 'num.require' => '请填写数量', + 'num.integer' => '数量值必须时整数', + 'num.gt' => '数量值必须大于0', + ]; + + + /** + * @notes 添加场景 + * @return ProjectMaterialBudgetDetailValidate + * @author likeadmin + * @date 2024/01/08 21:52 + */ + public function sceneAdd() + { + return $this->remove('id',true); + } + + + /** + * @notes 编辑场景 + * @return ProjectMaterialBudgetDetailValidate + * @author likeadmin + * @date 2024/01/08 21:52 + */ + public function sceneEdit() + {} + + + /** + * @notes 删除场景 + * @return ProjectMaterialBudgetDetailValidate + * @author likeadmin + * @date 2024/01/08 21:52 + */ + public function sceneDelete() + { + return $this->only(['id']); + } + + + /** + * @notes 详情场景 + * @return ProjectMaterialBudgetDetailValidate + * @author likeadmin + * @date 2024/01/08 21:52 + */ + public function sceneDetail() + { + return $this->only(['id']); + } + + public function checkMaterialBudget($value): bool|string + { + $material_budget = ProjectMaterialBudget::where('id',$value)->findOrEmpty(); + if($material_budget->isEmpty()){ + return '材料预算信息不存在'; + } + return true; + } + + public function checkMaterial($value): bool|string + { + $material = Material::where('id',$value)->findOrEmpty(); + if($material->isEmpty()){ + return '材料信息不存在'; + } + return true; + } + + public function checkBudgetType($value): bool|string + { + $dict = DictData::where('type_value','budget_type')->column('value'); + if(!in_array($value,$dict)){ + return '类型值无效'; + } + return true; + } + } \ No newline at end of file diff --git a/app/adminapi/validate/project/ProjectMaterialBudgetValidate.php b/app/adminapi/validate/project/ProjectMaterialBudgetValidate.php index f2759ebaf..9992c1f32 100644 --- a/app/adminapi/validate/project/ProjectMaterialBudgetValidate.php +++ b/app/adminapi/validate/project/ProjectMaterialBudgetValidate.php @@ -18,6 +18,7 @@ use app\common\model\dept\Dept; use app\common\model\dept\Orgs; use app\common\model\material\Material; use app\common\model\project\Project; +use app\common\model\project\ProjectMaterialBudgetDetail; use app\common\validate\BaseValidate; @@ -44,6 +45,8 @@ class ProjectMaterialBudgetValidate extends BaseValidate protected $message = [ 'id.require' => '缺少必要参数', + 'org_id.require' => '请选择组织', + 'dept_id.require' => '请选择部门', 'project_id.require' => '请选择项目', 'material_budget_detail.require' => '请填写材料预算清单' ]; @@ -146,6 +149,12 @@ class ProjectMaterialBudgetValidate extends BaseValidate return '材料预算清单数据格式错误'; } foreach($detail as $v) { + if(isset($v['id']) && $v['id'] != ''){ + $data_detail = ProjectMaterialBudgetDetail::where('id',$v['id'])->findOrEmpty(); + if($data_detail->isEmpty()){ + return '材料预算明细信息不存在'; + } + } if(empty($v['material_id'])){ return '请选择材料'; }else{ diff --git a/app/common/model/project/ProjectMaterialBudgetDetail.php b/app/common/model/project/ProjectMaterialBudgetDetail.php index 82a8e2b45..1143ff1d4 100644 --- a/app/common/model/project/ProjectMaterialBudgetDetail.php +++ b/app/common/model/project/ProjectMaterialBudgetDetail.php @@ -16,6 +16,7 @@ namespace app\common\model\project; use app\common\model\BaseModel; +use app\common\model\dict\DictData; use think\model\concern\SoftDelete; @@ -30,5 +31,8 @@ class ProjectMaterialBudgetDetail extends BaseModel protected $name = 'project_material_budget_detail'; protected $deleteTime = 'delete_time'; - + public function getBudgetTypeTextAttr($value,$data){ + $dict = DictData::where('type_value','budget_type')->column('name','value'); + return $dict[$data['budget_type']]; + } } \ No newline at end of file