ServiceWorkLogic.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  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->explanation = $params['explanation']??'';
  251. $work->user_confirm_status = 1;//待确认报价
  252. $work->price_approval = 0;
  253. $work->save();
  254. //添加变更日志
  255. $work_log = [
  256. 'work_id'=>$work->id,
  257. 'master_worker_id'=>$work->master_worker_id,
  258. '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()).'提交了报价单',
  259. ];
  260. ServiceWorkLogLogic::add($work_log);
  261. Db::commit();
  262. }
  263. catch (\Exception $e) {
  264. Db::rollback();
  265. self::setError($e->getMessage());
  266. return false;
  267. }
  268. }
  269. /**
  270. * 工程师确认服务完成
  271. * @param $params
  272. * @return false|void
  273. */
  274. public static function confirmServiceFinish($params)
  275. {
  276. Db::startTrans();
  277. try {
  278. $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  279. if($work->isEmpty()){
  280. throw new Exception('工单不存在');
  281. }
  282. if($work->user_confirm_status !=2){
  283. throw new Exception('请勿重复操作');
  284. }
  285. $work->finished_images = $params['finished_images'];
  286. $work->user_confirm_status = 3;//待确认服务完成
  287. $work->save();
  288. //添加变更日志
  289. $work_log = [
  290. 'work_id'=>$work->id,
  291. 'master_worker_id'=>$work->master_worker_id,
  292. '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()).'提交了待用户确认服务完成',
  293. ];
  294. ServiceWorkLogLogic::add($work_log);
  295. Db::commit();
  296. } catch (\Exception $e) {
  297. Db::rollback();
  298. self::setError($e->getMessage());
  299. return false;
  300. }
  301. }
  302. public static function allocateWorker($params,$userInfo){
  303. Db::startTrans();
  304. try {
  305. $work = ServiceWork::findOrEmpty($params['id']);
  306. if($work->isEmpty()){
  307. throw new Exception('工单不存在');
  308. }
  309. if($work->work_status >=6 ){
  310. throw new \Exception('工单状态只能修改待结算之前的');
  311. }
  312. if($work->master_worker_id == $params['master_worker_id']){
  313. throw new \Exception('分配的工程师相同');
  314. }
  315. $worker = MasterWorker::where(['id'=>$params['master_worker_id'],'is_disable' =>0])->findOrEmpty();
  316. if($worker->isEmpty()){
  317. throw new \Exception('工程师不存在或被禁用');
  318. }
  319. if($worker->master_worker_id){
  320. MasterWorker::setWorktotal('dec',$worker->master_worker_id);
  321. }
  322. $work->master_worker_id = $params['master_worker_id'];
  323. $work->work_status = 1;
  324. $work->dispatch_time = time();
  325. MasterWorker::setWorktotal('inc',$params['master_worker_id']);
  326. $work->save();
  327. $work_log = [
  328. 'work_id'=>$work->id,
  329. 'master_worker_id'=>$work->master_worker_id,
  330. 'opera_log'=>'后台用户['.$userInfo['admin_id'].']'.$userInfo['name'].'于'.date('Y-m-d H:i:s',time()).'分配了工程师'.'编号['.$worker->worker_number.']'.$worker->real_name
  331. ];
  332. ServiceWorkerAllocateWorkerLogic::add($work_log);
  333. Db::commit();
  334. return true;
  335. }catch(\Exception $e){
  336. Db::rollback();
  337. self::setError($e->getMessage());
  338. return false;
  339. }
  340. }
  341. /**
  342. * 工单详情
  343. * @param $params
  344. * @return array|false
  345. */
  346. public static function detail($params){
  347. $work_where = !empty($params['work_sn'])?['work_sn'=>$params['work_sn']]:['id'=>$params['id']];
  348. $result = ServiceWork::with([
  349. 'worker'=> function(Query $query) {
  350. $query->field('id,worker_number,real_name,mobile');
  351. },
  352. 'allocateWorkerLog' =>function(Query $query){
  353. $query->field('id,work_id,opera_log,create_time');
  354. },
  355. 'serviceWorkLog' =>function(Query $query){
  356. $query->field('id,work_id,opera_log,create_time');
  357. }
  358. ])->append(['id','work_status_text','service_status_text'])
  359. ->where($work_where)
  360. ->findOrEmpty()->toArray();
  361. if(isset($params['user_id']) && $params['user_id'] && ($params['user_id'] != $result['master_worker_id'])){
  362. throw new \Exception('您没有权限操作该工单');
  363. }
  364. //工程师工单按钮状态
  365. $work_service_status = 0;
  366. $work_service_status_text = '待派单';
  367. //工单状态
  368. if($result['work_status'] == 1){
  369. $work_service_status = 1;
  370. $work_service_status_text = '待领单';
  371. }
  372. if($result['work_status'] == 2){
  373. $work_service_status = 2;
  374. $work_service_status_text = '预约上门';
  375. }
  376. if($result['work_status'] == 3){
  377. $work_service_status = 3;
  378. $work_service_status_text = '等待上门';
  379. if(date('Y-m-d') === date('Y-m-d',strtotime($result['appointment_time']))){
  380. $work_service_status = 4;
  381. $work_service_status_text = '确认上门';
  382. }
  383. }
  384. if($result['work_status'] == 4 and $result['user_confirm_status']==0){
  385. $work_service_status = 5;
  386. $work_service_status_text = '确认报价';
  387. }
  388. if($result['work_status'] == 4 and $result['user_confirm_status']==1){
  389. $work_service_status = 6;
  390. $work_service_status_text = '用户确认报价中';
  391. }
  392. if($result['work_status'] == 5 and $result['user_confirm_status']==2){
  393. $work_service_status = 7;
  394. $work_service_status_text = '完成服务';
  395. }
  396. if($result['work_status'] == 5 and $result['user_confirm_status']==3){
  397. $work_service_status = 8;
  398. $work_service_status_text = '用户确认完成服务中';
  399. }
  400. if($result['work_status'] ==6){
  401. $work_service_status = 9;
  402. $work_service_status_text = '待结算';
  403. }
  404. if($result['work_status'] ==7){
  405. $work_service_status = 10;
  406. $work_service_status_text = '已完结';
  407. }
  408. if($result['work_status'] ==8){
  409. $work_service_status = 11;
  410. $work_service_status_text = '已评价';
  411. }
  412. if($result['work_status'] ==9){
  413. $work_service_status = 12;
  414. $work_service_status_text = '已退费';
  415. }
  416. $result['work_service_status'] = $work_service_status;
  417. $result['work_service_status_text'] = $work_service_status_text;
  418. //搜索当前工单下的所有订单记录
  419. $result['pay_orders'] = RechargeOrder::with(['orderGoods'=>function(Query $query){
  420. $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']);
  421. }])->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();
  422. $pay_status_data = DictData::where('type_value','pay_status')->column('name','value');
  423. $payment_type_data = DictData::where('type_value','payment_type')->column('name','value');
  424. $pay_way_data = DictData::where('type_value','pay_way')->column('name','value');
  425. $order_type_data = DictData::where('type_value','order_type')->column('name','value');
  426. $coupon_price = 0;
  427. foreach ($result['pay_orders'] as $k=>&$v){
  428. $v['pay_status_name'] = $pay_status_data[$v['pay_status']];
  429. $v['payment_type_name'] = $payment_type_data[$v['payment_type']];
  430. $v['pay_way_name'] = $pay_way_data[$v['pay_way']];
  431. $v['order_type_name'] = $order_type_data[$v['order_type']];
  432. $v['pay_time'] = $v['pay_time'] && is_numeric($v['pay_time']) ? date('Y-m-d H:i:s',$v['pay_time']):'';
  433. if($v['payment_type']!=1 and !empty($result['spare_total'])){
  434. $v['order_total'] = $v['order_total'] - $result['spare_total'];
  435. $v['order_amount'] = $v['order_amount'] - $result['spare_total'];
  436. }
  437. $coupon_price += $v['coupon_price'];
  438. //服务支付类别
  439. if(!empty($v['orderGoods'][0]['goods_payment_type'])){
  440. $goods_payment_type = $v['orderGoods'][0]['goods_payment_type'];
  441. }
  442. }
  443. $result['goods_payment_type'] = !empty($goods_payment_type)?$goods_payment_type:1;
  444. //汇总优惠卷额度
  445. $result['coupon_price'] = $coupon_price;
  446. //工单总支付金额
  447. $result['worker_account'] = $result['work_amount'];
  448. // 配件信息
  449. $result['spare_parts'] = [];
  450. if($result['service_work_spare_id']){
  451. $work_spare_parts = json_decode(ServiceWorkSpare::where('id',$result['service_work_spare_id'])->value('spare_parts'),true);
  452. $spare_parts = SparePart::where('id','in',array_column($work_spare_parts,'id'))
  453. ->field(['id', 'goods_category_id', 'spare_name', 'spare_image', 'spare_number', 'spare_unit','spare_status'])
  454. ->select()
  455. ->toArray();
  456. $spare_parts = array_column($spare_parts,null,'id');
  457. foreach (array_column($work_spare_parts,null,'id') as $k=>&$v){
  458. $spare_parts[$k] = array_merge($spare_parts[$k],$v);
  459. }
  460. $result['spare_parts'] = array_values($spare_parts)??[];
  461. }
  462. // 保修卡信息
  463. $result['order_effectives'] = OrderEffectiveLog::with(['goods'=>function ($query) {
  464. $query->with(['goodsCategory'=>function ($query1) {
  465. $query1->field(['name','picture']);
  466. }]);
  467. }])->where('work_id',$result['id'])
  468. ->field(['id','goods_id','sn','effective_unit','effective_num','remark','end_effective_time'])
  469. ->append(['effective_unit_text'])
  470. ->order('create_time desc')
  471. ->select()
  472. ->toArray();
  473. //查收工程师提成金额
  474. $change_amount = MasterWorkerAccountLog::where(['work_sn'=>$result['work_sn'],'action'=>1])->value('change_amount');
  475. $result['change_amount'] = !empty($change_amount)?$change_amount:0;
  476. //质保金相关金额
  477. $result['retention'] = MasterWorkerRetentionMoneyLog::where('work_id',$result['id'])->visible(['action','amount'])->select()
  478. ->each(function ($item){
  479. $item['amount'] = $item['action'] == 1 ? '+'.$item['amount'] : '-'.$item['amount'];
  480. })->toArray();
  481. return $result;
  482. }
  483. public static function getDetailWorkServiceStatus($params)
  484. {
  485. $result = ServiceWork::where('id',$params['id'])->field('work_status,user_confirm_status,appointment_time,price_approval,appoint_approval')->findOrEmpty()->toArray();
  486. //工程师工单按钮状态
  487. $work_service_status = 0;
  488. //工单状态
  489. if($result['work_status'] == 1){
  490. $work_service_status = 1;
  491. }
  492. if($result['work_status'] == 2){
  493. $work_service_status = 2;
  494. }
  495. if($result['work_status'] == 3){
  496. $work_service_status = 3;
  497. if(date('Y-m-d') === date('Y-m-d',strtotime($result['appointment_time']))){
  498. $work_service_status = 4;
  499. }
  500. }
  501. if($result['work_status'] == 4 and $result['user_confirm_status']==0){
  502. $work_service_status = 5;
  503. }
  504. if($result['work_status'] == 4 and $result['user_confirm_status']==1){
  505. $work_service_status = 6;
  506. }
  507. if($result['work_status'] == 5 and $result['user_confirm_status']==2){
  508. $work_service_status = 7;
  509. }
  510. if($result['work_status'] == 5 and $result['user_confirm_status']==3){
  511. $work_service_status = 8;
  512. }
  513. if($result['work_status'] ==6){
  514. $work_service_status = 9;
  515. }
  516. if($result['work_status'] ==7){
  517. $work_service_status = 10;
  518. }
  519. if($result['work_status'] ==8){
  520. $work_service_status = 11;
  521. }
  522. if($result['work_status'] ==9){
  523. $work_service_status = 12;
  524. }
  525. return ['work_service_status'=>$work_service_status,'price_approval'=>$result['price_approval'],'appoint_approval'=>$result['appoint_approval']];
  526. }
  527. /**
  528. * @notes 取消操作
  529. * @param array $params
  530. * @return bool
  531. * @author likeadmin
  532. * @date 2024/09/19 10:48
  533. */
  534. public static function cancel(array $params): bool
  535. {
  536. Db::startTrans();
  537. try {
  538. // 04-11-12 不做任何限制强制取消,已支付的费用给工程师余额
  539. $serviceWorkInfo = ServiceWork::find($params['id']);
  540. if((int)$serviceWorkInfo['user_confirm_status'] === 5 || (int)$serviceWorkInfo['service_status'] > 2){
  541. throw new \Exception('用户已完结该工单或已取消,已退款,不可执行取消');
  542. }
  543. ServiceWork::where('id', $params['id'])->update([
  544. //'work_status' => 9,
  545. 'service_status' => 4,
  546. 'remark' => $params['remark']??''
  547. ]);
  548. /*// 对应订单状态修改
  549. RechargeOrder::where('work_id', $params['id'])->update([
  550. 'pay_status' => 2
  551. ]);*/
  552. ServiceWorkLog::create([
  553. 'work_id' => $params['id'],
  554. 'master_worker_id' => $serviceWorkInfo['master_worker_id'],
  555. 'opera_log' => "工单:{$serviceWorkInfo['work_sn']}已取消"
  556. ]);
  557. $paid_amount = RechargeOrder::where('work_id', $params['id'])->where('payment_type', 1)->where('order_type', 0)->where('pay_status', 1)->value('paid_amount');
  558. if($paid_amount > 0){
  559. // 4=已上门,5=服务中,6=待结算, 即工程师已上门服务过
  560. if(in_array((int)$serviceWorkInfo['work_status'], [4,5,6])){
  561. WorkerAccountLogLogic::addAccountLog($serviceWorkInfo,$paid_amount,WorkerAccountLogEnum::UM_INC_ADMIN,WorkerAccountLogEnum::INC,$params['remark']??'');
  562. }
  563. }
  564. Db::commit();
  565. Log::info('取消工单'.json_encode([$serviceWorkInfo]));
  566. return true;
  567. } catch (\Exception $e) {
  568. Db::rollback();
  569. self::setError($e->getMessage());
  570. Log::info('取消工单-Error'.json_encode([$e->getMessage()]));
  571. return false;
  572. }
  573. }
  574. /**
  575. * @notes 工程师结算操作
  576. * @param array $params
  577. * @return bool
  578. * @author likeadmin
  579. * @date 2024/09/19 10:48
  580. */
  581. public static function settlement(array $params): bool
  582. {
  583. Db::startTrans();
  584. try {
  585. $serviceWorkInfo = ServiceWork::find($params['id']);
  586. if((int)$serviceWorkInfo['work_pay_status'] !== 3){
  587. throw new \Exception('该工单非待结算状态');
  588. }
  589. //获取工单对应的商品id
  590. $order_sns = \app\common\model\orders\RechargeOrder::where('work_id',$serviceWorkInfo->id)->column('sn');
  591. $goods_id = OrderGoods::whereIn('sn',$order_sns)->value('goods_id');
  592. $rule = PerformanceRules::whereFindInSet('goods_id',$goods_id)->findOrEmpty();
  593. if($rule->isEmpty()){
  594. throw new \Exception('请配置商品业绩规则');
  595. }
  596. PerformanceLogic::calculatePerformance($serviceWorkInfo);
  597. Db::commit();
  598. return true;
  599. } catch (\Exception $e) {
  600. Db::rollback();
  601. self::setError($e->getMessage());
  602. return false;
  603. }
  604. }
  605. /**
  606. * 获取所有改约通知
  607. * @param $userId
  608. * @return array|false
  609. */
  610. public static function getAppointmentNotice($userId)
  611. {
  612. return ServiceWork::where(['master_worker_id'=>$userId,'appoint_approval'=>1])
  613. ->where('work_status','>',1)
  614. ->where('work_status','<',7)
  615. ->field(['id', 'work_sn','real_name','mobile', 'address', 'title', 'appointment_time','work_status'])
  616. ->order(['appointment_time' => 'asc'])//上门时间排序
  617. ->select()->each(function (&$item){
  618. $item['last_appointment_time'] = date('Y-m-d H:i:s',ServiceWorkAppointmentLog::where('work_id',$item['id'])->order('id desc')->value('last_appointment_time'));
  619. })
  620. ->toArray();
  621. }
  622. /**
  623. * @param $params
  624. * @return bool
  625. */
  626. public static function submitAppointment($params)
  627. {
  628. Db::startTrans();
  629. try {
  630. $serviceWork = ServiceWork::where('work_sn',$params['work_sn'])->findOrEmpty();
  631. if($serviceWork->isEmpty()){
  632. throw new \Exception('工单不存在');
  633. }
  634. $serviceWork->appoint_approval = 2;
  635. $serviceWork->save();
  636. Db::commit();
  637. return true;
  638. } catch (\Exception $e) {
  639. Db::rollback();
  640. self::setError($e->getMessage());
  641. return false;
  642. }
  643. }
  644. /**
  645. * @param $params
  646. * @return bool
  647. */
  648. public static function submitChangePrice($params)
  649. {
  650. Db::startTrans();
  651. try {
  652. $serviceWork = ServiceWork::where('work_sn',$params['work_sn'])->findOrEmpty();
  653. if($serviceWork->isEmpty()){
  654. throw new \Exception('工单不存在');
  655. }
  656. $serviceWork->work_status = 4;
  657. $serviceWork->user_confirm_status = 0;
  658. $serviceWork->price_approval = 2;
  659. $serviceWork->save();
  660. Db::commit();
  661. return true;
  662. } catch (\Exception $e) {
  663. Db::rollback();
  664. self::setError($e->getMessage());
  665. return false;
  666. }
  667. }
  668. public static function notApproved($params)
  669. {
  670. Db::startTrans();
  671. try {
  672. $serviceWork = ServiceWork::where('id',$params['id'])->findOrEmpty();
  673. if($serviceWork->isEmpty()){
  674. throw new \Exception('工单不存在');
  675. }
  676. $serviceWork->refund_approval = 3;
  677. $serviceWork->save();
  678. Db::commit();
  679. return true;
  680. } catch (\Exception $e) {
  681. Db::rollback();
  682. self::setError($e->getMessage());
  683. return false;
  684. }
  685. }
  686. public static function againDoor($params)
  687. {
  688. Db::startTrans();
  689. try {
  690. $serviceWork = ServiceWork::where('work_sn',$params['work_sn'])->findOrEmpty();
  691. if($serviceWork->isEmpty()){
  692. throw new \Exception('工单不存在');
  693. }
  694. //更新预约日志
  695. //更新工单未确认上门的状态
  696. $serviceWork->work_status = 3;
  697. $serviceWork->user_confirm_status = 0;
  698. ServiceWorkAppointmentLog::create([
  699. 'work_id'=>$serviceWork->id,
  700. 'last_appointment_time'=>$serviceWork->appointment_time,
  701. 'this_appointment_time'=>strtotime($params['appointment_time']),
  702. ]);
  703. $serviceWork->appointment_time = strtotime($params['appointment_time']);
  704. $serviceWork->save();
  705. Db::commit();
  706. return true;
  707. } catch (\Exception $e) {
  708. Db::rollback();
  709. self::setError($e->getMessage());
  710. return false;
  711. }
  712. }
  713. public static function cancelAllocation($params,$userInfo){
  714. Db::startTrans();
  715. try {
  716. $work = ServiceWork::findOrEmpty($params['id']);
  717. if($work->isEmpty()){
  718. throw new Exception('工单不存在');
  719. }
  720. if($work->work_status >=6 ){
  721. throw new \Exception('工单状态只能修改待结算之前的');
  722. }
  723. $worker = MasterWorker::where(['id'=>$work->master_worker_id])->findOrEmpty();
  724. if($worker->isEmpty()){
  725. throw new \Exception('工程师不存在');
  726. }
  727. MasterWorker::setWorktotal('dec',$work->master_worker_id);
  728. $work->master_worker_id = 0;
  729. $work->work_status = 0;
  730. $work->dispatch_time = 0;
  731. $work->save();
  732. $work_log = [
  733. 'work_id'=>$work->id,
  734. 'master_worker_id'=>$params['master_worker_id'],
  735. 'opera_log'=>'后台用户['.$userInfo['admin_id'].']'.$userInfo['name'].'于'.date('Y-m-d H:i:s',time()).'取消了工程师'.'编号['.$worker->worker_number.']'.$worker->real_name
  736. ];
  737. ServiceWorkerAllocateWorkerLogic::add($work_log);
  738. Db::commit();
  739. return true;
  740. }catch(\Exception $e){
  741. Db::rollback();
  742. dd($e->getMessage());
  743. self::setError($e->getMessage());
  744. return false;
  745. }
  746. }
  747. }