diff --git a/hsbcpdf/helpers/accountstatement.py b/hsbcpdf/helpers/accountstatement.py index d9e03af..0d49db1 100644 --- a/hsbcpdf/helpers/accountstatement.py +++ b/hsbcpdf/helpers/accountstatement.py @@ -23,12 +23,20 @@ class EnumSumAccountTypes: HKDCURRENT = 'HKD Current' FCYSAVINGS = 'FCY Savings' FCYCURRENT = 'FCY Current' + TIMEDEPOSIT = 'Time Deposit' + FCYTIMEDEPOSIT = 'FCY Time Deposit' + UNITTRUST = 'Unit Trusts' + BOND = 'Bonds/Certificates of Deposit' class AccountTypes: HKDSAVINGS = 'HKDSavings' HKDCURRENT = 'HKDCurrent' FCYSAVINGS = 'FCYSavings' FCYCURRENT = 'FCYCurrent' + TIMEDEPOSIT = 'TimeDeposit' + FCYTIMEDEPOSIT = 'FCYTimeDeposit' + UNITTRUST = 'UnitTrust' + BOND = 'Bond' class TableZone: @@ -206,11 +214,21 @@ def clean_table(self): logger.debug(shape) logger.debug('table shape: {}'.format(self.table.shape)) + last_dt = "" dt = "" ccy = "" desc = "" new_balance = 0. for index, row in self.table.iterrows(): + # skip time deposit lines + if ( + row.isnull().all() + or str(row[0]).strip() in ("Time", "No.", "Deposi") + or str(row[0]).strip().isdigit() + or str(row[0]).strip() == "" and str(row[1]).strip() == "" and str(row[2]).strip().isdigit() + or str(row[0]).strip() == "" and str(row[1]).strip() == "" and str(row[2]).strip() == "" + ): + continue # first line with new currency is previous balance if row[0] != ccy and row[0] != "": if ccy != "": @@ -231,13 +249,18 @@ def clean_table(self): new_balance = previous_balance - if row[1] != "": dt = self.extract_date(row[1]) + if row[1] != "": + dt = self.extract_date(row[1]) + last_dt = dt + else: + dt = last_dt + desc = (desc + " " if desc != "" else "") + row[2] credit = row[3] debit = row[4] amount = None - logger.debug("ccy[{}] date[{}] desc[{}] credit[{}] debit[{}]".format(ccy, dt, desc, credit, debit)) + logger.debug("[{}] ccy[{}] date[{}] desc[{}] credit[{}] debit[{}]".format(index, ccy, dt, desc, credit, debit)) if credit is not None and credit != "": amount = float(credit.replace(",", "")) elif debit is not None and debit != "": @@ -266,7 +289,11 @@ class TableZoneSum(TableZone): EnumSumAccountTypes.HKDSAVINGS: AccountTypes.HKDSAVINGS, EnumSumAccountTypes.HKDCURRENT: AccountTypes.HKDCURRENT, EnumSumAccountTypes.FCYSAVINGS: AccountTypes.FCYSAVINGS, - EnumSumAccountTypes.FCYCURRENT: AccountTypes.FCYCURRENT + EnumSumAccountTypes.FCYCURRENT: AccountTypes.FCYCURRENT, + EnumSumAccountTypes.TIMEDEPOSIT: AccountTypes.TIMEDEPOSIT, + EnumSumAccountTypes.FCYTIMEDEPOSIT: AccountTypes.FCYTIMEDEPOSIT, + EnumSumAccountTypes.UNITTRUST: AccountTypes.UNITTRUST, + EnumSumAccountTypes.BOND: AccountTypes.BOND } def __init__(self, page_height, page_width, section, account, st_date): @@ -293,9 +320,27 @@ def clean_table(self): # skip first 2 lines that are header part and account narrative for index, row in self.table[2:].iterrows(): logger.debug("process row ({}): <{}>".format(index, row)) + + # skip inverstment title + if row[0] == 'HSBC Premier\n- Investments': + continue + + # skip exchange rate + if row[0] == 'Exchange Rate': + continue + + # skip (DR=Debit \n) in other line + if row[4] == "(DR=Debit \n)" and row[6] == "(DR=Debit \n)": + continue + if row[0] is not None and row[0] != "": if row[0] == 'Total': - self.summary['total_balance_hkd'] = self.extract_amount(row[6], row[7]) + total_amount = self.extract_amount(row[6], row[7]) + if self.summary['total_balance_hkd'] is None: + self.summary['total_balance_hkd'] = total_amount + else: + # add investments total + self.summary['total_balance_hkd'] += total_amount continue elif row[0] not in TableZoneSum.map_type.keys(): raise TemplateException("Summary contains an unknow Account type [{}]".format(row[0]))