diff --git a/bpfin/financials/cash_balance.py b/bpfin/financials/cash_balance.py index 55cd11cae1d32dc29efe30003176f65c543f3a3e..6b43215d6a6786b02a253a1b0f1bdeec3f89dbae 100644 --- a/bpfin/financials/cash_balance.py +++ b/bpfin/financials/cash_balance.py @@ -1,25 +1,48 @@ -def cash_balance(cash_dictionary): +def cash_balance(analysis_date, cash_dictionary): """Returns current cash balance Args: + analysis_date (dictionary) = {starting_date: months} cash_dictionary (dictionary): {datetime:(cash value,year)} Returns: dictionary: {datetime, cash value} """ - cash_balance_dictionary = {} + analysis_years = {} + + start_year = analysis_date['proforma_start'].year + for i in range(analysis_date['proforma_duration']): + analysis_years[start_year + i] = [] + cash_balance_dictionary = {} bank_statement = {} - for my_date, value_tuple in cash_dictionary.items(): + for date, value_tuple in cash_dictionary.items(): if value_tuple[1] is True: - cash_balance_dictionary[my_date.year] = value_tuple[0] + cash_balance_dictionary[date.year] = value_tuple[0] else: - # check if year (key) not is present in bank_statement - if my_date.year not in bank_statement: - bank_statement[my_date.year] = [] - bank_statement[my_date.year].append(value_tuple[0]) + if date.year not in bank_statement: + bank_statement[date.year] = [] + bank_statement[date.year].append(value_tuple[0]) for year, value in bank_statement.items(): if year not in cash_balance_dictionary: cash_balance_dictionary[year] = sum(value) / len(value) - return cash_balance_dictionary + + sum_cash = [] + for year in cash_balance_dictionary: + sum_cash.append(cash_balance_dictionary[year]) + cash_average = sum(sum_cash)/len(sum_cash) + + final_dict = {} + for year in analysis_years: + if year in cash_balance_dictionary: + final_dict[year] = cash_balance_dictionary[year] + if year < min(cash_balance_dictionary): + min_value = min(cash_balance_dictionary) + final_dict[year] = cash_balance_dictionary[min_value] + if year > min(cash_balance_dictionary) and year < max(cash_balance_dictionary): + if year not in cash_balance_dictionary: + final_dict[year] = cash_average + + return final_dict + diff --git a/bpfin/financials/liability.py b/bpfin/financials/liability.py index a946ba0e129ea7b9ca8dd2b2df9d024cca465e72..436893a6a3e541c93b1af99e8265f2f67edeb966 100644 --- a/bpfin/financials/liability.py +++ b/bpfin/financials/liability.py @@ -92,8 +92,10 @@ def final_liability_dict(start_date, liability_dictionary, months): pro_date.year, pro_date.month, calendar.monthrange( pro_date.year, pro_date.month)[1])] = [] final_dict[(pro_date.year, - pro_date.month, calendar.monthrange( - pro_date.year, pro_date.month)[1] + pro_date.month, + calendar.monthrange( + pro_date.year, + pro_date.month)[1] )].append(debt_date_dict[debt_date]) for pro_date in pro_forma_calendar: if (pro_date.year, pro_date.month, calendar.monthrange( diff --git a/bpfin/tests/test_financials/test_cash_balance.py b/bpfin/tests/test_financials/test_cash_balance.py index 9a33d9af2d207e6b489111e72f6337dcb38b0037..fa1a08d15285c56f9d971680500c90b3a0b5c45d 100644 --- a/bpfin/tests/test_financials/test_cash_balance.py +++ b/bpfin/tests/test_financials/test_cash_balance.py @@ -9,7 +9,23 @@ def test_cash_balance(): date(2016, 11, 11): (500, False), date(2016, 10, 10): (400, False) } - output_cash_balance = {2014: 500.0, 2015: 600, 2016: 450.0} - result = cash_balance(input_dictionary) + input_analysis_date = { + 'proforma_start': date(2006, 5, 3), + 'proforma_duration': 12 + } + output_cash_balance = { + 2006: 500, + 2007: 500, + 2008: 500, + 2009: 500, + 2010: 500, + 2011: 500, + 2012: 500, + 2013: 500, + 2014: 500.0, + 2015: 600, + 2016: 450.0 + } + result = cash_balance(input_analysis_date, input_dictionary) assert result == output_cash_balance