Commit 449199b0 authored by Frank Bergmann's avatar Frank Bergmann

- Added import of im_invoice_items

  in the same file as the im_invoices
parent 8d3939e9
......@@ -375,6 +375,35 @@ ad_proc -public im_csv_import_parser_cost_center {
}
}
ad_proc -public im_csv_import_parser_material {
{-parser_args "" }
arg
} {
Parses a material name or nr into a material_id
} {
# Empty input - empty output
if {"" == $arg} { return [list "" ""] }
# Parse the category
set arg [string trim [string tolower $arg]]
set ccids [db_list ccid1 "
select material_id
from im_materials
where lower(material_nr) = :arg OR
lower(material_name) = :arg
order by material_id
"]
set result [lindex $ccids 0]
if {"" == $result} {
return [list "" "Material parser: We did not find any material nr or name matching the value='$arg'."]
} else {
return [list $result ""]
}
}
ad_proc -public im_csv_import_parser_hard_coded {
{-parser_args "" }
arg
......
......@@ -33,11 +33,18 @@ ad_proc -public im_csv_import_guess_im_company { } {} {
ad_proc -public im_csv_import_guess_im_invoice { } {} {
set mapping {
{project_id "Project Nr" project_parent_nrs ""}
{project_id "Project Nrs" project_parent_nrs ""}
{project_id "Cost Project Nrs" project_parent_nrs ""}
{cost_name "Name" no_change ""}
{cost_name "Cost Name" no_change ""}
{cost_nr "Nr" no_change ""}
{cost_nr "Cost Nr" no_change ""}
{cost_center_id "Cost Center" cost_center ""}
{cost_status_id "Status" category "Intranet Cost Status"}
{cost_status_id "Cost Status" category "Intranet Cost Status"}
{cost_type_id "Type" category "Intranet Cost Type"}
{cost_type_id "Cost Type" category "Intranet Cost Type"}
{customer_id "Customer" company_name ""}
{provider_id "Provider" company_name ""}
{effective_date "Effective Date" date ""}
......@@ -53,7 +60,15 @@ ad_proc -public im_csv_import_guess_im_invoice { } {} {
{company_contact_id "Customer Contact" user_name ""}
{cause_object_id "Cause Object" user_name ""}
{payment_method_id "Payment Method" category "Intranet Invoice Payment Method"}
{template_id "Template" category "Intranet Invoice Templates"}
{invoice_office_id "Invoice Office" office_name ""}
{item_name "Item Name" no_change ""}
{sort_order "Item Sort Order" number ""}
{item_units "Item Units" number ""}
{price_per_unit "Item Price" number ""}
{price_per_unit "Price Per Unit" number ""}
{item_uom_id "Item UoM" category "Intranet UoM"}
{item_material_id "Item Material" material ""}
}
return $mapping
}
......@@ -160,25 +175,26 @@ ad_proc -public im_csv_import_parsers {
switch $object_type {
im_project - im_company - im_conf_item - im_cost - im_invoice - im_risk - im_timesheet_task - im_ticket - im_hour {
set parsers {
no_change "No Change"
hard_coded "Hard Coded Functionality"
boolean "Boolean"
category "Category ID from Category Name"
company_name "Company ID from Company Name"
conf_item_parent_nrs "Conf Item Parent Nrs"
cost_center "Cost Center"
date "Date (generic)"
date_american "Date US (MM/DD/YYYY)"
date_european "Date European (DD.MM.YYYY)"
date_european_dashes "Date ISO (YYYY-MM-DD)"
date_american "Date US (MM/DD/YYYY)"
hard_coded "Hard Coded Functionality"
no_change "No Change"
material "Material"
number "Number (generic)"
number_european "Number European (20.000,00)"
number_american "Number US (20,000.00)"
category "Category ID from Category Name"
cost_center "Cost Center Parser"
project_nr "Project from Project Nr"
number_european "Number European (20.000,00)"
office_name "Office ID from Office Name"
project_name "Project from Project Name"
project_nr "Project from Project Nr"
project_parent_nrs "Project from Parent Nrs"
company_name "Company ID from Company Name"
office_name "Office ID from Office Name"
user_name "User ID from email, username or full name"
conf_item_parent_nrs "Conf Item Parent Nrs"
}
}
im_membership {
......@@ -208,7 +224,6 @@ ad_proc -public im_csv_import_object_fields {
} {
Returns a list of database columns for the specified object type.
} {
# Special case: im_hour is not an object
if { "im_hour" == $object_type } {
return "project_id project_nr project_nr_path user_id day hours note"
......@@ -276,12 +291,19 @@ ad_proc -public im_csv_import_object_fields {
lappend selected_tables $table_name
incr cnt
}
return [lsort $selected_columns]
}
# Check for static mapping additions
set static_mapping_lol {}
catch { set static_mapping_lol [im_csv_import_guess_$object_type] }
foreach static_mapping_tuple $static_mapping_lol {
set field_name [lindex $static_mapping_tuple 0]
if {[lsearch $selected_columns $field_name] < 0} {
lappend selected_columns $field_name
}
}
return [lsort $selected_columns]
}
# ---------------------------------------------------------------------
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -116,8 +116,10 @@ if {$ns_write_p} {
# ------------------------------------------------------------
set cnt 1
set old_cost_name ""
foreach csv_line_fields $values_list_of_lists {
incr cnt
# if {$cnt > 50} { break }
if {$ns_write_p} { ns_write "</ul><hr><ul>\n" }
if {$ns_write_p} { ns_write "<li>Starting to parse line $cnt\n" }
......@@ -164,6 +166,14 @@ foreach csv_line_fields $values_list_of_lists {
set invoice_office ""
set invoice_office_id ""
# im_invoice_items
set sort_order ""
set item_name ""
set item_units ""
set item_uom_id ""
set price_per_unit ""
set item_material_id ""
foreach attribute_name $attribute_names {
set $attribute_name ""
}
......@@ -234,7 +244,105 @@ foreach csv_line_fields $values_list_of_lists {
if {$ns_write_p} { ns_write "<li>Before cost_nr: name=$cost_name, nr=$cost_nr</font>\n" }
# Use the current cost_name in the next line.
# That's useful for invoice_items right after the respective Financial Document
if {"" ne $cost_name} { set old_cost_name $cost_name }
# -------------------------------------------------------
# Check if the cost already exists
# Check if we find the same cost by name with it's project.
set cost_id [db_string cost_id "select cost_id from im_costs where lower(trim(cost_nr)) = lower(trim(:cost_nr))" -default ""]
if {"" eq $cost_id} {
set cost_id [db_string cost_id "select cost_id from im_costs where lower(trim(cost_name)) = lower(trim(:cost_name))" -default ""]
}
if {$ns_write_p} { ns_write "<li>id=$cost_id, name='$cost_name', nr='$cost_nr'\n" }
# -------------------------------------------------------
# Check if we've got an invoice item
set item_field_count 0
if {"" ne $item_name} { incr item_field_count }
if {"" ne $item_units} { incr item_field_count }
if {"" ne $item_uom_id} { incr item_field_count }
if {"" ne $price_per_unit} { incr item_field_count }
# We should get either 1 (cost item) or 5 (line).
# Everything else is something incomplete
if {$item_field_count > 0} {
# Check for an incomplete line
if {$item_field_count < 4} {
if {$ns_write_p} {
ns_write "<li><font color=red>Error: We have found line $cnt with data from an invoice line<br>
including Cost Name, Item name, Item Units, Item UoM and Price per Unit.<br>
However, some of these required fields are missing. Please complete the line.</font>\n"
}
continue
}
# Special logic for invoice_items:
# If no cost_name was specified, just use the cost_name from the last lin
if {"" eq $cost_name} {
set cost_name $old_cost_name
set cost_id [db_string cost_id "select cost_id from im_costs where lower(trim(cost_name)) = lower(trim(:cost_name))" -default ""]
}
# We've got a complete invoice_item.
# Check for identity
if {"" eq $cost_id} {
if {$ns_write_p} {
ns_write "<li><font color=red>Error: We didn't find a financial document to reference</font>\n"
}
continue
}
set item_id [db_string item_id "select min(item_id) from im_invoice_items where invoice_id = :cost_id and item_name = :item_name" -default ""]
if {"" eq $currency} { set currency [db_string cur "select currency from im_costs where cost_id = :cost_id" -default $default_currency] }
if {"" eq $sort_order} {
# Create a new sort order based on the existing number of invoice_items
set sort_order [db_string sort_order "select 1+count(*) from im_invoice_items where invoice_id = :cost_id"]
}
if {"" eq $item_id} {
# Item doesn't exist yet - insert
if {$ns_write_p} { ns_write "<li>Going to create line: cost_name='$cost_name', item_name='$item_name'\n" }
db_dml insert_item "
insert into im_invoice_items (
item_id,
invoice_id, sort_order, item_name, item_units, item_uom_id, price_per_unit, item_material_id, currency
) values (
nextval('im_invoice_items_seq'),
:cost_id, :sort_order, :item_name, :item_units, :item_uom_id, :price_per_unit, :item_material_id, :currency
)
"
} else {
# Item already exists - update
if {$ns_write_p} { ns_write "<li>Going to update line: cost_name='$cost_name', item_name='$item_name'\n" }
db_dml update_item "
update im_invoice_items set
invoice_id = :cost_id,
sort_order = :sort_order,
item_name = :item_name,
item_units = :item_units,
item_uom_id = :item_uom_id,
price_per_unit = :price_per_unit,
item_material_id = :item_material_id,
currency = :currency
where
item_id = :item_id;
"
}
# Skip the rest of the code dealing with im_invoices
continue
}
# ------------------------------------------------------------------------
# We have found a line with a normal invoice (quote, invoice, purchase order, ...)
#
# -------------------------------------------------------
# Specific field transformations
......@@ -253,13 +361,6 @@ foreach csv_line_fields $values_list_of_lists {
set cost_nr [regsub {[^a-z0-9_]} [string trim [string tolower $cost_name]] "_"]
}
# 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]
}
# Type is a required field
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>
......@@ -268,6 +369,12 @@ foreach csv_line_fields $values_list_of_lists {
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]
}
# Customer and provider are required
if {"" == $provider_id} {
if {[im_category_is_a $cost_type_id [im_cost_type_customer_doc]]} { set provider_id [im_company_internal] }
......@@ -313,25 +420,6 @@ foreach csv_line_fields $values_list_of_lists {
set currency $default_currency
}
# -------------------------------------------------------
# Check if the cost already exists
# Check if we find the same cost by name with it's project.
set cost_id [db_string cost_id "
select cost_id from im_costs
where lower(trim(cost_nr)) = lower(trim(:cost_nr))
" -default ""]
if {"" eq $cost_id} {
set cost_id [db_string cost_id "
select cost_id from im_costs
where lower(trim(cost_name)) = lower(trim(:cost_name))
" -default ""]
}
if {$ns_write_p} { ns_write "<li>id=$cost_id, name='$cost_name', nr='$cost_nr'\n" }
# -------------------------------------------------------
# Create a new cost if necessary
if {"" == $cost_id} {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment