我正在构建一个codeigniter的委托/推荐应用程序,但不确定如何编码一个特定的功能。
在我的数据库中,我有一个名为users的表,其结构如下(简化版本)
user_id user_name user_referredby
1 Billy Bob null
2 Janes Smith 1
3 Test Person 2
4 Another Person 3
5 Peter Green 4
6 Jess White 5
7 Sally Smith 6
8 John Pink 3当管理员用户将用户添加到系统时,user_referredby为空。其他人是由系统中的其他人推荐的人。
我需要的新功能有两个方面的作用:
1)管理员输入合同号,选择(中奖)用户并指定佣金总额。
即CN123、7、1000
2)然后根据推荐系统为每个用户生成佣金-最多7个推荐级别。每个级别的佣金都是按百分比计算的:
级别1- 10%,级别2- 7%,级别3- 6%,级别4- 5%,级别5- 4%,级别6- 3%,级别7- 2%
例如:获胜的用户是user_id 7。
User id 7 gets (10% of 1000) = 100
User id 7 has been referred by userid 6 so
User id 6 gets (7% of 1000) = 70
User id 6 has been referred by userid 5 so
User id 5 gets (6% of 1000) = 60
and so on until it hits a maximum number of 7 referrals.如果引用的数量小于7,则仅从级别1开始,并在达到user_referredby null时结束。
例如:
如果获奖用户为user_id 3
User id 3 gets (10% of 1000) = 100
User id 3 has been referred by userid 2 so
User id 2 gets (7% of 1000) = 70
User id 2 has been referred by userid 1 so
User id 1 gets (6% of 1000) = 60
User id 1 has been referred by userid NULL so
PROCESS ENDs为了捕获所有的佣金数据,我预见我们需要一个名为佣金的表。我想应该是这样的:
com_id contract_id com_date user_id com_value com_total
1 CN123 2014-06-12 7 100 1000
2 CN123 2014-06-12 6 70 1000
3 CN123 2014-06-12 5 60 1000
4 CN123 2014-06-12 Admin 770 1000这样,我就可以在用户配置文件页面中显示收到的合同佣金。
我还想捕获剩余佣金,并将其分配给管理员用户,如com_id 4所示。这是通过Com Total -Com值(1000 -(100+70+60))计算的
所以..。
这是我的问题。
1)如何获得基于用户表的所有推荐ids 2)计算每个级别的佣金,基于通信总数和推荐级别3)保存每个数据库我的每个用户都可以看到他们的佣金,包括管理员的剩余。
我知道这是一项艰巨的任务,所以如果有人有空,我很乐意支付一些帮助。
谢谢。
更新:
我试过这样做,但不起作用:
public function add($user_id, $contract, $level)
{
$this->form_validation->set_rules("contract", "Contract Number", "required");
$this->form_validation->set_rules("property", "Property", "required");
$this->form_validation->set_rules("client", "Client", "required");
$this->form_validation->set_rules("commission", "Commission", "required");
$this->form_validation->set_error_delimiters('<span>', '~~</span>');
if($this->form_validation->run() == FALSE){
$data["page_title"] = $this->lang->line("page_title_agents");
$this->layout->view('administration/commissions/add', $data);
}
else
{
$levels = array(10, 7, 6, 5, 4, 3, 2);
$id = $this->input->post('user_id');
$user_referral = $this->commissions_model->referred_clients($id);
$contract_id = $this->input->post('contract');
$contract_date = date("Y-m-d");
$contract_property = $this->input->post('property');
$contract_total = $this->input->post('commission');
$com_value = $levels[$level] * $contract_total;
$sql = "INSERT INTO commissions (com_contract, com_property, com_date, com_client, com_commission, com_total_commission)
VALUES ('$contract_id', '$contract_property', '$contract_date', '$user_referral', '$com_value', )";
add($user_referral, $contract, $level+1);
$this->session->set_flashdata('message', 'Commission has been is successfully created!');
redirect('administration/commission', 'refresh');
}
}有什么想法吗?
发布于 2014-07-01 07:47:38
递归函数应该做到这一点:
$levels = array(10, 7, 6, 5, 4, 3, 2);
function get_referral($user_id, $contract, $level) {
$user_referral = "SELECT user_referredby FROM users WHERE user_id = $user_id";
$com_value = $levels[$level] * $contract[total];
$sql = "INSERT INTO commission ( contract_id, com_date, user_id, com_value, com_total )
VALUES ( $contract['id'], $contract['date'], $user_referral, $com_value, $contract['total'] )";
get_referral($user_referral, $contract, $level+1);
}注意:这不是有效的代码!请使用您正在使用的系统替换此处的SQL字符串。
https://stackoverflow.com/questions/24500446
复制相似问题