diff --git a/app/Services/Client/ClientService.php b/app/Services/Client/ClientService.php index a30cccd16531..6e22782a298f 100644 --- a/app/Services/Client/ClientService.php +++ b/app/Services/Client/ClientService.php @@ -151,21 +151,11 @@ class ClientService 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)) { + 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 $this->emailStatement($pdf, $statement->options); } return $pdf; diff --git a/app/Services/Client/Statement.php b/app/Services/Client/Statement.php index 8894ff7208a8..4fe0ccc36fbe 100644 --- a/app/Services/Client/Statement.php +++ b/app/Services/Client/Statement.php @@ -225,23 +225,16 @@ class Statement */ public function getInvoices(): \Illuminate\Support\LazyCollection { - $query = Invoice::withTrashed() + return 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'); - - 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(); + ->orderBy('date', 'ASC') + ->cursor(); } private function invoiceStatuses() :array @@ -276,22 +269,15 @@ class Statement */ protected function getPayments(): \Illuminate\Support\LazyCollection { - $query = Payment::withTrashed() + return 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]) - ->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(); + ->whereBetween('date', [Carbon::parse($this->options['start_date']), Carbon::parse($this->options['end_date'])]) + ->orderBy('date', 'ASC') + ->cursor(); } /** @@ -301,26 +287,19 @@ class Statement */ protected function getCredits(): \Illuminate\Support\LazyCollection { - $query = Credit::withTrashed() + return 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]) - ->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(); + ->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(); } /** diff --git a/app/Services/PdfMaker/Design.php b/app/Services/PdfMaker/Design.php index 9568444c7d65..0ca138f12823 100644 --- a/app/Services/PdfMaker/Design.php +++ b/app/Services/PdfMaker/Design.php @@ -391,27 +391,23 @@ 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()); - - $headerParts = [ + return [ ['element' => 'tr', 'properties' => ['data-ref' => 'statement-label'], 'elements' => [ ['element' => 'th', 'properties' => [], 'content' => ""], - ['element' => 'th', 'properties' => [], 'content' => "