2021-09-14 13:55:24 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2021-09-14 13:55:24 +02:00
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Client;
|
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Factory\InvoiceFactory;
|
|
|
|
use App\Factory\InvoiceInvitationFactory;
|
|
|
|
use App\Factory\InvoiceItemFactory;
|
2021-09-14 13:55:24 +02:00
|
|
|
use App\Models\Client;
|
2023-10-12 00:17:28 +02:00
|
|
|
use App\Models\Credit;
|
2021-09-14 13:55:24 +02:00
|
|
|
use App\Models\Design;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Payment;
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Services\PdfMaker\Design as PdfMakerDesign;
|
|
|
|
use App\Services\PdfMaker\PdfMaker;
|
|
|
|
use App\Utils\HostedPDF\NinjaPdf;
|
2021-09-14 13:55:24 +02:00
|
|
|
use App\Utils\HtmlEngine;
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Utils\Number;
|
2023-10-12 00:17:28 +02:00
|
|
|
use App\Utils\PhantomJS\Phantom;
|
2023-10-12 03:13:08 +02:00
|
|
|
use App\Utils\Traits\MakesDates;
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2023-10-12 00:17:28 +02:00
|
|
|
use App\Utils\Traits\Pdf\PdfMaker as PdfMakerTrait;
|
2023-10-26 04:57:44 +02:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
use Illuminate\Support\Carbon;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
2021-09-14 13:55:24 +02:00
|
|
|
|
|
|
|
class Statement
|
|
|
|
{
|
|
|
|
use PdfMakerTrait;
|
2023-09-25 05:19:08 +02:00
|
|
|
use MakesHash;
|
2023-10-12 03:13:08 +02:00
|
|
|
use MakesDates;
|
2021-09-14 13:55:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Invoice|Payment|null
|
|
|
|
*/
|
|
|
|
protected $entity;
|
|
|
|
|
2021-09-16 15:04:24 +02:00
|
|
|
protected bool $rollback = false;
|
|
|
|
|
2023-10-27 08:13:23 +02:00
|
|
|
private array $variables = [];
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
public function __construct(protected Client $client, public array $options)
|
|
|
|
{
|
|
|
|
}
|
2021-09-14 13:55:24 +02:00
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
public function run(): ?string
|
2021-09-14 13:55:24 +02:00
|
|
|
{
|
2023-11-07 22:59:49 +01:00
|
|
|
|
2024-10-01 04:51:44 +02:00
|
|
|
try {
|
|
|
|
$this
|
|
|
|
->setupOptions()
|
|
|
|
->setupEntity();
|
2021-09-14 13:55:24 +02:00
|
|
|
|
2024-10-01 04:51:44 +02:00
|
|
|
$html = new HtmlEngine($this->getInvitation());
|
2021-09-14 13:55:24 +02:00
|
|
|
|
2024-10-01 04:51:44 +02:00
|
|
|
$variables = [];
|
|
|
|
$variables = $html->generateLabelsAndValues();
|
2024-06-14 09:09:44 +02:00
|
|
|
|
2024-10-01 04:51:44 +02:00
|
|
|
$option_template = &$this->options['template'];
|
2023-10-12 00:17:28 +02:00
|
|
|
|
2024-10-01 04:51:44 +02:00
|
|
|
$custom_statement_template = \App\Models\Design::where('id', $this->decodePrimaryKey($this->client->getSetting('statement_design_id')))->where('is_template', true)->first();
|
2024-04-19 06:26:15 +02:00
|
|
|
|
2024-10-01 04:51:44 +02:00
|
|
|
if($custom_statement_template || $option_template && $option_template != '') {
|
2023-10-12 03:13:08 +02:00
|
|
|
|
2024-10-01 04:51:44 +02:00
|
|
|
$variables['values']['$start_date'] = $this->translateDate($this->options['start_date'], $this->client->date_format(), $this->client->locale());
|
|
|
|
$variables['values']['$end_date'] = $this->translateDate($this->options['end_date'], $this->client->date_format(), $this->client->locale());
|
|
|
|
$variables['labels']['$start_date_label'] = ctrans('texts.start_date');
|
|
|
|
$variables['labels']['$end_date_label'] = ctrans('texts.end_date');
|
2023-10-12 03:13:08 +02:00
|
|
|
|
2024-10-01 04:51:44 +02:00
|
|
|
return $this->templateStatement($variables);
|
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2024-10-01 04:51:44 +02:00
|
|
|
if ($this->getDesign()->is_custom) {
|
|
|
|
$this->options['custom_partials'] = \json_decode(\json_encode($this->getDesign()->design), true);
|
2021-09-14 13:55:24 +02:00
|
|
|
|
2024-10-01 04:51:44 +02:00
|
|
|
$template = new PdfMakerDesign(\App\Services\PdfMaker\Design::CUSTOM, $this->options);
|
|
|
|
} else {
|
|
|
|
$template = new PdfMakerDesign(strtolower($this->getDesign()->name), $this->options);
|
|
|
|
}
|
2021-09-14 13:55:24 +02:00
|
|
|
|
2024-10-01 04:51:44 +02:00
|
|
|
$variables = $html->generateLabelsAndValues();
|
|
|
|
$variables['values']['$show_paid_stamp'] = 'none'; //do not show paid stamp on statement
|
|
|
|
|
|
|
|
$state = [
|
|
|
|
'template' => $template->elements([
|
|
|
|
'client' => $this->client,
|
|
|
|
'entity' => $this->entity,
|
|
|
|
'pdf_variables' => (array) $this->entity->company->settings->pdf_variables,
|
|
|
|
'$product' => $this->getDesign()->design->product,
|
|
|
|
'variables' => $variables,
|
|
|
|
'invoices' => $this->getInvoices()->cursor(),
|
|
|
|
'payments' => $this->getPayments()->cursor(),
|
|
|
|
'credits' => $this->getCredits()->cursor(),
|
|
|
|
'aging' => $this->getAging(),
|
2024-10-07 01:39:10 +02:00
|
|
|
'unapplied' => $this->getUnapplied()->cursor(),
|
2024-10-01 04:51:44 +02:00
|
|
|
], \App\Services\PdfMaker\Design::STATEMENT),
|
2021-09-14 13:55:24 +02:00
|
|
|
'variables' => $variables,
|
2024-10-01 04:51:44 +02:00
|
|
|
'options' => [
|
|
|
|
],
|
|
|
|
'process_markdown' => $this->entity->client->company->markdown_enabled,
|
|
|
|
];
|
|
|
|
|
|
|
|
$maker = new PdfMaker($state);
|
|
|
|
|
|
|
|
$maker
|
|
|
|
->design($template)
|
|
|
|
->build();
|
|
|
|
|
|
|
|
$pdf = null;
|
|
|
|
$html = $maker->getCompiledHTML(true);
|
|
|
|
|
2024-10-07 01:39:10 +02:00
|
|
|
nlog($html);
|
|
|
|
|
2024-10-01 04:51:44 +02:00
|
|
|
if ($this->rollback) {
|
|
|
|
\DB::connection(config('database.default'))->rollBack();
|
|
|
|
$this->rollback = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$pdf = $this->convertToPdf($html);
|
|
|
|
|
|
|
|
$this->setVariables($variables);
|
|
|
|
|
|
|
|
$maker = null;
|
|
|
|
$state = null;
|
|
|
|
|
|
|
|
return $pdf;
|
2023-09-25 05:19:08 +02:00
|
|
|
}
|
2024-10-01 04:51:44 +02:00
|
|
|
catch(\Throwable $th){
|
|
|
|
|
|
|
|
nlog("STATEMENT:: Throwable::" . $th->getMessage());
|
2023-09-25 05:19:08 +02:00
|
|
|
|
2024-10-01 04:51:44 +02:00
|
|
|
if ($this->rollback) {
|
|
|
|
\DB::connection(config('database.default'))->rollBack();
|
|
|
|
}
|
2021-09-14 13:55:24 +02:00
|
|
|
|
2024-10-01 04:51:44 +02:00
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2024-10-01 04:51:44 +02:00
|
|
|
return null;
|
2021-09-14 13:55:24 +02:00
|
|
|
|
2023-10-12 00:17:28 +02:00
|
|
|
}
|
|
|
|
|
2023-10-27 08:13:23 +02:00
|
|
|
public function setVariables($variables): self
|
|
|
|
{
|
|
|
|
$this->variables = $variables;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getVariables(): array
|
|
|
|
{
|
|
|
|
return $this->variables;
|
|
|
|
}
|
|
|
|
|
2023-10-12 00:17:28 +02:00
|
|
|
private function templateStatement($variables)
|
2023-10-12 03:13:08 +02:00
|
|
|
{
|
2023-10-26 04:57:44 +02:00
|
|
|
if(isset($this->options['template'])) {
|
2023-10-12 00:17:28 +02:00
|
|
|
$statement_design_id = $this->options['template'];
|
2023-10-26 04:57:44 +02:00
|
|
|
} else {
|
2023-10-12 00:17:28 +02:00
|
|
|
$statement_design_id = $this->client->getSetting('statement_design_id');
|
2023-10-26 04:57:44 +02:00
|
|
|
}
|
2023-10-12 00:17:28 +02:00
|
|
|
|
2023-11-15 03:25:34 +01:00
|
|
|
$template = Design::query()
|
|
|
|
->where('id', $this->decodePrimaryKey($statement_design_id))
|
2023-10-12 00:17:28 +02:00
|
|
|
->where('company_id', $this->client->company_id)
|
|
|
|
->first();
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-11-23 06:13:06 +01:00
|
|
|
$ts = $template->service();
|
|
|
|
$ts->addGlobal(['show_credits' => $this->options['show_credits_table']]);
|
|
|
|
$ts->addGlobal(['show_aging' => $this->options['show_aging_table']]);
|
|
|
|
$ts->addGlobal(['show_payments' => $this->options['show_payments_table']]);
|
2024-03-15 01:01:44 +01:00
|
|
|
$ts->addGlobal(['currency_code' => $this->client->company->currency()->code]);
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-11-23 06:13:06 +01:00
|
|
|
$ts->build([
|
2023-10-12 03:13:08 +02:00
|
|
|
'variables' => collect([$variables]),
|
2023-10-12 00:17:28 +02:00
|
|
|
'invoices' => $this->getInvoices()->get(),
|
2023-10-12 03:13:08 +02:00
|
|
|
'payments' => $this->options['show_payments_table'] ? $this->getPayments()->get() : collect([]),
|
2023-10-13 04:43:59 +02:00
|
|
|
'credits' => $this->options['show_credits_table'] ? $this->getCredits()->get() : collect([]),
|
|
|
|
'aging' => $this->options['show_aging_table'] ? $this->getAging() : collect([]),
|
2024-10-07 01:39:10 +02:00
|
|
|
'unapplied' => $this->options['show_payments_table'] ? $this->getPayments()->get() : collect([]),
|
2023-10-12 00:17:28 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$html = $ts->getHtml();
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-10-12 00:17:28 +02:00
|
|
|
return $this->convertToPdf($html);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function convertToPdf(string $html): mixed
|
|
|
|
{
|
|
|
|
$pdf = false;
|
2021-09-14 13:55:24 +02:00
|
|
|
|
2024-10-01 04:51:44 +02:00
|
|
|
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
|
|
|
|
$pdf = (new Phantom())->convertHtmlToPdf($html);
|
|
|
|
} elseif (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
|
|
|
|
$pdf = (new NinjaPdf())->build($html);
|
|
|
|
} else {
|
|
|
|
$pdf = $this->makePdf(null, null, $html);
|
2021-09-14 13:55:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $pdf;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Setup correct entity instance.
|
|
|
|
*
|
|
|
|
* @return Statement
|
|
|
|
*/
|
|
|
|
protected function setupEntity(): self
|
|
|
|
{
|
2021-12-08 00:16:13 +01:00
|
|
|
if ($this->getInvoices()->count() >= 1) {
|
2021-09-14 13:55:24 +02:00
|
|
|
$this->entity = $this->getInvoices()->first();
|
|
|
|
}
|
|
|
|
|
2021-09-16 15:04:24 +02:00
|
|
|
if (\is_null($this->entity)) {
|
2023-01-17 09:42:34 +01:00
|
|
|
DB::connection(config('database.default'))->beginTransaction();
|
2021-12-18 22:54:38 +01:00
|
|
|
|
2021-09-16 15:04:24 +02:00
|
|
|
$this->rollback = true;
|
2024-10-01 04:51:44 +02:00
|
|
|
|
2021-09-16 15:04:24 +02:00
|
|
|
$invoice = InvoiceFactory::create($this->client->company->id, $this->client->user->id);
|
|
|
|
$invoice->client_id = $this->client->id;
|
|
|
|
$invoice->line_items = $this->buildLineItems();
|
|
|
|
$invoice->save();
|
|
|
|
|
|
|
|
$invitation = InvoiceInvitationFactory::create($invoice->company_id, $invoice->user_id);
|
|
|
|
$invitation->invoice_id = $invoice->id;
|
|
|
|
$invitation->client_contact_id = $this->client->contacts->first()->id;
|
|
|
|
$invitation->save();
|
|
|
|
|
|
|
|
$this->entity = $invoice;
|
|
|
|
}
|
|
|
|
|
2021-09-14 13:55:24 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-09-16 15:04:24 +02:00
|
|
|
protected function buildLineItems($count = 1)
|
|
|
|
{
|
|
|
|
$line_items = [];
|
|
|
|
|
|
|
|
for ($x = 0; $x < $count; $x++) {
|
|
|
|
$item = InvoiceItemFactory::create();
|
|
|
|
$item->quantity = 1;
|
|
|
|
//$item->cost = 10;
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$item->tax_name1 = 'GST';
|
|
|
|
$item->tax_rate1 = 10.00;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$item->tax_name1 = 'VAT';
|
|
|
|
$item->tax_rate1 = 17.50;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$item->tax_name1 = 'Sales Tax';
|
|
|
|
$item->tax_rate1 = 5;
|
|
|
|
}
|
|
|
|
|
2022-01-27 06:15:30 +01:00
|
|
|
//$product = Product::first();
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
$product = new \stdClass();
|
2022-01-27 06:15:30 +01:00
|
|
|
|
|
|
|
$item->cost = (float) 10;
|
|
|
|
$item->product_key = 'test';
|
|
|
|
$item->notes = 'test notes';
|
|
|
|
$item->custom_value1 = 'custom value1';
|
|
|
|
$item->custom_value2 = 'custom value2';
|
|
|
|
$item->custom_value3 = 'custom value3';
|
|
|
|
$item->custom_value4 = 'custom value4';
|
2021-09-16 15:04:24 +02:00
|
|
|
|
|
|
|
$line_items[] = $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $line_items;
|
|
|
|
}
|
|
|
|
|
2021-09-14 13:55:24 +02:00
|
|
|
/**
|
|
|
|
* Setup & prepare options.
|
|
|
|
*
|
|
|
|
* @return Statement
|
|
|
|
*/
|
|
|
|
protected function setupOptions(): self
|
|
|
|
{
|
2021-09-15 17:21:31 +02:00
|
|
|
if (! \array_key_exists('start_date', $this->options)) {
|
2021-09-14 13:55:24 +02:00
|
|
|
$this->options['start_date'] = now()->startOfYear()->format('Y-m-d');
|
|
|
|
}
|
|
|
|
|
2021-09-15 17:21:31 +02:00
|
|
|
if (! \array_key_exists('end_date', $this->options)) {
|
2021-09-14 13:55:24 +02:00
|
|
|
$this->options['end_date'] = now()->format('Y-m-d');
|
|
|
|
}
|
|
|
|
|
2021-09-15 17:21:31 +02:00
|
|
|
if (! \array_key_exists('show_payments_table', $this->options)) {
|
2021-09-14 13:55:24 +02:00
|
|
|
$this->options['show_payments_table'] = false;
|
|
|
|
}
|
|
|
|
|
2021-09-15 17:21:31 +02:00
|
|
|
if (! \array_key_exists('show_aging_table', $this->options)) {
|
2021-09-14 13:55:24 +02:00
|
|
|
$this->options['show_aging_table'] = false;
|
|
|
|
}
|
|
|
|
|
2023-04-19 04:31:27 +02:00
|
|
|
if (! \array_key_exists('show_credits_table', $this->options)) {
|
|
|
|
$this->options['show_credits_table'] = false;
|
|
|
|
}
|
|
|
|
|
2023-06-13 21:23:54 +02:00
|
|
|
if (!\array_key_exists('only_clients_with_invoices', $this->options)) {
|
|
|
|
$this->options['only_clients_with_invoices'] = false;
|
|
|
|
}
|
2023-04-19 04:31:27 +02:00
|
|
|
|
2021-09-14 13:55:24 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The collection of invoices for the statement.
|
|
|
|
*
|
2023-10-12 00:17:28 +02:00
|
|
|
* @return Builder
|
2021-09-14 13:55:24 +02:00
|
|
|
*/
|
2023-10-12 00:17:28 +02:00
|
|
|
public function getInvoices(): Builder
|
2021-09-14 13:55:24 +02:00
|
|
|
{
|
2023-06-15 19:46:11 +02:00
|
|
|
return Invoice::withTrashed()
|
2022-06-22 06:26:10 +02:00
|
|
|
->with('payments.type')
|
2021-10-13 08:29:23 +02:00
|
|
|
->where('is_deleted', false)
|
|
|
|
->where('company_id', $this->client->company_id)
|
2021-09-14 13:55:24 +02:00
|
|
|
->where('client_id', $this->client->id)
|
2022-01-03 08:22:10 +01:00
|
|
|
->whereIn('status_id', $this->invoiceStatuses())
|
2023-06-15 19:46:11 +02:00
|
|
|
->whereBetween('date', [Carbon::parse($this->options['start_date']), Carbon::parse($this->options['end_date'])])
|
2022-04-19 01:56:26 +02:00
|
|
|
->orderBy('due_date', 'ASC')
|
2023-10-12 00:17:28 +02:00
|
|
|
->orderBy('date', 'ASC');
|
2021-09-14 13:55:24 +02:00
|
|
|
}
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
private function invoiceStatuses(): array
|
2022-01-03 08:22:10 +01:00
|
|
|
{
|
|
|
|
$status = 'all';
|
2022-01-11 10:01:03 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (array_key_exists('status', $this->options)) {
|
2022-01-03 08:22:10 +01:00
|
|
|
$status = $this->options['status'];
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-01-03 08:22:10 +01:00
|
|
|
|
|
|
|
switch ($status) {
|
|
|
|
case 'all':
|
|
|
|
return [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL, Invoice::STATUS_PAID];
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2022-01-03 08:22:10 +01:00
|
|
|
case 'paid':
|
2022-05-13 09:20:16 +02:00
|
|
|
return [Invoice::STATUS_PAID];
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2022-01-03 08:22:10 +01:00
|
|
|
case 'unpaid':
|
2022-05-13 09:20:16 +02:00
|
|
|
return [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL];
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2022-01-03 08:22:10 +01:00
|
|
|
default:
|
|
|
|
return [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL, Invoice::STATUS_PAID];
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2022-01-03 08:22:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-14 13:55:24 +02:00
|
|
|
/**
|
|
|
|
* The collection of payments for the statement.
|
|
|
|
*
|
2023-10-12 00:17:28 +02:00
|
|
|
* @return Builder
|
2021-09-14 13:55:24 +02:00
|
|
|
*/
|
2023-10-12 00:17:28 +02:00
|
|
|
protected function getPayments(): Builder
|
2021-09-14 13:55:24 +02:00
|
|
|
{
|
2023-06-15 19:46:11 +02:00
|
|
|
return Payment::withTrashed()
|
2022-06-21 11:57:17 +02:00
|
|
|
->with('client.country', 'invoices')
|
2021-10-12 10:10:43 +02:00
|
|
|
->where('company_id', $this->client->company_id)
|
2021-09-14 13:55:24 +02:00
|
|
|
->where('client_id', $this->client->id)
|
|
|
|
->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment::STATUS_PARTIALLY_REFUNDED, Payment::STATUS_REFUNDED])
|
2023-06-15 19:46:11 +02:00
|
|
|
->whereBetween('date', [Carbon::parse($this->options['start_date']), Carbon::parse($this->options['end_date'])])
|
2024-10-07 01:39:10 +02:00
|
|
|
->where('is_deleted', false)
|
2023-10-12 00:17:28 +02:00
|
|
|
->orderBy('date', 'ASC');
|
2021-09-14 13:55:24 +02:00
|
|
|
}
|
|
|
|
|
2024-10-07 01:39:10 +02:00
|
|
|
protected function getUnapplied(): Builder
|
|
|
|
{
|
|
|
|
return Payment::query()
|
|
|
|
->withTrashed()
|
|
|
|
->where('company_id', $this->client->company_id)
|
|
|
|
->where('client_id', $this->client->id)
|
|
|
|
->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment::STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED, Payment::STATUS_REFUNDED])
|
|
|
|
->where('is_deleted', 0)
|
|
|
|
->whereRaw('payments.amount > payments.applied');
|
|
|
|
}
|
|
|
|
|
2023-04-19 04:31:27 +02:00
|
|
|
/**
|
|
|
|
* The collection of credits for the statement.
|
|
|
|
*
|
2023-10-12 00:17:28 +02:00
|
|
|
* @return Builder
|
2023-04-19 04:31:27 +02:00
|
|
|
*/
|
2023-10-12 00:17:28 +02:00
|
|
|
protected function getCredits(): Builder
|
2023-04-19 04:31:27 +02:00
|
|
|
{
|
2023-06-15 19:46:11 +02:00
|
|
|
return Credit::withTrashed()
|
2023-10-26 04:57:44 +02:00
|
|
|
->with('client.country', 'invoice')
|
2023-04-19 04:31:27 +02:00
|
|
|
->where('is_deleted', false)
|
|
|
|
->where('company_id', $this->client->company_id)
|
|
|
|
->where('client_id', $this->client->id)
|
|
|
|
->whereIn('status_id', [Credit::STATUS_SENT, Credit::STATUS_PARTIAL, Credit::STATUS_APPLIED])
|
2023-06-15 19:46:11 +02:00
|
|
|
->whereBetween('date', [Carbon::parse($this->options['start_date']), Carbon::parse($this->options['end_date'])])
|
|
|
|
->where(function ($query) {
|
2024-05-11 01:22:59 +02:00
|
|
|
$query->whereDate('due_date', '>=', now())
|
2023-06-15 19:46:11 +02:00
|
|
|
->orWhereNull('due_date');
|
|
|
|
})
|
2023-10-12 00:17:28 +02:00
|
|
|
->orderBy('date', 'ASC');
|
2023-04-19 04:31:27 +02:00
|
|
|
}
|
|
|
|
|
2021-09-14 13:55:24 +02:00
|
|
|
/**
|
|
|
|
* Get correct invitation ID.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected function getInvitation()
|
|
|
|
{
|
|
|
|
if ($this->entity instanceof Invoice || $this->entity instanceof Payment) {
|
|
|
|
return $this->entity->invitations->first();
|
|
|
|
}
|
2021-09-16 15:04:24 +02:00
|
|
|
|
2021-09-14 13:55:24 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the array of aging data.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getAging(): array
|
|
|
|
{
|
|
|
|
return [
|
2023-11-23 06:13:06 +01:00
|
|
|
ctrans('texts.current') => $this->getAgingAmount('0'),
|
2021-09-14 13:55:24 +02:00
|
|
|
'0-30' => $this->getAgingAmount('30'),
|
|
|
|
'30-60' => $this->getAgingAmount('60'),
|
|
|
|
'60-90' => $this->getAgingAmount('90'),
|
|
|
|
'90-120' => $this->getAgingAmount('120'),
|
|
|
|
'120+' => $this->getAgingAmount('120+'),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate aging amount.
|
|
|
|
*
|
|
|
|
* @param mixed $range
|
|
|
|
* @return string
|
|
|
|
*/
|
2023-10-12 03:13:08 +02:00
|
|
|
private function getAgingAmount($range): string
|
2021-09-14 13:55:24 +02:00
|
|
|
{
|
|
|
|
$ranges = $this->calculateDateRanges($range);
|
2021-09-21 14:50:44 +02:00
|
|
|
|
2021-09-14 13:55:24 +02:00
|
|
|
$from = $ranges[0];
|
|
|
|
$to = $ranges[1];
|
|
|
|
|
2023-11-23 06:13:06 +01:00
|
|
|
$query = Invoice::withTrashed()
|
2021-10-13 08:29:23 +02:00
|
|
|
->where('client_id', $this->client->id)
|
2021-09-21 14:45:28 +02:00
|
|
|
->where('company_id', $this->client->company_id)
|
2021-09-14 13:55:24 +02:00
|
|
|
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
|
|
|
|
->where('balance', '>', 0)
|
2023-11-23 06:13:06 +01:00
|
|
|
->where('is_deleted', 0);
|
|
|
|
|
2023-11-26 08:41:42 +01:00
|
|
|
if($range == '0') {
|
|
|
|
$query->where(function ($q) use ($to, $from) {
|
|
|
|
$q->whereBetween('due_date', [$to, $from])->orWhereNull('due_date');
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
$query->whereBetween('due_date', [$to, $from]);
|
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-11-26 08:41:42 +01:00
|
|
|
$amount = $query->sum('balance');
|
2021-09-14 13:55:24 +02:00
|
|
|
|
2021-10-12 10:10:43 +02:00
|
|
|
return Number::formatMoney($amount, $this->client);
|
2021-09-14 13:55:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate date ranges for aging.
|
|
|
|
*
|
|
|
|
* @param mixed $range
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function calculateDateRanges($range)
|
|
|
|
{
|
|
|
|
$ranges = [];
|
|
|
|
|
|
|
|
switch ($range) {
|
2023-11-23 06:13:06 +01:00
|
|
|
case '0':
|
|
|
|
$ranges[0] = now()->subYears(50);
|
|
|
|
$ranges[1] = now()->startOfDay()->subMinute();
|
|
|
|
return $ranges;
|
2021-09-14 13:55:24 +02:00
|
|
|
case '30':
|
2021-09-21 14:45:28 +02:00
|
|
|
$ranges[0] = now()->startOfDay();
|
|
|
|
$ranges[1] = now()->startOfDay()->subDays(30);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-09-14 13:55:24 +02:00
|
|
|
return $ranges;
|
|
|
|
case '60':
|
2023-06-09 06:52:59 +02:00
|
|
|
$ranges[0] = now()->startOfDay()->subDays(31);
|
2021-09-21 14:45:28 +02:00
|
|
|
$ranges[1] = now()->startOfDay()->subDays(60);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-09-14 13:55:24 +02:00
|
|
|
return $ranges;
|
|
|
|
case '90':
|
2023-06-09 06:52:59 +02:00
|
|
|
$ranges[0] = now()->startOfDay()->subDays(61);
|
2021-09-21 14:45:28 +02:00
|
|
|
$ranges[1] = now()->startOfDay()->subDays(90);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-09-14 13:55:24 +02:00
|
|
|
return $ranges;
|
|
|
|
case '120':
|
2023-06-09 06:52:59 +02:00
|
|
|
$ranges[0] = now()->startOfDay()->subDays(91);
|
2021-09-21 14:45:28 +02:00
|
|
|
$ranges[1] = now()->startOfDay()->subDays(120);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-09-14 13:55:24 +02:00
|
|
|
return $ranges;
|
|
|
|
case '120+':
|
2023-06-09 06:52:59 +02:00
|
|
|
$ranges[0] = now()->startOfDay()->subDays(121);
|
2021-10-12 23:43:38 +02:00
|
|
|
$ranges[1] = now()->startOfDay()->subYears(20);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-09-14 13:55:24 +02:00
|
|
|
return $ranges;
|
|
|
|
default:
|
2023-06-09 06:52:59 +02:00
|
|
|
$ranges[0] = now()->startOfDay();
|
|
|
|
$ranges[1] = now()->startOfDay()->subDays(30);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-09-14 13:55:24 +02:00
|
|
|
return $ranges;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get correct design for statement.
|
|
|
|
*
|
|
|
|
* @return \App\Models\Design
|
|
|
|
*/
|
|
|
|
protected function getDesign(): Design
|
|
|
|
{
|
|
|
|
$id = 1;
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! empty($this->client->getSetting('entity_design_id'))) {
|
2021-09-14 13:55:24 +02:00
|
|
|
$id = (int) $this->client->getSetting('entity_design_id');
|
|
|
|
}
|
|
|
|
|
2023-04-29 13:01:02 +02:00
|
|
|
return Design::withTrashed()->find($id);
|
2021-09-14 13:55:24 +02:00
|
|
|
}
|
|
|
|
}
|