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

invoice_number: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"submitted",


"approved",


"posted",


"void"

        ],

      },

matching_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"unmatched",


"partially_matched",


"matched"

        ],

      },

invoice_date: {
        type: DataTypes.DATE,
      
      

      },

due_at: {
        type: DataTypes.DATE,
      
      

      },

subtotal: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_total: {
        type: DataTypes.DECIMAL,
      
      

      },

total_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

amount_paid: {
        type: DataTypes.DECIMAL,
      
      

      },

attachment_note: {
        type: DataTypes.TEXT,
      
      

      },

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

  supplier_invoices.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.supplier_invoices.belongsTo(db.suppliers, {
      as: 'supplier',
      foreignKey: {
        name: 'supplierId',
      },
      constraints: false,
    });

    db.supplier_invoices.belongsTo(db.purchase_orders, {
      as: 'purchase_order',
      foreignKey: {
        name: 'purchase_orderId',
      },
      constraints: false,
    });

    db.supplier_invoices.belongsTo(db.goods_receipts, {
      as: 'goods_receipt',
      foreignKey: {
        name: 'goods_receiptId',
      },
      constraints: false,
    });

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



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


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

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



  return supplier_invoices;
};


