1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 23:52:33 +01:00
invoiceninja/app/Services/EDocument/Imports/ZugferdEDocument.php

132 lines
6.0 KiB
PHP
Raw Normal View History

2024-06-12 16:14:24 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\EDocument\Imports;
use App\Factory\ExpenseFactory;
use App\Factory\VendorFactory;
use App\Jobs\Util\UploadFile;
2024-06-12 16:17:24 +02:00
use App\Models\Country;
2024-06-12 16:14:24 +02:00
use App\Models\Currency;
use App\Models\Expense;
use App\Models\Vendor;
use App\Services\AbstractService;
use App\Utils\TempFile;
2024-06-22 18:04:35 +02:00
use App\Utils\Traits\SavesDocuments;
2024-06-12 16:14:24 +02:00
use Exception;
use horstoeko\zugferd\ZugferdDocumentReader;
2024-06-12 16:17:24 +02:00
use horstoeko\zugferdvisualizer\renderer\ZugferdVisualizerLaravelRenderer;
2024-06-12 16:14:24 +02:00
use horstoeko\zugferdvisualizer\ZugferdVisualizer;
2024-06-22 18:04:35 +02:00
class ZugferdEDocument extends AbstractService
{
use SavesDocuments;
2024-06-12 16:14:24 +02:00
public ZugferdDocumentReader|string $document;
/**
* @throws Exception
*/
public function __construct(public string $tempdocument, public string $documentname)
{
# curl -X POST http://localhost:8000/api/v1/edocument/upload -H "Content-Type: multipart/form-data" -H "X-API-TOKEN: 7tdDdkz987H3AYIWhNGXy8jTjJIoDhkAclCDLE26cTCj1KYX7EBHC66VEitJwWhn" -H "X-Requested-With: XMLHttpRequest" -F _method=PUT -F documents[]=@einvoice.xml
}
/**
* @throws Exception
*/
public function run(): Expense
{
$user = auth()->user();
$this->document = ZugferdDocumentReader::readAndGuessFromContent($this->tempdocument);
$this->document->getDocumentInformation($documentno, $documenttypecode, $documentdate, $invoiceCurrency, $taxCurrency, $documentname, $documentlanguage, $effectiveSpecifiedPeriod);
$this->document->getDocumentSummation($grandTotalAmount, $duePayableAmount, $lineTotalAmount, $chargeTotalAmount, $allowanceTotalAmount, $taxBasisTotalAmount, $taxTotalAmount, $roundingAmount, $totalPrepaidAmount);
$expense = Expense::where('amount', $grandTotalAmount)->where("transaction_reference", $documentno)->whereDate("date", $documentdate)->first();
if (empty($expense)) {
// The document does not exist as an expense
// Handle accordingly
$visualizer = new ZugferdVisualizer($this->document);
$visualizer->setDefaultTemplate();
2024-06-12 16:17:24 +02:00
$visualizer->setRenderer(app(ZugferdVisualizerLaravelRenderer::class));
2024-06-12 16:14:24 +02:00
$visualizer->setPdfFontDefault("arial");
$visualizer->setPdfPaperSize('A4-P');
2024-06-12 16:17:24 +02:00
$visualizer->setTemplate('edocument.xinvoice');
2024-06-12 16:14:24 +02:00
$expense = ExpenseFactory::create($user->company()->id, $user->id);
$expense->date = $documentdate;
$expense->user_id = $user->id;
$expense->company_id = $user->company->id;
$expense->public_notes = $documentno;
$expense->currency_id = Currency::whereCode($invoiceCurrency)->first()->id;
$expense->save();
$origin_file = TempFile::UploadedFileFromRaw($this->tempdocument, $this->documentname, "application/xml");
2024-06-22 18:04:35 +02:00
$uploaded_file = TempFile::UploadedFileFromRaw($visualizer->renderPdf(), $documentno . "_visualiser.pdf", "application/pdf");
$this->saveDocuments([$origin_file, $uploaded_file], $expense);
2024-06-12 16:14:24 +02:00
$expense->save();
2024-06-22 18:04:35 +02:00
2024-06-12 16:14:24 +02:00
if ($taxCurrency && $taxCurrency != $invoiceCurrency) {
$expense->private_notes = ctrans("texts.tax_currency_mismatch");
}
2024-06-12 16:17:24 +02:00
$expense->uses_inclusive_taxes = True;
2024-06-12 16:14:24 +02:00
$expense->amount = $grandTotalAmount;
$counter = 1;
if ($this->document->firstDocumentTax()) {
do {
$this->document->getDocumentTax($categoryCode, $typeCode, $basisAmount, $calculatedAmount, $rateApplicablePercent, $exemptionReason, $exemptionReasonCode, $lineTotalBasisAmount, $allowanceChargeBasisAmount, $taxPointDate, $dueDateTypeCode);
$expense->{"tax_amount$counter"} = $calculatedAmount;
$expense->{"tax_rate$counter"} = $rateApplicablePercent;
$counter++;
} while ($this->document->nextDocumentTax());
}
$this->document->getDocumentSeller($name, $buyer_id, $buyer_description);
$this->document->getDocumentSellerContact($person_name, $person_department, $contact_phone, $contact_fax, $contact_email);
2024-06-12 16:17:24 +02:00
$this->document->getDocumentSellerAddress($address_1, $address_2, $address_3, $postcode, $city, $country, $subdivision);
2024-06-12 16:14:24 +02:00
$this->document->getDocumentSellerTaxRegistration($taxtype);
$taxid = null;
if (array_key_exists("VA", $taxtype)) {
$taxid = $taxtype["VA"];
}
2024-06-12 16:17:24 +02:00
$vendor = Vendor::where('vat_number', $taxid)->first();
2024-06-12 16:14:24 +02:00
2024-06-12 16:17:24 +02:00
if (!empty($vendor)) {
2024-06-12 16:14:24 +02:00
// Vendor found
$expense->vendor_id = $vendor->id;
} else {
$vendor = VendorFactory::create($user->company()->id, $user->id);
$vendor->name = $name;
if ($taxid != null) {
$vendor->vat_number = $taxid;
}
2024-06-12 16:17:24 +02:00
$vendor->currency_id = Currency::whereCode($invoiceCurrency)->first()->id;
$vendor->phone = $contact_phone;
$vendor->address1 = $address_1;
$vendor->address2 = $address_2;
$vendor->city = $city;
$vendor->postal_code = $postcode;
$vendor->country_id = Country::where('iso_3166_2', $country)->first()->id;
2024-06-12 16:14:24 +02:00
$vendor->save();
$expense->vendor_id = $vendor->id;
}
$expense->transaction_reference = $documentno;
2024-06-22 18:04:35 +02:00
} else {
2024-06-12 16:14:24 +02:00
// The document exists as an expense
// Handle accordingly
nlog("Document already exists");
$expense->private_notes = $expense->private_notes . ctrans("texts.edocument_import_already_exists", ["date" => time()]);
}
$expense->save();
return $expense;
}
}
2024-06-12 16:17:24 +02:00