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

voucher_number: {
        type: DataTypes.TEXT,
      
      

      },

voucher_date: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"note",


"temporary",


"in_review",


"approved",


"posted",


"rejected"

        ],

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

total_debit: {
        type: DataTypes.DECIMAL,
      
      

      },

total_credit: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

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

  journal_vouchers.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_vouchers.hasMany(db.journal_lines, {
      as: 'journal_lines_voucher',
      foreignKey: {
          name: 'voucherId',
      },
      constraints: false,
    });




    db.journal_vouchers.hasMany(db.voucher_approvals, {
      as: 'voucher_approvals_voucher',
      foreignKey: {
          name: 'voucherId',
      },
      constraints: false,
    });


    db.journal_vouchers.hasMany(db.year_end_operations, {
      as: 'year_end_operations_generated_voucher',
      foreignKey: {
          name: 'generated_voucherId',
      },
      constraints: false,
    });










//end loop



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

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

    db.journal_vouchers.belongsTo(db.branches, {
      as: 'branch',
      foreignKey: {
        name: 'branchId',
      },
      constraints: false,
    });

    db.journal_vouchers.belongsTo(db.journal_voucher_types, {
      as: 'voucher_type',
      foreignKey: {
        name: 'voucher_typeId',
      },
      constraints: false,
    });



    db.journal_vouchers.hasMany(db.file, {
      as: 'attachments',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.journal_vouchers.getTableName(),
        belongsToColumn: 'attachments',
      },
    });


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

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



  return journal_vouchers;
};


