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

name: {
        type: DataTypes.TEXT,
      
      

      },

net_days: {
        type: DataTypes.INTEGER,
      
      

      },

discount_days: {
        type: DataTypes.INTEGER,
      
      

      },

discount_percent: {
        type: DataTypes.DECIMAL,
      
      

      },

is_default: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  payment_terms.associate = (db) => {


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









    db.payment_terms.hasMany(db.customers, {
      as: 'customers_payment_term',
      foreignKey: {
          name: 'payment_termId',
      },
      constraints: false,
    });


    db.payment_terms.hasMany(db.suppliers, {
      as: 'suppliers_payment_term',
      foreignKey: {
          name: 'payment_termId',
      },
      constraints: false,
    });
























//end loop



    db.payment_terms.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });




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

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



  return payment_terms;
};


