mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-08 17:44:32 -04:00
Working on statement templates
This commit is contained in:
parent
c44310fa09
commit
149637bb10
@ -12,24 +12,25 @@
|
|||||||
|
|
||||||
namespace App\Services\Client;
|
namespace App\Services\Client;
|
||||||
|
|
||||||
use App\Factory\InvoiceFactory;
|
use App\Utils\Number;
|
||||||
use App\Factory\InvoiceInvitationFactory;
|
|
||||||
use App\Factory\InvoiceItemFactory;
|
|
||||||
use App\Models\Client;
|
use App\Models\Client;
|
||||||
|
use App\Models\Credit;
|
||||||
use App\Models\Design;
|
use App\Models\Design;
|
||||||
use App\Models\Invoice;
|
use App\Models\Invoice;
|
||||||
use App\Models\Payment;
|
use App\Models\Payment;
|
||||||
use App\Services\PdfMaker\Design as PdfMakerDesign;
|
|
||||||
use App\Services\PdfMaker\PdfMaker;
|
|
||||||
use App\Utils\HostedPDF\NinjaPdf;
|
|
||||||
use App\Utils\HtmlEngine;
|
use App\Utils\HtmlEngine;
|
||||||
use App\Utils\Number;
|
|
||||||
use App\Utils\PhantomJS\Phantom;
|
|
||||||
use App\Utils\Traits\Pdf\PdfMaker as PdfMakerTrait;
|
|
||||||
use Illuminate\Support\Carbon;
|
use Illuminate\Support\Carbon;
|
||||||
use Illuminate\Support\Facades\DB;
|
use App\Factory\InvoiceFactory;
|
||||||
use App\Models\Credit;
|
|
||||||
use App\Utils\Traits\MakesHash;
|
use App\Utils\Traits\MakesHash;
|
||||||
|
use App\Utils\PhantomJS\Phantom;
|
||||||
|
use App\Utils\HostedPDF\NinjaPdf;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use App\Factory\InvoiceItemFactory;
|
||||||
|
use App\Services\PdfMaker\PdfMaker;
|
||||||
|
use App\Factory\InvoiceInvitationFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use App\Services\PdfMaker\Design as PdfMakerDesign;
|
||||||
|
use App\Utils\Traits\Pdf\PdfMaker as PdfMakerTrait;
|
||||||
|
|
||||||
class Statement
|
class Statement
|
||||||
{
|
{
|
||||||
@ -54,6 +55,12 @@ class Statement
|
|||||||
|
|
||||||
$html = new HtmlEngine($this->getInvitation());
|
$html = new HtmlEngine($this->getInvitation());
|
||||||
|
|
||||||
|
$variables = $html->generateLabelsAndValues();
|
||||||
|
|
||||||
|
if($this->client->getSetting('statement_design_id') != '') {
|
||||||
|
return $this->templateStatement($variables);
|
||||||
|
}
|
||||||
|
|
||||||
if ($this->getDesign()->is_custom) {
|
if ($this->getDesign()->is_custom) {
|
||||||
$this->options['custom_partials'] = \json_decode(\json_encode($this->getDesign()->design), true);
|
$this->options['custom_partials'] = \json_decode(\json_encode($this->getDesign()->design), true);
|
||||||
|
|
||||||
@ -62,8 +69,6 @@ class Statement
|
|||||||
$template = new PdfMakerDesign(strtolower($this->getDesign()->name), $this->options);
|
$template = new PdfMakerDesign(strtolower($this->getDesign()->name), $this->options);
|
||||||
}
|
}
|
||||||
|
|
||||||
$variables = $html->generateLabelsAndValues();
|
|
||||||
|
|
||||||
$state = [
|
$state = [
|
||||||
'template' => $template->elements([
|
'template' => $template->elements([
|
||||||
'client' => $this->client,
|
'client' => $this->client,
|
||||||
@ -71,9 +76,9 @@ class Statement
|
|||||||
'pdf_variables' => (array) $this->entity->company->settings->pdf_variables,
|
'pdf_variables' => (array) $this->entity->company->settings->pdf_variables,
|
||||||
'$product' => $this->getDesign()->design->product,
|
'$product' => $this->getDesign()->design->product,
|
||||||
'variables' => $variables,
|
'variables' => $variables,
|
||||||
'invoices' => $this->getInvoices(),
|
'invoices' => $this->getInvoices()->cursor(),
|
||||||
'payments' => $this->getPayments(),
|
'payments' => $this->getPayments()->cursor(),
|
||||||
'credits' => $this->getCredits(),
|
'credits' => $this->getCredits()->cursor(),
|
||||||
'aging' => $this->getAging(),
|
'aging' => $this->getAging(),
|
||||||
], \App\Services\PdfMaker\Design::STATEMENT),
|
], \App\Services\PdfMaker\Design::STATEMENT),
|
||||||
'variables' => $variables,
|
'variables' => $variables,
|
||||||
@ -81,44 +86,65 @@ class Statement
|
|||||||
'client' => $this->client,
|
'client' => $this->client,
|
||||||
'entity' => $this->entity,
|
'entity' => $this->entity,
|
||||||
'variables' => $variables,
|
'variables' => $variables,
|
||||||
'invoices' => $this->getInvoices(),
|
'invoices' => $this->getInvoices()->cursor(),
|
||||||
'payments' => $this->getPayments(),
|
'payments' => $this->getPayments()->cursor(),
|
||||||
'credits' => $this->getCredits(),
|
'credits' => $this->getCredits()->cursor(),
|
||||||
'aging' => $this->getAging(),
|
'aging' => $this->getAging(),
|
||||||
],
|
],
|
||||||
'process_markdown' => $this->entity->client->company->markdown_enabled,
|
'process_markdown' => $this->entity->client->company->markdown_enabled,
|
||||||
];
|
];
|
||||||
|
|
||||||
if($this->options['template'] ?? false){
|
|
||||||
|
|
||||||
$template = Design::where('id', $this->decodePrimaryKey($this->options['template']))
|
$maker = new PdfMaker($state);
|
||||||
->where('company_id', $this->client->company_id)
|
|
||||||
->first();
|
|
||||||
|
|
||||||
$ts = $template->service()->build([
|
$maker
|
||||||
'client' => $this->client,
|
->design($template)
|
||||||
'entity' => $this->entity,
|
->build();
|
||||||
'variables' => $variables,
|
|
||||||
'invoices' => $this->getInvoices(),
|
|
||||||
'payments' => $this->getPayments(),
|
|
||||||
'credits' => $this->getCredits(),
|
|
||||||
'aging' => $this->getAging(),
|
|
||||||
]);
|
|
||||||
|
|
||||||
$html = $ts->getHtml();
|
$pdf = null;
|
||||||
|
$html = $maker->getCompiledHTML(true);
|
||||||
|
|
||||||
|
|
||||||
|
if ($this->rollback) {
|
||||||
|
\DB::connection(config('database.default'))->rollBack();
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
|
|
||||||
$maker = new PdfMaker($state);
|
$pdf = $this->convertToPdf($html);
|
||||||
|
|
||||||
$maker
|
$maker = null;
|
||||||
->design($template)
|
$state = null;
|
||||||
->build();
|
|
||||||
|
|
||||||
$pdf = null;
|
return $pdf;
|
||||||
$html = $maker->getCompiledHTML(true);
|
}
|
||||||
}
|
|
||||||
|
private function templateStatement($variables)
|
||||||
|
{nlog($this->options);
|
||||||
|
if(isset($this->options['template']))
|
||||||
|
$statement_design_id = $this->options['template'];
|
||||||
|
else
|
||||||
|
$statement_design_id = $this->client->getSetting('statement_design_id');
|
||||||
|
|
||||||
|
$template = Design::where('id', $this->decodePrimaryKey($statement_design_id))
|
||||||
|
->where('company_id', $this->client->company_id)
|
||||||
|
->first();
|
||||||
|
|
||||||
|
$ts = $template->service()->build([
|
||||||
|
'variables' => $variables,
|
||||||
|
'invoices' => $this->getInvoices()->get(),
|
||||||
|
'payments' => $this->getPayments()->get(),
|
||||||
|
'credits' => $this->getCredits()->get(),
|
||||||
|
'aging' => $this->getAging(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$html = $ts->getHtml();
|
||||||
|
|
||||||
|
return $this->convertToPdf($html);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function convertToPdf(string $html): mixed
|
||||||
|
{
|
||||||
|
$pdf = false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
|
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
|
||||||
@ -132,16 +158,9 @@ class Statement
|
|||||||
nlog(print_r($e->getMessage(), 1));
|
nlog(print_r($e->getMessage(), 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->rollback) {
|
|
||||||
\DB::connection(config('database.default'))->rollBack();
|
|
||||||
}
|
|
||||||
|
|
||||||
$maker = null;
|
|
||||||
$state = null;
|
|
||||||
|
|
||||||
return $pdf;
|
return $pdf;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setup correct entity instance.
|
* Setup correct entity instance.
|
||||||
*
|
*
|
||||||
@ -253,9 +272,9 @@ class Statement
|
|||||||
/**
|
/**
|
||||||
* The collection of invoices for the statement.
|
* The collection of invoices for the statement.
|
||||||
*
|
*
|
||||||
* @return Invoice[]|\Illuminate\Support\LazyCollection
|
* @return Builder
|
||||||
*/
|
*/
|
||||||
public function getInvoices(): \Illuminate\Support\LazyCollection
|
public function getInvoices(): Builder
|
||||||
{
|
{
|
||||||
return Invoice::withTrashed()
|
return Invoice::withTrashed()
|
||||||
->with('payments.type')
|
->with('payments.type')
|
||||||
@ -265,8 +284,7 @@ class Statement
|
|||||||
->whereIn('status_id', $this->invoiceStatuses())
|
->whereIn('status_id', $this->invoiceStatuses())
|
||||||
->whereBetween('date', [Carbon::parse($this->options['start_date']), Carbon::parse($this->options['end_date'])])
|
->whereBetween('date', [Carbon::parse($this->options['start_date']), Carbon::parse($this->options['end_date'])])
|
||||||
->orderBy('due_date', 'ASC')
|
->orderBy('due_date', 'ASC')
|
||||||
->orderBy('date', 'ASC')
|
->orderBy('date', 'ASC');
|
||||||
->cursor();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function invoiceStatuses() :array
|
private function invoiceStatuses() :array
|
||||||
@ -297,9 +315,9 @@ class Statement
|
|||||||
/**
|
/**
|
||||||
* The collection of payments for the statement.
|
* The collection of payments for the statement.
|
||||||
*
|
*
|
||||||
* @return Payment[]|\Illuminate\Support\LazyCollection
|
* @return Builder
|
||||||
*/
|
*/
|
||||||
protected function getPayments(): \Illuminate\Support\LazyCollection
|
protected function getPayments(): Builder
|
||||||
{
|
{
|
||||||
return Payment::withTrashed()
|
return Payment::withTrashed()
|
||||||
->with('client.country', 'invoices')
|
->with('client.country', 'invoices')
|
||||||
@ -308,16 +326,15 @@ class Statement
|
|||||||
->where('client_id', $this->client->id)
|
->where('client_id', $this->client->id)
|
||||||
->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment::STATUS_PARTIALLY_REFUNDED, Payment::STATUS_REFUNDED])
|
->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment::STATUS_PARTIALLY_REFUNDED, Payment::STATUS_REFUNDED])
|
||||||
->whereBetween('date', [Carbon::parse($this->options['start_date']), Carbon::parse($this->options['end_date'])])
|
->whereBetween('date', [Carbon::parse($this->options['start_date']), Carbon::parse($this->options['end_date'])])
|
||||||
->orderBy('date', 'ASC')
|
->orderBy('date', 'ASC');
|
||||||
->cursor();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The collection of credits for the statement.
|
* The collection of credits for the statement.
|
||||||
*
|
*
|
||||||
* @return Credit[]|\Illuminate\Support\LazyCollection
|
* @return Builder
|
||||||
*/
|
*/
|
||||||
protected function getCredits(): \Illuminate\Support\LazyCollection
|
protected function getCredits(): Builder
|
||||||
{
|
{
|
||||||
return Credit::withTrashed()
|
return Credit::withTrashed()
|
||||||
->with('client.country', 'invoices')
|
->with('client.country', 'invoices')
|
||||||
@ -330,8 +347,7 @@ class Statement
|
|||||||
$query->whereDate('due_date', '>=', $this->options['end_date'])
|
$query->whereDate('due_date', '>=', $this->options['end_date'])
|
||||||
->orWhereNull('due_date');
|
->orWhereNull('due_date');
|
||||||
})
|
})
|
||||||
->orderBy('date', 'ASC')
|
->orderBy('date', 'ASC');
|
||||||
->cursor();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -71,10 +71,13 @@ class TemplateService
|
|||||||
$this->document->validateOnParse = true;
|
$this->document->validateOnParse = true;
|
||||||
|
|
||||||
$loader = new \Twig\Loader\FilesystemLoader(storage_path());
|
$loader = new \Twig\Loader\FilesystemLoader(storage_path());
|
||||||
$this->twig = new \Twig\Environment($loader);
|
$this->twig = new \Twig\Environment($loader,[
|
||||||
|
'debug' => true,
|
||||||
|
]);
|
||||||
$string_extension = new \Twig\Extension\StringLoaderExtension();
|
$string_extension = new \Twig\Extension\StringLoaderExtension();
|
||||||
$this->twig->addExtension($string_extension);
|
$this->twig->addExtension($string_extension);
|
||||||
$this->twig->addExtension(new IntlExtension());
|
$this->twig->addExtension(new IntlExtension());
|
||||||
|
$this->twig->addExtension(new \Twig\Extension\DebugExtension());
|
||||||
|
|
||||||
$function = new \Twig\TwigFunction('img', function ($string, $style = '') {
|
$function = new \Twig\TwigFunction('img', function ($string, $style = '') {
|
||||||
return '<img src="'.$string.'" style="'.$style.'"></img>';
|
return '<img src="'.$string.'" style="'.$style.'"></img>';
|
||||||
@ -123,7 +126,12 @@ class TemplateService
|
|||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the HTML as string
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public function getHtml(): string
|
public function getHtml(): string
|
||||||
{
|
{
|
||||||
return $this->compiled_html;
|
return $this->compiled_html;
|
||||||
@ -172,6 +180,22 @@ class TemplateService
|
|||||||
nlog($e->getMessage());
|
nlog($e->getMessage());
|
||||||
throw ($e);
|
throw ($e);
|
||||||
}
|
}
|
||||||
|
catch(\Twig\Error\Error $e) {
|
||||||
|
nlog("error = " .$e->getMessage());
|
||||||
|
throw ($e);
|
||||||
|
}
|
||||||
|
catch(\Twig\Error\RuntimeError $e) {
|
||||||
|
nlog("runtime = " .$e->getMessage());
|
||||||
|
throw ($e);
|
||||||
|
}
|
||||||
|
catch(\Twig\Error\LoaderError $e) {
|
||||||
|
nlog("loader = " . $e->getMessage());
|
||||||
|
throw ($e);
|
||||||
|
}
|
||||||
|
catch(\Twig\Error\SecurityError $e) {
|
||||||
|
nlog("security = " . $e->getMessage());
|
||||||
|
throw ($e);
|
||||||
|
}
|
||||||
|
|
||||||
$template = $template->render($this->data);
|
$template = $template->render($this->data);
|
||||||
|
|
||||||
@ -284,7 +308,7 @@ class TemplateService
|
|||||||
|
|
||||||
$processed = [];
|
$processed = [];
|
||||||
|
|
||||||
if(in_array($key, ['tasks','projects']) || !$value->first() )
|
if(in_array($key, ['tasks','projects','aging']) || is_array($value) || !$value->first() )
|
||||||
return $processed;
|
return $processed;
|
||||||
|
|
||||||
match ($key) {
|
match ($key) {
|
||||||
@ -295,6 +319,8 @@ class TemplateService
|
|||||||
'tasks' => $processed = [],
|
'tasks' => $processed = [],
|
||||||
'projects' => $processed = [],
|
'projects' => $processed = [],
|
||||||
'purchase_orders' => (new VendorHtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues() ?? [],
|
'purchase_orders' => (new VendorHtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues() ?? [],
|
||||||
|
'aging' => $processed = [],
|
||||||
|
default => $processed = [],
|
||||||
};
|
};
|
||||||
|
|
||||||
return $processed;
|
return $processed;
|
||||||
@ -317,6 +343,8 @@ class TemplateService
|
|||||||
'tasks' => $processed = $this->processTasks($value),
|
'tasks' => $processed = $this->processTasks($value),
|
||||||
'projects' => $processed = $this->processProjects($value),
|
'projects' => $processed = $this->processProjects($value),
|
||||||
'purchase_orders' => $processed = $this->processPurchaseOrders($value),
|
'purchase_orders' => $processed = $this->processPurchaseOrders($value),
|
||||||
|
'aging' => $processed = $value,
|
||||||
|
default => $processed = [],
|
||||||
};
|
};
|
||||||
|
|
||||||
return $processed;
|
return $processed;
|
||||||
@ -333,7 +361,6 @@ class TemplateService
|
|||||||
|
|
||||||
if($invoice->payments ?? false) {
|
if($invoice->payments ?? false) {
|
||||||
$payments = $invoice->payments->map(function ($payment) {
|
$payments = $invoice->payments->map(function ($payment) {
|
||||||
// nlog(microtime(true));
|
|
||||||
return $this->transformPayment($payment);
|
return $this->transformPayment($payment);
|
||||||
})->toArray();
|
})->toArray();
|
||||||
}
|
}
|
||||||
@ -530,8 +557,8 @@ class TemplateService
|
|||||||
'refund_activity' => $this->getPaymentRefundActivity($payment),
|
'refund_activity' => $this->getPaymentRefundActivity($payment),
|
||||||
];
|
];
|
||||||
|
|
||||||
nlog($this->getPaymentRefundActivity($payment));
|
nlog($data);
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user