COMPILE SUBROUTINE Print_Coc( ShipNo, PrintBackPage, ShipRec )

DECLARE FUNCTION FieldCount, Msg, Key_Sort, Set_Printer, obj_RDS, obj_Install
DECLARE SUBROUTINE Btree.Extract,Extract_SI_Keys

$insert company_equates
$insert coc_equ
$insert email_pENDing_equ
$insert logical
$insert lsl_users_equ
$insert msg_equates
$insert popup_equates
$insert rds_equ
$insert rds_layer_info_equ
$insert quote_equ
$insert quote_spec_equ
$insert oiprint_equates
$insert schedule_equ
$insert security_rights_equ
$insert prod_spec_equ
$insert srp_coc_results_equ

$INSERT RDS_LAYER_EQUATES
$INSERT RDS_TEST_EQUATES

DEBUG
* Initialize Common Printer parms

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

* 	This is the id code number FOR first wafer inspection out of the tw_codes table

EQU FirstWaferInspection$ TO 5


IF NOT(ASSIGNED(ShipNo)) THEN RETURN
IF NOT(ASSIGNED(PrintBackPage)) THEN PrintBackPage = ''

IF ShipNo = '' THEN RETURN

IF ShipRec = '' THEN
	ShipRec = XLATE('COC',ShipNo,'','X')
END

RDSNos = ShipRec<COC_RDS_NO$>

RDSCnt = COUNT(RDSNos,@VM) + (RDSNos NE '')

* Initialize Common Printer parms

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

PageSetup	= '0' 				;* Portrait
PrintSetup  = '2'				;* Print Preview

stat = Set_Printer("INIT",'Printing','CofC Back',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: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)


* * * *  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,5.7:@FM:-1.15,font,1)
stat = Set_Printer('TEXTXY',Division,5.7:@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'), 5.7:@fm:-0.775, font,1)
stat = Set_Printer('TEXTXY',obj_Install('Get_Prop','CSZC'), 5.7:@fm:-0.625, font,1)
stat = Set_Printer('TEXTXY','Tel:      ':obj_Install('Get_Prop','Phone'), 5.7:@fm:-0.475, font, 1)
stat = Set_Printer('TEXTXY','FAX:    ':obj_Install('Get_Prop','FAX'), 5.7:@fm:-0.325, font, 1)

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



font	= 'Arial'
font<2>	= '18'		;* Big type
font<4>	= 1			;* Bold on
font<5> = 1			;* Italics
stat = Set_Printer('TEXTXY','Certificate of Compliance',1.9:@FM:-0.80,font,0)

Font = "Courier New,9,L,1"
convert ',' to @fm in Font
Stat = set_printer( 'FONT', Font )


TShipQty = xlate( 'RDS', RDSNos, 'WFRS_OUT', 'X' )
		
CocFrontShipQty	= sum( TShipQty )
ThisRdsRec		= xlate( 'RDS', RDSNos<1,1>, '', 'X' )
Dopant			= xlate( 'RDS', RDSNos<1,1>, 'DOPANT_L1', 'X' )
SameLayCnt		= fieldcount( ThisRdsRec<rds_layer_info$>, char(248) )

Tfont = Font
Tfont<2> = 12
Tfont<3> = 'R'
Tfont<4> = 1
Tfont<6> = 0
Void = set_printer( 'FONT', Tfont )
Void = set_printer( 'TEXT', 'Date: ':oconv( date(), 'D2/' ) )
Void = set_printer( 'TEXT', '' )
Void = set_printer( 'TEXT', '' )
Tfont<2> = 10
Tfont<3> = 'L'
Tfont<4> = 1
Void = set_printer( 'FONT', Tfont )
Void = set_printer( 'TEXT', 'Customer: ':oconv( ThisRdsRec<rds_cust_no$>, '[XLATE_CONV,COMPANY*CO_NAME]' ) )
Addr = xlate( 'COMPANY', ThisRdsRec<rds_cust_no$>, company_address$, 'X' )
Acnt = fieldcount( Addr, @vm )
Void = set_printer( 'TEXT', 'Address: ':Addr<1,1> )
for k = 2 to Acnt
    Tvar = fmt( '', 'L#9' ):Addr<1,k>
    Void = set_printer( 'TEXT', Tvar )
next k
Void = set_printer( 'TEXT', '' )
Void = set_printer( 'TEXT', '' )
Tvar = fmt( 'P.O. Number: ':ThisRdsRec<rds_po$>, 'L#65' ):' Part Number: ':ThisRdsRec<rds_part_num$>
Void = set_printer( 'TEXT', Tvar )

* ADD THE SPECIFICATION INFORMATION FROM THE PSN

SpecInfo = ''
SpecNums = xlate( 'PROD_SPEC', ThisRdsRec<rds_prod_spec_id$>, prod_spec_spec_num$, 'X' )
convert @vm to ';' in SpecNums
RevNum   = xlate( 'PROD_SPEC', ThisRdsRec<rds_prod_spec_id$>, prod_spec_rev_num$, 'X' )
RevDate  = oconv( xlate( 'PROD_SPEC', ThisRdsRec<rds_prod_spec_id$>, prod_spec_rev_date$, 'X' ), 'D2/' )
if ( SpecNums <> '' ) then
   SpecInfo:= 'Spec#: ':SpecNums:space(2)
end 
if ( RevNum <> '' ) then
   SpecInfo:= 'Rev#: ':RevNum:space(2)
end
if ( RevDate <> '' ) then
   SpecInfo:= 'Rev Date: ':RevDate:space(2)
end
SpecInfo[-2,2] = ''
*
Tvar = fmt( SpecInfo, 'L#68' ):' Quantity: ':CocFrontShipQty
Void = set_printer( 'TEXT', Tvar )
Void = set_printer( 'TEXT', '' )

* lets figure out what layer number to start with

StartingLayerNo = 1

stat = set_printer( 'LINESTYLE', ps_solid:@fm:0 )

* first print base layer
Tfont<2> = 9
Tfont<4> = 0
Tfont<1> = 'MS LineDraw'
Void = set_printer( 'FONT', Tfont )
DEBUG

SortedLSKeys = XLATE('RDS',RDSNos<1,1>,'RDS_LS_SORTED','X')

IF SortedLSKeys NE '' THEN
	LayerCnt = COUNT(SortedLSKeys,@VM) + (SortedLSKeys NE '')
	FOR I = 1 TO LayerCnt
		LSKey = SortedLSKeys<1,I>
		LayerRec		= XLATE('RDS_LAYER',LSKey,'','X')
		FirstTestKey	= LayerRec<RDS_LAYER_RDS_TEST_KEYS$,1>
		TestRec			= XLATE('RDS_TEST',FirstTestKey,'','X')
		
		LineOut = ''
		IF I > 1 THEN
			LineOut := FMT( 'Layer ':I, 'L#10' )
		END ELSE
			LineOut := STR(' ',10)
		END
		Dopant		= LayerRec<RDS_LAYER_DOPANT$>
		ThickTarget = TestRec<RDS_TEST_SPEC_THICK_TARGET$>
		ThickUnits	= TestRec<RDS_TEST_SPEC_THICK_UNITS$>
		ResTarget	= TestRec<RDS_TEST_SPEC_RES_TARGET$>
		ResTarget	= TestRec<RDS_TEST_SPEC_RES_UNITS$>
		ConTarget	= TestRec<RDS_TEST_SPEC_CON_TARGET$>
		ContUnits	= TestRec<RDS_TEST_SPEC_CON_UNITS$>
		
		LineOut := FMT('Dopant: ':Dopant,'L#30')
		LineOut := FMT('Target Thickness ':OCONV(ThickTarget,'MD2'):ThickUnits,'L#30')
		
		IF ResTarget NE '' THEN
			LineOut := FMT('Target Resistivity ':OCONV(ResTarget,'MD3'):ResUnits,'L#30')
		END ELSE
			LineOut := FMT('Target Concentration ':OCONV(ConTarget,'MS21'):ConUnits,'L#30')
		END
		
		stat = Set_Printer('TEXT',LineOut)
		
	NEXT I
END ELSE
	LineOut = ''
	if StartingLayerNo > 1 then
		LineOut := fmt( 'Layer ':StartingLayerNo, 'L#10' )
	end
	LineOut:= fmt( 'Dopant: ':Dopant, 'L#30' )
	LineOut:= fmt( 'Target Thickness ':oconv( ThisRdsRec<rds_thick_target$>, 'MD2' ):ThisRdsRec<rds_thick_units$>, 'L#30' )
	if ThisRdsRec<rds_res_target$> then
		LineOut:= fmt( 'Target Resistivity ':oconv( ThisRdsRec<rds_res_target$>, 'MD3' ):ThisRdsRec<rds_res_units$>, 'L#30' )
	end else
		LineOut:= 'Target Concentration ':oconv( ThisRdsRec<rds_con_target$,1>, 'MS21' ):ThisRdsRec<rds_con_units$>
		if ThisRdsRec<rds_con_min$,2> then
	   		LineOut:= ' - ':oconv( ThisRdsRec<rds_con_target$,2>, 'MS21' ):ThisRdsRec<rds_con_units$>
		end
	end
	Void = set_printer( 'TEXT', LineOut )
	StartingLayerNo += 1

	for k = 1 to SameLayCnt
		LineOut = ''
		if StartingLayerNo > 1 then
			LineOut:= fmt( 'Layer ':StartingLayerNo, 'L#10' )
		end
		Tl = field( ThisRdsRec<rds_layer_info$>, char(248), k )
		Dopant = xlate( 'RDS', LayerIds<i,1>, 'DOPANT_L':K+1, 'X' )
		LineOut:= fmt( 'Dopant: ':Dopant, 'L#30' )
		LineOut:= fmt( 'Target Thickness ':oconv( Tl<1,RLThickTarget$>, 'MD2' ):Tl<1,RLThickUnits$>, 'L#30' )
		if Tl<1,RLResTarget$> then
			LineOut:= fmt( 'Target Resistivity ':oconv( Tl<1,RLResTarget$>, 'MD3' ):Tl<1,RLResUnits$>, 'L#30' )
		end else
			LineOut:= 'Target Concentration ':oconv( Tl<1,RLConTarget$,1>, 'MS21' ):Tl<1,RLConUnits$>
			if Tl<1,RLConMin$,2> then
				LineOut:= ' - ':oconv( Tl<1,RLConTarget$,2>, 'MS21' ):Tl<1,RLConUnits$>
			end
		end
		Void = set_printer( 'TEXT', LineOut )
		StartingLayerNo += 1
	next k
END


Tfont = Font
Tfont<4> = 0
Tfont<2> = 11
Void = set_printer( 'FONT', Tfont )
Void = set_printer( 'TEXT', '' )
Void = set_printer( 'TEXT', '' )
TextOut = 'The enclosed material has been manufactured, inspected and/or tested in the'
Void = set_printer( 'TEXT', TextOut )
TextOut= 'U.S.A. in accordance with specifications outlined on your purchase order.'
Void = set_printer( 'TEXT', TextOut )
Void = set_printer( 'TEXT', '' )
Tfont<4> = 1
Void = set_printer( 'FONT', Tfont )
TextOut = 'This material was processed as Work Order Number: ':quote( ThisRdsRec<rds_wo$> ):' and manufactured under process control recipe ':quote( oconv( ThisRdsRec<rds_recipe_no$>, '[XLATE_CONV,RECIPE*RECIPE_NAME]' ) ):'.'
Void = set_printer( 'TEXT', TextOut )
Tfont<4> = 0
Void = set_printer( 'FONT', Tfont )
Void = set_printer( 'TEXT', '' )
TextOut = 'Pertinent test and/or inspection records are on file and available for review'
if PrintBackPage then
    Void = set_printer( 'TEXT', TextOut )            
    TextOut = 'under run numbers specified in the following page(s) of this document.'
    Void = set_printer( 'TEXT', TextOut )
end else
    TextOut := '.'
    Void = set_printer( 'TEXT', TextOut )
end
for k = 1 to 5
    Void = set_printer( 'TEXT', '' )
next k
Void = set_printer( 'TEXT', 'Approved by,' )
for k = 1 to 5
    Void = set_printer( 'TEXT', '' )
next k
Tvar = fmt( ' ', 'L#65' )
Tfont<6> = 1
Void = set_printer( 'FONT', Tfont )
Void = set_printer( 'TEXT', Tvar )
Tfont<6> = 0
Void = set_printer( 'FONT', Tfont )
TextOut = fmt( '', 'L#50' ):'Date'
Void = set_printer( 'TEXT', TextOut )
Void = set_printer( 'TEXT', '' )
Void = set_printer( 'TEXT', '' )
TextOut = 'Document Control'
Void = set_printer( 'TEXT', TextOut )
for k = 1 to 7
    Void = set_printer( 'TEXT', '' )
next k
Tfont<4> = 1
Void = set_printer( 'FONT', Tfont ) 
TextOut = 'Quality Assurance Registration Number ':quote( ThisRdsRec<rds_wo$> )
Void = set_printer( 'TEXT', TextOut )
Tfont<4> = 0
Void = set_printer( 'FONT', Tfont )
Void = set_printer( 'PAGEBREAK' )                 



stat = Set_Printer('TERM')

