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,
      },

regularDaysWorked: {
        type: DataTypes.DECIMAL,
      
      

      },

absentDays: {
        type: DataTypes.DECIMAL,
      
      

      },

tardinessMinutes: {
        type: DataTypes.INTEGER,
      
      

      },

overtimeHours: {
        type: DataTypes.DECIMAL,
      
      

      },

nightDiffHours: {
        type: DataTypes.DECIMAL,
      
      

      },

holidayDaysWorked: {
        type: DataTypes.DECIMAL,
      
      

      },

basicPay: {
        type: DataTypes.DECIMAL,
      
      

      },

overtimePay: {
        type: DataTypes.DECIMAL,
      
      

      },

nightDifferential: {
        type: DataTypes.DECIMAL,
      
      

      },

holidayPay: {
        type: DataTypes.DECIMAL,
      
      

      },

allowances: {
        type: DataTypes.DECIMAL,
      
      

      },

otherEarnings: {
        type: DataTypes.DECIMAL,
      
      

      },

grossPay: {
        type: DataTypes.DECIMAL,
      
      

      },

sssEmployee: {
        type: DataTypes.DECIMAL,
      
      

      },

philhealthEmployee: {
        type: DataTypes.DECIMAL,
      
      

      },

pagibigEmployee: {
        type: DataTypes.DECIMAL,
      
      

      },

withholdingTax: {
        type: DataTypes.DECIMAL,
      
      

      },

sssEmployer: {
        type: DataTypes.DECIMAL,
      
      

      },

philhealthEmployer: {
        type: DataTypes.DECIMAL,
      
      

      },

pagibigEmployer: {
        type: DataTypes.DECIMAL,
      
      

      },

sssLoanDeduction: {
        type: DataTypes.DECIMAL,
      
      

      },

pagibigLoanDeduction: {
        type: DataTypes.DECIMAL,
      
      

      },

companyLoanDeduction: {
        type: DataTypes.DECIMAL,
      
      

      },

otherDeductions: {
        type: DataTypes.DECIMAL,
      
      

      },

totalDeductions: {
        type: DataTypes.DECIMAL,
      
      

      },

netPay: {
        type: DataTypes.DECIMAL,
      
      

      },

ytdGrossPay: {
        type: DataTypes.DECIMAL,
      
      

      },

ytdWithholdingTax: {
        type: DataTypes.DECIMAL,
      
      

      },

ytdSssEmployee: {
        type: DataTypes.DECIMAL,
      
      

      },

ytdPhilhealthEmployee: {
        type: DataTypes.DECIMAL,
      
      

      },

ytdPagibigEmployee: {
        type: DataTypes.DECIMAL,
      
      

      },

ytdBasicPay: {
        type: DataTypes.DECIMAL,
      
      

      },

payslipHash: {
        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.loan_deductions, {
      as: 'loan_deductions_payrollItem',
      foreignKey: {
          name: 'payrollItemId',
      },
      constraints: false,
    });
























//end loop



    db.payroll_items.belongsTo(db.payroll_runs, {
      as: 'payrollRun',
      foreignKey: {
        name: 'payrollRunId',
      },
      constraints: false,
    });

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



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


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

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



  return payroll_items;
};


