Skip to content

Commit a2fd86c

Browse files
committed
feat: system comments for p1 items
1 parent 4b201d4 commit a2fd86c

File tree

6 files changed

+1299
-55
lines changed

6 files changed

+1299
-55
lines changed

apps/internal/tasks.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@
44

55
from django.db.models import Q
66
from django_q.models import OrmQ, Schedule
7+
8+
from fyle_accounting_library.system_comments.models import SystemComment
79
from fyle_accounting_library.fyle_platform.enums import ExpenseImportSourceEnum
810

911
from apps.tasks.models import TaskLog
1012
from apps.fyle.models import ExpenseGroup
1113
from apps.workspaces.actions import export_to_intacct
1214
from apps.workspaces.models import LastExportDetail, Workspace
15+
from apps.workspaces.system_comments import add_system_comment
1316
from workers.helpers import RoutingKeyEnum, WorkerActionEnum, publish_to_rabbitmq
1417
from apps.fyle.actions import post_accounting_export_summary, update_failed_expenses
18+
from apps.workspaces.enums import SystemCommentEntityTypeEnum, SystemCommentIntentEnum, SystemCommentReasonEnum, SystemCommentSourceEnum
1519

1620
logger = logging.getLogger(__name__)
1721
logger.level = logging.INFO
@@ -36,6 +40,7 @@ def retrigger_stuck_exports() -> None:
3640
Re-triggers export stuck exports by identifying failed export attempts
3741
and retrying them.
3842
"""
43+
system_comments = []
3944
prod_workspace_ids = Workspace.objects.filter(
4045
~Q(name__icontains='fyle for') & ~Q(name__iendswith='test')
4146
).values_list('id', flat=True)
@@ -83,10 +88,29 @@ def retrigger_stuck_exports() -> None:
8388
export_expense_group_ids = list(expense_groups.filter(workspace_id=workspace_id).values_list('id', flat=True))
8489
if export_expense_group_ids and len(export_expense_group_ids) < 200:
8590
logger.info('Re-triggering export for expense group %s since no 1 hour schedule for workspace %s', export_expense_group_ids, workspace_id)
91+
92+
for expense_group_id in export_expense_group_ids:
93+
expense_group = expense_groups.filter(id=expense_group_id, workspace_id=workspace_id).first()
94+
if expense_group:
95+
stuck_duration_seconds = (datetime.now(timezone.utc) - expense_group.updated_at.replace(tzinfo=timezone.utc)).total_seconds()
96+
add_system_comment(
97+
system_comments=system_comments,
98+
source=SystemCommentSourceEnum.RETRIGGER_STUCK_EXPORTS,
99+
intent=SystemCommentIntentEnum.EXPORT_RETRIGGERED,
100+
entity_type=SystemCommentEntityTypeEnum.EXPENSE_GROUP,
101+
workspace_id=workspace_id,
102+
entity_id=expense_group_id,
103+
reason=SystemCommentReasonEnum.EXPORT_RETRIGGERED_STUCK,
104+
info={'stuck_duration_seconds': stuck_duration_seconds}
105+
)
106+
86107
export_to_intacct(workspace_id=workspace_id, expense_group_ids=export_expense_group_ids, triggered_by=ExpenseImportSourceEnum.INTERNAL)
87108
else:
88109
logger.info('Skipping export for workspace %s since it has more than 200 expense groups', workspace_id)
89110

111+
if system_comments:
112+
SystemComment.bulk_create_comments(system_comments)
113+
90114

91115
def pause_and_resume_export_schedules() -> None:
92116
"""

apps/sage_intacct/models.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,23 @@ def create_bill_lineitems(expense_group: ExpenseGroup, configuration: Configurat
11261126
allocation_dimensions = set(allocation_detail.keys())
11271127
user_defined_dimensions = [user_defined_dimension for user_defined_dimension in user_defined_dimensions if list(user_defined_dimension.keys())[0] not in allocation_dimensions]
11281128

1129+
if lineitem.billable and not (dimensions_values['customer_id'] and dimensions_values['item_id']):
1130+
missing_fields = []
1131+
if not dimensions_values['customer_id']:
1132+
missing_fields.append('customer_id')
1133+
if not dimensions_values['item_id']:
1134+
missing_fields.append('item_id')
1135+
add_system_comment(
1136+
system_comments=system_comments,
1137+
source=SystemCommentSourceEnum.CREATE_BILL_LINEITEMS,
1138+
intent=SystemCommentIntentEnum.BILLABLE_DISABLED,
1139+
entity_type=SystemCommentEntityTypeEnum.EXPENSE,
1140+
workspace_id=expense_group.workspace_id,
1141+
entity_id=lineitem.id,
1142+
reason=SystemCommentReasonEnum.BILLABLE_SET_TO_FALSE_MISSING_DIMENSIONS,
1143+
info={'missing_fields': missing_fields, 'original_billable': lineitem.billable}
1144+
)
1145+
11291146
bill_lineitem_object, _ = BillLineitem.objects.update_or_create(
11301147
bill=bill,
11311148
expense_id=lineitem.id,
@@ -1344,6 +1361,23 @@ def create_expense_report_lineitems(expense_group: ExpenseGroup, configuration:
13441361
info={'original_merchant': merchant, 'vendor_used': 'Credit Card Misc'}
13451362
)
13461363

1364+
if lineitem.billable and not (customer_id and item_id):
1365+
missing_fields = []
1366+
if not customer_id:
1367+
missing_fields.append('customer_id')
1368+
if not item_id:
1369+
missing_fields.append('item_id')
1370+
add_system_comment(
1371+
system_comments=system_comments,
1372+
source=SystemCommentSourceEnum.CREATE_EXPENSE_REPORT_LINEITEMS,
1373+
intent=SystemCommentIntentEnum.BILLABLE_DISABLED,
1374+
entity_type=SystemCommentEntityTypeEnum.EXPENSE,
1375+
workspace_id=expense_group.workspace_id,
1376+
entity_id=lineitem.id,
1377+
reason=SystemCommentReasonEnum.BILLABLE_SET_TO_FALSE_MISSING_DIMENSIONS,
1378+
info={'missing_fields': missing_fields, 'original_billable': lineitem.billable}
1379+
)
1380+
13471381
expense_report_lineitem_object, _ = ExpenseReportLineitem.objects.update_or_create(
13481382
expense_report=expense_report,
13491383
expense_id=lineitem.id,
@@ -1568,6 +1602,23 @@ def create_journal_entry_lineitems(expense_group: ExpenseGroup, configuration: C
15681602

15691603
allocation_id, _ = get_allocation_id_or_none(expense_group=expense_group, lineitem=lineitem)
15701604

1605+
if lineitem.billable and not (customer_id and item_id):
1606+
missing_fields = []
1607+
if not customer_id:
1608+
missing_fields.append('customer_id')
1609+
if not item_id:
1610+
missing_fields.append('item_id')
1611+
add_system_comment(
1612+
system_comments=system_comments,
1613+
source=SystemCommentSourceEnum.CREATE_JOURNAL_ENTRY_LINEITEMS,
1614+
intent=SystemCommentIntentEnum.BILLABLE_DISABLED,
1615+
entity_type=SystemCommentEntityTypeEnum.EXPENSE,
1616+
workspace_id=expense_group.workspace_id,
1617+
entity_id=lineitem.id,
1618+
reason=SystemCommentReasonEnum.BILLABLE_SET_TO_FALSE_MISSING_DIMENSIONS,
1619+
info={'missing_fields': missing_fields, 'original_billable': lineitem.billable}
1620+
)
1621+
15711622
journal_entry_lineitem_object, _ = JournalEntryLineitem.objects.update_or_create(
15721623
journal_entry=journal_entry,
15731624
expense_id=lineitem.id,
@@ -1795,6 +1846,23 @@ def create_charge_card_transaction_lineitems(expense_group: ExpenseGroup, config
17951846
info={'original_merchant': lineitem.vendor, 'vendor_used': 'Credit Card Misc'}
17961847
)
17971848

1849+
if lineitem.billable and not (customer_id and item_id):
1850+
missing_fields = []
1851+
if not customer_id:
1852+
missing_fields.append('customer_id')
1853+
if not item_id:
1854+
missing_fields.append('item_id')
1855+
add_system_comment(
1856+
system_comments=system_comments,
1857+
source=SystemCommentSourceEnum.CREATE_CHARGE_CARD_TRANSACTION_LINEITEMS,
1858+
intent=SystemCommentIntentEnum.BILLABLE_DISABLED,
1859+
entity_type=SystemCommentEntityTypeEnum.EXPENSE,
1860+
workspace_id=expense_group.workspace_id,
1861+
entity_id=lineitem.id,
1862+
reason=SystemCommentReasonEnum.BILLABLE_SET_TO_FALSE_MISSING_DIMENSIONS,
1863+
info={'missing_fields': missing_fields, 'original_billable': lineitem.billable}
1864+
)
1865+
17981866
charge_card_transaction_lineitem_object, _ = ChargeCardTransactionLineitem.objects.update_or_create(
17991867
charge_card_transaction=charge_card_transaction,
18001868
expense_id=lineitem.id,

0 commit comments

Comments
 (0)