feat(BE-47,48,49,50): implement inventory adjustment system

- Extend DB schema with product_warehouses and stock_logs tables
- Implement stock adjustment API (increase/decrease operations)
- Add comprehensive validation for all adjustment operations
- Implement audit log system for each adjustment with history tracking
- Include transaction handling, DTOs, seeders, and proper error handling
- Add adjustment history API with pagination and filtering

TODO: Integration testing pending
This commit is contained in:
aguhh18
2025-10-10 09:24:17 +07:00
parent a0bdc7b23c
commit 91b320d489
23 changed files with 719 additions and 502 deletions
-18
View File
@@ -1,18 +0,0 @@
package entities
import (
"time"
"gorm.io/gorm"
)
type Adjustment struct {
Id uint `gorm:"primaryKey"`
Name string `gorm:"not null;uniqueIndex:idx_name,where:deleted_at IS NULL"`
CreatedBy uint `gorm:"not null"`
CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
CreatedUser User `gorm:"foreignKey:CreatedBy;references:Id"`
}
+1 -1
View File
@@ -10,7 +10,7 @@ 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 int `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"`
+22 -15
View File
@@ -6,23 +6,30 @@ import (
"gorm.io/gorm"
)
const (
LogTypeAdjustment = "ADJUSTMENT"
)
const (
TransactionTypeIncrease = "INCREASE"
TransactionTypeDecrease = "DECREASE"
)
type StockLog struct {
Id uint `json:"id" gorm:"primaryKey;"`
TransactionType string `json:"transaction_type" gorm:"type:varchar(20);not null"`
Quantity float64 `json:"quantity" gorm:"type:numeric(15,3);not null"`
BeforeQuantity float64 `json:"before_quantity" gorm:"type:numeric(15,3);not null"`
AfterQuantity float64 `json:"after_quantity" gorm:"type:numeric(15,3);not null"`
LogType string `json:"log_type" gorm:"type:varchar(50);not null"`
LogId uint `json:"log_id" gorm:"not null"`
Note string `json:"note" gorm:"type:text"`
ProductWarehouseId uint `json:"product_warehouse_id" gorm:"not null;index"`
CreatedBy uint `json:"created_by" gorm:"not null;index"`
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
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"`
ProductWarehouse *ProductWarehouse `json:"product_warehouse,omitempty" gorm:"foreignKey:ProductWarehouseId;references:Id"`
CreatedUser *User `json:"created_user,omitempty" gorm:"foreignKey:CreatedBy;references:Id"`
}