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

expense_no: {
        type: DataTypes.TEXT,
      
      

      },

date: {
        type: DataTypes.DATE,
      
      

      },

expense_category: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

amount: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

gross_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"approved",


"posted",


"voided",


"reversed"

        ],

      },

created_on: {
        type: DataTypes.DATE,
      
      

      },

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

  vehicle_expenses.associate = (db) => {


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





















    db.vehicle_expenses.hasMany(db.ledger_entries, {
      as: 'ledger_entries_vehicle_expense',
      foreignKey: {
          name: 'vehicle_expenseId',
      },
      constraints: false,
    });







//end loop



    db.vehicle_expenses.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

    db.vehicle_expenses.belongsTo(db.vehicles, {
      as: 'vehicle',
      foreignKey: {
        name: 'vehicleId',
      },
      constraints: false,
    });

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

    db.vehicle_expenses.belongsTo(db.chart_of_accounts, {
      as: 'expense_account',
      foreignKey: {
        name: 'expense_accountId',
      },
      constraints: false,
    });

    db.vehicle_expenses.belongsTo(db.chart_of_accounts, {
      as: 'payment_account',
      foreignKey: {
        name: 'payment_accountId',
      },
      constraints: false,
    });



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


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

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



  return vehicle_expenses;
};


