d112.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. '''Created Jan 25, 2022 Levi'''
  2. from collections import defaultdict
  3. import xlsxwriter as xlsw
  4. from lxml import etree as ET
  5. from declarations.declaration import Declaration
  6. class D112(Declaration):
  7. def __init__(self):
  8. super().__init__()
  9. def from_xml(self, fp):
  10. self.obligs = []
  11. self.list_obligs = ()
  12. xml = ET.parse(fp)
  13. root = xml.getroot()
  14. def_root = defaultdict(lambda: 0)
  15. def_root.update(root.attrib)
  16. self.def_root = def_root
  17. # print(def_root)
  18. self.employer = xml.xpath('//x:angajator', namespaces={'x': 'mfp:anaf:dgti:declaratie_unica:declaratie:v6'})
  19. self.cui = int(self.employer[0].attrib['cif'])
  20. self.den = self.employer[0].attrib['den']
  21. self.month = int(def_root['luna_r'])
  22. self.year = int(def_root['an_r'])
  23. self.drect = int(def_root['d_rec'])
  24. self.total_payable = int(self.employer[0].attrib['totalPlata_A'])
  25. # print(self.month)
  26. self.employerA = xml.xpath('//x:angajator/*', namespaces={'x': 'mfp:anaf:dgti:declaratie_unica:declaratie:v6'})
  27. # self.tree = xml.xpath('/x:declaratieUnica/*', namespaces={'x': 'mfp:anaf:dgti:declaratie_unica:declaratie:v6'})
  28. # print(self.employer[0].attrib['cif'])
  29. self.root = root
  30. self.decl_type = '112'
  31. for oblig in self.employerA:
  32. if ET.QName(oblig).localname == 'angajatorA':
  33. self.obligs.append(defaultdict(lambda: 0, oblig.attrib)['A_codOblig'])
  34. self.obligs.append(defaultdict(lambda: 0, oblig.attrib)['A_codBugetar'])
  35. self.obligs.append(int(defaultdict(lambda: 0, oblig.attrib)['A_datorat']))
  36. self.obligs.append(int(defaultdict(lambda: 0, oblig.attrib)['A_scutit']))
  37. self.obligs.append(int(defaultdict(lambda: 0, oblig.attrib)['A_plata']))
  38. for oblig in self.employerA:
  39. if ET.QName(oblig).localname == 'angajatorA':
  40. # print(oblig)
  41. obligs = []
  42. obligs.append(defaultdict(lambda: 0, oblig.attrib)['A_codOblig'])
  43. obligs.append(defaultdict(lambda: 0, oblig.attrib)['A_codBugetar'])
  44. obligs.append(int(defaultdict(lambda: 0, oblig.attrib)['A_datorat']))
  45. obligs.append(int(defaultdict(lambda: 0, oblig.attrib)['A_scutit']))
  46. obligs.append(int(defaultdict(lambda: 0, oblig.attrib)['A_plata']))
  47. self.list_obligs += obligs,
  48. return self
  49. @property
  50. def data(self):
  51. data = {
  52. self.cui:
  53. [
  54. self.den,
  55. self.month,
  56. self.year,
  57. self.drect,
  58. self.total_payable
  59. ]
  60. }
  61. self.dict_data = data
  62. return self.dict_data
  63. def to_xlsx(self, xl_name, w_sheet_name, files, headers=[], **kwargs):
  64. book = xlsw.Workbook(xl_name + '.xlsm')
  65. book.add_vba_project('./vbaProject.bin')
  66. sheet = book.add_worksheet(w_sheet_name)
  67. # sheet2 = book.add_worksheet('all')
  68. row, row2, col = 1, 1, 0
  69. headformat = book.add_format({'bold': True,
  70. 'text_wrap': True})
  71. # text_format = book.add_format({'text_wrap': True})
  72. hcol= 0
  73. for h in headers:
  74. sheet.write(1, hcol, h, headformat)
  75. hcol += 1
  76. for file in files:
  77. d = D112() # obj['type']()
  78. d = d.from_xml(file)
  79. for i in d.data:
  80. row += 1
  81. sheet.write(row, col, i)
  82. print(f'write {row}', i)
  83. data = d.data[i]
  84. data.extend(d.obligs)
  85. sheet.write_row(row, 1, data) # d.data[i]
  86. for k, a in kwargs.items():
  87. if k == 'autofilter':
  88. sheet.autofilter(a[0], a[1], 100, 100)
  89. # sheet2.autofilter(a[0], a[1], 1000, 1000)
  90. if k == 'freeze':
  91. sheet.freeze_panes(a[0], a[1])
  92. # sheet2.freeze_panes(a[0], a[1])
  93. book.close()
  94. def to_xlsx2(self, xl_name, w_sheet_name, files, headers=[], **kwargs):
  95. book = xlsw.Workbook(xl_name + '.xlsm')
  96. book.add_vba_project('./vbaProject.bin')
  97. sheet = book.add_worksheet(w_sheet_name)
  98. # sheet2 = book.add_worksheet('all')
  99. row, row2, col = 1, 1, 0
  100. headformat = book.add_format({'bold': True,
  101. 'text_wrap': True})
  102. # text_format = book.add_format({'text_wrap': True})
  103. hcol, hcol2 = 0, 0
  104. for h in headers:
  105. sheet.write(1, hcol, h, headformat)
  106. hcol += 1
  107. for file in files:
  108. d = D112() # obj['type']()
  109. d = d.from_xml(file)
  110. # print(d.list_obligs)
  111. for cod, bug, dat, sc, plata in d.list_obligs:
  112. row+=1
  113. sheet.write(row, hcol2, self.cui)
  114. sheet.write(row, hcol2 + 1, self.den)
  115. sheet.write(row, hcol2 + 2, self.month)
  116. sheet.write(row, hcol2 + 3, self.year)
  117. sheet.write(row, hcol2 + 4, self.drect)
  118. sheet.write(row, hcol2 + 5, self.total_payable)
  119. sheet.write(row, hcol2 + 6, cod)
  120. sheet.write(row, hcol2 + 7, bug)
  121. sheet.write(row, hcol2 + 8, dat)
  122. sheet.write(row, hcol2 + 9, sc)
  123. sheet.write(row, hcol2 + 10, plata)
  124. for k, a in kwargs.items():
  125. if k == 'autofilter':
  126. sheet.autofilter(a[0], a[1], 100, 100)
  127. # sheet2.autofilter(a[0], a[1], 1000, 1000)
  128. if k == 'freeze':
  129. sheet.freeze_panes(a[0], a[1])
  130. # sheet2.freeze_panes(a[0], a[1])
  131. book.close()
  132. if __name__ == '__main__':
  133. headers = ['cod\nfiscal', 'den', 'luna', 'an', 'rect', 'total', 'cod\noblig', 'cod\nbuget', 'dat', 'scutit', 'plata', 'cod\noblig', 'cod\nbuget', 'dat', 'scutit', 'plata',
  134. 'cod\noblig', 'cod\nbuget', 'dat', 'scutit', 'plata', 'cod\noblig', 'cod\nbuget', 'dat', 'scutit', 'plata']
  135. d100 = D112()
  136. d100.from_xml('D112_44122255_2022_12.xml')
  137. d100.to_xlsx2('D112', 'data', ['D112_44122255_2022_12.xml'], headers, autofilter=[1, 0], freeze=[2, 1])
  138. # print(d100.data)