'STATEMNT - prints Statement/Balance Requests for chosen customer

external   fentrybox() messbox() vloadif() vunloadif() dpath shopmask scr
external   sch scw progress() fgp bgp messline() wraptext() popuplist()
external   userid menuchoice cpath entryline() navrecs() messboxwait()
external   remove() makeidx() addidxrec() getidxrecs() ipath
external   Background() statemntpath fgi bgi progtag()
external   X_path _SWIP_Crystal() Xreppath

public     ptstr custcode dsa abbrv_name ptval custordname custaddr1 custaddr2 jobnr
public     custcity custpost custtitle $b psa #os_bal custname cr_limit $file

global     ReturnToMenu() jobdesc ftgcomm ftginstr ftgscrn y z slotrec #recs m1 m2
global     EnterCustName() x p1 p2 p3 p4 p5 p6 i y1 y2 y3 y4 PrepStatement()
global     ProcessStatement() ProcessItem() TitlesA() $index
global     SearchCustomers() $branch TitlesB() EnterComment() PrintRequest()
global     #bal2show ProcessAll() PrintSWIP() PrepStatementRED() PrintSWIPRED() BalReqst() 			'


MAIN
single-step off
  Background()
  error off
  #bal2show = 0
  while true
    file unload all
    x = EnterCustName()
    if x = -1
      exit while
    elseif x = 1
      continue while
    elseif x = 0
      x = SearchCustomers()
      if x = -1
        continue while
      elseif x = 1
        continue while
      end if
    end if
  end while

  ReturnToMenu()

END MAIN


FUNCTION ReturnToMenu()
  file unload all
  Background()
  transfer cpath|"pm_menu.psl" in-memory
END FUNCTION ' ReturnToMenu()


FUNCTION EnterCustName()
  	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
  	Background()
	vloadif(dpath|"statmnt1.vw")
'     	data query execute "not_del.dfq" index "not_del.idx"
' ' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' ' not (deleted)
' ' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  	order change key "[Abbrv_Name]"
  	data find "[Abbrv_Name]" equal abbrv_name options ""
  	if cerror
    		messboxwait(" Name"&chr(34)|abbrv_name|chr(34)|" not on file ",0,0,1)
    		return (1)
  	end if
END FUNCTION 'EnterCustName()


FUNCTION SearchCustomers()
	ptval=0
	repaint off
' 	vloadif(dpath|"statmnt1.vw")
'     	data query execute "not_del.dfq" index "not_del.idx"
' ' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' ' not (deleted)
' ' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
	while true
    		vloadif(dpath|"statmnt1.vw")
		repaint on
    		repaint
    		y1 = format(" Name                                Addressÿÿÿÿÿÿÿÿÿ","L71")
    		y2 = format(" {Enter} selects - {Esc} exits ","M71")
    		screen print 3 6 fgp bgp y1
    		screen print 19 6 fgp bgp y2
    		ptval = navrecs()
    		if ptval = {Enter}
      		repaint off
			if (deleted)
				messboxwait(" Deleted record - use another record for this customer ",0,0,1)
		    		continue while
			end if	
		     progress(15,10," Please wait ... calculating balance outstanding ",0)
		     custcode = [Customer_Code]       'message "custcode) is:"&str(custcode)
      		custname = [Customer_Name]       'message "custname) is:"&str(custname)
      		vloadif(dpath|"cust_ord.vws")
      		order change key "[Customer_Code]"
      		data query execute "statmnt2.dfq" index "x.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' ³ [Customer_Code] = custcode                                         ³
' ³ and                                                                ³
' ³ not (deleted)                                                      ³
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
      		if cerror
        			messbox(" No orders on active file for"&custname,0,0,1)
			     repaint off
        			continue while
      		end if
      		data query execute "statmnt1.dfq" index "x1.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'     days([Invoice_Date])>0
'     and
'     [Balance_Due]>0.01 or [Balance_Due]<0.01
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
     		if cerror
        			messbox(" No o/s balances on invoices for"&custname,0,0,1)
        			repaint off
     			continue while
     		end if
			#os_bal = filesum([Balance_Due]) 'message "#os_bal is:"&fixed(#os_bal,10)
     		if #os_bal > 0.01
     			messbox(" Outstanding balance is"&currency(#os_bal)|", prepare statement or Balance Request? (y/n) ",1,1,1)
        			if ptstr == "y"
          			vloadif(dpath|"statmnt1.vw")
          			repaint off

'           custcode    = [Customer_Code]
'           custordname = [Customer_Name]
'           custaddr1   = [Address_1]
'           custaddr2   = [Address_2]
'           custcity    = [City/Town]
'           custpost    = [Postcode]
'           custtitle   = [Title]

          			error off
					m1="ÿÿÿStatement"
 					m2="BalanceÿRequest"
 					x=popuplist(10,43,13,m1&m2,"",1,0)
 					if ptstr=m1
		          		x=ProcessStatement(0) ' all o/s balances
		          		vunloadif("statmnt2.vw")
          				continue while
 					else
		          		x=BalReqst() 			'
						if x = -1
	          				continue while
' 						     exit while
    						end if
		          		vunloadif("statmnt2.vw")
          				continue while
           			end if
		     	end if
	     	else
        			messbox(" No o/s balances on invoices for"&custname,0,1,1)
	        		repaint off
     	   		continue while
      		end if
		elseif ptval = {Esc}
     		Background()
      		error off
	      	window close
     	 	file unload all
      		return (-1)
	    	end if
	end while
END FUNCTION 'SearchCustomers()


FUNCTION ProcessStatement(n)
local $delrecs

	vloadif(dpath|"statmnt2.vw")
  	order change index "x1.idx"
  	x=remove("statemnt.idx")
  	x=makeidx("cust_ord","statemnt.idx","0",1)
	Background()
    	TitlesA()
    	ptval=0
    	while true
     	ptval = navrecs()
      	if ptval = {F10}
        		x = ProcessAll()
        		repaint off                      ' create INV_LIST
        		data goto record first
        		for i = 1 to records
          		if [Marker] = "¯"
     	       		x = addidxrec("statemnt.idx",precord,7)   ' add to temp index
	            		lock-record
              				[Marker] = ""
            			write-record
      		    	end if
          		data goto record next
        		end for
        		x = getidxrecs("statemnt.idx",2)
        		if x = 0
          		if ptval = 0
          	  		messbox(" No Orders selected ",0,0,1)
            			continue while
          		end if
        		end if

        		PrintSWIP()

          	exit while
      	elseif ptval = {Esc}
        		repaint off
        		screen clear box 1 1 sch scw 0 0 no-border
        		data goto record first
        		return (-1)
     	end if
    	end while
	vunloadif("statmnt2.vw")
END FUNCTION ' ProcessStatement()


FUNCTION BalReqst()
'   vloadif(dpath|"ordstat4.vw")
	while true
		vloadif(dpath|"inv_1.vw")
		order change key "[Job_Nr]"
		x = fentrybox(" Enter Order Nr or {Esc} to exit ",6,shopmask,"")
		if x = -1
    			return (-1)
	  	elseif x = 0
    			jobnr = ptstr
			exit while
	  	end if
	end while

' message "jobnr is:"&str(jobnr)

	data find "[Job_Nr]" equal jobnr options ""
	if cerror                               '   if none - then return
		messboxwait(" Job Nr"&jobnr&"not found ",0,0,1)
		return (1)
	end if
	if [Balance_Due]<1                               '   no balance O/S
		messboxwait(" No Balance Outstanding ",0,0,1)
		return (1)
	end if

  	x=PrintRequest()
  	if x = -1
  		repaint off
  		return (-1)
  	end if

END FUNCTION ' BalReqst()


FUNCTION PrintRequest()
	Background()
  	$index="onlyone.idx"
  	$file="cust_ord"
  	remove($index)
  	x = makeidx($file,$index,str(precord),3)    '   message "x is:"&str(x)
  	order change index $index
	
    	remove(X_path|"X_inv_a.*")
    	data query execute "not_del.dfq" Smart4 X_path|"X_inv_a" fields "[1|"|str(dbinfo(db_fields))|"]"
    	vunloadif("X_inv_a.vws")
    	vloadif(dpath|"variat_n.vws")
    	remove(X_path|"X_inv_b.*")
    	data query execute "allVreqn.dfq" Smart4 X_path|"X_inv_b" fields "[1|"|str(dbinfo(db_fields))|"]"
    	vunloadif("X_inv_b.vws")

      _SWIP_Crystal(Xreppath|"balreqst","S",0,1,"")

	vloadif(dpath|"comments.vws")
  	data enter lock
    		[Job_Nr]   =jobnr
    		[EnterDate]=today
    		[Comment]  ="Balance request letter"
    		[Author]   =userid
    		[Category] ="B"
  	write-record
  	vunloadif("comments.vws")
	vunloadif("inv_1.vw")

END FUNCTION ' PrintRequest()


FUNCTION EnterComment()
local $ordnote
' message "$ord is:"&str($ord)
	$ordnote = "Statement"
  	vloadif(dpath|"comments.vws")
  	data enter lock
    		[Job_Nr]   =jobnr
    		[EnterDate]=date2(today)
    		[Comment]  ="Statement sent"
    		[Author]   =userid
    		[Category] ="C"
  	write-record
  	vunloadif("comments.vws")

' message "jobnr is:"&str(jobnr)
' message "$ordnote is:"&str($ordnote)
'   data enter lock
'     [Job_Nr]   =jobnr
'     [EnterDate]=today
'     [Comment]  =$ordnote
'     [Author]   =userid
'     [Category] = "C"
'   write-record
'   vunloadif("comments.vws")
END FUNCTION ' EnterComment()


FUNCTION ProcessAll()
  for i = 1 to records
    if abs([Balance_Due]) > .01
      lock-record
        [Marker] = "¯"
      write-record
      #bal2show = #bal2show + [Balance_Due]
    end if
    data goto record next
  end for
  return (0)
END FUNCTION ' ProcessAll()


FUNCTION ProcessItem()
  if [Marker] = "¯"
    lock-record
      [Marker] = ""
    write-record
    #bal2show = #bal2show - [Balance_Due]
  else
    lock-record
      [Marker] = "¯"
    write-record
    #bal2show = #bal2show + [Balance_Due]
  end if
  return (0)
END FUNCTION ' ProcessItem()


FUNCTION TitlesA()
  repaint on
  repaint
  y1 = format("Prepare statement for"&custordname,"M78")
  y2 = format("  Site Address                                      Fitted     Total   Balance","L80")
'   y3 = format(" ¯ = included - {A}ll invoices - add/remove {I}nvoice - {F10} finishes - {Esc} ","M80")
  y3 = format(" {F10} to continue - {Esc} exits ","M80")
  screen clear box 1 1 3 80 fgp bgp
  screen print 2 2 fgp bgp y1
  screen print 4 1 fgp bgp y2
  screen print 22 1 fgp bgp y3
END FUNCTION ' TitlesA()


FUNCTION TitlesB()
  screen clear box 21 1 22 scw 0 0 no-border
  y3 = format(" ¯ = included - {A}ll invoices - add/remove {I}nvoice - {F10} finishes - {Esc} ","M80")
  screen print 22 1 fgp bgp y3
  y4 = format(" Balance shown on statement will be"&currency(#bal2show),"M80")
  screen print 23 1 fgp bgp y4
END FUNCTION ' TitlesB()


FUNCTION PrepStatement()
'   custaddr1   = [Address_1]
'   custaddr2   = [Address_2]
'   custcity    = [City/Town]
'   custpost    = [Postcode]
'   custtitle   = [Title]
  vloadif(dpath|"statmnt2.vw")
  order change index "x1.idx"
  x = remove("statemnt.idx")
  if x = -1
  ' message "remove() failed"
  end if

  x = makeidx("cust_ord","statemnt.idx","0",1)
  if x = -1
  ' message "makeidx() failed"
  end if

  vloadif(dpath|"statmnt2.vw")
  order change key "[Customer_Code]"
  data query execute "statmnt4.dfq" index "x.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   days([Invoice_Date])>0
'   and
'   [Balance_Due]>0.01 or [Balance_Due]<0.01
'   and
'   [Customer_Code]=custcode
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    messbox(" No orders on active file for"&custname,0,1,1)
    repaint off
  else
    #os_bal = filesum([Balance_Due])   'message "#os_bal is:"&str(#os_bal)
'     #os_inv = records
' message "#os_inv is:"&str(#os_inv)
    if #os_bal > 1
      messbox(" Due from"&custname&"-"&currency(#os_bal)&"- print statement? (y/n) ",1,1,1)
      if ptstr == "y"
        x = ProcessAll()
        repaint off                      ' create INV_LIST
        data goto record first
        for i = 1 to records
          if [Marker] = "¯"
            x = addidxrec("statemnt.idx",precord,7)   ' add to temp index
            lock-record
              [Marker] = ""
            write-record
          end if
          data goto record next
        end for
        x = getidxrecs("statemnt.idx",2)
        if x = 0
          if ptval = 0
            messbox(" No Orders selected ",0,0,1)
            return (1)
          end if
        end if
        order change index "statemnt.idx"
        order sort now dictionary "ptg_stat" fields "[Invoice_Date]" ascending
        PrintSWIP()
        return (0)
      end if
    end if
  end if
END FUNCTION ' PrepStatement()


FUNCTION PrintSWIP()
	vunloadif("X_stmnt.vws")
	x=remove(X_path|"X_stmnt.*")
' 	if x<>0
' 		messboxwait(" Print file in Xtal_data not removed ",0,0,1)
' 	end if
	vloadif(dpath|"statmnt4.vw")
	order change index "statemnt.idx"
	order sort now dictionary "ptg_stat" fields "[Invoice_Date]" ascending
	data query execute "not_del.dfq" Smart4 X_path|"X_stmnt" fields "[Job_Nr|Title]"
	vunloadif("statmnt4.vw")
	_SWIP_Crystal(Xreppath|"X_stmnt","S",0,1,"")
	x=remove(X_path|"X_stmnt.*")
' 	if x<>0
' 		messboxwait(" Print file in Xtal_data not removed ",0,0,1)
' 	end if
END FUNCTION ' PrintSWIP()


FUNCTION PrintSWIPRED()
	vunloadif("X_stmnt.vws")
	x=remove(X_path|"X_stmnt.db")
' 	if x<>0
' 		messboxwait(" L349 DB print file in \Xtal_data not removed ",0,0,1)
' 	end if
	x=remove(X_path|"X_stmnt.vws")
' 	if x<>0
' 		messboxwait(" L353 VWS print file in \Xtal_data not removed ",0,0,1)
' 	end if
  vloadif(dpath|"statmnt4.vw")
  order change index "statemnt.idx"
  order sort now dictionary "ptg_stat" fields "[Invoice_Date]" ascending
  data query execute "not_del.dfq" Smart4 X_path|"X_stmnt" fields "[Job_Nr|Br_Phone]"
	vunloadif("X_stmnt.vws")
	_SWIP_Crystal(Xreppath|"X_stmtR","S",1,1,"")
' 	x=remove(X_path|"X_stmnt.*")
' 	if x<>0
' 		messboxwait(" Print file in Xtal_data not removed ",0,0,1)
' 	end if
END FUNCTION ' PrintSWIPRED()


FUNCTION PrepStatementRED()
'   custaddr1   = [Address_1]
'   custaddr2   = [Address_2]
'   custcity    = [City/Town]
'   custpost    = [Postcode]
'   custtitle   = [Title]
  vloadif(dpath|"statmnt2.vw")
  order change index "x1.idx"
  x = remove("statemnt.idx")
  if x = -1
  ' message "remove() failed"
  end if

  x = makeidx("cust_ord","statemnt.idx","0",1)
  if x = -1
  ' message "makeidx() failed"
  end if

  vloadif(dpath|"statmnt2.vw")
  order change key "[Customer_Code]"
  data query execute "statmnt4.dfq" index "x.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   days([Invoice_Date])>0
'   and
'   [Balance_Due]>0.01 or [Balance_Due]<0.01
'   and
'   [Customer_Code]=custcode
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    messbox(" No orders on active file for"&custname,0,1,1)
    repaint off
  else
    #os_bal = filesum([Balance_Due])   'message "#os_bal is:"&str(#os_bal)
    if #os_bal > 1
      messbox(" Due from"&custname&"-"&currency(#os_bal)&"- print statement? (y/n) ",1,1,1)
      if ptstr == "y"
        x = ProcessAll()
        repaint off                      ' create INV_LIST
        data goto record first
        for i = 1 to records
          if [Marker] = "¯"
            x = addidxrec("statemnt.idx",precord,7)   ' add to temp index
            lock-record
              [Marker] = ""
            write-record
          end if
          data goto record next
        end for
        x = getidxrecs("statemnt.idx",2)
        if x = 0
          if ptval = 0
            messbox(" No Orders selected ",0,0,1)
            return (1)
          end if
        end if
        order change index "statemnt.idx"
        order sort now dictionary "ptg_stat" fields "[Invoice_Date]" ascending
        PrintSWIPRED()

'enter COMMENTS record
        return (0)
      end if
    end if
  end if
END FUNCTION ' PrepStatementRED()

