逻辑
我在codeigniter中构建了佣金/推荐应用程序,但不确定如何编写特定功能。
在我的数据库中,我有一个叫做用户的表,就像这样(简化版)
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 - Comm值(1000 - (100 + 70 + 60))
所以...
这是我的问题。
1)如何根据用户表获取所有推荐ID 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');
}
}
任何想法?
递归函数应该这样做:
$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字符串的系统。
链接地址: http://www.djcxy.com/p/93947.html上一篇: Logic
下一篇: Risks and benefits of a modified closure table for hierarchical data