I want to subtract the Balance field of Reseller. Now Reseller can allot his balance to his users. So when a reseller adds balance to his users i want to deduct his balance by 5 EUROS(as the min and max balance he can allot is only 5). And There would be many reseller and many users. Each user is mapped by a unique of Reseller. So whenever a reseller add balance to his users only his balance should be deducted. It quite complicated, i think i must update the reseller balance field whenever the user is allotted balance. Please help me with my code.
The Reseller Controller :
public function edit ($id = NULL)
{
$usertype=$this->session->userdata('usertype');
if($usertype ==="admin")
{
// Fetch a user or set a new one
if ($id) {
$this->data['user'] = $this->reseller_m->get($id);
count($this->data['user']) || $this->data['errors'][] = 'User could not be found';
}
else
{
$this->data['user'] = $this->reseller_m->get_new();
}
// Set up the form
$rules = $this->reseller_m->rules_admin;
$id || $rules['password']['rules'] .= '|required';
$this->form_validation->set_rules($rules);
// Process the form
if ($this->form_validation->run() == TRUE) {
$data = $this->reseller_m->array_from_post(array('sip_username','sip_password','key','allocation_block','name','email','password','phone','balance','user_num','address','country','country_code','created','modified','status'));
$data['password'] = $this->reseller_m->hash($data['password']);
$key=$this->reseller_m->save($data, $id);
//here we get the last inserted record id in $last_id//
$last_id = $this->db->insert_id();
//The logic to create key to uniquely identify a reseller.
$values=array($this->input->post('name'),$this->input->post('country_code'),$last_id,$this->input->post('allocation_block'),$this->input->post('user_num'));
$key=implode('-',$values);
$this->db->where('id',$last_id);
$this->db->update('reseller',array('key'=>$key));
//Loop To Creates Users Dynamically as user_num value.
$this->reseller_m->get($id);
if($id === NULl)
{
for($i=1; $i<=$data['user_num'];$i++)
{
$userdata=array('key'=>$key);
$this->db->insert('users',$userdata);
}
}
redirect('admin/reseller');
}
// Load the view
$this->data['subview'] = 'admin/reseller/edit';
$this->load->view('admin/_layout_main', $this->data);
}
else
{
$this->load->view('permission');
}
}
The User Controller :
public function edit ($id = NULL)
{
$usertype=$this->session->userdata('usertype');
if($usertype ==="admin")
{
// Fetch a user or set a new one
if ($id)
{
$this->data['user'] = $this->user_m->get($id);
count($this->data['user']) || $this->data['errors'][] = 'User could not be found';
}
else
{
$this->data['user'] = $this->user_m->get_new();
}
// Set up the form
$rules = $this->user_m->rules_admin;
$id || $rules['password']['rules'] .= '|required';
$this->form_validation->set_rules($rules);
// Process the form
if ($this->form_validation->run() == TRUE) {
$data = $this->user_m->array_from_post(array('sip_id','sip_pass','name','email', 'password','phone','status','created','balance'));
$data['password'] = $this->user_m->hash($data['password']);
$this->user_m->save($data, $id);
redirect('admin/user');
}
// Load the view
$this->data['subview'] = 'admin/user/edit';
$this->load->view('admin/_layout_main', $this->data);
}
else
{
$this->load->view('permission');
}
}