﻿// Determine years
Set V[zcso_year0]  &V[today_y]
Set V[zcso_year1] &V[zcso_year0] - 1
Set V[zcso_year2] &V[zcso_year0] - 2
Set V[zcso_year3] &V[zcso_year0] - 3
Set V[zcso_year4] &V[zcso_year0] - 4

// search condition
Set V[condition]  "KUNNR = '&V[zcso_kunnr]' and VKORG = '&V[zcso_vkorg]' and VTWEG = '&V[zcso_vtweg]' and SPART = '&V[zcso_spart]' and SPMON GE '&V[zcso_year4]01'"

// Clear result tables
Set text[r1] ""
Set text[r2] ""
Set text[r3] ""

// read VIS statistics table S001
Call /guixt/select in.Table="S001" in.Condition="&V[condition]" in.Fields="MATNR,SPMON,AENETWR"  table.V1table="r1" table.V2table="r2" table.V3table="r3"

// pass data to VB
CallVB charts.zcso.materialsales

// Clear result tables
Set text[r1] ""
Set text[r2] ""
Set text[r3] ""


// obtain all product numbers from VB
CallVB charts.zcso.products_to_guixt


// read product names
if text[zcso_products]
  
  
  Set text[ctab]  ""
  Set V[i] 1
  
  label next
  CopyText fromText="zcso_products" toString="matnr" line="&V[i]"
  if  Q[ok]
    if  V[i=1]
      Set V[condline]  "SPRAS = 'E' AND (MATNR = '&V[matnr]'"
    else
      Set V[condline]  "OR MATNR = '&V[matnr]'"
    endif
    
    CopyText fromString="condline" toText="ctab"  -appendline
    Set V[i] &V[i] + 1
    goto next
    
  endif
  
  if V[i>1]
    Set V[condline]  ")"
    CopyText fromString="condline" toText="ctab"  -appendline
  endif 
  
  Call /guixt/select in.table="MAKT" in.fields="MATNR,MAKTX"   table.conditiontable="ctab"  table.V1table="r1" table.V2table="r2"
  
endif

// product names to VB
CallVB charts.zcso.productnames_from_guixt


// Clear result and condition tables
//Set text[r1] ""
//Set text[r2] ""
//Set text[ctab] ""



