CSV2MentorOtp.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. '''Created 15 Jun 2023 Levi'''
  2. from datetime import date
  3. import pandas as pd
  4. import csv
  5. import os
  6. import shutil
  7. from pymt940.utils import (get_partners, re_check_otp, dig_it, get_last_month)
  8. class PaymentsProcessorOtp:
  9. def __init__(self, csv_path, partner_data, account_name=None, account_number=None,
  10. currency=None, account_location=None, account_agency=None, account_symbol=None):
  11. self.csv_path = csv_path
  12. self.partner_data = partner_data
  13. self._account_name = account_name
  14. self._account_number = account_number
  15. self._currency = currency
  16. self._account_location = account_location
  17. self._account_agency = account_agency
  18. self._account_symbol = account_symbol
  19. self.trans_dict = None
  20. self.names_dict = None
  21. self.obs_dict = None
  22. self.counts = None
  23. self.doc_list = None
  24. self.unique_count = None
  25. self.fiscal_code_dict = None
  26. self.days = None
  27. @property
  28. def account_name(self):
  29. return self._account_name
  30. @account_name.setter
  31. def account_name(self, value):
  32. self._account_name = value
  33. @property
  34. def account_number(self):
  35. return self._account_number
  36. @account_number.setter
  37. def account_number(self, value):
  38. self._account_number = value
  39. @property
  40. def currency(self):
  41. return self._currency
  42. @currency.setter
  43. def currency(self, value):
  44. self._currency = value
  45. @property
  46. def account_location(self):
  47. return self._account_location
  48. @account_location.setter
  49. def account_location(self, value):
  50. self._account_location = value
  51. @property
  52. def account_agency(self):
  53. return self._account_agency
  54. @account_agency.setter
  55. def account_agency(self, value):
  56. self._account_agency = value
  57. @property
  58. def account_symbol(self):
  59. return self._account_symbol
  60. @account_symbol.setter
  61. def account_symbol(self, value):
  62. self._account_symbol = value
  63. def process_payment_data(self, day_slice=(0, 2)):
  64. df_dict = get_partners(self.partner_data)
  65. fiscal_codes = df_dict['CodFiscal']
  66. mentor_names = df_dict['Denumire']
  67. self.fiscal_code_dict = {k: v for k, v in zip(mentor_names, fiscal_codes)}
  68. # empty_row_index = -1
  69. # with open(self.csv_path, 'r') as csvfile:
  70. # reader = csv.reader(csvfile)
  71. # for i, row in enumerate(reader):
  72. # if all(not cell or cell.isspace() for cell in row):
  73. # empty_row_index = i
  74. # break
  75. # # Temporary file to hold the non-empty rows
  76. # temp_file_path = os.path.join(os.path.expanduser('~'), self.csv_path) # '/home/deeejas/Documents/temp.csv'
  77. #
  78. # with open(self.csv_path, 'r') as csvfile, open(temp_file_path, 'w', newline='') as temp_csv:
  79. # reader = csv.reader(csvfile)
  80. # writer = csv.writer(temp_csv)
  81. # for i, row in enumerate(reader):
  82. # if i >= empty_row_index:
  83. # break
  84. # writer.writerow(row)
  85. # # Overwrite the original file by renaming the temp file
  86. # shutil.move(temp_file_path, self.csv_path)
  87. payments_df = pd.read_csv(self.csv_path, keep_default_na=False)
  88. payments_df = payments_df.iloc[1:]
  89. payments_df = payments_df.loc[(payments_df['Suma Debit'] != '') & (payments_df['Suma Debit'] != '0.00')]
  90. # print(payments_df['Suma Debit'])
  91. payments_df['Amount'] = payments_df['Suma Debit'].map(str).apply(dig_it).map(abs)
  92. payments_df['symbols'] = ''
  93. payments_df['types'] = ''
  94. payments_df.loc[payments_df['symbols'] == '', 'symbols'] = 401
  95. payments_df.loc[payments_df['types'] == '', 'types'] = 'ALIMENTARE CREDIT'
  96. self._calculate_payment_attributes(payments_df, mentor_names=mentor_names, day_slice=day_slice)
  97. return payments_df
  98. def process_income_data(self, day_slice=(0, 2)):
  99. df_dict = get_partners(self.partner_data)
  100. fiscal_codes = df_dict['CodFiscal']
  101. mentor_names = df_dict['Denumire']
  102. self.fiscal_code_dict = {k: v for k, v in zip(mentor_names, fiscal_codes)}
  103. # empty_row_index = -1
  104. # with open(self.csv_path, 'r') as csvfile:
  105. # reader = csv.reader(csvfile)
  106. # for i, row in enumerate(reader):
  107. # if all(not cell or cell.isspace() for cell in row):
  108. # empty_row_index = i
  109. # break
  110. # # Temporary file to hold the non-empty rows
  111. # temp_file_path = os.path.join(os.path.expanduser('~'), self.csv_path) # '/home/deeejas/Documents/temp.csv'
  112. #
  113. # with open(self.csv_path, 'r') as csvfile, open(temp_file_path, 'w', newline='') as temp_csv:
  114. # reader = csv.reader(csvfile)
  115. # writer = csv.writer(temp_csv)
  116. # for i, row in enumerate(reader):
  117. # if i >= empty_row_index:
  118. # break
  119. # writer.writerow(row)
  120. # # Overwrite the original file by renaming the temp file
  121. # shutil.move(temp_file_path, self.csv_path)
  122. income_df = pd.read_csv(self.csv_path, keep_default_na=False)
  123. income_df = income_df.iloc[1:]
  124. # income_df['Credit'].replace(' ', '', regex=True, inplace=True)
  125. # income_df['Debit'].replace(' ', '', regex=True, inplace=True)
  126. income_df = income_df.loc[(income_df['Suma Credit'] != '') & (income_df['Suma Credit'] != 0.00)]
  127. # print(income_df['Suma Credit'])
  128. income_df['Amount'] = income_df['Suma Credit'].map(str).apply(dig_it)
  129. income_df['symbols'] = ''
  130. income_df['types'] = ''
  131. income_df.loc[income_df['symbols'] == '', 'symbols'] = 411.01
  132. income_df.loc[income_df['types'] == '', 'types'] = 'ALIMENTARE CREDIT'
  133. self._calculate_payment_attributes(income_df, mentor_names, day_slice=day_slice)
  134. return income_df
  135. def _calculate_payment_attributes(self, df, mentor_names, day_slice=(0, 2)):
  136. # df['Amount'] = df['Credit'].map(str).apply(dig_it).map(abs)
  137. df['Name'] = df['Explicatie'].apply(re_check_otp)
  138. names = [name for name in df['Name'] if name != '']
  139. for name2 in names:
  140. index_ = name2.count(' ')
  141. # nname = name2 # ' '.join(name2.split()[:index_]).upper()
  142. # print(name2)
  143. for name in mentor_names:
  144. # index_ = name.count(' ')
  145. if len(' '.join(name.split()[:index_]).upper()) < 3:
  146. index_ = 2
  147. elif len(' '.join(name.split()[:index_]).upper()) < 2:
  148. index_ = 1
  149. if ' '.join(name2.split()[:index_]).upper() in name.upper():
  150. # print(name)
  151. for index, row in df.iterrows():
  152. if row['Name'] == name2:
  153. df.loc[index, 'Name'] = name
  154. df['Transaction details'] = df['Explicatie']
  155. df['day'] = df['Data op.'].str.slice(*day_slice)
  156. df.loc[df['Transaction details'].str.lower().str.startswith('comision'), 'symbols'] = 627
  157. df.loc[df['Transaction details'].str.lower().str.startswith('abonament'), 'symbols'] = 627
  158. # df.loc[df['Transaction details'].str.lower().str.startswith('inchidere depozit'), 'symbols'] = 508.01
  159. df.loc[df['symbols'] == 627, 'types'] = 'DIRECT PE CHELTUIELI'
  160. df.loc[df['Transaction details'].str.lower().str.startswith('dobanda'), 'symbols'] = 766
  161. # df.loc[df['Transaction details'].str.lower().str.startswith('alimentare cont depozit'), 'symbols'] = 508.01
  162. df.loc[df['symbols'] == 766, 'types'] = 'DIRECT PE VENITURI'
  163. # df.loc[df['symbols'] == 508.01, 'types'] = direction
  164. unique_count = df['day'].nunique()
  165. self.doc_list = list(range(1, unique_count + 1))
  166. days = df['day'].unique().tolist()
  167. self.days = days
  168. trans = []
  169. cl_names = []
  170. obs = []
  171. counts = []
  172. symbols = []
  173. types = []
  174. for day in days:
  175. values = df.loc[df['day'] == day, 'Amount'].tolist()
  176. trans.append(values)
  177. self.trans_dict = {k: v for k, v in zip(days, trans)}
  178. for day in days:
  179. values = df.loc[df['day'] == day, 'Name'].tolist()
  180. for i in range(len(values)):
  181. if values[i] not in mentor_names:
  182. values[i] = ''
  183. cl_names.append(values)
  184. self.names_dict = {k: v for k, v in zip(days, cl_names)}
  185. # print(self.names_dict)
  186. for day in days:
  187. values = df.loc[df['day'] == day, 'Transaction details'].tolist()
  188. obs.append(values)
  189. self.obs_dict = {k: v for k, v in zip(days, obs)}
  190. for day in days:
  191. value_counts = df['day'].value_counts()
  192. count_of_value = value_counts[day]
  193. counts.append(count_of_value)
  194. self.counts = counts
  195. for day in days:
  196. acc_symbol = df.loc[df['day'] == day, 'symbols'].tolist()
  197. symbols.append(acc_symbol)
  198. self.symbol_list = {k: v for k, v in zip(days, symbols)}
  199. for day in days:
  200. type_ = df.loc[df['day'] == day, 'types'].tolist()
  201. types.append(type_)
  202. self.type_list = {k: v for k, v in zip(days, types)}
  203. self.unique_count = unique_count
  204. def to_dict(self, kind: str, month_back: int, std_client: str, acc_symbol: int):
  205. kinds = ['TotalIncasari', 'TotalPlati']
  206. if kind not in kinds:
  207. raise TypeError(f'type not in {kinds}')
  208. else:
  209. data = {'AnLucru': date.today().year,
  210. 'LunaLucru': get_last_month(month_back),
  211. 'TotalDocumente': self.unique_count,
  212. 'documents': self.doc_list,
  213. 'AccountName': self.account_name,
  214. 'AccountSymbol': self.account_symbol,
  215. 'AccountNumber': self.account_number,
  216. 'AccountLocation': self.account_location,
  217. 'AccountAgencyLocation': self.account_agency,
  218. 'Currency': self.currency,
  219. 'days': self.days,
  220. kind: self.counts,
  221. 'incasari': self.counts,
  222. 'clients': self.names_dict,
  223. 'cods': self.fiscal_code_dict,
  224. 'amount': self.trans_dict,
  225. 'cont': self.symbol_list,
  226. 'std_client': std_client,
  227. 'obs': self.obs_dict,
  228. 'represents': self.type_list
  229. }
  230. return data