const config = require('../../config');
const providers = config.providers;
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const moment = require('moment');

module.exports = function(sequelize, DataTypes) {
  const ar_invoices = sequelize.define(
    'ar_invoices',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

invoiceNumber: {
        type: DataTypes.TEXT,
      
      

      },

invoiceDate: {
        type: DataTypes.DATE,
      
      

      },

dueDate: {
        type: DataTypes.DATE,
      
      

      },

subtotal: {
        type: DataTypes.DECIMAL,
      
      

      },

vatAmount: {
        type: DataTypes.DECIMAL,
      
      

      },

totalAmountDue: {
        type: DataTypes.DECIMAL,
      
      

      },

collectedAmount: {
        type: DataTypes.DECIMAL,
      
      

      },

outstandingBalance: {
        type: DataTypes.DECIMAL,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"OPEN",


"PARTIALLY_COLLECTED",


"FULLY_COLLECTED",


"OVERDUE",


"WRITTEN_OFF"

        ],

      },

      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  ar_invoices.associate = (db) => {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity



































    db.ar_invoices.hasMany(db.ar_invoice_items, {
      as: 'ar_invoice_items_invoice',
      foreignKey: {
          name: 'invoiceId',
      },
      constraints: false,
    });











//end loop



    db.ar_invoices.belongsTo(db.customers, {
      as: 'customer',
      foreignKey: {
        name: 'customerId',
      },
      constraints: false,
    });




    db.ar_invoices.belongsTo(db.users, {
      as: 'createdBy',
    });

    db.ar_invoices.belongsTo(db.users, {
      as: 'updatedBy',
    });
  };



  return ar_invoices;
};


