﻿// create table variable with country name, region and customer count
CreateTable V[ctabr]  country region count

// selected country
Set V[country] "&V[ctab.country.&V[_tabrowabs]]"

// Set SQL parameters
Set V[table] "KNA1 join T005U on T005U~LAND1 = KNA1~LAND1 and T005U~BLAND = KNA1~REGIO AND T005U~SPRAS = '&V[_language]'"
Set V[fields] "KNA1~LAND1,T005U~BEZEI,COUNT(*) AS I1"
Set V[condition] "KNA1~LAND1 = '&V[country]'"
Set V[groupby] "KNA1~LAND1,T005U~BEZEI"
Set V[orderby] "I1 descending,KNA1~LAND1,T005U~BEZEI"

//  determine customer count per country 
Call "/guixt/dbselect" _
  export.table="table" _
  export.fields="fields" _
  export.condition="condition" _
  export.groupby="groupby" _ 
  export.orderby="orderby" _ 
  import.values="ctabr"


// temporary image file
Set V[imagefilename] "&%[TEMP]\mvta_chart.&V[today_y-m-d hms].png"


// statusmessage 
StatusMessage title="Please wait a little bit..." addString="Preparing regional chart for country &V[country]"

// start Excel and copy data
CallJS XL_chart "ctabr" "E:\MyCharts\charts.xlsm" "chartmapregions" "&V[imagefilename]"

// remove status message
StatusMessage -remove

// done
Return
