修改商品出入库溯源

This commit is contained in:
lewis 2025-03-13 17:49:03 +08:00
parent ad25d6c599
commit 0ad56182cf
7 changed files with 198 additions and 83 deletions

View File

@ -20,7 +20,6 @@ class ProductSourceLinkInfoController extends BaseAdminController
/** /**
* @notes 获取商品溯源详细列表 * @notes 获取商品溯源详细列表
* @return \think\response\Json
* @author admin * @author admin
* @date 2025/03/12 10:08 * @date 2025/03/12 10:08
*/ */
@ -32,7 +31,6 @@ class ProductSourceLinkInfoController extends BaseAdminController
/** /**
* @notes 添加商品溯源详细 * @notes 添加商品溯源详细
* @return \think\response\Json
* @author admin * @author admin
* @date 2025/03/12 10:08 * @date 2025/03/12 10:08
*/ */
@ -49,7 +47,6 @@ class ProductSourceLinkInfoController extends BaseAdminController
/** /**
* @notes 编辑商品溯源详细 * @notes 编辑商品溯源详细
* @return \think\response\Json
* @author admin * @author admin
* @date 2025/03/12 10:08 * @date 2025/03/12 10:08
*/ */
@ -66,7 +63,6 @@ class ProductSourceLinkInfoController extends BaseAdminController
/** /**
* @notes 删除商品溯源详细 * @notes 删除商品溯源详细
* @return \think\response\Json
* @author admin * @author admin
* @date 2025/03/12 10:08 * @date 2025/03/12 10:08
*/ */
@ -80,7 +76,6 @@ class ProductSourceLinkInfoController extends BaseAdminController
/** /**
* @notes 获取商品溯源详细详情 * @notes 获取商品溯源详细详情
* @return \think\response\Json
* @author admin * @author admin
* @date 2025/03/12 10:08 * @date 2025/03/12 10:08
*/ */

View File

@ -26,7 +26,7 @@ class ProductSourceLinkInfoLists extends BaseAdminDataLists implements ListsSear
public function setSearch(): array public function setSearch(): array
{ {
return [ return [
'=' => ['oid']
]; ];
} }

View File

@ -42,6 +42,7 @@ class ProductSourceLinkLogic extends BaseLogic
'oid' => $model['id'], 'oid' => $model['id'],
'product_id' => $offer['product_id'], 'product_id' => $offer['product_id'],
'nums' => $offer['buyer_nums'], 'nums' => $offer['buyer_nums'],
'current_nums' => $offer['buyer_nums'],
'types' => $info['types'], 'types' => $info['types'],
'link_id' => $info['link_id'], 'link_id' => $info['link_id'],
'price' => $offer['price'], 'price' => $offer['price'],
@ -102,4 +103,74 @@ class ProductSourceLinkLogic extends BaseLogic
{ {
return ProductSourceLink::findOrEmpty($params['id'])->toArray(); return ProductSourceLink::findOrEmpty($params['id'])->toArray();
} }
/**
* 出库
* @param array $info
* @return true
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function outbound(array $info)
{
$productSourceLinkInfo = ProductSourceLink::alias('t1')
->field('t2.id,t2.product_id,oid,price,current_nums')
->join('product_source_link_info t2', 't1.id = t2.oid')
->where('t1.product_id', $info['product']['product_id'])
->where('types', ProductSourceLinkInfo::TypeIn)
->where('current_nums', '>', 0)
->select()->toArray();
$update = [];
$insert = [];
$needNum = $info['product']['nums'];
foreach ($productSourceLinkInfo as $item) {
$currentNum = max(bcsub($item['current_nums'], $needNum, 2), 0);
if ($needNum - $item['current_nums'] <= 0) {
[$update, $insert] = self::getInsertAndUpdate($update, $insert, $currentNum, $item, $info, $needNum);
break;
} else {
[$update, $insert] = self::getInsertAndUpdate($update, $insert, 0, $item, $info, $item['current_nums']);
}
$needNum = $needNum - $item['current_nums'];
}
(new ProductSourceLinkInfo())->saveAll($update);
(new ProductSourceLinkInfo())->insertAll($insert);
return true;
}
public static function getInsertAndUpdate($update, $insert, $currentNum, $item, $info, $needNum)
{
$time = time();
$update[] = [
'id' => $item['id'],
'current_nums' => $currentNum,
'update_time' => $time,
];
$exist = ProductSourceLinkInfo::field('id,from_id,nums')->where('link_id', $info['link_id'])->where('types', ProductSourceLinkInfo::TypeOut)->findOrEmpty()->toArray();
if (!empty($exist) && $exist['from_id'] == $item['id']) {
$itemNums = bcadd($exist['nums'], $needNum, 2);
$update[] = [
'id' => $exist['id'],
'nums' => $itemNums,
'total_price' => bcmul($item['price'], $itemNums, 2),
'update_time' => $time,
];
} else {
$insert[] = [
'product_id' => $item['product_id'],
'oid' => $item['oid'],
'types' => ProductSourceLinkInfo::TypeOut,
'link_id' => $info['link_id'],
'from_id' => $item['id'],
'nums' => $needNum,
'price' => $item['price'],
'total_price' => bcmul($item['price'], $needNum, 2),
'create_time' => $time,
'update_time' => $time,
];
}
return [$update, $insert];
}
} }

View File

@ -3,6 +3,8 @@
namespace app\admin\logic\product_source_link_info; namespace app\admin\logic\product_source_link_info;
use app\admin\logic\product_source_link\ProductSourceLinkLogic;
use app\common\model\product_source_link\ProductSourceLink;
use app\common\model\product_source_link_info\ProductSourceLinkInfo; use app\common\model\product_source_link_info\ProductSourceLinkInfo;
use app\common\logic\BaseLogic; use app\common\logic\BaseLogic;
use support\exception\BusinessException; use support\exception\BusinessException;
@ -89,15 +91,87 @@ class ProductSourceLinkInfoLogic extends BaseLogic
public static function updateByLinkId($linkId, $types, array $params) public static function updateByLinkId($linkId, $types, array $params)
{ {
ProductSourceLinkInfo::where('link_id', $linkId)->where('types', $types)->update($params); $data = ProductSourceLinkInfo::where('link_id', $linkId)->where('types', $types)->findOrEmpty()->toArray();
if ($types == ProductSourceLinkInfo::TypeIn && isset($params['add_nums'])) {
$params['current_nums'] = bcadd($data['current_nums'], $params['add_nums'], 2);
unset($params['add_nums']);
$params['total_price'] = bcmul($params['nums'], $data['price'], 2);
ProductSourceLinkInfo::where('id', $data['id'])->update($params);
} elseif ($types == ProductSourceLinkInfo::TypeOut && isset($params['add_nums'])) {
if ($params['add_nums'] < 0) {
$otherData = ProductSourceLinkInfo::where('id', '<>', $data['id'])->where('link_id', $linkId)->where('types', $types)->order('id desc')->select()->toArray();
if (!empty($otherData)) {
$rollbackNum = abs($params['add_nums']);
$update = [];
foreach ($otherData as $item) {
if ($item['nums'] > $rollbackNum) {
$update = self::setUpdate($item, $rollbackNum, $update);
break;
} else {
$update = self::setUpdate($item, $item['nums'], $update);
}
$rollbackNum = bcsub($rollbackNum, $item['nums'], 2);
}
if ($rollbackNum > 0) {
$update = self::setUpdate($data, $rollbackNum, $update);
}
(new ProductSourceLinkInfo())->saveAll($update);
}
} else {
$warehouseId = ProductSourceLink::where('id', $data['oid'])->value('warehouse_id');
ProductSourceLinkLogic::outbound([
'product' => [
'product_id' => $data['product_id'],
'nums' => $params['add_nums'],
],
'warehouse_id' => $warehouseId,
'link_id' => $data['link_id'],
]);
}
}
} }
public static function deleteByLinkId($linkId, $types) public static function deleteByLinkId($linkId, $types)
{ {
$data = ProductSourceLinkInfo::where('link_id', $linkId)->where('types', $types)->find(); $list = ProductSourceLinkInfo::where('link_id', $linkId)->where('types', $types)->select()->toArray();
if (!empty($data)) { $update = [];
$data->delete(); foreach ($list as $item) {
if ($types == ProductSourceLinkInfo::TypeOut && $item['from_id'] > 0) {
$update[] = [
'id' => $item['from_id'],
'current_nums' => Db::raw("current_nums+{$item['nums']}"),
];
} }
$update[] = [
'id' => $item['id'],
'delete_time' => time(),
];
}
(new ProductSourceLinkInfo())->saveAll($update);
}
/**
* @param array $data
* @param float|int|string $rollbackNum
* @param array $update
* @return array
*/
public static function setUpdate(array $data, float|int|string $rollbackNum, array $update): array
{
$dataNums = bcsub($data['nums'], $rollbackNum, 2);
$update[] = [
'id' => $data['id'],
'nums' => $dataNums,
'total_price' => bcmul($data['price'], $dataNums, 2),
'delete_time' => $dataNums > 0 ? null : time(),
];
if ($data['from_id'] > 0) {
$update[] = [
'id' => $data['from_id'],
'current_nums' => Db::raw("current_nums+{$rollbackNum}"),
];
}
return $update;
} }
} }

View File

@ -2,7 +2,9 @@
namespace app\admin\logic\store_branch_product; namespace app\admin\logic\store_branch_product;
use app\admin\logic\product_source_link\ProductSourceLinkLogic;
use app\admin\logic\store_product\StoreProductLogic; use app\admin\logic\store_product\StoreProductLogic;
use app\common\model\product_source_link_info\ProductSourceLinkInfo;
use app\common\model\store_branch_product\StoreBranchProduct; use app\common\model\store_branch_product\StoreBranchProduct;
use app\common\logic\BaseLogic; use app\common\logic\BaseLogic;
use app\common\model\store_product\StoreProduct; use app\common\model\store_product\StoreProduct;
@ -141,6 +143,13 @@ class StoreBranchProductLogic extends BaseLogic
], ['product_id' => $productId,'store_id'=>$storeId]); ], ['product_id' => $productId,'store_id'=>$storeId]);
SqlChannelLog('StoreBranchProduct',$branchProduct['id'], $num, -1, Request()->url()); SqlChannelLog('StoreBranchProduct',$branchProduct['id'], $num, -1, Request()->url());
} }
ProductSourceLinkLogic::add([
'purchase_product_offer' => [$params],
'types' => ProductSourceLinkInfo::TypeIn,
'buyer_id' => $params['buyer_id'],
'warehouse_id' => $params['warehouse_id'],
'link_id' => $res['id'],
]);
} }
} }

View File

@ -197,6 +197,12 @@ class WarehouseProductLogic extends BaseLogic
$res = WarehouseProduct::create($data); $res = WarehouseProduct::create($data);
SqlChannelLog('WarehouseProduct', $res->id, $params['nums'], $params['financial_pm'] == 1 ? 1 : -1, Request()->url(),$admin_id); SqlChannelLog('WarehouseProduct', $res->id, $params['nums'], $params['financial_pm'] == 1 ? 1 : -1, Request()->url(),$admin_id);
ProductSourceLinkLogic::outbound([
'product' => $data,
'warehouse_id' => $params['warehouse_id'],
'link_id' => $res['id'],
]);
Db::commit(); Db::commit();
return $res; return $res;
} catch (\Throwable $e) { } catch (\Throwable $e) {
@ -219,23 +225,21 @@ class WarehouseProductLogic extends BaseLogic
$find = WarehouseOrder::where('id', $params['oid'])->find(); $find = WarehouseOrder::where('id', $params['oid'])->find();
if ($find) { if ($find) {
$res = WarehouseProduct::where('id', $params['id'])->withTrashed()->find(); $res = WarehouseProduct::where('id', $params['id'])->withTrashed()->find();
if($params['nums']>$res['nums']){ $updateNums = bcsub($params['nums'], $res['nums'],2);
$nums=bcsub($params['nums'], $res['nums'],2); if ($updateNums != 0) {
if ($res['financial_pm'] == 0) { $storageNum = $res['financial_pm'] == 0 ? -$updateNums : $updateNums;
self::decWarehouseProduct($res, $nums); $productNum = $updateNums;
self::updateWarehouseProduct($res, $storageNum, $productNum);
$ProductSourceLinkInfoParams = ['nums' => $params['nums'], 'total_price' => $params['total_price']];
if ($find['financial_pm'] == 1) {
$ProductSourceLinkInfoParams['price'] = $params['purchase'];
$ProductSourceLinkInfoParams['add_nums'] = $updateNums;
ProductSourceLinkInfoLogic::updateByLinkId($res['id'], ProductSourceLinkInfo::TypeIn, $ProductSourceLinkInfoParams);
} else { } else {
self::incWarehouseProduct($res, $nums); $ProductSourceLinkInfoParams['origin_nums'] = $res['nums'];
} $ProductSourceLinkInfoParams['add_nums'] = bcsub($params['nums'], $res['nums'],2);
}else{ ProductSourceLinkInfoLogic::updateByLinkId($res['id'], ProductSourceLinkInfo::TypeOut, $ProductSourceLinkInfoParams);
if($params['nums']==0){
self::decWarehouseProduct($res, $res['nums']);
}else{
$nums = bcsub($res['nums'], $params['nums'], 2);
if ($res['financial_pm'] == 0) {
self::incWarehouseProduct($res, $nums);
} else {
self::decWarehouseProduct($res, $nums);
}
} }
} }
$datas = [ $datas = [
@ -255,14 +259,6 @@ class WarehouseProductLogic extends BaseLogic
$datas['expiration_date'] = strtotime($params['expiration_date']); $datas['expiration_date'] = strtotime($params['expiration_date']);
} }
$res->save($datas); $res->save($datas);
$ProductSourceLinkInfoParams = ['nums' => $params['nums'], 'total_price' => $params['total_price']];
if ($find['financial_pm'] == 1) {
$ProductSourceLinkInfoParams['price'] = $params['purchase'];
ProductSourceLinkInfoLogic::updateByLinkId($res['id'], ProductSourceLinkInfo::TypeIn, $ProductSourceLinkInfoParams);
} else {
$ProductSourceLinkInfoParams['price'] = $params['price'];
ProductSourceLinkInfoLogic::updateByLinkId($res['id'], ProductSourceLinkInfo::TypeOut, $ProductSourceLinkInfoParams);
}
} }
Db::commit(); Db::commit();
return $res; return $res;
@ -285,11 +281,9 @@ class WarehouseProductLogic extends BaseLogic
if ($res) { if ($res) {
Db::startTrans(); Db::startTrans();
try { try {
if ($res['financial_pm'] == 1) { $storageNum = $res['financial_pm'] == 1 ? -$res['nums'] : $res['nums'];
self::decProductIncStorege($res, $res['nums'], $admin_id); $productNum = -$res['nums'];
} elseif ($res['financial_pm'] == 0) { self::updateWarehouseProduct($res, $storageNum, $productNum);
self::incProductDecStorege($res, $res['nums'], $admin_id);
}
$res->delete(); $res->delete();
$types = $res['financial_pm'] == 1 ? ProductSourceLinkInfo::TypeIn : ProductSourceLinkInfo::TypeOut; $types = $res['financial_pm'] == 1 ? ProductSourceLinkInfo::TypeIn : ProductSourceLinkInfo::TypeOut;
ProductSourceLinkInfoLogic::deleteByLinkId($res['id'], $types); ProductSourceLinkInfoLogic::deleteByLinkId($res['id'], $types);
@ -471,20 +465,20 @@ class WarehouseProductLogic extends BaseLogic
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
*/ */
public static function incWarehouseProduct($warehouseProduct, $nums) public static function updateWarehouseProduct($warehouseProduct, $storageNum, $productNum)
{ {
$warehouseProductStorage = WarehouseProductStorege::where('warehouse_id', $warehouseProduct['warehouse_id']) $warehouseProductStorage = WarehouseProductStorege::where('warehouse_id', $warehouseProduct['warehouse_id'])
->where('product_id', $warehouseProduct['product_id'])->find(); ->where('product_id', $warehouseProduct['product_id'])->find();
$warehouseProductStorage->nums = bcadd($warehouseProductStorage->nums, $nums, 2); $warehouseProductStorage->nums = bcadd($warehouseProductStorage->nums, $storageNum, 2);
$warehouseProductStorage->save(); $warehouseProductStorage->save();
SqlChannelLog('WarehouseProductStorege', $warehouseProductStorage['id'], $nums, 1, Request()->url()); SqlChannelLog('WarehouseProductStorege', $warehouseProductStorage['id'], $storageNum, $storageNum > 0 ? 1 : -1, Request()->url());
$update = [ $update = [
'nums' => bcadd($warehouseProduct['nums'], $nums, 2), 'nums' => bcadd($warehouseProduct['nums'], $productNum, 2),
'total_price' => bcadd($warehouseProduct['total_price'], bcmul($nums, $warehouseProduct['price'], 2), 2), 'total_price' => bcadd($warehouseProduct['total_price'], bcmul($productNum, $warehouseProduct['price'], 2), 2),
]; ];
WarehouseProduct::where('id',$warehouseProduct['id'])->update($update); WarehouseProduct::where('id',$warehouseProduct['id'])->update($update);
SqlChannelLog('WarehouseProduct', $warehouseProduct['id'], $nums, 1, Request()->url()); SqlChannelLog('WarehouseProduct', $warehouseProduct['id'], $productNum, $productNum > 0 ? 1 : -1, Request()->url());
$find = WarehouseProduct::where('oid', $warehouseProduct['oid'])->field('sum(nums) as nums,sum(total_price) as total_price')->find(); $find = WarehouseProduct::where('oid', $warehouseProduct['oid'])->field('sum(nums) as nums,sum(total_price) as total_price')->find();
if ($find) { if ($find) {
@ -493,41 +487,7 @@ class WarehouseProductLogic extends BaseLogic
'total_price' => $find['total_price'] 'total_price' => $find['total_price']
]); ]);
} }
self::updateStoreStorage2($warehouseProduct, $nums); self::updateStoreStorage2($warehouseProduct, $productNum);
}
/**
* 减少商品入库数量
* @param $warehouseProduct
* @param $nums
* @return void
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function decWarehouseProduct($warehouseProduct, $nums)
{
$warehouseProductStorage = WarehouseProductStorege::where('warehouse_id', $warehouseProduct['warehouse_id'])
->where('product_id', $warehouseProduct['product_id'])->find();
$warehouseProductStorage->nums = bcsub($warehouseProductStorage->nums, $nums, 2);
$warehouseProductStorage->save();
SqlChannelLog('WarehouseProductStorege', $warehouseProductStorage['id'], $nums, -1, Request()->url());
$update = [
'nums' => bcsub($warehouseProduct['nums'], $nums, 2),
'total_price' => bcsub($warehouseProduct['total_price'], bcmul($nums, $warehouseProduct['price'], 2), 2),
];
WarehouseProduct::where('id',$warehouseProduct['id'])->update($update);
SqlChannelLog('WarehouseProduct', $warehouseProduct['id'], $nums, -1, Request()->url());
$find = WarehouseProduct::where('oid', $warehouseProduct['oid'])->field('sum(nums) as nums,sum(total_price) as total_price')->find();
if ($find) {
WarehouseOrder::where('id', $warehouseProduct['oid'])->update([
'nums' => $find['nums'],
'total_price' => $find['total_price']
]);
}
self::updateStoreStorage2($warehouseProduct, $nums);
} }
/** /**

View File

@ -23,14 +23,20 @@ class ProductSourceLinkInfo extends BaseModel
const TypeOut = 2; const TypeOut = 2;
const TypeStoreIn = 3; const TypeStoreIn = 3;
const TypeOrder = 4; const TypeOrder = 4;
const TypeOrderRefund = 5;
const TypeTransferToWarehouse = 6;
const TypeTransferToStore = 7;
public function getTypeName() public function getTypeName()
{ {
$typeMap = [ $typeMap = [
self::TypeIn => '入库', self::TypeIn => '仓库入库',
self::TypeOut => '出库', self::TypeOut => '仓库出库',
self::TypeStoreIn => '门店入库', self::TypeStoreIn => '门店入库',
self::TypeOrder => '订单', self::TypeOrder => '订单出库',
self::TypeOrderRefund => '订单退货入库',
self::TypeTransferToWarehouse => '门店调拨至仓库',
self::TypeTransferToStore => '门店调拨至门店',
]; ];
return $typeMap[$this->getAttr('types')]; return $typeMap[$this->getAttr('types')];
} }