ServiceOrderLogic.php 35 KB

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