ServiceOrderLogic.php 58 KB

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