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

invoice_no: {
        type: DataTypes.TEXT,
      
      

      },

issue_date: {
        type: DataTypes.DATE,
      
      

      },

due_date: {
        type: DataTypes.DATE,
      
      

      },

week_start: {
        type: DataTypes.DATE,
      
      

      },

week_end: {
        type: DataTypes.DATE,
      
      

      },

billing_days: {
        type: DataTypes.INTEGER,
      
      

      },

holiday_days: {
        type: DataTypes.INTEGER,
      
      

      },

billable_days: {
        type: DataTypes.INTEGER,
      
      

      },

daily_rate: {
        type: DataTypes.DECIMAL,
      
      

      },

gross_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

expense_deductions: {
        type: DataTypes.DECIMAL,
      
      

      },

net_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

amount_paid: {
        type: DataTypes.DECIMAL,
      
      

      },

balance_due: {
        type: DataTypes.DECIMAL,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"approved",


"posted",


"partially_paid",


"paid",


"voided",


"overdue"

        ],

      },

created_on: {
        type: DataTypes.DATE,
      
      

      },

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

  invoices.associate = (db) => {


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












    db.invoices.hasMany(db.invoice_lines, {
      as: 'invoice_lines_invoice',
      foreignKey: {
          name: 'invoiceId',
      },
      constraints: false,
    });



    db.invoices.hasMany(db.payment_allocations, {
      as: 'payment_allocations_invoice',
      foreignKey: {
          name: 'invoiceId',
      },
      constraints: false,
    });




    db.invoices.hasMany(db.driver_expenses, {
      as: 'driver_expenses_invoice',
      foreignKey: {
          name: 'invoiceId',
      },
      constraints: false,
    });





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







//end loop



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

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

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

    db.invoices.belongsTo(db.agreements, {
      as: 'agreement',
      foreignKey: {
        name: 'agreementId',
      },
      constraints: false,
    });




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

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



  return invoices;
};


