<%
'
'    Filename: mld_workflowrecord2.asp
'    Generated with CodeCharge 2.0.5
'    ASP 2.0 & Templates.ccp build 11/30/2001
'

'-------------------------------
' 
' mld_workflowrecord2 CustomIncludes Event begin
%>
<!-- #INCLUDE FILE="Common.asp" -->
<%
' mld_workflowrecord2 CustomIncludes Event end


'===============================
' Save Page and File Name available into variables
'-------------------------------
sFileName = "mld_workflowrecord2.asp"
sTemplateFileName = "mld_workflowrecord2.html"
'===============================


'===============================
' mld_workflowrecord2 PageSecurity begin
' mld_workflowrecord2 PageSecurity end
'===============================

'===============================
' mld_workflowrecord2 Open Event begin
' mld_workflowrecord2 Open Event end
'===============================

'===============================
' mld_workflowrecord2 OpenAnyPage Event begin
' mld_workflowrecord2 OpenAnyPage Event end
'===============================

'===============================
'Save the name of the form and type of action into the variables
'-------------------------------
sAction = GetParam("FormAction")
sForm = GetParam("FormName")
'===============================

' mld_workflowrecord2 Show begin
'===============================
' Perform the form's action
'-------------------------------
' Initialize error variables
'-------------------------------
sNextStepErr = ""

'-------------------------------
' Select the FormAction
'-------------------------------
Select Case sForm
  Case "NextStep"
    NextStepAction(sAction)
end select
'===============================

'===============================
' Display page
'-------------------------------
' Load HTML template for this page
'-------------------------------
LoadTemplate sAppPath & sTemplateFileName, "main"
'-------------------------------
' Load HTML template of Header and Footer
'-------------------------------
'-------------------------------
SetVar "FileName", sFileName


'-------------------------------
' Step through each form
'-------------------------------
NextStep_Show
'-------------------------------
' Process page templates
'-------------------------------
Parse "main", False
'-------------------------------
' Output the page to the browser
'-------------------------------
Response.write PrintVar("main")

' mld_workflowrecord2 Show end

'-------------------------------
' Destroy all object variables
'-------------------------------

' mld_workflowrecord2 Close Event begin
' mld_workflowrecord2 Close Event end

cn.Close
Set cn = Nothing
UnloadTemplate
'===============================
'===============================
' Action of the Record Form
'-------------------------------
Sub NextStepAction(sAction)
'-------------------------------
' Initialize variables  
'-------------------------------
  Dim bExecSQL: bExecSQL = true
  Dim sActionFileName : sActionFileName = ""
  Dim sParams : sParams = "?"
  Dim sWhere : sWhere = "" 
  Dim bErr : bErr = False
  Dim pPKMLD_WORKFLOWSTEP_KEY : pPKMLD_WORKFLOWSTEP_KEY = ""
  Dim fldMLD_WORKFLOWRULE_OMSCHRIJVING : fldMLD_WORKFLOWRULE_OMSCHRIJVING = ""
  Dim fldMLD_WORKFLOWSTEP_EXISTING_KEY : fldMLD_WORKFLOWSTEP_EXISTING_KEY = ""
  Dim fldMLD_STDMELDING_KEY : fldMLD_STDMELDING_KEY = ""
  Dim fldMLD_WORKFLOWSTEP_OMSCHRIJVING : fldMLD_WORKFLOWSTEP_OMSCHRIJVING = ""
  Dim fldMLD_WORKFLOWRULE_RESULT : fldMLD_WORKFLOWRULE_RESULT = ""
  Dim fldMLD_WORKFLOWSTEP_FROM_KEY : fldMLD_WORKFLOWSTEP_FROM_KEY = ""
  Dim fldMLD_WORKFLOWSTEP_START_KEY : fldMLD_WORKFLOWSTEP_START_KEY = ""
'-------------------------------

'-------------------------------
' NextStep Action begin
'-------------------------------
  sActionFileName = "mld_workflowrecord1.asp"
  sParams = sParams & "f_mld_workflowstep_key=" & ToURL(GetParam("Trn_f_mld_workflowstep_key"))

'-------------------------------
' CANCEL action
'-------------------------------
  if sAction = "cancel" then

'-------------------------------
' NextStep BeforeCancel Event begin
sParams = "?F_MLD_WORKFLOWSTEP_KEY="&ToURL(getParam("trn_F_MLD_WORKFLOWSTEP_KEY"))&_
          "&mld_workflowstep_key="&ToURL(getParam("mld_workflowstep_from_key"))
' NextStep BeforeCancel Event end
'-------------------------------
    cn.Close
    Set cn = Nothing
    response.redirect sActionFileName & sParams
  end if
'-------------------------------

'-------------------------------
' Build WHERE statement
'-------------------------------
  if sAction = "update" or sAction = "delete" then
    pPKMLD_WORKFLOWSTEP_KEY = GetParam("PK_MLD_WORKFLOWSTEP_KEY")
    if IsEmpty(pPKMLD_WORKFLOWSTEP_KEY) then exit sub
    sWhere = "MLD_WORKFLOWSTEP_KEY=" & ToSQL(pPKMLD_WORKFLOWSTEP_KEY, "Number")
  end if
'-------------------------------


'-------------------------------
' Load all form fields into variables
'-------------------------------
  fldMLD_WORKFLOWRULE_OMSCHRIJVING = GetParam("MLD_WORKFLOWRULE_OMSCHRIJVING")
  fldMLD_WORKFLOWSTEP_EXISTING_KEY = GetParam("MLD_WORKFLOWSTEP_EXISTING_KEY")
  fldMLD_STDMELDING_KEY = GetParam("MLD_STDMELDING_KEY")
  fldMLD_WORKFLOWSTEP_OMSCHRIJVING = GetParam("MLD_WORKFLOWSTEP_OMSCHRIJVING")
  fldMLD_WORKFLOWRULE_RESULT = GetParam("MLD_WORKFLOWRULE_RESULT")
  fldMLD_WORKFLOWSTEP_FROM_KEY = GetParam("MLD_WORKFLOWSTEP_FROM_KEY")
  fldMLD_WORKFLOWSTEP_START_KEY = GetParam("MLD_WORKFLOWSTEP_START_KEY")

'-------------------------------
' Validate fields
'-------------------------------
  if sAction = "insert" or sAction = "update" then
    if IsEmpty(fldMLD_WORKFLOWRULE_OMSCHRIJVING) then
      sNextStepErr = sNextStepErr & "De waarde in veld Regelomschrijving is verplicht.<br>"
    end if
    if not isNumeric(fldMLD_STDMELDING_KEY) then
      sNextStepErr = sNextStepErr & "De waarde in veld Melding is niet correct.<br>"
    end if
'-------------------------------
' NextStep Check Event begin
if isEmpty(fldMLD_WORKFLOWSTEP_EXISTING_KEY) then
  if IsEmpty(fldMLD_STDMELDING_KEY) then
    sNextStepErr = sNextStepErr & "De waarde in veld Melding is verplicht.<br>"
  end if
  if IsEmpty(fldMLD_WORKFLOWSTEP_OMSCHRIJVING) then
    sNextStepErr = sNextStepErr & "De waarde in veld Omschrijving is verplicht.<br>"
  end if
  if not isNumeric(fldMLD_STDMELDING_KEY) then
    sNextStepErr = sNextStepErr & "De waarde in veld Melding is niet correct.<br>"
  end if
end if
' NextStep Check Event end
'-------------------------------
    If len(sNextStepErr) > 0 then
      exit sub
    end if
  end if
'-------------------------------


'-------------------------------
' Create SQL statement
'-------------------------------
  select case sAction
    case "insert"
'-------------------------------
' NextStep Insert Event begin
if fldMLD_WORKFLOWSTEP_EXISTING_KEY="" then
  sSql = "insert into mld_workflowstep ("&_
         "  mld_workflowstep_start_key, "&_
         "  mld_workflowstep_omschrijving, "&_
         "  mld_stdmelding_key) "&_
         "values ("&_
         ToSql(fldMLD_WORKFLOWSTEP_START_KEY,"Number") &"," &_
         ToSql(fldMLD_WORKFLOWSTEP_OMSCHRIJVING,"Text") &"," &_
         ToSql(fldMLD_STDMELDING_KEY,"Number") &")"

  if len(sNextStepErr) > 0 then Exit Sub
  on error resume next
  if bExecSQL then
    cn.execute sSQL
  end if
  sNextStepErr = ProcessError
  on error goto 0
  if len(sWorkflowRecordErr) > 0 then Exit Sub

  sStepKey = ""
  sqlStepKey = "select mld_workflowstep_key "&_
               "from mld_workflowstep "&_
               "where mld_workflowstep_omschrijving = "&ToSql(fldMLD_WORKFLOWSTEP_OMSCHRIJVING,"Text")&_
               " and mld_workflowstep_start_key = "&ToSql(fldMLD_WORKFLOWSTEP_START_KEY,"Number")&_
               " and mld_stdmelding_key = "&ToSql(fldMLD_STDMELDING_KEY,"Number")
  openrs rsStepKey,sqlStepKey
  if not rsStepKey.EOF then
    sStepKey = getValue(rsStepKey,"mld_workflowstep_key")
  end if
else
  sStepKey = fldMLD_WORKFLOWSTEP_EXISTING_KEY
end if

sSql = "insert into mld_workflowrule ("&_
       "  mld_workflowstep_key, "&_
       "  mld_workflowrule_omschrijving, "&_
       "  mld_workflowrule_result, "&_
       "  mld_workflowstep_next_key) "&_
       "values("&_
       ToSql(fldMLD_WORKFLOWSTEP_FROM_KEY,"Number")&","&_
       ToSql(fldMLD_WORKFLOWRULE_OMSCHRIJVING,"Text")&","&_
       ToSql(getParam("MLD_WORKFLOWRULE_RESULT"),"Number")&","&_
       ToSql(sStepKey,"Number")&")"
' NextStep Insert Event end
'-------------------------------
      if sSQL = "" then
      sSQL = "insert into MLD_WORKFLOWSTEP (" & _
          "MLD_STDMELDING_KEY," & _
          "MLD_WORKFLOWSTEP_OMSCHRIJVING)" & _
          " values (" & _
          ToSQL(fldMLD_STDMELDING_KEY, "Number") & "," & _
          ToSQL(fldMLD_WORKFLOWSTEP_OMSCHRIJVING, "Text") & _
          ")"
      end if
    case "update"
'-------------------------------
' NextStep Update Event begin
' NextStep Update Event end
'-------------------------------
      sSQL = "update MLD_WORKFLOWSTEP set " & _
        "MLD_STDMELDING_KEY=" & ToSQL(fldMLD_STDMELDING_KEY, "Number") & _
        ",MLD_WORKFLOWSTEP_OMSCHRIJVING=" & ToSQL(fldMLD_WORKFLOWSTEP_OMSCHRIJVING, "Text")
      sSQL = sSQL & " where " & sWhere
    case "delete"
'-------------------------------
' NextStep Delete Event begin
' NextStep Delete Event end
'-------------------------------
      sSQL = "delete from MLD_WORKFLOWSTEP where " & sWhere
  end select
'-------------------------------
'-------------------------------
' NextStep BeforeExecute Event begin
sParams = "?F_MLD_WORKFLOWSTEP_KEY="&ToURL(getParam("trn_F_MLD_WORKFLOWSTEP_KEY"))&_
          "&mld_workflowstep_key="&ToURL(getParam("mld_workflowstep_from_key"))
' NextStep BeforeExecute Event end
'-------------------------------

'-------------------------------
' Execute SQL statement
'-------------------------------
if len(sNextStepErr) > 0 then Exit Sub
  on error resume next
  if bExecSQL then 
    cn.execute sSQL
  end if
  sNextStepErr = ProcessError
  on error goto 0
  if len(sNextStepErr) > 0 then Exit Sub
  cn.Close
  Set cn = Nothing
  response.redirect sActionFileName & sParams
'-------------------------------
' NextStep Action end
'-------------------------------
end sub
'===============================

'===============================
' Display Record Form
'-------------------------------
Sub NextStep_Show()
'-------------------------------
' NextStep Show begin
'-------------------------------
  Dim sWhere : sWhere = ""
  Dim sFormTitle: sFormTitle = "Toevoegen workflow"
  Dim bPK : bPK = True
  Dim sMLD_WORKFLOWSTEP_EXISTING_KEYDisplayValue: sMLD_WORKFLOWSTEP_EXISTING_KEYDisplayValue = ""
  Dim sMLD_STDMELDING_KEYDisplayValue: sMLD_STDMELDING_KEYDisplayValue = ""

'-------------------------------
' Load primary key and form parameters
'-------------------------------
  if sNextStepErr = "" then
    SetVar "Trn_f_mld_workflowstep_key", GetParam("f_mld_workflowstep_key")
    pMLD_WORKFLOWSTEP_KEY = GetParam("MLD_WORKFLOWSTEP_KEY")
    SetVar "NextStepError", ""
  else
    fldMLD_WORKFLOWSTEP_KEY = GetParam("MLD_WORKFLOWSTEP_KEY")
    fldMLD_STDMELDING_KEY = GetParam("MLD_STDMELDING_KEY")
    fldMLD_WORKFLOWSTEP_OMSCHRIJVING = GetParam("MLD_WORKFLOWSTEP_OMSCHRIJVING")
    SetVar "Trn_f_mld_workflowstep_key", GetParam("Trn_f_mld_workflowstep_key")
    pMLD_WORKFLOWSTEP_KEY = GetParam("PK_MLD_WORKFLOWSTEP_KEY")
    SetVar "sNextStepErr", sNextStepErr
    SetVar "FormTitle", sFormTitle
    Parse "NextStepError", False
  end if
'-------------------------------

'-------------------------------
' Load all form fields

  fldMLD_WORKFLOWSTEP_FROM_TEXT = GetParam("MLD_WORKFLOWSTEP_FROM_TEXT")
  fldMLD_WORKFLOWRULE_RESULTTEXT = GetParam("MLD_WORKFLOWRULE_RESULTTEXT")
  fldMLD_WORKFLOWRULE_OMSCHRIJVING = GetParam("MLD_WORKFLOWRULE_OMSCHRIJVING")
  fldField2 = GetParam("Field2")
  fldMLD_WORKFLOWSTEP_EXISTING_KEY = GetParam("MLD_WORKFLOWSTEP_EXISTING_KEY")
  fldField3 = GetParam("Field3")
  fldMLD_WORKFLOWRULE_RESULT = GetParam("MLD_WORKFLOWRULE_RESULT")
  fldMLD_WORKFLOWSTEP_FROM_KEY = GetParam("MLD_WORKFLOWSTEP_FROM_KEY")
  fldMLD_WORKFLOWSTEP_START_KEY = GetParam("MLD_WORKFLOWSTEP_START_KEY")
'-------------------------------

'-------------------------------
' Build WHERE statement

  if IsEmpty(pMLD_WORKFLOWSTEP_KEY) then bPK = False
  
  sWhere = sWhere & "MLD_WORKFLOWSTEP_KEY=" & ToSQL(pMLD_WORKFLOWSTEP_KEY, "Number")
  SetVar "PK_MLD_WORKFLOWSTEP_KEY", pMLD_WORKFLOWSTEP_KEY
'-------------------------------
'-------------------------------
' NextStep Open Event begin
if getParam("MLD_WORKFLOWRULE_RESULT") <> "" then
  fldMLD_WORKFLOWRULE_RESULT = getParam("MLD_WORKFLOWRULE_RESULT")
end if

if getParam("MLD_WORKFLOWSTEP_FROM_KEY") <> "" then
  fldMLD_WORKFLOWSTEP_FROM_KEY = getParam("MLD_WORKFLOWSTEP_FROM_KEY")
end if

if getParam("F_MLD_WORKFLOWSTEP_KEY") <> "" then
  fldMLD_WORKFLOWSTEP_START_KEY = getParam("F_MLD_WORKFLOWSTEP_KEY")
end if
' NextStep Open Event end
'-------------------------------

  SetVar "FormTitle", sFormTitle

'-------------------------------
' Build SQL statement and open recordset
'-------------------------------
  sSQL = "select * from MLD_WORKFLOWSTEP where " & sWhere
  openrs rs, sSQL
  bIsUpdateMode = (bPK and not(sAction = "insert" and sForm = "NextStep") and not rs.eof)
'-------------------------------

'-------------------------------
' Load all fields into variables from recordset or input parameters
'-------------------------------
  if bIsUpdateMode then
    fldMLD_WORKFLOWSTEP_KEY = GetValue(rs, "MLD_WORKFLOWSTEP_KEY")

'-------------------------------
' Load data from recordset when form displayed first time
'-------------------------------
    if sNextStepErr = "" then
      fldMLD_STDMELDING_KEY = GetValue(rs, "MLD_STDMELDING_KEY")
      fldMLD_WORKFLOWSTEP_OMSCHRIJVING = GetValue(rs, "MLD_WORKFLOWSTEP_OMSCHRIJVING")
    end if
    SetVar "NextStepInsert", ""
    Parse "NextStepEdit", False
'-------------------------------
' NextStep ShowEdit Event begin
' NextStep ShowEdit Event end
'-------------------------------
  else
    if sNextStepErr = "" then
      fldField2= "<i>Kies een bestaande stap</i>"
      fldField3= "<i>Of definieer een nieuwe</i>"
    end if
    SetVar "NextStepEdit", ""
    Parse "NextStepInsert", False
'-------------------------------
' NextStep ShowInsert Event begin
' NextStep ShowInsert Event end
'-------------------------------
  end if
  Parse "NextStepCancel", false

'-------------------------------
' Set default values
'-------------------------------
  fldField2= "<i>Kies een bestaande stap</i>"
  fldField3= "<i>Of definieer een nieuwe</i>"
  if sNextStepErr = "" then
'-------------------------------
' NextStep Show Event begin
end if
if true then

sqlNextKeys = "select w.mld_workflowstep_key, "&_
              "w.mld_workflowstep_omschrijving "&str_concat&_
              " '( ' "&str_concat&" d.ins_discipline_omschrijving "&str_concat&" ' / ' "&str_concat&" s.mld_stdmelding_omschrijving "&str_concat&" ')' "&_
              "from mld_workflowstep w, mld_discipline d, mld_stdmelding s "&_
              "where s.mld_ins_discipline_key = d.ins_discipline_key "&_
              "  and w.mld_stdmelding_key = s.mld_stdmelding_key "&_
              "  and w.mld_workflowstep_start_key = " & ToSQL(fldMLD_WORKFLOWSTEP_START_KEY, "Number")


if fldMLD_WORKFLOWRULE_RESULT = "1" then
  fldMLD_WORKFLOWRULE_RESULTTEXT = "Afgemeld"
elseif fldMLD_WORKFLOWRULE_RESULT = "0" then
  fldMLD_WORKFLOWRULE_RESULTTEXT = "Afgewezen"
end if

fldMLD_WORKFLOWSTEP_FROM_TEXT = getWorkflowstepOmschr(fldMLD_WORKFLOWSTEP_FROM_KEY)
' NextStep Show Event end
'-------------------------------
  end if

'-------------------------------
' Show form field
'-------------------------------
      SetVar "MLD_WORKFLOWSTEP_FROM_TEXT", ToHTML(fldMLD_WORKFLOWSTEP_FROM_TEXT)
      SetVar "MLD_WORKFLOWRULE_RESULTTEXT", ToHTML(fldMLD_WORKFLOWRULE_RESULTTEXT)
      SetVar "MLD_WORKFLOWSTEP_KEY", ToHTML(fldMLD_WORKFLOWSTEP_KEY)
      SetVar "MLD_WORKFLOWRULE_OMSCHRIJVING", ToHTML(fldMLD_WORKFLOWRULE_OMSCHRIJVING)
      SetVar "Field2", fldField2
      SetVar "NextStepLBMLD_WORKFLOWSTEP_EXISTING_KEY", ""
      SetVar "Selected", ""
      SetVar "ID", ""
      SetVar "Value", sMLD_WORKFLOWSTEP_EXISTING_KEYDisplayValue
      Parse "NextStepLBMLD_WORKFLOWSTEP_EXISTING_KEY", True
      openrs rsMLD_WORKFLOWSTEP_EXISTING_KEY, ""&sqlNextKeys&""
      while not rsMLD_WORKFLOWSTEP_EXISTING_KEY.EOF
        SetVar "ID", GetValue(rsMLD_WORKFLOWSTEP_EXISTING_KEY, 0) : SetVar "Value", GetValue(rsMLD_WORKFLOWSTEP_EXISTING_KEY, 1)
        if cstr(GetValue(rsMLD_WORKFLOWSTEP_EXISTING_KEY, 0)) = cstr(fldMLD_WORKFLOWSTEP_EXISTING_KEY) then SetVar "Selected", "SELECTED" else SetVar "Selected", ""
        Parse "NextStepLBMLD_WORKFLOWSTEP_EXISTING_KEY", True
        rsMLD_WORKFLOWSTEP_EXISTING_KEY.MoveNext
      wend
      set rsMLD_WORKFLOWSTEP_EXISTING_KEY = nothing
    
      SetVar "Field3", fldField3
      SetVar "NextStepLBMLD_STDMELDING_KEY", ""
      SetVar "Selected", ""
      SetVar "ID", ""
      SetVar "Value", sMLD_STDMELDING_KEYDisplayValue
      Parse "NextStepLBMLD_STDMELDING_KEY", True
      openrs rsMLD_STDMELDING_KEY, "select s.mld_stdmelding_key, sr.ins_srtdiscipline_prefix || '-' || d.ins_discipline_omschrijving || ' / ' ||  s.mld_stdmelding_omschrijving from mld_discipline d, mld_stdmelding s,INS_SRTDISCIPLINE sr where  d.ins_discipline_verwijder is null and        s.mld_ins_discipline_key = d.ins_discipline_key        and d.ins_srtdiscipline_key = sr.ins_srtdiscipline_key order by 2"
      while not rsMLD_STDMELDING_KEY.EOF
        SetVar "ID", GetValue(rsMLD_STDMELDING_KEY, 0) : SetVar "Value", GetValue(rsMLD_STDMELDING_KEY, 1)
        if cstr(GetValue(rsMLD_STDMELDING_KEY, 0)) = cstr(fldMLD_STDMELDING_KEY) then SetVar "Selected", "SELECTED" else SetVar "Selected", ""
        Parse "NextStepLBMLD_STDMELDING_KEY", True
        rsMLD_STDMELDING_KEY.MoveNext
      wend
      set rsMLD_STDMELDING_KEY = nothing
    
      SetVar "MLD_WORKFLOWSTEP_OMSCHRIJVING", ToHTML(fldMLD_WORKFLOWSTEP_OMSCHRIJVING)
      SetVar "MLD_WORKFLOWRULE_RESULT", ToHTML(fldMLD_WORKFLOWRULE_RESULT)
      SetVar "MLD_WORKFLOWSTEP_FROM_KEY", ToHTML(fldMLD_WORKFLOWSTEP_FROM_KEY)
      SetVar "MLD_WORKFLOWSTEP_START_KEY", ToHTML(fldMLD_WORKFLOWSTEP_START_KEY)
  Parse "FormNextStep", False

'-------------------------------
' NextStep Close Event begin
' NextStep Close Event end
'-------------------------------

Set rs = Nothing

'-------------------------------
' NextStep Show end
'-------------------------------
End Sub
'===============================
%>