multi-store/app/common/logic/CommissionLogic.php

221 lines
10 KiB
PHP

<?php
namespace app\common\logic;
use app\common\enum\OrderEnum;
use app\common\model\system_store\SystemStore;
use app\common\model\user\User;
use app\common\model\user\UserAddress;
class CommissionLogic extends BaseLogic
{
/**
* 走村长分润
*/
public static function setVillage($order, $village_uid = 0, $brigade_uid = 0, $transaction_id = 0)
{
$user_1 = self::user($order, 0.05, $transaction_id, $village_uid, 14); //村长
$user_2 = self::user($order, 0.03, $transaction_id, 0, 12); //会员、厨师
$user_3 = self::user($order, 0.01, $transaction_id, $brigade_uid, 15); //队长
$platform = self::platform($order, 0.02, $transaction_id); //平台
$store = self::store($order, 0.05, $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, $attrition, 2), 2);
self::suppliter($order, $moeny, $transaction_id);
}
/**
* 走队长分润
*/
public static function setBrigade($order, $village_uid = 0, $brigade_uid = 0, $transaction_id = 0)
{
$user_1 = self::user($order, 0.05, $transaction_id, $brigade_uid, 15); //队长
$user_2 = self::user($order, 0.03, $transaction_id, 0, 12); ////会员、厨师
$user_3 = self::user($order, 0.01, $transaction_id, $village_uid, 14); //村长
$platform = self::platform($order, 0.02, $transaction_id); //平台
$store = self::store($order, 0.05, $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, $attrition, 2), 2);
self::suppliter($order, $moeny, $transaction_id);
}
/**
* 走厨师分润
*/
public static function setCook($order, $village_uid = 0, $brigade_uid = 0, $transaction_id = 0)
{
$user_1 = self::user($order, 0.07, $transaction_id, $order['spread_uid'], 12); //会员、厨师
$user_2 = self::user($order, 0.01, $transaction_id, $village_uid, 14); //村长
$user_3 = self::user($order, 0.01, $transaction_id, $brigade_uid, 15); //队长
$platform = self::platform($order, 0.02, $transaction_id); //平台
$store = self::store($order, 0.05, $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, $attrition, 2), 2);
self::suppliter($order, $moeny, $transaction_id);
}
/**
* 走线下分润
*/
public static function setStore($order, $transaction_id = 0)
{
$platform = self::platform($order, 0.02, $transaction_id); //平台
$store = self::store($order, 0.05, $transaction_id, 0); //门店
$attrition = self::attrition($order, 0.01, $transaction_id, 16); //损耗
$moeny = bcadd(bcadd($platform, $store, 2), $attrition, 2);
self::suppliter($order, $moeny, $transaction_id);
}
/**
* 平台分润
*/
public static function platform($order, $platformRate, $transaction_id)
{
$financeLogic = new StoreFinanceFlowLogic();
$financeLogic->order = $order;
$financeLogic->user['uid'] = $order['uid'];
$pay_price = $order['pay_price'];
if (isset($order['dealVipAmount']) && $order['dealVipAmount'] > 0) {
$pay_price = bcsub($order['pay_price'], $order['dealVipAmount'], 2);
}
$fees = bcdiv(bcmul($pay_price, $platformRate, 2), 1, 2);
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, $platformRate, $transaction_id)
{
$financeLogic = new StoreFinanceFlowLogic();
$financeLogic->order = $order;
$financeLogic->user['uid'] = $order['uid'];
$pay_price = $order['pay_price'];
if (isset($order['dealVipAmount']) && $order['dealVipAmount'] > 0) {
$pay_price = bcsub($order['pay_price'], $order['dealVipAmount'], 2);
}
$fees = bcsub($pay_price, $platformRate, 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, $platformRate, $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); //保证金剩余额度
$pay_price = $order['pay_price'];
if (isset($order['dealVipAmount']) && $order['dealVipAmount'] > 0) {
$pay_price = bcsub($order['pay_price'], $order['dealVipAmount'], 2);
}
$store_profit = bcmul($pay_price, $platformRate, 2);
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, $userRate, $transaction_id, $uid = 0, $enum = 0)
{
$financeLogic = new StoreFinanceFlowLogic();
$pay_price = $order['pay_price'];
if (isset($order['dealVipAmount']) && $order['dealVipAmount'] > 0) {
$pay_price = bcsub($order['pay_price'], $order['dealVipAmount'], 2);
}
$fees = bcmul($pay_price, $userRate, 2);
if ($fees > 0) {
//记录用户余额收入
if ($uid) {
if(in_array($enum,[14,15])&&$userRate==0.05){
$purchase_funds=User::where('id', $uid)->value('purchase_funds');
if($purchase_funds>0){
$fees_two = bcmul($purchase_funds, $userRate, 2);
if($fees_two<$fees){
$fees=$fees_two;
}
}
}
if($enum==12&&$userRate==0.07){
$purchase_funds=User::where('id', $uid)->value('purchase_funds');
if($purchase_funds>0){
$fees_two = bcmul($purchase_funds, $userRate, 2);
if($fees_two<$fees){
$fees=$fees_two;
}
}
}
$GiveUser = User::where('id', $order['uid'])->find();
$capitalFlowDao = new CapitalFlowLogic($GiveUser);
$capitalFlowDao->userIncome('system_balance_add', 'order', $order['id'], $fees);
}
$financeLogic->user['uid'] = $order['uid'];
$financeLogic->other_arr['vip_uid'] = $uid;
$financeLogic->order = $order;
$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;
}
/**
* 损耗金
*/
public static function attrition($order, $userRate, $transaction_id, $enum)
{
$financeLogic = new StoreFinanceFlowLogic();
$financeLogic->order = $order;
$financeLogic->user['uid'] = $order['uid'];
$pay_price = $order['pay_price'];
if (isset($order['dealVipAmount']) && $order['dealVipAmount'] > 0) {
$pay_price = bcsub($order['pay_price'], $order['dealVipAmount'], 2);
}
$fees = bcmul($pay_price, $userRate, 2);
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;
}
}