# /packages/intranet-leaflet/www/country.tcl
#
# Copyright (C) 2003-2011 ]project-open[
#
# All rights reserved. Please check
# https://www.project-open.com/license/ for details.

ad_page_contract {
    Show CRM IP statistics for one country
    @author frank.bergmann@project-open.com
} {
    { diagram_country_code "" }
    { diagram_width 1000 }
    { diagram_height 500 }
    { diagram_title "Country CRM Activity" }
    { diagram_min_start_date "2010-01-01" }
    { diagram_default_interval "all_time" }
    { diagram_default_fact "cnt" }
}


# ----------------------------------------------------
# Diagram Setup
# ----------------------------------------------------

im_sencha_extjs_load_libraries

# Create a random ID for the diagram
set diagram_rand [expr {round(rand() * 100000000.0)}]
set diagram_id "country_crm_interaction_type_$diagram_rand"
set default_currency [im_parameter -package_id [im_package_cost_id] "DefaultCurrency" "" "EUR"]

# Get the list of all interaction types
set type_sql "
	select	im_category_from_id(c.category_id) as type
	from	im_categories c
	where	c.category_type = 'Intranet CRM Interaction' and
		c.enabled_p = 't' and
		c.category_id not in (0)
	order by
		coalesce(sort_order, category_id)
"

set type_sql "
select	im_category_from_id(type_id) as type
from	(select	count(*) as cnt,
		oiac.interaction_type_id as type_id
	from	tmp_crm_online_interactions_aggregate_country oiac
	group by oiac.interaction_type_id
	) t
where	cnt > 100
order by cnt DESC
"


set type_list [db_list type_list $type_sql]
set first_type [lindex $type_list 0]
# ad_return_complaint 1 $type_list



set type_list_json "\['[join $type_list "', '"]'\]"

# The header of the Sencha store:
set header_list [linsert $type_list 0 "Date"]
set header_json "\['[join $header_list "', '"]'\]"

# Show Axis only until 1st of current month.
# Everything within the current month is vague,
# because invoices are probably not yet written...
set axis_from_date [db_string to_date "select to_char(now()::date-31, 'YYYY-MM-01')"]
set axis_to_date [db_string to_date "select to_char(now(), 'YYYY-MM-01')"]
