payservedb 9.1.7 → 9.1.8

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.
Files changed (227) hide show
  1. package/.env +2 -2
  2. package/ZOHO_INTEGRATION_SCHEMA.md +644 -644
  3. package/index.js +402 -414
  4. package/package.json +17 -17
  5. package/src/models/InvoiceWithholdingTax.js +67 -67
  6. package/src/models/account.js +52 -52
  7. package/src/models/agent_departments.js +59 -59
  8. package/src/models/agent_notifications.js +53 -53
  9. package/src/models/agent_performance.js +127 -127
  10. package/src/models/agent_roles.js +77 -77
  11. package/src/models/agents.js +154 -154
  12. package/src/models/apilog.js +18 -18
  13. package/src/models/approvalsWorkflows.js +49 -49
  14. package/src/models/archivedapilog.js +18 -18
  15. package/src/models/asset.js +92 -92
  16. package/src/models/assetsAssignment.js +64 -64
  17. package/src/models/auditTrail.js +346 -346
  18. package/src/models/auto_reply_rule.js +68 -68
  19. package/src/models/bankdetails.js +47 -47
  20. package/src/models/billerAddress.js +124 -124
  21. package/src/models/booking_invoice.js +165 -165
  22. package/src/models/bookinganalytics.js +63 -63
  23. package/src/models/bookingconfig.js +45 -45
  24. package/src/models/bookingproperty.js +179 -179
  25. package/src/models/bookingreservation.js +239 -239
  26. package/src/models/bookingrevenuerecord.js +84 -84
  27. package/src/models/budget.js +95 -95
  28. package/src/models/budgetCategory.js +19 -19
  29. package/src/models/campaigns.js +108 -108
  30. package/src/models/cashpayment.js +290 -290
  31. package/src/models/combinedUnits.js +62 -62
  32. package/src/models/combined_invoice.js +424 -424
  33. package/src/models/common_area_electricity.js +38 -38
  34. package/src/models/common_area_generator.js +41 -41
  35. package/src/models/common_area_utility_alert.js +37 -37
  36. package/src/models/common_area_water.js +39 -39
  37. package/src/models/communication_status.js +33 -33
  38. package/src/models/communication_user_opt.js +32 -32
  39. package/src/models/community_guidelines.js +35 -35
  40. package/src/models/company.js +53 -53
  41. package/src/models/coreBaseSettings.js +16 -16
  42. package/src/models/coreInvoiceSettings.js +100 -100
  43. package/src/models/counter_schema.js +21 -21
  44. package/src/models/country_tax.js +42 -42
  45. package/src/models/currency_settings.js +39 -39
  46. package/src/models/customer.js +234 -234
  47. package/src/models/customer_preference.js +52 -52
  48. package/src/models/customer_satisfaction_survey.js +297 -297
  49. package/src/models/customer_surveys.js +139 -139
  50. package/src/models/customer_tickets.js +237 -237
  51. package/src/models/dailyChecklist.js +312 -312
  52. package/src/models/default_payment_details.js +17 -17
  53. package/src/models/deliveryTimeMarks.js +18 -18
  54. package/src/models/document_type.js +19 -19
  55. package/src/models/dutyRosterChecklist.js +250 -250
  56. package/src/models/dutyroster.js +136 -136
  57. package/src/models/email.js +37 -37
  58. package/src/models/email_cc_config.js +48 -48
  59. package/src/models/email_sms_queue.js +61 -61
  60. package/src/models/email_thread.js +35 -35
  61. package/src/models/entry_exit.js +53 -53
  62. package/src/models/expense.js +99 -99
  63. package/src/models/expense_category.js +45 -45
  64. package/src/models/facility.js +76 -76
  65. package/src/models/facilityBillingPrices.js +29 -29
  66. package/src/models/facilityInvoice.js +240 -240
  67. package/src/models/facilityInvoicePayment.js +52 -52
  68. package/src/models/facilityInvoiceRecipient.js +32 -32
  69. package/src/models/facilityWalletTransactionsMetadata.js +236 -236
  70. package/src/models/facility_departements.js +20 -20
  71. package/src/models/facility_etims_config.js +116 -116
  72. package/src/models/facility_payment_details.js +20 -20
  73. package/src/models/facility_rating.js +78 -78
  74. package/src/models/facilityasset.js +25 -25
  75. package/src/models/faq.js +15 -15
  76. package/src/models/gl_account_double_entries.js +25 -25
  77. package/src/models/gl_accounts.js +56 -56
  78. package/src/models/gl_entries.js +49 -49
  79. package/src/models/goodsReceivedNotes.js +115 -115
  80. package/src/models/guard.js +47 -47
  81. package/src/models/handover.js +258 -258
  82. package/src/models/inspection_category.js +38 -38
  83. package/src/models/invoice.js +528 -528
  84. package/src/models/invoiceCreditAdjustment.js +45 -45
  85. package/src/models/invoice_edit_log.js +81 -81
  86. package/src/models/invoice_generation_approval.js +86 -86
  87. package/src/models/invoicing_schedule.js +40 -40
  88. package/src/models/item_inspection.js +96 -96
  89. package/src/models/knowledge_base.js +109 -109
  90. package/src/models/knowledge_base_rating.js +44 -44
  91. package/src/models/leaseagreement.js +243 -243
  92. package/src/models/leasetemplate.js +17 -17
  93. package/src/models/levy.js +212 -212
  94. package/src/models/levy_deposits.js +126 -126
  95. package/src/models/levy_invoice_settings.js +26 -26
  96. package/src/models/levycontract.js +213 -213
  97. package/src/models/levytype.js +23 -23
  98. package/src/models/maintenance_service_vendor.js +38 -38
  99. package/src/models/maintenance_services.js +17 -17
  100. package/src/models/maintenancerequisition.js +31 -31
  101. package/src/models/master_workplan.js +32 -32
  102. package/src/models/master_workplan_child.js +34 -34
  103. package/src/models/message.js +38 -38
  104. package/src/models/module.js +21 -21
  105. package/src/models/movein_application.js +29 -29
  106. package/src/models/movein_audit_log.js +21 -21
  107. package/src/models/movein_booking.js +33 -33
  108. package/src/models/movein_commission.js +46 -46
  109. package/src/models/movein_conversation.js +25 -25
  110. package/src/models/movein_deal.js +79 -79
  111. package/src/models/movein_handoff_token.js +16 -16
  112. package/src/models/movein_landlord.js +18 -18
  113. package/src/models/movein_landlord_user.js +20 -20
  114. package/src/models/movein_message.js +27 -27
  115. package/src/models/movein_notification.js +27 -27
  116. package/src/models/movein_otp.js +14 -14
  117. package/src/models/movein_payment.js +46 -46
  118. package/src/models/movein_reminder.js +77 -77
  119. package/src/models/movein_reservation.js +31 -31
  120. package/src/models/movein_unit.js +59 -59
  121. package/src/models/movein_user.js +15 -15
  122. package/src/models/movein_viewing_slot.js +21 -21
  123. package/src/models/notification.js +44 -44
  124. package/src/models/paymentTermsMarks.js +19 -19
  125. package/src/models/penalty.js +76 -76
  126. package/src/models/pendingCredentials.js +32 -32
  127. package/src/models/powerMeterCommunicationProtocol.js +17 -17
  128. package/src/models/powerMeterCustomerAccount.js +78 -78
  129. package/src/models/powerMeterCustomerBand.js +14 -14
  130. package/src/models/powerMeterDailyReading.js +30 -30
  131. package/src/models/powerMeterGateways.js +40 -40
  132. package/src/models/powerMeterMonthlyReading.js +34 -34
  133. package/src/models/powerMeterPowerCharges.js +85 -85
  134. package/src/models/powerMeterSettings.js +211 -200
  135. package/src/models/powerMeterSingleDayReading.js +32 -32
  136. package/src/models/powerMeters.js +149 -149
  137. package/src/models/powerMetersManufacturer.js +14 -14
  138. package/src/models/power_invoice.js +371 -371
  139. package/src/models/power_meter_account.js +81 -81
  140. package/src/models/power_meter_command_logs.js +30 -30
  141. package/src/models/power_meter_command_queue.js +33 -33
  142. package/src/models/power_meter_negative_balance.js +44 -44
  143. package/src/models/power_prepaid_credits.js +47 -47
  144. package/src/models/power_prepaid_debits.js +53 -53
  145. package/src/models/power_prepaid_orders.js +78 -78
  146. package/src/models/power_sms_notification.js +26 -26
  147. package/src/models/privacy_policy.js +19 -19
  148. package/src/models/propertyManagerContract.js +556 -556
  149. package/src/models/propertyManagerRevenue.js +195 -195
  150. package/src/models/purchaseOrderInvoice.js +74 -74
  151. package/src/models/purchase_order.js +213 -213
  152. package/src/models/purchase_request.js +110 -110
  153. package/src/models/quickbooks_config.js +52 -52
  154. package/src/models/recipient_group.js +61 -61
  155. package/src/models/recipient_group_member.js +62 -62
  156. package/src/models/refresh_token.js +23 -23
  157. package/src/models/reminder.js +197 -197
  158. package/src/models/report.js +13 -13
  159. package/src/models/resident.js +121 -121
  160. package/src/models/rfq_details.js +131 -131
  161. package/src/models/rfq_response.js +153 -153
  162. package/src/models/service_charge_invoice_upload.js +42 -42
  163. package/src/models/service_charge_payments.js +27 -27
  164. package/src/models/servicerequest.js +55 -55
  165. package/src/models/settings.js +62 -62
  166. package/src/models/short_urls.js +21 -21
  167. package/src/models/smart_meter_daily_consumption.js +44 -44
  168. package/src/models/sms_africastalking.js +20 -20
  169. package/src/models/sms_balance_notification.js +26 -26
  170. package/src/models/sms_meliora.js +20 -20
  171. package/src/models/staff.js +36 -36
  172. package/src/models/stocksandspare.js +161 -161
  173. package/src/models/suppliers.js +79 -79
  174. package/src/models/terms_and_conditions.js +19 -19
  175. package/src/models/tickets.js +186 -186
  176. package/src/models/tickets_category.js +72 -72
  177. package/src/models/unitManagementTemplate.js +44 -44
  178. package/src/models/unitasset.js +25 -25
  179. package/src/models/units.js +130 -130
  180. package/src/models/user.js +186 -186
  181. package/src/models/valueaddedservices.js +21 -21
  182. package/src/models/vas_invoices_upload.js +50 -50
  183. package/src/models/vas_payments.js +24 -24
  184. package/src/models/vasinvoice.js +196 -196
  185. package/src/models/vasvendor.js +52 -52
  186. package/src/models/visitLog.js +95 -95
  187. package/src/models/visitor.js +67 -67
  188. package/src/models/waitlist.js +45 -45
  189. package/src/models/wallet.js +44 -44
  190. package/src/models/wallet_transactions.js +50 -50
  191. package/src/models/water_invoice.js +362 -362
  192. package/src/models/water_meter_Command_Queue.js +33 -33
  193. package/src/models/water_meter_account.js +86 -86
  194. package/src/models/water_meter_billing.js +58 -58
  195. package/src/models/water_meter_combined_accounts.js +92 -92
  196. package/src/models/water_meter_communication.js +17 -17
  197. package/src/models/water_meter_communication_logs.js +39 -39
  198. package/src/models/water_meter_concentrator.js +70 -70
  199. package/src/models/water_meter_daily_history.js +32 -32
  200. package/src/models/water_meter_high_risk.js +36 -36
  201. package/src/models/water_meter_iot_cards.js +34 -34
  202. package/src/models/water_meter_loan_deduction.js +134 -134
  203. package/src/models/water_meter_manufacturer.js +35 -35
  204. package/src/models/water_meter_monthly_history.js +36 -36
  205. package/src/models/water_meter_negative_amounts.js +44 -44
  206. package/src/models/water_meter_settings.js +296 -296
  207. package/src/models/water_meter_single_day_history.js +34 -34
  208. package/src/models/water_meter_size.js +15 -15
  209. package/src/models/water_meters.js +176 -176
  210. package/src/models/water_meters_delivery.js +76 -76
  211. package/src/models/water_prepaid_credit.js +47 -47
  212. package/src/models/water_prepaid_debit.js +50 -50
  213. package/src/models/whatsapp_conversation.js +23 -23
  214. package/src/models/workorder.js +49 -49
  215. package/src/models/zohoAccount.js +453 -453
  216. package/src/models/zohoIntegration.js +262 -262
  217. package/src/models/zohoItem.js +504 -504
  218. package/src/models/movein_amenity.js +0 -14
  219. package/src/models/movein_disbursement.js +0 -37
  220. package/src/models/movein_email_thread.js +0 -20
  221. package/src/models/movein_enquiry.js +0 -17
  222. package/src/models/movein_featured_package.js +0 -17
  223. package/src/models/movein_invoice.js +0 -25
  224. package/src/models/movein_lease.js +0 -38
  225. package/src/models/movein_settings.js +0 -26
  226. package/src/models/movein_tenancy.js +0 -56
  227. package/src/models/movein_want_unit.js +0 -21
@@ -1,504 +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;
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;