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

expense_number: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"submitted",


"approved",


"rejected",


"paid",


"void"

        ],

      },

expense_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"bill",


"purchase",


"reimbursement",


"other"

        ],

      },

expense_date: {
        type: DataTypes.DATE,
      
      

      },

due_date: {
        type: DataTypes.DATE,
      
      

      },

subtotal_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

total_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

currency_code: {
        type: DataTypes.TEXT,
      
      

      },

exchange_rate: {
        type: DataTypes.DECIMAL,
      
      

      },

memo: {
        type: DataTypes.TEXT,
      
      

      },

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

  expenses.associate = (db) => {


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




























//end loop



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

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

    db.expenses.belongsTo(db.tax_codes, {
      as: 'tax_code',
      foreignKey: {
        name: 'tax_codeId',
      },
      constraints: false,
    });



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


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

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



  return expenses;
};


