ServiceOrderLogic.php 59 KB

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