ServiceWorkLogic.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\logic\works;
  15. use app\api\logic\PerformanceLogic;
  16. use app\common\enum\worker\WorkerAccountLogEnum;
  17. use app\common\logic\WorkerAccountLogLogic;
  18. use app\common\model\dict\DictData;
  19. use app\common\model\master_worker\MasterWorker;
  20. use app\common\model\master_worker\MasterWorkerAccountLog;
  21. use app\common\model\master_worker\MasterWorkerRetentionMoneyLog;
  22. use app\common\model\orders\OrderEffectiveLog;
  23. use app\common\model\performance\PerformanceRules;
  24. use app\common\model\recharge\OrderGoods;
  25. use app\common\model\recharge\RechargeOrder;
  26. use app\common\model\spare_part\SparePart;
  27. use app\common\model\works\ServiceWork;
  28. use app\common\logic\BaseLogic;
  29. use app\common\model\works\ServiceWorkAllocateWorkerLog;
  30. use app\common\model\works\ServiceWorkAppointmentLog;
  31. use app\common\model\works\ServiceWorkLog;
  32. use app\common\model\works\ServiceWorkSpare;
  33. use app\workerapi\logic\ServiceWorkerAllocateWorkerLogic;
  34. use app\workerapi\logic\ServiceWorkLogLogic;
  35. use think\db\Query;
  36. use think\Exception;
  37. use think\facade\Db;
  38. use think\facade\Log;
  39. /**
  40. * ServiceWork逻辑
  41. * Class ServiceWorkLogic
  42. * @package app\adminapi\logic\works
  43. */
  44. class ServiceWorkLogic extends BaseLogic
  45. {
  46. /**
  47. * @notes 编辑
  48. * @param array $params
  49. * @return bool
  50. * @author likeadmin
  51. * @date 2024/07/10 18:17
  52. */
  53. public static function edit(array $params): bool
  54. {
  55. Db::startTrans();
  56. try {
  57. ServiceWork::where('id', $params['id'])->update([
  58. 'address' => $params['address'],
  59. 'appointment_time' => strtotime($params['appointment_time']),
  60. 'lon' => $params['lon'],
  61. 'lat' => $params['lat'],
  62. ]);
  63. Db::commit();
  64. return true;
  65. } catch (\Exception $e) {
  66. Db::rollback();
  67. self::setError($e->getMessage());
  68. return false;
  69. }
  70. }
  71. /**
  72. *
  73. * @return false|void
  74. */
  75. public static function pickWork($params)
  76. {
  77. Db::startTrans();
  78. try {
  79. $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  80. if($work->isEmpty()){
  81. throw new Exception('工单不存在');
  82. }
  83. $receive_time = time();
  84. $work->work_status = 2;//待联系
  85. $work->service_status = 1;//服务中
  86. $work->receive_time = $receive_time;
  87. $work->save();
  88. //添加变更日志
  89. $work_log = [
  90. 'work_id'=>$work->id,
  91. 'master_worker_id'=>$work->master_worker_id,
  92. 'opera_log'=>'编号['.$params['user_info']['worker_number'].']'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',$receive_time).'领取了工单',
  93. ];
  94. ServiceWorkLogLogic::add($work_log);
  95. Db::commit();
  96. }
  97. catch (\Exception $e) {
  98. Db::rollback();
  99. self::setError($e->getMessage());
  100. return false;
  101. }
  102. }
  103. /**
  104. * 预约成功,等待上门
  105. * @return false|void
  106. */
  107. public static function appointWork($params)
  108. {
  109. Db::startTrans();
  110. try {
  111. $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  112. if($work->isEmpty()){
  113. throw new Exception('工单不存在');
  114. }
  115. if($work->work_status != 2){
  116. throw new Exception('请勿重复点击');
  117. }
  118. //验证更改的预约时间必须是在领单时间内的半小内修改,否则不允许修改
  119. if(strtotime($work->appointment_time) != strtotime($params['appointment_time']) && (time()-strtotime($work->receive_time))>1800){
  120. throw new Exception('距离领单时间已超过半小时,无法修改预约时间,请联系客服');
  121. }
  122. $work->work_status = 3;//待上门
  123. $work->appointment_time = strtotime($params['appointment_time']);
  124. $work->save();
  125. //添加变更日志
  126. $work_log = [
  127. 'work_id'=>$work->id,
  128. 'master_worker_id'=>$work->master_worker_id,
  129. 'opera_log'=>'编号['.$params['user_info']['worker_number'].']'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'联系了客户,确认了于'.$params['appointment_time'].$params['address'].'预约上门',
  130. ];
  131. ServiceWorkLogLogic::add($work_log);
  132. Db::commit();
  133. }
  134. catch (\Exception $e) {
  135. self::setError($e->getMessage());
  136. return false;
  137. }
  138. }
  139. /**
  140. * 工程师确认上门
  141. * @param $params
  142. * @return false|void
  143. */
  144. public static function confirmDoor($params)
  145. {
  146. Db::startTrans();
  147. try {
  148. $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  149. if($work->isEmpty()){
  150. throw new Exception('工单不存在');
  151. }
  152. if($work->work_status != 3){
  153. throw new Exception('请勿重复点击');
  154. }
  155. $work->finally_door_time = time();//最后上门时间
  156. $work->work_status = 4;//已上门
  157. $work->save();
  158. //添加变更日志
  159. $work_log = [
  160. 'work_id'=>$work->id,
  161. 'master_worker_id'=>$work->master_worker_id,
  162. 'opera_log'=>'编号['.$params['user_info']['worker_number'].']'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'已上门',
  163. ];
  164. ServiceWorkLogLogic::add($work_log);
  165. Db::commit();
  166. }
  167. catch (\Exception $e) {
  168. Db::rollback();
  169. self::setError($e->getMessage());
  170. return false;
  171. }
  172. }
  173. /**
  174. * 工程师确认报价单
  175. * @param $params
  176. * @return false|void
  177. */
  178. public static function confirmPrice($params)
  179. {
  180. Db::startTrans();
  181. try {
  182. $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  183. if($work->isEmpty()){
  184. throw new Exception('工单不存在');
  185. }
  186. //搜索待支付订单
  187. $paid_order = RechargeOrder::where(['work_id'=>$work['id'],'pay_status'=>1])->findOrEmpty()->toArray();
  188. if(empty($paid_order)){
  189. throw new Exception('订单错误');
  190. }
  191. if($work->work_status != 4){
  192. throw new Exception('请勿重复操作');
  193. }
  194. // 关联配件信息.计算配件总价 id company_price original_price offering_price number
  195. $spare_total = 0;
  196. isset($params['spare_parts']) && $params['spare_parts'] && $params['spare_parts'] = json_decode($params['spare_parts'], true);
  197. if(isset($params['spare_parts']) && $params['spare_parts']){
  198. $spare_parts = $params['spare_parts'];
  199. foreach ($spare_parts as $spare){
  200. $spare_total += $spare['company_price']*$spare['number'];
  201. }
  202. $work->spare_total = $spare_total;
  203. $service_work_spare = ServiceWorkSpare::where(['service_work_id'=>$work['id']])->findOrEmpty();
  204. if($service_work_spare->isEmpty()){
  205. //新增
  206. $service_work_spare = ServiceWorkSpare::create([
  207. 'service_work_id'=>$work['id'],
  208. 'spare_parts'=>$params['spare_parts'],
  209. 'remark'=>''
  210. ]);
  211. }else{
  212. //修改
  213. $service_work_spare->spare_parts = $params['spare_parts'];
  214. $service_work_spare->save();
  215. }
  216. $work->service_work_spare_id = $service_work_spare->id;
  217. }
  218. // order_amount 原 = $params['amount'] 修改为 = 配件总价 + 服务尾款
  219. $order_amount = $params['amount'] + $spare_total;
  220. //定金存在尾款结算功能,全款直接提交
  221. if($paid_order['payment_type']==1){
  222. $un_order = RechargeOrder::where(['work_id'=>$work['id'],'pay_status'=>0])->findOrEmpty();
  223. if($un_order->isEmpty()){
  224. //新增待支付尾款
  225. $order_data = [
  226. 'order_type'=>$paid_order['order_type'],
  227. 'sn'=>generate_sn(\app\common\model\orders\RechargeOrder::class, 'sn'),
  228. 'work_id'=>$paid_order['work_id'],
  229. 'user_id'=>$paid_order['user_id'],
  230. 'payment_type'=>2,
  231. 'order_total'=>$order_amount,
  232. //'order_amount'=>$params['amount'],
  233. 'order_amount'=>$order_amount,
  234. 'order_terminal'=>$paid_order['order_terminal']
  235. ];
  236. RechargeOrder::create($order_data);
  237. }else{
  238. //修改尾款信息
  239. $un_order->order_total = $order_amount;
  240. //$un_order->order_amount = $params['amount'];
  241. $un_order->order_amount = $order_amount;
  242. $un_order->save();
  243. }
  244. //更新服务费用
  245. $work->service_fee = $paid_order['paid_amount']+$params['amount'];
  246. }
  247. //总工单费用
  248. $work->work_total = $order_amount + $paid_order['paid_amount'];
  249. $work->work_images = $params['work_images'];
  250. $work->user_confirm_status = 1;//待确认报价
  251. $work->price_approval = 0;
  252. $work->save();
  253. //添加变更日志
  254. $work_log = [
  255. 'work_id'=>$work->id,
  256. 'master_worker_id'=>$work->master_worker_id,
  257. 'opera_log'=>'编号['.$params['user_info']['worker_number'].']'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'提交了报价单',
  258. ];
  259. ServiceWorkLogLogic::add($work_log);
  260. Db::commit();
  261. }
  262. catch (\Exception $e) {
  263. Db::rollback();
  264. self::setError($e->getMessage());
  265. return false;
  266. }
  267. }
  268. /**
  269. * 工程师确认服务完成
  270. * @param $params
  271. * @return false|void
  272. */
  273. public static function confirmServiceFinish($params)
  274. {
  275. Db::startTrans();
  276. try {
  277. $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  278. if($work->isEmpty()){
  279. throw new Exception('工单不存在');
  280. }
  281. if($work->user_confirm_status !=2){
  282. throw new Exception('请勿重复操作');
  283. }
  284. $work->finished_images = $params['finished_images'];
  285. $work->user_confirm_status = 3;//待确认服务完成
  286. $work->save();
  287. //添加变更日志
  288. $work_log = [
  289. 'work_id'=>$work->id,
  290. 'master_worker_id'=>$work->master_worker_id,
  291. 'opera_log'=>'编号['.$params['user_info']['worker_number'].']'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'提交了待用户确认服务完成',
  292. ];
  293. ServiceWorkLogLogic::add($work_log);
  294. Db::commit();
  295. } catch (\Exception $e) {
  296. Db::rollback();
  297. self::setError($e->getMessage());
  298. return false;
  299. }
  300. }
  301. public static function allocateWorker($params,$userInfo){
  302. Db::startTrans();
  303. try {
  304. $work = ServiceWork::findOrEmpty($params['id']);
  305. if($work->isEmpty()){
  306. throw new Exception('工单不存在');
  307. }
  308. if($work->work_status >=6 ){
  309. throw new \Exception('工单状态只能修改待结算之前的');
  310. }
  311. if($work->master_worker_id == $params['master_worker_id']){
  312. throw new \Exception('分配的工程师相同');
  313. }
  314. $worker = MasterWorker::where(['id'=>$params['master_worker_id'],'is_disable' =>0])->findOrEmpty();
  315. if($worker->isEmpty()){
  316. throw new \Exception('工程师不存在或被禁用');
  317. }
  318. if($worker->master_worker_id){
  319. MasterWorker::setWorktotal('dec',$worker->master_worker_id);
  320. }
  321. $work->master_worker_id = $params['master_worker_id'];
  322. $work->work_status = 1;
  323. $work->dispatch_time = time();
  324. MasterWorker::setWorktotal('inc',$params['master_worker_id']);
  325. $work->save();
  326. $work_log = [
  327. 'work_id'=>$work->id,
  328. 'master_worker_id'=>$work->master_worker_id,
  329. 'opera_log'=>'后台用户['.$userInfo['admin_id'].']'.$userInfo['name'].'于'.date('Y-m-d H:i:s',time()).'分配了工程师'.'编号['.$worker->worker_number.']'.$worker->real_name
  330. ];
  331. ServiceWorkerAllocateWorkerLogic::add($work_log);
  332. Db::commit();
  333. return true;
  334. }catch(\Exception $e){
  335. Db::rollback();
  336. self::setError($e->getMessage());
  337. return false;
  338. }
  339. }
  340. /**
  341. * 工单详情
  342. * @param $params
  343. * @return array|false
  344. */
  345. public static function detail($params){
  346. $work_where = !empty($params['work_sn'])?['work_sn'=>$params['work_sn']]:['id'=>$params['id']];
  347. $result = ServiceWork::with([
  348. 'worker'=> function(Query $query) {
  349. $query->field('id,worker_number,real_name,mobile');
  350. },
  351. 'allocateWorkerLog' =>function(Query $query){
  352. $query->field('id,work_id,opera_log,create_time');
  353. },
  354. 'serviceWorkLog' =>function(Query $query){
  355. $query->field('id,work_id,opera_log,create_time');
  356. }
  357. ])->append(['id','work_status_text','service_status_text'])
  358. ->where($work_where)
  359. ->findOrEmpty()->toArray();
  360. if(isset($params['user_id']) && $params['user_id'] && ($params['user_id'] != $result['master_worker_id'])){
  361. throw new \Exception('您没有权限操作该工单');
  362. }
  363. //工程师工单按钮状态
  364. $work_service_status = 0;
  365. $work_service_status_text = '待派单';
  366. //工单状态
  367. if($result['work_status'] == 1){
  368. $work_service_status = 1;
  369. $work_service_status_text = '待领单';
  370. }
  371. if($result['work_status'] == 2){
  372. $work_service_status = 2;
  373. $work_service_status_text = '预约上门';
  374. }
  375. if($result['work_status'] == 3){
  376. $work_service_status = 3;
  377. $work_service_status_text = '等待上门';
  378. if(date('Y-m-d') === date('Y-m-d',strtotime($result['appointment_time']))){
  379. $work_service_status = 4;
  380. $work_service_status_text = '确认上门';
  381. }
  382. }
  383. if($result['work_status'] == 4 and $result['user_confirm_status']==0){
  384. $work_service_status = 5;
  385. $work_service_status_text = '确认报价';
  386. }
  387. if($result['work_status'] == 4 and $result['user_confirm_status']==1){
  388. $work_service_status = 6;
  389. $work_service_status_text = '用户确认报价中';
  390. }
  391. if($result['work_status'] == 5 and $result['user_confirm_status']==2){
  392. $work_service_status = 7;
  393. $work_service_status_text = '完成服务';
  394. }
  395. if($result['work_status'] == 5 and $result['user_confirm_status']==3){
  396. $work_service_status = 8;
  397. $work_service_status_text = '用户确认完成服务中';
  398. }
  399. if($result['work_status'] ==6){
  400. $work_service_status = 9;
  401. $work_service_status_text = '待结算';
  402. }
  403. if($result['work_status'] ==7){
  404. $work_service_status = 10;
  405. $work_service_status_text = '已完结';
  406. }
  407. if($result['work_status'] ==8){
  408. $work_service_status = 11;
  409. $work_service_status_text = '已评价';
  410. }
  411. if($result['work_status'] ==9){
  412. $work_service_status = 12;
  413. $work_service_status_text = '已退费';
  414. }
  415. $result['work_service_status'] = $work_service_status;
  416. $result['work_service_status_text'] = $work_service_status_text;
  417. //搜索当前工单下的所有订单记录
  418. $result['pay_orders'] = RechargeOrder::with(['orderGoods'=>function(Query $query){
  419. $query->field('id,sn,goods_id,goods_name,goods_image,goods_number,good_unit,goods_size,goods_payment_type,goods_type,goods_brand,base_service_fee,service_total,service_fee')->order(['id'=>'desc']);
  420. }])->where(['work_id'=>$result['id']])->field('id as order_id,sn,order_type,pay_status,payment_type,pay_way,pay_time,order_amount,order_total,coupon_price,create_time')->order('id asc')->select()->toArray();
  421. $pay_status_data = DictData::where('type_value','pay_status')->column('name','value');
  422. $payment_type_data = DictData::where('type_value','payment_type')->column('name','value');
  423. $pay_way_data = DictData::where('type_value','pay_way')->column('name','value');
  424. $order_type_data = DictData::where('type_value','order_type')->column('name','value');
  425. $coupon_price = 0;
  426. foreach ($result['pay_orders'] as $k=>&$v){
  427. $v['pay_status_name'] = $pay_status_data[$v['pay_status']];
  428. $v['payment_type_name'] = $payment_type_data[$v['payment_type']];
  429. $v['pay_way_name'] = $pay_way_data[$v['pay_way']];
  430. $v['order_type_name'] = $order_type_data[$v['order_type']];
  431. $v['pay_time'] = $v['pay_time'] && is_numeric($v['pay_time']) ? date('Y-m-d H:i:s',$v['pay_time']):'';
  432. if($v['payment_type']!=1 and !empty($result['spare_total'])){
  433. $v['order_total'] = $v['order_total'] - $result['spare_total'];
  434. $v['order_amount'] = $v['order_amount'] - $result['spare_total'];
  435. }
  436. $coupon_price += $v['coupon_price'];
  437. //服务支付类别
  438. if(!empty($v['orderGoods'][0]['goods_payment_type'])){
  439. $goods_payment_type = $v['orderGoods'][0]['goods_payment_type'];
  440. }
  441. }
  442. $result['goods_payment_type'] = !empty($goods_payment_type)?$goods_payment_type:1;
  443. //汇总优惠卷额度
  444. $result['coupon_price'] = $coupon_price;
  445. //工单总支付金额
  446. $result['worker_account'] = $result['work_amount'];
  447. // 配件信息
  448. $result['spare_parts'] = [];
  449. if($result['service_work_spare_id']){
  450. $work_spare_parts = json_decode(ServiceWorkSpare::where('id',$result['service_work_spare_id'])->value('spare_parts'),true);
  451. $spare_parts = SparePart::where('id','in',array_column($work_spare_parts,'id'))
  452. ->field(['id', 'goods_category_id', 'spare_name', 'spare_image', 'spare_number', 'spare_unit','spare_status'])
  453. ->select()
  454. ->toArray();
  455. $spare_parts = array_column($spare_parts,null,'id');
  456. foreach (array_column($work_spare_parts,null,'id') as $k=>&$v){
  457. $spare_parts[$k] = array_merge($spare_parts[$k],$v);
  458. }
  459. $result['spare_parts'] = array_values($spare_parts)??[];
  460. }
  461. // 保修卡信息
  462. $result['order_effectives'] = OrderEffectiveLog::with(['goods'=>function ($query) {
  463. $query->with(['goodsCategory'=>function ($query1) {
  464. $query1->field(['name','picture']);
  465. }]);
  466. }])->where('work_id',$result['id'])
  467. ->field(['id','goods_id','sn','effective_unit','effective_num','remark','end_effective_time'])
  468. ->append(['effective_unit_text'])
  469. ->order('create_time desc')
  470. ->select()
  471. ->toArray();
  472. //查收工程师提成金额
  473. $change_amount = MasterWorkerAccountLog::where(['work_sn'=>$result['work_sn'],'action'=>1])->value('change_amount');
  474. $result['change_amount'] = !empty($change_amount)?$change_amount:0;
  475. //质保金相关金额
  476. $result['retention'] = MasterWorkerRetentionMoneyLog::where('work_id',$result['id'])->visible(['action','amount'])->select()
  477. ->each(function ($item){
  478. $item['amount'] = $item['action'] == 1 ? '+'.$item['amount'] : '-'.$item['amount'];
  479. })->toArray();
  480. return $result;
  481. }
  482. public static function getDetailWorkServiceStatus($params)
  483. {
  484. $result = ServiceWork::where('id',$params['id'])->field('work_status,user_confirm_status,appointment_time,price_approval,appoint_approval')->findOrEmpty()->toArray();
  485. //工程师工单按钮状态
  486. $work_service_status = 0;
  487. //工单状态
  488. if($result['work_status'] == 1){
  489. $work_service_status = 1;
  490. }
  491. if($result['work_status'] == 2){
  492. $work_service_status = 2;
  493. }
  494. if($result['work_status'] == 3){
  495. $work_service_status = 3;
  496. if(date('Y-m-d') === date('Y-m-d',strtotime($result['appointment_time']))){
  497. $work_service_status = 4;
  498. }
  499. }
  500. if($result['work_status'] == 4 and $result['user_confirm_status']==0){
  501. $work_service_status = 5;
  502. }
  503. if($result['work_status'] == 4 and $result['user_confirm_status']==1){
  504. $work_service_status = 6;
  505. }
  506. if($result['work_status'] == 5 and $result['user_confirm_status']==2){
  507. $work_service_status = 7;
  508. }
  509. if($result['work_status'] == 5 and $result['user_confirm_status']==3){
  510. $work_service_status = 8;
  511. }
  512. if($result['work_status'] ==6){
  513. $work_service_status = 9;
  514. }
  515. if($result['work_status'] ==7){
  516. $work_service_status = 10;
  517. }
  518. if($result['work_status'] ==8){
  519. $work_service_status = 11;
  520. }
  521. if($result['work_status'] ==9){
  522. $work_service_status = 12;
  523. }
  524. return ['work_service_status'=>$work_service_status,'price_approval'=>$result['price_approval'],'appoint_approval'=>$result['appoint_approval']];
  525. }
  526. /**
  527. * @notes 取消操作
  528. * @param array $params
  529. * @return bool
  530. * @author likeadmin
  531. * @date 2024/09/19 10:48
  532. */
  533. public static function cancel(array $params): bool
  534. {
  535. Db::startTrans();
  536. try {
  537. // 04-11-12 不做任何限制强制取消,已支付的费用给工程师余额
  538. $serviceWorkInfo = ServiceWork::find($params['id']);
  539. if((int)$serviceWorkInfo['user_confirm_status'] === 5 || (int)$serviceWorkInfo['service_status'] > 2){
  540. throw new \Exception('用户已完结该工单或已取消,已退款,不可执行取消');
  541. }
  542. ServiceWork::where('id', $params['id'])->update([
  543. //'work_status' => 9,
  544. 'service_status' => 4,
  545. 'remark' => $params['remark']??''
  546. ]);
  547. /*// 对应订单状态修改
  548. RechargeOrder::where('work_id', $params['id'])->update([
  549. 'pay_status' => 2
  550. ]);*/
  551. ServiceWorkLog::create([
  552. 'work_id' => $params['id'],
  553. 'master_worker_id' => $serviceWorkInfo['master_worker_id'],
  554. 'opera_log' => "工单:{$serviceWorkInfo['work_sn']}已取消"
  555. ]);
  556. $paid_amount = RechargeOrder::where('work_id', $params['id'])->where('payment_type', 1)->where('order_type', 0)->where('pay_status', 1)->value('paid_amount');
  557. if($paid_amount > 0){
  558. // 4=已上门,5=服务中,6=待结算, 即工程师已上门服务过
  559. if(in_array((int)$serviceWorkInfo['work_status'], [4,5,6])){
  560. WorkerAccountLogLogic::addAccountLog($serviceWorkInfo,$paid_amount,WorkerAccountLogEnum::UM_INC_ADMIN,WorkerAccountLogEnum::INC,$params['remark']??'');
  561. }
  562. }
  563. Db::commit();
  564. Log::info('取消工单'.json_encode([$serviceWorkInfo]));
  565. return true;
  566. } catch (\Exception $e) {
  567. Db::rollback();
  568. self::setError($e->getMessage());
  569. Log::info('取消工单-Error'.json_encode([$e->getMessage()]));
  570. return false;
  571. }
  572. }
  573. /**
  574. * @notes 工程师结算操作
  575. * @param array $params
  576. * @return bool
  577. * @author likeadmin
  578. * @date 2024/09/19 10:48
  579. */
  580. public static function settlement(array $params): bool
  581. {
  582. Db::startTrans();
  583. try {
  584. $serviceWorkInfo = ServiceWork::find($params['id']);
  585. if((int)$serviceWorkInfo['work_pay_status'] !== 3){
  586. throw new \Exception('该工单非待结算状态');
  587. }
  588. //获取工单对应的商品id
  589. $order_sns = \app\common\model\orders\RechargeOrder::where('work_id',$serviceWorkInfo->id)->column('sn');
  590. $goods_id = OrderGoods::whereIn('sn',$order_sns)->value('goods_id');
  591. $rule = PerformanceRules::whereFindInSet('goods_id',$goods_id)->findOrEmpty();
  592. if($rule->isEmpty()){
  593. throw new \Exception('请配置商品业绩规则');
  594. }
  595. PerformanceLogic::calculatePerformance($serviceWorkInfo);
  596. Db::commit();
  597. return true;
  598. } catch (\Exception $e) {
  599. Db::rollback();
  600. self::setError($e->getMessage());
  601. return false;
  602. }
  603. }
  604. /**
  605. * 获取所有改约通知
  606. * @param $userId
  607. * @return array|false
  608. */
  609. public static function getAppointmentNotice($userId)
  610. {
  611. return ServiceWork::where(['master_worker_id'=>$userId,'appoint_approval'=>1])
  612. ->where('work_status','>',1)
  613. ->where('work_status','<',7)
  614. ->field(['id', 'work_sn','real_name','mobile', 'address', 'title', 'appointment_time','work_status'])
  615. ->order(['appointment_time' => 'asc'])//上门时间排序
  616. ->select()->each(function (&$item){
  617. $item['last_appointment_time'] = date('Y-m-d H:i:s',ServiceWorkAppointmentLog::where('work_id',$item['id'])->order('id desc')->value('last_appointment_time'));
  618. })
  619. ->toArray();
  620. }
  621. /**
  622. * @param $params
  623. * @return bool
  624. */
  625. public static function submitAppointment($params)
  626. {
  627. Db::startTrans();
  628. try {
  629. $serviceWork = ServiceWork::where('work_sn',$params['work_sn'])->findOrEmpty();
  630. if($serviceWork->isEmpty()){
  631. throw new \Exception('工单不存在');
  632. }
  633. $serviceWork->appoint_approval = 2;
  634. $serviceWork->save();
  635. Db::commit();
  636. return true;
  637. } catch (\Exception $e) {
  638. Db::rollback();
  639. self::setError($e->getMessage());
  640. return false;
  641. }
  642. }
  643. /**
  644. * @param $params
  645. * @return bool
  646. */
  647. public static function submitChangePrice($params)
  648. {
  649. Db::startTrans();
  650. try {
  651. $serviceWork = ServiceWork::where('work_sn',$params['work_sn'])->findOrEmpty();
  652. if($serviceWork->isEmpty()){
  653. throw new \Exception('工单不存在');
  654. }
  655. $serviceWork->work_status = 4;
  656. $serviceWork->user_confirm_status = 0;
  657. $serviceWork->price_approval = 2;
  658. $serviceWork->save();
  659. Db::commit();
  660. return true;
  661. } catch (\Exception $e) {
  662. Db::rollback();
  663. self::setError($e->getMessage());
  664. return false;
  665. }
  666. }
  667. public static function notApproved($params)
  668. {
  669. Db::startTrans();
  670. try {
  671. $serviceWork = ServiceWork::where('id',$params['id'])->findOrEmpty();
  672. if($serviceWork->isEmpty()){
  673. throw new \Exception('工单不存在');
  674. }
  675. $serviceWork->refund_approval = 3;
  676. $serviceWork->save();
  677. Db::commit();
  678. return true;
  679. } catch (\Exception $e) {
  680. Db::rollback();
  681. self::setError($e->getMessage());
  682. return false;
  683. }
  684. }
  685. public static function againDoor($params)
  686. {
  687. Db::startTrans();
  688. try {
  689. $serviceWork = ServiceWork::where('work_sn',$params['work_sn'])->findOrEmpty();
  690. if($serviceWork->isEmpty()){
  691. throw new \Exception('工单不存在');
  692. }
  693. //更新预约日志
  694. //更新工单未确认上门的状态
  695. $serviceWork->work_status = 3;
  696. $serviceWork->user_confirm_status = 0;
  697. ServiceWorkAppointmentLog::create([
  698. 'work_id'=>$serviceWork->id,
  699. 'last_appointment_time'=>$serviceWork->appointment_time,
  700. 'this_appointment_time'=>strtotime($params['appointment_time']),
  701. ]);
  702. $serviceWork->appointment_time = strtotime($params['appointment_time']);
  703. $serviceWork->save();
  704. Db::commit();
  705. return true;
  706. } catch (\Exception $e) {
  707. Db::rollback();
  708. self::setError($e->getMessage());
  709. return false;
  710. }
  711. }
  712. public static function cancelAllocation($params,$userInfo){
  713. Db::startTrans();
  714. try {
  715. $work = ServiceWork::findOrEmpty($params['id']);
  716. if($work->isEmpty()){
  717. throw new Exception('工单不存在');
  718. }
  719. if($work->work_status >=6 ){
  720. throw new \Exception('工单状态只能修改待结算之前的');
  721. }
  722. $worker = MasterWorker::where(['id'=>$work->master_worker_id])->findOrEmpty();
  723. if($worker->isEmpty()){
  724. throw new \Exception('工程师不存在');
  725. }
  726. MasterWorker::setWorktotal('dec',$work->master_worker_id);
  727. $work->master_worker_id = 0;
  728. $work->work_status = 0;
  729. $work->dispatch_time = 0;
  730. $work->save();
  731. $work_log = [
  732. 'work_id'=>$work->id,
  733. 'master_worker_id'=>$params['master_worker_id'],
  734. 'opera_log'=>'后台用户['.$userInfo['admin_id'].']'.$userInfo['name'].'于'.date('Y-m-d H:i:s',time()).'取消了工程师'.'编号['.$worker->worker_number.']'.$worker->real_name
  735. ];
  736. ServiceWorkerAllocateWorkerLogic::add($work_log);
  737. Db::commit();
  738. return true;
  739. }catch(\Exception $e){
  740. Db::rollback();
  741. dd($e->getMessage());
  742. self::setError($e->getMessage());
  743. return false;
  744. }
  745. }
  746. }