COMPILE SUBROUTINE Print_Packing_Slip( CocIds, PrintPath, PDFileName ,PrinterInitialized)

DECLARE FUNCTION set_printer, get_printer, msg, fieldcount, Printer_Select, obj_Install
DECLARE SUBROUTINE security_err_msg

$INSERT SECURITY_RIGHTS_EQU
$INSERT OIPRINT_EQUATES
$INSERT MSG_EQUATES
$INSERT QUOTE_EQU
$INSERT QUOTE_SPEC_EQU
$INSERT PROD_SPEC_EQU
$INSERT ORDER_EQU
$INSERT COC_EQU
$INSERT APPCOLORS

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(PrintPath))				THEN PrintPath = ''
IF NOT(ASSIGNED(PDFFileName))			THEN PDFFileName = ''
IF NOT(ASSIGNED(PrinterInitialized))	THEN PrinterInitialized = 0

IF NOT(PrinterInitialized) THEN
   
	* * *   PRINT SETUP   * * *
	
	PageInfo	= ''
	PageInfo<PI$LEFT>	= '0.5'
	PageInfo<PI$TOP>	= '1.35'
	PageInfo<PI$RIGHT>	= '0.5'
	PageInfo<PI$BOTTOM>	= '0.25'
	
	PageSetup	= 0				;* 0 = Portrait, 1 = Landscape mode
	
	PrintSetup	= ''
	PrintSetup<1,1>	= '2'	;* Specific Location
	PrintSetup<1,2> = '5'	;* Show Print and PDF, hide Print Setup
	PrintSetup<1,3>	= '0'	;* Show the printing window
	PDFParms = 'Printing PDF Document':@FM: '' :@FM: 6 :@FM: ''
			
	stat = Set_Printer( 'INIT', PDFParms, 'Packing Slip',PageInfo,PageSetup , PrintSetup)
	
	
END ELSE
	stat = Set_Printer('PAGEBREAK')
END
	
IF NOT(PrinterInitialized) THEN
	   
	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:7.5:@FM:-1.2, 1)
	stat = Set_Printer('LINE', -0.1:@FM:-0.1:@FM:7.5:@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)

	font<2> = 12	;* Drop the font size
	font<4> = 1		;* Bold on
	font<5> = 0		;* Italics off
	
	stat = Set_Printer('TEXTXY',obj_Install('Get_Prop','Company'),5.5:@FM:-1.15,font,1)

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

	stat = Set_Printer('TEXTXY',obj_Install('Get_Prop','Address'),5.5:@fm:-0.925,font,1)
	stat = Set_Printer('TEXTXY',obj_Install('Get_Prop','CSZC'),5.5:@fm:-0.775,font,1)
	stat = Set_Printer('TEXTXY','Phone:  ':obj_Install('Get_Prop','Phone'),5.5:@fm:-0.625,font,1)
	stat = Set_Printer('TEXTXY','FAX:    ':obj_Install('Get_Prop','FAX'),5.5:@fm:-0.475,font,1)
	stat = Set_Printer('TEXTXY','Web:    ':obj_Install('Get_Prop','WebSite'),5.5:@fm:-0.325,font,1)
END



CCnt = fieldcount( CocIds, @fm )

FOR I = 1 TO CCnt
   ThisCOC = CocIds<i>
   ThisCOCRec = xlate( 'COC', ThisCOC, '', 'X' )
   
   ThisOrder 	= ThisCOCRec<coc_order_no$,I> 
                                        
   ThisOrderRec = xlate( 'ORDER', ThisOrder, '', 'X' ) 
   QuoteIDs     = ThisOrderRec<order_quote_ids$>
   PSNIDs       = ThisOrderRec<order_prod_spec_ids$>
   BillToAttn   = ThisOrderRec<order_bill_to_attn$>
   BillToAddr   = ThisOrderRec<order_bill_to_address$>
   BillToCity   = ThisOrderRec<order_bill_to_city$>
   BillToState  = ThisOrderRec<order_bill_to_state$>
   BillToZip    = ThisOrderRec<order_bill_to_zip$>
   BillToCo     = ThisOrderRec<order_bill_to_company$>
   ShipToAttn   = ThisOrderRec<order_ship_to_attn$>
   ShipToAddr   = ThisOrderRec<order_ship_to_address$>
   ShipToCity   = ThisOrderRec<order_ship_to_city$>
   ShipToState  = ThisOrderRec<order_ship_to_state$>
   ShipToZip    = ThisOrderRec<order_ship_to_zip$>
   ShipToCo     = ThisOrderRec<order_ship_to_company$>
   
	BillToData  = BillToAttn:CRLF$
	BillToData := BillToCo:CRLF$

	SWAP @VM WITH CRLF$ IN BillToAddr

	BillToData := BillToAddr :CRLF$  
	BillToData := BillToCity:', ':BillToState:' ':OCONV(BillToZip,'[ZIP_FORMAT]')

	ShipToData  = ShipToAttn:CRLF$
	ShipToData := ShiptoCo:CRLF$
	
	SWAP @VM WITH CRLF$ IN ShipToAddr
	
	ShipToData := ShipToAddr:CRLF$
	ShipToData := ShipToCity:', ':ShipToState:' ':OCONV(ShipToZip,'[ZIP_FORMAT]')
	
   stat = set_printer( 'TEXTXY', Today, 6.38:@fm:.7, Hf, 0 )
   

   Today        = oconv( date(), 'D2/' )
   ShipVia      = ThisOrderRec<order_ship_via$>
   Terms        = ThisOrderRec<order_terms$>
   PO           = ThisOrderRec<order_po$>
   WaferQtys    = ThisCOCRec<coc_wafer_qty$>
   TotalShipQty = sum( WaferQtys )
   CustNo       = ThisCOCRec<coc_cust_no$>                                             
   OrderLines   = ThisCOCRec<coc_order_line$>
   WO           = ThisCOCRec<coc_wo$>
   PartNumbers  = ThisCOCRec<coc_part_num$>
   LotNumbers   = ThisCOCRec<coc_lot_num$>
   OrderNumbers = ThisCOCRec<coc_order_no$>
   
   OLCnt = fieldcount( OrderLines, @vm )
   QuoteIDsToUse = ''
   for j = 1 to OLCnt
      QuoteIDsToUse<1,-1> = QuoteIDs<1,OrderLines<1,j>>
   next j
   
   
   PSNToUse = PSNIDs<1,OrderLines<1,1>> ;* TAKE THE FIRST PSN
   TSub  = xlate( 'PROD_SPEC', PSNToUse, prod_spec_spec_substrate$, 'X' )
   WSize = TSub<1,QSSubWafersize$>

	* Print the title line
 
	font	= 'Arial'
	font<2>	= '18'		;* Big type
	font<4>	= 1			;* Bold on
	font<5> = 1			;* Italics
	stat = Set_Printer('TEXTXY','Packing Slip',2.9:@FM:-0.80,font,0)
 

	font<2> = 12
	font<4> = 0		;* Bold off
	font<5> = 0		;* Italics off
	stat = set_printer( 'FONT', font )
	
	stat = Set_Printer('TEXTXY','Ship Date: ':OCONV(Date(),'D4/'),0.0:@FM:0.4,0,0)
	
	bcFont = ''
	bcFont<1> = '3 of 9 Barcode'
	bcFont<2> = '24'
	bcFont<3> = 'R'
	bcFont<4> = '0'

	stat = Set_Printer('TEXTXY','*':'2S':'V':ThisCOC:'*',4.00:@FM:.10,bcFont,0)	;* Added JCH

	font<2> = 12	;* 12 points
	font<4> = 1		;* Bold On
	stat = Set_Printer('TEXTXY', 'Packing List - V':ThisCOC, 4.0:@fm:-.1, font, 0 )
	stat = Set_Printer('TEXTXY','SID (2S) V':ThisCOC,4:@FM:.35,font,0)
	
	stat = Set_Printer("POS", 0:@FM:.60)

	
	* Bill to and Ship to information	
	
	colFormat = '>1080'
	colFormat<1,2> = '<4700'
	colFormat<1,3> = '>1080'
	colFormat<1,4> = '<3620'
	
	colData = 'Bill To:'
	colData<1,2> = BillToData
	colData<1,3> = 'Ship To:'
	colData<1,4> = ShipToData
	
	font<2> = 10
	font<4> = 0
	stat = Set_Printer('FONT',font)
	
	stat = Set_Printer('ADDTABLE',colFormat,'',colData,LTGREY$,'',1,3)
	
	stat = Set_Printer('TEXT')
	
	* Shipment Information
	
	colHeader = 'Customer No'			; colFormat = '^1440'
	colHeader<1,2> = 'Ship Date'		; colFormat<1,2> = '^1440'
	colHeader<1,3> = 'Ship Via'			; colFormat<1,3> = '^2880'
	colHeader<1,4> = 'Terms'			; colFormat<1,4> = '^2160'
	colHeader<1,5> = 'PO Number'		; colFormat<1,5> = '^2880'

	colData = CustNo
	colData<1,2> = OCONV(Date(),'D4/')
	colData<1,3> = ShipVia
	colData<1,4> = Terms
	colData<1,5> = PO

	font<2> = 10
	font<4> = 1		;* Bold
	stat = Set_Printer('FONT',font)
	
	stat = Set_Printer('ADDTABLE', colFormat,colHeader,'',LTGREY$,'',0,6)
	
	font<2> = 10
	font<4> = 0
	stat = Set_Printer('FONT',font)
	
	stat = Set_Printer('ADDTABLE',colFormat,'',colData,LTGREY$,'',1,6)

	stat = Set_Printer('TEXT')
	
	* Order detail

	colHeader = 'Quantity'					; colFormat = '^1440'
	colHeader<1,2> = 'Item'					; colFormat<1,2> = '^1440'
	colHeader<1,3> = 'WO No'				; colFormat<1,3> = '^1440'
	colHeader<1,4> = 'Part No'				; colFormat<1,4> = '^2160'
	colHeader<1,5> = 'Lot No':CRLF$:WSize	; colFormat<1,5> = '^2880'
	colHeader<1,6> = 'Quote No'				; colFormat<1,6> = '^1440'
		
	ShipDetail = ''
	FOR D = 1 TO COUNT(WaferQtys,@VM) + (WaferQtys NE '')
		ShipDetail<D,1> = WaferQtys<1,D>
		ShipDetail<D,2> = OrderLines<1,D>
		ShipDetail<D,3> = WO<1,1>
		ShipDetail<D,4> = PartNumbers<1,D>
		ShipDetail<D,5> = LotNumbers<1,D>
		ShipDetail<D,6> = QuoteIDSToUse<1,D>
	NEXT D

	font<2> = 10
	font<4> = 1		;* Bold
	stat = Set_Printer('FONT',font)
	
	stat = Set_Printer('ADDTABLE', colFormat,colHeader,'',LTGREY$,'',0,6)
	
	font<2> = 10
	font<4> = 0		;* Bold
	stat = Set_Printer('FONT',font)
	
	FOR J = 1 TO COUNT(WaferQtys,@VM) + (WaferQtys NE '')
		stat = Set_Printer('CALCTABLE',colFormat:@FM:ShipDetail<J>)
		TableSize = Get_Printer('CALCTABLE')
		TableHeight = TableSize<2>
		IF Get_Printer('POS')<2> + TableHeight > 6.75 THEN
			stat = Set_Printer('PAGEBREAK')
			
			font	= 'Arial'
			font<2>	= '18'		;* Big type
			font<4>	= 1			;* Bold on
			font<5> = 1			;* Italics
			stat = Set_Printer('TEXTXY','Packing Slip',2.3:@FM:-0.80,font,0)
			stat = Set_Printer('ADDTABLE',colFormat,colHeader,ShipDetail<J>,LTGREY$,'',0,7)
		END ELSE
			stat = Set_Printer('ADDTABLE',colFormat,'',ShipDetail<J>,LTGREY$,'',1,7)
		END		
	
	NEXT J
	
	stat = Set_Printer('TEXT')
	stat = Set_Printer('TEXT')
		
	* Notes	
	
	colHeader = 'Notes:'	; colFormat = '<10840'
	
	colData = xlate( 'ORDER', OrderNumbers<1,1>, order_invoice_notes$, 'X' )
	SWAP @TM WITH CRLF$ IN colData
	IF colData NE '' THEN
		stat = Set_Printer('CALCTABLE',colFormat:@FM:ShipDetail<J>)
		TableSize = Get_Printer('CALCTABLE')
		TableHeight = TableSize<2>
		IF Get_Printer('POS')<2> + TableHeight > 6.75 THEN
			stat = Set_Printer('PAGEBREAK')
			
			font	= 'Arial'
			font<2>	= '18'		;* Big type
			font<4>	= 1			;* Bold on
			font<5> = 1			;* Italics
			stat = Set_Printer('TEXTXY','Packing Slip',2.3:@FM:-0.80,font,0)
			
		END

		font<2> = 10
		font<4> = 1		;* Bold
		stat = Set_Printer('FONT',font)
		
		stat = Set_Printer('ADDTABLE', colFormat,colHeader,'',LTGREY$,'',0,6)
		
		font<2> = 10
		font<4> = 0
		stat = Set_Printer('FONT',font)
		
		stat = Set_Printer('ADDTABLE',colFormat,'',colData,LTGREY$,'',1,6)
	END 	;* End of check for notes
	
	* Check boxes at bottom of the sheet
	
	colFormat = '<+2160'
	colFormat<1,2> = '<+2160'
	colFormat<1,3> = '<+2160'
	colFormat<1,4> = '<+2160'
	colFormat<1,5> = '<+2160'
	
	colData<1> = 'Date Shipped:':@VM:'Checked By:':@VM:'Packed By:':@VM:'Total Pieces:':@VM:'Total Weight':CRLF$
	colData<2> = 'Box 1:':@VM:'Box 2':@VM:'Box 3':@VM:'Box 4':@VM:'Box 5':CRLF$
	colData<3> = 'Box 6:':@VM:'Box 7':@VM:'Box 8':@VM:'Box 9':@VM:'Box 10':CRLF$
	
	SWAP @VM WITH CRLF$:@VM IN colData
	
	stat = Set_Printer('POS',0:@FM:'8.1')
	
	font<2> = 8
	stat = Set_Printer('FONT',font)
	
	stat = Set_Printer("ADDTABLE", colFormat, '', colData, '', '', 1, TB_ALL)
	
	* Order totals at bottom of the page
	
	font<2> = 10
	stat = Set_Printer('FONT',font)
	
	stat = set_printer('TEXTXY', 'Total Qty Shipped:':fmt( TotalShipQty, 'R#7' ), 0:@fm:9.63, font, 0 )
	stat = set_Printer('TEXTXY','Manufacturing DUNs Number: 121457774',2.5:@FM:9.63,font,0)
 
       
	IF NOT(PrinterInitialized) THEN
		stat = set_printer( 'TERM' )
	END  
NEXT I
RETURN


