857 lines
34 KiB
PHP
Executable File
857 lines
34 KiB
PHP
Executable File
<?php
|
||
// +----------------------------------------------------------------------
|
||
// | likeshop开源商城系统
|
||
// +----------------------------------------------------------------------
|
||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||
// | 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系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
|
||
// | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
|
||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||
// | likeshop团队版权所有并拥有最终解释权
|
||
// +----------------------------------------------------------------------
|
||
// | author: likeshop.cn.team
|
||
// +----------------------------------------------------------------------
|
||
|
||
namespace app\adminapi\logic\order;
|
||
|
||
|
||
use app\common\enum\notice\NoticeEnum;
|
||
use app\common\enum\OrderEnum;
|
||
use app\common\enum\OrderLogEnum;
|
||
use app\common\enum\OrderRefundEnum;
|
||
use app\common\model\coach\Coach;
|
||
use app\common\model\order\OrderAppend;
|
||
use app\common\model\order\OrderGap;
|
||
use app\common\model\order\OrderRefund;
|
||
use app\common\enum\PayEnum;
|
||
use app\common\logic\BaseLogic;
|
||
use app\common\logic\CoachLogic;
|
||
use app\common\logic\OrderLogLogic;
|
||
use app\common\logic\RefundLogic;
|
||
use app\common\model\order\Order;
|
||
use app\common\model\order\OrderLog;
|
||
use app\common\model\settle\SettleOrder;
|
||
use app\common\model\shop\Shop;
|
||
use app\common\service\FileService;
|
||
use think\Exception;
|
||
use think\facade\Db;
|
||
|
||
class OrderLogic extends BaseLogic
|
||
{
|
||
/**
|
||
* @notes 订单详情
|
||
* @param $id
|
||
* @return array
|
||
* @author ljj
|
||
* @date 2022/2/11 3:01 下午
|
||
*/
|
||
public function detail($id)
|
||
{
|
||
$detail = Order::field('id,sn,shop_id,goods_price,total_order_amount,user_id,order_remarks,order_status,server_finish_time,is_settle,order_terminal,user_remark,pay_status,refund_amount,appoint_time,order_amount,user_remark,address_snap,pay_way,server_finish_time,pay_time,true_server_finish_time,total_amount,total_refund_amount,total_gap_amount,total_append_amount,car_amount,create_time,coach_id,order_distance')
|
||
->order('id','desc')
|
||
->append(['order_distance_desc','pay_way_desc','order_terminal_desc','appoint_time_desc','appoint_time','appoint_date','order_status_desc','take_order_btn','depart_btn','arrive_btn','server_start_btn','server_finish_btn','refund_btn','cancel_btn','order_cancel_time','dispatch_btn'])
|
||
->with(['order_goods' => function($query){
|
||
$query->field('order_id,goods_snap,duration,goods_num,goods_image,goods_name,goods_price')->hidden(['goods_snap']);
|
||
},'order_gap','order_append','order_log','user_info','coach_info'])
|
||
->where(['id'=>$id])
|
||
->findOrEmpty()
|
||
->toArray();
|
||
if(!isset($detail['address_snap']['house_number'])){
|
||
$detail['address_snap']['house_number'] = '';
|
||
}
|
||
$orderLog = OrderLog::where(['order_id'=>$id,'type'=>OrderLogEnum::TYPE_COACH])
|
||
->field('content,location,extra,create_time')
|
||
->select()->toArray();
|
||
$detail['shop_info'] = [];
|
||
if($detail['shop_id']){
|
||
$detail['shop_info'] = Shop::where(['id'=>$detail['shop_id']])->field('id,sn,legal_person,mobile,name,logo')->findOrEmpty();
|
||
}
|
||
$serverLogLists = [];
|
||
foreach ($orderLog as $key => $log){
|
||
$date = date('Y-m-d',strtotime($log['create_time']));
|
||
if($log['extra']){
|
||
foreach ($log['extra'] as $key => $extra){
|
||
$log['extra'][$key] = FileService::getFileUrl($extra);
|
||
}
|
||
}
|
||
$serverLogLists[] = $log;
|
||
}
|
||
// $serverLogLists = $orderLog;
|
||
if ($detail['order_status'] == OrderEnum::ORDER_STATUS_WAIT_RECEIVING) {
|
||
$detail['cancel_btn'] = 1;
|
||
}
|
||
$detail['server_log_lists'] = $serverLogLists;
|
||
$settleOrder = [];
|
||
if($detail['is_settle']){
|
||
$settleOrder = SettleOrder::where(['order_id'=>$detail['id']])->findOrEmpty();
|
||
}
|
||
$shopCommission = $settleOrder['shop_commission'] ?? 0;
|
||
$shopCarAmount = $settleOrder['shop_car_amount'] ?? 0;
|
||
$coachCommission = $settleOrder['coach_commission'] ?? 0;
|
||
$coachCarAmount = $settleOrder['coach_car_amount'] ?? 0;
|
||
// $totalOrderAmount = $settleOrder['order_amount'] ?? 0;
|
||
// $carAmount = $settleOrder['car_amount'] ?? 0;
|
||
$detail['settle_info'] = [
|
||
'status' => $detail['is_settle'],
|
||
'order_amount' => $detail['total_order_amount'],
|
||
'refund_amount' => $detail['total_refund_amount'],
|
||
'settle_car' => $settleOrder['car_amount'] ?? 0,
|
||
'settle_amount' => $settleOrder['order_amount'] ?? 0,
|
||
'coach_settle' => $settleOrder['coach_commission'] ?? 0,
|
||
'shop_settle' => $settleOrder['shop_commission'] ?? 0,
|
||
// 'total_settle_amount'=> round($totalOrderAmount + $carAmount ,2),
|
||
|
||
];
|
||
$refundInfo = OrderRefund::where(['order_id'=>$detail['id'],'refund_status'=>[OrderRefundEnum::STATUS_ING,OrderRefundEnum::STATUS_SUCCESS]])->field('sum(refund_amount) as total_refund_amount,sum(refund_car_amount) as total_refund_car_amount')->findOrEmpty();
|
||
$detail['refund_amount'] = round($refundInfo['total_refund_amount']- $refundInfo['total_refund_car_amount'],2);
|
||
$detail['refund_car_amount'] = $refundInfo['total_refund_car_amount'] ?: 0;
|
||
return $detail;
|
||
}
|
||
|
||
/**
|
||
* @notes 子订单列表
|
||
* @param $id
|
||
* @return array
|
||
* @author cjhao
|
||
* @date 2024/11/9 13:11
|
||
*/
|
||
public function subOrderLists($id)
|
||
{
|
||
|
||
$lists = Order::where(['id'=>$id])
|
||
->field('id,sn,order_amount,refund_amount,refund_car_amount,pay_time')
|
||
->with(['order_gap','order_append'])
|
||
->findOrEmpty();
|
||
$orderGapLists = [];
|
||
$orderAppendLists = [];
|
||
$subLists[] = [
|
||
'id' => $lists['id'],
|
||
'sn' => $lists['sn'],
|
||
'order_amount' => $lists['order_amount'],
|
||
'refund_amount' => $lists['refund_amount'],
|
||
'pay_time_timestamp' => $lists->getData('pay_time'),
|
||
'pay_time' => $lists['pay_time'],
|
||
'type' => 1,
|
||
'type_desc' => '基础',
|
||
'refund_btn' => $lists['order_amount'] > $lists['refund_amount'] ? 1 : 0,
|
||
];
|
||
foreach ($lists['order_gap'] as $key => $gap){
|
||
$gap['pay_time_timestamp'] = $gap->getData('pay_time');
|
||
$gap['type'] = 2;
|
||
$gap['type_desc'] = '补差价';
|
||
$gap['refund_btn'] = $gap['order_amount'] > $gap['refund_amount'] ? 1 : 0;
|
||
$orderGapLists[] = $gap->toArray();
|
||
}
|
||
foreach ($lists['order_append'] as $append){
|
||
$append['pay_time_timestamp'] = $append->getData('pay_time');
|
||
$append['type'] = 3;
|
||
$append['type_desc'] = '加钟';
|
||
$append['refund_btn'] = $append['order_amount'] > $append['refund_amount'] ? 1 : 0;
|
||
$orderAppendLists[] = $append->toArray();
|
||
}
|
||
$subLists = array_merge($subLists,$orderGapLists,$orderAppendLists);
|
||
usort($subLists, function ($item1, $item2) {
|
||
// 从小到大排序
|
||
return $item1['pay_time'] <=>$item2['pay_time'];
|
||
});
|
||
return $subLists;
|
||
}
|
||
/**
|
||
* @notes 取消订单
|
||
* @param $params
|
||
* @return bool|string
|
||
* @author ljj
|
||
* @date 2022/2/11 4:10 下午
|
||
*/
|
||
public function cancel($params)
|
||
{
|
||
// 启动事务
|
||
Db::startTrans();
|
||
try {
|
||
|
||
|
||
//TODO 已支付订单原路退回金额
|
||
$order = Order::where('id',$params['id'])->findOrEmpty()->toArray();
|
||
if(OrderEnum::ORDER_STATUS_WAIT_RECEIVING < $order['order_status'] && $order['order_status'] < OrderEnum::ORDER_STATUS_START_SERVER){
|
||
throw new Exception('订单状态已改变,请刷新页面');
|
||
}
|
||
|
||
if($order['pay_status'] == PayEnum::ISPAID) {
|
||
$orderAmount = round($params['order_amount'] + $params['car_amount']);
|
||
if($orderAmount > $order['order_amount']){
|
||
throw new Exception('退款金额大于订单金额,请重新输入');
|
||
}
|
||
(new RefundLogic())->refund($order,$order['total_order_amount'],0,OrderRefundEnum::TYPE_ADMIN,1,$params['admin_id']);
|
||
}
|
||
//更新订单状态
|
||
Order::update([
|
||
'order_status' => OrderEnum::ORDER_STATUS_CLOSE,
|
||
'cancel_time' => time(),
|
||
],['id'=>$params['id']]);
|
||
|
||
//添加订单日志
|
||
(new OrderLogLogic())->record(OrderLogEnum::TYPE_ADMIN,OrderLogEnum::SYSTEM_CANCEL_ORDER,$params['id'],$params['admin_id']);
|
||
|
||
// 提交事务
|
||
Db::commit();
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
// 回滚事务
|
||
Db::rollback();
|
||
return $e->getMessage();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @notes 删除订单
|
||
* @param $params
|
||
* @author ljj
|
||
* @date 2022/2/11 4:27 下午
|
||
*/
|
||
public function del($params)
|
||
{
|
||
Order::destroy($params['id']);
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* @notes 商家备注
|
||
* @param $params
|
||
* @return bool
|
||
* @author ljj
|
||
* @date 2022/2/11 4:45 下午
|
||
*/
|
||
public function remark($params)
|
||
{
|
||
Order::update(['order_remarks'=>$params['order_remarks'] ?? ''],['id'=>$params['id']]);
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* @notes 商家备注详情
|
||
* @param $id
|
||
* @return array
|
||
* @author ljj
|
||
* @date 2022/2/11 4:56 下午
|
||
*/
|
||
public function remarkDetail($id)
|
||
{
|
||
return Order::where('id',$id)->field('order_remarks')->findOrEmpty()->toArray();
|
||
}
|
||
|
||
/**
|
||
* @notes 核销订单
|
||
* @param $params
|
||
* @return bool|string
|
||
* @author ljj
|
||
* @date 2022/2/11 5:03 下午
|
||
*/
|
||
public function verification($params)
|
||
{
|
||
// 启动事务
|
||
Db::startTrans();
|
||
try {
|
||
//更新订单状态
|
||
Order::update([
|
||
'order_status' => OrderEnum::ORDER_STATUS_SERVER_FINISH,
|
||
'verification_status' => OrderEnum::VERIFICATION,
|
||
'finish_time' => time(),
|
||
],['id'=>$params['id']]);
|
||
|
||
//添加订单日志
|
||
(new OrderLogLogic())->record(OrderLogEnum::TYPE_ADMIN,OrderLogEnum::SHOP_VERIFICATION,$params['id'],$params['admin_id']);
|
||
|
||
$order = Order::where('id',$params['id'])->findOrEmpty()->toArray();
|
||
|
||
// 订单完成通知 - 通知买家
|
||
event('Notice', [
|
||
'scene_id' => NoticeEnum::ORDER_FINISH_NOTICE,
|
||
'params' => [
|
||
'user_id' => $order['user_id'],
|
||
'order_id' => $order['id']
|
||
]
|
||
]);
|
||
|
||
// 提交事务
|
||
Db::commit();
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
// 回滚事务
|
||
Db::rollback();
|
||
return $e->getMessage();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @notes 接单操作
|
||
* @param $params
|
||
* @return bool
|
||
* @author cjhao
|
||
* @date 2024/9/13 17:21
|
||
*/
|
||
public function take($params)
|
||
{
|
||
try {
|
||
Db::startTrans();
|
||
|
||
$order = Order::where(['id'=>$params['id']])
|
||
->findOrEmpty();
|
||
|
||
if($order->isEmpty()){
|
||
throw new Exception('订单不存在');
|
||
}
|
||
if(OrderEnum::ORDER_STATUS_WAIT_RECEIVING != $order->order_status){
|
||
throw new Exception('订单状态已改变,请刷新页面');
|
||
}
|
||
$order->order_status = OrderEnum::ORDER_STATUS_WAIT_DEPART;
|
||
$order->save();
|
||
|
||
//订单日志
|
||
(new OrderLogLogic())->record(OrderLogEnum::TYPE_COACH,OrderLogEnum::COACH_TAKE_ORDER,$order['id'],$params['admin_id']);
|
||
|
||
//提交事务
|
||
Db::commit();
|
||
return true;
|
||
}catch (Exception $e) {
|
||
Db::rollback();
|
||
self::$error = $e->getMessage();
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 出发操作
|
||
* @param $params
|
||
* @return bool
|
||
* @author cjhao
|
||
* @date 2024/9/13 17:21
|
||
*/
|
||
public function depart($params)
|
||
{
|
||
try {
|
||
Db::startTrans();
|
||
|
||
$order = Order::where(['id'=>$params['id']])
|
||
->findOrEmpty();
|
||
if($order->isEmpty()){
|
||
throw new Exception('订单不存在');
|
||
}
|
||
if(OrderEnum::ORDER_STATUS_WAIT_DEPART != $order->order_status){
|
||
throw new Exception('订单状态已改变,请刷新页面');
|
||
}
|
||
$order->order_status = OrderEnum::ORDER_STATUS_DEPART;
|
||
$order->save();
|
||
//订单日志
|
||
(new OrderLogLogic())->record(OrderLogEnum::TYPE_COACH,OrderLogEnum::COACH_DEPART,$order['id'],$params['admin_id']);
|
||
|
||
//提交事务
|
||
Db::commit();
|
||
return true;
|
||
}catch (Exception $e) {
|
||
Db::rollback();
|
||
self::$error = $e->getMessage();
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 达到操作
|
||
* @param $params
|
||
* @return bool
|
||
* @author cjhao
|
||
* @date 2024/9/13 17:21
|
||
*/
|
||
public function arrive($params)
|
||
{
|
||
try {
|
||
Db::startTrans();
|
||
|
||
$order = Order::where(['id'=>$params['id']])
|
||
->findOrEmpty();
|
||
if($order->isEmpty()){
|
||
throw new Exception('订单不存在');
|
||
}
|
||
if(OrderEnum::ORDER_STATUS_DEPART != $order->order_status){
|
||
throw new Exception('订单状态已改变,请刷新页面');
|
||
}
|
||
$order->order_status = OrderEnum::ORDER_STATUS_ARRIVE;
|
||
$order->save();
|
||
|
||
//订单日志
|
||
(new OrderLogLogic())->record(OrderLogEnum::TYPE_COACH,OrderLogEnum::COACH_ARRIVE,$order['id'],$params['admin_id'],'');
|
||
//提交事务
|
||
Db::commit();
|
||
return true;
|
||
}catch (Exception $e) {
|
||
Db::rollback();
|
||
self::$error = $e->getMessage();
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @notes 服务开始操作
|
||
* @param $params
|
||
* @return bool
|
||
* @author cjhao
|
||
* @date 2024/9/13 17:21
|
||
*/
|
||
public function startServer($params)
|
||
{
|
||
try {
|
||
Db::startTrans();
|
||
$order = Order::where(['id'=>$params['id']])
|
||
->findOrEmpty();
|
||
if($order->isEmpty()){
|
||
throw new Exception('订单不存在');
|
||
}
|
||
if(OrderEnum::ORDER_STATUS_ARRIVE != $order->order_status){
|
||
throw new Exception('订单状态已改变,请刷新页面');
|
||
}
|
||
$order->order_status = OrderEnum::ORDER_STATUS_START_SERVER;
|
||
$order->save();
|
||
//订单日志
|
||
(new OrderLogLogic())->record(OrderLogEnum::TYPE_COACH,OrderLogEnum::COACH_START_SERVER,$order['id'],$params['admin_id']);
|
||
|
||
//提交事务
|
||
Db::commit();
|
||
return true;
|
||
}catch (Exception $e) {
|
||
Db::rollback();
|
||
self::$error = $e->getMessage();
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 服务完成
|
||
* @param $params
|
||
* @return bool
|
||
* @author cjhao
|
||
* @date 2024/9/14 14:52
|
||
*/
|
||
public function finishServer($params)
|
||
{
|
||
try {
|
||
Db::startTrans();
|
||
|
||
$order = Order::where(['id'=>$params['id']])
|
||
->findOrEmpty();
|
||
|
||
if($order->isEmpty()){
|
||
throw new Exception('订单不存在');
|
||
}
|
||
if(OrderEnum::ORDER_STATUS_START_SERVER != $order->order_status){
|
||
throw new Exception('订单状态已改变,请刷新页面');
|
||
}
|
||
$order->order_status = OrderEnum::ORDER_STATUS_SERVER_FINISH;
|
||
$order->true_server_finish_time = time();
|
||
$order->save();
|
||
|
||
(new OrderLogLogic())->record(OrderLogEnum::TYPE_ADMIN,OrderLogEnum::COACH_SERVER_FINISH,$order['id'],$params['admin_id'],'');
|
||
Coach::update(['order_num'=>['inc',1]],['id'=>$order['coach_id']]);
|
||
|
||
//提交事务
|
||
Db::commit();
|
||
return true;
|
||
}catch (Exception $e) {
|
||
Db::rollback();
|
||
self::$error = $e->getMessage();
|
||
return false;
|
||
}
|
||
|
||
}
|
||
|
||
/**
|
||
* @notes 订单退款
|
||
* @param $params
|
||
* @return bool
|
||
* @author cjhao
|
||
* @date 2024/9/27 23:54
|
||
*/
|
||
public function refund($params)
|
||
{
|
||
try {
|
||
$allRefund = $params['all_refund'] ?? 0;
|
||
$refundOrder = $params['refund_order'] ?? [];
|
||
if(empty($refundOrder) && empty($allRefund)){
|
||
throw new Exception('请选择退款订单');
|
||
}
|
||
$mainorder = [];
|
||
//全部退款的退款
|
||
if($allRefund){
|
||
$orderId = $params['order_id'] ?? '';
|
||
if(empty($orderId)){
|
||
throw new Exception('请选择退款订单');
|
||
}
|
||
$order = Order::where(['id'=>$orderId])
|
||
->field('id,sn,order_amount,refund_amount,pay_time')
|
||
->with(['order_gap','order_append'])
|
||
->findOrEmpty();
|
||
$mainOrder = $order;
|
||
if($order['order_amount'] > $order['refund_amount']){
|
||
$refundOrder[] = [
|
||
'order_sn' => $order['sn'],
|
||
'type' => 1,
|
||
];
|
||
}
|
||
if($order['order_gap']){
|
||
foreach ($order['order_gap'] as $orderGap) {
|
||
if ($orderGap['refund_amount'] >= $orderGap['order_amount']) {
|
||
continue;
|
||
}
|
||
$refundOrder[] = [
|
||
'order_sn' => $orderGap['sn'],
|
||
'type' => 2,
|
||
];
|
||
}
|
||
}
|
||
if($order['order_append']){
|
||
foreach ($order['order_append'] as $orderAppend) {
|
||
if ($orderAppend['refund_amount'] >= $orderAppend['order_amount']) {
|
||
continue;
|
||
}
|
||
$refundOrder[] = [
|
||
'order_sn' => $orderAppend['sn'],
|
||
'type' => 3,
|
||
];
|
||
}
|
||
}
|
||
if(empty($refundOrder)){
|
||
throw new Exception('订单已全部退款');
|
||
}
|
||
}
|
||
$refundOrderAmount = $params['refund_order_amount'] ?? 0;
|
||
$refundCarAmount = $params['refund_car_amount'] ?? 0;
|
||
$refundCarAmount = $refundCarAmount ?: 0;
|
||
$payWay = $params['pay_way'] ?? 1;
|
||
$refundCount = count($refundOrder);
|
||
$refundTotalOrderAmount = 0;
|
||
if(1 == $refundCount && (empty($refundOrderAmount)) && empty($refundCarAmount)){
|
||
throw new Exception('请输入退款金额');
|
||
}
|
||
//只退单个订单情况
|
||
if(1 == $refundCount){
|
||
Db::startTrans();
|
||
|
||
$type = $refundOrder[0]['type'] ?? '';
|
||
$orderSn = $refundOrder[0]['order_sn'] ?? '';
|
||
if(empty($type) || empty($orderSn)){
|
||
throw new Exception('订单参数错误');
|
||
}
|
||
switch ($type){
|
||
case 1:
|
||
$order = Order::where(['sn'=>$orderSn])->findOrEmpty();
|
||
$mainOrder = $order->toArray();
|
||
break;
|
||
case 2:
|
||
$order = OrderGap::where(['sn'=>$orderSn])->findOrEmpty();
|
||
$mainOrder = Order::where(['id'=>$order['order_id']])->findOrEmpty();;
|
||
break;
|
||
case 3:
|
||
$order = OrderAppend::where(['sn'=>$orderSn])->findOrEmpty();
|
||
$mainOrder = Order::where(['id'=>$order['order_id']])->findOrEmpty();;
|
||
break;
|
||
}
|
||
if($order->isEmpty()){
|
||
throw new Exception('订单不存在');
|
||
}
|
||
$surplusAmount = round($order['order_amount'] - $order['refund_amount'],2);
|
||
if($surplusAmount < $refundOrderAmount){
|
||
throw new Exception('订单可退金额仅剩:'.$surplusAmount);
|
||
}
|
||
if($refundCarAmount > 0){
|
||
$surplusCarAmount = round($order['car_amount'] - $order['refund_car_amount'],2);
|
||
if($surplusCarAmount < $refundCarAmount){
|
||
throw new Exception('订单可退车费仅剩:'.$surplusCarAmount);
|
||
}
|
||
}
|
||
if(2 == $payWay){
|
||
$order['pay_way'] = PayEnum::BALANCE_PAY;
|
||
}
|
||
(new RefundLogic())->refund($order,$refundOrderAmount,$refundCarAmount,OrderRefundEnum::TYPE_ADMIN,$type,$params['admin_id']);
|
||
if(1 == $type){
|
||
$order->total_refund_amount += round($refundOrderAmount+$refundCarAmount,2);
|
||
$order->refund_car_amount += $refundCarAmount;
|
||
$order->refund_amount += round($refundOrderAmount+$refundCarAmount,2);
|
||
$order->save();
|
||
}else{
|
||
$order->refund_amount += $refundOrderAmount;
|
||
$order->save();
|
||
$totalGapRefundAmount = OrderGap::where(['order_id'=>$order['order_id']])->sum('refund_amount');
|
||
$totalAppendRefundAmount = OrderAppend::where(['order_id'=>$order['order_id']])->sum('refund_amount');
|
||
$refundAmount = Order::where(['id'=>$order['order_id']])->value('refund_amount');
|
||
// $totalRefundCarAmount = Order::where(['id'=>$order['order_id']])->value('refund_car_amount');
|
||
Order::update(['total_refund_amount'=>round($totalGapRefundAmount+$totalAppendRefundAmount+$refundAmount,2)],['id'=>$order['order_id']]);
|
||
}
|
||
//未完成的订单,全部变为关闭
|
||
if(OrderEnum::ORDER_STATUS_SERVER_FINISH != $mainOrder['order_status']){
|
||
Order::where(['id'=>$mainOrder['id']])->update([
|
||
'order_status' => OrderEnum::ORDER_STATUS_CLOSE
|
||
]);
|
||
}
|
||
$refundTotalOrderAmount = $refundOrderAmount+$refundCarAmount;
|
||
|
||
Db::commit();
|
||
|
||
}else{
|
||
foreach ($refundOrder as $order) {
|
||
Db::startTrans();
|
||
|
||
$type = $order['type'] ?? '';
|
||
$orderSn = $order['order_sn'] ?? '';
|
||
if(empty($orderSn)){
|
||
throw new Exception('订单数据错误');
|
||
}
|
||
if(empty($type)){
|
||
throw new Exception($orderSn.',订单不存在');
|
||
}
|
||
switch ($type){
|
||
case 1:
|
||
$order = Order::where(['sn'=>$orderSn])->findOrEmpty();
|
||
$mainOrder = $order->toArray();
|
||
break;
|
||
case 2:
|
||
$order = OrderGap::where(['sn'=>$orderSn])->findOrEmpty();
|
||
break;
|
||
case 3:
|
||
$order = OrderAppend::where(['sn'=>$orderSn])->findOrEmpty();
|
||
break;
|
||
}
|
||
if($order->isEmpty()){
|
||
throw new Exception('订单不存在');
|
||
}
|
||
$refundCarAmount = 0;
|
||
if(1 == $type){
|
||
$refundCarAmount = round($order['car_amount'] - $order['refund_car_amount'],2);
|
||
$refundOrderAmount = round($order['order_amount'] - $order['refund_amount'] - $order['refund_car_amount'],2);
|
||
$order->total_refund_amount += $refundOrderAmount+$refundCarAmount;
|
||
$order->refund_car_amount += $refundCarAmount;
|
||
$order->refund_amount += round($refundOrderAmount+$refundCarAmount,2);
|
||
$order->save();
|
||
}else{
|
||
$refundOrderAmount = round($order['order_amount'] - $order['refund_amount'],2);
|
||
$order->refund_amount += $refundOrderAmount;
|
||
$order->save();
|
||
$totalGapRefundAmount = OrderGap::where('refund_amount','>',0)->where(['order_id'=>$order['order_id']])->sum('refund_amount');
|
||
$totalAppendRefundAmount = OrderAppend::where('refund_amount','>',0)->where(['order_id'=>$order['order_id']])->sum('refund_amount');
|
||
$refundAmount = Order::where(['id'=>$order['order_id']])->value('refund_amount');
|
||
// $totalRefundCarAmount = Order::where(['id'=>$order['order_id']])->value('refund_car_amount');
|
||
Order::update(['total_refund_amount'=>round($totalGapRefundAmount+$totalAppendRefundAmount+$refundAmount,2)],['id'=>$order['order_id']]);
|
||
}
|
||
if($refundOrderAmount <= 0 && $refundCarAmount <= 0){
|
||
break;
|
||
}
|
||
|
||
(new RefundLogic())->refund($order,$refundOrderAmount,$refundCarAmount,OrderRefundEnum::TYPE_ADMIN,$type,$params['admin_id']);
|
||
//未完成的订单,全部变为关闭
|
||
if(OrderEnum::ORDER_STATUS_SERVER_FINISH != $mainOrder['order_status']){
|
||
Order::where(['id'=>$mainOrder['id']])->update([
|
||
'order_status' => OrderEnum::ORDER_STATUS_CLOSE
|
||
]);
|
||
}
|
||
$refundTotalOrderAmount += $refundOrderAmount+$refundCarAmount;
|
||
Db::commit();
|
||
}
|
||
|
||
|
||
}
|
||
return true;
|
||
}catch (Exception $e) {
|
||
Db::rollback();
|
||
self::$error = $e->getMessage();
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @notes 订单更换技师时获取技师列表
|
||
* @param $params
|
||
* @return array|string
|
||
* @author cjhao
|
||
* @date 2024/10/9 16:30
|
||
*/
|
||
public function coachLists($params)
|
||
{
|
||
try {
|
||
$order = Order::where(['id'=>$params['id']])->findOrEmpty();
|
||
if(OrderEnum::ORDER_STATUS_WAIT_PAY == $order->order_status || OrderEnum::ORDER_STATUS_DEPART < $order->order_status){
|
||
throw new Exception('当前订单不可更改技师');
|
||
}
|
||
$startTime = $order->getData('appoint_time');
|
||
$serverFinishTime = $order->getData('server_finish_time');
|
||
$coachLists = CoachLogic::getLeisureCoach($order['coach_id'],$startTime,$serverFinishTime,$order);
|
||
return $coachLists;
|
||
}catch (Exception $e){
|
||
self::$error = $e->getMessage();
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @notes 获取退款信息
|
||
* @param $id
|
||
* @return array|string
|
||
* @author cjhao
|
||
* @date 2024/11/21 11:53
|
||
*/
|
||
public function getRefundInfo($params)
|
||
{
|
||
try {
|
||
$id = $params['id'] ?? 0;
|
||
if($id){
|
||
$order = Order::where(['id'=>$id])->field('is_settle,total_amount,order_amount,total_order_amount,car_amount,refund_car_amount,total_refund_amount')->findOrEmpty();
|
||
$surplusAmount = round($order['total_order_amount'] - $order['total_refund_amount'],2);
|
||
$surplusCarAmount = round($order['car_amount'] - $order['refund_car_amount'],2);
|
||
return [
|
||
'total_order_amount' => $order['total_order_amount'],
|
||
'order_amount' => round($order['total_order_amount'] - $order['car_amount'],2),
|
||
'refund_amount' => round($order['total_refund_amount'] - $order['refund_car_amount'],2),
|
||
'surplus_amount' => $surplusAmount,
|
||
'car_amount' => $order['car_amount'],
|
||
'refund_car_amount' => $order['refund_car_amount'],
|
||
'surplus_car_amount' => $surplusCarAmount,
|
||
'is_settle' => $order['is_settle'],
|
||
'type' => 0,
|
||
];
|
||
|
||
}else{
|
||
$refundOrder = $params['refund_order'] ?? [];
|
||
if(empty($refundOrder)){
|
||
throw new Exception('请选择订单');
|
||
}
|
||
$orderAmount = 0;
|
||
$refundAmount = 0;
|
||
$orderCarAmount = 0;
|
||
$surplusCarAmount = 0;
|
||
$refundCarAmount = 0;
|
||
$type = 0;
|
||
if(1 == count($refundOrder) && 1 == $refundOrder[0]['type']){
|
||
$type = 1;
|
||
}
|
||
foreach ($refundOrder as $order){
|
||
$type = $order['type'] ?? '';
|
||
$orderSn = $order['order_sn'] ?? '';
|
||
if(empty($type) || empty($orderSn)){
|
||
continue;
|
||
}
|
||
switch ($type){
|
||
case 1:
|
||
$order = Order::where(['sn'=>$orderSn])->findOrEmpty();
|
||
$isSettle = $order['is_settle'];
|
||
$orderCarAmount = $order['car_amount'];
|
||
$surplusCarAmount = round($order['car_amount'] - $order['refund_car_amount'],2);
|
||
$refundCarAmount = $order['refund_car_amount'];
|
||
$order['order_amount'] = round($order['order_amount'] - $order['car_amount']);
|
||
break;
|
||
|
||
case 2:
|
||
$order = OrderGap::where(['sn'=>$orderSn])->findOrEmpty();
|
||
break;
|
||
case 3:
|
||
$order = OrderAppend::where(['sn'=>$orderSn])->findOrEmpty();
|
||
break;
|
||
}
|
||
$orderAmount+=$order['order_amount'];
|
||
$refundAmount+=round($order['refund_amount'] - $order['refund_car_amount'],2);
|
||
}
|
||
if(!isset($isSettle)){
|
||
$isSettle = Order::where(['id'=>$order['order_id']])->value('is_settle');
|
||
}
|
||
return [
|
||
'total_order_amount' => round($orderAmount+$orderCarAmount,2),
|
||
'order_amount' => floatval($orderAmount),
|
||
'refund_amount' => $refundAmount,
|
||
'surplus_amount' => round($orderAmount - $refundAmount,2),
|
||
'car_amount' => floatval($orderCarAmount),
|
||
'surplus_car_amount' => $surplusCarAmount,
|
||
'refund_car_amount' => floatval($refundCarAmount),
|
||
'is_settle' => $isSettle,
|
||
'type' => $type,
|
||
];
|
||
|
||
|
||
}
|
||
|
||
}catch (Exception $e){
|
||
return $e->getMessage();
|
||
}
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
/**
|
||
* @notes 指派技师
|
||
* @param $params
|
||
* @return bool
|
||
* @author cjhao
|
||
* @date 2024/10/10 23:10
|
||
*/
|
||
public function dispatchCoach($params)
|
||
{
|
||
try {
|
||
Db::startTrans();
|
||
|
||
$order = Order::where(['id'=>$params['id']])
|
||
->findOrEmpty();
|
||
|
||
if(OrderEnum::ORDER_STATUS_WAIT_PAY == $order->order_status || OrderEnum::ORDER_STATUS_DEPART < $order->order_status){
|
||
throw new Exception('当前订单不可更改技师');
|
||
}
|
||
$startTime = $order->getData('appoint_time');
|
||
$serverFinishTime = $order->getData('server_finish_time');
|
||
$coachLists = CoachLogic::getLeisureCoach($order['coach_id'],$startTime,$serverFinishTime,$order);
|
||
$coachIds = array_column($coachLists,'id');
|
||
if(!in_array($params['coach_id'],$coachIds)){
|
||
throw new Exception('该技师该时间段忙');
|
||
}
|
||
//技师更换
|
||
$order->coach_id = $params['coach_id'];
|
||
$order->save();
|
||
$originalCoachId = $order['coach_id'];
|
||
$extra = [
|
||
'original_coach_id' => $originalCoachId,
|
||
'coach_id' => $params['coach_id']
|
||
];
|
||
(new OrderLogLogic())
|
||
->record(OrderLogEnum::TYPE_ADMIN,OrderLogEnum::ADMIN_CHANGE_COACH,$order['id'],$params['admin_id'],'','',$extra);
|
||
Db::commit();
|
||
return true;
|
||
|
||
}catch (Exception $e){
|
||
Db::rollback();
|
||
self::$error = $e->getMessage();
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 获取订单的结算状态
|
||
* @param $params
|
||
* @return mixed
|
||
* @author cjhao
|
||
* @date 2025/4/18 16:27
|
||
*/
|
||
public function getOrderSettle($params)
|
||
{
|
||
$id = $params['id'];
|
||
$order = Order::where(['id'=>$id])->field('id,sn,is_settle')->findOrEmpty();
|
||
return [
|
||
'id' => $order['id'],
|
||
'sn' => $order['sn'],
|
||
'is_settle' => $order['is_settle']
|
||
];
|
||
|
||
}
|
||
} |