<%
' MyPAYE
' Module: UserPayrollAccess.asp
'
' This software is the copyright of Mr T J Sheppard and may not be copied, duplicated or 
' modified other than as permitted in the licence agreement.
'
'       © 2012 - 2021  Mr T J Sheppard
'                      Lyndale House
'                      Church Street
'                      Bradwell
'                      Hope Valley
'                      S33 9HJ
'
'  Tel 0845 643 5191
'  Email Support@MyPAYE.co.uk
'
' MyPAYE is a Registered Trademark of MyPAYE Ltd
'-------------------------------------------------------------------
'
' Updated 11 November 2021

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
'   Description -    This module displays the Payroll that the currently logged in user
'                    has access to and which of those the displayed user is permitted to access
'
' Amendment Log
'------------------------------------------------------------------------------------------
' Date     | Name            | Vers.  | Description
'------------------------------------------------------------------------------------------
' 11/03/12 | TJS             | 2.0.02 | Page added
' 08/08/12 | TJS             | 2.0.05 | Google Analytics added
' 01/06/14 | TJS             | 4.0.00 | Modified for new styling and operation without framesets
' 23/09/14 | TJS             | 4.0.03 | Modified to use fieldset rather than nested tables for iPad compatibility
' 05/10/14 | TJS             | 4.0.04 | Modified for Encrypted Employer ID check
' 24/03/15 | TJS             | 4.0.12 | Corrected sql to resolve ambiguous field name
' 26/11/15 | TJS             | 5.0.00 | Modified to cater for separate User_Permissions table, to add CIS Paylist access 
'                                     | control and hide payrolls when polling of PAYE notices inhibited
' 10/10/17 | TJS             | 5.1.10 | Corrected conflict where PSID param was used for both PaySlipID and PenSchID on new employee wizard
' 17/04/18 | TJS             | 5.2.03 | Modified to cater for MenuPayrollExists on MainMenu.asp
' 20/05/18 | TJS             | 5.2.07 | Modified to cater for Deleted field on User_Payroll_Access table
' 20/10/19 | TJS             | 5.7.00 | Modified to use GetMainMenuParam to save having to read lots of parameters for each page build
' 25/02/20 | TJS             | 5.7.04 | Modified to cater for Partner only reports on Multi Employer Overview
' 30/05/20 | TJS             | 5.8.00 | Modified for new styling - added centered class on body and modified to replace stylesheet.css, mainmenu.css and buttons-css.asp with BodyAndMenu-css.asp
' 25/09/20 | TJS             | 5.9.00 | Modified to prevent double clicking on change employer listbox
' 09/04/21 | TJS             | 5.9.09 | Modified to use ASPDOTNET/LogOut.aspx and ASPDOTNET/AgencyCancel.aspx pages
' 11/11/21 | TJS             | 5.9.13 | Modified to use ASPDotNet/UpdatePayrollAccess.ashx page
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 
PageID = 23805
NoBack = True
SessionID = request.querystring("ID")
SessionVarsID = RemoveHTML(request.querystring("SV"))
SourceFrame = RemoveHTML(request.querystring("S"))
DestFrame = RemoveHTML(request.querystring("D"))
UserID = CheckURLDBIDField(RemoveHTML(request.querystring("UID")), "UserID")
EmployeeID = CheckURLDBIDField(RemoveHTML(request.querystring("EE")), "EmployeeID")
EmployerID = CheckURLDBIDField(RemoveHTML(request.querystring("ER")), "EmployerID")
%>

<!--#include file ="Functions\CoreValidationRoutines.asp"-->

<!--#include file ="Functions\ConnectDatabaseMain.asp"-->

<!--#include file ="Functions\CreateSessionID.asp"-->

<!--#include file ="Functions\FrameNameConst.asp"-->

<%GetSessionVarsIDRecord(SessionVarsID)%>

<!--#include file ="Functions\DataProtection.asp"-->

<!--#include file ="Functions\URLValidation.asp"-->

<%
AddNewEdit = RemoveHTML(request.querystring("ANE"))
CancelPage = RemoveHTML(request.querystring("CPge"))

' start of code added TJS 01/06/14 and moved TJS 26/11/15
sql = "SELECT " & EmployerTableMenuOptionFields & " FROM " & DBSchema & "Employers WHERE Employer_ID = " & EmployerID
set rsEmployer = OpenRecordset(objConn, sql)
' end of code added TJS 01/06/14 and moved TJS 26/11/15

sql = "SELECT User_Title, User_Fore_Name, User_Last_Name, Employer_ID, Restrict_Payroll_List, User_Perm_ID FROM " & DBSchema & "Users " ' TJS 26/11/15
sql = sql & "INNER JOIN " & DBSchema & "User_Permissions ON Users.User_ID = User_Permissions.User_ID AND (Employee_Employer_ID = '' " ' TJS 26/11/15
sql = sql & "OR Employee_Employer_ID = '" & EncodeEmployerID(UserPermissionsTable, EmployerID) & "') WHERE Users.User_ID = " ' TJS 26/11/15
sql = sql & UserID & " AND User_Permissions.Deleted = 0 ORDER BY Employee_Employer_ID DESC" ' TJS 26/11/15
set rsUserDetails = OpenRecordset(objConn, sql)
UserName = ""
if "" & DecryptData(rsUserDetails("User_Title")) <> "" then
	UserName = UserName & DecryptData(rsUserDetails("User_Title")) & " "
end if
if "" & DecryptData(rsUserDetails("User_Fore_Name")) <> "" then
	UserName = UserName & DecryptData(rsUserDetails("User_Fore_Name")) & " "
end if
UserName = UserName & DecryptData(rsUserDetails("User_Last_Name"))
PayrollAccessRestricted = cBool(rsUserDetails("Restrict_Payroll_List"))

if PayrollAccessRestricted then
	sql = "SELECT Payroll_ID, Payroll_Access_ID, Enable_Access FROM " & DBSchema & "User_Payroll_Access WHERE Employer_ID = "
	sql = sql & EmployerID & " AND User_ID = '" & EncodeUserID(UserPayrollAccessTable, UserID) & "' AND Payroll_ID > 0 AND Deleted = 0" ' TJS 20/05/18
else
	sql = "SELECT Payroll_ID, -1 AS Payroll_Access_ID, -1 AS Enable_Access FROM " & DBSchema & "Payrolls WHERE Employer_ID = "
	sql = sql & EmployerID & " AND Deleted = 0"
end if
set rsExistPayroll = OpenRecordset(objConn, sql)

' get details for user who is logged in 
sql = "SELECT " & UserTableMenuPermissionFields & " FROM " & DBSchema & "Users INNER JOIN " & DBSchema & "User_Permissions " ' TJS 26/11/15
sql = sql & "ON Users.User_ID = User_Permissions.User_ID AND (Employee_Employer_ID = '' OR Employee_Employer_ID = '" ' TJS 26/11/15
sql = sql & EncodeEmployerID(UserPermissionsTable, EmployerID) & "') WHERE Users.User_ID = " & GetSessionVar("UserID") ' TJS 26/11/15
sql = sql & " AND User_Permissions.Deleted = 0 ORDER BY Employee_Employer_ID DESC" ' TJS 26/11/15
set rsUser = OpenRecordset(objConn, sql)

ReadMainMenuParams SessionVarsID, cBool(rsUser("User_Admin")), (cBool(rsUser("Run_Payroll")) or cBool(rsUser("Payroll_EOY")) or cBool(rsUser("CIS_Returns"))), _
	cBool(rsUser("Run_Payroll")), rsUser("Support_User_ID") ' TJS 20/10/19

if cBool(rsUser("Restrict_Payroll_List")) then
	sql = "SELECT Payrolls.Payroll_ID, Payroll_Name, Payroll_Type FROM " & DBSchema & "Payrolls LEFT JOIN " & DBSchema & "User_Payroll_Access " ' TJS 24/03/15 TJS 26/11/15
	sql = sql & "ON Payrolls.Payroll_ID = User_Payroll_Access.Payroll_ID AND Payrolls.Employer_ID = User_Payroll_Access.Employer_ID "
	sql = sql & "WHERE Payrolls.Employer_ID = " & EmployerID & " AND User_ID = '" & EncodeUserID(UserPayrollAccessTable, GetSessionVar("UserID"))
	sql = sql & "' AND Payrolls.Deleted = 0 AND Enable_Access = -1 AND User_Payroll_Access.Deleted = 0" ' TJS 20/05/18
else
	sql = "SELECT Payroll_ID, Payroll_Name, Payroll_Type FROM " & DBSchema & "Payrolls WHERE Employer_ID = "
	sql = sql & EmployerID & " AND Deleted = 0"
end if
sql = sql & " ORDER BY Payroll_Name" ' TJS 26/11/15
set rsPossPayroll = OpenRecordset(objConn, sql)

' start of code added TJS 26/11/15
if cBool(rsEmployer("Enable_CIS")) then
	if cBool(rsUser("Restrict_Payroll_List")) then
		sql = "SELECT CIS_PayLists.CIS_Paylist_ID, Paylist_Name, Paylist_Type FROM " & DBSchema & "CIS_PayLists LEFT JOIN " & DBSchema
		sql = sql & "User_Payroll_Access ON CIS_PayLists.CIS_Paylist_ID = User_Payroll_Access.CIS_Paylist_ID AND CIS_PayLists.Employer_ID = "
		sql = sql & "User_Payroll_Access.Employer_ID WHERE CIS_PayLists.Employer_ID = " & EmployerID & " AND User_ID = '"
		sql = sql & EncodeUserID(UserPayrollAccessTable, GetSessionVar("UserID")) & "' AND CIS_PayLists.Deleted = 0 AND Enable_Access = -1 " ' TJS 20/05/18
		sql = sql & "AND User_Payroll_Access.Deleted = 0" ' TJS 20/05/18
	else
		sql = "SELECT CIS_Paylist_ID, Paylist_Name, Paylist_Type FROM " & DBSchema & "CIS_PayLists WHERE Employer_ID = "
		sql = sql & EmployerID & " AND Deleted = 0"
	end if
	sql = sql & " ORDER BY Paylist_Name"
	set rsPossPaylist = OpenRecordset(objConn, sql)
end if

if PayrollAccessRestricted then
	sql = "SELECT CIS_Paylist_ID, Payroll_Access_ID, Enable_Access FROM " & DBSchema & "User_Payroll_Access WHERE Employer_ID = "
	sql = sql & EmployerID & " AND User_ID = '" & EncodeUserID(UserPayrollAccessTable, UserID) & "' AND CIS_Paylist_ID > 0 AND Deleted = 0" ' TJS 20/05/18
else
	sql = "SELECT CIS_Paylist_ID, -1 AS Payroll_Access_ID, -1 AS Enable_Access FROM " & DBSchema & "CIS_PayLists WHERE Employer_ID = "
	sql = sql & EmployerID & " AND Deleted = 0"
end if
set rsExistPaylist = OpenRecordset(objConn, sql)
' end of code added TJS 26/11/15

response.Expires = -1500
response.Expiresabsolute = Now() - 2
response.AddHeader "pragma", "no-cache"
response.AddHeader "cache-control", "no-store"
response.CacheControl = "no-cache"
%>
<!DOCTYPE html>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>User Payroll Access</title>

<!-- START OF STYLES -->
<link rel="stylesheet" type="text/css" href="css/BodyAndMenu-css.asp" /> <!-- TJS 30/05/20 -->
<link rel="stylesheet" type="text/css" href="css/forms-css.asp" />
<%if CheckBrowserVersion("MSIE", 8, true, false, false) then ' [if LT IE 8]%>
	<link type="text/css" rel="stylesheet" href="css/buttons-ie.css" />
<%end if
if CheckBrowserVersion("MSIE", 7, true, true, false) then ' [if lte IE 7]%>
	<style>
		.content { margin-right: -1px; } /* this 1px negative margin can be placed on any of the columns in this layout with the same corrective effect. */
		ul.nav a { zoom: 1; }  /* the zoom property gives IE the hasLayout trigger it needs to correct extra whiltespace between the links */
	</style>
<%end if
if "" & GetSessionVar("PathModifier") <> "" then%>
	<link rel="stylesheet" type="text/css" href="css/<%=GetSessionVar("PathModifier")%>Partner-CSS.asp" />
<%end if%>
<!-- END OF STYLES -->

<script src="JavaScript/KeepAliveJS.asp?SV=<%=SessionVarsID%>">
</script>

<link rel="meta" href="http://www.mypaye.co.uk/labels.xml" type="application/rdf+xml" title="ICRA labels" />
<meta http-equiv="pics-Label" content='(pics-1.1 "http://www.icra.org/pics/vocabularyv03/" l gen true for "http://mypaye.co.uk" r (n 0 s 0 v 0 l 0 oa 0 ob 0 oc 0 od 0 oe 0 of 0 og 0 oh 0 c 0) gen true for "http://www.mypaye.co.uk" r (n 0 s 0 v 0 l 0 oa 0 ob 0 oc 0 od 0 oe 0 of 0 og 0 oh 0 c 0))' />

<script>
var buttonsdisabled = false; // TJS 25/09/20

function closewindow(){

	cls=document.getElementById('Close');
	if (cls.innerHTML == 'Cancel') // TJS 01/06/14
	{
		window.open('https://<%=request.servervariables("SERVER_NAME")%>/Secure/UserPayrollAccess.asp?ID=<%=SetPageSessionID(23805, DestFrame)%>&SV=<%=SessionVarsID%>&S=<%=DestFrame%>&D=<%=DestFrame%>&UID=<%=UserID%>&ER=<%=EmployerID%>&EE=<%=EmployeeID%>&CPge=<%=CancelPage%>','_self', '', true);
	}
	else
	{
		<%if EmployerID < 0 then%>
			window.open('https://<%=request.servervariables("SERVER_NAME")%>/Secure/UserDetails.asp?ID=<%=SetPageSessionID(84682, DestFrame)%>&SV=<%=SessionVarsID%>&S=<%=DestFrame%>&D=<%=DestFrame%>&UID=<%=UserID%>&ER=<%=EmployerID%>&EE=<%=EmployeeID%>&ANE=Add&CPge=<%=CancelPage%>','_self','',true);
		<%else%>
			window.open('https://<%=request.servervariables("SERVER_NAME")%>/Secure/UserDetails.asp?ID=<%=SetPageSessionID(84682, DestFrame)%>&SV=<%=SessionVarsID%>&S=<%=DestFrame%>&D=<%=DestFrame%>&UID=<%=UserID%>&ER=<%=EmployerID%>&EE=<%=EmployeeID%>&ANE=<%=AddNewEdit%>&CPge=<%=CancelPage%>','_self','',true);
		<%end if%>
	}
}

function updatesubmit(){

	cls=document.getElementById('Close');
	if (cls.innerHTML == 'Cancel') // TJS 01/06/14
	{
		upd=document.getElementById('Update');
		if ((upd.className != 'continue_button_disabled')) // TJS 01/06/14
		{
	 		document.UserForm.submit();
		}
	}	
}

function datachanged(){

	cls=document.getElementById('Close');
	upd=document.getElementById('Update');
	if ((cls.innerHTML == 'Close') || (upd.className == 'continue_button_disabled')) // TJS 01/06/14
	{
		cls.innerHTML = 'Cancel'; // TJS 01/06/14
		upd.className = 'continue_button'; // TJS 01/06/14
	}
}

function OpenTabERForm(ctrl, destination, id)
{ 
	// Pension Scheme ID (PenSchID) set for all windows even though only Pension Scheme needs it
	window.open('https://<%=request.servervariables("SERVER_NAME")%>/Secure/' + destination + '?ID=' + id + '&SV=<%=SessionVarsID%>&S=<%=DestFrame%>&D=<%=DestFrame%>&ER=<%=EmployerID%>&PenSchID=-1&ANE=Edit&CPge=PayrollHome.asp', '_self','',true);
}

function GoPayrollHome() 
{
	window.open('https://<%=request.servervariables("SERVER_NAME")%>/Secure/PayrollHome.asp?ID=<%=SetPageSessionID(38684, DestFrame)%>&SV=<%=SessionVarsID%>&S=<%=DestFrame%>&D=<%=MainFrame%>&PR=-1&ER=<%=EmployerID%>&RPy=No&RrP=No&CNP=No','PayrollHome','',true);
}

function LogOut() 
{
	if (!buttonsdisabled)
 	{
		buttonsdisabled = true;
		<%if GetSessionVar("AgencyID") = "" or GetSessionVar("AgencyID") = -1 then
			CancelPath = "/Secure/ASPDOTNET/LogOut.aspx" ' TJS 09/04/21
		else
			CancelPath = "/Secure/ASPDOTNET/AgencyCancel.aspx" ' TJS 09/04/21
		end if%>
		window.open('https://<%=request.servervariables("SERVER_NAME") & CancelPath%>?ID=<%=SetPageSessionID(10022, DestFrame)%>&SV=<%=SessionVarsID%>&S=<%=DestFrame%>&D=<%=MainFrame%>&ER=<%=EmployerID%>','PayrollHome','', true);
	}
}

function OpenSupportCentre()
{
	window.open('https://<%=request.servervariables("SERVER_NAME")%>/Secure/Support/UserSupport.asp?ID=<%=SetPageSessionID(96334, DestFrame)%>&SV=<%=SessionVarsID%>&S=<%=DestFrame%>&D=<%=MainFrame%>&ER=<%=EmployerID%>&WthMnu=Yes','PayrollHome','', true);
}
</script>

<!--#include file ="Functions\GoogleAnalytics.asp"-->

</head>

<body class="centered"> <!-- TJS 30/05/20 -->

<!--#include file ="PayrollHeader.asp"-->

<%MenuShowHome = true
MenuShowEmployer = true
MenuShowFindEmployee = true
MenuShowUsers = true
MenuShowCreate = true
MenuShowCreatePayroll = true
MenuShowCreateEmployer = true
MenuShowCreateUser = true
MenuShowHMRC = true
MenuShowReports = true
MenuPartnerReportsOnly = false ' TJS 25/02/20
MenuShowPayrollData = true
MenuShowTools = true
MenuShowSupport = true
MenuSelected = ""
MenuEmployerExists = true
MenuShowAccount = true%>

<!--#include file ="MainMenu.asp"-->

<script>
	//set location in header
	document.getElementById("ProgramLocation").innerHTML = "<h1>User Payroll Access</h1>";
	setTimeout("KeepAlive('<%=SessionVarsID%>')", 60000);
</script>

<form method="post" name="UserForm" action="https://<%=request.servervariables("SERVER_NAME")%>/Secure/ASPDotNet/UpdatePayrollAccess.ashx"> <!-- TJS 11/11/21 -->
	<input type="hidden" name="ID" value="<%=SetPageSessionID(86112, DestFrame)%>">
	<input type="hidden" name="SV" value="<%=SessionVarsID%>">
	<input type="hidden" name="S" value="<%=DestFrame%>">
	<input type="hidden" name="D" value="<%=DestFrame%>">
	<input type="hidden" name="UID" value="<%=UserID%>">
	<input type="hidden" name="UPID" value="<%=rsUserDetails("User_Perm_ID")%>"> <!-- TJS 26/11/15 -->
	<input type="hidden" name="EE" value="<%=EmployeeID%>">
	<input type="hidden" name="ER" value="<%=EmployerID%>">
	<input type="hidden" name="ANE" id="ANE" value="<%=AddNewEdit%>">
	<input type="hidden" name="CPge" value="<%=CancelPage%>">

	<fieldset class="FormPanelCentered" style="width: 500px;">
		<table class="FormPanel" style="width:490px;">
			<tr>
				<td colspan="4"><p class="panelhdr">User Payroll Access for <%=UserName%></p></td>
			</tr>
			<tr>
				<td colspan="4">This User is allowed to access the following Payroll records :-</td>
			</tr>
			<tr>
				<td class="payroll-list-hdr" style="width:200px;"><b>Payroll Name</b></td>
				<td class="payroll-list-hdr" style="width:20px;">&nbsp;</td>
				<td class="payroll-list-hdr" style="width:140px;"><b>Pay Frequency</b></td>
				<td class="payroll-list-hdr" style="width:120px; text-align:center;"><b>Permit Access</b></td>
			</tr>
			<%iLoop = 0
			if not cBool(rsEmployer("Inhibit_Poll_PAYE_Notices")) or not cBool(rsEmployer("Enable_CIS")) then ' TJS 26/11/15
				do while not rsPossPayroll.EOF
					iPRAID = -1%>
					<tr>
						<td style="width:200px;"><%=rsPossPayroll("Payroll_Name")%></td>
						<td style="width:20px;">&nbsp;</td>
						<td style="width:140px;"><%select case rsPossPayroll("Payroll_Type")
							case "W"
								response.write "Weekly"
							case "B"
								response.write "Bi-Weekly"
							case "F"
								response.write "Four-Weekly"
							case "M"
								response.write "Monthly"
							case "Q"
								response.write "Quarterly"
							case "A"
								response.write "Annual"
							case else
						end select%></td>
						<td style="width:120px; text-align:center;"><input type="checkbox" name="UserAccess<%=iLoop%>" value="<%=rsPossPayroll("Payroll_ID")%>"
							<%do while not rsExistPayroll.EOF
								if rsExistPayroll("Payroll_ID") = rsPossPayroll("Payroll_ID") then
									if cBool(rsExistPayroll("Enable_Access")) then
										response.write "checked"
									end if
									iPRAID = rsExistPayroll("Payroll_Access_ID")
									exit do
								end if
								rsExistPayroll.movenext
							loop
							if not rsExistPayroll.BOF then
								rsExistPayroll.movefirst
							end if%> onclick="datachanged()"><input type="hidden" name="PRorPL<%=iLoop%>" value="PR">
							<input type="hidden" name="PRAID<%=iLoop%>" value="<%=iPRAID%>"></td>
					</tr>
					<%rsPossPayroll.movenext
					iLoop = iLoop + 1
				loop
			end if
			' start of code added TJS 26/11/15
			if cBool(rsEmployer("Enable_CIS")) then
				do while not rsPossPaylist.EOF
					iPRAID = -1%>
					<tr>
						<td style="width:200px;"><%=rsPossPaylist("Paylist_Name")%></td>
						<td style="width:20px;">&nbsp;</td>
						<td style="width:140px;"><%select case rsPossPaylist("Paylist_Type")
							case "W"
								response.write "Weekly"
							case "M"
								response.write "Monthly"
							case else
						end select%></td>
						<td style="width:120px; text-align:center;"><input type="checkbox" name="UserAccess<%=iLoop%>" value="<%=rsPossPaylist("CIS_Paylist_ID")%>"
							<%do while not rsExistPaylist.EOF
								if rsExistPaylist("CIS_Paylist_ID") = rsPossPaylist("CIS_Paylist_ID") then
									if cBool(rsExistPaylist("Enable_Access")) then
										response.write "checked"
									end if
									iPRAID = rsExistPaylist("Payroll_Access_ID")
									exit do
								end if
								rsExistPaylist.movenext
							loop
							if not rsExistPaylist.BOF then
								rsExistPaylist.movefirst
							end if%> onclick="datachanged()"><input type="hidden" name="PRorPL<%=iLoop%>" value="PL">
							<input type="hidden" name="PRAID<%=iLoop%>" value="<%=iPRAID%>"></td>
					</tr>
					<%rsPossPaylist.movenext
					iLoop = iLoop + 1
				loop
			end if%>
		</table>
		<input type="hidden" name="PRCount" value="<%=iLoop%>">
	</fieldset>
</form>

<p style="clear: left; padding: 10px;"><a id="Update" class="continue_button_disabled" onclick="updatesubmit()">Update</a>
	<a id="Close" class="cancel_button" onclick="closewindow()">Close</a></p>

</body>

</html>
<%
ReleaseASPEncrypt

rsUserDetails.close
rsPossPayroll.close
rsExistPayroll.close
' start of code added TJS 26/11/15
if cBool(rsEmployer("Enable_CIS")) then
	rsPossPaylist.close
	rsExistPaylist.close
end if
' end of code added TJS 26/11/15
rsUser.close
rsEmployer.close
set rsUserDetails = nothing
set rsPossPayroll = nothing
set rsExistPayroll = nothing
' start of code added TJS 26/11/15
set rsPossPaylist = nothing
set rsExistPaylist = nothing
' end of code added TJS 26/11/15
set rsUser = nothing
set rsEmployer = nothing

SaveSessionVarsRecord SessionVarsID ' TJS 19/06/07

objConn.close
set objConn = nothing%>