Skip to content

Commit

Permalink
Restructured code using traits
Browse files Browse the repository at this point in the history
  • Loading branch information
ruff committed Aug 5, 2024
1 parent 778f480 commit 75eff2a
Show file tree
Hide file tree
Showing 4 changed files with 331 additions and 165 deletions.
176 changes: 11 additions & 165 deletions src/XmlConverterCiiToUbl.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

use DateTime;
use Exception;
use horstoeko\zugferdublbridge\traits\HandlesAmountFormatting;
use horstoeko\zugferdublbridge\traits\HandlesDocumentTypes;
use horstoeko\zugferdublbridge\traits\HandlesProfiles;

/**
* Class representing the converter from CII syntax to UBL syntax
Expand All @@ -23,66 +26,9 @@
*/
class XmlConverterCiiToUbl extends XmlConverterBase
{
/**
* List of supported profiles
*
* @var string[]
*/
private const SUPPORTED_PROFILES = [
'urn:factur-x.eu:1p0:minimum',
'urn:factur-x.eu:1p0:basicwl',
'urn:cen.eu:en16931:2017#compliant#urn:factur-x.eu:1p0:basic',
'urn:cen.eu:en16931:2017',
'urn:cen.eu:en16931:2017#conformant#urn:factur-x.eu:1p0:extended',
'urn:cen.eu:en16931:2017#compliant#urn:xoev-de:kosit:standard:xrechnung_1.2',
'urn:cen.eu:en16931:2017#compliant#urn:xoev-de:kosit:standard:xrechnung_2.0',
'urn:cen.eu:en16931:2017#compliant#urn:xoev-de:kosit:standard:xrechnung_2.1',
'urn:cen.eu:en16931:2017#compliant#urn:xoev-de:kosit:standard:xrechnung_2.2',
'urn:cen.eu:en16931:2017#compliant#urn:xoev-de:kosit:standard:xrechnung_2.3',
'urn:cen.eu:en16931:2017#compliant#urn:xeinkauf.de:kosit:xrechnung_3.0',
];

/**
* List of document types which represent an invoice
*
* @var string[]
*/
private const INVOICE_TYPES = [
'80', '82', '84', '130', '202', '203', '204', '211', '295', '325', '326', '380', '383',
'384', '385', '386', '387', '388', '389', '390', '393', '394', '395', '456', '457', '527',
'575', '623', '633', '751', '780', '935',
];

/**
* List of document types which represent a credit note
*
* @var string[]
*/

private const CREDITNOTE_TYPES = [
'81', '83', '261', '262', '296', '308', '381', '396', '420', '458', '532',
];

/**
* Internal flag to disable amount formattting
*
* @var boolean
*/
private $amountFormatDisabled = true;

/**
* Internal flag to disable the automatic detection of Invoice or CreditNote
*
* @var boolean
*/
private $automaticModeDisabled = true;

/**
* Internal Flag to force a profile in destination
*
* @var string
*/
private $forceDestinationProfile = "";
use HandlesProfiles,
HandlesAmountFormatting,
HandlesDocumentTypes;

/**
* @inheritDoc
Expand Down Expand Up @@ -128,7 +74,7 @@ protected function checkValidSource()

$submittedProfile = $this->source->queryValue('./ram:GuidelineSpecifiedDocumentContextParameter/ram:ID', $invoiceExchangeDocumentContext);

if (!in_array($submittedProfile, static::SUPPORTED_PROFILES)) {
if (!$this->isSupportedProfile($submittedProfile)) {
throw new \RuntimeException(sprintf('The submitted profile %s is not supported', $submittedProfile));
}
}
Expand All @@ -155,98 +101,21 @@ protected function doConvert()
return $this;
}

/**
* Disable amount formatting
*
* @return XmlConverterCiiToUbl
*/
public function disableAmountFormatDisabled(): XmlConverterCiiToUbl
{
$this->amountFormatDisabled = true;

return $this;
}

/**
* Enable amount formatting
*
* @return XmlConverterCiiToUbl
*/
public function enableAmountFormatDisabled(): XmlConverterCiiToUbl
{
$this->amountFormatDisabled = false;

return $this;
}

/**
* Disable automatic detection of Invoice/CreditNote
*
* @return XmlConverterCiiToUbl
*/
public function disableAutomaticMode(): XmlConverterCiiToUbl
{
$this->automaticModeDisabled = true;

return $this;
}

/**
* Enable automatic detection of Invoice/CreditNote
*
* @return XmlConverterCiiToUbl
*/
public function enableAutomaticMode(): XmlConverterCiiToUbl
{
$this->automaticModeDisabled = false;

return $this;
}

/**
* Set the profile to force in the destination (UBL) document
*
* @param string $forceDestinationProfile
* @return XmlConverterCiiToUbl
*/
public function setForceDestinationProfile(string $forceDestinationProfile): XmlConverterCiiToUbl
{
if (!in_array($forceDestinationProfile, static::SUPPORTED_PROFILES)) {
return $this;
}

$this->forceDestinationProfile = $forceDestinationProfile;

return $this;
}

/**
* Unsert the profile to force in the destination (UBL) document
*
* @return XmlConverterCiiToUbl
*/
public function clearForceDestinationProfile(): XmlConverterCiiToUbl
{
$this->forceDestinationProfile = "";

return $this;
}

/**
* Returns true if source is a credit note, otherwise false
*
* @return boolean
*/
private function getIsCreditNote(): bool
{
if ($this->automaticModeDisabled === true) {
if ($this->getAutomaticDocumentTypeModeDisabled()) {
return false;
}

$invoiceElement = $this->source->query('//rsm:CrossIndustryInvoice')->item(0);
$invoiceExchangeDocument = $this->source->query('./rsm:ExchangedDocument', $invoiceElement)->item(0);

return in_array($this->source->queryValue('./ram:TypeCode', $invoiceExchangeDocument), static::CREDITNOTE_TYPES);
return $this->isCreditMemoDocumentType($this->source->queryValue('./ram:TypeCode', $invoiceExchangeDocument));
}

/**
Expand Down Expand Up @@ -281,8 +150,8 @@ private function convertGeneral(): void

$customizationId = $this->source->queryValue('./ram:GuidelineSpecifiedDocumentContextParameter/ram:ID', $invoiceExchangeDocumentContext);

if ($this->forceDestinationProfile) {
$customizationId = $this->forceDestinationProfile;
if ($this->getForceDestinationProfile()) {
$customizationId = $this->getForceDestinationProfile();
}

$this->destination->element('cbc:CustomizationID', $customizationId);
Expand Down Expand Up @@ -1747,27 +1616,4 @@ private function toDateTime(?string $dateTimeString, ?string $format)
throw new Exception($format);
}
}

/**
* Format amount value
*
* @param string|null $amount
* @return string|null
*/
private function formatAmount(?string $amount): ?string
{
if ($this->amountFormatDisabled === true) {
return $amount;
}

if (is_null($amount)) {
return $amount;
}

if (!is_numeric($amount)) {
return $amount;
}

return (string)((float)$amount);
}
}
96 changes: 96 additions & 0 deletions src/traits/HandlesAmountFormatting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

/**
* This file is a part of horstoeko/zugferdublbridge.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace horstoeko\zugferdublbridge\traits;

/**
* Trait for handling supported profiles
*
* @category Zugferd-UBL-Bridge
* @package Zugferd-UBL-Bridge
* @author D. Erling <horstoeko@erling.com.de>
* @license https://opensource.org/licenses/MIT MIT
* @link https://github.com/horstoeko/zugferdublbridge
*/
trait HandlesAmountFormatting
{
/**
* Internal flag to disable amount formattting
*
* @var boolean
*/
private $amountFormatDisabled = true;

/**
* Disable amount formatting
*
* @return static
*/
public function disableAmountFormatDisabled()
{
$this->amountFormatDisabled = true;

return $this;
}

/**
* Enable amount formatting
*
* @return static
*/
public function enableAmountFormatDisabled()
{
$this->amountFormatDisabled = false;

return $this;
}

/**
* Returns true if the amount formatting is disabled
*
* @return boolean
*/
public function getAmountFormatDisabled(): bool
{
return $this->amountFormatDisabled;
}

/**
* Returns true if the amount formatting is enabled
*
* @return boolean
*/
public function getAmountFormatEnabled(): bool
{
return $this->getAmountFormatDisabled() === false;
}

/**
* Format amount value
*
* @param string|null $amount
* @return string|null
*/
private function formatAmount(?string $amount): ?string
{
if ($this->getAmountFormatDisabled() === true) {
return $amount;
}

if (is_null($amount)) {
return $amount;
}

if (!is_numeric($amount)) {
return $amount;
}

return (string)((float)$amount);
}
}
Loading

0 comments on commit 75eff2a

Please sign in to comment.