'UPD_CUST - update customer/delivery details

external  dpath Background() messbox() fentrybox() vloadif() sch scw cpath shopmask nr5
external   fgp userid scr dsa strtoary() arytostr() vkeybox() $enternow
external   lpath bge popuplist() city wraptext() #maxleft #minleft
external   entryline() messline() strcount() posnpopup() messboxwait()

public    psa ptstr ptval custcode ptary[1] codes[1]

global     $newstat $showdel custref $jobstr i $test jobnr
global     FindJobNr() ReturnToMenu() WriteDetails() CheckCustomer() $nrreqs
global     #jobrec #bline #tline ftgdate slotrec hometel offtel linenr refcode
global     x custname ftgscrn ftginstr ftgplan
global     deladdr1 custpostcode deladdr2 deladdr3
global     deladdr4 $color carpettypes $ctgplan ftgcomm ftginit
global     $instruct y custaddr custpost m1 m5
global     offax mobile custaddr1 custaddr2 custcity custcontact cust_title
global     z y1 y2 #rem ShowCustomer() ques jobdesc $completed $invd
global     a1 a2 a3 a4 a5 $unitdes $unit $areainit
global     $nextarea $allareas $area_list #start #lenareas #listcount
global     #startr #startc j #volume $line x1 x2
global     CopyDelivery() namelist[1,2] DeliveryAddr() poplist[1]
global     CheckDetails() CustomerAddr() ReviewAddr()
global     deladdr ShowDetails() $sameaddr $delonly CopyCustomer()


MAIN
  Background()
  repaint off
  file unload all
  while true
    x = FindJobNr()                    ' find Cust_Ord & update - L691
    if x = -1
      Background()
      exit while
    elseif x = 1		       ' already updated
      screen clear box 1 1 sch scw 0 0 no-border
      repaint off
      continue while
    end if

    x = CheckDetails()

    WriteDetails()

    exit while
  end while

  ReturnToMenu()

END MAIN


FUNCTION FindJobNr()                   ' finds Job & updates Cust_Ord
local l1 c3 c2 c1 ques $reqstr fj
  while true
    x = fentrybox(" Update Customer Details for Order Nr or {Esc} for Menu ",6,shopmask,"")
    if x = -1
      return (-1)                      ' ReturnToMenu
    end if
    jobnr = ptstr
    vloadif(dpath|"upd_cust.vw")           ' load view for updating
    order change physical
    order change key "[Job_Nr]"
    data find "[Job_Nr]" equal jobnr options "g"   '  find correct JOB
    if cerror
      messbox(" Job NOT entered - use INITIAL ORDER first ",0,0,1)
      return (-1)                      ' ReturnToMenu
    else
      #jobrec = precord    '???????????????????????
    end if
    custcode    = [Customer_Code]
    custname    = [Customer_Name]
    deladdr1    = [Delivery_Address_1]
'     custcontact = [Contact_Name]
'     offtel      = [Office_Tel]
'     offax       = [Office_Fax]
'     hometel     = [Home_Tel]
'     cust_title  = [Title]
'     mobile      = [Mobile/Other_Nr]

'     if [Completed] = "Y"             ' "U"pdated
'       messbox(" Cannot change "|jobnr&custname&"- already marked as completed ",0,1,1)
'       return (1)
'     else       ' CUST_ORD not yet updated - Extract details from Initial Order
    x = messbox(" "|custname&"@"&deladdr1|"?(y/n)",1,1,1)
    if x = 0
      if ptstr == "n"
        continue while
      end if
    end if
    repaint off
'     end if
    return (0)
  end while
END FUNCTION ' FindJobNr()


FUNCTION CheckCustomer()
local   $nophone
  while true
    y1 = format(" Select Title ","M70")
    screen print 21 5 15 1 y1
    y = popuplist(11,5,18,"Mr Mrs Mr˙&˙Mrs Miss Ms Other Ltd˙Co","",1,0)
    if ptstr = "Other"
      while true
        x = entryline(" Enter title ",20,"","",21,5,70)
        if x = 0
          cust_title = ptstr
          exit while
        end if
      end while
    elseif ptstr = "Ltd˙Co"
      cust_title = ""
      exit while
    else
      cust_title = ptstr
      x = entryline(" Enter Forename or Initial ",35,"","",21,5,70)
      cust_title = cust_title&proper(ptstr)
    end if
    exit while
  end while

  while true
'   custcontact = trim(cust_title)&trim(custname)
    y1 = format(" ","M70")
    screen print 21 5 15 1 y1
    x = entryline(" Contact name (if different to customer name) ",45,"",custcontact,21,5,70)
    if x = 0
      custcontact = ptstr
      exit while
    end if
  end while

  $nophone = "N"
  while true
    while true
      if hometel = "N/P"
        hometel = "0"
      end if
      x = entryline(" Home 'phone number (eg 0171-498-1453) - {Esc} if none ",15,"\0*14{[1234567890\-]}",hometel,21,5,70)
      if x = 0
        if ptstr = "0"
          hometel = "N/P"
        else
          hometel = ptstr
          $nophone = "Y"
          exit while
        end if
      elseif x = -1
        hometel = "N/P"
        exit while
      end if
    end while

    while true
      if offtel = "N/P"
        offtel = "0"
      end if
      x = entryline(" Office 'phone number (eg 0171-498-1453) - {Esc} if none ",15,"\0*14{[1234567890\-]}",offtel,21,5,70)
      if x = 0
        if ptstr = "0"
          offtel = "N/P"
        else
          offtel = ptstr
          $nophone = "Y"
          exit while
        end if
      elseif x = -1
        offtel = "N/P"
        exit while
      end if
    end while

    while true
      if mobile = "N/P"
        mobile = "0"
      end if
      x = entryline(" Mobile 'phone number (eg 0860-291565) - {Esc} if none ",15,"\0*14{[1234567890\-]}",mobile,21,5,70)
      if x = 0
        if ptstr = "0"
          mobile = "N/P"
        else
          mobile = ptstr
          $nophone = "Y"
          exit while
        end if
      elseif x = -1
        mobile = "N/P"
        exit while
      end if
    end while

    if $nophone = "N"
      x = messline(" You cannot be serious! Everyone has a 'phone. Re-enter? (y/n) ",1,1,1,21,5,70)
      if ptstr=="y"
        continue while
      else
        exit while
      end if
    else
      exit while
    end if
  end while

  while true
    if offax = "N/P"
      offax = "0"
    end if
    x = entryline(" Fax number (eg 0171-498-1455) - {Esc} if none ",15,"\0*14{[1234567890\-]}",offax,21,5,70)
    if x = 0
      offax = @if(ptstr=="0","N/P",ptstr)
      exit while
    elseif x = -1
      offax = "N/P"
      exit while
    end if
  end while
END FUNCTION 'CheckCustomer()


FUNCTION ReviewAddr()
  while true
    ShowDetails()
    y1 = format("Change {C}ustomer/{D}elivery addresses or {S}undry details ","M70")
    screen print 21 5 15 1 y1
    y2 = format("{F10} to finish","M70")
    screen print 22 5 15 1 y2

    while true
      x = inchar                    'message "x is:"&str(x)
      if x = 99 or x = 67           ' c - change Customer
        CustomerAddr()
        exit while

      elseif x = 100 or x = 68      ' d - change DELIVERY
        DeliveryAddr()
        exit while

      elseif x = 115 or x = 83      ' s - change SUNDRY
        CheckCustomer()
        exit while

      elseif x = 324                ' F10 - finish
        x = messbox(" Confirm correct? (y/n) ",1,1,1)
        if ptstr=="n"
          continue while
'         else
'           exit while
        end if
        screen clear box 1 1 sch scw 0 0 no-border
        repaint off
        return (0)
      end if
    end while
  end while
END FUNCTION 'ReviewAddr()


FUNCTION WriteDetails()         ' write customer & job details to SALORD02.VW
  lock-record
    [Title]              = cust_title
    [Customer_Name]      = custname
    [Contact_Name]       = custcontact
    [Office_Tel]         = offtel
    [Home_Tel]           = hometel
    [Office_Fax]         = offax
    [Mobile/Other_Nr]    = mobile
    [Address_1]          = custaddr1
    [Address_2]          = custaddr2
    [Customer City/Town] = custcity
    [Postcode]           = custpostcode
    [Delivery_Address_1] = deladdr1
    [Delivery_Address_2] = deladdr2
    [Del_City]           = deladdr3
    [Del_Postcode]       = deladdr4
'     [Description]        = jobdesc
'     [Instructions]       = ftginstr
'     [Cutting_Plan]       = $ctgplan
'     [Fitting_Comment]    = ftgcomm
'     [Appt_Slots]         = slotrec
'     [Slots_Rem]          = slotrec
    [PDA]                = $showdel
    [Customer_Ref]       = custref
    [Last_Update]        = today
    [Updated_By]         = userid
'     [Order_Status]       = $newstat
    [Updated_On]         = today
    [Updater]            = userid
  write-record
END FUNCTION ' WriteDetails()


FUNCTION ReturnToMenu()
'   lock module jobnr
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  error off
  while true
    window close
    if cerror
      exit while
    end if
  end while
  file unload all
  transfer cpath|"pm_menu.psl" in-memory
END FUNCTION


FUNCTION CopyDelivery()   ' message "copy deladdr from custaddr"
  deladdr1 = custaddr1
  deladdr2 = custaddr2
  deladdr3 = custcity
  deladdr4 = custpostcode
' message "deladdr4 is:"&str(deladdr4)
END FUNCTION ' CopyDelivery()   ' copy deladdr from custaddr


FUNCTION CopyCustomer()
  custaddr1    = [Address_1]
  custaddr2    = [Address_2]
  custcity     = [Customer City/Town]
  custpostcode = [Postcode]
END FUNCTION 'CopyCustomer()


FUNCTION CustomerAddr()
  while true
    x = entryline(" Customer's Address - 1st line ",35,"",custaddr1,21,5,70)
    if x = 0
      custaddr1 = proper(ptstr)
      exit while
    end if
  end while

  while true
    x = entryline(" Customer's Address - 2nd line (NOT Town or City!) ",35,"",custaddr2,21,5,70)
    if x = 0
      custaddr2 = proper(ptstr)
      exit while
    end if
  end while

  while true
    x = entryline(" Customer's Town or City (NOT County!) ",20,city,custcity,21,5,70)
    if x = 0
      custcity = ptstr
      exit while
    end if
  end while

  while true
    x = entryline(" Customer's Postcode (NOT County!) ",8,"AU*7{[A-Za-z1234567890\ ]U}",custpostcode,21,5,70)
    if x = 0
      custpostcode = ptstr
      exit while
    else
      continue while
    end if
  end while
END FUNCTION 'CustomerAddr()


FUNCTION DeliveryAddr()   'message "enter deladdr using deladdr"
  if $invd = "Y"
    messboxwait(" Already invoiced - cannot alter ",0,0,1)
    return (1)
  elseif $invd = "N" and $completed = "Y"
    messboxwait(" Invoice MAY have been printed - check with Head Office ",0,0,1)
  end if

  while true
    x = entryline(" Enter Delivery Address - Line 1 ",35,"",deladdr1,21,5,70)
    if x = 0
      deladdr1 = proper(ptstr)
      exit while
    end if
  end while

  while true
    x = entryline(" Enter Delivery Address - Line 2 ",35,"",deladdr2,21,5,70)
    if x = 0
      deladdr2 = proper(ptstr)
      exit while
    end if
  end while

  while true
    x = entryline(" Enter Town/City ",20,city,deladdr3,21,5,70)
    if x = 0
      deladdr3 = ptstr
      exit while
    end if
  end while

  while true
    x = entryline(" Delivery Postcode ",8,"AU*7{[A-Za-z1234567890\ ]U}",deladdr4,21,5,70)
    if x = 0
      deladdr4 = ptstr
      exit while
    else
      continue while
    end if
  end while
' message "deladdr3 is:"&str(deladdr3)
' message "deladdr4 is:"&str(deladdr4)
END FUNCTION ' DeliveryAddr()


FUNCTION ShowCustomer()
local c1 c2 c3 c4 d1 d2 d3 d4 cc1 cr1 cc2 cr2
  cr1 = 4
  cc1 = 2
  cr2 = cr1+5
  cc2 = cc1+37
  screen clear box cr1 cc1 cr2 cc2 15 1
  c1 = left(custaddr1|"˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙",35)
  c2 = left(custaddr2|"˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙",35)
  c3 = left(custcity|"˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙",35)
  c4 = left(custpostcode|"˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙",35)
  screen print cr1 cc1+1 15 1 "˙Customer's address˙"
  screen print cr1+1 cc1+2 15 1 c1
  screen print cr1+2 cc1+2 15 1 c2
  screen print cr1+3 cc1+2 15 1 c3
  screen print cr1+4 cc1+2 15 1 c4
  screen save cr1 cc1 cr2 cc2 custaddr
END FUNCTION 'ShowCustomer()


FUNCTION CheckDetails()
  custaddr1    = [Address_1]
  custaddr2    = [Address_2]
  custcity     = [Customer City/Town]
  custpostcode = [Postcode]

  deladdr1     = [Delivery_Address_1]
  deladdr2     = [Delivery_Address_2]
  deladdr3     = [Del_City]
  deladdr4     = [Del_Postcode]

  cust_title   = [Title]
  custname     = [Customer_Name]
  custcontact  = [Contact_Name]
  offtel       = @if([Office_Tel]=blank,"",[Office_Tel])
  hometel      = @if([Home_Tel]=blank,"",[Home_Tel])

  $completed   = [Completed]
  if days([Invoice_Date]) > 0
    $invd = "Y"
  else
    $invd = "N"
  end if
  if $invd = "Y"
    messboxwait(" Already invoiced - cannot alter delivery details ",0,0,1)
  else
    messbox(" Do you want the invoice to show the Delivery address? (y/n)",1,1,1)
    if ptstr == "y"
      $showdel = "Y"
    else
      $showdel = "N"
    end if
  end if

  ShowDetails()

  x = ReviewAddr()

END FUNCTION ' CheckDetails()


FUNCTION ShowDetails()
local c1 c2 c3 c4 d1 d2 d3 d4 cc1 cr1 cc2 cr2 dc1 dr1 dc2 dr2 pc1 pr1 pc2 pr2 \
      ordets p1 p2 p3 p4 p5 p6 p7 p8 df

' message "$showdel is:"&str($showdel)
  if $showdel = "Y"
    df  = 15
  elseif $showdel = "N"
    df  = 8
  end if

  cr1 = 4
  cc1 = 2
  cr2 = cr1+5
  cc2 = cc1+37
  dr1 = cr1
  dc1 = 42
  dr2 = cr2
  dc2 = dc1+37
  pr1 = cr2+2
  pc1 = cc1+14
  pr2 = pr1+8
  pc2 = pc1+50

  screen clear box cr1 cc1 cr2 cc2 15 1
  c1 = left(custaddr1|"˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙",35)
  c2 = left(custaddr2|"˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙",35)
  c3 = left(custcity|"˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙",35)
  c4 = left(custpostcode|"˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙",35)
  screen print cr1 cc1+1 15 1 "˙Customer's address˙"
  screen print cr1+1 cc1+2 15 1 c1
  screen print cr1+2 cc1+2 15 1 c2
  screen print cr1+3 cc1+2 15 1 c3
  screen print cr1+4 cc1+2 15 1 c4
  screen save cr1 cc1 cr2 cc2 custaddr

  screen clear box dr1 dc1 dr2 dc2 df 1
  d1 = left(deladdr1|"˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙",35)
  d2 = left(deladdr2|"˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙",35)
  d3 = left(deladdr3|"˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙",35)
  d4 = left(deladdr4|"˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙",35)
  screen print dr1 dc1+1 df 1 "˙Delivery address˙"
  screen print dr1+1 dc1+2 df 1 d1
  screen print dr1+2 dc1+2 df 1 d2
  screen print dr1+3 dc1+2 df 1 d3
  screen print dr1+4 dc1+2 df 1 d4
  if $showdel = "N"
    screen print dr2 dc1+1 df 1 "˙Not to be shown on invoice˙"
  end if
  screen save dr1 dc1 dr2 dc2 deladdr

  screen clear box pr1 pc1 pr2 pc2 15 1
  p1 = format("Title:  ˙   "|left(cust_title|"˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙",35),"L47")
  p2 = format("Name:     ˙ "|left(custname|"˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙",35),"L47")
  p3 = format("Contact:   ˙"|left(custcontact|"˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙",35),"L47")
  p4 = format("Office tel:˙"|left(offtel|"˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙",35),"L47")
  p5 = format("Home tel:  ˙"|left(hometel|"˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙",35),"L47")
  p6 = format("Fax nr:˙˙  ˙"|left(offax|"˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙",35),"L47")
  p7 = format("Mobile nr: ˙"|left(mobile|"˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙",35),"L47")
'   p8 = format("Timeslots: ˙"|left(slotrec|"˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙",35),"L47")
  screen print pr1 pc1+1 15 1 "˙Sundry details˙"
  screen print pr1+1 pc1+2 15 1 p1
  screen print pr1+2 pc1+2 15 1 p2
  screen print pr1+3 pc1+2 15 1 p3
  screen print pr1+4 pc1+2 15 1 p4
  screen print pr1+5 pc1+2 15 1 p5
  screen print pr1+6 pc1+2 15 1 p6
  screen print pr1+7 pc1+2 15 1 p7
'   screen print pr1+8 pc1+2 15 1 p8
  screen save pr1 pc1 pr2 pc2 ordets
END FUNCTION 'ShowDetails()
