【发布时间】:2014-08-21 10:24:42
【问题描述】:
我正在使用 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 空值时结束。
例如:
如果获胜用户是 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 - Comm values (1000 -(100+70+60))
所以...
这是我的问题。
1) 如何根据用户表获取所有的推荐ID 2)根据comm总和推荐级别计算每个级别的佣金 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');
}
}
有什么想法吗?
【问题讨论】:
标签: php mysql sql codeigniter