'PrintCnf - print Order Confirmation

external  vloadif() Background() userid cpath dpath fentrybox() $menu
external  shopmask messbox() remove() makeidx() vatrate messboxwait()
external  popuplist() vunloadif()
external   X_path _SWIP_Crystal() Xreppath

public    jobnr ptstr #due #gross #net #vat #receipts

global    FindJobNr() OrderSheet() ReturnToMenu() x #jobrec custname deladdr1


MAIN
single-step off
  	Background()
  	file unload all

  	FindJobNr()

  	ReturnToMenu()

END MAIN


FUNCTION ReturnToMenu()
  	Background()
  	file unload all
  	transfer cpath|"pm_menu.psl" in-memory
END FUNCTION ' ReturnToMenu()


FUNCTION FindJobNr()                   ' finds Job & updates Cust_Ord
local l1 c3 c2 c1 ques $reqstr fj
  	Background()
  	while true
    		x=fentrybox(" Print Order Confirmation for Order Nr ",6,shopmask,"")
    	if x = -1
      return (-1)                      ' ReturnToMenu
    end if
    jobnr = ptstr
    vloadif(dpath|"cust_ord.vws")
    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
    end if
    if [Order_Status]="A"
      messbox(" Order details must be entered before printing ",0,0,1)
      return (-1)                      ' ReturnToMenu
    end if
    custname    = [CustOrd_Name]
    deladdr1    = [Delivery_Address_1]
    x = messbox(" "|custname&"@"&deladdr1|"?(y/n)",1,1,1)
    if x = 0
      if ptstr == "n"
        fj = -1
        continue while
      else
        OrderSheet()
        return (0)
      end if
    end if
    repaint off
  end while
END FUNCTION ' FindJobNr()


FUNCTION OrderSheet()
local $index $file p1 p2 p3 p4 p5 p6
'   p2 = "Confirmation"   ' 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 = 1                ' p6 = nr of copies

  	Background()
  	$index = "onlyone.idx"
  	$file = "cust_ord"
  	remove($index)
  	x = makeidx($file,$index,str(precord),3)    '   message "x is:"&str(x)
  	vloadif(dpath|"intordr1.vw")
  	order change index $index
  	if [ConfPrt]="Y"
    		messboxwait(" Order Confirmation already printed ",0,0,1)
    		return (1)
  	end if
  	#due = [Balance_Due]                 '
  	if #due > 0
    		#due = "˙Balance due"&currency(#due)
  	elseif #due = 0
    		#due = "˙˙˙Order paid in full"
  	end if
  	#gross = [Invoice_Total]             '
  	#receipts = "˙˙Payments received by"&date2(today)&"˙˙"&currency(tablesum([Amount_Received]))
  	#net = #gross/((100+vatrate)/100)    'message "#net is:"&str(#net)
  	#vat = #gross - #net                 'message "#vat is:"&str(#vat)
'   #net   = currency(#net)
'   #gross = currency(#gross)
'   #vat   = currency(#vat)
  	#net   = "Net:"&currency(#net)
  	#gross = "Total Order value:"&currency(#gross)
'   #vat   = "VAT at 20%:"&currency(#vat)
  	#vat="VAT at"&str(vatrate)|"%:"&currency(#vat)


'choose type of report

	messboxwait(" Insert Logo'd paper for Order Confirmation ",0,1,1)
    	while true
      	remove(X_path|"X_ordcnf.*")
      	data query execute "not_del.dfq" Smart4 X_path|"X_ordcnf" fields "[Job_Nr|Order_Status]"
      	vunloadif("X_ordcnf.vws")
      	_SWIP_Crystal(Xreppath|"ord_conf","S",0,1,"")

	     messbox(" Has Order Nr"&jobnr&"printed correctly? (y/n) ",1,1,1)
     	if ptstr == "y"                      '  store file & exit
			vloadif(dpath|"intordr1.vw")
	        	lock-record                        ' update record with inv nr & date
     	     	[ConfPrt]="Y"
        		write-record
	        	return (0)
		else
		     messbox(" Abandon and print later? (y/n) ",1,1,1)
        		if ptstr == "y"                      '  store file & exit
          		return (0)
        		else
          		continue while
        		end if
      	end if
    	end while
END FUNCTION ' OrderSheet()
