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

title: {
        type: DataTypes.TEXT,
      
      

      },

start_date: {
        type: DataTypes.DATE,
      
      

      },

end_date: {
        type: DataTypes.DATE,
      
      

      },

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

      },

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

      },

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

  fiscal_years.associate = (db) => {


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



































    db.fiscal_years.hasMany(db.journal_vouchers, {
      as: 'journal_vouchers_fiscal_year',
      foreignKey: {
          name: 'fiscal_yearId',
      },
      constraints: false,
    });






    db.fiscal_years.hasMany(db.year_end_operations, {
      as: 'year_end_operations_fiscal_year',
      foreignKey: {
          name: 'fiscal_yearId',
      },
      constraints: false,
    });










//end loop



    db.fiscal_years.belongsTo(db.businesses, {
      as: 'business',
      foreignKey: {
        name: 'businessId',
      },
      constraints: false,
    });




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

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



  return fiscal_years;
};


