invoiceninja/app/Jobs/GenerateCalendarEvents.php

55 lines
1.5 KiB
PHP
Raw Normal View History

2017-09-12 13:43:59 +03:00
<?php
namespace App\Jobs;
use App\Jobs\Job;
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\Expense;
use App\Models\Task;
2017-12-24 17:34:17 +02:00
use App\Models\Project;
2017-09-12 13:43:59 +03:00
class GenerateCalendarEvents extends Job
{
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
2017-09-12 17:16:18 +03:00
$events = [];
$filter = request()->filter ?: [];
$data = [
ENTITY_INVOICE => Invoice::scope()->invoices(),
ENTITY_QUOTE => Invoice::scope()->quotes(),
2017-09-13 11:56:26 +03:00
ENTITY_TASK => Task::scope()->with(['project']),
ENTITY_PAYMENT => Payment::scope()->with(['invoice']),
ENTITY_EXPENSE => Expense::scope()->with(['expense_category']),
2017-12-24 17:34:17 +02:00
ENTITY_PROJECT => Project::scope(),
2017-09-12 17:16:18 +03:00
];
foreach ($data as $type => $source) {
if (! count($filter) || in_array($type, $filter)) {
2017-09-13 16:34:16 +03:00
$source->where(function($query) use ($type) {
$start = date_create(request()->start);
$end = date_create(request()->end);
return $query->dateRange($start, $end);
});
2017-09-13 11:56:26 +03:00
foreach ($source->with(['account', 'client.contacts'])->get() as $entity) {
if ($entity->client && $entity->client->trashed()) {
continue;
}
2017-09-13 00:20:18 +03:00
$subColors = count($filter) == 1;
$events[] = $entity->present()->calendarEvent($subColors);
2017-09-12 17:16:18 +03:00
}
}
2017-09-12 13:43:59 +03:00
}
2017-09-12 17:16:18 +03:00
return $events;
2017-09-12 13:43:59 +03:00
}
}