增加:进货商品售出后 返还对应的进货额度

This commit is contained in:
wuhui_zzw 2024-06-11 16:29:30 +08:00
parent f40f180d8e
commit 644f2b0c18
5 changed files with 223 additions and 12 deletions

View File

@ -0,0 +1,13 @@
<?php
namespace app\common\dao\store\product;
use app\common\dao\BaseDao;
use app\common\model\store\product\WithGoodsLog;
class WithGoodsLogDao extends BaseDao{
protected function getModel(): string{
return WithGoodsLog::class;
}
}

View File

@ -0,0 +1,18 @@
<?php
namespace app\common\model\store\product;
use app\common\model\BaseModel;
class WithGoodsLog extends BaseModel{
public static function tablePk(): string{
return 'id';
}
public static function tableName(): string{
return 'with_goods_log';
}
}

View File

@ -2,9 +2,11 @@
namespace app\common\repositories\store\product;
use app\common\dao\store\product\ProductDao;
use app\common\model\store\order\StoreOrderProduct;
use app\common\model\store\product\Product;
use app\common\model\store\product\ProductAttr;
use app\common\model\store\product\ProductAttrValue;
use app\common\model\store\product\WithGoodsLog;
use app\common\repositories\BaseRepository;
use app\common\repositories\store\order\StoreOrderProductRepository;
use app\common\repositories\store\parameter\ParameterValueRepository;
@ -29,10 +31,22 @@ class WineProductRepository extends BaseRepository{
*/
public function stockWithGoodsInit($orderId,$withGoodsMerId){
// 获取全部商品
$orderProductList = app()->make(StoreOrderProductRepository::class)
->getSearch([])
->field(['order_product_id','order_id','product_id','product_sku','product_num','mer_quota_title','mer_quota_other'])
->where('order_id',$orderId)
$orderProductList = (new StoreOrderProduct())
->alias('a')
->leftJoin('StoreProduct sp', 'a.product_id = sp.product_id')
->field([
'a.order_product_id',
'a.order_id',
'a.product_id',
'a.product_sku',
'a.product_num',
'a.mer_quota_title',
'a.mer_quota_other',
'a.total_price',
'a.product_num',
'sp.mer_id',
])
->where('a.order_id',$orderId)
->select()
->toArray();
// 循环处理
@ -46,7 +60,12 @@ class WineProductRepository extends BaseRepository{
if(($productInfo->product_id ?? 0) > 0) {
// 商品已经存在
$this->stockWithGoodsAddStock($item,$productInfo);
// 增加已使用进货额度
$this->addMerQuota((int)$productInfo->product_id, (float)$item['mer_quota_title'], (float)$item['mer_quota_other']);
// 进货记录
$this->addLog((int)$productInfo->product_id, $withGoodsMerId, $item);
}else{
// 商品不存在
$this->stockWithGoodsCopy($item,$withGoodsMerId);
@ -106,9 +125,10 @@ class WineProductRepository extends BaseRepository{
->value('mer_labels');
app()->make(SpuRepository::class)->create($product, $result->product_id, 0, $oldGood['product_type']);
}
// 增加已使用进货额度
$this->addMerQuota((int)$result->product_id, (float)$item['mer_quota_title'], (float)$item['mer_quota_other']);
// 进货记录
$this->addLog((int)$result->product_id, $withGoodsMerId, $item);
});
}
/**
@ -194,6 +214,7 @@ class WineProductRepository extends BaseRepository{
* @param int $productId
* @param float $merQuotaTitle
* @param float $merQuotaOther
* @return bool
*/
public function addMerQuota(int $productId,float $merQuotaTitle,float $merQuotaOther){
$productInfo = app()->make(ProductRepository::class)->getSearch([])->where('product_id',$productId)->findOrEmpty();
@ -204,10 +225,35 @@ class WineProductRepository extends BaseRepository{
if($merQuotaOther > 0) $productInfo->mer_quota_other += $merQuotaOther;
$productInfo->save();
}
return true;
}
/**
* Common: 进货完成 - 记录进货记录
* Author: wu-hui
* Time: 2024/06/11 11:16
* @param $productId
* @param $withGoodsMerId
* @param $item
*/
public function addLog($productId, $withGoodsMerId, $item){
$unique = (new StoreProductAttrValue())
->where('copy_unique', $item['product_sku'])
->where('product_id',$productId)
->value('unique');
WithGoodsLog::insert([
'supplier_mer_id' => $item['mer_id'],
'mer_id' => $withGoodsMerId,
'with_product_id' => $item['product_id'],
'product_id' => $productId,
'with_product_price' => (float)sprintf("%.2f",$item['total_price'] / $item['product_num']),
'with_product_num' => $item['product_num'],
'with_product_sku' => $item['product_sku'],
'product_sku' => $unique,
'mer_quota_title' => $item['mer_quota_title'],
'mer_quota_other' => $item['mer_quota_other'],
]);
}
}

View File

@ -0,0 +1,18 @@
<?php
namespace app\common\repositories\store\product;
use app\common\dao\store\product\WithGoodsLogDao;
use app\common\repositories\BaseRepository;
class WithGoodsLogRepository extends BaseRepository{
public function __construct(WithGoodsLogDao $dao){
$this->dao = $dao;
}
}

View File

@ -3,7 +3,11 @@
namespace app\listener\exchangeQuota;
use app\common\model\store\order\StoreOrderProduct;
use app\common\model\store\product\WithGoodsLog;
use app\common\repositories\store\product\WineProductRepository;
use app\common\repositories\system\merchant\MerchantQuotaRecordRepository;
use app\common\repositories\system\merchant\MerchantQuotaRepository;
use app\common\repositories\system\merchant\MerchantRepository;
use app\common\repositories\user\ExchangeQuotaRecordRepository;
use app\common\repositories\user\ExchangeQuotaRepository;
@ -42,6 +46,8 @@ class OrderTakeEvent{
$this->wineQuotaThawing($order);
// 商户佣金结算
$this->merMoneyHandle($order->order_id);
// 品牌额度返还
$this->returnBrandQuota($order->order_id,$order->mer_id);
}
}catch(\Exception $e){
@ -149,9 +155,119 @@ class OrderTakeEvent{
]);
}
}
// 品牌额度返还
private function returnBrandQuota($order_id, $merId){
// 获取订单商品列表
$orderProductList = (new StoreOrderProduct())
->field(['order_product_id','product_id','product_num','product_sku','order_id'])
->where(['order_id' => $order_id])
->select()
->toArray();
// 循环处理每个商品
$totalTitleReturnQuota = 0;// 本订单 总返还冠名品牌额度
$totalOtherReturnQuota = 0;// 本订单 总返还其他品牌额度
$withProductUpdateData = [];// 进货额度返还信息
foreach($orderProductList as $orderProductInfo){
$productNum = $orderProductInfo['product_num'] ?? 0;// 本订单商品购买数量
// 获取当前商户、当前商品、当前规格进货记录信息;根据是否存在补货额度信息 进行操作
$withGoodsLogList = (new WithGoodsLog())
->field([
'id',
'with_product_price',
'mer_quota_title',
'mer_quota_other',
'return_mer_quota_title',
'return_mer_quota_other',
'product_sku'
])
->where('product_sku', $orderProductInfo['product_sku'])
->whereRaw('mer_quota_title > return_mer_quota_title or mer_quota_other > return_mer_quota_other')
->order('id ASC')
->select()
->toArray();
// 判断:无有效补货额度信息(存在未返还的补货额度)
if(count($withGoodsLogList) <= 0 || $productNum <= 0) continue;
// 循环处理 返还补货额度
$returnQuota = 0;
foreach($withGoodsLogList as $withGoodsInfo){
// 计算剩余未返还补货额度 同时只会存在一种补货额度,因此直接同时计算两种额度
$surplusTitleQuota = (float)sprintf("%.2f",$withGoodsInfo['mer_quota_title'] - $withGoodsInfo['return_mer_quota_title']);// 剩余冠名品牌额度
$surplusOtherQuota = (float)sprintf("%.2f",$withGoodsInfo['mer_quota_other'] - $withGoodsInfo['return_mer_quota_other']);// 剩余其他品牌额度
// 计算:根据剩余购买数量 * 进货单价 = 应返还补货额度
if($returnQuota <= 0) $returnQuota = (float)sprintf("%.2f", $productNum * $withGoodsInfo['with_product_price']);
// 判断:应返还额度类型
if($surplusTitleQuota > 0){
// 冠名品牌额度 返还
$realReturnQuota = $surplusTitleQuota < $returnQuota ? $surplusTitleQuota : $returnQuota;// 实际返还额度
$surplusReturnQuota = (float)sprintf("%.2f", $returnQuota - $realReturnQuota);// 剩余未返还额度
$totalTitleReturnQuota += $realReturnQuota;
// 进货记录修改信息
$withProductUpdateData[] = [
'id' => $withGoodsInfo['id'],
'return_mer_quota_title' => (float)sprintf("%.2f",$withGoodsInfo['return_mer_quota_title'] + $realReturnQuota),
'return_mer_quota_other' => (float)$withGoodsInfo['return_mer_quota_other'],
];
}
else if($surplusOtherQuota > 0){
// 其他品牌额度 返还
$realReturnQuota = $surplusOtherQuota < $returnQuota ? $surplusOtherQuota : $returnQuota;// 实际返还额度
$surplusReturnQuota = (float)sprintf("%.2f", $returnQuota - $realReturnQuota);// 剩余未返还额度
$totalOtherReturnQuota += $realReturnQuota;
$withProductUpdateData[] = [
'id' => $withGoodsInfo['id'],
'return_mer_quota_title' => (float)$withGoodsInfo['return_mer_quota_title'],
'return_mer_quota_other' => (float)sprintf("%.2f",$withGoodsInfo['return_mer_quota_other'] + $realReturnQuota),
];
}
else{
// 额度类型不明确 错误 跳出
continue;
}
// 判断:是否存在剩余未返还额度 存在则计算剩余数量
if($surplusReturnQuota > 0) $returnQuota = (float)$surplusReturnQuota;
else break;
}
}
// 修改进货记录信息
WithGoodsLog::batchUpdate(array_values($withProductUpdateData));
// 额度返还处理
$quotaRecordInsertData = [];
$quotaHoldInfo = app()->make(MerchantQuotaRepository::class)->getQuotaInfo($merId);
if((float)$totalTitleReturnQuota > 0){
// 变更持有信息
$changeFront = (float)sprintf("%.2f", $quotaHoldInfo->title_brand_limit - $quotaHoldInfo->title_brand_used);
$quotaHoldInfo->title_brand_used -= (float)$totalTitleReturnQuota;
//变更记录
$quotaRecordInsertData[] = [
'mer_id' => $merId,
'change_type' => (int)1,
'change_front' => $changeFront,
'change_quantity' => (float)$totalTitleReturnQuota,
'change_after' => (float)sprintf("%.2f", $quotaHoldInfo->title_brand_limit - $quotaHoldInfo->title_brand_used),
'source' => 2,
'order_id' => $order_id,
'quota_type' => 2
];
}
if((float)$totalOtherReturnQuota > 0){
// 变更持有信息
$changeFront = (float)sprintf("%.2f", $quotaHoldInfo->other_brand_limit - $quotaHoldInfo->other_brand_used);
$quotaHoldInfo->other_brand_used -= (float)$totalOtherReturnQuota;
//变更记录
$quotaRecordInsertData[] = [
'mer_id' => $merId,
'change_type' => (int)1,
'change_front' => $changeFront,
'change_quantity' => (float)$totalOtherReturnQuota,
'change_after' => (float)sprintf("%.2f", $quotaHoldInfo->other_brand_limit - $quotaHoldInfo->other_brand_used),
'source' => 2,
'order_id' => $order_id,
'quota_type' => 3
];
}
$quotaHoldInfo->save();
if(count($quotaRecordInsertData) > 0) app()->make(MerchantQuotaRecordRepository::class)->insertAll($quotaRecordInsertData);
}