Spaces:
Sleeping
Sleeping
File size: 7,669 Bytes
4dbde5b 0159174 4dbde5b 0159174 ba7fcc6 0159174 4dbde5b 0159174 ba7fcc6 4dbde5b 0330133 ea9a026 0330133 ea9a026 4dbde5b ea9a026 4dbde5b 0159174 ba7fcc6 0159174 0330133 545ad7d 4dbde5b 2698900 0330133 0159174 4dbde5b 0330133 4dbde5b 0159174 ba7fcc6 0159174 4dbde5b 0330133 0159174 ba7fcc6 0159174 4dbde5b 0330133 4dbde5b 0330133 4dbde5b 2698900 4dbde5b 0330133 4dbde5b 0330133 4dbde5b 0330133 4dbde5b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | import mongoose from "mongoose";
const invoiceSchema = new mongoose.Schema(
{
tenant: {
type: mongoose.Schema.Types.ObjectId,
ref: "Tenant",
required: true,
index: true,
},
branch: {
type: mongoose.Schema.Types.ObjectId,
ref: "Branch",
default: null,
index: true,
},
invoiceNumber: {
type: String,
required: true,
trim: true,
},
customer: {
type: mongoose.Schema.Types.ObjectId,
ref: "Customer",
required: true,
},
issuedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
default: null,
},
items: [
{
name: String,
price: Number,
quantity: Number,
total: Number,
date: { type: Date, default: Date.now },
type: {
type: String,
enum: ["product", "service", "catalog"],
default: "product",
},
productId: { type: mongoose.Schema.Types.ObjectId, ref: "Product" },
serviceId: { type: mongoose.Schema.Types.ObjectId, ref: "Service" },
catalogItemId: { type: mongoose.Schema.Types.ObjectId, ref: "CatalogItem" },
},
],
subtotal: {
type: Number,
default: 0,
},
discount: {
type: Number,
default: 0,
},
taxAmount: {
type: Number,
default: 0,
},
totalAmount: {
type: Number,
default: 0,
},
payments: [
{
amount: Number,
date: { type: Date, default: Date.now },
method: {
type: String,
default: "cash",
enum: ["cash", "transfer", "partial", "unpaid"]
},
transferType: {
type: String,
enum: ["instapay", "vodafone_cash", "bank_transfer", "wallet"],
default: null
},
receivedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
default: null,
},
confirmed: {
type: Boolean,
default: false
},
confirmedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
default: null,
},
confirmedAt: Date,
},
],
paidAmount: {
type: Number,
default: 0,
},
remainingAmount: {
type: Number,
default: 0,
},
status: {
type: String,
enum: ["draft", "open", "partial", "paid", "cancelled"],
default: "open",
},
notes: {
type: String,
default: "",
},
serviceRevenue: {
type: Number,
default: 0,
},
productRevenue: {
type: Number,
default: 0,
},
catalogRevenue: {
type: Number,
default: 0,
},
hasServices: {
type: Boolean,
default: false,
},
// Restaurant/Cafe Specific Data
restaurantData: {
orderType: {
type: String,
enum: ["dine-in", "takeaway", "delivery"],
default: null,
},
table: {
type: mongoose.Schema.Types.ObjectId,
ref: "Table",
default: null,
},
guestInfo: {
name: String,
phone: String,
},
deliveryAddress: String,
driver: {
driverId: {
type: mongoose.Schema.Types.ObjectId,
ref: "Driver",
default: null,
},
driverUserId: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
default: null,
},
driverName: { type: String, default: "" },
driverPhone: { type: String, default: "" },
assignedAt: { type: Date, default: null },
// ✅ NEW: Track actual payment received from driver
paymentReceived: {
amount: { type: Number, default: 0 },
receivedAt: { type: Date, default: null },
receivedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
default: null,
},
notes: { type: String, default: "" },
},
// ✅ NEW: Track driver status when assigned
driverStatus: {
type: String,
enum: ["pending_assignment", "available", "busy", "off-duty"],
default: "pending_assignment",
},
},
},
serviceCharge: {
type: Number,
default: 0,
},
deliveryCharge: {
type: Number,
default: 0,
},
restaurantStatus: {
type: String,
enum: ["pending", "preparing", "ready_for_delivery", "shipped", "delivered"],
default: "pending",
},
isDeleted: {
type: Boolean,
default: false,
},
},
{ timestamps: true }
);
const round2 = (n) => Math.round((n));
invoiceSchema.index({ tenant: 1, branch: 1, invoiceNumber: 1 }, { unique: true });
invoiceSchema.methods.calculateTotals = function () {
this.serviceRevenue = 0;
this.productRevenue = 0;
this.catalogRevenue = 0;
this.hasServices = false;
this.items.forEach((item) => {
item.total = round2(Number(item.price || 0) * Number(item.quantity || 0));
// Track granular revenue
if (item.type === "service") {
this.serviceRevenue += item.total;
this.hasServices = true;
} else if (item.type === "catalog") {
this.catalogRevenue += item.total;
} else {
this.productRevenue += item.total;
}
});
this.subtotal = round2(
this.items.reduce((sum, item) => sum + Number(item.total || 0), 0)
);
const discount = round2(Number(this.discount || 0));
const tax = round2(Number(this.taxAmount || 0));
const serviceCharge = round2(Number(this.serviceCharge || 0));
const deliveryCharge = round2(Number(this.deliveryCharge || 0));
this.totalAmount = round2(
Math.max(this.subtotal - discount + tax + serviceCharge + deliveryCharge, 0)
);
if (this.payments?.length || this.isModified("payments")) {
this.paidAmount = round2(
this.payments.reduce((sum, payment) => {
if (payment.method === "cash") return sum + Number(payment.amount || 0);
return sum + (payment.confirmed === true ? Number(payment.amount || 0) : 0);
}, 0)
);
} else {
// ✅ IMPORTANT: If no payments exist, paidAmount must be 0 (for unpaid/آجل orders)
this.paidAmount = 0;
}
this.remainingAmount = round2(
Math.max(this.totalAmount - Number(this.paidAmount || 0), 0)
);
if (this.status === "cancelled") return;
if (this.remainingAmount <= 0) this.status = "paid";
else if (this.paidAmount > 0) this.status = "partial";
else this.status = "open";
};
invoiceSchema.pre("save", function () {
this.calculateTotals();
});
export default mongoose.model("Invoice", invoiceSchema);
|