<%
' MyPAYE
' Module: UpdateGenAEFileFormat.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 - 2017  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 21 February 2017
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
'   Description -    This module validates the data entered for the Generic AE export file format entry and 
'                    if valid, it creates or updates the format record
'
' Amendment Log
'------------------------------------------------------------------------------------------
' Date     | Name            | Vers.  | Description
'------------------------------------------------------------------------------------------
' 21/03/16 | DC              | 5.0.05 | Page added
' 17/06/15 | DC              | 5.0.08 | Added Gross earnings
' 26/05/16 | TJS             | 5.0.09 | Added response.end to prevent errors when validation fails
' 03/06/16 | DC              | 5.0.09a| Corrected logic for finding duplicate 'sort' values.
' 08/12/15 | DC              | 5.0.17 | Added Taxable earnings
' 21/02/17 | DC              | 5.0.17a| Added Non-Banded pens earnings.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

PageID = 68344
NoBack = True
SessionID = request.form("ID")
SessionVarsID = RemoveHTML(request.form("SV"))
SourceFrame = RemoveHTML(request.form("S"))
DestFrame = RemoveHTML(request.form("D"))
EmployerID = CheckURLDBIDField(RemoveHTML(request.form("ER")), "EmployerID")
AddNewEdit = RemoveHTML(request.form("ANE"))
CancelPage = RemoveHTML(request.form("CPge"))
%>

<!--#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"-->

<!DOCTYPE html>
<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

<title>Update Generic AE File Format</title>

<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))' />

<!--#include virtual ="Secure\Functions\GoogleAnalytics.asp"-->

</head>

<%
dim arColUsed(32) ' DC 17/05/16 DC 08/12/16 DC 21/02/17
dim arBaseNames(32) ' DC 17/05/16 DC 08/12/16 DC 21/02/17
	arBaseNames(1) = "Title"
	arBaseNames(2) = "EE 1st Name"
	arBaseNames(3) = "EE Surname"
	arBaseNames(4) = "DoB"
	arBaseNames(5) = "NI No"
	arBaseNames(6) = "Gender"
	arBaseNames(7) = "Pay Ref"
	arBaseNames(8) = "Address 1"
	arBaseNames(9) = "Address 2"
	arBaseNames(10) = "Town/City"
	arBaseNames(11) = "County"
	arBaseNames(12) = "Post Code"
	arBaseNames(13) = "Country"
	arBaseNames(14) = "E-mail Address"
	arBaseNames(15) = "Start Date"
	arBaseNames(16) = "Leaving Date"
	arBaseNames(17) = "Pay Period"
	arBaseNames(18) = "Pay Frequency"
	arBaseNames(19) = "Enrolment Date"
	arBaseNames(20) = "Postponement Date"
	arBaseNames(21) = "Opt Out Date"
	arBaseNames(22) = "AE Status"
	arBaseNames(23) = "Pensionable Earnings"
	arBaseNames(24) = "Employee Cont £"
	arBaseNames(25) = "Employee Cont %"
	arBaseNames(26) = "Employer Cont £"
	arBaseNames(27) = "Employer Cont %"
	arBaseNames(28) = "AVC Cont £"
	arBaseNames(29) = "AVC Cont %"
	arBaseNames(30) = "Gross Earnings" ' DC 17/05/16
	arBaseNames(31) = "Taxable Earnings" ' DC 08/12/16
	arBaseNames(32) = "NonBanded Pensionable Earnings" ' DC 21/02/17

For iLoop = 0 to UBound(arBaseNames)
	arColUsed(iLoop) = False
next

iOutCount = 0

For iLoop = 1 to UBound(arBaseNames) ' DC 03/06/16
 	if arColUsed(cInt(RemoveHTML(request.form("Sort" & iLoop)))) then
 		AddFailureField("Sort" & iLoop)
 		
 		for iLoop2 = 1 to UBound(arBaseNames) ' DC 03/06/16
 			if cInt(RemoveHTML(request.form("Sort" & iLoop2))) = cInt(RemoveHTML(request.form("Sort" & iLoop))) and iLoop <> iLoop2 then ' DC 03/06/16
 				AddFailureField("Sort" & iLoop2) ' TJS 26/05/16 DC 03/06/16
 			end if
 		next

		AlertMessage =  AlertMessage & "Duplicate values used in Sort entries."
	end if
	
	if cInt(RemoveHTML(request.form("Sort" & iLoop))) > 0 then
		arColUsed(cInt(RemoveHTML(request.form("Sort" & iLoop)))) = True
	end if
	
	if "" & RemoveHTML(request.form("Include" & iLoop)) = "ON" then
		iOutCount = iOutCount + 1
	end if

 	if "" & RemoveHTML(request.form("CustName" & iLoop)) = "" and "" & RemoveHTML(request.form("Include" & iLoop)) = "ON" then
		AddFailureField("CustName" & iLoop)
		if "" & AlertMessage <> "" then
			AlertMessage = AlertMessage & "\r\n"
		end if
		AlertMessage = AlertMessage & "Empty custom column name."
	end if
Next

For iLoop = 1 to UBound(arBaseNames) ' DC 03/06/16
	if iLoop > iOutCount then	
	 	for iLoop2 = 1 to UBound(arBaseNames) ' DC 03/06/16
 			if cInt(RemoveHTML(request.form("Sort" & iLoop2))) = iLoop then ' DC 03/06/16
 				AddFailureField("Sort" & iLoop2) ' TJS 26/05/16 DC 03/06/16
  				if "" & AlertMessage <> "" then
					AlertMessage = AlertMessage & "\r\n"
				end if
				AlertMessage = AlertMessage & "Column position " & iloop & " exceeds number of output columns."				
 			end if
 		next
	else
 		if not arColUsed(iLoop) then
 			AddFailureField("Dummy" & iLoop) ' TJS 26/05/16 DC 03/06/16
 			if "" & AlertMessage <> "" then
				AlertMessage = AlertMessage & "\r\n"
			end if
			AlertMessage = AlertMessage & "Column " & iloop & " not defined, correct sort order."
		end if
	end if
next

' were there any validation errors ?
if "" & FailureFields <> "" then
	' yes - redisplay for correction%>	
	<body>
		<form name="DispGenAEErr" id="DispGenAEErr" method="POST" action="https://<%=request.servervariables("SERVER_NAME")%>/Secure/GenericAEExportFormat.asp">
			<input type="hidden" name="ID" value="<%=SetPageSessionID(43183, 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="ER" value="<%=EmployerID%>">
			<input type="hidden" name="ANE" value="<%=AddNewEdit%>">
			<input type="hidden" name="CPge" value="<%=CancelPage%>">
			
		<%For iLoop = 1 to UBound(arBaseNames)%>
			<input type="hidden" name="Incl<%=iLoop%>" value="<%=RemoveHTML(request.form("Include" & iLoop))%>">
			<input type="hidden" name="CuNme<%=iLoop%>" value="<%=RemoveHTML(request.form("CustName" & iLoop))%>">
			<input type="hidden" name="Srt<%=iLoop%>" value="<%=RemoveHTML(request.form("Sort" & iLoop))%>">
		<%Next%>
	
			<input type="hidden" name="FFld" value="<%=FailureFields%>">
			<input type="hidden" name="FMsg" value="<%=FailureMessage%>">
			<input type="hidden" name="AlMsg" value="<%=AlertMessage%>">
			<input type="hidden" name="iOutCount" value="<%=iOutCount%>">

		</form>

		<%SaveSessionVarsRecord SessionVarsID 
		objConn.close
		set objConn = nothing%>
	
		<script>
			document.DispGenAEErr.submit();
		</script>
	</body>	
	</html>	
	<%response.end ' TJS 26/05/16
end if%>

<!--#include file ="Functions\ConnectDBTransact.asp"-->

<%objConnTransact.BeginTrans
TimeStamp = Now()

For iLoop = 1 to UBound(arBaseNames)
	AddNewEdit  = ""

	sql = "SELECT AE_File_Fmt_ID, Deleted  FROM " & DBSchema & "Gen_AE_File_Format WHERE Employer_ID = " & EmployerID
	sql = sql & " AND Source_Col_ID = " & iLoop 
	set rsFileFmt = OpenRecordset(objConn, sql)		

	if "" & RemoveHTML(request.form("Include" & iLoop)) = "ON" then
		Include = -1 
	else 
		Include = 0
	end if
	
	sCustName = "" & RemoveHTML(request.form("CustName" & iLoop))
	iSort = cInt(RemoveHTML(request.form("Sort" & iLoop)))
	
	if rsFileFmt.EOF and cBool(Include) then
		sql = "INSERT INTO " & DBSchema & "Gen_AE_File_Format (Employer_ID, Source_Col_ID, Source_Name, Custom_Name, Sort_Order, EnteredBy)"	
		sql = sql & " VALUES (" & EmployerID & ", " & iLoop & ", '" & arBaseNames(iLoop) & "', '" & sCustName & "', " & iSort & ", "
		sql = sql & GetSessionVar("UserID") & ")"
		AddNewEdit = "Add"
	else
		if cBool(Include) then
			sql = "UPDATE " & DBSchema & "Gen_AE_File_Format SET Custom_Name = '" & ConvertQuoteChars(sCustName)
			sql = sql & "', Source_Name = '" & arBaseNames(iLoop) & "', Sort_Order = " & iSort & ", Deleted = 0 "
			sql = sql & "WHERE Employer_ID = " & EmployerID & " AND Source_Col_ID = " & iLoop 
		else
			sql = "UPDATE " & DBSchema & "Gen_AE_File_Format SET Deleted = -1 "
			sql = sql & "WHERE Employer_ID = " & EmployerID & " AND Source_Col_ID = " & iLoop 
		end if
		AddNewEdit = "Edit"
	end if	
	ExecuteSQL objConnTransact, sql
next

'	if AddNewEdit = "Add" or AddNewEdit = "Edit" then
'		sql = "INSERT INTO " & DBSchema & "Activity_Log (User_ID, Employer_ID, Activity, Time_Stamp) VALUES ("
'		if AddNewEdit = "Add" then
'			sql = sql & GetSessionVar("UserID") & ", " & EmployerID & ", 'Add GFmt', " & DBFormatDateTime(TimeStamp, True) & ")"
'		else
'			sql = sql & GetSessionVar("UserID") & ", " & EmployerID & ", 'Upd GFmt', " & DBFormatDateTime(TimeStamp, True) & ")"
'		end if
'		ExecuteSQL objConnTransact, sql
'
'		sql = "SELECT Activity_ID FROM " & DBSchema & "Activity_Log WHERE User_ID = " & GetSessionVar("UserID")
'		if AddNewEdit = "Add" then
'			sql = sql & " AND Activity = 'Add GFmt' AND Time_Stamp = " & DBFormatDateTime(TimeStamp, True)
'		else
'			sql = sql & " AND Activity = 'Upd GFmt' AND Time_Stamp = " & DBFormatDateTime(TimeStamp, True)
'		end if
'		set rsActivityID = OpenRecordset(objConnTransact, sql)
'
'		sql = "INSERT INTO " & DBSchema & "Activity_Items (Activity_ID, Table_Updated, Row_Updated) "
'		sql = sql & "VALUES (" & rsActivityID(0) & ", 'Gen_AE_File_Format', " & EmployerID & ")"
'		ExecuteSQL objConnTransact, sql
'		rsActivityID.close
'		set rsActivityID = nothing
'	end if
	
objConnTransact.CommitTrans
objConnTransact.close
set objConnTransact = nothing

SaveSessionVarsRecord SessionVarsID 

ReleaseASPEncrypt

objConn.close
set objConn = nothing

response.Expires = -1500
response.Expiresabsolute = Now() - 2
response.AddHeader "pragma", "no-cache"
response.AddHeader "cache-control", "no-store"
response.CacheControl = "no-cache"
%>
<body>

	<script>
		window.open('https://<%=request.servervariables("SERVER_NAME")%>/Secure/GenericAEExportFormat.asp?ID=<%=SetPageSessionID(43183, DestFrame)%>&SV=<%=SessionVarsID%>&S=<%=DestFrame%>&D=<%=DestFrame%>&ER=<%=EmployerID%>&ANE=<%=AddNewEdit%>&CPge=<%=CancelPage%>','_self','',true);
	</script>
</body>

</html>