ServiceWorkLogic.php 37 KB

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