diff --git a/internal/entities/product_warehouse.go b/internal/entities/product_warehouse.go index 0c837ab1..82607723 100644 --- a/internal/entities/product_warehouse.go +++ b/internal/entities/product_warehouse.go @@ -10,11 +10,11 @@ type ProductWarehouse struct { Id uint `json:"id" gorm:"primaryKey;autoIncrement"` ProductId uint `json:"product_id" gorm:"not null"` WarehouseId uint `json:"warehouse_id" gorm:"not null"` - Quantity float64 `json:"quantity" gorm:"default:0"` + Quantity float64 `json:"quantity" gorm:"default:0"` CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"` UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"` CreatedBy uint `json:"created_by" gorm:"not null"` - DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"` + DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` // Relations Product Product `json:"product,omitempty" gorm:"foreignKey:ProductId;references:Id"` diff --git a/internal/entities/stock_log.go b/internal/entities/stock_log.go index df505aa8..21e86bd4 100644 --- a/internal/entities/stock_log.go +++ b/internal/entities/stock_log.go @@ -16,19 +16,19 @@ const ( ) type StockLog struct { - Id uint `json:"id" gorm:"primaryKey;column:id"` - TransactionType string `json:"transaction_type" gorm:"column:transaction_type;type:varchar(20);not null"` - Quantity float64 `json:"quantity" gorm:"column:quantity;type:numeric(15,3);not null"` - BeforeQuantity float64 `json:"before_quantity" gorm:"column:before_quantity;type:numeric(15,3);not null"` - AfterQuantity float64 `json:"after_quantity" gorm:"column:after_quantity;type:numeric(15,3);not null"` - LogType string `json:"log_type" gorm:"column:log_type;type:varchar(50);not null;index:stock_logs_flaggable_lookup,priority:1"` - LogId uint `json:"log_id" gorm:"column:log_id;not null;index:stock_logs_flaggable_lookup,priority:2"` - Note string `json:"note" gorm:"column:note;type:text"` - ProductWarehouseId uint `json:"product_warehouse_id" gorm:"column:product_warehouse_id;not null;index"` - CreatedBy uint `json:"created_by" gorm:"column:created_by;not null;index"` - CreatedAt time.Time `json:"created_at" gorm:"column:created_at;autoCreateTime"` - UpdatedAt time.Time `json:"updated_at" gorm:"column:updated_at;autoUpdateTime"` - DeletedAt gorm.DeletedAt `json:"deleted_at,omitempty" gorm:"column:deleted_at;index"` + Id uint `gorm:"primaryKey;column:id"` + TransactionType string `gorm:"type:varchar(20);not null"` + Quantity float64 `gorm:"type:numeric(15,3);not null"` + BeforeQuantity float64 `gorm:"type:numeric(15,3);not null"` + AfterQuantity float64 `gorm:"type:numeric(15,3);not null"` + LogType string `gorm:"type:varchar(50);not null;index:stock_logs_flaggable_lookup,priority:1"` + LogId uint `gorm:"not null;index:stock_logs_flaggable_lookup,priority:2"` + Note string `gorm:"type:text"` + ProductWarehouseId uint `gorm:"not null;index"` + CreatedBy uint `gorm:"index"` + CreatedAt time.Time `gorm:"autoCreateTime"` + UpdatedAt time.Time `gorm:"autoUpdateTime"` + DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` ProductWarehouse *ProductWarehouse `json:"product_warehouse,omitempty" gorm:"foreignKey:ProductWarehouseId;references:Id"` CreatedUser *User `json:"created_user,omitempty" gorm:"foreignKey:CreatedBy;references:Id"` diff --git a/internal/modules/inventory/adjustments/controllers/adjustment.controller.go b/internal/modules/inventory/adjustments/controllers/adjustment.controller.go index b5d275de..d152f360 100644 --- a/internal/modules/inventory/adjustments/controllers/adjustment.controller.go +++ b/internal/modules/inventory/adjustments/controllers/adjustment.controller.go @@ -48,7 +48,6 @@ func (u *AdjustmentController) AdjustmentHistory(c *fiber.Ctx) error { query := &validation.Query{ Page: c.QueryInt("page", 1), Limit: c.QueryInt("limit", 10), - Search: c.Query("search", ""), ProductID: c.QueryInt("product_id", 0), WarehouseID: c.QueryInt("warehouse_id", 0), TransactionType: c.Query("transaction_type", ""), diff --git a/internal/modules/inventory/adjustments/services/adjustment.service.go b/internal/modules/inventory/adjustments/services/adjustment.service.go index ec500e1b..252cf7a3 100644 --- a/internal/modules/inventory/adjustments/services/adjustment.service.go +++ b/internal/modules/inventory/adjustments/services/adjustment.service.go @@ -159,13 +159,24 @@ func (s *adjustmentService) AdjustmentHistory(c *fiber.Ctx, query *validation.Qu db = db.Where("log_type = ?", entity.LogTypeAdjustment) - if query.Search != "" { - db = db.Where("note ILIKE ?", "%"+query.Search+"%") - } - if query.TransactionType != "" { db = db.Where("transaction_type = ?", strings.ToUpper(query.TransactionType)) } + if query.ProductID > 0 { + db = db.Joins("JOIN product_warehouses ON product_warehouses.id = stock_logs.product_warehouse_id"). + Where("product_warehouses.product_id = ?", query.ProductID) + } + + if query.WarehouseID > 0 { + if query.ProductID > 0 { + + db = db.Where("product_warehouses.warehouse_id = ?", query.WarehouseID) + } else { + + db = db.Joins("JOIN product_warehouses ON product_warehouses.id = stock_logs.product_warehouse_id"). + Where("product_warehouses.warehouse_id = ?", query.WarehouseID) + } + } return db.Order("created_at DESC") }) @@ -175,7 +186,6 @@ func (s *adjustmentService) AdjustmentHistory(c *fiber.Ctx, query *validation.Qu return nil, 0, fiber.NewError(fiber.StatusInternalServerError, "Failed to get adjustment history") } - // Convert to pointer slice result := make([]*entity.StockLog, len(stockLogs)) for i, v := range stockLogs { result[i] = &v diff --git a/internal/modules/inventory/adjustments/validations/adjustment.validation.go b/internal/modules/inventory/adjustments/validations/adjustment.validation.go index 1245108a..7d2385cc 100644 --- a/internal/modules/inventory/adjustments/validations/adjustment.validation.go +++ b/internal/modules/inventory/adjustments/validations/adjustment.validation.go @@ -11,7 +11,6 @@ type Create struct { type Query struct { Page int `query:"page" validate:"omitempty,min=1"` Limit int `query:"limit" validate:"omitempty,min=1,max=100"` - Search string `query:"search" validate:"omitempty"` ProductID int `query:"product_id" validate:"omitempty,min=0"` WarehouseID int `query:"warehouse_id" validate:"omitempty,min=0"` TransactionType string `query:"transaction_type" validate:"omitempty,oneof=increase decrease"` diff --git a/internal/modules/master/products/validations/product.validation.go b/internal/modules/master/products/validations/product.validation.go index 3e00aa8a..70e23a74 100644 --- a/internal/modules/master/products/validations/product.validation.go +++ b/internal/modules/master/products/validations/product.validation.go @@ -32,5 +32,5 @@ type Query struct { Page int `query:"page" validate:"omitempty,number,min=1"` Limit int `query:"limit" validate:"omitempty,number,min=1"` Search string `query:"search" validate:"omitempty,max=50"` - ProductCategoryID int `query:"product_category_id" validate:"omitempty,number,min=1"` + ProductCategoryID int `query:"product_category_id" validate:"omitempty,number,min=1"` }