module.exports = (sequelize, DataTypes) => {
  const MaterialIssueReceive = sequelize.define(
    'MaterialIssueReceive',
    {
      id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true,
      },
      company_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
      },
      plant_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
      },
      fiscal_year_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
      },
      material_issue_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
      },
      receive_number: {
        type: DataTypes.STRING(50),
        allowNull: true,
      },
      receive_date: {
        type: DataTypes.DATEONLY,
        allowNull: true,
      },
      received_by: {
        type: DataTypes.INTEGER,
        allowNull: true,
      },
      remarks: {
        type: DataTypes.TEXT,
        allowNull: true,
      },
      created_by: {
        type: DataTypes.INTEGER,
        allowNull: true,
      },
      updated_by: {
        type: DataTypes.INTEGER,
        allowNull: true,
      },
    },
    {
      tableName: 'material_issue_receive',
      timestamps: false,
    }
  );

  MaterialIssueReceive.associate = function (models) {
    MaterialIssueReceive.hasMany(models.MaterialIssueReceiveItem, {
      foreignKey: 'material_issue_receive_id',
      as: 'items',
    });
    MaterialIssueReceive.belongsTo(models.MaterialIssue, {
      foreignKey: 'material_issue_id',
      as: 'materialIssue',
    });
    MaterialIssueReceive.belongsTo(models.User, {
      foreignKey: 'received_by',
      as: 'receivedByUser',
      constraints: false,
    });
  };

  return MaterialIssueReceive;
};

