调试财务溯源

This commit is contained in:
lewis 2025-03-21 17:15:38 +08:00
parent 0ddfd9c483
commit b4e8f188ab
9 changed files with 464 additions and 272 deletions

View File

@ -3,7 +3,7 @@
namespace app\admin\controller;
use app\admin\logic\beforehand_order_cart_info\BeforehandOrderCartInfoLogic;
use app\admin\logic\product_source_link\ProductSourceLinkLogic;
use app\admin\logic\product_source_link_info\ProductSourceLinkInfoLogic;
use app\common\model\beforehand_order\BeforehandOrder;
use app\common\model\beforehand_order_cart_info\BeforehandOrderCartInfo;
use app\common\model\CeshiCopy;
@ -69,13 +69,15 @@ class LocalController extends BaseAdminController
foreach ($outboundOrders as $order) {
$products = WarehouseProduct::field('id,product_id,nums,purchase,create_time')->where('oid', $order['id'])->select()->toArray();
foreach ($products as $product) {
ProductSourceLinkLogic::outbound([
'product' => ['product_id' => $product['product_id'], 'nums' => $product['nums']],
$productSourceLinkInfoLogic = new ProductSourceLinkInfoLogic();
$productSourceLinkInfoLogic->warehouseProduct = $product;
$productSourceLinkInfoLogic->setInfo([
'warehouse_id' => $order['warehouse_id'],
'store_id' => $order['store_id'],
'link_id' => $product['id'],
'create_time' => strtotime($product['create_time']),
]);
$productSourceLinkInfoLogic->outbound();
}
}
}

View File

@ -107,6 +107,12 @@ class DeliveryServiceLogic extends BaseLogic
}
}
/**
* 扣减采购人员资金
* @param $uid
* @param $amount
* @return void
*/
public static function subPurchaseFunds($uid, $amount)
{
$model = DeliveryService::where('uid', $uid)->findOrEmpty();

View File

@ -4,6 +4,7 @@ namespace app\admin\logic\inventory_transfer_order;
use app\admin\logic\product_source_link\ProductSourceLinkLogic;
use app\admin\logic\product_source_link_info\ProductSourceLinkInfoLogic;
use app\common\model\inventory_transfer_order\InventoryTransferOrder;
use app\common\logic\BaseLogic;
use app\common\model\inventory_transfer\InventoryTransfer;
@ -131,10 +132,14 @@ class InventoryTransferOrderLogic extends BaseLogic
}
SqlChannelLog('WarehouseProductStorege', $find['id'], $v['nums'], 1, Request()->url(),$admin_id);
}
ProductSourceLinkLogic::transfer([
$productSourceLinkInfoLogic = new ProductSourceLinkInfoLogic();
$productSourceLinkInfoLogic->setInfo([
'oid' => $v['oid'],
'product_id' => $v['product_id'],
]);
$productSourceLinkInfoLogic->transfer();
}
Db::commit();
return true;

View File

@ -119,202 +119,4 @@ class ProductSourceLinkLogic extends BaseLogic
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)
{
$query = ProductSourceLinkInfo::where('product_id', $info['product']['product_id'])->where('current_nums', '>', 0);
if (!empty($info['is_store_order']) && $info['store_id']) {
$query->where('store_id', $info['store_id'])->whereIn('types', [ProductSourceLinkInfo::TypeStoreIn, ProductSourceLinkInfo::TypeS2S]);
} else {
$query->whereIn('types', [ProductSourceLinkInfo::TypeIn, ProductSourceLinkInfo::TypeS2W, ProductSourceLinkInfo::TypeW2W]);
}
$productSourceLinkInfo = $query->select()->toArray();
$update = [];
$insert = [];
$needNum = $info['product']['nums'];
foreach ($productSourceLinkInfo as $item) {
$info['warehouse_id'] = empty($info['warehouse_id']) ? $item['warehouse_id'] : $info['warehouse_id'];
$info['store_id'] = empty($info['store_id']) ? $item['store_id'] : $info['store_id'];
$currentNum = max(bcsub($item['current_nums'], $needNum, 2), 0);
if ($item['current_nums'] > $needNum) {
[$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,current_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);
$itemCurrentNums = bcadd($exist['current_nums'], $needNum, 2);
$update[] = [
'id' => $exist['id'],
'nums' => $itemNums,
'current_nums' => $itemCurrentNums,
'total_price' => bcmul($item['price'], $itemNums, 2),
'update_time' => $time,
];
} else {
$insertItem = [
'product_id' => $item['product_id'],
'warehouse_id' => $info['warehouse_id'] ?? 0,
'store_id' => $info['store_id'] ?? 0,
'oid' => $item['oid'],
'types' => $info['types'] ?? ProductSourceLinkInfo::TypeOut,
'link_id' => $info['link_id'],
'from_id' => $item['id'],
'nums' => $needNum,
'current_nums' => $needNum,
'price' => $item['price'],
'total_price' => bcmul($item['price'], $needNum, 2),
'purchase_funds_id' => $item['purchase_funds_id'],
'create_time' => !empty($info['create_time']) ? $info['create_time'] : $time,
'update_time' => $time,
];
if ($insertItem['types'] == ProductSourceLinkInfo::TypeOut) {
// 商品出库,价格为供货价,总价为供货总价
$insertItem['price'] = bcdiv($item['supply_price'], $item['nums'], 2);
$insertItem['total_price'] = bcmul($insertItem['price'], $insertItem['nums'], 2);
} elseif ($insertItem['types'] == ProductSourceLinkInfo::TypeOrder) {
// 商品订单出库,价格为销售价,总价为销售总价
$insertItem['price'] = $info['product']['price'];
$insertItem['total_price'] = bcmul($insertItem['price'], $insertItem['nums'], 2);
$insertItem['supply_price'] = bcmul($info['product']['supply_price'], $insertItem['nums'], 2);
}
$insert[] = $insertItem;
}
return [$update, $insert];
}
/**
* 门店入库
* @param array $info
* @return void
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public static function storeInStorage(array $info)
{
$list = ProductSourceLinkInfo::where('link_id', $info['link_id'])
->where('product_id', $info['product_id'])
->where('types', ProductSourceLinkInfo::TypeOut)
->select()->toArray();
if (empty($list)) {
return;
}
foreach ($list as $item) {
ProductSourceLinkInfoLogic::add([
'oid' => $item['oid'],
'product_id' => $item['product_id'],
'warehouse_id' => $info['warehouse_id'] ?? 0,
'store_id' => $info['store_id'] ?? 0,
'nums' => $item['nums'],
'current_nums' => $item['nums'],
'types' => $info['types'],
'link_id' => $info['link_id'],
'from_id' => $item['id'],
'price' => $item['price'],
'total_price' => $item['total_price'],
'purchase_funds_id' => $item['purchase_funds_id'],
]);
}
}
/**
* 门店退库
* @param array $info
* @return void
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public static function storeRollback(array $info)
{
$list = ProductSourceLinkInfo::where('link_id', $info['link_id'])
->where('product_id', $info['product_id'])
->where('types', ProductSourceLinkInfo::TypeStoreIn)
->select()->toArray();
if (empty($list)) {
return;
}
ProductSourceLinkInfoLogic::updateByLinkId($info['link_id'], ProductSourceLinkInfo::TypeStoreIn, ['add_nums' => -$info['nums']]);
}
/**
* 商品调拨
* @param array $info
* @return void
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public static function transfer(array $info)
{
$inventoryTransfer = InventoryTransfer::field('id,nums,one_type,one_id,two_type,two_id')
->where('oid', $info['oid'])
->where('product_id', $info['product_id'])
->findOrEmpty()->toArray();
if (empty($inventoryTransfer)) {
return;
}
$info['link_id'] = $inventoryTransfer['id'];
$query = ProductSourceLinkInfo::where('product_id', $info['product_id']);
if ($inventoryTransfer['one_type'] == 1) {
$query->where('store_id', $inventoryTransfer['one_id'])->whereIn('types', [ProductSourceLinkInfo::TypeStoreIn, ProductSourceLinkInfo::TypeS2S]);
$info['types'] = $inventoryTransfer['two_type'] == 1 ? ProductSourceLinkInfo::TypeS2S : ProductSourceLinkInfo::TypeS2W;
} else {
$info['types'] = ProductSourceLinkInfo::TypeW2W;
$query->where('warehouse_id', $inventoryTransfer['one_id'])->whereIn('types', [ProductSourceLinkInfo::TypeIn, ProductSourceLinkInfo::TypeS2W, ProductSourceLinkInfo::TypeW2W]);
}
if ($inventoryTransfer['two_type'] == 1) {
$info['store_id'] = $inventoryTransfer['two_id'];
} else {
$info['store_id'] = $inventoryTransfer['one_id'];
$info['warehouse_id'] = $inventoryTransfer['two_id'];
}
$list = $query->where('current_nums', '>', 0)
->field('id,oid,product_id,warehouse_id,store_id,nums,current_nums,link_id,from_id,price')
->select()->toArray();
if (empty($list)) {
return;
}
$update = [];
$insert = [];
$needNum = $inventoryTransfer['nums'];
foreach ($list as $item) {
$currentNum = max(bcsub($item['current_nums'], $needNum, 2), 0);
if ($item['current_nums'] > $needNum) {
[$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);
}
}

View File

@ -4,10 +4,16 @@ namespace app\admin\logic\product_source_link_info;
use app\admin\logic\product_source_link\ProductSourceLinkLogic;
use app\admin\logic\purchase_product_offer\PurchaseProductOfferLogic;
use app\common\model\inventory_transfer\InventoryTransfer;
use app\common\model\product_source_link\ProductSourceLink;
use app\common\model\product_source_link_info\ProductSourceLinkInfo;
use app\common\logic\BaseLogic;
use support\exception\BusinessException;
use support\Log;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db;
@ -19,6 +25,29 @@ use think\facade\Db;
class ProductSourceLinkInfoLogic extends BaseLogic
{
public $purchaseProductOffer = []; // 采购商品数据
public $orderProduct = []; // 订单商品数据
public $warehouseProduct = []; // 出入库商品数据
public $storeProductPrice = []; // 商品改价数据
public $info = [
'warehouse_id' => 0,
'buyer_id' => 0,
'types' => 0,
'link_id' => 0,
'is_store_order' => 0,
'store_id' => 0,
'create_time' => 0,
'product_id' => 0,
'nums' => 0,
'add_nums' => 0,
'oid' => 0,
];
public function setInfo($info)
{
$this->info = array_merge($this->info, $info);
}
/**
* @notes 添加商品溯源详细
@ -90,43 +119,131 @@ class ProductSourceLinkInfoLogic extends BaseLogic
return ProductSourceLinkInfo::findOrEmpty($params['id'])->toArray();
}
public static function updateByLinkId($linkId, $types, array $params)
/**
* @notes 仓库入库
* @return bool
*/
public function putInStorage(): bool
{
$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 (isset($params['add_nums']) && in_array($types, [ProductSourceLinkInfo::TypeOut, ProductSourceLinkInfo::TypeStoreIn])) {
if ($params['add_nums'] < 0) {
$subQuery = ProductSourceLinkInfo::where('link_id', $linkId)->where('types', $types);
$otherData = $subQuery->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);
}
(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'],
]);
if (empty($this->purchaseProductOffer)) {
return false;
}
try {
$model = ProductSourceLink::where('product_id', $this->purchaseProductOffer['product_id'])->where('purchase_uid', $this->info['buyer_id'])->where('warehouse_id', $this->info['warehouse_id'])->find();
if (empty($model)) {
$model = new ProductSourceLink();
$model->product_id = $this->purchaseProductOffer['product_id'];
$model->purchase_uid = $this->info['buyer_id'];
$model->warehouse_id = $this->info['warehouse_id'];
$model->save();
}
$attrs = [
'oid' => $model['id'],
'product_id' => $this->purchaseProductOffer['product_id'],
'warehouse_id' => $this->info['warehouse_id'],
'nums' => $this->purchaseProductOffer['buyer_nums'],
'current_nums' => $this->purchaseProductOffer['buyer_nums'],
'types' => $this->info['types'],
'link_id' => $this->info['link_id'],
'price' => $this->purchaseProductOffer['price'],
'total_price' => $this->purchaseProductOffer['total_price'],
'purchase_funds_id' => $this->purchaseProductOffer['purchase_funds_id'] ?? 0,
];
if ($attrs['types'] == ProductSourceLinkInfo::TypeIn) {
// 商品入库,记录采购总价和供货总价
$priceRate = PurchaseProductOfferLogic::getProductPriceRate(['id' => $this->purchaseProductOffer['product_id']]);
$attrs['purchase_price'] = $this->purchaseProductOffer['total_price'];
$rate = bcdiv($priceRate['supply_rate'], 100, 2);
$attrs['supply_price'] = bcmul(bcmul($this->purchaseProductOffer['price'], $rate, 2), $this->purchaseProductOffer['nums'], 2);
}
ProductSourceLinkInfoLogic::add($attrs);
return true;
} catch (BusinessException $e) {
Log::error('商品入库溯源信息保存失败' . $e->getMessage());
}
return false;
}
/**
* 仓库出库
* @return true
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function outbound()
{
$query = ProductSourceLinkInfo::where('product_id', $this->warehouseProduct['product_id'])->where('current_nums', '>', 0);
$query->whereIn('types', [ProductSourceLinkInfo::TypeIn, ProductSourceLinkInfo::TypeS2W, ProductSourceLinkInfo::TypeW2W]);
$productSourceLinkInfo = $query->select()->toArray();
$needNum = $this->warehouseProduct['nums'];
return $this->putOutStorage($productSourceLinkInfo, $needNum);
}
/**
* 订单销售出库
* @return true
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function sale()
{
$query = ProductSourceLinkInfo::where('product_id', $this->orderProduct['product_id'])->where('current_nums', '>', 0);
$query->where('store_id', $this->info['store_id'])->whereIn('types', [ProductSourceLinkInfo::TypeStoreIn, ProductSourceLinkInfo::TypeS2S]);
$productSourceLinkInfo = $query->select()->toArray();
$needNum = $this->orderProduct['nums'];
return $this->putOutStorage($productSourceLinkInfo, $needNum);
}
/**
* 更新入库单的数量和价格
* @return void
* @throws DbException
*/
public function updateInStorageNum()
{
$data = ProductSourceLinkInfo::where('link_id', $this->info['link_id'])->where('types', ProductSourceLinkInfo::TypeIn)->findOrEmpty()->toArray();
$attrs['nums'] = bcadd($data['nums'], $this->info['add_nums'], 2);
$attrs['current_nums'] = bcadd($data['current_nums'], $this->info['add_nums'], 2);
$attrs['total_price'] = bcmul($this->info['nums'], $data['price'], 2);
$attrs['supply_price'] = bcmul($this->info['nums'], bcdiv($data['supply_price'], $data['nums'], 2), 2);
ProductSourceLinkInfo::where('id', $data['id'])->update($attrs);
}
/**
* 更新出库单的数量和价格
* @return void
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function updateOutboundNum()
{
if ($this->info['add_nums'] < 0) {
$list = ProductSourceLinkInfo::where('link_id', $this->info['link_id'])->where('types', ProductSourceLinkInfo::TypeOut)->order('id desc')->select()->toArray();
if (empty($list)) {
return;
}
$rollbackNum = abs($this->info['add_nums']);
$update = [];
foreach ($list as $item) {
if ($item['nums'] > $rollbackNum) {
$update = $this->setUpdate($item, $rollbackNum, $update);
break;
} else {
$update = $this->setUpdate($item, $item['nums'], $update);
}
$rollbackNum = bcsub($rollbackNum, $item['nums'], 2);
}
(new ProductSourceLinkInfo())->saveAll($update);
} else {
$data = ProductSourceLinkInfo::where('link_id', $this->info['link_id'])->where('types', ProductSourceLinkInfo::TypeOut)->findOrEmpty()->toArray();
if (empty($data)) {
return;
}
$update = $this->setUpdate($data, -$this->info['add_nums'], []);
(new ProductSourceLinkInfo())->saveAll($update);
}
}
@ -139,7 +256,7 @@ class ProductSourceLinkInfoLogic extends BaseLogic
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function deleteByLinkId($linkId, $types)
public function deleteByLinkId($linkId, $types)
{
$list = ProductSourceLinkInfo::where('link_id', $linkId)->where('types', $types)->select()->toArray();
$update = [];
@ -158,17 +275,130 @@ class ProductSourceLinkInfoLogic extends BaseLogic
(new ProductSourceLinkInfo())->saveAll($update);
}
private function getInsertAndUpdate($update, $insert, $currentNum, $item, $needNum)
{
$time = time();
$update[] = [
'id' => $item['id'],
'current_nums' => $currentNum,
'update_time' => $time,
];
$exist = ProductSourceLinkInfo::field('id,from_id,nums,current_nums')->where('link_id', $this->info['link_id'])->where('types', ProductSourceLinkInfo::TypeOut)->findOrEmpty()->toArray();
if (!empty($exist) && $exist['from_id'] == $item['id']) {
$itemNums = bcadd($exist['nums'], $needNum, 2);
$itemCurrentNums = bcadd($exist['current_nums'], $needNum, 2);
$update[] = [
'id' => $exist['id'],
'nums' => $itemNums,
'current_nums' => $itemCurrentNums,
'total_price' => bcmul($item['price'], $itemNums, 2),
'update_time' => $time,
];
} else {
$insertItem = [
'product_id' => $item['product_id'],
'warehouse_id' => $this->info['warehouse_id'],
'store_id' => $this->info['store_id'],
'oid' => $item['oid'],
'types' => empty($this->info['types']) ? ProductSourceLinkInfo::TypeOut: $this->info['types'],
'link_id' => $this->info['link_id'],
'from_id' => $item['id'],
'nums' => $needNum,
'current_nums' => $needNum,
'price' => $item['price'],
'total_price' => bcmul($item['price'], $needNum, 2),
'purchase_funds_id' => $item['purchase_funds_id'],
'create_time' => !empty($this->info['create_time']) ? $this->info['create_time'] : $time,
'update_time' => $time,
'trace_info' => empty($item['trace_info']) ? $item['id'] : $item['trace_info'] . ',' . $item['id'],
];
if ($insertItem['types'] == ProductSourceLinkInfo::TypeOut) {
// 商品出库,价格为供货价,总价为供货总价
$insertItem['price'] = bcdiv($item['supply_price'], $item['nums'], 2);
$insertItem['total_price'] = bcmul($insertItem['price'], $insertItem['nums'], 2);
} elseif ($insertItem['types'] == ProductSourceLinkInfo::TypeOrder) {
// 商品订单出库,价格为销售价,总价为销售总价
$insertItem['price'] = $this->orderProduct['price'];
$insertItem['total_price'] = bcmul($insertItem['price'], $insertItem['nums'], 2);
$insertItem['supply_price'] = bcmul($this->orderProduct['supply_price'], $insertItem['nums'], 2);
}
$insert[] = $insertItem;
}
return [$update, $insert];
}
/**
* @param array $data
* @param float|int|string $rollbackNum
* @param array $update
* @return array
* 更新商品溯源供货价
* @return void
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function setUpdate(array $data, float|int|string $rollbackNum, array $update): array
public function updateSupplyPrice()
{
$productSourceLinkInfo = ProductSourceLinkInfo::where('product_id', $this->storeProductPrice['product_id'])
->where('link_id', $this->storeProductPrice['warehouse_product_id'])
->where('types', ProductSourceLinkInfo::TypeIn)
->find();
if (!empty($productSourceLinkInfo)) {
ProductSourceLinkInfo::where('id', $productSourceLinkInfo['id'])->update(['supply_price' => bcmul($this->storeProductPrice['purchase'], $productSourceLinkInfo['nums'], 2)]);
$list = ProductSourceLinkInfo::field('id,nums,types')
->whereFindInSet('trace_info', $productSourceLinkInfo['id'])
->select()->toArray();
$update = [];
foreach ($list as $item) {
if (in_array($item['types'], [ProductSourceLinkInfo::TypeOrder, ProductSourceLinkInfo::TypeOrderRefund])) {
$update[] = [
'id' => $item['id'],
'supply_price' => bcmul($this->storeProductPrice['purchase'], $item['nums'], 2),
];
} else {
$update[] = [
'id' => $item['id'],
'price' => $this->storeProductPrice['purchase'],
'total_price' => bcmul($this->storeProductPrice['purchase'], $item['nums'], 2),
];
}
}
(new ProductSourceLinkInfo())->saveAll($update);
}
}
/**
* 门店退库
* @return void
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function storeRollback()
{
$list = ProductSourceLinkInfo::where('link_id', $this->info['link_id'])
->where('product_id', $this->info['product_id'])
->where('types', ProductSourceLinkInfo::TypeStoreIn)
->order('id desc')->select()->toArray();
if (empty($list)) {
return;
}
$rollbackNum = abs($this->info['nums']);
$update = [];
foreach ($list as $item) {
if ($item['nums'] > $rollbackNum) {
$update = $this->setUpdate2($item, $rollbackNum, $update);
break;
} else {
$update = $this->setUpdate2($item, $item['nums'], $update);
}
$rollbackNum = bcsub($rollbackNum, $item['nums'], 2);
}
(new ProductSourceLinkInfo())->saveAll($update);
}
private function setUpdate(array $data, float|int|string $rollbackNum, array $update): array
{
$dataNums = bcsub($data['nums'], $rollbackNum, 2);
$currentNums = max(bcsub($data['current_nums'], $rollbackNum, 2), 0);
$totalPrice = bcmul($data['price'], $dataNums, 2);
$rollbackPrice = bcmul($data['price'], $rollbackNum, 2);
$update[] = [
'id' => $data['id'],
'nums' => $dataNums,
@ -184,7 +414,7 @@ class ProductSourceLinkInfoLogic extends BaseLogic
'id' => $data['from_id'],
'nums' => Db::raw("nums-{$rollbackNum}"),
'current_nums' => Db::raw("current_nums-{$rollbackNum}"),
'total_price' => Db::raw("total_price-{$totalPrice}"),
'total_price' => Db::raw("total_price-{$rollbackPrice}"),
];
$update[] = [
'id' => $outboundLinkInfoId,
@ -201,4 +431,116 @@ class ProductSourceLinkInfoLogic extends BaseLogic
return $update;
}
/**
* 商品调拨
* @return void
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function transfer()
{
$inventoryTransfer = InventoryTransfer::field('id,nums,one_type,one_id,two_type,two_id')
->where('oid', $this->info['oid'])
->where('product_id', $this->info['product_id'])
->findOrEmpty()->toArray();
if (empty($inventoryTransfer)) {
return;
}
$this->info['link_id'] = $inventoryTransfer['id'];
$query = ProductSourceLinkInfo::where('product_id', $this->info['product_id']);
if ($inventoryTransfer['one_type'] == 1) {
$query->where('store_id', $inventoryTransfer['one_id'])->whereIn('types', [ProductSourceLinkInfo::TypeStoreIn, ProductSourceLinkInfo::TypeS2S]);
$this->info['types'] = $inventoryTransfer['two_type'] == 1 ? ProductSourceLinkInfo::TypeS2S : ProductSourceLinkInfo::TypeS2W;
} else {
$this->info['types'] = ProductSourceLinkInfo::TypeW2W;
$query->where('warehouse_id', $inventoryTransfer['one_id'])->whereIn('types', [ProductSourceLinkInfo::TypeIn, ProductSourceLinkInfo::TypeS2W, ProductSourceLinkInfo::TypeW2W]);
}
if ($inventoryTransfer['two_type'] == 1) {
$this->info['store_id'] = $inventoryTransfer['two_id'];
} else {
$this->info['store_id'] = $inventoryTransfer['one_id'];
$this->info['warehouse_id'] = $inventoryTransfer['two_id'];
}
$list = $query->where('current_nums', '>', 0)
->field('id,oid,product_id,warehouse_id,store_id,purchase_funds_id,nums,current_nums,link_id,from_id,price,trace_info')
->select()->toArray();
if (empty($list)) {
return;
}
$update = [];
$insert = [];
$needNum = $inventoryTransfer['nums'];
foreach ($list as $item) {
$currentNum = max(bcsub($item['current_nums'], $needNum, 2), 0);
if ($item['current_nums'] > $needNum) {
[$update, $insert] = $this->getInsertAndUpdate2($update, $insert, $currentNum, $item, $needNum);
break;
} else {
[$update, $insert] = $this->getInsertAndUpdate2($update, $insert, 0, $item, $item['current_nums']);
}
$needNum = $needNum - $item['current_nums'];
}
(new ProductSourceLinkInfo())->saveAll($update);
(new ProductSourceLinkInfo())->insertAll($insert);
}
/**
* 门店入库
* @return void
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function storeInStorage()
{
$list = ProductSourceLinkInfo::where('link_id', $this->info['link_id'])
->where('product_id', $this->info['product_id'])
->where('types', ProductSourceLinkInfo::TypeOut)
->select()->toArray();
if (empty($list)) {
return;
}
$insert = [];
foreach ($list as $item) {
$insert[] = [
'oid' => $item['oid'],
'product_id' => $item['product_id'],
'warehouse_id' => $this->info['warehouse_id'],
'store_id' => $this->info['store_id'],
'nums' => $item['nums'],
'current_nums' => $item['nums'],
'types' => $this->info['types'],
'link_id' => $this->info['link_id'],
'from_id' => $item['id'],
'price' => $item['price'],
'total_price' => $item['total_price'],
'purchase_funds_id' => $item['purchase_funds_id'],
'trace_info' => empty($item['trace_info']) ? $item['id'] : $item['trace_info'] . ',' . $item['id'],
];
}
ProductSourceLinkInfo::insertAll($insert);
}
public function putOutStorage(array $productSourceLinkInfo, mixed $needNum): bool
{
$update = [];
$insert = [];
foreach ($productSourceLinkInfo as $item) {
$this->info['warehouse_id'] = empty($this->info['warehouse_id']) ? $item['warehouse_id'] : $this->info['warehouse_id'];
$this->info['store_id'] = empty($this->info['store_id']) ? $item['store_id'] : $this->info['store_id'];
$currentNum = max(bcsub($item['current_nums'], $needNum, 2), 0);
if ($item['current_nums'] > $needNum) {
[$update, $insert] = $this->getInsertAndUpdate($update, $insert, $currentNum, $item, $needNum);
break;
} else {
[$update, $insert] = $this->getInsertAndUpdate($update, $insert, 0, $item, $item['current_nums']);
}
$needNum = $needNum - $item['current_nums'];
}
(new ProductSourceLinkInfo())->saveAll($update);
(new ProductSourceLinkInfo())->insertAll($insert);
return true;
}
}

View File

@ -3,6 +3,7 @@
namespace app\admin\logic\store_product_price;
use app\admin\logic\product_source_link_info\ProductSourceLinkInfoLogic;
use app\admin\service\ProductPriceService;
use app\common\model\product_source_link_info\ProductSourceLinkInfo;
use app\common\model\store_product_group_price\StoreProductGroupPrice;
@ -111,13 +112,9 @@ class StoreProductPriceLogic extends BaseLogic
'ot_price' => $find['price']
]);
$productSourceLinkInfo = ProductSourceLinkInfo::where('product_id', $find['product_id'])
->where('link_id', $find['warehouse_product_id'])
->where('types', ProductSourceLinkInfo::TypeIn)
->find();
if (!empty($productSourceLinkInfo)) {
ProductSourceLinkInfo::where('id', $productSourceLinkInfo['id'])->update(['supply_price' => bcmul($find['purchase'], $productSourceLinkInfo['nums'], 2)]);
}
$productSourceLinkInfoLogic = new ProductSourceLinkInfoLogic();
$productSourceLinkInfoLogic->storeProductPrice = $find;
$productSourceLinkInfoLogic->updateSupplyPrice();
}
Db::commit();

View File

@ -3,6 +3,7 @@
namespace app\admin\logic\system_store_storage;
use app\admin\logic\product_source_link\ProductSourceLinkLogic;
use app\admin\logic\product_source_link_info\ProductSourceLinkInfoLogic;
use app\admin\logic\store_product\StoreProductLogic;
use app\common\model\product_source_link_info\ProductSourceLinkInfo;
use app\common\model\store_branch_product\StoreBranchProduct;
@ -80,13 +81,17 @@ class SystemStoreStorageLogic extends BaseLogic
$storeBranchProduct->save();
SqlChannelLog('StoreBranchProduct', $storeBranchProduct['id'], $find['nums'], 1,Request()->url(),$admin_id);
}
ProductSourceLinkLogic::storeInStorage([
$productSourceLinkInfoLogic = new ProductSourceLinkInfoLogic();
$productSourceLinkInfoLogic->setInfo([
'link_id' => $find['id'],
'product_id' => $find['product_id'],
'warehouse_id' => $find['warehouse_id'],
'store_id' => $find['store_id'],
'types' => ProductSourceLinkInfo::TypeStoreIn,
]);
$productSourceLinkInfoLogic->storeInStorage();
}
Db::commit();
return true;
@ -200,12 +205,16 @@ class SystemStoreStorageLogic extends BaseLogic
$warehouseStorage->save();
SqlChannelLog('WarehouseProductStorege', $warehouseStorage['id'], $params['num'], 1,Request()->url());
SqlChannelLog('StoreBranchProduct', $StoreProduct['id'], $params['num'], -1,Request()->url());
ProductSourceLinkLogic::storeRollback([
$productSourceLinkInfoLogic = new ProductSourceLinkInfoLogic();
$productSourceLinkInfoLogic->setInfo([
'link_id' => $warehouseProduct['id'],
'product_id' => $warehouseProduct['product_id'],
'nums' => $params['num'],
'types' => ProductSourceLinkInfo::TypeStoreOut,
]);
$productSourceLinkInfoLogic->storeRollback();
Db::commit();
return true;
} catch (\Exception $e) {

View File

@ -124,13 +124,16 @@ class WarehouseProductLogic extends BaseLogic
PurchaseFunds::where('id', $params['purchase_funds_id'])->dec('current_amount', $params['total_price'])->save();
}
ProductSourceLinkLogic::add([
'purchase_product_offer' => [$params],
$productSourceLinkInfoLogic = new ProductSourceLinkInfoLogic();
$productSourceLinkInfoLogic->purchaseProductOffer = $params;
$productSourceLinkInfoLogic->setInfo([
'types' => ProductSourceLinkInfo::TypeIn,
'buyer_id' => $params['buyer_id'],
'warehouse_id' => $params['warehouse_id'],
'link_id' => $res['id'],
]);
$productSourceLinkInfoLogic->putInStorage();
DeliveryServiceLogic::subPurchaseFunds($params['buyer_id'], $params['total_price']);
return $res;
} catch (\Throwable $e) {
@ -194,12 +197,21 @@ class WarehouseProductLogic extends BaseLogic
$res = WarehouseProduct::create($data);
SqlChannelLog('WarehouseProduct', $res->id, $params['nums'], $params['financial_pm'] == 1 ? 1 : -1, Request()->url(),$admin_id);
ProductSourceLinkLogic::outbound([
'product' => $data,
// ProductSourceLinkInfoLogic::outbound([
// 'product' => $data,
// 'warehouse_id' => $params['warehouse_id'],
// 'store_id' => $data['store_id'],
// 'link_id' => $res['id'],
// ]);
$productSourceLinkInfoLogic = new ProductSourceLinkInfoLogic();
$productSourceLinkInfoLogic->warehouseProduct = $data;
$productSourceLinkInfoLogic->setInfo([
'warehouse_id' => $params['warehouse_id'],
'store_id' => $data['store_id'],
'link_id' => $res['id'],
]);
$productSourceLinkInfoLogic->outbound();
Db::commit();
return $res;
@ -225,6 +237,9 @@ class WarehouseProductLogic extends BaseLogic
$res = WarehouseProduct::where('id', $params['id'])->withTrashed()->find();
$updateNums = bcsub($params['nums'], $res['nums'],2);
if ($updateNums != 0) {
if ($res['status'] == 1) {
throw new BusinessException('门店已确认入库,不能修改数量');
}
$storageNum = $res['financial_pm'] == 0 ? -$updateNums : $updateNums;
$productNum = $updateNums;
self::updateWarehouseProduct($res, $storageNum, $productNum);
@ -277,7 +292,10 @@ class WarehouseProductLogic extends BaseLogic
$res->delete();
self::updateWarehouseOrder($res['oid']);
$types = $res['financial_pm'] == 1 ? ProductSourceLinkInfo::TypeIn : ProductSourceLinkInfo::TypeOut;
ProductSourceLinkInfoLogic::deleteByLinkId($res['id'], $types);
$productSourceLinkInfoLogic = new ProductSourceLinkInfoLogic();
$productSourceLinkInfoLogic->deleteByLinkId($res['id'], $types);
Db::commit();
return true;
} catch (\Throwable $th) {
@ -528,14 +546,20 @@ class WarehouseProductLogic extends BaseLogic
public static function updateProductSourceLink($warehouseProduct, $params, $updateNums)
{
$ProductSourceLinkInfoParams = ['nums' => $params['nums'], 'total_price' => bcmul($warehouseProduct['price'], $params['nums'], 2)];
$productSourceLinkInfoLogic = new ProductSourceLinkInfoLogic();
$productSourceLinkInfoLogic->setInfo([
'link_id' => $warehouseProduct['id'],
'product_id' => $warehouseProduct['product_id'],
'nums' => $params['nums'],
'add_nums' => $updateNums,
]);
if ($warehouseProduct['financial_pm'] == 1) {
$ProductSourceLinkInfoParams['price'] = $params['purchase'];
$ProductSourceLinkInfoParams['add_nums'] = $updateNums;
ProductSourceLinkInfoLogic::updateByLinkId($warehouseProduct['id'], ProductSourceLinkInfo::TypeIn, $ProductSourceLinkInfoParams);
$productSourceLinkInfoLogic->updateInStorageNum();
} else {
$ProductSourceLinkInfoParams['add_nums'] = bcsub($params['nums'], $warehouseProduct['nums'],2);
ProductSourceLinkInfoLogic::updateByLinkId($warehouseProduct['id'], ProductSourceLinkInfo::TypeOut, $ProductSourceLinkInfoParams);
$productSourceLinkInfoLogic->setInfo([
'add_nums' => bcsub($params['nums'], $warehouseProduct['nums'],2),
]);
$productSourceLinkInfoLogic->updateOutboundNum();
}
}

View File

@ -3,6 +3,7 @@
namespace app\common\logic;
use app\admin\logic\product_source_link\ProductSourceLinkLogic;
use app\admin\logic\product_source_link_info\ProductSourceLinkInfoLogic;
use app\admin\logic\store_product\StoreProductLogic;
use app\admin\logic\user_ship\UserShipLogic;
use app\api\logic\order\OrderLogic;
@ -641,18 +642,22 @@ class PayNotifyLogic extends BaseLogic
'sales' => bcadd($branchProduct['sales'], $v['cart_num'], 2)
], ['id' => $branchProduct['id']]);
SqlChannelLog('StoreBranchProduct', $branchProduct['id'], $v['cart_num'], -1, Request()->url());
ProductSourceLinkLogic::outbound([
'product' => [
'product_id' => $v['product_id'],
'nums' => $v['cart_num'],
'price' => $branchProduct['price'],
'supply_price' => $branchProduct['purchase'],
],
$productSourceLinkInfoLogic = new ProductSourceLinkInfoLogic();
$productSourceLinkInfoLogic->orderProduct = [
'product_id' => $v['product_id'],
'nums' => $v['cart_num'],
'price' => $branchProduct['price'],
'supply_price' => $branchProduct['purchase'],
];
$productSourceLinkInfoLogic->setInfo([
'is_store_order' => true,
'store_id' => $v['store_id'],
'link_id' => $v['id'],
'types' => ProductSourceLinkInfo::TypeOrder,
]);
$productSourceLinkInfoLogic->sale();
} else {
StoreProductLogic::ordinary(['id' => $v['product_id']], $v['store_id'], 0, $storeProduct);
StoreBranchProduct::update([