diff --git a/app/DataMapper/Schedule/EmailStatement.php b/app/DataMapper/Schedule/EmailStatement.php index b9ece221c7ee..6f5499fdf7c3 100644 --- a/app/DataMapper/Schedule/EmailStatement.php +++ b/app/DataMapper/Schedule/EmailStatement.php @@ -41,6 +41,7 @@ class EmailStatement public const LAST_QUARTER = "last_quarter"; public const THIS_YEAR = "this_year"; public const LAST_YEAR = "last_year"; + public const ALL_TIME = "all_time"; public const CUSTOM_RANGE = "custom"; diff --git a/app/Http/Requests/TaskScheduler/StoreSchedulerRequest.php b/app/Http/Requests/TaskScheduler/StoreSchedulerRequest.php index 47156bf84a48..1c132dbb9b06 100644 --- a/app/Http/Requests/TaskScheduler/StoreSchedulerRequest.php +++ b/app/Http/Requests/TaskScheduler/StoreSchedulerRequest.php @@ -40,7 +40,7 @@ class StoreSchedulerRequest extends Request 'template' => 'bail|required|string', 'parameters' => 'bail|array', 'parameters.clients' => ['bail','sometimes', 'array', new ValidClientIds()], - 'parameters.date_range' => 'bail|sometimes|string|in:last7_days,last30_days,last365_days,this_month,last_month,this_quarter,last_quarter,this_year,last_year,custom', + 'parameters.date_range' => 'bail|sometimes|string|in:last7_days,last30_days,last365_days,this_month,last_month,this_quarter,last_quarter,this_year,last_year,all_time,custom', 'parameters.start_date' => ['bail', 'sometimes', 'date:Y-m-d', 'required_if:parameters.date_rate,custom'], 'parameters.end_date' => ['bail', 'sometimes', 'date:Y-m-d', 'required_if:parameters.date_rate,custom', 'after_or_equal:parameters.start_date'], 'parameters.entity' => ['bail', 'sometimes', 'string', 'in:invoice,credit,quote,purchase_order'], diff --git a/app/Http/Requests/TaskScheduler/UpdateSchedulerRequest.php b/app/Http/Requests/TaskScheduler/UpdateSchedulerRequest.php index 775b45db5603..74d011722ca2 100644 --- a/app/Http/Requests/TaskScheduler/UpdateSchedulerRequest.php +++ b/app/Http/Requests/TaskScheduler/UpdateSchedulerRequest.php @@ -37,7 +37,7 @@ class UpdateSchedulerRequest extends Request 'template' => 'bail|required|string', 'parameters' => 'bail|array', 'parameters.clients' => ['bail','sometimes', 'array', new ValidClientIds()], - 'parameters.date_range' => 'bail|sometimes|string|in:last7_days,last30_days,last365_days,this_month,last_month,this_quarter,last_quarter,this_year,last_year,custom', + 'parameters.date_range' => 'bail|sometimes|string|in:last7_days,last30_days,last365_days,this_month,last_month,this_quarter,last_quarter,this_year,last_year,all_time,custom', 'parameters.start_date' => ['bail', 'sometimes', 'date:Y-m-d', 'required_if:parameters.date_rate,custom'], 'parameters.end_date' => ['bail', 'sometimes', 'date:Y-m-d', 'required_if:parameters.date_rate,custom', 'after_or_equal:parameters.start_date'], 'parameters.entity' => ['bail', 'sometimes', 'string', 'in:invoice,credit,quote,purchase_order'], diff --git a/app/Services/Client/ClientService.php b/app/Services/Client/ClientService.php index 6492bdde5f89..a30cccd16531 100644 --- a/app/Services/Client/ClientService.php +++ b/app/Services/Client/ClientService.php @@ -18,6 +18,7 @@ use App\Services\Email\Email; use App\Services\Email\EmailObject; use App\Utils\Number; use App\Utils\Traits\MakesDates; +use Carbon\Carbon; use Illuminate\Mail\Mailables\Address; use Illuminate\Support\Facades\DB; @@ -149,7 +150,22 @@ class ClientService $pdf = $statement->run(); if ($send_email) { - return $this->emailStatement($pdf, $statement->options); + // If selected, ignore clients that don't have any invoices to put on the statement. + if (!empty($options['only_clients_with_invoices'] && $statement->getInvoices()->count() == 0)) { + return false; + } + + $options = $statement->options; + + if (empty($options['start_date'])) { + $options['start_date'] = $statement->getInvoices()->first()->date; + } + + if (empty($options['end_date'])) { + $options['end_date'] = Carbon::now(); + } + + return $this->emailStatement($pdf, $options); } return $pdf; diff --git a/app/Services/Client/Statement.php b/app/Services/Client/Statement.php index 63406638a343..8894ff7208a8 100644 --- a/app/Services/Client/Statement.php +++ b/app/Services/Client/Statement.php @@ -211,6 +211,9 @@ class Statement $this->options['show_credits_table'] = false; } + if (!\array_key_exists('only_clients_with_invoices', $this->options)) { + $this->options['only_clients_with_invoices'] = false; + } return $this; } @@ -220,18 +223,25 @@ class Statement * * @return Invoice[]|\Illuminate\Support\LazyCollection */ - protected function getInvoices(): \Illuminate\Support\LazyCollection + public function getInvoices(): \Illuminate\Support\LazyCollection { - return Invoice::withTrashed() + $query = Invoice::withTrashed() ->with('payments.type') ->where('is_deleted', false) ->where('company_id', $this->client->company_id) ->where('client_id', $this->client->id) ->whereIn('status_id', $this->invoiceStatuses()) - ->whereBetween('date', [Carbon::parse($this->options['start_date']), Carbon::parse($this->options['end_date'])]) ->orderBy('due_date', 'ASC') - ->orderBy('date', 'ASC') - ->cursor(); + ->orderBy('date', 'ASC'); + + if (!empty($this->options['start_date'])) { + $query->whereDate('date', '>=', $this->options['start_date']); + } + if (!empty($this->options['end_date'])) { + $query->whereDate('date', '<=', $this->options['end_date']); + } + + return $query->cursor(); } private function invoiceStatuses() :array @@ -266,15 +276,22 @@ class Statement */ protected function getPayments(): \Illuminate\Support\LazyCollection { - return Payment::withTrashed() + $query = Payment::withTrashed() ->with('client.country', 'invoices') ->where('is_deleted', false) ->where('company_id', $this->client->company_id) ->where('client_id', $this->client->id) ->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment::STATUS_PARTIALLY_REFUNDED, Payment::STATUS_REFUNDED]) - ->whereBetween('date', [Carbon::parse($this->options['start_date']), Carbon::parse($this->options['end_date'])]) - ->orderBy('date', 'ASC') - ->cursor(); + ->orderBy('date', 'ASC'); + + if (!empty($this->options['start_date'])) { + $query->whereDate('date', '>=', $this->options['start_date']); + } + if (!empty($this->options['end_date'])) { + $query->whereDate('date', '<=', $this->options['end_date']); + } + + return $query->cursor(); } /** @@ -284,19 +301,26 @@ class Statement */ protected function getCredits(): \Illuminate\Support\LazyCollection { - return Credit::withTrashed() + $query = Credit::withTrashed() ->with('client.country', 'invoices') ->where('is_deleted', false) ->where('company_id', $this->client->company_id) ->where('client_id', $this->client->id) ->whereIn('status_id', [Credit::STATUS_SENT, Credit::STATUS_PARTIAL, Credit::STATUS_APPLIED]) - ->whereBetween('date', [Carbon::parse($this->options['start_date']), Carbon::parse($this->options['end_date'])]) - ->where(function ($query) { - $query->whereDate('due_date', '>=', $this->options['end_date']) - ->orWhereNull('due_date'); - }) - ->orderBy('date', 'ASC') - ->cursor(); + ->orderBy('date', 'ASC'); + + if (!empty($this->options['start_date'])) { + $query->whereDate('date', '>=', $this->options['start_date']); + } + if (!empty($this->options['end_date'])) { + $query->whereDate('date', '<=', $this->options['end_date']) + ->where(function ($query) { + $query->whereDate('due_date', '>=', $this->options['end_date']) + ->orWhereNull('due_date'); + }); + } + + return $query->cursor(); } /** diff --git a/app/Services/PdfMaker/Design.php b/app/Services/PdfMaker/Design.php index 0ca138f12823..9568444c7d65 100644 --- a/app/Services/PdfMaker/Design.php +++ b/app/Services/PdfMaker/Design.php @@ -391,23 +391,27 @@ class Design extends BaseDesign { if ($this->type === 'statement') { // $s_date = $this->translateDate(now(), $this->client->date_format(), $this->client->locale()); - - $s_date = $this->translateDate($this->options['start_date'], $this->client->date_format(), $this->client->locale()) . " - " . $this->translateDate($this->options['end_date'], $this->client->date_format(), $this->client->locale()); - return [ + + $headerParts = [ ['element' => 'tr', 'properties' => ['data-ref' => 'statement-label'], 'elements' => [ ['element' => 'th', 'properties' => [], 'content' => ""], - ['element' => 'th', 'properties' => [], 'content' => "