ServiceOrderLogic.php 39 KB

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