ServiceOrderLogic.php 38 KB

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