multi-store/app/admin/logic/inventory_transfer/InventoryTransferLogic.php
2025-01-11 16:15:52 +08:00

175 lines
7.5 KiB
PHP

<?php
namespace app\admin\logic\inventory_transfer;
use app\admin\logic\warehouse_product\WarehouseProductLogic;
use app\common\model\inventory_transfer\InventoryTransfer;
use app\common\logic\BaseLogic;
use app\common\model\store_branch_product\StoreBranchProduct;
use app\common\model\store_product\StoreProduct;
use app\common\model\warehouse_product_storege\WarehouseProductStorege;
use support\exception\BusinessException;
use think\facade\Db;
/**
* 商品调拨逻辑
* Class InventoryTransferLogic
* @package app\admin\logic\inventory_transfer
*/
class InventoryTransferLogic extends BaseLogic
{
/**
* @notes 添加商品调拨
* @param array $params
* @return bool
* @author admin
* @date 2024/08/13 16:18
*/
public static function add(array $params,$admin_id=0): bool
{
if (empty($params['product_arr'])) {
throw new BusinessException('请选择商品');
}
$productIds = array_column($params['product_arr'], 'product_id');
if ($params['one_type'] == 1) {
$outProducts = StoreBranchProduct::whereIn('product_id', $productIds)->where('store_id', $params['one_id'])->field('id,product_id,stock')->select()->toArray();
} else {
$outProducts = WarehouseProductStorege::whereIn('product_id', $productIds)->where('warehouse_id', $params['one_id'])->field('id,product_id,nums stock')->select()->toArray();
}
$outProducts = reset_index($outProducts, 'product_id');
if ($params['two_type'] == 1) {
$inProducts = StoreBranchProduct::whereIn('product_id', $productIds)->where('store_id', $params['two_id'])->field('id,product_id,stock')->select()->toArray();
} else {
$inProducts = WarehouseProductStorege::whereIn('product_id', $productIds)->where('warehouse_id', $params['two_id'])->field('id,product_id,nums stock')->select()->toArray();
}
$inProducts = reset_index($inProducts, 'product_id');
$insert = [];
foreach ($params['product_arr'] as $v) {
$outProduct = !empty($outProducts[$v['product_id']]) ? $outProducts[$v['product_id']] : ['stock' => 0, 'id' => 0, 'product_id' => $v['product_id']];
$inProduct = !empty($inProducts[$v['product_id']]) ? $inProducts[$v['product_id']] : ['stock' => 0, 'id' => 0, 'product_id' => $v['product_id']];
if ($outProduct['stock'] < $v['nums']) {
throw new BusinessException("出库商品 {$outProduct['product_id']} 调拨数量不能大于当前仓库库存");
}
$insert[] = [
'product_id' => $v['product_id'],
'nums' => $v['nums'],
'one_before_nums' => $outProduct['stock'],
'one_after_nums' => bcsub($outProduct['stock'], $v['nums']),
'two_before_nums' => $inProduct['stock'],
'two_after_nums' => bcadd($inProduct['stock'], $v['nums']),
'one_type' => $params['one_type'],
'two_type' => $params['two_type'],
'one_id' => $params['one_id'],
'two_id' => $params['two_id'],
'create_time' => time(),
];
}
Db::startTrans();
try {
InventoryTransfer::insertAll($insert);
foreach ($insert as $v) {
if($params['one_type']==1){
$find=StoreBranchProduct::where('product_id', $v['product_id'])->where('store_id', $params['one_id'])->find();
$find->save(['stock' =>bcsub( $find['stock'],$v['nums'],2)]);
SqlChannelLog('StoreBranchProduct', $find['id'], $v['nums'], -1, Request()->url(),$admin_id);
} elseif ($params['one_type'] == 2) {
$find=WarehouseProductStorege::where('product_id', $v['product_id'])->where('warehouse_id', $params['one_id'])->find();
$find->save(['nums' =>bcsub( $find['nums'],$v['nums'],2)]);
SqlChannelLog('WarehouseProductStorege', $find['id'], $v['nums'], -1, Request()->url(),$admin_id);
}
if($params['two_type']==1){
$find=StoreBranchProduct::where('product_id', $v['product_id'])->where('store_id', $params['two_id'])->find();
if (empty($find)) {
$storeProduct = StoreProduct::field('top_cate_id,two_cate_id,cate_id,store_name,image,price,vip_price,cost,purchase,keyword,bar_code,store_info,rose,product_type,unit,batch,store_batch,label_id,is_lack,manufacturer_information')->where('id', $v['product_id'])->find()->toArray();
$find = new StoreBranchProduct();
$find->product_id = $v['product_id'];
$find->store_id = $params['two_id'];
$find->stock = $v['nums'];
$find->setAttrs($storeProduct);
$find->save();
} else {
$find->save(['stock' =>bcadd( $find['stock'],$v['nums'],2)]);
}
SqlChannelLog('StoreBranchProduct', $find['id'], $v['nums'], 1, Request()->url(),$admin_id);
} elseif ($params['two_type'] == 2) {
$find=WarehouseProductStorege::where('product_id', $v['product_id'])->where('warehouse_id', $params['two_id'])->find();
if (empty($find)) {
$find = new WarehouseProductStorege();
$find->warehouse_id = $params['two_id'];
$find->product_id = $v['product_id'];
$find->nums = $v['nums'];
$find->save();
} else {
$find->save(['nums' =>bcadd( $find['nums'],$v['nums'],2)]);
}
SqlChannelLog('WarehouseProductStorege', $find['id'], $v['nums'], 1, Request()->url(),$admin_id);
}
}
Db::commit();
return true;
} catch (\Throwable $e) {
Db::rollback();
throw new BusinessException($e->getMessage());
}
}
/**
* @notes 编辑商品调拨
* @param array $params
* @return bool
* @author admin
* @date 2024/08/13 16:18
*/
public static function edit(array $params): bool
{
Db::startTrans();
try {
InventoryTransfer::where('id', $params['id'])->update([
'product_id' => $params['product_id'],
'nums' => $params['nums'],
'before_nums' => $params['before_nums'],
'after_nums' => $params['after_nums'],
'type' => $params['type'],
'one_id' => $params['one_id'],
'two_id' => $params['two_id']
]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
throw new BusinessException($e->getMessage());
}
}
/**
* @notes 删除商品调拨
* @param array $params
* @return bool
* @author admin
* @date 2024/08/13 16:18
*/
public static function delete(array $params): bool
{
return InventoryTransfer::destroy($params['id']);
}
/**
* @notes 获取商品调拨详情
* @param $params
* @return array
* @author admin
* @date 2024/08/13 16:18
*/
public static function detail($params): array
{
return InventoryTransfer::findOrEmpty($params['id'])->toArray();
}
}