ServiceOrderLogic.php 55 KB

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