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

workdays: {
        type: DataTypes.INTEGER,
      
      

      },

sick_days: {
        type: DataTypes.INTEGER,
      
      

      },

leave_days: {
        type: DataTypes.INTEGER,
      
      

      },

absent_days: {
        type: DataTypes.INTEGER,
      
      

      },

late_count: {
        type: DataTypes.INTEGER,
      
      

      },

late_minutes_total: {
        type: DataTypes.INTEGER,
      
      

      },

base_salary_monthly: {
        type: DataTypes.DECIMAL,
      
      

      },

daily_rate: {
        type: DataTypes.DECIMAL,
      
      

      },

base_salary_total: {
        type: DataTypes.DECIMAL,
      
      

      },

meal_transport_total: {
        type: DataTypes.DECIMAL,
      
      

      },

position_allowance_total: {
        type: DataTypes.DECIMAL,
      
      

      },

kpi_score: {
        type: DataTypes.DECIMAL,
      
      

      },

kpi_allowance_total: {
        type: DataTypes.DECIMAL,
      
      

      },

overtime_hours_total: {
        type: DataTypes.DECIMAL,
      
      

      },

overtime_rate: {
        type: DataTypes.DECIMAL,
      
      

      },

overtime_total: {
        type: DataTypes.DECIMAL,
      
      

      },

revenue_bonus_total: {
        type: DataTypes.DECIMAL,
      
      

      },

service_charge_total: {
        type: DataTypes.DECIMAL,
      
      

      },

deduction_total: {
        type: DataTypes.DECIMAL,
      
      

      },

take_home_pay: {
        type: DataTypes.DECIMAL,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"hr_reviewed",


"approved",


"paid"

        ],

      },

adjustment_notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  payroll_items.associate = (db) => {


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





















    db.payroll_items.hasMany(db.payroll_adjustments, {
      as: 'payroll_adjustments_payroll_item',
      foreignKey: {
          name: 'payroll_itemId',
      },
      constraints: false,
    });


    db.payroll_items.hasMany(db.payslips, {
      as: 'payslips_payroll_item',
      foreignKey: {
          name: 'payroll_itemId',
      },
      constraints: false,
    });





//end loop



    db.payroll_items.belongsTo(db.payroll_periods, {
      as: 'payroll_period',
      foreignKey: {
        name: 'payroll_periodId',
      },
      constraints: false,
    });

    db.payroll_items.belongsTo(db.employees, {
      as: 'employee',
      foreignKey: {
        name: 'employeeId',
      },
      constraints: false,
    });

    db.payroll_items.belongsTo(db.outlets, {
      as: 'outlet',
      foreignKey: {
        name: 'outletId',
      },
      constraints: false,
    });

    db.payroll_items.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });




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

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



  return payroll_items;
};


