'RDY4INVC - checks whether Trade req'ns are complete (Allocated or Received);
'           if so, creates a new Cust_ord.db record and marks it as `Completed';
'           changes all other records to new JobNr

external  dpath vloadif() Background() messbox() sch scw cpath progress()
external  progtag() fgi bgi ipath increment() userid vatrate PrintReport()
external  messline() gdsoutpath chkstr() messboxwait()

public    ptstr jobnr custcode ptval docref custname

global    x i ReturnToMenu() cr_limit EnterVarnOrder() WriteDetails()
global    cr_status mu_profile FindReqns() AddVarn() #gross #addn varnr y1 y3
global    p1 p2 p3 p4 p5 p6  StoreReport() $shopuse #total
global    balancedue lastbal newtotal oldtotal newnet oldnet #prec #as #sr


MAIN
local fr
single-step off

  messboxwait(" No longer in use - old Trade system superceded ",0,0,1)
  ReturnToMenu()

  Background()
'(str,str,str,int,int,int)
'   p1 =                  ' p1 = report definition ("ord_stck.dfr")
  p2 = ""               ' p2 = title at top of choice popup ("LABEL")
  p3 = 1                ' p3 = printer to be used (1=HPIII_QC; 2=GEN_EPSN etc)
  p4 = 1                ' p4 = printer port to use (1,2 etc - network set to use 2=LASER; 3=LABEL)
  p5 = 1                ' p5 = choose VIEW/PRINT 1=PRINT; 2=VIEW; 3=CHOOSE
  p6 = 2                ' p6 = nr of copies
  file unload all
  $shopuse = "T02221 T11071 T02762 T11068 T02258 T02293"

  vloadif(dpath|"customer.vws")
  order change index ipath|"collectn.idx"
  repaint off
  progress(15,10," Looking for allocations/deliveries to invoice ",1)
  for i = 1 to records
    balancedue= 0
    newtotal  = 0
    newnet    = 0
    #gross    = 0
    custcode  = [Customer_Code]
' if custcode="T05204"
' repaint on
' repaint
' single-step on
' end if
    custname  = [Customer_Name]
    cr_limit  = [Credit_Limit]
    progtag(fgi,bgi,custname)
    fr = FindReqns()
    progress(15,10," Looking for allocations/deliveries to invoice ",1)
    vloadif(dpath|"customer.vws")
    if record = records
      exit for
    end if
    data goto record next
  end for

  ReturnToMenu()

END MAIN


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 FindReqns()
  vloadif(dpath|"r4invc_1.vw")
' message "custcode is:"&str(custcode)
  data query execute "r4inv_1.dfq" index "r4inv_1.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   [CustCode] = custcode and not (deleted)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
'     messbox(" NO req'ns for"&custname|" ",0,0,1)
    order change physical
    return (1)
  end if

  data query execute "r4inv_3.dfq" index "r4inv_2.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' ³ [Job_Nr]==left([Reference_Nr],6)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
'     messbox(" NO req'ns for"&custname|" ",0,0,1)
    order change physical
    return (1)
  end if

  data query execute "r4inv_2.dfq" index "r4inv_3.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   [RollNr]<>"BESPOK" and [RollNr]<>"00000/00"
'   and
'   [Item_Type]<>"A"
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
'     messbox(" NO delivered/allocated req'ns for"&custname|" ",0,0,1)
    order change physical
    return (1)
  end if

  y1 = format(left("Allocations/deliveries for"&custname&"("|custcode|")",72),"M72")
  y3 = format("  Description                    Colour              Length  ÿÿRollNr","L72")
  repaint on
  repaint
  screen print 5 5 15 1 y1
  screen print 6 5 15 1 y3
  repaint off

' message "custcode is:"&str(custcode)
' message "$shopuse is:"&str($shopuse)
  if chkstr(custcode,$shopuse) = 0
    x = messbox(" Invoice is for Shop use! Continue & invoice? (y/n) ",1,0,1)
    if ptstr == "n"
      Background()
      progress(15,10," Looking for allocations/deliveries to invoice ",1)
      order change physical
      return (1)
    end if
  end if

  x = messline(" Invoice req'ns for"&custname|"? (y/n) ",1,1,1,21,5,72)
' message "x is:"&str(x)
  if ptstr == "n"
    Background()
    progress(15,10," Looking for allocations/deliveries to invoice ",1)
    order change physical
    return (1)
  end if

  progress(15,10," Creating order for"&custname|" ",0)
  increment(dpath|"tradenrs.dat",1)
  jobnr = "T"|right("00000"|str(ptval),5) ' message "jobnr is:"&str(jobnr)

  WriteDetails()

  EnterVarnOrder()

' message "Print Allocated/Received List"

  vloadif(dpath|"requsn.vws")
'   order change index "r4inv_2.idx"
  order change index "r4inv_3.idx"
  order sort execute dictionary "lst_stck" index "lst_stck"
  PrintReport("collect4.dfr","",p3,p4,p5,p6) 'priced to go with invoice
  StoreReport("collect4.dfr","sch")

'   UpdateDocRef()                    'update GOODSOUT with docref

END FUNCTION  ' FindReqns()


FUNCTION EnterVarnOrder()
  vloadif(dpath|"requsn.vws")
'   order change index "r4inv_2.idx"
  order change index "r4inv_3.idx"
' message "jobnr for varnr is:"&str(jobnr) message "#total is:"&str(#total)
  for i = 1 to records
    #addn = [Retail]
' message "#addn is:"&str(#addn)
    #total = #total + #addn
'mark as invoiced & with new Order Nr
    lock-record
      [Job_Nr]=jobnr
    write-record
    data goto record next
  end for
' message "#total is:"&str(#total)
  #gross = #total*(100+vatrate)/100
' message "#gross is:"&str(#gross)
  varnr = jobnr|"-00"
  AddVarn(varnr,#gross,"Goods received or allocated","Collection","COLLECT",today)
  #gross = 0
  #total = 0
END FUNCTION ' EnterVarnOrder()


FUNCTION AddVarn(varnr,#gross,$reas,$ref,$notif,$varndate)
  vloadif(dpath|"addvarn3.vw")
  data enter lock
    [Var_Nr]        = varnr
    [VarnJobNr]     = left(varnr,6)
    [Amount_Gross]  = #gross
    [Reason]        = $reas
    [Customers_Ref] = $ref
    [Notif_Method]  = $notif
    [Date]          = $varndate
    [Entered_By]    = userid
  write-record

  lastbal = round([Balance_Due],2)
  balancedue = lastbal + #gross
  oldtotal = round([Invoice_Total],2)
  newtotal = oldtotal + #gross
  oldnet   = round([Net_Invoice],2)
'   newnet   = oldnet + round(#gross*100/(100+vatrate),2)
  newnet   = oldnet + #total
  lock-record
    [Balance_Due]   = balancedue
    [Invoice_Total] = newtotal
    [Net_Invoice]   = newnet
    [Order_Status]  = "U"
    [Completed]     = "Y"
  write-record
END FUNCTION 'AddVarn()


FUNCTION WriteDetails()         ' write customer & job details to CUSENT3B
  vloadif(dpath|"cust_ord.vws")
  data enter lock
    [Customer_Code]      = custcode
    [Job_Nr]             = jobnr
    [Type_Branch]        = "H"
    [Branch]             = left(jobnr,1)
    [SalesAnalysis]      = "T"
    [Order_Status]       = "P"
    [Date_Of_Order]      = today
    [Last_Update]        = today
    [Updated_By]         = userid
    [Description]        = "Trade order"
    [Delivery_Address_1] = "Goods received or allocated"
    [CustOrd_Name]       = custname
    [Abbrv_Name]         = left(custname,7)
    [Invoice_Total]      = 0
    [Net_Invoice]        = 0
    [Balance_Due]        = 0
    [PDA]                = "N"
    [Origin]             = "E"
    [Completed]          = "Y"
  write-record
END FUNCTION ' WriteDetails()


FUNCTION StoreReport(p1,ext)
local despdoc collectidx
  despdoc = gdsoutpath|jobnr|"."|ext  'message "dlyrep is:"&str(dlyrep)
  print report execute p1 disk despdoc detail start 1 end 0 copies 1
'   collectidx = gdsoutpath|docref|".idx"  'message "collectidx is:"&str(collectidx)
'   remove(collectidx)
'   makeidx("goodsout",collectidx,"0",1)
' 'save index
'   for i = 1 to records
'     x = addidxrec(collectidx,precord,7)  'message "x is:"&str(x)
'     data goto record next
'   end for
END FUNCTION ' StoreReport()




