Switch "All Time" option for statements to pull the earliest invoice date.

This commit is contained in:
Joshua Dwire 2023-06-15 13:46:11 -04:00
parent 2d54c4fdb3
commit 347c4f95d6
4 changed files with 43 additions and 73 deletions

View File

@ -151,21 +151,11 @@ class ClientService
if ($send_email) { if ($send_email) {
// If selected, ignore clients that don't have any invoices to put on the statement. // 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)) { if (!empty($options['only_clients_with_invoices']) && $statement->getInvoices()->count() == 0) {
return false; return false;
} }
$options = $statement->options; return $this->emailStatement($pdf, $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; return $pdf;

View File

@ -225,23 +225,16 @@ class Statement
*/ */
public function getInvoices(): \Illuminate\Support\LazyCollection public function getInvoices(): \Illuminate\Support\LazyCollection
{ {
$query = Invoice::withTrashed() return Invoice::withTrashed()
->with('payments.type') ->with('payments.type')
->where('is_deleted', false) ->where('is_deleted', false)
->where('company_id', $this->client->company_id) ->where('company_id', $this->client->company_id)
->where('client_id', $this->client->id) ->where('client_id', $this->client->id)
->whereIn('status_id', $this->invoiceStatuses()) ->whereIn('status_id', $this->invoiceStatuses())
->whereBetween('date', [Carbon::parse($this->options['start_date']), Carbon::parse($this->options['end_date'])])
->orderBy('due_date', 'ASC') ->orderBy('due_date', 'ASC')
->orderBy('date', 'ASC'); ->orderBy('date', 'ASC')
->cursor();
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 private function invoiceStatuses() :array
@ -276,22 +269,15 @@ class Statement
*/ */
protected function getPayments(): \Illuminate\Support\LazyCollection protected function getPayments(): \Illuminate\Support\LazyCollection
{ {
$query = Payment::withTrashed() return Payment::withTrashed()
->with('client.country', 'invoices') ->with('client.country', 'invoices')
->where('is_deleted', false) ->where('is_deleted', false)
->where('company_id', $this->client->company_id) ->where('company_id', $this->client->company_id)
->where('client_id', $this->client->id) ->where('client_id', $this->client->id)
->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment::STATUS_PARTIALLY_REFUNDED, Payment::STATUS_REFUNDED]) ->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment::STATUS_PARTIALLY_REFUNDED, Payment::STATUS_REFUNDED])
->orderBy('date', 'ASC'); ->whereBetween('date', [Carbon::parse($this->options['start_date']), Carbon::parse($this->options['end_date'])])
->orderBy('date', 'ASC')
if (!empty($this->options['start_date'])) { ->cursor();
$query->whereDate('date', '>=', $this->options['start_date']);
}
if (!empty($this->options['end_date'])) {
$query->whereDate('date', '<=', $this->options['end_date']);
}
return $query->cursor();
} }
/** /**
@ -301,26 +287,19 @@ class Statement
*/ */
protected function getCredits(): \Illuminate\Support\LazyCollection protected function getCredits(): \Illuminate\Support\LazyCollection
{ {
$query = Credit::withTrashed() return Credit::withTrashed()
->with('client.country', 'invoices') ->with('client.country', 'invoices')
->where('is_deleted', false) ->where('is_deleted', false)
->where('company_id', $this->client->company_id) ->where('company_id', $this->client->company_id)
->where('client_id', $this->client->id) ->where('client_id', $this->client->id)
->whereIn('status_id', [Credit::STATUS_SENT, Credit::STATUS_PARTIAL, Credit::STATUS_APPLIED]) ->whereIn('status_id', [Credit::STATUS_SENT, Credit::STATUS_PARTIAL, Credit::STATUS_APPLIED])
->orderBy('date', 'ASC'); ->whereBetween('date', [Carbon::parse($this->options['start_date']), Carbon::parse($this->options['end_date'])])
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) { ->where(function ($query) {
$query->whereDate('due_date', '>=', $this->options['end_date']) $query->whereDate('due_date', '>=', $this->options['end_date'])
->orWhereNull('due_date'); ->orWhereNull('due_date');
}); })
} ->orderBy('date', 'ASC')
->cursor();
return $query->cursor();
} }
/** /**

View File

@ -392,26 +392,22 @@ class Design extends BaseDesign
if ($this->type === 'statement') { if ($this->type === 'statement') {
// $s_date = $this->translateDate(now(), $this->client->date_format(), $this->client->locale()); // $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());
$headerParts = [ return [
['element' => 'tr', 'properties' => ['data-ref' => 'statement-label'], 'elements' => [ ['element' => 'tr', 'properties' => ['data-ref' => 'statement-label'], 'elements' => [
['element' => 'th', 'properties' => [], 'content' => ""], ['element' => 'th', 'properties' => [], 'content' => ""],
['element' => 'th', 'properties' => [], 'content' => "<h2>" . ctrans('texts.statement') . "</h2>"], ['element' => 'th', 'properties' => [], 'content' => "<h2>".ctrans('texts.statement')."</h2>"],
]], ]],
];
if (!empty($this->options['start_date']) || !empty($this->options['end_date'])) {
$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());
$headerParts[] =
['element' => 'tr', 'properties' => [], 'elements' => [ ['element' => 'tr', 'properties' => [], 'elements' => [
['element' => 'th', 'properties' => [], 'content' => ctrans('texts.statement_date')], ['element' => 'th', 'properties' => [], 'content' => ctrans('texts.statement_date')],
['element' => 'th', 'properties' => [], 'content' => $s_date ?? ''], ['element' => 'th', 'properties' => [], 'content' => $s_date ?? ''],
]]; ]],
} ['element' => 'tr', 'properties' => [], 'elements' => [
$headerParts[] = ['element' => 'tr', 'properties' => [], 'elements' => [
['element' => 'th', 'properties' => [], 'content' => '$balance_due_label'], ['element' => 'th', 'properties' => [], 'content' => '$balance_due_label'],
['element' => 'th', 'properties' => [], 'content' => Number::formatMoney($this->invoices->sum('balance'), $this->client)], ['element' => 'th', 'properties' => [], 'content' => Number::formatMoney($this->invoices->sum('balance'), $this->client)],
]]; ]],
return $headerParts; ];
} }
$variables = $this->context['pdf_variables']['invoice_details']; $variables = $this->context['pdf_variables']['invoice_details'];

View File

@ -16,6 +16,7 @@ use App\Models\Scheduler;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
use App\DataMapper\Schedule\EmailStatement; use App\DataMapper\Schedule\EmailStatement;
use App\Utils\Traits\MakesDates; use App\Utils\Traits\MakesDates;
use Carbon\Carbon;
class EmailStatementService class EmailStatementService
{ {
@ -46,7 +47,7 @@ class EmailStatementService
$this->client = $_client; $this->client = $_client;
//work out the date range //work out the date range
$statement_properties = $this->calculateStatementProperties(); $statement_properties = $this->calculateStatementProperties($_client);
$_client->service()->statement($statement_properties, true); $_client->service()->statement($statement_properties, true);
}); });
@ -61,17 +62,17 @@ class EmailStatementService
* *
* @return array The statement options array * @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 [ return [
'start_date' =>$start_end[0], 'start_date' => $start_end[0],
'end_date' =>$start_end[1], 'end_date' => $start_end[1],
'show_payments_table' => $this->scheduler->parameters['show_payments_table'] ?? true, 'show_payments_table' => $this->scheduler->parameters['show_payments_table'] ?? true,
'show_aging_table' => $this->scheduler->parameters['show_aging_table'] ?? true, 'show_aging_table' => $this->scheduler->parameters['show_aging_table'] ?? true,
'show_credits_table' => $this->scheduler->parameters['show_credits_table'] ?? true, 'show_credits_table' => $this->scheduler->parameters['show_credits_table'] ?? true,
'only_clients_with_invoices' => $this->scheduler->parameters['only_clients_with_invoices'] ?? true, 'only_clients_with_invoices' => $this->scheduler->parameters['only_clients_with_invoices'] ?? false,
'status' => $this->scheduler->parameters['status'] 'status' => $this->scheduler->parameters['status']
]; ];
} }
@ -81,7 +82,7 @@ class EmailStatementService
* *
* @return array [$start_date, $end_date]; * @return array [$start_date, $end_date];
*/ */
private function calculateStartAndEndDates(): array private function calculateStartAndEndDates(Client $client): array
{ {
return match ($this->scheduler->parameters['date_range']) { return match ($this->scheduler->parameters['date_range']) {
EmailStatement::LAST7 => [now()->startOfDay()->subDays(7)->format('Y-m-d'), now()->startOfDay()->format('Y-m-d')], EmailStatement::LAST7 => [now()->startOfDay()->subDays(7)->format('Y-m-d'), now()->startOfDay()->format('Y-m-d')],
@ -93,7 +94,11 @@ class EmailStatementService
EmailStatement::LAST_QUARTER => [now()->startOfDay()->subQuarterNoOverflow()->firstOfQuarter()->format('Y-m-d'), now()->startOfDay()->subQuarterNoOverflow()->lastOfQuarter()->format('Y-m-d')], 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::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::LAST_YEAR => [now()->startOfDay()->subYearNoOverflow()->firstOfYear()->format('Y-m-d'), now()->startOfDay()->subYearNoOverflow()->lastOfYear()->format('Y-m-d')],
EmailStatement::ALL_TIME => [null, null], EmailStatement::ALL_TIME => [
$client->invoices()->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']], 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')], default => [now()->startOfDay()->firstOfMonth()->format('Y-m-d'), now()->startOfDay()->lastOfMonth()->format('Y-m-d')],
}; };