Refactor for e-invoices

This commit is contained in:
David Bomba 2023-04-21 15:44:11 +10:00
parent 28637ae78c
commit f0487b2560
10 changed files with 65 additions and 36 deletions

View File

@ -12,7 +12,7 @@
namespace App\Jobs\Entity; namespace App\Jobs\Entity;
use App\Exceptions\FilePermissionsFailure; use App\Exceptions\FilePermissionsFailure;
use App\Jobs\Invoice\CreateXInvoice; use App\Jobs\Invoice\CreateEInvoice;
use App\Libraries\MultiDB; use App\Libraries\MultiDB;
use App\Models\Credit; use App\Models\Credit;
use App\Models\CreditInvitation; use App\Models\CreditInvitation;
@ -213,7 +213,7 @@ class CreateEntityPdf implements ShouldQueue
} }
} }
if ($this->entity_string == "invoice" && $this->company->enable_e_invoice){ if ($this->entity_string == "invoice" && $this->company->enable_e_invoice){
(new CreateXInvoice($this->entity, true))->handle(); (new CreateEInvoice($this->entity, true))->handle();
} }
$this->invitation = null; $this->invitation = null;
$this->entity = null; $this->entity = null;

View File

@ -2,23 +2,18 @@
namespace App\Jobs\Invoice; namespace App\Jobs\Invoice;
use App\Utils\Ninja;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\Product;
use App\Services\Invoice\EInvoice\FacturaEInvoice;
use App\Services\Invoice\EInvoice\ZugferdEInvoice;
use horstoeko\zugferd\codelists\ZugferdDutyTaxFeeCategories;
use horstoeko\zugferd\ZugferdDocumentBuilder;
use horstoeko\zugferd\ZugferdDocumentPdfBuilder;
use horstoeko\zugferd\ZugferdProfiles;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Support\Facades\App;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use App\Services\Invoice\EInvoice\FacturaEInvoice;
use Illuminate\Queue\SerializesModels; use App\Services\Invoice\EInvoice\ZugferdEInvoice;
use Illuminate\Support\Facades\Storage;
class CreateEInvoice implements ShouldQueue
class CreateXInvoice implements ShouldQueue
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
@ -36,8 +31,20 @@ class CreateXInvoice implements ShouldQueue
*/ */
public function handle(): string public function handle(): string
{ {
/* Forget the singleton*/
App::forgetInstance('translator');
switch ($this->invoice->client->getSetting('e_invoice_type')) { /* Init a new copy of the translator*/
$t = app('translator');
/* Set the locale*/
App::setLocale($this->invoice->client->locale());
/* Set customized translations _NOW_ */
$t->replace(Ninja::transformTranslations($this->invoice->client->getMergedSettings()));
$e_invoice_type = $this->invoice->client->getSetting('e_invoice_type');
switch ($e_invoice_type) {
case "EN16931": case "EN16931":
case "XInvoice_2_2": case "XInvoice_2_2":
case "XInvoice_2_1": case "XInvoice_2_1":
@ -47,12 +54,15 @@ class CreateXInvoice implements ShouldQueue
case "XInvoice-BasicWL": case "XInvoice-BasicWL":
case "XInvoice-Basic": case "XInvoice-Basic":
return (new ZugferdEInvoice($this->invoice, $this->alterPDF, $this->custom_pdf_path))->run(); return (new ZugferdEInvoice($this->invoice, $this->alterPDF, $this->custom_pdf_path))->run();
case "Facturae_3_2_2": case "Facturae_3.2":
return (new FacturaEInvoice($this->invoice))->run(); case "Facturae_3.2.1":
case "Facturae_3.2.2":
return (new FacturaEInvoice($this->invoice, str_replace("Facturae_", "", $e_invoice_type)))->run();
default: default:
return (new ZugferdEInvoice($this->invoice, $this->alterPDF, $this->custom_pdf_path))->run(); return (new ZugferdEInvoice($this->invoice, $this->alterPDF, $this->custom_pdf_path))->run();
break; break;
} }
} }
} }

View File

@ -79,7 +79,7 @@ class ZipInvoices implements ShouldQueue
$this->invoices->each(function ($invoice) { $this->invoices->each(function ($invoice) {
(new CreateEntityPdf($invoice->invitations()->first()))->handle(); (new CreateEntityPdf($invoice->invitations()->first()))->handle();
if ($this->company->use_xinvoice){ if ($this->company->use_xinvoice){
(new CreateXInvoice($invoice, false))->handle(); (new CreateEInvoice($invoice, false))->handle();
} }
}); });

View File

@ -11,7 +11,7 @@
namespace App\Services\Email; namespace App\Services\Email;
use App\Jobs\Invoice\CreateXInvoice; use App\Jobs\Invoice\CreateEInvoice;
use App\Services\Invoice\GetInvoiceXInvoice; use App\Services\Invoice\GetInvoiceXInvoice;
use App\DataMapper\EmailTemplateDefaults; use App\DataMapper\EmailTemplateDefaults;
use App\Jobs\Entity\CreateRawPdf; use App\Jobs\Entity\CreateRawPdf;
@ -301,7 +301,7 @@ class EmailDefaults
if ($this->email->email_object->company->enable_e_invoice && $this->email->email_object->entity instanceof Invoice) { if ($this->email->email_object->company->enable_e_invoice && $this->email->email_object->entity instanceof Invoice) {
$tempfile = tmpfile(); $tempfile = tmpfile();
file_put_contents(stream_get_meta_data($tempfile)['uri'], $pdf); file_put_contents(stream_get_meta_data($tempfile)['uri'], $pdf);
$xinvoice_path = (new CreateXInvoice($this->email->email_object->entity, true, stream_get_meta_data($tempfile)['uri']))->handle(); $xinvoice_path = (new CreateEInvoice($this->email->email_object->entity, true, stream_get_meta_data($tempfile)['uri']))->handle();
$this->email->email_object->attachments = array_merge($this->email->email_object->attachments, [['file' => base64_encode(file_get_contents(stream_get_meta_data($tempfile)['uri'])), 'name' => $this->email->email_object->entity->numberFormatter().'.pdf']]); $this->email->email_object->attachments = array_merge($this->email->email_object->attachments, [['file' => base64_encode(file_get_contents(stream_get_meta_data($tempfile)['uri'])), 'name' => $this->email->email_object->entity->numberFormatter().'.pdf']]);
$this->email->email_object->attachments = array_merge($this->email->email_object->attachments, [['file' => base64_encode(file_get_contents($xinvoice_path)), 'name' => explode(".", $this->email->email_object->entity->getFileName('xml'))[0]."-xinvoice.xml"]]); $this->email->email_object->attachments = array_merge($this->email->email_object->attachments, [['file' => base64_encode(file_get_contents($xinvoice_path)), 'name' => explode(".", $this->email->email_object->entity->getFileName('xml'))[0]."-xinvoice.xml"]]);
} }

View File

@ -12,10 +12,11 @@
namespace App\Services\Invoice\EInvoice; namespace App\Services\Invoice\EInvoice;
use App\Models\Invoice; use App\Models\Invoice;
use App\Services\AbstractService;
use josemmo\Facturae\Facturae; use josemmo\Facturae\Facturae;
use App\Services\AbstractService;
use josemmo\Facturae\FacturaeItem; use josemmo\Facturae\FacturaeItem;
use josemmo\Facturae\FacturaeParty; use josemmo\Facturae\FacturaeParty;
use Illuminate\Support\Facades\Storage;
class FacturaEInvoice extends AbstractService class FacturaEInvoice extends AbstractService
{ {
@ -110,15 +111,16 @@ class FacturaEInvoice extends AbstractService
// FacturaeCentre::ROLE_B2B_ISSUER Issuer in FACeB2B // FacturaeCentre::ROLE_B2B_ISSUER Issuer in FACeB2B
public function __construct(public Invoice $invoice) public function __construct(public Invoice $invoice, private mixed $profile)
{ {
} }
public function run() public function run()
{ {
$this->calc = $this->invoice->calc(); $this->calc = $this->invoice->calc();
$this->fac = new Facturae(); $this->fac = new Facturae($this->profile);
$this->fac->setNumber('', $this->invoice->number); $this->fac->setNumber('', $this->invoice->number);
$this->fac->setIssueDate($this->invoice->date); $this->fac->setIssueDate($this->invoice->date);
$this->fac->setPrecision(Facturae::PRECISION_LINE); $this->fac->setPrecision(Facturae::PRECISION_LINE);
@ -129,7 +131,17 @@ class FacturaEInvoice extends AbstractService
->setDiscount() ->setDiscount()
->setPoNumber(); ->setPoNumber();
return $this->fac->export();
$disk = config('filesystems.default');
if (!Storage::disk($disk)->exists($this->invoice->client->e_invoice_filepath($this->invoice->invitations->first()))) {
Storage::makeDirectory($this->invoice->client->e_invoice_filepath($this->invoice->invitations->first()));
}
$this->fac->export(Storage::disk($disk)->path($this->invoice->client->e_invoice_filepath($this->invoice->invitations->first()) . $this->invoice->getFileName("xsig")));
return $this->invoice->client->e_invoice_filepath($this->invoice->invitations->first()) . $this->invoice->getFileName("xsig");
} }
private function setPoNumber(): self private function setPoNumber(): self

View File

@ -78,17 +78,21 @@ class ZugferdEInvoice extends AbstractService
->setDocumentBuyerContact($client->primary_contact()->first()->first_name . " " . $client->primary_contact()->first()->last_name, "", $client->primary_contact()->first()->phone, "", $client->primary_contact()->first()->email) ->setDocumentBuyerContact($client->primary_contact()->first()->first_name . " " . $client->primary_contact()->first()->last_name, "", $client->primary_contact()->first()->phone, "", $client->primary_contact()->first()->email)
->setDocumentShipToAddress($client->shipping_address1, $client->shipping_address2, "", $client->shipping_postal_code, $client->shipping_city, $client->shipping_country->iso_3166_2, $client->shipping_state) ->setDocumentShipToAddress($client->shipping_address1, $client->shipping_address2, "", $client->shipping_postal_code, $client->shipping_city, $client->shipping_country->iso_3166_2, $client->shipping_state)
->addDocumentPaymentTerm(ctrans("texts.xinvoice_payable", ['payeddue' => date_create($this->invoice->date)->diff(date_create($this->invoice->due_date))->format("%d"), 'paydate' => $this->invoice->due_date])); ->addDocumentPaymentTerm(ctrans("texts.xinvoice_payable", ['payeddue' => date_create($this->invoice->date)->diff(date_create($this->invoice->due_date))->format("%d"), 'paydate' => $this->invoice->due_date]));
if (!empty($this->invoice->public_notes)) { if (!empty($this->invoice->public_notes)) {
$xrechnung->addDocumentNote($this->invoice->public_notes); $xrechnung->addDocumentNote($this->invoice->public_notes);
} }
if (!empty($this->invoice->po_number)) { if (!empty($this->invoice->po_number)) {
$xrechnung->setDocumentBuyerOrderReferencedDocument($this->invoice->po_number); $xrechnung->setDocumentBuyerOrderReferencedDocument($this->invoice->po_number);
} }
if (empty($client->routing_id)) { if (empty($client->routing_id)) {
$xrechnung->setDocumentBuyerReference(ctrans("texts.xinvoice_no_buyers_reference")); $xrechnung->setDocumentBuyerReference(ctrans("texts.xinvoice_no_buyers_reference"));
} else { } else {
$xrechnung->setDocumentBuyerReference($client->routing_id); $xrechnung->setDocumentBuyerReference($client->routing_id);
} }
$xrechnung->addDocumentPaymentMean(68, ctrans("texts.xinvoice_online_payment")); $xrechnung->addDocumentPaymentMean(68, ctrans("texts.xinvoice_online_payment"));
if (str_contains($company->getSetting('vat_number'), "/")) { if (str_contains($company->getSetting('vat_number'), "/")) {
@ -159,6 +163,7 @@ class ZugferdEInvoice extends AbstractService
$xrechnung->addDocumentTax($this->getTaxType(""), "VAT", $item["total"] / (explode("%", end($tax))[0] / 100), $item["total"], explode("%", end($tax))[0]); $xrechnung->addDocumentTax($this->getTaxType(""), "VAT", $item["total"] / (explode("%", end($tax))[0] / 100), $item["total"], explode("%", end($tax))[0]);
// TODO: Add correct tax type within getTaxType // TODO: Add correct tax type within getTaxType
} }
if (!empty($globaltax && isset($invoicing_data->getTotalTaxMap()[$globaltax]["name"]))) { if (!empty($globaltax && isset($invoicing_data->getTotalTaxMap()[$globaltax]["name"]))) {
$tax = explode(" ", $invoicing_data->getTotalTaxMap()[$globaltax]["name"]); $tax = explode(" ", $invoicing_data->getTotalTaxMap()[$globaltax]["name"]);
$xrechnung->addDocumentTax($this->getTaxType(""), "VAT", $invoicing_data->getTotalTaxMap()[$globaltax]["total"] / (explode("%", end($tax))[0] / 100), $invoicing_data->getTotalTaxMap()[$globaltax]["total"], explode("%", end($tax))[0]); $xrechnung->addDocumentTax($this->getTaxType(""), "VAT", $invoicing_data->getTotalTaxMap()[$globaltax]["total"] / (explode("%", end($tax))[0] / 100), $invoicing_data->getTotalTaxMap()[$globaltax]["total"], explode("%", end($tax))[0]);
@ -166,9 +171,11 @@ class ZugferdEInvoice extends AbstractService
} }
$disk = config('filesystems.default'); $disk = config('filesystems.default');
if (!Storage::exists($client->e_invoice_filepath($this->invoice->invitations->first()))) {
if (!Storage::disk($disk)->exists($client->e_invoice_filepath($this->invoice->invitations->first()))) {
Storage::makeDirectory($client->e_invoice_filepath($this->invoice->invitations->first())); Storage::makeDirectory($client->e_invoice_filepath($this->invoice->invitations->first()));
} }
$xrechnung->writeFile(Storage::disk($disk)->path($client->e_invoice_filepath($this->invoice->invitations->first()) . $this->invoice->getFileName("xml"))); $xrechnung->writeFile(Storage::disk($disk)->path($client->e_invoice_filepath($this->invoice->invitations->first()) . $this->invoice->getFileName("xml")));
// The validity can be checked using https://portal3.gefeg.com/invoice/validation // The validity can be checked using https://portal3.gefeg.com/invoice/validation

View File

@ -11,7 +11,7 @@
namespace App\Services\Invoice; namespace App\Services\Invoice;
use App\Jobs\Invoice\CreateXInvoice; use App\Jobs\Invoice\CreateEInvoice;
use App\Models\ClientContact; use App\Models\ClientContact;
use App\Models\Invoice; use App\Models\Invoice;
use App\Services\AbstractService; use App\Services\AbstractService;
@ -43,7 +43,7 @@ class GetInvoiceXInvoice extends AbstractService
$file = Storage::disk($disk)->exists($file_path); $file = Storage::disk($disk)->exists($file_path);
if (! $file) { if (! $file) {
$file_path = (new CreateXInvoice($this->invoice, false))->handle(); $file_path = (new CreateEInvoice($this->invoice, false))->handle();
} }
return $file_path; return $file_path;

View File

@ -14,7 +14,7 @@ namespace App\Services\Invoice;
use App\Events\Invoice\InvoiceWasArchived; use App\Events\Invoice\InvoiceWasArchived;
use App\Jobs\Entity\CreateEntityPdf; use App\Jobs\Entity\CreateEntityPdf;
use App\Jobs\Inventory\AdjustProductInventory; use App\Jobs\Inventory\AdjustProductInventory;
use App\Jobs\Invoice\CreateXInvoice; use App\Jobs\Invoice\CreateEInvoice;
use App\Libraries\Currency\Conversion\CurrencyApi; use App\Libraries\Currency\Conversion\CurrencyApi;
use App\Models\CompanyGateway; use App\Models\CompanyGateway;
use App\Models\Expense; use App\Models\Expense;
@ -451,7 +451,7 @@ class InvoiceService
(new CreateEntityPdf($invitation))->handle(); (new CreateEntityPdf($invitation))->handle();
if ($invitation instanceof InvoiceInvitation) if ($invitation instanceof InvoiceInvitation)
{ {
(new CreateXInvoice($invitation->invoice, true))->handle(); (new CreateEInvoice($invitation->invoice, true))->handle();
} }
}); });
@ -462,7 +462,7 @@ class InvoiceService
CreateEntityPdf::dispatch($invitation); CreateEntityPdf::dispatch($invitation);
if ($invitation instanceof InvoiceInvitation) if ($invitation instanceof InvoiceInvitation)
{ {
CreateXInvoice::dispatch($invitation->invoice, true); CreateEInvoice::dispatch($invitation->invoice, true);
} }
}); });
} catch (\Exception $e) { } catch (\Exception $e) {

View File

@ -40,7 +40,7 @@ class FacturaeTest extends TestCase
public function testInvoiceGeneration() public function testInvoiceGeneration()
{ {
$f = new FacturaEInvoice($this->invoice); $f = new FacturaEInvoice($this->invoice, "3.2.2");
$f->run(); $f->run();
$this->assertNotNull($f->run()); $this->assertNotNull($f->run());

View File

@ -12,7 +12,7 @@
use Tests\TestCase; use Tests\TestCase;
use Tests\MockAccountData; use Tests\MockAccountData;
use App\Jobs\Entity\CreateEntityPdf; use App\Jobs\Entity\CreateEntityPdf;
use App\Jobs\Invoice\CreateXInvoice; use App\Jobs\Invoice\CreateEInvoice;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use horstoeko\zugferd\ZugferdDocumentReader; use horstoeko\zugferd\ZugferdDocumentReader;
use Illuminate\Routing\Middleware\ThrottleRequests; use Illuminate\Routing\Middleware\ThrottleRequests;
@ -42,7 +42,7 @@ class EInvoiceTest extends TestCase
$this->company->e_invoice_type = "EN16931"; $this->company->e_invoice_type = "EN16931";
$this->invoice->client->routing_id = 'DE123456789'; $this->invoice->client->routing_id = 'DE123456789';
$this->invoice->client->save(); $this->invoice->client->save();
$xinvoice = (new CreateXInvoice($this->invoice, false))->handle(); $xinvoice = (new CreateEInvoice($this->invoice, false))->handle();
$this->assertNotNull($xinvoice); $this->assertNotNull($xinvoice);
$this->assertTrue(Storage::exists($xinvoice)); $this->assertTrue(Storage::exists($xinvoice));
} }
@ -56,7 +56,7 @@ class EInvoiceTest extends TestCase
$this->invoice->client->routing_id = 'DE123456789'; $this->invoice->client->routing_id = 'DE123456789';
$this->invoice->client->save(); $this->invoice->client->save();
$xinvoice = (new CreateXInvoice($this->invoice, false))->handle(); $xinvoice = (new CreateEInvoice($this->invoice, false))->handle();
nlog(Storage::path($xinvoice)); nlog(Storage::path($xinvoice));
$document = ZugferdDocumentReader::readAndGuessFromFile(Storage::path($xinvoice)); $document = ZugferdDocumentReader::readAndGuessFromFile(Storage::path($xinvoice));
$document->getDocumentInformation($documentno, $documenttypecode, $documentdate, $documentcurrency, $taxcurrency, $taxname, $documentlangeuage, $rest); $document->getDocumentInformation($documentno, $documenttypecode, $documentdate, $documentcurrency, $taxcurrency, $taxname, $documentlangeuage, $rest);
@ -69,7 +69,7 @@ class EInvoiceTest extends TestCase
public function checkEmbededPDFFile() public function checkEmbededPDFFile()
{ {
$pdf = (new CreateEntityPdf($this->invoice->invitations()->first()))->handle(); $pdf = (new CreateEntityPdf($this->invoice->invitations()->first()))->handle();
(new CreateXInvoice($this->invoice, true, $pdf))->handle(); (new CreateEInvoice($this->invoice, true, $pdf))->handle();
$document = ZugferdDocumentReader::readAndGuessFromFile($pdf); $document = ZugferdDocumentReader::readAndGuessFromFile($pdf);
$document->getDocumentInformation($documentno, $documenttypecode, $documentdate, $documentcurrency, $taxcurrency, $taxname, $documentlangeuage, $rest); $document->getDocumentInformation($documentno, $documenttypecode, $documentdate, $documentcurrency, $taxcurrency, $taxname, $documentlangeuage, $rest);
$this->assertEquals($this->invoice->number, $documentno); $this->assertEquals($this->invoice->number, $documentno);