This commit introduces a new `tax.js` module containing functions for tax rate resolution and amount calculation with tax. The `amountWithTax` and `resolveTaxRate` functions are integrated into various schemas, including `invoice`, `orderitem`, `shipment`, `courierservice`, `filament`, `part`, and `product`, enhancing their ability to compute amounts with applicable taxes. Additionally, corresponding tests are added to ensure the correctness of the new tax-related functionalities.
69 lines
2.3 KiB
JavaScript
69 lines
2.3 KiB
JavaScript
import mongoose from 'mongoose';
|
|
import { generateId } from '../../utils.js';
|
|
import { taxRateModel } from '../management/taxrate.schema.js';
|
|
import { editObject, getObject } from '../../database.js';
|
|
import { amountWithTax, resolveTaxRate } from '../../tax.js';
|
|
const { Schema } = mongoose;
|
|
|
|
// Define the main part schema - cost/price and tax; override at PartSku
|
|
const partSchema = new Schema(
|
|
{
|
|
_reference: { type: String, default: () => generateId()() },
|
|
name: { type: String, required: true },
|
|
file: { type: mongoose.SchemaTypes.ObjectId, ref: 'file', required: false },
|
|
cost: { type: Number, required: false },
|
|
price: { type: Number, required: false },
|
|
priceMode: { type: String, default: 'margin' },
|
|
margin: { type: Number, required: false },
|
|
amount: { type: Number, required: false },
|
|
costTaxRate: { type: Schema.Types.ObjectId, ref: 'taxRate', required: false },
|
|
priceTaxRate: { type: Schema.Types.ObjectId, ref: 'taxRate', required: false },
|
|
costWithTax: { type: Number, required: false },
|
|
priceWithTax: { type: Number, required: false },
|
|
},
|
|
{ timestamps: true }
|
|
);
|
|
|
|
partSchema.index({ name: 'text' });
|
|
|
|
// Add virtual id getter
|
|
partSchema.virtual('id').get(function () {
|
|
return this._id;
|
|
});
|
|
|
|
// Configure JSON serialization to include virtuals
|
|
partSchema.set('toJSON', { virtuals: true });
|
|
|
|
partSchema.statics.recalculate = async function (part, user) {
|
|
const costTaxRate = await resolveTaxRate(part.costTaxRate, getObject, taxRateModel);
|
|
const priceTaxRate = await resolveTaxRate(part.priceTaxRate, getObject, taxRateModel);
|
|
const taxUpdateData = {};
|
|
|
|
if (part.cost != null) {
|
|
taxUpdateData.costWithTax = amountWithTax(part.cost, costTaxRate);
|
|
}
|
|
if (part.price != null) {
|
|
taxUpdateData.priceWithTax = amountWithTax(part.price, priceTaxRate);
|
|
}
|
|
|
|
if (Object.keys(taxUpdateData).length > 0) {
|
|
await editObject({
|
|
model: this,
|
|
id: part._id,
|
|
updateData: taxUpdateData,
|
|
user,
|
|
recalculate: false,
|
|
});
|
|
Object.assign(part, taxUpdateData);
|
|
}
|
|
|
|
const partSkuModel = mongoose.model('partSku');
|
|
const skus = await partSkuModel.find({ part: part._id }).select('_id').lean();
|
|
for (const sku of skus) {
|
|
await partSkuModel.recalculate(sku, user);
|
|
}
|
|
};
|
|
|
|
// Create and export the model
|
|
export const partModel = mongoose.model('part', partSchema);
|