'ALT_STAT - change Order_Status] from P V H R to U

external   vloadif() sch scw fentrybox() shopmask dpath messbox() cpath
external   exception() userid vunloadif() messboxwait() reqnpath

public     ptstr jobnr $dayftr

global     x CheckJobNr() AltStat() deladdr1 custname $ordstat $mess cat
global     UpdApptRecs() $newstat i #apptnr current1


MAIN
single-step off
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  file unload all
'   quiet off
  current1 = reqnpath|jobnr|".idx"

  while true
    x = CheckJobNr()                    ' find Cust_Ord & update - L495
    if x = 0
      continue while
    elseif x = -1
      exit while
    end if

    x = AltStat()
    if x = -1
      exit while
    elseif x = 0
      continue while
    end if

  end while

  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  file unload all
  transfer cpath|"pm_menu.psl" in-memory

END MAIN


FUNCTION AltStat()
' change Order_Status] from P V H R to U

  if [Order_Status] = "A"             ' "U"pdated
    messboxwait(" Order & Customer details NOT entered - update order first ",0,1,1)
    cancel-record
    return (0)

  elseif [Order_Status] = "U"             ' "U"pdated
    messboxwait(" Order already in UPDATE status ",0,1,1)
    cancel-record
    return (0)

  elseif [Order_Status] = "L"             ' "U"pdated
    messboxwait(" Order currently being despatched - change not permitted ",0,0,1)
    cancel-record
    return (0)

  elseif [Order_Status] = "D"
    messboxwait(" Order completed & despatched - change not permitted ",0,0,1)
    cancel-record
    return (0)

  else
    messbox(" Confirm change of status to UPDATE? (y/n) ",1,0,1)
    if ptstr == "y"
      $newstat = "U"
      [Order_Status] = $newstat
      write-record
      UpdApptRecs()
      $mess = jobnr&"status changed to UPDATE"
      cat = "SUNDRY"
      x = exception(userid,today,time24,cat,$mess)
      vunloadif(dpath|"unread1.vw")
      vloadif(dpath|"alt_stat.vw")
    else
      cancel-record
      return (0)
    end if
    return (1)
  end if
END FUNCTION ' AltStat()


FUNCTION CheckJobNr()                   ' finds Job & updates Cust_Ord
' local l1 c3 c2 c1 ques $reqstr
while true
  repaint off
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' ³ Find Job Nr                                                        ³
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  x = fentrybox(" Job Nr to alter Status for or {Esc} for Menu ",6,shopmask,"")
  if x = -1
    return (-1)
  else
    jobnr = ptstr
  end if

  vloadif(dpath|"alt_stat.vw")
  order change key "[Job_Nr]"
  data find "[Job_Nr]" equal jobnr options ""   '  find correct JOB
  if cerror
    messbox(" Job Nr NOT found ",0,0,1)
    return (0)
  else
    deladdr1 = [Delivery_Address_1]
    custname = [CustOrd_Name]
    $ordstat = [Order_Status]
    messbox(" Confirm"&custname&"@"&deladdr1|"? (y/n) ",1,1,1)
    if ptstr == "y"
      lock-record
      return (1)
    else
      return (0)
    end if
  end if
end while
END FUNCTION ' CheckJobNr()


' UpdApptRecs()
' $newstat
FUNCTION UpdApptRecs()
  repaint off
  vloadif(dpath|"bookappt.vw")
  if file(current1) = 1
    order change index current1
    if records = 0
      return (0)                         ' no appts made yet
    end if
  else
    order change key "[Job_Nr]"
    data query execute "job_reqn.dfq" index current1
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' ³ QUERY is:  [Job_Nr] = jobnr                                        ³
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
    if cerror                            ' if none - then return
      return (0)                         ' no appts made yet
    end if
  end if

  for i = 1 to records
    $dayftr = left([DFA],11)
    #apptnr = [Appointment_Order]
    lock-record
      [Entered_By] = userid
      [Date_Altered] = today
      [Time] = now
      [Status] = $newstat
    write-record

    repaint off
    vloadif(dpath|"apptdate.vws")         ' message "jobnr is:"&str(jobnr)
    order change key "[DayFitter]"
    data find "[DayFitter]" equal $dayftr options ""
    if cerror
      return (0)
    end if
    lock-record
      dbput("[B"|str(#apptnr)|"]",$newstat)
    write-record
    vloadif(dpath|"bookappt.vw")
    data goto record next
  end for
END FUNCTION 'UpdApptRecs()
