Improve eager loads (#3289)

* Working on invoice designs

* Working on Invoice Designs

* Improve eager loads
This commit is contained in:
David Bomba 2020-02-06 20:35:51 +11:00 committed by GitHub
parent 797c3fb3f6
commit 4f82f1b903
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 100 additions and 23 deletions

View File

@ -3,6 +3,7 @@
namespace App\Console\Commands; namespace App\Console\Commands;
use App\DataMapper\DefaultSettings; use App\DataMapper\DefaultSettings;
use App\Events\Invoice\InvoiceWasCreated;
use App\Events\Invoice\InvoiceWasMarkedSent; use App\Events\Invoice\InvoiceWasMarkedSent;
use App\Events\Payment\PaymentWasCreated; use App\Events\Payment\PaymentWasCreated;
use App\Factory\ClientFactory; use App\Factory\ClientFactory;
@ -481,6 +482,8 @@ class CreateTestData extends Command
UpdateInvoicePayment::dispatchNow($payment, $payment->company); UpdateInvoicePayment::dispatchNow($payment, $payment->company);
} }
//@todo this slow things down, but gives us PDFs of the invoices for inspection whilst debugging.
//event(new InvoiceWasCreated($invoice, $invoice->company));
} }
private function createCredit($client) private function createCredit($client)

View File

@ -36,10 +36,11 @@ class Designer
* formatted HTML * formatted HTML
* @return string The HTML design built * @return string The HTML design built
*/ */
public function build(Invoice $invoice) public function build(Invoice $invoice) :Designer
{ {
$this->setDesign($this->getSection('header')) $this->exportVariables()
->setDesign($this->getSection('header'))
->setDesign($this->getSection('body')) ->setDesign($this->getSection('body'))
->setDesign($this->getTable($invoice)) ->setDesign($this->getTable($invoice))
->setDesign($this->getSection('footer')); ->setDesign($this->getSection('footer'));
@ -82,8 +83,6 @@ class Designer
*/ */
public function getSection($section) :string public function getSection($section) :string
{ {
$this->exportVariables();
return str_replace(array_keys($this->exported_variables), array_values($this->exported_variables), $this->design->{$section}()); return str_replace(array_keys($this->exported_variables), array_values($this->exported_variables), $this->design->{$section}());
} }

File diff suppressed because one or more lines are too long

View File

@ -172,7 +172,7 @@ class LoginController extends BaseController
$user->setCompany($user->company_user->account->default_company); $user->setCompany($user->company_user->account->default_company);
$ct = CompanyUser::whereUserId($user->id); $ct = CompanyUser::whereUserId($user->id)->with('company');
return $this->listResponse($ct); return $this->listResponse($ct);
} else { } else {
$this->incrementLoginAttempts($request); $this->incrementLoginAttempts($request);

View File

@ -49,8 +49,8 @@ class QueryLogging
$time = $timeEnd - $timeStart; $time = $timeEnd - $timeStart;
Log::info($request->method() . ' - ' . $request->url() . ": $count queries - " . $time); Log::info($request->method() . ' - ' . $request->url() . ": $count queries - " . $time);
// if($count > 50) if($count > 50)
// Log::info($queries); Log::info($queries);
} }
} }

View File

@ -94,9 +94,10 @@ class Client extends BaseModel
protected $with = [ protected $with = [
//'currency', //'currency',
// 'primary_contact', // 'primary_contact',
'country', // 'country',
'contacts',
// 'shipping_country', // 'shipping_country',
// 'company' // 'company',
]; ];
protected $casts = [ protected $casts = [

View File

@ -51,8 +51,8 @@ class ClientContact extends Authenticatable implements HasLocalePreference
]; ];
protected $with = [ protected $with = [
'client', // 'client',
'company' // 'company'
]; ];
protected $casts = [ protected $casts = [

View File

@ -30,7 +30,7 @@ class CreditInvitation extends BaseModel
]; ];
protected $with = [ protected $with = [
'company', // 'company',
]; ];
public function entityType() public function entityType()

View File

@ -96,8 +96,8 @@ class Invoice extends BaseModel
]; ];
protected $with = [ protected $with = [
'company', // 'company',
'client', // 'client',
]; ];
protected $appends = [ protected $appends = [

View File

@ -30,7 +30,7 @@ class InvoiceInvitation extends BaseModel
]; ];
protected $with = [ protected $with = [
'company', // 'company',
]; ];
public function entityType() public function entityType()

View File

@ -71,6 +71,10 @@ class Payment extends BaseModel
'is_deleted' => 'bool', 'is_deleted' => 'bool',
]; ];
protected $with = [
'paymentables',
];
public function client() public function client()
{ {
return $this->belongsTo(Client::class)->withTrashed(); return $this->belongsTo(Client::class)->withTrashed();

View File

@ -53,6 +53,10 @@ class Vendor extends BaseModel
'deleted_at' => 'timestamp', 'deleted_at' => 'timestamp',
]; ];
protected $with = [
'contacts',
];
public function documents() public function documents()
{ {
return $this->morphMany(Document::class, 'documentable'); return $this->morphMany(Document::class, 'documentable');

View File

@ -49,8 +49,8 @@ class VendorContact extends Authenticatable implements HasLocalePreference
]; ];
protected $with = [ protected $with = [
'vendor', // 'vendor',
'company' // 'company'
]; ];
protected $casts = [ protected $casts = [

View File

@ -33,8 +33,6 @@ trait MakesInvoiceHtml
{ {
$variables = array_merge($invoice->makeLabels(), $invoice->makeValues()); $variables = array_merge($invoice->makeLabels(), $invoice->makeValues());
// uasort($variables, 'arraySort');
$design = str_replace(array_keys($variables), array_values($variables), $design); $design = str_replace(array_keys($variables), array_values($variables), $design);
$data['invoice'] = $invoice; $data['invoice'] = $invoice;

View File

@ -140,6 +140,8 @@ trait MakesInvoiceValues
*/ */
public function makeLabels() :array public function makeLabels() :array
{ {
$custom_fields = $this->company->custom_fields;
//todo we might want to translate like this //todo we might want to translate like this
//trans('texts.labe', [], null, $this->client->locale()); //trans('texts.labe', [], null, $this->client->locale());
$data = []; $data = [];
@ -148,6 +150,9 @@ trait MakesInvoiceValues
$data['$'.$label . '_label'] = ctrans('texts.'.$label); $data['$'.$label . '_label'] = ctrans('texts.'.$label);
} }
if(property_exists($custom_fields,'invoice_text1'))
$data['$invoice_text1'] = $custom_fields->invoice_text1;
return $data; return $data;
} }
@ -169,6 +174,11 @@ trait MakesInvoiceValues
$data = []; $data = [];
$data['$total_tax_labels'] = $this->totalTaxLabels();
$data['$total_tax_values'] = $this->totalTaxValues();
$data['$line_tax_labels'] = $this->lineTaxLabels();
$data['$line_tax_values'] = $this->lineTaxValues();
$data['$date'] = $this->date; $data['$date'] = $this->date;
$data['$invoice.date'] = &$data['$date']; $data['$invoice.date'] = &$data['$date'];
$data['$due_date'] = $this->due_date; $data['$due_date'] = $this->due_date;
@ -402,7 +412,7 @@ trait MakesInvoiceValues
foreach ($items as $item) { foreach ($items as $item) {
$table_body = '<tr class="">'; $table_body .= '<tr class="">';
foreach ($columns as $column) { foreach ($columns as $column) {
$table_body .= '<td class="'.$css['table_body_td_class'].'">'. $item->{$column} . '</td>'; $table_body .= '<td class="'.$css['table_body_td_class'].'">'. $item->{$column} . '</td>';
@ -540,4 +550,58 @@ trait MakesInvoiceValues
return $data; return $data;
} }
private function totalTaxLabels() :string
{
$data = '';
if (!$this->calc()->getTotalTaxMap()) {
return $data;
}
foreach ($this->calc()->getTotalTaxMap() as $tax) {
$data .= '<span>'. $tax['name'] .'</span>';
}
return $data;
}
private function totalTaxValues() :string
{
$data = '';
if (!$this->calc()->getTotalTaxMap()) {
return $data;
}
foreach ($this->calc()->getTotalTaxMap() as $tax) {
$data .= '<span>'. Number::formatMoney($tax['total'], $this->client) .'</span>';
}
return $data;
}
private function lineTaxLabels() :string
{
$tax_map = $this->calc()->getTaxMap();
$data = '';
foreach ($tax_map as $tax)
$data .= '<span>'. $tax['name'] .'</span>';
return $data;
}
private function lineTaxValues() :string
{
$tax_map = $this->calc()->getTaxMap();
$data = '';
foreach ($tax_map as $tax)
$data .= '<span>'. Number::formatMoney($tax['total'], $this->client) .'</span>';
return $data;
}
} }