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

grn_number: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"received",


"posted",


"cancelled"

        ],

      },

received_at: {
        type: DataTypes.DATE,
      
      

      },

total_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

supplier_delivery_note: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  goods_receipts.associate = (db) => {


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


























    db.goods_receipts.hasMany(db.goods_receipt_lines, {
      as: 'goods_receipt_lines_goods_receipt',
      foreignKey: {
          name: 'goods_receiptId',
      },
      constraints: false,
    });


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
































//end loop



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

    db.goods_receipts.belongsTo(db.branches, {
      as: 'branch',
      foreignKey: {
        name: 'branchId',
      },
      constraints: false,
    });

    db.goods_receipts.belongsTo(db.warehouses, {
      as: 'warehouse',
      foreignKey: {
        name: 'warehouseId',
      },
      constraints: false,
    });

    db.goods_receipts.belongsTo(db.users, {
      as: 'received_by',
      foreignKey: {
        name: 'received_byId',
      },
      constraints: false,
    });

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




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

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



  return goods_receipts;
};


