mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Refactor for data providers for templates
This commit is contained in:
parent
9231dab999
commit
72e699a045
@ -63,7 +63,7 @@ class InvoiceItemFactory
|
||||
$item->line_total = $item->quantity * $item->cost;
|
||||
$item->is_amount_discount = true;
|
||||
$item->discount = $faker->numberBetween(1, 10);
|
||||
$item->notes = $faker->realText(50);
|
||||
$item->notes = str_replace(['"',"'"],['',""], $faker->realText(20));
|
||||
$item->product_key = $faker->word();
|
||||
// $item->custom_value1 = $faker->realText(10);
|
||||
// $item->custom_value2 = $faker->realText(10);
|
||||
|
File diff suppressed because one or more lines are too long
@ -13,6 +13,7 @@ namespace App\Services\Template;
|
||||
|
||||
use App\Models\Task;
|
||||
use App\Models\Quote;
|
||||
use App\Utils\Number;
|
||||
use App\Models\Credit;
|
||||
use App\Models\Design;
|
||||
use App\Models\Company;
|
||||
@ -24,6 +25,7 @@ use App\Models\ClientContact;
|
||||
use App\Models\PurchaseOrder;
|
||||
use App\Utils\VendorHtmlEngine;
|
||||
use App\Utils\PaymentHtmlEngine;
|
||||
use App\Utils\Traits\MakesDates;
|
||||
use Twig\Extra\Intl\IntlExtension;
|
||||
use App\Transformers\TaskTransformer;
|
||||
use App\Transformers\QuoteTransformer;
|
||||
@ -37,7 +39,8 @@ use League\Fractal\Serializer\JsonApiSerializer;
|
||||
|
||||
class TemplateService
|
||||
{
|
||||
|
||||
use MakesDates;
|
||||
|
||||
private \DomDocument $document;
|
||||
|
||||
public \Twig\Environment $twig;
|
||||
@ -99,6 +102,7 @@ class TemplateService
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function mock(): self
|
||||
{
|
||||
$tm = new TemplateMock($this->company);
|
||||
@ -107,6 +111,8 @@ class TemplateService
|
||||
$this->data = $tm->engines;
|
||||
$this->variables = $tm->variables[0];
|
||||
|
||||
nlog($this->data);
|
||||
|
||||
$this->parseNinjaBlocks()
|
||||
->parseVariables();
|
||||
|
||||
@ -162,7 +168,7 @@ class TemplateService
|
||||
|
||||
/**
|
||||
* Parses all variables in the document
|
||||
* @param array $data
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
private function parseVariables(): self
|
||||
@ -293,6 +299,79 @@ class TemplateService
|
||||
}
|
||||
|
||||
public function processInvoices($invoices): array
|
||||
{
|
||||
$invoices = collect($invoices)
|
||||
->map(function ($invoice){
|
||||
|
||||
$payments = $invoice->payments->map(function ($payment) {
|
||||
// nlog(microtime(true));
|
||||
return $this->transformPayment($payment);
|
||||
})->toArray();
|
||||
|
||||
return [
|
||||
'amount' => Number::formatMoney($invoice->amount, $invoice->client),
|
||||
'balance' => Number::formatMoney($invoice->balance, $invoice->client),
|
||||
'balance_raw' => $invoice->balance,
|
||||
'number' => $invoice->number ?: '',
|
||||
'discount' => $invoice->discount,
|
||||
'po_number' => $invoice->po_number ?: '',
|
||||
'date' => $this->translateDate($invoice->date, $invoice->client->date_format(), $invoice->client->locale()),
|
||||
'last_sent_date' => $this->translateDate($invoice->last_sent_date, $invoice->client->date_format(), $invoice->client->locale()),
|
||||
'next_send_date' => $this->translateDate($invoice->next_send_date, $invoice->client->date_format(), $invoice->client->locale()),
|
||||
'due_date' => $this->translateDate($invoice->due_date, $invoice->client->date_format(), $invoice->client->locale()),
|
||||
'terms' => $invoice->terms ?: '',
|
||||
'public_notes' => $invoice->public_notes ?: '',
|
||||
'private_notes' => $invoice->private_notes ?: '',
|
||||
'uses_inclusive_taxes' => (bool) $invoice->uses_inclusive_taxes,
|
||||
'tax_name1' => $invoice->tax_name1 ?? '',
|
||||
'tax_rate1' => (float) $invoice->tax_rate1,
|
||||
'tax_name2' => $invoice->tax_name2 ?? '',
|
||||
'tax_rate2' => (float) $invoice->tax_rate2,
|
||||
'tax_name3' => $invoice->tax_name3 ?? '',
|
||||
'tax_rate3' => (float) $invoice->tax_rate3,
|
||||
'total_taxes' => Number::formatMoney($invoice->total_taxes, $invoice->client),
|
||||
'total_taxes_raw' => $invoice->total_taxes,
|
||||
'is_amount_discount' => (bool) $invoice->is_amount_discount ?? false,
|
||||
'footer' => $invoice->footer ?? '',
|
||||
'partial' => $invoice->partial ?? 0,
|
||||
'partial_due_date' => $this->translateDate($invoice->partial_due_date, $invoice->client->date_format(), $invoice->client->locale()),
|
||||
'custom_value1' => (string) $invoice->custom_value1 ?: '',
|
||||
'custom_value2' => (string) $invoice->custom_value2 ?: '',
|
||||
'custom_value3' => (string) $invoice->custom_value3 ?: '',
|
||||
'custom_value4' => (string) $invoice->custom_value4 ?: '',
|
||||
'custom_surcharge1' => (float) $invoice->custom_surcharge1,
|
||||
'custom_surcharge2' => (float) $invoice->custom_surcharge2,
|
||||
'custom_surcharge3' => (float) $invoice->custom_surcharge3,
|
||||
'custom_surcharge4' => (float) $invoice->custom_surcharge4,
|
||||
'exchange_rate' => (float) $invoice->exchange_rate,
|
||||
'custom_surcharge_tax1' => (bool) $invoice->custom_surcharge_tax1,
|
||||
'custom_surcharge_tax2' => (bool) $invoice->custom_surcharge_tax2,
|
||||
'custom_surcharge_tax3' => (bool) $invoice->custom_surcharge_tax3,
|
||||
'custom_surcharge_tax4' => (bool) $invoice->custom_surcharge_tax4,
|
||||
'line_items' => $invoice->line_items ?: (array) [],
|
||||
'reminder1_sent' => $this->translateDate($invoice->reminder1_sent, $invoice->client->date_format(), $invoice->client->locale()),
|
||||
'reminder2_sent' => $this->translateDate($invoice->reminder2_sent, $invoice->client->date_format(), $invoice->client->locale()),
|
||||
'reminder3_sent' => $this->translateDate($invoice->reminder3_sent, $invoice->client->date_format(), $invoice->client->locale()),
|
||||
'reminder_last_sent' => $this->translateDate($invoice->reminder_last_sent, $invoice->client->date_format(), $invoice->client->locale()),
|
||||
'paid_to_date' => Number::formatMoney($invoice->paid_to_date, $invoice->client),
|
||||
'auto_bill_enabled' => (bool) $invoice->auto_bill_enabled,
|
||||
'client' => [
|
||||
'name' => $invoice->client->present()->name(),
|
||||
'balance' => $invoice->client->balance,
|
||||
'payment_balance' => $invoice->client->payment_balance,
|
||||
'credit_balance' => $invoice->client->credit_balance,
|
||||
],
|
||||
'payments' => $payments,
|
||||
];
|
||||
|
||||
});
|
||||
|
||||
nlog($invoices->count());
|
||||
return $invoices->toArray();
|
||||
|
||||
}
|
||||
|
||||
public function processInvoicesBak($invoices): array
|
||||
{
|
||||
$it = new InvoiceTransformer();
|
||||
$it->setDefaultIncludes(['client','payments', 'credits']);
|
||||
@ -321,6 +400,81 @@ class TemplateService
|
||||
return $invoices['data'];
|
||||
}
|
||||
|
||||
private function transformPayment(Payment $payment): array
|
||||
{
|
||||
|
||||
$data = [];
|
||||
|
||||
$credits = $payment->credits->map(function ($credit) use ($payment) {
|
||||
return [
|
||||
'credit' => $credit->number,
|
||||
'amount_raw' => $credit->pivot->amount,
|
||||
'refunded_raw' => $credit->pivot->refunded,
|
||||
'net_raw' => $credit->pivot->amount - $credit->pivot->refunded,
|
||||
'amount' => Number::formatMoney($credit->pivot->amount, $payment->client),
|
||||
'refunded' => Number::formatMoney($credit->pivot->refunded, $payment->client),
|
||||
'net' => Number::formatMoney($credit->pivot->amount - $credit->pivot->refunded, $payment->client),
|
||||
'is_credit' => true,
|
||||
'created_at' => $this->translateDate($credit->pivot->created_at, $payment->client->date_format(), $payment->client->locale()),
|
||||
'updated_at' => $this->translateDate($credit->pivot->updated_at, $payment->client->date_format(), $payment->client->locale()),
|
||||
'timestamp' => $credit->pivot->created_at->timestamp,
|
||||
];
|
||||
});
|
||||
|
||||
$pivot = $payment->invoices->map(function ($invoice) use ($payment) {
|
||||
return [
|
||||
'invoice' => $invoice->number,
|
||||
'amount_raw' => $invoice->pivot->amount,
|
||||
'refunded_raw' => $invoice->pivot->refunded,
|
||||
'net_raw' => $invoice->pivot->amount - $invoice->pivot->refunded,
|
||||
'amount' => Number::formatMoney($invoice->pivot->amount, $payment->client),
|
||||
'refunded' => Number::formatMoney($invoice->pivot->refunded, $payment->client),
|
||||
'net' => Number::formatMoney($invoice->pivot->amount - $invoice->pivot->refunded, $payment->client),
|
||||
'is_credit' => false,
|
||||
'created_at' => $this->translateDate($invoice->pivot->created_at, $payment->client->date_format(), $payment->client->locale()),
|
||||
'updated_at' => $this->translateDate($invoice->pivot->updated_at, $payment->client->date_format(), $payment->client->locale()),
|
||||
'timestamp' => $invoice->pivot->created_at->timestamp,
|
||||
];
|
||||
})->merge($credits)->sortBy('timestamp')->toArray();
|
||||
|
||||
return [
|
||||
'status' => $payment->stringStatus($payment->status_id),
|
||||
'badge' => $payment->badgeForStatus($payment->status_id),
|
||||
'amount' => Number::formatMoney($payment->amount, $payment->client),
|
||||
'applied' => Number::formatMoney($payment->applied, $payment->client),
|
||||
'balance' => Number::formatMoney(($payment->amount - $payment->refunded - $payment->applied), $payment->client),
|
||||
'refunded' => Number::formatMoney($payment->refunded, $payment->client),
|
||||
'amount_raw' => $payment->amount,
|
||||
'applied_raw' => $payment->applied,
|
||||
'refunded_raw' => $payment->refunded,
|
||||
'balance_raw' => ($payment->amount - $payment->refunded - $payment->applied),
|
||||
'date' => $this->translateDate($payment->date, $payment->client->date_format(), $payment->client->locale()),
|
||||
'method' => $payment->translatedType(),
|
||||
'currency' => $payment->currency->code,
|
||||
'exchange_rate' => $payment->exchange_rate,
|
||||
'transaction_reference' => $payment->transaction_reference,
|
||||
'is_manual' => $payment->is_manual,
|
||||
'number' => $payment->number,
|
||||
'custom_value1' => $payment->custom_value1 ?? '',
|
||||
'custom_value2' => $payment->custom_value2 ?? '',
|
||||
'custom_value3' => $payment->custom_value3 ?? '',
|
||||
'custom_value4' => $payment->custom_value4 ?? '',
|
||||
'client' => [
|
||||
'name' => $payment->client->present()->name(),
|
||||
'balance' => $payment->client->balance,
|
||||
'payment_balance' => $payment->client->payment_balance,
|
||||
'credit_balance' => $payment->client->credit_balance,
|
||||
],
|
||||
'paymentables' => $pivot,
|
||||
];
|
||||
|
||||
return $data;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function processQuotes($quotes): array
|
||||
{
|
||||
$it = new QuoteTransformer();
|
||||
|
@ -323,10 +323,11 @@ class TemplateTest extends TestCase
|
||||
$count = count($queries);
|
||||
|
||||
nlog("query count = {$count}");
|
||||
|
||||
$x = $invoices->toArray();
|
||||
nlog(json_encode(htmlspecialchars(json_encode($x), ENT_QUOTES, 'UTF-8')));
|
||||
nlog(json_encode($x));
|
||||
// nlog(json_encode(htmlspecialchars(json_encode($x), ENT_QUOTES, 'UTF-8')));
|
||||
// nlog($invoices->toJson());
|
||||
|
||||
$this->assertIsArray($invoices->toArray());
|
||||
|
||||
nlog("end invoices = " . microtime(true) - $start);
|
||||
|
Loading…
x
Reference in New Issue
Block a user