ServiceWorkLogic.php 34 KB

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