From 647c44ee572a985fce6a14bf35f5578f2e5ec5e7 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Wed, 13 Sep 2017 16:34:16 +0300 Subject: [PATCH] Working on the calendar --- app/Jobs/GenerateCalendarEvents.php | 6 ++++++ app/Models/Expense.php | 10 ++++++++++ app/Models/Invoice.php | 14 ++++++++++++++ app/Models/Payment.php | 10 ++++++++++ 4 files changed, 40 insertions(+) diff --git a/app/Jobs/GenerateCalendarEvents.php b/app/Jobs/GenerateCalendarEvents.php index 27a91a8db23b..6d27b6190db7 100644 --- a/app/Jobs/GenerateCalendarEvents.php +++ b/app/Jobs/GenerateCalendarEvents.php @@ -30,6 +30,12 @@ class GenerateCalendarEvents extends Job foreach ($data as $type => $source) { if (! count($filter) || in_array($type, $filter)) { + $source->where(function($query) use ($type) { + $start = date_create(request()->start); + $end = date_create(request()->end); + return $query->dateRange($start, $end); + }); + foreach ($source->with(['account', 'client.contacts'])->get() as $entity) { if ($entity->client && $entity->client->trashed()) { continue; diff --git a/app/Models/Expense.php b/app/Models/Expense.php index a76b59cae032..1dc81d2c381e 100644 --- a/app/Models/Expense.php +++ b/app/Models/Expense.php @@ -227,6 +227,16 @@ class Expense extends EntityModel return $array; } + /** + * @param $query + * + * @return mixed + */ + public function scopeDateRange($query, $startDate, $endDate) + { + return $query->whereBetween('expense_date', [$startDate, $endDate]); + } + /** * @param $query * @param null $bankdId diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 2297a7c2634e..e8b04b147713 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -423,6 +423,20 @@ class Invoice extends EntityModel implements BalanceAffecting ->where('is_recurring', '=', true); } + /** + * @param $query + * + * @return mixed + */ + public function scopeDateRange($query, $startDate, $endDate) + { + return $query->where(function ($query) use ($startDate, $endDate) { + $query->whereBetween('invoice_date', [$startDate, $endDate]); + })->orWhere(function ($query) use ($startDate, $endDate) { + $query->whereBetween('due_date', [$startDate, $endDate]); + }); + } + /** * @param $query * diff --git a/app/Models/Payment.php b/app/Models/Payment.php index 8e644a651db8..e1556b7d6046 100644 --- a/app/Models/Payment.php +++ b/app/Models/Payment.php @@ -146,6 +146,16 @@ class Payment extends EntityModel return $query; } + /** + * @param $query + * + * @return mixed + */ + public function scopeDateRange($query, $startDate, $endDate) + { + return $query->whereBetween('payment_date', [$startDate, $endDate]); + } + /** * @return mixed */