AisShopOrder.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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\ais\model;
  9. use app\common\model\SystemUserLevel;
  10. use think\Model;
  11. class AisShopOrder extends Model{
  12. protected $pk = 'id';
  13. protected $type = [
  14. 'shop_cache' => 'array',
  15. ];
  16. /**
  17. * @return \think\model\relation\HasOne
  18. * 商品
  19. */
  20. public function shop(){
  21. return $this->hasOne('AisShop','id','shop_id');
  22. }
  23. /**
  24. * @return \think\model\relation\HasOne
  25. * 店铺
  26. */
  27. public function store(){
  28. return $this->hasOne('AisStore','id','store_id');
  29. }
  30. /**
  31. * @return \think\model\relation\hasMany
  32. * 店铺管理员类别
  33. */
  34. public function storeWorker(){
  35. return $this->hasMany('AisStoreWorker','store_id','store_id');
  36. }
  37. /**
  38. * @return \think\model\relation\HasOne
  39. * 用户
  40. */
  41. public function user(){
  42. return $this->hasOne('app\common\model\SystemUser','id','uid');
  43. }
  44. /**
  45. * 订单是否核销
  46. */
  47. public function getStatuAttr($value,$data){
  48. $statu = [0 =>'待核销',1 =>'已核销'];
  49. return $statu[$data['status']];
  50. }
  51. /**
  52. * @param $data
  53. * @param $order_no
  54. * 购买商品生成订单数据
  55. */
  56. public static function createOrder($param,$order_no){
  57. $order = [
  58. 'shop_id' => $param['id'],
  59. 'store_id' => $param['store_id'],
  60. 'member_miniapp_id' => $param['member_miniapp_id'],
  61. 'phone' => $param['telphone'],
  62. 'message' => $param['message'],
  63. 'share_uid' => $param['share_uid'] ?: 0,
  64. 'uid' => $param['uid'],
  65. 'order_no' => $order_no,
  66. 'points' => $param['points'],
  67. 'amount' => $param['amount'],
  68. 'sell_price' => $param['sell_price'],
  69. 'thrifty' => $param['thrifty'],
  70. 'shop_cache' => $param['shop_cache'],
  71. 'status' => 0,
  72. 'is_del' => 0,
  73. 'paid_at' => 0,
  74. 'create_time' => time()
  75. ];
  76. return self::create($order);
  77. }
  78. public static function income($order,$total_fee,$flag){
  79. $allMoney = money(($total_fee - $total_fee * 6 / 1000));
  80. $ex = 0;
  81. $order->real = $allMoney;
  82. $store = AisStore::where(['id' => $order->store_id])->field('id,member_miniapp_id,charges,manage_uid,mch_id,name')->find();
  83. if(empty($store) && $flag == 'N'){
  84. return;
  85. }
  86. //商品信息
  87. $shopItme = AisShop::where(['member_miniapp_id'=>$order->member_miniapp_id,'id' => $order->shop_id])->find();
  88. $queen = []; //分账队列参数
  89. //上级会员
  90. $level = SystemUserLevel::where(['user_id' => $order->uid,'level' => 1])->find();
  91. //发放会员收益
  92. if($store->manage_uid != $level->parent_id && $shopItme->share_vip_price){
  93. if($level){
  94. $money = money($shopItme->sell_price * $shopItme->share_vip_price);
  95. $allMoney -= $money;
  96. $ex += $money;
  97. if($money >= 0){
  98. //会员余额增加
  99. $bank = AisBank::where(['uid' => $level->parent_id])->find();
  100. if(empty($bank)){
  101. $bank = new AisBank;
  102. $bank->uid = $level->parent_id;
  103. $bank->member_miniapp_id = $order->member_miniapp_id;
  104. $bank->money = 0;
  105. }
  106. $bank->money = $bank->money + $money;
  107. $bank->save();
  108. AisBill::add(['miniapp_id'=>$order->member_miniapp_id,'store_id' => 0,'money' => $money,'uid'=>$level->parent_id], '优选下单会员收益');
  109. }
  110. }
  111. }
  112. //发放分享收益
  113. if($order->share_uid){
  114. $money = money($shopItme->sell_price * $shopItme->share_price);
  115. $allMoney -= $money;
  116. $ex += $money;
  117. if($money >= 0){
  118. //会员余额增加
  119. $bank = AisBank::where(['uid' => $order->share_uid])->find();
  120. if(empty($bank)){
  121. $bank = new AisBank;
  122. $bank->uid = $order->share_uid;
  123. $bank->member_miniapp_id = $order->member_miniapp_id;
  124. $bank->money = 0;
  125. }
  126. $bank->money = $bank->money + $money;
  127. $bank->save();
  128. AisBill::add(['miniapp_id'=>$order->member_miniapp_id,'store_id' => 0,'money' => $money,'uid'=>$order->share_uid], '分享商品收益');
  129. }
  130. }
  131. if($allMoney > 0){
  132. if($store->mch_id){
  133. //分给商户
  134. $queen[] = [
  135. 'member_miniapp_id' => $store->member_miniapp_id,
  136. 'store_id' => $store->id,
  137. 'uid' => 0,
  138. 'mch_id' => $store->mch_id,
  139. 'amount' => $allMoney * 100,
  140. 'transaction_id' => $order->paid_no,
  141. 'out_order_no' => $order->order_no,
  142. 'is_finish' => 0,
  143. 'types' => 1,
  144. 'msg' => '结算到商户号',
  145. ];
  146. }
  147. if($ex > 0){
  148. //平台收入
  149. $queen[] = [
  150. 'member_miniapp_id' => $order->member_miniapp_id,
  151. 'store_id' => 0,
  152. 'uid' => 0,
  153. 'mch_id' => 0,
  154. 'amount' => $ex * 100,
  155. 'transaction_id' => $order->paid_no,
  156. 'out_order_no' => $order->order_no,
  157. 'is_finish' => 0,
  158. 'types' => 2,
  159. 'msg' => '结算到平台',
  160. ];
  161. }
  162. AisQueen::createQueen($queen);
  163. if($shopItme->warehouse_num >= 0){
  164. $shopItme->warehouse_num = $shopItme->warehouse_num - 1;
  165. $shopItme->save();
  166. }
  167. }
  168. $order->real = $allMoney;
  169. return $order;
  170. }
  171. }