<%@ Language=VBScript %>
<%
	'	Requisition Line list page
	'	Owner: Coen
	'	$Revision: 78 $
%>
<!-- #include file="../../include/General.asp" -->
<!-- #INCLUDE FILE="../../include/PageUtil.asp" -->
<!-- #INCLUDE FILE="../../include/Format.asp" -->
<!-- #INCLUDE FILE="../../include/AUO.asp" -->
<!-- #INCLUDE FILE="../../include/Utilities.asp" -->
<!-- #INCLUDE FILE="../../include/UtilitiesClient.asp" -->
<!-- #include file = "../../include/EETable.asp" -->
<!-- #include file = "../../include/DGrid.asp" -->
<!-- #include file = "../include/LabelsRequisition.asp" -->
<!-- #include file = "../include/RequisitionUtility.asp" -->
<!-- #include file="../../include/Labels.asp" -->
<!-- #include file = "../../include/RoleConstants.asp" -->
<!-- #INCLUDE FILE="../../include/Validate.asp" -->
<HTML>
<HEAD>
</HEAD> 
<%

'	Parameters:
		
'	"mode"	--> supported modes: "requisitioner", "buyer", "financial", "approver"
'	"view"	--> supported views: "saved", "approved", "rejected", "send",
'				"closed", "submitted", "review" (also known as assisted), toconsolidate


	dim gToReviewPresent
	dim gMode
	dim gView
	dim gEEUser
	dim gcnt

	on error resume next
	'Patch for showing page in Enduser style
	gEETableForEnduser = true
	call Main()
	if Err.number<>0 then
		DisplayErrorInterface(Err)
	end if
	on error goto 0

	sub Main()

		dim gWhereClause
		dim gUrl

		set gEEUser = getActiveUser()
		if getASPParameter("mode") <> "" then
			gMode = ucase(getASPParameter("mode"))
		else
			gMode = ucase(Request.Form("mode"))
		end if
		Response.Write(formhidden("mode", gMode))
		if getOptionalASPParameter("view") <> "" then
			gView = ucase(getOptionalASPParameter("view"))
		else
			gView = ucase(Request.Form("view"))
		end if
		Response.Write(formhidden("view", gView))
	%>
		<FORM method="post" action="<%=getMSCSPage.URL("E-Requisition/Enduser/RequisitionLine_List.asp","mode", gMode, "view", getOptionalASPParameter("view"))%>" id=form1 name=form1>
	<%
		'<BODY> tag is generated by setPageAttributes()
		call setPageAttributes()
		

		if Request.Form.Count>0 then
			call handleServerActions()
		end if

		gToReviewPresent = False

		call DisplayHeaderInfo()

		gWhereClause = ""
		if gMode = "REQUISITIONER" and gEEUser.RequisitionerRole then
			gWhereClause = "WHERE dbo.REQUISITIONHEADER.USERID='" & getUserName() & "' " 
		 		select case gView
					case "SAVED"	gWhereClause = gWhereClause & _
									"AND dbo.REQUISITIONLINE.LINESTATUS IN (" & ERequisition_ERequisitionState_Created & ") "
					case "APPROVED"	gWhereClause = gWhereClause & _
									"AND dbo.REQUISITIONLINE.LINESTATUS IN (" & ERequisition_ERequisitionState_Approved & ") "
					case "REJECTED"	gWhereClause = gWhereClause & _
									"AND dbo.REQUISITIONLINE.LINESTATUS IN (" & ERequisition_ERequisitionState_Rejected &") "
					case "SUBMITTED" gWhereClause = gWhereClause & _
									 "AND dbo.REQUISITIONLINE.LINESTATUS IN (" & ERequisition_ERequisitionState_BeingApproved & ") "
					case "SEND"		gWhereClause = gWhereClause & _
									"AND dbo.REQUISITIONLINE.LINESTATUS IN (" & ERequisition_ERequisitionState_Send & ") "
					case "CLOSED"	gWhereClause = gWhereClause & _
									"AND dbo.REQUISITIONLINE.LINESTATUS IN (" & ERequisition_ERequisitionState_Closed &","& ERequisition_ERequisitionState_Archived & ") "
					case "REVIEW"		gWhereClause = gWhereClause & "AND dbo.REQUISITIONLINE.LINESTATUS IN (" & ERequisition_ERequisitionState_UnderReview &") "
					case "TOCONSOLIDATE"	 gWhereClause = gWhereClause & " AND dbo.REQUISITIONLINE.LINESTATUS IN (" & ERequisition_ERequisitionState_ToConsolidate &") "
					case "RECEIVED"	 gWhereClause = gWhereClause & " AND dbo.REQUISITIONLINE.LINESTATUS IN (" & ERequisition_ERequisitionState_Received &") "
				case else
					gWhereClause = gWhereClause & _
					"AND NOT dbo.REQUISITIONLINE.LINESTATUS IN (" & ERequisition_ERequisitionState_Closed & "," & ERequisition_ERequisitionState_Archived &") "
				end select
		
		elseif gMode = "BUYER" and gEEUser.BuyerRole then
		 		select case gView
					case "SAVED"	gWhereClause = "WHERE dbo.REQUISITIONLINE.LINESTATUS IN (" & ERequisition_ERequisitionState_BeingApproved & "," & ERequisition_ERequisitionState_Approved & "," & ERequisition_ERequisitionState_Rejected &") "
					case "SEND"		gWhereClause = "WHERE dbo.REQUISITIONLINE.LINESTATUS IN (" & ERequisition_ERequisitionState_Send & ") "
					case "CLOSED"		gWhereClause = "WHERE dbo.REQUISITIONLINE.LINESTATUS IN (" & ERequisition_ERequisitionState_Closed & "," & ERequisition_ERequisitionState_Archived &") "
					case "REVIEW"		gWhereClause = "WHERE dbo.REQUISITIONLINE.LINESTATUS IN (" & ERequisition_ERequisitionState_UnderReview &") "
					case "TOCONSOLIDATE"		gWhereClause = "WHERE dbo.REQUISITIONLINE.LINESTATUS IN (" & ERequisition_ERequisitionState_ToConsolidate &") "
				case else
					gWhereClause = "WHERE NOT dbo.REQUISITIONLINE.LINESTATUS IN (" & ERequisition_ERequisitionState_Created & ") " 
				end select
					gWhereClause = gWhereClause & _
											" AND dbo.REQUISITIONLINE.USERID = '" & SQLEncode(getUserName()) & "' "
		
		elseif gMode = "FINANCIAL" and gEEUser.FinancialRole then
			gWhereClause = "WHERE dbo.REQUISITIONLINE.LINESTATUS = " & ERequisition_ERequisitionState_Send 
		
		elseif gMode = "APPROVAL" and gEEUser.ApproverRole then
			gWhereClause =  " FROM dbo.APPROVALENTRY INNER JOIN dbo.REQUISITIONLINE ON dbo.APPROVALENTRY.REQUISITIONID = dbo.REQUISITIONLINE.REQUISITIONID " & _
									  " AND dbo.APPROVALENTRY.REQUISITIONLINENUMBER = dbo.REQUISITIONLINE.REQUISITIONLINENUMBER " & _
									  " INNER JOIN dbo.REQUISITIONHEADER ON dbo.REQUISITIONLINE.REQUISITIONID = dbo.REQUISITIONHEADER.REQUISITIONID " & _
									  " WHERE dbo.APPROVALENTRY.USERID ='" & getUserName() &"'"		
			if Request.QueryString("view").Count<>0 then
				if gView="REVIEW" then
					gWhereClause = gWhereClause & " AND dbo.APPROVALENTRY.APPROVALSTATUS=" & EApproval_ApprovalStatus_ToReview & " AND dbo.REQUISITIONLINE.LINESTATUS=2 AND dbo.APPROVALENTRY.PROGRESS=1" 
				'#ST 548.sn					
				Elseif gView="" then 
					gWhereClause = gWhereClause & " AND dbo.APPROVALENTRY.PROGRESS=1"
				'#ST 548.en
				end if
			end if
		end if

		if gWhereClause <> "" then
			dim newSQL
			
			newSql = " SELECT dbo.REQUISITIONLINE.REQUISITIONID, dbo.REQUISITIONLINE.REQUISITIONERPREFIX, dbo.REQUISITIONLINE.REQUISITIONLINENUMBER," &_
							"dbo.REQUISITIONLINE.BUSINESSPARTNERID, dbo.REQUISITIONLINE.ADDRESSID, dbo.REQUISITIONLINE.PRODUCTID, dbo.REQUISITIONLINE.COMMODITYCODE," &_
							"dbo.REQUISITIONLINE.CHARGETYPECODE, dbo.REQUISITIONLINE.CONTRACTCODE, dbo.REQUISITIONLINE.NUMBER, dbo.REQUISITIONLINE.UNITCODE," &_
							"dbo.REQUISITIONLINE.ITEMDESCRIPTION, dbo.REQUISITIONLINE.LINEQUANTITY, dbo.REQUISITIONLINE.FREIGHT_COST, dbo.REQUISITIONLINE.UNITCOST," &_
							"dbo.REQUISITIONLINE.TAXPERCENTAGE,dbo.REQUISITIONLINE.PARTNUMBERLINE, dbo.REQUISITIONLINE.DISCOUNTPERCENTAGE," &_
							"dbo.REQUISITIONLINE.DISCOUNTAMOUNT, dbo.REQUISITIONLINE.BUYFROMEMAIL, dbo.REQUISITIONLINE.BUYFROMITEMCODE," &_
							"dbo.REQUISITIONLINE.SHIPTOCONTACTINFORMATION, dbo.REQUISITIONLINE.LINEPROJECT, dbo.REQUISITIONLINE.LINESTATUS," &_ 
							"dbo.REQUISITIONLINE.LINEREMARK, dbo.REQUISITIONLINE.LINESEARCHCODE, dbo.REQUISITIONLINE.PURCHASEORDERNUMBER," &_
							"dbo.REQUISITIONLINE.PURCHASEORDERLINE,dbo.REQUISITIONLINE.LINECOSTCENTER, dbo.REQUISITIONLINE.DATEREQUIRED," &_ 
							"dbo.REQUISITIONLINE.DATESEND, dbo.REQUISITIONLINE.NOTYETRECEIVED, dbo.REQUISITIONLINE.RECEIVED, dbo.REQUISITIONLINE.ID," &_
							"dbo.REQUISITIONLINE.DATEUPDATED, dbo.REQUISITIONLINE.BUDGETCODE, dbo.REQUISITIONLINE.DOCUMENTID, dbo.REQUISITIONLINE.MNTRECEIPTS," &_
							"dbo.REQUISITIONLINE.SENDPO, dbo.REQUISITIONLINE.SUPPLIERNAME, "			

			if gMode = "APPROVAL" and gEEUser.ApproverRole then
				' select on basis of approval entry
				newSQL = newSql & " dbo.APPROVALENTRY.*, dbo.REQUISITIONHEADER.CURRENCYCODE, dbo.REQUISITIONHEADER.USERID, dbo.REQUISITIONHEADER.SEARCHCODE " & _
					             gWhereClause
			else
				newSQL = newSql & " dbo.REQUISITIONLINE.USERID, dbo.REQUISITIONHEADER.REQUISITIONID, dbo.REQUISITIONHEADER.CURRENCYCODE, dbo.REQUISITIONHEADER.USERID, dbo.REQUISITIONHEADER.SEARCHCODE " & _
					 "FROM dbo.REQUISITIONLINE LEFT JOIN dbo.REQUISITIONHEADER ON dbo.REQUISITIONLINE.REQUISITIONID=dbo.REQUISITIONHEADER.REQUISITIONID " & _
					 gWhereClause
			end if

			if gMode = "APPROVAL" and gEEUser.ApproverRole then
				call DGridBegin( I18N(Lbl_Approvals), "EProcurementConnection", newSQL, "[dbo.APPROVALENTRY.REQUISITIONID],[dbo.APPROVALENTRY.REQUISITIONERPREFIX],[dbo.APPROVALENTRY.REQUISITIONLINENUMBER], [dbo.APPROVALENTRY.APPROVALID]")
				'call DGridColEval( I18N(Lbl_ApprovalState), "linkState(getApprovalState([dbo.APPROVALENTRY.APPROVALSTATUS]), [dbo.REQUISITIONLINE.REQUISITIONID],[REQUISITIONERPREFIX],[REQUISITIONLINENUMBER], [dbo.APPROVALENTRY.APPROVALID])", "[dbo.APPROVALENTRY.APPROVALSTATUS]", 5, 0 )
				call DGridColEval( I18N(Lbl_ReqLineReq), "linkIDPrefix([dbo.REQUISITIONLINE.REQUISITIONID],[REQUISITIONERPREFIX],[REQUISITIONLINENUMBER])", "[dbo.REQUISITIONLINE.REQUISITIONID]", 10, 0 )
			else
				call DGridBegin( I18N(Lbl_RequisitionLines), "EProcurementConnection", newSQL, "[dbo.REQUISITIONLINE.REQUISITIONID],[dbo.REQUISITIONLINE.REQUISITIONERPREFIX],[dbo.REQUISITIONLINE.REQUISITIONLINENUMBER]")
				call DGridColEval( I18N(Lbl_ReqLineReq), "linkIDPrefix([dbo.REQUISITIONLINE.REQUISITIONID],[REQUISITIONERPREFIX],[REQUISITIONLINENUMBER])","[dbo.REQUISITIONLINE.REQUISITIONID]" , 10, 0 ) '
			end if

			call DGridColEval( I18N(Lbl_ReqLineItemDesc), "getItem([ITEMDESCRIPTION], [LINEQUANTITY], [UNITCODE], [FREIGHT_COST], [UNITCOST], [TAXPERCENTAGE], [DISCOUNTPERCENTAGE], [DISCOUNTAMOUNT], [CURRENCYCODE])", "[ITEMDESCRIPTION]", 20, 0 )

			if gMode <> "APPROVAL" and gView<> "SEND" then
				call DGridCol( I18N(Lbl_ReqLineCommodity), "[COMMODITYCODE]", "[COMMODITYCODE]", 10, 0 )
			end if

			if gMode <>"REQUISITIONER" then
				call DGridColEval( I18N(Lbl_ReqLineRequisitioner), "linkMailLineList(getRequisitioner([REQUISITIONID],[REQUISITIONERPREFIX]),[REQUISITIONID],[REQUISITIONERPREFIX],[dbo.REQUISITIONLINE.REQUISITIONLINENUMBER])", "[dbo.REQUISITIONHEADER.USERID]", 1, 0 )
			end if

			if gMode <> "APPROVAL" then
				call DGridCol( I18N(Lbl_ReqLineSearchCode), "[dbo.REQUISITIONHEADER.SEARCHCODE]", "[dbo.REQUISITIONHEADER.SEARCHCODE]", 10, 0 )
			end if
			
			'call DGridColEval( I18N(Lbl_ReqLineDateRequired), "EEFormatDate([DATEREQUIRED])", "[DATEREQUIRED]", 10, 0 )'108686.o
			call DGridColEval( I18N(Lbl_ReqLineDateRequired), "EEFormatDate(GetValidDate(GetDatefromDateTime([DATEREQUIRED])))", "[DATEREQUIRED]", 10, 0 )'108686.n

			if gView = "SEND" then
				call DGridColEval( I18N(Lbl_ReqLineDateSend), "EEFormatDate([dbo.REQUISITIONLINE.DATESEND])", "[dbo.REQUISITIONLINE.DATESEND]", 10, 0 )
			end if
		
			if gView = "SEND" or gView = "REJECTED" or gView = "APPROVED" or gView = "SUBMITTED" or gView="RECEIVED" Then
				call DGridColEval (I18N(Lbl_TrackInfo), "getActionLink([dbo.REQUISITIONLINE.REQUISITIONID],[dbo.REQUISITIONLINE.REQUISITIONERPREFIX], [dbo.REQUISITIONLINE.REQUISITIONLINENUMBER], [dbo.REQUISITIONLINE.LINESTATUS] , [dbo.REQUISITIONLINE.PURCHASEORDERNUMBER], [dbo.REQUISITIONLINE.PURCHASEORDERLINE], [dbo.REQUISITIONLINE.DOCUMENTID], [dbo.REQUISITIONLINE.RECEIVED])", "", 20, 0)
			end if

			if gMode = "APPROVAL" and gEEUser.ApproverRole then
				call DGridColEval (I18N(Lbl_Approve), "getApproveBox([dbo.REQUISITIONLINE.REQUISITIONID],[dbo.REQUISITIONLINE.REQUISITIONERPREFIX], [dbo.REQUISITIONLINE.REQUISITIONLINENUMBER], [dbo.APPROVALENTRY.APPROVALID], [dbo.APPROVALENTRY.APPROVALSTATUS])", "", 10, 2)
				call DGridColEval (I18N("RE_Reject"), "getRejectBox([dbo.REQUISITIONLINE.REQUISITIONID],[dbo.REQUISITIONLINE.REQUISITIONERPREFIX], [dbo.REQUISITIONLINE.REQUISITIONLINENUMBER], [dbo.APPROVALENTRY.APPROVALID], [dbo.APPROVALENTRY.APPROVALSTATUS])", "", 10, 2)
				if gView="REVIEW" then
					call DGridColEval (I18N(Lbl_ToReview), "getReviewBox([dbo.REQUISITIONLINE.REQUISITIONID],[dbo.REQUISITIONLINE.REQUISITIONERPREFIX], [dbo.REQUISITIONLINE.REQUISITIONLINENUMBER], [dbo.APPROVALENTRY.APPROVALID], [dbo.APPROVALENTRY.APPROVALSTATUS])", "", 10, 2)
				end if
				call DGridColEval (I18N(Lbl_Description), "getReasonArea([dbo.REQUISITIONLINE.REQUISITIONID],[dbo.REQUISITIONLINE.REQUISITIONERPREFIX], [dbo.REQUISITIONLINE.REQUISITIONLINENUMBER], [dbo.APPROVALENTRY.APPROVALID],[DESCRIPTION],  [dbo.APPROVALENTRY.APPROVALSTATUS])", "", 20, 0)
				 call DGridColEval( I18N(Lbl_Reason), "ShowReason(getApprovalState([dbo.APPROVALENTRY.APPROVALSTATUS]), [dbo.REQUISITIONLINE.REQUISITIONID],[REQUISITIONERPREFIX],[REQUISITIONLINENUMBER], [dbo.APPROVALENTRY.APPROVALID])", "[dbo.APPROVALENTRY.APPROVALSTATUS]", 5, 0 )
			elseif (gView = "SAVED" or gView = "APPROVED" or gView = "REJECTED") and gMode ="REQUISITIONER" Then
				call DGridColEval ("", "getActionButton([dbo.REQUISITIONLINE.REQUISITIONID],[dbo.REQUISITIONLINE.REQUISITIONERPREFIX], [dbo.REQUISITIONLINE.REQUISITIONLINENUMBER], [dbo.REQUISITIONLINE.LINESTATUS])", "", 10, 0)
				dim lUser
                set lUser = GetEEUser()
                if   lUser.HasRole(mRoleCanSimulateApprovalEntries) then
                   ' The Simulate button is added for the SAVED and REJECTED views.
					if (gView = "SAVED" or gView = "REJECTED" ) Then
			       			call DGridColEval ("", "getSimulateButton([dbo.REQUISITIONLINE.REQUISITIONID],[dbo.REQUISITIONLINE.REQUISITIONERPREFIX], [dbo.REQUISITIONLINE.REQUISITIONLINENUMBER])", "", 10, 0)
					end if
				end if
			end if
			
			'if gMode = "REQUISITIONER" and not (gView = "SEND" or gView = "RECEIVED" )then		#ST 519.o
			if gMode = "REQUISITIONER" and not (gView = "SEND" or gView = "RECEIVED" or gView = "TOCONSOLIDATE" )then		'#ST 519.n
				call DGridColEval ("", "getRemoveButton([dbo.REQUISITIONLINE.REQUISITIONID],[dbo.REQUISITIONLINE.REQUISITIONERPREFIX], [dbo.REQUISITIONLINE.REQUISITIONLINENUMBER])", "", 10, 0)
			end if

			call DGridEnd()
			
			if gMode = "APPROVAL" and gEEUser.ApproverRole and gToReviewPresent then
				Response.Write("<BR>" & I18N(Lbl_PressToSubmitApproval))
				Response.Write("&nbsp;&nbsp;")
				Response.Write(formButton("btnallsubmit", I18NGif("BtnSubmit.gif"),  ""))
			end if

			Response.write "<P>"
		else
			call GenerateWarning( I18N(Lbl_ReqLineList), _
								  I18N(Lbl_ReqLineViewList) )
		end if
	%>
	</P>
	</FORM>
	<%	' EEMAPI functions do not run within a form
		if Request.QueryString("mailto") <> "" then
			dim lRecipients(0)
			lRecipients(0) = Request.QueryString("mailto")
			if ucase(getBrowser().browser) = "IE" then ' Internet Explorer section
				call buildMessageEEMAPI(buildSubject(Request.QueryString("RequisitionID"), Request.QueryString("RequisitionerPrefix")),_
						buildMailText(getBrowser().browser, Request.QueryString("RequisitionID"), Request.QueryString("RequisitionerPrefix"), Request.QueryString("RequisitionLineNumber")),_
						lRecipients)
			end if
		end if
	end sub 'Main

	Private Sub DisplayHeaderInfo()
		Dim lTitle, lTxt
		Select Case gMode
		Case "REQUISITIONER"
			Select Case UCASE(gView)
				case "SAVED"
					lTitle = Lbl_SavedRequisitions
					lTxt = Lbl_SavedExplain					
				case "APPROVED"
					lTitle = Lbl_ApprovedRequisitions
					lTxt = Lbl_ApprovedExplain					
				case "REJECTED"	
					lTitle = Lbl_RejectedRequisitions
					lTxt = Lbl_RejectedExplain					
				case "SUBMITTED" 
					lTitle = Lbl_SubmittedRequisitions
					lTxt = Lbl_SubmittedExplain					
				case "SEND"	
					lTitle = Lbl_SendRequisitions
					lTxt = Lbl_SendExplain					
				case "CLOSED"
					lTitle = Lbl_ClosedRequisitions
					lTxt = Lbl_ClosedExplain					
				case "REVIEW"	
					lTitle = Lbl_ReviewRequisitions
					lTxt = Lbl_ReviewExplain
				case "RECEIVED"
					lTitle = Lbl_Received
					lTxt = Lbl_ClosedExplain
				Case "TOCONSOLIDATE"
					lTitle =Lbl_ToConsolidate
					call putExplainHeader( I18N(lTitle),"")
					exit sub
			End Select
			call putExplainHeader( I18N(lTitle), I18N(lTxt))
		Case "APPROVAL"
			if gEEUser.ApproverRole then
				call putExplainHeader( I18N(Lbl_Approvals), I18N(Lbl_ExplainApprovals))
			end if
		Case "BUYER"
			lTxt = Lbl_BuyerExplain
			Select Case gView
				case "SAVED"
					lTitle = Lbl_SavedRequisitions
				case "SEND"	
					lTitle = Lbl_SendRequisitions
				case "CLOSED"
					lTitle = Lbl_ClosedRequisitions
				case "RECEIVED"
					lTitle = Lbl_Received
				case "REVIEW"
					lTitle = Lbl_ReviewRequisitions
					lTxt = Lbl_BuyerReviewExplain
				Case "TOCONSOLIDATE"
					lTitle =Lbl_ToConsolidate
			End Select
			if gEEUser.BuyerRole then
				call putExplainHeader( I18N(lTitle), I18N(lTxt))
			end if
		Case "FINANCIAL"
			call makesimpleheader( I18N(Lbl_ReqLineHeader) )
		End Select
		Response.write "<P>"
	end sub

	'---------------------------------------------------
	' Returns item with description, quantity, cost
	'----------------------------------------------------
	Function getItem(iDesc, iQty, iUnit, iFreightCost, iUnitCost, iTax, iDiscountPercentage, iDiscountAmount, iCurrency)
		getItem= iDesc & "<BR>" & setQuantity(iQty, iUnit) & "<BR>" & _
							getCost(iQty, iFreightCost, iUnitCost, iTax, iDiscountPercentage, iDiscountAmount, iCurrency)
	end Function

	'-----------------------------------------------
	' Returns quantity with unit
	'-----------------------------------------------
	function setQuantity(iQuan, iUnitCode)
		if not isnull(iUnitCode) and not isempty(iUnitCode) and iQuan <> "" then
			setQuantity = iQuan & " (" & trim(iUnitCode) & ")"
		else
			setQuantity = iQuan
		end if
	end function

	'------------------------------------------------------------------------------------------------------------------------------------------------------------------
	' Function handles the actions of the user
	'------------------------------------------------------------------------------------------------------------------------------------------------------------------
	private sub handleServerActions()
		Dim i
		Dim ReqComp
		Dim lReqID
		Dim lReqPrefix
		Dim lReqLine
		Dim lApprovalID
		Dim lLineRS		'#ST 549.n

		Set ReqComp = Server.CreateObject("ERequisition.Requisition")

		' loop through all form elements and find the button actions
		for i = 1 to Request.Form.Count
			Dim Name: Name = Request.Form.Key(i)
			Dim Value: Value = Request.Form.Item(i)
			Dim lReason
			
			If left(Name, 9) =  "btnremove" then
				' Delete receipt line
				call getRequisitionInformation("btnremove", Name, lReqID, lReqPrefix, lReqLine, lApprovalID)
				Response.Redirect( getMSCSPage.URL(_
								"E-Requisition/enduser/RequisitionLine_delete.asp",_
								"requisitionid", cstr(lReqId),_
								"requisitionerprefix", lReqPrefix,_
								"requisitionlinefrom", lReqLine, _
								"requisitionlineto", lReqLine, _
								"frompage", "E-Requisition/enduser/RequisitionLine_List.asp", _
								"mode", gMode, _
								"view", gView) )
				Exit Sub
			end if
			If left(Name, 9) =  "btnsubmit" then
				' Approve receipt line
				call getRequisitionInformation("btnsubmit", Name, lReqID, lReqPrefix, lReqLine, lApprovalID)
				if CheckForDateRequired(lReqID,lReqLine,lReqPrefix) then 
					call ReqComp.ApproveLine(cStr(getUserName()), CLng(lReqID), cStr(lReqPrefix), CLng(lReqLine))
					Response.Redirect( getMSCSPage.URL(_
									"E-Requisition/enduser/sendconfirm.asp",_
									"requisitionid", cstr(lReqId),_
									"requisitionerprefix", lReqPrefix,_
									"requisitionlinenumber", lReqLine) )
					Exit Sub
				Else
					'Date Required is less than current Date 
					Call GenerateWarning(I18N(Lbl_CanNotInsertUpdate),I18N(Lbl_InCorrectRequiredDate))
					Exit Sub
				End if 
			end if
			' For Approval Simulation 
			if left(Name,11) = "btnsimulate" then
			    'Simulate Requisition line
			  
			  call getRequisitionInformation("btnsimulate", Name, lReqID, lReqPrefix, lReqLine, lApprovalID)
			  'lReqState=ReqComp.getLineState(lReqID, lReqPrefix, lReqLine) '#ST549.n
			  
			  lLineRS=getRecordset("SELECT * FROM REQUISITIONLINE WHERE REQUISITIONID=" & lReqID & " AND REQUISITIONLINENUMBER=" & lReqLine) '#ST549.n
			  if lLineRS("LINESTATUS")=ERequisition_ERequisitionState_Created or  lLineRS("LINESTATUS")=ERequisition_ERequisitionState_Rejected then '#ST549.n
			  
				Response.Redirect (getMSCSPage.URL("E-Requisition/enduser/SimulationResult.asp",_
			                     "requisitionername",cStr(getUserName()),_
			                     "requisitionid", cstr(lReqId),_
								"requisitionerprefix", lReqPrefix,_
								"requisitionlinenumber", lReqLine ))
			  '#ST549.sn
			  else
			   call GenerateWarning(I18N(Lbl_SimulationResult),I18N(Lbl_CannnotSimulate))
			 		  
			  end if
			  'ST549.en  					
			  Exit Sub  
			end if  
			If left(Name, 7) =  "btnsend" then
				' Delete receipt line
				call getRequisitionInformation("btnsend", Name, lReqID, lReqPrefix, lReqLine, lApprovalID)
				call ReqComp.SendLine(cStr(getUserName()), CLng(lReqID), cStr(lReqPrefix), CLng(lReqLine))
				Response.Redirect( getMSCSPage.URL(_
								"E-Requisition/enduser/sendconfirm.asp",_
								"requisitionid", cstr(lReqId),_
								"requisitionerprefix", lReqPrefix,_
								"requisitionlinenumber", lReqLine) )
				Exit Sub
			end if
			if left(Name, 5) = "radio" then
				Dim lVal: lVal = Request.Form.Item(Name)
				If left(lVal, 10) =  "approvebox" then
					call getReqInfoCheckBox("approvebox", lVal, lReqID, lReqPrefix, lReqLine, lApprovalID)
					call ReqComp.UpdateApprovalEntry(CLng(lReqID), cStr(lReqPrefix), CLng(lReqLine), CLng(lApprovalID), cStr(getProcUser()), EApproval_ApprovalStatus_Approved, getReasonAreaValue(lReqID, lReqPrefix, lReqLine, lApprovalID))
				elseIf left(lVal, 9) = "rejectbox" then
					call getReqInfoCheckBox("rejectbox",lVal , lReqID, lReqPrefix, lReqLine, lApprovalID)
					call ReqComp.UpdateApprovalEntry(CLng(lReqID), cStr(lReqPrefix), CLng(lReqLine), CLng(lApprovalID), cStr(getUserName()), EApproval_ApprovalStatus_Rejected, getReasonAreaValue(lReqID, lReqPrefix, lReqLine, lApprovalID))
				end if
			end if
		next
		set ReqComp = Nothing
	end sub

	'------------------------------------------------------------------------------------------------------------------------------------------------------------------
	' This constant is used to code the button name without getting into trouble with the Requisition Prefix
	'------------------------------------------------------------------------------------------------------------------------------------------------------------------
	const BTNSPLIT = "mtaal"

	'------------------------------------------------------------------------------------------------------------------------------------------------------------------
	' Returns an approval/Rejection checkbox
	'------------------------------------------------------------------------------------------------------------------------------------------------------------------
	function getApproveBox(iReqID, iReqPrefix, iReqLine, iApprovalID, iStatus)
		Dim lId: lId = getName("approvebox", cStr(iReqID), iReqPrefix, cStr(iReqLine), cStr(iApprovalID)) 
		Dim lName: lName = getName("radio", cStr(iReqID), iReqPrefix, cStr(iReqLine), cStr(iApprovalID)) 
		if iStatus = EApproval_ApprovalStatus_ToReview then
			getApproveBox = Createradiobox(lName, lId, True, 0)
			gToReviewPresent = True
		else
			getApproveBox = Createradiobox(lName, lId, iStatus = EApproval_ApprovalStatus_Approved, 1)
		end if
	end function
	
	function getrejectBox(iReqID, iReqPrefix, iReqLine, iApprovalID, iStatus)
		Dim lId: lId = getName("rejectbox", cStr(iReqID), iReqPrefix, cStr(iReqLine), cStr(iApprovalID)) 
		Dim lName: lName = getName("radio", cStr(iReqID), iReqPrefix, cStr(iReqLine), cStr(iApprovalID)) 
		if iStatus = EApproval_ApprovalStatus_ToReview then
			getRejectBox = Createradiobox(lName, lID, False, 0)
			gToReviewPresent = True
		else
			getRejectBox = Createradiobox(lName, lID, iStatus = EApproval_ApprovalStatus_Rejected, 1)
		end if
	end function

	function getreviewBox(iReqID, iReqPrefix, iReqLine, iApprovalID, iStatus)
		Dim lId: lId = getName("reviewbox", cStr(iReqID), iReqPrefix, cStr(iReqLine), cStr(iApprovalID)) 
		Dim lName: lName = getName("radio", cStr(iReqID), iReqPrefix, cStr(iReqLine), cStr(iApprovalID)) 

		if iStatus = EApproval_ApprovalStatus_ToReview then
			getReviewBox = Createradiobox(lName, lId, False, 0)
			gToReviewPresent = True
		else
			getReviewBox = Createradiobox(lName, lId, False, 1)
		end if
	end function

	'------------------------------------------------------------------------------------------------------------------------------------------------------------------
	' Returns an approval/Rejection checkbox
	'------------------------------------------------------------------------------------------------------------------------------------------------------------------
	function getReasonArea(iReqID, iReqPrefix, iReqLine, iApprovalID, iValue, iStatus)
		Dim lName: lName = getName("reasonarea", cStr(iReqID), iReqPrefix, cStr(iReqLine), cStr(iApprovalID))
		if ivalue="" or ivalue=NULL then

			if iStatus = EApproval_ApprovalStatus_ToReview then
				getReasonArea = formTextArea(lName, trim(iValue), 3, 30, False)
			else
				getReasonArea = trim(iValue)
			end if
		else
		
			if iStatus = EApproval_ApprovalStatus_ToReview then
				getReasonArea = formTextArea(lName, trim(iValue), 3, 30, False)
			else
				getReasonArea = trim(iValue)
			end if		
		
		end if		
	end function

	function getReasonAreaValue(iReqID, iReqPrefix, iReqLine, iApprovalID)
		Dim lName: lName = getName("reasonarea", cStr(iReqID), iReqPrefix, cStr(iReqLine), cStr(iApprovalID))
	
		getReasonAreaValue = getFormValue(lName)
	end function
	
	Function getName(iPre, iReqID, iReqPrefix, iReqLine, iApprovalID)
		getName = cStr(iPre) & BTNSPLIT & cStr(iReqID) & BTNSPLIT & iReqPrefix & BTNSPLIT & cStr(iReqLine) & BTNSPLIT & cStr(iApprovalID)
	end function
	'------------------------------------------------------------------------------------------------------------------------------------------------------------------
	' Returns a remove button
	'------------------------------------------------------------------------------------------------------------------------------------------------------------------
	function getRemoveButton(iReqID, iReqPrefix, iReqLine)
		getRemoveButton = formbutton("btnremove" & BTNSPLIT & cStr(iReqID) & BTNSPLIT & iReqPrefix & BTNSPLIT & cStr(iReqLine),I18NGif("BtnRemove.gif"), "")
	end function

	'------------------------------------------------------------------------------------------------------------------------------------------------------------------
	' get buyer, need this strange function because the use of join's does not work
	'------------------------------------------------------------------------------------------------------------------------------------------------------------------
	function getRequisitioner(iReqID, iReqPrefix)
		Dim lRS
		set lRS = getrecordset("SELECT USERID FROM REQUISITIONHEADER WHERE REQUISITIONID=" & iReqID)
		if not lRS.Eof then
			getRequisitioner = lRS("USERID").Value
		else
			getRequisitioner = ""
		end if
	end function
	
	'------------------------------------------------------------------------------------------------------------------------------------------------------------------
	' Returns Simulate Button
	'------------------------------------------------------------------------------------------------------------------------------------------------------------------
	function getSimulateButton(iReqID, iReqPrefix, iReqLine)
		Dim lButtonName
		Dim lButtonGif
        lButtonGif = "btnSimulate.gif"
		lButtonName = "btnsimulate"
		getSimulatebutton = formbutton(lButtonName & BTNSPLIT & cStr(iReqID) & BTNSPLIT & iReqPrefix & BTNSPLIT & cStr(iReqLine), I18NGif(lButtonGif), "") 
    end function


	'------------------------------------------------------------------------------------------------------------------------------------------------------------------
	' Depending on the state of the requisition returns a send or submit button
	'------------------------------------------------------------------------------------------------------------------------------------------------------------------
	function getActionButton(iReqID, iReqPrefix, iReqLine, iStatus)
		Dim lButtonName
		Dim lButtonGif

		Select Case iStatus
		Case ERequisition_ERequisitionState_Created 
			lButtonName = "btnsubmit"
			lButtonGif = "btnsubmit.gif"
		Case ERequisition_ERequisitionState_BeingApproved  
			lButtonName = ""
		Case ERequisition_ERequisitionState_UnderReview
			lButtonName = ""
		Case ERequisition_ERequisitionState_Approved 
			lButtonName = "btnsend"
			lButtonGif = "btnsend.gif"
		Case ERequisition_ERequisitionState_Rejected
			lButtonName = "btnsubmit"
			lButtonGif = "btnsubmit.gif"
		Case ERequisition_ERequisitionState_Send 
			lButtonName = ""
			lButtonGif = ""
		Case ERequisition_ERequisitionState_Closed 
			lButtonName = ""
		Case ERequisition_ERequisitionState_Archived 
			lButtonName = ""
		Case ERequisition_ERequisitionState_Undefined
			lButtonName = ""
		end select
		if trim(lButtonName)="" then
			getActionButton =""
			Exit Function
		end if
		getActionButton = formbutton(lButtonName & BTNSPLIT & cStr(iReqID) & BTNSPLIT & iReqPrefix & BTNSPLIT & cStr(iReqLine), I18NGif(lButtonGif), "")
	end function

	'------------------------------------------------------------------------------------------------------------------------------------------------------------------
	' Depending on the state of the requisition an action link is created to support view of tracking information and send information
	'------------------------------------------------------------------------------------------------------------------------------------------------------------------
	function getActionLink(iReqID, iReqPrefix, iReqLine, iStatus, iPoNum, iPoLine, iDocument, iReceived)
		
		Select Case iStatus
		Case ERequisition_ERequisitionState_Created 
			getActionLink = ""
		Case ERequisition_ERequisitionState_BeingApproved  
			getActionLink = countApprovalState(iReqId, iReqPrefix, iReqLine) & getLinkState(iReqID, iReqPrefix, iReqLine, I18N(Lbl_ViewDetails), "approval")
		Case ERequisition_ERequisitionState_UnderReview
			getActionLink = ""
		Case ERequisition_ERequisitionState_Approved 
			getActionLink = countApprovalState(iReqId, iReqPrefix, iReqLine) & getLinkState(iReqID, iReqPrefix, iReqLine, I18N(Lbl_ViewDetails), "approval")
		Case ERequisition_ERequisitionState_Rejected
			getActionLink = countApprovalState(iReqId, iReqPrefix, iReqLine) & getLinkState(iReqID, iReqPrefix, iReqLine, I18N(Lbl_ViewDetails), "approval")
		Case ERequisition_ERequisitionState_Send
			if not isNull(iPoNum) then
				getActionLink = I18N(Lbl_PurchaseOrder) & ": " & cStr(iPoNum) & " - " & cStr(iPoLine)
			elseif not isnull(iDocument) then
				getActionLink = I18N(Lbl_EColDocument) & ": " & cStr(iDocument)
			end if
			getActionLink = getActionLink &  "<BR>" & I18N(Lbl_ReceivedQty) & ": " & cStr(iReceived) & "<BR>"
			getActionLink = getActionLink & getLinkState(iReqID, iReqPrefix, iReqLine, I18N(Lbl_ViewDetails), "send")
		Case ERequisition_ERequisitionState_Closed 
			getActionLink = ""
		Case ERequisition_ERequisitionState_Archived 
			getActionLink = ""
		Case ERequisition_ERequisitionState_Undefined
			getActionLink = ""
		Case ERequisition_ERequisitionState_Received
			if not isNull(iPoNum) then
				getActionLink = I18N(Lbl_PurchaseOrder) & ": " & cStr(iPoNum) & " - " & cStr(iPoLine)
			elseif not isnull(iDocument) then
				getActionLink = I18N(Lbl_EColDocument) & ": " & cStr(iDocument)
			end if
			getActionLink = getActionLink &  "<BR>" & I18N(Lbl_ReceivedQty) & ": " & cStr(iReceived) & "<BR>"
			getActionLink = getActionLink & getLinkState(iReqID, iReqPrefix, iReqLine, I18N(Lbl_ViewDetails), "received")			
		Case else
			getActionLink = ""
		end select
	end function

	function getLinkState(iReqId, iReqPrefix, iReqLine, iTxt, iMode)
			getLinkState = "<A HREF=""" & getMSCSPage().URL(_
			  					"E-Requisition/Enduser/ViewStateDetails.asp",_
			  					"mode", iMode, "requisitionid", iReqID, _
			  					"requisitionerprefix", iReqPrefix, _
			  					"requisitionlinenumber", iReqLine) & _
				  				""">" & iTxt & "</A>"
	end function

	'------------------------------------------------------------------------------------------------------------------------------------------------------------------
	' Decodes a button name and returns the requisition key information
	'------------------------------------------------------------------------------------------------------------------------------------------------------------------
	function getRequisitionInformation(iString, iButtonName, oReqID, oReqPrefix, oReqLine, oApprovalID)
		Dim lStrArray
		
		lStrArray = split(Replace(iButtonName, iString, ""), BTNSPLIT)
		' index 0 is space and does not mean anything
		oReqID = CLng(lStrArray(1))
		oReqPrefix = cStr(lStrArray(2))
		if uBound(lStrArray)=3 then
			oReqLine = CLng(left(lStrArray(3), len(lStrArray(3))-2))
			oApprovalID = 0
		else
			' In case of an approval button return a correct id
			oReqLine = CLng(lStrArray(3))
			oApprovalID = CLng(left(lStrArray(4), len(lStrArray(4))-2))	
		end if
	end function

	function getReqInfoCheckBox(iString, iButtonName, oReqID, oReqPrefix, oReqLine, oApprovalID)
		Dim lStrArray
		
		lStrArray = split(Replace(iButtonName, iString, ""), BTNSPLIT)

		' index 0 is space and does not mean anything
		oReqID = CLng(lStrArray(1))
		oReqPrefix = cStr(lStrArray(2))
		if uBound(lStrArray)=3 then
			oReqLine = CLng(lStrArray(3))
			oApprovalID = 0
		else
			' In case of an approval button return a correct id
			oReqLine = CLng(lStrArray(3))
			oApprovalID = CLng(lStrArray(4))	
		end if
	end function


	'------------------------------------------------------------------------------------------------------------------------------------------------------------------
	' Creates a hyperlink to requisitionline_detail.asp
	'------------------------------------------------------------------------------------------------------------------------------------------------------------------
	function linkIDPrefix( iRequisitionID, iRequisitionerPrefix, iLineNumber )
		linkIDPrefix = "<A HREF=""" & _
		  				getMSCSPage().URL("E-Requisition/Enduser/RequisitionLine_Detail.asp",	"mode",_
		  					"edit",_
		  					 "role", _
		  					gMode,_
		  					"requisitionid",_
							iRequisitionID,_
		  					"requisitionerprefix",_
		  					iRequisitionerPrefix,_
		  					"requisitionlinenumber",_
	  						iLineNumber) & _
		  				""">" & iRequisitionID & " - " & iRequisitionerPrefix & " - " & iLineNumber & "</A>"
	end function

	function IDPrefix( iRequisitionID, iRequisitionerPrefix, iLineNumber)
		IDPrefix =  iRequisitionID & " - " & iRequisitionerPrefix & " - " & iLineNumber
	end function
	
	
	function ShowReason( iState, iRequisitionID, iRequisitionerPrefix, iLineNumber, iApprovalID )
		ShowReason = "<A HREF=""" & _
		  				getMSCSPage().URL("E-Requisition/Enduser/ApprovalReasons.asp",_
		  					"requisitionid",_
							iRequisitionID,_
		  					"requisitionerprefix",_
		  					iRequisitionerPrefix,_
		  					"requisitionlinenumber",_
	  						iLineNumber,_
	  						"approvalid", iApprovalID, "view", gView) & _
		  				""">" & iState & "</A>"
	end function

	

	'------------------------------------------------------------------------------------------------------------------------------------------------------------------
	' Creates a hyperlink to approval_edit.asp
	'------------------------------------------------------------------------------------------------------------------------------------------------------------------
	function linkState( iState, iRequisitionID, iRequisitionerPrefix, iLineNumber, iApprovalID )
		linkState = "<A HREF=""" & _
		  				getMSCSPage().URL("E-Requisition/Enduser/Approval_Edit.asp",_
		  					"requisitionid",_
							iRequisitionID,_
		  					"requisitionerprefix",_
		  					iRequisitionerPrefix,_
		  					"requisitionlinenumber",_
	  						iLineNumber,_
	  						"approvalid", iApprovalID) & _
		  				""">" & iState & "</A>"
	end function

	'------------------------------------------------------------------------------------------------------------------------------------------------------------------
	' Creates the special mail hyperlink
	'---------------------------------------------------------------------------------------------------------------------------
	function linkMailLineList(iUserID, iRequisitionID, iRequisitionerPrefix, iRequisitionLineNumber)
		dim lUrl
		dim lUserEmail
		
		if IsNull(iUserID) or trim(iUserID)="" then
			linkMailLineList = ""
			Exit Function
		end if
		
		lUserEmail = getComponent("USER").GetEmailAddress(cstr(iUserID))
		
'		lUrl = getMSCSPage().URL(_												'#IT DF 220.so
'	  				"E-Requisition/Enduser/RequisitionLine_List.asp",_
'	  				"mode",_
'	  				gMode,_
'	  				"mailto",_
'					lUserEmail,_
'	  				"requisitionid",_
'	  				iRequisitionID,_
'	  				"requisitionerprefix",_
'	  				iRequisitionerPrefix,_
'					"requisitionlinenumber",_
'	  				iRequisitionLineNumber)											'#IT DF 220.eo
		
		'IT DF 220.sn  view parameter also added to the Link Else its taking default View as its value.
		lUrl = getMSCSPage().URL(_
	  				"E-Requisition/Enduser/RequisitionLine_List.asp",_
	  				"mode",_
	  				gMode,_
	  				"view",_	
	  				gView,_	
	  				"mailto",_
					lUserEmail,_
	  				"requisitionid",_
	  				iRequisitionID,_
	  				"requisitionerprefix",_
	  				iRequisitionerPrefix,_
					"requisitionlinenumber",_
	  				iRequisitionLineNumber)
	  	'#IT DF 22.en		

		linkMailLineList = getProcActualName(iUserID) & "&nbsp;" & getUserCommunicationLink(iUserID, iRequisitionID, iRequisitionerPrefix, iRequisitionLineNumber, lUrl)
	end function
	
	function DisplayErrorInterface(iErr)	
		dim lErrInfo( 4 )
		lErrInfo( 0 ) = iErr.number
		lErrInfo( 1 ) = iErr.source
		lErrInfo( 2 ) = iErr.description
		lErrInfo( 3 ) = iErr.helpfile
		lErrInfo( 4 ) = iErr.helpcontext
		DisplayErrorInfo(lErrInfo)
	end function 
	
	Function CheckForDateRequired(iReqID,iReqLine,iReqPrefix) 
		Dim lRS 
		
		Set lRS = getRecordset("SELECT DATEREQUIRED FROM REQUISITIONLINE WHERE REQUISITIONID=" & iReqID & " AND REQUISITIONLINENUMBER=" & iReqLine)
		if lRS("DATEREQUIRED") < Date() then 
			CheckForDateRequired = False
		Else
			CheckForDateRequired = True
		End if 
		
	End Function
	'108686.sn
	Function GetDatefromDateTime(iDateTime)
		Dim lDate
				
		iDateTime = Trim(CStr(iDateTime))
		lDate = Split(iDateTime," ")
		GetDatefromDateTime = lDate(0)
	End Function
	'108686.en

%>
</BODY>
</HTML>