120 lines
3.9 KiB
PHP
120 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace app\common\service;
|
|
|
|
use app\common\model\InvStockLog;
|
|
use support\exception\BusinessException;
|
|
|
|
/**
|
|
* 物品库存变化记录服务
|
|
*/
|
|
class InvStockLogService
|
|
{
|
|
/**
|
|
* 记录库存变化
|
|
*
|
|
* @param array $data 日志数据
|
|
* @return InvStockLog
|
|
* @throws BusinessException
|
|
*/
|
|
public function record(array $data): InvStockLog
|
|
{
|
|
$required = ['item_id', 'change_type', 'change_qty', 'before_qty', 'after_qty'];
|
|
foreach ($required as $field) {
|
|
if (!isset($data[$field])) {
|
|
throw new BusinessException("库存日志缺少必需字段: {$field}");
|
|
}
|
|
}
|
|
|
|
return InvStockLog::create([
|
|
'company_id' => $data['company_id'] ?? 0,
|
|
'item_id' => $data['item_id'],
|
|
'item_name_snap' => $data['item_name_snap'] ?? null,
|
|
'change_type' => $data['change_type'],
|
|
'change_qty' => $data['change_qty'],
|
|
'before_qty' => $data['before_qty'],
|
|
'after_qty' => $data['after_qty'],
|
|
'biz_type' => $data['biz_type'] ?? null,
|
|
'biz_no' => $data['biz_no'] ?? null,
|
|
'operator_id' => $data['operator_id'] ?? null,
|
|
'operator_name' => $data['operator_name'] ?? null,
|
|
'remark' => $data['remark'] ?? null,
|
|
'updated_at' => time(),
|
|
'created_at' => time(),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 查询库存变化记录列表
|
|
*
|
|
* @param array $params 查询参数
|
|
* @return array
|
|
*/
|
|
public function getList(array $params): array
|
|
{
|
|
$query = InvStockLog::query()->with(['item']);
|
|
|
|
// 物品ID筛选
|
|
if (!empty($params['item_id'])) {
|
|
$query->where('item_id', $params['item_id']);
|
|
}
|
|
|
|
// 变动类型筛选
|
|
if (!empty($params['change_type'])) {
|
|
$query->where('change_type', $params['change_type']);
|
|
}
|
|
|
|
// 业务类型筛选
|
|
if (!empty($params['biz_type'])) {
|
|
$query->where('biz_type', $params['biz_type']);
|
|
}
|
|
|
|
// 单据号筛选
|
|
if (!empty($params['biz_no'])) {
|
|
$query->where('biz_no', 'like', '%' . $params['biz_no'] . '%');
|
|
}
|
|
|
|
// 时间范围筛选
|
|
if (!empty($params['start_time'])) {
|
|
$query->where('created_at', '>=', strtotime($params['start_time']));
|
|
}
|
|
if (!empty($params['end_time'])) {
|
|
$query->where('created_at', '<=', strtotime($params['end_time']));
|
|
}
|
|
|
|
// 排序
|
|
$query->orderBy('id', 'desc');
|
|
|
|
// 分页
|
|
$perPage = $params['size'] ?? 20;
|
|
$page = $params['current'] ?? 1;
|
|
|
|
$result = $query->paginate($perPage, ['*'], 'page', $page);
|
|
|
|
return [
|
|
'records' => $result->getCollection()->map(function ($log) {
|
|
return [
|
|
'id' => $log->id,
|
|
'item_id' => $log->item_id,
|
|
'item_name' => $log->item_name_snap,
|
|
'change_type' => $log->change_type,
|
|
'change_type_text' => InvStockLog::getChangeTypeText($log->change_type),
|
|
'change_qty' => $log->change_qty,
|
|
'before_qty' => $log->before_qty,
|
|
'after_qty' => $log->after_qty,
|
|
'biz_type' => $log->biz_type,
|
|
'biz_type_text' => $log->biz_type ? InvStockLog::getBizTypeText($log->biz_type) : null,
|
|
'biz_no' => $log->biz_no,
|
|
'operator_id' => $log->operator_id,
|
|
'operator_name' => $log->operator_name,
|
|
'remark' => $log->remark,
|
|
'created_at' => date('Y-m-d H:i:s', is_int($log->created_at)?$log->created_at:strtotime($log->created_at)),
|
|
];
|
|
})->toArray(),
|
|
'total' => $result->total(),
|
|
'current' => $result->currentPage(),
|
|
'size' => $result->perPage(),
|
|
];
|
|
}
|
|
}
|