ServiceOrderLogic.php 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. <?php
  2. namespace app\api\logic;
  3. use app\adminapi\service\DistributeLeafletsService;
  4. use app\common\enum\GoodsEnum;
  5. use app\common\enum\PayEnum;
  6. use app\common\enum\RefundEnum;
  7. use app\common\enum\WorkEnum;
  8. use app\common\enum\YesNoEnum;
  9. use app\common\logic\BaseLogic;
  10. use app\common\logic\PaymentLogic;
  11. use app\common\logic\RefundLogic;
  12. use app\common\model\coupon\CouponCategory;
  13. use app\common\model\coupon\UserCoupon;
  14. use app\common\model\dict\DictData;
  15. use app\common\model\goods\Goods;
  16. use app\common\model\master_worker\MasterWorker;
  17. use app\common\model\master_worker\MasterWorkerRule;
  18. use app\common\model\orders\RechargeOrder;
  19. use app\common\model\recharge\OrderGoods;
  20. use app\common\model\refund\RefundRecord;
  21. use app\common\model\spare_part\SparePart;
  22. use app\common\model\works\ServiceWork;
  23. use app\common\model\works\ServiceWorkAppointmentLog;
  24. use app\common\model\works\ServiceWorkSpare;
  25. use app\workerapi\logic\ServiceWorkLogLogic;
  26. use think\Exception;
  27. use think\facade\Db;
  28. use think\facade\Log;
  29. /**
  30. * 订单逻辑层
  31. * Class ServiceOrderLogic
  32. * @package app\api\logic
  33. */
  34. class ServiceOrderLogic extends BaseLogic
  35. {
  36. /**
  37. * 判断是否在某服务区
  38. * @param $params
  39. * @return bool
  40. */
  41. public static function isService($params)
  42. {
  43. // 查询服务区所有的地点
  44. $rules = MasterWorkerRule::where('scene_id',MasterWorkerRule::SCENE_SERVICE)->column('description','id');
  45. //$masters = [];
  46. foreach ($rules as $key=>$value){
  47. $rule = explode(',',$value);
  48. //$masters[] = ['id'=>$key,'lon'=>$rule[0],'lat'=>$rule[1],'radius'=>$rule[2]];
  49. $distance = round(DistributeLeafletsService::haversineDistance($params['lat'], $params['lon'], $rule[1], $rule[0]), 2);
  50. if($distance <= (float)$rule[2]){
  51. return true;
  52. }
  53. }
  54. self::setError('已超出服务区域!');
  55. return false;
  56. }
  57. /**
  58. * 提交订单
  59. * @param array $params
  60. * @return array|false
  61. */
  62. public static function submitOrder($params)
  63. {
  64. Db::startTrans();
  65. try {
  66. // 订单位置是否在服务区内
  67. if(!self::isService($params)){
  68. throw new Exception('已超出服务区域!');
  69. }
  70. $goods = Goods::findOrEmpty($params['goods_id']);
  71. if($goods->isEmpty()){
  72. throw new Exception('产品不存在!');
  73. }
  74. if(empty($params['user_info']['mobile'])){
  75. throw new Exception('请先补充您的联系方式后在提交订单');
  76. }
  77. //根据服务工单计算当前订单应支付金额
  78. if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE){
  79. //一口价订单
  80. $order_total = $goods['service_fee'];
  81. $order_amount = $goods['service_fee'];
  82. }else if ($goods['goods_payment_type'] == GoodsEnum::DEP_GOODS_PAYMENT_TYPE){
  83. $order_total = $goods['service_fee'];
  84. $order_amount = $goods['service_fee'];
  85. }
  86. else{
  87. $order_total = $goods['base_service_fee'];
  88. $order_amount = $goods['service_fee'];
  89. }
  90. //优惠券验证
  91. if(!empty($params['coupon_id'])){
  92. $user_coupon = UserCoupon::where(['id'=>$params['coupon_id'],'user_id'=>$params['user_id'],'voucher_status'=>0])
  93. ->where('voucher_count','>',0)
  94. ->where('expire_time','>=',time())
  95. ->where('begin_use','<',time())
  96. ->findOrEmpty();
  97. if($user_coupon->isEmpty()){
  98. throw new Exception('该优惠券无法使用');
  99. }
  100. if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE and $order_amount<$user_coupon['amount_require']){
  101. throw new Exception('该优惠劵不满足满减使用条件');
  102. }
  103. if($goods['goods_payment_type'] != GoodsEnum::ISGOODS_PAYMENT_TYPE){
  104. throw new Exception('请在支付尾款的时候使用该优惠券');
  105. }
  106. //优惠券折扣
  107. if($user_coupon['mold_type'] == 1){
  108. //按比例折扣
  109. if($user_coupon['discount_ratio']>=1){
  110. throw new Exception('优惠券有误,请联系客服');
  111. }
  112. $order_coupon_amount = $order_amount*(1-$user_coupon['discount_ratio']);
  113. }else{
  114. $order_coupon_amount = $user_coupon['amount'];
  115. }
  116. if(!empty($user_coupon['max_deductible_price'])){
  117. $order_amount = ($order_coupon_amount>$user_coupon['max_deductible_price'])?($order_amount-$user_coupon['max_deductible_price']):($order_amount-$order_coupon_amount);
  118. }else{
  119. $order_amount = $order_amount-$order_coupon_amount;
  120. }
  121. $user_coupon->voucher_status = 1;
  122. $user_coupon->voucher_count = $user_coupon->voucher_count-1;
  123. $user_coupon->save();
  124. }
  125. //生成服务工单
  126. $work_data = [
  127. 'work_sn' => generate_sn(ServiceWork::class, 'work_sn'),
  128. 'real_name' => $params['contact_people'],
  129. 'mobile' => $params['contact_number'],
  130. 'address' => $params['address'],
  131. 'title' => $goods->goods_name . '*' . $goods->goods_number.$goods->good_unit,
  132. 'category_type' => $goods['category_type'],
  133. 'goods_category_ids' => $goods['goods_category_ids'],
  134. 'goods_category_id' => $goods['goods_category_id'],
  135. 'base_service_fee' => $goods['base_service_fee'],
  136. 'service_fee' => $goods['service_fee'],
  137. 'work_pay_status'=>WorkEnum::UN_PAY_STATUS,
  138. 'appointment_time' => strtotime($params['appointment_time']),
  139. 'user_id'=>$params['user_id'],
  140. 'lon'=>!empty($params['lon'])?$params['lon']:0,
  141. 'lat'=>!empty($params['lat'])?$params['lat']:0,
  142. ];
  143. //判断是否是加单
  144. if(!empty($params['worker'])){
  145. $worker_id = MasterWorker::where('worker_number',$params['worker'])->value('id');
  146. $work_data['master_worker_id'] = $worker_id;
  147. $work_data['work_status'] = 1;
  148. $work_data['dispatch_time'] = time();
  149. $work_data['work_type'] = 2;
  150. $work_data['data_type'] = 1;
  151. }
  152. //判断是否是复购单
  153. $is_work = ServiceWork::where(['user_id'=>$params['user_id'],'service_status'=>3])->findOrEmpty();
  154. if(!$is_work->isEmpty()){
  155. $work_data['data_type'] = 1;
  156. }
  157. $service_work = ServiceWork::create($work_data);
  158. //生成服务订单
  159. $data = [
  160. 'work_id'=> $service_work['id'],
  161. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  162. 'order_type'=>0,//服务订单
  163. 'order_terminal' => $params['terminal'],
  164. 'payment_type'=>$goods['goods_payment_type']==GoodsEnum::ISGOODS_PAYMENT_TYPE?0:1,
  165. 'user_id' => $params['user_id'],
  166. 'pay_status' => PayEnum::UNPAID,
  167. 'coupon_id'=>!empty($params['coupon_id'])?$params['coupon_id']:0,
  168. 'coupon_price'=>!empty($order_coupon_amount)?$order_coupon_amount:0,
  169. 'pay_way' => $params['pay_way'],
  170. 'order_total' => $order_total,
  171. 'order_amount' => $order_amount,
  172. ];
  173. $order = RechargeOrder::create($data);
  174. //生成订单服务详情
  175. OrderGoods::create([
  176. 'sn' => $order['sn'],
  177. 'goods_id' => $params['goods_id'],
  178. 'category_type' => $goods['category_type'],
  179. 'goods_category_ids' => $goods['goods_category_ids'],
  180. 'goods_category_id' => $goods['goods_category_id'],
  181. 'goods_name' => $goods['goods_name'],
  182. 'goods_image' => $goods['goods_image'],
  183. 'goods_video' => $goods['goods_video'],
  184. 'goods_number' => $goods['goods_number'],
  185. 'good_unit' => $goods['good_unit'],
  186. 'goods_size' => $goods['goods_size'],
  187. 'goods_type' => $goods['goods_type'],
  188. 'goods_brand' => $goods['goods_brand'],
  189. 'install_guide' => $goods['install_guide'],
  190. 'goods_payment_type'=>$goods['goods_payment_type'],
  191. 'base_service_fee' => $goods['base_service_fee'],
  192. 'service_total' => $goods['service_total'],
  193. 'service_fee' => $goods['service_fee'],
  194. 'service_image' => $goods['service_image'],
  195. 'warranty_period'=>$goods['warranty_period'],
  196. 'fee_schedule' => $goods['fee_schedule'],
  197. 'goods_status' => $goods['goods_status'],
  198. ]);
  199. Db::commit();
  200. } catch (\Exception $e) {
  201. self::setError($e->getMessage());
  202. return false;
  203. }
  204. return [
  205. 'order_id' => (int)$order['id'],
  206. 'work_id' => (int)$order['work_id'],
  207. ];
  208. }
  209. /**
  210. * 提交尾款订单
  211. * @param array $params
  212. * @return array|false
  213. */
  214. public static function submitFinalOrder($params)
  215. {
  216. Db::startTrans();
  217. try {
  218. $order = \app\common\model\recharge\RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  219. if ($order->isEmpty()) {
  220. throw new Exception('订单不存在');
  221. }
  222. //判断订单类型.服务订单尾款处理
  223. if($order['order_type'] == 0 and $order['pay_status'] == PayEnum::ISPAID)//服务工单
  224. {
  225. $order = RechargeOrder::where(['work_id'=>$order['work_id'],'pay_status'=>0])->findOrEmpty();
  226. if($order->isEmpty()){
  227. throw new Exception('订单已支付');
  228. }
  229. }
  230. if ($order['pay_status'] == PayEnum::ISPAID) {
  231. throw new Exception('订单已支付');
  232. }
  233. $order_goods = OrderGoods::where('sn',$params['sn'])->findOrEmpty();
  234. $goods = Goods::findOrEmpty($order_goods['goods_id']);
  235. //判断是否存在优惠券
  236. //优惠券验证
  237. if(!empty($order['coupon_id'])){
  238. $order->coupon_id = 0;
  239. $order->coupon_price = 0;
  240. $order->order_amount = $order->order_total;
  241. $order->save();
  242. $user_coupon_ed = UserCoupon::findOrEmpty($order['coupon_id']);
  243. $user_coupon_ed->voucher_count = $user_coupon_ed->voucher_count+1;
  244. $user_coupon_ed->voucher_status = 0;
  245. $user_coupon_ed->save();
  246. $order = \app\common\model\recharge\RechargeOrder::where('sn',$order['sn'])->findOrEmpty();
  247. }
  248. if(!empty($params['coupon_id']) && empty($order['coupon_id'])){
  249. $user_coupon = UserCoupon::where(['id'=>$params['coupon_id'],'user_id'=>$params['user_id'],'voucher_status'=>0])
  250. ->where('voucher_count','>',0)
  251. ->where('expire_time','>=',time())
  252. ->where('begin_use','<',time())
  253. ->findOrEmpty();
  254. if($user_coupon->isEmpty()){
  255. throw new Exception('该优惠券不满足使用条件');
  256. }
  257. if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE and $order['order_amount']<$user_coupon['amount_require']){
  258. throw new Exception('该优惠劵不满足满减使用条件');
  259. }
  260. //优惠券折扣
  261. if($user_coupon['mold_type'] == 1){
  262. //按比例折扣
  263. if($user_coupon['discount_ratio']>=1){
  264. throw new Exception('优惠券有误,请联系客服');
  265. }
  266. $order_coupon_amount = $order['order_amount']*(1-$user_coupon['discount_ratio']);
  267. }else{
  268. $order_coupon_amount = $user_coupon['amount'];
  269. }
  270. if(!empty($user_coupon['max_deductible_price'])){
  271. $order_amount = ($order_coupon_amount>$user_coupon['max_deductible_price'])?($order['order_amount']-$user_coupon['max_deductible_price']):($order['order_amount']-$order_coupon_amount);
  272. }else{
  273. $order_amount = $order['order_amount']-$order_coupon_amount;
  274. }
  275. $user_coupon->voucher_status = 1;
  276. $user_coupon->voucher_count = $user_coupon->voucher_count-1;
  277. $user_coupon->save();
  278. }
  279. $order->coupon_id = !empty($params['coupon_id'])?$params['coupon_id']:0;
  280. $order->coupon_price = !empty($order_coupon_amount)?$order_coupon_amount:0;
  281. $order->order_amount = !empty($order_amount)?$order_amount:$order->order_amount;
  282. $order->save();
  283. Db::commit();
  284. } catch (\Exception $e) {
  285. self::setError($e->getMessage());
  286. return false;
  287. }
  288. return [
  289. 'order_id' => (int)$order['id'],
  290. 'work_id' => (int)$order['work_id'],
  291. ];
  292. }
  293. /**
  294. * 重置订单优惠券
  295. * * @param $params
  296. * * * @return array|false
  297. */
  298. public static function cancelOrderCoupon($params)
  299. {
  300. Db::startTrans();
  301. try {
  302. $order = \app\common\model\recharge\RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  303. if ($order->isEmpty()) {
  304. throw new Exception('订单不存在');
  305. }
  306. //判断订单类型.服务订单尾款处理
  307. if($order['order_type'] == 0 and $order['pay_status'] == PayEnum::ISPAID)//服务工单
  308. {
  309. $order = RechargeOrder::where(['work_id'=>$order['work_id'],'pay_status'=>0])->findOrEmpty();
  310. if($order->isEmpty()){
  311. throw new Exception('订单已支付');
  312. }
  313. }
  314. if ($order['pay_status'] == PayEnum::ISPAID) {
  315. throw new Exception('订单已支付');
  316. }
  317. if(!empty($order['coupon_id'])){
  318. $user_coupon_ed = UserCoupon::findOrEmpty($order['coupon_id']);
  319. if(!$user_coupon_ed->isEmpty()){
  320. $user_coupon_ed->voucher_count = $user_coupon_ed->voucher_count+1;
  321. $user_coupon_ed->voucher_status = 0;
  322. $user_coupon_ed->save();
  323. $order->order_amount = $order->order_amount+$order->coupon_price;
  324. $order->coupon_id = 0;
  325. $order->coupon_price = 0;
  326. $order->save();
  327. }
  328. }
  329. Db::commit();
  330. } catch (\Exception $e) {
  331. self::setError($e->getMessage());
  332. return false;
  333. }
  334. }
  335. /**
  336. * 获取订单工程师信息
  337. * * @param $params
  338. * * @return array|false
  339. */
  340. public static function getMasterWorker($params)
  341. {
  342. try {
  343. $order_info = \app\common\model\recharge\RechargeOrder::with(['order_goods'=>function ($query) {
  344. $query->visible(['goods_name','goods_image','goods_number','good_unit']);
  345. },'service_work'=>function ($query) {
  346. $query->visible(['real_name','mobile','address','service_status','appointment_time','master_worker_id','work_images','finished_images','finished_time'])->append(['service_status_text','user_service_status','user_service_status_text']);
  347. }])
  348. ->visible(['id','sn','payment_type','order_total','order_amount','pay_status','create_time','title','work_id'])
  349. ->where([
  350. 'order_type' => 0,
  351. 'user_id' => $params['user_id'],
  352. 'sn'=>$params['sn']
  353. ])->findOrEmpty()->toArray();
  354. $data = [];
  355. //获取工程师参数
  356. if(!empty($order_info['service_work']['master_worker_id'])){
  357. $worker = MasterWorker::find($order_info['service_work']['master_worker_id']);
  358. $data['avatar'] = $worker['avatar'];
  359. $data['real_name'] = $worker['real_name'];
  360. $data['worker_number'] = $worker['worker_number'];
  361. $data['mobile'] = $worker['mobile'];
  362. $maintain_exp_type = !empty($worker->worker_register->maintain_exp_type)?$worker->worker_register->maintain_exp_type:'';
  363. $data['worker_exp'] = DictData::where(['type_value'=>'worker_exp_type','value'=>$maintain_exp_type])->value('name');
  364. $data['appointment_time'] = $order_info['service_work']['appointment_time'];
  365. }
  366. return $data;
  367. }
  368. catch (\Exception $e) {
  369. self::setError($e->getMessage());
  370. return false;
  371. }
  372. }
  373. /**
  374. * 获取订单详情
  375. * @param $params
  376. * @return array|false
  377. */
  378. public static function detail($params)
  379. {
  380. try {
  381. $order_info = \app\common\model\recharge\RechargeOrder::with(['order_goods'=>function ($query) {
  382. $query->visible(['goods_name','goods_image','goods_number','good_unit']);
  383. },'service_work'=>function ($query) {
  384. $query->visible(['work_sn','real_name','mobile','address','service_status','appointment_time','master_worker_id','work_images','finished_images','finished_time','spare_total','service_work_spare_id','refund_approval'])->append(['service_status_text','user_service_status','user_service_status_text']);
  385. }])
  386. ->visible(['id','sn','payment_type','order_total','order_amount','paid_amount','pay_status','create_time','title','work_id'])
  387. ->where([
  388. 'order_type' => 0,
  389. 'user_id' => $params['user_id'],
  390. 'sn'=>$params['sn']
  391. ])->findOrEmpty()->toArray();
  392. if(empty($order_info)){
  393. throw new Exception('订单不存在');
  394. }
  395. $order_info['master_worker'] = [
  396. 'avatar' => '',
  397. 'real_name'=>'',
  398. 'worker_number'=>'',
  399. 'mobile'=>'',
  400. 'worker_exp'=>''
  401. ];
  402. //查询总价
  403. $order_info['order_amount'] = \app\common\model\recharge\RechargeOrder::where(['order_type'=>0,'user_id'=>$params['user_id'],'work_id'=>$order_info['work_id']])->sum('order_amount');
  404. $order_info['paid_amount'] = \app\common\model\recharge\RechargeOrder::where(['order_type'=>0,'user_id'=>$params['user_id'],'work_id'=>$order_info['work_id']])->sum('paid_amount');
  405. //退款金额
  406. $order_refund_amount = 0;
  407. //获取工程师参数
  408. if(!empty($order_info['service_work']['master_worker_id'])){
  409. $worker = MasterWorker::find($order_info['service_work']['master_worker_id']);
  410. $order_info['master_worker']['avatar'] = !empty($worker)?$worker['avatar']:'';
  411. $order_info['master_worker']['real_name'] = !empty($worker)?$worker['real_name']:'';
  412. $order_info['master_worker']['worker_number'] = !empty($worker)?$worker['worker_number']:'';
  413. $order_info['master_worker']['mobile'] = !empty($worker)?$worker['mobile']:'';
  414. $maintain_exp_type = !empty($worker->worker_register->maintain_exp_type)?$worker->worker_register->maintain_exp_type:'';
  415. $order_info['master_worker']['worker_exp'] = DictData::where(['type_value'=>'worker_exp_type','value'=>$maintain_exp_type])->value('name');
  416. }
  417. //搜索当前工单下的所有订单记录
  418. $order_info['pay_orders'] = \app\common\model\recharge\RechargeOrder::where(['work_id'=>$order_info['work_id']])->field('id as order_id, pay_status ,refund_status,payment_type,pay_way,pay_time,order_total,order_amount,coupon_price')->order('id asc')->select()->toArray();
  419. $pay_status_data = DictData::where('type_value','pay_status')->column('name','value');
  420. $payment_type_data = DictData::where('type_value','payment_type')->column('name','value');
  421. $pay_way_data = DictData::where('type_value','pay_way')->column('name','value');
  422. $coupon_price = 0;
  423. foreach ($order_info['pay_orders'] as $k=>&$v){
  424. $v['pay_status_name'] = $pay_status_data[$v['pay_status']];
  425. $v['payment_type_name'] = $payment_type_data[$v['payment_type']];
  426. $v['pay_way_name'] = $pay_way_data[$v['pay_way']];
  427. if($v['refund_status']==1){
  428. $order_refund_amount += $v['order_amount'];
  429. $refund_status = RefundRecord::where('order_id',$v['order_id'])->value('refund_status');
  430. }
  431. if($v['payment_type']!=1 and !empty($order_info['service_work']['spare_total'])){
  432. $v['order_amount_total'] = $v['order_amount'];
  433. $v['order_amount'] = $v['order_amount'] - $order_info['service_work']['spare_total'];
  434. }
  435. $coupon_price += $v['coupon_price'];
  436. }
  437. //汇总优惠卷额度
  438. $order_info['coupon_price'] = $coupon_price;
  439. //退款汇总
  440. $order_info['refund_amount'] = $order_refund_amount;
  441. if(isset($refund_status)){
  442. switch ($refund_status){
  443. case 0:
  444. $order_info['refund_status'] = '退款中';
  445. break;
  446. case 1:
  447. $order_info['refund_status'] = '退款成功';
  448. break;
  449. case 2:
  450. $order_info['refund_status'] = '退款失败';
  451. break;
  452. }
  453. }else{
  454. $order_info['refund_status'] = '未退款';
  455. }
  456. // 所有配件
  457. $order_info['spare_total'] = $order_info['service_work']['spare_total']??0;
  458. $order_info['spare_parts'] = [];
  459. if($order_info['service_work']['service_work_spare_id']){
  460. $work_spare_parts = json_decode(ServiceWorkSpare::where('id',$order_info['service_work']['service_work_spare_id'])->value('spare_parts'),true);
  461. $spare_parts = SparePart::where('id','in',array_column($work_spare_parts,'id'))
  462. ->field(['id', 'goods_category_id', 'spare_name', 'spare_image', 'spare_number', 'spare_unit','spare_status'])
  463. ->select()
  464. ->toArray();
  465. $spare_parts = array_column($spare_parts,null,'id');
  466. foreach (array_column($work_spare_parts,null,'id') as $k=>&$v){
  467. $spare_parts[$k] = array_merge($spare_parts[$k],$v);
  468. }
  469. $order_info['spare_parts'] = array_values($spare_parts)??[];
  470. }
  471. //获取所有的改约记录
  472. $order_info['appoint_list'] = [];
  473. $appoint_log = ServiceWorkAppointmentLog::where('work_id',$order_info['work_id'])->order('id desc')->select()->toArray();
  474. if(!empty($appoint_log)){
  475. $order_info['appoint_list'][0]['appointment_time'] = date('Y-m-d H:i:s',$appoint_log[0]['this_appointment_time']);
  476. foreach ($appoint_log as $k1=>$v1){
  477. $order_info['appoint_list'][$k1+1]['appointment_time'] = date('Y-m-d H:i:s',$v1['last_appointment_time']);
  478. }
  479. }
  480. return $order_info;
  481. }
  482. catch (\Exception $e) {
  483. self::setError($e->getMessage());
  484. return false;
  485. }
  486. }
  487. public static function getDetailStatus($params)
  488. {
  489. try {
  490. $order_info = \app\common\model\recharge\RechargeOrder::with(['service_work'=>function ($query) {
  491. $query->visible(['work_sn'])->append(['service_status_text','user_service_status','user_service_status_text']);
  492. }])
  493. ->visible(['id','sn'])
  494. ->where([
  495. 'order_type' => 0,
  496. 'user_id' => $params['user_id'],
  497. 'sn'=>$params['sn']
  498. ])->findOrEmpty()->toArray();
  499. if(empty($order_info)){
  500. throw new Exception('订单不存在');
  501. }
  502. return $order_info;
  503. }
  504. catch (\Exception $e) {
  505. self::setError($e->getMessage());
  506. return false;
  507. }
  508. }
  509. /**
  510. * 获取订单支付详情
  511. * @param $params
  512. * @return array|false
  513. */
  514. public static function orderPayInfo($params)
  515. {
  516. try {
  517. //判断是否存在优惠券
  518. $is_coupon_used_work_id = \app\common\model\recharge\RechargeOrder::where([
  519. 'order_type' => 0,
  520. 'user_id' => $params['user_id'],
  521. 'sn'=>$params['sn']
  522. ])->value('work_id');
  523. $is_coupon_used = \app\common\model\recharge\RechargeOrder::where('work_id',$is_coupon_used_work_id)->where('pay_status',0)->findOrEmpty();
  524. if(!empty($is_coupon_used['coupon_id'])){
  525. $user_coupon = UserCoupon::where(['user_id'=>$is_coupon_used->user_id,'id'=>$is_coupon_used->coupon_id])->findOrEmpty();
  526. $user_coupon->voucher_status = 0;
  527. $user_coupon->voucher_count = $user_coupon->voucher_count+1;
  528. $user_coupon->save();
  529. $is_coupon_used->order_amount = $is_coupon_used->order_amount+$is_coupon_used->coupon_price;
  530. $is_coupon_used->coupon_price = 0;
  531. $is_coupon_used->coupon_id = 0;
  532. $is_coupon_used->save();
  533. }
  534. $order_info = \app\common\model\recharge\RechargeOrder::with(['order_goods'=>function ($query) {
  535. $query->visible(['goods_payment_type','goods_category_id']);
  536. },'service_work'=>function ($query) {
  537. $query->visible(['service_fee','spare_total','service_work_spare_id','goods_category_id']);
  538. }])
  539. ->visible(['id','sn','work_id'])
  540. ->where([
  541. 'order_type' => 0,
  542. 'user_id' => $params['user_id'],
  543. 'sn'=>$params['sn']
  544. ])->findOrEmpty()->toArray();
  545. if(empty($order_info)){
  546. throw new Exception('订单不存在');
  547. }
  548. //查询总价
  549. $order_info['order_amount'] = \app\common\model\recharge\RechargeOrder::where(['order_type'=>0,'user_id'=>$params['user_id'],'work_id'=>$order_info['work_id']])->sum('order_amount');
  550. $order_info['paid_amount'] = \app\common\model\recharge\RechargeOrder::where(['order_type'=>0,'user_id'=>$params['user_id'],'work_id'=>$order_info['work_id']])->sum('paid_amount');
  551. //搜索当前工单下的所有订单记录
  552. $order_info['pay_orders'] = \app\common\model\recharge\RechargeOrder::where(['work_id'=>$order_info['work_id']])->field('id as order_id, pay_status,refund_status,payment_type,pay_way,pay_time,order_total,order_amount,coupon_price')->order('id asc')->select()->toArray();
  553. $pay_status_data = DictData::where('type_value','pay_status')->column('name','value');
  554. $payment_type_data = DictData::where('type_value','payment_type')->column('name','value');
  555. $pay_way_data = DictData::where('type_value','pay_way')->column('name','value');
  556. $coupon_price = 0;
  557. //退款金额
  558. $order_refund_amount = 0;
  559. $payment_type = 0;
  560. foreach ($order_info['pay_orders'] as $k=>&$v){
  561. $v['pay_status_name'] = $pay_status_data[$v['pay_status']];
  562. $v['payment_type_name'] = $payment_type_data[$v['payment_type']];
  563. $v['pay_way_name'] = $pay_way_data[$v['pay_way']];
  564. if($v['refund_status']==1){
  565. $order_refund_amount += $v['order_amount'];
  566. $refund_status = RefundRecord::where('order_id',$v['order_id'])->value('refund_status');
  567. }
  568. if($v['payment_type']!=1 and !empty($order_info['service_work']['spare_total'])){
  569. $v['order_total'] = $v['order_total'] - $order_info['service_work']['spare_total'];
  570. $v['order_amount'] = $v['order_amount'] - $order_info['service_work']['spare_total'] + $v['coupon_price'];
  571. }
  572. $coupon_price += $v['coupon_price'];
  573. $payment_type = $v['payment_type'];
  574. }
  575. $order_info['payment_type'] = $payment_type;
  576. //汇总优惠卷额度
  577. $order_info['coupon_price'] = $coupon_price;
  578. //退款汇总
  579. $order_info['refund_amount'] = $order_refund_amount;
  580. if(isset($refund_status)){
  581. switch ($refund_status){
  582. case 0:
  583. $order_info['refund_status'] = '退款中';
  584. break;
  585. case 1:
  586. $order_info['refund_status'] = '退款成功';
  587. break;
  588. case 2:
  589. $order_info['refund_status'] = '退款失败';
  590. break;
  591. }
  592. }else{
  593. $order_info['refund_status'] = '未退款';
  594. }
  595. // 所有配件
  596. $order_info['spare_total'] = $order_info['service_work']['spare_total']??0;
  597. $order_info['spare_parts'] = [];
  598. if($order_info['service_work']['service_work_spare_id']){
  599. $work_spare_parts = json_decode(ServiceWorkSpare::where('id',$order_info['service_work']['service_work_spare_id'])->value('spare_parts'),true);
  600. $spare_parts = SparePart::where('id','in',array_column($work_spare_parts,'id'))
  601. ->field(['id', 'goods_category_id', 'spare_name', 'spare_image', 'spare_number', 'spare_unit','spare_status'])
  602. ->select()
  603. ->toArray();
  604. $spare_parts = array_column($spare_parts,null,'id');
  605. foreach (array_column($work_spare_parts,null,'id') as $k=>&$v){
  606. $spare_parts[$k] = array_merge($spare_parts[$k],$v);
  607. }
  608. $order_info['spare_parts'] = array_values($spare_parts)??[];
  609. }
  610. //获取是否存在可使用优惠券
  611. $coupon_ids = CouponCategory::where('goods_category_id',$order_info['service_work']['goods_category_id'])
  612. ->whereIn('coupon_id',UserCoupon::where('user_id',$params['user_id'])
  613. ->where('voucher_count','>',0)
  614. ->where('voucher_status',0)
  615. ->where('expire_time','>',time())
  616. ->where('amount_require','<=',$order_info['order_amount'])
  617. ->column('coupon_id')
  618. )->column('coupon_id');
  619. $usable_coupon = UserCoupon::where('user_id',$params['user_id'])
  620. ->whereIn('coupon_id',$coupon_ids)
  621. ->append(['discount_ratio_text'])
  622. ->where('expire_time','>',time())
  623. ->count();
  624. $order_info['usable_coupon'] = $usable_coupon>0?true:false;
  625. return $order_info;
  626. }
  627. catch (\Exception $e) {
  628. self::setError($e->getMessage());
  629. return false;
  630. }
  631. }
  632. /**
  633. * 取消订单
  634. * @param $params
  635. * @return false|void
  636. */
  637. public static function cancelOrder($params)
  638. {
  639. Db::startTrans();
  640. try {
  641. $work_id = \app\common\model\recharge\RechargeOrder::where([
  642. 'order_type' => 0,
  643. 'user_id' => $params['user_id'],
  644. 'sn'=>$params['sn']
  645. ])->value('work_id');
  646. if(empty($work_id)){
  647. throw new Exception('订单不存在');
  648. }
  649. $payed_order = \app\common\model\recharge\RechargeOrder::where(['user_id'=>$params['user_id'],'work_id'=>$work_id,'pay_status'=>1])->findOrEmpty();
  650. if(!$payed_order->isEmpty()){
  651. throw new Exception('存在已支付订单,不允许取消订单,请联系客服');
  652. }
  653. //软删除订单
  654. $cancel_order = \app\common\model\recharge\RechargeOrder::where(['user_id'=>$params['user_id'],'work_id'=>$work_id])->findOrEmpty();
  655. $cancel_order->pay_status = 2;
  656. $cancel_order->save();
  657. //更新工单状态为已取消
  658. $service_work = ServiceWork::find($work_id);
  659. $service_work->service_status = 4;
  660. $service_work->save();
  661. //判断如果存在优惠券,返还优惠券
  662. if(!empty($payed_order->coupon_id)){
  663. $user_coupon = UserCoupon::where(['user_id'=>$payed_order->user_id,'id'=>$payed_order->coupon_id])->findOrEmpty();
  664. $user_coupon->voucher_status = 0;
  665. $user_coupon->voucher_count = $user_coupon->voucher_count+1;
  666. $user_coupon->save();
  667. }
  668. Db::commit();
  669. }
  670. catch (\Exception $e) {
  671. self::setError($e->getMessage());
  672. return false;
  673. }
  674. }
  675. /**
  676. * 用户确认尾款报价单
  677. * @param $params
  678. * @return false|void
  679. */
  680. public static function confirmOrder($params)
  681. {
  682. Db::startTrans();
  683. try {
  684. $work_id = \app\common\model\recharge\RechargeOrder::where([
  685. 'order_type' => 0,
  686. 'user_id' => $params['user_id'],
  687. 'sn'=>$params['sn']
  688. ])->value('work_id');
  689. if(empty($work_id)){
  690. throw new Exception('订单不存在');
  691. }
  692. //更新工单状态为已取消
  693. $service_work = ServiceWork::find($work_id);
  694. if($service_work->user_confirm_status==2){
  695. throw new Exception('请勿重复操作');
  696. }
  697. $service_work->work_status = 5;
  698. $service_work->user_confirm_status = 2;
  699. $service_work->save();
  700. $work_log = [
  701. 'work_id'=>$work_id,
  702. 'master_worker_id'=>$service_work->master_worker_id,
  703. 'opera_log'=>'用户'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'确认了报价单',
  704. ];
  705. ServiceWorkLogLogic::add($work_log);
  706. Db::commit();
  707. }
  708. catch (\Exception $e) {
  709. self::setError($e->getMessage());
  710. return false;
  711. }
  712. }
  713. /**
  714. * 用户确认服务完成
  715. * @param $params
  716. * @return false|void
  717. */
  718. public static function confirmServiceFinish($params)
  719. {
  720. Db::startTrans();
  721. try {
  722. $work_id = \app\common\model\recharge\RechargeOrder::where([
  723. 'order_type' => 0,
  724. 'user_id' => $params['user_id'],
  725. 'sn'=>$params['sn']
  726. ])->value('work_id');
  727. if(empty($work_id)){
  728. throw new Exception('订单不存在');
  729. }
  730. $service_work = ServiceWork::find($work_id);
  731. if($service_work->user_confirm_status!=3){
  732. throw new Exception('请勿重复操作');
  733. }
  734. $orders = RechargeOrder::where(['work_id'=>$work_id,'user_id'=>$params['user_id']])->select()->toArray();
  735. //确认所有订单总金额和结算金额
  736. //若订单是全款已支付订单
  737. if(count($orders)==1 and $orders[0]['payment_type']==0 and $orders[0]['pay_status']==1){
  738. $service_work->work_status = 7;
  739. $service_work->user_confirm_status = 5;
  740. $service_work->service_status = 3;
  741. $service_work->work_pay_status = 1;
  742. }else{
  743. $service_work->work_status = 6;
  744. $service_work->user_confirm_status = 4;
  745. }
  746. $service_work->finished_time = time();
  747. $service_work->save();
  748. //更新工程师的进行工单数量
  749. MasterWorker::setWorktotal('dec',$service_work->master_worker_id);
  750. $work_log = [
  751. 'work_id'=>$work_id,
  752. 'master_worker_id'=>$service_work->master_worker_id,
  753. 'opera_log'=>'用户'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'确认服务完成',
  754. ];
  755. ServiceWorkLogLogic::add($work_log);
  756. Db::commit();
  757. }
  758. catch (\Exception $e) {
  759. self::setError($e->getMessage());
  760. return false;
  761. }
  762. }
  763. public static function approvalChangePrice($params)
  764. {
  765. Db::startTrans();
  766. try {
  767. $order = RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  768. if($order->isEmpty()){
  769. throw new Exception('订单不存在');
  770. }
  771. $work = ServiceWork::findOrEmpty($order->work_id);
  772. if($work->isEmpty()){
  773. throw new Exception('工单不存在');
  774. }
  775. $work->work_status = 4;
  776. $work->user_confirm_status = 0;
  777. $work->price_approval = 1;
  778. $work->save();
  779. Db::commit();
  780. }
  781. catch (\Exception $e) {
  782. self::setError($e->getMessage());
  783. return false;
  784. }
  785. }
  786. public static function approvalChangeAppointment($params)
  787. {
  788. Db::startTrans();
  789. try {
  790. $order = RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  791. if($order->isEmpty()){
  792. throw new Exception('订单不存在');
  793. }
  794. $work = ServiceWork::findOrEmpty($order->work_id);
  795. if($work->isEmpty()){
  796. throw new Exception('工单不存在');
  797. }
  798. //更新工单未确认上门的状态
  799. $work->work_status = 3;
  800. $work->user_confirm_status = 0;
  801. ServiceWorkAppointmentLog::create([
  802. 'work_id'=>$work->id,
  803. 'last_appointment_time'=>strtotime($work->appointment_time),
  804. 'this_appointment_time'=>strtotime($params['appointment_time']),
  805. ]);
  806. $work->appointment_time = strtotime($params['appointment_time']);
  807. $work->appoint_approval = 1;
  808. $work->save();
  809. Db::commit();
  810. }
  811. catch (\Exception $e) {
  812. self::setError($e->getMessage());
  813. return false;
  814. }
  815. return $work;
  816. }
  817. public static function approvalRefund($params)
  818. {
  819. Db::startTrans();
  820. try {
  821. $order = RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  822. if($order->isEmpty()){
  823. throw new Exception('订单不存在');
  824. }
  825. $work = ServiceWork::findOrEmpty($order->work_id);
  826. if($work->isEmpty()){
  827. throw new Exception('工单不存在');
  828. }
  829. $work->refund_approval = 1;
  830. $work->save();
  831. //判断是否已下单时间过了两小时,并且师傅暂未上门,距离预约时间两小时以上
  832. if(($order['pay_time']+7200)>time() and $work['work_status']<4 and (strtotime($work['appointment_time'])-7200)>time()){
  833. //生成退款订单
  834. \app\common\model\recharge\RechargeOrder::update([
  835. 'id' => $order['id'],
  836. 'refund_status' => YesNoEnum::YES,
  837. ]);
  838. // 生成退款记录
  839. $recordSn = generate_sn(RefundRecord::class, 'sn');
  840. $record = RefundRecord::create([
  841. 'sn' => $recordSn,
  842. 'user_id' => $order['user_id'],
  843. 'order_id' => $order['id'],
  844. 'order_sn' => $order['sn'],
  845. 'order_type' => RefundEnum::ORDER_TYPE_ORDER,
  846. 'order_amount' => $order['order_amount'],
  847. 'refund_amount' => $order['order_amount'],
  848. 'refund_type' => RefundEnum::TYPE_ADMIN,
  849. 'transaction_id' => $order['transaction_id'] ?? '',
  850. 'refund_way' => RefundEnum::getRefundWayByPayWay($order['pay_way']),
  851. ]);
  852. //更新工单状态
  853. ServiceWork::update([
  854. 'id'=>$order['work_id'],
  855. 'service_status'=>5
  856. ]);
  857. // 退款
  858. RefundLogic::refund($order, $record['id'], $order['order_amount'], 1);
  859. }
  860. Db::commit();
  861. }
  862. catch (\Exception $e) {
  863. self::setError($e->getMessage());
  864. return false;
  865. }
  866. }
  867. /**
  868. * 订单完成通知【给用户】 - 全款 -通知
  869. * @param $params
  870. * @return bool
  871. */
  872. public static function serviceFinishNotice($params)
  873. {
  874. try {
  875. $order = RechargeOrder::where('sn', $params['sn'])
  876. ->where('payment_type','=',0)
  877. ->where('pay_status','=',1)
  878. ->findOrEmpty();
  879. if(!$order->isEmpty()){
  880. event('Notice', [
  881. 'scene_id' => 120,
  882. 'params' => [
  883. 'user_id' => $order['user_id']
  884. ]
  885. ]);
  886. }
  887. return true;
  888. }catch (\Exception $e) {
  889. return false;
  890. }
  891. }
  892. /**
  893. * 提交订单
  894. * @param array $params
  895. * @return array|false
  896. */
  897. public static function firmSubmitOrder($params)
  898. {
  899. Db::startTrans();
  900. try {
  901. $goods = Goods::findOrEmpty($params['goods_id']);
  902. if($goods->isEmpty()){
  903. throw new Exception('产品不存在!');
  904. }
  905. if(empty($params['user_info']['mobile'])){
  906. throw new Exception('请先补充您的联系方式后在提交订单');
  907. }
  908. //根据服务工单计算当前订单应支付金额
  909. if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE){
  910. //一口价订单
  911. $order_total = $goods['service_fee'];
  912. $order_amount = $goods['service_fee'];
  913. }else if ($goods['goods_payment_type'] == GoodsEnum::DEP_GOODS_PAYMENT_TYPE){
  914. $order_total = $goods['service_fee'];
  915. $order_amount = $goods['service_fee'];
  916. }
  917. else{
  918. $order_total = $goods['base_service_fee'];
  919. $order_amount = $goods['service_fee'];
  920. }
  921. //生成服务工单
  922. $work_data = [
  923. 'work_sn' => generate_sn(ServiceWork::class, 'work_sn'),
  924. 'real_name' => $params['contact_people'],
  925. 'mobile' => $params['contact_number'],
  926. 'address' => $params['address'],
  927. 'title' => $goods->goods_name . '*' . $goods->goods_number.$goods->good_unit,
  928. 'category_type' => $goods['category_type'],
  929. 'goods_category_ids' => $goods['goods_category_ids'],
  930. 'goods_category_id' => $goods['goods_category_id'],
  931. 'base_service_fee' => $goods['base_service_fee'],
  932. 'service_fee' => $goods['service_fee'],
  933. 'work_pay_status'=>WorkEnum::UN_PAY_STATUS,
  934. 'work_type'=> 1,
  935. 'appointment_time' => strtotime($params['appointment_time']),
  936. 'user_id'=>$params['user_id'],
  937. 'lon'=>!empty($params['lon'])?$params['lon']:0,
  938. 'lat'=>!empty($params['lat'])?$params['lat']:0,
  939. ];
  940. $service_work = ServiceWork::create($work_data);
  941. //生成服务订单
  942. $data = [
  943. 'work_id'=> $service_work['id'],
  944. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  945. 'order_type'=>0,//服务订单
  946. 'order_terminal' => $params['terminal'],
  947. 'payment_type'=>$goods['goods_payment_type']==GoodsEnum::ISGOODS_PAYMENT_TYPE?0:1,
  948. 'user_id' => $params['user_id'],
  949. 'pay_status' => PayEnum::UNPAID,
  950. 'coupon_id'=>!empty($params['coupon_id'])?$params['coupon_id']:0,
  951. 'coupon_price'=>!empty($order_coupon_amount)?$order_coupon_amount:0,
  952. 'pay_way' => $params['pay_way'],
  953. 'order_total' => $order_total,
  954. 'order_amount' => $order_amount,
  955. ];
  956. $order = RechargeOrder::create($data);
  957. //生成订单服务详情
  958. OrderGoods::create([
  959. 'sn' => $order['sn'],
  960. 'goods_id' => $params['goods_id'],
  961. 'category_type' => $goods['category_type'],
  962. 'goods_category_ids' => $goods['goods_category_ids'],
  963. 'goods_category_id' => $goods['goods_category_id'],
  964. 'goods_name' => $goods['goods_name'],
  965. 'goods_image' => $goods['goods_image'],
  966. 'goods_video' => $goods['goods_video'],
  967. 'goods_number' => $goods['goods_number'],
  968. 'good_unit' => $goods['good_unit'],
  969. 'goods_size' => $goods['goods_size'],
  970. 'goods_type' => $goods['goods_type'],
  971. 'goods_brand' => $goods['goods_brand'],
  972. 'install_guide' => $goods['install_guide'],
  973. 'goods_payment_type'=>$goods['goods_payment_type'],
  974. 'base_service_fee' => $goods['base_service_fee'],
  975. 'service_total' => $goods['service_total'],
  976. 'service_fee' => $goods['service_fee'],
  977. 'service_image' => $goods['service_image'],
  978. 'warranty_period'=>$goods['warranty_period'],
  979. 'fee_schedule' => $goods['fee_schedule'],
  980. 'goods_status' => $goods['goods_status'],
  981. ]);
  982. Db::commit();
  983. } catch (\Exception $e) {
  984. self::setError($e->getMessage());
  985. return false;
  986. }
  987. return [
  988. 'order_id' => (int)$order['id'],
  989. 'work_id' => (int)$order['work_id'],
  990. ];
  991. }
  992. }