ServiceWorkLogic.php 34 KB

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