Commit 3d66077b authored by eicom's avatar eicom

- added stuff

parent a8475b4e
-- /packages/intranet-ubl/sql/postgresql/intranet-ubl-create.sql
--
-- Copyright (c) 2003-2007 ]project-open[
--
-- All rights reserved. Please check
-- http://www.project-open.com/license/ for details.
--
-- @author all@devcon.project-open.com
-----------------------------------------------------------
-- Universal Business Language (UBL) Interface
--
-- This module just exports (future: imports) XML documents
-- in UBL formal
......@@ -107,58 +107,116 @@ ad_proc -private im_package_ubl_invoice2ubl_v10_eicom {
if {![db_0or1row project_info "
select c.*,
i.*,
to_char(o.creation_date, 'YYYY-MM-DD') as issue_date_pretty,
paymeth.category_description as payment_method_desc,
cust.company_name as customer_name,
ocust.address_line1 as customer_address_line1,
ocust.address_line2 as customer_address_line2,
ocust.address_city as customer_address_city,
ocust.address_state as customer_address_state,
ocust.address_postal_code as customer_address_postal_code,
upper(ocust.address_country_code) as customer_address_country_code,
cust_countries.country_name as customer_address_country_name,
cust.main_office_id as customer_main_office_id,
i.invoice_office_id as invoice_office_id,
coalesce(
i.company_contact_id,
cust.accounting_contact_id,
cust.primary_contact_id
) as cust_contact_id,
prov.company_name as provider_name,
oprov.address_line1 as provider_address_line1,
oprov.address_line2 as provider_address_line2,
oprov.address_city as provider_address_city,
oprov.address_state as provider_address_state,
oprov.address_postal_code as provider_address_postal_code,
upper(oprov.address_country_code) as provider_address_country_code,
prov_countries.country_name as provider_address_country_name,
im_name_from_user_id(i.company_contact_id) as customer_contact_name,
cust_contact.work_phone as customer_contact_work_phone,
im_name_from_user_id(i.company_contact_id) as provider_contact_name,
prov_contact.work_phone as provider_contact_work_phone,
prov.main_office_id as provider_main_office_id,
coalesce(
prov.accounting_contact_id,
prov.primary_contact_id
) as prov_contact_id,
paymeth.category_description as payment_method_desc
to_char(o.creation_date, 'YYYY-MM-DD') as issue_date_pretty
from
im_costs c,
im_invoices i
LEFT OUTER JOIN im_categories paymeth ON (i.payment_method_id = paymeth.category_id),
acs_objects o,
im_companies cust,
im_companies prov,
im_offices ocust
LEFT OUTER JOIN country_codes cust_countries ON (ocust.address_country_code = cust_countries.iso),
im_offices oprov
LEFT OUTER JOIN country_codes prov_countries ON (oprov.address_country_code = prov_countries.iso),
users_contact cust_contact,
users_contact prov_contact
im_companies prov
where
i.invoice_id = :invoice_id
and i.invoice_id = c.cost_id
and i.invoice_id = o.object_id
and c.customer_id = cust.company_id
and c.provider_id = prov.company_id
and ocust.office_id = i.invoice_office_id
and oprov.office_id = prov.main_office_id
and i.company_contact_id = cust_contact.user_id
and prov.accounting_contact_id = prov_contact.user_id
"]} {
# ad_return_complaint 1 [lang::message::lookup "" intranet-ubl.Financial_Document_Not_Found "Didn't find financial document \#%invoice_id%"]
return ""
ad_return_complaint 1 [lang::message::lookup "" intranet-ubl.Financial_Document_Not_Found "Didn't find financial document \#%invoice_id%"]
ad_script_abort
}
set cust_contact_sql "
select
im_name_from_user_id(uc.user_id) as customer_contact_name,
uc.work_phone as customer_contact_work_phone
from
users_contact uc
where
uc.user_id = :cust_contact_id
"
if {![db_0or1row cust_contact $cust_contact_sql]} {
ad_return_complaint 1 [lang::message::lookup "" intranet-ubl.Customer_Contact_for_Financial_Document_Not_Found "Didn't find a Customer Contact for financial document \#%invoice_id%"]
ad_script_abort
}
set prov_contact_sql "
select
im_name_from_user_id(uc.user_id) as provider_contact_name,
uc.work_phone as provider_contact_work_phone
from
users_contact uc
where
uc.user_id = :prov_contact_id
"
if {![db_0or1row prov_contact $prov_contact_sql]} {
ad_return_complaint 1 [lang::message::lookup "" intranet-ubl.Provider_Contact_for_Financial_Document_Not_Found "Didn't find a Provider Contact for financial document \#%invoice_id%"]
ad_script_abort
}
# Office Logic: Take the customers and provider's main office,
# unless there has been a specific delivery office specified for the customer.
set cust_office_id $customer_main_office_id
set prov_office_id $provider_main_office_id
if {"" != $invoice_office_id} { set cust_office_id $invoice_office_id}
set prov_office_sql "
select
oprov.address_line1 as provider_address_line1,
oprov.address_line2 as provider_address_line2,
oprov.address_city as provider_address_city,
oprov.address_state as provider_address_state,
oprov.address_postal_code as provider_address_postal_code,
upper(oprov.address_country_code) as provider_address_country_code,
prov_countries.country_name as provider_address_country_name
from
im_offices oprov
LEFT OUTER JOIN country_codes prov_countries ON (oprov.address_country_code = prov_countries.iso)
where
oprov.office_id = :prov_office_id
"
if {![db_0or1row prov_office $prov_office_sql]} {
ad_return_complaint 1 [lang::message::lookup "" intranet-ubl.Provider_Office_for_Financial_Document_Not_Found "Didn't find Provider's office for financial document \#%invoice_id%"]
ad_script_abort
}
set cust_office_sql "
select
ocust.address_line1 as customer_address_line1,
ocust.address_line2 as customer_address_line2,
ocust.address_city as customer_address_city,
ocust.address_state as customer_address_state,
ocust.address_postal_code as customer_address_postal_code,
upper(ocust.address_country_code) as customer_address_country_code,
cust_countries.country_name as customer_address_country_name
from
im_offices ocust
LEFT OUTER JOIN country_codes cust_countries ON (ocust.address_country_code = cust_countries.iso)
where
ocust.office_id = :cust_office_id
"
if {![db_0or1row cust_office $cust_office_sql]} {
ad_return_complaint 1 [lang::message::lookup "" intranet-ubl.Customer_Office_for_Financial_Document_Not_Found "Didn't find customer office for financial document \#%invoice_id%"]
ad_script_abort
}
......
......@@ -26,259 +26,6 @@ set user_id [ad_maybe_redirect_for_registration]
set today [db_string today "select to_char(now(), 'YYYY-MM-DD')"]
set invoice_url "/intranet-invoices/view?invoice_id=$invoice_id"
# rounding precision can be between 2 (USD,EUR, ...)
# and -5 (Old Turkish Lira, ...).
set rounding_precision 2
set required_field "<font color=red size=+1><B>*</B></font>"
set rounding_factor [expr exp(log(10) * $rounding_precision)]
set rf $rounding_factor
# Locale
set locale "en_US"
# set locale [lang::user::locale]
set cur_format [im_l10n_sql_currency_format]
set vat_format $cur_format
set tax_format $cur_format
# ---------------------------------------------------------------
# Get information about the financial document ("invoice")
# ---------------------------------------------------------------
if {![db_0or1row project_info "
select c.*,
i.*,
to_char(o.creation_date, 'YYYY-MM-DD') as issue_date_pretty,
cust.company_name as customer_name,
ocust.address_line1 as customer_address_line1,
ocust.address_line2 as customer_address_line2,
ocust.address_city as customer_address_city,
ocust.address_state as customer_address_state,
ocust.address_postal_code as customer_address_postal_code,
ocust.address_country_code as customer_address_country_code,
prov.company_name as provider_name,
oprov.address_line1 as provider_address_line1,
oprov.address_line2 as provider_address_line2,
oprov.address_city as provider_address_city,
oprov.address_state as provider_address_state,
oprov.address_postal_code as provider_address_postal_code,
oprov.address_country_code as provider_address_country_code,
im_name_from_user_id(i.company_contact_id) as customer_contact_name,
cust_contact.work_phone as customer_contact_work_phone,
im_name_from_user_id(i.company_contact_id) as provider_contact_name,
prov_contact.work_phone as provider_contact_work_phone,
paymeth.category_description as payment_method_desc
from
im_costs c,
im_invoices i
LEFT OUTER JOIN im_categories paymeth ON (i.payment_method_id = paymeth.category_id),
acs_objects o,
im_companies cust,
im_companies prov,
im_offices ocust,
im_offices oprov,
users_contact cust_contact,
users_contact prov_contact
where
i.invoice_id = :invoice_id
and i.invoice_id = c.cost_id
and i.invoice_id = o.object_id
and c.customer_id = cust.company_id
and c.provider_id = prov.company_id
and ocust.office_id = i.invoice_office_id
and oprov.office_id = prov.main_office_id
and i.company_contact_id = cust_contact.user_id
and prov.accounting_contact_id = prov_contact.user_id
"]} {
ad_return_complaint 1 [lang::message::lookup "" intranet-ubl.Financial_Document_Not_Found "Didn't find financial document \#%invoice_id%"]
return
}
# Calculate grand total based on the same inner SQL
db_1row calc_grand_total ""
set subtotal_pretty [lc_numeric [im_numeric_add_trailing_zeros [expr $subtotal+0] $rounding_precision] "" $locale]
set vat_amount_pretty [lc_numeric [im_numeric_add_trailing_zeros [expr $vat_amount+0] $rounding_precision] "" $locale]
set tax_amount_pretty [lc_numeric [im_numeric_add_trailing_zeros [expr $tax_amount+0] $rounding_precision] "" $locale]
set grand_total_pretty [lc_numeric [im_numeric_add_trailing_zeros [expr $grand_total+0] $rounding_precision] "" $locale]
set total_due_pretty [lc_numeric [im_numeric_add_trailing_zeros [expr $total_due+0] $rounding_precision] "" $locale]
set discount_perc_pretty $discount_perc
set surcharge_perc_pretty $surcharge_perc
# Get the right outer tag name, depending on the document type
set document_type "error"
switch $cost_type_id {
3700 { set document_type "Invoice" }
3702 { set document_type "Quote" }
3704 { set document_type "Invoice" }
3706 { set document_type "Order" }
3724 { set document_type "DelNote" }
}
# ---------------------------------------------------------------
# Create the XML
# ---------------------------------------------------------------
set version "1.12"
set view_index 0
# ---------------------------------------------------------------
# Main Document Node
set doc [dom createDocument $document_type]
set main_node [$doc documentElement]
$main_node setAttribute xmlns:cbc "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-1.0"
$main_node setAttribute xmlns:cac "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-1.0"
$main_node setAttribute xmlns:cur "urn:oasis:names:specification:ubl:schema:xsd:CurrencyCode-1.0"
$main_node setAttribute xmlns:xsi "http://www.w3.org/2001/XMLSchema-instance"
$main_node setAttribute xmlns "urn:oasis:names:specification:ubl:schema:xsd:Invoice-1.0"
$main_node setAttribute xsi:schemaLocation "urn:oasis:names:specification:ubl:schema:xsd:Invoice-1.0 ../../xsd/maindoc/UBL-Invoice-1.0.xsd"
$main_node appendXML "<ID>$cost_name</ID>"
$main_node appendXML "<cbc:IssueDate>$issue_date_pretty</cbc:IssueDate>"
$main_node appendXML "<TaxPointDate>$issue_date_pretty</TaxPointDate>"
$main_node appendXML "
<cac:OrderReference>
<cac:BuyersID>$customer_id</cac:BuyersID>
<cac:SellersID>$provider_id</cac:SellersID>
<cbc:IssueDate>$issue_date_pretty</cbc:IssueDate>
</cac:OrderReference>"
$main_node appendXML "
<cac:BuyerParty>
<cac:Party>
<cac:PartyName>
<cbc:Name>$customer_name</cbc:Name>
</cac:PartyName>
<cac:Address>
<cbc:StreetName>$customer_address_line1 $customer_address_line2</cbc:StreetName>
<cbc:CityName>$customer_address_city</cbc:CityName>
<cbc:PostalZone>$customer_address_postal_code</cbc:PostalZone>
<cac:CountrySubentityCode>$customer_address_state</cac:CountrySubentityCode>
</cac:Address>
</cac:Party>
<cac:AccountsContact>
<cbc:Name>$customer_contact_name</cbc:Name>
<cbc:Telephone>$customer_contact_work_phone</cbc:Telephone>
</cac:AccountsContact>
</cac:BuyerParty>
"
$main_node appendXML "
<cac:SellerParty>
<cac:Party>
<cac:PartyName>
<cbc:Name>$provider_name</cbc:Name>
</cac:PartyName>
<cac:Address>
<cbc:StreetName>$provider_address_line1 $provider_address_line2</cbc:StreetName>
<cbc:CityName>$provider_address_city</cbc:CityName>
<cbc:PostalZone>$provider_address_postal_code</cbc:PostalZone>
<cac:CountrySubentityCode>$provider_address_state</cac:CountrySubentityCode>
</cac:Address>
</cac:Party>
<cac:AccountsContact>
<cbc:Name>$provider_contact_name</cbc:Name>
<cbc:Telephone>$provider_contact_work_phone</cbc:Telephone>
</cac:AccountsContact>
</cac:SellerParty>
"
$main_node appendXML "
<cac:PaymentTerms>
<cbc:Note>$payment_method_desc</cbc:Note>
<cac:ReferenceEventCode>!</cac:ReferenceEventCode>
</cac:PaymentTerms>
"
$main_node appendXML "
<cac:TaxTotal>
<cbc:TotalTaxAmount amountCurrencyCodeListVersionID=\"0.3\" amountCurrencyID=\"$currency\">$vat_amount_pretty</cbc:TotalTaxAmount>
<cac:TaxSubTotal>
<cbc:TaxableAmount amountCurrencyCodeListVersionID=\"0.3\" amountCurrencyID=\"$currency\">$amount</cbc:TaxableAmount>
<cbc:TaxAmount amountCurrencyCodeListVersionID=\"0.3\" amountCurrencyID=\"$currency\">$vat_amount_pretty</cbc:TaxAmount>
<cac:TaxCategory>
<cac:ID>STD</cac:ID>
<cbc:Percent>$vat</cbc:Percent>
<cac:TaxScheme>
<cac:TaxTypeCode>VAT</cac:TaxTypeCode>
</cac:TaxScheme>
</cac:TaxCategory>
</cac:TaxSubTotal>
</cac:TaxTotal>
"
$main_node appendXML "
<cac:LegalTotal>
<cbc:LineExtensionTotalAmount amountCurrencyCodeListVersionID=\"0.3\"
amountCurrencyID=\"$currency\">$subtotal_pretty</cbc:LineExtensionTotalAmount>
<cbc:TaxExclusiveTotalAmount amountCurrencyCodeListVersionID=\"0.3\"
amountCurrencyID=\"$currency\">$subtotal_pretty</cbc:TaxExclusiveTotalAmount>
<cbc:TaxInclusiveTotalAmount amountCurrencyCodeListVersionID=\"0.3\"
amountCurrencyID=\"$currency\">$grand_total_pretty</cbc:TaxInclusiveTotalAmount>
</cac:LegalTotal>
"
set ctr 1
db_foreach invoice_items {} {
set amount_pretty [lc_numeric [im_numeric_add_trailing_zeros [expr $amount+0] $rounding_precision] "" $locale]
set item_units_pretty [lc_numeric [expr $item_units+0] "" $locale]
set price_per_unit_pretty [lc_numeric [im_numeric_add_trailing_zeros [expr $price_per_unit+0] $rounding_precision] "" $locale]
$main_node appendXML "
<cac:${document_type}Line>
<cac:ID>$ctr</cac:ID>
<cbc:InvoicedQuantity quantityUnitCode=\"$item_uom\">$item_units_pretty</cbc:InvoicedQuantity>
<cbc:LineExtensionAmount amountCurrencyCodeListVersionID=\"0.3\"
amountCurrencyID=\"$currency\">[string trim $amount_pretty]</cbc:LineExtensionAmount>
<cac:OrderLineReference>
<cac:BuyersLineID>$ctr</cac:BuyersLineID>
</cac:OrderLineReference>
<cac:Item>
<cbc:Description>$item_name</cbc:Description>
<cac:BuyersItemIdentification>
<cac:ID>$ctr</cac:ID>
</cac:BuyersItemIdentification>
<cac:SellersItemIdentification>
<cac:ID>32145-12</cac:ID>
</cac:SellersItemIdentification>
<cac:BasePrice>
<cbc:PriceAmount amountCurrencyCodeListVersionID=\"0.3\"
amountCurrencyID=\"$currency\">$price_per_unit_pretty</cbc:PriceAmount>
</cac:BasePrice>
</cac:Item>
</cac:${document_type}Line>
"
incr ctr
}
ns_return 200 text/xxx [$doc asXML -indent 2]
# ns_return 200 text/xxx [$doc asXML -indent 2 -escapeNonASCII]
set xml [im_package_ubl_invoice2ubl_v10_eicom -invoice_id $invoice_id]
ns_return 200 text/xxx $xml
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