multi-store/app/common/logic/CommissionnLogic.php
mkm 4d883fd749 优化退款流程和佣金逻辑
- 修改了预订单逻辑中的出库操作
- 优化了门店订单的退款处理
- 调整了佣金分配顺序
- 完善了支付通知中的退款流程
2024-10-13 18:02:46 +08:00

154 lines
6.9 KiB
PHP

<?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 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::store($order, 1, $transaction_id, 0); //门店
self::platform($order, 2, $transaction_id); //平台
self::attrition($order, 6, $transaction_id, 16); //损耗金
self::suppliter($order, $transaction_id);
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); //队长
}
/**
* 平台分润
*/
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=bcsub($order['pay_price'],$order['refund_price'],2);
$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, bcsub($order['pay_price'],$order['refund_price'],2), 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;
}
}