/*

Owner: Explore Consulting
Copyright (c) 2006, Explore Consulting
All rights reserved.


Agreement

Redistribution and use of this software in source and binary forms, with or 
without modification, are permitted provided that all of the following 
conditions are met:

(1) Redistributions of source code must retain:
	(i) the above copyright notice,
 	(ii) this list of redistribution conditions, and 
 	(iii) the disclaimer appearing below.

(2) Redistributions in binary form must retain: 
	(i) the above copyright notice, 
	(ii) this list of redistribution conditions, and 
	(iii) the disclaimer appearing below in the documentation and/or any other 
	materials provided with the distribution.
	
(3)	Neither the name of the Explore Consulting nor the names of its 
	contributors may be used to endorse or promote products derived from this 
	software without specific prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/
/**
 * Company		Explore Consulting, LLC - www.exploreconsulting.com
 * 
 * Name of Script	emailNotification.js
 * 
 * Date			10/10/2006
 * 
 * Version		1.0
 * 
 * Type			Server
 * 
 * Sub-Type		Business Workflow
 * 
 * Categories		Email notification, business logic
 * 
 * Description		Server-Side SuiteScript that notifies managers/employees if a SO with a discount of 
 * 			more than $250 has been created, or an existing SO has been modified.
 * 
 * NetSuite Ver.	11.0.4 or later 
 * 
 * Installation		1. Modify emailRecipient, and maximumDiscountAmount appropriate for your situation
 * 			2. Upload this file into your account's SuiteScripts folder
 * 			3. Create server script User Event (Setup > Customization > Server Scripts)
 * 			4. Under the Scripts tab, select the newly uploaded file, and add "afterSubmit" in the
 * 			   "After Submit Function" field.
 * 			5. Under Deployments tab, add new line, where Applies To is Sales Order.
 * 
 * License 		THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 *			EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 *			MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 *			THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *			SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 *			OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 *			HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 *			TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *			SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**/

function afterSubmit(type)
{
	var emailRecipient = 3; // Internal ID of an employee receiving the notification
	var maximumDiscountAmount = 250.00;
	
	// Run the script only if the sales order has just been created or edited
	if(type == "create" || type == "edit")
	{
		// Get the record currently being used
		var record = nlapiGetNewRecord();
		
		// Get discount amount
		var discountTotal = parseFloat(record.getFieldValue("discounttotal"));
		
		// If the discount is more than maximumDiscountAmount, then send an email to the Sales Manager
		if(discountTotal < (-1*(Math.abs(maximumDiscountAmount))))
		{
			// Get the user's Internal ID
			var userID = nlapiGetUser();
			
			// Create a message body for the email that will send to the Sales manager 
			var messageBody = "SALES ORDER: " + record.getFieldValue("tranid");
			messageBody += "\nCREATED BY: " + nlapiLookupField("employee", userID, "entityid", null);
			messageBody += "\nDISCOUNT: " + discountTotal.toFixed(2);

			nlapiSendEmail(userID, emailRecipient, "Sales Order created with a discount of more than $" + maximumDiscountAmount, messageBody, null, null);
		}
	}
}