'PRCH_PRC - updates Product prices - BOSS menu ONLY!!!!! because it allows access to Stock prices
'  insert code to ask if Disc% is to be updated
'  reviews all stock/selling prices before creating new
'		 database or printing price lists

external   vloadif() vunloadif() messbox() fentrybox() sch scw cpath dpath
external   bpopdb() navrecs() progress() fgp bgp pagerec() scr dsa fgs bgs
external   chkdate() userid Background()

public     ptstr prodcode suppcode ptval nrindex

global     x EnterSupplier() ShowProducts()
global     change()
global     currSMLC           ' current SMLC price
global     currSMLR           ' current SMLR price
global     prevSMLC           ' previous SMLC price
global     prevSMLR           ' previous SMLR price
global     prevupdate         ' date of previous update
global     nrrecs
global     suppname
global     effectdate ReturnToMenu()

MAIN
single-step off
  Background()

  while true
  ' choose supplier
    x = EnterSupplier()
    if x = -1
      exit while
    end if

    ShowProducts()

  end while

  ReturnToMenu()
END MAIN


FUNCTION ReturnToMenu()
  Background()
  vunloadif("chngprc2.vw")
  vunloadif("supplier.vws")
  transfer cpath|"pm_menu.psl" in-memory
END FUNCTION ' ReturnToMenu()


FUNCTION change()
local  y1 y2 y3 proddes prevSMLC
  prevSMLC = [SM_List_Cuts]
  prevSMLR = [SM_List_Rolls]
  proddes = [Product_Supplier]
  prevupdate = [Effect_Date]
  while true
    while true
      x = fentrybox(" New SM Cut price for"&proddes&"- enter dec. point ",6,"*6{[-1234567890.]}","")
      if x = 0
        currSMLC = ptstr
        if value(prevSMLC) > value(currSMLC)
          messbox(" New price is lower than old price - Correct? (y/n)",1,1,1)
          if ptstr == "y"
            exit while
          end if
          continue while
        end if
        exit while
      else
        exit function
      end if
    end while

    while true
      x = fentrybox(" New SM ROLL price for"&proddes&"- enter dec. point ",6,"*6{[-1234567890.]}","")
      if x = 0
        currSMLR = ptstr
        if value(prevSMLR) > value(currSMLR)
          messbox(" New price is lower than old price - Correct? (y/n)",1,1,1)
          if ptstr == "y"
            exit while
          end if
          continue while
        end if
        exit while
      else
        exit function
      end if
    end while

  ' check that SMLC > SMLR
    y1 = value(currSMLR)               'message "SMLR is"&str(y1)
    y2 = value(currSMLC)               'message "SMLC is"&str(y2)
    y3 = y2 - y1                       'message "Diff is"&str(y3)

    if y3 < 0
      messbox(" Roll price is greater than Cut price - Correct? (y/n)",1,1,1)
        if ptstr == "n"
          continue while
        end if
    end if

    messbox("SqMtr Cut price is:"&format(value(currSMLC),"2r$")&" SqMtr Roll price is:"&format(value(currSMLR),"2r$")&"-"&"- Correct? (y/n)",1,1,1)
    if ptstr == "y"
      screen clear box 1 1 sch scw 0 0 no-border
      exit while
    end if
  end while

  lock-record
    [Prev_SMLR] = prevSMLR
    [Prev_SMLC] = prevSMLC
    [SM_List_Rolls] = currSMLR
    [SM_List_Cuts] = currSMLC
    [Effect_Date] = effectdate
    [Last_Update] = prevupdate
    [Updated_On] = today
    [Updated_By] = userid
  write-record
  repaint on
  repaint
END FUNCTION ' change()


FUNCTION EnterSupplier()
local bpop_ret y1
  vloadif(dpath|"supplier.vws")
  order change physical
  order sort now dictionary "suppname" fields "[Name]" ascending
  repaint off
  while true
    y1 = format(" Choose Supplier and press {Enter} ","M38")
    screen print 7 21 15 1 y1
    screen print 8 21 15 1 (format(" {Enter} views orders - {Esc} exits ","M38"))
    x = bpopdb("supplier",6,"","[Name]","l35","[Supplier_Code]","L6","[Supplier_Code]",8,21,19,58,"",0)
    if x = 0
      exit while
    elseif x = -1
      screen clear box 1 1 sch scw 0 0 no-border
      return (-1)
    end if
  end while
  screen clear box 1 1 sch scw 0 0 no-border
  suppcode = ptstr
  suppname = [Name]
END FUNCTION


FUNCTION ShowProducts()
local i
' confirm date of Price List
  while true
    x = fentrybox(" Enter date of Price List ",10,"##\/##\/####",today)
    if x = 0
      effectdate = ptstr
        if chkdate(effectdate,1) = -1
	  messbox(" Incorrect date - re-enter ",0,0,1)
	  continue while
	end if
      exit while
    else
      message "FENTRY Error:"&str(x)
    end if
  end while

  Background()

' find Products from suppcode
  vloadif(dpath|"chngprc2.vw")
  order change physical
  nrrecs = precords
' repaint on
' repaint
' single-step on

  Background()
  progress(fgp,bgp," Sorting "|str(nrrecs)|" records ... please wait ",0)
  order change key "[Supplier_Code]"
'     data query execute "chngprc2.dfq" index "chngprc2.idx"
' ' ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
' ' º [Supplier_Code] = suppcode                                         º
' '   and
' '   left([Product_Code],1)<>"A"
' '   and
' '   not (deleted)
' ' ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ¼

    data query execute "chngprc4.dfq" index "chngprc2.idx"
' ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
' º [Supplier_Code] = suppcode                                         º
' '   and
'   left([Product_Code],1)<>"A"
' '   and
'   not (deleted)
' ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ¼

    if cerror
      messbox(" No records for this Supplier ",0,0,1)
      exit function
    end if

    data query execute "chngprc3.dfq" index "chngprc3.idx"
' ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
'   mid([Product_Code],3,1)<>"7"
'   and
'   not (deleted)
' ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ¼
    if cerror
      messbox(" No records for this Supplier ",0,0,1)
      exit function
    end if
  order sort execute dictionary "chngprc1.dfs" index "chngprc4.idx"
  nrindex = records

  repaint on
  repaint
  screen print 2 9 15 1 (format("  Code       Product Name               SqMt Cut SqMt Roll Bkg","M64"))
  screen print 3 9 15 1 (format("   {C}hange price  -  {Esc} to exit ","M64"))
  ptval=0
  while ptval <> {Esc}
    screen print 2 9 15 1 (format("  Code       Product Name               SqMt Cut SqMt Roll Bkg","M64"))
    screen print 22 9 15 1 (format("   {C}hange price  -  {Esc} to exit ","M64"))
    ptval = navrecs()
    if ptval = {C} or ptval = {c}
      Change()
    elseif ptval = {Esc}
      messbox(" Finished with this Supplier (y/n) ",1,0,1)
        if ptstr ! "y"
          screen clear box 1 1 sch scw 0 0 no-border
          repaint off
          exit function
         end if
      ptval = {^Home}
    end if
  end while
END FUNCTION
