Skip to content

Commit

Permalink
add multiple time format to deserialize (#66)
Browse files Browse the repository at this point in the history
* add multiple time format to deserialize

"HH:mm:ss",
"HH:mm",
"HH:mm:ss.fffff",
"HH:mm:ss.fffffff",
"HH:mm:ss.fffffffzzz"

closes #64
  • Loading branch information
hkutluay committed Oct 20, 2022
1 parent 8f742ae commit 4d822f6
Show file tree
Hide file tree
Showing 10 changed files with 755 additions and 23 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-multi-target.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ jobs:
runs-on: windows-latest

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v3
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x
- name: Build with dotnet
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ jobs:
runs-on: windows-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x

Expand Down
29 changes: 27 additions & 2 deletions Ubl-Tr/Common/UnqualifiedDataTypes/TimeType.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Globalization;
using System.Xml.Serialization;
using System.Collections.Generic;

namespace UblTr.Common
{
Expand Down Expand Up @@ -42,6 +42,14 @@ namespace UblTr.Common
public partial class TimeType
{

private List<string> applicableTimeFormats = new List<string> {
"HH:mm:ss",
"HH:mm",
"HH:mm:ss.fffff",
"HH:mm:ss.fffffff",
"HH:mm:ss.fffffffzzz"
};

private System.DateTime valueField;

/// <remarks/>
Expand All @@ -62,7 +70,24 @@ public System.DateTime Value
public String TimeString
{
get { return this.valueField.ToString("HH:mm:ss"); }
set { this.valueField = DateTime.ParseExact(value, "HH:mm:ss", CultureInfo.InvariantCulture); }
set {

bool parsed = false;
DateTime parsedValue;

foreach (var format in applicableTimeFormats)
{
if(DateTime.TryParseExact(value, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedValue))
{
this.valueField = parsedValue;
parsed = true;
}
}

if(!parsed)
throw new FormatException($"time {value} is not in correct format");

}
}
}
}
6 changes: 3 additions & 3 deletions Ubl-Tr/UblTr.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
<RepositoryType>Github</RepositoryType>
<PackageTags>ubltr ubl-tr e-invoice ubl ubl-tr-1.2.1 e-fatura fatura efatura e-despatch eirsaliye irsaliye applicationresponse uygulamayaniti creditnote receiptadvice </PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Version>1.1.6</Version>
<Version>1.2.0</Version>
<PackageIcon>icon.png</PackageIcon>
<AssemblyVersion>1.1.6.0</AssemblyVersion>
<FileVersion>1.1.6.0</FileVersion>
<AssemblyVersion>1.2.0.0</AssemblyVersion>
<FileVersion>1.2.0.0</FileVersion>
</PropertyGroup>
<ItemGroup>
<None Include="Images\icon.png">
Expand Down
52 changes: 40 additions & 12 deletions UblTr.Tests/InvoiceTypeTest.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Serialization;
using UblTr.MainDoc;
using UblTr.Serialization;
using System.Globalization;

namespace UblTr.Tests
{
[TestClass]
public class InvoiceTypeTest
{
private readonly string _basicInvoicePath;
private readonly string _commercialInvoicePath;

private readonly string _testFilesPath;

public InvoiceTypeTest()
{
var path = "TestFiles/InvoiceType/";
_basicInvoicePath = $"{path}BasicInvoice.xml";
_commercialInvoicePath = $"{path}CommercialInvoice.xml";
_testFilesPath = "TestFiles/InvoiceType";
}

InvoiceType DeserializeInvoiceXml(string path)
{
XmlSerializer serializer = new XmlSerializer(typeof(InvoiceType));
Expand All @@ -35,27 +33,56 @@ InvoiceType DeserializeInvoiceXml(string path)
[TestMethod]
public void InvoiceType_BasicInvoice_Deserialize()
{
var invoice = DeserializeInvoiceXml(_basicInvoicePath);
var invoice = DeserializeInvoiceXml($"{_testFilesPath}/BasicInvoice.xml");
Assert.AreEqual("GIB20090000000001", invoice.ID.Value);
Assert.AreEqual("TEMELFATURA", invoice.ProfileID.Value);
Assert.AreEqual("F47AC10B-58CC-4372-A567-0E02B2C3D479", invoice.UUID.Value);
Assert.AreEqual("1288331521", invoice.Signature[0].SignatoryParty.PartyIdentification.FirstOrDefault().ID.Value);
Assert.AreEqual(101, invoice.InvoiceLine.FirstOrDefault().InvoicedQuantity.Value);
Assert.AreEqual("14:42:00", invoice.IssueTime.Value.ToString("HH:mm:ss"));
Assert.AreEqual("14:42:00", invoice.IssueTime.Value.ToString("HH:mm:ss", CultureInfo.InvariantCulture));
}

[TestMethod]
public void InvoiceType_BasicInvoiceLongTime_Deserialize()
{
var invoice = DeserializeInvoiceXml( $"{_testFilesPath}/BasicInvoiceLongTime.xml");
Assert.AreEqual("17:11:02", TimeZoneInfo.ConvertTimeToUtc(invoice.IssueTime.Value).ToString("HH:mm:ss"));
}


[TestMethod]
public void InvoiceType_BasicInvoiceHourMinute_Deserialize()
{
var invoice = DeserializeInvoiceXml( $"{_testFilesPath}/BasicInvoiceHourMinuteTime.xml");
Assert.AreEqual("14:42", invoice.IssueTime.Value.ToString("HH:mm", CultureInfo.InvariantCulture));
}

[TestMethod]
public void InvoiceType_BasicInvoiceIncorrectTime_Deserialize()
{
Assert.ThrowsException<System.InvalidOperationException>(() =>
{
var invoice = DeserializeInvoiceXml($"{_testFilesPath}/BasicInvoiceIncorrectTime.xml");
});
}

[TestMethod]
public void InvoiceType_BasicInvoiceNoTime_Deserialize()
{
var invoice = DeserializeInvoiceXml($"{_testFilesPath}/BasicInvoiceNoTime.xml");
Assert.IsNull(invoice.IssueTime);
}

[TestMethod]
public void InvoiceType_CommercialInvoice_Deserialize()
{
var invoice = DeserializeInvoiceXml(_commercialInvoicePath);
var invoice = DeserializeInvoiceXml($"{_testFilesPath}/CommercialInvoice.xml");
Assert.AreEqual("GIB2009000000011", invoice.ID.Value);
Assert.AreEqual("TICARIFATURA", invoice.ProfileID.Value);
Assert.AreEqual("F47AC10B-58CC-4372-A567-0E02B2C3D479", invoice.UUID.Value);
Assert.AreEqual("1288331521", invoice.Signature[0].SignatoryParty.PartyIdentification.FirstOrDefault().ID.Value);
Assert.AreEqual(12, invoice.InvoiceLine.FirstOrDefault().InvoicedQuantity.Value);
Assert.AreEqual("14:42:00", invoice.IssueTime.Value.ToString("HH:mm:ss"));

}

[TestMethod]
Expand All @@ -81,7 +108,7 @@ public void InvoiceType_BasicInvoice_Serialize()
IssueDate = new Common.IssueDateType() { Value = date }
};


XmlSerializer xmlSerializer = new XmlSerializer(typeof(InvoiceType));
var stream = new MemoryStream();
xmlSerializer.Serialize(stream, invoice, new UblTrNamespaces());
Expand All @@ -97,6 +124,7 @@ public void InvoiceType_BasicInvoice_Serialize()
}



[TestMethod]
public void InvoiceType_BasicInvoice_TimeSerialize()
{
Expand Down
167 changes: 167 additions & 0 deletions UblTr.Tests/TestFiles/InvoiceType/BasicInvoiceHourMinuteTime.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="general.xslt"?>
<Invoice xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 ../xsdrt/maindoc/UBL-Invoice-2.1.xsd" xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:n4="http://www.altova.com/samplexml/other-namespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2">
<ext:UBLExtensions>
<ext:UBLExtension>
<ext:ExtensionContent>
<n4:auto-generated_for_wildcard/>
</ext:ExtensionContent>
</ext:UBLExtension>
</ext:UBLExtensions>
<cbc:UBLVersionID>2.1</cbc:UBLVersionID>
<cbc:CustomizationID>TR1.2</cbc:CustomizationID>
<cbc:ProfileID>TEMELFATURA</cbc:ProfileID>
<cbc:ID>GIB20090000000001</cbc:ID>
<cbc:CopyIndicator>false</cbc:CopyIndicator>
<cbc:UUID>F47AC10B-58CC-4372-A567-0E02B2C3D479</cbc:UUID>
<cbc:IssueDate>2009-01-05</cbc:IssueDate>
<cbc:IssueTime>14:42</cbc:IssueTime>
<cbc:InvoiceTypeCode>SATIS</cbc:InvoiceTypeCode>
<cbc:DocumentCurrencyCode>TRY</cbc:DocumentCurrencyCode>
<cbc:LineCountNumeric>1</cbc:LineCountNumeric>
<cac:InvoicePeriod>
<cbc:StartDate>2008-12-05</cbc:StartDate>
<cbc:EndDate>2009-01-05</cbc:EndDate>
</cac:InvoicePeriod>
<cac:Signature>
<cbc:ID schemeID="VKN_TCKN">1288331521</cbc:ID>
<cac:SignatoryParty>
<cac:PartyIdentification>
<cbc:ID schemeID="VKN">1288331521</cbc:ID>
</cac:PartyIdentification>
<cac:PostalAddress>
<cbc:StreetName>Papatya Caddesi Yasemin Sokak</cbc:StreetName>
<cbc:BuildingNumber>21</cbc:BuildingNumber>
<cbc:CitySubdivisionName>Beşiktaş</cbc:CitySubdivisionName>
<cbc:CityName>İstanbul</cbc:CityName>
<cbc:PostalZone>34100</cbc:PostalZone>
<cac:Country>
<cbc:Name>Türkiye</cbc:Name>
</cac:Country>
</cac:PostalAddress>
</cac:SignatoryParty>
<cac:DigitalSignatureAttachment>
<cac:ExternalReference>
<cbc:URI>#Signature</cbc:URI>
</cac:ExternalReference>
</cac:DigitalSignatureAttachment>
</cac:Signature>
<cac:AccountingSupplierParty>
<cac:Party>
<cbc:WebsiteURI>http://www.aaa.com.tr/</cbc:WebsiteURI>
<cac:PartyIdentification>
<cbc:ID schemeID="VKN">1288331521</cbc:ID>
</cac:PartyIdentification>
<cac:PartyName>
<cbc:Name>AAA Anonim Şirketi</cbc:Name>
</cac:PartyName>
<cac:PostalAddress>
<cbc:ID>1234567890</cbc:ID>
<cbc:StreetName>Papatya Caddesi Yasemin Sokak</cbc:StreetName>
<cbc:BuildingNumber>21</cbc:BuildingNumber>
<cbc:CitySubdivisionName>Beşiktaş</cbc:CitySubdivisionName>
<cbc:CityName>İstanbul</cbc:CityName>
<cbc:PostalZone>34100</cbc:PostalZone>
<cac:Country>
<cbc:Name>Türkiye</cbc:Name>
</cac:Country>
</cac:PostalAddress>
<cac:PartyTaxScheme>
<cac:TaxScheme>
<cbc:Name>Büyük Mükellefler</cbc:Name>
</cac:TaxScheme>
</cac:PartyTaxScheme>
<cac:Contact>
<cbc:Telephone>(212) 925 51515</cbc:Telephone>
<cbc:Telefax>(212) 925505015</cbc:Telefax>
<cbc:ElectronicMail>[email protected]</cbc:ElectronicMail>
</cac:Contact>
</cac:Party>
</cac:AccountingSupplierParty>
<cac:AccountingCustomerParty>
<cac:Party>
<cbc:WebsiteURI/>
<cac:PartyIdentification>
<cbc:ID schemeID="TCKN">1234567890</cbc:ID>
</cac:PartyIdentification>
<cac:PartyIdentification>
<cbc:ID schemeID="TESISATNO">1234567</cbc:ID>
</cac:PartyIdentification>
<cac:PartyIdentification>
<cbc:ID schemeID="SAYACNO">12345678</cbc:ID>
</cac:PartyIdentification>
<cac:PostalAddress>
<cbc:ID>ATATÜRK MAH.</cbc:ID>
<cbc:Room>1</cbc:Room>
<cbc:StreetName>6. Sokak</cbc:StreetName>
<cbc:BuildingNumber>1</cbc:BuildingNumber>
<cbc:CitySubdivisionName>Beşiktaş</cbc:CitySubdivisionName>
<cbc:CityName>İstanbul</cbc:CityName>
<cbc:PostalZone>34100</cbc:PostalZone>
<cac:Country>
<cbc:Name>Türkiye</cbc:Name>
</cac:Country>
</cac:PostalAddress>
<cac:Contact>
<cbc:ElectronicMail>[email protected]</cbc:ElectronicMail>
</cac:Contact>
<cac:Person>
<cbc:FirstName>Ali</cbc:FirstName>
<cbc:FamilyName>YILMAZ</cbc:FamilyName>
</cac:Person>
</cac:Party>
</cac:AccountingCustomerParty>
<cac:PaymentTerms>
<cbc:Note>BBB Bank Otomatik Ödeme</cbc:Note>
<cbc:PaymentDueDate>2009-01-20</cbc:PaymentDueDate>
</cac:PaymentTerms>
<cac:TaxTotal>
<cbc:TaxAmount currencyID="TRY">2.73</cbc:TaxAmount>
<cac:TaxSubtotal>
<cbc:TaxableAmount currencyID="TRY">15.15</cbc:TaxableAmount>
<cbc:TaxAmount currencyID="TRY">2.73</cbc:TaxAmount>
<cac:TaxCategory>
<cac:TaxScheme>
<cbc:TaxTypeCode>0015</cbc:TaxTypeCode>
</cac:TaxScheme>
</cac:TaxCategory>
</cac:TaxSubtotal>
</cac:TaxTotal>
<cac:LegalMonetaryTotal>
<cbc:LineExtensionAmount currencyID="TRY">15.15</cbc:LineExtensionAmount>
<cbc:TaxExclusiveAmount currencyID="TRY">15.15</cbc:TaxExclusiveAmount>
<cbc:TaxInclusiveAmount currencyID="TRY">17.88</cbc:TaxInclusiveAmount>
<cbc:PayableAmount currencyID="TRY">17.88</cbc:PayableAmount>
</cac:LegalMonetaryTotal>
<cac:InvoiceLine>
<cbc:ID>1</cbc:ID>
<cbc:InvoicedQuantity unitCode="KWH">101</cbc:InvoicedQuantity>
<cbc:LineExtensionAmount currencyID="TRY">15.15</cbc:LineExtensionAmount>
<cac:AllowanceCharge>
<cbc:ChargeIndicator>false</cbc:ChargeIndicator>
<cbc:MultiplierFactorNumeric>0.0</cbc:MultiplierFactorNumeric>
<cbc:Amount currencyID="TRY">0</cbc:Amount>
<cbc:BaseAmount currencyID="TRY">15.15</cbc:BaseAmount>
</cac:AllowanceCharge>
<cac:TaxTotal>
<cbc:TaxAmount currencyID="TRY">2.73</cbc:TaxAmount>
<cac:TaxSubtotal>
<cbc:TaxableAmount currencyID="TRY">15.15</cbc:TaxableAmount>
<cbc:TaxAmount currencyID="TRY">2.73</cbc:TaxAmount>
<cbc:Percent>18.0</cbc:Percent>
<cac:TaxCategory>
<cac:TaxScheme>
<cbc:Name>KDV</cbc:Name>
<cbc:TaxTypeCode>0015</cbc:TaxTypeCode>
</cac:TaxScheme>
</cac:TaxCategory>
</cac:TaxSubtotal>
</cac:TaxTotal>
<cac:Item>
<cbc:Name>Elektrik Tüketim Bedeli</cbc:Name>
</cac:Item>
<cac:Price>
<cbc:PriceAmount currencyID="TRY">0.15</cbc:PriceAmount>
</cac:Price>
</cac:InvoiceLine>
</Invoice>
Loading

0 comments on commit 4d822f6

Please sign in to comment.