File: D:/HostingSpaces/SBogers10/hours.komma.pro/app/Komma/Notifications/NotificationService.php
<?php
namespace App\Komma\Notifications;
use App\Komma\Users\User;
use Carbon\Carbon;
use App\Komma\Companies\Company;
use App\Komma\Projects\Project;
use App\Komma\Settings\AbsenceBalances\AbsenceBalance;
class NotificationService
{
public function __construct()
{
//
}
/**
* @param $request
*/
public function create($title, $message, $url, $role = 0, $subject = null)
{
$notification = new Notification();
$notification->title = $title;
$notification->message = $message;
$notification->url = $url;
$notification->role_id = $role;
if (!empty($subject)) $subject->Notifications()->save($notification);
else $notification->save();
return $notification;
}
/**
* @param $id
* @param $title
* @param $message
*/
public function update($id, $title, $message)
{
$notification = Notification::find($id);
$notification->title = $title;
$notification->message = $message;
$notification->save();
}
/**
* @param $id
*/
public function done($id)
{
$notification = Notification::find($id);
$notification->done_at = Carbon::now();
$notification->save();
}
/**
* @param $id
*/
public function delete($id)
{
Notification::find($id)->forceDelete();
}
/**
* @return mixed
*/
public function getAllNotifications(){
$precheckNotifications = Notification::orderBy('done_at')->get();
// check if the (company) notification is still relevant
foreach ($precheckNotifications as $notification){
if($notification->notification_type == Company::class && !Company::find($notification->notification_id)){
// company does not exist anymore
$this->delete($notification->id);
}
}
$notifications = Notification::orderBy('done_at');
return $notifications;
}
/**
* @param $collection
* @param $company
*/
public function companyNotification($collection, $company)
{
$notification = !empty($company->Notifications()->where('done_at', '0000-00-00 00:00:00')->first()) ? $company->Notifications()->where('done_at', '0000-00-00 00:00:00')->first() : null;
if (!empty($collection->deleted)) {
if (!empty($notification)) $this->delete($notification->id);
} else {
$emptyFields = "";
$checkFields = ['email' => "Email", 'phone' => "Telefoon", 'person' => "Contactpersoon", 'street' => "Straatnaam", 'zip_code' => "Postcode", 'housenumber' => "Huisnummer", 'city' => "Plaatsnaam"];
foreach ($checkFields as $field => $label) {
if (empty($collection[$field])) {
$emptyFields = $emptyFields . $label . ', ';
}
}
if ($company->Country()->first()->name == "Nederland") {
if (empty($collection['kvk'])) {
$emptyFields = $emptyFields . 'KVK nummer, ';
}
}
if (!empty($notification) and empty($emptyFields)) $this->done($notification->id);
if (!empty($emptyFields) and !empty($notification)) $this->update($notification->id, "Klantgegevens aanvullen", "Er missen nog klantgegevens bij klant <b>" . $collection['name'] . "</b>. <br> " . rtrim($emptyFields, ", "));
if (!empty($emptyFields) and empty($notification)) $this->create("Klantgegevens aanvullen", "Er missen nog klantgegevens bij klant <b>" . $collection['name'] . "</b>. <br> " . rtrim($emptyFields, ", "), "/klanten/" . $company->id . "/edit", 2, $company);
}
}
/**
* @param $project
*/
public function archivedNotification($project)
{
$notification = !empty($project->Notifications()->first()->id) ? $project->Notifications()->first()->id : null;
if ($project->archived == 0 and !empty($notification)) $this->delete($project->Notifications()->first()->id);
if ($project->calculated == 1 and !empty($notification)) $this->done($project->Notifications()->first()->id);
elseif ($project->archived == 1) $this->create("Project gearchiveerd", "Het project: " . $project->name . " is gearchiveerd. De factuur kan worden klaargezet.", "/projecten/" . $project->id . "", 2, $project);
}
/**
* @param null $year
*/
public function balanceNotification($year = null)
{
if (empty($year)) $year = Carbon::now()->format('Y');
$balances = AbsenceBalance::where('year', $year)->where('balance', 0)->count();
$notificationExist = Notification::where('message', "De saldo's van het jaar " . $year . " zijn nog niet compleet.")->where('done_at', '0000-00-00 00:00:00')->get()->first();
if ($balances != 0 and empty($notificationExist)) {
$this->create("Saldo's niet compleet", "De saldo's van het jaar " . $year . " zijn nog niet compleet.", "/instellingen/balance", 2);
} elseif (!empty($notificationExist)) {
$this->done($notificationExist->id);
}
}
/**
* @param $ProjectID
* @param $treshold
*/
public function budgetNotification($subproject, $treshold)
{
$project = Project::find($subproject->project_id);
$notificationExist = Notification::where('title', 'Deelproject '.$subproject->name.' nadert of gaat over het budget heen!')->get()->first();
if (empty($notificationExist)) {
$newNotif = $this->create('Deelproject '.$subproject->name.' nadert of gaat over het budget heen!', 'Project '. $project->name .' - deelproject '.$subproject->name.' - Overschrijding: '. $treshold, "/projecten/".$project->id, 2);
$this->done($newNotif->id);
$this->mailTim($subproject, $project, $treshold);
} elseif($notificationExist->message !== 'Project '. $project->name .' - deelproject '.$subproject->name.' - Overschrijding: '. $treshold) {
$this->update($notificationExist->id, 'Deelproject '.$subproject->name.' nadert of gaat over het budget heen!', 'Project '. $project->name .' - deelproject '.$subproject->name.' - Overschrijding: '. $treshold);
$notificationExist->save();
$this->done($notificationExist->id);
$this->mailTim($subproject, $project, $treshold);
}
}
/**
* TODO: comments toevoegen
* @param $subproject
* @param $project
* @param $treshold
*/
private function mailTim($subproject, $project, $treshold){
\Log::warning('NotificationService::mailTim => ' . $project->name.' / '. $subproject->name .' / '. $treshold);
$subProjectLink = "<a href=\"". url('/subprojecten/'.$subproject->id)."\">$subproject->name</a>";
$to = 'tim@komma.pro';
$projectManager = $project->ProjectWorkers->first();
if($projectManager) {
$to .= ', '.$projectManager->User->email;
}
$subject = 'Project '. $project->name .' - deelproject '.$subproject->name.' - Overschrijding: '. $treshold;
$message = 'Beste Manager, <br/>
Project: '. $project->name .'. <br/>
Deelproject: '.$subProjectLink.'.<br/>
Overschrijding: '. $treshold;
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
$headers[] = 'From: Komma Urensysteem <support@komma.pro>';
mail($to, $subject, $message, implode("\r\n", $headers));
}
}