Guest User

update_late_jobs

a guest
Jun 29th, 2020
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. <?php
  2.  
  3. require_once('includes/initialize.php');
  4.  
  5. $jobs = get_all_late_jobs();
  6.  
  7. if (mysqli_num_rows($jobs) > 0) {
  8.  
  9. while ($job = mysqli_fetch_assoc($jobs)) {
  10.  
  11. $job_id = $job['job_id'];
  12.  
  13. // get all job applicants
  14. $applicants = get_job_applicants($job_id);
  15.  
  16. if (mysqli_num_rows($applicants) > 0) {
  17.  
  18. while ($applicant = mysqli_fetch_assoc($applicants)) {
  19.  
  20. $applicant_id = $applicant['applicant_id'];
  21.  
  22. // get job deliveries of this applicant
  23. $deliveries = find_all_by_id($applicant_id, 'applicant_id', 'job_deliveries', 'DESC', 's', 0);
  24.  
  25. if (mysqli_num_rows($deliveries) > 0) {
  26.  
  27. while ($delivery = mysqli_fetch_assoc($deliveries)) {
  28.  
  29. // if the applicants has delivered the job auto complete the job
  30.  
  31. if ($delivery['status'] === 'waiting_for_approval') {
  32. // mark delivery accepted
  33. update_by_id('accepted', $delivery['delivery_id'], 'status', 'delivery_id', 'job_deliveries');
  34.  
  35. // update applicant job status
  36. mark_seller_job_status('completed', $job_id, $applicant_id);
  37.  
  38. // update seller wallet
  39. $wallet = [
  40. 'action' => 'add',
  41. 'user_id' => $delivery['applicant_id'],
  42. 'amount' => round($job['budget'] / $job['required_freelancers']),
  43. 'used_for' => 'completed_job'
  44. ];
  45.  
  46. update_user_wallet_history($wallet);
  47. }
  48.  
  49. // if the delivery is in revision cancel the job and provide user refund
  50. if ($delivery['status'] === 'in_revision') {
  51. cancel_seller_job_give_refund($applicant_id, $job);
  52. add_review_to_user_profile($job['posted_by'], $applicant_id, $job_id, 'Cancelled job. Seller failed to deliver on time!', '1');
  53. }
  54. }
  55. } else {
  56. // if the applicants didn't delivered the job cancel the job and provide user refund
  57. cancel_seller_job_give_refund($applicant_id, $job);
  58. add_review_to_user_profile($job['posted_by'], $applicant_id, $job_id, 'Cancelled job. Seller failed to deliver on time!', '1');
  59. }
  60. }
  61. } else {
  62. // if there are no applicants provide user full refund mark the job as cancelled
  63. give_user_full_refund($job);
  64. }
  65.  
  66. // mark the job as completed
  67. update_by_id('completed', $job['job_id'], 'status', 'job_id', 'jobs');
  68. }
  69. } else {
  70. return false;
  71. }
  72.  
  73.  
  74. function cancel_seller_job_give_refund($applicant_id, $job)
  75. {
  76. mark_seller_job_status('cancelled', $job['job_id'], $applicant_id);
  77.  
  78. $amount = round($job['budget'] / $job['required_freelancers']);
  79.  
  80. $wallet = [
  81. 'action' => 'add',
  82. 'user_id' => $job['posted_by'],
  83. 'amount' => $amount,
  84. 'used_for' => 'cancelled_job'
  85. ];
  86.  
  87. update_user_wallet_history($wallet);
  88. }
  89.  
  90. function mark_seller_job_status($status, $job_id, $applicant_id)
  91. {
  92. global $db;
  93. $sql = "UPDATE job_applicants SET status = ? WHERE job_id = ? AND applicant_id = ?";
  94. $stmt = mysqli_stmt_init($db);
  95. mysqli_stmt_prepare($stmt, $sql);
  96.  
  97. mysqli_stmt_bind_param($stmt, 'sss', $status, $job_id, $applicant_id);
  98. mysqli_stmt_execute($stmt);
  99. mysqli_stmt_close($stmt);
  100. }
  101.  
  102. function get_all_late_jobs()
  103. {
  104. global $db;
  105. $status = 'active';
  106. $sql = "SELECT * FROM jobs WHERE status = ? AND job_expiration < NOW() + INTERVAL 240 MINUTE";
  107. $stmt = mysqli_stmt_init($db);
  108. mysqli_stmt_prepare($stmt, $sql) or die(__LINE__ . ' Prepare Failed ' . mysqli_stmt_error($stmt));
  109. mysqli_stmt_bind_param($stmt, 's', $status);
  110. mysqli_stmt_execute($stmt) or die(__LINE__ . ' Prepare Failed ' . mysqli_stmt_error($stmt));
  111. $result = mysqli_stmt_get_result($stmt);
  112. mysqli_stmt_close($stmt);
  113. return $result;
  114. }
  115.  
  116. function update_by_id($value, $id, $column_name, $id_name = "id", $table_name, $value_type = "s", $id_type = "s")
  117. {
  118. global $db;
  119. $sql = "UPDATE " . $table_name . " SET " . $column_name . "= ? WHERE " . $id_name . " = ?";
  120. $stmt = mysqli_stmt_init($db);
  121. mysqli_stmt_prepare($stmt, $sql) or die('Prepare Failed ' . mysqli_stmt_error($stmt));
  122. mysqli_stmt_bind_param($stmt, $value_type . $id_type, $value, $id);
  123. $result = mysqli_stmt_execute($stmt);
  124. mysqli_stmt_close($stmt);
  125. }
  126.  
  127. function add_review_to_user_profile($reviewer, $reviewed, $job, $comment, $rating)
  128. {
  129. global $db;
  130. $sql = "INSERT INTO reviews(reviewer, reviewed, job_id, rating, comment, reviewed_at) VALUES (?, ?, ?, ?, ?, ?)";
  131. $stmt = mysqli_stmt_init($db);
  132. mysqli_stmt_prepare($stmt, $sql);
  133.  
  134. $time = date('U');
  135. mysqli_stmt_bind_param($stmt, 'sssisi', $reviewer, $reviewed, $job, $rating, $comment, $time);
  136. $result = mysqli_stmt_execute($stmt);
  137. mysqli_stmt_close($stmt);
  138. }
  139.  
  140. function give_user_full_refund($job)
  141. {
  142. $amount = $job['budget'] + round($job['budget'] * .2);
  143.  
  144. $wallet = [
  145. 'action' => 'add',
  146. 'user_id' => $job['posted_by'],
  147. 'amount' => $amount,
  148. 'used_for' => 'cancelled_job'
  149. ];
  150.  
  151. update_user_wallet_history($wallet);
  152. }
  153.  
  154. function update_user_wallet_history($wallet)
  155. {
  156.  
  157. $user = find_by_id($wallet['user_id'], 'user_id', 'users');
  158.  
  159. if ($wallet['action'] === 'add') {
  160. $amount_left = $user['wallet'] + $wallet['amount'];
  161. }
  162.  
  163. if ($wallet['action'] === 'update') {
  164. $amount_left = $user['wallet'] - $wallet['amount'];
  165. }
  166.  
  167. $new_status = [
  168. 'used_by' => $wallet['user_id'],
  169. 'amount' => $wallet['amount'],
  170. 'amount_left' => $amount_left,
  171. 'used_for' => $wallet['used_for']
  172. ];
  173.  
  174. // updating user wallet history
  175. update_wallet_history_table($new_status);
  176. // updating user wallet amount
  177. update_user_wallet($new_status['amount_left'], $new_status['used_by']);
  178. }
Add Comment
Please, Sign In to add comment