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 */