Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement reading and setting default values #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions facturx/facturx.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def __init__(self, pdf_invoice, flavor='factur-x', level='minimum'):
else:
self.flavor, self.xml = xml_flavor.XMLFlavor.from_template(flavor, level)
logger.info('PDF does not have XML embedded. Adding from template.')
self._add_default_to_xml()

self.flavor.check_xsd(self.xml)
self._namespaces = self.xml.nsmap
Expand Down Expand Up @@ -95,18 +96,25 @@ def _xml_from_file(self, pdf_file):
xml_filename)
return xml_content

def _add_default_to_xml(self):
fields_data = xml_flavor.FIELDS

for field, values in fields_data.items():
if '_default' in values and (self[field] is None or self[field] == "0"):
self[field] = fields_data[field]['_default']

def __getitem__(self, field_name):
path = self.flavor._get_xml_path(field_name)
value = self.xml.xpath(path, namespaces=self._namespaces)
if value is not None:
value = self.xml.xpath(path, namespaces=self.xml.nsmap)
if len(value) > 0:
value = value[0].text
if 'date' in field_name:
value = datetime.strptime(value, '%Y%m%d')
return value

def __setitem__(self, field_name, value):
path = self.flavor._get_xml_path(field_name)
res = self.xml.xpath(path, namespaces=self._namespaces)
res = self.xml.xpath(path, namespaces=self.xml.nsmap)
if len(res) > 1:
raise LookupError('Multiple nodes found for this path. Refusing to edit.')

Expand Down
2 changes: 1 addition & 1 deletion facturx/flavors/factur-x/xml/basic.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</rsm:ExchangedDocumentContext>
<rsm:ExchangedDocument>
<ram:ID></ram:ID>
<ram:TypeCode>380</ram:TypeCode>
<ram:TypeCode>0</ram:TypeCode>
<ram:IssueDateTime>
<udt:DateTimeString format="102"></udt:DateTimeString>
</ram:IssueDateTime>
Expand Down
2 changes: 1 addition & 1 deletion facturx/flavors/factur-x/xml/basicwl.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</rsm:ExchangedDocumentContext>
<rsm:ExchangedDocument>
<ram:ID></ram:ID>
<ram:TypeCode>380</ram:TypeCode>
<ram:TypeCode>0</ram:TypeCode>
<ram:IssueDateTime>
<udt:DateTimeString format="102"></udt:DateTimeString>
</ram:IssueDateTime>
Expand Down
2 changes: 1 addition & 1 deletion facturx/flavors/factur-x/xml/en16931.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</rsm:ExchangedDocumentContext>
<rsm:ExchangedDocument>
<ram:ID></ram:ID>
<ram:TypeCode>380</ram:TypeCode>
<ram:TypeCode>0</ram:TypeCode>
<ram:IssueDateTime>
<udt:DateTimeString format="102"></udt:DateTimeString>
</ram:IssueDateTime>
Expand Down
2 changes: 1 addition & 1 deletion facturx/flavors/factur-x/xml/minimum.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</rsm:ExchangedDocumentContext>
<rsm:ExchangedDocument>
<ram:ID></ram:ID>
<ram:TypeCode>380</ram:TypeCode>
<ram:TypeCode>0</ram:TypeCode>
<ram:IssueDateTime>
<udt:DateTimeString format="102"></udt:DateTimeString>
</ram:IssueDateTime>
Expand Down
2 changes: 1 addition & 1 deletion facturx/flavors/fields.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type:
factur-x: //rsm:ExchangedDocument/ram:TypeCode
zugferd: //rsm:HeaderExchangedDocument/ram:TypeCode
_required: true
_default: 380
_default: "380"
currency:
_path:
factur-x: //rsm:SupplyChainTradeTransaction/ram:ApplicableHeaderTradeSettlement/ram:InvoiceCurrencyCode
Expand Down
2 changes: 1 addition & 1 deletion facturx/flavors/zugferd/xml/basic.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<rsm:HeaderExchangedDocument>
<ram:ID></ram:ID>
<ram:Name></ram:Name>
<ram:TypeCode>380</ram:TypeCode>
<ram:TypeCode>0</ram:TypeCode>
<ram:IssueDateTime>
<udt:DateTimeString format="102"></udt:DateTimeString>
</ram:IssueDateTime>
Expand Down
2 changes: 1 addition & 1 deletion facturx/flavors/zugferd/xml/comfort.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<rsm:HeaderExchangedDocument>
<ram:ID></ram:ID>
<ram:Name></ram:Name>
<ram:TypeCode>380</ram:TypeCode>
<ram:TypeCode>0</ram:TypeCode>
<ram:IssueDateTime><udt:DateTimeString format="102"></udt:DateTimeString></ram:IssueDateTime>
<ram:IncludedNote>
<ram:Content></ram:Content>
Expand Down
30 changes: 15 additions & 15 deletions facturx/tests/compare/no_embedded_data.xml
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
<rsm:CrossIndustryInvoice xmlns:qdt="urn:un:unece:uncefact:data:standard:QualifiedDataType:100" xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100" xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100" xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<rsm:ExchangedDocumentContext>
<ram:BusinessProcessSpecifiedDocumentContextParameter>
<ram:ID>A1</ram:ID>
<ram:ID/>
</ram:BusinessProcessSpecifiedDocumentContextParameter>
<ram:GuidelineSpecifiedDocumentContextParameter>
<ram:ID>urn:factur-x.eu:1p0:minimum</ram:ID>
</ram:GuidelineSpecifiedDocumentContextParameter>
</rsm:ExchangedDocumentContext>
<rsm:ExchangedDocument>
<ram:ID>2017-TEST-04</ram:ID>
<ram:ID/>
<ram:TypeCode>380</ram:TypeCode>
<ram:IssueDateTime>
<udt:DateTimeString format="102">20171031</udt:DateTimeString>
<udt:DateTimeString format="102"/>
</ram:IssueDateTime>
</rsm:ExchangedDocument>
<rsm:SupplyChainTradeTransaction>
<ram:ApplicableHeaderTradeAgreement>
<ram:BuyerReference>CodeSERVICE A</ram:BuyerReference>
<ram:BuyerReference/>
<ram:SellerTradeParty>
<ram:Name>FOURNISSEUR F</ram:Name>
<ram:Name/>
<ram:SpecifiedLegalOrganization>
<ram:ID schemeID="0002">99988877900017</ram:ID>
<ram:ID schemeID=""/>
</ram:SpecifiedLegalOrganization>
<ram:PostalTradeAddress>
<ram:CountryID>FR</ram:CountryID>
<ram:CountryID/>
</ram:PostalTradeAddress>
<ram:SpecifiedTaxRegistration>
<ram:ID schemeID="VA">FR34999888779</ram:ID>
<ram:ID schemeID=""/>
</ram:SpecifiedTaxRegistration>
</ram:SellerTradeParty>
<ram:BuyerTradeParty>
<ram:Name>CLIENT 1</ram:Name>
<ram:Name/>
<ram:SpecifiedLegalOrganization>
<ram:ID schemeID="0002">77788899100018</ram:ID>
<ram:ID schemeID=""/>
</ram:SpecifiedLegalOrganization>
</ram:BuyerTradeParty>
<ram:BuyerOrderReferencedDocument>
<ram:IssuerAssignedID>CDE234</ram:IssuerAssignedID>
<ram:IssuerAssignedID/>
</ram:BuyerOrderReferencedDocument>
</ram:ApplicableHeaderTradeAgreement>
<ram:ApplicableHeaderTradeDelivery/>
<ram:ApplicableHeaderTradeSettlement>
<ram:InvoiceCurrencyCode>EUR</ram:InvoiceCurrencyCode>
<ram:SpecifiedTradeSettlementHeaderMonetarySummation>
<ram:TaxBasisTotalAmount currencyID="EUR">15976.87</ram:TaxBasisTotalAmount>
<ram:TaxTotalAmount currencyID="EUR">3195.37</ram:TaxTotalAmount>
<ram:GrandTotalAmount currencyID="EUR">19172.24</ram:GrandTotalAmount>
<ram:DuePayableAmount currencyID="EUR">19172.24</ram:DuePayableAmount>
<ram:TaxBasisTotalAmount currencyID="EUR">0</ram:TaxBasisTotalAmount>
<ram:TaxTotalAmount currencyID="EUR">0</ram:TaxTotalAmount>
<ram:GrandTotalAmount currencyID="EUR">0</ram:GrandTotalAmount>
<ram:DuePayableAmount currencyID="EUR">0</ram:DuePayableAmount>
</ram:SpecifiedTradeSettlementHeaderMonetarySummation>
</ram:ApplicableHeaderTradeSettlement>
</rsm:SupplyChainTradeTransaction>
Expand Down
17 changes: 13 additions & 4 deletions facturx/tests/test_facturx.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import unittest
from facturx.facturx import *
from lxml import etree
from datetime import datetime as dt


class TestReading(unittest.TestCase):
Expand Down Expand Up @@ -36,7 +37,7 @@ def test_file_embedded_data(self, file_name='embedded_data.pdf'):
self.assertTrue(FacturX(file_path)._xml_from_file(file_path) is not None, "The PDF file has no embedded file")

def test_write_pdf(self):
file_path = self.find_file('no_embedded_data.pdf')
file_path = self.find_file('embedded_data.pdf')
factx = FacturX(file_path)
test_file_path = os.path.join(self.test_files_dir, 'test.pdf')

Expand All @@ -46,13 +47,15 @@ def test_write_pdf(self):
self.discover_files()

# checking that xml is embedded
self.assertTrue(self.test_file_embedded_data(file_name='test.pdf') is None)
self.assertTrue(
self.test_file_embedded_data(file_name='test.pdf') is None)

os.remove(test_file_path)

def test_write_xml(self):
compare_file_dir = os.path.join(os.path.dirname(__file__), 'compare')
expected_file_path = os.path.join(compare_file_dir, 'no_embedded_data.xml')
expected_file_path = os.path.join(compare_file_dir,
'no_embedded_data.xml')
test_file_path = os.path.join(compare_file_dir, 'test.xml')

factx = FacturX(self.find_file('no_embedded_data.pdf'))
Expand All @@ -67,9 +70,15 @@ def test_write_xml(self):
test_file_root = etree.XML(test_file.read(), parser)
test_file_str = etree.tostring(test_file_root)

self.assertTrue(expected_file_str == test_file_str, "Files don't match")
self.assertEqual(expected_file_str, test_file_str, "Files don't match")
os.remove(test_file_path)

def test_default_value(self):
file_path = self.find_file('no_embedded_data.pdf')
factx = FacturX(file_path)
self.assertEqual(factx['type'], "380",
"default value of type doesn't match")


def main():
unittest.main()
Expand Down