ServiceOrderLogic.php 60 KB

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