1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00

Storecove

This commit is contained in:
David Bomba 2024-11-01 13:36:33 +11:00
parent 68b7264c74
commit 5698c59322
7 changed files with 227 additions and 211 deletions

View File

@ -44,7 +44,7 @@ class AllowanceCharges
?string $base_amount_excluding_tax,
?string $amount_including_tax,
?string $base_amount_including_tax,
?Tax $tax,
// ?Tax $tax,
?array $taxes_duties_fees,
?string $reason,
?string $reason_code
@ -85,11 +85,6 @@ class AllowanceCharges
return $this->base_amount_including_tax;
}
public function getTax(): ?Tax
{
return $this->tax;
}
/**
* @return TaxesDutiesFees[]
*/
@ -138,12 +133,6 @@ class AllowanceCharges
return $this;
}
public function setTax(?Tax $tax): self
{
$this->tax = $tax;
return $this;
}
/**
* @param TaxesDutiesFees[] $taxes_duties_fees
*/

View File

@ -2,11 +2,20 @@
namespace App\Services\EDocument\Gateway\Storecove\Models;
use Symfony\Component\Serializer\Attribute\SerializedName;
use Symfony\Component\Serializer\Attribute\SerializedPath;
class Contact
{
#[SerializedPath('[cbc:Name]')]
public ?string $first_name;
public ?string $last_name;
#[SerializedPath('[cbc:ElectronicMail]')]
public ?string $email;
#[SerializedPath('[cbc:Telephone]')]
public ?string $phone;
public ?string $id;

View File

@ -13,10 +13,10 @@ class Invoice
#[SerializedPath('[cbc:ID][#]')]
public $invoice_number;
/** @var ?\DateTime */
// /** @var ?\DateTime */
#[SerializedPath('[cbc:IssueDate]')]
#[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
public ?DateTime $issue_date;
// #[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
public $issue_date;
#[SerializedPath('[cac:AccountingCustomerParty]')]
/** @var ?AccountingCustomerParty */
@ -64,10 +64,10 @@ class Invoice
#[SerializedPath('[cbc:DocumentCurrencyCode][#]')]
public $document_currency_code;
/** @var ?\DateTime */
// /** @var ?\DateTime */
#[SerializedPath('[cbc:DueDate]')]
#[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
public ?DateTime $due_date;
// #[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
public $due_date;
//may need something custom for this
public ?string $invoice_period;
@ -117,7 +117,6 @@ class Invoice
/** @var TaxSubtotals[] */
public $tax_subtotals;
/** @var InvoiceLines[] */
//storecove - no mappings - tax_line_percentages
public ?string $tax_system;
@ -165,11 +164,9 @@ class Invoice
#[SerializedPath('[cac:ProjectReference][cbc:ID][#]')]
public ?string $project_reference;
public function __construct(
?string $invoice_number,
?DateTime $issue_date,
$issue_date,
?AccountingCustomerParty $accounting_customer_party,
?array $invoice_lines,
?string $accounting_cost,
@ -186,7 +183,7 @@ class Invoice
?Delivery $delivery,
?DeliveryTerms $delivery_terms,
?string $document_currency_code,
?DateTime $due_date,
$due_date,
?string $invoice_period,
?array $issue_reasons,
?string $issue_time,
@ -376,7 +373,7 @@ class Invoice
return $this->document_currency_code;
}
public function getDueDate(): ?string
public function getDueDate()
{
return $this->due_date;
}
@ -575,7 +572,7 @@ class Invoice
return $this;
}
public function setIssueDate(?string $issue_date): self
public function setIssueDate($issue_date): self
{
$this->issue_date = $issue_date;
return $this;
@ -686,7 +683,7 @@ class Invoice
return $this;
}
public function setDueDate(?string $due_date): self
public function setDueDate($due_date): self
{
$this->due_date = $due_date;
return $this;

View File

@ -206,11 +206,6 @@ class InvoiceLines
return $this->amount_including_tax;
}
public function getTax(): ?Tax
{
return $this->tax;
}
/**
* @return TaxesDutiesFees[]
*/
@ -351,11 +346,6 @@ class InvoiceLines
return $this;
}
public function setTax(?Tax $tax): self
{
$this->tax = $tax;
return $this;
}
/**
* @param TaxesDutiesFees[] $taxes_duties_fees

View File

@ -18,6 +18,7 @@ class Party
#[SerializedPath('[cac:PostalAddress]')]
public ?Address $address;
#[SerializedPath('[cac:Contact]')]
public ?Contact $contact;
public function __construct(

View File

@ -30,14 +30,14 @@ class StorecoveTransformer implements TransformerInterface
public function transform(mixed $invoice)
{
$this->s_invoice = new StorecoveInvoice();
$this->s_invoice = (new \ReflectionClass(StorecoveInvoice::class))->newInstanceWithoutConstructor();
$this->s_invoice->setDocumentCurrency($invoice->DocumentCurrencyCode ?? '');
$this->s_invoice->setInvoiceNumber($invoice->ID ?? '');
$this->s_invoice->setIssueDate($invoice->IssueDate->format('Y-m-d'));
// $this->s_invoice->setDocumentCurrency($invoice->DocumentCurrencyCode ?? '');
// $this->s_invoice->setInvoiceNumber($invoice->ID ?? '');
// $this->s_invoice->setIssueDate($invoice->IssueDate->format('Y-m-d'));
$this->s_invoice->setTaxPointDate($invoice->IssueDate->format('Y-m-d'));
$this->s_invoice->setDueDate($invoice->DueDate->format('Y-m-d') ?? '');
$this->s_invoice->setNote($invoice->Note ?? '');
// $this->s_invoice->setNote($invoice->Note ?? '');
// Only use this if we are billing for services between a period.
if (isset($invoice->InvoicePeriod[0]) &&
@ -46,144 +46,99 @@ class StorecoveTransformer implements TransformerInterface
$this->s_invoice->setInvoicePeriod("{$invoice->InvoicePeriod[0]->StartDate->format('Y-m-d')} - {$invoice->InvoicePeriod[0]->EndDate->format('Y-m-d')}");
}
if($invoice->BuyerReference ?? false){
$ref = new References(documentId: $invoice->BuyerReference, documentType: 'buyer_reference');
$this->s_invoice->addReferences($ref);
}
if ($invoice->OrderReference->ID ?? false) {
$ref = new References(documentId: $invoice->OrderReference->ID->value, documentType: 'sales_order');
$this->s_invoice->addReferences($ref);
}
// $supplier_contact = new Contact(
// email: $invoice->AccountingSupplierParty->Party->Contact->ElectronicMail,
// firstName: $invoice->AccountingSupplierParty->Party->Contact->Name ?? null,
// phone: $invoice->AccountingSupplierParty->Party->Contact->Telephone ?? null,
// );
if($invoice->AccountingCostCode ?? false){
$this->s_invoice->setAccountingCost($invoice->AccountingCostCode);
}
$customer_company_name = $invoice->AccountingCustomerParty->Party->PartyName[0]->Name ?? '';
$address = new Address(
street1: $invoice->AccountingCustomerParty->Party->PostalAddress->StreetName,
street2: $invoice->AccountingCustomerParty->Party->PostalAddress->AdditionalStreetName ?? null,
city: $invoice->AccountingCustomerParty->Party->PostalAddress->CityName,
zip: $invoice->AccountingCustomerParty->Party->PostalAddress->PostalZone,
county: $invoice->AccountingCustomerParty->Party->PostalAddress->CountrySubentity ?? null,
country: $invoice->AccountingCustomerParty->Party->PostalAddress->Country->IdentificationCode->value,
);
$contact = new Contact(
email: $invoice->AccountingCustomerParty->Party->Contact->ElectronicMail,
firstName: $invoice->AccountingCustomerParty->Party->Contact->Name ?? null,
phone: $invoice->AccountingCustomerParty->Party->Contact->Telephone ?? null,
);
$customer_party = new Party(companyName: $customer_company_name, address: $address, contact: $contact);
$party_identifiers = []; // do this outside the transformer.
$acp = new AccountingCustomerParty($party_identifiers, $customer_party);
$this->s_invoice->setAccountingCustomerParty($acp);
$supplier_contact = new Contact(
email: $invoice->AccountingSupplierParty->Party->Contact->ElectronicMail,
firstName: $invoice->AccountingSupplierParty->Party->Contact->Name ?? null,
phone: $invoice->AccountingSupplierParty->Party->Contact->Telephone ?? null,
);
$supplier_party = new Party(contact: $supplier_contact);
$asp = new AccountingSupplierParty($supplier_party);
$this->s_invoice->setAccountingSupplierParty($asp);
if (isset($invoice->PaymentMeans[0])) {
$payment_means = new PaymentMeans();
$payment_means->setCodeProps($invoice->PaymentMeans[0]);
$this->s_invoice->addPaymentMeans($payment_means);
}
// $supplier_party = new Party(contact: $supplier_contact);
// $asp = new AccountingSupplierParty($supplier_party);
// $this->s_invoice->setAccountingSupplierParty($asp);
$lines = [];
foreach($invoice->InvoiceLine as $peppolLine)
{
// foreach($invoice->InvoiceLine as $peppolLine)
// {
$line = new InvoiceLines();
// $line = (new \ReflectionClass(InvoiceLines::class))->newInstanceWithoutConstructor();
// Basic line details
$line->setLineId($peppolLine->ID->value);
$line->setQuantity((int)$peppolLine->InvoicedQuantity->amount);
$line->setItemPrice((float)$peppolLine->Price->PriceAmount->amount);
$line->setAmountExcludingVat((float)$peppolLine->LineExtensionAmount->amount);
// // Basic line details
// $line->setLineId($peppolLine->ID->value);
// $line->setQuantity((int)$peppolLine->InvoicedQuantity->amount);
// $line->setItemPrice((float)$peppolLine->Price->PriceAmount->amount);
// $line->setAmountExcludingVat((float)$peppolLine->LineExtensionAmount->amount);
// Item details
$line->setName($peppolLine->Item->Name);
$line->setDescription($peppolLine->Item->Description);
// // Item details
// $line->setName($peppolLine->Item->Name);
// $line->setDescription($peppolLine->Item->Description);
// Tax handling
if(isset($peppolLine->Item->ClassifiedTaxCategory) && is_array($peppolLine->Item->ClassifiedTaxCategory)){
foreach($peppolLine->Item->ClassifiedTaxCategory as $ctc)
{
$this->setTaxMap($ctc, $peppolLine, $invoice);
$tax = new Tax((float)$ctc->Percent, $this->resolveJurisdication($ctc, $invoice));
$line->setTax($tax);
}
}
// // Tax handling
// if(isset($peppolLine->Item->ClassifiedTaxCategory) && is_array($peppolLine->Item->ClassifiedTaxCategory)){
// foreach($peppolLine->Item->ClassifiedTaxCategory as $ctc)
// {
// $this->setTaxMap($ctc, $peppolLine, $invoice);
// $tax = new Tax((float)$ctc->Percent, $this->resolveJurisdication($ctc, $invoice));
// $line->setTax($tax);
// }
// }
//discounts
if(isset($peppolLine->Price->AllowanceCharge) && is_array($peppolLine->Price->AllowanceCharge)){
// //discounts
// if(isset($peppolLine->Price->AllowanceCharge) && is_array($peppolLine->Price->AllowanceCharge)){
foreach($peppolLine->Price->AllowanceCharge as $allowance)
{
$reason = isset($allowance->ChargeIndicator) ? ctrans('texts.discount') : ctrans('texts.fee');
$amount = $allowance->Amount->amount;
// foreach($peppolLine->Price->AllowanceCharge as $allowance)
// {
// $reason = isset($allowance->ChargeIndicator) ? ctrans('texts.discount') : ctrans('texts.fee');
// $amount = $allowance->Amount->amount;
$ac = new AllowanceCharges(reason: $reason, amountExcludingTax: $amount);
$line->addAllowanceCharge($ac);
}
}
// $ac = new AllowanceCharges(reason: $reason, amountExcludingTax: $amount);
// $line->addAllowanceCharge($ac);
// }
// }
$lines[] = $line;
// $lines[] = $line;
}
// }
$this->s_invoice->invoiceLines = $lines;
// $this->s_invoice->invoiceLines = $lines;
//invoice level discounts + surcharges
if(isset($peppolLine->AllowanceCharge) && is_array($peppolLine->AllowanceCharge)){
// //invoice level discounts + surcharges
// if(isset($peppolLine->AllowanceCharge) && is_array($peppolLine->AllowanceCharge)){
foreach ($peppolLine->AllowanceCharge as $allowance)
{
// foreach ($peppolLine->AllowanceCharge as $allowance)
// {
$reason = $allowance->ChargeIndicator ? ctrans('texts.fee') : ctrans('texts.discount');
$amount = $allowance->Amount->amount;
// $reason = $allowance->ChargeIndicator ? ctrans('texts.fee') : ctrans('texts.discount');
// $amount = $allowance->Amount->amount;
$ac = new AllowanceCharges(reason: $reason, amountExcludingTax: $amount);
$this->s_invoice->addAllowanceCharge($ac); //todo handle surcharge taxes
// $ac = new AllowanceCharges(reason: $reason, amountExcludingTax: $amount);
// $this->s_invoice->addAllowanceCharge($ac); //todo handle surcharge taxes
}
}
// }
// }
collect($this->tax_map)
->groupBy('percentage')
->each(function ($group) {
// collect($this->tax_map)
// ->groupBy('percentage')
// ->each(function ($group) {
$taxSubtotals = new TaxSubtotals(
taxableAmount: $group->sum('taxableAmount'),
taxAmount: $group->sum('taxAmount'),
percentage: $group->first()['percentage'],
country: $group->first()['country']
);
// $taxSubtotals = new TaxSubtotals(
// taxableAmount: $group->sum('taxableAmount'),
// taxAmount: $group->sum('taxAmount'),
// percentage: $group->first()['percentage'],
// country: $group->first()['country']
// );
$this->s_invoice->addTaxSubtotals($taxSubtotals);
// $this->s_invoice->addTaxSubtotals($taxSubtotals);
});
// });
$this->s_invoice->setAmountIncludingVat($invoice->LegalMonetaryTotal->TaxInclusiveAmount->amount);
$this->s_invoice->setPrepaidAmount(0);
// $this->s_invoice->setAmountIncludingVat($invoice->LegalMonetaryTotal->TaxInclusiveAmount->amount);
// $this->s_invoice->setPrepaidAmount(0);
return $this->s_invoice;
// return $this->s_invoice;
}

View File

@ -31,21 +31,20 @@ use App\Services\EDocument\Gateway\Storecove\Storecove;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;
use InvoiceNinja\EInvoice\Models\Peppol\Invoice as PeppolInvoice;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use App\Services\EDocument\Gateway\Transformers\StorecoveTransformer;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use App\Services\EDocument\Gateway\Storecove\PeppolToStorecoveNormalizer;
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
use App\Services\EDocument\Gateway\Storecove\Models\Invoice as StorecoveInvoice;
use App\Services\EDocument\Gateway\Transformers\StorecoveTransformer;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
class StorecoveTest extends TestCase
{
@ -65,79 +64,155 @@ class StorecoveTest extends TestCase
}
}
public function testNormalizingToStorecove()
public function testTransformPeppolToStorecove()
{
$e_invoice = new \InvoiceNinja\EInvoice\Models\Peppol\Invoice();
$phpDocExtractor = new PhpDocExtractor();
$reflectionExtractor = new ReflectionExtractor();
// list of PropertyListExtractorInterface (any iterable)
$typeExtractors = [$reflectionExtractor,$phpDocExtractor];
// list of PropertyDescriptionExtractorInterface (any iterable)
$descriptionExtractors = [$phpDocExtractor];
// list of PropertyAccessExtractorInterface (any iterable)
$propertyInitializableExtractors = [$reflectionExtractor];
$propertyInfo = new PropertyInfoExtractor(
$propertyInitializableExtractors,
$descriptionExtractors,
$typeExtractors,
);
$xml_encoder = new XmlEncoder(['xml_format_output' => true, 'remove_empty_tags' => true,]);
$json_encoder = new JsonEncoder();
$invoice = $this->createATData();
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
$metadataAwareNameConverter = new MetadataAwareNameConverter($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
$stub = json_decode('{"Invoice":{"Note":"Nooo","PaymentMeans":[{"ID":{"value":"afdasfasdfasdfas"},"PayeeFinancialAccount":{"Name":"PFA-NAME","ID":{"value":"DE89370400440532013000"},"AliasName":"PFA-Alias","AccountTypeCode":{"value":"CHECKING"},"AccountFormatCode":{"value":"IBAN"},"CurrencyCode":{"value":"EUR"},"FinancialInstitutionBranch":{"ID":{"value":"DEUTDEMMXXX"},"Name":"Deutsche Bank"}}}]}}');
foreach ($stub as $key => $value) {
$e_invoice->{$key} = $value;
}
$normalizer = new ObjectNormalizer($classMetadataFactory, $metadataAwareNameConverter, null, $propertyInfo);
$invoice->e_invoice = $e_invoice;
$normalizers = [new DateTimeNormalizer(), $normalizer, new ArrayDenormalizer()];
$encoders = [$xml_encoder, $json_encoder];
$serializer = new Serializer($normalizers, $encoders);
$p = new Peppol($invoice);
$p->run();
$peppolInvoice = $p->getInvoice();
$context = [
DateTimeNormalizer::FORMAT_KEY => 'Y-m-d',
AbstractObjectNormalizer::SKIP_NULL_VALUES => true,
];
$p = file_get_contents(base_path('tests/Integration/Einvoice/samples/peppol.xml'));
$s_transformer = new StorecoveTransformer();
$s_transformer->transform($peppolInvoice);
$e = new \InvoiceNinja\EInvoice\EInvoice();
$peppolInvoice = $e->decode('Peppol', $p, 'xml');
$json = $s_transformer->toJson();
$this->assertInstanceOf(\InvoiceNinja\EInvoice\Models\Peppol\Invoice::class, $peppolInvoice);
$this->assertJson($json);
$parent = \App\Services\EDocument\Gateway\Storecove\Models\Invoice::class;
// $peppolInvoice = $e->encode($peppolInvoice, 'json');
$peppolInvoice = $data = $e->encode($peppolInvoice, 'json', $context);
$invoice = $serializer->deserialize($peppolInvoice, $parent, 'json', $context);
$this->assertInstanceOf($parent, $invoice);
$s_invoice = $serializer->encode($invoice, 'json', $context);
$arr = json_decode($s_invoice, true);
$arr = $this->removeEmptyValues($arr);
nlog($arr);
nlog($json);
}
public function testStorecoveTransformer()
private function removeEmptyValues(array $array): array
{
$e_invoice = new \InvoiceNinja\EInvoice\Models\Peppol\Invoice();
$invoice = $this->createATData();
$item = new InvoiceItem();
$item->product_key = "Product Key";
$item->notes = "Product Description";
$item->cost = 10;
$item->quantity = 10;
$item->is_amount_discount = true;
$item->discount=5;
$item->tax_rate1 = 20;
$item->tax_name1 = 'VAT';
$invoice->line_items = [$item];
$invoice->calc()->getInvoice();
$stub = json_decode('{"Invoice":{"Note":"Nooo","PaymentMeans":[{"ID":{"value":"afdasfasdfasdfas"},"PayeeFinancialAccount":{"Name":"PFA-NAME","ID":{"value":"DE89370400440532013000"},"AliasName":"PFA-Alias","AccountTypeCode":{"value":"CHECKING"},"AccountFormatCode":{"value":"IBAN"},"CurrencyCode":{"value":"EUR"},"FinancialInstitutionBranch":{"ID":{"value":"DEUTDEMMXXX"},"Name":"Deutsche Bank"}}}]}}');
foreach ($stub as $key => $value) {
$e_invoice->{$key} = $value;
}
$invoice->e_invoice = $e_invoice;
$p = new Peppol($invoice);
$p->run();
$peppolInvoice = $p->getInvoice();
$s_transformer = new StorecoveTransformer();
$s_transformer->transform($peppolInvoice);
$json = $s_transformer->toJson();
$this->assertJson($json);
nlog($json);
foreach ($array as $key => $value) {
if (is_array($value)) {
$array[$key] = $this->removeEmptyValues($value);
if (empty($array[$key])) {
unset($array[$key]);
}
} elseif ($value === null || $value === '') {
unset($array[$key]);
}
}
// nlog($array);
return $array;
}
// public function testNormalizingToStorecove()
// {
// $e_invoice = new \InvoiceNinja\EInvoice\Models\Peppol\Invoice();
// $invoice = $this->createATData();
// $stub = json_decode('{"Invoice":{"Note":"Nooo","PaymentMeans":[{"ID":{"value":"afdasfasdfasdfas"},"PayeeFinancialAccount":{"Name":"PFA-NAME","ID":{"value":"DE89370400440532013000"},"AliasName":"PFA-Alias","AccountTypeCode":{"value":"CHECKING"},"AccountFormatCode":{"value":"IBAN"},"CurrencyCode":{"value":"EUR"},"FinancialInstitutionBranch":{"ID":{"value":"DEUTDEMMXXX"},"Name":"Deutsche Bank"}}}]}}');
// foreach ($stub as $key => $value) {
// $e_invoice->{$key} = $value;
// }
// $invoice->e_invoice = $e_invoice;
// $p = new Peppol($invoice);
// $p->run();
// $peppolInvoice = $p->getInvoice();
// $s_transformer = new StorecoveTransformer();
// $s_transformer->transform($peppolInvoice);
// $json = $s_transformer->toJson();
// $this->assertJson($json);
// nlog($json);
// }
// public function testStorecoveTransformer()
// {
// $e_invoice = new \InvoiceNinja\EInvoice\Models\Peppol\Invoice();
// $invoice = $this->createATData();
// $item = new InvoiceItem();
// $item->product_key = "Product Key";
// $item->notes = "Product Description";
// $item->cost = 10;
// $item->quantity = 10;
// $item->is_amount_discount = true;
// $item->discount=5;
// $item->tax_rate1 = 20;
// $item->tax_name1 = 'VAT';
// $invoice->line_items = [$item];
// $invoice->calc()->getInvoice();
// $stub = json_decode('{"Invoice":{"Note":"Nooo","PaymentMeans":[{"ID":{"value":"afdasfasdfasdfas"},"PayeeFinancialAccount":{"Name":"PFA-NAME","ID":{"value":"DE89370400440532013000"},"AliasName":"PFA-Alias","AccountTypeCode":{"value":"CHECKING"},"AccountFormatCode":{"value":"IBAN"},"CurrencyCode":{"value":"EUR"},"FinancialInstitutionBranch":{"ID":{"value":"DEUTDEMMXXX"},"Name":"Deutsche Bank"}}}]}}');
// foreach ($stub as $key => $value) {
// $e_invoice->{$key} = $value;
// }
// $invoice->e_invoice = $e_invoice;
// $p = new Peppol($invoice);
// $p->run();
// $peppolInvoice = $p->getInvoice();
// $s_transformer = new StorecoveTransformer();
// $s_transformer->transform($peppolInvoice);
// $json = $s_transformer->toJson();
// $this->assertJson($json);
// nlog($json);
// }
public function testStorecoveTransformerWithPercentageDiscount()
{