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

order_number: {
        type: DataTypes.TEXT,
      
      

      },

order_date: {
        type: DataTypes.DATE,
      
      

      },

expected_delivery_date: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"pending",


"confirmed",


"preparing",


"shipped",


"delivered",


"canceled",


"invoiced"

        ],

      },

subtotal_ht: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_total: {
        type: DataTypes.DECIMAL,
      
      

      },

total_ttc: {
        type: DataTypes.DECIMAL,
      
      

      },

stock_reserved: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  sales_orders.associate = (db) => {


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



















    db.sales_orders.hasMany(db.sales_order_items, {
      as: 'sales_order_items_sales_order',
      foreignKey: {
          name: 'sales_orderId',
      },
      constraints: false,
    });


    db.sales_orders.hasMany(db.delivery_notes, {
      as: 'delivery_notes_sales_order',
      foreignKey: {
          name: 'sales_orderId',
      },
      constraints: false,
    });



    db.sales_orders.hasMany(db.invoices, {
      as: 'invoices_sales_order',
      foreignKey: {
          name: 'sales_orderId',
      },
      constraints: false,
    });










    db.sales_orders.hasMany(db.stock_reservations, {
      as: 'stock_reservations_sales_order',
      foreignKey: {
          name: 'sales_orderId',
      },
      constraints: false,
    });








//end loop



    db.sales_orders.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

    db.sales_orders.belongsTo(db.clients, {
      as: 'client',
      foreignKey: {
        name: 'clientId',
      },
      constraints: false,
    });

    db.sales_orders.belongsTo(db.quotes, {
      as: 'source_quote',
      foreignKey: {
        name: 'source_quoteId',
      },
      constraints: false,
    });

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

    db.sales_orders.belongsTo(db.payment_terms, {
      as: 'payment_terms',
      foreignKey: {
        name: 'payment_termsId',
      },
      constraints: false,
    });




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

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



  return sales_orders;
};


