Skip to content

Commit

Permalink
Support for refund for Bill module (#608)
Browse files Browse the repository at this point in the history
* Support for refund for Bill module

* linting

* Test cases and comment resolved

* added more tests

* making test case better

* corrected test

* corrected fixture

* corrected test

* spent_at test fixed

* linting fixed
  • Loading branch information
Ashutosh619-sudo authored May 14, 2024
1 parent 1a71974 commit 3fa18d5
Show file tree
Hide file tree
Showing 3 changed files with 647 additions and 39 deletions.
142 changes: 109 additions & 33 deletions apps/fyle/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,62 @@ def _group_expenses(expenses, group_fields, workspace_id):
return expense_groups


def filter_expense_groups(
expense_groups,
expenses: Expense,
expense_group_fields,
reimbursable_export_type=None,
ccc_export_type=None,
):

filtered_expense_groups = []

for expense_group in expense_groups:
expense_group_expenses_ids = expense_group['expense_ids']
filtered_expenses = [
item for item in expenses if item.id in expense_group_expenses_ids
]
if 'expense_id' not in expense_group_fields and (
reimbursable_export_type in ('EXPENSE', 'BILL') or ccc_export_type == 'BILL'
):
total_amount = 0
if "spent_at" in expense_group_fields:
grouped_data = defaultdict(list)
for expense in filtered_expenses:
spent_at = expense.spent_at
grouped_data[spent_at].append(expense)
grouped_expenses = list(grouped_data.values())
expense_groups = []
for group_expense in grouped_expenses:
total_amount = 0
for expense in group_expense:
total_amount += expense.amount
if total_amount < 0:
filtered_expenses = list(
filter(lambda expense: expense.amount > 0, group_expense)
)
else:
for expense in filtered_expenses:
total_amount += expense.amount

if total_amount < 0:
filtered_expenses = list(
filter(lambda expense: expense.amount > 0, filtered_expenses)
)
elif reimbursable_export_type != 'JOURNAL ENTRY':
filtered_expenses = list(
filter(lambda expense: expense.amount > 0, filtered_expenses)
)

filtered_expense_ids = [item.id for item in filtered_expenses]

if len(filtered_expense_ids) != 0:
expense_group['expense_ids'] = filtered_expense_ids
filtered_expense_groups.append(expense_group)

return filtered_expense_groups


class ExpenseGroup(models.Model):
"""
Expense Group
Expand All @@ -319,48 +375,68 @@ class Meta:
db_table = 'expense_groups'

@staticmethod
def create_expense_groups_by_report_id_fund_source(expense_objects: List[Expense], workspace_id):
def create_expense_groups_by_report_id_fund_source(
expense_objects: List[Expense], workspace_id
):
"""
Group expense by report_id and fund_source
"""
expense_group_settings = ExpenseGroupSettings.objects.get(workspace_id=workspace_id)

reimbursable_expense_group_fields = expense_group_settings.reimbursable_expense_group_fields
reimbursable_expenses = list(filter(lambda expense: expense.fund_source == 'PERSONAL', expense_objects))
general_settings = WorkspaceGeneralSettings.objects.get(workspace_id=workspace_id)
expense_groups = []
expense_group_settings = ExpenseGroupSettings.objects.get(
workspace_id=workspace_id
)

if general_settings.reimbursable_expenses_object == 'EXPENSE' and 'expense_id' not in reimbursable_expense_group_fields:
total_amount = 0
if 'spent_at' in reimbursable_expense_group_fields:
grouped_data = defaultdict(list)
for expense in reimbursable_expenses:
spent_at = expense.spent_at
grouped_data[spent_at].append(expense)
grouped_expenses = list(grouped_data.values())
reimbursable_expenses = []
for expense_group in grouped_expenses:
total_amount = 0
for expense in expense_group:
total_amount += expense.amount
if total_amount < 0:
expense_group = list(filter(lambda expense: expense.amount > 0, expense_group))
reimbursable_expenses.extend(expense_group)
else:
for expense in reimbursable_expenses:
total_amount += expense.amount
reimbursable_expense_group_fields = (
expense_group_settings.reimbursable_expense_group_fields
)
reimbursable_expenses = list(
filter(lambda expense: expense.fund_source == "PERSONAL", expense_objects)
)
general_settings = WorkspaceGeneralSettings.objects.get(
workspace_id=workspace_id
)

if total_amount < 0:
reimbursable_expenses = list(filter(lambda expense: expense.amount > 0, reimbursable_expenses))
elif general_settings.reimbursable_expenses_object != 'JOURNAL ENTRY':
reimbursable_expenses = list(filter(lambda expense: expense.amount > 0, reimbursable_expenses))
reimbursable_expense_groups = _group_expenses(
reimbursable_expenses, reimbursable_expense_group_fields, workspace_id
)

filtered_reimbursable_expense_groups = filter_expense_groups(
reimbursable_expense_groups,
reimbursable_expenses,
reimbursable_expense_group_fields,
general_settings.reimbursable_expenses_object,
None
)

expense_groups.extend(filtered_reimbursable_expense_groups)

expense_groups = _group_expenses(reimbursable_expenses, reimbursable_expense_group_fields, workspace_id)
corporate_credit_card_expense_group_field = (
expense_group_settings.corporate_credit_card_expense_group_fields
)
corporate_credit_card_expenses = list(
filter(lambda expense: expense.fund_source == "CCC", expense_objects)
)

corporate_credit_card_expense_group_field = expense_group_settings.corporate_credit_card_expense_group_fields
corporate_credit_card_expenses = list(filter(lambda expense: expense.fund_source == 'CCC', expense_objects))
corporate_credit_card_expense_groups = _group_expenses(corporate_credit_card_expenses, corporate_credit_card_expense_group_field, workspace_id)
filtered_corporate_credit_card_expense_groups = _group_expenses(
corporate_credit_card_expenses,
corporate_credit_card_expense_group_field,
workspace_id,
)

if (
general_settings.corporate_credit_card_expenses_object == "BILL"
and "expense_id" not in corporate_credit_card_expense_group_field
):
filtered_corporate_credit_card_expense_groups = filter_expense_groups(
filtered_corporate_credit_card_expense_groups,
corporate_credit_card_expenses,
corporate_credit_card_expense_group_field,
None,
general_settings.corporate_credit_card_expenses_object
)

expense_groups.extend(corporate_credit_card_expense_groups)
expense_groups.extend(filtered_corporate_credit_card_expense_groups)

expense_group_objects = []

Expand Down
Loading

0 comments on commit 3fa18d5

Please sign in to comment.