COMPILE SUBROUTINE Print_Part_Master( Dummy )

/*
	Print Ready to Ship Routine
	11/5/2010 - John C. Henry, J.C. Henry, Inc. - Initial coding
	
*/

DECLARE SUBROUTINE Reduce,MSG,Utility,ErrMsg, Set_Status, Btree.Extract, RList, Make.List
DECLARE FUNCTION Set_Printer,Get_Printer,Msg, Get_Status, Dialog_Box, obj_Install
DECLARE FUNCTION FieldCount,Get_Status,Set_Status,Set_FSError,Printer_Select

$INSERT OIPRINT_EQUATES
$INSERT PART_EQUATES

$INSERT APPCOLORS

EQU CRLF	TO \0D0A\	;* CHAR(13):CHAR(10) for multiline messages
EQU	TAB		TO CHAR(9)	;* TAB character

EQU 		TARGET_ACTIVELIST$ TO 5

ErrorTitle		= 'Error in Store Procedure Print_Part_Master'

OPEN 'PART' TO PartTable ELSE
	ErrorMsg = 'Unable to Open "PART" table!'
	Set_Status(1,ErrorTitle:@SVM:ErrorMsg)
	RETURN
END

OPEN 'DICT.PART' TO @DICT ELSE
	ErrorMsg = 'Unable to Open "DICT.PART" table!'
	Set_Status(1,ErrorTitle:@SVM:ErrorMsg)
	RETURN
END


SelectSent = 'SELECT PART WITH STATUS = "A" BY PSN_ID '

Set_Status(0)


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

**START PRINTING PROCESS**

FileName	= 'Print Ready To Ship'
Title		= 'Printing Ready To Ship':@VM:'Report'

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

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

PageSetup	= '1'		;* Portrait

PrintSetup	= '2'		;* Preview Normal
PrintSetup<1,2>	= 5		;* Print & PDF buttons
PrintSetup<1,5>	= 1		;* Page Range Dialog

PrintPath = Printer_Select('',1)		;* Get Default printer path

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


Header		= @VM:obj_Install('Get_Prop','CompTitle'):' Active Part Master'
Header<2>	= @VM
Header<3> = ''


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

stat = Set_Printer("FONTHEADFOOT", font)

stat = Set_Printer("HEADER",Header)


footer = " 'D' 'T'":@VM:@VM:" Page: 'P'"
colfooter = " "

stat = Set_Printer("FOOTER",footer,colfooter)


colHeader		= 'Part No'							; colFmt		= '^+1080'	
colHeader<1,2>	= 'Description'						; colFmt<1,2>	= '<+2160'
colHeader<1,3>	= 'PSN ID'							; colFmt<1,3>	= '^+1080'
*colHeader<1,4>	= 'Step'							; *colFmt<1,4>	= '^+540'	
*colHeader<1,5>	= 'Step PSN'						; *colFmt<1,5>	= '^+720'
*colHeader<1,6>	= 'Step Desc'						; *colFmt<1,6>	= '<+1440'
colHeader<1,4>	= 'Cust No'							; colFmt<1,4>	= '^+720'
colHeader<1,5>	= 'Customer'						; colFmt<1,5>	= '<+4600'
colHeader<1,6>	= 'Cust Part No'					; colFmt<1,6>	= '^+1800'
colHeader<1,7>	= 'Cust Part Desc'					; colFmt<1,7>	= '<+1800'
colHeader<1,8>	= 'Sub Part No'						; colFmt<1,8>  	= '^+1440'

fontSpacing = 100




@RECCOUNT	= 0
FirstPass	= 1
LastRecord	= 0
ReadErr		= 0
FirstLine	= 1

* Null Previous Break Buckets

Prev_PsnID	 = ''
Last_PsnID_Break = 1

* * * * * * *
READRECORD:
* * * * * * *

PSNID_Break = 0		;* Zero break flag to false

READNEXT @ID ELSE
	LastRecord = 1
	PsnID_Break = 1
	PsnID = Prev_PsnID
END

IF FirstPass AND LastRecord THEN
	GOTO Bail
END

IF LastRecord THEN GOTO Breaks

READO @RECORD FROM PartTable,@ID ELSE
	stat = Set_Printer('TEXT', 'Unable to read record ':QUOTE(@ID):' from PART table.')
	GOTO READRECORD
END


@RECCOUNT += 1

PartNo			= @ID
Desc			= @RECORD<PART_DESC$>
S.PsnId			= {PSN_ID}
ProcStepNo		= @RECORD<PART_PROC_STEP_NO$>
ProcStepPSN		= @RECORD<PART_PROC_STEP_PSN$>
ProcStepDesc	= @RECORD<PART_PROC_STEP_DESC$>
CustNo			= @RECORD<PART_CUST_NO$>
CustName 		= {CUST_NAME}
CustPartNo		= @RECORD<PART_CUST_PART_NO$>
CustPartDesc	= {CUST_PART_DESC}
SubPartNo		= @RECORD<PART_SUB_PART_NO$>


SWAP @VM WITH CRLF$ IN ProcStepNo
SWAP @VM WITH CRLF$ IN ProcStepPSN
SWAP @VM WITH CRLF$ IN ProcStepDesc

IF S.PsnID NE Prev_PsnID OR PsnId_Break THEN
	PsnID		 = Prev_PsnID
	Prev_PsnID	 = S.PsnID
	PsnID_Break += 1
END

IF FirstPass THEN
	FirstPass = 0
	GOTO Detail
END


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

IF PsnID_Break THEN
	colData = ''
	colData<1,2> = ''
	colData<1,3> = '* * *'
	colData<1,4> = ''
	colData<1,5> = ''
	colData<1,6> = ''
	colData<1,7> = ''
	colData<1,8> = ''
	
	GOSUB PrintTable
	
END

* * * * * * *
DETAIL:
* * * * * * *

colData	= PartNo
colData<1,2> = Desc
colData<1,3> = S.PSNId
*colData<1,4> = ProcStepNo
*colData<1,5> = ProcStepPSN
*colData<1,6> = ProcStepDesc
colData<1,4> = CustNo
colData<1,5> = CustName
colData<1,6> = CustPartNo
colData<1,7> = CustPartDesc
colData<1,8> = SubPartNo


GOSUB PrintTable

IF LastRecord THEN
	GOTO Bail
END



GOTO READRECORD


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

stat = Set_Printer("TERM",1)	   			     ;* Terminiate this printing session
IF stat < 0 THEN  GOTO OIPrint_Err

Utility('CURSOR','A') 

RETURN


* * * * * * *
OIPrint_Err:
* * * * * * *
	
*Set Error for OIPrint function and return to calling procedure

Msg('',stat)

Error_Msg	= 'Set_Printer Returned Error Status: ':QUOTE(stat)
IF Get_Status(errCode) THEN
	Stat  = Set_Status(-1,'STPROC',ErrorTitle:@SVM:Error_Msg)
END ELSE
	Stat  = Set_Status(1,'STPROC',ErrorTitle:@SVM:Error_Msg)
END 

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 > 6.50 OR FirstLine THEN
	IF NOT(FirstLine) THEN
		stat = Set_Printer('PAGEBREAK')
	END
	FirstLine = 0
	font<2> = 8
	font<4> = 1		;* Bold
	stat = Set_Printer('FONT',font,'100')

	stat = Set_Printer('ADDTABLE',colFmt,colHeader,'',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> = 8
	font<4> = 0
	stat = Set_Printer('FONT',font,fontSpacing)
	stat = Set_Printer('ADDTABLE',colFmt,'',colData,LTGREY$,'',1,TB_ALL)

END
	
							
RETURN	 

