﻿// select customers from database
Set V[tabname] "KNA1 left outer join T005T on T005T~LAND1 = KNA1~LAND1"
Set V[fields] "kna1~kunnr,kna1~name1,kna1~ort01,t005t~landx"
Set V[orderby] "kna1~kunnr"

// build up where-condition for database select
Clear V[condition]

// kunnr selection
if V[range_kunnr.rowcount>0]
  Set V[condition] "kna1~kunnr in @range1 and"
else
  if V[kunnr_low] or V[kunnr_high]
    
    // add leading zeros if numeric
    process V[kunnr_low_int] = convert_kunnr_input "&V[kunnr_low]"
    process V[kunnr_high_int] = convert_kunnr_input "&V[kunnr_high]"
    
    if V[kunnr_high]
      Set V[condition] "kna1~kunnr between '&V[kunnr_low_int]' and '&V[kunnr_high_int]' and"
    else
      Set V[condition] "kna1~kunnr = '&V[kunnr_low_int]' and"
    endif
  endif
endif

// land1 selection
if V[range_land1.rowcount>0]
  Set V[condition] "&V[condition] kna1~land1 in @range2 and"
else
  if V[land1_low] 
    Set V[condition] "&V[condition] kna1~land1 = '&V[land1_low]' and"
  endif
endif

// add language selection for country name
Set V[condition] "&V[condition] t005t~spras = '&V[_language]'"

// select from database
Call /guixt/dbselect _
  export.table="tabname" _
  export.fields="fields" _
  export.orderby="orderby" _
  export.condition="condition" _
  export.range1="range_kunnr" _
  export.range2="range_land1" _
  import.values="customers"

// scroll to first row
Set V[customers.stat.firstvisiblerow] 1

Return

