1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 15:42:51 +01:00
invoiceninja/app/Services/EDocument/Gateway/MutatorUtil.php

151 lines
4.3 KiB
PHP
Raw Normal View History

2024-09-16 05:12:46 +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\Gateway;
use App\Exceptions\PeppolValidationException;
2024-09-16 05:35:23 +02:00
use App\Services\EDocument\Gateway\MutatorInterface;
2024-09-16 05:12:46 +02:00
use App\Services\EDocument\Standards\Settings\PropertyResolver;
2024-09-16 05:35:23 +02:00
use InvoiceNinja\EInvoice\Models\Peppol\IdentifierType\CustomerAssignedAccountID;
2024-09-16 05:12:46 +02:00
2024-09-16 05:35:23 +02:00
/**
* Class MutatorUtil
*
* Utility class for e-document mutations.
*/
2024-09-16 05:12:46 +02:00
class MutatorUtil
{
2024-09-16 05:35:23 +02:00
/**
* MutatorUtil constructor.
*/
2024-09-16 05:12:46 +02:00
public function __construct(public MutatorInterface $mutator)
{
}
/**
* setPaymentMeans
*
* Sets the payment means - if it exists
* @param bool $required
* @return self
*/
public function setPaymentMeans(bool $required = false): self
{
2024-09-16 05:35:23 +02:00
$peppol = $this->mutator->getPeppol();
2024-09-16 05:12:46 +02:00
2024-09-16 05:35:23 +02:00
if(isset($peppol->PaymentMeans)) {
2024-09-16 05:12:46 +02:00
return $this;
} elseif($paymentMeans = $this->getSetting('Invoice.PaymentMeans')) {
2024-09-16 05:35:23 +02:00
$peppol->PaymentMeans = is_array($paymentMeans) ? $paymentMeans : [$paymentMeans];
$this->mutator->setPeppol($peppol);
2024-09-16 05:12:46 +02:00
return $this;
}
return $this->checkRequired($required, "Payment Means");
}
/**
* getClientSetting
*
* @param string $property_path
* @return mixed
*/
public function getClientSetting(string $property_path): mixed
{
2024-09-16 05:35:23 +02:00
return PropertyResolver::resolve($this->mutator->getClientSettings(), $property_path);
2024-09-16 05:12:46 +02:00
}
/**
* getCompanySetting
*
* @param string $property_path
* @return mixed
*/
public function getCompanySetting(string $property_path): mixed
{
2024-09-16 05:35:23 +02:00
return PropertyResolver::resolve($this->mutator->getCompanySettings(), $property_path);
2024-09-16 05:12:46 +02:00
}
/**
* getSetting
*
* Attempts to harvest and return a preconfigured prop from company / client / invoice settings
*
* @param string $property_path
* @return mixed
*/
public function getSetting(string $property_path): mixed
{
2024-09-16 05:35:23 +02:00
if($prop_value = PropertyResolver::resolve($this->mutator->getPeppol(), $property_path)) {
2024-09-16 05:12:46 +02:00
return $prop_value;
2024-09-16 05:35:23 +02:00
} elseif($prop_value = PropertyResolver::resolve($this->mutator->getClientSettings(), $property_path)) {
2024-09-16 05:12:46 +02:00
return $prop_value;
2024-09-16 05:35:23 +02:00
} elseif($prop_value = PropertyResolver::resolve($this->mutator->getCompanySettings(), $property_path)) {
2024-09-16 05:12:46 +02:00
return $prop_value;
}
return null;
}
/**
* Check Required
*
* Throws if a required field is missing.
*
* @param bool $required
* @param string $section
* @return self
*/
public function checkRequired(bool $required, string $section): self
{
return $required ? throw new PeppolValidationException("e-invoice generation halted:: {$section} required", $section, 400) : $this;
}
/**
* setCustomerAssignedAccountId
*
* Sets the client id_number CAN rely on settings
*
* @param bool $required
* @return self
*/
public function setCustomerAssignedAccountId(bool $required = false): self
{
2024-09-16 05:35:23 +02:00
$peppol = $this->mutator->getPeppol();
$invoice = $this->mutator->getInvoice();
2024-09-16 05:12:46 +02:00
//@phpstan-ignore-next-line
2024-09-16 05:35:23 +02:00
if(isset($peppol->AccountingCustomerParty->CustomerAssignedAccountID)) {
2024-09-16 05:12:46 +02:00
return $this;
} elseif($customer_assigned_account_id = $this->getSetting('Invoice.AccountingCustomerParty.CustomerAssignedAccountID')) {
2024-09-16 05:35:23 +02:00
$peppol->AccountingCustomerParty->CustomerAssignedAccountID = $customer_assigned_account_id;
$this->mutator->setPeppol($peppol);
2024-09-16 05:12:46 +02:00
return $this;
2024-09-16 05:35:23 +02:00
} elseif(strlen($invoice->client->id_number ?? '') > 1) {
2024-09-16 05:12:46 +02:00
$customer_assigned_account_id = new CustomerAssignedAccountID();
2024-09-16 05:35:23 +02:00
$customer_assigned_account_id->value = $invoice->client->id_number;
2024-09-16 05:12:46 +02:00
2024-09-16 05:35:23 +02:00
$peppol->AccountingCustomerParty->CustomerAssignedAccountID = $customer_assigned_account_id;
2024-09-16 05:12:46 +02:00
return $this;
}
//@phpstan-ignore-next-line
return $this->checkRequired($required, 'Client ID Number');
}
}