Working on importing expense einvoices
This commit is contained in:
parent
a63cc03af5
commit
7402f7c110
|
|
@ -11,10 +11,16 @@
|
|||
|
||||
namespace App\Services\EDocument\Imports;
|
||||
|
||||
use App\Models\Vendor;
|
||||
use App\Models\Company;
|
||||
use App\Models\Country;
|
||||
use App\Factory\VendorFactory;
|
||||
use App\Factory\ExpenseFactory;
|
||||
use App\Factory\VendorContactFactory;
|
||||
use App\Services\AbstractService;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
|
||||
use InvoiceNinja\EInvoice\EInvoice;
|
||||
use App\Utils\Traits\SavesDocuments;
|
||||
|
||||
class UblEDocument extends AbstractService
|
||||
|
|
@ -36,13 +42,102 @@ class UblEDocument extends AbstractService
|
|||
{
|
||||
|
||||
//parse doc
|
||||
// try{
|
||||
$e = new EInvoice();
|
||||
$invoice = $e->decode('Peppol', $this->file->get(), 'xml');
|
||||
|
||||
//append file as an attachment to the document
|
||||
return $this->buildAndSaveExpense($invoice);
|
||||
|
||||
// }
|
||||
// catch(\Throwable $e){
|
||||
// return $e->getMessage();
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
private function buildAndSaveExpense(\InvoiceNinja\EInvoice\Models\Peppol\Invoice $invoice): Expense
|
||||
{
|
||||
|
||||
$vendor = $this->findOrCreateVendor($invoice);
|
||||
|
||||
|
||||
/** @var \App\Models\Expense $expense */
|
||||
$expense = ExpenseFactory::create($this->company->id, $this->company->owner()->id);
|
||||
|
||||
return $expense;
|
||||
|
||||
|
||||
}
|
||||
|
||||
private function findOrCreateVendor(\InvoiceNinja\EInvoice\Models\Peppol\Invoice $invoice): Vendor
|
||||
{
|
||||
$asp = $invoice->AccountingSupplierParty;
|
||||
|
||||
$vat_number = $asp->Party->PartyTaxScheme->CompanyID ?? false;
|
||||
$id_number = $asp->Party->PartyIdentification->ID ?? false;
|
||||
$routing_id = $asp->Party->EndpointID ?? false;
|
||||
$vendor_name = $asp->Party->PartyName->Name ?? false;
|
||||
|
||||
$vendor = Vendor::query()
|
||||
->where('company_id', $this->company->id)
|
||||
->where(function ($q) use ($vat_number, $routing_id, $id_number, $vendor_name){
|
||||
|
||||
$q->when($routing_id, function ($q) use ($routing_id){
|
||||
$q->where('routing_id', $routing_id);
|
||||
})
|
||||
->when($vat_number, function ($q) use ($vat_number){
|
||||
$q->orWhere('vat_number', $vat_number);
|
||||
})
|
||||
->when($id_number, function ($q) use ($id_number){
|
||||
$q->orWhere('id_number', $id_number);
|
||||
})
|
||||
->when($vendor_name, function ($q) use ($vendor_name){
|
||||
$q->orWhere('name', $vendor_name);
|
||||
});
|
||||
|
||||
})->first();
|
||||
|
||||
return $vendor ?? $this->newVendor($invoice);
|
||||
}
|
||||
|
||||
private function newVendor(\InvoiceNinja\EInvoice\Models\Peppol\Invoice $invoice): Vendor
|
||||
{
|
||||
$vendor = VendorFactory::create($this->company->id, $this->company->owner()->id);
|
||||
|
||||
$data = [
|
||||
'name' => data_get($invoice, 'AccountingSupplierParty.Party.PartyName.Name', ''),
|
||||
'routing_id' => data_get($invoice, 'AccountingSupplierParty.Party.EndpointID', ''),
|
||||
'vat_number' => data_get($invoice, 'AccountingSupplierParty.Party.PartyTaxScheme.CompanyID', ''),
|
||||
'address1' => data_get($invoice, 'AccountingSupplierParty.Party.PostalAddress.StreetName',''),
|
||||
'address2' => data_get($invoice, 'AccountingSupplierParty.Party.PostalAddress.AdditionalStreetName',''),
|
||||
'city' => data_get($invoice, 'AccountingSupplierParty.Party.PostalAddress.CityName',''),
|
||||
'state' => data_get($invoice, 'AccountingSupplierParty.Party.PostalAddress.CountrySubentity',''),
|
||||
'postal_code' => data_get($invoice, 'AccountingSupplierParty.Party.PostalAddress.PostalZone',''),
|
||||
'country_id' => $this->resovelCountry(data_get($invoice, 'AccountingSupplierParty.Party.PostalAddress.Country.IdentificationCode','')),
|
||||
];
|
||||
|
||||
$vendor->fill($data);
|
||||
$vendor->save();
|
||||
|
||||
if(data_get($invoice, 'AccountingSupplierParty.Party.Contact',false))
|
||||
{
|
||||
$vc = VendorContactFactory::create($this->company->id, $this->company->owner()->id);
|
||||
$vc->vendor_id = $vendor->id;
|
||||
$vc->first_name = data_get($invoice, 'AccountingSupplierParty.Party.Contact.Name','');
|
||||
$vc->phone = data_get($invoice, 'AccountingSupplierParty.Party.Contact.Telephone', '');
|
||||
$vc->email = data_get($invoice, 'AccountingSupplierParty.Party.Contact.ElectronicMail', '');
|
||||
$vc->save();
|
||||
}
|
||||
|
||||
return $vendor->fresh();
|
||||
|
||||
}
|
||||
|
||||
private function resolveCountry(?string $iso_country_code): int
|
||||
{
|
||||
return Country::query()
|
||||
->where('iso_3166_2', $iso_country_code)
|
||||
->orWhere('iso_3166_3', $iso_country_code)
|
||||
->first() ?? (int)$this->company->settings->country_id;
|
||||
}
|
||||
}
|
||||
|
|
@ -992,7 +992,6 @@ class Peppol extends AbstractService
|
|||
$physical_location->Address = $address;
|
||||
|
||||
$party->PhysicalLocation = $physical_location;
|
||||
;
|
||||
|
||||
$contact = new Contact();
|
||||
$contact->ElectronicMail = $this->invoice->client->present()->email();
|
||||
|
|
|
|||
|
|
@ -45,6 +45,57 @@ class ImportEInvoiceTest extends TestCase
|
|||
$this->assertInstanceOf(\InvoiceNinja\EInvoice\Models\Peppol\Invoice::class, $invoice);
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// public function testHtmlConversion()
|
||||
// {
|
||||
|
||||
// // Load the XML source
|
||||
// $xml = new \DOMDocument();
|
||||
// $xml->load(base_path('tests/Integration/Einvoice/samples/peppol.xml'));
|
||||
|
||||
// // Load XSLT stylesheet
|
||||
// $xsl = new \DOMDocument();
|
||||
// $xsl->load(base_path('tests/Integration/Einvoice/samples/peppol.xslt'));
|
||||
|
||||
// // Configure the transformer
|
||||
// $proc = new \XSLTProcessor();
|
||||
// $proc->importStyleSheet($xsl); // attach the xsl rules
|
||||
|
||||
// $transformed = $proc->transformToXML($xml);
|
||||
|
||||
// // determining if output is html document
|
||||
// $html = $transformed;
|
||||
|
||||
// // splitting up html document at doctype and doc
|
||||
// $html_array = explode("\n", $html, 15);
|
||||
|
||||
// $html_doc = array_pop($html_array);
|
||||
|
||||
// $html_doctype = implode("\n", $html_array);
|
||||
|
||||
// // convert XHTML syntax to HTML5
|
||||
// // <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
||||
// // <!DOCTYPE html>
|
||||
|
||||
|
||||
// $html_doctype = preg_replace("/<!DOCTYPE [^>]+>/", "<!DOCTYPE html>", $html_doctype);
|
||||
|
||||
// // <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
|
||||
// // <html lang="en">
|
||||
|
||||
|
||||
// $html_doctype = preg_replace('/ xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"| xml:lang="[^\"]*\"/', '', $html_doctype);
|
||||
|
||||
|
||||
// // <meta http-equiv="content-type" content="text/html; charset=utf-8" />
|
||||
// // to this --> <meta charset="utf-8" />
|
||||
|
||||
// $html_doctype = preg_replace('/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=(.*[a-z0-9-])\" \/>/i', '<meta charset="\1" />', $html_doctype);
|
||||
|
||||
// $html = $html_doctype . "\n" . $html_doc;
|
||||
// nlog($html);
|
||||
|
||||
// }
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue