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, Lot_Event_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
        msg(@Window, 'Only Lead, Supervisors, or engineering may adjust lot quantities')
        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 NewLotQty NE ''  then
        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.')
                Set_Property(@Window : '.EDL_NEW_LOT_WFR_QTY', 'TEXT', '')
            end
        end else
            If NewLotQty NE '' then
                msg(@Window, 'New wafer quantity value must be a number between 0 and 25.')
            end
        end 
    end
    
end event

Event EDL_LOT_NO.CHANGED(LotNo)
    
    LotNo = Get_Property(@Window : '.EDL_LOT_NO', 'TEXT')
    If LotNo[1, 4] EQ '1T' then
        LotNo = LotNo[3, 999]
        Set_Property(@Window : '.EDL_LOT_NO', 'TEXT', LotNo)
    end
end event

Event EDL_LOT_NO.LOSTFOCUS(Flag, FocusID)
    Set_Property(@Window : '.EDL_CURR_WFR_QTY', 'TEXT', '')
    Set_Property(@Window : '.EDL_NEW_LOT_WFR_QTY', 'TEXT', '')
    LotNo = Get_Property(@Window : '.EDL_LOT_NO', 'TEXT')
    If LotNo[1, 4] EQ '1T' then
        LotNo = LotNo[3, 999]
        Set_Property(@Window : '.EDL_LOT_NO', 'TEXT', LotNo)
    end
    If RowExists('LOT', LotNo) then
        GoSub   GetLotCurrData
    end
            
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 RowExists('LOT', LotId) then
        If NewWfrQty NE ''  then
            if Num(NewWfrQty) then
                If NewWfrQty NE CurrWfrQty then
                    MsgHead	= 'Adjust lot wafer quantity'
                    MsgText	= 'Are you sure you wish to adjust lot ' : LotId : ' wafer quantity from ' : CurrWfrQty : ' to ' : NewWfrQty : '?'
                    
                    OK = Msg(@WINDOW,'','YESNO','',MsgHead:@FM:MsgText)
                    If OK then
                        Begin Case
                            Case NewWfrQty LT CurrWfrQty
                                AmountToDecrease = CurrWfrQty - NewWfrQty
                                Lot_Services('ReduceLotWaferCount', LotId, AmountToDecrease, @User4)
                                If Error_Services('NoError') then
                                    Lot_Event_Services('CreateLotEvent', LotId, Datetime(), 'COMMENT', 'Manual Lot Qty decrease performed.', '', @User4, '' , '')
                                end
                            Case NewWfrQty GT CurrWfrQty
                                AmountToIncrease = NewWfrQty - CurrWfrQty
                                Lot_Services('IncreaseLotWaferCount', LotId, AmountToIncrease, @User4)
                                If Error_Services('NoError') then
                                    Lot_Event_Services('CreateLotEvent', LotId, Datetime(), 'COMMENT', 'Manual Lot Qty increase performed.', '', @User4, '' , '')
                                end
                        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
            end else
                msg(@Window, 'New wafer count must be a number between 0-25')
            end
        end else
            msg(@Window, 'New wafer count cannot be blank and must be a number between 0-25')
        end
    end else
       msg(@Window, 'Lot # ' : LotId : ' not found!') 
    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





