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

line_no: {
        type: DataTypes.INTEGER,
      
      

      },

debit: {
        type: DataTypes.DECIMAL,
      
      

      },

credit: {
        type: DataTypes.DECIMAL,
      
      

      },

currency_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

exchange_rate: {
        type: DataTypes.DECIMAL,
      
      

      },

line_description: {
        type: DataTypes.TEXT,
      
      

      },

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

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

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

    db.journal_lines.belongsTo(db.currencies, {
      as: 'currency',
      foreignKey: {
        name: 'currencyId',
      },
      constraints: false,
    });

    db.journal_lines.belongsTo(db.counterparties, {
      as: 'counterparty',
      foreignKey: {
        name: 'counterpartyId',
      },
      constraints: false,
    });

    db.journal_lines.belongsTo(db.cost_centers, {
      as: 'cost_center',
      foreignKey: {
        name: 'cost_centerId',
      },
      constraints: false,
    });

    db.journal_lines.belongsTo(db.projects, {
      as: 'project',
      foreignKey: {
        name: 'projectId',
      },
      constraints: false,
    });

    db.journal_lines.belongsTo(db.contracts, {
      as: 'contract',
      foreignKey: {
        name: 'contractId',
      },
      constraints: false,
    });

    db.journal_lines.belongsTo(db.analytic_dimensions, {
      as: 'analytic_level_1',
      foreignKey: {
        name: 'analytic_level_1Id',
      },
      constraints: false,
    });

    db.journal_lines.belongsTo(db.analytic_dimensions, {
      as: 'analytic_level_2',
      foreignKey: {
        name: 'analytic_level_2Id',
      },
      constraints: false,
    });

    db.journal_lines.belongsTo(db.analytic_dimensions, {
      as: 'analytic_level_3',
      foreignKey: {
        name: 'analytic_level_3Id',
      },
      constraints: false,
    });

    db.journal_lines.belongsTo(db.analytic_dimensions, {
      as: 'analytic_level_4',
      foreignKey: {
        name: 'analytic_level_4Id',
      },
      constraints: false,
    });

    db.journal_lines.belongsTo(db.analytic_dimensions, {
      as: 'analytic_level_5',
      foreignKey: {
        name: 'analytic_level_5Id',
      },
      constraints: false,
    });

    db.journal_lines.belongsTo(db.analytic_dimensions, {
      as: 'analytic_level_6',
      foreignKey: {
        name: 'analytic_level_6Id',
      },
      constraints: false,
    });

    db.journal_lines.belongsTo(db.analytic_dimensions, {
      as: 'analytic_level_7',
      foreignKey: {
        name: 'analytic_level_7Id',
      },
      constraints: false,
    });

    db.journal_lines.belongsTo(db.analytic_dimensions, {
      as: 'analytic_level_8',
      foreignKey: {
        name: 'analytic_level_8Id',
      },
      constraints: false,
    });

    db.journal_lines.belongsTo(db.analytic_dimensions, {
      as: 'analytic_level_9',
      foreignKey: {
        name: 'analytic_level_9Id',
      },
      constraints: false,
    });

    db.journal_lines.belongsTo(db.analytic_dimensions, {
      as: 'analytic_level_10',
      foreignKey: {
        name: 'analytic_level_10Id',
      },
      constraints: false,
    });




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

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



  return journal_lines;
};


