Skip to content

Commit

Permalink
fix edit expenses (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hrishabh17 authored Jul 12, 2024
1 parent c5ae1f0 commit 2cb05b7
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 64 deletions.
82 changes: 46 additions & 36 deletions apps/fyle/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class Meta:
db_table = 'expenses'

@staticmethod
def create_expense_objects(expenses: List[Dict], workspace_id: int):
def create_expense_objects(expenses: List[Dict], workspace_id: int, skip_update: bool = False):
"""
Bulk create expense objects
"""
Expand All @@ -110,45 +110,55 @@ def create_expense_objects(expenses: List[Dict], workspace_id: int):
if expense['custom_properties'][custom_property_field] == '':
expense['custom_properties'][custom_property_field] = None

# Create or update an Expense object based on expense_id
expense_object, _ = Expense.objects.update_or_create(
expense_id=expense['id'],
defaults={
'employee_email': expense['employee_email'],
'employee_name': expense['employee_name'],
'category': expense['category'],
'sub_category': expense['sub_category'],
'project': expense['project'],
'expense_number': expense['expense_number'],
'org_id': expense['org_id'],
expense_data_to_append = None
if not skip_update:
expense_data_to_append = {
'claim_number': expense['claim_number'],
'amount': round(expense['amount'], 2),
'currency': expense['currency'],
'foreign_amount': expense['foreign_amount'],
'foreign_currency': expense['foreign_currency'],
'tax_amount': expense['tax_amount'],
'tax_group_id': expense['tax_group_id'],
'reimbursable': expense['reimbursable'],
'billable': expense['billable'] if expense['billable'] else False,
'state': expense['state'],
'vendor': expense['vendor'][:250] if expense['vendor'] else None,
'cost_center': expense['cost_center'],
'purpose': expense['purpose'],
'report_id': expense['report_id'],
'report_title': expense['report_title'],
'spent_at': expense['spent_at'],
'approved_at': expense['approved_at'],
'posted_at': expense['posted_at'],
'expense_created_at': expense['expense_created_at'],
'expense_updated_at': expense['expense_updated_at'],
'fund_source': SOURCE_ACCOUNT_MAP[expense['source_account_type']],
'verified_at': expense['verified_at'],
'custom_properties': expense['custom_properties'],
'payment_number': expense['payment_number'],
'file_ids': expense['file_ids'],
'corporate_card_id': expense['corporate_card_id'],
'workspace_id': workspace_id
'payment_number': expense['payment_number']
}

defaults = {
'employee_email': expense['employee_email'],
'employee_name': expense['employee_name'],
'category': expense['category'],
'sub_category': expense['sub_category'],
'project': expense['project'],
'expense_number': expense['expense_number'],
'org_id': expense['org_id'],
'amount': round(expense['amount'], 2),
'currency': expense['currency'],
'foreign_amount': expense['foreign_amount'],
'foreign_currency': expense['foreign_currency'],
'tax_amount': expense['tax_amount'],
'tax_group_id': expense['tax_group_id'],
'reimbursable': expense['reimbursable'],
'billable': expense['billable'] if expense['billable'] else False,
'state': expense['state'],
'vendor': expense['vendor'][:250] if expense['vendor'] else None,
'cost_center': expense['cost_center'],
'purpose': expense['purpose'],
'report_id': expense['report_id'],
'spent_at': expense['spent_at'],
'posted_at': expense['posted_at'],
'expense_created_at': expense['expense_created_at'],
'expense_updated_at': expense['expense_updated_at'],
'fund_source': SOURCE_ACCOUNT_MAP[expense['source_account_type']],
'verified_at': expense['verified_at'],
'custom_properties': expense['custom_properties'],
'file_ids': expense['file_ids'],
'corporate_card_id': expense['corporate_card_id'],
'workspace_id': workspace_id
}

if expense_data_to_append:
defaults.update(expense_data_to_append)

# Create or update an Expense object based on expense_id
expense_object, _ = Expense.objects.update_or_create(
expense_id=expense['id'],
defaults=defaults
)

# Check if an AccountingExport related to the expense object already exists
Expand Down
2 changes: 1 addition & 1 deletion apps/fyle/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,5 @@ def update_non_exported_expenses(data: Dict) -> None:
expense_obj.append(data)
expense_objects = FyleExpenses().construct_expense_object(expense_obj, expense.workspace_id)
Expense.create_expense_objects(
expense_objects, expense.workspace_id
expense_objects, expense.workspace_id, skip_update=True
)
2 changes: 1 addition & 1 deletion ms_business_central_api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@
'cached': False,
'orm': 'default',
'ack_failures': True,
'poll': 1,
'poll': 5,
'max_attempts': 1,
'attempt_count': 1,
# The number of tasks a worker will process before recycling.
Expand Down
2 changes: 1 addition & 1 deletion ms_business_central_api/tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
'cached': False,
'orm': 'default',
'ack_failures': True,
'poll': 1,
'poll': 5,
'max_attempts': 1,
'attempt_count': 1,
# The number of tasks a worker will process before recycling.
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ms-dynamics-business-central-sdk==1.4.2
# Reusable Fyle Packages
fyle-rest-auth==1.7.2
fyle-accounting-mappings==1.33.1
fyle-integrations-platform-connector==1.38.1
fyle-integrations-platform-connector==1.38.4

# Postgres Dependincies
psycopg2-binary==2.9.9
Expand Down
24 changes: 0 additions & 24 deletions tests/test_fyle/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,30 +159,6 @@
},
'project_id': 330241,
'purpose': None,
'report': {
'amount': 12,
'approvals': [
{
'approver_user': {
'email': 'admin1@fyleforimporrttest.in',
'full_name': 'Theresa Brown',
'id': 'usVN2WTtPqE7'
},
'approver_user_id': 'usVN2WTtPqE7',
'state': 'APPROVAL_DONE'
}
],
'id': 'rpN41rGGnxNI',
'last_approved_at': '2024-05-10T07:53:25.774+00:00',
'last_paid_at': None,
'last_submitted_at': '2024-05-10T07:53:09.457+00:00',
'last_verified_at': '2024-05-10T07:55:02.32928+00:00',
'reimbursement_id': 'reimYNNUkKQiWp',
'reimbursement_seq_num': 'P/2024/05/T/P/2024/05/R/30',
'seq_num': 'C/2024/05/R/45',
'state': 'PAYMENT_PROCESSING',
'title': '#5: May 2024'
},
'report_id': 'rpN41rGGnxNI',
'report_last_approved_at': '2024-05-10T07:53:25.774000+00:00',
'report_last_paid_at': None,
Expand Down

0 comments on commit 2cb05b7

Please sign in to comment.