初始版本
This commit is contained in:
289
server/app/common/command/SettleOrder.php
Executable file
289
server/app/common/command/SettleOrder.php
Executable file
@@ -0,0 +1,289 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeshop100%开源免费商用商城系统
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | 商业版本务必购买商业授权,以免引起法律纠纷
|
||||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee
|
||||
// | github下载:https://github.com/likeshop-github
|
||||
// | 访问官网:https://www.likeshop.cn
|
||||
// | 访问社区:https://home.likeshop.cn
|
||||
// | 访问手册:http://doc.likeshop.cn
|
||||
// | 微信公众号:likeshop技术社区
|
||||
// | likeshop团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeshopTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\common\command;
|
||||
|
||||
|
||||
use app\common\enum\accountLog\CoachAccountLogEnum;
|
||||
use app\common\enum\accountLog\ShopAccountLogEnum;
|
||||
use app\common\enum\OrderEnum;
|
||||
use app\common\enum\OrderLogEnum;
|
||||
use app\common\enum\PayEnum;
|
||||
use app\common\logic\CoachAccountLogLogic;
|
||||
use app\common\logic\OrderLogLogic;
|
||||
use app\common\logic\ShopAccountLogLogic;
|
||||
use app\common\model\coach\Coach;
|
||||
use app\common\model\order\Order;
|
||||
use app\common\model\settle\Settle;
|
||||
use app\common\model\shop\Shop;
|
||||
use app\common\service\ConfigService;
|
||||
use Exception;
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\Output;
|
||||
use think\facade\Db;
|
||||
use think\facade\Log;
|
||||
|
||||
/**
|
||||
* 结算订单
|
||||
* Class SettleOrder
|
||||
* @package app\common\command
|
||||
*/
|
||||
class SettleOrder extends Command
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('settle_order')
|
||||
->setDescription('结算订单');
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
try {
|
||||
$settleSettingConfig = [
|
||||
//结算方式(包含车费)
|
||||
'commission_settle' => ConfigService::get('settle_setting', 'commission_settle'),
|
||||
//结算周期
|
||||
'commission_settle_cycle' => ConfigService::get('settle_setting', 'commission_settle_cycle'),
|
||||
//结算类型
|
||||
'commission_settle_cycle_type' => ConfigService::get('settle_setting', 'commission_settle_cycle_type'),
|
||||
//结算天
|
||||
'commission_settle_cycle_day' => ConfigService::get('settle_setting', 'commission_settle_cycle_day'),
|
||||
];
|
||||
$whereRaw = '((order_status = '.OrderEnum::ORDER_STATUS_SERVER_FINISH.') or (order_status = '.OrderEnum::ORDER_STATUS_CLOSE.' and total_order_amount > total_refund_amount and pay_status = '.PayEnum::ISPAID.')) and is_settle = 0';
|
||||
$now = time();
|
||||
//结算周期:1-按状态;2-周期
|
||||
if(1 == $settleSettingConfig['commission_settle_cycle']){
|
||||
//订单结束后X天
|
||||
$overTime = $settleSettingConfig['commission_settle_cycle_day'] * 60 * 60 * 24;
|
||||
$whereRaw .= ' and true_server_finish_time + '.$overTime.' < '.$now;
|
||||
}else{
|
||||
//按周期:1-每周、2-每月
|
||||
if( 1== $settleSettingConfig['commission_settle_cycle_type']){
|
||||
$date = date('N');
|
||||
}else{
|
||||
$date = date('j');
|
||||
}
|
||||
if($date != $settleSettingConfig['commission_settle_cycle_day']){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
$lists = Order::whereRaw($whereRaw)
|
||||
->append(['order_goods'])
|
||||
->select()
|
||||
->toArray();
|
||||
if(empty($lists)){
|
||||
return true;
|
||||
}
|
||||
$settleData = [
|
||||
'total_order_amount' => 0,
|
||||
'total_car_amount' => 0,
|
||||
'total_gap_amount' => 0,
|
||||
'total_append_amount' => 0,
|
||||
'total_commission' => 0,
|
||||
'total_car_commission' => 0,
|
||||
'total_car_coach_commission'=> 0,
|
||||
'total_car_shop_commission' => 0,
|
||||
'total_coach_commission' => 0,
|
||||
'total_shop_commission' => 0,
|
||||
'settle_num' => 0,
|
||||
'settle_config' => $settleSettingConfig,
|
||||
];
|
||||
$settleOrderData = [];
|
||||
|
||||
Db::startTrans();
|
||||
$settle = Settle::create($settleData);
|
||||
|
||||
foreach ($lists as $order){
|
||||
$goods = $order['order_goods'][0]->toArray();
|
||||
$commissionRatio = $goods['goods_snap']['commission_ratio'] ?? 0;
|
||||
$shopRatio = $goods['goods_snap']['shop_ratio'] ?? 0;
|
||||
$totalAmount = $order['total_order_amount'];
|
||||
$refundAmount = round($order['total_refund_amount'] - $order['refund_car_amount'],2);
|
||||
$totalAmount = round($totalAmount-$refundAmount-$order['car_amount'],2);
|
||||
$orderCarAmount = round($order['car_amount'] - $order['refund_car_amount'],2);
|
||||
$coachCarAmount = 0;
|
||||
$shopCarAmount = 0;
|
||||
$shopCommission = 0;
|
||||
$coachCommission = 0;
|
||||
//用于判断是否商家商品
|
||||
$goodsShopId = $goods['goods_snap']['shop_id'] ?? $order['shop_id'];
|
||||
if($goodsShopId){
|
||||
$shopRatio = round(100 - $commissionRatio,2);
|
||||
}
|
||||
//技师佣金
|
||||
if($totalAmount > 0 && $commissionRatio > 0){
|
||||
$coachCommission = round($totalAmount * ($commissionRatio / 100),2);
|
||||
}
|
||||
//商家佣金
|
||||
if($totalAmount > 0 && $shopRatio > 0 && $order['shop_id']){
|
||||
$shopCommission = round($totalAmount * ($shopRatio / 100),2);
|
||||
}
|
||||
//如果是包含车费
|
||||
if(1 == $settleSettingConfig['commission_settle']){
|
||||
if($orderCarAmount > 0){
|
||||
if($commissionRatio > 0){
|
||||
$coachCarAmount = round($orderCarAmount * ($commissionRatio / 100),2);
|
||||
}
|
||||
//商家车佣金
|
||||
if($shopRatio > 0 && $order['shop_id']){
|
||||
$shopCarAmount = round($orderCarAmount * ($shopRatio / 100),2);
|
||||
}
|
||||
}
|
||||
//
|
||||
}else{
|
||||
$coachCarAmount = $orderCarAmount;
|
||||
// $coachCommission = round($coachCommission+$coachCarAmount,2);
|
||||
}
|
||||
|
||||
if($coachCommission > 0){
|
||||
Coach::where(['id'=>$order['coach_id']])->update([
|
||||
'money'=> Db::raw('money+'.$coachCommission)
|
||||
]);
|
||||
CoachAccountLogLogic::add(
|
||||
$order['coach_id'],
|
||||
CoachAccountLogEnum::MONEY,
|
||||
CoachAccountLogEnum::ORDER_ADD_MONEY,
|
||||
1,
|
||||
$coachCommission,
|
||||
$order['sn']
|
||||
);
|
||||
|
||||
}
|
||||
//车费技师
|
||||
if($coachCarAmount > 0){
|
||||
Coach::where(['id'=>$order['coach_id']])->update([
|
||||
'money'=> Db::raw('money+'.$coachCarAmount)
|
||||
]);
|
||||
CoachAccountLogLogic::add(
|
||||
$order['coach_id'],
|
||||
CoachAccountLogEnum::MONEY,
|
||||
CoachAccountLogEnum::ORDER_ADD_CART_MONEY,
|
||||
1,
|
||||
$coachCarAmount,
|
||||
$order['sn']
|
||||
);
|
||||
|
||||
}
|
||||
if($shopCommission > 0){
|
||||
Shop::where(['id'=>$order['shop_id']])->update([
|
||||
'money'=> Db::raw('money+'.$shopCommission)
|
||||
]);
|
||||
ShopAccountLogLogic::add(
|
||||
$order['shop_id'],
|
||||
ShopAccountLogEnum::MONEY,
|
||||
ShopAccountLogEnum::ORDER_ADD_MONEY,
|
||||
1,
|
||||
$shopCommission,
|
||||
$order['sn']
|
||||
);
|
||||
}
|
||||
//结算商家车费
|
||||
if($shopCarAmount > 0){
|
||||
Shop::where(['id'=>$order['shop_id']])->update([
|
||||
'money'=> Db::raw('money+'.$shopCarAmount)
|
||||
]);
|
||||
ShopAccountLogLogic::add(
|
||||
$order['shop_id'],
|
||||
ShopAccountLogEnum::MONEY,
|
||||
ShopAccountLogEnum::ORDER_ADD_CART_MONEY,
|
||||
1,
|
||||
$shopCarAmount,
|
||||
$order['sn']
|
||||
);
|
||||
}
|
||||
|
||||
$coachCommission = round($coachCommission+$coachCarAmount,2);
|
||||
$shopCommission = round($shopCommission+$shopCarAmount,2);
|
||||
|
||||
$totalCommissionAmount = round($coachCommission+$shopCommission,2);
|
||||
$totalCarCommissionAmount = round($coachCarAmount+$shopCarAmount,2);
|
||||
//标记已结算
|
||||
Order::where(['id'=>$order['id']])->update([
|
||||
'is_settle' => 1,
|
||||
'settle_coach_amount' => $coachCommission,
|
||||
'settle_shop_amount' => $shopCommission,
|
||||
'settle_commission_amount' => $totalCommissionAmount,
|
||||
'settle_coach_car_amount' => $coachCarAmount,
|
||||
'settle_shop_car_amount' => $shopCarAmount,
|
||||
'settle_car_commission_amount' => $totalCarCommissionAmount,
|
||||
'settle_total_commission_amount' => round($totalCommissionAmount+$totalCarCommissionAmount,2),
|
||||
]);
|
||||
(new OrderLogLogic())
|
||||
->record(OrderLogEnum::TYPE_SYSTEM,OrderLogEnum::SYSTEM_SETTLEMENT_ORDER,$order['id']);
|
||||
//结算订单表
|
||||
$settleOrderData[] = [
|
||||
'order_id' => $order['id'],
|
||||
'settle_id' => $settle['id'],
|
||||
//订单总金额
|
||||
'total_order_amount'=> $order['total_order_amount'],
|
||||
'order_amount' => $totalAmount,
|
||||
//订单车费
|
||||
'car_amount' => $orderCarAmount,
|
||||
//技师车费佣金
|
||||
'coach_car_amount' => $coachCarAmount,
|
||||
//商家车费佣金
|
||||
'shop_car_amount' => $shopCarAmount,
|
||||
//总车费佣金
|
||||
'total_car_amount' => round($coachCarAmount+$shopCarAmount,2),
|
||||
'gap_amount' => $order['total_gap_amount'],
|
||||
'append_amount' => $order['total_append_amount'],
|
||||
//技师佣金
|
||||
'coach_commission' => $coachCommission,
|
||||
//商家佣金
|
||||
'shop_commission' => $shopCommission,
|
||||
//总佣金
|
||||
'total_commission' => $totalCommissionAmount,
|
||||
//技师佣金比例和商家佣金比例
|
||||
'commission_ratio' => $goods['goods_snap']['commission_ratio'] ?? 0,
|
||||
'shop_ratio' => $goods['goods_snap']['shop_ratio'] ?? 0,
|
||||
];
|
||||
//结算表
|
||||
$settleData['total_car_amount'] += $order['car_amount'];
|
||||
$settleData['total_gap_amount'] += $order['total_gap_amount'];
|
||||
$settleData['total_append_amount'] += $order['total_append_amount'];
|
||||
$settleData['total_order_amount'] += $order['total_order_amount'];
|
||||
//总技师和商家佣金,总佣金
|
||||
$settleData['total_coach_commission'] += $coachCommission;
|
||||
$settleData['total_shop_commission'] += $shopCommission;
|
||||
$settleData['total_commission'] += $totalCommissionAmount;
|
||||
//总技师和商家车费佣金,总佣金
|
||||
$settleData['total_car_coach_commission'] += $coachCarAmount;
|
||||
$settleData['total_car_shop_commission'] += $shopCarAmount;
|
||||
$settleData['total_car_commission'] += $totalCarCommissionAmount;
|
||||
|
||||
$settleData['settle_num']++;
|
||||
}
|
||||
Settle::where(['id'=>$settle->id])->update($settleData);
|
||||
(new \app\common\model\settle\SettleOrder())->saveAll($settleOrderData);
|
||||
Db::commit();
|
||||
}catch (Exception $e){
|
||||
Db::rollback();
|
||||
Log::write('结算订单计划任务执行失败:'.implode('-', [
|
||||
__CLASS__,
|
||||
__FUNCTION__,
|
||||
$e->getFile(),
|
||||
$e->getLine(),
|
||||
$e->getMessage()
|
||||
]));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user