Codeigniter Unilevel MLM earning Distribution - php

I am here to have some help from you.
I am making a Unilevel MLM using Codeigniter
and now I can sucessfully add new member
But the problem is I need to distribute the earnings to other level
after a new member is successfully Added
See pic below:
Distribution of earnings
I need to distribute like the image above.
I hope you can help me with this guys.

Okay, I have a solution for you. The process i used is based on my understanding of the question.
So this is it, first i checked for a registration post, if a post request is made, i use the referral id from the post to fetch the number of registrations tied to that referral id that has not been given awarded the 100 earning. If the count of the result of this query is equal to 4, i loop through all of them and give them the earning of 100 and update their paid status to reflect that they have been paid then i insert the record, else i just insert the record.
So too much text, lets see the code
//this is the controller code
//first check for post of registration
if($_POST){
//kindly do your form validation here
$register = array(
"name" => $this->input->post('name'),
"refid" => $this->input->post('refID')
);
//during post, get the referral id from the post
$refID = $this->input->post('refID');
//before registering, use referral id to get the referred that have not been given earnings yet
$thereffered = $this->referral_m->getReferred($refID);
//check for the number of registration
if(count($thereffered) == 4){
//then get their ids and give them their earnings and update them to paid
foreach($thereffered as $referred){
$earnings = array(
"userID" => $referred->id,
"amount" => 100
);
$paid = array(
"paid" => 1
);
//give earning
$this->referral_m->giveEarning($earnings); //this adds the user id to earning table and give it an amount of 100
$this->referral_m->updateUser($paid, $referred->id); //this updates the user with the paid status
}
//then proceed to register the new record
$this->referral_m->register($register);
}else{
//register the new record
$this->referral_m->register($register);
}
//redirect after registration
redirect();
}else{
//load view here
}
This is how the model looks like
function getReferred($refID){
return $this->db->get_where('referral', array("refid" => $refID, "paid" => '0'))->result();
}
function giveEarning($record){
$this->db->insert('earnings', $record);
}
function register($array){
$this->db->insert('referral', $array);
}
function updateUser($array, $id){
$this->db->where('id', $id);
$this->db->update('referral', $array);
}
From the model, you would discover that i created 2 database tables, I assume you already have those tables created, just use the logic to update your code. If you find any difficulty, kindly comment lets sort it out

Related

Verification of payments and ceiling of trainings in Laravel

My goal is that when I encode a student for a payment. He (student) can follow 2 trainings per payment. (this is the ceiling - 1 payment = 2 trainings)
In my form Payment, I encode a student for example Menier.
The student is entitled to two trainings, (this is the ceiling)
In my form Training: I encode 2 trainings for the student Menier.
My first question: how can I block the number of trainings by two?
(so, if I encode another training, it must block!)
My second queston, if I encode 2 payments for the same student. The student is entitled to 4 trainings. How to create this type of algorithm?
Here is my code for now, I know it's not a lot...
Edit 05-10-2019 - Controller Training
public function store(Request $request)
{
$request->validate([
'date_seance' => 'required',
'hour_start' => 'required',
'hour_end' => 'required',
'fk_motorbike' => 'required',
'fk_former' => 'required',
'fk_student' => 'required',
'fk_typeseance' => 'required'
]);
$date_seance = $request->get('date_seance');
$hour_start = $request->get('hour_start');
$hour_end = $request->get('hour_end');
$fk_motorbike = $request->get('fk_motorbike');
$fk_student = $request->get('fk_student');
$fk_former = $request->get('fk_former');
$fk_typeseance = $request->get('fk_typeseance');
$payments = Payment::where('fk_student', $request->get('fk_student'))->first();
if(!isset($payments)){
return redirect()->route('trainings.index')
->with('error', 'No payment, no training! ');
}
$thisStudentsTrainings = Training::where('fk_student', $fk_student)->get();
if(count($thisStudentsTrainings) >= 2){
return redirect()->route('trainings.index')
->with('error', 'The ceiling is 2 trainings! ');
}
$thisStudentsPayments = Payment::where('fk_student', $request->get('fk_student'))->get();
if( (count($thisStudentsPayments) * 2) < count($thisStudentsTrainings) ) {
return redirect()->route('trainings.index')
->with('error', 'test!');
}
else{
Training::create($request->all());
return redirect()->route('trainings.index')
->with('success', 'Add');
}
}
Do you have an idea of how I could solve my problems, I am still a beginner in laravel.
For Watercayman
Thank you for your time.
This is not too bad to do. Since the payment is not directly associated with a specific training (ie you have a credit system), you can do this pretty easily with a couple of queries.
My first question: how can I block the number of trainings by two?
Start with the basics and find the number of trainings in the database for this student:
$thisStudentsTrainings = Training::where('fk_student', $fk_student)->get();
Or you can come in from the reverse for this simply query:
$student = Student::with('trainings')->get();
$thisStudentsTrainings = $student->trainings;
Then, to limit to two trainings (without payment consideration yet):
if(count($thisStudentsTrainings) >= 2){ too many trainings }
Now that you have a count of trainings, if you also want to make sure they have a payment in the system, lets get the payments:
$thisStudentsPayments = Payment::where('fk_student', $request->get('fk_student'))->get();
To check if they have paid for trainings, you now have both pieces of data that you need. You just have to figure out if they have paid for the right amount of trainings based on 2 payments = 1 training. So:
if( (count($thisStudentsPayments) * 2) < count($thisStudentsTrainings) ) {
// They have not made enough payments!
}
My second queston, if I encode 2 payments for the same student. The student is entitled to 4 trainings. How to create this type of algorithm?
The above will work for 2 or 4 or whatever you want.
Now, if you want to enforce a max of 2 trainings per each payment, we can check on this too. BUT, this is starting to get a little complex or circular in the logic. If you can avoid this, it will be a lot easier. But, let's check on the max of 2 per payment, which is just the adding an equals check, AFTER the one above:
if( (count($thisStudentsTrainings) >= count($thisStudentsPayments) * 2) {
// They are at their limit of trainings based on their payments!
// Note we include >= so that if they have purchased 2 trainings,
// this blocks them from a 3rd until they pay again.
}
This should solve your issue. However, you didn't ask, but I assume you don't want a student to allow a training if they have already used up a payment. IE if they've taken a training and they have 'spent their credit', they should not be allowed to take the training. If this is important to you, I suggest that in another part of your program, you write to the database when a payment has been consumed. So - if a student uses 2 trainings and has paid for them, maybe a boolean field on the Payment model spent (or something to indicate the payment is no longer valid). You could also remove the payment from the system if you don't need historical data. But, assuming you do, and you use $payment->spent, you can still do the above algorithm, just add the spent line to the query something like:
$student = Student::with(['trainings' => function($query){
$query->where('spent', 0)
}])->get();
Then all the rest should be the same. This isn't cut & paste, but I think now that you have separated out payments and trainings, this should be a pretty easy solve to understand based on the above. :)

CakePHP how to add data from another field of a related model

This is one of my first applications out of tutorials so I don't know how to express my issue well.
Well I have these 2 tables:
User ( id, code )
Hours ( id, user_id, created)
I want to know how I can add an entry to the Hours table using the user_code.
I tried to grab the data of the User table with the code value and then findBy and pass for the patchEntity but it did not work.
I don't have a whole lot of information to work with, but I'll give it a go.
I want to know how I can add an entry to the Hours table using the
user_code
You mention using patchEntity, so that's updating information that's already there. Assuming user_code is the 'code' column you're talking about there, first find the user by his code:
$users_tbl = TableRegistry::get('Users');
// find the user
$user = $users_tbl->findByCode($user_code)->first();
if ($user) {
// replace '$this->request->data() with whatever patch data you wanted
$users_tbl->patchEntity($user, $this->request->data(), [
'associated' => ['Hours']
]
if ($users_tbl->save($user)) {
// success!
} else {
// error!
}
} else {
// error!
}
It will also depend on how you have the data you passed in (where my '$this->request->data() is, or whatever your array might be) - it needs to match the right column names and be in the correct format listed here.
However, this is updating the data. Just adding the data, you can load the hours table and add a new entry with the user_id acquired from the user search:
$hours_tbl = TableRegistry::get('Hours');
$hours = $hours_tbl->newEntity([
'user_id' => $user->id // $user populated from same method earlier
]);
/* assumed 'id' was autoincrementing and 'created' was populated
through Timestamp behavior */
if ($hours_tbl->save($hours)) {
// yay!
} else {
// boo
}

Yiiframework First time login

I'm currently busy with a project that needs users to go to a specific page to create a profile when they log in for the first time (and haven't created one yet). Honestly, I don't know where to start. I would like to do it in a good way.
So in short:
User signs up -> logs in -> needs to fill in form before anything else is allowed -> continue to rest of application
Question: What is a neat way to do this? A solution that isn't going to give me problems in the future development of the application.
I suggest you to use filters. In every controller where the completed profile is neeeded add this code:
public function filters() {
return array(
'completedProfile + method1, method2, method3', // Replace your actions here
);
}
In your base controller (if you don't use base controller, in any controllers) you need to create the filter named completedProfile with the simular code:
public function filterCompletedProfile($filterChain) {
$criteria = new CDBCriteria(array(
'condition' => 'id = :id AND firstname IS NOT NULL AND lastname IS NOT NULL',
'params' => array(':id' => Yii::app()->user->getId())
));
$count = User::model()->count($criteria);
if ($count == 1) {
$filterChain->run();
} else {
$this->redirect(array('user/profile'));
}
}
Possibly add a field to the user profile database table which denotes if they have filled out their profile information. Something like profile_complete. Then you can do a test on pages to see if profile_complete is true and display the page if so, and display the profile page if not.

how to use Native sessions and MySQL to join information

I have never used sessions before and I am trying to figure out the best way to handle this. I basically am trying to do:
1 step selecting a service
2 step selecting a time
3 step review and book
I can get it to work with no problems using mysql. What I would usually do is save the information into the database after each step and by the time I get to the review part I would have all the information saved and was OK.
However I don't think this is the best way to approach this and might cause problems down the road (what if they stopped at step 2 blah blah)
I decided to try the Laravel 4 sessions and it was super easy to save the session and move on to the next step. However, when I get to the final step I need to join mysql tables to fully show the information about their booking. Can I use the session information to join the information? Can I use the Sessions Database to save this information? Or use different tables?
My controller that POST after reviewing the information:
public function getReview() {
//sets id of user
$user = User::find(Auth::user()->id);
//gets the time and date that they booked from #getSchedule
$scheduler = Session::get('schedule');
//formats time to put in db
$date = strtotime($scheduler['date']);
//same thing as the line above
$dateFormat = date('Y-m-d',$date);
//model to save the schedule
$schedule = new Schedule();
$schedule->userID = $user->id;
$schedule->date = $dateFormat;
$schedule->block = $scheduler['timeslot'];
$schedule->status = "1";
$schedule->save();
//gets the services the user picked from #getServices
$service = Session::get('service');
//saves the services as individual rows in db table
foreach($service as $services)
{
if(!empty($services)) {
$service = new Service();
$service->userID = $user->id;
$service->services = $services;
$service->save();
}
}
return Redirect::to('dashboard');
}
This is the GET review page (where I am having the issues with all the JOINS)
public function showReview() {
$user = User::find(Auth::user()->id);
//show the information and confirm that they want all this crap...if they do..save it and return them to their dashboard
$time = DB::table('bk_schedule')
->leftJoin('bk_timeslot', 'bk_schedule.block', '=', 'bk_timeslot.id')
->where('bk_schedule.id', Auth::user()->id)->first();
$date = strtotime($time->date);
$dateFormat = date('D: F d, Y',$date);
$service = Session::get('service');
$serviceSummary = DB::table('bk_service')
->leftJoin('pr_service', 'pr_service.id', '=', 'bk_service.services')
->where('bk_service.userID', Auth::user()->id)
->get();
$total = DB::table('bk_service')
->leftJoin('pr_service', 'pr_service.id', '=', 'bk_service.services')
->where('bk_service.userID', Auth::user()->id)
->sum('pr_service.price');
return View::make('book.review', array('pageTitle' => 'Booking Appointment Review and Finalize', 'service' => $service, 'date' => $dateFormat,
'time' => $time, 'email' => $user->email, 'serviceSummary' => $serviceSummary, 'total' => $total));
}
Is it possible to save the information at the GET and delete it if they don't submit to POST? Could I maybe use my session data to and use the MySQL queries I have?
You don't understand what the session is, with you approach users will not be able to fill several forms (open in several tabs) simultaneously.
So, general way to do this is:
First page shows just HTML code with some fields
User selects them and POST data back to server
Server validates data and open ANOTHER HTML page with new fields AND adds several "hidden" field with values selected in step 1 (of course server can present page1 with error messages)
Users posts this form, server can open THIRD form where new visible fields and ALL previous fields are stored in hidden inputs
Finally user posts form to last page, where you have all data from all previous pages.
Just to notice: another approach is to store "temporary" data in session, for this you will need to obtain some generated ID on 2nd step and pass it through pages as described before

Developer Response - Payment Callback. PHP - Making me go crazy

Okay so I am trying to set up payments in my app that I am developing, I have the dialog working and it confirms the purchase of the item. I have no idea what it does after here tbh, I have read articles after articles, but no joy. I need information on how I can update mysql database on the purchase. I know the question is vague, but any guidance will be appreciated!
The code i have so far is below.
if ($request_type == 'payments_get_items') {
// Get order info from Pay Dialog's order_info.
// Assumes order_info is a JSON encoded string.
$order_info = json_decode($request['credits']['order_info'], true);
// Get item id.
$item_id = $order_info['item_id'];
// Simulutates item lookup based on Pay Dialog's order_info.
if ($item_id == '10Kremeggs') {
$item = array(
'title' => '10 Kremeggs',
'description' => 'Spend Kremeggs in Alien Abduction.',
// Price must be denominated in credits.
'price' => 5,
'image_url' => 'https://afternoon-snow-5267.herokuapp.com/images/bung.png',
'product_url' => 'https://afternoon-snow-5267.herokuapp.com/Purchase/10xKremeggs.php'
);
// Construct response.
$response = array(
'content' => array(
0 => $item,
),
'method' => $request_type,
);
// Response must be JSON encoded.
$response = json_encode($response);
}
} else if ($request_type == "payments_status_update") {
// Get order details.
$order_details = json_decode($request['credits']['order_details'], true);
// Determine if this is an earned currency order.
$item_data = json_decode($order_details['items'][0]['data'], true);
$earned_currency_order = (isset($item_data['modified'])) ?
$item_data['modified'] : null;
// Get order status.
$current_order_status = $order_details['status'];
if ($current_order_status == 'placed') {
// Fulfill order based on $order_details unless...
if ($earned_currency_order) {
// Fulfill order based on the information below...
// URL to the application's currency webpage.
$product = $earned_currency_order['product'];
// Title of the application currency webpage.
$product_title = $earned_currency_order['product_title'];
// Amount of application currency to deposit.
$product_amount = $earned_currency_order['product_amount'];
// If the order is settled, the developer will receive this
// amount of credits as payment.
$credits_amount = $earned_currency_order['credits_amount'];
}
$next_order_status = 'settled';
// Construct response.
$response = array(
'content' => array(
'status' => $next_order_status,
'order_id' => $order_details['order_id'],
),
'method' => $request_type,
);
// Response must be JSON encoded.
$response = json_encode($response);
} else if ($current_order_status == 'disputed') {
// 1. Track disputed item orders.
// 2. Investigate user's dispute and resolve by settling or refunding the order.
// 3. Update the order status asychronously using Graph API.
} else if ($current_order_status == 'refunded') {
// Track refunded item orders initiated by Facebook. No need to respond.
} else {
// Track other order statuses.
}
}
Above is part of the payment callback
<? mysql_query("UPDATE users SET Kremeggs = Kremeggs+10 WHERE Facebook_id = '$PurchaseUpdate'");
header ("Location: http://apps.facebook.com/alien_abduction/purchaseComplete.php");
?>
</body>
</html>
Above is what i need the payment to do.
It sounds like you're attempting to handle two very separate parts of the process at the same time in one file (your second code sample). The first code sample you posted (looks like their sample code, which is fine) is the proper place to award the user - your mysql query belongs inside the block which starts on line 43 (if ($current_order_status == 'placed') {). I'll leave the details of that for you to work out.
The second half of what you want to do (redirect the user to a confirmation page) does not belong in that callback, but rather on the page with the payment dialog. If you're using the sample code there as well, it belongs inside the js_callback function - try window.location.href='purchase_complete.php' (ensuring that this only happens on successful orders, ofc).
Hope that gets you pointed in the right direction!
else if ($request_type == "payments_status_update") {mysql_query("update users_table set payment_column='isok' where kulid like 'getFacebookUserId'");}

Categories