ServiceOrderLogic.php 40 KB

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