multi-store/app/common/logic/CommissionProductLogic.php
mkm 0214b50ca8 feat: 修改了商品列表的查询字段和计算方式,并新增了商品的计价方式、自编码、供应商名称等字段;
fix: 修复了订单退款状态未更新的问题;
refactor: 重构了佣金计算逻辑,调整了部分字段的命名;
style: 调整了代码格式,如空格、分号等;
test: 添加了相关测试;
docs: 更新了相关文档;
build: 更新了依赖;
ops: 更新了部署脚本;
chore: 更新了项目配置文件。
2024-09-09 17:47:08 +08:00

402 lines
13 KiB
PHP

<?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;
use support\Log;
/**
* 产品佣金计算
*
*/
class CommissionProductLogic extends BaseLogic
{
/**
* 根据毛利率计算
*/
function calculate_product_flow($find, $order, $village_uid = 0, $brigade_uid = 0, $user_ship = 0, $spread_user_ship = 0)
{
$product = StoreProduct::where('id', $find['product_id'])->find();
if ($product) {
if ($product['product_type'] == 4) {
$this->c($find, $order, $village_uid, $brigade_uid, $user_ship, $product);
return true;
} else {
if ($user_ship == 5) {
$top_cate_id = $product['top_cate_id'];
if ($top_cate_id == 15189) {
$this->b($find, $order, $product, $user_ship);
return true;
}
} elseif ($user_ship == 0) {
$this->b($find, $order, $product, $user_ship);
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)
{
$total_price = bcmul($product['price'], $find['cart_num']);
$purchase_price = bcmul($product['purchase'], $find['cart_num']);
$price = $product['purchase'];
$brigade_number = bcmul($purchase_price, 0.02, 2); //队长
$village_number = bcmul($brigade_number, 0.1, 2); //村长
$platform_number = bcmul($purchase_price, 0.02, 2); //平台
$attrition_number = bcmul($purchase_price, 0.01, 2); //损耗
$number1 = bcadd($brigade_number, $village_number, 2);
$number2 = bcadd($number1, $platform_number, 2);
//零售-供货价
$number3 = bcsub($total_price, $purchase_price, 2);
$number4 = bcadd($attrition_number, $number2, 2);
//会员
if ($order['spread_uid'] > 0 || $user_ship > 0) {
if (in_array($user_ship, [2, 3])) {
$vip_number = bcmul($purchase_price, 0.05, 2); //会员利润
$data[] = [
'nickname' => '会员',
'store_id' => $order['store_id'],
'product_id' => $find['product_id'],
'price' => $price,
'other_uid' => $order['spread_uid'],
'total_price' => $purchase_price,
'cart_num' => $find['cart_num'],
'rate' => 0.05,
'number' => $vip_number,
'oid' => $order['id'],
'type' => 0,
'status' => 1,
];
$number4=bcadd($number4, $vip_number, 2);
} else {
$vip_number = bcmul($purchase_price, 0.07, 2); //会员利润
$data[] = [
'nickname' => '会员',
'store_id' => $order['store_id'],
'product_id' => $find['product_id'],
'other_uid' => $order['spread_uid'],
'price' => $price,
'total_price' => $purchase_price,
'cart_num' => $find['cart_num'],
'rate' => 0.07,
'number' => $vip_number,
'oid' => $order['id'],
'type' => 0,
'status' => 1,
];
$number4=bcadd($number4, $vip_number, 2);
}
}
//门店利润
if ($number3 <= 0) {
$store_number = 0;
} else {
$store_number = bcsub($number3, $number4, 2);
}
//队长
$data[] = [
'nickname' => '零售队长',
'store_id' => $order['store_id'],
'product_id' => $find['product_id'],
'other_uid' => 0,
'price' => $price,
'total_price' => $purchase_price,
'cart_num' => $find['cart_num'],
'rate' => 0.02,
'number' => $brigade_number,
'oid' => $order['id'],
'type' => 4,
'status' => 1,
'is_activity' => 1,
];
//村长
$data[] = [
'nickname' => '零售村长',
'store_id' => $order['store_id'],
'product_id' => $find['product_id'],
'other_uid' => 0,
'price' => $price,
'total_price' => $brigade_number,
'cart_num' => 0,
'rate' => 0.01,
'number' => $village_number,
'oid' => $order['id'],
'type' => 3,
'status' => 1,
'is_activity' => 1,
];
//门店
$data[] = [
'nickname' => '零售门店',
'store_id' => $order['store_id'],
'product_id' => $find['product_id'],
'other_uid' => 0,
'price' => $price,
'total_price' => $total_price,
'cart_num' => $find['cart_num'],
'rate' => 0,
'number' => $store_number,
'oid' => $order['id'],
'type' => 1,
'status' => 1,
'is_activity' => 1,
];
//平台
$data[] = [
'nickname' => '零售平台',
'store_id' => $order['store_id'],
'product_id' => $find['product_id'],
'other_uid' => 0,
'price' => $price,
'total_price' => $purchase_price,
'cart_num' => $find['cart_num'],
'rate' => 0.02,
'number' => $platform_number,
'oid' => $order['id'],
'type' => 2,
'status' => 1,
'is_activity' => 1,
];
$data[] = [
'nickname' => '零售消耗',
'store_id' => $order['store_id'],
'product_id' => $find['product_id'],
'other_uid' => 0,
'price' => $price,
'total_price' => $purchase_price,
'cart_num' => $find['cart_num'],
'rate' => 0.01,
'number' => $attrition_number,
'oid' => $order['id'],
'type' => 6,
'status' => 1,
];
(new StoreFinanceFlowProduct())->saveAll($data);
}
/**
* 商户价结算
*/
public function b($find, $order, $product, $user_ship)
{
$total_price = bcmul($product['price'], $find['cart_num']);
$purchase_price = bcmul($product['purchase'], $find['cart_num']);
$price = $product['purchase'];
$brigade_number = bcmul($purchase_price, 0.02, 2); //队长
$village_number = bcmul($brigade_number, 0.1, 2); //村长
$platform_number = bcmul($purchase_price, 0.02, 2); //平台
$attrition_number = bcmul($purchase_price, 0.01, 2); //损耗
$number1 = bcadd($brigade_number, $village_number, 2);
$number2 = bcadd($number1, $platform_number, 2);
//零售-供货价
$number3 = bcsub($total_price, $purchase_price, 2);
$number4 = bcadd($attrition_number, $number2, 2);
//门店利润
if ($number3 <= 0) {
$store_number = 0;
} else {
$store_number = bcsub($number3, $number4, 2);
}
//队长
$data[] = [
'nickname' => '商户价队长预留',
'store_id' => $order['store_id'],
'product_id' => $find['product_id'],
'other_uid' => 0,
'price' => $price,
'total_price' => $purchase_price,
'cart_num' => $find['cart_num'],
'rate' => 0.02,
'number' => $brigade_number,
'oid' => $order['id'],
'type' => 4,
'status' => 1,
'is_activity' => 1,
];
//村长
$data[] = [
'nickname' => '商户价村长预留',
'store_id' => $order['store_id'],
'product_id' => $find['product_id'],
'other_uid' => 0,
'price' => $price,
'total_price' => $brigade_number,
'cart_num' => 0,
'rate' => 0.01,
'number' => $village_number,
'oid' => $order['id'],
'type' => 3,
'status' => 1,
'is_activity' => 1,
];
//门店
$data[] = [
'nickname' => '商户价门店',
'store_id' => $order['store_id'],
'product_id' => $find['product_id'],
'other_uid' => 0,
'price' => $price,
'total_price' => $total_price,
'cart_num' => $find['cart_num'],
'rate' => 0,
'number' => $store_number,
'oid' => $order['id'],
'type' => 1,
'status' => 1,
'is_activity' => 1,
];
//平台
$data[] = [
'nickname' => '商户价平台',
'store_id' => $order['store_id'],
'product_id' => $find['product_id'],
'other_uid' => 0,
'price' => $price,
'total_price' => $purchase_price,
'cart_num' => $find['cart_num'],
'rate' => 0.02,
'number' => $platform_number,
'oid' => $order['id'],
'type' => 2,
'status' => 1,
'is_activity' => 1,
];
$data[] = [
'nickname' => '商户价消耗',
'store_id' => $order['store_id'],
'product_id' => $find['product_id'],
'other_uid' => 0,
'price' => $price,
'total_price' => $purchase_price,
'cart_num' => $find['cart_num'],
'rate' => 0.01,
'number' => $attrition_number,
'oid' => $order['id'],
'type' => 6,
'status' => 1,
];
(new StoreFinanceFlowProduct())->saveAll($data);
}
//活动商品结算
public function c($find, $order, $village_uid, $brigade_uid, $user_ship, $product)
{
// $rose = bcdiv($product['rose'], 100, 2);
$total_price = bcmul($product['price'], $find['cart_num']);
$purchase_price = bcmul($product['purchase'], $find['cart_num']);
$price = $product['price'];
$brigade_number = bcmul($total_price, 0.02, 2); //队长
$village_number = bcmul($brigade_number, 0.1, 2); //村长
$platform_number = bcmul($total_price, 0.02, 2); //平台
$number1 = bcadd($brigade_number, $village_number, 2);
$number2 = bcadd($number1, $platform_number, 2);
//零售-供货价
$number3 = bcsub($total_price, $purchase_price, 2);
//门店利润
if ($number3 <= 0) {
$store_number = 0;
} else {
$store_number = bcsub($number3, $number2, 2);
}
//队长
$data[] = [
'nickname' => '活动队长',
'store_id' => $order['store_id'],
'product_id' => $find['product_id'],
'other_uid' => $brigade_uid,
'price' => $price,
'total_price' => $total_price,
'cart_num' => $find['cart_num'],
'rate' => 0.02,
'number' => $brigade_number,
'oid' => $order['id'],
'type' => 4,
'status' => 1,
'is_activity' => 1,
];
//村长
$data[] = [
'nickname' => '活动村长',
'store_id' => $order['store_id'],
'product_id' => $find['product_id'],
'other_uid' => $village_uid,
'price' => $price,
'total_price' => $brigade_number,
'cart_num' => 0,
'rate' => 0.01,
'number' => $village_number,
'oid' => $order['id'],
'type' => 3,
'status' => 1,
'is_activity' => 1,
];
//门店
$data[] = [
'nickname' => '活动门店',
'store_id' => $order['store_id'],
'product_id' => $find['product_id'],
'other_uid' => 0,
'price' => $price,
'total_price' => $store_number,
'cart_num' => $find['cart_num'],
'rate' => 0,
'number' => $store_number,
'oid' => $order['id'],
'type' => 1,
'status' => 1,
'is_activity' => 1,
];
//平台
$data[] = [
'nickname' => '活动平台',
'store_id' => $order['store_id'],
'product_id' => $find['product_id'],
'other_uid' => 0,
'price' => $price,
'total_price' => $platform_number,
'cart_num' => $find['cart_num'],
'rate' => 0.02,
'number' => $platform_number,
'oid' => $order['id'],
'type' => 2,
'status' => 1,
'is_activity' => 1,
];
(new StoreFinanceFlowProduct())->saveAll($data);
}
}