feat(OrderLogic, PayNotifyLogic): Update order and payment logic, optimize activity discount processing, adjust profit sharing methods

This commit is contained in:
mkm 2024-08-06 23:03:19 +08:00
parent 962f0055d0
commit 86de33cd04
6 changed files with 434 additions and 2 deletions

View File

@ -107,6 +107,12 @@ class OrderLogic extends BaseLogic
$price = $find['cost'];
} else {
$price = $find['price'];
if(isset($params['store_id']) &&$params['store_id']==getenv('ACTIVITY_STORE_ID')){
$storeBranchPrice=StoreBranchProduct::where('store_id',getenv('ACTIVITY_STORE_ID'))->where('product_id',$v['product_id'])->value('price');
if($storeBranchPrice){
$price = $storeBranchPrice;
}
}
}
if ($off_activity == 0 && $find['top_cate_id'] == 15189 && $user && $user['user_ship'] == 5) {
$price = $find['cost'];
@ -172,7 +178,7 @@ class OrderLogic extends BaseLogic
// } else {
$pay_price = bcsub(self::$pay_price, self::$activity_price, 2); //减去活动优惠金额
$pay_price =self::$pay_price;// bcsub(self::$pay_price, self::$activity_price, 2); //减去活动优惠金额
//判断生鲜是否大于200
if ($createOrder == 1 && self::$fresh_price > 0) {
if (self::$pay_price < 200) {
@ -180,6 +186,9 @@ class OrderLogic extends BaseLogic
return false;
}
}
if(isset($params['store_id']) &&$params['store_id']==getenv('ACTIVITY_STORE_ID')){
$off_activity=1;
}
// }
//成本价 收益
$order = [
@ -196,7 +205,7 @@ class OrderLogic extends BaseLogic
'store_id' => $params['store_id'] ?? 0,
'shipping_type' => $params['shipping_type'] ?? 2, //配送方式 1=快递 2=门店自提
'activity' => '减免',
'activity_price' => self::$activity_price,
'activity_price' => 0,
'activities' => $off_activity,
'deduction_price' => self::$deduction_price, //抵扣金额
'frozen_money' => 0, //self::$frozen_money, //返还金额(活动关闭得时候有)

View File

@ -0,0 +1,159 @@
<?php
namespace app\common\logic;
use app\common\model\distribution\Distribution;
use app\common\model\store_branch_product\StoreBranchProduct;
use app\common\model\store_finance_flow_product\StoreFinanceFlowProduct;
use app\common\model\store_order_cart_info\StoreOrderCartInfo;
use app\common\model\store_product\StoreProduct;
use PDO;
/**
* 产品佣金计算
*
*/
class CommissionProductLogic extends BaseLogic
{
/**
* 根据毛利率计算
*/
function calculate_product_flow($find, $order, $village_uid = 0, $brigade_uid = 0, $user_ship = 0, $spread_user_ship = 0)
{
$product = StoreBranchProduct::where('id', $find['product_id'])->where('store_id', $order['store_id'])->find();
if (!$product) {
$product = StoreProduct::where('id', $find['product_id'])->find();
}
if ($product) {
if ($user_ship == 5) {
$top_cate_id = StoreBranchProduct::where('product_id', $find['product_id'])->value('top_cate_id');
if ($top_cate_id == 15189) {
$this->b($find, $order, $product);
return true;
}
} else {
$this->a($find, $order, $village_uid, $brigade_uid, $user_ship, $product);
}
return true;
}
}
/**
* 零售价结算
*/
public function a($find, $order, $village_uid, $brigade_uid, $user_ship, $product)
{
// $rose = bcdiv($product['rose'], 100, 2);
$total_price = bcmul($product['price'], $find['cart_num']);
// $Distribution = Distribution::where('rate', $rose)->find();
//门店
$data[] = [
'store_id' => $order['store_id'],
'product_id' => $find['product_id'],
'other_uid' => 0,
'number' => bcmul($total_price, 0.05, 2),
'oid' => $order['id'],
'type' => 1,
'status' => 1,
];
//平台
$data[] = [
'store_id' => $order['store_id'],
'product_id' => $find['product_id'],
'other_uid' => 0,
'number' => bcmul($total_price, 0.02, 2),
'oid' => $order['id'],
'type' => 2,
'status' => 1,
];
//村长
$data[] = [
'store_id' => $order['store_id'],
'product_id' => $find['product_id'],
'other_uid' => $village_uid,
'number' => bcmul($total_price, 0.01, 2),
'oid' => $order['id'],
'type' => 3,
'status' => 1,
];
//队长
$data[] = [
'store_id' => $order['store_id'],
'product_id' => $find['product_id'],
'other_uid' => $brigade_uid,
'number' => bcmul($total_price, 0.01, 2),
'oid' => $order['id'],
'type' => 4,
'status' => 1,
];
//会员
if ($user_ship == 1) {
$uid = $order['spread_uid'];
} else {
$uid = 0;
}
// $data[] = [
// 'store_id' => $order['store_id'],
// 'product_id' => $find['product_id'],
// 'other_uid' => $uid,
// 'number' => bcmul($total_price, $Distribution['user'], 2),
// 'oid' => $order['id'],
// 'type' => 0,
// 'status' => 1,
// ];
//个人店铺
if ($order['spread_uid'] > 0) {
$data[] = [
'store_id' => $order['store_id'],
'product_id' => $find['product_id'],
'other_uid' => $order['spread_uid'],
'number' => bcmul($total_price, 0.07, 2),
'oid' => $order['id'],
'type' => 5,
'status' => 1,
];
}
$data[] = [
'store_id' => $order['store_id'],
'product_id' => $find['product_id'],
'other_uid' => 0,
'number' => bcmul($total_price, 0.01, 2),
'oid' => $order['id'],
'type' => 6,
'status' => 1,
];
(new StoreFinanceFlowProduct())->saveAll($data);
}
/**
* 商户价结算
*/
public function b($find, $order, $product)
{
// $rose = bcdiv($product['rose'], 100, 2);
$total_price = bcmul($product['purchase'], $find['cart_num']);
//门店
$data[] = [
'store_id' => $order['store_id'],
'product_id' => $find['product_id'],
'other_uid' => 0,
'number' => bcmul($total_price, 0.05, 2),
'oid' => $order['id'],
'type' => 1,
'status' => 1,
];
//平台
$data[] = [
'store_id' => $order['store_id'],
'product_id' => $find['product_id'],
'other_uid' => 0,
'number' => bcmul($total_price, 0.02, 2),
'oid' => $order['id'],
'type' => 2,
'status' => 1,
];
(new StoreFinanceFlowProduct())->saveAll($data);
}
}

View File

@ -0,0 +1,192 @@
<?php
namespace app\common\logic;
use app\common\enum\OrderEnum;
use app\common\model\store_finance_flow_product\StoreFinanceFlowProduct;
use app\common\model\system_store\SystemStore;
use app\common\model\user\User;
use app\common\model\user\UserAddress;
class CommissionnLogic extends BaseLogic
{
/**
* 走村长分润
*/
public static function setVillage($order, $village_uid = 0, $brigade_uid = 0, $transaction_id = 0)
{
self::user($order, 3, $transaction_id, $village_uid, 14); //村长
self::user($order, 0, $transaction_id, 0, 12); //会员、厨师
self::user($order, 5, $transaction_id, 0, 20); //个人店铺
self::user($order, 4, $transaction_id, $brigade_uid, 15); //队长
self::platform($order, 2, $transaction_id); //平台
self::store($order, 1, $transaction_id, 0); //门店
// $attrition = self::attrition($order, 0.02, $transaction_id, 16); //损耗
self::suppliter($order, $transaction_id);
}
/**
* 走队长分润
*/
public static function setBrigade($order, $village_uid = 0, $brigade_uid = 0, $transaction_id = 0)
{
self::user($order, 4, $transaction_id, $brigade_uid, 15); //队长
self::user($order, 0, $transaction_id, 0, 12); ////会员、厨师
self::user($order, 5, $transaction_id, 0, 20); ////会员、厨师
self::user($order, 3, $transaction_id, $village_uid, 14); //村长
self::platform($order, 2, $transaction_id); //平台
self::store($order, 1, $transaction_id, 0); //门店
// $attrition = self::attrition($order, 0.02, $transaction_id, 16); //损耗
self::suppliter($order, $transaction_id);
}
/**
* 走厨师分润
*/
public static function setCook($order, $village_uid = 0, $brigade_uid = 0, $transaction_id = 0)
{
if ($order['spread_uid'] <= 0) {
$uid = $order['uid'];
} else {
$uid = $order['spread_uid'];
}
self::user($order, 0, $transaction_id, $uid, 12); //会员、厨师
self::user($order, 5, $transaction_id, $uid, 20); //会员、厨师
self::user($order, 3, $transaction_id, $village_uid, 14); //村长
self::user($order, 4, $transaction_id, $brigade_uid, 15); //队长
self::platform($order, 2, $transaction_id); //平台
self::store($order, 1, $transaction_id, 0); //门店
// $attrition = self::attrition($order, 0.02, $transaction_id, 16); //损耗
// $moeny = bcadd(bcadd(bcadd(bcadd($user_1, $user_2, 2), $user_3, 2), $platform, 2), bcadd($store, 0, 2), 2);
self::suppliter($order, $transaction_id);
}
/**
* 零售分润
*/
public static function setStore($order, $village_uid = 0, $brigade_uid = 0, $transaction_id = 0)
{
if ($order['spread_uid'] <= 0) {
$uid = $order['uid'];
} else {
$uid = $order['spread_uid'];
}
self::user($order, 0, $transaction_id, $uid, 12); //会员、厨师
// self::user($order, 5, $transaction_id, $uid, 20); //会员、厨师
self::user($order, 3, $transaction_id, $village_uid, 14); //村长
self::user($order, 4, $transaction_id, $brigade_uid, 15); //队长
self::platform($order, 2, $transaction_id); //平台
self::store($order, 1, $transaction_id, 0); //门店
self::attrition($order, 6, $transaction_id, 16); //损耗金
self::suppliter($order, $transaction_id);
}
/**
* 平台分润
*/
public static function platform($order, $type, $transaction_id)
{
$financeLogic = new StoreFinanceFlowLogic();
$financeLogic->order = $order;
$financeLogic->user['uid'] = $order['uid'];
$fees = StoreFinanceFlowProduct::where('oid', $order['id'])->where('type', $type)->sum('number');
if ($fees > 0) {
$financeLogic->in($transaction_id, $fees, OrderEnum::ORDER_HANDLING_FEES, $order['store_id'], $order['staff_id'], 0, $order['pay_type']); //平台手续费
$financeLogic->out($transaction_id, $fees, OrderEnum::ORDER_HANDLING_FEES, $order['store_id'], $order['staff_id'], 0, $order['pay_type']); //商户平台手续费支出
$financeLogic->save();
}
return $fees;
}
/**
* 供应链订单获得
*/
public static function suppliter($order, $transaction_id)
{
$financeLogic = new StoreFinanceFlowLogic();
$financeLogic->order = $order;
$financeLogic->user['uid'] = $order['uid'];
$pay_price = $order['pay_price'];
$number = StoreFinanceFlowProduct::where('oid', $order['id'])->sum('number');
$fees = bcsub($pay_price, $number, 2);
if ($fees > 0) {
$financeLogic->in($transaction_id, $fees, OrderEnum::SUPPLIER_ORDER_OBTAINS, $order['store_id'], $order['staff_id'], 0, $order['pay_type']);
$financeLogic->out($transaction_id, $fees, OrderEnum::SUPPLIER_ORDER_OBTAINS, $order['store_id'], $order['staff_id'], 0, $order['pay_type']);
$financeLogic->save();
}
}
/**
* 门店分润
*/
public static function store($order, $type, $transaction_id, $uid)
{
$financeLogic = new StoreFinanceFlowLogic();
$financeLogic->user['uid'] = $order['uid'];
$financeLogic->other_arr['vip_uid'] = $uid;
$financeLogic->order = $order;
$financeLogic->in($transaction_id, $order['pay_price'], OrderEnum::USER_ORDER_PAY, $order['store_id'], $order['staff_id'], 0, $order['pay_type']); //用户订单支付
//缴纳齐全了就加商户没有就加到平台
$money_limt = SystemStore::where('id', $order['store_id'])->field('paid_deposit,security_deposit')->find();
$deposit = bcsub($money_limt['security_deposit'], $money_limt['paid_deposit'], 2); //保证金剩余额度
$store_profit = StoreFinanceFlowProduct::where('oid', $order['id'])->where('type', $type)->sum('number');
if ($deposit > 0) {
if ($deposit > $store_profit) {
if ($store_profit > 0) {
$financeLogic->out($transaction_id, $store_profit, OrderEnum::ORDER_MARGIN, $order['store_id'], $order['staff_id'], 0, $order['pay_type']);
$financeLogic->in($transaction_id, 0, OrderEnum::MERCHANT_ORDER_OBTAINS, $order['store_id'], $order['staff_id'], 0, $order['pay_type']); //平台手续费
}
} else {
$money = bcsub($store_profit, $deposit, 2);
if ($deposit > 0) {
$financeLogic->out($transaction_id, $deposit, OrderEnum::ORDER_MARGIN, $order['store_id'], $order['staff_id'], 0, $order['pay_type']);
}
if ($money) {
$financeLogic->in($transaction_id, $money, OrderEnum::MERCHANT_ORDER_OBTAINS, $order['store_id'], $order['staff_id'], 0, $order['pay_type']); //平台手续费
}
}
} else {
if ($store_profit > 0) {
$financeLogic->in($transaction_id, $store_profit, OrderEnum::MERCHANT_ORDER_OBTAINS, $order['store_id'], $order['staff_id'], 0, $order['pay_type']); //平台手续费
}
}
$financeLogic->save();
return $store_profit;
}
/**
* 分给用户
*/
public static function user($order, $type, $transaction_id, $uid = 0, $enum = 0)
{
$financeLogic = new StoreFinanceFlowLogic();
$fees = StoreFinanceFlowProduct::where('oid', $order['id'])->where('type', $type)->field('sum(number) as fees,other_uid')->find();
if ($fees && $fees['fees'] > 0) {
//记录用户余额收入
$financeLogic->user['uid'] = $order['uid'];
$financeLogic->other_arr['vip_uid'] = $fees['other_uid'];
$financeLogic->order = $order;
$financeLogic->in($transaction_id, $fees['fees'], $enum, $order['store_id'], $order['staff_id'], 0, $order['pay_type']);
$financeLogic->out($transaction_id, $fees['fees'], $enum, $order['store_id'], $order['staff_id'], 0, $order['pay_type']);
$financeLogic->save();
}
return $fees['fees'];
}
/**
* 损耗金
*/
public static function attrition($order,$type, $transaction_id, $enum)
{
$financeLogic = new StoreFinanceFlowLogic();
$financeLogic->order = $order;
$financeLogic->user['uid'] = $order['uid'];
$fees = StoreFinanceFlowProduct::where('oid', $order['id'])->where('type', $type)->sum('number');
if ($fees > 0) {
$financeLogic->in($transaction_id, $fees, $enum, $order['store_id'], $order['staff_id'], 0, $order['pay_type']);
$financeLogic->out($transaction_id, $fees, $enum, $order['store_id'], $order['staff_id'], 0, $order['pay_type']);
$financeLogic->save();
}
return $fees;
}
}

View File

@ -652,6 +652,21 @@ class PayNotifyLogic extends BaseLogic
}
}
}
if(isset($user) && $order['store_id']==getenv('ACTIVITY_STORE_ID')&& !in_array($user['user_ship'], [4, 6, 7])){
try{
$info = StoreOrderCartInfo::where('oid', $order['id'])->field('store_id,product_id,cart_num')->select();
$comm = new CommissionProductLogic();
foreach ($info as $k=>$v) {
$comm->calculate_product_flow($v, $order, $village_uid, $brigade_uid,$user_ship);
}
CommissionnLogic::setStore($order,$village_uid, $brigade_uid, $transaction_id);
return true;
}catch (\Exception $e){
Log::error('活动分润报错'.$e->getMessage());
return false;
}
}
switch ($user_ship) {
case 1: // 厨师
//case 4: // 商户
@ -672,6 +687,21 @@ class PayNotifyLogic extends BaseLogic
break;
}
} else {
if(isset($user) && $order['store_id']==getenv('ACTIVITY_STORE_ID')&& !in_array($user['user_ship'], [4, 6, 7])){
try{
$info = StoreOrderCartInfo::where('oid', $order['id'])->field('store_id,product_id,cart_num')->select();
$comm = new CommissionProductLogic();
foreach ($info as $k=>$v) {
$comm->calculate_product_flow($v, $order, $village_uid, $brigade_uid,$user_ship);
}
CommissionnLogic::setStore($order,$village_uid, $brigade_uid, $transaction_id);
return true;
}catch (\Exception $e){
Log::error('活动分润报错'.$e->getMessage());
return false;
}
}
CommissionLogic::setStore($order, $transaction_id);
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace app\common\model\distribution;
use app\common\model\BaseModel;
use think\model\concern\SoftDelete;
/**
* 角色分润规则模型
* Class distribution
* @package app\common\model\order
*/
class Distribution extends BaseModel
{
use SoftDelete;
protected $name = 'distribution';
protected $deleteTime = 'delete_time';
}

View File

@ -0,0 +1,20 @@
<?php
namespace app\common\model\store_finance_flow_product;
use app\common\model\BaseModel;
use think\model\concern\SoftDelete;
/**
* 财务流水商品明细
* Class StoreFinanceFlowProduct
* @package app\common\model\store_finance_flow_product
*/
class StoreFinanceFlowProduct extends BaseModel
{
use SoftDelete;
protected $name = 'store_finance_flow_product';
protected $deleteTime = 'delete_time';
}