Order.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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\fastshop\widget;
  9. use think\facade\Cookie;
  10. class Order{
  11. /**
  12. * 保存订单
  13. * @param array $item [计算参数]
  14. * @return array 商品价格信息
  15. */
  16. public function saveOrder(array $data){
  17. $order['order_no'] = order_no();
  18. $order['user_id'] = $data['user_id'];
  19. $order['sale_id'] = $data['sale_id'];
  20. $order['member_miniapp_id'] = $data['member_miniapp_id'];
  21. $order['payment_id'] = $data['payment_id'];
  22. $order['real_amount'] = $data['real_amount'];
  23. $order['real_freight'] = $data['real_freight'];
  24. $order['order_amount'] = $data['order_amount'];
  25. $order['express_name'] = $data['express_name'];
  26. $order['express_phone'] = $data['express_phone'];
  27. $order['express_address'] = $data['express_address'];
  28. $order['paid_at'] = $data['paid_at'];
  29. $order['is_fusion'] = $data['is_fusion']; //订单类型
  30. if($data['paid_at'] == 1){
  31. $order['paid_time'] = time();
  32. $order['paid_no'] = $order['order_no'];
  33. }
  34. $order['status'] = 0;
  35. $order['is_del'] = 0;
  36. $order['express_status'] = 0;
  37. $order['order_starttime'] = time();
  38. $rel = model('fastshop/Order')->insertGetId($order);
  39. return empty($rel) ? false : $order['order_no'];
  40. }
  41. /**
  42. * 计算运费多少钱
  43. * @param array $item [计算参数]
  44. * @return array 商品价格信息
  45. */
  46. public function realAmount(array $item,$miniapp_id){
  47. $fare = model('fastshop/fare')->get(['member_miniapp_id' => $miniapp_id]);
  48. $real_amount = $item['sale_price']; //商品总价
  49. $real_freight = 0; //运费总价
  50. $weight = $item['weight']; //商品重量
  51. if($weight <= $fare['first_weight'] || 0 == $fare['second_weight']){
  52. $real_freight = $fare['first_price'];
  53. }else{
  54. $weight = $weight - $fare['second_weight'];
  55. $real_freight = $fare['first_price'] + ceil($weight/$fare['second_weight']) * $fare['second_price'];
  56. }
  57. $data['real_amount'] = money($real_amount); //商品价格
  58. $data['real_freight'] = money($real_freight); //运费
  59. $data['order_amount'] = money($real_freight+$real_amount); //商品总价+运费
  60. return $data;
  61. }
  62. /**
  63. * 读取订单的赠品价格和所属产品图片数据
  64. */
  65. public function gift(array $gift){
  66. $gift_id = array_column($gift,'item_id');
  67. $list = model('fastshop/item')->field('id,name,img,imgs,content,weight')->whereIn('id',$gift_id)->select()->toArray();
  68. $gift_data = [];
  69. foreach ($gift as $k => $v) {
  70. $gift_value['item_id'] = $v['item_id'];
  71. $gift_value['sale_price'] = money($v['sale_price']/100);
  72. $gift_value['market_price'] = money($v['market_price']/100);
  73. foreach ($list as $value) {
  74. if ($v['item_id'] == $value['id']) {
  75. $gift_data[$k] = array_merge($value,$gift_value);
  76. $gift_data[$k]['img'] = $value['img']."?x-oss-process = style/auto";
  77. $gift_data[$k]['imgs'] = json_decode($value['imgs'],true);
  78. $gift_data[$k]['content'] = $value['content'];
  79. }
  80. }
  81. }
  82. return $gift_data;
  83. }
  84. /**
  85. * 指定成交(后台)
  86. * @param [type] $array
  87. * @param [type] $uid
  88. * @return void
  89. */
  90. public function userGift($array,$uid){
  91. $where_entrust['member_miniapp_id'] = $array['member_miniapp_id'];
  92. $where_entrust['item_id'] = $array['item_id'];
  93. $where_entrust['is_rebate'] = 0;
  94. $where_entrust['is_under'] = 0; //1下架
  95. return model('EntrustList')->where($where_entrust)->where(['user_id' => $uid])->find();
  96. }
  97. /**
  98. * 交易顺序
  99. */
  100. public function goodGift($array,$uid,$config){
  101. $where_entrust['member_miniapp_id'] = $array['member_miniapp_id'];
  102. $where_entrust['item_id'] = $array['item_id'];
  103. $where_entrust['is_rebate'] = 0; //未成交
  104. $where_entrust['is_under'] = 0; //1下架
  105. /**
  106. * is_priority 0 市场优先 1客户优先(已关闭配置)
  107. */
  108. $entrust = [];
  109. if($config['is_priority'] == 1){
  110. $level = model('SystemUserLevel')->field('parent_id')->where(['user_id' => $uid,'level' => 1])->find();
  111. if (!empty($level)) {
  112. $entrust = model('EntrustList')->where($where_entrust)->where(['user_id' => $level->parent_id])->order('id asc')->find();
  113. }
  114. }
  115. //市场优先
  116. if (empty($entrust)) {
  117. $entrust = model('EntrustList')->where($where_entrust)->order('id asc')->find();
  118. }
  119. return $entrust;
  120. }
  121. /**
  122. * 计算商品托买利润
  123. * @param integer $miniapp_id //来自哪个小程序
  124. * @param [type] $order_no //订单号(当订单号是0的时候本$sale_id参数作为商品ID)
  125. * @param integer $sale_id //来自哪个活动(当订单号是0的时候本参数作为商品ID)
  126. * @return void //成功返回订单委托金额
  127. */
  128. public function rebate(int $miniapp_id,$order_no,int $item_id,int $uid,$config){
  129. $item = [];
  130. if(empty($order_no)){
  131. $item[] = $item_id;
  132. }else{
  133. $order = model('OrderCache')->field('item_id,order_no,entrust,fusion_state,gift')->where(['order_no' => $order_no])->find();
  134. if(empty($order)){
  135. return;
  136. }
  137. if($order->order->is_fusion){ //买二送一
  138. $item = array_column(json_decode($order->gift,true),'item_id');
  139. }else{
  140. $item[] = $order->item_id;
  141. }
  142. }
  143. //计算利润
  144. foreach ($item as $id) {
  145. $where_entrust['member_miniapp_id'] = $miniapp_id;
  146. $where_entrust['item_id'] = $id;
  147. $where_entrust['is_rebate'] = 0;
  148. //成交判断
  149. if(empty($order_no) && $uid > 0){
  150. $entrust = self::userGift($where_entrust,$uid);
  151. }else{
  152. $entrust = self::goodGift($where_entrust,$uid,$config);
  153. }
  154. $is_diy = empty($order_no) ? 1 : 0;
  155. if (!empty($entrust)){
  156. //读取委托产品(聚变还是裂变)本金(分)
  157. $order_amount = $entrust->is_fusion ? $entrust->entrust_price : $entrust->entrust_price/2;
  158. $cash_fee = abs($entrust['entrust_price']*($config->profit/100)); //计算利润
  159. $income = $cash_fee + $order_amount; //利润+本金
  160. //增加购物积分和应付积分
  161. if($order_amount > 0){
  162. $shop_money = intval($cash_fee*($config->shopping/100)); //购物积分(利润的百分之多少)
  163. $due_money = intval($cash_fee-$shop_money+$order_amount); //应付(本金+利润)
  164. model('Bank')->due_up($miniapp_id,$entrust->user_id,$due_money,$shop_money);
  165. model('Bank')->isProfit($entrust->user_id,money($cash_fee/100)); //增加净收入(需要传入元)
  166. $income = $cash_fee + $order_amount; //委托销售总收益(分)
  167. model('BankLogs')->add($miniapp_id,$entrust->user_id,$income,money($income/100).'成交积分');
  168. }
  169. model('EntrustList')->where(['id' => $entrust->id])->update(['is_diy'=> $is_diy,'is_rebate'=>1,'is_under'=>1,'update_time'=>time(),'rebate' => $income]);
  170. model('entrust')->where(['item_id' => $item_id])->setDec('gite_count',1);//库存减1
  171. }
  172. }
  173. return empty($item_id) ? false : true;
  174. }
  175. /**
  176. * 抢购积分支付
  177. * @param integer $miniapp_id 来源小程序
  178. * @param integer $uid 用户ID
  179. * @param float $cash_fee (分)
  180. * @param [type] $config 系统配置
  181. * @return void
  182. */
  183. public function pointPay(int $miniapp_id,int $uid,float $cash_fee,$config){
  184. $payment_point = $config->payment_point/100;
  185. $point = intval(($cash_fee*$payment_point)*100); //积分支付
  186. $info = model('Bank')->where(['member_miniapp_id' => $miniapp_id,'user_id' => $uid])->find();
  187. if(empty($info)){
  188. return;
  189. }
  190. if($config->payment_type == 1){
  191. $info->due_money = ['dec',$point];
  192. $info->money = ['dec',$point];
  193. }else{
  194. $info->shop_money = ['dec',$point];
  195. $info->money = ['dec',$point];
  196. }
  197. $info->update_time = time();
  198. return $info->save();
  199. }
  200. /**
  201. * 商城积分支付
  202. * @param integer $miniapp_id 来源小程序
  203. * @param integer $uid 用户ID
  204. * @param float $cash_fee (分)
  205. * @param [type] $config 系统配置
  206. * @return void
  207. */
  208. public function shopPointPay(int $miniapp_id,int $uid,float $cash_fee){
  209. $config = model('Config')->get(['member_miniapp_id' => $miniapp_id]);
  210. $payment_point = $config['payment_point_shop']/100;
  211. $point = intval(($cash_fee*$payment_point)*100); //积分支付
  212. $info = model('Bank')->where(['member_miniapp_id'=>$miniapp_id,'user_id' => $uid])->find();
  213. if(empty($info)){
  214. return;
  215. }
  216. if($config['payment_type_shop'] == 1){
  217. $info->due_money = ['dec',$point];
  218. $info->money = ['dec',$point];
  219. }else{
  220. $info->shop_money = ['dec',$point];
  221. $info->money = ['dec',$point];
  222. }
  223. $info->update_time = time();
  224. return $info->save();
  225. }
  226. }