I'm looking for assistance with understanding this code. Several parts have commenting in regard to their function, however, towards the end there is very little to describe what the code section function is. Any assistance (or educated guess) with this is appreciated:
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;
// credit the uploader 50% of the video cost
$uploader_amount = $video_cost_new *0.50;
$time_start = microtime(true);
// add data to paid table
$insert_buy = $db->insert('paid_videos', [
'id_user' => $user_id,
'video_play_price' => (string)$video_cost_new,
'id_video' => $video_id,
'user_id_uploaded' => $video->user_id, // the user who uploaded the video
'video_title' => $video->title, // storing the title
'earned_amount' => $uploader_amount,
'time' => $time_start,
'short_id' => $video->short_id,
'session_key' => $_SESSION['session_key']
]);
// 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);
// 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();
}