123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- '''Created Jan 25, 2022 Levi'''
- from collections import defaultdict
- import xlsxwriter as xlsw
- from lxml import etree as ET
- from declarations.declaration import Declaration
- class D112(Declaration):
- def __init__(self):
- super().__init__()
- def from_xml(self, fp):
- self.obligs = []
- self.list_obligs = ()
- xml = ET.parse(fp)
- root = xml.getroot()
- def_root = defaultdict(lambda: 0)
- def_root.update(root.attrib)
- self.def_root = def_root
- # print(def_root)
- self.employer = xml.xpath('//x:angajator', namespaces={'x': 'mfp:anaf:dgti:declaratie_unica:declaratie:v6'})
- self.cui = int(self.employer[0].attrib['cif'])
- self.den = self.employer[0].attrib['den']
- self.month = int(def_root['luna_r'])
- self.year = int(def_root['an_r'])
- self.drect = int(def_root['d_rec'])
- self.total_payable = int(self.employer[0].attrib['totalPlata_A'])
- # print(self.month)
- self.employerA = xml.xpath('//x:angajator/*', namespaces={'x': 'mfp:anaf:dgti:declaratie_unica:declaratie:v6'})
- # self.tree = xml.xpath('/x:declaratieUnica/*', namespaces={'x': 'mfp:anaf:dgti:declaratie_unica:declaratie:v6'})
- # print(self.employer[0].attrib['cif'])
- self.root = root
- self.decl_type = '112'
- for oblig in self.employerA:
- if ET.QName(oblig).localname == 'angajatorA':
- self.obligs.append(defaultdict(lambda: 0, oblig.attrib)['A_codOblig'])
- self.obligs.append(defaultdict(lambda: 0, oblig.attrib)['A_codBugetar'])
- self.obligs.append(int(defaultdict(lambda: 0, oblig.attrib)['A_datorat']))
- self.obligs.append(int(defaultdict(lambda: 0, oblig.attrib)['A_scutit']))
- self.obligs.append(int(defaultdict(lambda: 0, oblig.attrib)['A_plata']))
- for oblig in self.employerA:
- if ET.QName(oblig).localname == 'angajatorA':
- # print(oblig)
- obligs = []
- obligs.append(defaultdict(lambda: 0, oblig.attrib)['A_codOblig'])
- obligs.append(defaultdict(lambda: 0, oblig.attrib)['A_codBugetar'])
- obligs.append(int(defaultdict(lambda: 0, oblig.attrib)['A_datorat']))
- obligs.append(int(defaultdict(lambda: 0, oblig.attrib)['A_scutit']))
- obligs.append(int(defaultdict(lambda: 0, oblig.attrib)['A_plata']))
- self.list_obligs += obligs,
- return self
- @property
- def data(self):
- data = {
- self.cui:
- [
- self.den,
- self.month,
- self.year,
- self.drect,
- self.total_payable
- ]
- }
- self.dict_data = data
- return self.dict_data
- def to_xlsx(self, xl_name, w_sheet_name, files, headers=[], **kwargs):
- book = xlsw.Workbook(xl_name + '.xlsm')
- book.add_vba_project('./vbaProject.bin')
- sheet = book.add_worksheet(w_sheet_name)
- # sheet2 = book.add_worksheet('all')
- row, row2, col = 1, 1, 0
- headformat = book.add_format({'bold': True,
- 'text_wrap': True})
- # text_format = book.add_format({'text_wrap': True})
- hcol= 0
- for h in headers:
- sheet.write(1, hcol, h, headformat)
- hcol += 1
- for file in files:
- d = D112() # obj['type']()
- d = d.from_xml(file)
- for i in d.data:
- row += 1
- sheet.write(row, col, i)
- print(f'write {row}', i)
- data = d.data[i]
- data.extend(d.obligs)
- sheet.write_row(row, 1, data) # d.data[i]
- for k, a in kwargs.items():
- if k == 'autofilter':
- sheet.autofilter(a[0], a[1], 100, 100)
- # sheet2.autofilter(a[0], a[1], 1000, 1000)
- if k == 'freeze':
- sheet.freeze_panes(a[0], a[1])
- # sheet2.freeze_panes(a[0], a[1])
- book.close()
-
- def to_xlsx2(self, xl_name, w_sheet_name, files, headers=[], **kwargs):
- book = xlsw.Workbook(xl_name + '.xlsm')
- book.add_vba_project('./vbaProject.bin')
- sheet = book.add_worksheet(w_sheet_name)
- # sheet2 = book.add_worksheet('all')
- row, row2, col = 1, 1, 0
- headformat = book.add_format({'bold': True,
- 'text_wrap': True})
- # text_format = book.add_format({'text_wrap': True})
- hcol, hcol2 = 0, 0
- for h in headers:
- sheet.write(1, hcol, h, headformat)
- hcol += 1
- for file in files:
- d = D112() # obj['type']()
- d = d.from_xml(file)
- # print(d.list_obligs)
- for cod, bug, dat, sc, plata in d.list_obligs:
- row+=1
- sheet.write(row, hcol2, self.cui)
- sheet.write(row, hcol2 + 1, self.den)
- sheet.write(row, hcol2 + 2, self.month)
- sheet.write(row, hcol2 + 3, self.year)
- sheet.write(row, hcol2 + 4, self.drect)
- sheet.write(row, hcol2 + 5, self.total_payable)
- sheet.write(row, hcol2 + 6, cod)
- sheet.write(row, hcol2 + 7, bug)
- sheet.write(row, hcol2 + 8, dat)
- sheet.write(row, hcol2 + 9, sc)
- sheet.write(row, hcol2 + 10, plata)
- for k, a in kwargs.items():
- if k == 'autofilter':
- sheet.autofilter(a[0], a[1], 100, 100)
- # sheet2.autofilter(a[0], a[1], 1000, 1000)
- if k == 'freeze':
- sheet.freeze_panes(a[0], a[1])
- # sheet2.freeze_panes(a[0], a[1])
- book.close()
- if __name__ == '__main__':
- headers = ['cod\nfiscal', 'den', 'luna', 'an', 'rect', 'total', 'cod\noblig', 'cod\nbuget', 'dat', 'scutit', 'plata', 'cod\noblig', 'cod\nbuget', 'dat', 'scutit', 'plata',
- 'cod\noblig', 'cod\nbuget', 'dat', 'scutit', 'plata', 'cod\noblig', 'cod\nbuget', 'dat', 'scutit', 'plata']
- d100 = D112()
- d100.from_xml('D112_44122255_2022_12.xml')
- d100.to_xlsx2('D112', 'data', ['D112_44122255_2022_12.xml'], headers, autofilter=[1, 0], freeze=[2, 1])
- # print(d100.data)
|