compile function WO_LOG_RPT3()
begin condition
pre:
post:
end condition
declare function msg, set_property, send_event, dialog_box, utility, get_property
declare function set_printer, fieldcount
declare subroutine rlist, activate_save_select, yield, btree.extract, SetInitDirOptions, extract_si_keys

$insert prod_spec_equ
$insert rlist_equates
$insert logical
$insert msg_equates
$insert oiprint_equates
$insert wo_log_equ
$insert rds_equ

open 'WO_LOG' to WOLogTable else
   Void = msg( '', 'Unable to open WO_LOG' )
   return 0
end

open 'DICT.RDS' to DictRDSTable else
   Void = msg( '', 'Unable to open DICT_RDS' )
   return 0
end

AtmiSuppliedCusts = xlate( 'CONFIG', "ATMI_SUPPLIED_CUSTS", '', 'X' )
swap @fm with '" "' in AtmiSuppliedCusts
AtmiSuppliedCusts = '"':AtmiSuppliedCusts:'"'

Void = utility( 'CURSOR', 'H' )
Stmt = 'SELECT WO_LOG WITH CUST_NO = ':AtmiSuppliedCusts:' AND WITH STATUS = "O" BY CUST_NAME BY PROD_SPEC_ID'

rlist( Stmt, target_activelist$, '', '', '' )

* SET UP VARIABLES FOR THE REPORT
WeekResultArray    = 0:@fm:0:@fm:0:@fm:0:@fm:0:@fm:0:@fm:0:@fm:0
WRCnt = fieldcount( WeekResultArray, @fm )
ReceivedQty        = 0
WIPQty             = 0
ReadyToShipQty     = 0
HoldQty            = 0

Today  = date()
W1  = Today+7
W2  = Today+14
W3  = Today+21
W4  = Today+28
W5  = Today+35
W6  = Today+42
W7  = Today+49
W8  = Today+99999 ;* CAUSE YOU WANT IT TO NOT LOCATE PAST 8 IN THE ARRAY
WeekArray = W1:@fm:W2:@fm:W3:@fm:W4:@fm:W5:@fm:W6:@fm:W7:@fm:W8
ThisPSN = ''
OrigPSN = ''
* END OF REPORT VARIABLES

if @reccount else
   Void = msg( '', 'No Rows Found...' )
   return 0
end
BytePos = 0
OSOpen 'V:\AUTOEXEC.BAT' To TPathHandle then
   OSClose TPathHandle
   DriveToUse = 'V:'
end else
   DriveToUse = 'C:'
end
DosTable = DriveToUse:'\WOLOG3.TXT'
CfgFile = ''
CfgFile<1> = 17  ;* NUMBER OF EXCEL COLUMNS
CfgFile<2> = DosTable ;* DOS TEXT FILE
CfgFile<3> = 1   ;* NUMBER OF COLUMN HEADING ROWS
CfgFile<4> = 2   ;* NUMBER OF TEXT FIELD COLUMNS
CfgFile<5> = 1   ;* TEXT FIELD COLUMN ONE
CfgFile<6> = 3   ;* TEXT FIELD COLUMN TWO
swap @fm with CrLf$ in CfgFile
* WRITE OUT THE CONFIG FILE FOR EXCEL MACRO
oswrite CfgFile on DriveToUse:'\IMP.CFG' 
if status() then
	Void = msg( '', 'Error creating Excel import config file.' )
	return 0
end
* NOW WRITE OUT A BLANK DOSTABLE WHERE WE WILL BE WRITING THE DATA
status() = 0
oswrite '' on DosTable
Tstat = status()
if Tstat then
    Err = 'Bad operating system filename'
    Err<-1> = 'Access Denied'
    Err<-1> = 'Disk or Directory full'
    Err<-1> = 'Operating system error not defined elsewhere'
    Err<-1> = 'Attempt to write a read only file'
    Void = msg( '', Err<Tstat> )
    return 0
end

osopen DosTable to DosTableVar else
    Err = 'Bad operating system filename'
    Err<-1> = 'Access Denied'
    Err<-1> = 'File does not exist'
    Err<-1> = 'Undefined error'
    Void = msg( '', Err<status()> )
    return 0
end

* WRITE OUT THE HEADINGS FOR THE EXCEL WORKBOOK
LineOut = quote('Customer'):','
LineOut:= quote('PSN'):','
LineOut:= quote('Sub Part Nums'):','
Lineout:= quote('Rec'):','
LineOut:= quote('WIP'):','
LineOut:= quote('Ready'):','
LineOut:= quote('Hold'):','
for i = 1 to WRCnt
	LineOut:= quote('WK':i):','
next i

LineOut:= CrLf$
* NOW WRITE OUT THE LINE
osbwrite LineOut on DosTableVar AT BytePos
BytePos += len( LineOut )	   
Tstat = status()
if Tstat then
   Err = 'Bad operating system filename'
   Err<-1> = 'Access Denied'
   Err<-1> = 'Disk or directory full'
   Err<-1> = 'File does not exist'
   Err<-1> = 'Undefined error'
   Err<-1> = 'Attempt to write a read only file'
   Err<-1> = 'Invalid beginning byte position'
   Void = msg( '', Err<Tstat> )
   return 0
end      

Eof = false$
loop
  readnext @id else Eof = true$
 until Eof
   read @record from WOLogTable, @id else
      Void = msg( '', 'Unable to read ':@id:' from WO_LOG Table...' )
      return 0
   end
   ThisPSN = @record<wo_log_prod_spec_id$>
   if ( ThisPSN <> OrigPSN ) and ( OrigPSN <> '' ) then
      gosub PrintPSNLine
   end
   OrigPSN = ThisPSN
   CommitDate = @record<wo_log_commit_date$>
   locate CommitDate in WeekArray by 'AR' using @fm setting FPos then
      * If it located it then the commit date right on one of the weekarray values
      * What we are doing here is finding which week it falls into
   end
	* Go against the rds and find all rdss for that wo and sum up the quantity that has not shipped
	* We can'T JUST ADD THE QUANITY FROM THE WO_LOG CAUSE SOME OF THE WAFERS ARE ALREADY SHIPPED
   Keys = ''
   SearchStr = ''
   SearchStr<-1> = 'WO':@vm:@id   
   SearchStr<-1> = 'STATUS':@vm:'#S':@fm
   Keys = ''
   btree.extract( SearchStr, 'RDS', DictRDSTable, Keys, '', Flag ) 
   if Flag <> 0 then
      Void = msg( '', 'Error while extracting RDS records...' )
      return 0
   end
   if Keys <> '' then
      WeekResultArray<FPos> = WeekResultArray<FPos> + sum( xlate( 'RDS', Keys, 'WAFERS_SCHEDULED', 'X' ) )
   end else
      KeyList = ''
      extract_si_keys( 'RDS', 'WO', @id, KeyList )
      if KeyList = '' then
         * ONLY THE WORK ORDER HAS BEEN ENTERED SO WE WILL WANT TO USE THE WO_LOG QTY SO IT SHOWS UP
         * ON THE REPORT 
         WeekResultArray<FPos> = WeekResultArray<FPos> + @record<wo_log_qty$>
      end else
         * OBVIOUSLY THEY HAVE ALL SHIPPED BUT THE WO IS STILL OPEN
         WeekResultArray<FPos> = WeekResultArray<FPos> + 0
      end
   end
repeat
gosub PrintPSNLine
* PRINT THE TOTAL LINE AT THE BOTTOM OF THE REPORT
osclose DosTableVar
Void = utility( 'CURSOR', 'A' )
SetInitDirOptions("D")
List = ''
for i = 1 to 9
	OfficePath = DriveToUse:'\PROGRA~1\MICROS~':i:'\OFFICE'
	initdir OfficePath
	list = DirList()
 until list <> ''
next i
RetVal = utility( 'RUNWIN', OfficePath:'\EXCEL.EXE R:\OINSIGHT\IMPORT.XLS', '' )
return 0
*===========================================================================================================================*
PrintPSNLine:
   * DO PRINTING LOGIC FOR THE OrigPSN
   * SET ALL VARIABLES TO NULL
   Customer = xlate( 'PROD_SPEC', OrigPSN, 'ABBREV_OR_CO_NAME', 'X' )
   Thickness = oconv( xlate( 'PROD_SPEC', OrigPSN, 'THICK_TARGET_L1', 'X' ), 'MD2' )
   * NOW GET INVENTORY ITEMS
   Keys = ''
   SearchStr = ''
   SearchStr<-1> = 'STATUS':@vm:'#S'
   SearchStr<-1> = 'PROD_SPEC_ID':@vm:OrigPSN:@fm
   btree.extract( SearchStr, 'RDS', DictRDSTable, Keys, '', Flag ) 
   if Flag <> 0 then
      Void = msg( '', 'Error while extracting RDS records...' )
      return 0
   end
   *
   RDSStatus = xlate( 'RDS', Keys, rds_status$, 'X' )
   SchedQtys = xlate( 'RDS', Keys, 'WAFERS_SCHEDULED', 'X' )
   HoldStatus = xlate( 'RDS', Keys, rds_hold$, 'X' )
   RCnt = fieldcount( Keys, @vm )
   for i = 1 to RCnt
      ThisStatus     = RDSStatus<1,i>
      ThisSchedQty   = SchedQtys<1,i>
      ThisHoldStatus = HoldStatus<1,i>
      if ThisHoldStatus then
         HoldQty += ThisSchedQty
      end else
	      begin case
	         case ThisStatus = 'C'
	    			ReceivedQty += ThisSchedQty         
	         case ThisStatus = 'E' or ThisStatus = 'I' or ThisStatus = 'T'
	         	WIPQty += ThisSchedQty
	         case ThisStatus = 'R'
	         	ReadyToShipQty += ThisSchedQty
	         case otherwise$
	            * DO NOTHING THE STATUS MUST BE NULL BAD RDS
	      end case
	   end
   next i
   * IF REPORT LINE IS ALL ZERO THEN DO NOT PRINT A LINE
   * IF THE SHIPMENT WENT OUT AND THE WO_LOG RECORD HAS NOT BEEN CLOSED
   * THIS WILL HAPPEN BECAUSE THE NIGHTLY PROCESS IS RUN MANUALLY EVERY
   * MORNING AND IT THEN CLOSES OUT ALL SHIPPED WORK ORDERS
   if ( sum( WeekResultArray ) = 0 ) and ( ( HoldQty + ReceivedQty + WIPQty + ReadyToShipQty ) = 0 ) else  
	   LineOut = quote(Customer):','
	   LineOut:= quote(OrigPSN):','
	   SubPartNums = xlate( 'PROD_SPEC', OrigPSN, prod_spec_sub_part_nums$, 'X' )
	   convert @vm to ',' in SubPartNums
	   LineOut:= quote(SubPartNums):','
	   Lineout:= quote(ReceivedQty):','
	   LineOut:= quote(WIPQty):','
      LineOut:= quote(ReadyToShipQty):','
      LineOut:= quote(HoldQty):','
      for i = 1 to WRCnt
         LineOut:= quote(WeekResultArray<i>):','
      next i
      LineOut:= CrLf$
      * NOW WRITE OUT THE LINE
	   osbwrite LineOut on DosTableVar AT BytePos
	   BytePos += len( LineOut )	   
	   Tstat = status()
	   if Tstat then
	      Err = 'Bad operating system filename'
	      Err<-1> = 'Access Denied'
	      Err<-1> = 'Disk or directory full'
	      Err<-1> = 'File does not exist'
	      Err<-1> = 'Undefined error'
	      Err<-1> = 'Attempt to write a read only file'
	      Err<-1> = 'Invalid beginning byte position'
	      Void = msg( '', Err<Tstat> )
	      return 0
	   end      
	end

   * AND THEN CLEAR OUT THE REST OF THE VARIABLES
   WeekResultArray = 0:@fm:0:@fm:0:@fm:0:@fm:0:@fm:0:@fm:0:@fm:0
   ReceivedQty     = 0
   WIPQty          = 0
   ReadyToShipQty  = 0
   HoldQty         = 0
   CalcYield       = 0
return

