🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
### 通过时尚顾问ID和日期计算业绩和提成 **位置:** Common\Lib\UserLib; **参数:** * @param $uid 时尚顾问的Id * @param $month 月份时间 **调用:** * $user = new UserLib(); * $result = $user->createCommission($uid, $month); **完整代码:** ~~~ /* * 计算时尚顾问业绩和提成 * @param $uid 时尚顾问Id * @param $month 月份时间 * * @return bool 计算是否成功 */ function createCommission ($uid, $month) { //1、获取参数(计算时尚顾问销售业绩) $days = date("t", strtotime($month)); //计算出某年某月的天数 $map['list.create_time'] = array('between', array( strtotime($month), strtotime(trim($month) . '-' . $days . ' 23:59:59'))); M()->startTrans(); //2、基于时尚顾问查询当月销售业绩 $map_results['sales_time'] = array('between', array( strtotime($month), strtotime(trim($month) . '-' . $days . ' 23:59:59'))); $map_results['is_delete'] = 0; $map_results['sales_uid'] = $uid; $results_sale = M('results_sales') ->field('id,sales_uid') ->where($map_results) ->select(); if (count($results_sale) < 1) { return array('success' => false, 'code' => 201, 'msg' => '该月没有录入时尚顾问业绩目标!'); } //3、统计时尚顾问销售业绩 foreach ($results_sale as $key => $value) { // 增加一个判断结算方式是否伪删除的条件 $mapB['a.is_delete'] = array('eq', 0); // 3是礼券的结算方式id,礼券是不能算业绩的 // $mapB['a.pay_id'] = array('neq',3); $mapB['a.create_time'] = array('between', array( strtotime($month), strtotime(trim($month) . '-' . $days . ' 23:59:59'))); $mapB['a.sales_uid'] = $value['sales_uid']; // 计算财务审核后得到每个小票单的业绩金额 $sales = M('order_sales as a') ->field('id,sales_money') ->where($mapB) ->select(); $sales_total = 0; foreach ($sales as $k => $v) { $sales_total += $v['sales_money']; } $save_sale = M('results_sales') ->where(array('id' => $value['id'])) ->save(array('sales_total' => $sales_total)); if ($save_sale === false) { M()->rollback(); return array('success' => false, 'code' => 201, 'msg' => '销售用户销售总金额保存失败'); } } //3、根据规则计算时尚顾问业绩提成 $where['list.create_time'] = array('between', array( strtotime($month), strtotime(trim($month) . '-' . $days . ' 23:59:59'))); $results_sale = M('results_sales') ->where($map_results) ->select(); foreach ($results_sale as $key => $value) { $sales_describe = array(); $money_a = 0; //该类销售金额 $money_b = 0; //该类销售业绩 //2.1、循环计算时尚顾问完成率 $results_rate = $value['sales_total'] / $value['sales_target']; //2.2、根据算的完成率查询适合的业绩计算规则 if ($results_rate >= 0.5 && $results_rate < 0.75) { $results_rates = 0.5; } elseif ($results_rate >= 0.75 && $results_rate < 1) { $results_rates = 0.75; } elseif ($results_rate >= 1 && $results_rate < 1.25) { $results_rates = 1; } elseif ($results_rate > 1.25) { $results_rates = 1.25; } array_push($sales_describe, '个人业绩完成率为:' . $this->getPercent($results_rate)); $whereA['start_time'] = array('elt', strtotime(trim($month) . '-01')); $whereA['end_time'] = array('egt', strtotime(trim($month) . '-' . $days)); $whereA['is_delete'] = 0; $rule_list = M('results_rules') ->where($whereA) ->select(); foreach ($rule_list as $k => $v) { $brang_name = $v['rule_name']; //针对性的过滤 if (($results_rate < 0.5) && !in_array($brang_name, array('自由品牌', '设计师品牌'))) { continue; } $brand_sum = 0; //记录每条规则业绩 $t_money = 0; //记录每条规则提成 $whereB = array(); $whereC = array(); $brands = json_decode($v['brand_id']); $cat_ids = json_decode($v['cat_id']); $percent = object_to_array(json_decode($v['percent'])); //针对性取提点 if (in_array($brang_name, array('自由品牌', '设计师品牌'))) { $abs = '0.5'; $bai = $percent[$abs]; } else { $abd = strval($results_rates); $bai = $percent[$abd]; } if ($v['is_in'] == 1) { $is_in = 'in'; } elseif ($v['is_in'] == 2) { $is_in = 'not in'; } //有品牌的逻辑 if (count($brands) > 0) { foreach ($brands as $kk => $vv) { $a = ''; $string = ''; if (!empty($vv)) { $whereB['a.brand_id'] = $vv; $brand_name = get_brand_en($vv); $a .= $brand_name; } if (!empty($cat_ids)) { $cat_arr = array(); $is_ins = $v['is_in'] == 1 ? '包含' : '不包含'; $a .= $is_ins; foreach ($cat_ids as $kkk => $vvv) { if (!empty($vvv)) { $cat_info = M('goods_category') ->where(array('id' => $vvv)) ->find(); $whereB['e.level'] = $cat_info['level']; array_push($cat_arr, $vvv); $mall_obj = new MallLib(); $cat_name = $mall_obj->getChFullPath($vvv); $a .= $cat_name; } } $whereB['e.cat_id'] = array($is_in, $cat_arr); } $whereB['a.is_delete'] = 0; $whereB['e.is_delete'] = 0; $spu_ids = M('goods_spu as a') ->field('a.id') ->join('coscia_goods_spu_category as e on e.spu_id = a.id', 'left') ->where($whereB) ->select(); if (empty($spu_ids)) { continue; } if (!empty($spu_ids)) { $spu_where = array(); foreach ($spu_ids as $kk => $vv) { array_push($spu_where, $vv['id']); } $whereC['a.spu_id'] = array('in', $spu_where); } //查询对应品牌和品类的销售业绩金额 $whereC['a.create_time'] = array('between', array( strtotime($month), strtotime(trim($month) . '-' . $days . ' 23:59:59'))); $whereC['b.sale_uid'] = $value['sales_uid']; $whereC['b.order_status'] = array('not in', array(1, 6, 15, 16, 7)); $data = M('order_sku as a') ->field('a.sku_id,a.goods_num,a.subtotal,a.create_time') ->join('coscia_order_list as b on a.order_id = b.id', 'left') ->where($whereC) ->select(); //如果该分类没有销售记录就过掉 if (empty($data)) { continue; } else { $string .= $a; //该业绩规则销售金额汇总 $money_sum = 0; foreach ($data as $kkk => $vvv) { $money_sum += $vvv['subtotal']; } $money_a += $money_sum; $brand_sum += $money_sum; //计算该业绩规则下业绩提成 $money_result = round($money_sum * $bai, 2); $money_b += $money_result; $t_money += $money_result; $string .= '销售业绩' . $money_sum . ',销售提成:' . $money_sum . '*' . ($bai * 100) . '%=' . $money_result; array_push($sales_describe, $string); } } } else { if (!empty($cat_ids)) { foreach ($cat_ids as $kkk => $vvv) { $a = ''; $string = ''; $cat_arr = array(); $is_ins = $v['is_in'] == 1 ? '包含' : '不包含'; $a .= $is_ins; if (!empty($vvv)) { $cat_info = M('goods_category') ->where(array('id' => $vvv)) ->find(); $whereB['e.level'] = $cat_info['level']; array_push($cat_arr, $vvv); $whereB['e.cat_id'] = array($is_in, $cat_arr); $mall_obj = new MallLib(); $cat_name = $mall_obj->getChFullPath($vvv); $a .= $cat_name; } $whereB['a.is_delete'] = 0; $whereB['e.is_delete'] = 0; $spu_ids = M('goods_spu as a') ->field('a.id') ->join('coscia_goods_spu_category as e on e.spu_id = a.id', 'left') ->where($whereB) ->select(); if (empty($spu_ids)) { continue; } if (!empty($spu_ids)) { $spu_where = array(); foreach ($spu_ids as $kk => $vv) { array_push($spu_where, $vv['id']); } $whereC['a.spu_id'] = array('in', $spu_where); } //查询对应品牌和品类的销售业绩金额 $whereC['a.create_time'] = array('between', array( strtotime($month), strtotime(trim($month) . '-' . $days . ' 23:59:59'))); $whereC['b.sale_uid'] = $value['sales_uid']; $whereC['b.order_status'] = array('not in', array(1, 6, 15, 16, 7)); $data = M('order_sku as a') ->field('a.sku_id,a.goods_num,a.subtotal,a.create_time') ->join('coscia_order_list as b on a.order_id = b.id', 'left') ->where($whereC) ->select(); //如果该分类没有销售记录就过掉 if (empty($data)) { continue; } else { $string .= $a; //该业绩规则销售金额汇总 $money_sum = 0; foreach ($data as $kkk => $vvv) { $money_sum += $vvv['subtotal']; } $money_a += $money_sum; $brand_sum += $money_sum; //计算该业绩规则下业绩提成 $money_result = round($money_sum * $bai, 2); $money_b += $money_result; $t_money += $money_result; $string .= '销售业绩' . $money_sum . ',销售提成:' . $money_sum . '*' . ($bai * 100) . '%=' . $money_result; array_push($sales_describe, $string); } } } } if ($brand_sum != 0) { array_push($sales_describe, $brang_name . '总销售业绩:' . $brand_sum); array_push($sales_describe, $brang_name . '总销售提成:' . $t_money); array_push($sales_describe, ''); } } //把时尚顾问销售业绩信息存到销售业绩信息表 $save_sales = M('results_sales') ->where(array('id' => $value['id'])) ->save(array('sales_amount' => $money_b, 'sales_describe' => json_encode($sales_describe))); if ($save_sales === false) { M()->rollback(); return array('success' => false, 'code' => 201, 'msg' => get_user_name($value['sales_uid'], 1) . '的业绩信息保存失败'); } } M()->commit(); return array('success' => true, 'code' => 200, 'msg' => '数据生成成功!'); } ~~~