SmartbcOrder.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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\smartbc\model;
  9. use think\Model;
  10. use app\smartbc\model\SmartbcCouponUser; //用户优惠券
  11. class SmartbcOrder extends Model{
  12. public function store(){
  13. return $this->hasOne('SmartbcStore','id','store_id');
  14. }
  15. public function conponUser(){
  16. return $this->hasOne('SmartbcCouponUser','id','coupon_user_id');
  17. }
  18. /**
  19. * 创建订单
  20. * @param array $param
  21. * @return void
  22. */
  23. public static function addOrder(array $param){
  24. $data = [];
  25. $data['member_miniapp_id'] = $param['member_miniapp_id'];
  26. $data['store_id'] = $param['store_id'];
  27. $data['store_chain_id'] = $param['store_chain_id'];
  28. $data['parent_store_id'] = $param['parent_store_id'] ?? 0;
  29. $data['uid'] = $param['uid'];
  30. $data['coupon_user_id'] = $param['user_couponr_id'];
  31. $data['order_no'] = $param['order_no'];
  32. $data['amount'] = $param['money']; //输入金额
  33. $data['price'] = $param['price']; //实际金额
  34. $data['coupon_cache'] = empty($param['coupon_cache'])?'':json_encode($param['coupon_cache']); //留存优惠券
  35. $data['state'] = 0;
  36. $data['update_time'] = time();
  37. return self::insertGetId($data);
  38. }
  39. /**
  40. * 计算器付款价格
  41. * @param array $param [计算参数]
  42. * @return type
  43. */
  44. public static function countPrice(array $param){
  45. $money = floatval($param['money']); //用户默认付款价
  46. //我的优惠券
  47. $coupon_user = [];
  48. $coupon_user['member_miniapp_id'] = $param['member_miniapp_id'];
  49. $coupon_user['store_id'] = $param['store_id'];
  50. $coupon_user['id'] = $param['user_couponr_id'];
  51. $coupon_user['uid'] = $param['uid'];
  52. $coupon_user['is_end'] = 0;
  53. $coupon_user_rel = SmartbcCouponUser::where($coupon_user)->find();
  54. //需要优惠的价格(分)
  55. $coupon_user_price = 0;
  56. if(!empty($coupon_user_rel)){
  57. if($money < $coupon_user_rel->howmuch){
  58. return false;
  59. }
  60. $config = SmartbcConfig::getConfig($coupon_user['member_miniapp_id']);
  61. if(!empty($config->end_time) && ($coupon_user_rel->create_time + $config->end_time * 60 * 60) <= time()){
  62. return false;
  63. }
  64. if($coupon_user_rel->types){//折扣
  65. $coupon_user_price = $money - $money * ($coupon_user_rel->discount/10);
  66. }else{//抵扣
  67. $coupon_user_price = $coupon_user_rel->price;
  68. }
  69. }
  70. $amount = $money - $coupon_user_price;
  71. $data['price'] = $amount <= 0 ? 0.01 : $amount;
  72. $data['coupon'] = $coupon_user_rel;
  73. return $data;
  74. }
  75. /**
  76. * 微信买单收益分账
  77. * @param array $order 订单信息
  78. * @return void
  79. */
  80. public static function income($order,$allMoney){
  81. $store = SmartbcStore::where(['id' => $order->store_id])->find();
  82. if(empty($store)){
  83. $order->parent_store_price = 0;
  84. $order->real = $order->price;
  85. return $order;
  86. }
  87. $setting = SmartbcConfig::getConfig($store->member_miniapp_id);
  88. if($order->parent_store_id > 0 && !empty($setting) && !empty($setting->profit)){
  89. $storeProfit = SmartbcStore::where(['id' => $order->parent_store_id])->find();
  90. if(empty($storeProfit)){
  91. return;
  92. }
  93. $queen = []; //分账队列参数
  94. $storeMoney = ($allMoney - money($allMoney * $setting->profit)) * 100;
  95. $profitMoney = money($allMoney * $setting->profit) * 100;
  96. //分给商户
  97. $queen[] = [
  98. 'member_miniapp_id' => $store->member_miniapp_id,
  99. 'store_id' => $store->id,
  100. 'uid' => 0,
  101. 'mch_id' => $store->mch_id,
  102. 'amount' => $storeMoney,
  103. 'transaction_id' => $order->paid_no,
  104. 'out_order_no' => $order->order_no,
  105. 'is_finish' => 0,
  106. 'types' => 1,
  107. 'msg' => '结算到商户号',
  108. ];
  109. $queen[] = [
  110. 'member_miniapp_id' => $storeProfit->member_miniapp_id,
  111. 'store_id' => $storeProfit->id,
  112. 'uid' => 0,
  113. 'mch_id' => $storeProfit->mch_id,
  114. 'amount' => $profitMoney,
  115. 'transaction_id' => $order->paid_no,
  116. 'out_order_no' => $order->order_no,
  117. 'is_finish' => 0,
  118. 'types' => 1,
  119. 'msg' =>'结算到商户号',
  120. ];
  121. $order->parent_store_price = $profitMoney;
  122. $order->real = $storeMoney;
  123. SmartbcQueen::createQueen($queen);
  124. SmartbcStoreBill::add(['miniapp_id' => $storeProfit->member_miniapp_id,'uid' => $storeProfit->manage_uid,'store_id' => $storeProfit->id,'pay_uid' => $order['user_id'],'money' => $profitMoney],'商圈引荐买单奖励'); //引荐商家账单
  125. }
  126. return $order;
  127. }
  128. }