allocatr/application/common/Logic/OrderLogic.php
2025-06-01 22:31:08 +08:00

175 lines
5.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\common\Logic;
use app\admin\controller\orders\Dispatch;
use app\admin\model\Aftersale;
use app\admin\model\Order;
use app\admin\model\OrderDispatch;
use fast\Auth;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\Hook;
class OrderLogic
{
private $OrderModel;
public function __construct()
{
$this->OrderModel = new Order();
}
/**
* 任务完成以后,修改订单
* $roleInfo = ['role'=>1,'auth'=>$userInfo,'remark'='']
*
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @throws Exception
*/
public function dispachFinishAfter(OrderDispatch $orderDispatch,$roleInfo=[],$byAdmin=false): void
{
$order = $this->OrderModel->where('id',$orderDispatch->order_id)->find();
if(!$byAdmin){
if($order->status != Order::STATUS_DISPATCHED){
throw new Exception('订单状态不允许当前操作');
}
}else{
if(in_array($order->status ,[Order::STATUS_FINISHED,Order::STATUS_CANCEL])) {
throw new Exception('订单状态不允许当前操作');
}
}
$orderUpdate = [
'status' => Order::STATUS_CHECKING
];
$orderDispatch->follow = 2; //结束跟进
$orderDispatch->save();
if(!$byAdmin){
if($orderDispatch->is_receipt == 1){ //要收款,计算确认尾款
$offline_amount = $orderDispatch->total;
$total = bcadd($order->online_amount,$offline_amount,2);
$orderUpdate['offline_amount'] = $offline_amount;
$orderUpdate['total'] = $total;
//$orderUpdate['offline_amount_type'] = $orderDispatch->offline_total_type;
}
$order->allowField(true)->save($orderUpdate);
}
$params['role'] = $roleInfo['role'];
$params['auth'] = $roleInfo['auth'];
$params['order']= $order;
$params['remark'] = $roleInfo['remark'] ?? $orderDispatch->remark;
Hook::listen('order_change', $params);
$hookParams = [
'dispatch' => $orderDispatch,
'remark' => $roleInfo['remark'] ?? $orderDispatch->remark,
];
Hook::listen('order_dispatch_change', $hookParams);
}
/**
*
* 师傅超时未接单的处理逻辑
* @return void
*/
public function noWorkerCanGetIt(OrderDispatch $dispatch)
{
$maxNoticeNum = 3;
if($dispatch->notice > $maxNoticeNum){ //超过三次,直接取消
Db::startTrans();
try {
$dispatch->notice_num ++;
$dispatch->notice_time = date('Y-m-d H:i:s');
$remark = '师傅超时未接单,任务取消';
$this->cancelOrderDispatch($dispatch,null,$remark);
Db::commit();
}catch (Exception $exception){
Db::rollback();
$remark = '任务取消异常,请联系技术人员:'.$exception->getMessage();
$dispatch->notice_num ++;
$dispatch->remark = $remark;
$dispatch->notice_time = date('Y-m-d H:i:s');
$dispatch->save();
}
}else{ //未超过最大值,则通知短信通知
$dispatch->notice_num ++;
$dispatch->notice_time = date('Y-m-d H:i:s');
$dispatch->save();
//发送短信通知师傅 todo...
$smsLogic = new NoticeLogic();
}
}
/**
* 取消任务
* @param Dispatch $dispatch
* @return void
*/
public function cancelOrderDispatch(OrderDispatch $dispatch,$auth=null,$remark='',$nocancelOrder=true)
{
//取消
$dispatch->allowField(true)->save(['status' => OrderDispatch::STATUS_CANCEL, 'follow'=>2,'remark' => $remark]);
$hookParams = [
'dispatch' => $dispatch,
'remark' => '后台取消,操作说明:'.$remark?:'无'.',操作人:'.$auth->nickname
];
Hook::listen('order_dispatch_change', $hookParams);
if($nocancelOrder){
$order = Order::where('id',$dispatch->order_id)->where('status',Order::STATUS_DISPATCHED)->find();
$order->worker_id = 0;
$order->save();
if(empty($order)){
throw new Exception('未找到关联订单');
}
//回退订单状态
$order->status = Order::STATUS_DISPATCHING;
$order->save();
$params['order'] = $order;
$params['role'] = 1;
$params['auth'] = $auth;
$params['remark'] = '任务被取消[ID' . $dispatch->id . '],订单状态回退';
if (!empty($remark)) {
$params['remark'] .= ',备注:' . $remark;
}
Hook::listen('order_change', $params);
}
}
public function recacle(Order $order,Aftersale $aftersale)
{
if($aftersale->handle_type == 1)
{
$order->refund_amount = $aftersale->company_refund_amount;
$order->worker_refund_amount = $aftersale->worker_refund_amount;
//平台实际收款要扣减
$order->real_amount = bcsub($order->real_amount,$aftersale->company_refund_amount,2);
//师傅成本要扣减
$order->cost = bcsub($order->cost,$aftersale->worker_refund_amount);
$order->performance = bcsub($order->performance,$aftersale->company_refund_amount,2);
$order->save();
}
}
}