Implemented sales order calculations.
All checks were successful
farmcontrol/farmcontrol-ws/pipeline/head This commit looks good
All checks were successful
farmcontrol/farmcontrol-ws/pipeline/head This commit looks good
This commit is contained in:
parent
028fa9dd94
commit
ab70b6e11e
@ -1,5 +1,6 @@
|
|||||||
import mongoose from 'mongoose';
|
import mongoose from 'mongoose';
|
||||||
import { purchaseOrderModel } from './purchaseorder.schema.js';
|
import { purchaseOrderModel } from './purchaseorder.schema.js';
|
||||||
|
import { salesOrderModel } from '../sales/salesorder.schema.js';
|
||||||
import { taxRateModel } from '../management/taxrate.schema.js';
|
import { taxRateModel } from '../management/taxrate.schema.js';
|
||||||
import {
|
import {
|
||||||
aggregateRollups,
|
aggregateRollups,
|
||||||
@ -78,8 +79,7 @@ orderItemSchema.statics.history = async function (from, to) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
orderItemSchema.statics.recalculate = async function (orderItem, user) {
|
orderItemSchema.statics.recalculate = async function (orderItem, user) {
|
||||||
// Only purchase orders are supported for now
|
if (orderItem.orderType !== 'purchaseOrder' && orderItem.orderType !== 'salesOrder') {
|
||||||
if (orderItem.orderType !== 'purchaseOrder') {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -144,14 +144,15 @@ orderItemSchema.statics.recalculate = async function (orderItem, user) {
|
|||||||
const totalAmount = totals.totalAmount.sum?.toFixed(2) || 0;
|
const totalAmount = totals.totalAmount.sum?.toFixed(2) || 0;
|
||||||
const totalAmountWithTax = totals.totalAmountWithTax.sum?.toFixed(2) || 0;
|
const totalAmountWithTax = totals.totalAmountWithTax.sum?.toFixed(2) || 0;
|
||||||
|
|
||||||
const purchaseOrder = await getObject({
|
const orderModel = orderItem.orderType === 'purchaseOrder' ? purchaseOrderModel : salesOrderModel;
|
||||||
model: purchaseOrderModel,
|
const order = await getObject({
|
||||||
|
model: orderModel,
|
||||||
id: orderId,
|
id: orderId,
|
||||||
cached: true,
|
cached: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
const grandTotalAmount =
|
const grandTotalAmount =
|
||||||
parseFloat(totalAmountWithTax || 0) + parseFloat(purchaseOrder.shippingAmountWithTax || 0);
|
parseFloat(totalAmountWithTax || 0) + parseFloat(order.shippingAmountWithTax || 0);
|
||||||
|
|
||||||
var updateData = {
|
var updateData = {
|
||||||
totalAmount: parseFloat(totalAmount).toFixed(2),
|
totalAmount: parseFloat(totalAmount).toFixed(2),
|
||||||
@ -164,24 +165,36 @@ orderItemSchema.statics.recalculate = async function (orderItem, user) {
|
|||||||
const shippedCount = rollupResults.shipped.count || 0;
|
const shippedCount = rollupResults.shipped.count || 0;
|
||||||
const receivedCount = rollupResults.received.count || 0;
|
const receivedCount = rollupResults.received.count || 0;
|
||||||
|
|
||||||
|
if (orderItem.orderType === 'purchaseOrder') {
|
||||||
if (shippedCount > 0 && shippedCount < overallCount) {
|
if (shippedCount > 0 && shippedCount < overallCount) {
|
||||||
updateData = { ...updateData, state: { type: 'partiallyShipped' } };
|
updateData = { ...updateData, state: { type: 'partiallyShipped' } };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (shippedCount > 0 && shippedCount == overallCount) {
|
if (shippedCount > 0 && shippedCount == overallCount) {
|
||||||
updateData = { ...updateData, state: { type: 'shipped' } };
|
updateData = { ...updateData, state: { type: 'shipped' } };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (receivedCount > 0 && receivedCount < overallCount) {
|
if (receivedCount > 0 && receivedCount < overallCount) {
|
||||||
updateData = { ...updateData, state: { type: 'partiallyReceived' } };
|
updateData = { ...updateData, state: { type: 'partiallyReceived' } };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (receivedCount > 0 && receivedCount == overallCount) {
|
if (receivedCount > 0 && receivedCount == overallCount) {
|
||||||
updateData = { ...updateData, state: { type: 'received' } };
|
updateData = { ...updateData, state: { type: 'received' } };
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
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({
|
await editObject({
|
||||||
model: purchaseOrderModel,
|
model: orderModel,
|
||||||
id: orderId,
|
id: orderId,
|
||||||
updateData: updateData,
|
updateData: updateData,
|
||||||
user,
|
user,
|
||||||
|
|||||||
@ -2,6 +2,7 @@ import mongoose from 'mongoose';
|
|||||||
import { generateId } from '../../utils.js';
|
import { generateId } from '../../utils.js';
|
||||||
const { Schema } = mongoose;
|
const { Schema } = mongoose;
|
||||||
import { purchaseOrderModel } from './purchaseorder.schema.js';
|
import { purchaseOrderModel } from './purchaseorder.schema.js';
|
||||||
|
import { salesOrderModel } from '../sales/salesorder.schema.js';
|
||||||
import { taxRateModel } from '../management/taxrate.schema.js';
|
import { taxRateModel } from '../management/taxrate.schema.js';
|
||||||
import { aggregateRollups, editObject, getObject } from '../../database.js';
|
import { aggregateRollups, editObject, getObject } from '../../database.js';
|
||||||
|
|
||||||
@ -34,8 +35,7 @@ const shipmentSchema = new Schema(
|
|||||||
);
|
);
|
||||||
|
|
||||||
shipmentSchema.statics.recalculate = async function (shipment, user) {
|
shipmentSchema.statics.recalculate = async function (shipment, user) {
|
||||||
// Only purchase orders are supported for now
|
if (shipment.orderType !== 'purchaseOrder' && shipment.orderType !== 'salesOrder') {
|
||||||
if (shipment.orderType !== 'purchaseOrder') {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,16 +90,17 @@ shipmentSchema.statics.recalculate = async function (shipment, user) {
|
|||||||
const totalShippingAmount = totals.amount.sum?.toFixed(2) || 0;
|
const totalShippingAmount = totals.amount.sum?.toFixed(2) || 0;
|
||||||
const totalShippingAmountWithTax = totals.amountWithTax.sum?.toFixed(2) || 0;
|
const totalShippingAmountWithTax = totals.amountWithTax.sum?.toFixed(2) || 0;
|
||||||
|
|
||||||
const purchaseOrder = await getObject({
|
const orderModel = shipment.orderType === 'purchaseOrder' ? purchaseOrderModel : salesOrderModel;
|
||||||
model: purchaseOrderModel,
|
const order = await getObject({
|
||||||
|
model: orderModel,
|
||||||
id: orderId,
|
id: orderId,
|
||||||
cached: true,
|
cached: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
const grandTotalAmount =
|
const grandTotalAmount =
|
||||||
parseFloat(purchaseOrder.totalAmountWithTax || 0) + parseFloat(totalShippingAmountWithTax || 0);
|
parseFloat(order.totalAmountWithTax || 0) + parseFloat(totalShippingAmountWithTax || 0);
|
||||||
await editObject({
|
await editObject({
|
||||||
model: purchaseOrderModel,
|
model: orderModel,
|
||||||
id: orderId,
|
id: orderId,
|
||||||
updateData: {
|
updateData: {
|
||||||
shippingAmount: parseFloat(totalShippingAmount).toFixed(2),
|
shippingAmount: parseFloat(totalShippingAmount).toFixed(2),
|
||||||
|
|||||||
@ -1,7 +1,11 @@
|
|||||||
import mongoose from 'mongoose';
|
import mongoose from 'mongoose';
|
||||||
import { generateId } from '../../utils.js';
|
import { generateId } from '../../utils.js';
|
||||||
const { Schema } = mongoose;
|
const { Schema } = mongoose;
|
||||||
import { aggregateRollups, aggregateRollupsHistory } from '../../database.js';
|
import {
|
||||||
|
aggregateRollups,
|
||||||
|
aggregateRollupsHistory,
|
||||||
|
editObject,
|
||||||
|
} from '../../database.js';
|
||||||
|
|
||||||
const salesOrderSchema = new Schema(
|
const salesOrderSchema = new Schema(
|
||||||
{
|
{
|
||||||
@ -95,6 +99,109 @@ salesOrderSchema.statics.history = async function (from, to) {
|
|||||||
return results;
|
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
|
// Add virtual id getter
|
||||||
salesOrderSchema.virtual('id').get(function () {
|
salesOrderSchema.virtual('id').get(function () {
|
||||||
return this._id;
|
return this._id;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user