Compile function Labeling_Services(@Service, @Params)
#pragma precomp SRP_PreCompiler

Declare Function Database_Services, Environment_Services, Set_Printer, Direct_Print, Printer_Select, Error_Services
Declare Function Logging_Services, Datetime
Declare subroutine Error_Services, Labeling_Services, Lot_Event_Services, SRP_Set_Clipboard

$insert LOGICAL
$Insert LOT_EQUATES
$Insert TEST_WAFER_PROD_EQUATES
$Insert RETURN_TO_FAB_LOTS_EQUATES
$Insert LSL_USERS_EQUATES
$Insert WO_MAT_EQUATES

LogPath      = Environment_Services('GetApplicationRootPath') : '\LogFiles\LabelPrinting'
LogDate      = Oconv(Date(), 'D4/')
LogTime      = Oconv(Time(), 'MTS')
LoggingDTM   = LogDate : ' ' : LogTime   ; // Logging DTM

LogFileName        = LogDate[7, 4] : '-' : LogDate[1, 2] : '-' : LogDate[4, 2] : ' TestWaferLabelPrinting.csv'
Headers            = 'Logging DTM' : @FM : 'Lot Id' : @FM : 'Username' : @FM : 'PrinterID' : @FM : 'Message'
objTWLabelPrinting = Logging_Services('NewLog', LogPath, LogFileName, CRLF$, ',', Headers, '', False$, False$)

GoToService

Return Response or ""

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Service Parameter Options
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Options LOAD_LOCK_SIDE = 'Left', 'Right', 'Both'
Options STAGES = 'PRE', 'POST'

//-----------------------------------------------------------------------------
// SERVICES
//-----------------------------------------------------------------------------

Service PrintLabel(ZPLString, PrinterId)
	
    SuccessfulPrint = False$
    ErrorMessage    = ''
    If PrinterId EQ '' then
        PrinterId = Printer_Select(PrinterID)
    end
    If PrinterID NE '' then
        stat = Direct_Print('START', PrinterId, '', '')
        If Stat GE 0 then
           stat = Direct_Print('PRINT', ZPLString)
           If Stat GE 0  then
               stat = Direct_Print('STOP')
           end
        end
        If Stat LT 0 then
            Begin Case
                Case Stat EQ -1
                    ErrorMessage = "A subsequent START message was sent prior to a previous report's STOP message was invoked."
                Case Stat EQ -2
                    ErrorMessage = 'Unable to initialize printer. The printer name may be incorrect or the printer is not avaible'
                Case Stat EQ -3 OR Stat EQ -4
                    ErrorMessage = 'Problems starting the report.'
                Case Stat EQ -5 OR Stat EQ -6
                    ErrorMessage = 'Problems stopping the report.'
                Case Stat EQ -7
                    ErrorMessage = 'Error with printing data - fatal error sending data to the printer.'
                Case Stat EQ -8
                    ErrorMessage = 'Error with printing data - wrong number of bytes sent to the printer.'
                Case Stat EQ -9 OR Stat EQ -10
                    ErrorMessage = 'Error occurred sending the PAGE message'
                Case Stat EQ -11
                    ErrorMessage = 'Problems aborting the report.'
                Case Stat EQ -12
                    ErrorMessage = 'The Direct_Print function was called prior to the START message being invoked.'
            End Case
        end
    end
    If ErrorMessage NE '' then
        Error_Services('Add', ErrorMessage)
    end
    
end service


Service PrintCalibrationLabel(EquipmentID, LastCalibration, NextCalibration, CalibratedBy, FrequencyVal, FrequencyUnit, PrinterSelect)
    
    If (EquipmentID NE '') AND (LastCalibration NE '') AND (NextCalibration NE '') AND (CalibratedBy NE '') AND (FrequencyVal NE '') AND (FrequencyUnit NE '') then
        ZPLString = ''
        
        // Print Params 1x2
         ZPLString := '^XA'
         ZPLString := '^LH0,0'
         ZPLString := '^PR1'
         ZPLString := '^LL406'
         ZPLString := '^PW406'
         ZPLString := '^MD22'
         ZPLString := '^MMT'

         ZPLString := '^FO125,15^A025,25^FDCalibration^FS'

         ZPLString := '^FO30,50^FW^A020,20^FDEquip ID:^FS'
         ZPLString := '^FO125,50,2^FW^A020,20^FD<EquipmentID>^FS'

         ZPLString := '^FO30,80,2^FW^A020,20^FDLast Cal:^FS'
         ZPLString := '^FO125,80^FW^A020,20^FD<LastCalibration>^FS'

         ZPLString := '^FO30,110^FW^A020,20^FDNext Cal:^FS'
         ZPLString := '^FO125,110^FW^A020,20^FD<NextCalibration>^FS'

         ZPLString := '^FO30,140^FW^A020,20^FDCal By:^FS'
         ZPLString := '^FO125,140^FW^A020,20^FD<CalibratedBy>^FS'

         ZPLString := '^FO30,170^FW^A020,20^FDCal Freq:^FS'
         ZPLString := '^FO125,170^FW^A020,20^FD<FreqVal>^FS'
         ZPLString := '^FO145,170,2^FW^A020,20^FD<FreqUnit>^FS'

         ZPLString := '^XZ'
        
        Swap '<EquipmentID>' With EquipmentID in ZPLString
        Swap '<LastCalibration>' With LastCalibration in ZPLString
        Swap '<NextCalibration>' With NextCalibration in ZPLString        
        Swap '<CalibratedBy>' With CalibratedBy in ZPLString
        Swap '<FreqVal>' With FrequencyVal in ZPLString
        Swap '<FreqUnit>' With FrequencyUnit in ZPLString
        
*        SRP_Set_Clipboard(ZPLString)
        
        If PrinterSelect NE '' then
            Labeling_Services('PrintLabel', ZPLString, PrinterSelect)
        end else
            Labeling_Services('PrintLabel', ZPLString, Printer)
        end
        
    end else
        Error_Services('Add', 'Missing parameter. EquipmentID, LastCalibration, NextCalibration, CalibratedBy, FrequencyVal, and FrequencyUnit are required fields.')
    end
    
end service


Service ReprintHoldLabel(EntityType, LotID)
	
	WOMatKey = ''
	Begin Case
		Case EntityType EQ 'RDS'
			WOMatKey = Xlate('RDS', LotID, 'WO_MAT_KEY', 'X')
			Reactor = Xlate('RDS', LotID, 'REACTOR', 'X')
			PSN = Xlate('RDS', LotID, 'PROD_SPEC_ID', 'X')
		Case EntityType EQ 'WM_IN'
			WOMatKey = Xlate('WM_IN', LotID, 'WO_MAT_KEY', 'X')
			Reactor = 'EPP'
			PSN = Xlate('WM_IN', LotID, 'PS_NO', 'X')
		Case EntityType EQ 'WM_OUT'
			WOMatKey = Xlate('WM_OUT', LotID, 'WO_MAT_KEY', 'X')
			Reactor = 'EPP'
			PSN = Xlate('WM_OUT', LotID, 'PS_NO', 'X')
	End Case
	WOMatRec = Xlate('WO_MAT', WOMatKey, '', 'X')
	Stage = WOMatRec<WO_MAT_HOLD_STAGE$>
	Reason = WOMatRec<WO_MAT_HOLD_START_REASON$,1>
	HoldBy = WOMatRec<WO_MAT_HOLD_START_USER$,1>
	DTM	   = WOMatRec<WO_MAT_HOLD_START_DTM$,1>
	Interrupted = WOMatRec<WO_MAT_HOLD_INTERRUPTED$>
	If (Stage NE '') AND (Reason NE '') AND (HoldBy NE '') AND (DTM NE '') then
		Labeling_Services('PrintHoldLabel', EntityType, LotID, Stage, Reason, HoldBy, DTM, PSN, Reactor, Interrupted)
	end else
		Error_Services('Add', 'Error reprinting label.')
	end
	
end service


Service PrintHoldLabel(EntityType, LotID, Stage=STAGES, Reason, HoldBy, DTM, PSN, Reactor, Interrupted, PrinterSelect)
    
    If (EntityType NE '') AND (LotID NE '') AND (Stage NE '') AND (Reason NE '') AND (HoldBy NE '') AND (DTM NE '') then
        ZPLString = ''
        // Print Params
        ZPLString := '^XA'
        ZPLString := '^LH0,0'
        ZPLString := '^PR1'
        ZPLString := '^LL406'
        ZPLString := '^PW900'
        ZPLString := '^MD22'
        ZPLString := '^MMT'
		// Hold Header
        ZPLString := '^FO350,24,2^FWN^A050,50^FDHold^FS'
		// Lot ID
		ZPLString := '^FO40,80,2^FWN^A035,35^FDLot ID:^FS'
		ZPLString := '^FO140,80,2^FWN^A035,35^FD<LotVal>^FS'
		// Reactor
		ZPLString := '^FO450,80,2^FWN^A035,35^FDReactor:^FS'
		ZPLString := '^FO570,80,2^FWN^A035,35^FD<ReactorVal>^FS'
		// PSN
		ZPLString := '^FO640,80,2^FWN^A035,35^FDPSN:^FS'
		ZPLString := '^FO715,80,2^FWN^A035,35^FD<PSNVal>^FS'
		// Pre box and X
		ZPLString := '^FO40,125,2^FWN^A035,35^FDPre:^FS'
		ZPLString := '^FO120,120^GB35,35,2^FS'
		ZPLString := '^FO127,125,2^FWN^A035,35^FD<PreX>^FS'
		// Post box and X
		ZPLString := '^FO170,125,2^FWN^A035,35^FDPost:^FS'
		ZPLString := '^FO252,120^GB35,35,2^FS'
		ZPLString := '^FO259,125,2^FWN^A035,35^FD<PostX>^FS'
		// Process interrupted box and X
		ZPLString := '^FO465,125,2^FWN^A035,35^FDProcess Interrupted:^FS'
		ZPLString := '^FO760,120^GB35,35,2^FS'
		ZPLString := '^FO767,125,2^FWN^A035,35^FD<InterruptX>^FS'
		// Reason
		ZPLString := '^FO40,175,2^FWN^A035,35^FDReason:^FS'
		ZPLString := '^FO160,175,2^FWN^A035,35^FD<ReasonVal1>^FS'
		ZPLString := '^FO40,220,2^FWN^A035,35^FD<ReasonVal2>^FS'
		ZPLString := '^FO40,265,2^FWN^A035,35^FD<ReasonVal3>^FS'
		// Hold By
		ZPLString := '^FO40,310,2^FWN^A035,35^FDOn Hold By:^FS'
		ZPLString := '^FO210,310,2^FWN^A035,35^FD<HoldByVal>^FS'
		// Date/Time
		ZPLString := '^FO40,355,2^FWN^A035,35^FDDate/Time:^FS'
		ZPLString := '^FO195,355,2^FWN^A035,35^FD<DTMVal>^FS'
		// End
		ZPLString := '^XZ'
        
        Begin Case
            Case EntityType EQ 'WM_IN'
                EntityType = 'WMI'
            Case EntityType EQ 'WM_OUT'
                EntityType = 'WMO'
            Case Otherwise$
        End Case
        EntityString = EntityType :'-': LotID
        UserRec = Database_Services('ReadDataRow', 'LSL_USERS', HoldBy)
	    FullName = UserRec<LSL_USERS_FIRST$> : ' ' : UserRec<LSL_USERS_LAST$>
	    DTM = Oconv(DTM, 'DT2/^H')
	    
	    If Len(Reason) GE 45 then
			// Breaks the reason up to fit the lines
            Dummy = Reason[45, 'B':' ']
            ReasonLine1 = Reason[1, Col1()]
            Reason = Reason[Col1() + 1, 999]
            Dummy = Reason[55, 'B':' ']
            ReasonLine2 = Reason[1, Col1()]
            Reason = Reason[Col1() + 1, 999]
            Dummy = Reason[55, 'B':' ']
            ReasonLine3 = Reason[1, Col1()]
        end else
            ReasonLine1 = Reason
            ReasonLine2 = ''
            ReasonLine3 = ''
        end
               
        Swap '<LotVal>' With EntityString in ZPLString
        Swap '<PSNVal>' With PSN in ZPLString
        Swap '<ReactorVal>' With Reactor in ZPLString
        
        If Stage EQ 'PRE' then
            Swap '<PreX>' With 'X' in ZPLString
            Swap '<PostX>' With '' in ZPLString
        end else
            Swap '<PreX>' With '' in ZPLString
            Swap '<PostX>' With 'X' in ZPLString
        end
        
        If Interrupted EQ True$ then
            Swap '<InterruptX>'with 'X' in ZPLString
        end else
            Swap '<InterruptX>'with '' in ZPLString
        end
        
        Swap '<ReasonVal1>' With ReasonLine1 in ZPLString
        Swap '<ReasonVal2>' With ReasonLine2 in ZPLString
        Swap '<ReasonVal3>' With ReasonLine3 in ZPLString
        
        Swap '<HoldByVal>' With FullName in ZPLString
        Swap '<DTMVal>' With DTM in ZPLString
        
        If PrinterSelect NE '' then
            Labeling_Services('PrintLabel', ZPLString, PrinterSelect)
        end else
            Labeling_Services('PrintLabel', ZPLString, Printer)
        end
        
    end else
        Error_Services('Add', 'Missing parameter. EntityType, LotID, Stage, Reason, HoldBy, DTM, PSN, Reactor, and Interrupted are required fields.')
    end
    
end service


Service PrintTestWaferLotLabel(LotId, Username, PrinterId)
	
    ErrorMessage = ''
    If Username EQ '' then
        Username = @User4
        If Username EQ '' then Username = @USERNAME
    end
    If RowExists('LOT', LotId) then
        ZPLStringLabel  = ''
        LotRec           = Database_Services('ReadDataRow', 'LOT', LotId)
        PartNoId         = LotRec<LOT_PROD_ID$>
        PartNo           = Xlate('TEST_WAFER_PROD', PartNoId, TEST_WAFER_PROD_PART_NAME$, 'X')
        VendorPartNo     = LotRec<LOT_VENDOR_PART_NO$>
        VendorLotNo      = LotRec<LOT_VENDOR_LOT_NO$>
        VendorCode       = LotRec<LOT_VENDOR_CODE$>
        LotQty           = LotRec<LOT_WAFER_QTY$>
        For LabelCnt = 1 to 2
            
            ZPLStringLabel := '^XA'
            ZPLStringLabel := '^LH0,0'
            ZPLStringLabel := '^PR1'
            ZPLStringLabel := '^LL406'
            ZPLStringLabel := '^PW900'
            ZPLStringLabel := '^MD22'
            ZPLStringLabel := '^MMT'
            ZPLStringLabel := '^FO25,25^A045,70,40^FD(1T)Lot Number: ^FS^FX(Lot Number Title)^FS'
            ZPLStringLabel := '^FO320,25^A045,70,40^FD':LotId:'^FS^FX(Lot Number Text)^FS'
            ZPLStringLabel := '^FO25,200^A070,40^FD':PartNo:'^FS^FX(Part Number Text)^FS'
            ZPLStringLabel := '^FO60,300^BY7^B3,,50,N^FD1T':LotId:'^FS^FX(Lot Number 1D Barcode)^FS'
            ZPLStringLabel := "^FO25,370^A045,28^FDWe do what we promise. That's quality made by Infineon.^FS^FX(Infineon Quality Statement)^FS"
            ZPLStringLabel := '^FO500,105^CI28'
            ZPLStringLabel := '^BXN,5,200^FDP':PartNo:'|S':VendorPartNo:'|1T':LotId:'|2T':VendorLotNo:'|(PSN)|Q':LotQty:'|1V':VendorCode:'|SEQ':LabelCnt:'^FS^FX(2DBarcode)^FS'
            ZPLStringLabel := '^XZ'
        Next LabelCnt
        If ZPLStringLabel NE '' then
            Labeling_Services('PrintLabel', ZPLStringLabel, PrinterId)
            If Error_Services('NoError') then
                //Log Event for lot.
            end else
                //Log Error for lot
                ErrorMessage = Error_Services('GetMessage')
            end
        end
    end else
        ErrorMessage = 'Error printing label for lot ' : LotId : '. LotId was null or did not exist in database.'
    end
    If ErrorMessage EQ '' then
        Lot_Event_Services('CreateLotEvent', LotId, Datetime(), 'COMMENT', 'Lot Labels Printed', '', Username)
    end else
        Error_Services('Add', ErrorMessage)
    end
    
End Service


Service GetTestWaferLotZPL(LotId, Username)
	
    ZPLStringLabel = ''
    LotRec         = Database_Services('ReadDataRow', 'LOT', LotId)
    PartNoId       = LotRec<LOT_PROD_ID$>
    PartNo         = Xlate('TEST_WAFER_PROD', PartNoId, TEST_WAFER_PROD_PART_NAME$, 'X')
    VendorPartNo   = LotRec<LOT_VENDOR_PART_NO$>
    VendorLotNo    = LotRec<LOT_VENDOR_LOT_NO$>
    VendorCode     = LotRec<LOT_VENDOR_CODE$>
    LotQty         = LotRec<LOT_WAFER_QTY$>
    For LabelCnt = 1 to 2
        
        ZPLStringLabel := '^XA'
        ZPLStringLabel := '^LH0,0'
        ZPLStringLabel := '^PR1'
        ZPLStringLabel := '^LL406'
        ZPLStringLabel := '^PW900'
        ZPLStringLabel := '^MD22'
        ZPLStringLabel := '^MMT'
        ZPLStringLabel := '^FO25,25^A045,70,40^FD(1T)Lot Number: ^FS^FX(Lot Number Title)^FS'
        ZPLStringLabel := '^FO320,25^A045,70,40^FD':LotId:'^FS^FX(Lot Number Text)^FS'
        ZPLStringLabel := '^FO25,175^A070,75^FD':PartNo:'^FS^FX(Part Number Text)^FS'
        ZPLStringLabel := '^FO60,300^BY2^B3,,50,N^FD1T':LotId:'^FS^FX(Lot Number 1D Barcode)^FS'
        ZPLStringLabel := "^FO25,370^A045,28^FDWe do what we promise. That's quality made by Infineon.^FS^FX(Infineon Quality Statement)^FS"
        ZPLStringLabel := '^FO500,105^CI28'
        ZPLStringLabel := '^XZ'
    Next LabelCnt
    Response = ZPLStringLabel
    
end service


Service GetAllZebraPrinters()
	
    Server               = Environment_Services('GetServer')
    ZebraPrinterList     = ''
    ZebraPrinterList<1>  = 'MESZBRPRT002' : @VM : '10.95.30.247' : @VM : 9100
    ZebraPrinterList<2>  = 'MESZBRPRT003' : @VM : '10.95.1.27' : @VM : 9100
    ZebraPrinterList<3>  = 'MESZBRPRT004' : @VM : '10.95.15.252' : @VM : 9100
    ZebraPrinterList<4>  = 'MESZBRPRT005' : @VM : '10.95.30.42' : @VM : 9100
    ZebraPrinterList<5>  = 'MESZBRPRT006' : @VM : '10.95.25.25' : @VM : 9100
    ZebraPrinterList<6>  = 'MESZBRPRT007' : @VM : '10.95.1.13' : @VM : 9100
    ZebraPrinterList<7>  = 'MESZBRPRT008' : @VM : '10.95.164.24' : @VM : 9100
    ZebraPrinterList<8>  = 'MESZBRPRT0011' : @VM : '10.95.15.33' : @VM : 6101
    ZebraPrinterList<9>  = 'FI_ZEBRA_PRINTER' : @VM : '10.95.164.55' : @VM : 9100
    If Server EQ 'MESTSA01EC'  OR Server EQ 'MESSA01EC' or Server EQ 'MESTSA09EC' |
    or Server EQ 'MESTSA010EC' or Server EQ 'MESTSA011EC' or Server EQ 'MESTSA012EC' then
        ZebraPrinterList<10> = 'JONATHAN_HOME_TEST' : @VM : '192.168.0.123' : @VM : 9100
    end
    
    Response = ZebraPrinterList
    
end service


Service PrintReactorLabel(ReactNo, LoadLockSide=LOAD_LOCK_SIDE)

	If ( (ReactNo NE '') and (LoadLockSide NE '') ) then
		BaseZpl            = '^XA^LH10,10^FO35,5^BY2^B3N,,80,N^FD10SR{ReactNo}.{LoadLockSidePrefix}^FS^FO10,105^A045,90^FB400,1,,C^FDR{ReactNo} {LoadLockSide}^FS^XZ'			
		Convert 'R' to '' in ReactNo
		If LoadLockSide _EQC 'Both' then
			LoadLockSidePrefix = 'L'
			ZplLeft            = BaseZpl
			Swap '{ReactNo}' with ReactNo in ZplLeft
			Swap '{LoadLockSidePrefix}' with LoadLockSidePrefix in ZplLeft
			Swap '{LoadLockSide}' with 'Left' in ZplLeft			
			ZplRight           = BaseZpl
			LoadLockSidePrefix = 'R'
			Swap '{ReactNo}' with ReactNo in ZplRight
			Swap '{LoadLockSidePrefix}' with LoadLockSidePrefix in ZplRight
			Swap '{LoadLockSide}' with 'Right' in ZplRight
			Zpl                = ZplLeft:ZplRight
		end else
			LoadLockSidePrefix = LoadLockSide[1, 1]
			Zpl                = BaseZpl
			Swap '{ReactNo}' with ReactNo in Zpl
			Swap '{LoadLockSidePrefix}' with LoadLockSidePrefix in Zpl
			Swap '{LoadLockSide}' with LoadLockSide in Zpl
		end
		Labeling_Services('PrintLabel', Zpl, '\\messp1003.infineon.com\MESZBRPRT008')
	end else
		Error_Services('Add', 'Error in ':Service:' service. Null ReactNo or LoadLockSide passed in.')
	end
	
end service


Service GetReturnToFabLabelZPL(RTFId)
	
    ZPLStringLabel    = ''
    LotRec            = Database_Services('ReadDataRow', 'RETURN_TO_FAB_LOTS', RTFId)
    CassId            = LotRec<RETURN_TO_FAB_LOTS_CASS_ID$>
    CassType          = LotRec<RETURN_TO_FAB_LOTS_LOT_TYPE$>
    ReturnToFabReason = LotRec<RETURN_TO_FAB_LOTS_REASON$>
    CassIdText        = CassId
    BarcodeText       = ""
    Begin Case
        Case CassType EQ 'RDS'
            BarcodeText = '1T' : CassId
        Case CassType EQ 'WM_OUT'
            swap '*' with '.' in CassId
            CassIdText  = CassId
            BarcodeText = 'O' : CassId
    End Case
    
    ZPLStringLabel := '^XA'
    ZPLStringLabel := '^LH0,0'
    ZPLStringLabel := '^PR1'
    ZPLStringLabel := '^LL406'
    ZPLStringLabel := '^PW900'
    ZPLStringLabel := '^MD22'
    ZPLStringLabel := '^MMT'
    ZPLStringLabel := '^FO25,25^A070,75^FDReturn To Fab^FS^FX(Title)^FS'
    ZPLStringLabel := '^FO25,100^A045,70,40^FD(1T)Lot Number:':CassIdText:' ^FS^FX(Lot Number Title)^FS'
    ZPLStringLabel := '^FB780,5,0,L^FO25,180^A045,30,30^FD':ReturnToFabReason:'^FS'
    ZPLStringLabel := '^FO60,325^BY2^B3,,50,N^FD':BarcodeText:'^FS^FX(Lot Number 1D Barcode)^FS'
    ZPLStringLabel := '^FO500,105^CI28'
    ZPLStringLabel := '^XZ'
    Response        = ZPLStringLabel
    
end service



