This file code is for video purchases, where a portion of the purchase amount goes to the Uploader of the video. His earnings portion, in this code, is his ‘balance’. It works successfully, but I’d like some guidance on how to calculate the Uploader’s earnings ‘balance’ per day (and per month). Here’s the code:
<?php
ob_start();
if (IS_LOGGED == false) {
$data = array('status' => 400, 'error' => 'Not logged in');
echo json_encode($data);
exit();
}
if (!empty($_POST['id'])) {
if (!is_array($_POST['id'])) {
$id_array[] = $_POST['id'];
} else {
$id_array = $_POST['id'];
}
// get cost video
// get the default video price, to use if there is no per video play price
$db->where('name', 'video_play_price');
$db_cost = $db->getOne('config');
$video_cost = (float)$db_cost->value;
// the number of submitted videos - used to determine if all records were inserted
$count_video = count($id_array);
$user_id = $user->id;
$wallet = (float)str_replace(',', '', $user->wallet);
$balance = (float)str_replace(',', '', $user->balance);
// add up the video prices
$amount = 0;
foreach ($id_array as $id) {
$video_id = (int)PT_Secure($id);
// get video data
$video = $db->where('id', $id)->getOne(T_VIDEOS);
// add the video play price if any, or the default price
$amount += $video->video_play_price?$video->video_play_price:$video_cost;
}
// determine if the user has enough credits
if( ($wallet >= $amount) OR ($balance + $wallet >= $amount) ) {
$db->startTransaction();
$inserted_records = 0;
foreach ($id_array as $id){
$video_id = (int)PT_Secure($id);
// get video data
$video = $db->where('id', $id)->getOne(T_VIDEOS);
// use the video play price if any, or the default price
$video_cost_new = $video->video_play_price?$video->video_play_price:$video_cost;
// add data to paid table
$insert_buy = $db->insert('u_paid_videos', [
'id_user' => $user_id,
'id_video' => $video_id,
'session_key' => $_SESSION['session_key'],
'video_play_price' => (string)$video_cost, // the cost at the time of purchase // this is the default video cost not the $video_cost_new
'video_title' => $video->title, // storing the title
'user_id_uploaded' => $video->user_id, // the user who uploaded the video
]);
// count successful inserted records
if ($insert_buy) {
$inserted_records++;
}
//update the 'balance' of the user who uploaded the video
// get the user's record
$userwallet = $db->where('id', $video->user_id)->getOne(T_USERS);
// credit the user 50% of the video cost
$uploader_amount = $video_cost_new *0.50;
// add to the balance
$videouserwallet = $userwallet->balance+$uploader_amount;
// update the record
$db->where('id', $video->user_id);
$update_balance = $db->update(T_USERS, [
'balance' => number_format($videouserwallet, 1, '.', ''),
]);
}
$update_wallet = null;
$update_user_balance = null;
if($wallet >= $amount){
$wallet = (string)($wallet - $amount);
$db->where('id', $user_id);
$update_wallet = $db->update(T_USERS, [
'wallet' => $wallet
]);
}else if ($wallet + $balance >= $amount) {
$balance = (string)($balance - ($amount - $wallet));
$wallet = (string)($wallet - $wallet);
$db->where('id', $user_id);
$update_user_balance = $db->update(T_USERS, [
'balance' => $balance
]);
$update_wallet = $db->update(T_USERS, [
'wallet' => $wallet
]);
} else {
echo json_encode([
'status' => 400,
'error_num' => 1,
'error' => 'Not enough money'
]);
exit();
}
// if all the video records were inserted and the current user's wallet was updated, commit the changes
if (($inserted_records == $count_video) && ($update_wallet OR $update_user_balance) ) {
$db->commit();
echo json_encode([
'status' => 200
]);
exit();
} else {
$db->rollback();
echo json_encode([
'status' => 400,
'_data' => [
'inserted_records' => $inserted_records,
'count_video' => $count_video,
'update_wallet' => $update_wallet,
'update_user_balance' => $update_user_balance
],
'error' => 'Buy process error'
]);
exit();
}
} else {
echo json_encode([
'status' => 400,
'error_num' => 1,
'error' => 'Not enough money'
]);
exit();
}
} else {
echo json_encode([
'status' => 400,
'error' => 'Bad Request, Invalid or missing parameter'
]);
exit();
}
Any help is appreciated