Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once('includes/initialize.php');
- $jobs = get_all_late_jobs();
- if (mysqli_num_rows($jobs) > 0) {
- while ($job = mysqli_fetch_assoc($jobs)) {
- $job_id = $job['job_id'];
- // get all job applicants
- $applicants = get_job_applicants($job_id);
- if (mysqli_num_rows($applicants) > 0) {
- while ($applicant = mysqli_fetch_assoc($applicants)) {
- $applicant_id = $applicant['applicant_id'];
- // get job deliveries of this applicant
- $deliveries = find_all_by_id($applicant_id, 'applicant_id', 'job_deliveries', 'DESC', 's', 0);
- if (mysqli_num_rows($deliveries) > 0) {
- while ($delivery = mysqli_fetch_assoc($deliveries)) {
- // if the applicants has delivered the job auto complete the job
- if ($delivery['status'] === 'waiting_for_approval') {
- // mark delivery accepted
- update_by_id('accepted', $delivery['delivery_id'], 'status', 'delivery_id', 'job_deliveries');
- // update applicant job status
- mark_seller_job_status('completed', $job_id, $applicant_id);
- // update seller wallet
- $wallet = [
- 'action' => 'add',
- 'user_id' => $delivery['applicant_id'],
- 'amount' => round($job['budget'] / $job['required_freelancers']),
- 'used_for' => 'completed_job'
- ];
- update_user_wallet_history($wallet);
- }
- // if the delivery is in revision cancel the job and provide user refund
- if ($delivery['status'] === 'in_revision') {
- cancel_seller_job_give_refund($applicant_id, $job);
- add_review_to_user_profile($job['posted_by'], $applicant_id, $job_id, 'Cancelled job. Seller failed to deliver on time!', '1');
- }
- }
- } else {
- // if the applicants didn't delivered the job cancel the job and provide user refund
- cancel_seller_job_give_refund($applicant_id, $job);
- add_review_to_user_profile($job['posted_by'], $applicant_id, $job_id, 'Cancelled job. Seller failed to deliver on time!', '1');
- }
- }
- } else {
- // if there are no applicants provide user full refund mark the job as cancelled
- give_user_full_refund($job);
- }
- // mark the job as completed
- update_by_id('completed', $job['job_id'], 'status', 'job_id', 'jobs');
- }
- } else {
- return false;
- }
- function cancel_seller_job_give_refund($applicant_id, $job)
- {
- mark_seller_job_status('cancelled', $job['job_id'], $applicant_id);
- $amount = round($job['budget'] / $job['required_freelancers']);
- $wallet = [
- 'action' => 'add',
- 'user_id' => $job['posted_by'],
- 'amount' => $amount,
- 'used_for' => 'cancelled_job'
- ];
- update_user_wallet_history($wallet);
- }
- function mark_seller_job_status($status, $job_id, $applicant_id)
- {
- global $db;
- $sql = "UPDATE job_applicants SET status = ? WHERE job_id = ? AND applicant_id = ?";
- $stmt = mysqli_stmt_init($db);
- mysqli_stmt_prepare($stmt, $sql);
- mysqli_stmt_bind_param($stmt, 'sss', $status, $job_id, $applicant_id);
- mysqli_stmt_execute($stmt);
- mysqli_stmt_close($stmt);
- }
- function get_all_late_jobs()
- {
- global $db;
- $status = 'active';
- $sql = "SELECT * FROM jobs WHERE status = ? AND job_expiration < NOW() + INTERVAL 240 MINUTE";
- $stmt = mysqli_stmt_init($db);
- mysqli_stmt_prepare($stmt, $sql) or die(__LINE__ . ' Prepare Failed ' . mysqli_stmt_error($stmt));
- mysqli_stmt_bind_param($stmt, 's', $status);
- mysqli_stmt_execute($stmt) or die(__LINE__ . ' Prepare Failed ' . mysqli_stmt_error($stmt));
- $result = mysqli_stmt_get_result($stmt);
- mysqli_stmt_close($stmt);
- return $result;
- }
- function update_by_id($value, $id, $column_name, $id_name = "id", $table_name, $value_type = "s", $id_type = "s")
- {
- global $db;
- $sql = "UPDATE " . $table_name . " SET " . $column_name . "= ? WHERE " . $id_name . " = ?";
- $stmt = mysqli_stmt_init($db);
- mysqli_stmt_prepare($stmt, $sql) or die('Prepare Failed ' . mysqli_stmt_error($stmt));
- mysqli_stmt_bind_param($stmt, $value_type . $id_type, $value, $id);
- $result = mysqli_stmt_execute($stmt);
- mysqli_stmt_close($stmt);
- }
- function add_review_to_user_profile($reviewer, $reviewed, $job, $comment, $rating)
- {
- global $db;
- $sql = "INSERT INTO reviews(reviewer, reviewed, job_id, rating, comment, reviewed_at) VALUES (?, ?, ?, ?, ?, ?)";
- $stmt = mysqli_stmt_init($db);
- mysqli_stmt_prepare($stmt, $sql);
- $time = date('U');
- mysqli_stmt_bind_param($stmt, 'sssisi', $reviewer, $reviewed, $job, $rating, $comment, $time);
- $result = mysqli_stmt_execute($stmt);
- mysqli_stmt_close($stmt);
- }
- function give_user_full_refund($job)
- {
- $amount = $job['budget'] + round($job['budget'] * .2);
- $wallet = [
- 'action' => 'add',
- 'user_id' => $job['posted_by'],
- 'amount' => $amount,
- 'used_for' => 'cancelled_job'
- ];
- update_user_wallet_history($wallet);
- }
- function update_user_wallet_history($wallet)
- {
- $user = find_by_id($wallet['user_id'], 'user_id', 'users');
- if ($wallet['action'] === 'add') {
- $amount_left = $user['wallet'] + $wallet['amount'];
- }
- if ($wallet['action'] === 'update') {
- $amount_left = $user['wallet'] - $wallet['amount'];
- }
- $new_status = [
- 'used_by' => $wallet['user_id'],
- 'amount' => $wallet['amount'],
- 'amount_left' => $amount_left,
- 'used_for' => $wallet['used_for']
- ];
- // updating user wallet history
- update_wallet_history_table($new_status);
- // updating user wallet amount
- update_user_wallet($new_status['amount_left'], $new_status['used_by']);
- }
Add Comment
Please, Sign In to add comment