dbread.py 30 KB

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