ServiceOrderLogic.php 58 KB

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