mkm 8567f2de4a fix(inventory_store): 修复库存盘点逻辑
- 引入 Raw 类以处理复杂的数据库查询
- 使用 Raw 实例解决 nums 和 enter_nums 直接比较的问题
2025-02-14 18:00:33 +08:00

108 lines
3.0 KiB
PHP

<?php
namespace app\admin\logic\inventory_warehouse;
use app\common\model\inventory_warehouse\InventoryWarehouse;
use app\common\logic\BaseLogic;
use app\common\model\warehouse_product_storege\WarehouseProductStorege;
use support\exception\BusinessException;
use think\db\Raw;
use think\facade\Db;
/**
* 仓库盘存逻辑
* Class InventoryWarehouseLogic
* @package app\admin\logic\inventory_warehouse
*/
class InventoryWarehouseLogic extends BaseLogic
{
/**
* @notes 添加仓库盘存
* @param array $params
* @return bool
* @author admin
* @date 2025/02/14 17:24
*/
public static function add(array $params): bool
{
Db::startTrans();
try {
$find=InventoryWarehouse::where('warehouse_id', 1)->whereDay('create_time')->find();
if($find){
throw new BusinessException('今日数据已生成');
}
$arr=WarehouseProductStorege::where('warehouse_id',1)->field('product_id,nums,warehouse_id')->select()->toArray();
(new InventoryWarehouse())->saveAll($arr);
Db::commit();
return true;
} catch (\Throwable $e) {
Db::rollback();
throw new BusinessException($e->getMessage());
}
}
/**
* @notes 编辑仓库盘存
* @param array $params
* @return bool
* @author admin
* @date 2025/02/14 17:24
*/
public static function edit(array $params): bool
{
Db::startTrans();
try {
InventoryWarehouse::where('id', $params['id'])->update([
'admin_id'=>$params['admin_id']??0,
'enter_nums'=>$params['nums']
]);
Db::commit();
return true;
} catch (\Throwable $e) {
Db::rollback();
throw new BusinessException($e->getMessage());
}
}
/**
* @notes 删除仓库盘存
* @param array $params
* @return bool
* @author admin
* @date 2025/02/14 17:24
*/
public static function delete(array $params): bool
{
return InventoryWarehouse::destroy($params['id']);
}
public static function enterNums(array $params): bool
{
Db::startTrans();
try {
InventoryWarehouse::where('warehouse_id', $params['warehouse_id'])->whereDay('create_time',$params['create_time'])->update([
'status'=>2
]);
$arr=InventoryWarehouse::where('warehouse_id', $params['warehouse_id'])->where('nums','<>',new Raw('enter_nums'))->whereDay('create_time',$params['create_time'])->select();
foreach ($arr as $k=>$v){
WarehouseProductStorege::where('product_id',$v['product_id'])->where('warehouse_id',$v['warehouse_id'])->update([
'nums'=>$v['enter_nums']
]);
}
Db::commit();
return true;
} catch (\Throwable $e) {
Db::rollback();
throw new BusinessException($e->getMessage());
}
}
}