190 lines
6.1 KiB
PHP
190 lines
6.1 KiB
PHP
<?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'];
|
||
$auth = clone $roleInfo['auth'];
|
||
$params['order']= $order;
|
||
$params['remark'] = $roleInfo['remark'] ?? $orderDispatch->remark;
|
||
|
||
Hook::listen('order_change', $params);
|
||
|
||
if($roleInfo['role'] == 1){ //后台操作
|
||
$groups = $auth->getGroups($auth->id);
|
||
$groupName = '';
|
||
if(!empty($groups)){
|
||
$groupNames = array_column($groups,'name');
|
||
$groupName = implode(',',$groupNames);
|
||
}
|
||
|
||
$orderDispatch->admin_user = $groupName.':'. $auth->nickname;
|
||
}else{
|
||
$orderDispatch->admin_user = '工程师:'. $roleInfo['auth']->name;
|
||
}
|
||
$hookParams = [
|
||
'dispatch' => $orderDispatch,
|
||
'remark' => $roleInfo['remark'] ?? $orderDispatch->remark,
|
||
];
|
||
Hook::listen('order_dispatch_change', $hookParams);
|
||
}
|
||
|
||
|
||
/**
|
||
*
|
||
* 师傅超时未接单的处理逻辑
|
||
* @return void
|
||
*/
|
||
public function noWorkerCanGetIt(OrderDispatch $dispatch,$remark = null)
|
||
{//超过三次,直接取消
|
||
Db::startTrans();
|
||
try {
|
||
if (is_null($remark)){
|
||
$remark = '工程师('.$dispatch->worker_name.')超时未接单,任务取消';
|
||
}
|
||
$this->cancelOrderDispatch($dispatch,null,$remark);
|
||
Db::commit();
|
||
}catch (Exception $exception){
|
||
Db::rollback();
|
||
$remark = '任务取消异常,请联系技术人员:'.$exception->getMessage();
|
||
$dispatch->remark = $remark;
|
||
$dispatch->save();
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* 取消任务
|
||
* @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]);
|
||
|
||
$auth2 = null;
|
||
if(!empty($auth)){
|
||
|
||
$auth2 = clone $auth;
|
||
|
||
$groups = $auth->getGroups($auth->id);
|
||
$groupName = '';
|
||
if(!empty($groups)){
|
||
$groupNames = array_column($groups,'name');
|
||
$groupName = implode(',',$groupNames);
|
||
}
|
||
$dispatch->admin_user = $groupName.':'.$auth->nickname;
|
||
}else{
|
||
$dispatch->admin_user = config('system_name');
|
||
}
|
||
$hookParams = [
|
||
'dispatch' => $dispatch,
|
||
'remark' => '后台取消,操作说明:'.$remark?:'无'
|
||
];
|
||
Hook::listen('order_dispatch_change', $hookParams);
|
||
|
||
|
||
if($nocancelOrder){
|
||
$order = Order::where('id',$dispatch->order_id)->find();
|
||
$order->worker_id = 0;
|
||
if(empty($order)){
|
||
throw new Exception('未找到关联订单');
|
||
}
|
||
//回退订单状态
|
||
if($order->status != Order::STATUS_DISPATCHING){
|
||
$order->status = Order::STATUS_DISPATCHING;
|
||
$order->save();
|
||
$params['order'] = $order;
|
||
$params['role'] = 1;
|
||
$params['auth'] = $auth2;
|
||
$params['remark'] = '取消任务,订单状态回退';
|
||
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();
|
||
}
|
||
}
|
||
|
||
} |