﻿Set V[va01_pdf_errcnt] 0
Clear V[va01_pdf_itemstab.stat.rowselection.*]

// Check name
if V[va01_pdf_name1=guixt]
  Set V[va01_pdf_errcnt] "&V[va01_pdf_errcnt]" + 1
  StatusMessage addString="E: Invalid company name!" title="&V[va01_pdf_errcnt] errors detected!"
endif

// Check items
Set V[va01_pdf_chk_item] 1
label chk_item
Set V[va01_pdf_chk_inr] "&V[va01_pdf_itemstab.id.&V[va01_pdf_chk_item]]"
Set V[va01_pdf_chk_mkt] "&V[va01_pdf_itemstab.text.&V[va01_pdf_chk_item]]"
Set V[va01_pdf_chk_unt] "&V[va01_pdf_itemstab.unit.&V[va01_pdf_chk_item]]"

// Check if item exists
CreateTable V[va01_pdf_chk_mat] matnr maktx meins

Set V[table] "MAKT JOIN MARA ON MAKT~MATNR = MARA~MATNR"
Set V[condition] "MARA~MATNR = @MATNR AND MAKT~SPRAS = '&V[va01_pdf_chk_lang.lang.1]'"
Set V[domname] "MATNR"
Set V[domvalue] "&V[va01_pdf_chk_inr]"
Set V[fields] "MARA~MATNR, MAKT~MAKTX, MARA~MEINS"

// Select data from tables MAKT and MARA
Call /guixt/dbselect cache="session" _
  export.table="table" _
  export.condition="condition" _
  export.fields="fields" _
  export.domname1="domname" _
  export.domvalue1="va01_pdf_chk_inr" _
  import.values="va01_pdf_chk_mat"

// Perform item existence and coherence checks
if not V[va01_pdf_chk_mat.matnr.1=&V[va01_pdf_chk_inr]]
  Set V[va01_pdf_errcnt] "&V[va01_pdf_errcnt]" + 1
  Set V[va01_pdf_itemstab.stat.rowselection.&V[va01_pdf_chk_item]] "X"
  StatusMessage addString="E: Item &V[va01_pdf_chk_inr] does not exist!" title="&V[va01_pdf_errcnt] errors detected!"
else
  // Check if the item description deviates from the stored description
  if V[va01_pdf_chk_lang.lang.1] and not V[va01_pdf_chk_mat.maktx.1=&V[va01_pdf_chk_mkt]]
    Set V[va01_pdf_errcnt] "&V[va01_pdf_errcnt]" + 1
    Set V[va01_pdf_itemstab.stat.rowselection.&V[va01_pdf_chk_item]] "X"
    StatusMessage addString="E: Name difference on item &V[va01_pdf_chk_inr]: Expected &V[va01_pdf_chk_mat.maktx.1], got &V[va01_pdf_chk_mkt]" title="&V[va01_pdf_errcnt] errors detected!" size=(0,90)
  endif
  // Translate unit
  CreateTable V[va01_pdf_chk_unit] unit
  Set V[table] "T006A"
  Set V[condition] "MSEHI = ( SELECT MSEHI FROM T006A WHERE SPRAS = '&V[va01_pdf_chk_lang.lang.1]' AND MSEH3 = '&V[va01_pdf_chk_unt]' ) AND SPRAS = '&V[_language]'"
  Set V[fields] "MSEH3"
  
  // select data from table T006A
  Call /guixt/dbselect cache="session" _
    export.table="table" _
    export.condition="condition" _
    export.fields="fields" _
    import.values="va01_pdf_chk_unit"
  // Check if the item unit deviates from the stored unit
  if not V[va01_pdf_chk_mat.meins.1=&V[va01_pdf_chk_unit.unit.1]]
    Set V[va01_pdf_errcnt] "&V[va01_pdf_errcnt]" + 1
    Set V[va01_pdf_itemstab.stat.rowselection.&V[va01_pdf_chk_item]] "X"
    StatusMessage addString="E: Unit difference on item &V[va01_pdf_chk_inr]: Expected &V[va01_pdf_chk_mat.meins.1], got &V[va01_pdf_chk_unit.unit.1]" title="&V[va01_pdf_errcnt] errors detected!" size=(0,90)
  endif
endif
Set V[va01_pdf_chk_item] "&V[va01_pdf_chk_item]" + 1
if not V[va01_pdf_chk_item>&V[va01_pdf_itemstab.rowcount]]
  goto chk_item
endif

Return "W: Check finished! &V[va01_pdf_errcnt] errors found." -statusline
