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

entry_no: {
        type: DataTypes.TEXT,
      
      

      },

transaction_date: {
        type: DataTypes.DATE,
      
      

      },

source_module: {
        type: DataTypes.ENUM,
      
      

        values: [

"invoice",


"payment",


"vehicle_expense",


"driver_expense",


"bill",


"bill_payment",


"adjustment",


"reversal",


"opening_balance"

        ],

      },

source_ref: {
        type: DataTypes.TEXT,
      
      

      },

debit: {
        type: DataTypes.DECIMAL,
      
      

      },

credit: {
        type: DataTypes.DECIMAL,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

created_on: {
        type: DataTypes.DATE,
      
      

      },

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

  ledger_entries.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.ledger_entries.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

    db.ledger_entries.belongsTo(db.chart_of_accounts, {
      as: 'account',
      foreignKey: {
        name: 'accountId',
      },
      constraints: false,
    });

    db.ledger_entries.belongsTo(db.drivers, {
      as: 'driver',
      foreignKey: {
        name: 'driverId',
      },
      constraints: false,
    });

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

    db.ledger_entries.belongsTo(db.invoices, {
      as: 'invoice',
      foreignKey: {
        name: 'invoiceId',
      },
      constraints: false,
    });

    db.ledger_entries.belongsTo(db.payments, {
      as: 'payment',
      foreignKey: {
        name: 'paymentId',
      },
      constraints: false,
    });

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

    db.ledger_entries.belongsTo(db.bills, {
      as: 'bill',
      foreignKey: {
        name: 'billId',
      },
      constraints: false,
    });

    db.ledger_entries.belongsTo(db.ledger_entries, {
      as: 'reversal_entry',
      foreignKey: {
        name: 'reversal_entryId',
      },
      constraints: false,
    });




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

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



  return ledger_entries;
};


