mentorutils.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. '''
  2. Created on Jul 12, 2018 @author: levente.marton
  3. '''
  4. # from . import WinMentor
  5. import xlsxwriter as xlw # type: ignore
  6. from xlsxwriter.worksheet import Worksheet as sheet_ # type: ignore
  7. from .dbread import WinMentor
  8. class Mentorutils(WinMentor):
  9. '''
  10. winmentor subclass to generate xlsx situations
  11. '''
  12. def __init__(self): # , winment_path='c:/winment/data/'):
  13. '''
  14. Constructor
  15. '''
  16. # self.winment_path = winment_path
  17. self.row, self.col = 3, 0
  18. super().__init__()
  19. def gen_txt(self, db_file='/firme.db', file_name=None, attr_index=1, name=None):
  20. self.db_file = db_file
  21. self.file_name = file_name
  22. self.attr_index = attr_index
  23. if file_name:
  24. with open(file_name, 'w') as my_file:
  25. my_file.write(''.join('{0},\n'.format(_row) for _row in self.verif_corp(db_file, name) if attr_index == 'all'))
  26. my_file.write(''.join('{0},\n'.format(_row[attr_index]) for _row in self.verif_corp(db_file, name)))
  27. return file_name
  28. elif attr_index == 'all':
  29. return [_row for _row in self.verif_corp(db_file, name)]
  30. else:
  31. return [_row[attr_index] for _row in self.verif_corp(db_file, name) if _row[attr_index] is not None]
  32. def _preformat_wb(self, xl_name, w_sheet_name, n_headers=[], n_headers2=[]):
  33. dest_book = xlw.Workbook(xl_name + '.xlsm')
  34. dest_book.add_vba_project('./vbaProject.bin')
  35. sheet_ = dest_book.add_worksheet(w_sheet_name)
  36. sheet2_ = dest_book.add_worksheet('to')
  37. head_format = dest_book.add_format({'bold': True, 'text_wrap': True})
  38. num_format = dest_book.add_format()
  39. num_format.set_num_format('#,##0.00')
  40. sheet_.autofilter(self.row - 1, 0, 100, len(n_headers) - 1)
  41. sheet_.freeze_panes(self.row, 0)
  42. sheet2_.autofilter(self.row - 1, 0, 100, len(n_headers) - 1)
  43. sheet2_.freeze_panes(self.row, 0)
  44. hcol = 0
  45. for header in n_headers:
  46. sheet_.write(self.row - 1, hcol, header, head_format)
  47. hcol += 1
  48. hcol = 0
  49. for header in n_headers2:
  50. sheet2_.write(self.row - 1, hcol, header, head_format)
  51. hcol += 1
  52. return (sheet_, sheet2_, dest_book, num_format)
  53. def gen_turnover(self, xl_name, w_sheet_name, seq, m_month=None, n_headers=[]):
  54. sheet_ = self._preformat_wb(xl_name, w_sheet_name, n_headers)
  55. sheet_[0].set_column('L:Q', 11, cell_format=sheet_[2])
  56. sheet_[0].set_column('A:A', 20)
  57. sheet_[0].set_column('B:B', 11)
  58. for company in sorted(seq):
  59. if m_month:
  60. if m_month >= self.get_last_month(company[6]):
  61. current_month = self.get_last_month(company[6])
  62. else:
  63. current_month = m_month
  64. else:
  65. current_month = self.get_last_month(company[6])
  66. company.append(self.an_inc(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB'), 0, 6, 8))
  67. company.append(self.result(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB'), 0, 6, 8))
  68. company.append(self.divid(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  69. company.append(self.divid_current(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  70. company.append(self.divid_intermed(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  71. company.append(self.divid_inter_current(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  72. company.append(self.spons(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  73. company.append(current_month)
  74. sheet_[0].write_row(self.row, self.col, company,)
  75. self.row += 1
  76. print('Processing:', company[0])
  77. print('_done_')
  78. sheet_[1].close()
  79. def gen_tax_payable(self, xl_name, w_sheet_name, seq, m_month=None, n_headers=[], n_headers2=[], firm_list=False):
  80. sheet_ = self._preformat_wb(xl_name, w_sheet_name, n_headers, n_headers2)
  81. # sheet2_ = self._preformat_wb(xl_name, w_sheet_name, n_headers2)
  82. sheet_[0].set_column('L:U', 11, cell_format=sheet_[3])
  83. sheet_[0].set_column('A:A', 20)
  84. sheet_[0].set_column('B:B', 11)
  85. sheet_[1].set_column('L:Q', 11, cell_format=sheet_[3])
  86. sheet_[1].set_column('A:A', 20)
  87. sheet_[1].set_column('B:B', 11)
  88. # seq2 = seq.copy()
  89. if not firm_list:
  90. sheet_[0].set_column('D:J', None, None, {'hidden': True})
  91. for company in sorted(seq):
  92. if m_month:
  93. if m_month >= self.get_last_month(company[6]):
  94. current_month = self.get_last_month(company[6])
  95. else:
  96. current_month = m_month
  97. else:
  98. current_month = self.get_last_month(company[6])
  99. company.append(self.vat_payable(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  100. company.append(self.vat_final(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  101. company.append(self.tax_payable(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  102. company.append(self.div_tax_payable(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  103. company.append(self.other_tax_payable(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  104. company.append(self.CAS_payable(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  105. company.append(self.CASS_payable(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  106. company.append(self.sal_tax_payable(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  107. company.append(self.cam_payable(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  108. company.append(self.an_inc(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB'), 0, 6, 8))
  109. company.append(self.advance_revenue(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  110. company.append(self.result(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB'), 0, 6, 8))
  111. company.append(current_month)
  112. sheet_[0].write_row(self.row, self.col, company,)
  113. self.row += 1
  114. print('Processing:', company[0], '>> taxes')
  115. # seq2 = sorted(seq[:11])
  116. self.row = 3
  117. sheet_[1].set_column('D:J', None, None, {'hidden': True})
  118. for company in sorted(seq):
  119. if m_month:
  120. if m_month >= self.get_last_month(company[6]):
  121. current_month = self.get_last_month(company[6])
  122. else:
  123. current_month = m_month
  124. else:
  125. current_month = self.get_last_month(company[6])
  126. company = company[:11]
  127. # company.append(self.an_inc(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB'), 0, 6, 8))
  128. # company.append(self.result(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB'), 0, 6, 8))
  129. company.append(self.divid(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  130. company.append(self.divid_current(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  131. company.append(self.divid_intermed(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  132. company.append(self.divid_inter_current(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  133. company.append(self.spons(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  134. company.append(self.advance_final(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  135. company.append(self.deb_div(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  136. company.append(self.credit_final(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  137. company.append(current_month)
  138. sheet_[1].write_row(self.row, self.col, company,)
  139. self.row += 1
  140. print('Processing:', company[0], ' >> indicators')
  141. # print('_done_')
  142. else:
  143. for company in sorted(seq):
  144. sheet_[0].write_row(self.row, self.col, company,)
  145. self.row += 1
  146. print('Processing:', company[0])
  147. print('_done_')
  148. sheet_[2].close()
  149. def gen_society(self, xl_name, w_sheet_name, seq, society, n_headers=[], range_year=None, range_month=None):
  150. sheet_ = self._preformat_wb(xl_name, w_sheet_name, n_headers)
  151. sheet_[0].set_column('L:U', 11, cell_format=sheet_[2])
  152. sheet_[0].set_column('A:A', 20)
  153. sheet_[0].set_column('B:B', 11)
  154. sheet_[0].set_column('D:J', None, None, {'hidden': True})
  155. for year_ in range(int(range_year[:4]), int(range_year[-4:]) + 1):
  156. for month_ in range(int(range_month[:2]), int(range_month[-2:]) + 1):
  157. for company in sorted(seq):
  158. # print(company)
  159. if company[6] == society:
  160. company = company[:11]
  161. company.append(self.vat_payable(self.verif_cont(company[6] + '/' + '{}_{:02d}'.format(year_, month_) + './NCONT.DB')))
  162. company.append(self.vat_final(self.verif_cont(company[6] + '/' + '{}_{:02d}'.format(year_, month_) + './NCONT.DB')))
  163. company.append(self.tax_payable(self.verif_cont(company[6] + '/' + '{}_{:02d}'.format(year_, month_) + './NCONT.DB')))
  164. company.append(self.div_tax_payable(self.verif_cont(company[6] + '/' + '{}_{:02d}'.format(year_, month_) + './NCONT.DB')))
  165. company.append(self.CAS_payable(self.verif_cont(company[6] + '/' + '{}_{:02d}'.format(year_, month_) + './NCONT.DB')))
  166. company.append(self.CASS_payable(self.verif_cont(company[6] + '/' + '{}_{:02d}'.format(year_, month_) + './NCONT.DB')))
  167. company.append(self.sal_tax_payable(self.verif_cont(company[6] + '/' + '{}_{:02d}'.format(year_, month_) + './NCONT.DB')))
  168. company.append(self.cam_payable(self.verif_cont(company[6] + '/' + '{}_{:02d}'.format(year_, month_) + './NCONT.DB')))
  169. company.append(self.an_inc(self.verif_cont(company[6] + '/' + '{}_{:02d}'.format(year_, month_) + './NCONT.DB'), 0, 6, 8))
  170. company.append(self.result(self.verif_cont(company[6] + '/' + '{}_{:02d}'.format(year_, month_) + './NCONT.DB'), 0, 6, 8))
  171. company.append(year_)
  172. company.append(month_)
  173. sheet_[0].write_row(self.row, self.col, company)
  174. self.row += 1
  175. print('Processing:', company[0], '{}_{}'.format(year_, month_))
  176. sheet_[1].close()
  177. def gen_sal(self, xl_name, w_sheet_name, seq, m_month=None, n_headers=[]):
  178. sheet_ = self._preformat_wb(xl_name, w_sheet_name, n_headers)
  179. for company in sorted(seq):
  180. # print(company)
  181. if m_month:
  182. if m_month >= self.get_last_month(company[6]):
  183. current_month = self.get_last_month(company[6])
  184. else:
  185. current_month = m_month
  186. else:
  187. current_month = self.get_last_month(company[6])
  188. sal_list = self.make_sal_list(company[6] + './NPERS.DB', company[6] + '/' + current_month + './LIKISAL.DB')
  189. print(sal_list)
  190. for employee in sal_list:
  191. company.extend(employee)
  192. company.append(current_month)
  193. sheet_[0].write_row(self.row, self.col, company,)
  194. self.row += 1
  195. del company[11:]
  196. sheet_[0].write_formula(1, 0, '=SUMPRODUCT(1/COUNTIF(A5:A1000,A5:A1000 & ""))-1')
  197. sheet_[0].set_column('G:K', None, None, {'hidden': True})
  198. sheet_[2].close()
  199. # mentor = WinMentor()
  200. # utils = Mentorutils()
  201. # utils.gen_turnover('turn_over', 'to', mentor.verif_corp('/firme.db'), m_month='2018_06', n_headers=['Denumire', 'CF', 'J', 'Adresa', 'Oras', 'Judet',
  202. # 'Prescurtat', 'Obs', 'TurnO', 'Result', 'month'])
  203. # utils.gen_tax_payable('taxes', 'tx', mentor.verif_corp('/firme.db'), m_month='2018_06', n_headers=['Denumire', 'CF', 'J', 'Adresa', 'Oras', 'Judet',
  204. # 'Prescurtat', 'Obs', 'Vat', 'Ins' , 'Tax', 'Sal_tax', 'month'])
  205. # utils.gen_txt('/firme.db', 'lista_cf.txt', 1)