ServiceOrderLogic.php 55 KB

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