|
| 1 | +from abc import ABC, abstractmethod |
| 2 | +from item import ItemsDB |
| 3 | +import statistics |
| 4 | + |
| 5 | + |
| 6 | +class ExpenseIncomeStats: |
| 7 | + def __init__(self, db_path: str, start: str = None, end: str = None): |
| 8 | + self._items_db = ItemsDB(db_path) |
| 9 | + self.start = start |
| 10 | + self.end = end |
| 11 | + |
| 12 | + if start is None and end is None: |
| 13 | + # Get all Items |
| 14 | + self._items = self._items_db.get_all_items() |
| 15 | + pass |
| 16 | + elif start is None and end is not None: |
| 17 | + # Start from the Oldest Item(s) to the given end date string |
| 18 | + oldest = min(item.date for item in self._items_db.get_all_items()) |
| 19 | + self._items = self._items_db.get_items_by_date_range(oldest.strftime("%Y-%m-%d"), end) |
| 20 | + elif start is not None and end is None: |
| 21 | + # Start from the given start date string to the latest date |
| 22 | + latest = max(item.date for item in self._items_db.get_all_items()) |
| 23 | + self._items = self._items_db.get_items_by_date_range(start, latest.strftime("%Y-%m-%d")) |
| 24 | + else: |
| 25 | + # Get item from given date range |
| 26 | + self._items = self._items_db.get_items_by_date_range(start, end) |
| 27 | + |
| 28 | + @staticmethod |
| 29 | + def _get_stats(items) -> dict: |
| 30 | + expense_items = [item.amount for item in items if item.amount < 0] |
| 31 | + if len(expense_items) > 0: |
| 32 | + expense_stats = { |
| 33 | + "average_expense": statistics.mean(expense_items), |
| 34 | + "min_expense": max(expense_items), # Warn: Negative Sign |
| 35 | + "max_expense": min(expense_items), |
| 36 | + } |
| 37 | + else: |
| 38 | + expense_stats = {} |
| 39 | + |
| 40 | + income_items = [item.amount for item in items if item.amount > 0] |
| 41 | + if len(income_items) > 0: |
| 42 | + income_stats = { |
| 43 | + "average_income": statistics.mean(income_items), |
| 44 | + "min_income": min(income_items), |
| 45 | + "max_income": max(income_items), |
| 46 | + } |
| 47 | + else: |
| 48 | + income_stats = {} |
| 49 | + |
| 50 | + return expense_stats | income_stats |
| 51 | + |
| 52 | + def get_stats(self): |
| 53 | + return self._get_stats(self._items) |
| 54 | + |
| 55 | + def get_stats_by_category(self) -> dict: |
| 56 | + # The 'root' of category (i.e. it aggregates the subcategories). We do not care about the subcategories. |
| 57 | + # Set of Root Categories |
| 58 | + # item.get_category_str() for an item without category would return 'Uncategorized' |
| 59 | + category_names = {item.category.name if item.category is not None else item.get_category_str() for item in |
| 60 | + self._items} |
| 61 | + |
| 62 | + out_dict = {} |
| 63 | + for category_name in category_names: |
| 64 | + items = [item for item in self._items if item.category.name == category_name] |
| 65 | + out_dict[category_name] = self._get_stats(items) |
| 66 | + |
| 67 | + return out_dict |
| 68 | + |
| 69 | + def get_stats_by_category_with_subcategories(self) -> dict: |
| 70 | + category_names = self._items_db.get_category_names() |
| 71 | + stats_dict = {} |
| 72 | + for category_name in category_names: |
| 73 | + # Case: With Category and Without Subcategory |
| 74 | + stats_dict[f'{category_name}-NoSubcategory'] = self._get_stats( |
| 75 | + self._items_db.get_items_without_subcategory(category_name)) |
| 76 | + # Case: With Category and With Subcategory |
| 77 | + for subcategory in self._items_db.get_subcategory_name(category_name): |
| 78 | + stats_dict[f'{category_name}-{subcategory}'] = \ |
| 79 | + self._get_stats(self._items_db.get_items_by_category_and_subcategory(category_name, subcategory)) |
| 80 | + |
| 81 | + return stats_dict |
| 82 | + |
| 83 | + @staticmethod |
| 84 | + def currency_converter(base_currency: str, quote_currency: str) -> float: |
| 85 | + # TODO: Implement currency_converter() method. |
| 86 | + pass |
| 87 | + |
| 88 | + |
| 89 | +class Report(ABC): |
| 90 | + @abstractmethod |
| 91 | + def generate_report(self, *args, **kwargs): |
| 92 | + pass |
| 93 | + |
| 94 | + |
| 95 | +class ExcelReport(Report): |
| 96 | + def __init__(self, file_path: str): |
| 97 | + self.file_path = file_path |
| 98 | + |
| 99 | + def generate_report(self): |
| 100 | + pass |
| 101 | + |
| 102 | + |
| 103 | +class PdfReport(Report): |
| 104 | + def generate_report(self): |
| 105 | + pass |
0 commit comments