hst applied

This commit is contained in:
Alaguraj0361 2025-10-11 22:52:54 +05:30
parent 5eea071398
commit 95b3c6c9f5
3 changed files with 49 additions and 19 deletions

View File

@ -4,7 +4,7 @@ import { sendCakeOrderMail } from "../../utils/mailer.js";
// POST → Create new cake order
export const createCakeOrder = async (req, res) => {
try {
const { order, email, totalPieces, totalPrice } = req.body;
let { order, email, totalPieces, totalPrice, hst } = req.body;
if (!order || typeof order !== "object") {
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" });
}
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) {
sendCakeOrderMail(email, order, totalPieces, totalPrice)
sendCakeOrderMail(email, order, totalPieces, totalPrice, hst)
.then(() => console.log("Cake order email sent to", email))
.catch((err) => console.error("Email send failed:", err));
}
// ✅ Return HST in response
res.status(201).json({
message: "Cake order created successfully",
data: newOrder,

View File

@ -19,6 +19,7 @@ const CakeOrderSchema = new mongoose.Schema(
},
totalPieces: { type: Number, required: true },
totalPrice: { type: Number, required: true },
hst: { type: Number, required: false, default: 0 }, // ✅ Added HST field
},
{ timestamps: true }
);

View File

@ -63,8 +63,7 @@ export async function sendResetPasswordMail(email, token) {
}
export const sendCakeOrderMail = async (email, order, totalPieces, totalPrice) => {
export const sendCakeOrderMail = async (email, order, totalPieces, totalPrice, hst) => {
try {
const transporter = nodemailer.createTransport({
host: "mail.metatron-admin-backend.metatronhost.com",
@ -77,6 +76,10 @@ export const sendCakeOrderMail = async (email, order, totalPieces, totalPrice) =
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
let orderRows = "";
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 = `
<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;">
@ -107,10 +111,11 @@ export const sendCakeOrderMail = async (email, order, totalPieces, totalPrice) =
</table>
<div style="padding:15px 25px;text-align:center;">
<div style="text-align:center;padding:30px 0;">
<img src="cid:banner" style="width:100%;max-height:300px;object-fit:cover;border-radius:0 0 20px 20px;"/>
</div>
<div style="text-align:center;padding:30px 0;">
<img src="cid:banner" style="width:100%;max-height:300px;object-fit:cover;border-radius:0 0 20px 20px;"/>
</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>
<table style="width:100%;border-collapse:collapse;font-size:14px;margin:0 auto 15px;">
<thead>
<tr style="background-color:#faf4f0;color:#d72631;font-weight:600;">
@ -121,18 +126,34 @@ export const sendCakeOrderMail = async (email, order, totalPieces, totalPrice) =
<th style="padding:10px 12px;">Total Price</th>
</tr>
</thead>
<tbody>
${orderRows}
<tr style="font-weight:bold;background-color:#f8f8f8;">
<td colspan="2" style="text-align:right;padding:10px 12px;">Total Pieces</td>
<td style="padding:10px 12px;">${totalPieces}</td>
<td style="text-align:right;padding:10px 12px;">Total Price ($)</td>
<td style="padding:10px 12px;">$${totalPrice.toFixed(2)}</td>
</tr>
</tbody>
<tbody>
${orderRows}
<tr style="font-weight:bold;background-color:#f8f8f8;">
<td colspan="2" style="text-align:right;padding:10px 12px;">Total Pieces</td>
<td style="padding:10px 12px;">${totalPieces}</td>
<td style="text-align:right;padding:10px 12px;">Subtotal ($)</td>
<td style="padding:10px 12px;">$${totalPrice.toFixed(2)}</td>
</tr>
<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>
<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 style="text-align:center;padding:30px 0;background-color:#d72631;color:#fff;">
@ -158,4 +179,5 @@ export const sendCakeOrderMail = async (email, order, totalPieces, totalPrice) =
} catch (err) {
console.error("❌ Failed to send cake order email:", err);
}
};
};