ServiceOrderLogic.php 38 KB

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