<% Response.Expires = -1 %>
<!-- #include file="../datefunc2.inc.asp" -->
<!-- #include file="../dealerconfig\adminsetup.asp" -->
<!-- #include file="../dealerconfig\usermodules.asp" -->
<!-- #include file="../dataconn.inc.asp" -->
<!-- #include file="../displayinfo.inc.asp" -->
<!-- #include file="../encrypt2.inc.asp" -->
<!-- #include file="../dealerconfig\sst.asp" -->

<%
Function checkmoduleaccess(fmodule)
	checkmoduleaccess=0
	'Response.write fmodule
	If (UCustomer="y" AND INSTR(UCASE(role_customer_modules),"|" & fmodule & "")>0) Then 
		checkmoduleaccess=1
	End If
End Function

Function checkreportaccess(fmodule)
	checkreportaccess=0
	'Response.write fmodule
	If (UCustomer="y" AND INSTR(UCASE(role_customer_reports),"|" & fmodule & "")>0) Then 
		checkreportaccess=1
	End If
End Function

defaultbankcode="UOB"
saveservicerecs="N"
defuserpass="Polaris@2005"

authenkey = TRIM(Request("authenkey"))
qsupuserid = TRIM(Request("u"))
qsuppass = TRIM(Request("p"))
qmobile = TRIM(Request("qmobile"))
transtype = TRIM(Request("transtype"))
isemailapproval = TRIM(Request("isemailapproval"))

If hidesecqty="N" Then
	str2ndfactor=",qty9, u_measure9, sndfactor "
	str2nduom2=", icitem.u_measure2 "
ELSE
	str2ndfactor=", 0 as qty9, '' as u_measure9, 0 as sndfactor "
	str2nduom2=", '' as u_measure2 "
End If


If qmobile = "Y" Then
	Ref = EnDeCrypt(authenkey, psw4)
	UniqueNo = Ref
ELSE
	OrderID = EnDeCrypt(authenkey, psw4)
End If

UserName = EnDeCrypt(qsupuserid, psw3)

If sys_approvals_email_autologin="Y" Then
	Password = EnDeCrypt(qsuppass, psw4)
ELSE
	'redirect to login page
	If 1=1 Then
		IF isemailapproval="Y" Then
			If OrderID<> "" AND TransType<>"" Then
				If TransType="PV" Then
					path = "paymentvouchersc.asp?rv_no=" & Server.URLENCODE(OrderID) & endvar
				ELSEIf TransType="CUSTOMER" Then
					path = "customersc.asp?ID=" & Server.URLENCODE(OrderID) & endvar
				ELSE
					'path = "approvals.asp?TransType=" & TransType & "&Ref1=" & Server.URLENCODE(OrderID) & endvar
					path = TranCodetoURL(TransType) & "?OrderID=" & OrderID & "" & endvar
				End If
			ELSE		
				path = "noaccess.asp"
			End If
			If Session("loginCustomerID")<>"" Then
				Response.redirect path
			ELSE
				Response.redirect "customerlogin.asp?return_url=" & Server.URLENCODE(path)
			End If
		End If		
	End If	
End If

Set oConn = Server.CreateObject("ADODB.Connection")
Call dataconn

'insert GST 0, sales
Set rsVis = Server.CreateObject("ADODB.Recordset")
' Create and open an ADO recordset object.
sql = "SELECT code, desc1, desc2, country, type, gtaxcode, gtaxrate, moduleref " & _
	  "FROM stax " & _
	  "WHERE UPPER(code) = 'SR-0' "

rsVis.Open sql, oConn, adOpenDynamic, adLockOptimistic
' Login user
If rsVis.EOF Then
	rsVis.AddNew
		rsVis("code") = "SR-0"
		rsVis("desc1") ="Standard-rated supplies with GST charges 0%"
		rsVis("desc2") = "Standard-rate supplies"
		rsVis("country") = "M"
		rsVis("type") = "S"
		rsVis("gtaxcode") = "SR"
		rsVis("gtaxrate") = 0
		rsVis("moduleref") = 0
	
	rsVis.Update
End If
rsVis.Close
Set rsVis = Nothing

'insert GST 0, purchases
Set rsVis = Server.CreateObject("ADODB.Recordset")
' Create and open an ADO recordset object.
sql = "SELECT code, desc1, desc2, country, type, gtaxcode, gtaxrate, moduleref " & _
	  "FROM stax " & _
	  "WHERE UPPER(code) = 'TX-0' "

rsVis.Open sql, oConn, adOpenDynamic, adLockOptimistic
' Login user
If rsVis.EOF Then
	rsVis.AddNew
		rsVis("code") = "TX-0"
		rsVis("desc1") ="Purchases with GST incurred at 0% and directry attributable to taxable suppliers"
		rsVis("desc2") = "Purchases with GST"
		rsVis("country") = "M"
		rsVis("type") = "P"
		rsVis("gtaxcode") = "ZP"
		rsVis("gtaxrate") = 0
		rsVis("moduleref") = 0
	
	rsVis.Update
End If
rsVis.Close
Set rsVis = Nothing


'insert SST 6%, sales
Set rsVis = Server.CreateObject("ADODB.Recordset")
' Create and open an ADO recordset object.
sql = "SELECT code, desc1, desc2, country, type, gtaxcode, gtaxrate, moduleref " & _
	  "FROM stax " & _
	  "WHERE UPPER(code) = 'SST' "

rsVis.Open sql, oConn, adOpenDynamic, adLockOptimistic
' Login user
If rsVis.EOF Then
	rsVis.AddNew
		rsVis("code") = "SST"
		rsVis("desc1") ="Government Service tax or Sales Tax"
		rsVis("desc2") = "Government Service tax or Sales Tax"
		rsVis("country") = "M"
		rsVis("type") = "S"
		rsVis("gtaxcode") = "SR"
		rsVis("gtaxrate") = 6
		rsVis("moduleref") = 0
	
	rsVis.Update
End If
rsVis.Close
Set rsVis = Nothing
'Response.write USACDATE
'after 1st June 2018, change the default codes
If DateDiff("d","2018-06-01",ConvertMYSQL(USACDATE))>=0 AND enableGSTZero="Y" Then
	gstctaxcodesdef="SR-0"
	gstctaxcodepdef="TX-0"
	gsttaxcodesdef="SR-0"
	gsttaxcodepdef="TX-0"
	gstdefrate=0
End If

'after 1st Sept 2018, change the default codes
If DateDiff("d","2018-09-01",ConvertMYSQL(USACDATE))>=0 AND enableSST="Y" Then
	'gstctaxcodesdef="SR-0"
	'gstctaxcodepdef="TX-0"
	'gsttaxcodesdef="SR-0"
	'gsttaxcodepdef="TX-0"
	'gstdefrate=0
End If

' Determine which button the Web visitor clicked and take appropriate action.
If OrderID <> "" OR Ref <> "" Then
	

   rsp11 = "df6g"

   psw = UCASE(username) & UKACDATE & rsp11
   txt = Request.ServerVariables("REMOTE_HOST") & UCASE(username)

   strTemp = EnDeCrypt(txt, psw)

   for x = 1 to len(strTemp)
      DayPass = DayPass & right(string(2,"0") & hex(asc(mid(strTemp, x, 1))),2) & ""
      if x mod 26 = 0 then response.write vbCRLF
   next

		If username <> "" Then
		Set rsVis = Server.CreateObject("ADODB.Recordset")
		' Create and open an ADO recordset object.
		sql = "SELECT UserID, Password, DayPass, PassTS, LoggedIn, ActiveRec " & _
			  "FROM users " & _
			  "WHERE UPPER(UserID) = '" & UCASE(CleanseInput3(username)) & "'"
		
		rsVis.Open sql, oConn, adOpenDynamic, adLockOptimistic
		' Login user
		If rsVis.EOF Then
			message = "User ID not found. You are not authorised to enter the secure area."
			loginfailure = loginfailure + 1
		Else
			If CleanseInput(Password) = rsVis("Password") Then
				activerec = TRIM(rsVis("activerec"))
				rsVis("DayPass") = DayPass
				rsVis("PassTS") = uautoid
				rsVis("LoggedIn") = "y"
				rsVis.Update
				loggedin = True
			Else
				message = "Password and/or User ID error. You are not authorised to enter the secure area."
				loginfailure = loginfailure + 1
			End If
		End If
		rsVis.Close
		Set rsVis = Nothing
	End If ' validate userid in sql
End If

usertype = "[C]"

	' Get and set time/date settings
	Set rsVis = Server.CreateObject("ADODB.Recordset")
	sql = "SELECT CompanyName, LCID, companyurl as peppolid " & _
		  "FROM companies " & _
		  "WHERE companyid <> '' "
	rsVis.Open sql, oConn, adOpenStatic, adLockReadOnly
	If Not rsVis.EOF Then
		CompanyName = rsVis("CompanyName")
		Upeppolid = rsVis("peppolid")
		strLCID = rsVis("LCID")
		If DefCompanyDesc="" Then
			DefCompanyDesc=CompanyName
		End If
	Else
		CompanyName = ""
		strLCID = 1033  ' Default to English(United States)
	End If
	rsVis.Close
	Set rsVis = Nothing
	
If loggedin Then
	Session("loginCustomerID") = UCASE(username)



	Session.LCID = strLCID

	If usertype = "[E]" AND sys_approvals_email_autologin="Y" Then
		IF isemailapproval="Y" Then
			If OrderID<> "" AND TransType<>"" Then
				If TransType="PV" Then
					path = "paymentvouchersc.asp?rv_no=" & Server.URLENCODE(OrderID) & endvar
				ELSEIf TransType="CUSTOMER" Then
					path = "customersc.asp?ID=" & Server.URLENCODE(OrderID) & endvar
				ELSE
					'path = "approvals.asp?TransType=" & TransType & "&Ref1=" & Server.URLENCODE(OrderID) & endvar
					path = TranCodetoURL(TransType) & "?OrderID=" & OrderID & "" & endvar
				End If
			End If
		ELSEIf qmobile = "Y" Then
			path = "foxcussoprint.asp?Ref=" & Ref & "&UniqueNo=" & UniqueNo & "&type=SO" & endvar
		ELSE
			If OrderID<> "" Then
				If transtype="CR" Then
					path = "cnreqcartall.asp?OrderID=" & OrderID & endvar
				ELSE
					If transtype="DR" Then
						path = "dnreqcartall.asp?OrderID=" & OrderID & endvar
					ELSE
						path = "ordercartall.asp?OrderID=" & OrderID & endvar
					End If
				End If
			Else
				path = "noaccess.asp"
			End If
		End If
		Response.redirect path
	End If
End If

If 1=2 Then
	If Session("loginCustomerID") <> "" Then
		'lengthuser = len(Session("loginCustomerID"))
		curuser = CLEANSEINPUT(Session("loginCustomerID"))
		company = ""
		department = ""
		location = ""
		If Session("uniquesession") <> "" Then
			Set rsVis = Server.CreateObject("ADODB.Recordset")
			' Create and open an ADO recordset object.
			sql = "SELECT UserID, Password, DayPass, PassTS, LoggedIn, ActiveRec, sessionid, ipaddress, lastactivetime " & _
				  "FROM users " & _
				  "WHERE UPPER(sessionid) = '" & UCASE(CleanseInput3(Session("uniquesession"))) & "'"
			rsVis.Open sql, oConn, adOpenDynamic, adLockOptimistic
			' Login user
			If rsVis.EOF Then
				stillloggedin="n"
				message = "Session has expired. Please login again. Code: 100"
			Else
				stillloggedin=TRIM(rsVis("LoggedIn"))
				If stillloggedin="y" Then
					rsVis("lastactivetime") = Now()
				End If
				rsVis.Update
			End If
			rsVis.Close
			Set rsVis = Nothing
		ELSE
			stillloggedin="n"
			message = "Session has expired. Please login again. Code: 101"
		End If
		
		If stillloggedin="n" Then
			Session("uniquesession")=""
			Session("loginCustomerID")=""
			Response.write "<Script Language=""JavaScript"">"
			Response.write "parent.parent.location.href='anotlogin.asp';"
			Response.write "</Script>"
			Response.redirect "anotlogin.asp?"	 & endvar
		End If
	ELSE
		Response.write "<Script Language=""JavaScript"">"
		Response.write "parent.parent.location.href='anotlogin.asp';"
		Response.write "</Script>"
		Response.redirect "anotlogin.asp?"	 & endvar
	End If
End If

If curuser = "" Then
	Response.write "<Script Language=""JavaScript"">"
	Response.write "parent.parent.location.href='anotlogin.asp';"
	Response.write "</Script>"
	Response.redirect "anotlogin.asp?" & endvar
	If dbverify = "y" Then
		Response.Redirect "anotlogin.asp?"	 & endvar
	End If
End If



Set rsItm = Server.CreateObject("ADODB.Recordset")
sql = "SELECT CustomerNo, CompanyName1, CompanyName2, email " & _
      "FROM customers " & _
      "WHERE (UPPER(CustomerNo)='" & UCASE(CleanseInput3(curuser)) & "') " & _
      "ORDER BY CustomerNo "
rsItm.Open sql, oConn, adOpenStatic, adLockReadOnly
If rsItm.EOF Then
	Response.write "<Script Language=""JavaScript"">"
	Response.write "parent.location.href='anotlogin.asp?" & endvar
	Response.write "</Script>"
	Response.redirect "anotlogin.asp?" & endvar
Else
	UPrice = "y"
	UStock = "y"
	ULocation = ""
	UActive = "y"
	UCustomer = "y"
	UCusName = TRIM(TRIM(rsItm("CompanyName1")) & " " & TRIM(rsItm("CompanyName2")))
	UEmail = TRIM(rsItm("email"))
End If
rsItm.Close
set rsItm = nothing

' Get path of accounting DB
Set RS = Server.CreateObject("ADODB.Recordset")
sql = "SELECT AccDBPath, ScriptPath " & _
      "FROM companies " & _
      "WHERE (CompanyID <>'') "
RS.Open sql, oconn, adOpenStatic, adLockReadOnly
If Not RS.EOF Then
	arrprod = RS.GetRows
	AccDBPath = arrprod(0,0)
	'ScriptPath = arrprod(1,0)
	StrScriptPath = Request.ServerVariables("SCRIPT_NAME")
	arrscript = SPLIT(StrScriptPath,"/")
	ScriptPath = "/" & TRIM(arrscript(1)) & "/"

End If
RS.close
set RS = nothing

' Script Paths
prescriptname = ScriptPath
lenprescriptname = len(prescriptname)
sn = Request.ServerVariables("SCRIPT_NAME")
lensn = len(sn)
scriptname = RIGHT(sn,lensn-lenprescriptname)
rsp11 = "df6g"

If UActive <> "y" Then
	If scriptname = "sidemenusales.asp" OR scriptname = "sidemenu.asp" OR scriptname = "passmaintc.asp" Then
		'do nothing
	Else
		Response.write "<Script Language=""JavaScript"">"
		Response.write "parent.parent.location.href='anotlogin.asp';"
		Response.write "</Script>"
		Response.redirect "anotlogin.asp?" & endvar
	End If
End If

securityname = curuser & "[C]"
securitytype = "[C]"

oConn.Close
Set oConn = Nothing

taxdisp = "hidden"
%>
