'CUSTOM01 - used for updating Customer details

external   cpath dpath navrecs() vloadif() messbox() fentrybox() pagerec()
external   vunloadif() sch scw $menu PrintReport() fgp bgp popuplist()
external   entryline() messboxwait()

public     ptval ptstr dsa jobnr

global     custname #prec Key Approve() UpdateCustomer() x t1 custcity t5 t6
global     ReplaceHardSpace() w_$title t3 w_addr1 t4 w_addr2 Update() y1 y2 z
global     $title addr1 addr2 w_custname w_custcity custdet[1,1] ShowTitles()
global     w_custcode w_abbrv_name custcode abbrv_name EnterCustName()
global     ReturnToMenu() $line


MAIN
  single-step off
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  redimension custdet[11,2]
  while true
    z = 0
    x = EnterCustName(z)
    if x = -1
      exit while
    elseif x = 0                   ' using current
      continue while
'       exit while
    end if

  end while

  ReturnToMenu()

END MAIN


FUNCTION Approve()
local   t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 y7 p5
'   screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  while true
    t1 = ReplaceHardSpace(custdet[1,1])
    t2 = ReplaceHardSpace(custdet[2,1])
    t3 = ReplaceHardSpace(custdet[3,1])
    t4 = ReplaceHardSpace(custdet[4,1])
    t5 = ReplaceHardSpace(custdet[5,1])
    t6 = ReplaceHardSpace(custdet[6,1])
    t7 = ReplaceHardSpace(custdet[7,1])
    t8 = ReplaceHardSpace(custdet[8,1])
    t9 = ReplaceHardSpace(custdet[9,1])
    t10 = ReplaceHardSpace(custdet[10,1])
    t11 = ReplaceHardSpace(custdet[11,1])
    p5 = "˙˙˙˙˙˙˙˙CORRECT!˙-˙save˙changes˙˙˙˙˙˙˙˙"
    screen clear box 1 1 sch scw 0 0 no-border
'   repaint off

    ShowTitles()

    x = popuplist(5,21,17,t1&t2&t3&t4&t5&t6&t7&t8&t9&t10&t11&p5,chr(24)&chr(25)&"{Enter} to change ",1,0)
    if x = -1
      return (-1)
    end if

    screen shortrestore dsa
    if ptstr = t1
      $line = 1
      custdet[1,1] = Update(custdet[1,1],custdet[1,2])

    elseif ptstr = t2
      $line = 2
      custdet[2,1] = Update(custdet[2,1],custdet[2,2])

    elseif ptstr = t3
      $line = 3
      custdet[3,1] = Update(custdet[3,1],custdet[3,2])

    elseif ptstr = t4
      $line = 4
      custdet[4,1] = Update(custdet[4,1],custdet[4,2])

    elseif ptstr = t5
      $line = 5
      custdet[5,1] = Update(custdet[5,1],custdet[5,2])

    elseif ptstr = t6
      $line = 6
      custdet[6,1] = Update(custdet[6,1],custdet[6,2])

    elseif ptstr = t7
      $line = 7
      custdet[7,1] = Update(custdet[7,1],custdet[7,2])

    elseif ptstr = t8
      $line = 8
      custdet[8,1] = Update(custdet[8,1],custdet[8,2])

    elseif ptstr = t9
      $line = 9
      custdet[9,1] = Update(custdet[9,1],custdet[9,2])

    elseif ptstr = t10
      $line = 10
      custdet[10,1] = Update(custdet[10,1],custdet[10,2])

    elseif ptstr = t11
      $line = 11
      custdet[11,1] = Update(custdet[11,1],custdet[11,2])

    elseif ptstr == p5
      screen clear box 1 1 sch scw 0 0 no-border
      repaint off
      UpdateCustomer()
      exit while
    end if
  end while
END FUNCTION ' Approve()


FUNCTION Update(e,w)
local $m
  while true
    case $line
      when 5
       $m = "*20{[A-Za-z\ ]U}"
      when 6
       $m = "AU*7{[A-Za-z1234567890\ ]U}"
      otherwise
       $m = ""
    end case

    x = entryline(" Correct as necessary ",w,$m,e,19,21,43)
    if $line<>4
      if len(ptstr)=0
        continue while
      end if
    end if

    return (ptstr)
  end while
END FUNCTION ' Update(e)


FUNCTION ReplaceHardSpace(str1)
local j r m bw l_last #addn
  bw = 39                              ' boxwidth
  m = ""
  for j = 1 to len(str1)
    r = mid(str1,j,1)
    if r = " "
      r = "˙"                          ' replace hard space
    end if
    m = m|r
  end for

  if len(m) < bw
    #addn = bw-len(m)
  else
    #addn = mod(len(m),bw)
  end if
  m = m|repeat("˙",#addn)
  return (m)
END FUNCTION ' ReplaceHardSpace()


FUNCTION EnterCustName(z)
  while true
    x = fentrybox(" Enter first 7 letters of Name (inc. capitals) - {Esc} ",7,"","")
    if x = 0
      if len(ptstr)=0
        continue while
      end if
      exit while
    elseif x = -1
      return (-1)
    end if
  end while
  abbrv_name = ptstr

  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  vloadif(dpath|"custom02.vw")
  order change key "[Abbrv_Name]"
  data find "[Abbrv_Name]" equal abbrv_name options ""
  if cerror
    messboxwait(" Name"&chr(34)|abbrv_name|chr(34)|" not found - check correct capitals ",0,0,1)
    screen clear box 1 1 sch scw 0 0 no-border
    repaint off
    return (2) ' using archived files
  end if

  while true
    repaint on
    repaint
    ptval=0
    y1 = format(" Name                                Delivery Address","L71")
    y2 = format(" {Enter} to update - {Esc} exits ","M71")
    screen print 4 6 fgp bgp y1
    screen print 18 6 fgp bgp y2

    ptval = navrecs()
    if ptval = {Enter}
      repaint off

      custdet[1,1] = [Title]
      custdet[1,2] = dbfldinfo("[Title]",2)
      custdet[2,1] = [Customer_Name]
      custdet[2,2] = dbfldinfo("[Customer_Name]",2)
      custdet[3,1] = [Address_1]
      custdet[3,2] = dbfldinfo("[Address_1]",2)
      custdet[4,1] = [Address_2]
      custdet[4,2] = dbfldinfo("[Address_2]",2)
      custdet[5,1] = [City/Town]
      custdet[5,2] = dbfldinfo("[City/Town]",2)
      custdet[6,1] = [Postcode]
      custdet[6,2] = dbfldinfo("[Postcode]",2)
      custdet[7,1] = [Contact_Name]
      custdet[7,2] = dbfldinfo("[Contact_Name]",2)
      custdet[8,1] = [Home_Tel]
      custdet[8,2] = dbfldinfo("[Home_Tel]",2)
      custdet[9,1] = [Office_Tel]
      custdet[9,2] = dbfldinfo("[Office_Tel]",2)
      custdet[10,1] = [Mobile/Other_Nr]
      custdet[10,2] = dbfldinfo("[Mobile/Other_Nr]",2)
      custdet[11,1] = [Office_Fax]
      custdet[11,2] = dbfldinfo("[Office_Fax]",2)
      custcode      = [Customer_Code]
      Approve()
'       UpdateCustomer()

    elseif ptval = {Esc}
      screen clear box 1 1 sch scw 0 0 no-border
      repaint off
      return (0)
    end if
  end while
END FUNCTION 'EnterCustName()


FUNCTION ReturnToMenu()
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  file unload all
  transfer cpath|"pm_menu.psl" in-memory
END FUNCTION ' ReturnToMenu()


FUNCTION UpdateCustomer()
  abbrv_name = proper(left(custdet[2,1],5))
  if len(custdet[2,1]) = 0
    messboxwait(" Customer's name has been omitted - pls contact David @ HO ",0,0,1)
  end if
  if len(abbrv_name) = 0
    messboxwait(" Customer's abbrv'd name has been omitted - pls contact David @ HO ",0,0,1)
  end if
' message "custdet[8,1]) is:"&str(custdet[8,1])
' message "custdet[9,1]) is:"&str(custdet[9,1])
' message "custdet[10,1]) is:"&str(custdet[10,1])
' message "custdet[11,1]) is:"&str(custdet[11,1])
  lock-record
    [Title]         = custdet[1,1]
    [Customer_Name] = custdet[2,1]
    [Address_1]     = custdet[3,1]
    [Address_2]     = custdet[4,1]
    [City/Town]     = custdet[5,1]
    [Postcode]      = custdet[6,1]
    [Contact_Name]  = custdet[7,1]
    [Home_Tel]      = custdet[8,1]
    [Office_Tel]    = custdet[9,1]
    [Mobile/Other_Nr]= custdet[10,1]
    [Office_Fax]    = custdet[11,1]
'     [Abbrv_Name]    = abbrv_name
  write-record
END FUNCTION ' UpdateCustomer()


FUNCTION ShowTitles()
local r1b r1t c1b c1t r2b c2b cfb cbb cft cbt
  r1b = 5
  r1t = r1b+1
  c1b = 6
  c1t = c1b+1
  r2b = 16
  c2b = 20
  cfb = 1
  cbb = 15
  cft = 1
  cbt = 15
  screen clear box r1b c1b r2b c2b cfb cbb no-border
  screen print r1t-1 c1b+1 0 cbt custcode
  screen print r1t c1b+1 cft cbt "Title"
  screen print r1t+1 c1b+1 cft cbt "Surname"
  screen print r1t+2 c1b+1 cft cbt "Address 1"
  screen print r1t+3 c1b+1 cft cbt "Address 2"
  screen print r1t+4 c1b+1 cft cbt "TOWN/CITY"
  screen print r1t+5 c1b+1 cft cbt "Postcode"
  screen print r1t+6 c1b+1 cft cbt "Contact"
  screen print r1t+7 c1b+1 cft cbt "Home 'phone"
  screen print r1t+8 c1b+1 cft cbt "Office 'phone"
  screen print r1t+9 c1b+1 cft cbt "Mobile 'phone"
  screen print r1t+10 c1b+1 cft cbt "Fax nr"
END FUNCTION ' ShowTitles()
