调整商品溯源

This commit is contained in:
lewis 2025-03-12 17:54:08 +08:00
parent 8c4128ccd4
commit ad25d6c599
10 changed files with 109 additions and 49 deletions

View File

@ -40,7 +40,7 @@ class ProductSourceLinkInfoController extends BaseAdminController
{
$params = (new ProductSourceLinkInfoValidate())->post()->goCheck('add');
$result = ProductSourceLinkInfoLogic::add($params);
if (true === $result) {
if ($result) {
return $this->success('添加成功', [], 1, 1);
}
return $this->fail(ProductSourceLinkInfoLogic::getError());

View File

@ -51,7 +51,7 @@ class ProductSourceLinkLists extends BaseAdminDataLists implements ListsSearchIn
->select()
->each(function ($item) {
$item->total_nums = ProductSourceLinkInfo::where('product_id', $item->product_id)->where('oid',$item->id)->where('types',1)->sum('nums');
$item->warehouse_nums = ProductSourceLinkInfo::where('product_id', $item->product_id)->where('oid',$item->id)->where('types',2)->sum('nums');
$item->warehouse_outbound_nums = ProductSourceLinkInfo::where('product_id', $item->product_id)->where('oid',$item->id)->where('types',2)->sum('nums');
})
->toArray();
}

View File

@ -43,10 +43,13 @@ class ProductSourceLinkInfoLists extends BaseAdminDataLists implements ListsSear
public function lists(): array
{
return ProductSourceLinkInfo::where($this->searchWhere)
->field(['id', 'oid', 'product_id', 'nums', 'types', 'link_id', 'total_price'])
->with(['product'])
->limit($this->limitOffset, $this->limitLength)
->order(['id' => 'desc'])
->select()
->each(function ($item) {
$item['type_name'] = $item->getTypeName();
})
->toArray();
}

View File

@ -2,11 +2,14 @@
namespace app\admin\logic\beforehand_order_cart_info;
use app\admin\logic\product_source_link\ProductSourceLinkLogic;
use app\admin\logic\purchase_product_offer\PurchaseProductOfferLogic;
use app\admin\logic\warehouse_product\WarehouseProductLogic;
use app\common\model\beforehand_order_cart_info\BeforehandOrderCartInfo;
use app\common\logic\BaseLogic;
use app\common\model\beforehand_order\BeforehandOrder;
use app\common\model\product_source_link\ProductSourceLink;
use app\common\model\product_source_link_info\ProductSourceLinkInfo;
use app\common\model\purchase_product_offer\PurchaseProductOffer;
use app\common\model\store_product\StoreProduct;
use app\common\model\warehouse_order\WarehouseOrder;
@ -290,6 +293,12 @@ class BeforehandOrderCartInfoLogic extends BaseLogic
if (!$result) {
throw new BusinessException('出库失败,预订单更新出错');
}
ProductSourceLink::add([
'purchase_product_offer' => $offer_list,
'types' => ProductSourceLinkInfo::TypeIn,
'buyer_id' => $res['buyer_id'],
'warehouse_id' => $params['warehouse_id'],
]);
Db::commit();
return true;
} catch (\Throwable $e) {
@ -389,7 +398,7 @@ class BeforehandOrderCartInfoLogic extends BaseLogic
if ($purchaseProductOffer['is_storage'] == 1) {
throw new BusinessException('商品已入库');
}
$beforehandOrder = BeforehandOrder::where('id', $params['bhoid'])->field('id,order_type,warehousing_id,is_warehousing')->find();
$beforehandOrder = BeforehandOrder::where('id', $params['bhoid'])->field('id,buyer_id,order_type,warehousing_id,is_warehousing')->find();
$completed_amount = PurchaseProductOffer::where(['order_id' => $params['bhoid'], 'pay_type' => 1, 'is_storage' => 1])->sum('total_price');
$outstanding_amount = PurchaseProductOffer::where(['order_id' => $params['bhoid'], 'pay_type' => 2, 'is_storage' => 1])->sum('total_price');
@ -435,6 +444,9 @@ class BeforehandOrderCartInfoLogic extends BaseLogic
$data['purchase'] = $purchaseProductOffer['price'];
$data['total_price'] = $purchaseProductOffer['total_price'];
$data['financial_pm'] = 1;
$data['buyer_id'] = $beforehandOrder['buyer_id'];
$data['buyer_nums'] = $data['nums'];
$data['price'] = $data['purchase'];
$data['manufacture'] = $purchaseProductOffer['manufacture'] > 0 ? date('Y-m-d H:i:s', $purchaseProductOffer['manufacture']) : '';
$data['expiration_date'] = $purchaseProductOffer['expiration_date'] > 0 ? date('Y-m-d H:i:s', $purchaseProductOffer['expiration_date']) : '';
if ($data['nums'] > 0) {
@ -461,7 +473,6 @@ class BeforehandOrderCartInfoLogic extends BaseLogic
if (!in_array($beforehandOrder['order_type'], [6, 9])) {
PurchaseProductOfferLogic::setProductGroupPrice($purchaseProductOffer, $product, $params['warehouse_id']);
}
Db::commit();
return true;
} catch (\Throwable $e) {

View File

@ -3,8 +3,10 @@
namespace app\admin\logic\product_source_link;
use app\admin\logic\product_source_link_info\ProductSourceLinkInfoLogic;
use app\common\model\product_source_link\ProductSourceLink;
use app\common\logic\BaseLogic;
use app\common\model\product_source_link_info\ProductSourceLinkInfo;
use support\exception\BusinessException;
use think\facade\Db;
@ -20,31 +22,33 @@ class ProductSourceLinkLogic extends BaseLogic
/**
* @notes 添加商品溯源管理
* @param array $params
* @param array $info
* @return bool
* @author admin
* @date 2025/03/12 10:03
*/
public static function add(array $params): bool
public static function add(array $info): bool
{
Db::startTrans();
try {
ProductSourceLink::create([
'purchase_uid' => $params['purchase_uid'],
'product_id' => $params['product_id'],
'total_nums' => $params['total_nums'],
'nums' => $params['nums'],
'warehouse_id' => $params['warehouse_id'],
'warehouse_total_nums' => $params['warehouse_total_nums'],
'warehouse_nums' => $params['warehouse_nums']
foreach ($info['purchase_product_offer'] as $offer) {
$model = ProductSourceLink::where('product_id', $offer['product_id'])->where('purchase_uid', $info['buyer_id'])->where('warehouse_id', $info['warehouse_id'])->find();
if (empty($model)) {
$model = new ProductSourceLink();
$model->product_id = $offer['product_id'];
$model->purchase_uid = $info['buyer_id'];
$model->warehouse_id = $info['warehouse_id'];
$model->save();
}
ProductSourceLinkInfoLogic::add([
'oid' => $model['id'],
'product_id' => $offer['product_id'],
'nums' => $offer['buyer_nums'],
'types' => $info['types'],
'link_id' => $info['link_id'],
'price' => $offer['price'],
'total_price' => $offer['total_price'],
]);
Db::commit();
return true;
} catch (\Throwable $e) {
Db::rollback();
throw new BusinessException($e->getMessage());
}
return true;
}
@ -62,11 +66,7 @@ class ProductSourceLinkLogic extends BaseLogic
ProductSourceLink::where('id', $params['id'])->update([
'purchase_uid' => $params['purchase_uid'],
'product_id' => $params['product_id'],
'total_nums' => $params['total_nums'],
'nums' => $params['nums'],
'warehouse_id' => $params['warehouse_id'],
'warehouse_total_nums' => $params['warehouse_total_nums'],
'warehouse_nums' => $params['warehouse_nums']
]);
Db::commit();

View File

@ -21,29 +21,15 @@ class ProductSourceLinkInfoLogic extends BaseLogic
/**
* @notes 添加商品溯源详细
* @param array $params
* @return bool
* @author admin
* @date 2025/03/12 10:08
*/
public static function add(array $params): bool
public static function add(array $params)
{
Db::startTrans();
try {
ProductSourceLinkInfo::create([
'oid' => $params['oid'],
'product_id' => $params['product_id'],
'nums' => $params['nums'],
'types' => $params['types'],
'link_id' => $params['link_id'],
'total_price' => $params['total_price']
]);
Db::commit();
return true;
} catch (\Throwable $e) {
Db::rollback();
throw new BusinessException($e->getMessage());
}
$model = new ProductSourceLinkInfo();
$model->setAttrs($params);
$model->save();
return $model;
}
@ -100,4 +86,18 @@ class ProductSourceLinkInfoLogic extends BaseLogic
{
return ProductSourceLinkInfo::findOrEmpty($params['id'])->toArray();
}
public static function updateByLinkId($linkId, $types, array $params)
{
ProductSourceLinkInfo::where('link_id', $linkId)->where('types', $types)->update($params);
}
public static function deleteByLinkId($linkId, $types)
{
$data = ProductSourceLinkInfo::where('link_id', $linkId)->where('types', $types)->find();
if (!empty($data)) {
$data->delete();
}
}
}

View File

@ -195,7 +195,8 @@ class SystemStoreStorageLogic extends BaseLogic
$warehouseProduct->save();
$warehouseStorage->nums += $params['num'];
$warehouseStorage->save();
SqlChannelLog('StoreBranchProduct', $warehouseProduct['id'], $warehouseProduct['nums'], 1,Request()->url());
SqlChannelLog('WarehouseProductStorege', $warehouseStorage['id'], $params['num'], 1,Request()->url());
SqlChannelLog('StoreBranchProduct', $StoreProduct['id'], $params['num'], -1,Request()->url());
Db::commit();
return true;
} catch (\Exception $e) {

View File

@ -20,6 +20,7 @@ class WarehouseOrderLogic extends BaseLogic
/**
* @deprecated 已禁止直接创建入库单
* @notes 添加仓储商品单
* @param array $params
* @return bool
@ -92,9 +93,11 @@ class WarehouseOrderLogic extends BaseLogic
throw new BusinessException('订单不存在');
}
$order_type=BeforehandOrder::where('warehousing_id|outbound_id',$find['id'])->value('order_type')??0;
$buyerId = BeforehandOrder::where('warehousing_id|outbound_id',$find['id'])->value('buyer_id') ?? 0;
Db::startTrans();
try {
foreach ($params['product_arr'] as $k => $v) {
$data['buyer_id'] = $buyerId;
$data['order_type'] = $order_type;
$data['supplier_id'] = $v['supplier_id']??0;
$data['pay_type'] = $v['pay_type']??0;
@ -106,6 +109,8 @@ class WarehouseOrderLogic extends BaseLogic
$data['product_id'] = $v['id'];
$data['nums'] = $v['nums'];
$data['purchase'] = $v['purchase'];
$data['buyer_nums'] = $v['nums'];
$data['price'] = $v['purchase'];
$data['total_price'] = $v['total_price'];
$data['financial_pm'] = $find['financial_pm'];
if (!empty($v['manufacture'])) {

View File

@ -2,8 +2,12 @@
namespace app\admin\logic\warehouse_product;
use app\admin\logic\product_source_link\ProductSourceLinkLogic;
use app\admin\logic\product_source_link_info\ProductSourceLinkInfoLogic;
use app\admin\logic\store_branch_product\StoreBranchProductLogic;
use app\admin\logic\store_product\StoreProductLogic;
use app\common\model\beforehand_order\BeforehandOrder;
use app\common\model\product_source_link_info\ProductSourceLinkInfo;
use app\common\model\warehouse_product\WarehouseProduct;
use app\common\logic\BaseLogic;
use app\common\model\purchase_product_offer\PurchaseProductOffer;
@ -29,7 +33,6 @@ class WarehouseProductLogic extends BaseLogic
/**
* @notes 添加商品仓储信息
* @param array $params
* @return bool
* @author admin
* @date 2024/07/31 16:55
*/
@ -112,6 +115,13 @@ 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::add([
'purchase_product_offer' => [$params],
'types' => ProductSourceLinkInfo::TypeIn,
'buyer_id' => $params['buyer_id'],
'warehouse_id' => $params['warehouse_id'],
'link_id' => $res['id'],
]);
return $res;
} catch (\Throwable $e) {
throw new BusinessException($e->getMessage());
@ -245,12 +255,19 @@ class WarehouseProductLogic extends BaseLogic
$datas['expiration_date'] = strtotime($params['expiration_date']);
}
$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();
return $res;
} catch (\Exception $e) {
Db::rollback();
d($e);
throw new BusinessException($e->getMessage());
}
}
@ -274,6 +291,8 @@ class WarehouseProductLogic extends BaseLogic
self::incProductDecStorege($res, $res['nums'], $admin_id);
}
$res->delete();
$types = $res['financial_pm'] == 1 ? ProductSourceLinkInfo::TypeIn : ProductSourceLinkInfo::TypeOut;
ProductSourceLinkInfoLogic::deleteByLinkId($res['id'], $types);
Db::commit();
return true;
} catch (\Throwable $th) {

View File

@ -4,6 +4,7 @@ namespace app\common\model\product_source_link_info;
use app\common\model\BaseModel;
use app\common\model\store_product\StoreProduct;
use think\model\concern\SoftDelete;
@ -18,5 +19,25 @@ class ProductSourceLinkInfo extends BaseModel
protected $name = 'product_source_link_info';
protected $deleteTime = 'delete_time';
const TypeIn = 1;
const TypeOut = 2;
const TypeStoreIn = 3;
const TypeOrder = 4;
public function getTypeName()
{
$typeMap = [
self::TypeIn => '入库',
self::TypeOut => '出库',
self::TypeStoreIn => '门店入库',
self::TypeOrder => '订单',
];
return $typeMap[$this->getAttr('types')];
}
public function product()
{
return $this->hasOne(StoreProduct::class, 'id', 'product_id')->bind(['product_name' => 'store_name', 'image']);
}
}