ServiceOrderLogic.php 60 KB

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