compile SUBROUTINE Print_Order( OrderNo, Confirmation )

DECLARE FUNCTION SECURITY_CHECK, SET_PRINTER, GET_PRINTER, MSG, FIELDCOUNT, ENTID, REPOSITORY, PRINTER_SELECT
DECLARE FUNCTION OBJ_INSTALL
DECLARE SUBROUTINE SECURITY_ERR_MSG

$INSERT SECURITY_RIGHTS_EQU
$INSERT OIPRINT_EQUATES
$INSERT MSG_EQUATES
$INSERT QUOTE_EQU
$INSERT ORDER_EQU
$INSERT ORDER_DET_EQU
$INSERT WO_LOG_EQU
$INSERT WO_STEP_EQU
$INSERT APPCOLORS

$INSERT COC_EQU

EQU PI$LEFT		TO 1
EQU PI$TOP		TO 2
EQU PI$RIGHT	TO 3
EQU PI$BOTTOM	TO 4
EQU PI$WIDTH	TO 5
EQU PI$HEIGHT	TO 6
EQU PI$SIZE		TO 7


IF NOT(ASSIGNED(OrderNo)) THEN RETURN
IF NOT(ASSIGNED(Confirmation)) THEN Confirmation = ''

* * *   PRINT SETUP   * * *

PageInfo			= ''
PageInfo<PI$LEFT>	= 0.25		;* Margins
PageInfo<PI$TOP>	= 1.35
PageInfo<PI$RIGHT>	= 0.25
PageInfo<PI$BOTTOM>	= 0.50

PageSetup	= 1					;* 1 = Landscape mode

PrintSetup	= ''
PrintSetup<1,1>	= '2'	;* Preview Normal
PrintSetup<1,2> = '5'	;* Display all buttons except Printer Setup
PrintSetup<1,3>	= '0'	;* Display Printing Window
PrintSetup<1,6> = '7'	;* Preview window - keyboard and mouse support

stat = Set_Printer("INIT",'Printing','Order',PageInfo,PageSetup,PrintSetup)

font = 'Arial'		;* Font basics
font<2> = 10
font<4> = 0

stat = Set_Printer('FONTHEADFOOT', font)

stat = Set_Printer('FOOTER',"Page 'P' ":@VM:@VM:"'T' 'D'")

stat = Set_Printer('LINESTYLE', PS_SOLID:@FM:2)
stat = Set_Printer('LINE', -0.1:@FM:-1.2:@FM:10.50:@FM:-1.2, 1)
stat = Set_Printer('LINE', -0.1:@FM:-0.1:@FM:10.50:@FM:-0.1, 1)
 
location = -.15:@fm:-1.150:@fm:1.57:@fm:1
stat = Set_Printer('BMP',obj_Install('Get_Prop','ColorBMP'),location, 0,1)

* * * *  Added 10/8/2015 JCH - Updated 'CONFIG','COMPANY'  * * * *

InstDat		= obj_Install('Get_Prop','Company':@FM:'Division')
Company		= InstDat<1>
Division	= InstDat<2>
	
font	= 'Arial'
font<2>	= '12'		;* Big type
font<4>	= 1			;* Bold on
font<5> = 0			;* Italics

stat = Set_Printer('TEXTXY',Company,8.75:@FM:-1.15,font,1)
stat = Set_Printer('TEXTXY',Division,8.75:@FM:-0.970,font,1)

font<2> = 10	;* 10 point font
font<4> = 0		;* Bold off
font<5> = 0		;* Italics Off

stat = Set_Printer('TEXTXY',obj_Install('Get_Prop','Address'), 8.75:@fm:-0.775, font,1)
stat = Set_Printer('TEXTXY',obj_Install('Get_Prop','CSZC'), 8.75:@fm:-0.625, font,1)
stat = Set_Printer('TEXTXY','Tel:      ':obj_Install('Get_Prop','Phone'), 8.75:@fm:-0.475, font, 1)
stat = Set_Printer('TEXTXY','FAX:    ':obj_Install('Get_Prop','FAX'), 8.75:@fm:-0.325, font, 1)

* * * *  End of changes 10/8/2015 JCH  * * * *



stat = Set_Printer('TEXTXY','Order No: ':OrderNo,4.25:@FM:-0.325,font,1)

font<2>	= '18'		;* Big type
font<4>	= 1			;* Bold on
font<5> = 1			;* Italics

IF Confirmation THEN
	stat = Set_Printer('TEXTXY','Confirmation of Order',3.5:@FM:-0.8,font,1)
END ELSE
	stat = Set_Printer('TEXTXY','Customer Order',3.80:@FM:-0.8,font,1)
END


OrderRec 		= XLATE('ORDER',OrderNo,'','X')

CustNo			= OrderRec<ORDER_CUST_NO$>
CustBillToInfo	= XLATE('ORDER',OrderNo,'BILL_TO_INFO','X')
CustShipToInfo	= XLATE('ORDER',OrderNo,'SHIP_TO_INFO','X')
VisionOrderNo	= OrderRec<ORDER_VISION_ORDER_NO$>
PONo			= OrderRec<ORDER_PO_NO$>
POLine			= OrderRec<ORDER_PO_LINE$>
PORel			= OrderRec<ORDER_PO_REL$>
WOKeys			= OrderRec<ORDER_WO_KEYS$>

IF POLine NE '' THEN
	PONo := CRLF$:'Line ':POLine
END

SWAP @TM WITH CRLF$ IN CustBillToInfo
SWAP @TM WITH CRLF$ IN CustShipToInfo

OrderDetail = ''
OrderLotDetail = ''
OrderTotalAmt = 0
OrderItemTotalAmt = 0
OrderTotalWafers = 0

OrderDetKeys = OrderRec<ORDER_ORDER_DET_KEY$>

FOR I = 1 TO COUNT(OrderDetKeys,@VM) + (OrderDetKeys NE '')
	OrderDetKey		= OrderDetKeys<1,I>
	OrderLineItem	= FIELD(OrderDetKey,'*',2)
	
	OrderDetRec = XLATE('ORDER_DET',OrderDetKey,'','X')
	CustPN		= OrderDetRec<ORDER_DET_CUST_PN$>
	SubPN		= OrderDetRec<ORDER_DET_SUB_PART_NO$>
	ItemDesc	= OrderDetRec<ORDER_DET_ITEM_DESC$>
	Quote		= OrderDetRec<ORDER_DET_QUOTE_NO$>
	ItemQty		= OrderDetRec<ORDER_DET_ITEM_QTY$>
	UnitPrice	= OrderDetRec<ORDER_DET_UNIT_PRICE$>
	ItemAmt		= OCONV((ItemQty * UnitPrice),'MD2,')
	
	OrderItemTotalAmt += ItemQty
	OrderTotalAmt += (ItemQty * UnitPrice)
	
	UnitPrice	= OCONV(UnitPrice,'MD2,')
	EpiPN		= OrderDetRec<ORDER_DET_EPI_PN$>
	WONo		= OrderDetRec<ORDER_DET_WO_NO$>
	CustReqDt	= OCONV(OrderDetRec<ORDER_DET_CUST_REQ_DT$>,'D2/')
	PromiseDt	= OCONV(OrderDetRec<ORDER_DET_PROMISE_DT$>,'D2/')
		
	CustLotNos	= OrderDetRec<ORDER_DET_CUST_LOT_NO$>
	CustLotQtys	= OrderDetRec<ORDER_DET_CUST_LOT_QTY$>
	
	FOR N = 1 TO COUNT(CustLotNos,@VM) + (CustLotNos NE '')
		OrderLotDetail<I+N-1,1> = CustLotNos<1,N>
		OrderLotDetail<I+N-1,2> = CustLotQtys<1,N>
		OrderTotalWafers += CustLotQtys<1,N>
	NEXT N
	
	
	SWAP @VM WITH CRLF$ IN CustLotNos
	SWAP @VM WITH CRLF$ IN CustLotQtys
	
	OrderLine  = OrderLineItem:@VM
	OrderLine := ItemDesc:@VM
	OrderLine := Quote:@VM
	OrderLine := EpiPN:@VM
	OrderLine := CustPN:@VM
	OrderLine := SubPN:@VM
	OrderLine := ItemQty:@VM
	OrderLine := UnitPrice:@VM
	OrderLine := ItemAmt:@VM
	*OrderLine := CustReqDt:@VM
	OrderLine := PromiseDt:@VM
	OrderLine := WONo:@VM
	OrderLine := CustLotNos:@VM
	OrderLine := CustLotQtys
	
	OrderDetail<I> = OrderLine
	
NEXT I

WODetail = ''

FOR I = 1 TO COUNT(WOKeys,@VM) + (WOKeys NE '')
	WONo = WOKeys<1,I>
	WORec = XLATE('WO_LOG',WONo,'','X')
	RxDTM		= WORec<WO_LOG_CASS_DTM$>
	RelDTM		= WORec<WO_LOG_CASS_REL_DTM$>
	OrderItems	= WORec<WO_LOG_ORDER_ITEM$>
	SWAP @VM WITH ', ' IN OrderItems
	
	WOStepKeys	= WORec<WO_LOG_WO_STEP_KEY$>
	
	WODetail<I,1> = WONo
	WODetail<I,2> = OrderItems
	WODetail<I,3> = OCONV(RxDTM,'DT2/^HS')
	WODetail<I,4> = OCONV(RelDTM,'DT2/^HS')
	
	FOR N = 1 TO COUNT(WOStepKeys,@VM) + (WOStepKeys NE '')
	
		WOStepKey = WOStepKeys<1,N>
		WOStepRec = XLATE('WO_STEP',WOStepKey,'','X')
		
		StepNo = FIELD(WOStepKey,'*',2)
		
		StepPSN		= WOStepRec<WO_STEP_PROD_SPEC_ID$>
		StepDesc	= WOStepRec<WO_STEP_PROC_STEP_DESC$>
		WODetail<I,5,N> = StepNo
		WODetail<I,6,N> = StepPSN
		WODetail<I,7,N> = StepDesc
	NEXT N
NEXT I

SWAP @SVM WITH CRLF$ IN WODetail

colHeader = 'Order No'					; colFormat = '^1800'
colHeader<1,2> = 'Vision Order No'		; colFormat<1,2> = '^1800'
colHeader<1,3> = 'PO No'				; colFormat<1,3> = '^1800'
colHeader<1,4> = 'PO Line No'			; colFormat<1,4> = '^1800'
colHeader<1,5> = 'PO Rel No'			; colFormat<1,5> = '^1800'

colData = OrderNo
colData<1,2> = VisionOrderNo
colData<1,3> = PONo
colData<1,4> = POLine
colData<1,5> = PORel


font = 'Arial'		;* Font basics
font<2> = 8
font<4> = 0


TableFormat		= TB_ALL
HeaderFormat	= TB_ALL
TablePos		= 'L'
HdrShade		= LTGREY$
PrintHeads		= 1
BoldHeader		= 0
ULHeader		= 0
ItalicHeader	= 0
PrintHeads		= 1

* Basic Order Information


GOSUB PrintTable

stat = Set_Printer('TEXT')



* Bill to and Ship to Information *

colHeader = 'Bill To'					; colFormat = '<5400'
colHeader<1,2> = 'Ship To'				; colFormat<1,2> = '<5400'

colData = CustBillToInfo
colData<1,2> = CustShipToInfo

PrintHeads = 1
GOSUB PrintTable

stat = Set_Printer('TEXT')
stat = Set_Printer('TEXT')

* Order Detail Information *

colHeader = 'Itm':CRLF$:'No'						; colFormat = '^450'
colHeader<1,2> = 'Item Description'					; colFormat<1,2> = '<2700'
colHeader<1,3> = 'Quote No'							; colFormat<1,3> = '^630'
colHeader<1,4> = 'Epi Part No'						; colFormat<1,4> = '^1080'
colHeader<1,5> = 'Cust Part No'						; colFormat<1,5> = '<1800'
colHeader<1,6> = 'Substrate PN'						; colFormat<1,6> = '<1800'
colHeader<1,7> = 'Item':CRLF$:'Qty'					; colFormat<1,7> = '>540'
colHeader<1,8> = 'Unit':CRLF$:'Price'				; colFormat<1,8> = '>720'
colHeader<1,9> = 'Item Amt'							; colFormat<1,9> = '>1080'
colHeader<1,10> = 'Promise':CRLF$:'Date'			; colFormat<1,10> = '^900'
colHeader<1,11> = 'WO No'							; colFormat<1,11> = '^900'
colHeader<1,12> = 'Lot No'							; colFormat<1,12> = '<1800'
colHeader<1,13> = 'Lot Qty'							; colFormat<1,13> = '>720'

colData = OrderDetail

fontSpacing = 120

PrintHeads = 1
GOSUB PrintTable

stat = Set_Printer('TEXT')


colHeader = ''						; colFormat = '>3330'
colHeader<1,2> = ''					; colFormat<1,2> = '>5850'
colHeader<1,3> = ''					; colFormat<1,3> = '>1800'
colHeader<1,4> = ''					; colFormat<1,4> = '>4140'

colData = 'Order Totals:'
colData<1,2> = OCONV(OrderItemTotalAmt,'MD0,')
colData<1,3> = OCONV(OrderTotalAmt,'MD2,$')
colData<1,4> = OCONV(OrderTotalWafers,'MD0,')


TableFormat		= TB_COLUMNS_TOP_BOTTOM
PrintHeads		= 0

GOSUB PrintTable


stat = Set_Printer('TEXT')

stat = Set_Printer('TEXT')


* * * * * *  Print Work Order Detail  * * * * * *

colHeader = 'WO No'								; colFormat = '^1080'
colHeader<1,2> = 'Order Item(s)'				; colFormat<1,2> = '<1440'
colHeader<1,3> = 'Received'						; colFormat<1,3> = '<2160'
colHeader<1,4> = 'Released'						; colFormat<1,4> = '<2160'
colHeader<1,5> = 'WO Step'						; colFormat<1,5> = '^720'
colHeader<1,6> = 'Step PSN'						; colFormat<1,6> = '^1080'
colHeader<1,7> = 'Step Description'				; colFormat<1,7> = '<2440'


colData = WODetail

fontSpacing = 120
TableFormat = TB_ALL

PrintHeads = 1
GOSUB PrintTable

stat = Set_Printer('TEXT')



Void = Set_Printer('TERM')


RETURN



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

FOR I = 1 TO COUNT(colData,@FM) + (colData NE '')
	stat = Set_Printer('CALCTABLE',colFormat:@FM:colData<I>)
	TableSize = Get_Printer('CALCTABLE')
	TableHeight = TableSize<2>
	YPos = Get_Printer('POS')<2>
	IF YPos + TableHeight > 9.0 THEN 
		
		FirstLine = 0
		stat = Set_Printer('PAGEBREAK')
		font<2> = 8
		
		IF BoldHeader	THEN font<4> = 1 ELSE font<4> = 0
		IF ULHeader		THEN font<6> = 1 ELSE font<6> = 0
		IF ItalicHeader THEN font<5> = 1 ELSE font<5> = 0
		
		stat = Set_Printer('FONT',font,'100')
		
		IF HeaderFormat = '' THEN HeaderFormat = TableFormat
		
		stat = Set_Printer('ADDTABLE',colFormat,colHeader,'',HdrShade,'',0,HeaderFormat:@FM:TablePos)
		
		font<4> = 0
		font<5> = 0
		font<6> = 0
		stat = Set_Printer('FONT',font,fontSpacing)
		
		stat = Set_Printer('ADDTABLE',colFormat,'',colData<I>,HdrShade,'',0,TableFormat:@FM:TablePos)
		
	END ELSE
		
		IF PrintHeads THEN
			IF BoldHeader	THEN font<4> = 1 ELSE font<4> = 0
			IF ULHeader		THEN font<6> = 1 ELSE font<6> = 0
			IF ItalicHeader THEN font<5> = 1 ELSE font<5> = 0
			stat = Set_Printer('FONT',font,'100')
			
			IF HeaderFormat = '' THEN HeaderFormat = TableFormat
		
			stat = Set_Printer('ADDTABLE',colFormat,colHeader,'',HdrShade,'',0,HeaderFormat:@FM:TablePos)
			PrintHeads = 0
	
		END
	
		font<2> = 8
		font<4> = 0
		font<5> = 0
		font<6> = 0
		stat = Set_Printer('FONT',font,fontSpacing)
		stat = Set_Printer('ADDTABLE',colFormat,'',colData<I>,HdrShade,'',1,TableFormat:@FM:TablePos)
	
	END
	
NEXT I
							
RETURN


