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

description: {
        type: DataTypes.TEXT,
      
      

      },

quantity: {
        type: DataTypes.DECIMAL,
      
      

      },

unit_price: {
        type: DataTypes.DECIMAL,
      
      

      },

discount_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

line_total: {
        type: DataTypes.DECIMAL,
      
      

      },

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

  sales_order_lines.associate = (db) => {

    db.sales_order_lines.belongsToMany(db.sales_allocations, {
      as: 'allocations',
      foreignKey: {
        name: 'sales_order_lines_allocationsId',
      },
      constraints: false,
      through: 'sales_order_linesAllocationsSales_allocations',
    });

    db.sales_order_lines.belongsToMany(db.sales_allocations, {
      as: 'allocations_filter',
      foreignKey: {
        name: 'sales_order_lines_allocationsId',
      },
      constraints: false,
      through: 'sales_order_linesAllocationsSales_allocations',
    });


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






























    db.sales_order_lines.hasMany(db.sales_allocations, {
      as: 'sales_allocations_sales_order_line',
      foreignKey: {
          name: 'sales_order_lineId',
      },
      constraints: false,
    });



























//end loop



    db.sales_order_lines.belongsTo(db.sales_orders, {
      as: 'sales_order',
      foreignKey: {
        name: 'sales_orderId',
      },
      constraints: false,
    });

    db.sales_order_lines.belongsTo(db.items, {
      as: 'item',
      foreignKey: {
        name: 'itemId',
      },
      constraints: false,
    });

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




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

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



  return sales_order_lines;
};


