Order.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 订单逻辑
  7. */
  8. namespace app\allwin\widget;
  9. use app\allwin\widget\Inform;
  10. use app\allwin\model\CouponUser; //用户优惠券
  11. use app\allwin\model\CouponSubsidize; //平台补贴优惠券
  12. use app\allwin\model\Fund; //基金
  13. use app\allwin\model\MchIdQueen; //队列
  14. use app\allwin\model\MchId; //商户号
  15. use app\allwin\model\Bank;
  16. use app\allwin\model\AllwinStore;
  17. use app\allwin\model\BankBill;
  18. use app\allwin\model\StoreBill;
  19. use app\allwin\model\FundBill;
  20. class Order{
  21. /**
  22. * 微信买单收益分账
  23. * @param array $order 订单信息
  24. * @return void
  25. */
  26. public static function income(array $order){
  27. $store = AllwinStore::where(['id' => $order['store_id']])->field('id,member_miniapp_id,charges,manage_uid,mch_id,name')->find();
  28. if(empty($store)){
  29. return;
  30. }
  31. //判断当前会员是否买单好店的用户体系,是当前好店的不分润
  32. $setting = model('AllwinConfig')->getConfig($store->member_miniapp_id);
  33. $is_profitsharing = false;
  34. if($setting->is_fees_types <= 1){
  35. if($setting->is_fees_types == 0){
  36. $is_profitsharing = true;
  37. }else{
  38. if($order['coupon_user_id']){
  39. $is_profitsharing = true;
  40. }
  41. }
  42. }
  43. $default_charges = $setting->default_charges/1000; //默认手续费
  44. $charges = $default_charges; //好店服务费
  45. $user_store_id = 0; //下单用户来自商家
  46. $user_store_manage_uid = 0; //下单用户来自商家的管理员ID(用于奖励推荐开通会员的商家UID)
  47. $is_charges = false; //是否分润
  48. //判断是否这个店的会员
  49. if($store->charges > 0 && $is_profitsharing){
  50. $allwinUser = model('AllwinUser')->getUserStoreId($order['uid']);
  51. $user_store_id = empty($allwinUser) ? 0 : $allwinUser->store_id;
  52. if($user_store_id != $store->id){
  53. $user_store_manage_uid = empty($allwinUser->store) ? 0 : $allwinUser->store->manage_uid; //好店老板UID
  54. $charges = $store->charges/1000; //商家基础服务费
  55. $is_charges = true; //分润
  56. }
  57. }
  58. /**
  59. * 计算金额
  60. * $order['store_amount'] 好店应收金额
  61. * $order['order_amount'] 实际支付金额
  62. */
  63. $wechat_charges_amout = money(abs($order['order_amount']*$default_charges)); //微信扣除的服务费(厘四舍五入到分)
  64. $income = 0;
  65. if($is_charges == true){
  66. $service_amount = money($order['store_amount']*$charges);
  67. $income = money($service_amount-$wechat_charges_amout-0.01); //分账的本金(不包含微信手续费,减去1分保证分账金额)
  68. $store_amount = money($order['store_amount']-$service_amount); //老板收入
  69. }else{
  70. $store_amount = money($order['store_amount']-$wechat_charges_amout); //老板收入
  71. }
  72. if ($store_amount < 0){
  73. return; //金额小于0 不结算
  74. }
  75. $queen = []; //分账队列参数
  76. if ($setting->is_psp == 0 || empty($store->mch_id)) {
  77. Bank::dueMoney($store->member_miniapp_id,$store->manage_uid,$store_amount);
  78. $msg = '结算到平台';
  79. }else{
  80. $msg = '结算到商户号';
  81. }
  82. if ($setting->is_wechat_profitsharing && $is_profitsharing){
  83. if($store->mch_id == 0){
  84. $mch = MchId::getMch(0,$store->member_miniapp_id);
  85. $store->mch_id = $mch->id;
  86. }
  87. $queen[] = [
  88. 'member_miniapp_id' => $store->member_miniapp_id,
  89. 'store_id' => $store->id,
  90. 'uid' => 0,
  91. 'mch_id' => $store->mch_id,
  92. 'amount' => $store_amount*100,
  93. 'transaction_id' => $order['paid_no'],
  94. 'out_order_no' => $order['order_no'],
  95. 'is_finish' => 0,
  96. 'types' => 1,
  97. 'msg' => $msg,
  98. ];
  99. }
  100. StoreBill::add(['miniapp_id' => $store->member_miniapp_id,'uid' => $store->manage_uid,'store_id' => $store->id,'pay_uid' => $order['uid'],'money' => $store_amount],$msg); //商家账单
  101. BankBill::add(['miniapp_id' => $store->member_miniapp_id,'uid' => $store->manage_uid,'store_id' => $store->id,'pay_uid' => $order['uid'],'money' => $store_amount],$msg); //个人账单
  102. model('StoreStats')->payment($store,$store_amount); //增加统计
  103. //增加优惠券统计
  104. if($order['coupon_price'] > 0){
  105. model('StoreStats')->coupon($store,$order['coupon_price']);
  106. }
  107. //判断分账金额最小0.1元
  108. $nominate_money = 0;
  109. $agent_rebate = 0;
  110. $store_rebate = 0;
  111. $company_rebate = 0;
  112. $workers_rebate = 0;
  113. if($is_charges && $is_profitsharing && $income >= 0.1){
  114. //会员直推奖励
  115. $level = model('Levels')->where(['user_id' => $order['uid'],'level'=> 1])->find();
  116. if (!empty($level)) {
  117. $vip = model('Vip')->where(['user_id' => $level->parent_id,'is_lock' => 0,'state' => 1])->field('vipcard_id,user_id')->find();
  118. if(!empty($vip)){
  119. if($vip->vipcard->is_tax){
  120. $nominate_money = money($income*$vip->vipcard->nominate/100);
  121. if($nominate_money > 0.01){
  122. if($setting->is_wechat_profitsharing){
  123. $queen[] = [
  124. 'member_miniapp_id' => $store->member_miniapp_id,
  125. 'store_id' => $store->id,
  126. 'uid' => $level->parent_id,
  127. 'mch_id' => 0,
  128. 'amount' => $nominate_money*100,
  129. 'transaction_id' => $order['paid_no'],
  130. 'out_order_no' => $order['order_no'],
  131. 'is_finish' => 0,
  132. 'types' => 0,
  133. 'msg' => '['.$vip->vipcard->name.']推荐奖励',
  134. ];
  135. }else{
  136. Bank::dueMoney($store->member_miniapp_id,$level->parent_id,(float)$nominate_money);
  137. BankBill::add(['miniapp_id' => $store->member_miniapp_id,'store_id' => $store->id,'money' => $nominate_money,'uid'=> $level->parent_id,'pay_uid' => $order['uid']],'['.$vip->vipcard->name.']消费奖励');
  138. }
  139. }
  140. }
  141. }
  142. }
  143. //社群主奖励
  144. if($setting->agent_rebate > 0){
  145. $agentuid = model('AllwinUserAgent')->agentUid($order['uid'],$store->member_miniapp_id);
  146. if($agentuid){
  147. $agent_rebate = money($income*$setting->agent_rebate/100);
  148. if ($agent_rebate > 0.01) {
  149. if($setting->is_wechat_profitsharing){
  150. $queen[] = [
  151. 'member_miniapp_id' => $store->member_miniapp_id,
  152. 'store_id' => $store->id,
  153. 'uid' => $agentuid,
  154. 'mch_id' => 0,
  155. 'amount' => $agent_rebate*100,
  156. 'transaction_id' => $order['paid_no'],
  157. 'out_order_no' => $order['order_no'],
  158. 'is_finish' => 0,
  159. 'types' => 0,
  160. 'msg' => '[社群主]消费奖励',
  161. ];
  162. }else{
  163. Bank::dueMoney($store->member_miniapp_id,$agentuid,(float)$agent_rebate);
  164. BankBill::add(['miniapp_id' => $store->member_miniapp_id,'store_id' => $store->id,'money' => $agent_rebate,'uid'=> $agentuid,'pay_uid' =>$order['uid']],'[社群主]消费奖励');
  165. }
  166. }
  167. }
  168. }
  169. //判断分成用户所属商家ID
  170. if($user_store_manage_uid > 0){
  171. $store_rebate = money($income*$setting->store_rebate/100);
  172. if ($store_rebate > 0.01) {
  173. if($setting->is_wechat_profitsharing){
  174. $queen[] = [
  175. 'member_miniapp_id' => $store->member_miniapp_id,
  176. 'store_id' => $store->id,
  177. 'uid' => $user_store_manage_uid,
  178. 'mch_id' => 0,
  179. 'amount' => $store_rebate*100,
  180. 'transaction_id' => $order['paid_no'],
  181. 'out_order_no' => $order['order_no'],
  182. 'is_finish' => 0,
  183. 'types' => 0,
  184. 'msg' => '[其它好店]买单奖励',
  185. ];
  186. }else{
  187. $store_rebate_bill = [
  188. 'miniapp_id' => $store->member_miniapp_id,
  189. 'uid' => $user_store_manage_uid,
  190. 'store_id' => $allwinUser->store_id,
  191. 'pay_uid' => $order['uid'],
  192. 'money' => $store_rebate
  193. ];
  194. Bank::dueMoney($store->member_miniapp_id,$user_store_manage_uid,(float)$store_rebate); //商家分润
  195. StoreBill::add($store_rebate_bill,'[其它好店]买单奖励'); //商家账单
  196. BankBill::add($store_rebate_bill,'[其它好店]买单奖励'); //个人账单
  197. }
  198. }
  199. }
  200. //给合伙人
  201. if($setting->workers_rebate > 0){
  202. $worker = model('Worker')->whereRaw("FIND_IN_SET({$store->id},store_ids)")->field('uid')->find();
  203. if(!empty($worker)){
  204. $workers_rebate = money($income*$setting->workers_rebate/100);
  205. if ($workers_rebate > 0.01) {
  206. if ($setting->is_wechat_profitsharing) {
  207. $queen[] = [
  208. 'member_miniapp_id' => $store->member_miniapp_id,
  209. 'store_id' => $store->id,
  210. 'uid' => $worker->uid,
  211. 'mch_id' => 0,
  212. 'amount' => $workers_rebate*100,
  213. 'transaction_id' => $order['paid_no'],
  214. 'out_order_no' => $order['order_no'],
  215. 'is_finish' => 0,
  216. 'types' => 0,
  217. 'msg' => '[合伙人]买单奖励',
  218. ];
  219. } else {
  220. Bank::dueMoney($store->member_miniapp_id,$worker->uid,(float)$workers_rebate);
  221. BankBill::add(['miniapp_id' => $store->member_miniapp_id,'store_id' => $store->id,'money' => $workers_rebate,'uid'=> $worker->uid,'pay_uid' =>$order['uid']], '[合伙人]买单奖励');
  222. }
  223. }
  224. }
  225. }
  226. //给归属公司分账
  227. if($store->city_id > 0){
  228. $city = model('City')->where(['id' => $store->city_id])->find();
  229. if (!empty($city)) {
  230. $company_rebate = money($income*$setting->company_rebate/100);
  231. if ($company_rebate > 0.01) {
  232. if ($setting->is_wechat_profitsharing) {
  233. //分账到商户号
  234. $queen[] = [
  235. 'member_miniapp_id' => $store->member_miniapp_id,
  236. 'city_id' => $store->city_id,
  237. 'store_id' => $store->id,
  238. 'uid' => 0,
  239. 'mch_id' => $city->mch_id,
  240. 'amount' => $company_rebate*100,
  241. 'transaction_id' => $order['paid_no'],
  242. 'out_order_no' => $order['order_no'],
  243. 'is_finish' => 0,
  244. 'types' => 1,
  245. 'msg' => '[城市]买单奖励',
  246. ];
  247. } else {
  248. $ompany_rebate_bill = [
  249. 'miniapp_id' => $store->member_miniapp_id,
  250. 'store_id' => $store->id,
  251. 'city_id' => $store->city_id,
  252. 'uid' => $city->uid,
  253. 'pay_uid' => $order['uid'],
  254. 'money' => $company_rebate
  255. ];
  256. Bank::dueMoney($store->member_miniapp_id,$city->uid,(float)$company_rebate); //商家分润
  257. BankBill::add($ompany_rebate_bill, '[城市]买单奖励'); //个人账单
  258. model('CityBill')->add($ompany_rebate_bill, '[城市]买单奖励'); //公司奖励
  259. }
  260. }
  261. }
  262. }
  263. //扣除运营费(给运营城市)
  264. $platform_money = money($income-($nominate_money+$agent_rebate+$store_rebate+$company_rebate+$workers_rebate));
  265. if($platform_money > 0.01){
  266. $mch = MchId::getMch(0,$store->member_miniapp_id);
  267. if (!empty($mch)) {
  268. if($setting->is_wechat_profitsharing){
  269. $queen[] = [
  270. 'member_miniapp_id' => $store->member_miniapp_id,
  271. 'store_id' => $store->id,
  272. 'uid' => 0,
  273. 'mch_id' => $mch->id,
  274. 'amount' => $platform_money*100,
  275. 'transaction_id' => $order['paid_no'],
  276. 'out_order_no' => $order['order_no'],
  277. 'is_finish' => 0,
  278. 'types' => 1,
  279. 'msg' => '[运营公司]服务费结余',
  280. ];
  281. }else{
  282. if(isset($mch->city->uid)){
  283. $platform_rebate_bill = [
  284. 'miniapp_id' => $store->member_miniapp_id,
  285. 'store_id' => $store->id,
  286. 'uid' => $mch->city->uid,
  287. 'pay_uid' => $order['uid'],
  288. 'money' => $platform_money
  289. ];
  290. Bank::dueMoney($store->member_miniapp_id,$mch->city->uid,(float)$platform_money); //商家分润
  291. BankBill::add($platform_rebate_bill,'[运营公司]买单服务费结余'); //个人账单
  292. model('CityBill')->add($platform_rebate_bill,'[运营公司]买单服务费结余'); //公司奖励
  293. }
  294. }
  295. }
  296. }
  297. }
  298. //平台优惠券
  299. $platform_amount = [];
  300. if(!empty($order['platform_amount']) && $is_profitsharing){
  301. $platform_amount = self::platform_amount($order,$store,$setting);
  302. if(!empty($platform_amount)){
  303. $queen = array_merge($queen,$platform_amount);
  304. }
  305. }
  306. //添加一个无需分账的队列
  307. if(empty($queen) && $setting->is_wechat_profitsharing && $is_profitsharing){
  308. $queen[] = [
  309. 'member_miniapp_id' => $store->member_miniapp_id,
  310. 'store_id' => $store->id,
  311. 'uid' => 0,
  312. 'mch_id' => 0,
  313. 'amount' => 0,
  314. 'transaction_id' => $order['paid_no'],
  315. 'out_order_no' => $order['order_no'],
  316. 'is_finish' => 0,
  317. 'types' => 2,
  318. 'msg' => '无需分账,金额¥'.$store_amount,
  319. ];
  320. }
  321. //增加队列
  322. if(!empty($queen)){
  323. MchIdQueen::createQueen($queen);
  324. }
  325. //订单公众号模板通知
  326. if(!empty($setting->tplmsg_order)){
  327. Inform::order(['uid' => $store->manage_uid,'store_id' => $store->id,'store_name' => $store->name,'amount' => $order['store_amount'],'order_no' => $order['order_no'],'miniapp_id' => $store->member_miniapp_id,'tplmsg' => $setting->tplmsg_order]);
  328. }
  329. return true;
  330. }
  331. /**
  332. * 优惠券分配
  333. * @param array $store [分配金额]
  334. * @param object $store [店铺信息]
  335. * @param object $setting [配置信息]
  336. * @return array
  337. */
  338. public static function platform_amount($order,$store,$setting){
  339. if(empty($order['platform_amount'])){
  340. return [];
  341. }
  342. $platform_amount = $order['platform_amount'];
  343. $city = model('City')->where(['id' => $store->city_id])->find();
  344. $queen = [];
  345. //分配优惠券利润给加盟城市
  346. $city_money = $platform_amount*$setting->city_rate/100;
  347. if($city_money >= 0.01){
  348. if(!empty($city)) {
  349. if ($setting->is_wechat_profitsharing) {
  350. $queen[] = [
  351. 'member_miniapp_id' => $store->member_miniapp_id,
  352. 'city_id' => $store->city_id,
  353. 'store_id' => $store->id,
  354. 'uid' => 0,
  355. 'mch_id' => $city->mch_id,
  356. 'amount' => $city_money*100,
  357. 'transaction_id' => $order['paid_no'],
  358. 'out_order_no' => $order['order_no'],
  359. 'is_finish' => 0,
  360. 'types' => 1,
  361. 'msg' => '[城市]优惠券分润',
  362. ];
  363. } else {
  364. $bill = [
  365. 'miniapp_id' => $store->member_miniapp_id,
  366. 'store_id' => $store->id,
  367. 'city_id' => $store->city_id,
  368. 'uid' => $city->uid,
  369. 'pay_uid' => $order['uid'],
  370. 'money' => $city_money
  371. ];
  372. Bank::dueMoney($store->member_miniapp_id,$city->uid,$city_money); //商家分润
  373. BankBill::add($bill,'[城市]优惠券分润'); //个人账单
  374. model('CityBill')->add($bill,'[城市]优惠券分润'); //公司奖励
  375. }
  376. }
  377. }
  378. //分配优惠券给平台方
  379. $admin_money = empty($city->is_default) ? $platform_amount*($setting->admin_rate/100) : $platform_amount*(1-$setting->city_rate/100);
  380. if($admin_money >= 0.01){
  381. if ($setting->is_wechat_profitsharing) {
  382. $mch = MchId::getMch();
  383. $queen[] = [
  384. 'member_miniapp_id' => $store->member_miniapp_id,
  385. 'city_id' => $store->city_id,
  386. 'store_id' => $store->id,
  387. 'uid' => 0,
  388. 'mch_id' => $mch->id,
  389. 'amount' => $admin_money*100,
  390. 'transaction_id' => $order['paid_no'],
  391. 'out_order_no' => $order['order_no'],
  392. 'is_finish' => 0,
  393. 'types' => 1,
  394. 'msg' => '[平台]优惠券分润',
  395. ];
  396. }else{
  397. Bank::dueMoney($store->member_miniapp_id,$setting->admin_uid,$admin_money); //商家分润
  398. BankBill::add(['uid' => $setting->admin_uid,'money' => $admin_money,'miniapp_id' => $store->member_miniapp_id,'store_id' => $store->id,'city_id' => $store->city_id,'pay_uid' => $order['uid']],'[城市]优惠券分润'); //个人账单
  399. }
  400. }
  401. //分配优惠券给运营公司
  402. if(empty($city->is_default)){
  403. $agent_money = $platform_amount*($setting->company_rate/100);
  404. if($agent_money >= 0.01){
  405. $agent_mch = MchId::getMch(0,$store->member_miniapp_id);
  406. if ($setting->is_wechat_profitsharing) {
  407. $queen[] = [
  408. 'member_miniapp_id' => $store->member_miniapp_id,
  409. 'city_id' => $store->city_id,
  410. 'store_id' => $store->id,
  411. 'uid' => 0,
  412. 'mch_id' => $agent_mch->id,
  413. 'amount' => $agent_money*100,
  414. 'transaction_id' => $order['paid_no'],
  415. 'out_order_no' => $order['order_no'],
  416. 'is_finish' => 0,
  417. 'types' => 1,
  418. 'msg' => '[运营公司]优惠券分润',
  419. ];
  420. } else {
  421. $agent_bill = [
  422. 'miniapp_id' => $store->member_miniapp_id,
  423. 'store_id' => $store->id,
  424. 'city_id' => $store->city_id,
  425. 'uid' => $agent_mch->city->uid,
  426. 'pay_uid' => $order['uid'],
  427. 'money' => $agent_money
  428. ];
  429. Bank::dueMoney($store->member_miniapp_id,$agent_mch->city->uid,$agent_money); //商家分润
  430. BankBill::add($agent_bill, '[运营公司]优惠券分润'); //个人账单
  431. model('CityBill')->add($agent_bill, '[运营公司]优惠券分润'); //公司奖励
  432. }
  433. }
  434. }
  435. return $queen;
  436. }
  437. /**
  438. * 采购基金商户补贴
  439. * @param array $order [计算参数]
  440. * @return boolean
  441. */
  442. public static function fund(array $order){
  443. if($order['coupon_user_id'] <= 0){
  444. return;
  445. }
  446. $condition['uid'] = $order['uid'];
  447. $condition['member_miniapp_id'] = $order['member_miniapp_id'];
  448. $condition['id'] = $order['coupon_user_id'];
  449. $rel = CouponUser::getuser($condition);
  450. if(empty($rel)){
  451. return;
  452. }
  453. $fund_coupon = CouponSubsidize::where(['coupon_id' => $rel->coupon_id])->find();
  454. if(empty($fund_coupon)){
  455. return;
  456. }
  457. if($fund_coupon->how_much <= 0){
  458. return;
  459. }
  460. $fund = new Fund();
  461. $subsidy = $fund->subsidy($order['member_miniapp_id'],$fund_coupon->how_much);
  462. if($subsidy && $rel->store->manage_uid > 0){
  463. Bank::dueMoney($order['member_miniapp_id'],$rel->store->manage_uid,(float)$subsidy); //结算利润
  464. model('StoreStats')->payment($order['member_miniapp_id'],$order['store_id'],$subsidy); //增加统计
  465. $bill = [
  466. 'miniapp_id' => $order['member_miniapp_id'],
  467. 'store_id' => $order['store_id'],
  468. 'pay_uid' => $order['uid'],
  469. 'money' => $subsidy,
  470. 'order_no' => $order['order_no'],
  471. 'uid' => $rel->store->manage_uid,
  472. 'types' => 1
  473. ];
  474. BankBill::add($bill,'消费补贴'); //个人账单
  475. StoreBill::add($bill,'消费补贴'); //商家账单
  476. FundBill::add($bill); //补贴出账
  477. }
  478. }
  479. }