Your IP : 216.73.217.95


Current Path : /home/deltalab/PMS/buy-button-generator/models/mongoose/
Upload File :
Current File : //home/deltalab/PMS/buy-button-generator/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'
  },
  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(function() {
    throw new Error(`cannot set orders total amount directly: it should be directly derived from the orders list`);
  })
  .get(function() {
    // sanity check
    if (0==this.orders.length) 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(function() {
    throw new Error(`cannot set royaltiesTotal amount directly: it should be directly derived from the royalties list`);
  })
  .get(function() {
    // sanity check
    if (0==this.royalties.length) 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(function() {
    throw new Error(`cannot set servicesTotal amount directly: it should be directly derived from the services list`);
  })
  .get(function() {
    // sanity check
    if (0==this.services.length) 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(function () {
    throw new Error(`cannot set warehouseTotal amount directly: it should be directly derived from the services list`);
  })
  .get(function () {
    // sanity check
    if (0 == this.warehouseCosts.length) 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
};