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

entryNumber: {
        type: DataTypes.TEXT,
      
      

      },

entryDate: {
        type: DataTypes.DATE,
      
      

      },

journalType: {
        type: DataTypes.ENUM,
      
      

        values: [

"JV_PAYROLL",


"JV_AP",


"JV_AR",


"JV_ADJUSTMENT",


"JV_CLOSING",


"JV_BANK",


"JV_REVERSAL"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"DRAFT",


"POSTED",


"REVERSED"

        ],

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

reference: {
        type: DataTypes.TEXT,
      
      

      },

postedAt: {
        type: DataTypes.DATE,
      
      

      },

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

  journal_entries.associate = (db) => {


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























    db.journal_entries.hasMany(db.payroll_runs, {
      as: 'payroll_runs_journalEntry',
      foreignKey: {
          name: 'journalEntryId',
      },
      constraints: false,
    });






    db.journal_entries.hasMany(db.journal_lines, {
      as: 'journal_lines_journalEntry',
      foreignKey: {
          name: 'journalEntryId',
      },
      constraints: false,
    });



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
















//end loop



    db.journal_entries.belongsTo(db.payroll_runs, {
      as: 'payrollRun',
      foreignKey: {
        name: 'payrollRunId',
      },
      constraints: false,
    });

    db.journal_entries.belongsTo(db.journal_entries, {
      as: 'reversalOfEntry',
      foreignKey: {
        name: 'reversalOfEntryId',
      },
      constraints: false,
    });

    db.journal_entries.belongsTo(db.users, {
      as: 'postedByUser',
      foreignKey: {
        name: 'postedByUserId',
      },
      constraints: false,
    });




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

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



  return journal_entries;
};


