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