ServiceOrderLogic.php 38 KB

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