ServiceOrderLogic.php 57 KB

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