hst applied
This commit is contained in:
parent
5eea071398
commit
95b3c6c9f5
@ -4,7 +4,7 @@ import { sendCakeOrderMail } from "../../utils/mailer.js";
|
|||||||
// POST → Create new cake order
|
// POST → Create new cake order
|
||||||
export const createCakeOrder = async (req, res) => {
|
export const createCakeOrder = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { order, email, totalPieces, totalPrice } = req.body;
|
let { order, email, totalPieces, totalPrice, hst } = req.body;
|
||||||
|
|
||||||
if (!order || typeof order !== "object") {
|
if (!order || typeof order !== "object") {
|
||||||
return res.status(400).json({ message: "Order data is required" });
|
return res.status(400).json({ message: "Order data is required" });
|
||||||
@ -14,14 +14,21 @@ export const createCakeOrder = async (req, res) => {
|
|||||||
return res.status(400).json({ message: "Total pieces and price are required" });
|
return res.status(400).json({ message: "Total pieces and price are required" });
|
||||||
}
|
}
|
||||||
|
|
||||||
const newOrder = await CakeOrder.create({ order, email, totalPieces, totalPrice });
|
// ✅ Auto-calculate HST if not provided
|
||||||
|
if (!hst || hst <= 0) {
|
||||||
|
hst = parseFloat((totalPrice * 0.13).toFixed(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
const newOrder = await CakeOrder.create({ order, email, totalPieces, totalPrice, hst });
|
||||||
|
|
||||||
|
// Send email (async)
|
||||||
if (email) {
|
if (email) {
|
||||||
sendCakeOrderMail(email, order, totalPieces, totalPrice)
|
sendCakeOrderMail(email, order, totalPieces, totalPrice, hst)
|
||||||
.then(() => console.log("Cake order email sent to", email))
|
.then(() => console.log("Cake order email sent to", email))
|
||||||
.catch((err) => console.error("Email send failed:", err));
|
.catch((err) => console.error("Email send failed:", err));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ✅ Return HST in response
|
||||||
res.status(201).json({
|
res.status(201).json({
|
||||||
message: "Cake order created successfully",
|
message: "Cake order created successfully",
|
||||||
data: newOrder,
|
data: newOrder,
|
||||||
|
|||||||
@ -19,6 +19,7 @@ const CakeOrderSchema = new mongoose.Schema(
|
|||||||
},
|
},
|
||||||
totalPieces: { type: Number, required: true },
|
totalPieces: { type: Number, required: true },
|
||||||
totalPrice: { type: Number, required: true },
|
totalPrice: { type: Number, required: true },
|
||||||
|
hst: { type: Number, required: false, default: 0 }, // ✅ Added HST field
|
||||||
},
|
},
|
||||||
{ timestamps: true }
|
{ timestamps: true }
|
||||||
);
|
);
|
||||||
|
|||||||
@ -63,8 +63,7 @@ export async function sendResetPasswordMail(email, token) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export const sendCakeOrderMail = async (email, order, totalPieces, totalPrice, hst) => {
|
||||||
export const sendCakeOrderMail = async (email, order, totalPieces, totalPrice) => {
|
|
||||||
try {
|
try {
|
||||||
const transporter = nodemailer.createTransport({
|
const transporter = nodemailer.createTransport({
|
||||||
host: "mail.metatron-admin-backend.metatronhost.com",
|
host: "mail.metatron-admin-backend.metatronhost.com",
|
||||||
@ -77,6 +76,10 @@ export const sendCakeOrderMail = async (email, order, totalPieces, totalPrice) =
|
|||||||
tls: { rejectUnauthorized: false },
|
tls: { rejectUnauthorized: false },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ✅ Auto-calculate HST if not provided
|
||||||
|
const hstAmount = hst && hst > 0 ? hst : totalPrice * 0.13;
|
||||||
|
const grandTotal = totalPrice + hstAmount;
|
||||||
|
|
||||||
// Build table rows
|
// Build table rows
|
||||||
let orderRows = "";
|
let orderRows = "";
|
||||||
Object.entries(order).forEach(([category, items]) => {
|
Object.entries(order).forEach(([category, items]) => {
|
||||||
@ -93,6 +96,7 @@ export const sendCakeOrderMail = async (email, order, totalPieces, totalPrice) =
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ✅ New version: Show “HST (13%) included in total”
|
||||||
const htmlContent = `
|
const htmlContent = `
|
||||||
<div style="max-width:750px;margin:30px auto;background-color:#fff;border-radius:20px;overflow:hidden;font-family:'Segoe UI',Tahoma,sans-serif;color:#333;">
|
<div style="max-width:750px;margin:30px auto;background-color:#fff;border-radius:20px;overflow:hidden;font-family:'Segoe UI',Tahoma,sans-serif;color:#333;">
|
||||||
<table width="100%" cellpadding="0" cellspacing="0" style="background-color:#faf4f0;padding:15px 25px;">
|
<table width="100%" cellpadding="0" cellspacing="0" style="background-color:#faf4f0;padding:15px 25px;">
|
||||||
@ -111,6 +115,7 @@ export const sendCakeOrderMail = async (email, order, totalPieces, totalPrice) =
|
|||||||
<img src="cid:banner" style="width:100%;max-height:300px;object-fit:cover;border-radius:0 0 20px 20px;"/>
|
<img src="cid:banner" style="width:100%;max-height:300px;object-fit:cover;border-radius:0 0 20px 20px;"/>
|
||||||
</div>
|
</div>
|
||||||
<h2 style="color:rgb(255 135 174);font-family:'Brush Script MT',cursive;font-size:22px;margin-bottom:12px;font-weight:700;">Order Details</h2>
|
<h2 style="color:rgb(255 135 174);font-family:'Brush Script MT',cursive;font-size:22px;margin-bottom:12px;font-weight:700;">Order Details</h2>
|
||||||
|
|
||||||
<table style="width:100%;border-collapse:collapse;font-size:14px;margin:0 auto 15px;">
|
<table style="width:100%;border-collapse:collapse;font-size:14px;margin:0 auto 15px;">
|
||||||
<thead>
|
<thead>
|
||||||
<tr style="background-color:#faf4f0;color:#d72631;font-weight:600;">
|
<tr style="background-color:#faf4f0;color:#d72631;font-weight:600;">
|
||||||
@ -126,13 +131,29 @@ export const sendCakeOrderMail = async (email, order, totalPieces, totalPrice) =
|
|||||||
<tr style="font-weight:bold;background-color:#f8f8f8;">
|
<tr style="font-weight:bold;background-color:#f8f8f8;">
|
||||||
<td colspan="2" style="text-align:right;padding:10px 12px;">Total Pieces</td>
|
<td colspan="2" style="text-align:right;padding:10px 12px;">Total Pieces</td>
|
||||||
<td style="padding:10px 12px;">${totalPieces}</td>
|
<td style="padding:10px 12px;">${totalPieces}</td>
|
||||||
<td style="text-align:right;padding:10px 12px;">Total Price ($)</td>
|
<td style="text-align:right;padding:10px 12px;">Subtotal ($)</td>
|
||||||
<td style="padding:10px 12px;">$${totalPrice.toFixed(2)}</td>
|
<td style="padding:10px 12px;">$${totalPrice.toFixed(2)}</td>
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
<tr style="font-weight:bold;background-color:#f8f8f8;">
|
||||||
|
<td colspan="4" style="text-align:right;padding:10px 12px;">HST (13%)</td>
|
||||||
|
<td style="padding:10px 12px;">$${hstAmount.toFixed(2)}</td>
|
||||||
|
</tr>
|
||||||
|
<tr style="font-weight:bold;background-color:#faf4f0;">
|
||||||
|
<td colspan="4" style="text-align:right;padding:10px 12px;">Grand Total (includes 13% HST)</td>
|
||||||
|
<td style="padding:10px 12px;color:#d72631;">$${grandTotal.toFixed(2)}</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
<a href="https://maisondetreats.com/" style="display:inline-block;background-color:#d72631;color:#fff;padding:10px 25px;border-radius:30px;text-decoration:none;font-weight:bold;margin-top:15px;">Visit Our Website</a>
|
<p style="font-size:13px;color:#777;margin-top:8px;">
|
||||||
|
<em>Note: Grand total includes 13% HST.</em>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<a href="https://maisondetreats.com/"
|
||||||
|
style="display:inline-block;background-color:#d72631;color:#fff;padding:10px 25px;
|
||||||
|
border-radius:30px;text-decoration:none;font-weight:bold;margin-top:15px;">
|
||||||
|
Visit Our Website
|
||||||
|
</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div style="text-align:center;padding:30px 0;background-color:#d72631;color:#fff;">
|
<div style="text-align:center;padding:30px 0;background-color:#d72631;color:#fff;">
|
||||||
@ -159,3 +180,4 @@ export const sendCakeOrderMail = async (email, order, totalPieces, totalPrice) =
|
|||||||
console.error("❌ Failed to send cake order email:", err);
|
console.error("❌ Failed to send cake order email:", err);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user