Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Snippet Bakery Order Confirmation Email
function CakeOrder(e){
/* ----- CREATE NEWSUBMISSION OBJECT ----- */
var NewSubmission = {};
var Properties = ["Section One: Product Details","Size","Style","Filling","Topping","Decoration",
"Section Two: Delivery Details","First name","Family name","Email Address","Address","Delivery","Day","Month","Year"];
var i = 0;
var iSkip = 0;
for(i = 0; i < Properties.length; i++){
if(Properties[i].slice(0,7) == "Section"){
NewSubmission[Properties[i]] = " ";
iSkip++;
} else {
//add 1 because Timestamp is e.values[0] and we don't need this
NewSubmission[Properties[i]] = e.values[(i+1)-iSkip];
}
}
/* ----- DATE & COST CALCULATIONS ----- */
// Create string version of date entered to be read by humans
NewSubmission["Delivery Date"] = NewSubmission["Day"] + "-" + NewSubmission["Month"] + "-" + NewSubmission["Year"];
// create Javascript Date Object of Date entered
var OrderFor = new Date(NewSubmission["Month"] + " " + NewSubmission["Day"] + ", " + NewSubmission["Year"]);
var Submitted = new Date(e.values[0]);
// milliseconds between two dates divided by milliseconds in a day, rounded down
var Notice = Math.floor((OrderFor.getTime() - Submitted.getTime()) / (1000 * 60 * 60 * 24));
var Declined = false;
if(Notice < 3){
Declined = true;
} else if (Notice <6){
NewSubmission["Notice"] = Notice.toString() + " days - £5 Fast track surcharge";
} else {
NewSubmission["Notice"] = Notice.toString() + " days - No surcharge";
}
delete NewSubmission["Day"];
delete NewSubmission["Month"];
delete NewSubmission["Year"];
// Extract the cost from the description string
var CakeCost = parseInt(NewSubmission["Size"].slice(NewSubmission["Size"].indexOf("£")+1),10);
var DeliveryCost = (NewSubmission["Delivery"].slice(0,8) == "Delivery") ? 3 : 0;
var FastTrackCost = (Notice < 6) ? 5 : 0;
var TotalCost = CakeCost + DeliveryCost + FastTrackCost;
NewSubmission["Total Cost"] = "£" + TotalCost.toString() + ".00";
/* ----- BUILD HTML RESPONSE TABLE ----- */
var HTMLtable = "<table style=\"border:2px solid black;width:500\" >";
var rowCount = 0;
for (var property in NewSubmission) {
if(NewSubmission[property].length != 0){
if(property.substring(0,7) === "Section"){
HTMLtable += "<tr><th colspan=\"2\" bgcolor=\"#c7ced4\">"+ property +"</th></tr>";
} else {
if(rowCount % 2 === 0){
HTMLtable += "<tr><td>" + property + "</td><td>" + NewSubmission[property] + "</td></tr>";
} else {
HTMLtable += "<tr bgcolor=\"#d0eaf9\"><td>" + property + "</td><td>" + NewSubmission[property] + "</td></tr>"
}
}
rowCount++;
}
}
// don't forget to close your table tags!
HTMLtable += "</table>";
/* ----- SEND ORDER CONFIRMATION ----- */
if(Declined){
MailApp.sendEmail(NewSubmission["Email Address"] , "Sorry - your order for a " + NewSubmission["Style"] + " was declined", " ", {htmlBody:
"<br /><br />Dear " + NewSubmission["First name"] + "," +
"<br /><br />Unfortunately we need at least 3 days' notice to process orders. We hope that you understand and that you'll give us another try soon." +
"<br /><br />Kind regards" +
"<br />Snippets Bakery", name:"Snippets Bakery"})
} else {
MailApp.sendEmail(NewSubmission["Email Address"] , "Your order for a " + NewSubmission["Style"] + " was received!", " ", {htmlBody:
"<br /><br />Dear " + NewSubmission["First name"] + "," +
"<br /><br />Thank you for your order. Here are the details:" +
"<br /><br />" + HTMLtable +
"<br /><br />We hope you're looking forward to enjoying your cake!" +
"<br /><br />Kind regards" +
"<br />Snippets Bakery", name:"Snippets Bakery"})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment