Spaces:
Sleeping
Sleeping
Download models/OrderModel.js from Mahmoud7456/MultiUsersSalesSystem: direct link, hf CLI and curl.
- Browser
- Download file 4.85 kB
-
https://huggingface.co/spaces/Mahmoud7456/MultiUsersSalesSystem/resolve/main/models/OrderModel.js
- Command line
-
hf download hf://spaces/Mahmoud7456/MultiUsersSalesSystem/models/OrderModel.js
-
curl -L -o OrderModel.js https://huggingface.co/spaces/Mahmoud7456/MultiUsersSalesSystem/resolve/main/models/OrderModel.js
4.85 kB
| import mongoose from "mongoose"; | |
| const orderSchema = new mongoose.Schema( | |
| { | |
| tenant: { | |
| type: mongoose.Schema.Types.ObjectId, | |
| ref: "Tenant", | |
| required: true, | |
| index: true, | |
| }, | |
| branch: { | |
| type: mongoose.Schema.Types.ObjectId, | |
| ref: "Branch", | |
| required: true, | |
| index: true, | |
| }, | |
| // 🔥 FIX: remove global unique | |
| orderNumber: { | |
| type: String, | |
| required: true, | |
| trim: true, | |
| }, | |
| customer: { | |
| type: mongoose.Schema.Types.ObjectId, | |
| ref: "Customer", | |
| default: null, | |
| }, | |
| guestInfo: { | |
| name: String, | |
| phone: String, | |
| email: String, | |
| }, | |
| orderType: { | |
| type: String, | |
| enum: ["dine-in", "takeaway", "delivery"], | |
| required: true, | |
| }, | |
| table: { | |
| type: mongoose.Schema.Types.ObjectId, | |
| ref: "Table", | |
| default: null, | |
| }, | |
| // 🔥 Items | |
| items: [ | |
| { | |
| menuItem: { | |
| type: mongoose.Schema.Types.ObjectId, | |
| ref: "MenuItem", | |
| required: true, | |
| }, | |
| name: String, | |
| quantity: { | |
| type: Number, | |
| default: 1, | |
| min: 1, | |
| }, | |
| price: { | |
| type: Number, | |
| required: true, | |
| }, | |
| notes: String, | |
| itemTotal: { | |
| type: Number, | |
| default: function () { | |
| return this.quantity * this.price; | |
| }, | |
| }, | |
| }, | |
| ], | |
| // 🔥 Financials | |
| subtotal: { | |
| type: Number, | |
| default: 0, | |
| }, | |
| tax: { | |
| type: Number, | |
| default: 0, | |
| }, | |
| discount: { | |
| type: Number, | |
| default: 0, | |
| }, | |
| total: { | |
| type: Number, | |
| required: true, | |
| }, | |
| paidAmount: { | |
| type: Number, | |
| default: 0, | |
| }, | |
| remainingAmount: { | |
| type: Number, | |
| default: function () { | |
| return this.total; | |
| }, | |
| }, | |
| paymentStatus: { | |
| type: String, | |
| enum: ["pending", "partial", "paid"], | |
| default: "pending", | |
| }, | |
| // 🔥 Order Status (Improved) | |
| status: { | |
| type: String, | |
| enum: [ | |
| "pending", | |
| "preparing", | |
| "ready", | |
| "ready_for_delivery", | |
| "shipped", | |
| "completed", | |
| "cancelled", | |
| ], | |
| default: "pending", | |
| index: true, | |
| }, | |
| // 🔥 Driver (IMPORTANT) | |
| driver: { | |
| type: mongoose.Schema.Types.ObjectId, | |
| ref: "Driver", | |
| default: null, | |
| }, | |
| deliveryInfo: { | |
| address: String, | |
| phone: String, | |
| notes: String, | |
| estimatedTime: Number, | |
| }, | |
| issuedBy: { | |
| type: mongoose.Schema.Types.ObjectId, | |
| ref: "User", | |
| default: null, | |
| }, | |
| notes: { | |
| type: String, | |
| default: "", | |
| }, | |
| // 🔥 Split Payments / Orders | |
| splitInfo: { | |
| isSplit: { | |
| type: Boolean, | |
| default: false, | |
| }, | |
| splitCount: Number, | |
| splitGroups: [ | |
| { | |
| groupId: String, | |
| items: [String], | |
| total: Number, | |
| }, | |
| ], | |
| }, | |
| completedAt: { | |
| type: Date, | |
| default: null, | |
| }, | |
| }, | |
| { | |
| timestamps: true, // ✅ correct usage | |
| } | |
| ); | |
| // 🔥 ================= INDEXES ================= | |
| // ✅ Multi-tenant safe unique | |
| orderSchema.index( | |
| { tenant: 1, branch: 1, orderNumber: 1 }, | |
| { unique: true } | |
| ); | |
| // ✅ Performance indexes | |
| orderSchema.index({ tenant: 1, status: 1 }); | |
| orderSchema.index({ tenant: 1, orderType: 1 }); | |
| orderSchema.index({ tenant: 1, createdAt: -1 }); | |
| orderSchema.index({ tenant: 1, paymentStatus: 1 }); | |
| orderSchema.index({ tenant: 1, driver: 1 }); | |
| // 🔥 ================= VALIDATION ================= | |
| orderSchema.pre("validate", function () { | |
| // dine-in لازم table | |
| if (this.orderType === "dine-in" && !this.table) { | |
| throw new Error("Table is required for dine-in orders"); | |
| } | |
| // delivery لازم address | |
| if (this.orderType === "delivery" && !this.deliveryInfo?.address) { | |
| throw new Error("Address is required for delivery orders"); | |
| } | |
| }); | |
| orderSchema.pre("save", function () { | |
| // حساب subtotal | |
| this.subtotal = this.items.reduce( | |
| (sum, item) => sum + item.quantity * item.price, | |
| 0 | |
| ); | |
| // حساب total | |
| this.total = this.subtotal + this.tax - this.discount; | |
| // remaining | |
| this.remainingAmount = this.total - this.paidAmount; | |
| // payment status | |
| if (this.paidAmount === 0) { | |
| this.paymentStatus = "pending"; | |
| } else if (this.paidAmount < this.total) { | |
| this.paymentStatus = "partial"; | |
| } else { | |
| this.paymentStatus = "paid"; | |
| } | |
| }); | |
| const Order = mongoose.model("Order", orderSchema); | |
| export default Order; | |