mentorutils.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. '''
  2. Created on Jul 12, 2018 @author: levente.marton
  3. '''
  4. # from . import WinMentor
  5. import xlsxwriter as xlw
  6. from xlsxwriter.worksheet import Worksheet as sheet_
  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.CAS_payable(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  104. company.append(self.CASS_payable(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  105. company.append(self.sal_tax_payable(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  106. company.append(self.cam_payable(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  107. company.append(self.an_inc(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB'), 0, 6, 8))
  108. company.append(self.result(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB'), 0, 6, 8))
  109. company.append(current_month)
  110. sheet_[0].write_row(self.row, self.col, company,)
  111. self.row += 1
  112. print('Processing:', company[0], '>> taxes')
  113. # seq2 = sorted(seq[:11])
  114. self.row = 3
  115. sheet_[1].set_column('D:J', None, None, {'hidden': True})
  116. for company in sorted(seq):
  117. if m_month:
  118. if m_month >= self.get_last_month(company[6]):
  119. current_month = self.get_last_month(company[6])
  120. else:
  121. current_month = m_month
  122. else:
  123. current_month = self.get_last_month(company[6])
  124. company = company[:11]
  125. # company.append(self.an_inc(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB'), 0, 6, 8))
  126. # company.append(self.result(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB'), 0, 6, 8))
  127. company.append(self.divid(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  128. company.append(self.divid_current(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  129. company.append(self.divid_intermed(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  130. company.append(self.divid_inter_current(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  131. company.append(self.spons(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  132. company.append(self.advance_final(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  133. company.append(self.deb_div(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  134. company.append(self.credit_final(self.verif_cont(company[6] + '/' + current_month + './NCONT.DB')))
  135. company.append(current_month)
  136. sheet_[1].write_row(self.row, self.col, company,)
  137. self.row += 1
  138. print('Processing:', company[0], ' >> indicators')
  139. # print('_done_')
  140. else:
  141. for company in sorted(seq):
  142. sheet_[0].write_row(self.row, self.col, company,)
  143. self.row += 1
  144. print('Processing:', company[0])
  145. print('_done_')
  146. sheet_[2].close()
  147. def gen_society(self, xl_name, w_sheet_name, seq, society, n_headers=[], range_year=None, range_month=None):
  148. sheet_ = self._preformat_wb(xl_name, w_sheet_name, n_headers)
  149. sheet_[0].set_column('L:U', 11, cell_format=sheet_[2])
  150. sheet_[0].set_column('A:A', 20)
  151. sheet_[0].set_column('B:B', 11)
  152. sheet_[0].set_column('D:J', None, None, {'hidden': True})
  153. for year_ in range(int(range_year[:4]), int(range_year[-4:]) + 1):
  154. for month_ in range(int(range_month[:2]), int(range_month[-2:]) + 1):
  155. for company in sorted(seq):
  156. # print(company)
  157. if company[6] == society:
  158. company = company[:11]
  159. company.append(self.vat_payable(self.verif_cont(company[6] + '/' + '{}_{:02d}'.format(year_, month_) + './NCONT.DB')))
  160. company.append(self.vat_final(self.verif_cont(company[6] + '/' + '{}_{:02d}'.format(year_, month_) + './NCONT.DB')))
  161. company.append(self.tax_payable(self.verif_cont(company[6] + '/' + '{}_{:02d}'.format(year_, month_) + './NCONT.DB')))
  162. company.append(self.div_tax_payable(self.verif_cont(company[6] + '/' + '{}_{:02d}'.format(year_, month_) + './NCONT.DB')))
  163. company.append(self.CAS_payable(self.verif_cont(company[6] + '/' + '{}_{:02d}'.format(year_, month_) + './NCONT.DB')))
  164. company.append(self.CASS_payable(self.verif_cont(company[6] + '/' + '{}_{:02d}'.format(year_, month_) + './NCONT.DB')))
  165. company.append(self.sal_tax_payable(self.verif_cont(company[6] + '/' + '{}_{:02d}'.format(year_, month_) + './NCONT.DB')))
  166. company.append(self.cam_payable(self.verif_cont(company[6] + '/' + '{}_{:02d}'.format(year_, month_) + './NCONT.DB')))
  167. company.append(self.an_inc(self.verif_cont(company[6] + '/' + '{}_{:02d}'.format(year_, month_) + './NCONT.DB'), 0, 6, 8))
  168. company.append(self.result(self.verif_cont(company[6] + '/' + '{}_{:02d}'.format(year_, month_) + './NCONT.DB'), 0, 6, 8))
  169. company.append(year_)
  170. company.append(month_)
  171. sheet_[0].write_row(self.row, self.col, company)
  172. self.row += 1
  173. print('Processing:', company[0], '{}_{}'.format(year_, month_))
  174. sheet_[1].close()
  175. def gen_sal(self, xl_name, w_sheet_name, seq, m_month=None, n_headers=[]):
  176. sheet_ = self._preformat_wb(xl_name, w_sheet_name, n_headers)
  177. for company in sorted(seq):
  178. # print(company)
  179. if m_month:
  180. if m_month >= self.get_last_month(company[6]):
  181. current_month = self.get_last_month(company[6])
  182. else:
  183. current_month = m_month
  184. else:
  185. current_month = self.get_last_month(company[6])
  186. sal_list = self.make_sal_list(company[6] + './NPERS.DB', company[6] + '/' + current_month + './LIKISAL.DB')
  187. print(sal_list)
  188. for employee in sal_list:
  189. company.extend(employee)
  190. company.append(current_month)
  191. sheet_[0].write_row(self.row, self.col, company,)
  192. self.row += 1
  193. del company[11:]
  194. sheet_[0].write_formula(1, 0, '=SUMPRODUCT(1/COUNTIF(A5:A1000,A5:A1000 & ""))-1')
  195. sheet_[0].set_column('G:K', None, None, {'hidden': True})
  196. sheet_[2].close()
  197. # mentor = WinMentor()
  198. # utils = Mentorutils()
  199. # utils.gen_turnover('turn_over', 'to', mentor.verif_corp('/firme.db'), m_month='2018_06', n_headers=['Denumire', 'CF', 'J', 'Adresa', 'Oras', 'Judet',
  200. # 'Prescurtat', 'Obs', 'TurnO', 'Result', 'month'])
  201. # utils.gen_tax_payable('taxes', 'tx', mentor.verif_corp('/firme.db'), m_month='2018_06', n_headers=['Denumire', 'CF', 'J', 'Adresa', 'Oras', 'Judet',
  202. # 'Prescurtat', 'Obs', 'Vat', 'Ins' , 'Tax', 'Sal_tax', 'month'])
  203. # utils.gen_txt('/firme.db', 'lista_cf.txt', 1)