ServiceOrderLogic.php 60 KB

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