| Current Path : /home/deltalab/PMS/partner-manager-backend/models/mongoose/ |
| Current File : //home/deltalab/PMS/partner-manager-backend/models/mongoose/invoice.js |
const mongoose = require('mongoose');
const { OrderSchema } = require('./order');
const { IndacoModuleSchema } = require('./indaco-module');
// SCHEMA ============================================
const RoyaltySchema = new mongoose.Schema(
{
orderId: String,
royaltyAmount: Number,
currency: String,
},
{
_id: false,
versionKey: false,
},
);
const WarehouseCostSchema = new mongoose.Schema({
costObject: {
type: String,
enum: [
'INBOUND',
'STORAGE',
'ORDER',
'RETURN',
],
},
warehouseId: {
type: mongoose.Schema.Types.ObjectId,
required: false, // check if true is really needed
ref: 'Warehouse',
},
warehouseName: String,
rate: Number,
total: Number,
currency: String,
});
const InvoiceSchema = new mongoose.Schema(
{
// Sum partner details
partnerId: {
type: mongoose.Schema.Types.ObjectId,
required: true,
},
// eInvoicePartnerCode: String,
partnerVatNumber: String,
companyName: String,
companyAddress: String,
// INDACO financial details
indacoCompanyName: String,
indacoVatNumber: String,
indacoAddress: String,
// Amounts with all the bits and details about the royalties, fixed costs, TAX type, etc.
orders: [OrderSchema],
royalties: [RoyaltySchema],
services: [IndacoModuleSchema],
warehouseCosts: [WarehouseCostSchema],
// The ref period of the invoice
startDate: Date,
endDate: Date,
},
{
timestamps: true,
versionKey: false,
},
);
// Orders total revenue is based on the orders list
InvoiceSchema.virtual('ordersTotal')
.set(() => {
throw new Error('cannot set orders total amount directly: it should be directly derived from the orders list');
})
.get(function () {
// sanity check
if (this.orders.length === 0) return 0;
// reduce inventory amount to a single quantity
return this.orders.reduce((total, order) => total + order.totalPriceSet.amount, 0);
});
// Royalties total is based on the calculated royalty per order and summed
InvoiceSchema.virtual('royaltiesTotal')
.set(() => {
throw new Error('cannot set royaltiesTotal amount directly: it should be directly derived from the royalties list');
})
.get(function () {
// sanity check
if (this.royalties.length === 0) return 0;
// reduce inventory amount to a single quantity
return this.royalties.reduce((total, royalty) => total + royalty.royaltyAmount, 0);
});
// Royalties total is based on the calculated royalty per order and summed
InvoiceSchema.virtual('servicesTotal')
.set(() => {
throw new Error('cannot set servicesTotal amount directly: it should be directly derived from the services list');
})
.get(function () {
// sanity check
if (this.services.length === 0) return 0;
// reduce inventory amount to a single quantity
return this.services.reduce((total, service) => total + service.moduleCost, 0);
});
// Warehouse cost total is based on the calculated royalty per order and summed
InvoiceSchema.virtual('warehouseTotal')
.set(() => {
throw new Error('cannot set warehouseTotal amount directly: it should be directly derived from the services list');
})
.get(function () {
// sanity check
if (this.warehouseCosts.length === 0) return 0;
// reduce inventory amount to a single quantity
return this.warehouseCosts.reduce((total, warehouseCost) => total + warehouseCost.total, 0);
});
// MODELS ============================================
const invoiceModel = mongoose.model('Invoice', InvoiceSchema);
const royaltyModel = mongoose.model('Royalty', RoyaltySchema);
const warehouseCostModel = mongoose.model('WarehouseCost', WarehouseCostSchema);
// EXPORTS ===========================================
module.exports = {
invoiceModel,
royaltyModel,
warehouseCostModel,
InvoiceSchema,
RoyaltySchema,
WarehouseCostSchema,
};