ServiceOrderLogic.php 61 KB

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