ServiceOrderLogic.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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. //判断是否存在优惠券
  517. $is_coupon_used = \app\common\model\recharge\RechargeOrder::where([
  518. 'order_type' => 0,
  519. 'user_id' => $params['user_id'],
  520. 'sn'=>$params['sn']
  521. ])->findOrEmpty();
  522. if(!empty($is_coupon_used['coupon_id']) && $is_coupon_used['pay_status'] == 0){
  523. $user_coupon = UserCoupon::where(['user_id'=>$is_coupon_used->user_id,'id'=>$is_coupon_used->coupon_id])->findOrEmpty();
  524. $user_coupon->voucher_status = 0;
  525. $user_coupon->voucher_count = $user_coupon->voucher_count+1;
  526. $user_coupon->save();
  527. }
  528. $order_info = \app\common\model\recharge\RechargeOrder::with(['order_goods'=>function ($query) {
  529. $query->visible(['goods_payment_type','goods_category_id']);
  530. },'service_work'=>function ($query) {
  531. $query->visible(['service_fee','spare_total','service_work_spare_id','goods_category_id']);
  532. }])
  533. ->visible(['id','sn','work_id'])
  534. ->where([
  535. 'order_type' => 0,
  536. 'user_id' => $params['user_id'],
  537. 'sn'=>$params['sn']
  538. ])->findOrEmpty()->toArray();
  539. if(empty($order_info)){
  540. throw new Exception('订单不存在');
  541. }
  542. //查询总价
  543. $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');
  544. $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');
  545. //搜索当前工单下的所有订单记录
  546. $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();
  547. $pay_status_data = DictData::where('type_value','pay_status')->column('name','value');
  548. $payment_type_data = DictData::where('type_value','payment_type')->column('name','value');
  549. $pay_way_data = DictData::where('type_value','pay_way')->column('name','value');
  550. $coupon_price = 0;
  551. //退款金额
  552. $order_refund_amount = 0;
  553. $payment_type = 0;
  554. foreach ($order_info['pay_orders'] as $k=>&$v){
  555. $v['pay_status_name'] = $pay_status_data[$v['pay_status']];
  556. $v['payment_type_name'] = $payment_type_data[$v['payment_type']];
  557. $v['pay_way_name'] = $pay_way_data[$v['pay_way']];
  558. if($v['refund_status']==1){
  559. $order_refund_amount += $v['order_amount'];
  560. $refund_status = RefundRecord::where('order_id',$v['order_id'])->value('refund_status');
  561. }
  562. if($v['payment_type']!=1 and !empty($order_info['service_work']['spare_total'])){
  563. $v['order_total'] = $v['order_total'] - $order_info['service_work']['spare_total'];
  564. $v['order_amount'] = $v['order_amount'] - $order_info['service_work']['spare_total'] + $v['coupon_price'];
  565. }
  566. $coupon_price += $v['coupon_price'];
  567. $payment_type = $v['payment_type'];
  568. }
  569. $order_info['payment_type'] = $payment_type;
  570. //汇总优惠卷额度
  571. $order_info['coupon_price'] = $coupon_price;
  572. //退款汇总
  573. $order_info['refund_amount'] = $order_refund_amount;
  574. if(isset($refund_status)){
  575. switch ($refund_status){
  576. case 0:
  577. $order_info['refund_status'] = '退款中';
  578. break;
  579. case 1:
  580. $order_info['refund_status'] = '退款成功';
  581. break;
  582. case 2:
  583. $order_info['refund_status'] = '退款失败';
  584. break;
  585. }
  586. }else{
  587. $order_info['refund_status'] = '未退款';
  588. }
  589. // 所有配件
  590. $order_info['spare_total'] = $order_info['service_work']['spare_total']??0;
  591. $order_info['spare_parts'] = [];
  592. if($order_info['service_work']['service_work_spare_id']){
  593. $work_spare_parts = json_decode(ServiceWorkSpare::where('id',$order_info['service_work']['service_work_spare_id'])->value('spare_parts'),true);
  594. $spare_parts = SparePart::where('id','in',array_column($work_spare_parts,'id'))
  595. ->field(['id', 'goods_category_id', 'spare_name', 'spare_image', 'spare_number', 'spare_unit','spare_status'])
  596. ->select()
  597. ->toArray();
  598. $spare_parts = array_column($spare_parts,null,'id');
  599. foreach (array_column($work_spare_parts,null,'id') as $k=>&$v){
  600. $spare_parts[$k] = array_merge($spare_parts[$k],$v);
  601. }
  602. $order_info['spare_parts'] = array_values($spare_parts)??[];
  603. }
  604. //获取是否存在可使用优惠券
  605. $coupon_ids = CouponCategory::where('goods_category_id',$order_info['service_work']['goods_category_id'])
  606. ->whereIn('coupon_id',UserCoupon::where('user_id',$params['user_id'])
  607. ->where('voucher_count','>',0)
  608. ->where('voucher_status',0)
  609. ->where('expire_time','>',time())
  610. ->where('amount_require','<=',$order_info['order_amount'])
  611. ->column('coupon_id')
  612. )->column('coupon_id');
  613. $usable_coupon = UserCoupon::where('user_id',$params['user_id'])
  614. ->whereIn('coupon_id',$coupon_ids)
  615. ->append(['discount_ratio_text'])
  616. ->where('expire_time','>',time())
  617. ->count();
  618. $order_info['usable_coupon'] = $usable_coupon>0?true:false;
  619. return $order_info;
  620. }
  621. catch (\Exception $e) {
  622. self::setError($e->getMessage());
  623. return false;
  624. }
  625. }
  626. /**
  627. * 取消订单
  628. * @param $params
  629. * @return false|void
  630. */
  631. public static function cancelOrder($params)
  632. {
  633. Db::startTrans();
  634. try {
  635. $work_id = \app\common\model\recharge\RechargeOrder::where([
  636. 'order_type' => 0,
  637. 'user_id' => $params['user_id'],
  638. 'sn'=>$params['sn']
  639. ])->value('work_id');
  640. if(empty($work_id)){
  641. throw new Exception('订单不存在');
  642. }
  643. $payed_order = \app\common\model\recharge\RechargeOrder::where(['user_id'=>$params['user_id'],'work_id'=>$work_id,'pay_status'=>1])->findOrEmpty();
  644. if(!$payed_order->isEmpty()){
  645. throw new Exception('存在已支付订单,不允许取消订单,请联系客服');
  646. }
  647. //软删除订单
  648. $cancel_order = \app\common\model\recharge\RechargeOrder::where(['user_id'=>$params['user_id'],'work_id'=>$work_id])->findOrEmpty();
  649. $cancel_order->pay_status = 2;
  650. $cancel_order->save();
  651. //更新工单状态为已取消
  652. $service_work = ServiceWork::find($work_id);
  653. $service_work->service_status = 4;
  654. $service_work->save();
  655. //判断如果存在优惠券,返还优惠券
  656. if(!empty($payed_order->coupon_id)){
  657. $user_coupon = UserCoupon::where(['user_id'=>$payed_order->user_id,'id'=>$payed_order->coupon_id])->findOrEmpty();
  658. $user_coupon->voucher_status = 0;
  659. $user_coupon->voucher_count = $user_coupon->voucher_count+1;
  660. $user_coupon->save();
  661. }
  662. Db::commit();
  663. }
  664. catch (\Exception $e) {
  665. self::setError($e->getMessage());
  666. return false;
  667. }
  668. }
  669. /**
  670. * 用户确认尾款报价单
  671. * @param $params
  672. * @return false|void
  673. */
  674. public static function confirmOrder($params)
  675. {
  676. Db::startTrans();
  677. try {
  678. $work_id = \app\common\model\recharge\RechargeOrder::where([
  679. 'order_type' => 0,
  680. 'user_id' => $params['user_id'],
  681. 'sn'=>$params['sn']
  682. ])->value('work_id');
  683. if(empty($work_id)){
  684. throw new Exception('订单不存在');
  685. }
  686. //更新工单状态为已取消
  687. $service_work = ServiceWork::find($work_id);
  688. if($service_work->user_confirm_status==2){
  689. throw new Exception('请勿重复操作');
  690. }
  691. $service_work->work_status = 5;
  692. $service_work->user_confirm_status = 2;
  693. $service_work->save();
  694. $work_log = [
  695. 'work_id'=>$work_id,
  696. 'master_worker_id'=>$service_work->master_worker_id,
  697. 'opera_log'=>'用户'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'确认了报价单',
  698. ];
  699. ServiceWorkLogLogic::add($work_log);
  700. Db::commit();
  701. }
  702. catch (\Exception $e) {
  703. self::setError($e->getMessage());
  704. return false;
  705. }
  706. }
  707. /**
  708. * 用户确认服务完成
  709. * @param $params
  710. * @return false|void
  711. */
  712. public static function confirmServiceFinish($params)
  713. {
  714. Db::startTrans();
  715. try {
  716. $work_id = \app\common\model\recharge\RechargeOrder::where([
  717. 'order_type' => 0,
  718. 'user_id' => $params['user_id'],
  719. 'sn'=>$params['sn']
  720. ])->value('work_id');
  721. if(empty($work_id)){
  722. throw new Exception('订单不存在');
  723. }
  724. $service_work = ServiceWork::find($work_id);
  725. if($service_work->user_confirm_status!=3){
  726. throw new Exception('请勿重复操作');
  727. }
  728. $orders = RechargeOrder::where(['work_id'=>$work_id,'user_id'=>$params['user_id']])->select()->toArray();
  729. //确认所有订单总金额和结算金额
  730. //若订单是全款已支付订单
  731. if(count($orders)==1 and $orders[0]['payment_type']==0 and $orders[0]['pay_status']==1){
  732. $service_work->work_status = 7;
  733. $service_work->user_confirm_status = 5;
  734. $service_work->service_status = 3;
  735. $service_work->work_pay_status = 1;
  736. }else{
  737. $service_work->work_status = 6;
  738. $service_work->user_confirm_status = 4;
  739. }
  740. $service_work->finished_time = time();
  741. $service_work->save();
  742. //更新工程师的进行工单数量
  743. MasterWorker::setWorktotal('dec',$service_work->master_worker_id);
  744. $work_log = [
  745. 'work_id'=>$work_id,
  746. 'master_worker_id'=>$service_work->master_worker_id,
  747. 'opera_log'=>'用户'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'确认服务完成',
  748. ];
  749. ServiceWorkLogLogic::add($work_log);
  750. Db::commit();
  751. }
  752. catch (\Exception $e) {
  753. self::setError($e->getMessage());
  754. return false;
  755. }
  756. }
  757. public static function approvalChangePrice($params)
  758. {
  759. Db::startTrans();
  760. try {
  761. $order = RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  762. if($order->isEmpty()){
  763. throw new Exception('订单不存在');
  764. }
  765. $work = ServiceWork::findOrEmpty($order->work_id);
  766. if($work->isEmpty()){
  767. throw new Exception('工单不存在');
  768. }
  769. $work->work_status = 4;
  770. $work->user_confirm_status = 0;
  771. $work->price_approval = 1;
  772. $work->save();
  773. Db::commit();
  774. }
  775. catch (\Exception $e) {
  776. self::setError($e->getMessage());
  777. return false;
  778. }
  779. }
  780. public static function approvalChangeAppointment($params)
  781. {
  782. Db::startTrans();
  783. try {
  784. $order = RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  785. if($order->isEmpty()){
  786. throw new Exception('订单不存在');
  787. }
  788. $work = ServiceWork::findOrEmpty($order->work_id);
  789. if($work->isEmpty()){
  790. throw new Exception('工单不存在');
  791. }
  792. //更新工单未确认上门的状态
  793. $work->work_status = 3;
  794. $work->user_confirm_status = 0;
  795. ServiceWorkAppointmentLog::create([
  796. 'work_id'=>$work->id,
  797. 'last_appointment_time'=>strtotime($work->appointment_time),
  798. 'this_appointment_time'=>strtotime($params['appointment_time']),
  799. ]);
  800. $work->appointment_time = strtotime($params['appointment_time']);
  801. $work->appoint_approval = 1;
  802. $work->save();
  803. Db::commit();
  804. }
  805. catch (\Exception $e) {
  806. self::setError($e->getMessage());
  807. return false;
  808. }
  809. return $work;
  810. }
  811. public static function approvalRefund($params)
  812. {
  813. Db::startTrans();
  814. try {
  815. $order = RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  816. if($order->isEmpty()){
  817. throw new Exception('订单不存在');
  818. }
  819. $work = ServiceWork::findOrEmpty($order->work_id);
  820. if($work->isEmpty()){
  821. throw new Exception('工单不存在');
  822. }
  823. $work->refund_approval = 1;
  824. $work->save();
  825. //判断是否已下单时间过了两小时,并且师傅暂未上门,距离预约时间两小时以上
  826. if(($order['pay_time']+7200)>time() and $work['work_status']<4 and (strtotime($work['appointment_time'])-7200)>time()){
  827. //生成退款订单
  828. \app\common\model\recharge\RechargeOrder::update([
  829. 'id' => $order['id'],
  830. 'refund_status' => YesNoEnum::YES,
  831. ]);
  832. // 生成退款记录
  833. $recordSn = generate_sn(RefundRecord::class, 'sn');
  834. $record = RefundRecord::create([
  835. 'sn' => $recordSn,
  836. 'user_id' => $order['user_id'],
  837. 'order_id' => $order['id'],
  838. 'order_sn' => $order['sn'],
  839. 'order_type' => RefundEnum::ORDER_TYPE_ORDER,
  840. 'order_amount' => $order['order_amount'],
  841. 'refund_amount' => $order['order_amount'],
  842. 'refund_type' => RefundEnum::TYPE_ADMIN,
  843. 'transaction_id' => $order['transaction_id'] ?? '',
  844. 'refund_way' => RefundEnum::getRefundWayByPayWay($order['pay_way']),
  845. ]);
  846. //更新工单状态
  847. ServiceWork::update([
  848. 'id'=>$order['work_id'],
  849. 'service_status'=>5
  850. ]);
  851. // 退款
  852. RefundLogic::refund($order, $record['id'], $order['order_amount'], 1);
  853. }
  854. Db::commit();
  855. }
  856. catch (\Exception $e) {
  857. self::setError($e->getMessage());
  858. return false;
  859. }
  860. }
  861. /**
  862. * 订单完成通知【给用户】 - 全款 -通知
  863. * @param $params
  864. * @return bool
  865. */
  866. public static function serviceFinishNotice($params)
  867. {
  868. try {
  869. $order = RechargeOrder::where('sn', $params['sn'])
  870. ->where('payment_type','=',0)
  871. ->where('pay_status','=',1)
  872. ->findOrEmpty();
  873. if(!$order->isEmpty()){
  874. event('Notice', [
  875. 'scene_id' => 120,
  876. 'params' => [
  877. 'user_id' => $order['user_id']
  878. ]
  879. ]);
  880. }
  881. return true;
  882. }catch (\Exception $e) {
  883. return false;
  884. }
  885. }
  886. }