<!--#include file="../owFunctions/I_GeneralFunctions.asp"-->
<!--#include file="../owTemplates/Template_AllLeft.asp"-->
<%
	'************************************************************************************
	'*																					*
	'*	MODIFICATION TO THIS SCRIPT OR ANY FILES INCLUDED WITH 							*
	'*		ONEWORLDSTORE IS DONE AT YOUR OWN RISK!										*
	'*	Product  : OneWorldStore														*
	'*	Version  : 9.0000																*
	'*	Modified : June 2004															*
	'*	Copyright:	Copyright (C) 2004 OneWorldStore.Com								*
	'*				Registration Number: 1024507										*
	'*				http://strategis.ic.gc.ca/											*
	'*																					*
	'************************************************************************************


	'************************************************************************************
	'*																					*
	'*																					*
	'*																					*
	'************************************************************************************
	Sub ActionSubroutine()
	
		sSQLText = "SELECT idOrder, Total FROM Orders WHERE SessionID = '" & Session.SessionID & "' AND OrderStatus = 7"
		Call DB_getFromDatabase(sSQLText, rsStoreData, "")   
		If ( NOT rsStoreData.EOF ) Then
			sIDOrder		= rsStoreData("idOrder")
			sTotal			= CDbl(rsStoreData("Total"))
		End If
		Call DB_closeRecordset(rsStoreData)

		' must get this first in case the credit information below changes the payment method to blank
		sPaymentMethod = Trim(Request("sPaymentMethod"))
		If ( sPaymentMethod = "" ) Then
			sPaymentMethod = 0
		Else
			sPaymentMethod = CInt(sPaymentMethod)
		End If
		
		If ( sError = "" ) Then
			' the order has been saved so delete the old cart info
			sSQLText = "SELECT * FROM CartProducts WHERE idCart = " & sIDCart
			Call DB_getFromDatabase(sSQLText, rsStoreData, "")
			While ( NOT rsStoreData.EOF )
				Call DB_updateDatabase("DELETE FROM CartProductsOptions WHERE idCartRow = " & rsStoreData("idCartRow"))
				rsStoreData.MoveNext
			Wend
			Call DB_closeRecordset(rsStoreData)
			
			Call DB_updateDatabase("DELETE FROM CartProducts WHERE idCart = " & sIDCart)
			Call DB_updateDatabase("DELETE FROM Cart WHERE SessionUnq = '" & Session.SessionID & "'")
			
			Call SendFinalizedEmails(sIDOrder)
	
			Call DB_updateDatabase("UPDATE Orders SET OrderStatus = " & C_PENDING & " WHERE idOrder = " & sIDOrder)
			
			sSQLText = "SELECT OPR.idProduct, OPR.Quantity, P.Sales FROM OrdersProdRef OPR, Products P WHERE OPR.idOrder = " & sIDOrder & " AND OPR.idProduct = P.idProduct"
			Call DB_getFromDatabase(sSQLText, rsStoreData2, "")
			While ( NOT rsStoreData2.EOF )
				' go through and add the Quantity to the number sold of each product within this order
				'	also remove Quantity to the number in stock
				sSQLText = "UPDATE Products SET Sales = " & (rsStoreData2("Sales")+rsStoreData2("Quantity")) & " WHERE idProduct = " & rsStoreData2("idProduct")
				call DB_updateDatabase(sSQLText)
				rsStoreData2.MoveNext
			Wend
			Call DB_closeRecordset(rsStoreData2)
			
			' go to a payment form or to default order confirmation
			Select Case sPaymentMethod
				Case C_PM_MAILIN
					Response.Redirect "PaymentMethods/owMailIn.asp?idOrder=" & sIDOrder
				Case C_PM_FAXIN
					Response.Redirect "PaymentMethods/owFaxIn.asp?idOrder=" & sIDOrder
				Case C_PM_PHONEIN
					Response.Redirect "PaymentMethods/owPhoneIn.asp?idOrder=" & sIDOrder
				Case Else
					Response.Redirect "CustomerAdmin/owSavedOrderDetails.asp?idOrder=" & sIDOrder & "&sAction=New"
			End Select
		Else
			Response.Redirect "owOrderVerify.asp?sError=" & sError & "&sPaymentMethod=" & sPaymentMethod
		End If

	end Sub
	'************************************************************************************
	
	
	'************************************************************************************
	'*																					*
	'*																					*
	'*																					*
	'************************************************************************************
	Sub SendFinalizedEmails(sIDOrder)

		sSQLText = "SELECT AccountUnq, Address, CustomerComments, PaymentMethod, PaymentMethodFee, ShipmentDetails, ShippingFee, TaxAmount, Total FROM Orders WHERE idOrder = " & sIDOrder
		Call DB_getFromDatabase(sSQLText, rsStoreData, "")
		If ( NOT rsStoreData.EOF ) Then
			sAccountUnq			= rsStoreData("AccountUnq")
			sFName				= ACCNT_ReturnADV("OW_CUSTFNAME", "V", sAccountUnq, C_OVERRIDE_ADV)
			sLName				= ACCNT_ReturnADV("OW_CUSTLNAME", "V", sAccountUnq, C_OVERRIDE_ADV)
			sPhone				= ACCNT_ReturnADV("OW_CUSTPHONE", "V", sAccountUnq, C_OVERRIDE_ADV)
			sEmail				= ACCNT_ReturnADV("OW_CUSTEMAIL", "V", sAccountUnq, C_OVERRIDE_ADV)
			sAddress			= Trim(rsStoreData("Address"))
			sComments			= Trim(rsStoreData("CustomerComments"))
			sPaymentDetails		= Trim(rsStoreData("PaymentMethod")) & " : " & CONVERSION_Currency(Trim(rsStoreData("PaymentMethodFee"))) & " extra"
			pShipmentDetails	= Trim(rsStoreData("ShipmentDetails")) & " : " & CONVERSION_Currency(Trim(rsStoreData("ShippingFee")))
			sTaxAmount			= Trim(rsStoreData("TaxAmount"))
			sTotalAmount		= Trim(rsStoreData("Total"))
		End If
		Call DB_closeRecordset(rsStoreData)


		sSQLText = "SELECT * FROM OrdersProdRef OPR, products P WHERE OPR.idProduct = P.idProduct AND OPR.idOrder = " & sIDOrder
		Call DB_getFromDatabase(sSQLText, rsStoreData, "")    
		do while not rsStoreData.eof
			sIDProduct				= rsStoreData("idProduct")
			sQuantity				= rsStoreData("quantity")
			sUnitPrice				= Cdbl(rsStoreData("unitPrice"))
			sUnitBtoBPrice			= Cdbl(rsStoreData("unitBtoBPrice"))
			sDescription			= rsStoreData("description")
			sSku					= rsStoreData("sku")
			'sDetails				= rsStoreData("Details")
			sOptionGroupsTotal		= 0
			sOptionsDescripCompound	= ""

			' get optionals for current cart row
			sSQLText = "SELECT OptionDescription, PriceToAdd, OptionGroupDescription FROM OrdersProdOptions WHERE idOrder = " & sIDOrder & " AND idProduct = " & sIDProduct
			Call DB_getFromDatabase(sSQLText, rsStoreData2, "")
			do while not rsStoreData2.eof
				sPriceToAdd				= Cdbl(rsStoreData2("PriceToAdd"))
				sOptionDescrip			= rsStoreData2("OptionDescription")
				sOptionGroupDescription	= rsStoreData2("OptionGroupDescription")
				sOptionsDescripCompound	= sOptionsDescripCompound & sOptionGroupDescription & " : " & sOptionDescrip & " "
				if sPriceToAdd > 0 then
					sOptionsDescripCompound	= sOptionsDescripCompound & " : " & CONVERSION_Currency(sPriceToAdd)
				End If
				sOptionsDescripCompound	= sOptionsDescripCompound & VbCrlf
				sOptionGroupsTotal		= sOptionGroupsTotal + sPriceToAdd
				rsStoreData2.movenext
			loop      
			Call DB_closeRecordset(rsStoreData2)

			if ACCNT_InGroup(iLoginAccountUnq, C_GROUP_RETAIL) then
				sRowPrice = Cdbl(sQuantity * (sOptionGroupsTotal + sUnitPrice) )         
			else
				sRowPrice = Cdbl(sQuantity * (sOptionGroupsTotal + sUnitBtoBPrice) )                  
			End If             
			
			sDetails = sDetails & sQuantity & "x #" & sSku & "/" & sIDProduct & " " & sDescription 
			sDetails = sDetails & LANG_DT( "SAVEORDER_OPTIONS", " Options" ) & ": " & sOptionsDescripCompound & "= " & CONVERSION_Currency(sRowPrice) & Vbcrlf              
			
			rsStoreData.movenext
		loop
		Call DB_closeRecordset(rsStoreData)
		
	
		' Send email to the store owner
		sOwnerBody = LANG_DT( "SAVEORDER_ORDERRECEIVED_OWNER_EMAIL", "" )
		sOwnerBody = Replace(sOwnerBody, "#DETAILS#", sDetails)
		sOwnerBody = Replace(sOwnerBody, "#NAME#", sFName & " " & sLName)
		sOwnerBody = Replace(sOwnerBody, "#EMAIL#", sEmail)
		sOwnerBody = Replace(sOwnerBody, "#ADDRESS#", sAddress)
		sOwnerBody = Replace(sOwnerBody, "#PHONE#", sPhone)
		sOwnerBody = Replace(sOwnerBody, "#COMMENTS#", sComments)
		sOwnerBody = Replace(sOwnerBody, "#PAYMENT#", sPaymentDetails)
		sOwnerBody = Replace(sOwnerBody, "#SHIPPING#", pShipmentDetails)
		sOwnerBody = Replace(sOwnerBody, "#TAXES#", CONVERSION_Currency(sTaxAmount))
		sOwnerBody = Replace(sOwnerBody, "#TOTAL#", CONVERSION_Currency(sTotalAmount))
'Details: 
'#DETAILS#
'Customer: #NAME#
'Email: #EMAIL#
'Address: #ADDRESS#
'Phone #: #PHONE#
'Comments: #COMMENTS#
'Payment: #PAYMENTDETAILS#
'Shipping: #SHIPDETAILS#
'Taxes: #TAXES#
'Order total: #TOTAL#
'OneWorldStore
'http://www.OneWorldStore.com
		
		sSubject = Replace(LANG_DT( "SAVEORDER_ORDERRECEIVED_OWNER_EMAIL_SUBJECT" ,"A customer has placed a new order (##ORDERNUM#)" ), "#ORDERNUM#", sOWOrderPrefix & sIDOrder)
		Call sendmail (sOWCompany, sOWEmailSender, sOWEmailAdmin, sSubject, sOwnerBody)

		sSQLText = "SELECT * FROM OrdersTaxes WHERE idOrder = " & sIDOrder & " ORDER BY CountryCode, StateCode, ZipCode"
		Call DB_getFromDatabase(sSQLText, rsStoreData, "")
		While ( NOT rsStoreData.EOF )
			sTaxDetail = sTaxDetail & VbTab & Trim(rsStoreData("TaxName")) & " : " & "%" & Trim(rsStoreData("Percentage")) & VbCrlf
			rsStoreData.MoveNext
		Wend
	
		' Send email to the customer if the "Save Order" email is enabled
		If ( NOT bOWDisableSaveOrderEmail ) Then
			sCustomerBody = LANG_DT( "SAVEORDER_ORDERRECEIVED_CUST_EMAIL", "" )
			sCustomerBody = Replace(sCustomerBody, "#NAME#", sFName & " " & sLName)
			sCustomerBody = Replace(sCustomerBody, "#COMPANY#", sOWCompany)
			sCustomerBody = Replace(sCustomerBody, "#DETAILS#", sDetails)
			sCustomerBody = Replace(sCustomerBody, "#PAYMENT#", sPaymentDetails)
			sCustomerBody = Replace(sCustomerBody, "#SHIPPING#", pShipmentDetails)
			sCustomerBody = Replace(sCustomerBody, "#TAXES#", CONVERSION_Currency(sTaxAmount))
			sCustomerBody = Replace(sCustomerBody, "#TAXDETAILS#", sTaxDetail)
			sCustomerBody = Replace(sCustomerBody, "#TOTAL#", CONVERSION_Currency(sTotalAmount))
			sCustomerBody = Replace(sCustomerBody, "#ADMINEMAIL#", sOWEmailAdmin)
'#NAME#
'#COMPANY#
'Your order consisted of: 
'#DETAILS#
'#PAYMENT#
'#SHIPPING#
'Applicable taxes: #TAXES#
'#TAXDETAILS#
'-------------------
'Total #TOTAL#
'
'#COMPANY#
'#ADMINEMAIL#
'Payment confirmation will be sent to you in a second email.
'Powered by OneWorldStore
'http://www.OneWorldStore.com

			sSubject = Replace(Replace(LANG_DT("SAVEORDER_ORDERRECEIVED_CUST_EMAIL_SUBJECT" ,"#COMPANY# thanks you for your order! (##ORDERNUM#)" ), "#COMPANY#", sOWCompany), "#ORDERNUM#", sOWOrderPrefix & sIDOrder)
			Call sendmail (sOWCompany, sOWEmailSender, sEmail, sSubject, sCustomerBody)
		End If

	End Sub
	'************************************************************************************
%>