compile function TEST3() 
begin condition
pre:
post:
end condition
declare function msg, set_property, send_event, dialog_box, utility, get_property
declare function set_printer, fieldcount, key_sort
declare subroutine rlist, or_view, activate_save_select, yield, extract_si_keys
declare subroutine update_index, btree.extract
$insert logical
$insert rlist_equates
$insert msg_equates
$insert rds_equ
$insert wo_log_equ
$insert quote_equ
$insert quote_spec_equ
$insert company_equ
$insert reactor_log_equ
$insert oiprint_equates

Params = dialog_box( 'ENGINEER_RPT1', @window, '' )
if Params = 'CANCEL' or Params = '' then
   return 0
end
Stat = utility( 'CURSOR', 'H' )
open 'DICT.REACTOR_LOG' to DictReactorLogTable else
   Void = msg( '', 'Could not open DICT.REACTOR_LOG...' )
   return 0
end
****wo mast sched equates***
equ Days$       to 1
equ Month$      to 2
equ ReactorCnt$ to 3
****************************
****wo daily sched equates**
equ ReactControl$ to 1
****************************
****react mode equates******
equ Status$     to 1
equ User$       to 2
equ ChangeDate$ to 3
equ ChangeTime$ to 4
****************************
convert '*' to @fm in Params
Reactors     = Params<1>
DayRange     = Params<2>
DirToPrinter = Params<3>
DatesToUse   = ''
Today        = date()
begin case
   case DayRange = '3 Day View'
      DatesToUse = Today-1:@fm:Today:@fm:Today+1 
   case DayRange = 'Today'
      DatesToUse = Today
   case DayRange = 'Yesterday'
      DatesToUse = Today-1
   case DayRange = 'Tomorrow'
      DatesToUse = Today+1
end case
ODatesToUse = DatesToUse
Dcnt = fieldcount( DatesToUse, @fm )
for i = 1 to Dcnt
   DatesToUse<i> = oconv( DatesToUse<i>, 'D2/' )
   DatesToUse<i> = field( DatesToUse<i>, '/', 2 )+0 ;* pull out the day of the month
   * so it can be used in a locate on the master scheduler record
next i 
WoMastRec = xlate( 'CONFIG', 'WO_MAST_SCHED', '', 'X' )
if Reactors then
   * will be @vm delimited
end else
   * no reactors were specified so get the number of reactors from 
   * the master scheduler and make array of reactors 1-whatever
   Reactors = ''
   for i = 1 to WoMastRec<ReactorCnt$>
      Reactors := i:@vm
   next i 
   Reactors[-1,1] = ''
end
Rcnt = fieldcount( Reactors, @vm )
****The printing subroutines will handle oconverting the report****
****Report Variables****
rpCurrentStatus  = ''
rpCurrentWO      = ''
rpCurrentBoxOf   = ''
rpStartDate      = ''
rpBoxStart       = ''
rpLastBoxElapsed = ''
************************
MastSchedRec = xlate( 'CONFIG', 'WO_MAST_SCHED', '', 'X' )
gosub PrinterInit
for i = 1 to Rcnt
   ThisReactor = Reactors<1,i>
   * take each reactor and try to find it in one of the wo_daily_sched1-999
   * quit looking if we get a null record from wo_dail_sched:i
   * meaning we are at the end
   Done = false$
   ReactorFound = false$
   for j = 1 to 999
      DailySchedRec = xlate( 'CONFIG', 'WO_DAILY_SCHED':j, '', 'X' )
      if len(DailySchedRec) then
         * we have a daily sched rec, now try to find in
         locate 'RW':ThisReactor in DailySchedRec<ReactControl$> using @vm setting Fpos then
            ReactorFound = true$
			   RModeRec = xlate( 'CONFIG', 'REACT_MODE':ThisReactor, '', 'X' )
			   rpCurrentStatus = RModeRec<status$>
			   if rpCurrentStatus = 'N/A Not currently in use' then
			      goto NextReactor
			   end
			   rpCurrentWO     = DailySchedRec<Fpos+1> ;* cause of first field
			   trpCurrentWO    = rpCurrentWO
			   * strip off extra work order information such as layering etc.
			   * and assign as trpcurrentWO
			   SeperateLayer   = ''
				LayPos = index( trpCurrentWO, 'LAY', 1 )
				if LayPos then
				   BegPos = LayPos +3 ;* for length of lay
				   SeperateLayer = trpCurrentWO[BegPos,'F>']
				   swap '<LAY':SeperateLayer:'>' with '' in trpCurrentWO
				end
				swap '52nd' with '' in trpCurrentWO
				convert @upper_case to '' in trpCurrentWO
				convert @lower_case to '' in trpCurrentWO
				convert " `~!@#$%^&*()_+-=\][{}|';:/?.>,<" to '' in trpCurrentWO
				convert '"' to '' in trpCurrentWO
				* now use that trpCurrentWO val to get the box information
   		   extract_si_keys( 'RDS', 'WO',  trpCurrentWO, TRdsRecs )
  		      tRcnt = fieldcount( TRdsRecs, @vm )
   		   RdsToProc = ''
		      if SeperateLayer then
		         if SeperateLayer = 1 then
		            * eliminate all layers except one
		            Trds = TRdsRecs
		            RdsToProc = ''
		            for zz = 1 to tRcnt
		               if index( Trds<1,zz>, '.', 1 ) else
		                  RdsToProc := Trds<1,zz>:@vm
		               end
		            next zz
		            RdsToProc[-1,1] = ''
		         end else
		            * have to extract only the .layers
		            Trds = TRdsRecs
		            RdsToProc = ''
		            for zz = 1 to TRcnt
		               if index( Trds<1,zz>, '.':SeperateLayer, 1 ) then
		                  RdsToProc := Trds<1,zz>:@vm
		               end
		            next zz
		            RdsToProc[-1,1] = ''
		         end
		      end
   		   if RdsToProc then
   		      TRdsRecs = RdsToProc
   		   end
   		   if TRdsRecs then
   		      tRcnt = fieldcount( TRdsRecs, @vm )
   		      Rdone = false$
   		      CurrentBox = ''
   		      RdsForRightReact = '' ;* used to build array of boxes that ran on this reactor
   		                            ;* this way can find the previous one
               CurRds = '' 
   		      for k = 1 to tRcnt
   		         ThisRdsRec = xlate( 'RDS', TRdsRecs<1,k>, '', 'X' )
   		         if ThisRdsRec<rds_reactor$> = ThisReactor then ;* may have split the WO
   		            RdsForRightReact := TrdsRecs<1,k>:@fm
							begin case
							   case (ThisRdsRec<rds_date_in$> <> '') and (ThisRdsRec<rds_date_out$>)
							      * box is finished go to the next
							      CurrentBox = k
							      CurRds = TRdsRecs<1,k>
							      * set the current box even if it is done this way
							      * if the user has not started the next box then this will show 
							      * as the current box
							   case (ThisRdsRec<rds_date_in$> = '') and (ThisRdsRec<rds_date_out$> = '')
							      * box has not started 
							      Rdone = true$
							   case (ThisRdsRec<rds_date_in$> <> '') and (ThisRdsRec<rds_date_out$> = '')
							      * box has started but has not finished
							      CurrentBox = k
							      Rdone = true$
							      CurRds = TRdsRecs<1,k>							      
							end case
						end
   		        until Rdone
   		      next k
   		      if CurrentBox <> '' then
   		         * have to reread in the case of the user did not start the next box
   		         * then we would not have the current box rds
   		         ThisRdsRec = xlate( 'RDS', TrdsRecs<1,CurrentBox>, '', 'X' )
						rpCurrentBoxOf   = 'Box ':CurrentBox:' of ':tRcnt
						rpStartDate      = oconv( ThisRdsRec<rds_date_in$>, 'D2/' ) 
						rpBoxStart       = oconv( ThisRdsRec<rds_time_in$>, 'MTH' )
   		         if CurrentBox > 1 then
   		            locate CurRds in RdsForRightReact using @fm setting RdsFPos then
   		               rpLastBoxElapsed = xlate( 'RDS', RdsForRightReact<RdsFPos-1>, 'ELAPSED_HOURS', 'X' )
   		            end else
   		               rpLastBoxElapsed = ''
   		            end
   		         end else
   						rpLastBoxElapsed = 'N/A'    		         
   		         end
   		      end else
						rpCurrentBoxOf   = 'Has not started'
						rpStartDate      = 'N/A'
						rpBoxStart       = 'N/A'
						rpLastBoxElapsed = 'N/A'
   		      end
     		   end ;* no TRdsRecs   
  		      gosub PrintReactStat
  		      for l = 1 to Dcnt
  		         Table = 'Layer info':@vm:'Sched WO#':@vm:'Cnt':@vm:'Customer':@vm:'Target Thick':@vm:'Thick Measure':@vm:'Target Res':@vm:'Res Measure':@vm:'S.I.':@vm:"Conc":@vm:'Recipe'
  		         * this is set once for each day for the detail headings
  		         locate DatesToUse<l> in MastSchedRec<Days$> using @vm setting Dpos then
  		            * Looping through the possible 3 days
 		            	Font<2> = 10
                   	Void = set_printer( 'FONT', Font )
  		            Void = set_printer( 'TEXT', oconv( ODatesToUse<l>, 'D2/' ))
						Font<2> = 8
						Void = set_printer( 'FONT', Font )
  		            DayValues = MastSchedRec<Dpos+3> ;* plus three cause of first three fields
  		            ReactValues = DayValues<1,ThisReactor>
  		            * strip down ReactValues to get bare WO#'S
						ReactValues = field( ReactValues, '|', 1 ) ;* remove comments
						swap "52nd" with '' in ReactValues
						Convert ';' to @vm in ReactValues
						convert @upper_case to '' in ReactValues
						convert @lower_case to '' in ReactValues
						convert " `~!@#$%^&*()_+-=\][{}|';:/?.>,<" to '' in ReactValues
						convert '"' to '' in ReactValues
						rvCnt = fieldcount( ReactValues, @vm )
						for m = 1 to rvCnt
						   * tell Tom and Rick they need to put a value in the quote_no field in th
						   * work order record
						   RptRow = ''
						   RptRow<1,2> = ReactValues<1,m>
   	      		   extract_si_keys( 'RDS', 'WO',  RptRow<1,2>, TtRdsRecs )
  					      TBoxCnt = fieldcount( TtRdsRecs, @vm )
                     RptRow<1,3> = TBoxCnt
						   QuoteNo = xlate( 'WO_LOG', ReactValues<1,m>, wo_log_quote_no$, 'X' )
						   QuoteRec = xlate( 'QUOTE', QuoteNo, '', 'X' )
						   RptRow<1,4> = xlate( 'COMPANY', QuoteRec<quote_cust_no$>, company_abbrev$, 'X' )
						   SpecSubstrate = QuoteRec<quote_spec_substrate$>
							SpecEpi       = QuoteRec<quote_spec_epi$>
							EpiLayers = field( SpecEpi, char(245), 1 )
							EpiOther  = field( SpecEpi, char(245), 2 ) 
							convert char(247) to @fm in EpiLayers ;* each seperate layer
							SepLCnt = fieldcount( EpiLayers, @fm)
							******SEPERATE REACTOR LAYERS*********************************								
							for n = 1 to SepLCnt
							   RptRow<1,1> = 'Sep Lay ':n              
 							   ThisSepLayer = EpiLayers<n>
 								RptRow<1,6> = EpiOther<1,QSOEpiThickMeasure$>
 								RptRow<1,8> = EpiOther<1,QSOEpiResMeasure$>
 								if SpecSubstrate<1,QSSubInstructions$> <> '' then
 								   RptRow<1,9> = 'Yes'
								end else
								   RptRow<1,9> = 'No'
								end
     							ConMinThick  = iconv( ThisSepLayer<1,QSEpiConcMinThick$,1>, 'MS' );* take only first val of concentration
					         ConMaxThick  = iconv( ThisSepLayer<1,QSEpiConcMaxThick$,1>, 'MS' );* take only first val of concentration
		      	         if ConMinThick and ConMaxThick then
	                        if ConMinThick = ConMaxThick then
                              RptRow<1,10> = ConMinThick
                           end else
                              MinLessMax = (ConMaxThick-ConMinThick)
                              Tans = MinLessMax/2
                              RptRow<1,10> = ConMinThick+Tans
                           end
                        end 
							   ResMinThick  = ThisSepLayer<1,QSEpiResMinThick$>  
							   ResMaxThick  = ThisSepLayer<1,QSEpiResMaxThick$>   
							   ResUnits     = ThisSepLayer<1,QSEpiResUnits$>  
							   MinThick     = ThisSepLayer<1,QSEpiMinThick$>      
							   MaxThick     = ThisSepLayer<1,QSEpiMaxThick$>      
						      if ResMinThick and ResMaxThick then
							      if ResMinThick = ResMaxThick then
							         RptRow<1,7> = ResMinThick
							      end else
						            Mid = ResMinThick + ((ResMaxThick-ResMinThick)/2)
						            RptRow<1,7> = Mid
						         end	            
							   end    
							   if MinThick and MaxThick then
							      if MinThick = MaxThick then
							         RptRow<1,5> = MinThick
							      end else
							         Mid =  MinThick + ((MaxThick-MinThick)/2)
							         RptRow<1,5> = Mid
							      end
							   end
							   RptRow<1,11> = ThisSepLayer<1,QSEpiRecipe$>:' ':field(ThisSepLayer<1,QSEpiRecipeName$>, char(248), 1 )
							   gosub PrintLayer
								******SAME REACTOR LAYERS*********************************
                        convert char(248) to @fm in ThisSepLayer
                        RestOfLayers = field( ThisSepLayer, @fm, 2, 9999 )
                        RolCnt = fieldcount( RestOfLayers, @fm )
                        for o = 1 to RolCnt
   						      RptRow<1,1> = 'Same Lay ':o                                         
                           ThisSameLayer = RestOfLayers<o>
       							ConMinThick  = iconv( ThisSameLayer<1,QSEpiConcMinThick$,1>, 'MS' );* take only first val of concentration
						         ConMaxThick  = iconv( ThisSameLayer<1,QSEpiConcMaxThick$,1>, 'MS' );* take only first val of concentration
			      	         if ConMinThick and ConMaxThick then
			                     if ConMinThick = ConMaxThick then
                                 RptRow<1,10> = ConMinThick
                              end else
                                 MinLessMax = (ConMaxThick-ConMinThick)
                                 Tans = MinLessMax/2
                                 RptRow<1,10> = ConMinThick+Tans
                              end
                           end 
							      ResMinThick  = ThisSameLayer<1,QSEpiResMinThick$>  
							      ResMaxThick  = ThisSameLayer<1,QSEpiResMaxThick$>   
							      ResUnits     = ThisSameLayer<1,QSEpiResUnits$>  
							      MinThick     = ThisSameLayer<1,QSEpiMinThick$>      
							      MaxThick     = ThisSameLayer<1,QSEpiMaxThick$>      
						         if ResMinThick and ResMaxThick then
							         if ResMinThick = ResMaxThick then
							            RptRow<1,7> = ResMinThick
							         end else
						               Mid = ResMinThick + ((ResMaxThick-ResMinThick)/2)
						               RptRow<1,7> = Mid
						            end	            
							      end    
							      if MinThick and MaxThick then
							         if MinThick = MaxThick then
  								         RptRow<1,5> = MinThick
							         end else
							            Mid =  MinThick + ((MaxThick-MinThick)/2)
							            RptRow<1,5> = Mid
							         end
							      end
  								   gosub PrintLayer								      
							   next o ;* same reactor layer
 									********************************************************
 						   next n ;* seperate reactor layer
						next m ;* each work order value in each day
						*now extract the reactor log records for engineering for that day
						*for that reactor
						*SearchStr = 'CATEGORY':@vm:'E':@fm ;* show all logs requested by Tim Scullard
						SearchStr = ''
						SearchStr:= 'START_DATE':@vm:oconv( ODatesToUse<l>, 'D2/' ):@fm
						SearchStr:= 'REACTOR':@vm:ThisReactor 
					   SearchStr:= @fm
					   * do a btree.extract
					   btree.extract( SearchStr, 'REACTOR_LOG', DictReactorLogTable, RlKeys, '', Flag )
					   if Flag <> 0 then
					      Void = msg( '', 'Error while extracting REACTOR_LOG records...' )
					      return 0
					   end
						if Rlkeys then
						   convert @vm to @fm in Rlkeys
                       RlKeys = key_sort( RlKeys, 'REACTOR_LOG', 'START_TIME', 1 )
							Font<2> = 10
							Void = set_printer( 'FONT', Font )
     					   Void = set_printer( 'TEXT', 'Reactor Logs' )
							Font<2> = 8
							Void = set_printer( 'FONT', Font )
						   RlCnt = fieldcount( RlKeys, @fm )
					      Table = ''
					      Table<-1> = 'User':@vm:'Date':@vm:'Time':@vm:'Service':@vm:'Notes'
						   for p = 1 to RlCnt
						      ThisRlRec = xlate( 'REACTOR_LOG', RlKeys<p>, '', 'X' )
						      Tvar = oconv( ThisRlRec<reactor_log_entry_id$>, '[XLATE_CONV,LSL_USERS*FIRST_LAST]' )
						      Tvar<1,-1> = oconv( ThisRlRec<reactor_log_start_date$>, 'D2/' )
						      Tvar<1,-1> = oconv( ThisRlRec<reactor_log_start_time$>, 'MTH' )
						      Tvar<1,-1> = ThisRlRec<reactor_log_service$>
						      Tnotes = ThisRlRec<reactor_log_notes$>
						      convert @tm to ' ' in Tnotes
						      Tvar<1,-1> = Tnotes
						      Table<-1> = Tvar
							   x = Set_Printer("TABLE", Table, '', 1.5:@fm:.75:@fm:.75:@fm:1.5:@fm:5.63, 'L':@fm:'C':@fm:'C':@fm:'L', TB_ALL)
							   Table = ''
						   next p
						end   		            		            		            
  		         end else
  		            * day not found
  		            Void = set_printer( 'TEXT', oconv( ODatesToUse<l>, 'D2/' )):' not found in Master Scheduler...'
  		         end  
  		      next l
         end else
           * not found so continue on to the 'WO_DAILY_SCHED':NEXTJ
           * failed on locate
         end 
      end else
         * Daily sched rec j not found so quit 
         Done = true$
      end 
     until Done 
   next j
   if ReactorFound then
	   Void = set_printer( 'TEXT', '' )
	   Void = set_printer( 'TEXT', '' )
	end
	************
	NextReactor:
	************
next i
Void = set_printer( 'TERM' ) 
Stat = utility( 'CURSOR', 'A' )
return 0
*============================================================================*
PrintReactStat:
	Font<2> = 10
	Void = set_printer( 'FONT', Font )
   * printing starts here
   Void = set_printer( 'TEXT', 'Reactor ':ThisReactor )
	Font<2> = 8
	Void = set_printer( 'FONT', Font )
   Table = ''
   Tvar = 'Current Status/Current WO/Current Box/Start Date/Box Start/Last Box Elapsed'
   convert '/' to @vm in Tvar
   Table<-1> = Tvar
   Table<-1> = rpCurrentStatus:@vm:rpCurrentWo:@vm:rpCurrentBoxof:@vm:rpStartDate:@vm:rpBoxStart:@vm:oconv( rpLastBoxElapsed, 'MD2' ) 
   x = Set_Printer("TABLE", Table, '', 1.5:@fm:1.5:@fm:1.5:@fm:1:@fm:1:@fm:1.5, 'L':@fm:'L':@fm:'C':@fm:'C':@fm:'C', TB_ALL)
   rpCurrentStatus  = ''
   rpCurrentWo      = ''
   rpCurrentBoxof   = ''
   rpStartDate      = ''
   rpBoxStart       = ''
   rpLastBoxElapsed = ''   
return
*============================================================================*
PrintLayer:
   RptRow<1,5> = oconv( RptRow<1,5>, 'MD2' ) ;* target thickness
   RptRow<1,7> = oconv( RptRow<1,7>, 'MD2' ) ;* target resistivity
   RptRow<1,10> = oconv( RptRow<1,10>, 'MS21') ;* resistivity 
   Table<-1> = RptRow
   x = Set_Printer("TABLE", Table, '', .75:@fm:.75:@fm:.25:@fm:1.25:@fm:1:@fm:1.19:@fm:1:@fm:1.19:@fm:.5:@fm:.5:@fm:1.75, 'L':@fm:'C':@fm:'L':@fm:'C':@fm:'L':@fm:'C':@fm:'L':@fm:'L':@fm:'L':@fm:'L', TB_ALL)      
   Table     = '' ;* this will remove the initial colheadings for detail lines
   HoldSix    = RptRow<1,6>
   HoldEight  = RptRow<1,8>
   HoldNine   = RptRow<1,9>
   HoldEleven = RptRow<1,11>           
   RptRow = ''
   RptRow<1,6>  = HoldSix
	RptRow<1,8>  = HoldEight
	RptRow<1,9>  = HoldNine
	RptRow<1,11> = HoldEleven
return
*============================================================================*
PrinterInit:
   * initialize the printer and set the headings
   if DirToPrinter = 'Yes' then
      OutputParam = 0
   end else
      OutputParam = 3 ;* maximized print preview
   end 
   Void = set_printer( 'INIT', '', '', '', 1, OutputParam )             
	TheHeader = " Engineering Report 'D' 'T'                                          Page # 'P'"
	Font = 'Times New Roman,12,L,1'	;* 14 point, Left just, Bold
   convert ',' to @fm in font		;* Use @FM delimiter
	Void = set_printer( 'FONTHEADFOOT', font )   
	Void = set_printer( 'HEADER', TheHeader, '', '' )
return

*============================================================================*

