@@ -69,49 +69,59 @@ class Meta:
6969 db_table = 'expenses'
7070
7171 @staticmethod
72- def create_expense_objects (expenses : List [Dict ], workspace_id : int ):
72+ def create_expense_objects (expenses : List [Dict ], workspace_id : int , skip_update : bool = True ):
7373 """
7474 Bulk create expense objects
7575 """
7676 expense_objects = []
7777
7878 for expense in expenses :
79- expense_object , _ = Expense .objects .update_or_create (
80- expense_id = expense ['id' ],
81- workspace_id = workspace_id ,
82- defaults = {
83- 'employee_email' : expense ['employee_email' ],
84- 'employee_name' : expense ['employee_name' ],
85- 'category' : expense ['category' ],
86- 'sub_category' : expense ['sub_category' ],
87- 'project' : expense ['project' ],
88- 'expense_number' : expense ['expense_number' ],
89- 'org_id' : expense ['org_id' ],
79+ expense_data_to_append = None
80+ if not skip_update :
81+ expense_data_to_append = {
9082 'claim_number' : expense ['claim_number' ],
91- 'amount' : round (expense ['amount' ], 2 ),
92- 'currency' : expense ['currency' ],
93- 'foreign_amount' : expense ['foreign_amount' ],
94- 'foreign_currency' : expense ['foreign_currency' ],
95- 'reimbursable' : expense ['reimbursable' ],
96- 'state' : expense ['state' ],
97- 'vendor' : expense ['vendor' ][:250 ] if expense ['vendor' ] else None ,
98- 'cost_center' : expense ['cost_center' ],
99- 'corporate_card_id' : expense ['corporate_card_id' ],
100- 'purpose' : expense ['purpose' ],
101- 'report_id' : expense ['report_id' ],
102- 'file_ids' : expense ['file_ids' ],
103- 'spent_at' : expense ['spent_at' ],
10483 'approved_at' : expense ['approved_at' ],
105- 'posted_at' : expense ['posted_at' ],
106- 'expense_created_at' : expense ['expense_created_at' ],
107- 'expense_updated_at' : expense ['expense_updated_at' ],
108- 'fund_source' : SOURCE_ACCOUNT_MAP [expense ['source_account_type' ]],
109- 'verified_at' : expense ['verified_at' ],
110- 'custom_properties' : expense ['custom_properties' ],
111- 'tax_amount' : round (expense ['tax_amount' ], 2 ) if expense ['tax_amount' ] is not None else None ,
112- 'tax_group_id' : expense ['tax_group_id' ],
113- 'billable' : expense ['billable' ] if expense ['billable' ] else False
11484 }
85+
86+ defaults = {
87+ 'employee_email' : expense ['employee_email' ],
88+ 'employee_name' : expense ['employee_name' ],
89+ 'category' : expense ['category' ],
90+ 'sub_category' : expense ['sub_category' ],
91+ 'project' : expense ['project' ],
92+ 'expense_number' : expense ['expense_number' ],
93+ 'org_id' : expense ['org_id' ],
94+ 'amount' : round (expense ['amount' ], 2 ),
95+ 'currency' : expense ['currency' ],
96+ 'foreign_amount' : expense ['foreign_amount' ],
97+ 'foreign_currency' : expense ['foreign_currency' ],
98+ 'reimbursable' : expense ['reimbursable' ],
99+ 'state' : expense ['state' ],
100+ 'vendor' : expense ['vendor' ][:250 ] if expense ['vendor' ] else None ,
101+ 'cost_center' : expense ['cost_center' ],
102+ 'corporate_card_id' : expense ['corporate_card_id' ],
103+ 'purpose' : expense ['purpose' ],
104+ 'report_id' : expense ['report_id' ],
105+ 'file_ids' : expense ['file_ids' ],
106+ 'spent_at' : expense ['spent_at' ],
107+ 'posted_at' : expense ['posted_at' ],
108+ 'expense_created_at' : expense ['expense_created_at' ],
109+ 'expense_updated_at' : expense ['expense_updated_at' ],
110+ 'fund_source' : SOURCE_ACCOUNT_MAP [expense ['source_account_type' ]],
111+ 'verified_at' : expense ['verified_at' ],
112+ 'custom_properties' : expense ['custom_properties' ],
113+ 'tax_amount' : round (expense ['tax_amount' ], 2 ) if expense ['tax_amount' ] is not None else None ,
114+ 'tax_group_id' : expense ['tax_group_id' ],
115+ 'billable' : expense ['billable' ] if expense ['billable' ] else False
116+ }
117+
118+ if expense_data_to_append :
119+ defaults .update (expense_data_to_append )
120+
121+ expense_object , _ = Expense .objects .update_or_create (
122+ expense_id = expense ['id' ],
123+ workspace_id = workspace_id ,
124+ defaults = defaults
115125 )
116126 expense_objects .append (expense_object )
117127
0 commit comments