ServiceOrderLogic.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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'] = !empty($worker)?$worker['avatar']:'';
  399. $order_info['master_worker']['real_name'] = !empty($worker)?$worker['real_name']:'';
  400. $order_info['master_worker']['worker_number'] = !empty($worker)?$worker['worker_number']:'';
  401. $order_info['master_worker']['mobile'] = !empty($worker)?$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. $payment_type = 0;
  500. foreach ($order_info['pay_orders'] as $k=>&$v){
  501. $v['pay_status_name'] = $pay_status_data[$v['pay_status']];
  502. $v['payment_type_name'] = $payment_type_data[$v['payment_type']];
  503. $v['pay_way_name'] = $pay_way_data[$v['pay_way']];
  504. if($v['refund_status']==1){
  505. $order_refund_amount += $v['order_amount'];
  506. $refund_status = RefundRecord::where('order_id',$v['order_id'])->value('refund_status');
  507. }
  508. if($v['payment_type']!=1 and !empty($order_info['service_work']['spare_total'])){
  509. $v['order_total'] = $v['order_total'] - $order_info['service_work']['spare_total'];
  510. $v['order_amount'] = $v['order_amount'] - $order_info['service_work']['spare_total'] + $v['coupon_price'];
  511. }
  512. $coupon_price += $v['coupon_price'];
  513. $payment_type = $v['payment_type'];
  514. }
  515. $order_info['payment_type'] = $payment_type;
  516. //汇总优惠卷额度
  517. $order_info['coupon_price'] = $coupon_price;
  518. //退款汇总
  519. $order_info['refund_amount'] = $order_refund_amount;
  520. if(isset($refund_status)){
  521. switch ($refund_status){
  522. case 0:
  523. $order_info['refund_status'] = '退款中';
  524. break;
  525. case 1:
  526. $order_info['refund_status'] = '退款成功';
  527. break;
  528. case 2:
  529. $order_info['refund_status'] = '退款失败';
  530. break;
  531. }
  532. }else{
  533. $order_info['refund_status'] = '未退款';
  534. }
  535. // 所有配件
  536. $order_info['spare_total'] = $order_info['service_work']['spare_total']??0;
  537. $order_info['spare_parts'] = [];
  538. if($order_info['service_work']['service_work_spare_id']){
  539. $work_spare_parts = json_decode(ServiceWorkSpare::where('id',$order_info['service_work']['service_work_spare_id'])->value('spare_parts'),true);
  540. $spare_parts = SparePart::where('id','in',array_column($work_spare_parts,'id'))
  541. ->field(['id', 'goods_category_id', 'spare_name', 'spare_image', 'spare_number', 'spare_unit','spare_status'])
  542. ->select()
  543. ->toArray();
  544. $spare_parts = array_column($spare_parts,null,'id');
  545. foreach (array_column($work_spare_parts,null,'id') as $k=>&$v){
  546. $spare_parts[$k] = array_merge($spare_parts[$k],$v);
  547. }
  548. $order_info['spare_parts'] = array_values($spare_parts)??[];
  549. }
  550. return $order_info;
  551. }
  552. catch (\Exception $e) {
  553. self::setError($e->getMessage());
  554. return false;
  555. }
  556. }
  557. /**
  558. * 取消订单
  559. * @param $params
  560. * @return false|void
  561. */
  562. public static function cancelOrder($params)
  563. {
  564. Db::startTrans();
  565. try {
  566. $work_id = \app\common\model\recharge\RechargeOrder::where([
  567. 'order_type' => 0,
  568. 'user_id' => $params['user_id'],
  569. 'sn'=>$params['sn']
  570. ])->value('work_id');
  571. if(empty($work_id)){
  572. throw new Exception('订单不存在');
  573. }
  574. $payed_order = \app\common\model\recharge\RechargeOrder::where(['user_id'=>$params['user_id'],'work_id'=>$work_id,'pay_status'=>1])->findOrEmpty();
  575. if(!$payed_order->isEmpty()){
  576. throw new Exception('存在已支付订单,不允许取消订单,请联系客服');
  577. }
  578. //软删除订单
  579. $cancel_order = \app\common\model\recharge\RechargeOrder::where(['user_id'=>$params['user_id'],'work_id'=>$work_id])->findOrEmpty();
  580. $cancel_order->pay_status = 2;
  581. $cancel_order->save();
  582. //更新工单状态为已取消
  583. $service_work = ServiceWork::find($work_id);
  584. $service_work->service_status = 4;
  585. $service_work->save();
  586. //判断如果存在优惠券,返还优惠券
  587. if(!empty($payed_order->coupon_id)){
  588. $user_coupon = UserCoupon::where(['user_id'=>$payed_order->user_id,'id'=>$payed_order->coupon_id])->findOrEmpty();
  589. $user_coupon->voucher_status = 0;
  590. $user_coupon->voucher_count = $user_coupon->voucher_count+1;
  591. $user_coupon->save();
  592. }
  593. Db::commit();
  594. }
  595. catch (\Exception $e) {
  596. self::setError($e->getMessage());
  597. return false;
  598. }
  599. }
  600. /**
  601. * 用户确认尾款报价单
  602. * @param $params
  603. * @return false|void
  604. */
  605. public static function confirmOrder($params)
  606. {
  607. Db::startTrans();
  608. try {
  609. $work_id = \app\common\model\recharge\RechargeOrder::where([
  610. 'order_type' => 0,
  611. 'user_id' => $params['user_id'],
  612. 'sn'=>$params['sn']
  613. ])->value('work_id');
  614. if(empty($work_id)){
  615. throw new Exception('订单不存在');
  616. }
  617. //更新工单状态为已取消
  618. $service_work = ServiceWork::find($work_id);
  619. if($service_work->user_confirm_status==2){
  620. throw new Exception('请勿重复操作');
  621. }
  622. $service_work->work_status = 5;
  623. $service_work->user_confirm_status = 2;
  624. $service_work->save();
  625. $work_log = [
  626. 'work_id'=>$work_id,
  627. 'master_worker_id'=>$service_work->master_worker_id,
  628. 'opera_log'=>'用户'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'确认了报价单',
  629. ];
  630. ServiceWorkLogLogic::add($work_log);
  631. Db::commit();
  632. }
  633. catch (\Exception $e) {
  634. self::setError($e->getMessage());
  635. return false;
  636. }
  637. }
  638. /**
  639. * 用户确认服务完成
  640. * @param $params
  641. * @return false|void
  642. */
  643. public static function confirmServiceFinish($params)
  644. {
  645. Db::startTrans();
  646. try {
  647. $work_id = \app\common\model\recharge\RechargeOrder::where([
  648. 'order_type' => 0,
  649. 'user_id' => $params['user_id'],
  650. 'sn'=>$params['sn']
  651. ])->value('work_id');
  652. if(empty($work_id)){
  653. throw new Exception('订单不存在');
  654. }
  655. $service_work = ServiceWork::find($work_id);
  656. if($service_work->user_confirm_status!=3){
  657. throw new Exception('请勿重复操作');
  658. }
  659. $orders = RechargeOrder::where(['work_id'=>$work_id,'user_id'=>$params['user_id']])->select()->toArray();
  660. //确认所有订单总金额和结算金额
  661. //若订单是全款已支付订单
  662. if(count($orders)==1 and $orders[0]['payment_type']==0 and $orders[0]['pay_status']==1){
  663. $service_work->work_status = 7;
  664. $service_work->user_confirm_status = 5;
  665. $service_work->service_status = 3;
  666. $service_work->work_pay_status = 1;
  667. }else{
  668. $service_work->work_status = 6;
  669. $service_work->user_confirm_status = 4;
  670. }
  671. $service_work->finished_time = time();
  672. $service_work->save();
  673. //更新师傅的进行工单数量
  674. MasterWorker::setWorktotal('dec',$service_work->master_worker_id);
  675. $work_log = [
  676. 'work_id'=>$work_id,
  677. 'master_worker_id'=>$service_work->master_worker_id,
  678. 'opera_log'=>'用户'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'确认服务完成',
  679. ];
  680. ServiceWorkLogLogic::add($work_log);
  681. Db::commit();
  682. }
  683. catch (\Exception $e) {
  684. self::setError($e->getMessage());
  685. return false;
  686. }
  687. }
  688. public static function firmOrderSave($params)
  689. {
  690. Db::startTrans();
  691. try {
  692. $goodIds = array_column($params['order_goods'],'id');
  693. $goodInfo = Goods::whereIn('id',$goodIds)->select();
  694. $ids = $goodInfo->column('id');
  695. if(!empty(array_diff($goodIds,$ids))){
  696. throw new Exception('产品不存在!');
  697. }
  698. if(empty($params['user_info']['mobile'])){
  699. throw new Exception('请先补充您的联系方式后在提交订单');
  700. }
  701. $order_amount = 0;
  702. $order_total = 0;
  703. $orderGoodsData = [];
  704. $goodsPaymentTypeArr = [];
  705. //计算订单总金额和结算金额
  706. foreach($params['order_goods'] as $val){
  707. $goods = $goodInfo->where('id',$val['id'])->toArray()[0];
  708. //根据服务工单计算当前订单应支付金额
  709. $order_total = $goods['service_total'];
  710. if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE){
  711. //一口价订单
  712. $order_amount = $goods['service_fee']*$val['goods_number'];
  713. }else{
  714. $order_total = $goods['base_service_fee']*$val['goods_number'];
  715. $order_amount = $goods['service_fee']*$val['goods_number'];
  716. }
  717. $goodsPaymentTypeArr[] = $goods['goods_payment_type'];
  718. $orderGoodsData[] = [
  719. 'goods_id' => $goods['id'],
  720. 'category_type' => $goods['category_type'],
  721. 'goods_category_ids' => $goods['goods_category_ids'],
  722. 'goods_category_id' => $goods['goods_category_id'],
  723. 'goods_name' => $goods['goods_name'],
  724. 'goods_image' => $goods['goods_image'],
  725. 'goods_video' => $goods['goods_video'],
  726. 'goods_number' => $val['goods_number'],
  727. 'good_unit' => $goods['good_unit'],
  728. 'goods_size' => $goods['goods_size'],
  729. 'goods_type' => $goods['goods_type'],
  730. 'goods_brand' => $goods['goods_brand'],
  731. 'install_guide' => $goods['install_guide'],
  732. 'goods_payment_type'=>$goods['goods_payment_type'],
  733. 'base_service_fee' => $goods['base_service_fee'],
  734. 'service_total' => $goods['service_total'],
  735. 'service_fee' => $goods['service_fee'],
  736. 'service_image' => $goods['service_image'],
  737. 'warranty_period'=>$goods['warranty_period'],
  738. 'fee_schedule' => $goods['fee_schedule'],
  739. 'goods_status' => $goods['goods_status'],
  740. ];
  741. }
  742. if(count(array_unique($goodsPaymentTypeArr))>1){
  743. throw new Exception('订单中存在多种支付方式');
  744. }
  745. //生成服务工单
  746. $work_data = [
  747. 'work_sn' => generate_sn(ServiceWork::class, 'work_sn'),
  748. 'real_name' => $params['contact_people'],
  749. 'mobile' => $params['contact_number'],
  750. 'address' => $params['address'],
  751. 'title' => $goods['goods_name'] . '*' .$val['goods_number'].$goods['good_unit'],
  752. 'category_type' => $goods['category_type'],
  753. 'goods_category_ids' => $goods['goods_category_ids'],
  754. 'goods_category_id' => $goods['goods_category_id'],
  755. 'base_service_fee' => $goods['base_service_fee'],
  756. 'service_fee' => $goods['service_fee'],
  757. 'work_pay_status'=>WorkEnum::UN_PAY_STATUS,
  758. 'appointment_time' => strtotime($params['appointment_time']),
  759. 'user_id' => $params['user_id'],
  760. 'work_type' => 1,
  761. ];
  762. $service_work = ServiceWork::create($work_data);
  763. //生成服务订单
  764. $data = [
  765. 'work_id'=> $service_work['id'],
  766. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  767. 'order_type'=>0,//服务订单
  768. 'order_terminal' => $params['terminal'],
  769. 'payment_type'=>$goods['goods_payment_type']==1?1:0,
  770. 'user_id' => $params['user_id'],
  771. 'pay_status' => PayEnum::UNPAID,
  772. 'coupon_id'=>!empty($params['coupon_id'])?$params['coupon_id']:0,
  773. 'coupon_price'=>!empty($order_coupon_amount)?$order_coupon_amount:0,
  774. 'pay_way' => $params['pay_way'],
  775. 'order_total' => $order_total,
  776. 'order_amount' => $order_amount,
  777. ];
  778. $order = RechargeOrder::create($data);
  779. array_walk($orderGoodsData, function (&$value, $key, $data) {
  780. $value = array_merge($value, ['sn' => $data['sn']]);
  781. },$data);
  782. $orderGoodsModel = new OrderGoods();
  783. $orderGoodsModel->saveAll($orderGoodsData);
  784. Db::commit();
  785. return [
  786. 'order_id' => (int)$order['id'],
  787. ];
  788. } catch (\Exception $e) {
  789. Db::rollback();
  790. self::setError($e->getMessage());
  791. return false;
  792. }
  793. }
  794. }