ServiceOrderLogic.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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'])->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. return $order_info;
  464. }
  465. catch (\Exception $e) {
  466. self::setError($e->getMessage());
  467. return false;
  468. }
  469. }
  470. /**
  471. * 获取订单支付详情
  472. * @param $params
  473. * @return array|false
  474. */
  475. public static function orderPayInfo($params)
  476. {
  477. try {
  478. $order_info = \app\common\model\recharge\RechargeOrder::with(['order_goods'=>function ($query) {
  479. $query->visible(['goods_payment_type','goods_category_id']);
  480. },'service_work'=>function ($query) {
  481. $query->visible(['service_fee','spare_total','service_work_spare_id']);
  482. }])
  483. ->visible(['id','sn','work_id'])
  484. ->where([
  485. 'order_type' => 0,
  486. 'user_id' => $params['user_id'],
  487. 'sn'=>$params['sn']
  488. ])->findOrEmpty()->toArray();
  489. if(empty($order_info)){
  490. throw new Exception('订单不存在');
  491. }
  492. //查询总价
  493. $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');
  494. $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');
  495. //搜索当前工单下的所有订单记录
  496. $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();
  497. $pay_status_data = DictData::where('type_value','pay_status')->column('name','value');
  498. $payment_type_data = DictData::where('type_value','payment_type')->column('name','value');
  499. $pay_way_data = DictData::where('type_value','pay_way')->column('name','value');
  500. $coupon_price = 0;
  501. //退款金额
  502. $order_refund_amount = 0;
  503. $payment_type = 0;
  504. foreach ($order_info['pay_orders'] as $k=>&$v){
  505. $v['pay_status_name'] = $pay_status_data[$v['pay_status']];
  506. $v['payment_type_name'] = $payment_type_data[$v['payment_type']];
  507. $v['pay_way_name'] = $pay_way_data[$v['pay_way']];
  508. if($v['refund_status']==1){
  509. $order_refund_amount += $v['order_amount'];
  510. $refund_status = RefundRecord::where('order_id',$v['order_id'])->value('refund_status');
  511. }
  512. if($v['payment_type']!=1 and !empty($order_info['service_work']['spare_total'])){
  513. $v['order_total'] = $v['order_total'] - $order_info['service_work']['spare_total'];
  514. $v['order_amount'] = $v['order_amount'] - $order_info['service_work']['spare_total'] + $v['coupon_price'];
  515. }
  516. $coupon_price += $v['coupon_price'];
  517. $payment_type = $v['payment_type'];
  518. }
  519. $order_info['payment_type'] = $payment_type;
  520. //汇总优惠卷额度
  521. $order_info['coupon_price'] = $coupon_price;
  522. //退款汇总
  523. $order_info['refund_amount'] = $order_refund_amount;
  524. if(isset($refund_status)){
  525. switch ($refund_status){
  526. case 0:
  527. $order_info['refund_status'] = '退款中';
  528. break;
  529. case 1:
  530. $order_info['refund_status'] = '退款成功';
  531. break;
  532. case 2:
  533. $order_info['refund_status'] = '退款失败';
  534. break;
  535. }
  536. }else{
  537. $order_info['refund_status'] = '未退款';
  538. }
  539. // 所有配件
  540. $order_info['spare_total'] = $order_info['service_work']['spare_total']??0;
  541. $order_info['spare_parts'] = [];
  542. if($order_info['service_work']['service_work_spare_id']){
  543. $work_spare_parts = json_decode(ServiceWorkSpare::where('id',$order_info['service_work']['service_work_spare_id'])->value('spare_parts'),true);
  544. $spare_parts = SparePart::where('id','in',array_column($work_spare_parts,'id'))
  545. ->field(['id', 'goods_category_id', 'spare_name', 'spare_image', 'spare_number', 'spare_unit','spare_status'])
  546. ->select()
  547. ->toArray();
  548. $spare_parts = array_column($spare_parts,null,'id');
  549. foreach (array_column($work_spare_parts,null,'id') as $k=>&$v){
  550. $spare_parts[$k] = array_merge($spare_parts[$k],$v);
  551. }
  552. $order_info['spare_parts'] = array_values($spare_parts)??[];
  553. }
  554. return $order_info;
  555. }
  556. catch (\Exception $e) {
  557. self::setError($e->getMessage());
  558. return false;
  559. }
  560. }
  561. /**
  562. * 取消订单
  563. * @param $params
  564. * @return false|void
  565. */
  566. public static function cancelOrder($params)
  567. {
  568. Db::startTrans();
  569. try {
  570. $work_id = \app\common\model\recharge\RechargeOrder::where([
  571. 'order_type' => 0,
  572. 'user_id' => $params['user_id'],
  573. 'sn'=>$params['sn']
  574. ])->value('work_id');
  575. if(empty($work_id)){
  576. throw new Exception('订单不存在');
  577. }
  578. $payed_order = \app\common\model\recharge\RechargeOrder::where(['user_id'=>$params['user_id'],'work_id'=>$work_id,'pay_status'=>1])->findOrEmpty();
  579. if(!$payed_order->isEmpty()){
  580. throw new Exception('存在已支付订单,不允许取消订单,请联系客服');
  581. }
  582. //软删除订单
  583. $cancel_order = \app\common\model\recharge\RechargeOrder::where(['user_id'=>$params['user_id'],'work_id'=>$work_id])->findOrEmpty();
  584. $cancel_order->pay_status = 2;
  585. $cancel_order->save();
  586. //更新工单状态为已取消
  587. $service_work = ServiceWork::find($work_id);
  588. $service_work->service_status = 4;
  589. $service_work->save();
  590. //判断如果存在优惠券,返还优惠券
  591. if(!empty($payed_order->coupon_id)){
  592. $user_coupon = UserCoupon::where(['user_id'=>$payed_order->user_id,'id'=>$payed_order->coupon_id])->findOrEmpty();
  593. $user_coupon->voucher_status = 0;
  594. $user_coupon->voucher_count = $user_coupon->voucher_count+1;
  595. $user_coupon->save();
  596. }
  597. Db::commit();
  598. }
  599. catch (\Exception $e) {
  600. self::setError($e->getMessage());
  601. return false;
  602. }
  603. }
  604. /**
  605. * 用户确认尾款报价单
  606. * @param $params
  607. * @return false|void
  608. */
  609. public static function confirmOrder($params)
  610. {
  611. Db::startTrans();
  612. try {
  613. $work_id = \app\common\model\recharge\RechargeOrder::where([
  614. 'order_type' => 0,
  615. 'user_id' => $params['user_id'],
  616. 'sn'=>$params['sn']
  617. ])->value('work_id');
  618. if(empty($work_id)){
  619. throw new Exception('订单不存在');
  620. }
  621. //更新工单状态为已取消
  622. $service_work = ServiceWork::find($work_id);
  623. if($service_work->user_confirm_status==2){
  624. throw new Exception('请勿重复操作');
  625. }
  626. $service_work->work_status = 5;
  627. $service_work->user_confirm_status = 2;
  628. $service_work->save();
  629. $work_log = [
  630. 'work_id'=>$work_id,
  631. 'master_worker_id'=>$service_work->master_worker_id,
  632. 'opera_log'=>'用户'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'确认了报价单',
  633. ];
  634. ServiceWorkLogLogic::add($work_log);
  635. Db::commit();
  636. }
  637. catch (\Exception $e) {
  638. self::setError($e->getMessage());
  639. return false;
  640. }
  641. }
  642. /**
  643. * 用户确认服务完成
  644. * @param $params
  645. * @return false|void
  646. */
  647. public static function confirmServiceFinish($params)
  648. {
  649. Db::startTrans();
  650. try {
  651. $work_id = \app\common\model\recharge\RechargeOrder::where([
  652. 'order_type' => 0,
  653. 'user_id' => $params['user_id'],
  654. 'sn'=>$params['sn']
  655. ])->value('work_id');
  656. if(empty($work_id)){
  657. throw new Exception('订单不存在');
  658. }
  659. $service_work = ServiceWork::find($work_id);
  660. if($service_work->user_confirm_status!=3){
  661. throw new Exception('请勿重复操作');
  662. }
  663. $orders = RechargeOrder::where(['work_id'=>$work_id,'user_id'=>$params['user_id']])->select()->toArray();
  664. //确认所有订单总金额和结算金额
  665. //若订单是全款已支付订单
  666. if(count($orders)==1 and $orders[0]['payment_type']==0 and $orders[0]['pay_status']==1){
  667. $service_work->work_status = 7;
  668. $service_work->user_confirm_status = 5;
  669. $service_work->service_status = 3;
  670. $service_work->work_pay_status = 1;
  671. }else{
  672. $service_work->work_status = 6;
  673. $service_work->user_confirm_status = 4;
  674. }
  675. $service_work->finished_time = time();
  676. $service_work->save();
  677. //更新工程师的进行工单数量
  678. MasterWorker::setWorktotal('dec',$service_work->master_worker_id);
  679. $work_log = [
  680. 'work_id'=>$work_id,
  681. 'master_worker_id'=>$service_work->master_worker_id,
  682. 'opera_log'=>'用户'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'确认服务完成',
  683. ];
  684. ServiceWorkLogLogic::add($work_log);
  685. Db::commit();
  686. }
  687. catch (\Exception $e) {
  688. self::setError($e->getMessage());
  689. return false;
  690. }
  691. }
  692. public static function approvalChangePrice($params)
  693. {
  694. Db::startTrans();
  695. try {
  696. $order = RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  697. if($order->isEmpty()){
  698. throw new Exception('订单不存在');
  699. }
  700. $work = ServiceWork::findOrEmpty($order->work_id);
  701. if($work->isEmpty()){
  702. throw new Exception('工单不存在');
  703. }
  704. $work->price_approval = 1;
  705. $work->save();
  706. Db::commit();
  707. }
  708. catch (\Exception $e) {
  709. self::setError($e->getMessage());
  710. return false;
  711. }
  712. }
  713. public static function approvalChangeAppointment($params)
  714. {
  715. Db::startTrans();
  716. try {
  717. $order = RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  718. if($order->isEmpty()){
  719. throw new Exception('订单不存在');
  720. }
  721. $work = ServiceWork::findOrEmpty($order->work_id);
  722. if($work->isEmpty()){
  723. throw new Exception('工单不存在');
  724. }
  725. $work->appointment_time = strtotime($params['appointment_time']);
  726. //更新工单未确认上门的状态
  727. $work->work_status = 3;
  728. $work->user_confirm_status = 0;
  729. ServiceWorkAppointmentLog::create([
  730. 'work_id'=>$work->id,
  731. 'last_appointment_time'=>$work->appointment_time,
  732. 'this_appointment_time'=>strtotime($params['appointment_time']),
  733. ]);
  734. $work->save();
  735. Db::commit();
  736. }
  737. catch (\Exception $e) {
  738. self::setError($e->getMessage());
  739. return false;
  740. }
  741. }
  742. public static function approvalRefund($params)
  743. {
  744. Db::startTrans();
  745. try {
  746. $order = RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  747. if($order->isEmpty()){
  748. throw new Exception('订单不存在');
  749. }
  750. $work = ServiceWork::findOrEmpty($order->work_id);
  751. if($work->isEmpty()){
  752. throw new Exception('工单不存在');
  753. }
  754. $work->refund_approval = 1;
  755. $work->save();
  756. //判断是否已下单时间过了两小时,并且师傅暂未上门,距离预约时间两小时以上
  757. if(($order['pay_time']+7200)>time() and $work['work_status']<4 and ($work['appointment_time']-7200)>time()){
  758. //生成退款订单
  759. \app\common\model\recharge\RechargeOrder::update([
  760. 'id' => $order['id'],
  761. 'refund_status' => YesNoEnum::YES,
  762. ]);
  763. // 生成退款记录
  764. $recordSn = generate_sn(RefundRecord::class, 'sn');
  765. $record = RefundRecord::create([
  766. 'sn' => $recordSn,
  767. 'user_id' => $order['user_id'],
  768. 'order_id' => $order['id'],
  769. 'order_sn' => $order['sn'],
  770. 'order_type' => RefundEnum::ORDER_TYPE_ORDER,
  771. 'order_amount' => $order['order_amount'],
  772. 'refund_amount' => $order['order_amount'],
  773. 'refund_type' => RefundEnum::TYPE_ADMIN,
  774. 'transaction_id' => $order['transaction_id'] ?? '',
  775. 'refund_way' => RefundEnum::getRefundWayByPayWay($order['pay_way']),
  776. ]);
  777. //更新工单状态
  778. ServiceWork::update([
  779. 'id'=>$order['work_id'],
  780. 'service_status'=>5
  781. ]);
  782. // 退款
  783. RefundLogic::refund($order, $record['id'], $order['order_amount'], 1);
  784. }
  785. Db::commit();
  786. }
  787. catch (\Exception $e) {
  788. self::setError($e->getMessage());
  789. return false;
  790. }
  791. }
  792. }