﻿Clear V[myfilter] 
if V[filter_customer]
  Set V[myfilter] "SoldToParty eq '&V[filter_customer]' "
endif

if V[filter_salesorder]
  
  if V[myfilter]
    Set V[myfilter] "&V[myfilter] and"
  endif
  
  Set V[myfilter] "&V[myfilter] SalesOrder eq '&V[filter_salesorder]' "
endif

if V[filter_reference]
  
  if V[myfilter]
    Set V[myfilter] "&V[myfilter] and"
  endif
  
  Set V[myfilter] "&V[myfilter] contains(PurchaseOrderByCustomer,'&V[filter_reference]')"
endif

if V[filter_date1] or V[filter_date2]
  
  if V[myfilter]
    Set V[myfilter] "&V[myfilter] and"
  endif
  
  if V[filter_date1] and  V[filter_date2]
    Set V[myfilter] "&V[myfilter] SalesOrderDate ge &V[filter_date1] and SalesOrderDate le &V[filter_date2]"
  endif 
  
  if V[filter_date1] and not V[filter_date2]
    Set V[myfilter] "&V[myfilter] SalesOrderDate eq &V[filter_date1]"
  endif 
  
  if not V[filter_date1] and V[filter_date2]
    Set V[myfilter] "&V[myfilter] SalesOrderDate le &V[filter_date2]"
  endif 
endif

if V[filter_amount1] or V[filter_amount2]
  
  if V[myfilter]
    Set V[myfilter] "&V[myfilter] and"
  endif
  
  if V[filter_amount1] and  V[filter_amount2]
    Set V[myfilter] "&V[myfilter] TotalNetAmount ge &V[filter_amount1] and TotalNetAmount le &V[filter_amount2]"
  endif 
  
  if V[filter_amount1] and not V[filter_amount2]
    Set V[myfilter] "&V[myfilter] TotalNetAmount eq &V[filter_amount1]"
  endif 
  
  if not V[filter_amount1] and V[filter_amount2]
    Set V[myfilter] "&V[myfilter] TotalNetAmount le &V[filter_amount2]"
  endif 
endif

if V[filter_currency]
  
  if V[myfilter]
    Set V[myfilter] "&V[myfilter] and"
  endif
  
  Set V[myfilter] "&V[myfilter] TransactionCurrency eq '&V[filter_currency]' "
endif