const { DataTypes } = require("sequelize");

module.exports = (sequelize) => {
    const ImportSalesDistributor = sequelize.define(
        "importsalesdistributor",
        {
            id: {
                type: DataTypes.INTEGER,
                autoIncrement: true,
                primaryKey: true,
            },
            creation: { type: DataTypes.DATE, allowNull: false },
            modified: { type: DataTypes.DATE, allowNull: false },
            modified_by: { type: DataTypes.INTEGER, allowNull: true },
            owner: { type: DataTypes.INTEGER, allowNull: true },
            enabled: { type: DataTypes.BOOLEAN, allowNull: false }, 

            // data excel
            ip_type: { type: DataTypes.STRING, allowNull: true },
            no_dpc: { type: DataTypes.STRING, allowNull: true },
            no_invc: { type: DataTypes.STRING, allowNull: true },
            invoice_date: { type: DataTypes.DATEONLY, allowNull: true },
            ex_invc: { type: DataTypes.STRING, allowNull: true },
            no_rma: { type: DataTypes.STRING, allowNull: true },
            distr_code: { type: DataTypes.STRING, allowNull: true },
            branch_code: { type: DataTypes.STRING, allowNull: true },
            cust_name: { type: DataTypes.STRING, allowNull: true },
            cust_code: { type: DataTypes.STRING, allowNull: true },
            cust_city: { type: DataTypes.STRING, allowNull: true },
            cust_addr: { type: DataTypes.STRING, allowNull: true },
            part_num: { type: DataTypes.STRING, allowNull: true },
            part_desc: { type: DataTypes.STRING, allowNull: true },
            spec_disc: { type: DataTypes.STRING, allowNull: true },
            price: { type: DataTypes.INTEGER, allowNull: true },
            quantity: { type: DataTypes.STRING, allowNull: true },
            bonus: { type: DataTypes.INTEGER, allowNull: true },

            is_imported: { type: DataTypes.BOOLEAN, allowNull: true },
            filename: { type: DataTypes.STRING, allowNull: true },
            error_msg: { type: DataTypes.STRING, allowNull: true },

            // fk
            company_id: {type: DataTypes.INTEGER, allowNull: true, references: { model: 'companies', key: 'id'} },
            territory_id: {type: DataTypes.INTEGER, allowNull: true, references: { model: 'territories', key: 'id'} },
            distributor_id: {type: DataTypes.INTEGER, allowNull: true, references: { model: 'distributors', key: 'id'} },
            outlet_id: {type: DataTypes.INTEGER, allowNull: true, references: { model: 'outlets', key: 'id'} },
            item_id: {type: DataTypes.INTEGER, allowNull: true, references: { model: 'items', key: 'id'} },
            period_id: {type: DataTypes.INTEGER, allowNull: true, references: { model: 'periods', key: 'id'} },
        },
        {
            tableName: "importsalesdistributors",
            freezeTableName: true,
        }
    );

    ImportSalesDistributor.associate = (models) => {
        ImportSalesDistributor.belongsTo(models.Company, { foreignKey: "company_id", as: "company" });
        ImportSalesDistributor.belongsTo(models.Territory, { foreignKey: "territory_id", as: "territory" });
        ImportSalesDistributor.belongsTo(models.Distributor, { foreignKey: "distributor_id", as: "distributor" });
        ImportSalesDistributor.belongsTo(models.Outlet, { foreignKey: "outlet_id", as: "outlet" });
        ImportSalesDistributor.belongsTo(models.Item, { foreignKey: "item_id", as: "item" });
        ImportSalesDistributor.belongsTo(models.Period, { foreignKey: "period_id", as: "period" });
    };

    return ImportSalesDistributor;
};
