where("user_id", $order['user_id'])->find(); $pattern = tpCache('distribut.pattern',$stoid); // 分销模式 $first_rate = tpCache('distribut.first_rate',$stoid); // 一级比例 $second_rate = tpCache('distribut.second_rate',$stoid); // 二级比例 $third_rate = tpCache('distribut.third_rate',$stoid); // 三级比例 $first_money = 0; // 一级赚到的钱 $second_money = 0; // 二级赚到的钱 $thirdmoney = 0; // 三级赚到的钱 $allmoney= 0; //获取所有商品 $order_goods=$odr_garr; if(empty($order_goods)) $order_goods = M('order_goods')->where("order_id", $order['order_id'])->select(); // 订单所有商品 /*---积累分成---*/ $commission = 0; /*--循环判断商品的条件--*/ foreach($order_goods as $k => $v) { $tmp_commission = 0; $goods = M('goods')->where("goods_id", $v['goods_id'])->find(); // 单个商品的佣金 //获取活动的佣金,如果活动未设置佣金,则佣金0 if($v['prom_type']==1 || $v['prom_type']==2 || $v['prom_type']==4){ switch ($v['prom_type']){ case 1: $tb='flash_sale'; break; case 2: $tb='group_buy'; break; case 4: $tb='integral_buy'; break; } $rrss=M($tb)->where('id',$v['prom_id'])->find(); if($pattern==0) { if (empty($rrss['commission'])) { $tmp_commission = 0; } else { $tmp_commission = $rrss['commission']; } }else if($pattern==1) { $first_money+=$rrss['fir_rate']* $v['goods_num'];; $second_money+=$rrss['sec_rate']* $v['goods_num'];; $thirdmoney+=$rrss['thi_rate']* $v['goods_num'];; } if($tmp_commission!=0) $tmp_commission = $tmp_commission * $v['goods_num']; $jsondata[]=['goods_id'=>$v['goods_id'], 'commission'=>$tmp_commission, 'fir'=>$rrss['fir_rate'], 'sec'=>$rrss['sec_rate'], 'thi'=>$rrss['thi_rate'], 'fir_r'=>$first_rate, 'sec_r'=>$second_rate, 'thi_r'=>$third_rate, 'g_num'=>$v['goods_num'], ]; }else{ //直接获取商品的佣金 if($pattern==0) { $tmp_commission = $goods['commission']; } else if($pattern==1){ $first_money+=$goods['fir_rate']* $v['goods_num']; $second_money+=$goods['sec_rate']* $v['goods_num']; $thirdmoney+=$goods['thi_rate']* $v['goods_num']; } if($tmp_commission!=0) $tmp_commission = $tmp_commission * $v['goods_num']; $jsondata[]=['goods_id'=>$v['goods_id'], 'commission'=>$tmp_commission, 'fir'=>$goods['fir_rate'], 'sec'=>$goods['sec_rate'], 'thi'=>$goods['thi_rate'], 'fir_r'=>$first_rate, 'sec_r'=>$second_rate, 'thi_r'=>$third_rate, 'g_num'=>$v['goods_num'], ]; } $disgoods[]=$v['goods_id']; $commission += $tmp_commission; // 所有商品的累积佣金 } mlog("订单:".$order['order_id'].$order['order_sn'] ."分销金额".$commission.'-'.$first_money.'-'.$second_money.'-'.$thirdmoney, "rebate_log/".$stoid); if( $pattern==0) { // 如果这笔订单没有分销金额 if($commission == 0) return false; $first_money = $commission * ($first_rate / 100); // 一级赚到的钱 $second_money = $commission * ($second_rate / 100); // 二级赚到的钱 $thirdmoney = $commission * ($third_rate / 100); // 三级赚到的钱 } $allmoney= $commission; mlog("订单:".$order['order_id'].$order['order_sn']."一级赚到的钱".$first_money."二级赚到的钱".$second_money."三级赚到的钱".$thirdmoney,"rebate_log/".$stoid); // 微信消息推送 $erpid=tpCache('shop_info.ERPId',$stoid); $reb1=tpCache("distribut.first_rate",$stoid); $reb2=tpCache("distribut.second_rate",$stoid); $reb3=tpCache("distribut.third_rate",$stoid); $remark="交易成功后您将获得奖励"; //是否是分销商 if ( $user['is_distribut'] == 1){ /*--没有一级分享分和二级分享人--*/ if(empty($user['first_leader']) && empty($user['second_leader'])){ if(($first_money + $second_money + $thirdmoney)>0) { $data = array( 'user_id' => $user['user_id'], 'buy_user_id' => $user['user_id'], 'nickname' => $user['nickname'], 'order_sn' => $order['order_sn'], 'order_id' => $order['order_id'], 'goods_price' => $order['goods_price'], 'money' => $first_money + $second_money + $thirdmoney, 'level' => 1, 'create_time' => time(), 'store_id' => $stoid, 'first_rate' => ($first_rate + $second_rate + $third_rate), 'second_rate' => 0, 'third_rate' => 0, 'commission_json' => json_encode($jsondata), 'dis_moshi' => $pattern, 'user_type' => 1, 'pickup_id'=>$order['pickup_id'], ); if ($pattern == 1) { $data['first_rate'] = $first_money + $second_money + $thirdmoney; } M('rebate_log')->add($data); // 微信推送消息 if ($user['oauth'] == 'weixin') { $title = "您的一级会员刚下了一笔订单"; $this->post_msg($stoid,$erpid,$user['user_id'],$title,$order['order_sn'],$order['total_amount'],($first_money + $second_money + $thirdmoney),date('Y年m月d日 H:i'),$remark); } M('order_goods')->where('order_id', $order['order_id']) ->where('goods_id', 'in', $disgoods)->save(['is_distri' => 1]); } } /*--有一级分享分人,没有二级分享人--*/ if(!empty($user['first_leader']) && empty($user['second_leader'])){ if($first_money) { $data = array( 'user_id' => $user['user_id'], 'buy_user_id' => $user['user_id'], 'nickname' => $user['nickname'], 'order_sn' => $order['order_sn'], 'order_id' => $order['order_id'], 'goods_price' => $order['goods_price'], 'money' => $first_money, 'level' => 1, 'create_time' => time(), 'store_id' => $stoid, 'first_rate' => $first_rate, 'second_rate' => ($second_rate + $third_rate), 'third_rate' => 0, 'commission_json' => json_encode($jsondata), 'dis_moshi' => $pattern, 'user_type' => 1, 'pickup_id'=>$order['pickup_id'], ); if ($pattern == 1) { $data['first_rate'] = $first_money; $data['second_rate'] = $second_money + $thirdmoney; } M('rebate_log')->add($data); // 微信推送消息 if ($user['oauth'] == 'weixin') { $title = "您的一级会员刚下了一笔订单"; $this->post_msg($stoid,$erpid,$user['user_id'],$title,$order['order_sn'],$order['total_amount'],$first_money,date('Y年m月d日 H:i'),$remark); } } if(($second_money + $thirdmoney)>0) { $fuid2 = M('users')->where('store_id=' . $stoid . ' and user_id=' . $user['first_leader'])->find(); if($fuid2 && $fuid2['is_distribut']==1) { $data0 = array( 'user_id' => $user['first_leader'], 'buy_user_id' => $user['user_id'], 'nickname' => $user['nickname'], 'order_sn' => $order['order_sn'], 'order_id' => $order['order_id'], 'goods_price' => $order['goods_price'], 'money' => ($second_money + $thirdmoney), 'level' => 2, 'create_time' => time(), 'store_id' => $stoid, 'first_rate' => $first_rate, 'second_rate' => ($second_rate + $third_rate), 'third_rate' => 0, 'commission_json' => json_encode($jsondata), 'dis_moshi' => $pattern, 'user_type' => 1, 'pickup_id' => $order['pickup_id'], ); if ($pattern == 1) { $data0['first_rate'] = $first_money; $data0['second_rate'] = $second_money + $thirdmoney; } M('rebate_log')->add($data0); // 微信推送消息 if ($user['oauth'] == 'weixin') { $title = "您的二级会员刚下了一笔订单"; $this->post_msg($stoid, $erpid, $fuid2['user_id'], $title, $order['order_sn'], $order['total_amount'], ($second_money + $thirdmoney), date('Y年m月d日 H:i'), $remark); } } } if(($first_money + $second_money + $thirdmoney)>0) M('order_goods')->where('order_id',$order['order_id']) ->where('goods_id','in',$disgoods)->save(['is_distri'=>1]); } /*--有一级分享分人和二级分享人--*/ if(!empty($user['first_leader']) && !empty($user['second_leader'])){ if($first_money>0) { $data = array( 'user_id' => $user['user_id'], 'buy_user_id' => $user['user_id'], 'nickname' => $user['nickname'], 'order_sn' => $order['order_sn'], 'order_id' => $order['order_id'], 'goods_price' => $order['goods_price'], 'money' => $first_money, 'level' => 1, 'create_time' => time(), 'store_id' => $stoid, 'first_rate' => $first_rate, 'second_rate' => $second_rate, 'third_rate' => $third_rate, 'commission_json' => json_encode($jsondata), 'dis_moshi' => $pattern, 'user_type' => 1, 'pickup_id'=>$order['pickup_id'], ); if ($pattern == 1) { $data['first_rate'] = $first_money; $data['second_rate'] = $second_money; $data['third_rate'] = $thirdmoney; } M('rebate_log')->add($data); // 微信推送消息 if ($user['oauth'] == 'weixin') { $title = "您的一级会员刚下了一笔订单"; $this->post_msg($stoid,$erpid,$user['user_id'],$title,$order['order_sn'],$order['total_amount'],$first_money,date('Y年m月d日 H:i'),$remark); } } if($second_money>0){ $fuid2 = M('users')->where('store_id=' . $stoid . ' and user_id=' . $user['first_leader'])->find(); if($fuid2 && $fuid2['is_distribut']==1) { $data0 = array( 'user_id' => $user['first_leader'], 'buy_user_id' => $user['user_id'], 'nickname' => $user['nickname'], 'order_sn' => $order['order_sn'], 'order_id' => $order['order_id'], 'goods_price' => $order['goods_price'], 'money' => $second_money, 'level' => 2, 'create_time' => time(), 'store_id' => $stoid, 'first_rate' => $first_rate, 'second_rate' => $second_rate, 'third_rate' => $third_rate, 'commission_json' => json_encode($jsondata), 'dis_moshi' => $pattern, 'user_type' => 1, 'pickup_id' => $order['pickup_id'], ); if ($pattern == 1) { $data0['first_rate'] = $first_money; $data0['second_rate'] = $second_money; $data0['third_rate'] = $thirdmoney; } M('rebate_log')->add($data0); // 微信推送消息 if ($user['oauth'] == 'weixin') { $title = "您的二级会员刚下了一笔订单"; $this->post_msg($stoid, $erpid, $fuid2['user_id'], $title, $order['order_sn'], $order['total_amount'], $second_money, date('Y年m月d日 H:i'), $remark); } } } if($thirdmoney>0) { $fuid3 = M('users')->where('store_id=' . $stoid . ' and user_id=' . $user['second_leader'])->find(); if($fuid3 && $fuid3['is_distribut']==1) { $data1 = array( 'user_id' => $user['second_leader'], 'buy_user_id' => $user['user_id'], 'nickname' => $user['nickname'], 'order_sn' => $order['order_sn'], 'order_id' => $order['order_id'], 'goods_price' => $order['goods_price'], 'money' => $thirdmoney, 'level' => 3, 'create_time' => time(), 'store_id' => $stoid, 'first_rate' => $first_rate, 'second_rate' => $second_rate, 'third_rate' => $third_rate, 'commission_json' => json_encode($jsondata), 'dis_moshi' => $pattern, 'user_type' => 1, 'pickup_id' => $order['pickup_id'], ); if ($pattern == 1) { $data1['first_rate'] = $first_money; $data1['second_rate'] = $second_money; $data1['third_rate'] = $thirdmoney; } M('rebate_log')->add($data1); // 微信推送消息 if ($user['oauth'] == 'weixin') { $title = "您的三级会员刚下了一笔订单"; $this->post_msg($stoid, $erpid, $fuid3['user_id'], $title, $order['order_sn'], $order['total_amount'], $thirdmoney, date('Y年m月d日 H:i'), $remark); } } } if(($first_money + $second_money + $thirdmoney)>0) M('order_goods')->where('order_id',$order['order_id']) ->where('goods_id','in',$disgoods)->save(['is_distri'=>1]); } }else{ /*--有一级分享分人,没有二级分享人,没有三级分享人--*/ if(!empty($user['first_leader']) && empty($user['second_leader']) && empty($user['third_leader'])){ if(($first_money+$second_money+$thirdmoney) <= 0) return false; $fuid0 = M('users')->where('store_id=' . $stoid . ' and user_id=' . $user['first_leader'])->find(); if($fuid0 && $fuid0['is_distribut']==1) { $data = array( 'user_id' => $user['first_leader'], 'buy_user_id' => $user['user_id'], 'nickname' => $user['nickname'], 'order_sn' => $order['order_sn'], 'order_id' => $order['order_id'], 'goods_price' => $order['goods_price'], 'money' => ($first_money + $second_money + $thirdmoney), 'level' => 1, 'create_time' => time(), 'store_id' => $stoid, 'first_rate' => ($first_rate + $second_rate + $third_rate), 'second_rate' => 0, 'third_rate' => 0, 'commission_json' => json_encode($jsondata), 'dis_moshi' => $pattern, 'pickup_id' => $order['pickup_id'], ); if ($pattern == 1) { $data1['first_rate'] = ($first_money + $second_money + $thirdmoney); } M('rebate_log')->add($data); // 微信推送消息 if ($user['oauth'] == 'weixin') { $title = "您推荐的好友刚下了一笔订单"; $this->post_msg($stoid, $erpid, $fuid0['user_id'], $title, $order['order_sn'], $order['total_amount'], ($first_money + $second_money + $thirdmoney), date('Y年m月d日 H:i'), $remark); } M('order_goods')->where('order_id', $order['order_id']) ->where('goods_id', 'in', $disgoods)->save(['is_distri' => 1]); } } /*--有一级分享分人和二级分享人--*/ if(!empty($user['first_leader']) && !empty($user['second_leader']) && empty($user['third_leader']) ){ if($first_money>0) { $fuid0 = M('users')->where('store_id=' . $stoid . ' and user_id=' . $user['first_leader'])->find(); if($fuid0 && $fuid0['is_distribut']==1) { $data = array( 'user_id' => $user['first_leader'], 'buy_user_id' => $user['user_id'], 'nickname' => $user['nickname'], 'order_sn' => $order['order_sn'], 'order_id' => $order['order_id'], 'goods_price' => $order['goods_price'], 'money' => $first_money, 'level' => 1, 'create_time' => time(), 'store_id' => $stoid, 'first_rate' => $first_rate, 'second_rate' => ($second_rate + $third_rate), 'third_rate' => 0, 'commission_json' => json_encode($jsondata), 'dis_moshi' => $pattern, 'pickup_id' => $order['pickup_id'], ); if ($pattern == 1) { $data['first_rate'] = ($first_money); $data['first_rate'] = ($second_money + $thirdmoney); } M('rebate_log')->add($data); // 微信推送消息 if ($user['oauth'] == 'weixin') { $title = "您推荐的好友刚下了一笔订单"; $this->post_msg($stoid, $erpid, $fuid0['user_id'], $title, $order['order_sn'], $order['total_amount'], $first_money, date('Y年m月d日 H:i'), $remark); } } } mlog("第二级金额".($second_money+$thirdmoney),"rebate_log/".$stoid); if(($second_money+$thirdmoney)>0) { $fuid1 = M('users')->where('store_id=' . $stoid . ' and user_id=' . $user['second_leader'])->find(); mlog("第二级会员".(json_encode($fuid1)),"rebate_log/".$stoid); if($fuid1 && $fuid1['is_distribut']==1) { $data0 = array( 'user_id' => $user['second_leader'], 'buy_user_id' => $user['user_id'], 'nickname' => $user['nickname'], 'order_sn' => $order['order_sn'], 'order_id' => $order['order_id'], 'goods_price' => $order['goods_price'], 'money' => ($second_money + $thirdmoney), 'level' => 2, 'create_time' => time(), 'store_id' => $stoid, 'first_rate' => $first_rate, 'second_rate' => ($second_rate + $third_rate), 'third_rate' => 0, 'commission_json' => json_encode($jsondata), 'dis_moshi' => $pattern, 'pickup_id' => $order['pickup_id'], ); if ($pattern == 1) { $data0['first_rate'] = ($first_money); $data0['first_rate'] = ($second_money + $thirdmoney); } M('rebate_log')->add($data0); // 微信推送消息 if ($user['oauth'] == 'weixin') { $title = "您推荐的二级好友刚下了一笔订单"; $this->post_msg($stoid, $erpid, $fuid1['user_id'], $title, $order['order_sn'], $order['total_amount'], ($second_money + $thirdmoney), date('Y年m月d日 H:i'), $remark); } } } if(($first_money + $second_money + $thirdmoney)>0) M('order_goods')->where('order_id',$order['order_id']) ->where('goods_id','in',$disgoods)->save(['is_distri'=>1]); } /*--有一级分享分人和二级分享人,三级分享人--*/ if(!empty($user['first_leader']) && !empty($user['second_leader']) && !empty($user['third_leader']) ){ if($first_money>0) { $fuid0 = M('users')->where('store_id=' . $stoid . ' and user_id=' . $user['first_leader'])->find(); if($fuid0 && $fuid0['is_distribut']==1) { $data = array( 'user_id' => $user['first_leader'], 'buy_user_id' => $user['user_id'], 'nickname' => $user['nickname'], 'order_sn' => $order['order_sn'], 'order_id' => $order['order_id'], 'goods_price' => $order['goods_price'], 'money' => $first_money, 'level' => 1, 'create_time' => time(), 'store_id' => $stoid, 'first_rate' => $first_rate, 'second_rate' => $second_rate, 'third_rate' => $third_rate, 'commission_json' => json_encode($jsondata), 'dis_moshi' => $pattern, 'pickup_id' => $order['pickup_id'], ); if ($pattern == 1) { $data['first_rate'] = $first_money; $data['second_rate'] = $second_money; $data['third_rate'] = $thirdmoney; } M('rebate_log')->add($data); // 微信推送消息 if ($user['oauth'] == 'weixin') { $title = "您推荐的好友刚下了一笔订单"; $this->post_msg($stoid, $erpid, $fuid0['user_id'], $title, $order['order_sn'], $order['total_amount'], $first_money, date('Y年m月d日 H:i'), $remark); } } } if($second_money>0) { $fuid1 = M('users')->where('store_id=' . $stoid . ' and user_id=' . $user['second_leader'])->find(); if($fuid1 && $fuid1['is_distribut']==1) { $data0 = array( 'user_id' => $user['second_leader'], 'buy_user_id' => $user['user_id'], 'nickname' => $user['nickname'], 'order_sn' => $order['order_sn'], 'order_id' => $order['order_id'], 'goods_price' => $order['goods_price'], 'money' => $second_money, 'level' => 2, 'create_time' => time(), 'store_id' => $stoid, 'first_rate' => $first_rate, 'second_rate' => $second_rate, 'third_rate' => $third_rate, 'commission_json' => json_encode($jsondata), 'dis_moshi' => $pattern, 'pickup_id' => $order['pickup_id'], ); if ($pattern == 1) { $data0['first_rate'] = $first_money; $data0['second_rate'] = $second_money; $data0['third_rate'] = $thirdmoney; } M('rebate_log')->add($data0); // 微信推送消息 if ($user['oauth'] == 'weixin') { $title = "您推荐的二级好友刚下了一笔订单"; $this->post_msg($stoid, $erpid, $fuid1['user_id'], $title, $order['order_sn'], $order['total_amount'], $second_money, date('Y年m月d日 H:i'), $remark); } } } if($thirdmoney>0) { $fuid2 = M('users')->where('store_id=' . $stoid . ' and user_id=' . $user['third_leader'])->find(); if($fuid2 && $fuid2['is_distribut']==1) { $data1 = array( 'user_id' => $user['third_leader'], 'buy_user_id' => $user['user_id'], 'nickname' => $user['nickname'], 'order_sn' => $order['order_sn'], 'order_id' => $order['order_id'], 'goods_price' => $order['goods_price'], 'money' => $thirdmoney, 'level' => 3, 'create_time' => time(), 'store_id' => $stoid, 'first_rate' => $first_rate, 'second_rate' => $second_rate, 'third_rate' => $third_rate, 'commission_json' => json_encode($jsondata), 'dis_moshi' => $pattern, 'pickup_id' => $order['pickup_id'], ); if ($pattern == 1) { $data1['first_rate'] = $first_money; $data1['second_rate'] = $second_money; $data1['third_rate'] = $thirdmoney; } M('rebate_log')->add($data1); // 微信推送消息 if ($user['oauth'] == 'weixin') { $title = "您推荐的三级好友刚下了一笔订单"; $this->post_msg($stoid, $erpid, $fuid2['user_id'], $title, $order['order_sn'], $order['total_amount'], $thirdmoney, date('Y年m月d日 H:i'), $remark); } } } if(($first_money + $second_money + $thirdmoney)>0) M('order_goods')->where('order_id',$order['order_id']) ->where('goods_id','in',$disgoods)->save(['is_distri'=>1]); } } M('order')->where("order_id", $order['order_id'])->save(array("is_distribut"=>1)); //修改订单为已经分成 } /** * 自动分成 符合条件的 分成记录 */ function auto_confirm($stoid){ // mlog("执行分成","autoconfirm"); $switch = tpCache('distribut.switch',$stoid); if($switch == 0) return false; $today_time = time(); $distribut_date = tpCache('distribut.date',$stoid); $distribut_time = $distribut_date * (60 * 60 * 24); // 计算天数 时间戳 $rebate_log_arr = M('rebate_log')->where(" store_id=".$stoid." and status = 2 and ($today_time - confirm) > $distribut_time")->select(); foreach ($rebate_log_arr as $key => $val) { accountLog($val['user_id'], $val['money'], 0,"订单:{$val['order_sn']}分佣", $val['money'],$stoid,null,$val['order_sn'],1); $val['status'] = 3; $val['store_id']=$stoid; $val['confirm_time'] = $today_time; $val['remark'] = $val['remark']."满{$distribut_date}天,程序自动分成."; M("rebate_log")->where("id", $val['id'])->save($val); } } //推送消息 public function post_msg($stoid,$erpid,$user_id,$title,$order_sn,$order_money,$fc_num,$time,$remark){ $colorlist = urlencode("#FF0000|#173177|#173177|#173177|#173177|#FF0000|#FF0000|#ca003a"); //$url = curHostURL() . "/home/api/poswxcode?typeid=1021&Acc=" . $erpid . "&ErpVipid=" . $user_id . "&title=" . $title . "&key1=" . $order_sn . "&key2=" . number_format($order_money,2) . "元&key3=" . number_format($fc_num,2) . "元&key4=" . $time . "&remark=" . $remark.'&colorlist='. $colorlist;//. "&backurl=" . curHostURL() . "/Mobile/User/order_list/stoid/" . $stoid; //$resp=httpRequest($url, "GET"); $data['typeid']='1021';//微信消息类型 1001至1010 $data['title']=$title; $data['remark']=$remark; $data['key1']=$order_sn; $data['key2']=number_format($order_money,2) . "元"; $data['key3']=number_format($fc_num,2) . "元"; $data['key4']=$time; $data['colorlist']=$colorlist; $data['backurl']=curHostURL() . "/Mobile/User/order_list/stoid/" . $stoid; $data['Acc']=$erpid; $data['user_id']=$user_id; $api = new \app\home\controller\Api(); //$resp=httpRequest($url, "GET"); $resp=$api->poswxcode2($data); return $resp; } }