mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-10 13:12:50 +01:00
Fixes for N+1 queries in Client Statements
This commit is contained in:
parent
e8f79e54f5
commit
af8c56a1f1
@ -51,6 +51,6 @@ class CreateStatementRequest extends Request
|
||||
|
||||
public function client(): ?Client
|
||||
{
|
||||
return Client::where('id', $this->client_id)->first();
|
||||
return Client::with('company')->where('id', $this->client_id)->withTrashed()->first();
|
||||
}
|
||||
}
|
||||
|
@ -73,7 +73,7 @@ class Statement
|
||||
|
||||
$state = [
|
||||
'template' => $template->elements([
|
||||
'client' => $this->entity->client,
|
||||
'client' => $this->client,
|
||||
'entity' => $this->entity,
|
||||
'pdf_variables' => (array)$this->entity->company->settings->pdf_variables,
|
||||
'$product' => $this->getDesign()->design->product,
|
||||
@ -219,7 +219,7 @@ class Statement
|
||||
*/
|
||||
protected function getInvoices(): Collection
|
||||
{
|
||||
return Invoice::where('company_id', $this->client->company->id)
|
||||
return Invoice::where('company_id', $this->client->company_id)
|
||||
->where('client_id', $this->client->id)
|
||||
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL, Invoice::STATUS_PAID])
|
||||
->whereBetween('date', [$this->options['start_date'], $this->options['end_date']])
|
||||
@ -234,7 +234,8 @@ class Statement
|
||||
*/
|
||||
protected function getPayments(): Collection
|
||||
{
|
||||
return Payment::where('company_id', $this->client->company->id)
|
||||
return Payment::with('client.country','invoices')
|
||||
->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', [$this->options['start_date'], $this->options['end_date']])
|
||||
@ -285,10 +286,7 @@ class Statement
|
||||
$from = $ranges[0];
|
||||
$to = $ranges[1];
|
||||
|
||||
$client = Client::where('id', $this->client->id)->first();
|
||||
|
||||
$amount = Invoice::where('company_id', $this->client->company->id)
|
||||
->where('client_id', $client->id)
|
||||
$amount = Invoice::where('client_id', $this->client->id)
|
||||
->where('company_id', $this->client->company_id)
|
||||
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
|
||||
->where('balance', '>', 0)
|
||||
@ -296,7 +294,7 @@ class Statement
|
||||
->whereBetween('date', [$to, $from])
|
||||
->sum('balance');
|
||||
|
||||
return Number::formatMoney($amount, $client);
|
||||
return Number::formatMoney($amount, $this->client);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -195,15 +195,15 @@ class Design extends BaseDesign
|
||||
if ($this->type == self::DELIVERY_NOTE) {
|
||||
$elements = [
|
||||
['element' => 'p', 'content' => ctrans('texts.delivery_note'), 'properties' => ['data-ref' => 'delivery_note-label', 'style' => 'font-weight: bold; text-transform: uppercase']],
|
||||
['element' => 'p', 'content' => $this->entity->client->name, 'show_empty' => false, 'properties' => ['data-ref' => 'delivery_note-client.name']],
|
||||
['element' => 'p', 'content' => $this->entity->client->shipping_address1, 'show_empty' => false, 'properties' => ['data-ref' => 'delivery_note-client.shipping_address1']],
|
||||
['element' => 'p', 'content' => $this->entity->client->shipping_address2, 'show_empty' => false, 'properties' => ['data-ref' => 'delivery_note-client.shipping_address2']],
|
||||
['element' => 'p', 'content' => $this->client->name, 'show_empty' => false, 'properties' => ['data-ref' => 'delivery_note-client.name']],
|
||||
['element' => 'p', 'content' => $this->client->shipping_address1, 'show_empty' => false, 'properties' => ['data-ref' => 'delivery_note-client.shipping_address1']],
|
||||
['element' => 'p', 'content' => $this->client->shipping_address2, 'show_empty' => false, 'properties' => ['data-ref' => 'delivery_note-client.shipping_address2']],
|
||||
['element' => 'p', 'show_empty' => false, 'elements' => [
|
||||
['element' => 'span', 'content' => "{$this->entity->client->shipping_city} ", 'properties' => ['ref' => 'delivery_note-client.shipping_city']],
|
||||
['element' => 'span', 'content' => "{$this->entity->client->shipping_state} ", 'properties' => ['ref' => 'delivery_note-client.shipping_state']],
|
||||
['element' => 'span', 'content' => "{$this->entity->client->shipping_postal_code} ", 'properties' => ['ref' => 'delivery_note-client.shipping_postal_code']],
|
||||
['element' => 'span', 'content' => "{$this->client->shipping_city} ", 'properties' => ['ref' => 'delivery_note-client.shipping_city']],
|
||||
['element' => 'span', 'content' => "{$this->client->shipping_state} ", 'properties' => ['ref' => 'delivery_note-client.shipping_state']],
|
||||
['element' => 'span', 'content' => "{$this->client->shipping_postal_code} ", 'properties' => ['ref' => 'delivery_note-client.shipping_postal_code']],
|
||||
]],
|
||||
['element' => 'p', 'content' => optional($this->entity->client->shipping_country)->name, 'show_empty' => false],
|
||||
['element' => 'p', 'content' => optional($this->client->shipping_country)->name, 'show_empty' => false],
|
||||
];
|
||||
|
||||
if (!is_null($this->context['contact'])) {
|
||||
@ -232,7 +232,7 @@ class Design extends BaseDesign
|
||||
]],
|
||||
['element' => 'tr', 'properties' => [], 'elements' => [
|
||||
['element' => 'th', 'properties' => [], 'content' => '$balance_due_label'],
|
||||
['element' => 'th', 'properties' => [], 'content' => Number::formatMoney($this->invoices->sum('balance'), $this->entity->client)],
|
||||
['element' => 'th', 'properties' => [], 'content' => Number::formatMoney($this->invoices->sum('balance'), $this->client)],
|
||||
]],
|
||||
];
|
||||
}
|
||||
@ -363,10 +363,10 @@ class Design extends BaseDesign
|
||||
$element = ['element' => 'tr', 'elements' => []];
|
||||
|
||||
$element['elements'][] = ['element' => 'td', 'content' => $invoice->number];
|
||||
$element['elements'][] = ['element' => 'td', 'content' => $this->translateDate($invoice->date, $invoice->client->date_format(), $invoice->client->locale()) ?: ' '];
|
||||
$element['elements'][] = ['element' => 'td', 'content' => $this->translateDate($invoice->due_date, $invoice->client->date_format(), $invoice->client->locale()) ?: ' '];
|
||||
$element['elements'][] = ['element' => 'td', 'content' => Number::formatMoney($invoice->amount, $invoice->client) ?: ' '];
|
||||
$element['elements'][] = ['element' => 'td', 'content' => Number::formatMoney($invoice->balance, $invoice->client) ?: ' '];
|
||||
$element['elements'][] = ['element' => 'td', 'content' => $this->translateDate($invoice->date, $this->client->date_format(), $this->client->locale()) ?: ' '];
|
||||
$element['elements'][] = ['element' => 'td', 'content' => $this->translateDate($invoice->due_date, $this->client->date_format(), $this->client->locale()) ?: ' '];
|
||||
$element['elements'][] = ['element' => 'td', 'content' => Number::formatMoney($invoice->amount, $this->client) ?: ' '];
|
||||
$element['elements'][] = ['element' => 'td', 'content' => Number::formatMoney($invoice->balance, $this->client) ?: ' '];
|
||||
|
||||
$tbody[] = $element;
|
||||
}
|
||||
@ -386,7 +386,7 @@ class Design extends BaseDesign
|
||||
$outstanding = $this->invoices->sum('balance');
|
||||
|
||||
return [
|
||||
['element' => 'p', 'content' => '$outstanding_label: ' . Number::formatMoney($outstanding, $this->entity->client)],
|
||||
['element' => 'p', 'content' => '$outstanding_label: ' . Number::formatMoney($outstanding, $this->client)],
|
||||
];
|
||||
}
|
||||
|
||||
@ -412,9 +412,9 @@ class Design extends BaseDesign
|
||||
$element = ['element' => 'tr', 'elements' => []];
|
||||
|
||||
$element['elements'][] = ['element' => 'td', 'content' => $invoice->number];
|
||||
$element['elements'][] = ['element' => 'td', 'content' => $this->translateDate($payment->date, $payment->client->date_format(), $payment->client->locale()) ?: ' '];
|
||||
$element['elements'][] = ['element' => 'td', 'content' => $this->translateDate($payment->date, $this->client->date_format(), $this->client->locale()) ?: ' '];
|
||||
$element['elements'][] = ['element' => 'td', 'content' => $payment->type ? $payment->type->name : ctrans('texts.manual_entry')];
|
||||
$element['elements'][] = ['element' => 'td', 'content' => Number::formatMoney($payment->amount, $payment->client) ?: ' '];
|
||||
$element['elements'][] = ['element' => 'td', 'content' => Number::formatMoney($payment->amount, $this->client) ?: ' '];
|
||||
|
||||
$tbody[] = $element;
|
||||
}
|
||||
@ -439,7 +439,7 @@ class Design extends BaseDesign
|
||||
$payment = $this->payments->first();
|
||||
|
||||
return [
|
||||
['element' => 'p', 'content' => \sprintf('%s: %s', ctrans('texts.amount_paid'), Number::formatMoney($this->payments->sum('amount'), $payment->client))],
|
||||
['element' => 'p', 'content' => \sprintf('%s: %s', ctrans('texts.amount_paid'), Number::formatMoney($this->payments->sum('amount'), $this->client))],
|
||||
];
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user