ServiceOrderLogic.php 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  1. <?php
  2. namespace app\api\logic;
  3. use app\adminapi\service\DistributeLeafletsService;
  4. use app\common\enum\GoodsEnum;
  5. use app\common\enum\PayEnum;
  6. use app\common\enum\RefundEnum;
  7. use app\common\enum\WorkEnum;
  8. use app\common\enum\YesNoEnum;
  9. use app\common\logic\BaseLogic;
  10. use app\common\logic\PaymentLogic;
  11. use app\common\logic\PayNotifyLogic;
  12. use app\common\logic\RefundLogic;
  13. use app\common\model\coupon\CouponCategory;
  14. use app\common\model\coupon\UserCoupon;
  15. use app\common\model\dict\DictData;
  16. use app\common\model\goods\Goods;
  17. use app\common\model\master_commission\MasterWorkerCommissionConfig;
  18. use app\common\model\master_commission\MasterWorkerCommissionNotice;
  19. use app\common\model\master_worker\MasterWorker;
  20. use app\common\model\master_worker\MasterWorkerRule;
  21. use app\common\model\orders\RechargeOrder;
  22. use app\common\model\property\PropertyActivity;
  23. use app\common\model\recharge\OrderGoods;
  24. use app\common\model\refund\RefundRecord;
  25. use app\common\model\spare_part\SparePart;
  26. use app\common\model\works\ServiceWork;
  27. use app\common\model\works\ServiceWorkAppointmentLog;
  28. use app\common\model\works\ServiceWorkSpare;
  29. use app\workerapi\logic\ServiceWorkLogLogic;
  30. use think\Exception;
  31. use think\facade\Db;
  32. use think\facade\Log;
  33. /**
  34. * 订单逻辑层
  35. * Class ServiceOrderLogic
  36. * @package app\api\logic
  37. */
  38. class ServiceOrderLogic extends BaseLogic
  39. {
  40. /**
  41. * 判断是否在某服务区
  42. * @param $params
  43. * @return bool
  44. */
  45. public static function isService($params)
  46. {
  47. // 查询服务区所有的地点
  48. $rules = MasterWorkerRule::where('scene_id',MasterWorkerRule::SCENE_SERVICE)->column('description','id');
  49. //$masters = [];
  50. foreach ($rules as $key=>$value){
  51. $rule = explode(',',$value);
  52. //$masters[] = ['id'=>$key,'lon'=>$rule[0],'lat'=>$rule[1],'radius'=>$rule[2]];
  53. $distance = round(DistributeLeafletsService::haversineDistance($params['lat'], $params['lon'], $rule[1], $rule[0]), 2);
  54. if($distance <= (float)$rule[2]){
  55. return true;
  56. }
  57. }
  58. self::setError('已超出服务区域!');
  59. return false;
  60. }
  61. /**
  62. * 提交订单
  63. * @param array $params
  64. * @return array|false
  65. */
  66. public static function submitOrder($params)
  67. {
  68. Db::startTrans();
  69. try {
  70. // 订单位置是否在服务区内
  71. if(!self::isService($params)){
  72. throw new Exception('已超出服务区域!');
  73. }
  74. $goods = Goods::findOrEmpty($params['goods_id']);
  75. if($goods->isEmpty()){
  76. throw new Exception('产品不存在!');
  77. }
  78. if(empty($params['user_info']['mobile'])){
  79. throw new Exception('请先补充您的联系方式后在提交订单');
  80. }
  81. //根据服务工单计算当前订单应支付金额
  82. if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE){
  83. //一口价订单
  84. $order_total = $goods['service_fee'];
  85. $order_amount = $goods['service_fee'];
  86. }else if ($goods['goods_payment_type'] == GoodsEnum::DEP_GOODS_PAYMENT_TYPE){
  87. $order_total = $goods['service_fee'];
  88. $order_amount = $goods['service_fee'];
  89. }
  90. else{
  91. $order_total = $goods['base_service_fee'];
  92. $order_amount = $goods['service_fee'];
  93. }
  94. //优惠券验证
  95. if(!empty($params['coupon_id'])){
  96. $user_coupon = UserCoupon::where(['id'=>$params['coupon_id'],'user_id'=>$params['user_id'],'voucher_status'=>0])
  97. ->where('voucher_count','>',0)
  98. ->where('expire_time','>=',time())
  99. ->where('begin_use','<',time())
  100. ->findOrEmpty();
  101. if($user_coupon->isEmpty()){
  102. throw new Exception('该优惠券无法使用');
  103. }
  104. if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE and $order_amount<$user_coupon['amount_require']){
  105. throw new Exception('该优惠劵不满足满减使用条件');
  106. }
  107. if($goods['goods_payment_type'] != GoodsEnum::ISGOODS_PAYMENT_TYPE){
  108. throw new Exception('请在支付尾款的时候使用该优惠券');
  109. }
  110. //优惠券折扣
  111. if($user_coupon['mold_type'] == 1){
  112. //按比例折扣
  113. if($user_coupon['discount_ratio']>=1){
  114. throw new Exception('优惠券有误,请联系客服');
  115. }
  116. $order_coupon_amount = $order_amount*(1-$user_coupon['discount_ratio']);
  117. }else{
  118. $order_coupon_amount = $user_coupon['amount'];
  119. }
  120. if(!empty($user_coupon['max_deductible_price'])){
  121. $order_amount = ($order_coupon_amount>$user_coupon['max_deductible_price'])?($order_amount-$user_coupon['max_deductible_price']):($order_amount-$order_coupon_amount);
  122. }else{
  123. $order_amount = $order_amount-$order_coupon_amount;
  124. }
  125. $user_coupon->voucher_status = 1;
  126. $user_coupon->voucher_count = $user_coupon->voucher_count-1;
  127. $user_coupon->save();
  128. }
  129. //生成服务工单
  130. $work_data = [
  131. 'work_sn' => generate_sn(ServiceWork::class, 'work_sn'),
  132. 'real_name' => $params['contact_people'],
  133. 'mobile' => $params['contact_number'],
  134. 'address' => $params['address'],
  135. 'title' => $goods->goods_name,
  136. 'category_type' => $goods['category_type'],
  137. 'goods_category_ids' => $goods['goods_category_ids'],
  138. 'goods_category_id' => $goods['goods_category_id'],
  139. 'base_service_fee' => $goods['base_service_fee'],
  140. 'service_fee' => $goods['service_fee'],
  141. 'work_pay_status'=>WorkEnum::UN_PAY_STATUS,
  142. 'appointment_time' => strtotime($params['appointment_time']),
  143. 'user_id'=>$params['user_id'],
  144. 'lon'=>!empty($params['lon'])?$params['lon']:0,
  145. 'lat'=>!empty($params['lat'])?$params['lat']:0,
  146. 'property_activity_id'=>!empty($params['property_activity_id'])?$params['property_activity_id']:0,
  147. 'user_equity_id'=>$params['user_equity_id']??0,
  148. ];
  149. //判断是否是加单
  150. if(!empty($params['worker'])){
  151. $worker_id = MasterWorker::where('worker_number',$params['worker'])->value('id');
  152. $work_data['master_worker_id'] = $worker_id;
  153. $work_data['work_status'] = 1;
  154. $work_data['dispatch_time'] = time();
  155. $work_data['work_type'] = 2;
  156. $work_data['data_type'] = 1;
  157. }
  158. //判断是否是复购单
  159. $is_work = ServiceWork::where(['user_id'=>$params['user_id'],'service_status'=>3])->findOrEmpty();
  160. if(!$is_work->isEmpty()){
  161. $work_data['data_type'] = 1;
  162. }
  163. $service_work = ServiceWork::create($work_data);
  164. //使用权益卡时订单应支付金额=0
  165. if(isset($params['user_equity_id']) && $params['user_equity_id']){
  166. $order_total = 0;
  167. $order_amount = 0;
  168. }
  169. //生成服务订单
  170. $data = [
  171. 'work_id'=> $service_work['id'],
  172. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  173. 'order_type'=>0,//服务订单
  174. 'order_terminal' => $params['terminal'],
  175. 'payment_type'=>$goods['goods_payment_type']==GoodsEnum::ISGOODS_PAYMENT_TYPE?0:1,
  176. 'user_id' => $params['user_id'],
  177. 'pay_status' => PayEnum::UNPAID,
  178. 'coupon_id'=>!empty($params['coupon_id'])?$params['coupon_id']:0,
  179. 'coupon_price'=>!empty($order_coupon_amount)?$order_coupon_amount:0,
  180. 'pay_way' => $params['pay_way'],
  181. 'order_total' => $order_total,
  182. 'order_amount' => $order_amount,
  183. ];
  184. $order = RechargeOrder::create($data);
  185. //生成订单服务详情
  186. OrderGoods::create([
  187. 'sn' => $order['sn'],
  188. 'goods_id' => $params['goods_id'],
  189. 'category_type' => $goods['category_type'],
  190. 'goods_category_ids' => $goods['goods_category_ids'],
  191. 'goods_category_id' => $goods['goods_category_id'],
  192. 'goods_name' => $goods['goods_name'],
  193. 'goods_image' => $goods['goods_image'],
  194. 'goods_video' => $goods['goods_video'],
  195. 'goods_number' => $goods['goods_number'],
  196. 'good_unit' => $goods['good_unit'],
  197. 'goods_size' => $goods['goods_size'],
  198. 'goods_type' => $goods['goods_type'],
  199. 'goods_brand' => $goods['goods_brand'],
  200. 'install_guide' => $goods['install_guide'],
  201. 'goods_payment_type'=>$goods['goods_payment_type'],
  202. 'base_service_fee' => $goods['base_service_fee'],
  203. 'service_total' => $goods['service_total'],
  204. 'service_fee' => $goods['service_fee'],
  205. 'service_image' => $goods['service_image'],
  206. 'warranty_period'=>$goods['warranty_period'],
  207. 'fee_schedule' => $goods['fee_schedule'],
  208. 'goods_status' => $goods['goods_status'],
  209. ]);
  210. Db::commit();
  211. } catch (\Exception $e) {
  212. Db::rollback();
  213. self::setError($e->getMessage());
  214. return false;
  215. }
  216. return [
  217. 'order_id' => (int)$order['id'],
  218. 'work_id' => (int)$order['work_id'],
  219. ];
  220. }
  221. /**
  222. * 提交尾款订单
  223. * @param array $params
  224. * @return array|false
  225. */
  226. public static function submitFinalOrder($params)
  227. {
  228. Db::startTrans();
  229. try {
  230. $order = \app\common\model\recharge\RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  231. if ($order->isEmpty()) {
  232. throw new Exception('订单不存在');
  233. }
  234. //判断订单类型.服务订单尾款处理
  235. if($order['order_type'] == 0 and $order['pay_status'] == PayEnum::ISPAID)//服务工单
  236. {
  237. $order = RechargeOrder::where(['work_id'=>$order['work_id'],'pay_status'=>0])->findOrEmpty();
  238. if($order->isEmpty()){
  239. throw new Exception('订单已支付');
  240. }
  241. }
  242. if ($order['pay_status'] == PayEnum::ISPAID) {
  243. throw new Exception('订单已支付');
  244. }
  245. $order_goods = OrderGoods::where('sn',$params['sn'])->findOrEmpty();
  246. $goods = Goods::findOrEmpty($order_goods['goods_id']);
  247. //判断是否存在优惠券
  248. //优惠券验证
  249. if(!empty($order['coupon_id'])){
  250. $order->coupon_id = 0;
  251. $order->coupon_price = 0;
  252. $order->order_amount = $order->order_total;
  253. $order->save();
  254. $user_coupon_ed = UserCoupon::findOrEmpty($order['coupon_id']);
  255. $user_coupon_ed->voucher_count = $user_coupon_ed->voucher_count+1;
  256. $user_coupon_ed->voucher_status = 0;
  257. $user_coupon_ed->save();
  258. $order = \app\common\model\recharge\RechargeOrder::where('sn',$order['sn'])->findOrEmpty();
  259. }
  260. if(!empty($params['coupon_id']) && empty($order['coupon_id'])){
  261. $user_coupon = UserCoupon::where(['id'=>$params['coupon_id'],'user_id'=>$params['user_id'],'voucher_status'=>0])
  262. ->where('voucher_count','>',0)
  263. ->where('expire_time','>=',time())
  264. ->where('begin_use','<',time())
  265. ->findOrEmpty();
  266. if($user_coupon->isEmpty()){
  267. throw new Exception('该优惠券不满足使用条件');
  268. }
  269. if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE and $order['order_amount']<$user_coupon['amount_require']){
  270. throw new Exception('该优惠劵不满足满减使用条件');
  271. }
  272. //优惠券折扣
  273. if($user_coupon['mold_type'] == 1){
  274. //按比例折扣
  275. if($user_coupon['discount_ratio']>=1){
  276. throw new Exception('优惠券有误,请联系客服');
  277. }
  278. $order_coupon_amount = $order['order_amount']*(1-$user_coupon['discount_ratio']);
  279. }else{
  280. $order_coupon_amount = $user_coupon['amount'];
  281. }
  282. if(!empty($user_coupon['max_deductible_price'])){
  283. $order_amount = ($order_coupon_amount>$user_coupon['max_deductible_price'])?($order['order_amount']-$user_coupon['max_deductible_price']):($order['order_amount']-$order_coupon_amount);
  284. }else{
  285. $order_amount = $order['order_amount']-$order_coupon_amount;
  286. }
  287. $user_coupon->voucher_status = 1;
  288. $user_coupon->voucher_count = $user_coupon->voucher_count-1;
  289. $user_coupon->save();
  290. }
  291. $order->coupon_id = !empty($params['coupon_id'])?$params['coupon_id']:0;
  292. $order->coupon_price = !empty($order_coupon_amount)?$order_coupon_amount:0;
  293. $order->order_amount = !empty($order_amount)?$order_amount:$order->order_amount;
  294. $order->save();
  295. Db::commit();
  296. } catch (\Exception $e) {
  297. Db::rollback();
  298. self::setError($e->getMessage());
  299. return false;
  300. }
  301. return [
  302. 'order_id' => (int)$order['id'],
  303. 'work_id' => (int)$order['work_id'],
  304. ];
  305. }
  306. /**
  307. * 重置订单优惠券
  308. * * @param $params
  309. * * * @return array|false
  310. */
  311. public static function cancelOrderCoupon($params)
  312. {
  313. Db::startTrans();
  314. try {
  315. $order = \app\common\model\recharge\RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  316. if ($order->isEmpty()) {
  317. throw new Exception('订单不存在');
  318. }
  319. //判断订单类型.服务订单尾款处理
  320. if($order['order_type'] == 0 and $order['pay_status'] == PayEnum::ISPAID)//服务工单
  321. {
  322. $order = RechargeOrder::where(['work_id'=>$order['work_id'],'pay_status'=>0])->findOrEmpty();
  323. if($order->isEmpty()){
  324. throw new Exception('订单已支付');
  325. }
  326. }
  327. if ($order['pay_status'] == PayEnum::ISPAID) {
  328. throw new Exception('订单已支付');
  329. }
  330. if(!empty($order['coupon_id'])){
  331. $user_coupon_ed = UserCoupon::findOrEmpty($order['coupon_id']);
  332. if(!$user_coupon_ed->isEmpty()){
  333. $user_coupon_ed->voucher_count = $user_coupon_ed->voucher_count+1;
  334. $user_coupon_ed->voucher_status = 0;
  335. $user_coupon_ed->save();
  336. $order->order_amount = $order->order_amount+$order->coupon_price;
  337. $order->coupon_id = 0;
  338. $order->coupon_price = 0;
  339. $order->save();
  340. }
  341. }
  342. Db::commit();
  343. } catch (\Exception $e) {
  344. Db::rollback();
  345. self::setError($e->getMessage());
  346. return false;
  347. }
  348. }
  349. /**
  350. * 获取订单工程师信息
  351. * * @param $params
  352. * * @return array|false
  353. */
  354. public static function getMasterWorker($params)
  355. {
  356. try {
  357. $order_info = \app\common\model\recharge\RechargeOrder::with(['order_goods'=>function ($query) {
  358. $query->visible(['goods_name','goods_image','goods_number','good_unit']);
  359. },'service_work'=>function ($query) {
  360. $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']);
  361. }])
  362. ->visible(['id','sn','payment_type','order_total','order_amount','pay_status','create_time','title','work_id'])
  363. ->where([
  364. 'order_type' => 0,
  365. 'user_id' => $params['user_id'],
  366. 'sn'=>$params['sn']
  367. ])->findOrEmpty()->toArray();
  368. $data = [];
  369. //获取工程师参数
  370. if(!empty($order_info['service_work']['master_worker_id'])){
  371. $worker = MasterWorker::find($order_info['service_work']['master_worker_id']);
  372. $data['avatar'] = $worker['avatar'];
  373. $data['real_name'] = $worker['real_name'];
  374. $data['worker_number'] = $worker['worker_number'];
  375. $data['mobile'] = $worker['mobile'];
  376. $maintain_exp_type = !empty($worker->worker_register->maintain_exp_type)?$worker->worker_register->maintain_exp_type:'';
  377. $data['worker_exp'] = DictData::where(['type_value'=>'worker_exp_type','value'=>$maintain_exp_type])->value('name');
  378. $data['appointment_time'] = $order_info['service_work']['appointment_time'];
  379. }
  380. return $data;
  381. }
  382. catch (\Exception $e) {
  383. self::setError($e->getMessage());
  384. return false;
  385. }
  386. }
  387. /**
  388. * 获取订单详情
  389. * @param $params
  390. * @return array|false
  391. */
  392. public static function detail($params)
  393. {
  394. try {
  395. $order_info = \app\common\model\recharge\RechargeOrder::with(['order_goods'=>function ($query) {
  396. $query->visible(['goods_id','goods_name','goods_image','goods_number','good_unit']);
  397. },'service_work'=>function ($query) {
  398. $query->visible(['work_sn','real_name','mobile','address','service_status','appointment_time','master_worker_id','work_images','explanation','finished_images','finished_time','spare_total','service_work_spare_id','refund_approval','property_activity_id'])->append(['service_status_text','user_service_status','user_service_status_text']);
  399. }])
  400. ->visible(['id','sn','payment_type','order_total','order_amount','paid_amount','pay_status','create_time','title','work_id'])
  401. ->where([
  402. 'order_type' => 0,
  403. 'user_id' => $params['user_id'],
  404. 'sn'=>$params['sn']
  405. ])->findOrEmpty()->toArray();
  406. if(empty($order_info)){
  407. throw new Exception('订单不存在');
  408. }
  409. $order_info['master_worker'] = [
  410. 'avatar' => '',
  411. 'real_name'=>'',
  412. 'worker_number'=>'',
  413. 'mobile'=>'',
  414. 'worker_exp'=>''
  415. ];
  416. //查询总价
  417. $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');
  418. $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');
  419. //退款金额
  420. $order_refund_amount = 0;
  421. //获取工程师参数
  422. if(!empty($order_info['service_work']['master_worker_id'])){
  423. $worker = MasterWorker::find($order_info['service_work']['master_worker_id']);
  424. $order_info['master_worker']['avatar'] = !empty($worker)?$worker['avatar']:'';
  425. $order_info['master_worker']['real_name'] = !empty($worker)?$worker['real_name']:'';
  426. $order_info['master_worker']['worker_number'] = !empty($worker)?$worker['worker_number']:'';
  427. $order_info['master_worker']['mobile'] = !empty($worker)?$worker['mobile']:'';
  428. $maintain_exp_type = !empty($worker->worker_register->maintain_exp_type)?$worker->worker_register->maintain_exp_type:'';
  429. $order_info['master_worker']['worker_exp'] = DictData::where(['type_value'=>'worker_exp_type','value'=>$maintain_exp_type])->value('name');
  430. }
  431. //搜索当前工单下的所有订单记录
  432. $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();
  433. $pay_status_data = DictData::where('type_value','pay_status')->column('name','value');
  434. $payment_type_data = DictData::where('type_value','payment_type')->column('name','value');
  435. $pay_way_data = DictData::where('type_value','pay_way')->column('name','value');
  436. $coupon_price = 0;
  437. foreach ($order_info['pay_orders'] as $k=>&$v){
  438. $v['pay_status_name'] = $pay_status_data[$v['pay_status']];
  439. $v['payment_type_name'] = $payment_type_data[$v['payment_type']];
  440. $v['pay_way_name'] = $pay_way_data[$v['pay_way']];
  441. if($v['refund_status']==1){
  442. $order_refund_amount += $v['order_amount'];
  443. $refund_status = RefundRecord::where('order_id',$v['order_id'])->value('refund_status');
  444. }
  445. if($v['payment_type']!=1 and !empty($order_info['service_work']['spare_total'])){
  446. $v['order_amount_total'] = $v['order_amount'];
  447. $v['order_amount'] = $v['order_amount'] - $order_info['service_work']['spare_total'];
  448. }
  449. $coupon_price += $v['coupon_price'];
  450. }
  451. //汇总优惠卷额度
  452. $order_info['coupon_price'] = $coupon_price;
  453. //退款汇总
  454. $order_info['refund_amount'] = $order_refund_amount;
  455. if(isset($refund_status)){
  456. switch ($refund_status){
  457. case 0:
  458. $order_info['refund_status'] = '退款中';
  459. break;
  460. case 1:
  461. $order_info['refund_status'] = '退款成功';
  462. break;
  463. case 2:
  464. $order_info['refund_status'] = '退款失败';
  465. break;
  466. }
  467. }else{
  468. $order_info['refund_status'] = '未退款';
  469. }
  470. // 所有配件
  471. $order_info['spare_total'] = $order_info['service_work']['spare_total']??0;
  472. $order_info['spare_parts'] = [];
  473. if($order_info['service_work']['service_work_spare_id']){
  474. $work_spare_parts = json_decode(ServiceWorkSpare::where('id',$order_info['service_work']['service_work_spare_id'])->value('spare_parts'),true);
  475. $spare_parts = SparePart::where('id','in',array_column($work_spare_parts,'id'))
  476. ->field(['id', 'goods_category_id', 'spare_name', 'spare_image', 'spare_number', 'spare_unit','spare_status'])
  477. ->select()
  478. ->toArray();
  479. $spare_parts = array_column($spare_parts,null,'id');
  480. foreach (array_column($work_spare_parts,null,'id') as $k=>&$v){
  481. $spare_parts[$k] = array_merge($spare_parts[$k],$v);
  482. }
  483. $order_info['spare_parts'] = array_values($spare_parts)??[];
  484. }
  485. //获取所有的改约记录
  486. $order_info['appoint_list'] = [];
  487. $appoint_log = ServiceWorkAppointmentLog::where('work_id',$order_info['work_id'])->order('id desc')->select()->toArray();
  488. if(!empty($appoint_log)){
  489. $order_info['appoint_list'][0]['appointment_time'] = date('Y-m-d H:i:s',$appoint_log[0]['this_appointment_time']);
  490. foreach ($appoint_log as $k1=>$v1){
  491. $order_info['appoint_list'][$k1+1]['appointment_time'] = date('Y-m-d H:i:s',$v1['last_appointment_time']);
  492. }
  493. }
  494. //获取是否是活动工单
  495. if(!empty($order_info['service_work']['property_activity_id'])){
  496. $propertyActivity = PropertyActivity::findOrEmpty($order_info['service_work']['property_activity_id']);
  497. $order_info['service_work']['url_page'] = rawurlencode($params['domain'].'/static/wxapp/H5/'.$propertyActivity['url_page'].'/index.html?property_activity_id='.$propertyActivity['id'].'&token='.$params['user_info']['token']);
  498. }
  499. return $order_info;
  500. }
  501. catch (\Exception $e) {
  502. self::setError($e->getMessage());
  503. return false;
  504. }
  505. }
  506. public static function getDetailStatus($params)
  507. {
  508. try {
  509. $order_info = \app\common\model\recharge\RechargeOrder::with(['service_work'=>function ($query) {
  510. $query->visible(['work_sn'])->append(['service_status_text','user_service_status','user_service_status_text']);
  511. }])
  512. ->visible(['id','sn'])
  513. ->where([
  514. 'order_type' => 0,
  515. 'user_id' => $params['user_id'],
  516. 'sn'=>$params['sn']
  517. ])->findOrEmpty()->toArray();
  518. if(empty($order_info)){
  519. throw new Exception('订单不存在');
  520. }
  521. return $order_info;
  522. }
  523. catch (\Exception $e) {
  524. self::setError($e->getMessage());
  525. return false;
  526. }
  527. }
  528. /**
  529. * 获取订单支付详情
  530. * @param $params
  531. * @return array|false
  532. */
  533. public static function orderPayInfo($params)
  534. {
  535. try {
  536. //判断是否存在优惠券
  537. $is_coupon_used_work_id = \app\common\model\recharge\RechargeOrder::where([
  538. 'order_type' => 0,
  539. 'user_id' => $params['user_id'],
  540. 'sn'=>$params['sn']
  541. ])->value('work_id');
  542. $is_coupon_used = \app\common\model\recharge\RechargeOrder::where('work_id',$is_coupon_used_work_id)->where('pay_status',0)->findOrEmpty();
  543. if(!empty($is_coupon_used['coupon_id'])){
  544. $user_coupon = UserCoupon::where(['user_id'=>$is_coupon_used->user_id,'id'=>$is_coupon_used->coupon_id])->findOrEmpty();
  545. $user_coupon->voucher_status = 0;
  546. $user_coupon->voucher_count = $user_coupon->voucher_count+1;
  547. $user_coupon->save();
  548. $is_coupon_used->order_amount = $is_coupon_used->order_amount+$is_coupon_used->coupon_price;
  549. $is_coupon_used->coupon_price = 0;
  550. $is_coupon_used->coupon_id = 0;
  551. $is_coupon_used->save();
  552. }
  553. $order_info = \app\common\model\recharge\RechargeOrder::with(['order_goods'=>function ($query) {
  554. $query->visible(['goods_id','goods_payment_type','goods_category_id']);
  555. },'service_work'=>function ($query) {
  556. $query->visible(['service_fee','spare_total','service_work_spare_id','goods_category_id']);
  557. }])
  558. ->visible(['id','sn','work_id'])
  559. ->where([
  560. 'order_type' => 0,
  561. 'user_id' => $params['user_id'],
  562. 'sn'=>$params['sn']
  563. ])->findOrEmpty()->toArray();
  564. if(empty($order_info)){
  565. throw new Exception('订单不存在');
  566. }
  567. //查询总价
  568. $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');
  569. $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');
  570. //搜索当前工单下的所有订单记录
  571. $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();
  572. $pay_status_data = DictData::where('type_value','pay_status')->column('name','value');
  573. $payment_type_data = DictData::where('type_value','payment_type')->column('name','value');
  574. $pay_way_data = DictData::where('type_value','pay_way')->column('name','value');
  575. $coupon_price = 0;
  576. //退款金额
  577. $order_refund_amount = 0;
  578. $payment_type = 0;
  579. foreach ($order_info['pay_orders'] as $k=>&$v){
  580. $v['pay_status_name'] = $pay_status_data[$v['pay_status']];
  581. $v['payment_type_name'] = $payment_type_data[$v['payment_type']];
  582. $v['pay_way_name'] = $pay_way_data[$v['pay_way']];
  583. if($v['refund_status']==1){
  584. $order_refund_amount += $v['order_amount'];
  585. $refund_status = RefundRecord::where('order_id',$v['order_id'])->value('refund_status');
  586. }
  587. if($v['payment_type']!=1 and !empty($order_info['service_work']['spare_total'])){
  588. $v['order_total'] = $v['order_total'] - $order_info['service_work']['spare_total'];
  589. $v['order_amount'] = $v['order_amount'] - $order_info['service_work']['spare_total'] + $v['coupon_price'];
  590. }
  591. $coupon_price += $v['coupon_price'];
  592. $payment_type = $v['payment_type'];
  593. }
  594. $order_info['payment_type'] = $payment_type;
  595. //汇总优惠卷额度
  596. $order_info['coupon_price'] = $coupon_price;
  597. //退款汇总
  598. $order_info['refund_amount'] = $order_refund_amount;
  599. if(isset($refund_status)){
  600. switch ($refund_status){
  601. case 0:
  602. $order_info['refund_status'] = '退款中';
  603. break;
  604. case 1:
  605. $order_info['refund_status'] = '退款成功';
  606. break;
  607. case 2:
  608. $order_info['refund_status'] = '退款失败';
  609. break;
  610. }
  611. }else{
  612. $order_info['refund_status'] = '未退款';
  613. }
  614. // 所有配件
  615. $order_info['spare_total'] = $order_info['service_work']['spare_total']??0;
  616. $order_info['spare_parts'] = [];
  617. if($order_info['service_work']['service_work_spare_id']){
  618. $work_spare_parts = json_decode(ServiceWorkSpare::where('id',$order_info['service_work']['service_work_spare_id'])->value('spare_parts'),true);
  619. $spare_parts = SparePart::where('id','in',array_column($work_spare_parts,'id'))
  620. ->field(['id', 'goods_category_id', 'spare_name', 'spare_image', 'spare_number', 'spare_unit','spare_status'])
  621. ->select()
  622. ->toArray();
  623. $spare_parts = array_column($spare_parts,null,'id');
  624. foreach (array_column($work_spare_parts,null,'id') as $k=>&$v){
  625. $spare_parts[$k] = array_merge($spare_parts[$k],$v);
  626. }
  627. $order_info['spare_parts'] = array_values($spare_parts)??[];
  628. }
  629. //获取是否存在可使用优惠券
  630. $coupon_ids = CouponCategory::where('goods_category_id',$order_info['service_work']['goods_category_id'])
  631. ->whereIn('coupon_id',UserCoupon::where('user_id',$params['user_id'])
  632. ->where('voucher_count','>',0)
  633. ->where('voucher_status',0)
  634. ->where('expire_time','>',time())
  635. ->where('amount_require','<=',$order_info['order_amount'])
  636. ->column('coupon_id')
  637. )->column('coupon_id');
  638. $usable_coupon = UserCoupon::where('user_id',$params['user_id'])
  639. ->whereIn('coupon_id',$coupon_ids)
  640. ->append(['discount_ratio_text'])
  641. ->where('expire_time','>',time())
  642. ->count();
  643. $order_info['usable_coupon'] = $usable_coupon>0?true:false;
  644. return $order_info;
  645. }
  646. catch (\Exception $e) {
  647. self::setError($e->getMessage());
  648. return false;
  649. }
  650. }
  651. /**
  652. * 取消订单
  653. * @param $params
  654. * @return false|void
  655. */
  656. public static function cancelOrder($params)
  657. {
  658. Db::startTrans();
  659. try {
  660. $work_id = \app\common\model\recharge\RechargeOrder::where([
  661. 'order_type' => 0,
  662. 'user_id' => $params['user_id'],
  663. 'sn'=>$params['sn']
  664. ])->value('work_id');
  665. if(empty($work_id)){
  666. throw new Exception('订单不存在');
  667. }
  668. $payed_order = \app\common\model\recharge\RechargeOrder::where(['user_id'=>$params['user_id'],'work_id'=>$work_id,'pay_status'=>1])->findOrEmpty();
  669. if(!$payed_order->isEmpty()){
  670. throw new Exception('存在已支付订单,不允许取消订单,请联系客服');
  671. }
  672. //软删除订单
  673. $cancel_order = \app\common\model\recharge\RechargeOrder::where(['user_id'=>$params['user_id'],'work_id'=>$work_id])->findOrEmpty();
  674. $cancel_order->pay_status = 2;
  675. $cancel_order->save();
  676. //更新工单状态为已取消
  677. $service_work = ServiceWork::find($work_id);
  678. $service_work->service_status = 4;
  679. $service_work->save();
  680. //判断如果存在优惠券,返还优惠券
  681. if(!empty($payed_order->coupon_id)){
  682. $user_coupon = UserCoupon::where(['user_id'=>$payed_order->user_id,'id'=>$payed_order->coupon_id])->findOrEmpty();
  683. $user_coupon->voucher_status = 0;
  684. $user_coupon->voucher_count = $user_coupon->voucher_count+1;
  685. $user_coupon->save();
  686. }
  687. Db::commit();
  688. if($cancel_order->pay_time){
  689. // 订单取消通知【给用户】
  690. event('Notice', [
  691. 'scene_id' => 122,
  692. 'params' => [
  693. 'user_id' => $params['user_id']
  694. ]
  695. ]);
  696. }
  697. }
  698. catch (\Exception $e) {
  699. Db::rollback();
  700. self::setError($e->getMessage());
  701. return false;
  702. }
  703. }
  704. /**
  705. * 用户确认尾款报价单
  706. * @param $params
  707. * @return false|void
  708. */
  709. public static function confirmOrder($params)
  710. {
  711. Db::startTrans();
  712. try {
  713. $work_id = \app\common\model\recharge\RechargeOrder::where([
  714. 'order_type' => 0,
  715. 'user_id' => $params['user_id'],
  716. 'sn'=>$params['sn']
  717. ])->value('work_id');
  718. if(empty($work_id)){
  719. throw new Exception('订单不存在');
  720. }
  721. //更新工单状态为已取消
  722. $service_work = ServiceWork::find($work_id);
  723. if($service_work->user_confirm_status==2){
  724. throw new Exception('请勿重复操作');
  725. }
  726. $service_work->work_status = 5;
  727. $service_work->user_confirm_status = 2;
  728. $service_work->save();
  729. $work_log = [
  730. 'work_id'=>$work_id,
  731. 'master_worker_id'=>$service_work->master_worker_id,
  732. 'opera_log'=>'用户'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'确认了报价单',
  733. ];
  734. ServiceWorkLogLogic::add($work_log);
  735. Db::commit();
  736. }
  737. catch (\Exception $e) {
  738. Db::rollback();
  739. self::setError($e->getMessage());
  740. return false;
  741. }
  742. }
  743. /**
  744. * 用户确认服务完成
  745. * @param $params
  746. * @return false|void
  747. */
  748. public static function confirmServiceFinish($params)
  749. {
  750. Db::startTrans();
  751. try {
  752. $work_id = \app\common\model\recharge\RechargeOrder::where([
  753. 'order_type' => 0,
  754. 'user_id' => $params['user_id'],
  755. 'sn'=>$params['sn']
  756. ])->value('work_id');
  757. if(empty($work_id)){
  758. throw new Exception('订单不存在');
  759. }
  760. $service_work = ServiceWork::find($work_id);
  761. if($service_work->user_confirm_status!=3){
  762. throw new Exception('请勿重复操作');
  763. }
  764. $orders = RechargeOrder::where(['work_id'=>$work_id,'user_id'=>$params['user_id']])->select()->toArray();
  765. //确认所有订单总金额和结算金额
  766. //若订单是全款已支付订单
  767. if(count($orders)==1 and $orders[0]['payment_type']==0 and $orders[0]['pay_status']==1){
  768. $service_work->work_status = 7;
  769. $service_work->user_confirm_status = 5;
  770. $service_work->service_status = 3;
  771. $service_work->work_pay_status = 1;
  772. }else{
  773. $service_work->work_status = 6;
  774. $service_work->user_confirm_status = 4;
  775. }
  776. $service_work->finished_time = time();
  777. $service_work->save();
  778. //更新工程师的进行工单数量
  779. MasterWorker::setWorktotal('dec',$service_work->master_worker_id);
  780. $work_log = [
  781. 'work_id'=>$work_id,
  782. 'master_worker_id'=>$service_work->master_worker_id,
  783. 'opera_log'=>'用户'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'确认服务完成',
  784. ];
  785. ServiceWorkLogLogic::add($work_log);
  786. Db::commit();
  787. }
  788. catch (\Exception $e) {
  789. Db::rollback();
  790. self::setError($e->getMessage());
  791. return false;
  792. }
  793. }
  794. public static function approvalChangePrice($params)
  795. {
  796. Db::startTrans();
  797. try {
  798. $order = RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  799. if($order->isEmpty()){
  800. throw new Exception('订单不存在');
  801. }
  802. $work = ServiceWork::findOrEmpty($order->work_id);
  803. if($work->isEmpty()){
  804. throw new Exception('工单不存在');
  805. }
  806. $work->work_status = 4;
  807. $work->user_confirm_status = 0;
  808. $work->price_approval = 1;
  809. $work->save();
  810. Db::commit();
  811. }
  812. catch (\Exception $e) {
  813. Db::rollback();
  814. self::setError($e->getMessage());
  815. return false;
  816. }
  817. }
  818. public static function approvalChangeAppointment($params)
  819. {
  820. Db::startTrans();
  821. try {
  822. $order = RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  823. if($order->isEmpty()){
  824. throw new Exception('订单不存在');
  825. }
  826. $work = ServiceWork::findOrEmpty($order->work_id);
  827. if($work->isEmpty()){
  828. throw new Exception('工单不存在');
  829. }
  830. //更新工单未确认上门的状态
  831. $work->work_status = 3;
  832. $work->user_confirm_status = 0;
  833. ServiceWorkAppointmentLog::create([
  834. 'work_id'=>$work->id,
  835. 'last_appointment_time'=>strtotime($work->appointment_time),
  836. 'this_appointment_time'=>strtotime($params['appointment_time']),
  837. ]);
  838. $work->appointment_time = strtotime($params['appointment_time']);
  839. $work->appoint_approval = 1;
  840. $work->save();
  841. Db::commit();
  842. }
  843. catch (\Exception $e) {
  844. Db::rollback();
  845. self::setError($e->getMessage());
  846. return false;
  847. }
  848. return $work;
  849. }
  850. public static function approvalRefund($params)
  851. {
  852. Db::startTrans();
  853. try {
  854. $order = RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  855. if($order->isEmpty()){
  856. throw new Exception('订单不存在');
  857. }
  858. $work = ServiceWork::findOrEmpty($order->work_id);
  859. if($work->isEmpty()){
  860. throw new Exception('工单不存在');
  861. }
  862. $work->refund_approval = 1;
  863. $work->save();
  864. //判断是否已下单时间过了两小时,并且师傅暂未上门,距离预约时间两小时以上
  865. if(($order['pay_time']+7200)>time() and $work['work_status']<4 and (strtotime($work['appointment_time'])-7200)>time()){
  866. //生成退款订单
  867. \app\common\model\recharge\RechargeOrder::update([
  868. 'id' => $order['id'],
  869. 'refund_status' => YesNoEnum::YES,
  870. ]);
  871. // 生成退款记录
  872. $recordSn = generate_sn(RefundRecord::class, 'sn');
  873. $record = RefundRecord::create([
  874. 'sn' => $recordSn,
  875. 'user_id' => $order['user_id'],
  876. 'order_id' => $order['id'],
  877. 'order_sn' => $order['sn'],
  878. 'order_type' => RefundEnum::ORDER_TYPE_ORDER,
  879. 'order_amount' => $order['order_amount'],
  880. 'refund_amount' => $order['order_amount'],
  881. 'refund_type' => RefundEnum::TYPE_ADMIN,
  882. 'transaction_id' => $order['transaction_id'] ?? '',
  883. 'refund_way' => RefundEnum::getRefundWayByPayWay($order['pay_way']),
  884. ]);
  885. //更新工单状态
  886. ServiceWork::update([
  887. 'id'=>$order['work_id'],
  888. 'service_status'=>5
  889. ]);
  890. // 退款
  891. RefundLogic::refund($order, $record['id'], $order['order_amount'], 1);
  892. }
  893. Db::commit();
  894. }
  895. catch (\Exception $e) {
  896. Db::rollback();
  897. self::setError($e->getMessage());
  898. return false;
  899. }
  900. }
  901. /**
  902. * 订单完成通知【给用户】 - 全款 -通知
  903. * @param $params
  904. * @return bool
  905. */
  906. public static function serviceFinishNotice($params)
  907. {
  908. try {
  909. $order = RechargeOrder::where('sn', $params['sn'])
  910. ->where('payment_type','=',0)
  911. ->where('pay_status','=',1)
  912. ->findOrEmpty();
  913. if(!$order->isEmpty()){
  914. event('Notice', [
  915. 'scene_id' => 120,
  916. 'params' => [
  917. 'user_id' => $order['user_id']
  918. ]
  919. ]);
  920. }
  921. return true;
  922. }catch (\Exception $e) {
  923. return false;
  924. }
  925. }
  926. /**
  927. * 工程师完单的时候设置单量规则 - 通知
  928. * @param $params
  929. * @return bool
  930. */
  931. public static function orderQuantityRule($params)
  932. {
  933. try {
  934. $work_id = \app\common\model\recharge\RechargeOrder::where([
  935. 'order_type' => 0,
  936. 'user_id' => $params['user_id'],
  937. 'sn'=>$params['sn']
  938. ])->value('work_id');
  939. if(empty($work_id)){
  940. throw new Exception('订单不存在');
  941. }
  942. $service_work = ServiceWork::find($work_id);
  943. $commission_config_id = MasterWorkerCommissionConfig::where('voucher_status','=',2)
  944. ->where('master_worker_id', $service_work['master_worker_id'])
  945. ->value('id');
  946. if($commission_config_id){
  947. // 工程师完单的时候设置该规则关闭,以及短信通知工程师
  948. $noticeRule = Db::name('master_worker_commission_notice')->alias('n')
  949. ->leftJoin('service_work sw', 'n.master_worker_id = sw.master_worker_id AND sw.create_time >= n.signing_date AND sw.create_time <= n.signing_date_end')
  950. ->field([
  951. 'n.id', 'n.commission_config_id', 'n.master_worker_id', 'n.day_num', 'n.order_num', 'n.signing_date', 'n.signing_date_end','n.is_notice',
  952. Db::raw("COUNT(sw.id) AS order_count")
  953. ])
  954. ->where('n.commission_config_id', $commission_config_id)
  955. ->group('n.master_worker_id, n.id')
  956. ->select()->toArray();
  957. $is_off = true;
  958. foreach ($noticeRule as $item) {
  959. if($item['order_num'] <= $item['order_count'] && empty($item['is_notice'])){
  960. Log::info('orderQuantityRule:'.json_encode($item));
  961. MasterWorkerCommissionNotice::where('id',$item['id'])->update([
  962. 'is_notice'=>1
  963. ]);
  964. event('Notice', [
  965. 'scene_id' => 128,
  966. 'params' => [
  967. 'user_id' => $service_work['master_worker_id'],
  968. 'number1' => $item['day_num'],
  969. 'number' => $item['order_num']
  970. ]
  971. ]);
  972. }
  973. if($item['order_num'] > $item['order_count']){
  974. $is_off = false;
  975. }
  976. }
  977. if($is_off){
  978. MasterWorkerCommissionConfig::where('id',$commission_config_id)->update([
  979. 'voucher_status'=>1
  980. ]);
  981. }
  982. }
  983. return true;
  984. }catch (\Exception $e) {
  985. Log::info('orderQuantityRule:'.$e->getMessage());
  986. return false;
  987. }
  988. }
  989. /**
  990. * 提交订单
  991. * @param array $params
  992. * @return array|false
  993. */
  994. public static function firmSubmitOrder($params)
  995. {
  996. Db::startTrans();
  997. try {
  998. $goods = Goods::findOrEmpty($params['goods_id']);
  999. if($goods->isEmpty()){
  1000. throw new Exception('产品不存在!');
  1001. }
  1002. if(empty($params['user_info']['mobile'])){
  1003. throw new Exception('请先补充您的联系方式后在提交订单');
  1004. }
  1005. //根据服务工单计算当前订单应支付金额
  1006. if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE){
  1007. //一口价订单
  1008. $order_total = $goods['service_fee'];
  1009. $order_amount = $goods['service_fee'];
  1010. }else if ($goods['goods_payment_type'] == GoodsEnum::DEP_GOODS_PAYMENT_TYPE){
  1011. $order_total = $goods['service_fee'];
  1012. $order_amount = $goods['service_fee'];
  1013. }
  1014. else{
  1015. $order_total = $goods['base_service_fee'];
  1016. $order_amount = $goods['service_fee'];
  1017. }
  1018. //生成服务工单
  1019. $work_data = [
  1020. 'work_sn' => generate_sn(ServiceWork::class, 'work_sn'),
  1021. 'real_name' => $params['contact_people'],
  1022. 'mobile' => $params['contact_number'],
  1023. 'address' => $params['address'],
  1024. 'title' => $goods->goods_name . '*' . $goods->goods_number.$goods->good_unit,
  1025. 'category_type' => $goods['category_type'],
  1026. 'goods_category_ids' => $goods['goods_category_ids'],
  1027. 'goods_category_id' => $goods['goods_category_id'],
  1028. 'base_service_fee' => $goods['base_service_fee'],
  1029. 'service_fee' => $goods['service_fee'],
  1030. 'work_pay_status'=>WorkEnum::UN_PAY_STATUS,
  1031. 'work_type'=> 1,
  1032. 'appointment_time' => strtotime($params['appointment_time']),
  1033. 'user_id'=>$params['user_id'],
  1034. 'lon'=>!empty($params['lon'])?$params['lon']:0,
  1035. 'lat'=>!empty($params['lat'])?$params['lat']:0,
  1036. ];
  1037. $service_work = ServiceWork::create($work_data);
  1038. //生成服务订单
  1039. $data = [
  1040. 'work_id'=> $service_work['id'],
  1041. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  1042. 'order_type'=>0,//服务订单
  1043. 'order_terminal' => $params['terminal'],
  1044. 'payment_type'=>$goods['goods_payment_type']==GoodsEnum::ISGOODS_PAYMENT_TYPE?0:1,
  1045. 'user_id' => $params['user_id'],
  1046. 'pay_status' => PayEnum::UNPAID,
  1047. 'coupon_id'=>!empty($params['coupon_id'])?$params['coupon_id']:0,
  1048. 'coupon_price'=>!empty($order_coupon_amount)?$order_coupon_amount:0,
  1049. 'pay_way' => $params['pay_way'],
  1050. 'order_total' => $order_total,
  1051. 'order_amount' => $order_amount,
  1052. ];
  1053. $order = RechargeOrder::create($data);
  1054. //生成订单服务详情
  1055. OrderGoods::create([
  1056. 'sn' => $order['sn'],
  1057. 'goods_id' => $params['goods_id'],
  1058. 'category_type' => $goods['category_type'],
  1059. 'goods_category_ids' => $goods['goods_category_ids'],
  1060. 'goods_category_id' => $goods['goods_category_id'],
  1061. 'goods_name' => $goods['goods_name'],
  1062. 'goods_image' => $goods['goods_image'],
  1063. 'goods_video' => $goods['goods_video'],
  1064. 'goods_number' => $goods['goods_number'],
  1065. 'good_unit' => $goods['good_unit'],
  1066. 'goods_size' => $goods['goods_size'],
  1067. 'goods_type' => $goods['goods_type'],
  1068. 'goods_brand' => $goods['goods_brand'],
  1069. 'install_guide' => $goods['install_guide'],
  1070. 'goods_payment_type'=>$goods['goods_payment_type'],
  1071. 'base_service_fee' => $goods['base_service_fee'],
  1072. 'service_total' => $goods['service_total'],
  1073. 'service_fee' => $goods['service_fee'],
  1074. 'service_image' => $goods['service_image'],
  1075. 'warranty_period'=>$goods['warranty_period'],
  1076. 'fee_schedule' => $goods['fee_schedule'],
  1077. 'goods_status' => $goods['goods_status'],
  1078. ]);
  1079. Db::commit();
  1080. } catch (\Exception $e) {
  1081. Db::rollback();
  1082. self::setError($e->getMessage());
  1083. return false;
  1084. }
  1085. return [
  1086. 'order_id' => (int)$order['id'],
  1087. 'work_id' => (int)$order['work_id'],
  1088. ];
  1089. }
  1090. /**
  1091. * 绑定工程师-设置为加单
  1092. * * @param array $params
  1093. * * @return array|false
  1094. */
  1095. public static function bindWorkerAndWork(array $params)
  1096. {
  1097. Db::startTrans();
  1098. try {
  1099. $order = RechargeOrder::where(['sn'=>$params['sn'],'user_id'=>$params['user_id']])->findOrEmpty();
  1100. if($order->isEmpty()){
  1101. throw new Exception('订单不存在');
  1102. }
  1103. $work = ServiceWork::where(['id'=>$order['work_id']])->findOrEmpty();
  1104. if($work->isEmpty()){
  1105. throw new Exception('工单不存在');
  1106. }
  1107. if($work['work_status']!=WorkEnum::WORK_STATUS_WAIT_SERVICE){
  1108. throw new Exception('工单状态不正确,无法绑定');
  1109. }
  1110. $worker = MasterWorker::where('id',$params['worker_id'])->findOrEmpty();
  1111. if($worker->isEmpty()){
  1112. throw new Exception('工程师不存在');
  1113. }
  1114. $work->master_worker_id = $params['worker_id'];
  1115. $work->work_status = WorkEnum::WORK_STATUS_WORKER;
  1116. $work->receive_time = time();
  1117. $work->work_type = 2;//加单状态
  1118. $work->save();
  1119. Db::commit();
  1120. } catch (\Exception $e) {
  1121. Db::rollback();
  1122. self::setError($e->getMessage());
  1123. return false;
  1124. }
  1125. }
  1126. /**
  1127. * 0元支付直接完成支付
  1128. * @param array $params attach out_trade_no
  1129. * @return bool|void
  1130. */
  1131. public static function zeroDirectlyPayment(array $params, $extra = [])
  1132. {
  1133. try {
  1134. $message['out_trade_no'] = mb_substr($params['out_trade_no'], 0, 18);
  1135. $order = RechargeOrder::where(['sn' => $params['out_trade_no']])->findOrEmpty();
  1136. if($order->isEmpty() || $order->pay_status == PayEnum::ISPAID) {
  1137. return true;
  1138. }
  1139. switch ($params['attach']) {
  1140. case 'recharge':
  1141. PayNotifyLogic::handle('recharge', $params['out_trade_no'], $extra);
  1142. break;
  1143. case 'goods':
  1144. $res = PayNotifyLogic::handle('goods', $params['out_trade_no'], $extra);
  1145. if($res === true){
  1146. // 用户下单后,给订单运营专员(配置固定ID)发送公众号提醒(订单信息)
  1147. $order = RechargeOrder::where('sn', $message['out_trade_no'])
  1148. ->where('payment_type','IN',[0,1])
  1149. ->where('pay_status','=',1)
  1150. ->findOrEmpty();
  1151. if(!$order->isEmpty()){
  1152. $workDetail = ServiceWork::findOrEmpty($order->work_id);
  1153. if(!$workDetail->isEmpty()){
  1154. event('Notice', [
  1155. 'scene_id' => 100,
  1156. 'params' => [
  1157. 'user_id' => 0,
  1158. 'order_id' => $workDetail['id'],
  1159. 'thing3' => $workDetail['title'],
  1160. 'time6' => $workDetail['appointment_time'],
  1161. 'phone_number8' => asteriskString($workDetail['mobile']),
  1162. 'thing5' => (iconv_strlen($workDetail['address'])>15)?(mb_substr($workDetail['address'],0,15,'UTF-8').'...'):$workDetail['address'],
  1163. ]
  1164. ]);
  1165. }
  1166. }
  1167. // 订单完成通知【给用户】 - 尾款 -通知
  1168. $order = RechargeOrder::where('sn', $message['out_trade_no'])
  1169. ->where('payment_type','=',2)
  1170. ->where('pay_status','=',1)
  1171. ->findOrEmpty();
  1172. if(!$order->isEmpty()){
  1173. event('Notice', [
  1174. 'scene_id' => 120,
  1175. 'params' => [
  1176. 'user_id' => $order['user_id']
  1177. ]
  1178. ]);
  1179. }
  1180. }
  1181. break;
  1182. }
  1183. return true;
  1184. } catch (\Exception $e) {
  1185. self::setError($e->getMessage());
  1186. return false;
  1187. }
  1188. }
  1189. }