1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-22 17:31:35 +02:00
invoiceninja/app/Services/Template/TemplateService.php

661 lines
24 KiB
PHP
Raw Normal View History

2023-09-22 08:14:25 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Template;
use App\Utils\Number;
2023-10-03 07:38:36 +02:00
use App\Models\Client;
2023-09-22 15:31:12 +02:00
use App\Models\Credit;
2023-09-22 08:14:25 +02:00
use App\Models\Design;
2023-09-27 13:32:17 +02:00
use App\Models\Company;
2023-09-22 15:31:12 +02:00
use App\Models\Payment;
use App\Models\Project;
2023-09-22 14:35:43 +02:00
use App\Utils\HtmlEngine;
2023-09-22 15:31:12 +02:00
use League\Fractal\Manager;
use App\Models\PurchaseOrder;
2023-09-22 14:08:57 +02:00
use App\Utils\VendorHtmlEngine;
use App\Utils\PaymentHtmlEngine;
use App\Utils\Traits\MakesDates;
2023-10-04 06:29:31 +02:00
use App\Utils\HostedPDF\NinjaPdf;
2023-09-25 05:19:08 +02:00
use Twig\Extra\Intl\IntlExtension;
2023-09-22 15:31:12 +02:00
use App\Transformers\TaskTransformer;
use App\Transformers\QuoteTransformer;
2023-10-02 02:06:24 +02:00
use App\Services\Template\TemplateMock;
2023-09-22 15:31:12 +02:00
use App\Transformers\CreditTransformer;
use App\Transformers\InvoiceTransformer;
use App\Transformers\ProjectTransformer;
use App\Transformers\PurchaseOrderTransformer;
use League\Fractal\Serializer\ArraySerializer;
2023-10-04 06:29:31 +02:00
use App\Utils\Traits\Pdf\PdfMaker;
2023-09-22 08:14:25 +02:00
class TemplateService
{
2023-10-04 06:29:31 +02:00
use MakesDates, PdfMaker;
2023-09-22 08:14:25 +02:00
private \DomDocument $document;
2023-09-25 07:56:32 +02:00
public \Twig\Environment $twig;
2023-09-22 08:14:25 +02:00
private string $compiled_html = '';
2023-09-27 12:36:08 +02:00
private array $data = [];
2023-09-27 13:44:09 +02:00
private array $variables = [];
2023-09-27 13:32:17 +02:00
public ?Company $company;
2023-09-25 07:56:32 +02:00
public function __construct(public ?Design $template = null)
2023-09-22 08:14:25 +02:00
{
$this->template = $template;
$this->init();
}
/**
* Boot Dom Document
*
* @return self
*/
private function init(): self
{
$this->document = new \DOMDocument();
$this->document->validateOnParse = true;
2023-09-25 07:56:32 +02:00
$loader = new \Twig\Loader\FilesystemLoader(storage_path());
$this->twig = new \Twig\Environment($loader);
$string_extension = new \Twig\Extension\StringLoaderExtension();
$this->twig->addExtension($string_extension);
$this->twig->addExtension(new IntlExtension());
2023-10-02 04:43:07 +02:00
$function = new \Twig\TwigFunction('img', function ($string, $style = '') {
return '<img src="'.$string.'" style="'.$style.'"></img>';
});
$this->twig->addFunction($function);
2023-09-22 08:14:25 +02:00
return $this;
}
/**
* Iterate through all of the
* ninja nodes
*
* @param array $data - the payload to be passed into the template
* @return self
*/
2023-09-22 14:35:43 +02:00
public function build(array $data): self
2023-09-22 08:14:25 +02:00
{
$this->compose()
2023-09-27 12:36:08 +02:00
->processData($data)
->parseNinjaBlocks()
2023-09-27 13:44:09 +02:00
->processVariables($data)
->parseVariables();
2023-09-22 08:14:25 +02:00
return $this;
}
2023-09-27 13:44:09 +02:00
private function processVariables($data): self
{
$this->variables = $this->resolveHtmlEngine($data);
return $this;
}
2023-09-27 12:36:08 +02:00
public function mock(): self
{
2023-09-27 13:32:17 +02:00
$tm = new TemplateMock($this->company);
2023-09-27 13:44:09 +02:00
$tm->init();
2023-09-27 13:32:17 +02:00
$this->data = $tm->engines;
2023-09-27 13:44:09 +02:00
$this->variables = $tm->variables[0];
2023-09-27 13:32:17 +02:00
2023-09-27 13:32:17 +02:00
$this->parseNinjaBlocks()
2023-09-27 13:44:09 +02:00
->parseVariables();
2023-09-27 12:36:08 +02:00
return $this;
}
2023-09-22 08:14:25 +02:00
public function getHtml(): string
{
return $this->compiled_html;
}
2023-09-27 12:36:08 +02:00
2023-10-04 06:29:31 +02:00
public function getPdf(): mixed
{
if (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
$pdf = (new NinjaPdf())->build($this->compiled_html);
} else {
$pdf = $this->makePdf(null, null, $this->compiled_html);
}
return $pdf;
}
2023-09-27 12:36:08 +02:00
private function processData($data): self
{
$this->data = $this->preProcessDataBlocks($data);
return $this;
}
2023-09-22 08:14:25 +02:00
/**
* Parses all Ninja tags in the document
*
* @return self
*/
2023-09-27 12:36:08 +02:00
private function parseNinjaBlocks(): self
2023-09-22 08:14:25 +02:00
{
$replacements = [];
2023-09-25 07:56:32 +02:00
2023-09-22 08:14:25 +02:00
$contents = $this->document->getElementsByTagName('ninja');
foreach ($contents as $content) {
$template = $content->ownerDocument->saveHTML($content);
2023-10-02 02:06:24 +02:00
try {
$template = $this->twig->createTemplate(html_entity_decode($template));
}
catch(\Twig\Error\SyntaxError $e) {
nlog($e->getMessage());
throw ($e);
}
2023-09-27 12:36:08 +02:00
$template = $template->render($this->data);
2023-10-02 04:43:07 +02:00
2023-10-03 07:38:36 +02:00
$f = $this->document->createDocumentFragment();
$f->appendXML(html_entity_decode($template));
2023-09-22 08:14:25 +02:00
$replacements[] = $f;
}
foreach($contents as $key => $content) {
$content->parentNode->replaceChild($replacements[$key], $content);
}
$this->save();
return $this;
}
/**
* Parses all variables in the document
*
2023-09-22 08:14:25 +02:00
* @return self
*/
2023-09-27 13:44:09 +02:00
private function parseVariables(): self
2023-09-22 08:14:25 +02:00
{
2023-09-22 14:35:43 +02:00
$html = $this->getHtml();
2023-09-22 08:14:25 +02:00
2023-09-27 13:44:09 +02:00
foreach($this->variables as $key => $variable) {
2023-09-25 07:56:32 +02:00
if(isset($variable['labels']) && isset($variable['values']))
{
$html = strtr($html, $variable['labels']);
$html = strtr($html, $variable['values']);
}
2023-09-22 14:08:57 +02:00
}
2023-09-22 08:14:25 +02:00
@$this->document->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
$this->save();
return $this;
}
/**
* Saves the document and updates the compiled string.
*
* @return self
*/
private function save(): self
{
$this->compiled_html = str_replace('%24', '$', $this->document->saveHTML());
return $this;
}
/**
* compose
*
* @return self
*/
private function compose(): self
{
2023-09-25 07:56:32 +02:00
if(!$this->template)
return $this;
2023-09-22 08:14:25 +02:00
$html = '';
$html .= $this->template->design->includes;
$html .= $this->template->design->header;
$html .= $this->template->design->body;
$html .= $this->template->design->footer;
2023-10-01 12:21:57 +02:00
@$this->document->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
2023-09-22 08:14:25 +02:00
return $this;
}
2023-09-25 07:56:32 +02:00
/**
* Inject the template components
* manually
*
* @return self
*/
public function setTemplate(array $partials): self
2023-09-27 12:36:08 +02:00
{
2023-09-25 07:56:32 +02:00
$html = '';
$html .= $partials['design']['includes'];
$html .= $partials['design']['header'];
$html .= $partials['design']['body'];
$html .= $partials['design']['footer'];
2023-10-01 12:21:57 +02:00
@$this->document->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
2023-09-25 07:56:32 +02:00
return $this;
}
2023-09-22 08:14:25 +02:00
/**
* Resolves the labels and values needed to replace the string
* holders in the template.
*
* @return array
*/
2023-09-22 14:08:57 +02:00
private function resolveHtmlEngine(array $data): array
2023-09-22 08:14:25 +02:00
{
2023-09-22 14:35:43 +02:00
return collect($data)->map(function ($value, $key) {
$processed = [];
2023-09-22 14:08:57 +02:00
2023-09-27 03:18:35 +02:00
if(in_array($key, ['tasks','projects']) || !$value->first() )
2023-09-26 13:21:10 +02:00
return $processed;
2023-09-22 14:08:57 +02:00
match ($key) {
2023-09-26 13:21:10 +02:00
'invoices' => $processed = (new HtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues() ?? [],
'quotes' => $processed = (new HtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues() ?? [],
'credits' => $processed = (new HtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues() ?? [],
'payments' => $processed = (new PaymentHtmlEngine($value->first(), $value->first()->client->contacts()->first()))->generateLabelsAndValues() ?? [],
2023-09-22 14:35:43 +02:00
'tasks' => $processed = [],
'projects' => $processed = [],
2023-09-26 13:21:10 +02:00
'purchase_orders' => (new VendorHtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues() ?? [],
2023-09-22 14:08:57 +02:00
};
2023-10-03 09:52:46 +02:00
2023-09-22 14:08:57 +02:00
return $processed;
})->toArray();
2023-09-22 08:14:25 +02:00
}
private function preProcessDataBlocks($data): array
{
2023-09-22 14:35:43 +02:00
return collect($data)->map(function ($value, $key){
2023-09-22 08:14:25 +02:00
2023-09-22 14:35:43 +02:00
$processed = [];
2023-09-22 08:14:25 +02:00
match ($key) {
2023-09-22 14:35:43 +02:00
'invoices' => $processed = $this->processInvoices($value),
'quotes' => $processed = $this->processQuotes($value),
'credits' => $processed = $this->processCredits($value),
'payments' => $processed = $this->processPayments($value),
'tasks' => $processed = $this->processTasks($value),
'projects' => $processed = $this->processProjects($value),
'purchase_orders' => $processed = $this->processPurchaseOrders($value),
2023-09-22 08:14:25 +02:00
};
return $processed;
})->toArray();
}
2023-09-27 12:36:08 +02:00
public function processInvoices($invoices): array
{
$invoices = collect($invoices)
->map(function ($invoice){
2023-10-04 06:29:31 +02:00
$payments = [];
2023-10-04 23:08:49 +02:00
2023-10-06 12:39:22 +02:00
if($invoice->payments ?? false) {
2023-10-04 06:29:31 +02:00
$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,
2023-10-03 07:38:36 +02:00
'line_items' => $invoice->line_items ? $this->padLineItems($invoice->line_items, $invoice->client): (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,
2023-10-03 09:52:46 +02:00
'total_tax_map' => $invoice->calc()->getTotalTaxMap(),
'line_tax_map' => $invoice->calc()->getTaxMap(),
];
});
return $invoices->toArray();
}
2023-10-03 07:38:36 +02:00
public function padLineItems(array $items, Client $client): array
{
return collect($items)->map(function ($item) use ($client){
2023-10-04 06:29:31 +02:00
$item->cost_raw = $item->cost ?? 0;
$item->discount_raw = $item->discount ?? 0;
$item->line_total_raw = $item->line_total ?? 0;
$item->gross_line_total_raw = $item->gross_line_total ?? 0;
$item->tax_amount_raw = $item->tax_amount ?? 0;
$item->product_cost_raw = $item->product_cost ?? 0;
2023-10-03 07:38:36 +02:00
$item->cost = Number::formatMoney($item->cost_raw, $client);
if($item->is_amount_discount)
$item->discount = Number::formatMoney($item->discount_raw, $client);
$item->line_total = Number::formatMoney($item->line_total_raw, $client);
$item->gross_line_total = Number::formatMoney($item->gross_line_total_raw, $client);
$item->tax_amount = Number::formatMoney($item->tax_amount_raw, $client);
$item->product_cost = Number::formatMoney($item->product_cost_raw, $client);
return $item;
})->toArray();
}
public function processInvoicesBak($invoices): array
2023-09-22 08:14:25 +02:00
{
2023-09-22 15:31:12 +02:00
$it = new InvoiceTransformer();
2023-09-27 12:36:08 +02:00
$it->setDefaultIncludes(['client','payments', 'credits']);
2023-09-22 15:31:12 +02:00
$manager = new Manager();
2023-09-27 12:36:08 +02:00
$manager->parseIncludes(['client','payments','payments.type','credits']);
2023-09-25 05:19:08 +02:00
$resource = new \League\Fractal\Resource\Collection($invoices, $it, null);
$invoices = $manager->createData($resource)->toArray();
foreach($invoices['data'] as $key => $invoice)
{
$invoices['data'][$key]['client'] = $invoice['client']['data'] ?? [];
$invoices['data'][$key]['client']['contacts'] = $invoice['client']['data']['contacts']['data'] ?? [];
$invoices['data'][$key]['payments'] = $invoice['payments']['data'] ?? [];
2023-09-27 12:36:08 +02:00
$invoices['data'][$key]['credits'] = $invoice['credits']['data'] ?? [];
2023-09-25 05:19:08 +02:00
if($invoice['payments']['data'] ?? false) {
foreach($invoice['payments']['data'] as $keyx => $payment) {
2023-09-27 12:36:08 +02:00
$invoices['data'][$key]['payments'][$keyx]['paymentables'] = $payment['paymentables']['data'] ?? [];
$invoices['data'][$key]['payments'][$keyx]['type'] = $payment['type']['data'] ?? [];
2023-09-25 05:19:08 +02:00
}
}
}
return $invoices['data'];
2023-09-22 08:14:25 +02:00
}
2023-10-01 12:21:57 +02:00
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,
2023-10-08 07:06:37 +02:00
'date' => $this->translateDate($credit->date, $payment->client->date_format(), $payment->client->locale()),
'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,
2023-10-08 07:06:37 +02:00
'date' => $this->translateDate($invoice->date, $payment->client->date_format(), $payment->client->locale()),
'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 ?? '',
'created_at' => $this->translateDate($payment->created_at, $payment->client->date_format(), $payment->client->locale()),
'updated_at' => $this->translateDate($payment->updated_at, $payment->client->date_format(), $payment->client->locale()),
'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;
}
2023-09-27 13:11:47 +02:00
public function processQuotes($quotes): array
2023-09-22 08:14:25 +02:00
{
2023-09-22 15:31:12 +02:00
$it = new QuoteTransformer();
$it->setDefaultIncludes(['client']);
$manager = new Manager();
2023-09-25 07:56:32 +02:00
$manager->parseIncludes(['client']);
$resource = new \League\Fractal\Resource\Collection($quotes, $it, null);
$resources = $manager->createData($resource)->toArray();
foreach($resources['data'] as $key => $resource) {
$resources['data'][$key]['client'] = $resource['client']['data'] ?? [];
$resources['data'][$key]['client']['contacts'] = $resource['client']['data']['contacts']['data'] ?? [];
}
return $resources['data'];
2023-09-22 08:14:25 +02:00
}
2023-09-27 10:10:22 +02:00
/**
* Pushes credits through the appropriate transformer
* and builds any required relationships
*
* @param mixed $credits
* @return array
*/
2023-09-27 13:11:47 +02:00
public function processCredits($credits): array
2023-09-22 08:14:25 +02:00
{
2023-09-22 15:31:12 +02:00
$it = new CreditTransformer();
$it->setDefaultIncludes(['client']);
$manager = new Manager();
$resource = new \League\Fractal\Resource\Collection($credits, $it, Credit::class);
2023-09-25 07:56:32 +02:00
$resources = $manager->createData($resource)->toArray();
foreach($resources['data'] as $key => $resource) {
$resources['data'][$key]['client'] = $resource['client']['data'] ?? [];
$resources['data'][$key]['client']['contacts'] = $resource['client']['data']['contacts']['data'] ?? [];
}
return $resources['data'];
2023-09-22 15:31:12 +02:00
2023-09-22 08:14:25 +02:00
}
2023-09-27 10:10:22 +02:00
/**
* Pushes payments through the appropriate transformer
*
* @param mixed $payments
* @return array
*/
2023-09-27 13:11:47 +02:00
public function processPayments($payments): array
2023-09-22 08:14:25 +02:00
{
2023-09-25 07:56:32 +02:00
2023-10-01 12:21:57 +02:00
$payments = $payments->map(function ($payment) {
return $this->transformPayment($payment);
})->toArray();
2023-10-01 12:21:57 +02:00
return $payments;
2023-09-22 15:31:12 +02:00
2023-09-22 08:14:25 +02:00
}
2023-09-27 13:11:47 +02:00
public function processTasks($tasks): array
2023-09-22 08:14:25 +02:00
{
2023-09-22 15:31:12 +02:00
$it = new TaskTransformer();
2023-09-25 07:56:32 +02:00
$it->setDefaultIncludes(['client','project','invoice']);
2023-09-22 15:31:12 +02:00
$manager = new Manager();
2023-09-25 07:56:32 +02:00
$resource = new \League\Fractal\Resource\Collection($tasks, $it, null);
$resources = $manager->createData($resource)->toArray();
foreach($resources['data'] as $key => $resource) {
$resources['data'][$key]['client'] = $resource['client']['data'] ?? [];
$resources['data'][$key]['client']['contacts'] = $resource['client']['data']['contacts']['data'] ?? [];
$resources['data'][$key]['project'] = $resource['project']['data'] ?? [];
$resources['data'][$key]['invoice'] = $resource['invoice'] ?? [];
}
return $resources['data'];
2023-09-22 15:31:12 +02:00
2023-09-22 08:14:25 +02:00
}
2023-09-27 13:11:47 +02:00
public function processProjects($projects): array
2023-09-22 08:14:25 +02:00
{
2023-09-22 14:08:57 +02:00
2023-09-22 15:31:12 +02:00
$it = new ProjectTransformer();
$it->setDefaultIncludes(['client','tasks']);
$manager = new Manager();
2023-09-25 05:19:08 +02:00
$manager->setSerializer(new ArraySerializer());
2023-09-22 15:31:12 +02:00
$resource = new \League\Fractal\Resource\Collection($projects, $it, Project::class);
$i = $manager->createData($resource)->toArray();
2023-09-25 05:19:08 +02:00
return $i[Project::class];
2023-09-22 15:31:12 +02:00
2023-09-22 08:14:25 +02:00
}
2023-09-27 13:11:47 +02:00
public function processPurchaseOrders($purchase_orders): array
2023-09-22 08:14:25 +02:00
{
2023-09-22 15:31:12 +02:00
$it = new PurchaseOrderTransformer();
$it->setDefaultIncludes(['vendor','expense']);
$manager = new Manager();
2023-09-25 05:19:08 +02:00
$manager->setSerializer(new ArraySerializer());
2023-09-22 15:31:12 +02:00
$resource = new \League\Fractal\Resource\Collection($purchase_orders, $it, PurchaseOrder::class);
$i = $manager->createData($resource)->toArray();
2023-09-25 05:19:08 +02:00
return $i[PurchaseOrder::class];
2023-09-22 08:14:25 +02:00
}
2023-09-27 13:32:17 +02:00
public function setCompany(Company $company): self
{
$this->company = $company;
return $this;
}
public function getCompany(): Company
{
return $this->company;
}
2023-09-22 08:14:25 +02:00
}