if {"" ne $cost_id && !$overwrite_existing_expense_item_attributes_p }{
if {$ns_write_p}{
ns_write "<li><font color=red>Error: We have found an existing Expense:<br/><br/> id:$cost_id<br/>Amount:'$amount'<br/>Note:$note<br/>External Company Name: $external_company_name<br/>expense_type_id: $expense_type_id, expense_date: $expense_date <br/> ...and will not overwrite any of attributes.<br>Skipped record</font>\n"
}
continue
}
# cost_name needs to be there
if {"" == $cost_name}{
# cost_name is mandatory
if {"" == $expense_name}{
if {$ns_write_p}{
ns_write "<li><font color=red>Error: We have found an empty 'Cost Name' in line $cnt.<br>
Skipped record. Please correct the CSV file. Every costs needs to have a unique Cost Name.</font>\n"
set cost_nr [regsub{[^a-z0-9_]}[string trim [string tolower $cost_name]]"_"]
}
if {"" == $cost_type_id}{
if {$ns_write_p}{
ns_write "<li><font color=red>Error: We have found an empty 'Cost Type' in line $cnt.<br>
Skipped record. Please correct the CSV file.</font>\n"
# Expense Items only!
if {[im_cost_type_expense_item] != $cost_type_id || "" eq $cost_type_id }{
if {$ns_write_p}{
ns_write "<li><font color=red>Error: We have found an empty cost type or a cost type id that is different from: [im_cost_type_expense_item] (Expense)<br>
Skipped record - please correct the CSV file.</font>\n"
}
continue
}
# Status is optional
if {"" == $cost_status_id}{
if {$ns_write_p}{ ns_write "<li><font color=brown>Warning: Didn't find cost status '$cost_status', using default status 'Created'</font>\n"}
set cost_status_id [im_cost_status_created]
}
if {"" eq $currency}{
# VAT is optional
if {"" eq $vat}{
if {$ns_write_p}{ ns_write "<li><font color=brown>Warning: Didn't find a value for VAT, setting VAT to '0'</font>\n"}
set vat 0
}
# Currency is optional
if {"" eq $expense_currency }{
if {$ns_write_p}{ ns_write "<li><font color=brown>Warning: Didn't find currency, using default currency='$default_currency'</font>\n"}
set currency $default_currency
set expense_currency $default_currency
}
# Amount is mandatory
if {"" == $amount}{
if {$ns_write_p}{ ns_write "<li><font color=red>Error: Didn't find a value for expense amount. Skipping record.</font>\n"}
continue
}
# Provider is mandatory
if {"" == $provider_id}{
if {$ns_write_p}{
ns_write "<li><font color=red>Error: We have found an empty 'Provider' in line $cnt.<br>Skipped record. Please correct the CSV file.</font>\n"
}
continue
}
# expense_type_id is mandatory
if {"" == $expense_type_id}{
if {$ns_write_p}{
ns_write "<li><font color=red>Error: We have found an empty 'Expense Type ID' in line $cnt.<br>
Skipped record. Please correct the CSV file.</font>\n"
# Update bundle_id if expense has been part of a bundle
if {"" != $bundle_id_old }{
if {[catch{
# Note: We have used im_costs::investment_id to store the cost_id of the expense bundle. To make sure investment_id hasn't been used otherwise, we also check the cost_type
set bundle_id_new [db_string get_bundle_id "select cost_id from im_costs where investment_id = :bundle_id_old and cost_type_id = [im_cost_type_expense_bundle]" -default 0]
if { 0 != $bundle_id_new }{
if {$ns_write_p}{ ns_write "<li>Setting expense bundle (id: $bundle_id_new) for expense (id: $cost_id)\n</li>\n"}
db_dml set_bundle_id "update im_expenses set bundle_id = :bundle_id_new where expense_id = :cost_id"
}else{
if {$ns_write_p}{ ns_write "<li><font color=red>Error: Can't find Expense Bundle for bundle_id: $bundle_id_old - Expense will not be assigned to bundle</font>\n"}
}
} err_msg]}{
if {$ns_write_p}{ ns_write "<li><font color=red>Error: Updating expense bundle:<br><pre>$err_msg</pre></font>\n"}
}
}
# Write Audit Trail
im_audit -object_id $cost_id -action after_create
}else{
if {$ns_write_p}{ ns_write "<li>Cost already exists: name='$cost_name', nr='$cost_nr', id='$cost_id'</li>\n"}
if { !$overwrite_existing_invoice_attributes_p }{
if {$ns_write_p}{ ns_write "<li>You have choosen not to overwrite/update already existing objects. Skipping record.</li>\n"}
continue
}
}
if {$ns_write_p}{ ns_write "<li>Going to update the cost.\n"}
if {[catch{
db_dml update_cost "
if {$ns_write_p}{ ns_write "<li>Cost already exists: name='$cost_name', nr='$cost_nr', id='$cost_id', going to update the cost.\n</li>\n"}
set cost_id [db_string cost_id "select cost_id from im_costs where lower(trim(cost_name)) = lower(trim(:expense_name))" -default ""]
if {"" ne $cost_id && !$overwrite_existing_expense_bundles_p }{
if {$ns_write_p}{
ns_write "<li><font color=red>Error: We have found an Expense Bundle named '$cost_name' and will not overwrite any of attributes.<br>Skipped record</font>\n"
ns_write "<li><font color=red>Error: We have found an Expense Bundle named '$expense_name' and will not overwrite any of attributes.<br>Skipped record</font>\n</li>"