engineering/app/adminapi/validate/material/MaterialPurchaseRequestValidate.php
2024-02-18 15:53:06 +08:00

177 lines
5.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\validate\material;
use app\common\model\dept\Dept;
use app\common\model\dept\Orgs;
use app\common\model\material\MaterialPurchaseRequestDetail;
use app\common\model\project\Project;
use app\common\model\project\ProjectMaterialBudgetDetail;
use app\common\validate\BaseValidate;
/**
* 材料采购申请验证器
* Class MaterialPurchaseRequestValidate
* @package app\adminapi\validate\material
*/
class MaterialPurchaseRequestValidate extends BaseValidate
{
/**
* 设置校验规则
* @var string[]
*/
protected $rule = [
'id' => 'require',
'org_id' => 'require|checkOrg',
'dept_id' => 'require|checkDept',
'project_id' => 'require|checkProject',
'apply_date' => 'require|dateFormat:Y-m-d',
'arrival_date' => 'require|dateFormat:Y-m-d',
'annex' => 'checkAnnex',
'purchase_request_detail' => 'require|checkPurchaseRequestDetail',
];
protected $message = [
'id.require' => '缺少必要参数',
'org_id.require' => '请选择组织',
'dept_id.require' => '请选择部门',
'project_id.require' => '请选择项目',
'apply_date.require' => '请选择申请日期',
'apply_date.dateFormat' => '申请日期数据格式错误',
'arrival_date.require' => '请选择希望到货日期',
'arrival_date.dateFormat' => '希望到货日期数据格式错误',
'purchase_request_detail.require' => '申购明细内容不能为空',
];
/**
* @notes 添加场景
* @return MaterialPurchaseRequestValidate
* @author likeadmin
* @date 2024/01/09 13:47
*/
public function sceneAdd()
{
return $this->remove('id',true);
}
/**
* @notes 编辑场景
* @return MaterialPurchaseRequestValidate
* @author likeadmin
* @date 2024/01/09 13:47
*/
public function sceneEdit()
{}
/**
* @notes 删除场景
* @return MaterialPurchaseRequestValidate
* @author likeadmin
* @date 2024/01/09 13:47
*/
public function sceneDelete()
{
return $this->only(['id']);
}
/**
* @notes 详情场景
* @return MaterialPurchaseRequestValidate
* @author likeadmin
* @date 2024/01/09 13:47
*/
public function sceneDetail()
{
return $this->only(['id']);
}
public function checkOrg($value): bool|string
{
$org = Orgs::where('id',$value)->findOrEmpty();
if($org->isEmpty()) {
return '组织不存在';
}
return true;
}
public function checkDept($value,$rule,$data): bool|string
{
$dept = Dept::where('id',$value)->findOrEmpty();
if($dept->isEmpty()){
return '部门不存在';
}
if($dept['org_id'] != $data['org_id']){
return '当前部门不属于所选择的组织';
}
return true;
}
public function checkProject($value): bool|string
{
$project = Project::where('id',$value)->findOrEmpty();
if($project->isEmpty()){
return '项目不存在';
}
return true;
}
public function checkAnnex($value): bool|string
{
if(!empty($value) && $value != ''){
if(!is_array($value)){
return '附件格式错误';
}
}
return true;
}
public function checkPurchaseRequestDetail($value): bool|string
{
$detail = $value;//json_decode($value,true);
if(empty($detail) || !is_array($detail)){
return '申购明细数据格式错误';
}
foreach($detail as $v) {
$project_material_budget_detail = ProjectMaterialBudgetDetail::where('id',$v['project_material_budget_detail_id'])->findOrEmpty();
if(empty($v['project_material_budget_detail_id'])){
return '请选择材料预算明细';
}else{
if($project_material_budget_detail->isEmpty()){
return '材料预算明细信息不存在';
}
}
if(empty($v['num'])){
return '数量不能为空';
}else{
if(!is_numeric($v['num']) || $v['num'] < 0){
return '数量必须是大于0的数字';
}
$PurchaseRequestNum = MaterialPurchaseRequestDetail::where('project_material_budget_detail_id',$v['project_material_budget_detail_id'])->sum('num');
if($v['num'] > ($project_material_budget_detail['num'] - $PurchaseRequestNum)){
return '申请采购数量不能大于剩余预算数量';
}
}
}
return true;
}
}