COMPILE SUBROUTINE Print_Bookings(Dummy)

/*
	Print Bookings Report
	08/07/2005 - John C. Henry, J.C. Henry & Co., Inc. - Initial coding
*/


DECLARE SUBROUTINE Utility, ErrMsg, Set_Status, Set_Printer, RList
DECLARE FUNCTION Set_Printer, Get_Printer, Msg, Get_Status, Printer_Select, obj_Install, Dialog_Box

$INSERT OIPRINT_EQUATES
$INSERT INVOICE_EQU
$INSERT APPCOLORS
$INSERT MSG_EQUATES


EQU TAB$				TO \09\
EQU TARGET_ACTIVELIST$	TO 5


ErrorTitle = 'Error in stored procedure Print_Bookings'

OPEN 'ORDER' TO OrderTable ELSE
	ErrorMsg = 'Unable to open "ORDER" table.'
	ErrMsg(ErrorTitle:@SVM:ErrorMsg)
	RETURN
END

OPEN 'DICT.ORDER' TO @DICT ELSE
	ErrorMsg = 'Unable to open "ORDER" table.'
	ErrMsg(ErrorTitle:@SVM:ErrorMsg)
	RETURN
END

DateRange = Dialog_Box('DIALOG_DATE_RANGE',@WINDOW,'')

StartDt = DateRange<1>
EndDt	= DateRange<2>

Set_Status(0)

SelectSent  = 'SELECT ORDER WITH ENTRY_DATE FROM ':QUOTE(StartDt):' TO ':QUOTE(EndDt):' BY CUST_NAME'


RList(SelectSent,TARGET_ACTIVELIST$,'','','')
IF Get_Status(errCode) THEN
	ErrMsg(errCode)
	RETURN
END

* Start of printing process

FileName		= 'Print Bookings by Company Report'
Title			= 'Printing Bookings by Company'

TopMargin		= 1.0
BottomMargin	= 0.75
LeftMargin		= 0.5
RightMargin		= 0.5

Margins = LeftMargin:@FM:TopMargin:@FM:RightMargin:@FM:BottomMargin

PageSetup	= '0'	;* Landscape = 1
PrintSetup	= '2'	;* Preview Normal
PrintPath	= Printer_Select()

stat = Set_Printer('INIT',FileName,Title,Margins,PageSetup,PrintSetup,PrintPath)
IF stat < 0 THEN GOTO OIPrintErr

DateRange = 'With Order Dates From ':StartDt:' Thru ':EndDt

Header = "'D'":@VM:obj_Install('Get_Prop','CompTitle'):' Bookings by Company Report':@VM:"Page  'P'"
Header<-1> = "'T'":@VM:DateRange
Header<-1> = ''																;* Blank line following heading

font	= 'Arial'
font<2>	= '10'
font<4>	= '1'		;* Bold

stat = Set_Printer('FONTHEADFOOT',font)	; IF stat < 0 THEN GOTO OIPrintErr
stat = Set_Printer('HEADER',Header)		; IF stat < 0 THEN GOTO OIPrintErr

Footer = " 'D' 'T'":@VM:@VM:"Page:  'P'"
stat = Set_Printer('FOOTER',Footer)		; IF stat < 0 THEN GOTO OIPrintErr

@RECCOUNT= 0

FirstPass	= 1
LastRecord	= 0
FirstLine	= 1

* Make Column Heading

colHead = ''								; colFmt = ''
colHead<1,1> 	= 'Cust'					; colFmt<1,1>	= '^720'
colHead<1,2>	= 'Customer Name'			; colFmt<1,2>	= '<6480'
colHead<1,3>	= 'Order Qty'				; colFmt<1,3>	= '>1800'
colHead<1,4>	= 'Order Amount'			; colFmt<1,4>	= '>1800'

* Zero Accumulators For Each Break

Order_Qty.Cust_No.Total = 0
Order_Qty.Cust_No.Count = 0
Order_Qty.Grand.Total = 0
Order_Qty.Grand.Count = 0

Order_Amount.Cust_No.Total = 0
Order_Amount.Cust_No.Count = 0
Order_Amount.Grand.Total = 0
Order_Amount.Grand.Count = 0

* Null Previous Break Buckets

Prev.Cust_No = ''
Prev.Cust_Name = ''
Last.Cust_No.Break = 1


* * * * * * *
ReadRecord:
* * * * * * *

* Zero Break Flags To False

Cust_No.Break = 0

READNEXT @ID, WhichValue ELSE
	LastRecord			= 1
	Cust_No.Break		= 1 
	Cust_No				= Prev.Cust_No
	Cust_Name			= Prev.Cust_Name
END

S.ATID = @ID

IF FirstPass AND LastRecord THEN
 	GOTO Bail
END
 
IF LastRecord THEN GOTO BREAKS

READO @RECORD FROM OrderTable,@ID ELSE
	GOTO ReadRecord
END

@RECCOUNT += 1
 
* Calculate Value(s) For Column(s)
 
S.ATID			= { @ID}
I.ATID			= S.ATID
S.Cust_No		= {CUST_NO}
I.Cust_No		= S.Cust_No

S.Cust_Name		= {CUST_CITY}

SWAP '/' WITH ' - ' IN S.Cust_Name

I.Cust_Name		= S.Cust_Name

S.Order_Date		= {ENTRY_DATE}<1,WhichValue>

IF S.Order_Date < ICONV(StartDt,'D') OR S.Order_Date > ICONV(EndDt,'D') THEN GOTO ReadRecord	;* Limit multivalues to date range.


* Initialize Multivalue Break Check Variables

M.Order_Qty		= {ITEM_QTY}<1,WhichValue> ; P.Order_Qty = 1 ; C.Order_Qty = 7
S.Order_Qty		= M.Order_Qty

M.Order_Amount	= {ITEM_AMT}<1,WhichValue> ; P.Order_Amount = 1 ; C.Order_Amount = 7
S.Order_Amount 	= M.Order_Amount

Previous.Mark.Level= 0

* Test For Control Break(s)
 
IF (S.Cust_No NE Prev.Cust_No) OR Cust_No.Break THEN
	Cust_No = Prev.Cust_No
	Prev.Cust_No = S.Cust_No
	
	Cust_Name = Prev.Cust_Name
	Prev.Cust_Name = S.Cust_Name
	
	Cust_No.Break += 1
END

 
IF FirstPass THEN
	FirstPass= 0
	GOTO Detail
END


* * * * * * * 
BREAKS:
* * * * * * *

* Print Break Total(s) And Accumulate Total(s)

IF Cust_No.Break THEN

	colData = Cust_No
	colData<1,2> = Cust_Name
	colData<1,3> = OCONV(Order_Qty.Cust_No.Total ,"MD0,")
	colData<1,4> = OCONV(Order_Amount.Cust_No.Total ,"MD2,$")
		
	GOSUB PrintTable
	
	Order_Qty.Grand.Total+=Order_Qty.Cust_No.Total		; Order_Qty.Cust_No.Total = 0
	Order_Qty.Grand.Count+=Order_Qty.Cust_No.Count		; Order_Qty.Cust_No.Count = 0
	
	Order_Amount.Grand.Total+=Order_Amount.Cust_No.Total	; Order_Amount.Cust_No.Total = 0
	Order_Amount.Grand.Count+=Order_Amount.Cust_No.Count	; Order_Amount.Cust_No.Count = 0

END

* Perform Last Record Output If Done

IF LastRecord THEN

	colData = ""
	colData<1,1> = ''
	colData<1,2> = 'Report Totals:'
	colData<1,3> = OCONV(Order_Qty.Grand.Total ,"MD0,")
	colData<1,4> = OCONV(Order_Amount.Grand.Total ,"MD2,$")
	
	GOSUB PrintTable
	
	status = Set_Printer('TEXT','')
	status = Set_Printer('TEXT',@RECCOUNT:' Records Processed')
	
	GOTO Bail

END

* * * * * * * 
DETAIL:
* * * * * * *
 
* Remove Appropriate Value FromMulti-Valued Column(s)

New.Mark.Level = 0

IF C.Order_Qty GE PREVIOUS.MARK.LEVEL THEN
	REMOVE I.Order_Qty FROM M.Order_Qty AT P.Order_Qty SETTING C.Order_Qty
	S.Order_Qty=I.Order_Qty
END
IF C.Order_Qty GT New.Mark.Level THEN New.Mark.Level = C.Order_Qty

IF C.Order_Amount GE PREVIOUS.MARK.LEVEL THEN
	REMOVE I.Order_Amount FROM M.Order_Amount AT P.Order_Amount SETTING C.Order_Amount
	S.Order_Amount=I.Order_Amount
END
IF C.Order_Amount GT New.Mark.Level THEN New.Mark.Level=C.Order_Amount

* Do Conversions If Any
 
IF S.Cust_No NE ""		THEN S.Cust_No = OCONV(S.Cust_No,"MD0")
IF S.Order_Qty NE "" 	THEN S.Order_Qty=OCONV(S.Order_Qty,"MD0")
IF S.Order_Amount NE "" 	THEN S.Order_Amount=OCONV(S.Order_Amount,"MD2,$")

 
* Accumulate Total(s)
 
IF NUM(I.Order_Qty) AND I.Order_Qty NE "" THEN
	Order_Qty.Cust_No.Total += I.Order_Qty
	Order_Qty.Cust_No.Count += 1
	I.Order_Qty = ''
END
IF NUM(I.Order_Amount) AND I.Order_Amount NE "" THEN
	Order_Amount.Cust_No.Total += I.Order_Amount
	Order_Amount.Cust_No.Count += 1
	I.Order_Amount = ''
END
 
* Print Detail Line
 
colData = ""
colData<1,1> = S.Cust_No
colData<1,2> = S.Cust_Name
colData<1,3> = S.Order_Qty
colData<1,4> = S.Order_Amount

*GOSUB PrintTable

IF New.Mark.Level THEN
	S.Cust_No			= ''
	S.Cust_Name			= ''
	S.Order_Qty			= ''
	S.Order_Amount		= ''
	Previous.Mark.Level	= New.Mark.Level
	GOTO Detail
END

GOTO ReadRecord
 

* * * * * * *
Bail:
* * * * * * *

stat = Set_Printer('TERM',1)

RETURN


* * * * * * *
OIPrintErr:
* * * * * * *

ErrMsg(ErrorTitle:@SVM:ErrorMsg)
ErrMsg(ErrorTitle:@SVM:'Set_Printer returned errorcode ':stat)
stat = Set_Printer('TERM',1)

RETURN


* * * * * *
PrintTable:
* * * * * * 


stat = Set_Printer('CALCTABLE',colFmt:@FM:colData)
TableSize = Get_Printer('CALCTABLE')
TableHeight = TableSize<2>
Test = Get_Printer('POS')<2>

IF Get_Printer('POS')<2> + TableHeight > 9.00 OR FirstLine THEN
	IF NOT(FirstLine) THEN
		stat = Set_Printer('PAGEBREAK')
	END
	FirstLine = 0
	font<2> = 10
	font<4> = 1		;* Bold
	stat = Set_Printer('FONT',font,'100')

	stat = Set_Printer('ADDTABLE',colFmt,colHead,'',LTGREY$,'',0,TB_ALL)
	
	
	font<4> = 0
	stat = Set_Printer('FONT',font,fontSpacing)
	
	stat = Set_Printer('ADDTABLE',colFmt,'',colData,LTGREY$,'',0,7)
	
END ELSE
	font<2> = 10
	font<4> = 0
	stat = Set_Printer('FONT',font,fontSpacing)
	stat = Set_Printer('ADDTABLE',colFmt,'',colData,LTGREY$,'',1,TB_ALL)

END
	
							
RETURN	 


