dbread.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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. container = []
  294. for label, _df in df_obligsp.groupby('Denumire'):
  295. _df.loc[f'{label} Subtotal'] = _df[['Rest', 'RestRon']].sum()
  296. container.append(_df)
  297. df_summery = pd.concat(container)
  298. df_summery.fillna('', inplace=True)
  299. print(df_summery)
  300. df_obligsp.to_excel(writer, sheet_name='Furnizori', index=False)
  301. # df_summery.to_excel(writer, sheet_name='Furnizori', index=True)
  302. df_obligcl.to_excel(writer, sheet_name='Clienti', index=False)
  303. workbook = writer.book
  304. num_format = workbook.add_format()
  305. num_format.set_num_format('#,##0.00')
  306. b_format = workbook.add_format()
  307. b_format.set_bottom()
  308. sh = workbook.get_worksheet_by_name('Furnizori')
  309. sh.freeze_panes(1, 0)
  310. sh.autofilter('A1:N500')
  311. sh.set_column('E:F', 12, cell_format=num_format)
  312. sh.set_column('L:L', 12, cell_format=num_format)
  313. sh.set_column('D:D', 10)
  314. sh.set_column('G:G', 30)
  315. sh.conditional_format('A2:L500', {'type':'formula', 'criteria':'=NOT($G3=$G2)', 'format':b_format})
  316. sh2 = workbook.get_worksheet_by_name('Clienti')
  317. sh2.freeze_panes(1, 0)
  318. sh2.autofilter('A1:N500')
  319. sh2.set_column('E:F', 12, cell_format=num_format)
  320. sh2.set_column('L:L', 12, cell_format=num_format)
  321. sh2.set_column('D:D', 10)
  322. sh2.set_column('G:G', 30)
  323. sh2.conditional_format('A2:L500', {'type':'formula', 'criteria':'=NOT($G3=$G2)', 'format':b_format})
  324. writer._save()
  325. def _part_type(self, row):
  326. suppliers = ['401', '403', '404', '408']
  327. adv_suppliers = ['409', '409.01', '409.02', '409.03', '409.04']
  328. clients = ['411', '411.01', '418']
  329. adv_clients = ['419', '419.01', '419.02', '472', '472.01', '472.02', '472.04']
  330. if row.loc['Simbol_x'] in suppliers:
  331. return 'Furnizor'
  332. elif row.loc['Simbol_x'] in adv_suppliers:
  333. return 'avans furnizor'
  334. elif row.loc['Simbol_x'] in clients:
  335. return 'Client'
  336. elif row.loc['Simbol_x'] in adv_clients:
  337. return 'Avans client'
  338. def _get_oblig(self, short_name, db_file='/ObligPI.DB'):
  339. firm_list = self.filtered_firmlist(None)
  340. oblig_headers = ['Part', 'TipDoc', 'Cont', 'Doc', 'Moneda', 'Curs', 'NrDoc', 'DataDoc', 'Valoare', 'Rest']
  341. obligf_headers = ['Part', 'TipTranz', 'Cont', 'Doc', 'Moneda', 'Curs', 'NrDoc', 'DataDoc', 'Valoare', 'Rest']
  342. nparts_headres = ['Cod', 'Denumire', 'CodFiscal']
  343. cont_headers = ['Cod', 'Simbol']
  344. mon_headers = ['Cod', 'Simbol']
  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. mons_db = self.update_copy(short_path + '/NMONEDE.DB')
  351. oblig_db = self.update_copy(short_path + '/' + self.get_last_month(short_name) + db_file)
  352. obligf_db = self.update_copy(short_path + '/' + self.get_last_month(short_name) + '/ObligF.DB')
  353. conts_db = self.update_copy(short_path + '/' + self.get_last_month(short_name) + '/NCONT.DB')
  354. # convert dbs to dfs
  355. dbtodf_oblig = Dbtodf(oblig_db, *oblig_headers)
  356. dbtodf_parts = Dbtodf(nparts_db, *nparts_headres)
  357. dbtodf_conts = Dbtodf(conts_db, *cont_headers)
  358. dbtodf_obligf = Dbtodf(obligf_db, *obligf_headers)
  359. dbtodf_mons = Dbtodf(mons_db, *mon_headers)
  360. # actual converting
  361. oblig_dict = dbtodf_oblig.convert_oblig()
  362. obligf_dict = dbtodf_obligf.convert_obligf()
  363. parts_dict = dbtodf_parts.convert_parts()
  364. conts_dict = dbtodf_conts.convert_cont()
  365. mons_dict = dbtodf_mons.convert_mon()
  366. self._oblig_sheet_name = firm[0]
  367. return (oblig_dict, parts_dict, conts_dict, obligf_dict, mons_dict)
  368. def corp_list(self, name=None):
  369. '''returns company list from actual shortnames from winmwnt/data folder
  370. '''
  371. dir_list = [f.name for f in os.scandir(self.winment_path) if f.is_dir() and '@' not in f.name]
  372. if name:
  373. if type(name) is list:
  374. for r in name:
  375. dir_list.remove(r)
  376. else:
  377. dir_list.remove(name)
  378. return dir_list
  379. def verif_corp(self, file_='/firme.db', ban=None):
  380. headers = ['Denumire', 'CF', 'J', 'Adresa', 'Oras', 'Judet', 'Prescurtat', 'Obs']
  381. corplist = self.make_list(self.winment_path + file_, headers) # dbRead.make_list(m_path + '/FIRME.DB', headers)
  382. if ban:
  383. if type(ban) is list:
  384. for r in corplist:
  385. for i in ban:
  386. if i == r[6]:
  387. corplist.remove(r)
  388. else:
  389. for r in corplist:
  390. if r == ban:
  391. corplist.remove(r)
  392. return corplist
  393. def verif_cont(self, file_) -> list:
  394. '''returns an account with its values from the balance
  395. :param file_ is shortname/ncont.db'''
  396. accounts = []
  397. headers = ['Clasa', 'Simbol', 'Denumire', 'SoldID', 'SoldIC', 'PrecedentD', 'PrecedentC', 'CurentD', 'CurentC']
  398. accountlist = self.make_list(self.winment_path + file_, headers) # + lunaCurenta
  399. for elem in accountlist:
  400. account = Account(clasa=elem[0],
  401. simbol=elem[1],
  402. denumire=elem[2],
  403. soldid=elem[3],
  404. soldic=elem[4],
  405. precedentd=elem[5],
  406. precedentc=elem[6],
  407. curentd=elem[7],
  408. curentc=elem[8])
  409. accounts.append(account)
  410. return accountlist
  411. def an_inc(self, account_list, boolind, ind2, ind3):
  412. '''returns the annual turnover in given year
  413. :param int boolind:account class number
  414. :param int ind2, ind3:debit or credit position in balance
  415. (6-rulaj curent debit, 8-rulaj cumulat debit, index starting from 0)'''
  416. tt = 0
  417. for account in account_list:
  418. # n = len(account[boolind]) == 3 and account[boolind] != '...' and int(account[boolind]) > 700 and int(account[boolind]) < 760
  419. n = account[boolind] == 7
  420. can = account[1][:2] != '76'
  421. # print(account[1][:2])
  422. if n and can:
  423. tt += int(account[ind2]) + int(account[ind3])
  424. return tt
  425. def divid(self, account_list, account='457') -> int:
  426. '''returns dividends/year
  427. :param account_list is account from ncont.db'''
  428. div_ = 0
  429. for acc in account_list:
  430. if acc[1][:3] == account:
  431. div_ += acc[8] + acc[6]
  432. return round(div_)
  433. def divid_current(self, account_list, account='457') -> int:
  434. '''returns dividends from current month
  435. :param account_list is account from ncont.db'''
  436. div_ = 0
  437. for acc in account_list:
  438. if acc[1][:3] == account:
  439. div_ += acc[8]
  440. return round(div_)
  441. def divid_intermed(self, account_list, account='463') -> int:
  442. '''returns dividends from current result/year
  443. :param account_list is account from ncont.db'''
  444. div_ = 0
  445. for acc in account_list:
  446. if acc[1][:3] == account:
  447. div_ += acc[7] + acc[5]
  448. return round(div_)
  449. def divid_inter_current(self, account_list, account='463') -> int:
  450. '''returns dividends from current results in the current month
  451. :param account_list is account from ncont.db'''
  452. div_ = 0
  453. for acc in account_list:
  454. if acc[1][:3] == account:
  455. div_ += acc[7]
  456. return round(div_)
  457. def spons(self, account_list, account='658.02') -> int:
  458. '''returns sponsored money/year
  459. :param account_list is account from ncont.db'''
  460. spons_ = 0
  461. for acc in account_list:
  462. if acc[1] == account:
  463. spons_ += acc[8] + acc[6]
  464. return round(spons_)
  465. def result(self, account_list, boolind, ind2, ind3) -> int: # account='121'
  466. '''returns the final result in given year
  467. :param int boolind:account class number
  468. :param int ind2, ind3:debit or credit position in balance
  469. (6-rulaj curent debit, 8-rulaj cumulat debit, index starting from 0)'''
  470. res_minus = res_plus = 0
  471. for r_minus in account_list:
  472. p = r_minus[boolind] == 6
  473. if p:
  474. res_minus += int(r_minus[ind2]) + int(r_minus[ind3])
  475. for r_plus in account_list:
  476. i = r_plus[boolind] == 7
  477. if i:
  478. res_plus += int(r_plus[ind2]) + int(r_plus[ind3])
  479. return res_plus - res_minus
  480. def ins_payable(self, account_list, account='431') -> int:
  481. '''returns insurences payable in current month
  482. :param account_list is account from ncont.db'''
  483. ins = 0
  484. for acc in account_list:
  485. p = acc[1][:3] == account
  486. if p:
  487. ins += acc[8]
  488. return round(ins)
  489. def CAS_payable(self, account_list, accounts=('431.02', '431.05')) -> int:
  490. '''returns CAS payable in current month
  491. :param account_list is account from ncont.db'''
  492. CAS = 0
  493. acc_1, acc_2 = accounts
  494. for acc in account_list:
  495. p = acc[1] == acc_1
  496. d = acc[1] == acc_2
  497. if p: CAS += acc[8]
  498. if d: CAS += acc[8]
  499. return round(CAS)
  500. def CASS_payable(self, account_list, accounts=('431.04', '431.06')) -> int:
  501. '''returns CASS payable in current month
  502. :param account_list is account from ncont.db'''
  503. CASS = 0
  504. acc_1, acc_2 = accounts
  505. for acc in account_list:
  506. p = acc[1] == acc_1
  507. d = acc[1] == acc_2
  508. if p: CASS += acc[8]
  509. if d: CASS += acc[8]
  510. return round(CASS)
  511. def sal_tax_payable(self, account_list, account='431.44') -> int:
  512. '''returns salary tax payable in current month
  513. :param account_list is account from ncont.db'''
  514. tax = 0
  515. for acc in account_list:
  516. p = acc[1] == account
  517. if p:
  518. tax += acc[8]
  519. return round(tax)
  520. def cam_payable(self, account_list, account='436') -> int:
  521. '''returns CAM payable in current month
  522. :param account_list is account from ncont.db'''
  523. cam = 0
  524. for acc in account_list:
  525. p = acc[1][:3] == account
  526. if p:
  527. cam += acc[8]
  528. return round(cam)
  529. def vat_payable(self, account_list, accounts=('442.03', '442.04')) -> int:
  530. '''returns VAT payable in current month
  531. :param account_list is account from ncont.db'''
  532. tt = 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. tt += acc[8]
  539. elif d:
  540. tt -= acc[7] + acc[8]
  541. return round(tt)
  542. def vat_final(self, acc_list, accounts=('442.03', '442.04')) -> int:
  543. '''returns VAT final payable in current month
  544. :param account_list is account from ncont.db'''
  545. tt = 0
  546. acc_1, acc_2 = accounts
  547. for acc in acc_list:
  548. account = Account(clasa=acc[0],
  549. simbol=acc[1],
  550. denumire=acc[2],
  551. soldid=acc[3],
  552. soldic=acc[4],
  553. precedentd=acc[5],
  554. precedentc=acc[6],
  555. curentd=acc[7],
  556. curentc=acc[8])
  557. payable = account.simbol == acc_1
  558. deductible = account.simbol == acc_2
  559. if payable:
  560. tt += account.soldf('c')
  561. elif deductible:
  562. tt -= account.soldf('d')
  563. return round(tt)
  564. def tax_payable(self, account_list, account='441') -> int:
  565. '''returns income TAX payable in current month
  566. :param account_list is account from ncont.db'''
  567. tax = 0
  568. for acc in account_list:
  569. p = acc[1][:3] == account
  570. if p:
  571. tax += acc[8]
  572. return round(tax)
  573. def div_tax_payable(self, acc_list, accounts='446.07') -> int:
  574. '''returns dividend TAX 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 == accounts
  589. # deductible = account.simbol == acc_2
  590. if payable:
  591. tt += account.curentc
  592. # elif deductible:
  593. # tt -= account.soldf('d')
  594. return round(tt)
  595. def advance_final(self, acc_list, accounts='542') -> int:
  596. '''returns final dvances/year
  597. :param account_list is account from ncont.db'''
  598. tt = 0
  599. # acc_1, acc_2 = accounts
  600. for acc in acc_list:
  601. account = Account(clasa=acc[0],
  602. simbol=acc[1],
  603. denumire=acc[2],
  604. soldid=acc[3],
  605. soldic=acc[4],
  606. precedentd=acc[5],
  607. precedentc=acc[6],
  608. curentd=acc[7],
  609. curentc=acc[8])
  610. payable = account.simbol[:3] == accounts
  611. # deductible = account.simbol == acc_2
  612. if payable:
  613. tt += account.soldf('d')
  614. # elif deductible:
  615. # tt -= account.soldf('d')
  616. return round(tt)
  617. def deb_div(self, acc_list, accounts='461') -> int:
  618. '''returns advances transfered to deb. diversi/year
  619. :param account_list is account from ncont.db'''
  620. tt = 0
  621. # acc_1, acc_2 = accounts
  622. for acc in acc_list:
  623. account = Account(clasa=acc[0],
  624. simbol=acc[1],
  625. denumire=acc[2],
  626. soldid=acc[3],
  627. soldic=acc[4],
  628. precedentd=acc[5],
  629. precedentc=acc[6],
  630. curentd=acc[7],
  631. curentc=acc[8])
  632. payable = account.simbol[:3] == accounts
  633. # deductible = account.simbol == acc_2
  634. if payable:
  635. tt += account.soldf('d')
  636. # elif deductible:
  637. # tt -= account.soldf('d')
  638. return round(tt)
  639. def credit_final(self, acc_list, accounts='455') -> int:
  640. '''returns credited ammount/year
  641. :param account_list is account from ncont.db'''
  642. tt = 0
  643. # acc_1, acc_2 = accounts
  644. for acc in acc_list:
  645. account = Account(clasa=acc[0],
  646. simbol=acc[1],
  647. denumire=acc[2],
  648. soldid=acc[3],
  649. soldic=acc[4],
  650. precedentd=acc[5],
  651. precedentc=acc[6],
  652. curentd=acc[7],
  653. curentc=acc[8])
  654. payable = account.simbol[:3] == accounts
  655. # deductible = account.simbol == acc_2
  656. if payable:
  657. tt += account.soldf('d')
  658. # elif deductible:
  659. # tt -= account.soldf('d')
  660. return round(tt)
  661. if __name__ == '__main__':
  662. mentor = WinMentor()
  663. # accounts = list(mentor.get_bank_accounts('WEBS'))
  664. # account_num = [n for n in accounts[1] if n.startswith(' ')]
  665. for account in mentor.get_bank_accounts('WEBS'):
  666. # if account[2].startswith(' '):
  667. print(account)