I have function that will insert an amount to the users up to the last rank based on parent_id
. I can get the parent_id
up to the last rank, its fine and I'm using an iteration
or something like recursion
. But I can't add the amount if the user rank is not present.
The expected result of the function is to insert an amount to each User Rank
, but if the iteration/recursion didn't find a user with the rank
the amount to be inserted to him will be added on the next user rank
.
The function will trigger once a user bought a product
, so whoever user buy a product it will get his parent_id
and insert the amount
assigned to that rank.
I have a list of rank in order
who will receive the amount.
Basic - 50
Junior - 100
Premium - 150
Advanced - 200
Senior - 250
Database:
+------+------------+-------------+------------+
| id | username | parent_id | rank |
+------+------------+-------------+------------+
| 1 | john | NULL | Senior |
| 2 | jane | 1 | Advanced |
| 3 | josh | 2 | Premium |
| 4 | joey | 3 | Junior |
| 5 | jade | 4 | Basic |
------------------------------------------------
For example:
Jade
bought an item, once the order is success I will call the function to insert the earnings
.
In this scenario Joey, Josh, Jane and John
will received their earnings based on the corresponding amount of their rank
.
This is working fine in this scenario because all the ranks are present, but my problem is to add the amount
set to rank
if the user is not in the iteration
,
For example, Joey
with the rank - Junior
is not in the iteration, the amount designated to him which is 100
will be added on the next rank
which is the Premium
, and proceed the earnings insertion on the next ranks, but prevent adding the amount
to them, because the amount should only be added
to the next rank if the user with that rank is not present in the iteration.
// successfully bought a product and saved to database
$user_id = 5; // jade
$parent_id = 4;
// call the function to insert the earnings
self::insertEarnings($user_id,$parent_id);
This is the function that will insert the earnings:
private function insertEarnings($user_id,$parent_id) {
if ($parent_id > 0) {
$user_parent = $parent_id;
$has_parent = true;
// start iteration
while($has_parent == true){
$account = User::where('id',$user_parent)->first();
$amount = 0;
if ($account->rank == "Junior" ) {
$amount = 100;
} elseif ($account->rank == "Premium") {
$amount = 150;
} elseif ($account->rank == "Advanced") {
$amount = 200;
} elseif ($account->rank == "Senior") {
$amount = 250;
// set to false to stop the iteration
$has_parent = false;
}
$earnings = new Earnings;
$earnings->user_id = $account->id;
$earnings->amount = $amount;
$earnings->save();
$next_parent = User::where('id',$user_parent)->first();
$user_parent = $next_parent->parent_id;
if($user_parent == 0){
$has_parent = false;
}
}
}
}
The earnings was inserted to Joey, Josh, Jane and John because they are all present in the iteration, the real question is, how to insert the amount assigned to the rank if he is not present in the iteration.
EDIT:
Please someone help me? I can't really make a solution for this code. Adding the amount to the next user rank if the rank is not found in the iteration/loop.
Jade order a product, let's assume it is successful. Now I will trigger the insertEarnings
function. I will get his parent_id
which is Joey
,
Joey
will get an amount of 100
, also Josh, Jane and John
they will get an amount according to their rank.
The main problem is what if someone is NOT
in the iteration/loop, the amount for that rank
whatever that rank is will be added to the next rank.
from laravel iteration adding value to the next user if the user rank does not exist
No comments:
Post a Comment