codex/fix: recheck and fix purchase receive failed and farm stock not shown on recording

This commit is contained in:
Adnan Zahir
2026-04-01 11:46:44 +07:00
parent 030284a9b5
commit c4add1501d
7 changed files with 363 additions and 8 deletions
@@ -2,12 +2,14 @@ package service
import (
"context"
"errors"
"fmt"
"strings"
"time"
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
"gitlab.com/mbugroup/lti-api.git/internal/utils"
"gorm.io/gorm"
)
@@ -76,11 +78,53 @@ func resolvePurchaseFlagGroupByProductWarehouse(ctx context.Context, tx *gorm.DB
Order("rr.id ASC").
Limit(1).
Take(&selected).Error
if err == nil {
return strings.TrimSpace(selected.FlagGroupCode), nil
}
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return "", err
}
type categoryRow struct {
CategoryCode string `gorm:"column:category_code"`
}
var category categoryRow
err = tx.WithContext(ctx).
Table("product_warehouses pw").
Select("pc.code AS category_code").
Joins("JOIN products p ON p.id = pw.product_id").
Joins("JOIN product_categories pc ON pc.id = p.product_category_id").
Where("pw.id = ?", productWarehouseID).
Limit(1).
Take(&category).Error
if err != nil {
return "", err
}
return strings.TrimSpace(selected.FlagGroupCode), nil
flagGroupCode := utils.LegacyFlagGroupCodeByProductCategoryCode(category.CategoryCode)
if flagGroupCode == "" {
return "", gorm.ErrRecordNotFound
}
var matched int64
err = tx.WithContext(ctx).
Table("fifo_stock_v2_route_rules rr").
Joins("JOIN fifo_stock_v2_flag_groups fg ON fg.code = rr.flag_group_code AND fg.is_active = TRUE").
Where("rr.is_active = TRUE").
Where("rr.lane = ?", purchaseStockableLane).
Where("rr.function_code = ?", purchaseInFunctionCode).
Where("rr.source_table = ?", purchaseSourceTable).
Where("rr.flag_group_code = ?", flagGroupCode).
Count(&matched).Error
if err != nil {
return "", err
}
if matched == 0 {
return "", gorm.ErrRecordNotFound
}
return flagGroupCode, nil
}
func assignEarliestAsOf(m map[uint]time.Time, productWarehouseID uint, asOf time.Time) {
@@ -0,0 +1,80 @@
package service
import (
"context"
"testing"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
)
func TestResolvePurchaseFlagGroupByProductWarehouseFallsBackToProductCategory(t *testing.T) {
db := setupPurchaseFifoHelperTestDB(t)
ctx := context.Background()
flagGroupCode, err := resolvePurchaseFlagGroupByProductWarehouse(ctx, db, 1115)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if flagGroupCode != "PAKAN" {
t.Fatalf("expected PAKAN, got %s", flagGroupCode)
}
}
func TestResolvePurchaseFlagGroupByProductWarehouseUsesProductFlagsWhenPresent(t *testing.T) {
db := setupPurchaseFifoHelperTestDB(t)
ctx := context.Background()
flagGroupCode, err := resolvePurchaseFlagGroupByProductWarehouse(ctx, db, 2222)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if flagGroupCode != "OVK" {
t.Fatalf("expected OVK, got %s", flagGroupCode)
}
}
func setupPurchaseFifoHelperTestDB(t *testing.T) *gorm.DB {
t.Helper()
db, err := gorm.Open(sqlite.Open("file:"+t.Name()+"?mode=memory&cache=private"), &gorm.Config{})
if err != nil {
t.Fatalf("failed opening sqlite db: %v", err)
}
statements := []string{
`CREATE TABLE fifo_stock_v2_flag_groups (code TEXT PRIMARY KEY, is_active BOOLEAN NOT NULL)`,
`CREATE TABLE fifo_stock_v2_flag_members (flag_name TEXT NOT NULL, flag_group_code TEXT NOT NULL, is_active BOOLEAN NOT NULL)`,
`CREATE TABLE fifo_stock_v2_route_rules (
id INTEGER PRIMARY KEY,
flag_group_code TEXT NOT NULL,
lane TEXT NOT NULL,
function_code TEXT NOT NULL,
source_table TEXT NOT NULL,
is_active BOOLEAN NOT NULL
)`,
`CREATE TABLE product_categories (id INTEGER PRIMARY KEY, code TEXT NOT NULL)`,
`CREATE TABLE products (id INTEGER PRIMARY KEY, product_category_id INTEGER NOT NULL)`,
`CREATE TABLE flags (id INTEGER PRIMARY KEY, flagable_id INTEGER NOT NULL, flagable_type TEXT NOT NULL, name TEXT NOT NULL)`,
`CREATE TABLE product_warehouses (id INTEGER PRIMARY KEY, product_id INTEGER NOT NULL)`,
`INSERT INTO fifo_stock_v2_flag_groups (code, is_active) VALUES ('PAKAN', TRUE), ('OVK', TRUE)`,
`INSERT INTO fifo_stock_v2_flag_members (flag_name, flag_group_code, is_active) VALUES ('PAKAN', 'PAKAN', TRUE), ('OVK', 'OVK', TRUE), ('OBAT', 'OVK', TRUE)`,
`INSERT INTO fifo_stock_v2_route_rules (id, flag_group_code, lane, function_code, source_table, is_active) VALUES
(1, 'PAKAN', 'STOCKABLE', 'PURCHASE_IN', 'purchase_items', TRUE),
(2, 'OVK', 'STOCKABLE', 'PURCHASE_IN', 'purchase_items', TRUE)`,
`INSERT INTO product_categories (id, code) VALUES (1, 'RAW'), (2, 'OBT')`,
`INSERT INTO products (id, product_category_id) VALUES (37, 1), (112, 2)`,
`INSERT INTO flags (id, flagable_id, flagable_type, name) VALUES
(1, 112, 'products', 'OVK'),
(2, 112, 'products', 'OBAT')`,
`INSERT INTO product_warehouses (id, product_id) VALUES (1115, 37), (2222, 112)`,
}
for _, stmt := range statements {
if err := db.Exec(stmt).Error; err != nil {
t.Fatalf("failed preparing schema: %v", err)
}
}
return db
}