我在 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) 管理员输入契约(Contract)编号,选择(获胜)用户并指定佣金总额。
即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
这样,我可以在用户个人资料页面中显示收到的契约(Contract)佣金。
我还想获取剩余的佣金并将其分配给管理员用户,如 com_id 4 所示。计算公式为 Com Total - Comm values (1000 -(100+70+60))
所以...
这是我的问题。
1) 如何根据用户表获取所有推荐ID 2)根据comm total和referral level计算每个level的佣金 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');
}
}
有什么想法吗?
最佳答案
递归函数应该这样做:
$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 字符串。
关于php - Codeigniter - 逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24500446/