123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- '''
- Created on Feb 12, 2020 @author: deeejas
- '''
- import xml.etree.cElementTree as ET
- import pandas as pd
- import datetime as dt
- def indent(elem, level=0):
- i = "\n" + level * " "
- if len(elem):
- # print(len(elem))
- if not elem.text or not elem.text.strip():
- elem.text = i + " "
- # print(elem.text)
- if not elem.tail or not elem.tail.strip():
- elem.tail = i
- # print(elem.tail)
- for elem in elem:
- indent(elem, level + 1)
- if not elem.tail or not elem.tail.strip():
- elem.tail = i
- else:
- if level and (not elem.tail or not elem.tail.strip()):
- elem.tail = i
- class ContactPerson(object):
- def __init__(self, LastName=None, FirstName=None, Email=None,
- Phone=None, Fax=None, Position=None):
- self.LastName = LastName
- self.FirstName = FirstName
- self.Email = Email
- self.Phone = Phone
- self.Fax = Fax
- self.Position = Position
- class Company(object):
- '''
- classdocs
- '''
- def __init__(self, VatNr=None, FirmName=None, RefPeriod=None,
- CreateDt=None, ApplicationRef=None, ContactPerson=None):
- '''
- Constructor
- '''
- self.VatNr = VatNr
- self.FirmName = FirmName
- self.RefPeriod = RefPeriod
- self.CreateDt = CreateDt or dt.datetime.now().strftime('%Y-%m-%dT%H:%M:%S-08:00')
- self.ApplicationRef = ApplicationRef
- self.ContactPerson = ContactPerson
- class Intrastat(object):
- def __init__(self, company: object, person: object, versions: dict,
- header: tuple, elem_attribs: dict, supplucodes: set):
- # temporary remove filename attribute as it's only need for study
- # self.filename = filename
- self.company = company
- self.person = person
- self.versions = versions
- self.header = header
- self.elem_attribs = elem_attribs
- # self.decl = ET.parse(self.filename)
- self.supplucodes = supplucodes
- def make_arrival(self):
- # creates the type of declaration: arrival/exp
- arrival = ET.Element('InsNewArrival', self.elem_attribs)
- code_vers = ET.SubElement(arrival, self.header[0])
- decl_header = ET.SubElement(arrival, self.header[1])
- company_dict = vars(self.company)
- person_dict = vars(self.person)
- self.arrival = arrival
- for tagname, value in self.versions.items():
- e = ET.SubElement(code_vers, tagname)
- e.text = value
- for tagname, value in company_dict.items():
- e = ET.SubElement(decl_header, tagname)
- e.text = value
- if tagname == 'ContactPerson':
- for subtagname, subvalue in person_dict.items():
- se = ET.SubElement(e, subtagname)
- se.text = subvalue
- def make_arrival_items(self, source_xls):
- # makes each position of the decl. is subelement of newarrival
- dtypes = {'NETWEIGHT': 'int64', 'NETVALUE': 'int64'}
- self.source_xls = source_xls
- df = pd.read_excel(self.source_xls, dtype=dtypes)
- self.grouped_df = df.groupby(
- [
- 'VSCODE',
- 'ACode',
- 'Bcode',
- 'DeliveryTermsCode',
- 'TransportCode',
- 'CountryOfOrigin',
- 'EXP',
- 'SUPPL'
- ])[
- ['NETVALUE',
- 'NETWEIGHT',
- 'Q',
- 'SUPPLVALUE'
- ]].sum()
- # helper function called here
- print(self.grouped_df)
- # print(self.grouped_df['NETVALUE'].iloc[0])
- self._arrival_items(self.grouped_df)
- def _arrival_items(self, dframe):
- # loops over grouped df and adds items
- for n in range(1, len(dframe) + 1):
- order_attrib = {'OrderNr': f'{n}'}
- # print(order_attrib)
- new_item = ET.SubElement(self.arrival, 'InsArrivalItem', order_attrib)
- new_item_code = ET.SubElement(new_item, 'Cn8Code')
- new_item_code.text = f'{self.grouped_df.index[n-1][0]}'
- new_item_val = ET.SubElement(new_item, 'InvoiceValue')
- new_item_val.text = f'{self.grouped_df["NETVALUE"].iloc[n-1]}'
- new_item_statval = ET.SubElement(new_item, 'StatisticalValue')
- if self.grouped_df.index[n - 1][7] == 1:
- new_item_statval.text = f'{int(self.grouped_df["NETVALUE"].iloc[n-1]-self.grouped_df["SUPPLVALUE"].iloc[n-1]*0.12)}'
- elif self.grouped_df.index[n - 1][7] == 2:
- new_item_statval.text = f'{int(self.grouped_df["NETVALUE"].iloc[n-1]+self.grouped_df["SUPPLVALUE"].iloc[n-1]*0.88)}'
- else:
- new_item_statval.text = f'{self.grouped_df["NETVALUE"].iloc[n-1]}'
- new_item_netmass = ET.SubElement(new_item, 'NetMass')
- new_item_netmass.text = f'{self.grouped_df["NETWEIGHT"].iloc[n-1]}'
- new_item_acode = ET.SubElement(new_item, 'NatureOfTransactionACode')
- new_item_acode.text = f'{self.grouped_df.index[n-1][1]}'
- new_item_bcode = ET.SubElement(new_item, 'NatureOfTransactionBCode')
- new_item_bcode.text = f'{self.grouped_df.index[n-1][2]}'
- new_item_delterms = ET.SubElement(new_item, 'DeliveryTermsCode')
- new_item_delterms.text = f'{self.grouped_df.index[n-1][3]}'
- new_item_transpcode = ET.SubElement(new_item, 'ModeOfTransportCode')
- new_item_transpcode.text = f'{self.grouped_df.index[n-1][4]}'
- new_item_origin = ET.SubElement(new_item, 'CountryOfOrigin')
- new_item_origin.text = f'{self.grouped_df.index[n-1][5]}'
- if f'{self.grouped_df.index[n-1][0]}' in self.supplucodes:
- new_item_supplunits = ET.SubElement(new_item, 'InsSupplUnitsInfo')
- new_item_supplunitcode = ET.SubElement(new_item_supplunits, 'SupplUnitCode')
- new_item_supplunitcode.text = 'p/st'
- new_item_supplunits = ET.SubElement(new_item_supplunits, 'QtyInSupplUnits')
- new_item_supplunits.text = f'{self.grouped_df["Q"].iloc[n-1]}'
- new_item_exp = ET.SubElement(new_item, 'CountryOfConsignment')
- new_item_exp.text = f'{self.grouped_df.index[n-1][6]}'
- def write_xml(self):
- ns = ET.register_namespace('InsNewArrival',
- 'http://www.intrastat.ro/xml/InsSchema')
- todecl = ET.ElementTree(self.arrival)
- indent(todecl._root)
- todecl.write(
- 'xml/{}_{}.xml'.format(self.company.VatNr.replace('00', '', 1),
- self.company.RefPeriod),
- encoding='UTF-8',
- xml_declaration=True,
- default_namespace=ns,
- method='xml')
- # EOF
|