Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add split_expense_grouping to the expense group settings serializer #404

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 47 additions & 7 deletions apps/fyle/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from fyle_accounting_mappings.models import ExpenseAttribute

from apps.fyle.enums import ExpenseStateEnum, FundSourceEnum, PlatformExpensesEnum
from apps.workspaces.models import Workspace
from apps.workspaces.models import Workspace, WorkspaceGeneralSettings

logger = logging.getLogger(__name__)
logger.level = logging.INFO
Expand All @@ -35,6 +35,7 @@
"spent_at",
"expense_id",
"posted_at",
"bank_transaction_id",
]


Expand Down Expand Up @@ -515,13 +516,52 @@ def create_expense_groups_by_report_id_fund_source(
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,
)

expense_groups.extend(corporate_credit_card_expense_groups)
if corporate_credit_card_expenses:
workspace_general_settings = WorkspaceGeneralSettings.objects.get(
workspace_id=workspace_id
)
ccc_export_module = workspace_general_settings.corporate_credit_card_expenses_object

if ccc_export_module == "BANK TRANSACTION" and expense_group_settings.split_expense_grouping == 'MULTIPLE_LINE_ITEM':
ccc_expenses_without_bank_transaction_id = list(
filter(lambda expense: not expense.bank_transaction_id, corporate_credit_card_expenses)
)

ccc_expenses_with_bank_transaction_id = list(
filter(lambda expense: expense.bank_transaction_id, corporate_credit_card_expenses)
)

if ccc_expenses_without_bank_transaction_id:
groups_without_bank_transaction_id = _group_expenses(
ccc_expenses_without_bank_transaction_id,
corporate_credit_card_expense_group_field,
workspace_id,
)
expense_groups.extend(groups_without_bank_transaction_id)

if ccc_expenses_with_bank_transaction_id:
split_expense_group_fields = [
field for field in corporate_credit_card_expense_group_field
if field not in ('expense_id', 'expense_number')
]
split_expense_group_fields.append('bank_transaction_id')

groups_with_bank_transaction_id = _group_expenses(
ccc_expenses_with_bank_transaction_id,
split_expense_group_fields,
workspace_id,
)
expense_groups.extend(groups_with_bank_transaction_id)

else:
corporate_credit_card_expense_groups = _group_expenses(
corporate_credit_card_expenses,
corporate_credit_card_expense_group_field,
workspace_id,
)

expense_groups.extend(corporate_credit_card_expense_groups)

expense_group_objects = []

Expand Down
4 changes: 4 additions & 0 deletions apps/workspaces/apis/export_settings/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class ExpenseGroupSettingsSerializer(serializers.ModelSerializer):
ccc_expense_state = serializers.CharField(
allow_null=True, allow_blank=True, required=False
)
split_expense_grouping = serializers.CharField(
allow_null=False, allow_blank=False, required=True
)

class Meta:
model = ExpenseGroupSettings
Expand All @@ -74,6 +77,7 @@ class Meta:
"corporate_credit_card_expense_group_fields",
"ccc_export_date_type",
"ccc_expense_state",
"split_expense_grouping",
]


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
rollback;
begin;

UPDATE expense_group_settings
SET split_expense_grouping = 'SINGLE_LINE_ITEM';
Loading