1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

Merge pull request #8556 from joshuadwire/v5-develop

Add "All Time" to scheduled statements date range; allow excluding clients with no matching statements
This commit is contained in:
David Bomba 2023-06-16 04:04:00 +00:00 committed by GitHub
commit db38f32c25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 29 additions and 10 deletions

View File

@ -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";

View File

@ -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'],

View File

@ -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'],

View File

@ -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);
}

View File

@ -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')

View File

@ -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')],
};

View File

@ -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',