物品领用

This commit is contained in:
mzl
2025-12-05 09:22:53 +00:00
parent 253616b2bb
commit 399eaa2b5c
40 changed files with 2159 additions and 308 deletions
@@ -0,0 +1,33 @@
<?php
namespace app\common\controller;
use app\common\controller\BaseController;
use app\common\traits\crud\CrudControllerTrait;
use app\common\service\InvRequisitionAttachmentService;
class InvRequisitionAttachmentController extends BaseController
{
use CrudControllerTrait;
protected $service;
public function __construct()
{
$this->service = new InvRequisitionAttachmentService();
}
/**
* 列表页数据格式化
*
* @param array $data
* @return array
*/
// protected function indexFormat($data)
// {
// if (isset($data['created_at']))
// $data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
// if (isset($data['updated_at']))
// $data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
// return $data;
// }
}
@@ -0,0 +1,33 @@
<?php
namespace app\common\controller;
use app\common\controller\BaseController;
use app\common\traits\crud\CrudControllerTrait;
use app\common\service\InvRequisitionItemService;
class InvRequisitionItemController extends BaseController
{
use CrudControllerTrait;
protected $service;
public function __construct()
{
$this->service = new InvRequisitionItemService();
}
/**
* 列表页数据格式化
*
* @param array $data
* @return array
*/
// protected function indexFormat($data)
// {
// if (isset($data['created_at']))
// $data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
// if (isset($data['updated_at']))
// $data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
// return $data;
// }
}
@@ -0,0 +1,33 @@
<?php
namespace app\common\controller;
use app\common\controller\BaseController;
use app\common\traits\crud\CrudControllerTrait;
use app\common\service\InvRequisitionItemReturnAttachmentService;
class InvRequisitionItemReturnAttachmentController extends BaseController
{
use CrudControllerTrait;
protected $service;
public function __construct()
{
$this->service = new InvRequisitionItemReturnAttachmentService();
}
/**
* 列表页数据格式化
*
* @param array $data
* @return array
*/
// protected function indexFormat($data)
// {
// if (isset($data['created_at']))
// $data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
// if (isset($data['updated_at']))
// $data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
// return $data;
// }
}
@@ -0,0 +1,33 @@
<?php
namespace app\common\controller;
use app\common\controller\BaseController;
use app\common\traits\crud\CrudControllerTrait;
use app\common\service\InvRequisitionItemReturnService;
class InvRequisitionItemReturnController extends BaseController
{
use CrudControllerTrait;
protected $service;
public function __construct()
{
$this->service = new InvRequisitionItemReturnService();
}
/**
* 列表页数据格式化
*
* @param array $data
* @return array
*/
// protected function indexFormat($data)
// {
// if (isset($data['created_at']))
// $data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
// if (isset($data['updated_at']))
// $data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
// return $data;
// }
}
+17
View File
@@ -0,0 +1,17 @@
<?php
namespace app\common\logic;
use app\common\traits\crud\CrudLogicTrait;
use app\common\model\InvItem;
class InvItemLogic
{
use CrudLogicTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = InvItem::class;
}
@@ -0,0 +1,17 @@
<?php
namespace app\common\logic;
use app\common\traits\crud\CrudLogicTrait;
use app\common\model\InvRequisitionAttachment;
class InvRequisitionAttachmentLogic
{
use CrudLogicTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = InvRequisitionAttachment::class;
}
@@ -0,0 +1,17 @@
<?php
namespace app\common\logic;
use app\common\traits\crud\CrudLogicTrait;
use app\common\model\InvRequisitionItem;
class InvRequisitionItemLogic
{
use CrudLogicTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = InvRequisitionItem::class;
}
@@ -0,0 +1,17 @@
<?php
namespace app\common\logic;
use app\common\traits\crud\CrudLogicTrait;
use app\common\model\InvRequisitionItemReturnAttachment;
class InvRequisitionItemReturnAttachmentLogic
{
use CrudLogicTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = InvRequisitionItemReturnAttachment::class;
}
@@ -0,0 +1,17 @@
<?php
namespace app\common\logic;
use app\common\traits\crud\CrudLogicTrait;
use app\common\model\InvRequisitionItemReturn;
class InvRequisitionItemReturnLogic
{
use CrudLogicTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = InvRequisitionItemReturn::class;
}
+17
View File
@@ -0,0 +1,17 @@
<?php
namespace app\common\logic;
use app\common\traits\crud\CrudLogicTrait;
use app\common\model\InvRequisition;
class InvRequisitionLogic
{
use CrudLogicTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = InvRequisition::class;
}
+52
View File
@@ -0,0 +1,52 @@
<?php
namespace app\common\model;
use support\Model;
class InvItem extends Model
{
/**
* The connection name for the model.
*
* @var string|null
*/
protected $connection = 'mysql';
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'inv_item';
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'id';
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = true;
protected $dateFormat = 'U';
/**
* 允许自动填充模型字段
*/
protected $fillable = ['id', 'company_id', 'item_code', 'item_name',
'category_id', 'spec_model', 'unit', 'is_consumable', 'stock_qty', 'manage_user_id', 'location', 'remark',
'sort', 'status',
'created_at', 'updated_at'];
/**
* 不允许自动填充模型字段
*/
protected $guarded = [];
}
+58
View File
@@ -0,0 +1,58 @@
<?php
namespace app\common\model;
use support\Model;
class InvRequisition extends Model
{
/**
* The connection name for the model.
*
* @var string|null
*/
protected $connection = 'mysql';
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'inv_requisition';
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'id';
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = true;
protected $dateFormat = 'U';
/**
* 允许自动填充模型字段
*/
protected $fillable = ['id', 'order_no', 'applicant_company_id', 'dept_id', 'applicant_id', 'applicant_name', 'leader_id', 'manager_id', 'borrow_reason', 'use_start_date', 'use_end_date', 'status', 'is_returned', 'remark', 'created_at', 'updated_at'];
/**
* 不允许自动填充模型字段
*/
protected $guarded = [];
const STATUS_DRAFT = 0; // 草稿
const STATUS_LEADER = 10; // 待直属上级审批
const STATUS_MANAGE = 20; // 待管理员审批
const STATUS_APPROVED = 30; // 已通过
const STATUS_REJECTEDT = 40; // 已驳回
const STATUS_CANCELED = 50; // 已取消
}
@@ -0,0 +1,49 @@
<?php
namespace app\common\model;
use support\Model;
class InvRequisitionAttachment extends Model
{
/**
* The connection name for the model.
*
* @var string|null
*/
protected $connection = 'mysql';
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'inv_requisition_attachment';
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'id';
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = true;
protected $dateFormat = 'U';
/**
* 允许自动填充模型字段
*/
protected $fillable = ['id', 'requisition_id', 'file_name', 'file_path', 'file_size', 'created_at', 'updated_at'];
/**
* 不允许自动填充模型字段
*/
protected $guarded = [];
}
+49
View File
@@ -0,0 +1,49 @@
<?php
namespace app\common\model;
use support\Model;
class InvRequisitionItem extends Model
{
/**
* The connection name for the model.
*
* @var string|null
*/
protected $connection = 'mysql';
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'inv_requisition_item';
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'id';
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = true;
protected $dateFormat = 'U';
/**
* 允许自动填充模型字段
*/
protected $fillable = ['id', 'requisition_id', 'item_id', 'item_name_snap', 'spec_model_snap', 'unit_snap', 'is_consumable_snap', 'request_qty', 'approved_qty', 'returned_qty', 'is_finished', 'remark', 'created_at', 'updated_at'];
/**
* 不允许自动填充模型字段
*/
protected $guarded = [];
}
@@ -0,0 +1,49 @@
<?php
namespace app\common\model;
use support\Model;
class InvRequisitionItemReturn extends Model
{
/**
* The connection name for the model.
*
* @var string|null
*/
protected $connection = 'mysql';
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'inv_requisition_item_return';
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'id';
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = true;
protected $dateFormat = 'U';
/**
* 允许自动填充模型字段
*/
protected $fillable = ['id', 'requisition_id', 'requisition_item_id', 'item_id', 'apply_user_id', 'apply_user_name', 'return_qty', 'status', 'handler_id', 'handler_name', 'return_status', 'apply_remark', 'handle_remark', 'return_at', 'created_at', 'updated_at'];
/**
* 不允许自动填充模型字段
*/
protected $guarded = [];
}
@@ -0,0 +1,49 @@
<?php
namespace app\common\model;
use support\Model;
class InvRequisitionItemReturnAttachment extends Model
{
/**
* The connection name for the model.
*
* @var string|null
*/
protected $connection = 'mysql';
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'inv_requisition_item_return_attachment';
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'id';
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = true;
protected $dateFormat = 'U';
/**
* 允许自动填充模型字段
*/
protected $fillable = ['id', 'return_id', 'file_name', 'file_path', 'file_size', 'created_at', 'updated_at'];
/**
* 不允许自动填充模型字段
*/
protected $guarded = [];
}
+65
View File
@@ -0,0 +1,65 @@
<?php
namespace app\common\service;
use app\common\validate\InvItemValidate;
use app\common\traits\crud\CrudServiceTrait;
use app\common\model\InvItem;
use support\Request;
class InvItemService
{
use CrudServiceTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = InvItem::class;
/**
* 验证器
*/
protected $validator = InvItemValidate::class;
/**
* 获取验证器
*/
public function getValidator()
{
return new $this->validator;
}
/**
* 生成 8-12 位的物品编码(默认10位)
*
* @param int $length 编码长度,默认为10(范围 8-12)
* @return string 返回大写的字母数字组合编码
*/
private function generateItemCode($length = 10)
{
// 排除易混淆字符:0, O, o, 1, I, l
$chars = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
$code = '';
$maxIndex = strlen($chars) - 1;
for ($i = 0; $i < $length; $i++) {
$code .= $chars[mt_rand(0, $maxIndex)];
}
return $code;
}
public function beforeServiceStore($data, Request $request): array
{
$data['item_code'] = $this->generateItemCode();
$data['category_id'] = 0;
$data['spec_model'] = '';
$data['unit'] = '件';
$data['is_consumable'] = 0;
$data['stock_qty'] = 1;
$data['location'] = '';
return $data;
}
}
@@ -0,0 +1,31 @@
<?php
namespace app\common\service;
use app\common\validate\InvRequisitionAttachmentValidate;
use app\common\traits\crud\CrudServiceTrait;
use app\common\model\InvRequisitionAttachment;
class InvRequisitionAttachmentService
{
use CrudServiceTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = InvRequisitionAttachment::class;
/**
* 验证器
*/
protected $validator = InvRequisitionAttachmentValidate::class;
/**
* 获取验证器
*/
public function getValidator()
{
return new $this->validator;
}
}
@@ -0,0 +1,31 @@
<?php
namespace app\common\service;
use app\common\validate\InvRequisitionItemReturnAttachmentValidate;
use app\common\traits\crud\CrudServiceTrait;
use app\common\model\InvRequisitionItemReturnAttachment;
class InvRequisitionItemReturnAttachmentService
{
use CrudServiceTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = InvRequisitionItemReturnAttachment::class;
/**
* 验证器
*/
protected $validator = InvRequisitionItemReturnAttachmentValidate::class;
/**
* 获取验证器
*/
public function getValidator()
{
return new $this->validator;
}
}
@@ -0,0 +1,297 @@
<?php
namespace app\common\service;
use app\common\model\InvRequisitionItem;
use app\common\validate\InvRequisitionItemReturnValidate;
use app\common\traits\crud\CrudServiceTrait;
use app\common\model\InvRequisitionItemReturn;
use support\Request;
use support\Db;
use support\exception\BusinessException;
use app\common\service\UserService;
use Illuminate\Database\Eloquent\Model;
use app\common\model\InvRequisitionItemReturnAttachment;
class InvRequisitionItemReturnService
{
use CrudServiceTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = InvRequisitionItemReturn::class;
/**
* 验证器
*/
protected $validator = InvRequisitionItemReturnValidate::class;
/**
* 获取验证器
*/
public function getValidator()
{
return new $this->validator;
}
/**
* 将二维数组按指定 key 重组为关联数组,该 key 的值作为新数组的键,
* 其余元素组成子数组作为值。
*
* @param array $array 原始二维数组
* @param string $keyField 用作新键的字段名(如 'id')
* @return array 重组后的数组
*/
private function groupArrayByKey($array, $keyField = 'id')
{
$result = [];
foreach ($array as $item) {
if (!isset($item[$keyField])) {
continue; // 跳过没有该 key 的项
}
$key = $item[$keyField];
// 移除用作键的字段
unset($item[$keyField]);
$result[$key] = $item;
}
return $result;
}
/**
* 创建物品归还申请前置处理
*
* @param array $data 归还申请数据
* @param Request $request 请求对象
* @return array 处理后的数据
*/
public function beforeServiceStore($data, Request $request): array
{
try {
// // 模拟数据
// $data = [
// [
// 'requisition_item_id' => 9,
// 'return_qty' => 1,
// 'apply_remark' => '申请备注',
// 'attachment' => [
// [
// 'original_name' => '附件1.jpg',
// 'file_path' => '/uploads/2023/07/01/attachment1.jpg'
// ],
// [
// 'original_name' => '附件2.jpg',
// 'file_path' => '/uploads/2023/07/01/attachment2.jpg'
// ]
// ]
// ]
// ];
// 获取所有申请项ID
$reqItemIds = array_column($data, 'requisition_item_id');
// 参数校验
if (empty($reqItemIds)) {
throw new BusinessException('未提供有效的申请项ID');
}
// 将申请项按 ID 重组为关联数组
$newReqItems = $this->groupArrayByKey($data, 'requisition_item_id');
// 获取申请项明细
$reqItemsArr = InvRequisitionItem::whereIn('id', $reqItemIds)->get()->toArray();
// 检查是否获取到有效数据
if (empty($reqItemsArr)) {
throw new BusinessException('未找到对应的申请项');
}
// 获取用户信息
$userService = new UserService();
$user = $userService->getUserInfo($request->user->uid, '');
$arr = [];
foreach ($reqItemsArr as $reqItem) {
$reqItemId = $reqItem['id'];
// 检查是否有对应的新申请项数据
if (!isset($newReqItems[$reqItemId])) {
continue;
}
$arr[] = [
'requisition_id' => $reqItem['requisition_id'],
'requisition_item_id' => $reqItemId,
'item_id' => $reqItem['item_id'],
'apply_user_id' => $user['user']['id'],
'apply_user_name' => $user['user']['name'],
'return_qty' => $newReqItems[$reqItemId]['return_qty'],
'apply_remark' => $newReqItems[$reqItemId]['apply_remark'] ?? '',
'attachment' => $newReqItems[$reqItemId]['attachment'],
'return_at' => time()
];
}
return $arr;
} catch (BusinessException $e) {
throw $e;
} catch (\Throwable $th) {
throw new BusinessException('创建物品归还申请失败:' . $th->getMessage());
}
}
/**
* 处理物品申请
*
* @param Request $request
* @param array $data
* @return array
*/
public function serviceStore(Request $request, array $data, string $scene = '')
{
try {
// 关联表增加事务处理
return Db::transaction(function () use ($request, $data) {
$data = $this->preProcess('Store', $data, $request);
$dl = $this->getDataLimitFromRequest($request);
foreach ($data as $item) {
$created = $this->logicCreate($item, $dl);
$this->processRequisitionItemReturnAttachment($created, $item['attachment']);
}
return $this->postProcess('Store', $created);
});
} catch (\Throwable $e) {
throw new BusinessException("创建物品申请失败:" . $e->getMessage());
}
}
/**
* 更新物品归还申请前置处理
*
* @param array $data 归还申请数据
* @param Request $request 请求对象
* @return array 处理后的数据
*/
public function beforeServiceUpdate($data, Request $request): array
{
// // 模拟数据
// $data = [
// 'id' => 1,
// 'requisition_item_id' => 9,
// 'return_qty' => 1,
// 'apply_remark' => '申请备注22',
// 'attachment' => [
// [
// 'original_name' => '附件1.jpg',
// 'file_path' => '/uploads/2023/07/01/attachment1.jpg'
// ],
// [
// 'original_name' => '附件2.jpg',
// 'file_path' => '/uploads/2023/07/01/attachment2.jpg'
// ],
// [
// 'original_name' => '附件3.jpg',
// 'file_path' => '/uploads/2023/07/01/attachment3.jpg'
// ]
// ]
// ];
$data['return_at'] = time();
return $data;
}
/**
* 更新物品归还申请
*
* @param Request $request
* @param array $data
* @return array
*/
public function serviceUpdate(Request $request, array $data, string $scene = '')
{
try {
// 关联表增加事务处理
return Db::transaction(function () use ($request, $data) {
$data = $this->preProcess('Update', $data, $request);
$config = $this->buildQueryConfig($request, 'update');
$attachment = $data['attachment'];
unset($data['attachment']);
$this->logicUpdateBy($config, $data);
// 获取更新后的完整记录
$updated = $this->logicShow($config);
// 处理附件更新
$this->processRequisitionItemReturnAttachment($updated, $attachment, true);
return $this->postProcess('Update', $updated);
});
} catch (\Throwable $e) {
throw new BusinessException("更新物品归还申请失败:" . $e->getMessage());
}
}
/**
* 处理物品申请附件
*
* @param Model $created
* @param array $items
* @param bool $isUpdate
* @return void
*/
private function processRequisitionItemReturnAttachment(Model $created, array $attachment, bool $isUpdate = false): void
{
if ($isUpdate) {
$this->clearRequisitionItemReturnAttachment($created->id);
}
foreach ($attachment as $file) {
$attachmentData = [
'return_id' => $created->id,
'file_name' => $file['original_name'],
'file_path' => $file['file_path']
];
InvRequisitionItemReturnAttachment::create($attachmentData);
}
}
/**
* 清空物品申请附件
*
* @param int $returnId
* @return void
*/
private function clearRequisitionItemReturnAttachment(int $returnId): void
{
InvRequisitionItemReturnAttachment::where('return_id', $returnId)->delete();
}
public function returnConfirm(Request $request, array $data, string $scene = '')
{
try {
// 关联表增加事务处理
return Db::transaction(function () use ($request, $data) {
$data = $this->preProcess('Update', $data, $request);
$config = $this->buildQueryConfig($request, 'update');
$attachment = $data['attachment'];
unset($data['attachment']);
$this->logicUpdateBy($config, $data);
// 获取更新后的完整记录
$updated = $this->logicShow($config);
// 处理附件更新
$this->processRequisitionItemReturnAttachment($updated, $attachment, true);
return $this->postProcess('Update', $updated);
});
} catch (\Throwable $e) {
throw new BusinessException("确认归还物品失败:" . $e->getMessage());
}
}
}
@@ -0,0 +1,31 @@
<?php
namespace app\common\service;
use app\common\validate\InvRequisitionItemValidate;
use app\common\traits\crud\CrudServiceTrait;
use app\common\model\InvRequisitionItem;
class InvRequisitionItemService
{
use CrudServiceTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = InvRequisitionItem::class;
/**
* 验证器
*/
protected $validator = InvRequisitionItemValidate::class;
/**
* 获取验证器
*/
public function getValidator()
{
return new $this->validator;
}
}
@@ -0,0 +1,208 @@
<?php
namespace app\common\service;
use app\common\validate\InvRequisitionValidate;
use app\common\traits\crud\CrudServiceTrait;
use app\common\model\InvRequisition;
use app\common\model\InvRequisitionAttachment;
use app\common\model\InvRequisitionItem;
use Illuminate\Database\Eloquent\Model;
use support\Request;
use support\Db;
use support\exception\BusinessException;
class InvRequisitionService
{
use CrudServiceTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = InvRequisition::class;
/**
* 验证器
*/
protected $validator = InvRequisitionValidate::class;
/**
* 获取验证器
*/
public function getValidator()
{
return new $this->validator;
}
/**
* 创建物品申请 前置
*
* @param array $data
* @param Request $request
* @return array
*/
public function beforeServiceStore($data, Request $request): array
{
$user = (new UserService())->getUserInfo($request->user->uid, '');
$data['order_no'] = $this->generateOrderNo();
$data['applicant_id'] = $user['user']['id'];
$data['applicant_name'] = $user['user']['name'];
$data['leader_id'] = $user['user']['report_to'];
$data['status'] = InvRequisition::STATUS_LEADER;
return $data;
}
/**
* 处理物品申请
*
* @param Request $request
* @param array $data
* @return array
*/
public function serviceStore(Request $request, array $data, string $scene = '')
{
try {
// 关联表增加事务处理
return Db::transaction(function () use ($request, $data) {
$data = $this->preProcess('Store', $data, $request);
$dl = $this->getDataLimitFromRequest($request);
$created = $this->logicCreate($data, $dl);
// 处理物品申请项
$this->processRequisitionItems($created, $data['items']);
// 处理物品申请附件
$this->processRequisitionAttachmentItems($created, $data['attachment']);
return $this->postProcess('Store', $created);
});
} catch (\Throwable $e) {
throw new BusinessException("创建物品申请失败:" . $e->getMessage());
}
}
/**
* 更新物品申请
*
* @param Request $request
* @param array $data
* @return array
*/
public function serviceUpdate(Request $request, array $data, string $scene = '')
{
try {
// 关联表增加事务处理
return Db::transaction(function () use ($request, $data) {
$data = $this->preProcess('Update', $data, $request);
$config = $this->buildQueryConfig($request, 'update');
// 获取申请项/附件
$items = $data['items'];
$attachment = $data['attachment'];
unset($data['items']);
unset($data['attachment']);
// 执行更新操作
$this->logicUpdateBy($config, $data);
// 获取更新后的完整记录
$updated = $this->logicShow($config);
// 重新创建新的申请项
$this->processRequisitionItems((object)$updated, $items, true);
// 重新创建新的附件
$this->processRequisitionAttachmentItems((object)$updated, $attachment, true);
return $this->postProcess('Update', $updated);
});
} catch (\Throwable $e) {
throw new BusinessException("更新物品申请失败:" . $e->getMessage()."|".$e->getLine()."|".$e->getFile());
}
}
/**
* 处理物品申请项
*
* @param Model $created
* @param array $items
* @return void
*/
private function processRequisitionItems(Model $created, array $items, bool $isUpdate = false): void
{
if ($isUpdate) {
$this->clearRequisitionItems($created->id);
}
foreach ($items as $item) {
$itemData = [
'requisition_id' => $created->id,
'item_id' => $item['item_id'],
'item_name_snap' => $item['item_name_snap'], // 修改:使用 snap 字段
'spec_model_snap' => $item['spec_model_snap'], // 修改:使用 snap 字段
'unit_snap' => $item['unit_snap'], // 修改:使用 snap 字段
'is_consumable_snap' => $item['is_consumable_snap'], // 修改:使用 snap 字段
'request_qty' => (int) $item['request_qty'] ?? 1, // 动态获取数量,转为整型
'approved_qty' => (int) $item['request_qty'] ?? 1, // 初始审批数量等于申请数量
'remark' => $item['remark'] ?? ''
];
InvRequisitionItem::create($itemData);
}
}
/**
* 处理物品申请项
*
* @param Model $created
* @param array $items
* @return void
*/
private function processRequisitionAttachmentItems(Model $created, array $attachment, bool $isUpdate = false): void
{
if ($isUpdate) {
$this->clearRequisitionAttachmentItems($created->id);
}
foreach ($attachment as $file) {
$attachmentData = [
'requisition_id' => $created->id,
'file_name' => $file['original_name'],
'file_path' => $file['file_path']
];
InvRequisitionAttachment::create($attachmentData);
}
}
/**
* 删除申请项
*
* @param int $requisitionId
* @return void
*/
public function clearRequisitionItems(int $requisitionId): void
{
InvRequisitionItem::where('requisition_id', $requisitionId)->delete();
}
/**
* 删除申请项
*
* @param int $requisitionId
* @return void
*/
public function clearRequisitionAttachmentItems(int $requisitionId): void
{
InvRequisitionAttachment::where('requisition_id', $requisitionId)->delete();
}
/**
* 生成申请单号
*
* @return string
*/
private function generateOrderNo(): string
{
return 'RQ' . date('Ymd') . strtoupper(substr(uniqid(), -6));
}
}
+29
View File
@@ -0,0 +1,29 @@
<?php
namespace app\common\validate;
use think\Validate;
class InvItemValidate extends Validate
{
// 规则
protected $rule = [
];
// 提示
protected $message = [
];
// 场景
protected $scene = [
'store' => [
],
'update' => [
]
];
}
@@ -0,0 +1,29 @@
<?php
namespace app\common\validate;
use think\Validate;
class InvRequisitionAttachmentValidate extends Validate
{
// 规则
protected $rule = [
];
// 提示
protected $message = [
];
// 场景
protected $scene = [
'store' => [
],
'update' => [
]
];
}
@@ -0,0 +1,29 @@
<?php
namespace app\common\validate;
use think\Validate;
class InvRequisitionItemReturnAttachmentValidate extends Validate
{
// 规则
protected $rule = [
];
// 提示
protected $message = [
];
// 场景
protected $scene = [
'store' => [
],
'update' => [
]
];
}
@@ -0,0 +1,29 @@
<?php
namespace app\common\validate;
use think\Validate;
class InvRequisitionItemReturnValidate extends Validate
{
// 规则
protected $rule = [
];
// 提示
protected $message = [
];
// 场景
protected $scene = [
'store' => [
],
'update' => [
]
];
}
@@ -0,0 +1,29 @@
<?php
namespace app\common\validate;
use think\Validate;
class InvRequisitionItemValidate extends Validate
{
// 规则
protected $rule = [
];
// 提示
protected $message = [
];
// 场景
protected $scene = [
'store' => [
],
'update' => [
]
];
}
@@ -0,0 +1,55 @@
<?php
namespace app\common\validate;
use think\Validate;
class InvRequisitionValidate extends Validate
{
// 规则
protected $rule = [
'applicant_id' => 'number',
'borrow_reason' => 'require|max:255',
'use_start_date' => 'require|date',
'use_end_date' => 'date',
'items' => 'require|array',
'items.*.item_id' => 'require|number',
'items.*.request_qty' => 'require|number|gt:0',
];
// 提示
protected $message = [
'applicant_id.number' => '申请人格式错误',
'borrow_reason.require' => '领用事由不能为空',
'borrow_reason.max' => '领用事由最多不能超过255个字符',
'use_start_date.require' => '使用开始日期不能为空',
'use_start_date.date' => '使用开始日期格式错误',
'use_end_date.date' => '预计归还日期格式错误',
'items.require' => '申请物品明细不能为空',
'items.array' => '申请物品明细格式错误',
'items.*.item_id.require' => '物品ID不能为空',
'items.*.item_id.number' => '物品ID格式错误',
'items.*.request_qty.require' => '申请数量不能为空',
'items.*.request_qty.number' => '申请数量格式错误',
'items.*.request_qty.gt' => '申请数量必须大于0',
];
// 场景
protected $scene = [
'store' => [
],
'update' => [
],
'apply' => [
'applicant_id',
'borrow_reason',
'use_start_date',
'use_end_date',
'items'
]
];
}