api/resources/PD_Orders/invoiceModel.js
2025-02-07 18:03:54 +05:30

89 lines
1.8 KiB
JavaScript

import mongoose, { Schema } from "mongoose";
const orderItemSchema = new Schema({
productId: {
type: Schema.Types.ObjectId,
ref: "Product",
required: true,
},
SKU: {
type: String,
required: true,
},
name: {
type: String,
required: true,
},
categoryName: {
type: String, // Directly store category name
required: true,
},
brandName: {
type: String, // Directly store brand name
required: true,
},
price: {
type: Number,
required: true,
},
GST: {
type: Number,
required: true,
},
HSN_Code: {
type: Number,
required: true,
},
image: [
{
public_id: String,
url: String,
},
],
processquantity: {
//updated quantity
type: Number,
required: true,
default: 1,
},
});
const invoiceSchema = new mongoose.Schema({
invoiceId: { type: String, required: true, unique: true },
orderId: {
type: mongoose.Schema.Types.ObjectId,
ref: "PdOrder",
required: true,
},
items: [orderItemSchema],
subtotal: {
type: Number,
required: true,
},
gstTotal: {
type: Number,
required: true,
},
invoiceAmount: { type: Number, required: true },
courier_name: { type: String },
transpoter_Name: { type: String },
courier_tracking_id: { type: String },
courierStatus: {
type: String,
enum: ["processing", "dispatched", "delivered"],
default: "processing",
},
courierstatus_timeline: {
processing: { type: Date },
dispatched: { type: Date },
delivered: { type: Date },
},
});
// Middleware to set the processing date only when the invoice is created
invoiceSchema.pre("save", function (next) {
if (this.isNew && !this.courierstatus_timeline.processing) {
this.courierstatus_timeline.processing = new Date();
}
next();
});
export const Invoice = mongoose.model("Invoice", invoiceSchema);