import mongoose from 'mongoose'; import { generateId } from '../../utils.js'; const { Schema } = mongoose; import { aggregateRollups, aggregateRollupsHistory, editObject, } from '../../database.js'; const salesOrderSchema = new Schema( { _reference: { type: String, default: () => generateId()() }, totalAmount: { type: Number, required: true, default: 0 }, totalAmountWithTax: { type: Number, required: true, default: 0 }, shippingAmount: { type: Number, required: true, default: 0 }, shippingAmountWithTax: { type: Number, required: true, default: 0 }, grandTotalAmount: { type: Number, required: true, default: 0 }, totalTaxAmount: { type: Number, required: true, default: 0 }, timestamp: { type: Date, default: Date.now }, client: { type: Schema.Types.ObjectId, ref: 'client', required: true }, state: { type: { type: String, required: true, default: 'draft' }, }, postedAt: { type: Date, required: false }, confirmedAt: { type: Date, required: false }, cancelledAt: { type: Date, required: false }, completedAt: { type: Date, required: false }, }, { timestamps: true } ); const rollupConfigs = [ { name: 'draft', filter: { 'state.type': 'draft' }, rollups: [{ name: 'draft', property: 'state.type', operation: 'count' }], }, { name: 'sent', filter: { 'state.type': 'sent' }, rollups: [{ name: 'sent', property: 'state.type', operation: 'count' }], }, { name: 'confirmed', filter: { 'state.type': 'confirmed' }, rollups: [{ name: 'confirmed', property: 'state.type', operation: 'count' }], }, { name: 'partiallyShipped', filter: { 'state.type': 'partiallyShipped' }, rollups: [{ name: 'partiallyShipped', property: 'state.type', operation: 'count' }], }, { name: 'shipped', filter: { 'state.type': 'shipped' }, rollups: [{ name: 'shipped', property: 'state.type', operation: 'count' }], }, { name: 'partiallyDelivered', filter: { 'state.type': 'partiallyDelivered' }, rollups: [{ name: 'partiallyDelivered', property: 'state.type', operation: 'count' }], }, { name: 'delivered', filter: { 'state.type': 'delivered' }, rollups: [{ name: 'delivered', property: 'state.type', operation: 'count' }], }, { name: 'cancelled', filter: { 'state.type': 'cancelled' }, rollups: [{ name: 'cancelled', property: 'state.type', operation: 'count' }], }, { name: 'completed', filter: { 'state.type': 'completed' }, rollups: [{ name: 'completed', property: 'state.type', operation: 'count' }], }, ]; salesOrderSchema.statics.stats = async function () { const results = await aggregateRollups({ model: this, rollupConfigs: rollupConfigs, }); // Transform the results to match the expected format return results; }; salesOrderSchema.statics.history = async function (from, to) { const results = await aggregateRollupsHistory({ model: this, startDate: from, endDate: to, rollupConfigs: rollupConfigs, }); // Return time-series data array return results; }; salesOrderSchema.statics.recalculate = async function (salesOrder, user) { const orderId = salesOrder._id || salesOrder; if (!orderId) { return; } const orderItemModel = mongoose.model('orderItem'); const shipmentModel = mongoose.model('shipment'); const orderIdObj = new mongoose.Types.ObjectId(orderId); const baseFilter = { order: orderIdObj, orderType: 'salesOrder' }; const orderItemRollupResults = await aggregateRollups({ model: orderItemModel, baseFilter, rollupConfigs: [ { name: 'orderTotals', rollups: [ { name: 'totalAmount', property: 'totalAmount', operation: 'sum' }, { name: 'totalAmountWithTax', property: 'totalAmountWithTax', operation: 'sum' }, ], }, { name: 'overallCount', rollups: [{ name: 'overallCount', property: '_id', operation: 'count' }], }, { name: 'shipped', filter: { 'state.type': 'shipped' }, rollups: [{ name: 'shipped', property: 'state.type', operation: 'count' }], }, { name: 'received', filter: { 'state.type': 'received' }, rollups: [{ name: 'received', property: 'state.type', operation: 'count' }], }, ], }); const shipmentRollupResults = await aggregateRollups({ model: shipmentModel, baseFilter, rollupConfigs: [ { name: 'shipmentTotals', rollups: [ { name: 'amount', property: 'amount', operation: 'sum' }, { name: 'amountWithTax', property: 'amountWithTax', operation: 'sum' }, ], }, ], }); const orderTotals = orderItemRollupResults.orderTotals || {}; const totalAmount = orderTotals.totalAmount?.sum?.toFixed(2) || 0; const totalAmountWithTax = orderTotals.totalAmountWithTax?.sum?.toFixed(2) || 0; const shipmentTotals = shipmentRollupResults.shipmentTotals || {}; const totalShippingAmount = shipmentTotals.amount?.sum?.toFixed(2) || 0; const totalShippingAmountWithTax = shipmentTotals.amountWithTax?.sum?.toFixed(2) || 0; const grandTotalAmount = parseFloat(totalAmountWithTax || 0) + parseFloat(totalShippingAmountWithTax || 0); const overallCount = orderItemRollupResults.overallCount?.count || 0; const shippedCount = orderItemRollupResults.shipped?.count || 0; const receivedCount = orderItemRollupResults.received?.count || 0; let updateData = { totalAmount: parseFloat(totalAmount).toFixed(2), totalAmountWithTax: parseFloat(totalAmountWithTax).toFixed(2), totalTaxAmount: parseFloat((totalAmountWithTax - totalAmount).toFixed(2)), shippingAmount: parseFloat(totalShippingAmount).toFixed(2), shippingAmountWithTax: parseFloat(totalShippingAmountWithTax).toFixed(2), grandTotalAmount: parseFloat(grandTotalAmount).toFixed(2), }; if (shippedCount > 0 && shippedCount < overallCount) { updateData = { ...updateData, state: { type: 'partiallyShipped' } }; } if (shippedCount > 0 && shippedCount === overallCount) { updateData = { ...updateData, state: { type: 'shipped' } }; } if (receivedCount > 0 && receivedCount < overallCount) { updateData = { ...updateData, state: { type: 'partiallyDelivered' } }; } if (receivedCount > 0 && receivedCount === overallCount) { updateData = { ...updateData, state: { type: 'delivered' } }; } await editObject({ model: this, id: orderId, updateData, user, recalculate: false, }); }; // Add virtual id getter salesOrderSchema.virtual('id').get(function () { return this._id; }); // Configure JSON serialization to include virtuals salesOrderSchema.set('toJSON', { virtuals: true }); // Create and export the model export const salesOrderModel = mongoose.model('salesOrder', salesOrderSchema);