payservedb 8.4.5 → 8.4.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +2 -0
- package/package.json +1 -1
- package/src/models/customer.js +189 -189
- package/src/models/facility.js +44 -36
- package/src/models/invoice.js +192 -100
- package/src/models/invoicing_schedule.js +6 -1
- package/src/models/zohoAccount.js +453 -0
- package/src/models/zohoItem.js +504 -0
|
@@ -0,0 +1,504 @@
|
|
|
1
|
+
const mongoose = require("mongoose");
|
|
2
|
+
|
|
3
|
+
const ZohoItemSchema = new mongoose.Schema(
|
|
4
|
+
{
|
|
5
|
+
facilityId: {
|
|
6
|
+
type: mongoose.Schema.Types.ObjectId,
|
|
7
|
+
ref: "Facility",
|
|
8
|
+
required: [true, "Facility ID is required"],
|
|
9
|
+
index: true,
|
|
10
|
+
},
|
|
11
|
+
|
|
12
|
+
// PayServe Fields
|
|
13
|
+
name: {
|
|
14
|
+
type: String,
|
|
15
|
+
required: [true, "Item name is required"],
|
|
16
|
+
trim: true,
|
|
17
|
+
maxlength: [100, "Item name cannot exceed 100 characters"],
|
|
18
|
+
index: true,
|
|
19
|
+
},
|
|
20
|
+
description: {
|
|
21
|
+
type: String,
|
|
22
|
+
trim: true,
|
|
23
|
+
maxlength: [2000, "Description cannot exceed 2000 characters"],
|
|
24
|
+
default: null,
|
|
25
|
+
},
|
|
26
|
+
category: {
|
|
27
|
+
type: String,
|
|
28
|
+
enum: [
|
|
29
|
+
"Rent",
|
|
30
|
+
"Service Charge",
|
|
31
|
+
"Utilities",
|
|
32
|
+
"Parking",
|
|
33
|
+
"Maintenance",
|
|
34
|
+
"Penalties",
|
|
35
|
+
"Deposits",
|
|
36
|
+
"Other",
|
|
37
|
+
],
|
|
38
|
+
default: "Other",
|
|
39
|
+
index: true,
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
// Pricing
|
|
43
|
+
rate: {
|
|
44
|
+
type: Number,
|
|
45
|
+
required: [true, "Item rate is required"],
|
|
46
|
+
min: [0, "Rate cannot be negative"],
|
|
47
|
+
default: 0,
|
|
48
|
+
},
|
|
49
|
+
unit: {
|
|
50
|
+
type: String,
|
|
51
|
+
trim: true,
|
|
52
|
+
default: null,
|
|
53
|
+
},
|
|
54
|
+
sku: {
|
|
55
|
+
type: String,
|
|
56
|
+
trim: true,
|
|
57
|
+
unique: true,
|
|
58
|
+
sparse: true, // Allows multiple null values
|
|
59
|
+
default: null,
|
|
60
|
+
},
|
|
61
|
+
|
|
62
|
+
// Item Type Configuration
|
|
63
|
+
itemType: {
|
|
64
|
+
type: String,
|
|
65
|
+
enum: ["sales", "purchases", "sales_and_purchases", "inventory"],
|
|
66
|
+
default: "sales",
|
|
67
|
+
required: true,
|
|
68
|
+
},
|
|
69
|
+
productType: {
|
|
70
|
+
type: String,
|
|
71
|
+
enum: [
|
|
72
|
+
"goods",
|
|
73
|
+
"service",
|
|
74
|
+
"digital_service",
|
|
75
|
+
"capital_goods",
|
|
76
|
+
"capital_service",
|
|
77
|
+
],
|
|
78
|
+
default: "service",
|
|
79
|
+
},
|
|
80
|
+
|
|
81
|
+
// Zoho Sync Fields
|
|
82
|
+
zohoItemId: {
|
|
83
|
+
type: String,
|
|
84
|
+
trim: true,
|
|
85
|
+
unique: true,
|
|
86
|
+
sparse: true,
|
|
87
|
+
default: null,
|
|
88
|
+
index: true,
|
|
89
|
+
},
|
|
90
|
+
zohoAccountId: {
|
|
91
|
+
type: String,
|
|
92
|
+
trim: true,
|
|
93
|
+
default: null,
|
|
94
|
+
index: true,
|
|
95
|
+
},
|
|
96
|
+
zohoAccountName: {
|
|
97
|
+
type: String,
|
|
98
|
+
trim: true,
|
|
99
|
+
default: null,
|
|
100
|
+
},
|
|
101
|
+
zohoPurchaseAccountId: {
|
|
102
|
+
type: String,
|
|
103
|
+
trim: true,
|
|
104
|
+
default: null,
|
|
105
|
+
},
|
|
106
|
+
zohoInventoryAccountId: {
|
|
107
|
+
type: String,
|
|
108
|
+
trim: true,
|
|
109
|
+
default: null,
|
|
110
|
+
},
|
|
111
|
+
zohoTaxId: {
|
|
112
|
+
type: String,
|
|
113
|
+
trim: true,
|
|
114
|
+
default: null,
|
|
115
|
+
},
|
|
116
|
+
zohoTaxName: {
|
|
117
|
+
type: String,
|
|
118
|
+
trim: true,
|
|
119
|
+
default: null,
|
|
120
|
+
},
|
|
121
|
+
zohoTaxPercentage: {
|
|
122
|
+
type: Number,
|
|
123
|
+
min: 0,
|
|
124
|
+
max: 100,
|
|
125
|
+
default: null,
|
|
126
|
+
},
|
|
127
|
+
|
|
128
|
+
// Tax Configuration
|
|
129
|
+
isTaxable: {
|
|
130
|
+
type: Boolean,
|
|
131
|
+
default: true,
|
|
132
|
+
},
|
|
133
|
+
taxExemptionId: {
|
|
134
|
+
type: String,
|
|
135
|
+
trim: true,
|
|
136
|
+
default: null,
|
|
137
|
+
},
|
|
138
|
+
|
|
139
|
+
// Purchase Information (for inventory items)
|
|
140
|
+
purchaseDescription: {
|
|
141
|
+
type: String,
|
|
142
|
+
trim: true,
|
|
143
|
+
maxlength: [2000, "Purchase description cannot exceed 2000 characters"],
|
|
144
|
+
default: null,
|
|
145
|
+
},
|
|
146
|
+
purchaseRate: {
|
|
147
|
+
type: Number,
|
|
148
|
+
min: [0, "Purchase rate cannot be negative"],
|
|
149
|
+
default: null,
|
|
150
|
+
},
|
|
151
|
+
|
|
152
|
+
// Inventory Management
|
|
153
|
+
reorderLevel: {
|
|
154
|
+
type: Number,
|
|
155
|
+
min: [0, "Reorder level cannot be negative"],
|
|
156
|
+
default: null,
|
|
157
|
+
},
|
|
158
|
+
initialStock: {
|
|
159
|
+
type: Number,
|
|
160
|
+
min: [0, "Initial stock cannot be negative"],
|
|
161
|
+
default: null,
|
|
162
|
+
},
|
|
163
|
+
initialStockRate: {
|
|
164
|
+
type: Number,
|
|
165
|
+
min: [0, "Initial stock rate cannot be negative"],
|
|
166
|
+
default: null,
|
|
167
|
+
},
|
|
168
|
+
|
|
169
|
+
// Country-Specific Fields
|
|
170
|
+
hsnOrSac: {
|
|
171
|
+
type: String,
|
|
172
|
+
trim: true,
|
|
173
|
+
default: null,
|
|
174
|
+
},
|
|
175
|
+
satItemKeyCode: {
|
|
176
|
+
type: String,
|
|
177
|
+
trim: true,
|
|
178
|
+
default: null,
|
|
179
|
+
},
|
|
180
|
+
unitKeyCode: {
|
|
181
|
+
type: String,
|
|
182
|
+
trim: true,
|
|
183
|
+
default: null,
|
|
184
|
+
},
|
|
185
|
+
|
|
186
|
+
// Status
|
|
187
|
+
status: {
|
|
188
|
+
type: String,
|
|
189
|
+
enum: ["active", "inactive"],
|
|
190
|
+
default: "active",
|
|
191
|
+
index: true,
|
|
192
|
+
},
|
|
193
|
+
isActive: {
|
|
194
|
+
type: Boolean,
|
|
195
|
+
default: true,
|
|
196
|
+
index: true,
|
|
197
|
+
},
|
|
198
|
+
|
|
199
|
+
// Sync Status
|
|
200
|
+
isSynced: {
|
|
201
|
+
type: Boolean,
|
|
202
|
+
default: false,
|
|
203
|
+
index: true,
|
|
204
|
+
},
|
|
205
|
+
lastSyncedAt: {
|
|
206
|
+
type: Date,
|
|
207
|
+
default: null,
|
|
208
|
+
},
|
|
209
|
+
syncStatus: {
|
|
210
|
+
type: String,
|
|
211
|
+
enum: ["pending", "syncing", "synced", "failed"],
|
|
212
|
+
default: "pending",
|
|
213
|
+
index: true,
|
|
214
|
+
},
|
|
215
|
+
syncError: {
|
|
216
|
+
type: String,
|
|
217
|
+
trim: true,
|
|
218
|
+
default: null,
|
|
219
|
+
},
|
|
220
|
+
lastSyncAttemptAt: {
|
|
221
|
+
type: Date,
|
|
222
|
+
default: null,
|
|
223
|
+
},
|
|
224
|
+
|
|
225
|
+
// Usage Statistics
|
|
226
|
+
usageCount: {
|
|
227
|
+
type: Number,
|
|
228
|
+
default: 0,
|
|
229
|
+
min: [0, "Usage count cannot be negative"],
|
|
230
|
+
},
|
|
231
|
+
lastUsedAt: {
|
|
232
|
+
type: Date,
|
|
233
|
+
default: null,
|
|
234
|
+
},
|
|
235
|
+
|
|
236
|
+
// Audit Fields
|
|
237
|
+
createdBy: {
|
|
238
|
+
type: mongoose.Schema.Types.ObjectId,
|
|
239
|
+
ref: "User",
|
|
240
|
+
default: null,
|
|
241
|
+
},
|
|
242
|
+
updatedBy: {
|
|
243
|
+
type: mongoose.Schema.Types.ObjectId,
|
|
244
|
+
ref: "User",
|
|
245
|
+
default: null,
|
|
246
|
+
},
|
|
247
|
+
lastModifiedBy: {
|
|
248
|
+
type: mongoose.Schema.Types.ObjectId,
|
|
249
|
+
ref: "User",
|
|
250
|
+
default: null,
|
|
251
|
+
},
|
|
252
|
+
|
|
253
|
+
// Custom Fields (for Zoho custom fields)
|
|
254
|
+
customFields: {
|
|
255
|
+
type: mongoose.Schema.Types.Mixed,
|
|
256
|
+
default: {},
|
|
257
|
+
},
|
|
258
|
+
|
|
259
|
+
// Metadata
|
|
260
|
+
notes: {
|
|
261
|
+
type: String,
|
|
262
|
+
trim: true,
|
|
263
|
+
maxlength: [1000, "Notes cannot exceed 1000 characters"],
|
|
264
|
+
default: null,
|
|
265
|
+
},
|
|
266
|
+
metadata: {
|
|
267
|
+
type: mongoose.Schema.Types.Mixed,
|
|
268
|
+
default: {},
|
|
269
|
+
},
|
|
270
|
+
},
|
|
271
|
+
{
|
|
272
|
+
timestamps: true,
|
|
273
|
+
}
|
|
274
|
+
);
|
|
275
|
+
|
|
276
|
+
// Compound Indexes
|
|
277
|
+
ZohoItemSchema.index({ facilityId: 1, name: 1 });
|
|
278
|
+
ZohoItemSchema.index({ facilityId: 1, status: 1 });
|
|
279
|
+
ZohoItemSchema.index({ facilityId: 1, category: 1 });
|
|
280
|
+
ZohoItemSchema.index({ facilityId: 1, isActive: 1 });
|
|
281
|
+
ZohoItemSchema.index({ facilityId: 1, isSynced: 1 });
|
|
282
|
+
ZohoItemSchema.index({ zohoItemId: 1, facilityId: 1 });
|
|
283
|
+
|
|
284
|
+
// Text index for searching
|
|
285
|
+
ZohoItemSchema.index({
|
|
286
|
+
name: "text",
|
|
287
|
+
description: "text",
|
|
288
|
+
category: "text",
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
// Virtual for sync status indicator
|
|
292
|
+
ZohoItemSchema.virtual("needsSync").get(function () {
|
|
293
|
+
if (!this.zohoItemId) return true;
|
|
294
|
+
if (!this.isSynced) return true;
|
|
295
|
+
return false;
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
// Method to mark as synced
|
|
299
|
+
ZohoItemSchema.methods.markAsSynced = function (zohoItemId, zohoData = {}) {
|
|
300
|
+
this.zohoItemId = zohoItemId;
|
|
301
|
+
this.isSynced = true;
|
|
302
|
+
this.lastSyncedAt = new Date();
|
|
303
|
+
this.syncStatus = "synced";
|
|
304
|
+
this.syncError = null;
|
|
305
|
+
|
|
306
|
+
// Update Zoho-specific fields if provided
|
|
307
|
+
if (zohoData.accountId) this.zohoAccountId = zohoData.accountId;
|
|
308
|
+
if (zohoData.accountName) this.zohoAccountName = zohoData.accountName;
|
|
309
|
+
if (zohoData.taxId) this.zohoTaxId = zohoData.taxId;
|
|
310
|
+
if (zohoData.taxName) this.zohoTaxName = zohoData.taxName;
|
|
311
|
+
if (zohoData.taxPercentage !== undefined)
|
|
312
|
+
this.zohoTaxPercentage = zohoData.taxPercentage;
|
|
313
|
+
|
|
314
|
+
return this.save();
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
// Method to mark sync as failed
|
|
318
|
+
ZohoItemSchema.methods.markSyncFailed = function (errorMessage) {
|
|
319
|
+
this.syncStatus = "failed";
|
|
320
|
+
this.syncError = errorMessage;
|
|
321
|
+
this.lastSyncAttemptAt = new Date();
|
|
322
|
+
return this.save();
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
// Method to increment usage count
|
|
326
|
+
ZohoItemSchema.methods.incrementUsage = function () {
|
|
327
|
+
this.usageCount += 1;
|
|
328
|
+
this.lastUsedAt = new Date();
|
|
329
|
+
return this.save();
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
// Method to activate item
|
|
333
|
+
ZohoItemSchema.methods.activate = function () {
|
|
334
|
+
this.status = "active";
|
|
335
|
+
this.isActive = true;
|
|
336
|
+
return this.save();
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
// Method to deactivate item
|
|
340
|
+
ZohoItemSchema.methods.deactivate = function () {
|
|
341
|
+
this.status = "inactive";
|
|
342
|
+
this.isActive = false;
|
|
343
|
+
return this.save();
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
// Static method to find items by category
|
|
347
|
+
ZohoItemSchema.statics.findByCategory = function (facilityId, category) {
|
|
348
|
+
return this.find({
|
|
349
|
+
facilityId,
|
|
350
|
+
category,
|
|
351
|
+
isActive: true,
|
|
352
|
+
}).sort({ name: 1 });
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
// Static method to find unsynced items
|
|
356
|
+
ZohoItemSchema.statics.findUnsynced = function (facilityId) {
|
|
357
|
+
return this.find({
|
|
358
|
+
facilityId,
|
|
359
|
+
$or: [{ isSynced: false }, { zohoItemId: null }],
|
|
360
|
+
isActive: true,
|
|
361
|
+
}).sort({ createdAt: 1 });
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
// Static method to search items
|
|
365
|
+
ZohoItemSchema.statics.searchItems = function (facilityId, searchText) {
|
|
366
|
+
return this.find({
|
|
367
|
+
facilityId,
|
|
368
|
+
isActive: true,
|
|
369
|
+
$text: { $search: searchText },
|
|
370
|
+
}).sort({ score: { $meta: "textScore" } });
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
// Pre-save hook to update status
|
|
374
|
+
ZohoItemSchema.pre("save", function (next) {
|
|
375
|
+
// Sync status and isActive
|
|
376
|
+
if (this.status === "active") {
|
|
377
|
+
this.isActive = true;
|
|
378
|
+
} else {
|
|
379
|
+
this.isActive = false;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// Update lastModifiedBy
|
|
383
|
+
if (this.isModified() && !this.isNew) {
|
|
384
|
+
this.updatedAt = new Date();
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
next();
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
// Pre-save validation
|
|
391
|
+
ZohoItemSchema.pre("save", function (next) {
|
|
392
|
+
// Validate tax exemption
|
|
393
|
+
if (!this.isTaxable && !this.taxExemptionId) {
|
|
394
|
+
return next(
|
|
395
|
+
new Error("Tax exemption ID is required when item is not taxable")
|
|
396
|
+
);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// Validate purchase fields for inventory items
|
|
400
|
+
if (this.itemType === "inventory" && !this.zohoInventoryAccountId) {
|
|
401
|
+
// Only validate if already synced to Zoho
|
|
402
|
+
if (this.isSynced) {
|
|
403
|
+
return next(
|
|
404
|
+
new Error("Inventory account ID is required for inventory items")
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
next();
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
// Instance method to convert to Zoho format
|
|
413
|
+
ZohoItemSchema.methods.toZohoFormat = function () {
|
|
414
|
+
const zohoItem = {
|
|
415
|
+
name: this.name,
|
|
416
|
+
rate: this.rate,
|
|
417
|
+
description: this.description || "",
|
|
418
|
+
item_type: this.itemType,
|
|
419
|
+
product_type: this.productType,
|
|
420
|
+
sku: this.sku || undefined,
|
|
421
|
+
unit: this.unit || undefined,
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
// Add account IDs if available
|
|
425
|
+
if (this.zohoAccountId) {
|
|
426
|
+
zohoItem.account_id = this.zohoAccountId;
|
|
427
|
+
}
|
|
428
|
+
if (this.zohoTaxId) {
|
|
429
|
+
zohoItem.tax_id = this.zohoTaxId;
|
|
430
|
+
}
|
|
431
|
+
if (this.zohoPurchaseAccountId) {
|
|
432
|
+
zohoItem.purchase_account_id = this.zohoPurchaseAccountId;
|
|
433
|
+
}
|
|
434
|
+
if (this.zohoInventoryAccountId) {
|
|
435
|
+
zohoItem.inventory_account_id = this.zohoInventoryAccountId;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// Add tax configuration
|
|
439
|
+
if (this.isTaxable !== undefined) {
|
|
440
|
+
zohoItem.is_taxable = this.isTaxable;
|
|
441
|
+
}
|
|
442
|
+
if (this.taxExemptionId) {
|
|
443
|
+
zohoItem.tax_exemption_id = this.taxExemptionId;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
// Add purchase information
|
|
447
|
+
if (this.purchaseDescription) {
|
|
448
|
+
zohoItem.purchase_description = this.purchaseDescription;
|
|
449
|
+
}
|
|
450
|
+
if (this.purchaseRate) {
|
|
451
|
+
zohoItem.purchase_rate = this.purchaseRate;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
// Add inventory information
|
|
455
|
+
if (this.reorderLevel) {
|
|
456
|
+
zohoItem.reorder_level = this.reorderLevel;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
// Add country-specific fields
|
|
460
|
+
if (this.hsnOrSac) {
|
|
461
|
+
zohoItem.hsn_or_sac = this.hsnOrSac;
|
|
462
|
+
}
|
|
463
|
+
if (this.satItemKeyCode) {
|
|
464
|
+
zohoItem.sat_item_key_code = this.satItemKeyCode;
|
|
465
|
+
}
|
|
466
|
+
if (this.unitKeyCode) {
|
|
467
|
+
zohoItem.unitkey_code = this.unitKeyCode;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
return zohoItem;
|
|
471
|
+
};
|
|
472
|
+
|
|
473
|
+
// Instance method to update from Zoho data
|
|
474
|
+
ZohoItemSchema.methods.updateFromZoho = function (zohoData) {
|
|
475
|
+
if (zohoData.item_id) this.zohoItemId = zohoData.item_id;
|
|
476
|
+
if (zohoData.name) this.name = zohoData.name;
|
|
477
|
+
if (zohoData.rate !== undefined) this.rate = zohoData.rate;
|
|
478
|
+
if (zohoData.description) this.description = zohoData.description;
|
|
479
|
+
if (zohoData.status) this.status = zohoData.status;
|
|
480
|
+
if (zohoData.unit) this.unit = zohoData.unit;
|
|
481
|
+
if (zohoData.sku) this.sku = zohoData.sku;
|
|
482
|
+
if (zohoData.product_type) this.productType = zohoData.product_type;
|
|
483
|
+
if (zohoData.item_type) this.itemType = zohoData.item_type;
|
|
484
|
+
|
|
485
|
+
// Update account information
|
|
486
|
+
if (zohoData.account_id) this.zohoAccountId = zohoData.account_id;
|
|
487
|
+
if (zohoData.account_name) this.zohoAccountName = zohoData.account_name;
|
|
488
|
+
|
|
489
|
+
// Update tax information
|
|
490
|
+
if (zohoData.tax_id) this.zohoTaxId = zohoData.tax_id;
|
|
491
|
+
if (zohoData.tax_name) this.zohoTaxName = zohoData.tax_name;
|
|
492
|
+
if (zohoData.tax_percentage !== undefined)
|
|
493
|
+
this.zohoTaxPercentage = parseFloat(zohoData.tax_percentage);
|
|
494
|
+
|
|
495
|
+
this.isSynced = true;
|
|
496
|
+
this.lastSyncedAt = new Date();
|
|
497
|
+
this.syncStatus = "synced";
|
|
498
|
+
|
|
499
|
+
return this.save();
|
|
500
|
+
};
|
|
501
|
+
|
|
502
|
+
const ZohoItem = mongoose.model("ZohoItem", ZohoItemSchema);
|
|
503
|
+
|
|
504
|
+
module.exports = ZohoItem;
|