dbread.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. '''
  2. Created on Jun 21, 2018 @author: levente.marton
  3. '''
  4. import os
  5. import re
  6. import datetime
  7. from dataclasses import dataclass
  8. from collections.abc import Generator
  9. import shutil
  10. import pandas as pd
  11. from pypxlib import Table # type: ignore
  12. import beautiful_date as bd # type: ignore
  13. from .db_to_df import Dbtodf
  14. @dataclass
  15. class Company:
  16. name: str
  17. vat_code: str
  18. reg_number: str
  19. address: str
  20. location: str
  21. county: str
  22. shortname: str
  23. obs: str
  24. adm: str
  25. admp: str
  26. admcnp: str
  27. mail: str = ''
  28. price: int = 0
  29. @dataclass
  30. class Account:
  31. clasa: str
  32. simbol: str
  33. denumire: str
  34. soldid: int = 0
  35. soldic: int = 0
  36. precedentd: int = 0
  37. precedentc: int = 0
  38. curentd: int = 0
  39. curentc: int = 0
  40. def soldf(self, type_):
  41. if type_ == 'd':
  42. return round((self.soldid + self.precedentd + self.curentd) -
  43. (self.soldic + self.precedentc + self.curentc))
  44. elif type_ == 'c':
  45. return round((self.soldic + self.precedentc + self.curentc) -
  46. (self.soldid + self.precedentd + self.curentd))
  47. class WinMentor(object):
  48. '''
  49. Class to read winmentor database
  50. '''
  51. def __init__(self, winment_path=os.getenv('WINMENT', 'c:/winment/data/').replace('\\', '/')) -> None:
  52. '''
  53. Constructor
  54. '''
  55. self.winment_path = winment_path
  56. @property
  57. def get_winment_path(self):
  58. return self.winment_path
  59. @get_winment_path.setter # type: ignore
  60. def set_winment_path(self, value):
  61. # self.value = value
  62. self.winment_path = value
  63. return self.winment_path
  64. def update_copy(self, db_file) -> str:
  65. '''returns the updated version of a .db file
  66. :param db_file is a .db from winmentor'''
  67. # self._to_file = db_file
  68. _, file_name = os.path.split(db_file)
  69. dir_list = db_file.split('/')
  70. # !!NOTE: if path is ?/winment/data/ then this must be 3 & 5
  71. # if is ?/winment/winment/data/ then 4 & 6
  72. dir_to_file = dir_list[4]
  73. if len(dir_list) != 6:
  74. os.makedirs(os.path.join('cash', dir_to_file), exist_ok=True)
  75. cashed_copy_mtime = -1
  76. if os.path.isfile(os.path.join('./cash/', dir_to_file, file_name)):
  77. cashed_copy_mtime = os.stat(os.path.join('./cash/', dir_to_file, file_name)).st_mtime # type: ignore
  78. if os.stat(db_file).st_mtime != cashed_copy_mtime:
  79. shutil.copy2(db_file, os.path.join('./cash', dir_to_file))
  80. return(os.path.join('./cash/', dir_to_file, file_name))
  81. else:
  82. os.makedirs('cash', exist_ok=True)
  83. cashed_copy_mtime = -1
  84. if os.path.isfile('./cash/' + file_name):
  85. cashed_copy_mtime = os.stat('./cash/' + file_name).st_mtime # type: ignore
  86. if os.stat(db_file).st_mtime != cashed_copy_mtime:
  87. shutil.copy2(db_file, './cash')
  88. return('./cash/' + file_name)
  89. def make_list(self, db_file, headers) -> list:
  90. '''returns a list of lists with elements from a given db file
  91. :param db_file is .db file from winmentor
  92. :param headers are the fields of the specified db file'''
  93. file_name = self.update_copy(db_file)
  94. with Table(file_name) as table:
  95. m_list = []
  96. for row in table:
  97. item = []
  98. for header in headers:
  99. element = row[header]
  100. if type(element) == datetime.date:
  101. if element < datetime.date(1999, 1, 1):
  102. element = ''
  103. item.append(element)
  104. m_list.append(item)
  105. return m_list
  106. def make_sal_list(self, file_1, file_2) -> list:
  107. '''returns list with all_ employees in current month
  108. :param file_1 is npers.db from shortname
  109. :param file_2 is likisal.db shortname/current_month'''
  110. file_name1 = self.update_copy(self.winment_path + file_1)
  111. file_name2 = self.update_copy(self.winment_path + file_2)
  112. with Table(file_name1) as perss, Table(file_name2) as sals:
  113. empl_all = []
  114. # for field in perss.fields:
  115. for sal in sals:
  116. for pers in perss:
  117. # print(pers)
  118. if pers.Cod == sal.Cod:
  119. empl = []
  120. empl.append(pers.Cod)
  121. empl.append(f'{pers.Nume} {pers.Prenume}')
  122. empl.append(pers.DataAngF.strftime('%d-%m-%Y'))
  123. empl.append(sal.VenitBrut)
  124. empl.append(sal.SalRealizat)
  125. empl.append(sal.CO)
  126. empl.append(round(sal.SalOra, 2))
  127. empl.append(sal.ContribAngajat)
  128. empl.append(round(sal.ContribAngajator, 2))
  129. empl.append(sal.VenitNet)
  130. empl.append(sal.Impozit)
  131. empl.append(sal.SalarNet)
  132. empl.append(sal.OreLucrate)
  133. empl.append(sal.ZileLuk)
  134. empl.append(sal.ZileCO)
  135. empl_all.append(empl)
  136. return empl_all
  137. def gen_firms(self, db_file, headers) -> Generator:
  138. '''generates company list from firme.db file
  139. :param db_file is firme.db from winment/data folder'''
  140. file_db = self.update_copy(self.winment_path + db_file)
  141. with Table(file_db) as table:
  142. for row in table:
  143. yield [row[header] for header in headers]
  144. def firmlist(self, headers, db_file='/firme.db', ban=None) -> list:
  145. '''returns company list from firme.db file
  146. :param db_file is firme.db from winment/data folder
  147. :param list headers is the fields from firme.db file
  148. :param list|str ban companies excluded from list'''
  149. comp_list = []
  150. for a_company in self.gen_firms(db_file, headers):
  151. company = Company(
  152. name=a_company[0],
  153. vat_code=a_company[1],
  154. reg_number=a_company[2],
  155. address=a_company[3],
  156. location=a_company[4],
  157. county=a_company[5],
  158. shortname=a_company[6],
  159. adm=a_company[7],
  160. admp=a_company[8],
  161. admcnp=a_company[9],
  162. obs=a_company[10])
  163. comp_list.append(company)
  164. if ban:
  165. if type(ban) is list:
  166. for r in comp_list:
  167. for company_shortname in ban:
  168. if company_shortname == r.shortname:
  169. comp_list.remove(r)
  170. else:
  171. for r in comp_list:
  172. if r == ban:
  173. comp_list.remove(r)
  174. return comp_list
  175. def filtered_firmlist(self, headers, db_file='/firme.db', ban=None) -> Generator:
  176. '''generates company list from firme.db file
  177. :param db_file is firme.db from winment/data folder
  178. :param list headers is the fields from firme.db file
  179. :param list|str ban companies excluded from list'''
  180. headers = headers or ['Denumire', 'CF', 'J', 'Adresa', 'Oras', 'Judet', 'Prescurtat', 'Admin', 'AdminP', 'RCNP', 'Obs']
  181. if ban:
  182. if type(ban) is list:
  183. for a_company in self.gen_firms(db_file, headers):
  184. company = Company(
  185. name=a_company[0],
  186. vat_code=a_company[1],
  187. reg_number=a_company[2],
  188. address=a_company[3],
  189. location=a_company[4],
  190. county=a_company[5],
  191. shortname=a_company[6],
  192. adm=a_company[7],
  193. admp=a_company[8],
  194. admcnp=a_company[9],
  195. obs=a_company[10])
  196. if a_company[6] not in ban:
  197. yield [company.name, company.vat_code, company.reg_number,
  198. company.address, company.location, company.county,
  199. company.shortname, company.adm, company.admp,
  200. company.admcnp, company.obs]
  201. else:
  202. for a_company in self.gen_firms(db_file, headers):
  203. company = Company(
  204. name=a_company[0],
  205. vat_code=a_company[1],
  206. reg_number=a_company[2],
  207. address=a_company[3],
  208. location=a_company[4],
  209. county=a_company[5],
  210. shortname=a_company[6],
  211. adm=a_company[7],
  212. admp=a_company[8],
  213. admcnp=a_company[9],
  214. obs=a_company[10])
  215. yield [company.name, company.vat_code, company.reg_number,
  216. company.address, company.location, company.county,
  217. company.shortname, company.adm, company.admp,
  218. company.admcnp, company.obs]
  219. def get_last_month(self, short_name) -> str:
  220. '''returns last month of a company in format YYYY_MM
  221. :param str short_name is the company shortname'''
  222. short_name = self.winment_path + short_name
  223. month_folders = [f for f in os.listdir(short_name) if re.match(r'[0-9_]+$', f)]
  224. month_folders.reverse()
  225. return month_folders[0]
  226. def get_bank_accounts(self, short_name, db_file='/nbanca.db'):
  227. # .......................................................................
  228. # TO DO: put an all_ parameter to yield all_ accounts or
  229. # just one,
  230. # make a named tuple with bank accounts.
  231. # .......................................................................
  232. headers = ['Codbanca', 'Denumire', 'NrCont']
  233. headers2 = ['COD', 'DENUMIRE']
  234. short_name = self.winment_path + short_name
  235. nbanks = self.update_copy(short_name + '/nbanci.db')
  236. file_db = self.update_copy(short_name + db_file)
  237. bank_codes = {}
  238. with Table(file_db) as table, Table(nbanks) as nbanks:
  239. # tables = zip(table, nbanks)
  240. for bank in nbanks:
  241. data = [bank[header] for header in headers2]
  242. bdict = {data[i]: data[i + 1] for i in range(0, len(data), 2)}
  243. bank_codes.update(bdict)
  244. for row in table:
  245. if row.NrCont:
  246. bank_account = [row[header] for header in headers]
  247. bank_account.append(bank_codes[bank_account[0]])
  248. if bank_account[2].startswith(' '):
  249. yield bank_account
  250. def save_oblig(self, short_name):
  251. df_dicts = self._get_oblig(short_name)
  252. # create dfs from dicts
  253. df_parts = pd.DataFrame(df_dicts[1])
  254. df_conts = pd.DataFrame(df_dicts[2])
  255. df_oblig = pd.DataFrame(df_dicts[0])
  256. df_obligf = pd.DataFrame(df_dicts[3])
  257. df_mons = pd.DataFrame(df_dicts[4])
  258. df_obligf = df_obligf.rename(columns={'TipTranz': 'TipDoc'})
  259. # join oblig<>part<>cont with left join
  260. df_oblig = pd.merge(df_oblig, df_parts, how='left', left_on='Part', right_on='Cod')
  261. df_oblig.drop(columns=['Part', 'Cod'], inplace=True)
  262. df_oblig = pd.merge(df_oblig, df_conts, how='left', left_on='Cont', right_on='Cod')
  263. df_oblig.drop(columns=['Cont', 'Cod'], inplace=True)
  264. df_oblig = pd.merge(df_oblig, df_mons, how='left', left_on='Moneda', right_on='Cod')
  265. df_oblig.drop(columns=['Moneda', 'Cod'], inplace=True)
  266. df_oblig.drop(columns=['TipDoc'], inplace=True)
  267. # join obligf<>party<>cont
  268. try:
  269. df_obligf = pd.merge(df_obligf, df_parts, how='left', left_on='Part', right_on='Cod')
  270. df_obligf.drop(columns=['Part', 'Cod'], inplace=True)
  271. df_obligf = pd.merge(df_obligf, df_conts, how='left', left_on='Cont', right_on='Cod')
  272. df_obligf.drop(columns=['Cont', 'Cod'], inplace=True)
  273. df_obligf = pd.merge(df_obligf, df_mons, how='left', left_on='Moneda', right_on='Cod')
  274. df_obligf.drop(columns=['Moneda', 'Cod'], inplace=True)
  275. df_obligf.drop(columns=['TipDoc'], inplace=True)
  276. df_obligall = pd.concat([df_oblig, df_obligf])
  277. except KeyError:
  278. df_obligall = df_oblig
  279. options = ['581', '455', '455.01', '167', '666']
  280. suppliers = ['401', '404', '409', '409.01', '409.02', '409.03', '409.04']
  281. clients = ['411', '411.01', '419', '419.01', '419.02', '472', '472.01', '472.02', '472.03']
  282. df_obligall['TipPartener'] = df_obligall.apply(lambda row: self._part_type(row), axis=1)
  283. df_obligall.loc[df_obligall['Simbol_y'] == 'lei', 'Curs'] = 1
  284. df_obligall['RestRon'] = df_obligall['Rest'] * df_obligall['Curs']
  285. mask = (~df_obligall['Simbol_x'].isin(options)) & (df_obligall['Rest'] != 0)
  286. df_obligsp = df_obligall.loc[mask & (df_obligall['Simbol_x'].isin(suppliers))]
  287. df_obligcl = df_obligall.loc[mask & (df_obligall['Simbol_x'].isin(clients))]
  288. # print(df_obligall.head(10))
  289. # prepare sheet
  290. date = (bd.D.today() - 1 * bd.months).strftime('%m_%Y')
  291. writer = pd.ExcelWriter(
  292. '{}_{}_PART.xlsx'.format(self._oblig_sheet_name, date),
  293. engine='xlsxwriter')
  294. # for subtotal research
  295. # .......................................................................
  296. # container = []
  297. # for label, _df in df_obligsp.groupby('Denumire'):
  298. # _df.loc[f'{label} Subtotal'] = _df[['Rest', 'RestRon']].sum()
  299. # container.append(_df)
  300. # df_summery = pd.concat(container)
  301. # df_summery.fillna('', inplace=True)
  302. # print(df_summery)
  303. # .......................................................................
  304. df_obligsp.to_excel(writer, sheet_name='Furnizori', index=False)
  305. # df_summery.to_excel(writer, sheet_name='Furnizori', index=True)
  306. df_obligcl.to_excel(writer, sheet_name='Clienti', index=False)
  307. workbook = writer.book
  308. num_format = workbook.add_format()
  309. num_format.set_num_format('#,##0.00')
  310. b_format = workbook.add_format()
  311. b_format.set_bottom()
  312. sh = workbook.get_worksheet_by_name('Furnizori')
  313. sh.freeze_panes(1, 0)
  314. sh.autofilter('A1:N500')
  315. sh.set_column('E:F', 12, cell_format=num_format)
  316. sh.set_column('L:L', 12, cell_format=num_format)
  317. sh.set_column('D:D', 10)
  318. sh.set_column('G:G', 30)
  319. sh.conditional_format('A2:L500', {'type': 'formula', 'criteria': '=NOT($G3=$G2)', 'format': b_format})
  320. sh2 = workbook.get_worksheet_by_name('Clienti')
  321. sh2.freeze_panes(1, 0)
  322. sh2.autofilter('A1:N500')
  323. sh2.set_column('E:F', 12, cell_format=num_format)
  324. sh2.set_column('L:L', 12, cell_format=num_format)
  325. sh2.set_column('D:D', 10)
  326. sh2.set_column('G:G', 30)
  327. sh2.conditional_format('A2:L500', {'type': 'formula', 'criteria': '=NOT($G3=$G2)', 'format': b_format})
  328. writer._save()
  329. def _part_type(self, row):
  330. suppliers = ['401', '403', '404', '408']
  331. adv_suppliers = ['409', '409.01', '409.02', '409.03', '409.04']
  332. clients = ['411', '411.01', '418']
  333. adv_clients = ['419', '419.01', '419.02', '472', '472.01', '472.02', '472.04']
  334. if row.loc['Simbol_x'] in suppliers:
  335. return 'Furnizor'
  336. elif row.loc['Simbol_x'] in adv_suppliers:
  337. return 'avans furnizor'
  338. elif row.loc['Simbol_x'] in clients:
  339. return 'Client'
  340. elif row.loc['Simbol_x'] in adv_clients:
  341. return 'Avans client'
  342. def _get_parts(self, short_name):
  343. firm_list = self.filtered_firmlist(None)
  344. nparts_headres = ['Cod', 'Denumire', 'CodFiscal']
  345. for firm in firm_list:
  346. if firm[6] == short_name:
  347. short_path = self.winment_path + short_name
  348. # update files
  349. nparts_db = self.update_copy(short_path + '/NPART.DB')
  350. # convert dbs to dfs
  351. dbtodf_parts = Dbtodf(nparts_db, *nparts_headres)
  352. # actual converting
  353. parts_dict = dbtodf_parts.convert_parts()
  354. # self._oblig_sheet_name = firm[0]
  355. return parts_dict
  356. def _get_oblig(self, short_name, db_file='/ObligPI.DB'):
  357. firm_list = self.filtered_firmlist(None)
  358. oblig_headers = ['Part', 'TipDoc', 'Cont', 'Doc', 'Moneda', 'Curs', 'NrDoc', 'DataDoc', 'Valoare', 'Rest']
  359. obligf_headers = ['Part', 'TipTranz', 'Cont', 'Doc', 'Moneda', 'Curs', 'NrDoc', 'DataDoc', 'Valoare', 'Rest']
  360. nparts_headres = ['Cod', 'Denumire', 'CodFiscal']
  361. cont_headers = ['Cod', 'Simbol']
  362. mon_headers = ['Cod', 'Simbol']
  363. for firm in firm_list:
  364. if firm[6] == short_name:
  365. short_path = self.winment_path + short_name
  366. # update files
  367. nparts_db = self.update_copy(short_path + '/NPART.DB')
  368. mons_db = self.update_copy(short_path + '/NMONEDE.DB')
  369. oblig_db = self.update_copy(short_path + '/' + self.get_last_month(short_name) + db_file)
  370. obligf_db = self.update_copy(short_path + '/' + self.get_last_month(short_name) + '/ObligF.DB')
  371. conts_db = self.update_copy(short_path + '/' + self.get_last_month(short_name) + '/NCONT.DB')
  372. # convert dbs to dfs
  373. dbtodf_oblig = Dbtodf(oblig_db, *oblig_headers)
  374. dbtodf_parts = Dbtodf(nparts_db, *nparts_headres)
  375. dbtodf_conts = Dbtodf(conts_db, *cont_headers)
  376. dbtodf_obligf = Dbtodf(obligf_db, *obligf_headers)
  377. dbtodf_mons = Dbtodf(mons_db, *mon_headers)
  378. # actual converting
  379. oblig_dict = dbtodf_oblig.convert_oblig()
  380. obligf_dict = dbtodf_obligf.convert_obligf()
  381. parts_dict = dbtodf_parts.convert_parts()
  382. conts_dict = dbtodf_conts.convert_cont()
  383. mons_dict = dbtodf_mons.convert_mon()
  384. self._oblig_sheet_name = firm[0]
  385. return (oblig_dict, parts_dict, conts_dict, obligf_dict, mons_dict)
  386. def corp_list(self, name=None):
  387. '''returns company list from actual shortnames from winmwnt/data folder
  388. '''
  389. dir_list = [f.name for f in os.scandir(self.winment_path) if f.is_dir() and '@' not in f.name]
  390. if name:
  391. if type(name) is list:
  392. for r in name:
  393. dir_list.remove(r)
  394. else:
  395. dir_list.remove(name)
  396. return dir_list
  397. def verif_corp(self, file_='/firme.db', ban=None):
  398. headers = ['Denumire', 'CF', 'J', 'Adresa', 'Oras', 'Judet', 'Prescurtat', 'Obs']
  399. corplist = self.make_list(self.winment_path + file_, headers) # dbRead.make_list(m_path + '/FIRME.DB', headers)
  400. if ban:
  401. if type(ban) is list:
  402. for r in corplist:
  403. for i in ban:
  404. if i == r[6]:
  405. corplist.remove(r)
  406. else:
  407. for r in corplist:
  408. if r == ban:
  409. corplist.remove(r)
  410. return corplist
  411. def verif_cont(self, file_) -> list:
  412. '''returns an account with its values from the balance
  413. :param file_ is shortname/ncont.db'''
  414. accounts = []
  415. headers = ['Clasa', 'Simbol', 'Denumire', 'SoldID', 'SoldIC', 'PrecedentD', 'PrecedentC', 'CurentD', 'CurentC']
  416. accountlist = self.make_list(self.winment_path + file_, headers) # + lunaCurenta
  417. for elem in accountlist:
  418. account = Account(clasa=elem[0],
  419. simbol=elem[1],
  420. denumire=elem[2],
  421. soldid=elem[3],
  422. soldic=elem[4],
  423. precedentd=elem[5],
  424. precedentc=elem[6],
  425. curentd=elem[7],
  426. curentc=elem[8])
  427. accounts.append(account)
  428. return accountlist
  429. def an_inc(self, account_list, boolind, ind2, ind3):
  430. '''returns the annual turnover in given year
  431. :param int boolind:account class number
  432. :param int ind2, ind3:debit or credit position in balance
  433. (6-rulaj curent debit, 8-rulaj cumulat debit, index starting from 0)'''
  434. tt = 0
  435. for account in account_list:
  436. # n = len(account[boolind]) == 3 and account[boolind] != '...' and int(account[boolind]) > 700 and int(account[boolind]) < 760
  437. n = account[boolind] == 7
  438. can = account[1][:2] != '76'
  439. # print(account[1][:2])
  440. if n and can:
  441. tt += int(account[ind2]) + int(account[ind3])
  442. return tt
  443. def advance_revenue(self, account_list, account='419') -> int:
  444. '''returns billed advance revenue
  445. :param account_list is account from ncont.db'''
  446. div_ = 0
  447. for acc in account_list:
  448. if acc[1][:3] == account:
  449. div_ += acc[8] + acc[6]
  450. # div_ -= acc[7] + acc[5]
  451. return round(div_)
  452. def divid(self, account_list, account='457') -> int:
  453. '''returns dividends/year
  454. :param account_list is account from ncont.db'''
  455. div_ = 0
  456. for acc in account_list:
  457. if acc[1][:3] == account:
  458. div_ += acc[8] + acc[6]
  459. return round(div_)
  460. def divid_current(self, account_list, account='457') -> int:
  461. '''returns dividends from current month
  462. :param account_list is account from ncont.db'''
  463. div_ = 0
  464. for acc in account_list:
  465. if acc[1][:3] == account:
  466. div_ += acc[8]
  467. return round(div_)
  468. def divid_intermed(self, account_list, account='463') -> int:
  469. '''returns dividends from current result/year
  470. :param account_list is account from ncont.db'''
  471. div_ = 0
  472. for acc in account_list:
  473. if acc[1][:3] == account:
  474. div_ += acc[7] + acc[5]
  475. return round(div_)
  476. def divid_inter_current(self, account_list, account='463') -> int:
  477. '''returns dividends from current results in the current month
  478. :param account_list is account from ncont.db'''
  479. div_ = 0
  480. for acc in account_list:
  481. if acc[1][:3] == account:
  482. div_ += acc[7]
  483. return round(div_)
  484. def spons(self, account_list, account='658.02') -> int:
  485. '''returns sponsored money/year
  486. :param account_list is account from ncont.db'''
  487. spons_ = 0
  488. for acc in account_list:
  489. if acc[1] == account:
  490. spons_ += acc[8] + acc[6]
  491. return round(spons_)
  492. def result(self, account_list, boolind, ind2, ind3) -> int: # account='121'
  493. '''returns the final result in given year
  494. :param int boolind:account class number
  495. :param int ind2, ind3:debit or credit position in balance
  496. (6-rulaj curent debit, 8-rulaj cumulat debit, index starting from 0)'''
  497. res_minus = res_plus = 0
  498. for r_minus in account_list:
  499. p = r_minus[boolind] == 6
  500. if p:
  501. res_minus += int(r_minus[ind2]) + int(r_minus[ind3])
  502. for r_plus in account_list:
  503. i = r_plus[boolind] == 7
  504. if i:
  505. res_plus += int(r_plus[ind2]) + int(r_plus[ind3])
  506. return res_plus - res_minus
  507. def ins_payable(self, account_list, account='431') -> int:
  508. '''returns insurences payable in current month
  509. :param account_list is account from ncont.db'''
  510. ins = 0
  511. for acc in account_list:
  512. p = acc[1][:3] == account
  513. if p:
  514. ins += acc[8]
  515. return round(ins)
  516. def CAS_payable(self, account_list, accounts=('431.02', '431.05')) -> int:
  517. '''returns CAS payable in current month
  518. :param account_list is account from ncont.db'''
  519. CAS = 0
  520. acc_1, acc_2 = accounts
  521. for acc in account_list:
  522. p = acc[1] == acc_1
  523. d = acc[1] == acc_2
  524. if p:
  525. CAS += acc[8]
  526. if d:
  527. CAS += acc[8]
  528. return round(CAS)
  529. def CASS_payable(self, account_list, accounts=('431.04', '431.06')) -> int:
  530. '''returns CASS payable in current month
  531. :param account_list is account from ncont.db'''
  532. CASS = 0
  533. acc_1, acc_2 = accounts
  534. for acc in account_list:
  535. p = acc[1] == acc_1
  536. d = acc[1] == acc_2
  537. if p:
  538. CASS += acc[8]
  539. if d:
  540. CASS += acc[8]
  541. return round(CASS)
  542. def sal_tax_payable(self, account_list, account='431.44') -> int:
  543. '''returns salary tax payable in current month
  544. :param account_list is account from ncont.db'''
  545. tax = 0
  546. for acc in account_list:
  547. p = acc[1] == account
  548. if p:
  549. tax += acc[8]
  550. return round(tax)
  551. def cam_payable(self, account_list, account='436') -> int:
  552. '''returns CAM payable in current month
  553. :param account_list is account from ncont.db'''
  554. cam = 0
  555. for acc in account_list:
  556. p = acc[1][:3] == account
  557. if p:
  558. cam += acc[8]
  559. return round(cam)
  560. def vat_payable(self, account_list, accounts=('442.03', '442.04')) -> int:
  561. '''returns VAT payable in current month
  562. :param account_list is account from ncont.db'''
  563. tt = 0
  564. acc_1, acc_2 = accounts
  565. for acc in account_list:
  566. p = acc[1] == acc_1
  567. d = acc[1] == acc_2
  568. if p:
  569. tt += acc[8]
  570. elif d:
  571. tt -= acc[7] + acc[8]
  572. return round(tt)
  573. def vat_final(self, acc_list, accounts=('442.03', '442.04')) -> int:
  574. '''returns VAT final payable in current month
  575. :param account_list is account from ncont.db'''
  576. tt = 0
  577. acc_1, acc_2 = accounts
  578. for acc in acc_list:
  579. account = Account(clasa=acc[0],
  580. simbol=acc[1],
  581. denumire=acc[2],
  582. soldid=acc[3],
  583. soldic=acc[4],
  584. precedentd=acc[5],
  585. precedentc=acc[6],
  586. curentd=acc[7],
  587. curentc=acc[8])
  588. payable = account.simbol == acc_1
  589. deductible = account.simbol == acc_2
  590. if payable:
  591. tt += account.soldf('c')
  592. elif deductible:
  593. tt -= account.soldf('d')
  594. return round(tt)
  595. def tax_payable(self, account_list, account='441') -> int:
  596. '''returns income TAX payable in current month
  597. :param account_list is account from ncont.db'''
  598. tax = 0
  599. for acc in account_list:
  600. p = acc[1][:3] == account
  601. if p:
  602. tax += acc[8]
  603. return round(tax)
  604. def div_tax_payable(self, acc_list, accounts='446.07') -> int:
  605. '''returns dividend TAX payable in current month
  606. :param account_list is account from ncont.db'''
  607. tt = 0
  608. # acc_1, acc_2 = accounts
  609. for acc in acc_list:
  610. account = Account(clasa=acc[0],
  611. simbol=acc[1],
  612. denumire=acc[2],
  613. soldid=acc[3],
  614. soldic=acc[4],
  615. precedentd=acc[5],
  616. precedentc=acc[6],
  617. curentd=acc[7],
  618. curentc=acc[8])
  619. payable = account.simbol == accounts
  620. # deductible = account.simbol == acc_2
  621. if payable:
  622. tt += account.curentc
  623. # elif deductible:
  624. # tt -= account.soldf('d')
  625. return round(tt)
  626. def other_tax_payable(self, acc_list, accounts='446.08') -> int:
  627. '''returns dividend TAX payable in current month
  628. :param account_list is account from ncont.db'''
  629. tt = 0
  630. # acc_1, acc_2 = accounts
  631. for acc in acc_list:
  632. account = Account(clasa=acc[0],
  633. simbol=acc[1],
  634. denumire=acc[2],
  635. soldid=acc[3],
  636. soldic=acc[4],
  637. precedentd=acc[5],
  638. precedentc=acc[6],
  639. curentd=acc[7],
  640. curentc=acc[8])
  641. # payable = account.simbol == accounts
  642. payable = accounts in account.simbol
  643. # deductible = account.simbol == acc_2
  644. if payable:
  645. tt += account.curentc
  646. # elif deductible:
  647. # tt -= account.soldf('d')
  648. return round(tt)
  649. def cash_balance(self, acc_list, accounts='531.01') -> int:
  650. '''returns cash balance
  651. :param account_list is account from ncont.db'''
  652. tt = 0
  653. # acc_1, acc_2 = accounts
  654. for acc in acc_list:
  655. account = Account(clasa=acc[0],
  656. simbol=acc[1],
  657. denumire=acc[2],
  658. soldid=acc[3],
  659. soldic=acc[4],
  660. precedentd=acc[5],
  661. precedentc=acc[6],
  662. curentd=acc[7],
  663. curentc=acc[8])
  664. payable = accounts in account.simbol
  665. # deductible = account.simbol == acc_2
  666. if payable:
  667. tt += account.soldf('d')
  668. # elif deductible:
  669. # tt -= account.soldf('d')
  670. return round(tt)
  671. def advance_final(self, acc_list, accounts='542') -> int:
  672. '''returns final dvances/year
  673. :param account_list is account from ncont.db'''
  674. tt = 0
  675. # acc_1, acc_2 = accounts
  676. for acc in acc_list:
  677. account = Account(clasa=acc[0],
  678. simbol=acc[1],
  679. denumire=acc[2],
  680. soldid=acc[3],
  681. soldic=acc[4],
  682. precedentd=acc[5],
  683. precedentc=acc[6],
  684. curentd=acc[7],
  685. curentc=acc[8])
  686. payable = account.simbol[:3] == accounts
  687. # deductible = account.simbol == acc_2
  688. if payable:
  689. tt += account.soldf('d')
  690. # elif deductible:
  691. # tt -= account.soldf('d')
  692. return round(tt)
  693. def deb_div(self, acc_list, accounts='461') -> int:
  694. '''returns advances transfered to deb. diversi/year
  695. :param account_list is account from ncont.db'''
  696. tt = 0
  697. # acc_1, acc_2 = accounts
  698. for acc in acc_list:
  699. account = Account(clasa=acc[0],
  700. simbol=acc[1],
  701. denumire=acc[2],
  702. soldid=acc[3],
  703. soldic=acc[4],
  704. precedentd=acc[5],
  705. precedentc=acc[6],
  706. curentd=acc[7],
  707. curentc=acc[8])
  708. payable = account.simbol[:3] == accounts
  709. # deductible = account.simbol == acc_2
  710. if payable:
  711. tt += account.soldf('d')
  712. # elif deductible:
  713. # tt -= account.soldf('d')
  714. return round(tt)
  715. def credit_final(self, acc_list, accounts='455') -> int:
  716. '''returns credited ammount/year
  717. :param account_list is account from ncont.db'''
  718. tt = 0
  719. # acc_1, acc_2 = accounts
  720. for acc in acc_list:
  721. account = Account(clasa=acc[0],
  722. simbol=acc[1],
  723. denumire=acc[2],
  724. soldid=acc[3],
  725. soldic=acc[4],
  726. precedentd=acc[5],
  727. precedentc=acc[6],
  728. curentd=acc[7],
  729. curentc=acc[8])
  730. payable = account.simbol[:3] == accounts
  731. # deductible = account.simbol == acc_2
  732. if payable:
  733. tt += account.soldf('d')
  734. # elif deductible:
  735. # tt -= account.soldf('d')
  736. return round(tt)
  737. if __name__ == '__main__':
  738. mentor = WinMentor()
  739. # accounts = list(mentor.get_bank_accounts('WEBS'))
  740. # account_num = [n for n in accounts[1] if n.startswith(' ')]
  741. for account in mentor.get_bank_accounts('WEBS'):
  742. # if account[2].startswith(' '):
  743. print(account)