public function removeContractorFromJob($job, $contractor) { try { if (!$job->contractors->contains($contractor->id)) { throw new \Exception("This contractor has never applied to this job.", 1); return; } $job->contractors()->detach($contractor->id); $notificationData = ['contractor_id' => $contractor->id, 'alert_from' => 'System: Programme Chameleon', 'has_read' => false, 'title' => 'Removed from ' . $job->title, 'description' => 'You have been removed from ' . $job->title, 'url' => null]; $notification = \Contractor::addNotification($contractor, $notificationData); if ($job->status === 'taken' && $job->contractors()->count() <= 0) { $job->status = 'open'; $job->save(); } return $job; } catch (\Exception $e) { throw new \Exception($e->getMessage(), 1); return; } }
public function updateExpense($id, $agency, $status) { if (!($expense = \Contractor::findExpenseById($id))) { throw new \Exception("Expense not found", 1); return; } $job = $expense->job; if ($agency->id !== $job->agency_id) { throw new \Exception("Expense does not belong to the agency.", 1); return; } if ($status) { $expense->auth_agency = true; $expense->save(); if ($contractor = $expense->contractor) { $notificationData = ['contractor_id' => $contractor->id, 'alert_from' => 'System: Programme Chameleon', 'has_read' => false, 'title' => 'Your expense "' . $expense->title . '" for "' . $job->title . '" has been accepted.', 'description' => 'Accepted by ' . $agency->name, 'url' => '#']; \Contractor::addNotification($contractor, $notificationData); } if (!is_null($job->company_id)) { $_hash = new Hash(); $_hash = $_hash->getHasher(); if ($company = \Company::findCompanyById($job->company_id)) { $notificationData = ['company_id' => $company->id, 'alert_from' => 'System: Programme Chameleon', 'has_read' => false, 'title' => 'Expense "' . $expense->title . '" for "' . $job->title . '" has been accepted.', 'description' => 'Accepted by ' . $agency->name, 'url' => route('company.job.detail') . '?i=' . $_hash->encode($job->id)]; \Company::addNotification($company, $notificationData); } } } else { $expense->auth_agency = true; $expense->save(); if ($contractor = $expense->contractor) { $notificationData = ['contractor_id' => $contractor->id, 'alert_from' => 'System: Programme Chameleon', 'has_read' => false, 'title' => 'Your expense "' . $expense->title . '" for "' . $job->title . '" has been de-authorized.', 'description' => 'De-authorize by ' . $agency->name, 'url' => '#']; \Contractor::addNotification($contractor, $notificationData); } if (!is_null($job->company_id)) { if ($company = \Company::findCompanyById($job->company_id)) { $_hash = new Hash(); $_hash = $_hash->getHasher(); $notificationData = ['company_id' => $company->id, 'alert_from' => 'System: Programme Chameleon', 'has_read' => false, 'title' => 'Expense "' . $expense->title . '" for "' . $job->title . '" has been de-authorized.', 'description' => 'De-authorize by ' . $agency->name, 'url' => route('agency.job.detail') . '?i=' . $_hash->encode($job->id)]; \Company::addNotification($company, $notificationData); } } } return $expense; }