Skip to content

Commit

Permalink
Initial version creating valid UBL from the examples files
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Feb 21, 2025
1 parent a28a80c commit 5d40828
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,21 @@ private static IDType _convertIDType (@Nullable final com.helger.xsds.ccts.cct.s
return ret;
}

@Nonnull
private static AmountType _convertAmountType (final com.helger.xsds.ccts.cct.schemamodule.AmountType aUBLAmount)
@Nullable
private static AmountType _convertAmountType (@Nullable final com.helger.xsds.ccts.cct.schemamodule.AmountType aUBLAmount)
{
return _convertAmountType (aUBLAmount, false);
}

@Nullable
private static AmountType _convertAmountType (@Nullable final com.helger.xsds.ccts.cct.schemamodule.AmountType aUBLAmount,
final boolean bWithCurrency)
{
if (aUBLAmount == null)
return null;

final AmountType ret = new AmountType ();
if (false)
if (bWithCurrency)
ret.setCurrencyID (aUBLAmount.getCurrencyID ());
ret.setValue (MathHelper.getWithoutTrailingZeroes (aUBLAmount.getValue ()));
return ret;
Expand Down Expand Up @@ -245,14 +255,16 @@ private static List <SupplyChainTradeLineItemType> _convertInvoiceLines (final L
// SpecifiedLineTradeSettlement
final LineTradeSettlementType aLTST = new LineTradeSettlementType ();
{
for (final TaxCategoryType aTCT : aILT.getItem ().getClassifiedTaxCategory ())
for (final TaxCategoryType aTaxCategory : aILT.getItem ().getClassifiedTaxCategory ())
{
final TradeTaxType aTTT = new TradeTaxType ();
aTTT.setTypeCode (aTCT.getTaxScheme ().getIDValue ());
aTTT.setCategoryCode (aTCT.getIDValue ());
if (aTCT.getPercent () != null)
aTTT.setRateApplicablePercent (aTCT.getPercentValue ());
aLTST.addApplicableTradeTax (aTTT);
final TaxSchemeType aTaxScheme = aTaxCategory.getTaxScheme ();

final TradeTaxType aTradeTax = new TradeTaxType ();
if (aTaxScheme != null)
ifNotEmpty (aTradeTax::setTypeCode, aTaxCategory.getTaxScheme ().getIDValue ());
ifNotEmpty (aTradeTax::setCategoryCode, aTaxCategory.getIDValue ());
ifNotNull (aTradeTax::setRateApplicablePercent, aTaxCategory.getPercentValue ());
aLTST.addApplicableTradeTax (aTradeTax);
}
}
final TradeSettlementLineMonetarySummationType aTSLMST = new TradeSettlementLineMonetarySummationType ();
Expand Down Expand Up @@ -320,6 +332,12 @@ private static TradePartyType _convertParty (@Nullable final PartyType aParty)
ifNotNull (aLOT::setID, _convertIDType (aLE.getCompanyID ()));
ifNotNull (aLOT::setPostalTradeAddress, _convertAddress (aLE.getRegistrationAddress ()));

if (StringHelper.hasNoText (aTPT.getNameValue ()))
{
// Fill mandatory field
ifNotEmpty (aTPT::setName, aLE.getRegistrationNameValue ());
}

aTPT.setSpecifiedLegalOrganization (aLOT);
}

Expand Down Expand Up @@ -390,17 +408,17 @@ private static HeaderTradeDeliveryType _convertApplicableHeaderTradeDelivery (@N
@Nonnull
private static HeaderTradeSettlementType _convertApplicableHeaderTradeSettlement (@Nonnull final InvoiceType aUBLInvoice)
{
final HeaderTradeSettlementType aHTST = new HeaderTradeSettlementType ();
final HeaderTradeSettlementType ret = new HeaderTradeSettlementType ();

final PaymentMeansType aPM = aUBLInvoice.hasPaymentMeansEntries () ? aUBLInvoice.getPaymentMeansAtIndex (0) : null;

if (aPM != null && aPM.hasPaymentIDEntries ())
aHTST.addPaymentReference (_convertTextType (aPM.getPaymentID ().get (0).getValue ()));
ret.addPaymentReference (_convertTextType (aPM.getPaymentID ().get (0).getValue ()));

aHTST.setInvoiceCurrencyCode (aUBLInvoice.getDocumentCurrencyCode ().getValue ());
ret.setInvoiceCurrencyCode (aUBLInvoice.getDocumentCurrencyCode ().getValue ());

if (aUBLInvoice.getPayeeParty () != null)
aHTST.setPayeeTradeParty (_convertParty (aUBLInvoice.getPayeeParty ()));
ret.setPayeeTradeParty (_convertParty (aUBLInvoice.getPayeeParty ()));

if (aPM != null)
{
Expand All @@ -412,10 +430,10 @@ private static HeaderTradeSettlementType _convertApplicableHeaderTradeSettlement
aCFAT.setIBANID (aPM.getPayeeFinancialAccount ().getIDValue ());

aTSPMT.setPayeePartyCreditorFinancialAccount (aCFAT);
aHTST.addSpecifiedTradeSettlementPaymentMeans (aTSPMT);
ret.addSpecifiedTradeSettlementPaymentMeans (aTSPMT);
}

aHTST.setApplicableTradeTax (_convertApplicableTradeTax (aUBLInvoice));
ret.setApplicableTradeTax (_convertApplicableTradeTax (aUBLInvoice));

if (aUBLInvoice.hasInvoicePeriodEntries ())
{
Expand All @@ -428,21 +446,21 @@ private static HeaderTradeSettlementType _convertApplicableHeaderTradeSettlement
if (aIP.getEndDate () != null)
aSPT.setEndDateTime (_createDate (aIP.getEndDate ().getValueLocal ()));

aHTST.setBillingSpecifiedPeriod (aSPT);
ret.setBillingSpecifiedPeriod (aSPT);
}

aHTST.setSpecifiedTradeAllowanceCharge (_convertSpecifiedTradeAllowanceCharge (aUBLInvoice));
aHTST.setSpecifiedTradePaymentTerms (_convertSpecifiedTradePaymentTerms (aUBLInvoice));
aHTST.setSpecifiedTradeSettlementHeaderMonetarySummation (_convertSpecifiedTradeSettlementHeaderMonetarySummation (aUBLInvoice));
ret.setSpecifiedTradeAllowanceCharge (_convertSpecifiedTradeAllowanceCharge (aUBLInvoice));
ret.setSpecifiedTradePaymentTerms (_convertSpecifiedTradePaymentTerms (aUBLInvoice));
ret.setSpecifiedTradeSettlementHeaderMonetarySummation (_convertSpecifiedTradeSettlementHeaderMonetarySummation (aUBLInvoice));

if (aUBLInvoice.getAccountingCost () != null)
{
final TradeAccountingAccountType aTAAT = new TradeAccountingAccountType ();
aTAAT.setID (aUBLInvoice.getAccountingCost ().getValue ());
aHTST.addReceivableSpecifiedTradeAccountingAccount (aTAAT);
ret.addReceivableSpecifiedTradeAccountingAccount (aTAAT);
}

return aHTST;
return ret;
}

@Nonnull
Expand All @@ -452,35 +470,24 @@ private static TradeSettlementHeaderMonetarySummationType _convertSpecifiedTrade
final MonetaryTotalType aUBLLMT = aUBLInvoice.getLegalMonetaryTotal ();
if (aUBLLMT != null)
{
if (aUBLLMT.getLineExtensionAmount () != null)
aTSHMST.addLineTotalAmount (_convertAmountType (aUBLLMT.getLineExtensionAmount ()));

if (aUBLLMT.getChargeTotalAmount () != null)
aTSHMST.addChargeTotalAmount (_convertAmountType (aUBLLMT.getChargeTotalAmount ()));

if (aUBLLMT.getAllowanceTotalAmount () != null)
aTSHMST.addAllowanceTotalAmount (_convertAmountType (aUBLLMT.getAllowanceTotalAmount ()));

if (aUBLLMT.getTaxExclusiveAmount () != null)
aTSHMST.addTaxBasisTotalAmount (_convertAmountType (aUBLLMT.getTaxExclusiveAmount ()));
ifNotNull (aTSHMST::addLineTotalAmount, _convertAmountType (aUBLLMT.getLineExtensionAmount ()));
ifNotNull (aTSHMST::addChargeTotalAmount, _convertAmountType (aUBLLMT.getChargeTotalAmount ()));
ifNotNull (aTSHMST::addAllowanceTotalAmount, _convertAmountType (aUBLLMT.getAllowanceTotalAmount ()));
ifNotNull (aTSHMST::addTaxBasisTotalAmount, _convertAmountType (aUBLLMT.getTaxExclusiveAmount ()));
}

if (!aUBLInvoice.getTaxTotal ().isEmpty () && aUBLInvoice.getTaxTotal ().get (0).getTaxAmount () != null)
aTSHMST.addTaxTotalAmount (_convertAmountType (aUBLInvoice.getTaxTotal ().get (0).getTaxAmount ()));
if (aUBLInvoice.hasTaxTotalEntries ())
{
// Currency ID is required here
ifNotNull (aTSHMST::addTaxTotalAmount, _convertAmountType (aUBLInvoice.getTaxTotalAtIndex (0).getTaxAmount (), true));
}

if (aUBLLMT != null)
{
if (aUBLLMT.getPayableRoundingAmount () != null)
aTSHMST.addRoundingAmount (_convertAmountType (aUBLLMT.getPayableRoundingAmount ()));

if (aUBLLMT.getTaxInclusiveAmount () != null)
aTSHMST.addGrandTotalAmount (_convertAmountType (aUBLLMT.getTaxInclusiveAmount ()));

if (aUBLLMT.getPrepaidAmount () != null)
aTSHMST.addTotalPrepaidAmount (_convertAmountType (aUBLLMT.getPrepaidAmount ()));

if (aUBLLMT.getPayableAmount () != null)
aTSHMST.addDuePayableAmount (_convertAmountType (aUBLLMT.getPayableAmount ()));
ifNotNull (aTSHMST::addRoundingAmount, _convertAmountType (aUBLLMT.getPayableRoundingAmount ()));
ifNotNull (aTSHMST::addGrandTotalAmount, _convertAmountType (aUBLLMT.getTaxInclusiveAmount ()));
ifNotNull (aTSHMST::addTotalPrepaidAmount, _convertAmountType (aUBLLMT.getPrepaidAmount ()));
ifNotNull (aTSHMST::addDuePayableAmount, _convertAmountType (aUBLLMT.getPayableAmount ()));
}

return aTSHMST;
Expand All @@ -490,36 +497,31 @@ private static TradeSettlementHeaderMonetarySummationType _convertSpecifiedTrade
private static List <TradeTaxType> _convertApplicableTradeTax (@Nonnull final InvoiceType aUBLInvoice)
{
final List <TradeTaxType> ret = new ArrayList <> ();
for (final TaxTotalType aTTT : aUBLInvoice.getTaxTotal ())
if (aTTT.hasTaxSubtotalEntries ())
for (final TaxTotalType aTaxTotal : aUBLInvoice.getTaxTotal ())
for (final TaxSubtotalType aTaxSubtotal : aTaxTotal.getTaxSubtotal ())
{
final TaxSubtotalType aTST = aTTT.getTaxSubtotalAtIndex (0);
final TaxCategoryType aTC = aTST.getTaxCategory ();
final TaxSchemeType aTS = aTC == null ? null : aTC.getTaxScheme ();
final TaxCategoryType aTaxCategory = aTaxSubtotal.getTaxCategory ();
final TaxSchemeType aTaxScheme = aTaxCategory.getTaxScheme ();

final TradeTaxType aTTTST = new TradeTaxType ();
final TradeTaxType aTradeTax = new TradeTaxType ();

if (aTS != null)
aTTTST.setTypeCode (aTS.getIDValue ());
if (aTaxScheme != null)
ifNotEmpty (aTradeTax::setTypeCode, aTaxScheme.getIDValue ());
ifNotEmpty (aTradeTax::setCategoryCode, aTaxCategory.getIDValue ());

if (aTST.getTaxAmount () != null)
aTTTST.addCalculatedAmount (_convertAmountType (aTST.getTaxAmount ()));
ifNotNull (aTradeTax::addCalculatedAmount, _convertAmountType (aTaxSubtotal.getTaxAmount ()));

if (aTST.getTaxCategory () != null)
aTTTST.setCategoryCode (aTST.getTaxCategory ().getIDValue ());
if (aTaxSubtotal.getTaxCategory () != null)
ifNotEmpty (aTradeTax::setCategoryCode, aTaxSubtotal.getTaxCategory ().getIDValue ());

if (aTST.getTaxableAmount () != null)
aTTTST.addBasisAmount (_convertAmountType (aTST.getTaxableAmount ()));
ifNotNull (aTradeTax::addBasisAmount, _convertAmountType (aTaxSubtotal.getTaxableAmount ()));
ifNotNull (aTradeTax::setRateApplicablePercent, aTaxCategory.getPercentValue ());

if (aTC != null)
{
if (aTC.hasTaxExemptionReasonEntries ())
aTTTST.setExemptionReason (aTC.getTaxExemptionReason ().get (0).getValue ());
if (aTaxCategory.hasTaxExemptionReasonEntries ())
ifNotEmpty (aTradeTax::setExemptionReason, aTaxCategory.getTaxExemptionReasonAtIndex (0).getValue ());

if (aTC.getTaxExemptionReasonCode () != null)
aTTTST.setExemptionReasonCode (aTC.getTaxExemptionReasonCode ().getValue ());
}
ret.add (aTTTST);
ifNotEmpty (aTradeTax::setExemptionReasonCode, aTaxCategory.getTaxExemptionReasonCodeValue ());
ret.add (aTradeTax);
}
return ret;
}
Expand Down Expand Up @@ -574,15 +576,14 @@ private static List <TradeAllowanceChargeType> _convertSpecifiedTradeAllowanceCh

if (aItem.hasTaxCategoryEntries ())
{
final TaxCategoryType aTC = aItem.getTaxCategoryAtIndex (0);
final TradeTaxType aTradeTax = new TradeTaxType ();
aTradeTax.setCategoryCode (aTC.getIDValue ());
if (aTC.getPercent () != null)
aTradeTax.setRateApplicablePercent (aTC.getPercentValue ());
final TaxCategoryType aTaxCategory = aItem.getTaxCategoryAtIndex (0);
final TaxSchemeType aTaxSchene = aTaxCategory.getTaxScheme ();

final TaxSchemeType aTS = aTC.getTaxScheme ();
if (aTS != null)
aTradeTax.setTypeCode (aTS.getIDValue ());
final TradeTaxType aTradeTax = new TradeTaxType ();
if (aTaxSchene != null)
ifNotEmpty (aTradeTax::setTypeCode, aTaxSchene.getIDValue ());
ifNotEmpty (aTradeTax::setCategoryCode, aTaxCategory.getIDValue ());
ifNotNull (aTradeTax::setRateApplicablePercent, aTaxCategory.getPercentValue ());

aTDCT.addCategoryTradeTax (aTradeTax);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void testConvertAndValidateAll ()
assertTrue (eSuccess.isSuccess ());

// TODO fix validation errors
if (false)
if (true)
{
// Validate against EN16931 validation rules
final ValidationResultList aResultList = ValidationExecutionManager.executeValidation (IValidityDeterminator.createDefault (),
Expand Down

0 comments on commit 5d40828

Please sign in to comment.