From bfbf7f5c6a1c9467ff5c38e5085bd9f3b253068e Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 16 Sep 2024 13:35:23 +1000 Subject: [PATCH] Split einvoice gateways --- .../EDocument/Gateway/MutatorInterface.php | 6 +++ .../EDocument/Gateway/MutatorUtil.php | 41 +++++++++++++------ .../EDocument/Gateway/Qvalia/Mutator.php | 19 +++++++++ .../EDocument/Gateway/Storecove/Mutator.php | 23 +++++++++-- 4 files changed, 72 insertions(+), 17 deletions(-) diff --git a/app/Services/EDocument/Gateway/MutatorInterface.php b/app/Services/EDocument/Gateway/MutatorInterface.php index f308dbaa8f18..15a4bdc51ce4 100644 --- a/app/Services/EDocument/Gateway/MutatorInterface.php +++ b/app/Services/EDocument/Gateway/MutatorInterface.php @@ -28,6 +28,12 @@ interface MutatorInterface public function setCompanySettings($company_settings): self; + public function getClientSettings(): mixed; + + public function getCompanySettings(): mixed; + + public function getInvoice(): mixed; + // Country-specific methods public function DE(): self; diff --git a/app/Services/EDocument/Gateway/MutatorUtil.php b/app/Services/EDocument/Gateway/MutatorUtil.php index 3b6f48ef4667..98000c415dfa 100644 --- a/app/Services/EDocument/Gateway/MutatorUtil.php +++ b/app/Services/EDocument/Gateway/MutatorUtil.php @@ -12,11 +12,20 @@ namespace App\Services\EDocument\Gateway; use App\Exceptions\PeppolValidationException; +use App\Services\EDocument\Gateway\MutatorInterface; use App\Services\EDocument\Standards\Settings\PropertyResolver; +use InvoiceNinja\EInvoice\Models\Peppol\IdentifierType\CustomerAssignedAccountID; +/** + * Class MutatorUtil + * + * Utility class for e-document mutations. + */ class MutatorUtil { - + /** + * MutatorUtil constructor. + */ public function __construct(public MutatorInterface $mutator) { } @@ -30,11 +39,13 @@ class MutatorUtil */ public function setPaymentMeans(bool $required = false): self { + $peppol = $this->mutator->getPeppol(); - if(isset($this->mutator->p_invoice->PaymentMeans)) { + if(isset($peppol->PaymentMeans)) { return $this; } elseif($paymentMeans = $this->getSetting('Invoice.PaymentMeans')) { - $this->mutator->p_invoice->PaymentMeans = is_array($paymentMeans) ? $paymentMeans : [$paymentMeans]; + $peppol->PaymentMeans = is_array($paymentMeans) ? $paymentMeans : [$paymentMeans]; + $this->mutator->setPeppol($peppol); return $this; } @@ -51,7 +62,7 @@ class MutatorUtil */ public function getClientSetting(string $property_path): mixed { - return PropertyResolver::resolve($this->mutator->_client_settings, $property_path); + return PropertyResolver::resolve($this->mutator->getClientSettings(), $property_path); } /** @@ -62,7 +73,7 @@ class MutatorUtil */ public function getCompanySetting(string $property_path): mixed { - return PropertyResolver::resolve($this->mutator->_company_settings, $property_path); + return PropertyResolver::resolve($this->mutator->getCompanySettings(), $property_path); } /** @@ -76,11 +87,11 @@ class MutatorUtil public function getSetting(string $property_path): mixed { - if($prop_value = PropertyResolver::resolve($this->mutator->p_invoice, $property_path)) { + if($prop_value = PropertyResolver::resolve($this->mutator->getPeppol(), $property_path)) { return $prop_value; - } elseif($prop_value = PropertyResolver::resolve($this->mutator->_client_settings, $property_path)) { + } elseif($prop_value = PropertyResolver::resolve($this->mutator->getClientSettings(), $property_path)) { return $prop_value; - } elseif($prop_value = PropertyResolver::resolve($this->mutator->_company_settings, $property_path)) { + } elseif($prop_value = PropertyResolver::resolve($this->mutator->getCompanySettings(), $property_path)) { return $prop_value; } return null; @@ -111,19 +122,23 @@ class MutatorUtil */ public function setCustomerAssignedAccountId(bool $required = false): self { + $peppol = $this->mutator->getPeppol(); + $invoice = $this->mutator->getInvoice(); + //@phpstan-ignore-next-line - if(isset($this->mutator->p_invoice->AccountingCustomerParty->CustomerAssignedAccountID)) { + if(isset($peppol->AccountingCustomerParty->CustomerAssignedAccountID)) { return $this; } elseif($customer_assigned_account_id = $this->getSetting('Invoice.AccountingCustomerParty.CustomerAssignedAccountID')) { - $this->mutator->p_invoice->AccountingCustomerParty->CustomerAssignedAccountID = $customer_assigned_account_id; + $peppol->AccountingCustomerParty->CustomerAssignedAccountID = $customer_assigned_account_id; + $this->mutator->setPeppol($peppol); return $this; - } elseif(strlen($this->mutator->invoice->client->id_number ?? '') > 1) { + } elseif(strlen($invoice->client->id_number ?? '') > 1) { $customer_assigned_account_id = new CustomerAssignedAccountID(); - $customer_assigned_account_id->value = $this->mutator->invoice->client->id_number; + $customer_assigned_account_id->value = $invoice->client->id_number; - $this->mutator->p_invoice->AccountingCustomerParty->CustomerAssignedAccountID = $customer_assigned_account_id; + $peppol->AccountingCustomerParty->CustomerAssignedAccountID = $customer_assigned_account_id; return $this; } diff --git a/app/Services/EDocument/Gateway/Qvalia/Mutator.php b/app/Services/EDocument/Gateway/Qvalia/Mutator.php index 96eb44badbd2..8a2d02004ed1 100644 --- a/app/Services/EDocument/Gateway/Qvalia/Mutator.php +++ b/app/Services/EDocument/Gateway/Qvalia/Mutator.php @@ -11,6 +11,7 @@ namespace App\Services\EDocument\Gateway\Qvalia; +use App\Services\EDocument\Gateway\MutatorUtil; use App\Services\EDocument\Gateway\MutatorInterface; class Mutator implements MutatorInterface @@ -24,8 +25,11 @@ class Mutator implements MutatorInterface private $invoice; + private MutatorUtil $mutator_util; + public function __construct(public Qvalia $qvalia) { + $this->mutator_util = new MutatorUtil($this); } public function setInvoice($invoice): self @@ -45,6 +49,16 @@ class Mutator implements MutatorInterface return $this->p_invoice; } + public function getClientSettings(): mixed + { + return $this->_client_settings; + } + + public function getCompanySettings(): mixed + { + return $this->_company_settings; + } + public function setClientSettings($client_settings): self { $this->_client_settings = $client_settings; @@ -57,6 +71,11 @@ class Mutator implements MutatorInterface return $this; } + public function getInvoice(): mixed + { + return $this->invoice; + } + /** * senderSpecificLevelMutators * diff --git a/app/Services/EDocument/Gateway/Storecove/Mutator.php b/app/Services/EDocument/Gateway/Storecove/Mutator.php index fe451de98371..da566641b78d 100644 --- a/app/Services/EDocument/Gateway/Storecove/Mutator.php +++ b/app/Services/EDocument/Gateway/Storecove/Mutator.php @@ -19,13 +19,13 @@ use App\Services\EDocument\Gateway\Storecove\StorecoveRouter; class Mutator implements MutatorInterface { - public \InvoiceNinja\EInvoice\Models\Peppol\Invoice $p_invoice; + private \InvoiceNinja\EInvoice\Models\Peppol\Invoice $p_invoice; - public ?\InvoiceNinja\EInvoice\Models\Peppol\Invoice $_client_settings; + private ?\InvoiceNinja\EInvoice\Models\Peppol\Invoice $_client_settings; - public ?\InvoiceNinja\EInvoice\Models\Peppol\Invoice $_company_settings; + private ?\InvoiceNinja\EInvoice\Models\Peppol\Invoice $_company_settings; - public $invoice; + private $invoice; private array $storecove_meta = []; @@ -94,6 +94,21 @@ class Mutator implements MutatorInterface return $this; } + public function getClientSettings(): mixed + { + return $this->_client_settings; + } + + public function getCompanySettings(): mixed + { + return $this->_company_settings; + } + + public function getInvoice(): mixed + { + return $this->invoice; + } + /** * senderSpecificLevelMutators *