ServiceOrderLogic.php 40 KB

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