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..6e22782a298f 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,6 +150,11 @@ class ClientService $pdf = $statement->run(); if ($send_email) { + // 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; + } + return $this->emailStatement($pdf, $statement->options); } diff --git a/app/Services/Client/Statement.php b/app/Services/Client/Statement.php index 63406638a343..4fe0ccc36fbe 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,7 +223,7 @@ class Statement * * @return Invoice[]|\Illuminate\Support\LazyCollection */ - protected function getInvoices(): \Illuminate\Support\LazyCollection + public function getInvoices(): \Illuminate\Support\LazyCollection { return Invoice::withTrashed() ->with('payments.type') diff --git a/app/Services/Scheduler/EmailStatementService.php b/app/Services/Scheduler/EmailStatementService.php index 770ffdc203a2..3ce5d5f7f7bf 100644 --- a/app/Services/Scheduler/EmailStatementService.php +++ b/app/Services/Scheduler/EmailStatementService.php @@ -16,6 +16,7 @@ use App\Models\Scheduler; use App\Utils\Traits\MakesHash; use App\DataMapper\Schedule\EmailStatement; use App\Utils\Traits\MakesDates; +use Carbon\Carbon; class EmailStatementService { @@ -46,7 +47,7 @@ class EmailStatementService $this->client = $_client; //work out the date range - $statement_properties = $this->calculateStatementProperties(); + $statement_properties = $this->calculateStatementProperties($_client); $_client->service()->statement($statement_properties, true); }); @@ -61,26 +62,27 @@ class EmailStatementService * * @return array The statement options array */ - private function calculateStatementProperties(): array + private function calculateStatementProperties(Client $client): array { - $start_end = $this->calculateStartAndEndDates(); + $start_end = $this->calculateStartAndEndDates($client); return [ - 'start_date' =>$start_end[0], - 'end_date' =>$start_end[1], + 'start_date' => $start_end[0], + 'end_date' => $start_end[1], 'show_payments_table' => $this->scheduler->parameters['show_payments_table'] ?? true, 'show_aging_table' => $this->scheduler->parameters['show_aging_table'] ?? true, 'show_credits_table' => $this->scheduler->parameters['show_credits_table'] ?? true, + 'only_clients_with_invoices' => $this->scheduler->parameters['only_clients_with_invoices'] ?? false, 'status' => $this->scheduler->parameters['status'] ]; } - + /** * Start and end date of the statement * * @return array [$start_date, $end_date]; */ - private function calculateStartAndEndDates(): array + private function calculateStartAndEndDates(Client $client): array { return match ($this->scheduler->parameters['date_range']) { EmailStatement::LAST7 => [now()->startOfDay()->subDays(7)->format('Y-m-d'), now()->startOfDay()->format('Y-m-d')], @@ -92,6 +94,11 @@ class EmailStatementService EmailStatement::LAST_QUARTER => [now()->startOfDay()->subQuarterNoOverflow()->firstOfQuarter()->format('Y-m-d'), now()->startOfDay()->subQuarterNoOverflow()->lastOfQuarter()->format('Y-m-d')], EmailStatement::THIS_YEAR => [now()->startOfDay()->firstOfYear()->format('Y-m-d'), now()->startOfDay()->lastOfYear()->format('Y-m-d')], EmailStatement::LAST_YEAR => [now()->startOfDay()->subYearNoOverflow()->firstOfYear()->format('Y-m-d'), now()->startOfDay()->subYearNoOverflow()->lastOfYear()->format('Y-m-d')], + EmailStatement::ALL_TIME => [ + Invoice::withTrashed()->where('client_id', $client->id)->selectRaw('MIN(invoices.date) as start_date')->pluck('start_date')->first() + ?: Carbon::now()->format('Y-m-d'), + Carbon::now()->format('Y-m-d') + ], EmailStatement::CUSTOM_RANGE => [$this->scheduler->parameters['start_date'], $this->scheduler->parameters['end_date']], default => [now()->startOfDay()->firstOfMonth()->format('Y-m-d'), now()->startOfDay()->lastOfMonth()->format('Y-m-d')], }; diff --git a/lang/en/texts.php b/lang/en/texts.php index f3fc5bc6be49..a51ee140b234 100644 --- a/lang/en/texts.php +++ b/lang/en/texts.php @@ -2000,6 +2000,7 @@ $LANG = array( 'current_quarter' => 'Current Quarter', 'last_quarter' => 'Last Quarter', 'last_year' => 'Last Year', + 'all_time' => 'All Time', 'custom_range' => 'Custom Range', 'url' => 'URL', 'debug' => 'Debug', @@ -4907,6 +4908,7 @@ $LANG = array( 'all_clients' => 'All Clients', 'show_aging_table' => 'Show Aging Table', 'show_payments_table' => 'Show Payments Table', + 'only_clients_with_invoices' => 'Only Clients with Invoices', 'email_statement' => 'Email Statement', 'once' => 'Once', 'schedules' => 'Schedules',