ServiceOrderLogic.php 58 KB

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