Compile function NDW_ADJUST_LOT_QTY_EVENTS(CtrlEntId, Event, @PARAMS)
#pragma precomp SRP_PreCompiler
#window NDW_ADJUST_LOT_QTY

Declare function MemberOf, Database_Services, Error_Services, Datetime
Declare subroutine PlaceDialog, Database_Services, Lot_Services
$Insert App_Inserts
$Insert Lot_Equates

GoToEvent Event for CtrlEntId else
    // Event not implemented
end

Return EventFlow or 1

//-----------------------------------------------------------------------------
// EVENT HANDLERS
//-----------------------------------------------------------------------------

Event WINDOW.CREATE(CreateParam)
    PermissionAllowed = False$
    Begin Case
        Case MemberOf(@User4, 'ENGINEERING')
            PermissionAllowed = True$
        Case MemberOf(@User4, 'LEAD')
            PermissionAllowed = True$
        Case MemberOf(@User4, 'SUPERVISOR')
            PermissionAllowed = True$
        Case MemberOf(@User4, 'OI_ADMIN')
            PermissionsAllowed = True$
        Case Otherwise$
            PermissionAllowed = False$
    End Case
    
    If PermissionAllowed then
        PlaceDialog(-2, -2)
        Set_Property(@Window, 'VISIBLE', 1)
        LotNo = CreateParam
        If LotNo NE '' then
            GoSub GetLotCurrData
        end else
            Set_Property(@Window : '.PUB_SEARCH_LOT', 'VISIBLE', True$)
        end
    end else
        Result    = ''
        Result<1> = False$
        End_Dialog(@Window, Result)
    end
    
    
End Event

Event Window.CLOSE(CancelFlag, CloseFlags)
    Result      = ''
    Result<1>   = False$
    End_Dialog(@Window, Result)
end event

Event EDL_NEW_LOT_WFR_QTY.CHANGED(NewLotQty)
    NewLotQty = Get_Property(@Window : '.EDL_NEW_LOT_WFR_QTY', 'TEXT')
    If Num(NewLotQty) then
        If NewLotQty LE 25 AND NewLotQty GE 0 then
            Set_Property(@Window : '.PUB_SUBMIT', 'ENABLED', True$)
        end else
            msg(@Window, 'New wafer quantity value must be a number between 0 and 25.')
        end
    end else
        If NewLotQty NE '' then
            msg(@Window, 'New wafer quantity value must be a number between 0 and 25.')
        end
    end
end event

Event EDL_LOT_NO.CHANGED(LotNo)
    LotIdString = Get_Property(@Window : '.EDL_LOT_NO', 'TEXT')
    Begin Case
        Case LotIdString[1, 4] EQ '1TTW'
            Set_Property(@Window : '.EDL_LOT_NO', 'TEXT', LotIdString[3, 999])
        Case LotIdString[1, 2] EQ '1T'
            Set_Property(@Window : '.EDL_LOT_NO', 'TEXT', LotIdString[3, 999])
    End Case
end event

Event PUB_SEARCH_LOT.CLICK()
    LotNo = Get_Property(@Window : '.EDL_LOT_NO', 'TEXT')
    If LotNo NE '' then
        GoSub GetLotCurrData
    end
end event

Event PUB_SUBMIT.CLICK()
    LotId            = Get_Property(@Window : '.EDL_LOT_NO', 'TEXT')
    NewWfrQty        = Get_Property(@Window : '.EDL_NEW_LOT_WFR_QTY', 'TEXT')
    LotType          = Database_Services('ReadDataColumn', 'LOT', LotId, LOT_TYPE$, True$, 0, False$)
    CurrWfrQty       = Database_Services('ReadDataColumn', 'LOT', LotId, LOT_WAFER_QTY$, True$, 0, False$)
    AmountToDecrease = 0
    AmountToIncrease = 0
    If NewWfrQty NE CurrWfrQty then
        Begin Case
            Case NewWfrQty LT CurrWfrQty
                AmountToDecrease = CurrWfrQty - NewWfrQty
                Lot_Services('CreateLotEvent', LotId, LotType, Datetime(), 'REDUCE_WAFER_QTY', 'Adjust wafer count by ' : AmountToDecrease, '', AmountToDecrease, 0, '', @User4)
            Case NewWfrQty GT CurrWfrQty
                AmountToIncrease = NewWfrQty - CurrWfrQty
                Lot_Services('CreateLotEvent', LotId, LotType, Datetime(), 'BONUS_WAFER_QTY', 'Adjust wafer count by ' : AmountToIncrease, '', 0, AmountToIncrease, '', @User4)
        End Case
        If Error_Services('NoError') then
            msg(@Window, 'Wafer quantity adjusted successfully!')
            Result    = ''
            Result<1> = True$
            End_Dialog(@Window, Result)
        end else
            ErrorMessage = Error_Services('GetMessage')
            msg(@Window, 'Error Adjusting wafer quantity, ' : ErrorMessage)
        end
    end
    
end event

Event PUB_CANCEL.CLICK()
    Result      = ''
    Result<1>   = False$
    End_Dialog(@Window, Result)
end event

GetLotCurrData:
    If RowExists('LOT', LotNo) then
        Set_Property(@Window : '.EDL_LOT_NO', 'TEXT', LotNo)
        LotRec = Database_Services('ReadDataRow', 'LOT', LotNo, True$, 0, False$)
        If LotRec NE '' then
            LotCurrWfrQty = LotRec<LOT_WAFER_QTY$>
            Set_Property(@Window : '.EDL_CURR_WFR_QTY', 'TEXT', LotCurrWfrQty)
        end else
           msg(@Window, 'Error reading lot number ' : LotNo : '!') 
        end
    end else
        msg(@Window, 'Lot number ' : LotNo : ' not found!')
    end
return


