lundi 29 juin 2020

Auto completed late jobs, refund amount to user with php

This is long, so be patient with me.

here is a link to full code: https://pastebin.com/xGJ9J9i2

I am trying to create a script that will handle the late jobs of my recent project. the script will select all late jobs from the database and then we will check for job applicants. if there are no applicants, we will cancel the job and refund the amount to the job poster. If there are applicants, we will check to see if they have delivered the work. otherwise, mark applicant job as cancelled and refund the amount to the user. if the work was delivered and the delivery status is waiting_for_approval, we will accept the work and mark the applicant's work as completed and add funds to his account.

but I am confused about my code because only some part works. right now if no user applied to the job this code refunds amount to the user. If the user applied for the job, someone submitted the job, and if some deliveries are under review, it does nothing.

What works:

  • Select all jobs where delivery time is less than 4 hours
  • Select all job applicants.
  • If there are no applicants, refund the amount to the user

What doesn't work:

  • if there are applicants and they did not deliver the job or if the delivery status equals in_revision, cancel the job and provide the user with the refund and mark the applicant's job status as cancelled

  • if the delivery status equals wait_for_approval, the job automatically completes, adds funds to the requester's account

  • marks the requester's job status as completed

  • Finally, the status of the job as completed

Any suggestions?

<?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;
}

Aucun commentaire:

Enregistrer un commentaire