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 ap_invoices = sequelize.define(
    'ap_invoices',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

invoiceNumber: {
        type: DataTypes.TEXT,
      
      

      },

vendorInvoiceNumber: {
        type: DataTypes.TEXT,
      
      

      },

invoiceDate: {
        type: DataTypes.DATE,
      
      

      },

receiptDate: {
        type: DataTypes.DATE,
      
      

      },

dueDate: {
        type: DataTypes.DATE,
      
      

      },

grossAmount: {
        type: DataTypes.DECIMAL,
      
      

      },

vatAmount: {
        type: DataTypes.DECIMAL,
      
      

      },

ewtAmount: {
        type: DataTypes.DECIMAL,
      
      

      },

netPayable: {
        type: DataTypes.DECIMAL,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"UNPOSTED",


"POSTED",


"PARTIALLY_PAID",


"FULLY_PAID",


"OVERDUE",


"CANCELLED"

        ],

      },

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

  ap_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.ap_invoices.hasMany(db.ap_payment_lines, {
      as: 'ap_payment_lines_invoice',
      foreignKey: {
          name: 'invoiceId',
      },
      constraints: false,
    });














//end loop



    db.ap_invoices.belongsTo(db.vendors, {
      as: 'vendor',
      foreignKey: {
        name: 'vendorId',
      },
      constraints: false,
    });

    db.ap_invoices.belongsTo(db.accounts, {
      as: 'glAccount',
      foreignKey: {
        name: 'glAccountId',
      },
      constraints: false,
    });

    db.ap_invoices.belongsTo(db.cost_centers, {
      as: 'costCenter',
      foreignKey: {
        name: 'costCenterId',
      },
      constraints: false,
    });

    db.ap_invoices.belongsTo(db.journal_entries, {
      as: 'journalEntry',
      foreignKey: {
        name: 'journalEntryId',
      },
      constraints: false,
    });




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

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



  return ap_invoices;
};


