mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
Compare commits
93 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d1ba13de76 | |||
| e30ef5ef10 | |||
| bb76d27f25 | |||
| dbb13da7c4 | |||
| ac8536a4a1 | |||
| 96c2917834 | |||
| c3302397cc | |||
| c7ae836cf0 | |||
| 20f8a45823 | |||
| 67ddd8e667 | |||
| ebf0f8c5ab | |||
| 7dc5c9e9a5 | |||
| 306cf11fee | |||
| 9ee3b7582c | |||
| cdfa77566c | |||
| 1c875a916b | |||
| 85dc0ecd13 | |||
| c9633d1308 | |||
| b156e06cee | |||
| cd14de4dd2 | |||
| 54487b0fcf | |||
| a9037991ef | |||
| 12e5706318 | |||
| 3e575d96a7 | |||
| 98a34a1640 | |||
| c643e66282 | |||
| 9c3d0a44a6 | |||
| e935843cba | |||
| e33b23a2aa | |||
| c55fdb75a7 | |||
| 3a27917afc | |||
| c0132e5880 | |||
| 3d13cd966a | |||
| b41bb79125 | |||
| a2b8ebe665 | |||
| 2d8f20b70e | |||
| 824eb5905f | |||
| 817b6f82d0 | |||
| cbd3047a17 | |||
| ff4b4afcca | |||
| 240cd72204 | |||
| eae69a08fc | |||
| 17be6abc49 | |||
| ef117e66d1 | |||
| 4dfb988994 | |||
| dc726c49cf | |||
| a82df468d2 | |||
| 1af8f0a726 | |||
| 63068b8c3e | |||
| 5461c8b0ce | |||
| 5dc5f4c589 | |||
| ab9c7c216a | |||
| faa0861451 | |||
| 2eade07f0a | |||
| dbb9db960f | |||
| fa6d82b79a | |||
| 207382b3b0 | |||
| e551995c66 | |||
| cb076d92ac | |||
| f5c80fa560 | |||
| 14a4d9e944 | |||
| 84da0c27e0 | |||
| 047162699e | |||
| c95f90f0b9 | |||
| 9e0b4be4dd | |||
| f2df7f4847 | |||
| d675b1e826 | |||
| e52a02b1c0 | |||
| 096a446450 | |||
| 1b23861656 | |||
| a7069a2e50 | |||
| 3bfc401206 | |||
| 21d22c20a3 | |||
| d9a1372077 | |||
| 40f192660d | |||
| afe4b2ffe3 | |||
| eef254021c | |||
| cd739f41b9 | |||
| 8f77031e02 | |||
| 062a7937e2 | |||
| 4094d38d7b | |||
| d5bc6838c8 | |||
| efaeb89ca1 | |||
| a0a143b8ac | |||
| cbb3368141 | |||
| fc49cef781 | |||
| c79e35c217 | |||
| b8425c0f58 | |||
| 0de2021308 | |||
| c062d838e0 | |||
| 2dd3e3e271 | |||
| 2effa08648 | |||
| e6094528b5 |
@@ -3,7 +3,7 @@ root = "."
|
|||||||
tmp_dir = "tmp"
|
tmp_dir = "tmp"
|
||||||
|
|
||||||
[build]
|
[build]
|
||||||
cmd = "go build -o ./tmp/main ./cmd/api"
|
cmd = "go build -buildvcs=false -o ./tmp/main ./cmd/api"
|
||||||
bin = "tmp/main"
|
bin = "tmp/main"
|
||||||
full_bin = "APP_ENV=dev ./tmp/main"
|
full_bin = "APP_ENV=dev ./tmp/main"
|
||||||
include_ext = ["go", "tpl", "tmpl", "html"]
|
include_ext = ["go", "tpl", "tmpl", "html"]
|
||||||
|
|||||||
@@ -1,44 +0,0 @@
|
|||||||
package capabilities
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
permission "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
|
||||||
)
|
|
||||||
|
|
||||||
// FromPermissions returns a filtered map of capabilities that the frontend can use
|
|
||||||
// to toggle features. Only permissions recognized by the application are exposed.
|
|
||||||
func FromPermissions(perms []string) map[string]bool {
|
|
||||||
if len(perms) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
out := make(map[string]bool)
|
|
||||||
for _, perm := range perms {
|
|
||||||
if key, ok := normalizeAndAllow(perm); ok {
|
|
||||||
out[key] = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(out) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
func normalizeAndAllow(perm string) (string, bool) {
|
|
||||||
perm = strings.ToLower(strings.TrimSpace(perm))
|
|
||||||
if perm == "" {
|
|
||||||
return "", false
|
|
||||||
}
|
|
||||||
if _, ok := allowed[perm]; !ok {
|
|
||||||
return "", false
|
|
||||||
}
|
|
||||||
return perm, true
|
|
||||||
}
|
|
||||||
|
|
||||||
var allowed = map[string]struct{}{
|
|
||||||
permission.PermissionRecordingRead: {},
|
|
||||||
permission.PermissionRecordingCreate: {},
|
|
||||||
permission.PermissionRecordingUpdate: {},
|
|
||||||
permission.PermissionRecordingDelete: {},
|
|
||||||
}
|
|
||||||
@@ -84,8 +84,9 @@ func (r *approvalRepositoryImpl) LatestByTargets(
|
|||||||
result := make(map[uint]entity.Approval, len(approvableIDs))
|
result := make(map[uint]entity.Approval, len(approvableIDs))
|
||||||
|
|
||||||
q := r.DB().WithContext(ctx).
|
q := r.DB().WithContext(ctx).
|
||||||
|
Select("DISTINCT ON (approvable_id) *").
|
||||||
Where("approvable_type = ? AND approvable_id IN ?", workflow, approvableIDs).
|
Where("approvable_type = ? AND approvable_id IN ?", workflow, approvableIDs).
|
||||||
Order("action_at DESC")
|
Order("approvable_id, action_at DESC")
|
||||||
|
|
||||||
if modifier != nil {
|
if modifier != nil {
|
||||||
q = modifier(q)
|
q = modifier(q)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package repository
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@@ -9,45 +10,59 @@ import (
|
|||||||
|
|
||||||
// Exists reports whether a record with the given ID exists for type T.
|
// Exists reports whether a record with the given ID exists for type T.
|
||||||
func Exists[T any](ctx context.Context, db *gorm.DB, id uint) (bool, error) {
|
func Exists[T any](ctx context.Context, db *gorm.DB, id uint) (bool, error) {
|
||||||
var count int64
|
var marker int
|
||||||
if err := db.WithContext(ctx).
|
err := db.WithContext(ctx).
|
||||||
Model(new(T)).
|
Model(new(T)).
|
||||||
|
Select("1").
|
||||||
Where("id = ?", id).
|
Where("id = ?", id).
|
||||||
Count(&count).Error; err != nil {
|
Limit(1).
|
||||||
|
Take(&marker).Error
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
return count > 0, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExistsByName[T any](ctx context.Context, db *gorm.DB, name string, excludeID *uint) (bool, error) {
|
func ExistsByName[T any](ctx context.Context, db *gorm.DB, name string, excludeID *uint) (bool, error) {
|
||||||
var count int64
|
|
||||||
q := db.WithContext(ctx).
|
q := db.WithContext(ctx).
|
||||||
Model(new(T)).
|
Model(new(T)).
|
||||||
|
Select("1").
|
||||||
Where("name = ?", name).
|
Where("name = ?", name).
|
||||||
Where("deleted_at IS NULL")
|
Where("deleted_at IS NULL")
|
||||||
if excludeID != nil {
|
if excludeID != nil {
|
||||||
q = q.Where("id <> ?", *excludeID)
|
q = q.Where("id <> ?", *excludeID)
|
||||||
}
|
}
|
||||||
if err := q.Count(&count).Error; err != nil {
|
var marker int
|
||||||
|
if err := q.Limit(1).Take(&marker).Error; err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
return count > 0, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExistsByField[T any](ctx context.Context, db *gorm.DB, field string, value any, excludeID *uint) (bool, error) {
|
func ExistsByField[T any](ctx context.Context, db *gorm.DB, field string, value any, excludeID *uint) (bool, error) {
|
||||||
if field == "" {
|
if field == "" {
|
||||||
return false, fmt.Errorf("field is required")
|
return false, fmt.Errorf("field is required")
|
||||||
}
|
}
|
||||||
var count int64
|
|
||||||
q := db.WithContext(ctx).
|
q := db.WithContext(ctx).
|
||||||
Model(new(T)).
|
Model(new(T)).
|
||||||
|
Select("1").
|
||||||
Where(fmt.Sprintf("%s = ?", field), value).
|
Where(fmt.Sprintf("%s = ?", field), value).
|
||||||
Where("deleted_at IS NULL")
|
Where("deleted_at IS NULL")
|
||||||
if excludeID != nil {
|
if excludeID != nil {
|
||||||
q = q.Where("id <> ?", *excludeID)
|
q = q.Where("id <> ?", *excludeID)
|
||||||
}
|
}
|
||||||
if err := q.Count(&count).Error; err != nil {
|
var marker int
|
||||||
|
if err := q.Limit(1).Take(&marker).Error; err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
return count > 0, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -505,12 +505,25 @@ func (s *fifoService) fetchStockLots(ctx context.Context, tx *gorm.DB, productWa
|
|||||||
|
|
||||||
var lots []stockLot
|
var lots []stockLot
|
||||||
for key, cfg := range configs {
|
for key, cfg := range configs {
|
||||||
selectStmt := fmt.Sprintf(
|
|
||||||
"%s AS id, %s AS available_qty, %s AS created_at",
|
usesNumericTime := cfg.Columns.CreatedAt == cfg.Columns.ID
|
||||||
cfg.Columns.ID,
|
|
||||||
fmt.Sprintf("%s - COALESCE(%s,0)", cfg.Columns.TotalQuantity, cfg.Columns.TotalUsedQuantity),
|
var selectStmt string
|
||||||
cfg.Columns.CreatedAt,
|
if usesNumericTime {
|
||||||
)
|
|
||||||
|
selectStmt = fmt.Sprintf(
|
||||||
|
"%s AS id, %s AS available_qty, '1970-01-01 00:00:00 UTC'::timestamp AS created_at",
|
||||||
|
cfg.Columns.ID,
|
||||||
|
fmt.Sprintf("%s - COALESCE(%s,0)", cfg.Columns.TotalQuantity, cfg.Columns.TotalUsedQuantity),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
selectStmt = fmt.Sprintf(
|
||||||
|
"%s AS id, %s AS available_qty, %s AS created_at",
|
||||||
|
cfg.Columns.ID,
|
||||||
|
fmt.Sprintf("%s - COALESCE(%s,0)", cfg.Columns.TotalQuantity, cfg.Columns.TotalUsedQuantity),
|
||||||
|
cfg.Columns.CreatedAt,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
var rows []struct {
|
var rows []struct {
|
||||||
ID uint
|
ID uint
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
DROP INDEX IF EXISTS idx_payments_bank_id;
|
||||||
|
DROP INDEX IF EXISTS payments_party_polymorphic;
|
||||||
|
DROP TABLE IF EXISTS payments;
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
CREATE TABLE IF NOT EXISTS payments (
|
||||||
|
id BIGSERIAL PRIMARY KEY,
|
||||||
|
payment_code VARCHAR(50) NOT NULL,
|
||||||
|
reference_number VARCHAR(100) NULL,
|
||||||
|
transaction_type VARCHAR(50),
|
||||||
|
party_type VARCHAR(50) NOT NULL,
|
||||||
|
party_id BIGINT NOT NULL,
|
||||||
|
payment_date TIMESTAMPTZ NOT NULL,
|
||||||
|
payment_method VARCHAR(20) NOT NULL,
|
||||||
|
bank_id BIGINT NULL REFERENCES banks(id) ON DELETE RESTRICT ON UPDATE CASCADE,
|
||||||
|
direction VARCHAR(5) NOT NULL,
|
||||||
|
nominal NUMERIC(15, 3) NOT NULL,
|
||||||
|
notes TEXT NOT NULL,
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
deleted_at TIMESTAMPTZ NULL,
|
||||||
|
created_by BIGINT REFERENCES users (id) ON DELETE SET NULL ON UPDATE CASCADE
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Indexes
|
||||||
|
CREATE INDEX payments_party_polymorphic ON payments (party_type, party_id);
|
||||||
|
CREATE INDEX idx_payments_bank_id ON payments (bank_id);
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
DO $$
|
||||||
|
DECLARE
|
||||||
|
r record;
|
||||||
|
trigger_name text;
|
||||||
|
BEGIN
|
||||||
|
FOR r IN
|
||||||
|
SELECT table_schema, table_name
|
||||||
|
FROM information_schema.columns
|
||||||
|
WHERE column_name = 'deleted_at'
|
||||||
|
AND table_schema = 'public'
|
||||||
|
GROUP BY table_schema, table_name
|
||||||
|
LOOP
|
||||||
|
trigger_name := format('trg_soft_delete_fk_%s', r.table_name);
|
||||||
|
EXECUTE format('DROP TRIGGER IF EXISTS %I ON %I.%I', trigger_name, r.table_schema, r.table_name);
|
||||||
|
END LOOP;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
DROP FUNCTION IF EXISTS soft_delete_handle_fk();
|
||||||
@@ -0,0 +1,126 @@
|
|||||||
|
CREATE OR REPLACE FUNCTION soft_delete_handle_fk() RETURNS TRIGGER AS $$
|
||||||
|
DECLARE
|
||||||
|
fk record;
|
||||||
|
child_column text;
|
||||||
|
parent_column text;
|
||||||
|
parent_value text;
|
||||||
|
child_has_deleted_at boolean;
|
||||||
|
ref_exists boolean;
|
||||||
|
sql text;
|
||||||
|
BEGIN
|
||||||
|
IF OLD.deleted_at IS NULL AND NEW.deleted_at IS NOT NULL THEN
|
||||||
|
FOR fk IN
|
||||||
|
SELECT conrelid::regclass AS child_table,
|
||||||
|
conkey AS child_cols,
|
||||||
|
confkey AS parent_cols,
|
||||||
|
confdeltype
|
||||||
|
FROM pg_constraint
|
||||||
|
WHERE contype = 'f'
|
||||||
|
AND confrelid = TG_RELID
|
||||||
|
LOOP
|
||||||
|
IF array_length(fk.child_cols, 1) IS DISTINCT FROM 1
|
||||||
|
OR array_length(fk.parent_cols, 1) IS DISTINCT FROM 1 THEN
|
||||||
|
RAISE NOTICE 'soft_delete_handle_fk skipped composite fk on %', fk.child_table;
|
||||||
|
CONTINUE;
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
SELECT attname INTO child_column
|
||||||
|
FROM pg_attribute
|
||||||
|
WHERE attrelid = fk.child_table
|
||||||
|
AND attnum = fk.child_cols[1]
|
||||||
|
AND NOT attisdropped;
|
||||||
|
|
||||||
|
SELECT attname INTO parent_column
|
||||||
|
FROM pg_attribute
|
||||||
|
WHERE attrelid = TG_RELID
|
||||||
|
AND attnum = fk.parent_cols[1]
|
||||||
|
AND NOT attisdropped;
|
||||||
|
|
||||||
|
EXECUTE format('SELECT ($1).%I', parent_column)
|
||||||
|
INTO parent_value
|
||||||
|
USING OLD;
|
||||||
|
|
||||||
|
SELECT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM pg_attribute
|
||||||
|
WHERE attrelid = fk.child_table
|
||||||
|
AND attname = 'deleted_at'
|
||||||
|
AND NOT attisdropped
|
||||||
|
) INTO child_has_deleted_at;
|
||||||
|
|
||||||
|
IF fk.confdeltype IN ('r', 'a') THEN
|
||||||
|
sql := format(
|
||||||
|
'SELECT EXISTS (SELECT 1 FROM %s WHERE %I = $1 %s)',
|
||||||
|
fk.child_table,
|
||||||
|
child_column,
|
||||||
|
CASE WHEN child_has_deleted_at THEN 'AND deleted_at IS NULL' ELSE '' END
|
||||||
|
);
|
||||||
|
EXECUTE sql INTO ref_exists USING parent_value;
|
||||||
|
IF ref_exists THEN
|
||||||
|
RAISE EXCEPTION 'Cannot soft delete %, still referenced by %',
|
||||||
|
TG_TABLE_NAME, fk.child_table;
|
||||||
|
END IF;
|
||||||
|
ELSIF fk.confdeltype = 'n' THEN
|
||||||
|
sql := format(
|
||||||
|
'UPDATE %s SET %I = NULL WHERE %I = $1 %s',
|
||||||
|
fk.child_table,
|
||||||
|
child_column,
|
||||||
|
child_column,
|
||||||
|
CASE WHEN child_has_deleted_at THEN 'AND deleted_at IS NULL' ELSE '' END
|
||||||
|
);
|
||||||
|
EXECUTE sql USING parent_value;
|
||||||
|
ELSIF fk.confdeltype = 'c' THEN
|
||||||
|
IF child_has_deleted_at THEN
|
||||||
|
sql := format(
|
||||||
|
'UPDATE %s SET deleted_at = NOW() WHERE %I = $1 AND deleted_at IS NULL',
|
||||||
|
fk.child_table,
|
||||||
|
child_column
|
||||||
|
);
|
||||||
|
EXECUTE sql USING parent_value;
|
||||||
|
ELSE
|
||||||
|
sql := format(
|
||||||
|
'DELETE FROM %s WHERE %I = $1',
|
||||||
|
fk.child_table,
|
||||||
|
child_column
|
||||||
|
);
|
||||||
|
EXECUTE sql USING parent_value;
|
||||||
|
END IF;
|
||||||
|
ELSIF fk.confdeltype = 'd' THEN
|
||||||
|
sql := format(
|
||||||
|
'UPDATE %s SET %I = DEFAULT WHERE %I = $1 %s',
|
||||||
|
fk.child_table,
|
||||||
|
child_column,
|
||||||
|
child_column,
|
||||||
|
CASE WHEN child_has_deleted_at THEN 'AND deleted_at IS NULL' ELSE '' END
|
||||||
|
);
|
||||||
|
EXECUTE sql USING parent_value;
|
||||||
|
END IF;
|
||||||
|
END LOOP;
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
RETURN NEW;
|
||||||
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
||||||
|
|
||||||
|
DO $$
|
||||||
|
DECLARE
|
||||||
|
r record;
|
||||||
|
trigger_name text;
|
||||||
|
BEGIN
|
||||||
|
FOR r IN
|
||||||
|
SELECT table_schema, table_name
|
||||||
|
FROM information_schema.columns
|
||||||
|
WHERE column_name = 'deleted_at'
|
||||||
|
AND table_schema = 'public'
|
||||||
|
GROUP BY table_schema, table_name
|
||||||
|
LOOP
|
||||||
|
trigger_name := format('trg_soft_delete_fk_%s', r.table_name);
|
||||||
|
EXECUTE format('DROP TRIGGER IF EXISTS %I ON %I.%I', trigger_name, r.table_schema, r.table_name);
|
||||||
|
EXECUTE format(
|
||||||
|
'CREATE TRIGGER %I BEFORE UPDATE OF deleted_at ON %I.%I FOR EACH ROW EXECUTE FUNCTION soft_delete_handle_fk()',
|
||||||
|
trigger_name,
|
||||||
|
r.table_schema,
|
||||||
|
r.table_name
|
||||||
|
);
|
||||||
|
END LOOP;
|
||||||
|
END $$;
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
DROP SEQUENCE IF EXISTS payments_code_seq;
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
CREATE SEQUENCE IF NOT EXISTS payments_code_seq START WITH 1 INCREMENT BY 1;
|
||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
-- Rollback: restore document columns to expenses table
|
||||||
|
ALTER TABLE expenses ADD COLUMN IF NOT EXISTS document_path JSON;
|
||||||
|
ALTER TABLE expenses ADD COLUMN IF NOT EXISTS realization_document_path JSON;
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
-- Delete document columns from expenses table since we now use Document service with polymorphic relations
|
||||||
|
ALTER TABLE expenses DROP COLUMN IF EXISTS document_path;
|
||||||
|
ALTER TABLE expenses DROP COLUMN IF EXISTS realization_document_path;
|
||||||
+28
@@ -0,0 +1,28 @@
|
|||||||
|
-- ============================================
|
||||||
|
-- Rollback: Remove FIFO fields and restore qty column
|
||||||
|
-- ============================================
|
||||||
|
|
||||||
|
-- STEP 1: Drop indexes
|
||||||
|
DROP INDEX IF EXISTS idx_marketing_delivery_products_fifo_lookup;
|
||||||
|
DROP INDEX IF EXISTS idx_marketing_delivery_products_pending_qty;
|
||||||
|
DROP INDEX IF EXISTS idx_marketing_delivery_products_usage_qty;
|
||||||
|
DROP INDEX IF EXISTS idx_marketing_delivery_products_created_at;
|
||||||
|
|
||||||
|
-- STEP 2: Drop constraints
|
||||||
|
ALTER TABLE marketing_delivery_products
|
||||||
|
DROP CONSTRAINT IF EXISTS chk_marketing_delivery_products_fifo_nonneg;
|
||||||
|
|
||||||
|
-- STEP 3: Restore qty column from usage_qty data
|
||||||
|
ALTER TABLE marketing_delivery_products
|
||||||
|
ADD COLUMN IF NOT EXISTS qty NUMERIC(15, 3) DEFAULT 0 NOT NULL;
|
||||||
|
|
||||||
|
-- Migrate data back from usage_qty to qty
|
||||||
|
UPDATE marketing_delivery_products
|
||||||
|
SET qty = usage_qty
|
||||||
|
WHERE qty = 0;
|
||||||
|
|
||||||
|
-- STEP 4: Drop FIFO columns
|
||||||
|
ALTER TABLE marketing_delivery_products
|
||||||
|
DROP COLUMN IF EXISTS usage_qty,
|
||||||
|
DROP COLUMN IF EXISTS pending_qty,
|
||||||
|
DROP COLUMN IF EXISTS created_at;
|
||||||
+58
@@ -0,0 +1,58 @@
|
|||||||
|
-- ============================================
|
||||||
|
-- Add FIFO fields to marketing_delivery_products
|
||||||
|
-- This migration adds fields needed for FIFO stock management
|
||||||
|
-- and removes the old qty field in favor of FIFO-based allocation
|
||||||
|
-- ============================================
|
||||||
|
|
||||||
|
-- STEP 0: Drop orphan indexes from previous migration
|
||||||
|
DROP INDEX IF EXISTS idx_marketing_delivery_products_deleted_at;
|
||||||
|
|
||||||
|
-- STEP 1: Add created_at column (required for FIFO ordering)
|
||||||
|
ALTER TABLE marketing_delivery_products
|
||||||
|
ADD COLUMN IF NOT EXISTS created_at TIMESTAMPTZ DEFAULT NOW();
|
||||||
|
|
||||||
|
-- STEP 2: Add FIFO tracking fields
|
||||||
|
ALTER TABLE marketing_delivery_products
|
||||||
|
ADD COLUMN IF NOT EXISTS usage_qty NUMERIC(15, 3) DEFAULT 0,
|
||||||
|
ADD COLUMN IF NOT EXISTS pending_qty NUMERIC(15, 3) DEFAULT 0;
|
||||||
|
|
||||||
|
-- STEP 3: Migrate data from old qty to usage_qty for existing records
|
||||||
|
-- This preserves existing quantity data as allocated quantity
|
||||||
|
UPDATE marketing_delivery_products
|
||||||
|
SET
|
||||||
|
usage_qty = COALESCE(qty, 0),
|
||||||
|
pending_qty = 0
|
||||||
|
WHERE usage_qty = 0;
|
||||||
|
|
||||||
|
-- STEP 4: Drop the old qty column (replaced by usage_qty + pending_qty)
|
||||||
|
ALTER TABLE marketing_delivery_products
|
||||||
|
DROP COLUMN IF EXISTS qty;
|
||||||
|
|
||||||
|
-- STEP 5: Make FIFO fields NOT NULL
|
||||||
|
ALTER TABLE marketing_delivery_products
|
||||||
|
ALTER COLUMN usage_qty SET NOT NULL,
|
||||||
|
ALTER COLUMN pending_qty SET NOT NULL,
|
||||||
|
ALTER COLUMN created_at SET NOT NULL;
|
||||||
|
|
||||||
|
-- STEP 6: Add constraints to ensure non-negative values
|
||||||
|
ALTER TABLE marketing_delivery_products
|
||||||
|
ADD CONSTRAINT chk_marketing_delivery_products_fifo_nonneg CHECK (
|
||||||
|
usage_qty >= 0 AND
|
||||||
|
pending_qty >= 0
|
||||||
|
);
|
||||||
|
|
||||||
|
-- STEP 7: Create indexes for FIFO operations
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_marketing_delivery_products_created_at
|
||||||
|
ON marketing_delivery_products(created_at DESC);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_marketing_delivery_products_usage_qty
|
||||||
|
ON marketing_delivery_products(usage_qty)
|
||||||
|
WHERE usage_qty > 0;
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_marketing_delivery_products_pending_qty
|
||||||
|
ON marketing_delivery_products(pending_qty)
|
||||||
|
WHERE pending_qty > 0;
|
||||||
|
|
||||||
|
-- Composite index for FIFO lookups
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_marketing_delivery_products_fifo_lookup
|
||||||
|
ON marketing_delivery_products(marketing_product_id, created_at DESC);
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
-- Remove foreign key constraint
|
||||||
|
ALTER TABLE marketing_delivery_products
|
||||||
|
DROP CONSTRAINT IF EXISTS fk_marketing_delivery_products_product_warehouse;
|
||||||
|
|
||||||
|
-- Drop product_warehouse_id column
|
||||||
|
ALTER TABLE marketing_delivery_products
|
||||||
|
DROP COLUMN IF EXISTS product_warehouse_id;
|
||||||
+19
@@ -0,0 +1,19 @@
|
|||||||
|
-- Add product_warehouse_id column to marketing_delivery_products
|
||||||
|
ALTER TABLE marketing_delivery_products
|
||||||
|
ADD COLUMN IF NOT EXISTS product_warehouse_id INT NOT NULL DEFAULT 0;
|
||||||
|
|
||||||
|
-- Fill product_warehouse_id from marketing_products
|
||||||
|
UPDATE marketing_delivery_products mdp
|
||||||
|
SET product_warehouse_id = mp.product_warehouse_id
|
||||||
|
FROM marketing_products mp
|
||||||
|
WHERE mdp.marketing_product_id = mp.id
|
||||||
|
AND mdp.product_warehouse_id = 0;
|
||||||
|
|
||||||
|
-- Set NOT NULL constraint
|
||||||
|
ALTER TABLE marketing_delivery_products
|
||||||
|
ALTER COLUMN product_warehouse_id SET NOT NULL;
|
||||||
|
|
||||||
|
-- Add foreign key constraint
|
||||||
|
ALTER TABLE marketing_delivery_products
|
||||||
|
ADD CONSTRAINT fk_marketing_delivery_products_product_warehouse
|
||||||
|
FOREIGN KEY (product_warehouse_id) REFERENCES product_warehouses(id);
|
||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
-- Drop indexes
|
||||||
|
DROP INDEX IF EXISTS idx_standard_growth_details_standard_week;
|
||||||
|
DROP INDEX IF EXISTS idx_production_standard_details_standard_week;
|
||||||
|
DROP INDEX IF EXISTS idx_production_standards_project_category;
|
||||||
|
DROP INDEX IF EXISTS idx_production_standards_deleted_at;
|
||||||
|
|
||||||
|
-- Drop tables (in reverse order due to foreign keys)
|
||||||
|
DROP TABLE IF EXISTS standard_growth_details;
|
||||||
|
DROP TABLE IF EXISTS production_standard_details;
|
||||||
|
DROP TABLE IF EXISTS production_standards;
|
||||||
@@ -0,0 +1,96 @@
|
|||||||
|
-- Create production_standards table
|
||||||
|
CREATE TABLE IF NOT EXISTS production_standards (
|
||||||
|
id BIGSERIAL PRIMARY KEY,
|
||||||
|
name VARCHAR(100) UNIQUE NOT NULL,
|
||||||
|
project_category VARCHAR(20) NOT NULL CHECK (project_category IN ('GROWING', 'LAYING')),
|
||||||
|
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||||
|
updated_at TIMESTAMPTZ DEFAULT NOW(),
|
||||||
|
deleted_at TIMESTAMPTZ,
|
||||||
|
created_by BIGINT
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Create index for deleted_at (soft delete)
|
||||||
|
CREATE INDEX idx_production_standards_deleted_at ON production_standards(deleted_at);
|
||||||
|
|
||||||
|
-- Tambahkan Foreign Key ke users
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'users') THEN
|
||||||
|
ALTER TABLE production_standards
|
||||||
|
ADD CONSTRAINT fk_production_standards_created_by
|
||||||
|
FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
-- Index
|
||||||
|
CREATE INDEX idx_production_standards_created_by ON production_standards(created_by);
|
||||||
|
|
||||||
|
-- Create production_standard_details table
|
||||||
|
CREATE TABLE IF NOT EXISTS production_standard_details (
|
||||||
|
id BIGSERIAL PRIMARY KEY,
|
||||||
|
production_standard_id BIGINT NOT NULL,
|
||||||
|
week INT NOT NULL,
|
||||||
|
target_hen_day_production NUMERIC(15, 3),
|
||||||
|
target_hen_house_production NUMERIC(15, 3),
|
||||||
|
target_egg_weight NUMERIC(15, 3),
|
||||||
|
target_egg_mass NUMERIC(15, 3),
|
||||||
|
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||||
|
updated_at TIMESTAMPTZ DEFAULT NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Tambahkan Foreign Key ke production_standards
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'production_standards') THEN
|
||||||
|
ALTER TABLE production_standard_details
|
||||||
|
ADD CONSTRAINT fk_production_standard_details_standard
|
||||||
|
FOREIGN KEY (production_standard_id) REFERENCES production_standards(id) ON DELETE CASCADE;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
-- Create unique constraint for standard_id + week
|
||||||
|
CREATE UNIQUE INDEX idx_production_standard_details_standard_week
|
||||||
|
ON production_standard_details(production_standard_id, week);
|
||||||
|
|
||||||
|
-- Create standard_growth_details table
|
||||||
|
CREATE TABLE IF NOT EXISTS standard_growth_details (
|
||||||
|
id BIGSERIAL PRIMARY KEY,
|
||||||
|
production_standard_id BIGINT NOT NULL,
|
||||||
|
target_mean_bw NUMERIC(15, 3),
|
||||||
|
max_depletion NUMERIC(15, 3),
|
||||||
|
min_uniformity NUMERIC(15, 3) NOT NULL,
|
||||||
|
week INT NOT NULL,
|
||||||
|
feed_intake NUMERIC(15, 3),
|
||||||
|
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||||
|
created_by BIGINT
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Tambahkan Foreign Key ke production_standards
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'production_standards') THEN
|
||||||
|
ALTER TABLE standard_growth_details
|
||||||
|
ADD CONSTRAINT fk_standard_growth_details_standard
|
||||||
|
FOREIGN KEY (production_standard_id) REFERENCES production_standards(id) ON DELETE CASCADE;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
-- Tambahkan Foreign Key ke users
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'users') THEN
|
||||||
|
ALTER TABLE standard_growth_details
|
||||||
|
ADD CONSTRAINT fk_standard_growth_details_created_by
|
||||||
|
FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
-- Create unique constraint for standard_id + week
|
||||||
|
CREATE UNIQUE INDEX idx_standard_growth_details_standard_week
|
||||||
|
ON standard_growth_details(production_standard_id, week);
|
||||||
|
|
||||||
|
-- Index
|
||||||
|
CREATE INDEX idx_standard_growth_details_created_by ON standard_growth_details(created_by);
|
||||||
|
|
||||||
|
-- Create index for project_category
|
||||||
|
CREATE INDEX idx_production_standards_project_category ON production_standards(project_category);
|
||||||
@@ -588,6 +588,7 @@ func seedProducts(tx *gorm.DB, createdBy uint, uoms map[string]uint, categories
|
|||||||
Uom: "Ekor",
|
Uom: "Ekor",
|
||||||
Category: "Day Old Chick",
|
Category: "Day Old Chick",
|
||||||
Price: 1,
|
Price: 1,
|
||||||
|
Flags: []utils.FlagType{utils.FlagAyamAfkir},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "Ayam Mati",
|
Name: "Ayam Mati",
|
||||||
@@ -596,6 +597,7 @@ func seedProducts(tx *gorm.DB, createdBy uint, uoms map[string]uint, categories
|
|||||||
Uom: "Ekor",
|
Uom: "Ekor",
|
||||||
Category: "Day Old Chick",
|
Category: "Day Old Chick",
|
||||||
Price: 1,
|
Price: 1,
|
||||||
|
Flags: []utils.FlagType{utils.FlagAyamMati},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "Ayam Culling",
|
Name: "Ayam Culling",
|
||||||
@@ -604,6 +606,7 @@ func seedProducts(tx *gorm.DB, createdBy uint, uoms map[string]uint, categories
|
|||||||
Uom: "Ekor",
|
Uom: "Ekor",
|
||||||
Category: "Day Old Chick",
|
Category: "Day Old Chick",
|
||||||
Price: 1,
|
Price: 1,
|
||||||
|
Flags: []utils.FlagType{utils.FlagAyamCulling},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "Telur Konsumsi Baik",
|
Name: "Telur Konsumsi Baik",
|
||||||
@@ -612,6 +615,7 @@ func seedProducts(tx *gorm.DB, createdBy uint, uoms map[string]uint, categories
|
|||||||
Uom: "Unit",
|
Uom: "Unit",
|
||||||
Category: "Telur",
|
Category: "Telur",
|
||||||
Price: 1,
|
Price: 1,
|
||||||
|
Flags: []utils.FlagType{utils.FlagTelurUtuh},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "Telur Pecah",
|
Name: "Telur Pecah",
|
||||||
@@ -620,6 +624,7 @@ func seedProducts(tx *gorm.DB, createdBy uint, uoms map[string]uint, categories
|
|||||||
Uom: "Unit",
|
Uom: "Unit",
|
||||||
Category: "Telur",
|
Category: "Telur",
|
||||||
Price: 1,
|
Price: 1,
|
||||||
|
Flags: []utils.FlagType{utils.FlagTelurPecah},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "281 SPECIAL STARTER",
|
Name: "281 SPECIAL STARTER",
|
||||||
@@ -632,6 +637,16 @@ func seedProducts(tx *gorm.DB, createdBy uint, uoms map[string]uint, categories
|
|||||||
Suppliers: []string{"PT CHAROEN POKPHAND INDONESIA Tbk"},
|
Suppliers: []string{"PT CHAROEN POKPHAND INDONESIA Tbk"},
|
||||||
Flags: []utils.FlagType{utils.FlagPakan, utils.FlagStarter},
|
Flags: []utils.FlagType{utils.FlagPakan, utils.FlagStarter},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "Ayam Layer",
|
||||||
|
Brand: "-",
|
||||||
|
Sku: "LYR0001",
|
||||||
|
Uom: "Ekor",
|
||||||
|
Category: "Pullet",
|
||||||
|
Price: 20000,
|
||||||
|
Suppliers: []string{"PT CHAROEN POKPHAND INDONESIA Tbk"},
|
||||||
|
Flags: []utils.FlagType{utils.FlagLayer},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, seed := range seeds {
|
for _, seed := range seeds {
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package entities
|
package entities
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@@ -13,8 +12,6 @@ type Expense struct {
|
|||||||
SupplierId uint64 `gorm:""`
|
SupplierId uint64 `gorm:""`
|
||||||
Category string `gorm:"type:varchar(50);not null"`
|
Category string `gorm:"type:varchar(50);not null"`
|
||||||
PoNumber string `gorm:"type:varchar(50)"`
|
PoNumber string `gorm:"type:varchar(50)"`
|
||||||
DocumentPath sql.NullString `gorm:"type:json"`
|
|
||||||
RealizationDocumentPath sql.NullString `gorm:"type:json;column:realization_document_path"`
|
|
||||||
RealizationDate time.Time `gorm:"type:date;column:realization_date"`
|
RealizationDate time.Time `gorm:"type:date;column:realization_date"`
|
||||||
TransactionDate time.Time `gorm:"type:date;not null"`
|
TransactionDate time.Time `gorm:"type:date;not null"`
|
||||||
Notes string `gorm:"type:text;column:notes"`
|
Notes string `gorm:"type:text;column:notes"`
|
||||||
@@ -23,8 +20,10 @@ type Expense struct {
|
|||||||
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
||||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||||
|
|
||||||
Supplier *Supplier `gorm:"foreignKey:SupplierId;references:Id"`
|
Supplier *Supplier `gorm:"foreignKey:SupplierId;references:Id"`
|
||||||
CreatedUser *User `gorm:"foreignKey:CreatedBy;references:Id"`
|
CreatedUser *User `gorm:"foreignKey:CreatedBy;references:Id"`
|
||||||
Nonstocks []ExpenseNonstock `gorm:"foreignKey:ExpenseId;references:Id"`
|
Nonstocks []ExpenseNonstock `gorm:"foreignKey:ExpenseId;references:Id"`
|
||||||
LatestApproval *Approval `gorm:"-" json:"latest_approval,omitempty"`
|
Documents []Document `gorm:"foreignKey:DocumentableId;references:Id"`
|
||||||
|
RealizationDocuments []Document `gorm:"foreignKey:DocumentableId;references:Id"`
|
||||||
|
LatestApproval *Approval `gorm:"-" json:"latest_approval,omitempty"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package entities
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Initial struct {
|
||||||
|
Id uint `gorm:"primaryKey;autoIncrement"`
|
||||||
|
ReferenceNumber string `gorm:"type:varchar(100);not null"`
|
||||||
|
TransactionType string `gorm:"type:varchar(50);not null"`
|
||||||
|
InitialBalanceType string `gorm:"type:varchar(20);not null"`
|
||||||
|
PartyType string `gorm:"type:varchar(50);not null;index:initials_party_polymorphic,priority:1"`
|
||||||
|
PartyId uint `gorm:"not null;index:initials_party_polymorphic,priority:2"`
|
||||||
|
BankId *uint `gorm:"index"`
|
||||||
|
Direction string `gorm:"type:varchar(5);not null"`
|
||||||
|
Nominal float64 `gorm:"type:numeric(15,3);not null"`
|
||||||
|
Notes string `gorm:"type:text;not null"`
|
||||||
|
CreatedBy uint `gorm:"index" json:"-"`
|
||||||
|
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||||
|
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
||||||
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||||
|
|
||||||
|
Bank Bank `gorm:"foreignKey:BankId;references:Id"`
|
||||||
|
CreatedUser User `gorm:"foreignKey:CreatedBy;references:Id"`
|
||||||
|
Customer *Customer `gorm:"foreignKey:PartyId;references:Id"`
|
||||||
|
Supplier *Supplier `gorm:"foreignKey:PartyId;references:Id"`
|
||||||
|
LatestApproval *Approval `gorm:"-" json:"-"`
|
||||||
|
}
|
||||||
@@ -5,15 +5,20 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type MarketingDeliveryProduct struct {
|
type MarketingDeliveryProduct struct {
|
||||||
Id uint `gorm:"primaryKey;autoIncrement"`
|
Id uint `gorm:"primaryKey;autoIncrement"`
|
||||||
MarketingProductId uint `gorm:"uniqueIndex;not null"`
|
MarketingProductId uint `gorm:"uniqueIndex;not null"`
|
||||||
Qty float64 `gorm:"type:numeric(15,3)"`
|
ProductWarehouseId uint `gorm:"not null"`
|
||||||
UnitPrice float64 `gorm:"type:numeric(15,3)"`
|
UnitPrice float64 `gorm:"type:numeric(15,3)"`
|
||||||
TotalWeight float64 `gorm:"type:numeric(15,3)"`
|
TotalWeight float64 `gorm:"type:numeric(15,3)"`
|
||||||
AvgWeight float64 `gorm:"type:numeric(15,3)"`
|
AvgWeight float64 `gorm:"type:numeric(15,3)"`
|
||||||
TotalPrice float64 `gorm:"type:numeric(15,3)"`
|
TotalPrice float64 `gorm:"type:numeric(15,3)"`
|
||||||
DeliveryDate *time.Time `gorm:"type:timestamptz"`
|
DeliveryDate *time.Time `gorm:"type:timestamptz"`
|
||||||
VehicleNumber string `gorm:"type:varchar(50)"`
|
VehicleNumber string `gorm:"type:varchar(50)"`
|
||||||
|
|
||||||
|
// FIFO Fields
|
||||||
|
UsageQty float64 `gorm:"type:numeric(15,3);default:0;not null"`
|
||||||
|
PendingQty float64 `gorm:"type:numeric(15,3);default:0;not null"`
|
||||||
|
CreatedAt *time.Time `gorm:"type:timestamptz;not null"`
|
||||||
|
|
||||||
MarketingProduct MarketingProduct `gorm:"foreignKey:MarketingProductId;references:Id"`
|
MarketingProduct MarketingProduct `gorm:"foreignKey:MarketingProductId;references:Id"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package entities
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Payment struct {
|
||||||
|
Id uint `gorm:"primaryKey;autoIncrement"`
|
||||||
|
PaymentCode string `gorm:"type:varchar(50);not null"`
|
||||||
|
ReferenceNumber *string `gorm:"type:varchar(100)"`
|
||||||
|
TransactionType string `gorm:"type:varchar(50)"`
|
||||||
|
PartyType string `gorm:"type:varchar(50);not null;index:payments_party_polymorphic,priority:1"`
|
||||||
|
PartyId uint `gorm:"not null;index:payments_party_polymorphic,priority:2"`
|
||||||
|
PaymentDate time.Time `gorm:"not null"`
|
||||||
|
PaymentMethod string `gorm:"type:varchar(20);not null"`
|
||||||
|
BankId *uint `gorm:"not null;index:idx_payments_bank_id"`
|
||||||
|
Direction string `gorm:"type:varchar(5);not null"`
|
||||||
|
Nominal float64 `gorm:"type:numeric(15,3);not null"`
|
||||||
|
Notes string `gorm:"type:text;not null"`
|
||||||
|
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||||
|
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
||||||
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||||
|
CreatedBy uint `gorm:"index" json:"-"`
|
||||||
|
|
||||||
|
BankWarehouse Bank `gorm:"foreignKey:BankId;references:Id"`
|
||||||
|
CreatedUser User `gorm:"foreignKey:CreatedBy;references:Id"`
|
||||||
|
Customer *Customer `gorm:"foreignKey:PartyId;references:Id"`
|
||||||
|
Supplier *Supplier `gorm:"foreignKey:PartyId;references:Id"`
|
||||||
|
LatestApproval *Approval `gorm:"-" json:"-"`
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package entities
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ProductionStandard struct {
|
||||||
|
Id uint `gorm:"primaryKey;autoIncrement"`
|
||||||
|
Name string `gorm:"type:varchar(100);uniqueIndex;not null"`
|
||||||
|
ProjectCategory string `gorm:"type:varchar(20);not null"`
|
||||||
|
CreatedAt time.Time `gorm:"type:timestamptz;not null"`
|
||||||
|
UpdatedAt time.Time `gorm:"type:timestamptz;not null"`
|
||||||
|
DeletedAt *time.Time `gorm:"type:timestamptz"`
|
||||||
|
CreatedBy uint `gorm:"not null"`
|
||||||
|
|
||||||
|
CreatedUser User `gorm:"foreignKey:CreatedBy;references:Id"`
|
||||||
|
ProductionStandardDetails []ProductionStandardDetail `gorm:"foreignKey:ProductionStandardId;references:Id"`
|
||||||
|
StandardGrowthDetails []StandardGrowthDetail `gorm:"foreignKey:ProductionStandardId;references:Id"`
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package entities
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ProductionStandardDetail struct {
|
||||||
|
Id uint `gorm:"primaryKey;autoIncrement"`
|
||||||
|
ProductionStandardId uint `gorm:"not null"`
|
||||||
|
Week int `gorm:"not null"`
|
||||||
|
TargetHenDayProduction *float64 `gorm:"type:numeric(15,3)"`
|
||||||
|
TargetHenHouseProduction *float64 `gorm:"type:numeric(15,3)"`
|
||||||
|
TargetEggWeight *float64 `gorm:"type:numeric(15,3)"`
|
||||||
|
TargetEggMass *float64 `gorm:"type:numeric(15,3)"`
|
||||||
|
CreatedAt time.Time `gorm:"type:timestamptz;not null"`
|
||||||
|
UpdatedAt time.Time `gorm:"type:timestamptz;not null"`
|
||||||
|
|
||||||
|
ProductionStandard ProductionStandard `gorm:"foreignKey:ProductionStandardId;references:Id"`
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package entities
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type StandardGrowthDetail struct {
|
||||||
|
Id uint `gorm:"primaryKey;autoIncrement"`
|
||||||
|
ProductionStandardId uint `gorm:"not null"`
|
||||||
|
TargetMeanBw *float64 `gorm:"type:numeric(15,3)"`
|
||||||
|
MaxDepletion *float64 `gorm:"type:numeric(15,3)"`
|
||||||
|
MinUniformity float64 `gorm:"type:numeric(15,3);not null"`
|
||||||
|
Week int `gorm:"not null"`
|
||||||
|
FeedIntake *float64 `gorm:"type:numeric(15,3)"`
|
||||||
|
CreatedAt time.Time `gorm:"type:timestamptz;not null"`
|
||||||
|
CreatedBy uint `gorm:"not null"`
|
||||||
|
|
||||||
|
ProductionStandard ProductionStandard `gorm:"foreignKey:ProductionStandardId;references:Id"`
|
||||||
|
}
|
||||||
@@ -20,4 +20,5 @@ type StockTransfer struct {
|
|||||||
Details []StockTransferDetail `gorm:"foreignKey:StockTransferId"`
|
Details []StockTransferDetail `gorm:"foreignKey:StockTransferId"`
|
||||||
Deliveries []StockTransferDelivery `gorm:"foreignKey:StockTransferId"`
|
Deliveries []StockTransferDelivery `gorm:"foreignKey:StockTransferId"`
|
||||||
CreatedUser *User `gorm:"foreignKey:CreatedBy"`
|
CreatedUser *User `gorm:"foreignKey:CreatedBy"`
|
||||||
|
Documents []Document `gorm:"foreignKey:DocumentableId;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,16 +2,6 @@ package entities
|
|||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
const (
|
|
||||||
LogTypeAdjustment = "ADJUSTMENT"
|
|
||||||
LogTypeTransfer = "TRANSFER"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
TransactionTypeIncrease = "INCREASE"
|
|
||||||
TransactionTypeDecrease = "DECREASE"
|
|
||||||
)
|
|
||||||
|
|
||||||
type StockLog struct {
|
type StockLog struct {
|
||||||
Id uint `gorm:"primaryKey;column:id"`
|
Id uint `gorm:"primaryKey;column:id"`
|
||||||
ProductWarehouseId uint `gorm:"column:product_warehouse_id;not null;index"`
|
ProductWarehouseId uint `gorm:"column:product_warehouse_id;not null;index"`
|
||||||
|
|||||||
@@ -4,20 +4,21 @@ import "time"
|
|||||||
|
|
||||||
// DETAIL EKSPEDISI
|
// DETAIL EKSPEDISI
|
||||||
type StockTransferDelivery struct {
|
type StockTransferDelivery struct {
|
||||||
Id uint64 `gorm:"primaryKey;autoIncrement"`
|
Id uint64 `gorm:"primaryKey;autoIncrement"`
|
||||||
StockTransferId uint64
|
StockTransferId uint64
|
||||||
SupplierId uint64
|
SupplierId uint64
|
||||||
VehiclePlate string
|
VehiclePlate string
|
||||||
DriverName string
|
DriverName string
|
||||||
DocumentNumber string
|
DocumentNumber string
|
||||||
DocumentPath string
|
DocumentPath string
|
||||||
ShippingCostItem float64
|
ShippingCostItem float64
|
||||||
ShippingCostTotal float64
|
ShippingCostTotal float64
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
UpdatedAt time.Time
|
UpdatedAt time.Time
|
||||||
DeletedAt *time.Time `gorm:"index"`
|
DeletedAt *time.Time `gorm:"index"`
|
||||||
// Relations
|
// Relations
|
||||||
StockTransfer *StockTransfer `gorm:"foreignKey:StockTransferId"`
|
StockTransfer *StockTransfer `gorm:"foreignKey:StockTransferId"`
|
||||||
Supplier *Supplier `gorm:"foreignKey:SupplierId"`
|
Supplier *Supplier `gorm:"foreignKey:SupplierId"`
|
||||||
Items []StockTransferDeliveryItem `gorm:"foreignKey:StockTransferDeliveryId"`
|
Items []StockTransferDeliveryItem `gorm:"foreignKey:StockTransferDeliveryId"`
|
||||||
}
|
Documents []Document `gorm:"foreignKey:DocumentableId;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package entities
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Transaction 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"`
|
||||||
|
}
|
||||||
@@ -104,11 +104,12 @@ func AuthenticatedUser(c *fiber.Ctx) (*entity.User, bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ActorIDFromContext(c *fiber.Ctx) (uint, error) {
|
func ActorIDFromContext(c *fiber.Ctx) (uint, error) {
|
||||||
user, ok := AuthenticatedUser(c)
|
// user, ok := AuthenticatedUser(c)
|
||||||
if !ok || user == nil || user.Id == 0 {
|
// if !ok || user == nil || user.Id == 0 {
|
||||||
return 0, fiber.NewError(fiber.StatusUnauthorized, "Please authenticate")
|
// return 0, fiber.NewError(fiber.StatusUnauthorized, "Please authenticate")
|
||||||
}
|
// }
|
||||||
return user.Id, nil
|
// return user.Id, nil
|
||||||
|
return 1, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AuthDetails returns the full authentication context (token, claims, user).
|
// AuthDetails returns the full authentication context (token, claims, user).
|
||||||
|
|||||||
@@ -1,14 +1,208 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
//project-flock
|
// project-flock
|
||||||
const (
|
const (
|
||||||
PermissionProjectFlockClosing = "lti:project-flock:closing"
|
P_ProjectFlockKandangsClosing = "lti.production.project_flock_kandangs.closing"
|
||||||
|
P_ProjectFlockKandangsCheckClosing = "lti.production.project_flock_kandangs.closing.detail"
|
||||||
|
P_ProjectFlockKandangsGetAll = "lti.production.project_flock_kandangs.list"
|
||||||
|
P_ProjectFlockKandangsGetOne = "lti.production.project_flock_kandangs.detail"
|
||||||
|
|
||||||
|
P_ProjectFlockGetAll = "lti.production.project_flocks.list"
|
||||||
|
P_ProjectFlockCreate = "lti.production.project_flocks.create"
|
||||||
|
P_ProjectFlockGetOne = "lti.production.project_flocks.detail"
|
||||||
|
P_ProjectFlockUpdate = "lti.production.project_flocks.update"
|
||||||
|
P_ProjectFlockDelete = "lti.production.project_flocks.delete"
|
||||||
|
P_ProjectFlockApprove = "lti.production.project_flocks.approve"
|
||||||
|
P_ProjectFlockLookup = "lti.production.project_flocks.lookup"
|
||||||
|
P_ProjectFlockNextPeriod = "lti.production.project_flocks.next_period"
|
||||||
|
P_ProjectFlockResubmit = "lti.production.project_flocks.resubmit"
|
||||||
)
|
)
|
||||||
|
|
||||||
//recording
|
|
||||||
const (
|
const (
|
||||||
PermissionRecordingRead = "recording.index"
|
P_ExpenseGetAll = "lti.expense.list"
|
||||||
PermissionRecordingCreate = "recording.create"
|
P_ExpenseCreateOne = "lti.expense.create"
|
||||||
PermissionRecordingUpdate = "recording.update"
|
P_ExpenseUpdateOne = "lti.expense.update"
|
||||||
PermissionRecordingDelete = "recording.delete"
|
P_ExpenseGetOne = "lti.expense.detail"
|
||||||
)
|
P_ExpenseDeleteOne = "lti.expense.delete"
|
||||||
|
P_ExpenseApprovalManager = "lti.expense.approve.manager"
|
||||||
|
P_ExpenseApprovalFinance = "lti.expense.approve.finance"
|
||||||
|
P_ExpenseCreateRealizations = "lti.expense.create.realization"
|
||||||
|
P_ExpenseUpdateRealizations = "lti.expense.update.realization"
|
||||||
|
P_ExpenseCompleteExpense = "lti.expense.complete.expense"
|
||||||
|
P_ExpenseDocument = "lti.expense.document"
|
||||||
|
P_ExpenseDocumentRealizations = "lti.expense.document.realization"
|
||||||
|
)
|
||||||
|
const (
|
||||||
|
P_AdjustmentGetAll = "lti.inventory.list"
|
||||||
|
P_AdjustmentCreate = "lti.inventory.create"
|
||||||
|
P_AdjustmentGetOne = "lti.inventory.detail"
|
||||||
|
)
|
||||||
|
const (
|
||||||
|
P_ApprovalGetAll = "lti.approval.list"
|
||||||
|
)
|
||||||
|
const (
|
||||||
|
P_ReportExpenseGetAll = "lti.repport.expense.list"
|
||||||
|
P_ReportDeliveryGetAll = "lti.repport.delivery.list"
|
||||||
|
P_ReportPurchaseSupplierGetAll = "lti.repport.purchasesupplier.list"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
P_ProductStockGetAll = "lti.inventory.product_stock.list"
|
||||||
|
P_ProductStockGetOne = "lti.inventory.product_stock.detail"
|
||||||
|
P_ProductWarehousekGetAll = "lti.inventory.product_warehouses.list"
|
||||||
|
P_ProductWarehouseGetOne = "lti.inventory.product_warehouses.detail"
|
||||||
|
)
|
||||||
|
const (
|
||||||
|
P_ClosingGetAll = "lti.closing.list"
|
||||||
|
P_ClosingDetail = "lti.closing.detail"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
P_TransferGetAll = "lti.inventory.transfer.list"
|
||||||
|
P_TransferGetOne = "lti.inventory.transfer.detail"
|
||||||
|
P_TransferCreateOne = "lti.inventory.transfer.create"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
P_TransferToLaying_GetAll = "lti.production.transfer_to_laying.list"
|
||||||
|
P_TransferToLaying_GetOne = "lti.production.transfer_to_laying.detail"
|
||||||
|
P_TransferToLaying_CreateOne = "lti.production.transfer_to_laying.create"
|
||||||
|
P_TransferToLaying_UpdateOne = "lti.production.transfer_to_laying.update"
|
||||||
|
P_TransferToLaying_DeleteOne = "lti.production.transfer_to_laying.delete"
|
||||||
|
P_TransferToLaying_Approval = "lti.production.transfer_to_laying.approve"
|
||||||
|
P_TransferToLaying_GetAvailableQty = "lti.production.transfer_to_laying.getavailableqty"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
P_DeliveryGetAll = "lti.marketing.delivery_order.list"
|
||||||
|
P_DeliveryGetOne = "lti.marketing.delivery_order.detail"
|
||||||
|
P_DeliveryUpdateOne = "lti.marketing.delivery_order.update"
|
||||||
|
P_DeliveryCreateOne = "lti.marketing.delivery_order.Create"
|
||||||
|
P_SalesOrderDelete = "lti.marketing.sales_order.delete"
|
||||||
|
P_SalesOrderApproval = "lti.marketing.sales_order.approve"
|
||||||
|
P_SalesOrderCreateOne = "lti.marketing.sales_order.create"
|
||||||
|
P_SalesOrderUpdateOne = "lti.marketing.sales_order.update"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
P_AreaGetAll = "lti.master.area.list"
|
||||||
|
P_AreaGetOne = "lti.master.area.detail"
|
||||||
|
P_AreaCreateOne = "lti.master.area.create"
|
||||||
|
P_AreaUpdateOne = "lti.master.area.update"
|
||||||
|
P_AreaDeleteOne = "lti.master.area.delete"
|
||||||
|
|
||||||
|
P_BanksGetAll = "lti.master.banks.list"
|
||||||
|
P_BanksGetOne = "lti.master.banks.detail"
|
||||||
|
P_BanksCreateOne = "lti.master.banks.create"
|
||||||
|
P_BanksUpdateOne = "lti.master.banks.update"
|
||||||
|
P_BanksDeleteOne = "lti.master.banks.delete"
|
||||||
|
|
||||||
|
P_CustomerGetAll = "lti.master.customer.list"
|
||||||
|
P_CustomerGetOne = "lti.master.customer.detail"
|
||||||
|
P_CustomerCreateOne = "lti.master.customer.create"
|
||||||
|
P_CustomerUpdateOne = "lti.master.customer.update"
|
||||||
|
P_CustomerDeleteOne = "lti.master.customer.delete"
|
||||||
|
|
||||||
|
P_FcrGetAll = "lti.master.fcr.list"
|
||||||
|
P_FcrGetOne = "lti.master.fcr.detail"
|
||||||
|
P_FcrCreateOne = "lti.master.fcr.create"
|
||||||
|
P_FcrUpdateOne = "lti.master.fcr.update"
|
||||||
|
P_FcrDeleteOne = "lti.master.fcr.delete"
|
||||||
|
|
||||||
|
P_FlocksGetAll = "lti.master.flocks.list"
|
||||||
|
P_FlocksGetOne = "lti.master.flocks.detail"
|
||||||
|
P_FlocksCreateOne = "lti.master.flocks.create"
|
||||||
|
P_FlocksUpdateOne = "lti.master.flocks.update"
|
||||||
|
P_FlocksDeleteOne = "lti.master.flocks.delete"
|
||||||
|
|
||||||
|
P_KandangsGetAll = "lti.master.kandangs.list"
|
||||||
|
P_KandangsGetOne = "lti.master.kandangs.detail"
|
||||||
|
P_KandangsCreateOne = "lti.master.kandangs.create"
|
||||||
|
P_KandangsUpdateOne = "lti.master.kandangs.update"
|
||||||
|
P_KandangsDeleteOne = "lti.master.kandangs.delete"
|
||||||
|
|
||||||
|
P_LocationsGetAll = "lti.master.locations.list"
|
||||||
|
P_LocationsGetOne = "lti.master.locations.detail"
|
||||||
|
P_LocationsCreateOne = "lti.master.locations.create"
|
||||||
|
P_LocationsUpdateOne = "lti.master.locations.update"
|
||||||
|
P_LocationsDeleteOne = "lti.master.locations.delete"
|
||||||
|
|
||||||
|
P_NonstocksGetAll = "lti.master.nonstocks.list"
|
||||||
|
P_NonstocksGetOne = "lti.master.nonstocks.detail"
|
||||||
|
P_NonstocksCreateOne = "lti.master.nonstocks.create"
|
||||||
|
P_NonstocksUpdateOne = "lti.master.nonstocks.update"
|
||||||
|
P_NonstocksDeleteOne = "lti.master.nonstocks.delete"
|
||||||
|
|
||||||
|
P_ProductCategoriesGetAll = "lti.master.Product_categories.list"
|
||||||
|
P_ProductCategoriesGetOne = "lti.master.Product_categories.detail"
|
||||||
|
P_ProductCategoriesCreateOne = "lti.master.Product_categories.create"
|
||||||
|
P_ProductCategoriesUpdateOne = "lti.master.Product_categories.update"
|
||||||
|
P_ProductCategoriesDeleteOne = "lti.master.Product_categories.delete"
|
||||||
|
|
||||||
|
P_ProductsGetAll = "lti.master.Products.list"
|
||||||
|
P_ProductsGetOne = "lti.master.Products.detail"
|
||||||
|
P_ProductsCreateOne = "lti.master.Products.create"
|
||||||
|
P_ProductsUpdateOne = "lti.master.Products.update"
|
||||||
|
P_ProductsDeleteOne = "lti.master.Products.delete"
|
||||||
|
|
||||||
|
P_SuppliersGetAll = "lti.master.suppliers.list"
|
||||||
|
P_SuppliersGetOne = "lti.master.suppliers.detail"
|
||||||
|
P_SuppliersCreateOne = "lti.master.suppliers.create"
|
||||||
|
P_SuppliersUpdateOne = "lti.master.suppliers.update"
|
||||||
|
P_SuppliersDeleteOne = "lti.master.suppliers.delete"
|
||||||
|
|
||||||
|
P_UomsGetAll = "lti.master.uoms.list"
|
||||||
|
P_UomsGetOne = "lti.master.uoms.detail"
|
||||||
|
P_UomsCreateOne = "lti.master.uoms.create"
|
||||||
|
P_UomsUpdateOne = "lti.master.uoms.update"
|
||||||
|
P_UomsDeleteOne = "lti.master.uoms.delete"
|
||||||
|
|
||||||
|
P_WarehousesGetAll = "lti.master.warehouses.list"
|
||||||
|
P_WarehousesGetOne = "lti.master.warehouses.detail"
|
||||||
|
P_WarehousesCreateOne = "lti.master.warehouses.create"
|
||||||
|
P_WarehousesUpdateOne = "lti.master.warehouses.update"
|
||||||
|
P_WarehousesDeleteOne = "lti.master.warehouses.delete"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
P_ChickinsCreateOne = "lti.production.chickins.create"
|
||||||
|
P_ChickinsGetOne = "lti.production.chickins.detail"
|
||||||
|
P_ChickinsApproval = "lti.production.chickins.approve"
|
||||||
|
)
|
||||||
|
|
||||||
|
// recording
|
||||||
|
const (
|
||||||
|
P_RecordingGetAll = "lti.production.recording.list"
|
||||||
|
P_RecordingGetOne = "lti.production.recording.detail"
|
||||||
|
P_RecordingCreateOne = "lti.production.recording.create"
|
||||||
|
P_RecordingUpdateOne = "lti.production.recording.update"
|
||||||
|
P_RecordingDeleteOne = "lti.production.recording.delete"
|
||||||
|
P_RecordingNextDay = "lti.production.recording.next_day"
|
||||||
|
P_RecordingApproval = "lti.production.recording.approve"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
P_PurchaseGetAll = "lti.Purchase.list"
|
||||||
|
P_PurchaseGetOne = "lti.Purchase.detail"
|
||||||
|
P_PurchaseCreateOne = "lti.Purchase.create"
|
||||||
|
P_PurchaseUpdateOne = "lti.Purchase.update"
|
||||||
|
P_PurchaseDeleteOne = "lti.Purchase.delete"
|
||||||
|
P_PurchaseItemDeleteOne = "lti.Purchase.delete.item"
|
||||||
|
P_PurchaseReceive = "lti.Purchase.receive"
|
||||||
|
P_PurchaseApprovalStaff = "lti.Purchase.approve.staff"
|
||||||
|
P_PurchaseApprovalManager = "lti.Purchase.approve.manager"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
P_FinanceGetAll = "lti.finance.list"
|
||||||
|
P_FinanceGetOne = "lti.finance.detail"
|
||||||
|
P_FinanceCreateOne = "lti.finance.create"
|
||||||
|
P_FinanceUpdateOne = "lti.finance.update"
|
||||||
|
P_FinanceDeleteOne = "lti.finance.delete"
|
||||||
|
P_FinanceApproval = "lti.finance.approve"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
P_UserGetAll = "lti.users.list"
|
||||||
|
P_UserGetOne = "lti.users.detail"
|
||||||
|
)
|
||||||
|
|||||||
@@ -15,5 +15,5 @@ func ApprovalRoutes(v1 fiber.Router, u user.UserService, s common.ApprovalServic
|
|||||||
route := v1.Group("/approvals")
|
route := v1.Group("/approvals")
|
||||||
route.Use(m.Auth(u))
|
route.Use(m.Auth(u))
|
||||||
|
|
||||||
route.Get("/", ctrl.GetAll)
|
route.Get("/", ctrl.GetAll,m.RequirePermissions(m.P_ApprovalGetAll))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -245,3 +245,109 @@ func (u *ClosingController) GetSapronakByKandang(c *fiber.Ctx) error {
|
|||||||
Data: payload,
|
Data: payload,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *ClosingController) GetClosingKeuangan(c *fiber.Ctx) error {
|
||||||
|
param := c.Params("project_flock_id")
|
||||||
|
|
||||||
|
projectFlockID, err := strconv.Atoi(param)
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid Project Flock Id")
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := u.ClosingService.GetClosingKeuangan(c, uint(projectFlockID))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusOK).
|
||||||
|
JSON(response.Success{
|
||||||
|
Code: fiber.StatusOK,
|
||||||
|
Status: "success",
|
||||||
|
Message: "Get closing keuangan successfully",
|
||||||
|
Data: result,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *ClosingController) GetExpeditionHPP(c *fiber.Ctx) error {
|
||||||
|
param := c.Params("project_flock_id")
|
||||||
|
|
||||||
|
projectFlockID, err := strconv.Atoi(param)
|
||||||
|
if err != nil || projectFlockID <= 0 {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid project_flock_id")
|
||||||
|
}
|
||||||
|
|
||||||
|
var projectFlockKandangID *uint
|
||||||
|
if raw := c.Query("project_flock_kandang_id"); raw != "" {
|
||||||
|
idInt, convErr := strconv.Atoi(raw)
|
||||||
|
if convErr != nil || idInt <= 0 {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid project_flock_kandang_id")
|
||||||
|
}
|
||||||
|
idUint := uint(idInt)
|
||||||
|
projectFlockKandangID = &idUint
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := u.ClosingService.GetExpeditionHPP(c, uint(projectFlockID), projectFlockKandangID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusOK).
|
||||||
|
JSON(response.Success{
|
||||||
|
Code: fiber.StatusOK,
|
||||||
|
Status: "success",
|
||||||
|
Message: "Get expedition HPP successfully",
|
||||||
|
Data: result,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *ClosingController) GetExpeditionHPPByKandang(c *fiber.Ctx) error {
|
||||||
|
projectParam := c.Params("project_flock_id")
|
||||||
|
kandangParam := c.Params("project_flock_kandang_id")
|
||||||
|
|
||||||
|
projectFlockID, err := strconv.Atoi(projectParam)
|
||||||
|
if err != nil || projectFlockID <= 0 {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid project_flock_id")
|
||||||
|
}
|
||||||
|
|
||||||
|
pfkID, err := strconv.Atoi(kandangParam)
|
||||||
|
if err != nil || pfkID <= 0 {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid project_flock_kandang_id")
|
||||||
|
}
|
||||||
|
|
||||||
|
kandangID := uint(pfkID)
|
||||||
|
|
||||||
|
result, err := u.ClosingService.GetExpeditionHPP(c, uint(projectFlockID), &kandangID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusOK).
|
||||||
|
JSON(response.Success{
|
||||||
|
Code: fiber.StatusOK,
|
||||||
|
Status: "success",
|
||||||
|
Message: "Get expedition HPP successfully",
|
||||||
|
Data: result,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *ClosingController) GetClosingDataProduksi(c *fiber.Ctx) error {
|
||||||
|
param := c.Params("projectFlockId")
|
||||||
|
|
||||||
|
id, err := strconv.Atoi(param)
|
||||||
|
if err != nil || id <= 0 {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid projectFlockId")
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := u.ClosingService.GetClosingDataProduksi(c, uint(id))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusOK).
|
||||||
|
JSON(response.Success{
|
||||||
|
Code: fiber.StatusOK,
|
||||||
|
Status: "success",
|
||||||
|
Message: "Retrieved production data successfully",
|
||||||
|
Data: result,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -58,6 +58,52 @@ type ClosingSummaryDTO struct {
|
|||||||
StatusClosing string `json:"closing_status"`
|
StatusClosing string `json:"closing_status"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ClosingPurchaseDTO struct {
|
||||||
|
InitialPopulation int `json:"initial_population"`
|
||||||
|
ClaimCulling int `json:"claim_culling"`
|
||||||
|
FinalPopulation int `json:"final_population"`
|
||||||
|
FeedIn float64 `json:"feed_in"`
|
||||||
|
FeedUsed float64 `json:"feed_used"`
|
||||||
|
FeedUsedPerHead float64 `json:"feed_used_per_head"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ClosingSalesDTO struct {
|
||||||
|
SalesPopulation int `json:"sales_population"`
|
||||||
|
SalesWeight float64 `json:"sales_weight"`
|
||||||
|
AverageWeight float64 `json:"average_weight"`
|
||||||
|
AverageSellingPrice float64 `json:"chicken_average_selling_price"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ClosingEggSalesDTO struct {
|
||||||
|
EggPieces int `json:"egg_pieces"`
|
||||||
|
EggMassKg float64 `json:"egg_mass_kg"`
|
||||||
|
AverageEggWeightKg float64 `json:"average_egg_weight_kg"`
|
||||||
|
AverageSellingPrice float64 `json:"egg_average_selling_price"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ClosingPerformanceDTO struct {
|
||||||
|
Depletion float64 `json:"depletion"`
|
||||||
|
Age float64 `json:"age_day"`
|
||||||
|
MortalityStd float64 `json:"mortality_std"`
|
||||||
|
MortalityAct float64 `json:"mortality_act"`
|
||||||
|
DeffMortality float64 `json:"deff_mortality"`
|
||||||
|
FcrStd float64 `json:"fcr_std"`
|
||||||
|
FcrAct float64 `json:"fcr_act"`
|
||||||
|
DeffFcr float64 `json:"deff_fcr"`
|
||||||
|
Awg float64 `json:"awg"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ClosingSalesGroupDTO struct {
|
||||||
|
Chicken ClosingSalesDTO `json:"chicken"`
|
||||||
|
Egg *ClosingEggSalesDTO `json:"egg,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ClosingProductionReportDTO struct {
|
||||||
|
Purchase ClosingPurchaseDTO `json:"purchase"`
|
||||||
|
Sales ClosingSalesGroupDTO `json:"sales"`
|
||||||
|
Performance ClosingPerformanceDTO `json:"performance"`
|
||||||
|
}
|
||||||
|
|
||||||
func ToClosingSummaryDTO(project entity.ProjectFlock, statusProject, statusClosing string) ClosingSummaryDTO {
|
func ToClosingSummaryDTO(project entity.ProjectFlock, statusProject, statusClosing string) ClosingSummaryDTO {
|
||||||
history := project.KandangHistory
|
history := project.KandangHistory
|
||||||
|
|
||||||
@@ -158,3 +204,20 @@ func ToClosingDetailDTO(e entity.ProjectFlock) ClosingDetailDTO {
|
|||||||
ClosingListDTO: ToClosingListDTO(e),
|
ClosingListDTO: ToClosingListDTO(e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CalculateAgeFromChickinDataProduksi(projectFlockKandang *entity.ProjectFlockKandang, deliveryDate *time.Time) int {
|
||||||
|
if projectFlockKandang == nil || deliveryDate == nil || len(projectFlockKandang.Chickins) == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
earliestChickinDate := projectFlockKandang.Chickins[0].ChickInDate
|
||||||
|
for _, chickin := range projectFlockKandang.Chickins {
|
||||||
|
if chickin.ChickInDate.Before(earliestChickinDate) {
|
||||||
|
earliestChickinDate = chickin.ChickInDate
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ageInDays := int(deliveryDate.Sub(earliestChickinDate).Hours() / 24)
|
||||||
|
ageInWeeks := ageInDays / 7
|
||||||
|
return ageInWeeks
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package dto
|
||||||
|
|
||||||
|
// ExpeditionCostItemDTO merepresentasikan biaya ekspedisi per vendor.
|
||||||
|
type ExpeditionCostItemDTO struct {
|
||||||
|
Id uint64 `json:"id"`
|
||||||
|
ExpeditionVendorName string `json:"expedition_vendor_name"`
|
||||||
|
HPPAmount float64 `json:"hpp_amount"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExpeditionHPPDTO adalah struktur response utama untuk HPP Ekspedisi.
|
||||||
|
type ExpeditionHPPDTO struct {
|
||||||
|
ExpeditionCosts []ExpeditionCostItemDTO `json:"expedition_costs"`
|
||||||
|
TotalHPPAmount float64 `json:"total_hpp_amount"`
|
||||||
|
}
|
||||||
@@ -0,0 +1,568 @@
|
|||||||
|
package dto
|
||||||
|
|
||||||
|
import (
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
// === CONSTANTS ===
|
||||||
|
const (
|
||||||
|
HPPGroupPengeluaran = "HPP dan Pengeluaran"
|
||||||
|
HPPGroupBahanBaku = "HPP dan Bahan Baku"
|
||||||
|
HPPLabelOverhead = "Pengeluaran Overhead"
|
||||||
|
HPPLabelEkspedisi = "Beban Ekspedisi"
|
||||||
|
HPPSummaryLabel = "HPP"
|
||||||
|
|
||||||
|
PLSalesTypeChicken = "Penjualan Ayam Besar"
|
||||||
|
PLSalesTypeEgg = "Penjualan Telur"
|
||||||
|
|
||||||
|
PLItemTypeSapronak = "Pembelian Sapronak"
|
||||||
|
PLItemTypeOverhead = "Pengeluaran Overhead"
|
||||||
|
PLItemTypeEkspedisi = "Beban Ekspedisi"
|
||||||
|
|
||||||
|
PLSummaryLabelGrossProfit = "LABA RUGI BRUTTO"
|
||||||
|
PLSummaryLabelSubTotal = "SUB TOTAL"
|
||||||
|
PLSummaryLabelNetProfit = "LABA RUGI NETTO"
|
||||||
|
|
||||||
|
PurchaseLabelPrefix = "Pembelian "
|
||||||
|
)
|
||||||
|
|
||||||
|
// === CONTEXT STRUCTS ===
|
||||||
|
|
||||||
|
type CalculationContext struct {
|
||||||
|
TotalPopulation float64
|
||||||
|
TotalWeightProduced float64
|
||||||
|
TotalDepletion float64
|
||||||
|
TotalWeightSold float64
|
||||||
|
ActualPopulation float64
|
||||||
|
}
|
||||||
|
|
||||||
|
type ClosingKeuanganInput struct {
|
||||||
|
ProjectFlockCategory string
|
||||||
|
PurchaseItems []entities.PurchaseItem
|
||||||
|
Budgets []entities.ProjectBudget
|
||||||
|
Realizations []entities.ExpenseRealization
|
||||||
|
DeliveryProducts []entities.MarketingDeliveryProduct
|
||||||
|
Chickins []entities.ProjectChickin
|
||||||
|
TotalWeightProduced float64
|
||||||
|
TotalDepletion float64
|
||||||
|
}
|
||||||
|
|
||||||
|
// === BASE METRICS ===
|
||||||
|
|
||||||
|
type FinancialMetrics struct {
|
||||||
|
RpPerBird float64 `json:"rp_per_bird"`
|
||||||
|
RpPerKg float64 `json:"rp_per_kg"`
|
||||||
|
Amount float64 `json:"amount"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Comparison struct {
|
||||||
|
Budgeting FinancialMetrics `json:"budgeting"`
|
||||||
|
Realization FinancialMetrics `json:"realization"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// === HPP PURCHASES PACKAGE ===
|
||||||
|
|
||||||
|
type HppItem struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
Comparison
|
||||||
|
}
|
||||||
|
|
||||||
|
type HppGroup struct {
|
||||||
|
GroupName string `json:"group_name"`
|
||||||
|
Data []HppItem `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type SummaryHpp struct {
|
||||||
|
Label string `json:"label"`
|
||||||
|
Comparison
|
||||||
|
}
|
||||||
|
|
||||||
|
type HppPurchasesSection struct {
|
||||||
|
Hpp []HppGroup `json:"hpp"`
|
||||||
|
SummaryHpp SummaryHpp `json:"summary_hpp"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// === PROFIT LOSS PACKAGE ===
|
||||||
|
|
||||||
|
type PLItem struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
FinancialMetrics
|
||||||
|
}
|
||||||
|
|
||||||
|
type PLSummaryItem struct {
|
||||||
|
Label string `json:"label"`
|
||||||
|
FinancialMetrics
|
||||||
|
}
|
||||||
|
|
||||||
|
type PLSummaryGroup struct {
|
||||||
|
GrossProfit PLSummaryItem `json:"gross_profit"`
|
||||||
|
SubTotal PLSummaryItem `json:"sub_total"`
|
||||||
|
NetProfit PLSummaryItem `json:"net_profit"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ProfitLossData struct {
|
||||||
|
Penjualan []PLItem `json:"penjualan"`
|
||||||
|
Pembelian []PLItem `json:"pembelian"`
|
||||||
|
Overhead PLItem `json:"overhead"`
|
||||||
|
Ekspedisi PLItem `json:"ekspedisi"`
|
||||||
|
Summary PLSummaryGroup `json:"summary"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ProfitLossSection struct {
|
||||||
|
Data ProfitLossData `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// === RESPONSE DTO (ROOT) ===
|
||||||
|
|
||||||
|
type ReportResponse struct {
|
||||||
|
HppPurchases HppPurchasesSection `json:"hpp_purchases"`
|
||||||
|
ProfitLoss ProfitLossSection `json:"profit_loss"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// === MAPPER FUNCTIONS ===
|
||||||
|
|
||||||
|
func ToFinancialMetrics(rpPerBird, rpPerKg, amount float64) FinancialMetrics {
|
||||||
|
return FinancialMetrics{
|
||||||
|
RpPerBird: rpPerBird,
|
||||||
|
RpPerKg: rpPerKg,
|
||||||
|
Amount: amount,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToComparison(budgeting, realization FinancialMetrics) Comparison {
|
||||||
|
return Comparison{
|
||||||
|
Budgeting: budgeting,
|
||||||
|
Realization: realization,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// === HPP PENGELUARAN (from Purchase Items) ===
|
||||||
|
|
||||||
|
func getFlagLabel(flagType utils.FlagType) string {
|
||||||
|
return PurchaseLabelPrefix + string(flagType)
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildHppItemsByPurchaseFlags(purchaseItems []entities.PurchaseItem, ctx CalculationContext) []HppItem {
|
||||||
|
flags := []utils.FlagType{
|
||||||
|
utils.FlagDOC, utils.FlagPullet, utils.FlagLayer, utils.FlagPakan,
|
||||||
|
utils.FlagPreStarter, utils.FlagStarter, utils.FlagFinisher,
|
||||||
|
utils.FlagOVK, utils.FlagObat, utils.FlagVitamin, utils.FlagKimia,
|
||||||
|
}
|
||||||
|
|
||||||
|
items := []HppItem{}
|
||||||
|
seenFlags := make(map[utils.FlagType]bool)
|
||||||
|
|
||||||
|
for _, item := range purchaseItems {
|
||||||
|
if item.Product == nil || len(item.Product.Flags) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, flag := range item.Product.Flags {
|
||||||
|
flagType := utils.FlagType(flag.Name)
|
||||||
|
|
||||||
|
if slices.Contains(flags, flagType) && !seenFlags[flagType] {
|
||||||
|
amount := sumPurchasesByFlag(purchaseItems, flagType)
|
||||||
|
rpPerBird, rpPerKg := calculatePerUnitMetrics(amount, ctx.TotalPopulation, ctx.TotalWeightProduced)
|
||||||
|
|
||||||
|
items = append(items, HppItem{
|
||||||
|
Type: getFlagLabel(flagType),
|
||||||
|
Comparison: ToComparison(
|
||||||
|
ToFinancialMetrics(rpPerBird, rpPerKg, amount),
|
||||||
|
ToFinancialMetrics(rpPerBird, rpPerKg, amount),
|
||||||
|
),
|
||||||
|
})
|
||||||
|
seenFlags[flagType] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return items
|
||||||
|
}
|
||||||
|
|
||||||
|
// === HPP BAHAN BAKU (from ProjectBudget + ExpenseRealization) ===
|
||||||
|
|
||||||
|
func createHppOverheadItem(budgetAmount, realizationAmount float64, ctx CalculationContext) HppItem {
|
||||||
|
budgetRpPerBird, budgetRpPerKg := calculatePerUnitMetrics(budgetAmount, ctx.TotalPopulation, ctx.TotalWeightProduced)
|
||||||
|
realizationRpPerBird, realizationRpPerKg := calculatePerUnitMetrics(realizationAmount, ctx.TotalPopulation, ctx.TotalWeightProduced)
|
||||||
|
|
||||||
|
return HppItem{
|
||||||
|
Type: HPPLabelOverhead,
|
||||||
|
Comparison: ToComparison(
|
||||||
|
ToFinancialMetrics(budgetRpPerBird, budgetRpPerKg, budgetAmount),
|
||||||
|
ToFinancialMetrics(realizationRpPerBird, realizationRpPerKg, realizationAmount),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func createHppEkspedisiItem(ekspedisiAmount float64, ctx CalculationContext) HppItem {
|
||||||
|
ekspedisiRpPerBird, ekspedisiRpPerKg := calculatePerUnitMetrics(ekspedisiAmount, ctx.TotalPopulation, ctx.TotalWeightProduced)
|
||||||
|
|
||||||
|
return HppItem{
|
||||||
|
Type: HPPLabelEkspedisi,
|
||||||
|
Comparison: ToComparison(
|
||||||
|
ToFinancialMetrics(ekspedisiRpPerBird, ekspedisiRpPerKg, ekspedisiAmount),
|
||||||
|
ToFinancialMetrics(ekspedisiRpPerBird, ekspedisiRpPerKg, ekspedisiAmount),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToHppBahanBakuGroup(budgets []entities.ProjectBudget, realizations []entities.ExpenseRealization, ctx CalculationContext) HppGroup {
|
||||||
|
items := []HppItem{}
|
||||||
|
|
||||||
|
budgetAmount := sumBudgetsByFilter(budgets, func(*entities.ProjectBudget) bool { return true })
|
||||||
|
realizationAmount := getOperationalExpenses(realizations)
|
||||||
|
|
||||||
|
if budgetAmount > 0 || realizationAmount > 0 {
|
||||||
|
items = append(items, createHppOverheadItem(budgetAmount, realizationAmount, ctx))
|
||||||
|
}
|
||||||
|
|
||||||
|
ekspedisiAmount := sumRealizationsByFilter(realizations, filterRealizationByNonstockFlag(utils.FlagEkspedisi))
|
||||||
|
items = append(items, createHppEkspedisiItem(ekspedisiAmount, ctx))
|
||||||
|
|
||||||
|
return HppGroup{
|
||||||
|
GroupName: HPPGroupBahanBaku,
|
||||||
|
Data: items,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// === HPP SUMMARY ===
|
||||||
|
|
||||||
|
func ToSummaryHpp(label string, purchaseItems []entities.PurchaseItem, budgets []entities.ProjectBudget, realizations []entities.ExpenseRealization, ctx CalculationContext) SummaryHpp {
|
||||||
|
purchaseTotal := sumPurchaseTotal(purchaseItems)
|
||||||
|
budgetTotal := sumBudgetsByFilter(budgets, func(*entities.ProjectBudget) bool { return true })
|
||||||
|
totalBudget := purchaseTotal + budgetTotal
|
||||||
|
|
||||||
|
totalRealization := sumRealizationsByFilter(realizations, func(*entities.ExpenseRealization) bool { return true })
|
||||||
|
|
||||||
|
budgetRpPerBird, budgetRpPerKg := calculatePerUnitMetrics(totalBudget, ctx.TotalPopulation, ctx.TotalWeightProduced)
|
||||||
|
realizationRpPerBird, realizationRpPerKg := calculatePerUnitMetrics(totalRealization, ctx.TotalPopulation, ctx.TotalWeightProduced)
|
||||||
|
|
||||||
|
return SummaryHpp{
|
||||||
|
Label: label,
|
||||||
|
Comparison: ToComparison(
|
||||||
|
ToFinancialMetrics(budgetRpPerBird, budgetRpPerKg, totalBudget),
|
||||||
|
ToFinancialMetrics(realizationRpPerBird, realizationRpPerKg, totalRealization),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToHppPurchasesSection(purchaseItems []entities.PurchaseItem, budgets []entities.ProjectBudget, realizations []entities.ExpenseRealization, ctx CalculationContext) HppPurchasesSection {
|
||||||
|
hppGroups := []HppGroup{
|
||||||
|
{
|
||||||
|
GroupName: HPPGroupPengeluaran,
|
||||||
|
Data: buildHppItemsByPurchaseFlags(purchaseItems, ctx),
|
||||||
|
},
|
||||||
|
ToHppBahanBakuGroup(budgets, realizations, ctx),
|
||||||
|
}
|
||||||
|
|
||||||
|
summaryHpp := ToSummaryHpp(HPPSummaryLabel, purchaseItems, budgets, realizations, ctx)
|
||||||
|
|
||||||
|
return HppPurchasesSection{
|
||||||
|
Hpp: hppGroups,
|
||||||
|
SummaryHpp: summaryHpp,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// === PROFIT & LOSS ===
|
||||||
|
|
||||||
|
func ToPLItem(itemType string, metrics FinancialMetrics) PLItem {
|
||||||
|
return PLItem{
|
||||||
|
Type: itemType,
|
||||||
|
FinancialMetrics: metrics,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToPLSummaryItem(label string, metrics FinancialMetrics) PLSummaryItem {
|
||||||
|
return PLSummaryItem{
|
||||||
|
Label: label,
|
||||||
|
FinancialMetrics: metrics,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func createPLItemWithMetrics(itemType string, amount float64, ctx CalculationContext) PLItem {
|
||||||
|
rpPerBird, rpPerKg := calculatePerUnitMetrics(amount, ctx.ActualPopulation, ctx.TotalWeightProduced)
|
||||||
|
return ToPLItem(itemType, ToFinancialMetrics(rpPerBird, rpPerKg, amount))
|
||||||
|
}
|
||||||
|
|
||||||
|
func sumPLItems(items []PLItem) (totalAmount, totalPerBird float64) {
|
||||||
|
for _, item := range items {
|
||||||
|
totalAmount += item.Amount
|
||||||
|
totalPerBird += item.RpPerBird
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func createPenjualanItem(salesType string, amount float64, ctx CalculationContext) PLItem {
|
||||||
|
rpPerBird, rpPerKg := calculatePerUnitMetrics(amount, ctx.ActualPopulation, ctx.TotalWeightSold)
|
||||||
|
return ToPLItem(salesType, ToFinancialMetrics(rpPerBird, rpPerKg, amount))
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToPenjualanItems(projectFlockCategory string, deliveryProducts []entities.MarketingDeliveryProduct, ctx CalculationContext) []PLItem {
|
||||||
|
items := []PLItem{}
|
||||||
|
|
||||||
|
categorized := categorizeDeliveriesBySalesType(deliveryProducts)
|
||||||
|
|
||||||
|
if projectFlockCategory == string(utils.ProjectFlockCategoryLaying) {
|
||||||
|
ayamAmount := sumDeliveriesByCategory(categorized[PLSalesTypeChicken])
|
||||||
|
telurAmount := sumDeliveriesByCategory(categorized[PLSalesTypeEgg])
|
||||||
|
|
||||||
|
items = append(items, createPenjualanItem(PLSalesTypeChicken, ayamAmount, ctx))
|
||||||
|
items = append(items, createPenjualanItem(PLSalesTypeEgg, telurAmount, ctx))
|
||||||
|
} else {
|
||||||
|
ayamAmount := sumDeliveriesByCategory(categorized[PLSalesTypeChicken])
|
||||||
|
items = append(items, createPenjualanItem(PLSalesTypeChicken, ayamAmount, ctx))
|
||||||
|
}
|
||||||
|
|
||||||
|
return items
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToPembelianItems(purchases []entities.PurchaseItem, realizations []entities.ExpenseRealization, ctx CalculationContext) []PLItem {
|
||||||
|
purchaseAmount := sumPurchaseTotal(purchases)
|
||||||
|
bopAmount := getOperationalExpenses(realizations)
|
||||||
|
totalCost := purchaseAmount + bopAmount
|
||||||
|
|
||||||
|
return []PLItem{
|
||||||
|
createPLItemWithMetrics(PLItemTypeSapronak, totalCost, ctx),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToOverheadItems(realizations []entities.ExpenseRealization, ctx CalculationContext) []PLItem {
|
||||||
|
realizationAmount := getOperationalExpenses(realizations)
|
||||||
|
return []PLItem{
|
||||||
|
createPLItemWithMetrics(PLItemTypeOverhead, realizationAmount, ctx),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToEkspedisiItems(realizations []entities.ExpenseRealization, ctx CalculationContext) []PLItem {
|
||||||
|
amount := sumRealizationsByFilter(realizations, filterRealizationByNonstockFlag(utils.FlagEkspedisi))
|
||||||
|
return []PLItem{
|
||||||
|
createPLItemWithMetrics(PLItemTypeEkspedisi, amount, ctx),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToPLSummaryGroup(penjualanItems, pembelianItems, overheadItems, ekspedisiItems []PLItem) PLSummaryGroup {
|
||||||
|
totalPenjualan, totalPenjualanPerBird := sumPLItems(penjualanItems)
|
||||||
|
totalPembelian, totalPembelianPerBird := sumPLItems(pembelianItems)
|
||||||
|
totalOverhead, totalOverheadPerBird := sumPLItems(overheadItems)
|
||||||
|
totalEkspedisi, totalEkspedisiPerBird := sumPLItems(ekspedisiItems)
|
||||||
|
|
||||||
|
grossProfit := totalPenjualan - totalPembelian
|
||||||
|
grossProfitPerBird := totalPenjualanPerBird - totalPembelianPerBird
|
||||||
|
|
||||||
|
totalOtherExpenses := totalOverhead + totalEkspedisi
|
||||||
|
totalOtherExpensesPerBird := totalOverheadPerBird + totalEkspedisiPerBird
|
||||||
|
|
||||||
|
netProfit := grossProfit - totalOtherExpenses
|
||||||
|
netProfitPerBird := grossProfitPerBird - totalOtherExpensesPerBird
|
||||||
|
|
||||||
|
return PLSummaryGroup{
|
||||||
|
GrossProfit: ToPLSummaryItem(PLSummaryLabelGrossProfit, ToFinancialMetrics(grossProfitPerBird, 0, grossProfit)),
|
||||||
|
SubTotal: ToPLSummaryItem(PLSummaryLabelSubTotal, ToFinancialMetrics(totalOtherExpensesPerBird, 0, totalOtherExpenses)),
|
||||||
|
NetProfit: ToPLSummaryItem(PLSummaryLabelNetProfit, ToFinancialMetrics(netProfitPerBird, 0, netProfit)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToProfitLossData(penjualanItems, pembelianItems, overheadItems, ekspedisiItems []PLItem) ProfitLossData {
|
||||||
|
summary := ToPLSummaryGroup(penjualanItems, pembelianItems, overheadItems, ekspedisiItems)
|
||||||
|
|
||||||
|
totalOverhead := aggregatePLItems(overheadItems, PLItemTypeOverhead)
|
||||||
|
totalEkspedisi := aggregatePLItems(ekspedisiItems, PLItemTypeEkspedisi)
|
||||||
|
|
||||||
|
return ProfitLossData{
|
||||||
|
Penjualan: penjualanItems,
|
||||||
|
Pembelian: pembelianItems,
|
||||||
|
Overhead: totalOverhead,
|
||||||
|
Ekspedisi: totalEkspedisi,
|
||||||
|
Summary: summary,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToProfitLossSection(penjualanItems, pembelianItems, overheadItems, ekspedisiItems []PLItem) ProfitLossSection {
|
||||||
|
return ProfitLossSection{
|
||||||
|
Data: ToProfitLossData(penjualanItems, pembelianItems, overheadItems, ekspedisiItems),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func aggregatePLItems(items []PLItem, label string) PLItem {
|
||||||
|
totalAmount, totalPerBird := sumPLItems(items)
|
||||||
|
return ToPLItem(label, ToFinancialMetrics(totalPerBird, 0, totalAmount))
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToReportResponse(hppPurchases HppPurchasesSection, profitLoss ProfitLossSection) ReportResponse {
|
||||||
|
return ReportResponse{
|
||||||
|
HppPurchases: hppPurchases,
|
||||||
|
ProfitLoss: profitLoss,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToClosingKeuanganReport(input ClosingKeuanganInput) ReportResponse {
|
||||||
|
var totalPopulation float64
|
||||||
|
var totalWeightSold float64
|
||||||
|
|
||||||
|
for _, chickin := range input.Chickins {
|
||||||
|
totalPopulation += chickin.UsageQty
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, delivery := range input.DeliveryProducts {
|
||||||
|
totalWeightSold += delivery.TotalWeight
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := CalculationContext{
|
||||||
|
TotalPopulation: totalPopulation,
|
||||||
|
TotalWeightProduced: input.TotalWeightProduced,
|
||||||
|
TotalDepletion: input.TotalDepletion,
|
||||||
|
TotalWeightSold: totalWeightSold,
|
||||||
|
ActualPopulation: totalPopulation - input.TotalDepletion,
|
||||||
|
}
|
||||||
|
|
||||||
|
hppSection := ToHppPurchasesSection(input.PurchaseItems, input.Budgets, input.Realizations, ctx)
|
||||||
|
penjualanItems := ToPenjualanItems(input.ProjectFlockCategory, input.DeliveryProducts, ctx)
|
||||||
|
pembelianItems := ToPembelianItems(input.PurchaseItems, input.Realizations, ctx)
|
||||||
|
overheadItems := ToOverheadItems(input.Realizations, ctx)
|
||||||
|
ekspedisiItems := ToEkspedisiItems(input.Realizations, ctx)
|
||||||
|
plSection := ToProfitLossSection(penjualanItems, pembelianItems, overheadItems, ekspedisiItems)
|
||||||
|
|
||||||
|
return ToReportResponse(hppSection, plSection)
|
||||||
|
}
|
||||||
|
|
||||||
|
// === HELPER FUNCTIONS ===
|
||||||
|
|
||||||
|
func calculatePerUnitMetrics(amount, totalPopulation, totalWeightSold float64) (rpPerBird, rpPerKg float64) {
|
||||||
|
if totalPopulation > 0 {
|
||||||
|
rpPerBird = amount / totalPopulation
|
||||||
|
}
|
||||||
|
if totalWeightSold > 0 {
|
||||||
|
rpPerKg = amount / totalWeightSold
|
||||||
|
}
|
||||||
|
return rpPerBird, rpPerKg
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasProductFlag(flags []entities.Flag, flagType utils.FlagType) bool {
|
||||||
|
for _, flag := range flags {
|
||||||
|
if strings.ToUpper(flag.Name) == string(flagType) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func filterByPurchaseFlag(flagType utils.FlagType) func(*entities.PurchaseItem) bool {
|
||||||
|
return func(item *entities.PurchaseItem) bool {
|
||||||
|
if item.Product == nil || len(item.Product.Flags) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return hasProductFlag(item.Product.Flags, flagType)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func filterRealizationByNonstockFlag(flagType utils.FlagType) func(*entities.ExpenseRealization) bool {
|
||||||
|
return func(realization *entities.ExpenseRealization) bool {
|
||||||
|
if realization.ExpenseNonstock == nil || realization.ExpenseNonstock.Nonstock == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return hasProductFlag(realization.ExpenseNonstock.Nonstock.Flags, flagType)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func filterRealizationExceptFlag(flagType utils.FlagType) func(*entities.ExpenseRealization) bool {
|
||||||
|
hasFlag := filterRealizationByNonstockFlag(flagType)
|
||||||
|
return func(realization *entities.ExpenseRealization) bool {
|
||||||
|
return !hasFlag(realization)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func sumByFilter[T any](items []T, extractor func(*T) float64, filter func(*T) bool) float64 {
|
||||||
|
amount := 0.0
|
||||||
|
for i := range items {
|
||||||
|
if filter(&items[i]) {
|
||||||
|
amount += extractor(&items[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return amount
|
||||||
|
}
|
||||||
|
|
||||||
|
func sumPurchasesByFilter(purchases []entities.PurchaseItem, filter func(*entities.PurchaseItem) bool) float64 {
|
||||||
|
return sumByFilter(purchases, func(p *entities.PurchaseItem) float64 { return p.TotalPrice }, filter)
|
||||||
|
}
|
||||||
|
|
||||||
|
func sumPurchasesByFlag(purchases []entities.PurchaseItem, flagType utils.FlagType) float64 {
|
||||||
|
return sumPurchasesByFilter(purchases, filterByPurchaseFlag(flagType))
|
||||||
|
}
|
||||||
|
|
||||||
|
func sumPurchaseTotal(purchases []entities.PurchaseItem) float64 {
|
||||||
|
return sumByFilter(purchases, func(p *entities.PurchaseItem) float64 { return p.TotalPrice }, func(*entities.PurchaseItem) bool { return true })
|
||||||
|
}
|
||||||
|
|
||||||
|
func sumBudgetsByFilter(budgets []entities.ProjectBudget, filter func(*entities.ProjectBudget) bool) float64 {
|
||||||
|
return sumByFilter(budgets, func(b *entities.ProjectBudget) float64 { return b.Price * b.Qty }, filter)
|
||||||
|
}
|
||||||
|
|
||||||
|
func sumRealizationsByFilter(realizations []entities.ExpenseRealization, filter func(*entities.ExpenseRealization) bool) float64 {
|
||||||
|
return sumByFilter(realizations, func(r *entities.ExpenseRealization) float64 { return r.Price * r.Qty }, filter)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getOperationalExpenses(realizations []entities.ExpenseRealization) float64 {
|
||||||
|
return sumRealizationsByFilter(realizations, filterRealizationExceptFlag(utils.FlagEkspedisi))
|
||||||
|
}
|
||||||
|
|
||||||
|
func isChickenProductFlag(flagType utils.FlagType) bool {
|
||||||
|
switch flagType {
|
||||||
|
case utils.FlagDOC, utils.FlagPullet, utils.FlagLayer,
|
||||||
|
utils.FlagAyamAfkir, utils.FlagAyamCulling, utils.FlagAyamMati:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func isEggProductFlag(flagType utils.FlagType) bool {
|
||||||
|
switch flagType {
|
||||||
|
case utils.FlagTelur, utils.FlagTelurUtuh, utils.FlagTelurPecah,
|
||||||
|
utils.FlagTelurPutih, utils.FlagTelurRetak:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func getSalesTypeFromProductFlags(product *entities.Product) string {
|
||||||
|
if product == nil || len(product.Flags) == 0 {
|
||||||
|
return PLSalesTypeChicken
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, flag := range product.Flags {
|
||||||
|
flagType := utils.FlagType(strings.ToUpper(flag.Name))
|
||||||
|
|
||||||
|
if isEggProductFlag(flagType) {
|
||||||
|
return PLSalesTypeEgg
|
||||||
|
}
|
||||||
|
if isChickenProductFlag(flagType) {
|
||||||
|
return PLSalesTypeChicken
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return PLSalesTypeChicken
|
||||||
|
}
|
||||||
|
|
||||||
|
func categorizeDeliveriesBySalesType(deliveries []entities.MarketingDeliveryProduct) map[string][]entities.MarketingDeliveryProduct {
|
||||||
|
categorized := make(map[string][]entities.MarketingDeliveryProduct)
|
||||||
|
|
||||||
|
for _, delivery := range deliveries {
|
||||||
|
product := delivery.MarketingProduct.ProductWarehouse.Product
|
||||||
|
salesType := getSalesTypeFromProductFlags(&product)
|
||||||
|
|
||||||
|
categorized[salesType] = append(categorized[salesType], delivery)
|
||||||
|
}
|
||||||
|
|
||||||
|
return categorized
|
||||||
|
}
|
||||||
|
|
||||||
|
func sumDeliveriesByCategory(deliveries []entities.MarketingDeliveryProduct) float64 {
|
||||||
|
amount := 0.0
|
||||||
|
for _, delivery := range deliveries {
|
||||||
|
amount += delivery.TotalPrice
|
||||||
|
}
|
||||||
|
return amount
|
||||||
|
}
|
||||||
@@ -28,18 +28,14 @@ type SalesDTO struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type PenjualanRealisasiResponseDTO struct {
|
type PenjualanRealisasiResponseDTO struct {
|
||||||
ProjectType string `json:"project_type"`
|
Sales []SalesDTO `json:"sales"`
|
||||||
FlockId uint `json:"flock_id"`
|
|
||||||
Period int `json:"period"`
|
|
||||||
Sales []SalesDTO `json:"sales"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// === Mapper Functions ===
|
// === Mapper Functions ===
|
||||||
|
|
||||||
func ToSalesDTO(e entity.MarketingDeliveryProduct) SalesDTO {
|
func ToSalesDTO(e entity.MarketingDeliveryProduct) SalesDTO {
|
||||||
|
|
||||||
// todo: usia ayam masih dummy
|
age := calculateAgeFromChickin(e.MarketingProduct.ProductWarehouse.ProjectFlockKandang, e.DeliveryDate)
|
||||||
age := 0
|
|
||||||
|
|
||||||
var product *productDTO.ProductRelationDTO
|
var product *productDTO.ProductRelationDTO
|
||||||
if e.MarketingProduct.ProductWarehouse.Product.Id != 0 {
|
if e.MarketingProduct.ProductWarehouse.Product.Id != 0 {
|
||||||
@@ -68,7 +64,7 @@ func ToSalesDTO(e entity.MarketingDeliveryProduct) SalesDTO {
|
|||||||
DoNumber: doNumber,
|
DoNumber: doNumber,
|
||||||
Product: product,
|
Product: product,
|
||||||
Customer: customer,
|
Customer: customer,
|
||||||
Qty: e.Qty,
|
Qty: e.UsageQty, // Show allocated quantity from FIFO
|
||||||
Weight: e.TotalWeight,
|
Weight: e.TotalWeight,
|
||||||
AvgWeight: e.AvgWeight,
|
AvgWeight: e.AvgWeight,
|
||||||
Price: e.UnitPrice,
|
Price: e.UnitPrice,
|
||||||
@@ -87,12 +83,10 @@ func ToSalesDTOs(e []entity.MarketingDeliveryProduct) []SalesDTO {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ToPenjualanRealisasiResponseDTO(projectType string, projectFlockID uint, e []entity.MarketingDeliveryProduct) PenjualanRealisasiResponseDTO {
|
func ToPenjualanRealisasiResponseDTO(projectType string, projectFlockID uint, e []entity.MarketingDeliveryProduct) PenjualanRealisasiResponseDTO {
|
||||||
period := extractPeriodFromRealisasi(e)
|
|
||||||
return PenjualanRealisasiResponseDTO{
|
return PenjualanRealisasiResponseDTO{
|
||||||
ProjectType: projectType,
|
|
||||||
FlockId: projectFlockID,
|
Sales: ToSalesDTOs(e),
|
||||||
Period: period,
|
|
||||||
Sales: ToSalesDTOs(e),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,3 +100,20 @@ func extractPeriodFromRealisasi(realisasi []entity.MarketingDeliveryProduct) int
|
|||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func calculateAgeFromChickin(projectFlockKandang *entity.ProjectFlockKandang, deliveryDate *time.Time) int {
|
||||||
|
if projectFlockKandang == nil || deliveryDate == nil || len(projectFlockKandang.Chickins) == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
earliestChickinDate := projectFlockKandang.Chickins[0].ChickInDate
|
||||||
|
for _, chickin := range projectFlockKandang.Chickins {
|
||||||
|
if chickin.ChickInDate.Before(earliestChickinDate) {
|
||||||
|
earliestChickinDate = chickin.ChickInDate
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ageInDays := int(deliveryDate.Sub(earliestChickinDate).Hours() / 24)
|
||||||
|
ageInWeeks := ageInDays / 7
|
||||||
|
return ageInWeeks
|
||||||
|
}
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ func ToOverheadDTO(budget *entity.ProjectBudget, realization *entity.ExpenseReal
|
|||||||
return dto
|
return dto
|
||||||
}
|
}
|
||||||
|
|
||||||
func ToOverheadListDTOs(budgets []entity.ProjectBudget, realizations []entity.ExpenseRealization, totalChickinQty float64) OverheadListDTO {
|
func ToOverheadListDTOs(budgets []entity.ProjectBudget, realizations []entity.ExpenseRealization, totalChickinQty, totalActualPopulation float64) OverheadListDTO {
|
||||||
overheadsByNonstockID := make(map[uint]*OverheadDTO)
|
overheadsByNonstockID := make(map[uint]*OverheadDTO)
|
||||||
latestDateByNonstockID := make(map[uint]string)
|
latestDateByNonstockID := make(map[uint]string)
|
||||||
|
|
||||||
@@ -119,7 +119,8 @@ func ToOverheadListDTOs(budgets []entity.ProjectBudget, realizations []entity.Ex
|
|||||||
|
|
||||||
for nonstockID, overhead := range overheadsByNonstockID {
|
for nonstockID, overhead := range overheadsByNonstockID {
|
||||||
overhead.ActualDate = latestDateByNonstockID[nonstockID]
|
overhead.ActualDate = latestDateByNonstockID[nonstockID]
|
||||||
overhead.CostPerBird = calculateCostPerBird(overhead.ActualTotalAmount, totalChickinQty)
|
|
||||||
|
overhead.CostPerBird = calculateCostPerBird(overhead.ActualTotalAmount, totalActualPopulation)
|
||||||
|
|
||||||
if overhead.ActualQuantity > 0 {
|
if overhead.ActualQuantity > 0 {
|
||||||
overhead.ActualUnitPrice = overhead.ActualTotalAmount / overhead.ActualQuantity
|
overhead.ActualUnitPrice = overhead.ActualTotalAmount / overhead.ActualQuantity
|
||||||
@@ -139,7 +140,7 @@ func ToOverheadListDTOs(budgets []entity.ProjectBudget, realizations []entity.Ex
|
|||||||
BudgetTotalAmount: totalBudgetAmount,
|
BudgetTotalAmount: totalBudgetAmount,
|
||||||
ActualQuantity: totalActualQuantity,
|
ActualQuantity: totalActualQuantity,
|
||||||
ActualTotalAmount: totalActualAmount,
|
ActualTotalAmount: totalActualAmount,
|
||||||
CostPerBird: calculateCostPerBird(totalActualAmount, totalChickinQty),
|
CostPerBird: calculateCostPerBird(totalActualAmount, totalActualPopulation),
|
||||||
},
|
},
|
||||||
Overheads: overheadItems,
|
Overheads: overheadItems,
|
||||||
}
|
}
|
||||||
@@ -158,9 +159,9 @@ func calculateTotal(qty, price float64) float64 {
|
|||||||
return qty * price
|
return qty * price
|
||||||
}
|
}
|
||||||
|
|
||||||
func calculateCostPerBird(totalPrice, totalChickinQty float64) float64 {
|
func calculateCostPerBird(totalPrice, totalActualPopulation float64) float64 {
|
||||||
if totalChickinQty > 0 {
|
if totalActualPopulation > 0 {
|
||||||
return totalPrice / totalChickinQty
|
return totalPrice / totalActualPopulation
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ import (
|
|||||||
rMarketings "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/repositories"
|
rMarketings "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/repositories"
|
||||||
rChickin "gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins/repositories"
|
rChickin "gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins/repositories"
|
||||||
rProjectFlock "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
|
rProjectFlock "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
|
||||||
|
rRecording "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/repositories"
|
||||||
|
rPurchase "gitlab.com/mbugroup/lti-api.git/internal/modules/purchases/repositories"
|
||||||
|
|
||||||
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
|
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
|
||||||
sUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
sUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
||||||
@@ -30,10 +32,12 @@ func (ClosingModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *
|
|||||||
marketingDeliveryProductRepo := rMarketings.NewMarketingDeliveryProductRepository(db)
|
marketingDeliveryProductRepo := rMarketings.NewMarketingDeliveryProductRepository(db)
|
||||||
expenseRealizationRepo := rExpenseRealization.NewExpenseRealizationRepository(db)
|
expenseRealizationRepo := rExpenseRealization.NewExpenseRealizationRepository(db)
|
||||||
chickinRepo := rChickin.NewChickinRepository(db)
|
chickinRepo := rChickin.NewChickinRepository(db)
|
||||||
|
recordingRepo := rRecording.NewRecordingRepository(db)
|
||||||
|
purchaseRepo := rPurchase.NewPurchaseRepository(db)
|
||||||
approvalRepo := commonRepo.NewApprovalRepository(db)
|
approvalRepo := commonRepo.NewApprovalRepository(db)
|
||||||
approvalService := commonSvc.NewApprovalService(approvalRepo)
|
approvalService := commonSvc.NewApprovalService(approvalRepo)
|
||||||
|
|
||||||
closingService := sClosing.NewClosingService(closingRepo, projectFlockRepo, marketingRepo, marketingDeliveryProductRepo, approvalService, expenseRealizationRepo, projectBudgetRepo, chickinRepo, validate)
|
closingService := sClosing.NewClosingService(closingRepo, projectFlockRepo, marketingRepo, marketingDeliveryProductRepo, approvalService, expenseRealizationRepo, projectBudgetRepo, chickinRepo, purchaseRepo, recordingRepo, validate)
|
||||||
sapronakService := sClosing.NewSapronakService(closingRepo, projectFlockKandangRepo, validate)
|
sapronakService := sClosing.NewSapronakService(closingRepo, projectFlockKandangRepo, validate)
|
||||||
userService := sUser.NewUserService(userRepo, validate)
|
userService := sUser.NewUserService(userRepo, validate)
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,13 @@ import (
|
|||||||
type ClosingRepository interface {
|
type ClosingRepository interface {
|
||||||
repository.BaseRepository[entity.ProjectFlock]
|
repository.BaseRepository[entity.ProjectFlock]
|
||||||
GetSapronak(ctx context.Context, params SapronakQueryParams) ([]SapronakRow, int64, error)
|
GetSapronak(ctx context.Context, params SapronakQueryParams) ([]SapronakRow, int64, error)
|
||||||
|
SumFeedPurchaseAndUsedByProjectFlockKandangIDs(ctx context.Context, projectFlockKandangIDs []uint) (float64, float64, error)
|
||||||
|
SumClaimCullingByProjectFlockKandangIDs(ctx context.Context, projectFlockKandangIDs []uint) (float64, error)
|
||||||
|
SumMarketingWeightAndQtyByProjectFlockKandangIDs(ctx context.Context, projectFlockKandangIDs []uint) (float64, float64, float64, error)
|
||||||
|
SumMarketingWeightAndQtyByProjectFlockKandangIDsAndFlagNames(ctx context.Context, projectFlockKandangIDs []uint, flagNames []string) (float64, float64, float64, error)
|
||||||
|
SumRecordingEggQtyByProjectFlockKandangIDsAndFlagNames(ctx context.Context, projectFlockKandangIDs []uint, flagNames []string) (float64, error)
|
||||||
|
GetFcrStandardsByFcrID(ctx context.Context, fcrID uint) ([]entity.FcrStandard, error)
|
||||||
|
GetExpeditionHPP(ctx context.Context, projectFlockID uint, projectFlockKandangID *uint) ([]ExpeditionHPPRow, error)
|
||||||
FetchSapronakIncoming(ctx context.Context, kandangID uint) ([]SapronakIncomingRow, error)
|
FetchSapronakIncoming(ctx context.Context, kandangID uint) ([]SapronakIncomingRow, error)
|
||||||
FetchSapronakIncomingDetails(ctx context.Context, kandangID uint) (map[uint][]SapronakDetailRow, error)
|
FetchSapronakIncomingDetails(ctx context.Context, kandangID uint) (map[uint][]SapronakDetailRow, error)
|
||||||
FetchSapronakUsage(ctx context.Context, pfkID uint) ([]SapronakUsageRow, error)
|
FetchSapronakUsage(ctx context.Context, pfkID uint) ([]SapronakUsageRow, error)
|
||||||
@@ -53,6 +60,11 @@ type SapronakRow struct {
|
|||||||
Notes string `gorm:"column:notes"`
|
Notes string `gorm:"column:notes"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ExpeditionHPPRow struct {
|
||||||
|
SupplierName string `gorm:"column:supplier_name"`
|
||||||
|
TotalAmount float64 `gorm:"column:total_amount"`
|
||||||
|
}
|
||||||
|
|
||||||
type SapronakQueryParams struct {
|
type SapronakQueryParams struct {
|
||||||
Type string
|
Type string
|
||||||
WarehouseIDs []uint
|
WarehouseIDs []uint
|
||||||
@@ -111,6 +123,202 @@ func (r *ClosingRepositoryImpl) GetSapronak(ctx context.Context, params Sapronak
|
|||||||
return rows, totalResults, nil
|
return rows, totalResults, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *ClosingRepositoryImpl) SumFeedPurchaseAndUsedByProjectFlockKandangIDs(ctx context.Context, projectFlockKandangIDs []uint) (float64, float64, error) {
|
||||||
|
if len(projectFlockKandangIDs) == 0 {
|
||||||
|
return 0, 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var purchaseAgg struct {
|
||||||
|
TotalIn float64 `gorm:"column:total_in"`
|
||||||
|
}
|
||||||
|
|
||||||
|
err := r.DB().WithContext(ctx).
|
||||||
|
Table("purchase_items pi").
|
||||||
|
Joins("JOIN flags f ON f.flagable_id = pi.product_id AND f.flagable_type = 'products'").
|
||||||
|
Where("f.name = ?", "PAKAN").
|
||||||
|
Where("pi.project_flock_kandang_id IN ?", projectFlockKandangIDs).
|
||||||
|
Select("COALESCE(SUM(pi.total_qty), 0) AS total_in").
|
||||||
|
Scan(&purchaseAgg).Error
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var usageAgg struct {
|
||||||
|
TotalUsed float64 `gorm:"column:total_used"`
|
||||||
|
}
|
||||||
|
|
||||||
|
err = r.DB().WithContext(ctx).
|
||||||
|
Table("recording_stocks rs").
|
||||||
|
Joins("JOIN product_warehouses pw ON pw.id = rs.product_warehouse_id").
|
||||||
|
Joins("JOIN products prod ON prod.id = pw.product_id").
|
||||||
|
Joins("JOIN flags f ON f.flagable_id = prod.id AND f.flagable_type = ?", "products").
|
||||||
|
Where("pw.project_flock_kandang_id IN ?", projectFlockKandangIDs).
|
||||||
|
Where("f.name = ?", "PAKAN").
|
||||||
|
Select("COALESCE(SUM(COALESCE(rs.usage_qty, 0) + COALESCE(rs.pending_qty, 0)), 0) AS total_used").
|
||||||
|
Scan(&usageAgg).Error
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return purchaseAgg.TotalIn, usageAgg.TotalUsed, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ClosingRepositoryImpl) SumClaimCullingByProjectFlockKandangIDs(ctx context.Context, projectFlockKandangIDs []uint) (float64, error) {
|
||||||
|
if len(projectFlockKandangIDs) == 0 {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var agg struct {
|
||||||
|
Total float64 `gorm:"column:total_culling"`
|
||||||
|
}
|
||||||
|
|
||||||
|
err := r.DB().WithContext(ctx).
|
||||||
|
Table("recording_depletions rd").
|
||||||
|
Joins("JOIN product_warehouses pw ON pw.id = rd.product_warehouse_id").
|
||||||
|
Joins("JOIN products prod ON prod.id = pw.product_id").
|
||||||
|
Joins("JOIN flags f ON f.flagable_id = prod.id AND f.flagable_type = ?", "products").
|
||||||
|
Where("pw.project_flock_kandang_id IN ?", projectFlockKandangIDs).
|
||||||
|
Where("f.name = ?", utils.FlagAyamCulling).
|
||||||
|
Select("COALESCE(SUM(rd.qty), 0) AS total_culling").
|
||||||
|
Scan(&agg).Error
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return agg.Total, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ClosingRepositoryImpl) SumMarketingWeightAndQtyByProjectFlockKandangIDs(ctx context.Context, projectFlockKandangIDs []uint) (float64, float64, float64, error) {
|
||||||
|
if len(projectFlockKandangIDs) == 0 {
|
||||||
|
return 0, 0, 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var agg struct {
|
||||||
|
TotalWeight float64 `gorm:"column:total_weight"`
|
||||||
|
TotalQty float64 `gorm:"column:total_qty"`
|
||||||
|
TotalPrice float64 `gorm:"column:total_price"`
|
||||||
|
}
|
||||||
|
|
||||||
|
err := r.DB().WithContext(ctx).
|
||||||
|
Table("marketing_products mp").
|
||||||
|
Joins("JOIN product_warehouses pw ON pw.id = mp.product_warehouse_id").
|
||||||
|
Where("pw.project_flock_kandang_id IN ?", projectFlockKandangIDs).
|
||||||
|
Select("COALESCE(SUM(mp.total_weight), 0) AS total_weight, COALESCE(SUM(mp.qty), 0) AS total_qty, COALESCE(SUM(mp.total_price), 0) AS total_price").
|
||||||
|
Scan(&agg).Error
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return agg.TotalWeight, agg.TotalQty, agg.TotalPrice, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ClosingRepositoryImpl) SumMarketingWeightAndQtyByProjectFlockKandangIDsAndFlagNames(ctx context.Context, projectFlockKandangIDs []uint, flagNames []string) (float64, float64, float64, error) {
|
||||||
|
if len(projectFlockKandangIDs) == 0 || len(flagNames) == 0 {
|
||||||
|
return 0, 0, 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var agg struct {
|
||||||
|
TotalWeight float64 `gorm:"column:total_weight"`
|
||||||
|
TotalQty float64 `gorm:"column:total_qty"`
|
||||||
|
TotalPrice float64 `gorm:"column:total_price"`
|
||||||
|
}
|
||||||
|
|
||||||
|
err := r.DB().WithContext(ctx).
|
||||||
|
Table("marketing_products mp").
|
||||||
|
Joins("JOIN product_warehouses pw ON pw.id = mp.product_warehouse_id").
|
||||||
|
Joins("JOIN products prod ON prod.id = pw.product_id").
|
||||||
|
Joins("JOIN flags f ON f.flagable_id = prod.id AND f.flagable_type = ?", "products").
|
||||||
|
Where("pw.project_flock_kandang_id IN ?", projectFlockKandangIDs).
|
||||||
|
Where("f.name IN ?", flagNames).
|
||||||
|
Select("COALESCE(SUM(mp.total_weight), 0) AS total_weight, COALESCE(SUM(mp.qty), 0) AS total_qty, COALESCE(SUM(mp.total_price), 0) AS total_price").
|
||||||
|
Scan(&agg).Error
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return agg.TotalWeight, agg.TotalQty, agg.TotalPrice, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ClosingRepositoryImpl) SumRecordingEggQtyByProjectFlockKandangIDsAndFlagNames(ctx context.Context, projectFlockKandangIDs []uint, flagNames []string) (float64, error) {
|
||||||
|
if len(projectFlockKandangIDs) == 0 || len(flagNames) == 0 {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var agg struct {
|
||||||
|
TotalQty float64 `gorm:"column:total_qty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
err := r.DB().WithContext(ctx).
|
||||||
|
Table("recording_eggs re").
|
||||||
|
Joins("JOIN product_warehouses pw ON pw.id = re.product_warehouse_id").
|
||||||
|
Joins("JOIN products prod ON prod.id = pw.product_id").
|
||||||
|
Joins("JOIN flags f ON f.flagable_id = prod.id AND f.flagable_type = ?", "products").
|
||||||
|
Where("pw.project_flock_kandang_id IN ?", projectFlockKandangIDs).
|
||||||
|
Where("f.name IN ?", flagNames).
|
||||||
|
Select("COALESCE(SUM(re.qty), 0) AS total_qty").
|
||||||
|
Scan(&agg).Error
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return agg.TotalQty, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ClosingRepositoryImpl) GetFcrStandardsByFcrID(ctx context.Context, fcrID uint) ([]entity.FcrStandard, error) {
|
||||||
|
if fcrID == 0 {
|
||||||
|
return []entity.FcrStandard{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var standards []entity.FcrStandard
|
||||||
|
if err := r.DB().WithContext(ctx).
|
||||||
|
Where("fcr_id = ?", fcrID).
|
||||||
|
Order("weight ASC").
|
||||||
|
Find(&standards).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return standards, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ClosingRepositoryImpl) GetExpeditionHPP(ctx context.Context, projectFlockID uint, projectFlockKandangID *uint) ([]ExpeditionHPPRow, error) {
|
||||||
|
db := r.DB().WithContext(ctx)
|
||||||
|
|
||||||
|
if projectFlockID == 0 {
|
||||||
|
return nil, fmt.Errorf("invalid project flock id")
|
||||||
|
}
|
||||||
|
|
||||||
|
query := db.
|
||||||
|
Table("expense_realizations AS er").
|
||||||
|
Joins("JOIN expense_nonstocks ens ON ens.id = er.expense_nonstock_id").
|
||||||
|
Joins("JOIN expenses e ON e.id = ens.expense_id").
|
||||||
|
Joins("JOIN project_flock_kandangs pfk ON pfk.id = ens.project_flock_kandang_id").
|
||||||
|
Joins("JOIN nonstocks n ON n.id = ens.nonstock_id").
|
||||||
|
Joins("JOIN flags f ON f.flagable_id = n.id AND f.flagable_type = ?", entity.FlagableTypeNonstock).
|
||||||
|
Joins("JOIN suppliers s ON s.id = e.supplier_id").
|
||||||
|
Where("pfk.project_flock_id = ?", projectFlockID).
|
||||||
|
Where("e.category = ?", "BOP").
|
||||||
|
Where("UPPER(f.name) = ?", strings.ToUpper(string(utils.FlagEkspedisi)))
|
||||||
|
|
||||||
|
if projectFlockKandangID != nil && *projectFlockKandangID != 0 {
|
||||||
|
query = query.Where("pfk.id = ?", *projectFlockKandangID)
|
||||||
|
}
|
||||||
|
|
||||||
|
var rows []ExpeditionHPPRow
|
||||||
|
err := query.
|
||||||
|
Select(
|
||||||
|
"e.supplier_id AS supplier_id, " +
|
||||||
|
"s.name AS supplier_name, " +
|
||||||
|
"SUM(er.qty * er.price) AS total_amount",
|
||||||
|
).
|
||||||
|
Group("e.supplier_id, s.name").
|
||||||
|
Scan(&rows).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return rows, nil
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
sapronakIncomingPurchasesSQL = `
|
sapronakIncomingPurchasesSQL = `
|
||||||
SELECT
|
SELECT
|
||||||
@@ -260,7 +468,6 @@ type SapronakDetailRow struct {
|
|||||||
Price float64
|
Price float64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (r *ClosingRepositoryImpl) withCtx(ctx context.Context) *gorm.DB { return r.DB().WithContext(ctx) }
|
func (r *ClosingRepositoryImpl) withCtx(ctx context.Context) *gorm.DB { return r.DB().WithContext(ctx) }
|
||||||
|
|
||||||
func applyJoins(db *gorm.DB, joins ...string) *gorm.DB {
|
func applyJoins(db *gorm.DB, joins ...string) *gorm.DB {
|
||||||
@@ -323,7 +530,7 @@ func (r *ClosingRepositoryImpl) usageQuery(
|
|||||||
`)
|
`)
|
||||||
db = applyJoins(db, joins...)
|
db = applyJoins(db, joins...)
|
||||||
return db.
|
return db.
|
||||||
Joins("JOIN product_warehouses pw ON " + pwJoinCond).
|
Joins("JOIN product_warehouses pw ON "+pwJoinCond).
|
||||||
Joins("JOIN products p ON p.id = pw.product_id").
|
Joins("JOIN products p ON p.id = pw.product_id").
|
||||||
Joins("JOIN flags f ON f.flagable_id = p.id AND f.flagable_type = ?", entity.FlagableTypeProduct).
|
Joins("JOIN flags f ON f.flagable_id = p.id AND f.flagable_type = ?", entity.FlagableTypeProduct).
|
||||||
Where(where, args...)
|
Where(where, args...)
|
||||||
@@ -356,7 +563,7 @@ func (r *ClosingRepositoryImpl) detailQuery(
|
|||||||
) *gorm.DB {
|
) *gorm.DB {
|
||||||
db := r.withCtx(ctx).
|
db := r.withCtx(ctx).
|
||||||
Table(table).
|
Table(table).
|
||||||
Joins("JOIN product_warehouses pw ON " + pwJoinCond).
|
Joins("JOIN product_warehouses pw ON "+pwJoinCond).
|
||||||
Joins("JOIN products p ON p.id = pw.product_id").
|
Joins("JOIN products p ON p.id = pw.product_id").
|
||||||
Joins("JOIN flags f ON f.flagable_id = p.id AND f.flagable_type = ?", entity.FlagableTypeProduct)
|
Joins("JOIN flags f ON f.flagable_id = p.id AND f.flagable_type = ?", entity.FlagableTypeProduct)
|
||||||
|
|
||||||
@@ -450,7 +657,6 @@ func (r *ClosingRepositoryImpl) FetchSapronakChickinUsageDetails(ctx context.Con
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (r *ClosingRepositoryImpl) incomingPurchaseBase(ctx context.Context, kandangID uint) *gorm.DB {
|
func (r *ClosingRepositoryImpl) incomingPurchaseBase(ctx context.Context, kandangID uint) *gorm.DB {
|
||||||
return r.withCtx(ctx).
|
return r.withCtx(ctx).
|
||||||
Table("purchase_items AS pi").
|
Table("purchase_items AS pi").
|
||||||
@@ -527,7 +733,7 @@ func (r *ClosingRepositoryImpl) fetchStockLogs(ctx context.Context, kandangID ui
|
|||||||
COALESCE(sl.increase,0) AS increase,
|
COALESCE(sl.increase,0) AS increase,
|
||||||
COALESCE(sl.decrease,0) AS decrease,
|
COALESCE(sl.decrease,0) AS decrease,
|
||||||
COALESCE(p.product_price,0) AS price,
|
COALESCE(p.product_price,0) AS price,
|
||||||
` + movementSelect + `
|
`+movementSelect+`
|
||||||
`).
|
`).
|
||||||
Joins("JOIN product_warehouses pw ON pw.id = sl.product_warehouse_id").
|
Joins("JOIN product_warehouses pw ON pw.id = sl.product_warehouse_id").
|
||||||
Joins("JOIN products p ON p.id = pw.product_id").
|
Joins("JOIN products p ON p.id = pw.product_id").
|
||||||
@@ -577,7 +783,7 @@ func splitStockLogs(rows []stockLogSapronakRow, refFn func(stockLogSapronakRow)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *ClosingRepositoryImpl) FetchSapronakAdjustments(ctx context.Context, kandangID uint) (map[uint][]SapronakDetailRow, map[uint][]SapronakDetailRow, error) {
|
func (r *ClosingRepositoryImpl) FetchSapronakAdjustments(ctx context.Context, kandangID uint) (map[uint][]SapronakDetailRow, map[uint][]SapronakDetailRow, error) {
|
||||||
rows, err := r.fetchStockLogs(ctx, kandangID, entity.LogTypeAdjustment, false)
|
rows, err := r.fetchStockLogs(ctx, kandangID, string(utils.StockLogTypeAdjustment), false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
@@ -586,7 +792,7 @@ func (r *ClosingRepositoryImpl) FetchSapronakAdjustments(ctx context.Context, ka
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *ClosingRepositoryImpl) FetchSapronakTransfers(ctx context.Context, kandangID uint) (map[uint][]SapronakDetailRow, map[uint][]SapronakDetailRow, error) {
|
func (r *ClosingRepositoryImpl) FetchSapronakTransfers(ctx context.Context, kandangID uint) (map[uint][]SapronakDetailRow, map[uint][]SapronakDetailRow, error) {
|
||||||
rows, err := r.fetchStockLogs(ctx, kandangID, entity.LogTypeTransfer, true)
|
rows, err := r.fetchStockLogs(ctx, kandangID, string(utils.StockLogTypeTransfer), true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
@@ -597,4 +803,4 @@ func (r *ClosingRepositoryImpl) FetchSapronakTransfers(ctx context.Context, kand
|
|||||||
return fmt.Sprintf("TRF-%d", row.ID)
|
return fmt.Sprintf("TRF-%d", row.ID)
|
||||||
})
|
})
|
||||||
return in, out, nil
|
return in, out, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package closings
|
package closings
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
||||||
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/closings/controllers"
|
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/closings/controllers"
|
||||||
closing "gitlab.com/mbugroup/lti-api.git/internal/modules/closings/services"
|
closing "gitlab.com/mbugroup/lti-api.git/internal/modules/closings/services"
|
||||||
user "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
user "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
||||||
@@ -13,6 +13,7 @@ func ClosingRoutes(v1 fiber.Router, u user.UserService, s closing.ClosingService
|
|||||||
ctrl := controller.NewClosingController(s, sapronakSvc)
|
ctrl := controller.NewClosingController(s, sapronakSvc)
|
||||||
|
|
||||||
route := v1.Group("/closings")
|
route := v1.Group("/closings")
|
||||||
|
route.Use(m.Auth(u))
|
||||||
|
|
||||||
// route.Get("/", m.Auth(u), ctrl.GetAll)
|
// route.Get("/", m.Auth(u), ctrl.GetAll)
|
||||||
// route.Post("/", m.Auth(u), ctrl.CreateOne)
|
// route.Post("/", m.Auth(u), ctrl.CreateOne)
|
||||||
@@ -20,11 +21,15 @@ func ClosingRoutes(v1 fiber.Router, u user.UserService, s closing.ClosingService
|
|||||||
// route.Patch("/:id", m.Auth(u), ctrl.UpdateOne)
|
// route.Patch("/:id", m.Auth(u), ctrl.UpdateOne)
|
||||||
// route.Delete("/:id", m.Auth(u), ctrl.DeleteOne)
|
// route.Delete("/:id", m.Auth(u), ctrl.DeleteOne)
|
||||||
|
|
||||||
route.Get("/", ctrl.GetAll)
|
route.Get("/", m.RequirePermissions(m.P_ClosingGetAll), ctrl.GetAll)
|
||||||
route.Get("/:project_flock_id/penjualan", ctrl.GetPenjualan)
|
route.Get("/:project_flock_id/penjualan", m.RequirePermissions(m.P_ClosingDetail), ctrl.GetPenjualan)
|
||||||
route.Get("/:project_flock_id/overhead", ctrl.GetOverhead)
|
route.Get("/:projectFlockId", m.RequirePermissions(m.P_ClosingDetail), ctrl.GetClosingSummary)
|
||||||
route.Get("/:project_flock_id/:project_flock_kandang_id/perhitungan_sapronak", ctrl.GetSapronakByKandang)
|
route.Get("/:project_flock_id/overhead", m.RequirePermissions(m.P_ClosingDetail), ctrl.GetOverhead)
|
||||||
route.Get("/:project_flock_id/perhitungan_sapronak", ctrl.GetSapronakByProject)
|
route.Get("/:project_flock_id/:project_flock_kandang_id/perhitungan_sapronak", m.RequirePermissions(m.P_ClosingDetail), ctrl.GetSapronakByKandang)
|
||||||
route.Get("/:projectFlockId", ctrl.GetClosingSummary)
|
route.Get("/:project_flock_id/perhitungan_sapronak", m.RequirePermissions(m.P_ClosingDetail), ctrl.GetSapronakByProject)
|
||||||
route.Get("/:projectFlockId/sapronak", ctrl.GetClosingSapronak)
|
route.Get("/:projectFlockId/sapronak", m.RequirePermissions(m.P_ClosingDetail), ctrl.GetClosingSapronak)
|
||||||
|
route.Get("/:project_flock_id/expedition-hpp", m.RequirePermissions(m.P_ClosingDetail), ctrl.GetExpeditionHPP)
|
||||||
|
route.Get("/:project_flock_id/:project_flock_kandang_id/expedition-hpp", m.RequirePermissions(m.P_ClosingDetail), ctrl.GetExpeditionHPPByKandang)
|
||||||
|
route.Get("/:projectFlockId/data-produksi", m.RequirePermissions(m.P_ClosingDetail), ctrl.GetClosingDataProduksi)
|
||||||
|
route.Get("/:projectFlockId/keuangan", m.RequirePermissions(m.P_ClosingDetail), ctrl.GetClosingKeuangan)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ package service
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
@@ -15,6 +17,8 @@ import (
|
|||||||
marketingRepository "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/repositories"
|
marketingRepository "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/repositories"
|
||||||
chickinRepository "gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins/repositories"
|
chickinRepository "gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins/repositories"
|
||||||
projectflockRepository "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
|
projectflockRepository "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
|
||||||
|
recordingRepository "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/repositories"
|
||||||
|
purchaseRepository "gitlab.com/mbugroup/lti-api.git/internal/modules/purchases/repositories"
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
approvalutils "gitlab.com/mbugroup/lti-api.git/internal/utils/approvals"
|
approvalutils "gitlab.com/mbugroup/lti-api.git/internal/utils/approvals"
|
||||||
|
|
||||||
@@ -30,7 +34,10 @@ type ClosingService interface {
|
|||||||
GetPenjualan(ctx *fiber.Ctx, projectFlockID uint) ([]entity.MarketingDeliveryProduct, error)
|
GetPenjualan(ctx *fiber.Ctx, projectFlockID uint) ([]entity.MarketingDeliveryProduct, error)
|
||||||
GetClosingSummary(ctx *fiber.Ctx, projectFlockID uint) (*dto.ClosingSummaryDTO, error)
|
GetClosingSummary(ctx *fiber.Ctx, projectFlockID uint) (*dto.ClosingSummaryDTO, error)
|
||||||
GetOverhead(ctx *fiber.Ctx, projectFlockID uint) (*dto.OverheadListDTO, error)
|
GetOverhead(ctx *fiber.Ctx, projectFlockID uint) (*dto.OverheadListDTO, error)
|
||||||
|
GetClosingDataProduksi(ctx *fiber.Ctx, projectFlockID uint) (*dto.ClosingProductionReportDTO, error)
|
||||||
GetClosingSapronak(ctx *fiber.Ctx, projectFlockID uint, params *validation.ClosingSapronakQuery) ([]dto.ClosingSapronakItemDTO, int64, error)
|
GetClosingSapronak(ctx *fiber.Ctx, projectFlockID uint, params *validation.ClosingSapronakQuery) ([]dto.ClosingSapronakItemDTO, int64, error)
|
||||||
|
GetClosingKeuangan(ctx *fiber.Ctx, projectFlockID uint) (*dto.ReportResponse, error)
|
||||||
|
GetExpeditionHPP(ctx *fiber.Ctx, projectFlockID uint, projectFlockKandangID *uint) (*dto.ExpeditionHPPDTO, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type closingService struct {
|
type closingService struct {
|
||||||
@@ -44,9 +51,11 @@ type closingService struct {
|
|||||||
ExpenseRealizationRepo expenseRealizationRepository.ExpenseRealizationRepository
|
ExpenseRealizationRepo expenseRealizationRepository.ExpenseRealizationRepository
|
||||||
ProjectBudgetRepo projectflockRepository.ProjectBudgetRepository
|
ProjectBudgetRepo projectflockRepository.ProjectBudgetRepository
|
||||||
ChickinRepo chickinRepository.ProjectChickinRepository
|
ChickinRepo chickinRepository.ProjectChickinRepository
|
||||||
|
PurchaseRepo purchaseRepository.PurchaseRepository
|
||||||
|
RecordingRepo recordingRepository.RecordingRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClosingService(repo repository.ClosingRepository, projectFlockRepo projectflockRepository.ProjectflockRepository, marketingRepo marketingRepository.MarketingRepository, marketingDeliveryProductRepo marketingDeliveryProductRepository.MarketingDeliveryProductRepository, approvalSvc commonSvc.ApprovalService, expenseRealizationRepo expenseRealizationRepository.ExpenseRealizationRepository, projectBudgetRepo projectflockRepository.ProjectBudgetRepository, chickinRepo chickinRepository.ProjectChickinRepository, validate *validator.Validate) ClosingService {
|
func NewClosingService(repo repository.ClosingRepository, projectFlockRepo projectflockRepository.ProjectflockRepository, marketingRepo marketingRepository.MarketingRepository, marketingDeliveryProductRepo marketingDeliveryProductRepository.MarketingDeliveryProductRepository, approvalSvc commonSvc.ApprovalService, expenseRealizationRepo expenseRealizationRepository.ExpenseRealizationRepository, projectBudgetRepo projectflockRepository.ProjectBudgetRepository, chickinRepo chickinRepository.ProjectChickinRepository, purchaseRepo purchaseRepository.PurchaseRepository, recordingRepo recordingRepository.RecordingRepository, validate *validator.Validate) ClosingService {
|
||||||
return &closingService{
|
return &closingService{
|
||||||
Log: utils.Log,
|
Log: utils.Log,
|
||||||
Validate: validate,
|
Validate: validate,
|
||||||
@@ -58,6 +67,8 @@ func NewClosingService(repo repository.ClosingRepository, projectFlockRepo proje
|
|||||||
ExpenseRealizationRepo: expenseRealizationRepo,
|
ExpenseRealizationRepo: expenseRealizationRepo,
|
||||||
ProjectBudgetRepo: projectBudgetRepo,
|
ProjectBudgetRepo: projectBudgetRepo,
|
||||||
ChickinRepo: chickinRepo,
|
ChickinRepo: chickinRepo,
|
||||||
|
PurchaseRepo: purchaseRepo,
|
||||||
|
RecordingRepo: recordingRepo,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,6 +141,7 @@ func (s closingService) GetPenjualan(c *fiber.Ctx, projectFlockID uint) ([]entit
|
|||||||
Preload("MarketingProduct.ProductWarehouse.Warehouse").
|
Preload("MarketingProduct.ProductWarehouse.Warehouse").
|
||||||
Preload("MarketingProduct.ProductWarehouse.ProjectFlockKandang").
|
Preload("MarketingProduct.ProductWarehouse.ProjectFlockKandang").
|
||||||
Preload("MarketingProduct.ProductWarehouse.ProjectFlockKandang.Kandang").
|
Preload("MarketingProduct.ProductWarehouse.ProjectFlockKandang.Kandang").
|
||||||
|
Preload("MarketingProduct.ProductWarehouse.ProjectFlockKandang.Chickins").
|
||||||
Preload("MarketingProduct.Marketing").
|
Preload("MarketingProduct.Marketing").
|
||||||
Preload("MarketingProduct.Marketing.Customer").
|
Preload("MarketingProduct.Marketing.Customer").
|
||||||
Order("marketing_delivery_products.delivery_date DESC")
|
Order("marketing_delivery_products.delivery_date DESC")
|
||||||
@@ -375,7 +387,396 @@ func (s closingService) GetOverhead(c *fiber.Ctx, projectFlockID uint) (*dto.Ove
|
|||||||
totalChickinQty += chickin.UsageQty
|
totalChickinQty += chickin.UsageQty
|
||||||
}
|
}
|
||||||
|
|
||||||
result := dto.ToOverheadListDTOs(budgets, realizations, totalChickinQty)
|
totalDepletion, err := s.RecordingRepo.GetTotalDepletionByProjectFlockID(c.Context(), projectFlockID)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Warnf("GetTotalDepletionByProjectFlockID error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
totalActualPopulation := totalChickinQty - totalDepletion
|
||||||
|
|
||||||
|
result := dto.ToOverheadListDTOs(budgets, realizations, totalChickinQty, totalActualPopulation)
|
||||||
|
|
||||||
return &result, nil
|
return &result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s closingService) GetClosingKeuangan(c *fiber.Ctx, projectFlockID uint) (*dto.ReportResponse, error) {
|
||||||
|
if projectFlockID == 0 {
|
||||||
|
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid project flock id")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := commonSvc.EnsureRelations(c.Context(),
|
||||||
|
commonSvc.RelationCheck{Name: "Project Flock", ID: &projectFlockID, Exists: func(ctx context.Context, id uint) (bool, error) {
|
||||||
|
_, err := s.ProjectFlockRepo.GetByID(ctx, id, nil)
|
||||||
|
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
return err == nil, err
|
||||||
|
}},
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
projectFlock, err := s.ProjectFlockRepo.GetByID(c.Context(), projectFlockID, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch project flock")
|
||||||
|
}
|
||||||
|
|
||||||
|
budgets, err := s.ProjectBudgetRepo.GetByProjectFlockID(c.Context(), projectFlockID)
|
||||||
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch budgets")
|
||||||
|
}
|
||||||
|
|
||||||
|
purchaseItems, err := s.PurchaseRepo.GetItemsByProjectFlockID(c.Context(), projectFlockID)
|
||||||
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch purchase items")
|
||||||
|
}
|
||||||
|
|
||||||
|
realizations, err := s.ExpenseRealizationRepo.GetByProjectFlockID(c.Context(), projectFlockID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch realizations")
|
||||||
|
}
|
||||||
|
|
||||||
|
deliveryProducts, err := s.MarketingDeliveryProductRepo.GetDeliveryProductsByProjectFlockID(c.Context(), projectFlockID, func(db *gorm.DB) *gorm.DB {
|
||||||
|
return db.Preload("MarketingProduct").
|
||||||
|
Preload("MarketingProduct.ProductWarehouse").
|
||||||
|
Preload("MarketingProduct.ProductWarehouse.Product")
|
||||||
|
})
|
||||||
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch delivery products")
|
||||||
|
}
|
||||||
|
|
||||||
|
chickins, err := s.ChickinRepo.GetByProjectFlockID(c.Context(), projectFlockID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch chickins")
|
||||||
|
}
|
||||||
|
|
||||||
|
totalWeightProduced, _, err := s.RecordingRepo.GetProductionWeightAndQtyByProjectFlockID(c.Context(), projectFlockID)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Warnf("GetProductionWeightAndQtyByProjectFlockID error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
totalDepletion, err := s.RecordingRepo.GetTotalDepletionByProjectFlockID(c.Context(), projectFlockID)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Warnf("GetTotalDepletionByProjectFlockID error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
input := dto.ClosingKeuanganInput{
|
||||||
|
ProjectFlockCategory: projectFlock.Category,
|
||||||
|
PurchaseItems: purchaseItems,
|
||||||
|
Budgets: budgets,
|
||||||
|
Realizations: realizations,
|
||||||
|
DeliveryProducts: deliveryProducts,
|
||||||
|
Chickins: chickins,
|
||||||
|
TotalWeightProduced: totalWeightProduced,
|
||||||
|
TotalDepletion: totalDepletion,
|
||||||
|
}
|
||||||
|
|
||||||
|
report := dto.ToClosingKeuanganReport(input)
|
||||||
|
|
||||||
|
return &report, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetExpeditionHPP menghitung HPP ekspedisi per vendor untuk sebuah project flock.
|
||||||
|
// Jika projectFlockKandangID tidak nil, maka hanya data untuk kandang tersebut yang dihitung.
|
||||||
|
func (s closingService) GetExpeditionHPP(c *fiber.Ctx, projectFlockID uint, projectFlockKandangID *uint) (*dto.ExpeditionHPPDTO, error) {
|
||||||
|
if projectFlockID == 0 {
|
||||||
|
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid project flock id")
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := s.Repository.GetExpeditionHPP(c.Context(), projectFlockID, projectFlockKandangID)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Errorf("Failed to get expedition HPP for project flock %d: %+v", projectFlockID, err)
|
||||||
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch expedition HPP")
|
||||||
|
}
|
||||||
|
|
||||||
|
expeditionCosts := make([]dto.ExpeditionCostItemDTO, 0, len(rows))
|
||||||
|
var totalHPP float64
|
||||||
|
|
||||||
|
for idx, row := range rows {
|
||||||
|
expeditionCosts = append(expeditionCosts, dto.ExpeditionCostItemDTO{
|
||||||
|
Id: uint64(idx + 1),
|
||||||
|
ExpeditionVendorName: row.SupplierName,
|
||||||
|
HPPAmount: row.TotalAmount,
|
||||||
|
})
|
||||||
|
|
||||||
|
totalHPP += row.TotalAmount
|
||||||
|
}
|
||||||
|
|
||||||
|
result := &dto.ExpeditionHPPDTO{
|
||||||
|
ExpeditionCosts: expeditionCosts,
|
||||||
|
TotalHPPAmount: totalHPP,
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s closingService) GetClosingDataProduksi(c *fiber.Ctx, projectFlockID uint) (*dto.ClosingProductionReportDTO, error) {
|
||||||
|
if projectFlockID == 0 {
|
||||||
|
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid project flock id")
|
||||||
|
}
|
||||||
|
|
||||||
|
project, err := s.Repository.GetByID(c.Context(), projectFlockID, s.withClosingRelations)
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, fiber.NewError(fiber.StatusNotFound, "Project flock not found")
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Errorf("Failed get project flock %d for closing data produksi: %+v", projectFlockID, err)
|
||||||
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch project flock")
|
||||||
|
}
|
||||||
|
|
||||||
|
var population float64
|
||||||
|
for _, history := range project.KandangHistory {
|
||||||
|
for _, chickin := range history.Chickins {
|
||||||
|
population += chickin.UsageQty + chickin.PendingUsageQty
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
isGrowing := strings.EqualFold(project.Category, string(utils.ProjectFlockCategoryGrowing))
|
||||||
|
|
||||||
|
projectFlockKandangIDs, err := s.getProjectFlockKandangIDs(c.Context(), projectFlockID)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Errorf("Failed to fetch project flock kandangs for %d: %+v", projectFlockID, err)
|
||||||
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch project flock kandangs")
|
||||||
|
}
|
||||||
|
|
||||||
|
feedIn, feedUsed, err := s.Repository.SumFeedPurchaseAndUsedByProjectFlockKandangIDs(c.Context(), projectFlockKandangIDs)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Errorf("Failed to sum feed purchase/used qty for project flock %d: %+v", projectFlockID, err)
|
||||||
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch feed purchase data")
|
||||||
|
}
|
||||||
|
|
||||||
|
claimCulling, err := s.Repository.SumClaimCullingByProjectFlockKandangIDs(c.Context(), projectFlockKandangIDs)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Errorf("Failed to sum claim culling for project flock %d: %+v", projectFlockID, err)
|
||||||
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch claim culling data")
|
||||||
|
}
|
||||||
|
|
||||||
|
finalPopulation := population - claimCulling
|
||||||
|
|
||||||
|
var standards []entity.FcrStandard
|
||||||
|
if project.FcrId > 0 {
|
||||||
|
standards, err = s.Repository.GetFcrStandardsByFcrID(c.Context(), project.FcrId)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Errorf("Failed to fetch FCR standards for project flock %d: %+v", projectFlockID, err)
|
||||||
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch FCR standard data")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
age, err := s.calculateAverageSalesAge(c.Context(), projectFlockID)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Errorf("Failed to calculate sales age for project flock %d: %+v", projectFlockID, err)
|
||||||
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch sales age data")
|
||||||
|
}
|
||||||
|
|
||||||
|
feedUsedPerHead := 0.0
|
||||||
|
if population > 0 {
|
||||||
|
feedUsedPerHead = feedUsed / population
|
||||||
|
}
|
||||||
|
|
||||||
|
purchase := dto.ClosingPurchaseDTO{
|
||||||
|
InitialPopulation: int(population),
|
||||||
|
ClaimCulling: int(claimCulling),
|
||||||
|
FinalPopulation: int(finalPopulation),
|
||||||
|
FeedIn: feedIn,
|
||||||
|
FeedUsed: feedUsed,
|
||||||
|
FeedUsedPerHead: feedUsedPerHead,
|
||||||
|
}
|
||||||
|
|
||||||
|
chickenFlagNames := []string{string(utils.FlagPullet)}
|
||||||
|
chickenSalesWeight, chickenSalesQty, chickenSalesPrice, err := s.Repository.SumMarketingWeightAndQtyByProjectFlockKandangIDsAndFlagNames(c.Context(), projectFlockKandangIDs, chickenFlagNames)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Errorf("Failed to fetch chicken sales data for project flock %d: %+v", projectFlockID, err)
|
||||||
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch chicken sales data")
|
||||||
|
}
|
||||||
|
|
||||||
|
var chickenAverageWeight float64
|
||||||
|
if chickenSalesQty > 0 {
|
||||||
|
chickenAverageWeight = chickenSalesWeight / chickenSalesQty
|
||||||
|
}
|
||||||
|
|
||||||
|
var chickenAverageSellingPrice float64
|
||||||
|
if chickenSalesWeight > 0 {
|
||||||
|
chickenAverageSellingPrice = chickenSalesPrice / chickenSalesWeight
|
||||||
|
}
|
||||||
|
|
||||||
|
chickenSales := dto.ClosingSalesDTO{
|
||||||
|
SalesPopulation: int(chickenSalesQty),
|
||||||
|
SalesWeight: chickenSalesWeight,
|
||||||
|
AverageWeight: chickenAverageWeight,
|
||||||
|
AverageSellingPrice: chickenAverageSellingPrice,
|
||||||
|
}
|
||||||
|
|
||||||
|
chickenDepletion := population - chickenSalesQty
|
||||||
|
if chickenDepletion < 0 {
|
||||||
|
chickenDepletion = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
chickenPerformance := calculatePerformanceMetrics(chickenAverageWeight, chickenSalesWeight, feedUsed, population, chickenDepletion, age, standards)
|
||||||
|
|
||||||
|
var eggSales *dto.ClosingEggSalesDTO
|
||||||
|
var eggPerformance *dto.ClosingPerformanceDTO
|
||||||
|
if !isGrowing {
|
||||||
|
eggFlagNames := []string{
|
||||||
|
string(utils.FlagTelur),
|
||||||
|
string(utils.FlagTelurUtuh),
|
||||||
|
string(utils.FlagTelurPecah),
|
||||||
|
string(utils.FlagTelurPutih),
|
||||||
|
string(utils.FlagTelurRetak),
|
||||||
|
}
|
||||||
|
|
||||||
|
eggSalesWeight, eggSalesQty, eggSalesPrice, err := s.Repository.SumMarketingWeightAndQtyByProjectFlockKandangIDsAndFlagNames(c.Context(), projectFlockKandangIDs, eggFlagNames)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Errorf("Failed to fetch egg sales data for project flock %d: %+v", projectFlockID, err)
|
||||||
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch egg sales data")
|
||||||
|
}
|
||||||
|
|
||||||
|
var averageEggWeight float64
|
||||||
|
if eggSalesQty > 0 {
|
||||||
|
averageEggWeight = eggSalesWeight / eggSalesQty
|
||||||
|
}
|
||||||
|
|
||||||
|
var averageEggSellingPrice float64
|
||||||
|
if eggSalesWeight > 0 {
|
||||||
|
averageEggSellingPrice = eggSalesPrice / eggSalesWeight
|
||||||
|
}
|
||||||
|
|
||||||
|
eggSales = &dto.ClosingEggSalesDTO{
|
||||||
|
EggPieces: int(eggSalesQty),
|
||||||
|
EggMassKg: eggSalesWeight,
|
||||||
|
AverageEggWeightKg: averageEggWeight,
|
||||||
|
AverageSellingPrice: averageEggSellingPrice,
|
||||||
|
}
|
||||||
|
|
||||||
|
harvestEggQty, err := s.Repository.SumRecordingEggQtyByProjectFlockKandangIDsAndFlagNames(c.Context(), projectFlockKandangIDs, eggFlagNames)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Errorf("Failed to fetch recording egg qty for project flock %d: %+v", projectFlockID, err)
|
||||||
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch egg harvest data")
|
||||||
|
}
|
||||||
|
|
||||||
|
eggDepletion := harvestEggQty - eggSalesQty
|
||||||
|
if eggDepletion < 0 {
|
||||||
|
eggDepletion = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
eggPerf := calculatePerformanceMetrics(averageEggWeight, eggSalesWeight, feedUsed, harvestEggQty, eggDepletion, age, standards)
|
||||||
|
eggPerformance = &eggPerf
|
||||||
|
}
|
||||||
|
|
||||||
|
sales := dto.ClosingSalesGroupDTO{
|
||||||
|
Chicken: chickenSales,
|
||||||
|
Egg: eggSales,
|
||||||
|
}
|
||||||
|
|
||||||
|
performance := dto.ClosingPerformanceDTO{
|
||||||
|
Depletion: chickenPerformance.Depletion,
|
||||||
|
Age: age,
|
||||||
|
MortalityStd: chickenPerformance.MortalityStd,
|
||||||
|
MortalityAct: chickenPerformance.MortalityAct,
|
||||||
|
DeffMortality: chickenPerformance.DeffMortality,
|
||||||
|
}
|
||||||
|
if eggPerformance != nil {
|
||||||
|
performance.FcrStd = eggPerformance.FcrStd
|
||||||
|
performance.FcrAct = eggPerformance.FcrAct
|
||||||
|
performance.DeffFcr = eggPerformance.DeffFcr
|
||||||
|
performance.Awg = eggPerformance.Awg
|
||||||
|
} else {
|
||||||
|
performance.FcrStd = chickenPerformance.FcrStd
|
||||||
|
performance.FcrAct = chickenPerformance.FcrAct
|
||||||
|
performance.DeffFcr = chickenPerformance.DeffFcr
|
||||||
|
performance.Awg = chickenPerformance.Awg
|
||||||
|
}
|
||||||
|
|
||||||
|
result := dto.ClosingProductionReportDTO{
|
||||||
|
Purchase: purchase,
|
||||||
|
Sales: sales,
|
||||||
|
Performance: performance,
|
||||||
|
}
|
||||||
|
|
||||||
|
return &result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s closingService) calculateAverageSalesAge(ctx context.Context, projectFlockID uint) (float64, error) {
|
||||||
|
deliveryProducts, err := s.MarketingDeliveryProductRepo.GetDeliveryProductsByProjectFlockID(ctx, projectFlockID, func(db *gorm.DB) *gorm.DB {
|
||||||
|
return db.
|
||||||
|
Preload("MarketingProduct").
|
||||||
|
Preload("MarketingProduct.ProductWarehouse").
|
||||||
|
Preload("MarketingProduct.ProductWarehouse.ProjectFlockKandang").
|
||||||
|
Preload("MarketingProduct.ProductWarehouse.ProjectFlockKandang.Chickins")
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
totalQty float64
|
||||||
|
totalAgeWeeks float64
|
||||||
|
)
|
||||||
|
|
||||||
|
for _, product := range deliveryProducts {
|
||||||
|
if product.UsageQty == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
projectFlockKandang := product.MarketingProduct.ProductWarehouse.ProjectFlockKandang
|
||||||
|
ageWeeks := dto.CalculateAgeFromChickinDataProduksi(projectFlockKandang, product.DeliveryDate)
|
||||||
|
totalAgeWeeks += float64(ageWeeks) * product.UsageQty
|
||||||
|
totalQty += product.UsageQty
|
||||||
|
}
|
||||||
|
|
||||||
|
if totalQty == 0 {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return totalAgeWeeks / totalQty, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func calculatePerformanceMetrics(averageWeight, totalWeight, feedUsed, basePopulation, depletion, age float64, standards []entity.FcrStandard) dto.ClosingPerformanceDTO {
|
||||||
|
mortalityStd, fcrStd := closestFcrValues(standards, averageWeight)
|
||||||
|
|
||||||
|
fcrAct := 0.0
|
||||||
|
if totalWeight > 0 {
|
||||||
|
fcrAct = feedUsed / totalWeight
|
||||||
|
}
|
||||||
|
|
||||||
|
mortalityAct := 0.0
|
||||||
|
if basePopulation > 0 {
|
||||||
|
mortalityAct = (depletion / basePopulation) * 100
|
||||||
|
}
|
||||||
|
|
||||||
|
deffMortality := mortalityAct - mortalityStd
|
||||||
|
deffFcr := fcrAct - fcrStd
|
||||||
|
|
||||||
|
awg := 0.0
|
||||||
|
if age > 0 {
|
||||||
|
awg = averageWeight / age
|
||||||
|
}
|
||||||
|
|
||||||
|
return dto.ClosingPerformanceDTO{
|
||||||
|
Depletion: depletion,
|
||||||
|
Age: age,
|
||||||
|
MortalityStd: mortalityStd,
|
||||||
|
MortalityAct: mortalityAct,
|
||||||
|
DeffMortality: deffMortality,
|
||||||
|
FcrStd: fcrStd,
|
||||||
|
FcrAct: fcrAct,
|
||||||
|
DeffFcr: deffFcr,
|
||||||
|
Awg: awg,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func closestFcrValues(standards []entity.FcrStandard, averageWeight float64) (float64, float64) {
|
||||||
|
if len(standards) == 0 || averageWeight <= 0 {
|
||||||
|
return 0, 0
|
||||||
|
}
|
||||||
|
|
||||||
|
closest := standards[0]
|
||||||
|
minDiff := math.Abs(closest.Weight - averageWeight)
|
||||||
|
for _, std := range standards[1:] {
|
||||||
|
diff := math.Abs(std.Weight - averageWeight)
|
||||||
|
if diff < minDiff {
|
||||||
|
minDiff = diff
|
||||||
|
closest = std
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return closest.Mortality, closest.FcrNumber
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -12,6 +12,5 @@ func ConstantRoutes(v1 fiber.Router, s constant.ConstantService) {
|
|||||||
ctrl := controller.NewConstantController(s)
|
ctrl := controller.NewConstantController(s)
|
||||||
|
|
||||||
route := v1.Group("/constants")
|
route := v1.Group("/constants")
|
||||||
|
|
||||||
route.Get("/", ctrl.GetAll)
|
route.Get("/", ctrl.GetAll)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package dto
|
package dto
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
@@ -41,8 +40,8 @@ type ExpenseListDTO struct {
|
|||||||
|
|
||||||
type ExpenseDetailDTO struct {
|
type ExpenseDetailDTO struct {
|
||||||
ExpenseBaseDTO
|
ExpenseBaseDTO
|
||||||
Documents []DocumentDTO `json:"documents,omitempty"`
|
Documents []DocumentDTO `json:"documents"`
|
||||||
RealizationDocs []DocumentDTO `json:"realization_docs,omitempty"`
|
RealizationDocs []DocumentDTO `json:"realization_docs"`
|
||||||
Kandangs []KandangGroupDTO `json:"kandangs,omitempty"`
|
Kandangs []KandangGroupDTO `json:"kandangs,omitempty"`
|
||||||
TotalPengajuan float64 `json:"total_pengajuan"`
|
TotalPengajuan float64 `json:"total_pengajuan"`
|
||||||
TotalRealisasi float64 `json:"total_realisasi"`
|
TotalRealisasi float64 `json:"total_realisasi"`
|
||||||
@@ -179,12 +178,20 @@ func ToExpenseDetailDTO(e *entity.Expense) ExpenseDetailDTO {
|
|||||||
var pengajuans []ExpenseNonstockDTO
|
var pengajuans []ExpenseNonstockDTO
|
||||||
var realisasi []ExpenseRealizationDTO
|
var realisasi []ExpenseRealizationDTO
|
||||||
|
|
||||||
if e.DocumentPath.Valid && e.DocumentPath.String != "" {
|
// Map documents from Document service
|
||||||
json.Unmarshal([]byte(e.DocumentPath.String), &documents)
|
for _, doc := range e.Documents {
|
||||||
|
documents = append(documents, DocumentDTO{
|
||||||
|
ID: uint64(doc.Id),
|
||||||
|
Path: doc.Path,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if e.RealizationDocumentPath.Valid && e.RealizationDocumentPath.String != "" {
|
// Map realization documents from Document service
|
||||||
json.Unmarshal([]byte(e.RealizationDocumentPath.String), &realizationDocs)
|
for _, doc := range e.RealizationDocuments {
|
||||||
|
realizationDocs = append(realizationDocs, DocumentDTO{
|
||||||
|
ID: uint64(doc.Id),
|
||||||
|
Path: doc.Path,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(e.Nonstocks) > 0 {
|
if len(e.Nonstocks) > 0 {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package expenses
|
package expenses
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/go-playground/validator/v10"
|
"github.com/go-playground/validator/v10"
|
||||||
@@ -31,15 +32,20 @@ func (ExpenseModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *
|
|||||||
approvalRepo := commonRepo.NewApprovalRepository(db)
|
approvalRepo := commonRepo.NewApprovalRepository(db)
|
||||||
realizationRepo := rExpense.NewExpenseRealizationRepository(db)
|
realizationRepo := rExpense.NewExpenseRealizationRepository(db)
|
||||||
projectFlockKandangRepo := rProjectFlockKandang.NewProjectFlockKandangRepository(db)
|
projectFlockKandangRepo := rProjectFlockKandang.NewProjectFlockKandangRepository(db)
|
||||||
|
documentRepo := commonRepo.NewDocumentRepository(db)
|
||||||
|
|
||||||
approvalSvc := commonSvc.NewApprovalService(approvalRepo)
|
approvalSvc := commonSvc.NewApprovalService(approvalRepo)
|
||||||
|
documentSvc, err := commonSvc.NewDocumentServiceFromConfig(context.Background(), documentRepo)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf("failed to create document service: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
// Register workflow steps for EXPENSES approval
|
// Register workflow steps for EXPENSES approval
|
||||||
if err := approvalSvc.RegisterWorkflowSteps(utils.ApprovalWorkflowExpense, utils.ExpenseApprovalSteps); err != nil {
|
if err := approvalSvc.RegisterWorkflowSteps(utils.ApprovalWorkflowExpense, utils.ExpenseApprovalSteps); err != nil {
|
||||||
panic(fmt.Sprintf("failed to register expense approval workflow: %v", err))
|
panic(fmt.Sprintf("failed to register expense approval workflow: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
expenseService := sExpense.NewExpenseService(expenseRepo, supplierRepo, nonstockRepo, approvalSvc, realizationRepo, projectFlockKandangRepo, validate)
|
expenseService := sExpense.NewExpenseService(expenseRepo, supplierRepo, nonstockRepo, approvalSvc, realizationRepo, projectFlockKandangRepo, documentSvc, validate)
|
||||||
userService := sUser.NewUserService(userRepo, validate)
|
userService := sUser.NewUserService(userRepo, validate)
|
||||||
|
|
||||||
ExpenseRoutes(router, userService, expenseService)
|
ExpenseRoutes(router, userService, expenseService)
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import (
|
|||||||
|
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
|
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/repports/validations"
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -13,6 +15,7 @@ type ExpenseRealizationRepository interface {
|
|||||||
IdExists(ctx context.Context, id uint64) (bool, error)
|
IdExists(ctx context.Context, id uint64) (bool, error)
|
||||||
GetByExpenseNonstockID(ctx context.Context, expenseNonstockID uint64) (*entity.ExpenseRealization, error)
|
GetByExpenseNonstockID(ctx context.Context, expenseNonstockID uint64) (*entity.ExpenseRealization, error)
|
||||||
GetByProjectFlockID(ctx context.Context, projectFlockID uint) ([]entity.ExpenseRealization, error)
|
GetByProjectFlockID(ctx context.Context, projectFlockID uint) ([]entity.ExpenseRealization, error)
|
||||||
|
GetAllWithFilters(ctx context.Context, offset, limit int, filters *validation.ExpenseQuery) ([]entity.ExpenseRealization, int64, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type ExpenseRealizationRepositoryImpl struct {
|
type ExpenseRealizationRepositoryImpl struct {
|
||||||
@@ -41,12 +44,94 @@ func (r *ExpenseRealizationRepositoryImpl) GetByProjectFlockID(ctx context.Conte
|
|||||||
Preload("ExpenseNonstock").
|
Preload("ExpenseNonstock").
|
||||||
Preload("ExpenseNonstock.Nonstock").
|
Preload("ExpenseNonstock.Nonstock").
|
||||||
Preload("ExpenseNonstock.Nonstock.Uom").
|
Preload("ExpenseNonstock.Nonstock.Uom").
|
||||||
|
Preload("ExpenseNonstock.Nonstock.Flags").
|
||||||
Preload("ExpenseNonstock.Expense").
|
Preload("ExpenseNonstock.Expense").
|
||||||
Joins("JOIN expense_nonstocks ON expense_nonstocks.id = expense_realizations.expense_nonstock_id").
|
Joins("JOIN expense_nonstocks ON expense_nonstocks.id = expense_realizations.expense_nonstock_id").
|
||||||
Joins("JOIN project_flock_kandangs ON project_flock_kandangs.id = expense_nonstocks.project_flock_kandang_id").
|
|
||||||
Joins("JOIN expenses ON expenses.id = expense_nonstocks.expense_id").
|
Joins("JOIN expenses ON expenses.id = expense_nonstocks.expense_id").
|
||||||
Where("project_flock_kandangs.project_flock_id = ?", projectFlockID).
|
Joins("LEFT JOIN project_flock_kandangs ON project_flock_kandangs.id = expense_nonstocks.project_flock_kandang_id").
|
||||||
Where("expenses.category = ?", "BOP").
|
Joins("LEFT JOIN kandangs ON kandangs.id = expense_nonstocks.kandang_id").
|
||||||
|
Where("project_flock_kandangs.project_flock_id = ? OR kandangs.id IN (SELECT kandang_id FROM project_flock_kandangs WHERE project_flock_id = ?)", projectFlockID, projectFlockID).
|
||||||
Find(&realizations).Error
|
Find(&realizations).Error
|
||||||
return realizations, err
|
return realizations, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *ExpenseRealizationRepositoryImpl) GetAllWithFilters(ctx context.Context, offset, limit int, filters *validation.ExpenseQuery) ([]entity.ExpenseRealization, int64, error) {
|
||||||
|
var realizations []entity.ExpenseRealization
|
||||||
|
var total int64
|
||||||
|
|
||||||
|
db := r.DB().WithContext(ctx).
|
||||||
|
Model(&entity.ExpenseRealization{}).
|
||||||
|
Preload("ExpenseNonstock", func(db *gorm.DB) *gorm.DB {
|
||||||
|
return db.
|
||||||
|
Preload("Expense").
|
||||||
|
Preload("Expense.Supplier").
|
||||||
|
Preload("Kandang").
|
||||||
|
Preload("Kandang.Location").
|
||||||
|
Preload("Nonstock").
|
||||||
|
Preload("Nonstock.Flags")
|
||||||
|
}).
|
||||||
|
Joins("JOIN expense_nonstocks ON expense_nonstocks.id = expense_realizations.expense_nonstock_id").
|
||||||
|
Joins("JOIN expenses ON expenses.id = expense_nonstocks.expense_id").
|
||||||
|
Joins("LEFT JOIN suppliers ON suppliers.id = expenses.supplier_id")
|
||||||
|
|
||||||
|
if filters.Search != "" {
|
||||||
|
db = db.Where("expenses.category LIKE ? OR expenses.reference_number LIKE ? OR expenses.po_number LIKE ? OR expenses.notes LIKE ? OR suppliers.name LIKE ?",
|
||||||
|
"%"+filters.Search+"%", "%"+filters.Search+"%", "%"+filters.Search+"%", "%"+filters.Search+"%", "%"+filters.Search+"%")
|
||||||
|
}
|
||||||
|
|
||||||
|
if filters.Category != "" {
|
||||||
|
db = db.Where("expenses.category = ?", filters.Category)
|
||||||
|
}
|
||||||
|
|
||||||
|
if filters.SupplierId > 0 {
|
||||||
|
db = db.Where("expenses.supplier_id = ?", filters.SupplierId)
|
||||||
|
}
|
||||||
|
|
||||||
|
if filters.KandangId > 0 {
|
||||||
|
db = db.Where("expense_nonstocks.kandang_id = ?", filters.KandangId)
|
||||||
|
}
|
||||||
|
|
||||||
|
if filters.ProjectFlockKandangId > 0 {
|
||||||
|
db = db.Where("expense_nonstocks.project_flock_kandang_id = ?", filters.ProjectFlockKandangId)
|
||||||
|
}
|
||||||
|
|
||||||
|
if filters.NonstockId > 0 {
|
||||||
|
db = db.Where("expense_nonstocks.nonstock_id = ?", filters.NonstockId)
|
||||||
|
}
|
||||||
|
|
||||||
|
locationID := filters.LocationId
|
||||||
|
areaID := filters.AreaId
|
||||||
|
|
||||||
|
if locationID > 0 || areaID > 0 {
|
||||||
|
db = db.Joins("JOIN kandangs ON kandangs.id = expense_nonstocks.kandang_id")
|
||||||
|
|
||||||
|
if locationID > 0 {
|
||||||
|
db = db.Where("kandangs.location_id = ?", uint(locationID))
|
||||||
|
}
|
||||||
|
|
||||||
|
if areaID > 0 {
|
||||||
|
db = db.Joins("JOIN locations ON locations.id = kandangs.location_id").
|
||||||
|
Where("locations.area_id = ?", uint(areaID))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if filters.RealizationDate != "" {
|
||||||
|
if realizationDate, err := utils.ParseDateString(filters.RealizationDate); err == nil {
|
||||||
|
db = db.Where("DATE(expenses.realization_date) = ?", realizationDate)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := db.Count(&total).Error; err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := db.
|
||||||
|
Offset(offset).
|
||||||
|
Limit(limit).
|
||||||
|
Order("expense_realizations.created_at DESC").
|
||||||
|
Find(&realizations).Error; err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return realizations, total, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -22,16 +22,16 @@ func ExpenseRoutes(v1 fiber.Router, u user.UserService, s expense.ExpenseService
|
|||||||
// route.Patch("/:id", m.Auth(u), ctrl.UpdateOne)
|
// route.Patch("/:id", m.Auth(u), ctrl.UpdateOne)
|
||||||
// route.Delete("/:id", m.Auth(u), ctrl.DeleteOne)
|
// route.Delete("/:id", m.Auth(u), ctrl.DeleteOne)
|
||||||
|
|
||||||
route.Get("/", ctrl.GetAll)
|
route.Get("/",m.RequirePermissions(m.P_ExpenseGetAll), ctrl.GetAll)
|
||||||
route.Post("/", ctrl.CreateOne)
|
route.Post("/",m.RequirePermissions(m.P_ExpenseCreateOne), ctrl.CreateOne)
|
||||||
route.Get("/:id", ctrl.GetOne)
|
route.Get("/:id",m.RequirePermissions(m.P_ExpenseGetOne), ctrl.GetOne)
|
||||||
route.Patch("/:id", ctrl.UpdateOne)
|
route.Patch("/:id",m.RequirePermissions(m.P_ExpenseUpdateOne), ctrl.UpdateOne)
|
||||||
route.Delete("/:id", ctrl.DeleteOne)
|
route.Delete("/:id",m.RequirePermissions(m.P_ExpenseDeleteOne), ctrl.DeleteOne)
|
||||||
route.Post("/approvals/manager", ctrl.Approval)
|
route.Post("/approvals/manager",m.RequirePermissions(m.P_ExpenseApprovalManager), ctrl.Approval)
|
||||||
route.Post("/approvals/finance", ctrl.Approval)
|
route.Post("/approvals/finance",m.RequirePermissions(m.P_ExpenseApprovalFinance), ctrl.Approval)
|
||||||
route.Post("/:id/realizations", ctrl.CreateRealization)
|
route.Post("/:id/realizations",m.RequirePermissions(m.P_ExpenseCreateRealizations), ctrl.CreateRealization)
|
||||||
route.Patch("/:id/realizations", ctrl.UpdateRealization)
|
route.Patch("/:id/realizations",m.RequirePermissions(m.P_ExpenseUpdateRealizations), ctrl.UpdateRealization)
|
||||||
route.Post("/:id/complete", ctrl.CompleteExpense)
|
route.Post("/:id/complete",m.RequirePermissions(m.P_ExpenseCompleteExpense), ctrl.CompleteExpense)
|
||||||
route.Delete("/:id/documents/:documentId", ctrl.DeleteDocument)
|
route.Delete("/:id/documents/:documentId",m.RequirePermissions(m.P_ExpenseDocument), ctrl.DeleteDocument)
|
||||||
route.Delete("/:id/realization-documents/:documentId", ctrl.DeleteRealizationDocument)
|
route.Delete("/:id/realization-documents/:documentId",m.RequirePermissions(m.P_ExpenseDocumentRealizations), ctrl.DeleteRealizationDocument)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,8 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"mime/multipart"
|
|
||||||
|
|
||||||
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||||
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||||
@@ -49,9 +46,10 @@ type expenseService struct {
|
|||||||
ApprovalSvc commonSvc.ApprovalService
|
ApprovalSvc commonSvc.ApprovalService
|
||||||
RealizationRepository repository.ExpenseRealizationRepository
|
RealizationRepository repository.ExpenseRealizationRepository
|
||||||
ProjectFlockKandangRepo projectFlockKandangRepo.ProjectFlockKandangRepository
|
ProjectFlockKandangRepo projectFlockKandangRepo.ProjectFlockKandangRepository
|
||||||
|
DocumentSvc commonSvc.DocumentService
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewExpenseService(repo repository.ExpenseRepository, supplierRepo supplierRepo.SupplierRepository, nonstockRepo nonstockRepo.NonstockRepository, approvalSvc commonSvc.ApprovalService, realizationRepo repository.ExpenseRealizationRepository, projectFlockKandangRepo projectFlockKandangRepo.ProjectFlockKandangRepository, validate *validator.Validate) ExpenseService {
|
func NewExpenseService(repo repository.ExpenseRepository, supplierRepo supplierRepo.SupplierRepository, nonstockRepo nonstockRepo.NonstockRepository, approvalSvc commonSvc.ApprovalService, realizationRepo repository.ExpenseRealizationRepository, projectFlockKandangRepo projectFlockKandangRepo.ProjectFlockKandangRepository, documentSvc commonSvc.DocumentService, validate *validator.Validate) ExpenseService {
|
||||||
return &expenseService{
|
return &expenseService{
|
||||||
Log: utils.Log,
|
Log: utils.Log,
|
||||||
Validate: validate,
|
Validate: validate,
|
||||||
@@ -61,6 +59,7 @@ func NewExpenseService(repo repository.ExpenseRepository, supplierRepo supplierR
|
|||||||
ApprovalSvc: approvalSvc,
|
ApprovalSvc: approvalSvc,
|
||||||
RealizationRepository: realizationRepo,
|
RealizationRepository: realizationRepo,
|
||||||
ProjectFlockKandangRepo: projectFlockKandangRepo,
|
ProjectFlockKandangRepo: projectFlockKandangRepo,
|
||||||
|
DocumentSvc: documentSvc,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,7 +71,13 @@ func (s expenseService) withRelations(db *gorm.DB) *gorm.DB {
|
|||||||
Preload("Nonstocks.Realization").
|
Preload("Nonstocks.Realization").
|
||||||
Preload("Nonstocks.ProjectFlockKandang.Kandang.Location").
|
Preload("Nonstocks.ProjectFlockKandang.Kandang.Location").
|
||||||
Preload("Nonstocks.Kandang").
|
Preload("Nonstocks.Kandang").
|
||||||
Preload("Nonstocks.Kandang.Location")
|
Preload("Nonstocks.Kandang.Location").
|
||||||
|
Preload("Documents", func(db *gorm.DB) *gorm.DB {
|
||||||
|
return db.Where("documentable_type = ?", string(utils.DocumentableTypeExpense))
|
||||||
|
}).
|
||||||
|
Preload("RealizationDocuments", func(db *gorm.DB) *gorm.DB {
|
||||||
|
return db.Where("documentable_type = ?", string(utils.DocumentableTypeExpenseRealization))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s expenseService) GetAll(c *fiber.Ctx, params *validation.Query) ([]expenseDto.ExpenseListDTO, int64, error) {
|
func (s expenseService) GetAll(c *fiber.Ctx, params *validation.Query) ([]expenseDto.ExpenseListDTO, int64, error) {
|
||||||
@@ -213,7 +218,7 @@ func (s *expenseService) CreateOne(c *fiber.Ctx, req *validation.Create) (*expen
|
|||||||
|
|
||||||
var projectFlockKandangId *uint64
|
var projectFlockKandangId *uint64
|
||||||
|
|
||||||
if req.Category == "BOP" {
|
if req.Category == string(utils.ExpenseCategoryBOP) {
|
||||||
|
|
||||||
projectFlockKandang, err := projectFlockKandangRepoTx.GetActiveByKandangID(c.Context(), uint(expenseNonstock.KandangID))
|
projectFlockKandang, err := projectFlockKandangRepoTx.GetActiveByKandangID(c.Context(), uint(expenseNonstock.KandangID))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -230,10 +235,10 @@ func (s *expenseService) CreateOne(c *fiber.Ctx, req *validation.Create) (*expen
|
|||||||
|
|
||||||
nonstockId := costItem.NonstockID
|
nonstockId := costItem.NonstockID
|
||||||
var kandangId *uint64
|
var kandangId *uint64
|
||||||
if req.Category == "NON-BOP" {
|
if req.Category == string(utils.ExpenseCategoryNonBOP) {
|
||||||
id := uint64(expenseNonstock.KandangID)
|
id := uint64(expenseNonstock.KandangID)
|
||||||
kandangId = &id
|
kandangId = &id
|
||||||
} else if req.Category == "BOP" {
|
} else if req.Category == string(utils.ExpenseCategoryBOP) {
|
||||||
if projectFlockKandangId != nil {
|
if projectFlockKandangId != nil {
|
||||||
kandangId = &expenseNonstock.KandangID
|
kandangId = &expenseNonstock.KandangID
|
||||||
}
|
}
|
||||||
@@ -269,9 +274,23 @@ func (s *expenseService) CreateOne(c *fiber.Ctx, req *validation.Create) (*expen
|
|||||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create initial approval")
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create initial approval")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(req.Documents) > 0 {
|
if s.DocumentSvc != nil && len(req.Documents) > 0 {
|
||||||
if err := s.processDocuments(c, expenseRepoTx, uint(expense.Id), req.Documents, false); err != nil {
|
documentFiles := make([]commonSvc.DocumentFile, 0, len(req.Documents))
|
||||||
return err
|
for idx, file := range req.Documents {
|
||||||
|
documentFiles = append(documentFiles, commonSvc.DocumentFile{
|
||||||
|
File: file,
|
||||||
|
Type: string(utils.DocumentTypeExpense),
|
||||||
|
Index: &idx,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
_, err := s.DocumentSvc.UploadDocuments(c.Context(), commonSvc.DocumentUploadRequest{
|
||||||
|
DocumentableType: string(utils.DocumentableTypeExpense),
|
||||||
|
DocumentableID: expense.Id,
|
||||||
|
CreatedBy: &createdByUint,
|
||||||
|
Files: documentFiles,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to upload expense documents")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -385,7 +404,7 @@ func (s expenseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if categoryChanged {
|
if categoryChanged {
|
||||||
if currentExpense.Category == "BOP" && newCategory == "NON-BOP" {
|
if currentExpense.Category == string(utils.ExpenseCategoryBOP) && newCategory == string(utils.ExpenseCategoryNonBOP) {
|
||||||
|
|
||||||
var existingExpenseNonstocks []entity.ExpenseNonstock
|
var existingExpenseNonstocks []entity.ExpenseNonstock
|
||||||
if err := tx.Where("expense_id = ?", id).Find(&existingExpenseNonstocks).Error; err != nil {
|
if err := tx.Where("expense_id = ?", id).Find(&existingExpenseNonstocks).Error; err != nil {
|
||||||
@@ -400,7 +419,7 @@ func (s expenseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
|||||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update project flock kandang id to null")
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update project flock kandang id to null")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if currentExpense.Category == "NON-BOP" && newCategory == "BOP" {
|
} else if currentExpense.Category == string(utils.ExpenseCategoryNonBOP) && newCategory == string(utils.ExpenseCategoryBOP) {
|
||||||
|
|
||||||
var existingExpenseNonstocks []entity.ExpenseNonstock
|
var existingExpenseNonstocks []entity.ExpenseNonstock
|
||||||
if err := tx.Where("expense_id = ?", id).Find(&existingExpenseNonstocks).Error; err != nil {
|
if err := tx.Where("expense_id = ?", id).Find(&existingExpenseNonstocks).Error; err != nil {
|
||||||
@@ -457,7 +476,7 @@ func (s expenseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
|||||||
for _, expenseNonstock := range *req.ExpenseNonstocks {
|
for _, expenseNonstock := range *req.ExpenseNonstocks {
|
||||||
var projectFlockKandangId *uint64
|
var projectFlockKandangId *uint64
|
||||||
|
|
||||||
if updatedExpense.Category == "BOP" {
|
if updatedExpense.Category == string(utils.ExpenseCategoryBOP) {
|
||||||
projectFlockKandangRepoTx := projectFlockKandangRepo.NewProjectFlockKandangRepository(tx)
|
projectFlockKandangRepoTx := projectFlockKandangRepo.NewProjectFlockKandangRepository(tx)
|
||||||
projectFlockKandang, err := projectFlockKandangRepoTx.GetActiveByKandangID(c.Context(), uint(expenseNonstock.KandangID))
|
projectFlockKandang, err := projectFlockKandangRepoTx.GetActiveByKandangID(c.Context(), uint(expenseNonstock.KandangID))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -480,10 +499,10 @@ func (s expenseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
|||||||
}
|
}
|
||||||
|
|
||||||
var kandangId *uint64
|
var kandangId *uint64
|
||||||
if updatedExpense.Category == "NON-BOP" {
|
if updatedExpense.Category == string(utils.ExpenseCategoryNonBOP) {
|
||||||
id := uint64(expenseNonstock.KandangID)
|
id := uint64(expenseNonstock.KandangID)
|
||||||
kandangId = &id
|
kandangId = &id
|
||||||
} else if updatedExpense.Category == "BOP" {
|
} else if updatedExpense.Category == string(utils.ExpenseCategoryBOP) {
|
||||||
if projectFlockKandangId != nil {
|
if projectFlockKandangId != nil {
|
||||||
kandangId = &expenseNonstock.KandangID
|
kandangId = &expenseNonstock.KandangID
|
||||||
}
|
}
|
||||||
@@ -527,9 +546,23 @@ func (s expenseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(req.Documents) > 0 {
|
if s.DocumentSvc != nil && len(req.Documents) > 0 {
|
||||||
if err := s.processDocuments(c, expenseRepoTx, id, req.Documents, false); err != nil {
|
documentFiles := make([]commonSvc.DocumentFile, 0, len(req.Documents))
|
||||||
return err
|
for idx, file := range req.Documents {
|
||||||
|
documentFiles = append(documentFiles, commonSvc.DocumentFile{
|
||||||
|
File: file,
|
||||||
|
Type: string(utils.DocumentTypeExpense),
|
||||||
|
Index: &idx,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
_, err := s.DocumentSvc.UploadDocuments(c.Context(), commonSvc.DocumentUploadRequest{
|
||||||
|
DocumentableType: string(utils.DocumentableTypeExpense),
|
||||||
|
DocumentableID: uint64(id),
|
||||||
|
CreatedBy: &actorID,
|
||||||
|
Files: documentFiles,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to upload expense documents")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -658,9 +691,24 @@ func (s *expenseService) CreateRealization(c *fiber.Ctx, expenseID uint, req *va
|
|||||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update realization date")
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update realization date")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(req.Documents) > 0 {
|
if s.DocumentSvc != nil && len(req.Documents) > 0 {
|
||||||
if err := s.processDocuments(c, expenseRepoTx, expenseID, req.Documents, true); err != nil {
|
documentFiles := make([]commonSvc.DocumentFile, 0, len(req.Documents))
|
||||||
return err
|
for idx, file := range req.Documents {
|
||||||
|
documentFiles = append(documentFiles, commonSvc.DocumentFile{
|
||||||
|
File: file,
|
||||||
|
Type: string(utils.DocumentTypeExpenseRealization),
|
||||||
|
Index: &idx,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
actorID := uint(1) // TODO: replace with authenticated user id
|
||||||
|
_, err := s.DocumentSvc.UploadDocuments(c.Context(), commonSvc.DocumentUploadRequest{
|
||||||
|
DocumentableType: string(utils.DocumentableTypeExpenseRealization),
|
||||||
|
DocumentableID: uint64(expenseID),
|
||||||
|
CreatedBy: &actorID,
|
||||||
|
Files: documentFiles,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to upload expense realization documents")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -833,9 +881,24 @@ func (s *expenseService) UpdateRealization(c *fiber.Ctx, expenseID uint, req *va
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(req.Documents) > 0 {
|
if s.DocumentSvc != nil && len(req.Documents) > 0 {
|
||||||
if err := s.processDocuments(c, expenseRepoTx, expenseID, req.Documents, true); err != nil {
|
documentFiles := make([]commonSvc.DocumentFile, 0, len(req.Documents))
|
||||||
return err
|
for idx, file := range req.Documents {
|
||||||
|
documentFiles = append(documentFiles, commonSvc.DocumentFile{
|
||||||
|
File: file,
|
||||||
|
Type: string(utils.DocumentTypeExpenseRealization),
|
||||||
|
Index: &idx,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
actorID := uint(1) // TODO: replace with authenticated user id
|
||||||
|
_, err := s.DocumentSvc.UploadDocuments(c.Context(), commonSvc.DocumentUploadRequest{
|
||||||
|
DocumentableType: string(utils.DocumentableTypeExpenseRealization),
|
||||||
|
DocumentableID: uint64(expenseID),
|
||||||
|
CreatedBy: &actorID,
|
||||||
|
Files: documentFiles,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to upload expense realization documents")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -870,79 +933,6 @@ func (s *expenseService) UpdateRealization(c *fiber.Ctx, expenseID uint, req *va
|
|||||||
return responseDTO, nil
|
return responseDTO, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *expenseService) processDocuments(ctx *fiber.Ctx, expenseRepoTx repository.ExpenseRepository, expenseID uint, documents []*multipart.FileHeader, isRealization bool) error {
|
|
||||||
|
|
||||||
if len(documents) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var existingDocuments []expenseDto.DocumentDTO
|
|
||||||
var fieldName string
|
|
||||||
|
|
||||||
if isRealization {
|
|
||||||
fieldName = "realization_document_path"
|
|
||||||
} else {
|
|
||||||
fieldName = "document_path"
|
|
||||||
}
|
|
||||||
|
|
||||||
expense, err := expenseRepoTx.GetByID(ctx.Context(), expenseID, nil)
|
|
||||||
if err != nil {
|
|
||||||
|
|
||||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to get expense for document processing")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
|
|
||||||
var documentField sql.NullString
|
|
||||||
if isRealization {
|
|
||||||
documentField = expense.RealizationDocumentPath
|
|
||||||
} else {
|
|
||||||
documentField = expense.DocumentPath
|
|
||||||
}
|
|
||||||
|
|
||||||
if documentField.Valid && documentField.String != "" {
|
|
||||||
if err := json.Unmarshal([]byte(documentField.String), &existingDocuments); err != nil {
|
|
||||||
existingDocuments = []expenseDto.DocumentDTO{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var startID uint64 = 1
|
|
||||||
if len(existingDocuments) > 0 {
|
|
||||||
|
|
||||||
maxID := uint64(0)
|
|
||||||
for _, doc := range existingDocuments {
|
|
||||||
if doc.ID > maxID {
|
|
||||||
maxID = doc.ID
|
|
||||||
}
|
|
||||||
}
|
|
||||||
startID = maxID + 1
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, doc := range documents {
|
|
||||||
documentPath := doc.Filename
|
|
||||||
|
|
||||||
document := expenseDto.DocumentDTO{
|
|
||||||
ID: startID + uint64(i),
|
|
||||||
Path: documentPath,
|
|
||||||
}
|
|
||||||
existingDocuments = append(existingDocuments, document)
|
|
||||||
}
|
|
||||||
|
|
||||||
documentJSON, err := json.Marshal(existingDocuments)
|
|
||||||
if err != nil {
|
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to save documents")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := expenseRepoTx.PatchOne(ctx.Context(), expenseID, map[string]interface{}{
|
|
||||||
fieldName: string(documentJSON),
|
|
||||||
}, nil); err != nil {
|
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to save documents")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *expenseService) DeleteDocument(ctx *fiber.Ctx, expenseID uint, documentID uint64, isRealization bool) error {
|
func (s *expenseService) DeleteDocument(ctx *fiber.Ctx, expenseID uint, documentID uint64, isRealization bool) error {
|
||||||
|
|
||||||
if err := commonSvc.EnsureRelations(ctx.Context(),
|
if err := commonSvc.EnsureRelations(ctx.Context(),
|
||||||
@@ -951,62 +941,40 @@ func (s *expenseService) DeleteDocument(ctx *fiber.Ctx, expenseID uint, document
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.Repository.DB().WithContext(ctx.Context()).Transaction(func(tx *gorm.DB) error {
|
if s.DocumentSvc == nil {
|
||||||
expenseRepoTx := repository.NewExpenseRepository(tx)
|
return fiber.NewError(fiber.StatusInternalServerError, "Document service not available")
|
||||||
|
}
|
||||||
|
|
||||||
expense, err := expenseRepoTx.GetByID(ctx.Context(), expenseID, nil)
|
// Verify document exists and belongs to the expense
|
||||||
if err != nil {
|
var documentableType string
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to get expense for document deletion")
|
if isRealization {
|
||||||
|
documentableType = string(utils.DocumentableTypeExpenseRealization)
|
||||||
|
} else {
|
||||||
|
documentableType = string(utils.DocumentableTypeExpense)
|
||||||
|
}
|
||||||
|
|
||||||
|
documents, err := s.DocumentSvc.ListByTarget(ctx.Context(), documentableType, uint64(expenseID))
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to retrieve documents")
|
||||||
|
}
|
||||||
|
|
||||||
|
documentFound := false
|
||||||
|
var documentIDsToDelete []uint
|
||||||
|
for _, doc := range documents {
|
||||||
|
if uint64(doc.Id) == documentID {
|
||||||
|
documentFound = true
|
||||||
|
documentIDsToDelete = append(documentIDsToDelete, doc.Id)
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var existingDocuments []expenseDto.DocumentDTO
|
if !documentFound {
|
||||||
var fieldName string
|
return fiber.NewError(fiber.StatusNotFound, "Document not found")
|
||||||
|
}
|
||||||
|
|
||||||
if isRealization {
|
// Delete document from database and storage
|
||||||
fieldName = "realization_document_path"
|
if err := s.DocumentSvc.DeleteDocuments(ctx.Context(), documentIDsToDelete, true); err != nil {
|
||||||
if expense.RealizationDocumentPath.Valid && expense.RealizationDocumentPath.String != "" {
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to delete document")
|
||||||
if err := json.Unmarshal([]byte(expense.RealizationDocumentPath.String), &existingDocuments); err != nil {
|
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to parse existing realization documents")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
fieldName = "document_path"
|
|
||||||
if expense.DocumentPath.Valid && expense.DocumentPath.String != "" {
|
|
||||||
if err := json.Unmarshal([]byte(expense.DocumentPath.String), &existingDocuments); err != nil {
|
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to parse existing documents")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var updatedDocuments []expenseDto.DocumentDTO
|
|
||||||
documentFound := false
|
|
||||||
|
|
||||||
for _, doc := range existingDocuments {
|
|
||||||
if doc.ID == documentID {
|
|
||||||
documentFound = true
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
updatedDocuments = append(updatedDocuments, doc)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !documentFound {
|
|
||||||
return fiber.NewError(fiber.StatusNotFound, "Document not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
documentJSON, err := json.Marshal(updatedDocuments)
|
|
||||||
if err != nil {
|
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to save documents")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := expenseRepoTx.PatchOne(ctx.Context(), expenseID, map[string]interface{}{
|
|
||||||
fieldName: string(documentJSON),
|
|
||||||
}, nil); err != nil {
|
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to save documents")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -0,0 +1,92 @@
|
|||||||
|
package controller
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/modules/finance/initials/dto"
|
||||||
|
service "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/initials/services"
|
||||||
|
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/initials/validations"
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/response"
|
||||||
|
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type InitialController struct {
|
||||||
|
InitialService service.InitialService
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewInitialController(initialService service.InitialService) *InitialController {
|
||||||
|
return &InitialController{
|
||||||
|
InitialService: initialService,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *InitialController) GetOne(c *fiber.Ctx) error {
|
||||||
|
param := c.Params("id")
|
||||||
|
|
||||||
|
id, err := strconv.Atoi(param)
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := u.InitialService.GetOne(c, uint(id))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusOK).
|
||||||
|
JSON(response.Success{
|
||||||
|
Code: fiber.StatusOK,
|
||||||
|
Status: "success",
|
||||||
|
Message: "Get initial successfully",
|
||||||
|
Data: dto.ToInitialListDTO(*result),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *InitialController) CreateOne(c *fiber.Ctx) error {
|
||||||
|
req := new(validation.Create)
|
||||||
|
|
||||||
|
if err := c.BodyParser(req); err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := u.InitialService.CreateOne(c, req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusCreated).
|
||||||
|
JSON(response.Success{
|
||||||
|
Code: fiber.StatusCreated,
|
||||||
|
Status: "success",
|
||||||
|
Message: "Create initial successfully",
|
||||||
|
Data: dto.ToInitialListDTO(*result),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *InitialController) UpdateOne(c *fiber.Ctx) error {
|
||||||
|
req := new(validation.Update)
|
||||||
|
param := c.Params("id")
|
||||||
|
|
||||||
|
id, err := strconv.Atoi(param)
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := c.BodyParser(req); err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := u.InitialService.UpdateOne(c, req, uint(id))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusOK).
|
||||||
|
JSON(response.Success{
|
||||||
|
Code: fiber.StatusOK,
|
||||||
|
Status: "success",
|
||||||
|
Message: "Update initial successfully",
|
||||||
|
Data: dto.ToInitialListDTO(*result),
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,163 @@
|
|||||||
|
package dto
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
|
approvalDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/approvals/dto"
|
||||||
|
bankDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/banks/dto"
|
||||||
|
userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
// === DTO Structs ===
|
||||||
|
|
||||||
|
type InitialRelationDTO struct {
|
||||||
|
Id uint `json:"id"`
|
||||||
|
ReferenceNumber string `json:"reference_number"`
|
||||||
|
TransactionType string `json:"transaction_type"`
|
||||||
|
InitialBalanceType string `json:"initial_balance_type"`
|
||||||
|
InitialBalanceTypeLabel string `json:"initial_balance_type_label"`
|
||||||
|
Party Party `json:"party"`
|
||||||
|
Bank bankDTO.BankRelationDTO `json:"bank,omitempty"`
|
||||||
|
Direction string `json:"direction"`
|
||||||
|
Nominal float64 `json:"nominal"`
|
||||||
|
Notes string `json:"notes"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type InitialListDTO struct {
|
||||||
|
InitialRelationDTO
|
||||||
|
CreatedBy uint `json:"created_by"`
|
||||||
|
CreatedByUser userDTO.UserRelationDTO `json:"created_by_user,omitempty"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
|
Approval approvalDTO.ApprovalRelationDTO `json:"approval"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type InitialDetailDTO struct {
|
||||||
|
InitialListDTO
|
||||||
|
}
|
||||||
|
|
||||||
|
type Party struct {
|
||||||
|
Id uint `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
AccountNumber string `json:"account_number"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// === Mapper Functions ===
|
||||||
|
|
||||||
|
func ToInitialRelationDTO(e entity.Payment) InitialRelationDTO {
|
||||||
|
reference := ""
|
||||||
|
if e.ReferenceNumber != nil {
|
||||||
|
reference = *e.ReferenceNumber
|
||||||
|
}
|
||||||
|
|
||||||
|
initialBalanceType := initialBalanceTypeFromPayment(e)
|
||||||
|
return InitialRelationDTO{
|
||||||
|
Id: e.Id,
|
||||||
|
ReferenceNumber: reference,
|
||||||
|
TransactionType: transactionTypeLabel(e.TransactionType),
|
||||||
|
InitialBalanceType: initialBalanceType,
|
||||||
|
InitialBalanceTypeLabel: initialBalanceLabel(initialBalanceType),
|
||||||
|
Party: partyFromInitial(e),
|
||||||
|
Bank: bankFromInitial(e),
|
||||||
|
Direction: e.Direction,
|
||||||
|
Nominal: e.Nominal,
|
||||||
|
Notes: e.Notes,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToInitialListDTO(e entity.Payment) InitialListDTO {
|
||||||
|
approval := approvalDTO.ApprovalRelationDTO{}
|
||||||
|
if e.LatestApproval != nil {
|
||||||
|
approval = approvalDTO.ToApprovalDTO(*e.LatestApproval)
|
||||||
|
}
|
||||||
|
|
||||||
|
return InitialListDTO{
|
||||||
|
InitialRelationDTO: ToInitialRelationDTO(e),
|
||||||
|
CreatedBy: e.CreatedBy,
|
||||||
|
CreatedByUser: userFromInitial(e),
|
||||||
|
CreatedAt: e.CreatedAt,
|
||||||
|
UpdatedAt: e.UpdatedAt,
|
||||||
|
Approval: approval,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToInitialListDTOs(e []entity.Payment) []InitialListDTO {
|
||||||
|
result := make([]InitialListDTO, len(e))
|
||||||
|
for i, r := range e {
|
||||||
|
result[i] = ToInitialListDTO(r)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToInitialDetailDTO(e entity.Payment) InitialDetailDTO {
|
||||||
|
return InitialDetailDTO{
|
||||||
|
InitialListDTO: ToInitialListDTO(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func partyFromInitial(e entity.Payment) Party {
|
||||||
|
party := Party{
|
||||||
|
Id: e.PartyId,
|
||||||
|
Type: e.PartyType,
|
||||||
|
}
|
||||||
|
|
||||||
|
switch utils.PaymentParty(e.PartyType) {
|
||||||
|
case utils.PaymentPartyCustomer:
|
||||||
|
if e.Customer != nil && e.Customer.Id != 0 {
|
||||||
|
party.Name = e.Customer.Name
|
||||||
|
party.AccountNumber = e.Customer.AccountNumber
|
||||||
|
}
|
||||||
|
case utils.PaymentPartySupplier:
|
||||||
|
if e.Supplier != nil && e.Supplier.Id != 0 {
|
||||||
|
party.Name = e.Supplier.Name
|
||||||
|
if e.Supplier.AccountNumber != nil {
|
||||||
|
party.AccountNumber = *e.Supplier.AccountNumber
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return party
|
||||||
|
}
|
||||||
|
|
||||||
|
func bankFromInitial(e entity.Payment) bankDTO.BankRelationDTO {
|
||||||
|
if e.BankWarehouse.Id == 0 {
|
||||||
|
return bankDTO.BankRelationDTO{}
|
||||||
|
}
|
||||||
|
return bankDTO.ToBankRelationDTO(e.BankWarehouse)
|
||||||
|
}
|
||||||
|
|
||||||
|
func userFromInitial(e entity.Payment) userDTO.UserRelationDTO {
|
||||||
|
if e.CreatedUser.Id == 0 {
|
||||||
|
return userDTO.UserRelationDTO{}
|
||||||
|
}
|
||||||
|
return userDTO.ToUserRelationDTO(e.CreatedUser)
|
||||||
|
}
|
||||||
|
|
||||||
|
func transactionTypeLabel(transactionType string) string {
|
||||||
|
if strings.EqualFold(transactionType, string(utils.TransactionTypeSaldoAwal)) {
|
||||||
|
return "Saldo Awal"
|
||||||
|
}
|
||||||
|
return transactionType
|
||||||
|
}
|
||||||
|
|
||||||
|
func initialBalanceLabel(balanceType string) string {
|
||||||
|
switch strings.ToUpper(strings.TrimSpace(balanceType)) {
|
||||||
|
case "NEGATIVE":
|
||||||
|
return "Saldo Awal Negatif"
|
||||||
|
case "POSITIVE":
|
||||||
|
return "Saldo Awal Positif"
|
||||||
|
default:
|
||||||
|
return balanceType
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func initialBalanceTypeFromPayment(e entity.Payment) string {
|
||||||
|
if strings.EqualFold(e.Direction, "OUT") || e.Nominal < 0 {
|
||||||
|
return "NEGATIVE"
|
||||||
|
}
|
||||||
|
return "POSITIVE"
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package initials
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/go-playground/validator/v10"
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||||
|
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
|
||||||
|
rInitial "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/initials/repositories"
|
||||||
|
sInitial "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/initials/services"
|
||||||
|
|
||||||
|
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
|
||||||
|
sUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
||||||
|
)
|
||||||
|
|
||||||
|
type InitialModule struct{}
|
||||||
|
|
||||||
|
func (InitialModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
|
||||||
|
initialRepo := rInitial.NewInitialRepository(db)
|
||||||
|
userRepo := rUser.NewUserRepository(db)
|
||||||
|
|
||||||
|
approvalRepo := commonRepo.NewApprovalRepository(db)
|
||||||
|
approvalService := commonSvc.NewApprovalService(approvalRepo)
|
||||||
|
if err := approvalService.RegisterWorkflowSteps(utils.ApprovalWorkflowInitial, utils.InitialApprovalSteps); err != nil {
|
||||||
|
panic(fmt.Sprintf("failed to register initial approval workflow: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
initialService := sInitial.NewInitialService(initialRepo, approvalService, validate)
|
||||||
|
userService := sUser.NewUserService(userRepo, validate)
|
||||||
|
|
||||||
|
InitialRoutes(router, userService, initialService)
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||||
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type InitialRepository interface {
|
||||||
|
repository.BaseRepository[entity.Payment]
|
||||||
|
BankExists(ctx context.Context, bankId uint) (bool, error)
|
||||||
|
CustomerExists(ctx context.Context, customerId uint) (bool, error)
|
||||||
|
SupplierExists(ctx context.Context, supplierId uint) (bool, error)
|
||||||
|
NextPaymentSequence(ctx context.Context) (int64, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type InitialRepositoryImpl struct {
|
||||||
|
*repository.BaseRepositoryImpl[entity.Payment]
|
||||||
|
db *gorm.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewInitialRepository(db *gorm.DB) InitialRepository {
|
||||||
|
return &InitialRepositoryImpl{
|
||||||
|
BaseRepositoryImpl: repository.NewBaseRepository[entity.Payment](db),
|
||||||
|
db: db,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *InitialRepositoryImpl) BankExists(ctx context.Context, bankId uint) (bool, error) {
|
||||||
|
return repository.Exists[entity.Bank](ctx, r.db, bankId)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *InitialRepositoryImpl) CustomerExists(ctx context.Context, customerId uint) (bool, error) {
|
||||||
|
return repository.Exists[entity.Customer](ctx, r.db, customerId)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *InitialRepositoryImpl) SupplierExists(ctx context.Context, supplierId uint) (bool, error) {
|
||||||
|
return repository.Exists[entity.Supplier](ctx, r.db, supplierId)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *InitialRepositoryImpl) NextPaymentSequence(ctx context.Context) (int64, error) {
|
||||||
|
var next int64
|
||||||
|
if err := r.db.WithContext(ctx).
|
||||||
|
Raw("SELECT nextval('payments_code_seq')").
|
||||||
|
Scan(&next).Error; err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return next, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package initials
|
||||||
|
|
||||||
|
import (
|
||||||
|
// m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
||||||
|
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/initials/controllers"
|
||||||
|
initial "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/initials/services"
|
||||||
|
user "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
||||||
|
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func InitialRoutes(v1 fiber.Router, u user.UserService, s initial.InitialService) {
|
||||||
|
ctrl := controller.NewInitialController(s)
|
||||||
|
|
||||||
|
route := v1.Group("/initial-balances")
|
||||||
|
// route.Use(m.Auth(u))
|
||||||
|
|
||||||
|
route.Post("/", ctrl.CreateOne)
|
||||||
|
route.Get("/:id", ctrl.GetOne)
|
||||||
|
route.Patch("/:id", ctrl.UpdateOne)
|
||||||
|
}
|
||||||
@@ -0,0 +1,336 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||||
|
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||||
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
|
m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
||||||
|
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/initials/repositories"
|
||||||
|
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/initials/validations"
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
|
approvalutils "gitlab.com/mbugroup/lti-api.git/internal/utils/approvals"
|
||||||
|
|
||||||
|
"github.com/go-playground/validator/v10"
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type InitialService interface {
|
||||||
|
GetOne(ctx *fiber.Ctx, id uint) (*entity.Payment, error)
|
||||||
|
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.Payment, error)
|
||||||
|
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.Payment, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type initialService struct {
|
||||||
|
Log *logrus.Logger
|
||||||
|
Validate *validator.Validate
|
||||||
|
Repository repository.InitialRepository
|
||||||
|
ApprovalSvc commonSvc.ApprovalService
|
||||||
|
approvalWorkflow approvalutils.ApprovalWorkflowKey
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewInitialService(
|
||||||
|
repo repository.InitialRepository,
|
||||||
|
approvalSvc commonSvc.ApprovalService,
|
||||||
|
validate *validator.Validate,
|
||||||
|
) InitialService {
|
||||||
|
return &initialService{
|
||||||
|
Log: utils.Log,
|
||||||
|
Validate: validate,
|
||||||
|
Repository: repo,
|
||||||
|
ApprovalSvc: approvalSvc,
|
||||||
|
approvalWorkflow: utils.ApprovalWorkflowInitial,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s initialService) withRelations(db *gorm.DB) *gorm.DB {
|
||||||
|
return db.Preload("CreatedUser").
|
||||||
|
Preload("BankWarehouse").
|
||||||
|
Preload("Customer").
|
||||||
|
Preload("Supplier")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s initialService) GetOne(c *fiber.Ctx, id uint) (*entity.Payment, error) {
|
||||||
|
initial, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, fiber.NewError(fiber.StatusNotFound, "Initial not found")
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Errorf("Failed get initial by id: %+v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !isInitialTransaction(initial.TransactionType) {
|
||||||
|
return nil, fiber.NewError(fiber.StatusNotFound, "Initial not found")
|
||||||
|
}
|
||||||
|
if s.ApprovalSvc != nil {
|
||||||
|
approval, err := s.ApprovalSvc.LatestByTarget(c.Context(), s.approvalWorkflow, id, s.approvalQueryModifier())
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Warnf("Unable to load latest approval for initial %d: %+v", id, err)
|
||||||
|
} else {
|
||||||
|
initial.LatestApproval = approval
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return initial, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *initialService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.Payment, error) {
|
||||||
|
if err := s.Validate.Struct(req); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
party, err := normalizePartyType(req.PartyType)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.ensurePartyExists(c.Context(), party, req.PartyId); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := s.ensureBankExists(c.Context(), req.BankId); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
balanceType, err := normalizeInitialBalanceType(req.InitialBalanceType)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
actorID, err := m.ActorIDFromContext(c)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
code, err := s.generateInitialCode(c.Context())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
reference := req.ReferenceNumber
|
||||||
|
createBody := &entity.Payment{
|
||||||
|
PaymentCode: code,
|
||||||
|
ReferenceNumber: &reference,
|
||||||
|
TransactionType: string(utils.TransactionTypeSaldoAwal),
|
||||||
|
PartyType: party,
|
||||||
|
PartyId: req.PartyId,
|
||||||
|
PaymentDate: time.Now(),
|
||||||
|
PaymentMethod: string(utils.PaymentMethodSaldo),
|
||||||
|
BankId: req.BankId,
|
||||||
|
Direction: directionForInitialType(balanceType),
|
||||||
|
Nominal: signedNominal(balanceType, req.Nominal),
|
||||||
|
Notes: req.Note,
|
||||||
|
CreatedBy: actorID,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
||||||
|
initialRepoTx := repository.NewInitialRepository(dbTransaction)
|
||||||
|
if err := initialRepoTx.CreateOne(c.Context(), createBody, nil); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if s.ApprovalSvc != nil {
|
||||||
|
action := entity.ApprovalActionCreated
|
||||||
|
approvalSvcTx := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(dbTransaction))
|
||||||
|
_, err := approvalSvcTx.CreateApproval(
|
||||||
|
c.Context(),
|
||||||
|
s.approvalWorkflow,
|
||||||
|
createBody.Id,
|
||||||
|
utils.InitialStepPengajuan,
|
||||||
|
&action,
|
||||||
|
actorID,
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Errorf("Failed to create initial: %+v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.GetOne(c, createBody.Id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s initialService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.Payment, error) {
|
||||||
|
if err := s.Validate.Struct(req); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
updateBody := make(map[string]any)
|
||||||
|
|
||||||
|
if req.ReferenceNumber != nil {
|
||||||
|
updateBody["reference_number"] = *req.ReferenceNumber
|
||||||
|
}
|
||||||
|
if req.Note != nil {
|
||||||
|
updateBody["notes"] = *req.Note
|
||||||
|
}
|
||||||
|
if req.BankId != nil {
|
||||||
|
if err := s.ensureBankExists(c.Context(), req.BankId); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
updateBody["bank_id"] = *req.BankId
|
||||||
|
}
|
||||||
|
|
||||||
|
requiresExisting := req.PartyType != nil || req.PartyId != nil || req.InitialBalanceType != nil || req.Nominal != nil
|
||||||
|
requiresVerification := requiresExisting || req.ReferenceNumber != nil || req.Note != nil || req.BankId != nil
|
||||||
|
var existing *entity.Payment
|
||||||
|
if requiresVerification {
|
||||||
|
current, err := s.Repository.GetByID(c.Context(), id, nil)
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, fiber.NewError(fiber.StatusNotFound, "Initial not found")
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Errorf("Failed get initial by id: %+v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !isInitialTransaction(current.TransactionType) {
|
||||||
|
return nil, fiber.NewError(fiber.StatusNotFound, "Initial not found")
|
||||||
|
}
|
||||||
|
existing = current
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.PartyType != nil || req.PartyId != nil {
|
||||||
|
partyType := existing.PartyType
|
||||||
|
partyId := existing.PartyId
|
||||||
|
|
||||||
|
if req.PartyType != nil {
|
||||||
|
normalized, err := normalizePartyType(*req.PartyType)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
partyType = normalized
|
||||||
|
updateBody["party_type"] = partyType
|
||||||
|
}
|
||||||
|
if req.PartyId != nil {
|
||||||
|
partyId = *req.PartyId
|
||||||
|
updateBody["party_id"] = partyId
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.ensurePartyExists(c.Context(), partyType, partyId); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.InitialBalanceType != nil || req.Nominal != nil {
|
||||||
|
balanceType := balanceTypeFromPayment(existing)
|
||||||
|
if req.InitialBalanceType != nil {
|
||||||
|
normalized, err := normalizeInitialBalanceType(*req.InitialBalanceType)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
balanceType = normalized
|
||||||
|
}
|
||||||
|
|
||||||
|
nominal := math.Abs(existing.Nominal)
|
||||||
|
if req.Nominal != nil {
|
||||||
|
nominal = *req.Nominal
|
||||||
|
}
|
||||||
|
|
||||||
|
updateBody["direction"] = directionForInitialType(balanceType)
|
||||||
|
updateBody["nominal"] = signedNominal(balanceType, nominal)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(updateBody) == 0 {
|
||||||
|
return s.GetOne(c, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.Repository.PatchOne(c.Context(), id, updateBody, nil); err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, fiber.NewError(fiber.StatusNotFound, "Initial not found")
|
||||||
|
}
|
||||||
|
s.Log.Errorf("Failed to update initial: %+v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.GetOne(c, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func isInitialTransaction(transactionType string) bool {
|
||||||
|
return strings.EqualFold(transactionType, string(utils.TransactionTypeSaldoAwal))
|
||||||
|
}
|
||||||
|
|
||||||
|
func balanceTypeFromPayment(payment *entity.Payment) string {
|
||||||
|
if strings.EqualFold(payment.Direction, "OUT") || payment.Nominal < 0 {
|
||||||
|
return "NEGATIVE"
|
||||||
|
}
|
||||||
|
return "POSITIVE"
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizePartyType(partyType string) (string, error) {
|
||||||
|
party := strings.ToUpper(strings.TrimSpace(partyType))
|
||||||
|
if !utils.IsValidPaymentParty(party) {
|
||||||
|
return "", utils.BadRequest("`party_type` must be `customer` or `supplier`")
|
||||||
|
}
|
||||||
|
return party, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizeInitialBalanceType(balanceType string) (string, error) {
|
||||||
|
normalized := strings.ToUpper(strings.TrimSpace(balanceType))
|
||||||
|
switch normalized {
|
||||||
|
case "NEGATIVE", "POSITIVE":
|
||||||
|
return normalized, nil
|
||||||
|
default:
|
||||||
|
return "", utils.BadRequest("`initial_balance_type` must be `NEGATIVE` or `POSITIVE`")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func directionForInitialType(balanceType string) string {
|
||||||
|
if strings.EqualFold(balanceType, "NEGATIVE") {
|
||||||
|
return "OUT"
|
||||||
|
}
|
||||||
|
return "IN"
|
||||||
|
}
|
||||||
|
|
||||||
|
func signedNominal(balanceType string, nominal float64) float64 {
|
||||||
|
normalized := math.Abs(nominal)
|
||||||
|
if strings.EqualFold(balanceType, "NEGATIVE") {
|
||||||
|
return -normalized
|
||||||
|
}
|
||||||
|
return normalized
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s initialService) generateInitialCode(ctx context.Context) (string, error) {
|
||||||
|
sequence, err := s.Repository.NextPaymentSequence(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("INIT-%05d", sequence), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s initialService) approvalQueryModifier() func(*gorm.DB) *gorm.DB {
|
||||||
|
return func(db *gorm.DB) *gorm.DB {
|
||||||
|
return db.Preload("ActionUser")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s initialService) ensurePartyExists(ctx context.Context, partyType string, partyId uint) error {
|
||||||
|
switch utils.PaymentParty(partyType) {
|
||||||
|
case utils.PaymentPartyCustomer:
|
||||||
|
return commonSvc.EnsureRelations(ctx,
|
||||||
|
commonSvc.RelationCheck{Name: "Customer", ID: &partyId, Exists: s.Repository.CustomerExists},
|
||||||
|
)
|
||||||
|
case utils.PaymentPartySupplier:
|
||||||
|
return commonSvc.EnsureRelations(ctx,
|
||||||
|
commonSvc.RelationCheck{Name: "Supplier", ID: &partyId, Exists: s.Repository.SupplierExists},
|
||||||
|
)
|
||||||
|
default:
|
||||||
|
return utils.BadRequest("`party_type` must be `customer` or `supplier`")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s initialService) ensureBankExists(ctx context.Context, bankId *uint) error {
|
||||||
|
return commonSvc.EnsureRelations(ctx,
|
||||||
|
commonSvc.RelationCheck{Name: "Bank", ID: bankId, Exists: s.Repository.BankExists},
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package validation
|
||||||
|
|
||||||
|
type Create struct {
|
||||||
|
PartyType string `json:"party_type" validate:"required_strict,max=50"`
|
||||||
|
PartyId uint `json:"party_id" validate:"required_strict,number,gt=0"`
|
||||||
|
BankId *uint `json:"bank_id" validate:"required_strict,number,gt=0"`
|
||||||
|
ReferenceNumber string `json:"reference_number" validate:"required_strict,max=100"`
|
||||||
|
InitialBalanceType string `json:"initial_balance_type" validate:"required_strict,oneof=NEGATIVE POSITIVE"`
|
||||||
|
Nominal float64 `json:"nominal" validate:"required_strict,gt=0"`
|
||||||
|
Note string `json:"note" validate:"required_strict,max=500"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Update struct {
|
||||||
|
PartyType *string `json:"party_type,omitempty" validate:"omitempty,max=50"`
|
||||||
|
PartyId *uint `json:"party_id,omitempty" validate:"omitempty,number,gt=0"`
|
||||||
|
BankId *uint `json:"bank_id,omitempty" validate:"omitempty,number,gt=0"`
|
||||||
|
ReferenceNumber *string `json:"reference_number,omitempty" validate:"omitempty,max=100"`
|
||||||
|
InitialBalanceType *string `json:"initial_balance_type,omitempty" validate:"omitempty,oneof=NEGATIVE POSITIVE"`
|
||||||
|
Nominal *float64 `json:"nominal,omitempty" validate:"omitempty,gt=0"`
|
||||||
|
Note *string `json:"note,omitempty" validate:"omitempty,max=500"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Query struct {
|
||||||
|
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
||||||
|
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
|
||||||
|
Search string `query:"search" validate:"omitempty,max=50"`
|
||||||
|
}
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
package controller
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/modules/finance/injections/dto"
|
||||||
|
service "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/injections/services"
|
||||||
|
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/injections/validations"
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/response"
|
||||||
|
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type InjectionController struct {
|
||||||
|
InjectionService service.InjectionService
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewInjectionController(injectionService service.InjectionService) *InjectionController {
|
||||||
|
return &InjectionController{
|
||||||
|
InjectionService: injectionService,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *InjectionController) GetOne(c *fiber.Ctx) error {
|
||||||
|
param := c.Params("id")
|
||||||
|
|
||||||
|
id, err := strconv.Atoi(param)
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := u.InjectionService.GetOne(c, uint(id))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusOK).
|
||||||
|
JSON(response.Success{
|
||||||
|
Code: fiber.StatusOK,
|
||||||
|
Status: "success",
|
||||||
|
Message: "Get injection successfully",
|
||||||
|
Data: dto.ToInjectionListDTO(*result),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *InjectionController) CreateOne(c *fiber.Ctx) error {
|
||||||
|
req := new(validation.Create)
|
||||||
|
|
||||||
|
if err := c.BodyParser(req); err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := u.InjectionService.CreateOne(c, req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusCreated).
|
||||||
|
JSON(response.Success{
|
||||||
|
Code: fiber.StatusCreated,
|
||||||
|
Status: "success",
|
||||||
|
Message: "Balance injection created successfully",
|
||||||
|
Data: dto.ToInjectionListDTO(*result),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *InjectionController) UpdateOne(c *fiber.Ctx) error {
|
||||||
|
req := new(validation.Update)
|
||||||
|
param := c.Params("id")
|
||||||
|
|
||||||
|
id, err := strconv.Atoi(param)
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := c.BodyParser(req); err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := u.InjectionService.UpdateOne(c, req, uint(id))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusOK).
|
||||||
|
JSON(response.Success{
|
||||||
|
Code: fiber.StatusOK,
|
||||||
|
Status: "success",
|
||||||
|
Message: "Update injection successfully",
|
||||||
|
Data: dto.ToInjectionListDTO(*result),
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,102 @@
|
|||||||
|
package dto
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
|
approvalDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/approvals/dto"
|
||||||
|
bankDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/banks/dto"
|
||||||
|
userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
// === DTO Structs ===
|
||||||
|
|
||||||
|
type InjectionRelationDTO struct {
|
||||||
|
Id uint `json:"id"`
|
||||||
|
TransactionType string `json:"transaction_type"`
|
||||||
|
Bank bankDTO.BankRelationDTO `json:"bank,omitempty"`
|
||||||
|
AdjustmentDate string `json:"adjustment_date"`
|
||||||
|
Direction string `json:"direction"`
|
||||||
|
Nominal float64 `json:"nominal"`
|
||||||
|
Notes string `json:"notes"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type InjectionListDTO struct {
|
||||||
|
InjectionRelationDTO
|
||||||
|
CreatedBy uint `json:"created_by"`
|
||||||
|
CreatedByUser userDTO.UserRelationDTO `json:"created_by_user,omitempty"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
|
Approval approvalDTO.ApprovalRelationDTO `json:"approval"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type InjectionDetailDTO struct {
|
||||||
|
InjectionListDTO
|
||||||
|
}
|
||||||
|
|
||||||
|
// === Mapper Functions ===
|
||||||
|
|
||||||
|
func ToInjectionRelationDTO(e entity.Payment) InjectionRelationDTO {
|
||||||
|
return InjectionRelationDTO{
|
||||||
|
Id: e.Id,
|
||||||
|
TransactionType: transactionTypeLabel(e.TransactionType),
|
||||||
|
Bank: bankFromInjection(e),
|
||||||
|
AdjustmentDate: utils.FormatDate(e.PaymentDate),
|
||||||
|
Direction: e.Direction,
|
||||||
|
Nominal: e.Nominal,
|
||||||
|
Notes: e.Notes,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToInjectionListDTO(e entity.Payment) InjectionListDTO {
|
||||||
|
approval := approvalDTO.ApprovalRelationDTO{}
|
||||||
|
if e.LatestApproval != nil {
|
||||||
|
approval = approvalDTO.ToApprovalDTO(*e.LatestApproval)
|
||||||
|
}
|
||||||
|
|
||||||
|
return InjectionListDTO{
|
||||||
|
InjectionRelationDTO: ToInjectionRelationDTO(e),
|
||||||
|
CreatedBy: e.CreatedBy,
|
||||||
|
CreatedByUser: userFromInjection(e),
|
||||||
|
CreatedAt: e.CreatedAt,
|
||||||
|
UpdatedAt: e.UpdatedAt,
|
||||||
|
Approval: approval,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToInjectionListDTOs(e []entity.Payment) []InjectionListDTO {
|
||||||
|
result := make([]InjectionListDTO, len(e))
|
||||||
|
for i, r := range e {
|
||||||
|
result[i] = ToInjectionListDTO(r)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToInjectionDetailDTO(e entity.Payment) InjectionDetailDTO {
|
||||||
|
return InjectionDetailDTO{
|
||||||
|
InjectionListDTO: ToInjectionListDTO(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func bankFromInjection(e entity.Payment) bankDTO.BankRelationDTO {
|
||||||
|
if e.BankWarehouse.Id == 0 {
|
||||||
|
return bankDTO.BankRelationDTO{}
|
||||||
|
}
|
||||||
|
return bankDTO.ToBankRelationDTO(e.BankWarehouse)
|
||||||
|
}
|
||||||
|
|
||||||
|
func userFromInjection(e entity.Payment) userDTO.UserRelationDTO {
|
||||||
|
if e.CreatedUser.Id == 0 {
|
||||||
|
return userDTO.UserRelationDTO{}
|
||||||
|
}
|
||||||
|
return userDTO.ToUserRelationDTO(e.CreatedUser)
|
||||||
|
}
|
||||||
|
|
||||||
|
func transactionTypeLabel(transactionType string) string {
|
||||||
|
if strings.EqualFold(transactionType, string(utils.TransactionTypeInjection)) {
|
||||||
|
return "Injection"
|
||||||
|
}
|
||||||
|
return transactionType
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package injections
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/go-playground/validator/v10"
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||||
|
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
|
||||||
|
rInjection "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/injections/repositories"
|
||||||
|
sInjection "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/injections/services"
|
||||||
|
|
||||||
|
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
|
||||||
|
sUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
||||||
|
)
|
||||||
|
|
||||||
|
type InjectionModule struct{}
|
||||||
|
|
||||||
|
func (InjectionModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
|
||||||
|
injectionRepo := rInjection.NewInjectionRepository(db)
|
||||||
|
userRepo := rUser.NewUserRepository(db)
|
||||||
|
|
||||||
|
approvalRepo := commonRepo.NewApprovalRepository(db)
|
||||||
|
approvalService := commonSvc.NewApprovalService(approvalRepo)
|
||||||
|
if err := approvalService.RegisterWorkflowSteps(utils.ApprovalWorkflowInjection, utils.InjectionApprovalSteps); err != nil {
|
||||||
|
panic(fmt.Sprintf("failed to register injection approval workflow: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
injectionService := sInjection.NewInjectionService(injectionRepo, approvalService, validate)
|
||||||
|
userService := sUser.NewUserService(userRepo, validate)
|
||||||
|
|
||||||
|
InjectionRoutes(router, userService, injectionService)
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||||
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type InjectionRepository interface {
|
||||||
|
repository.BaseRepository[entity.Payment]
|
||||||
|
BankExists(ctx context.Context, bankId uint) (bool, error)
|
||||||
|
NextPaymentSequence(ctx context.Context) (int64, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type InjectionRepositoryImpl struct {
|
||||||
|
*repository.BaseRepositoryImpl[entity.Payment]
|
||||||
|
db *gorm.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewInjectionRepository(db *gorm.DB) InjectionRepository {
|
||||||
|
return &InjectionRepositoryImpl{
|
||||||
|
BaseRepositoryImpl: repository.NewBaseRepository[entity.Payment](db),
|
||||||
|
db: db,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *InjectionRepositoryImpl) BankExists(ctx context.Context, bankId uint) (bool, error) {
|
||||||
|
return repository.Exists[entity.Bank](ctx, r.db, bankId)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *InjectionRepositoryImpl) NextPaymentSequence(ctx context.Context) (int64, error) {
|
||||||
|
var next int64
|
||||||
|
if err := r.db.WithContext(ctx).
|
||||||
|
Raw("SELECT nextval('payments_code_seq')").
|
||||||
|
Scan(&next).Error; err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return next, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package injections
|
||||||
|
|
||||||
|
import (
|
||||||
|
// m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
||||||
|
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/injections/controllers"
|
||||||
|
injection "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/injections/services"
|
||||||
|
user "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
||||||
|
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func InjectionRoutes(v1 fiber.Router, u user.UserService, s injection.InjectionService) {
|
||||||
|
ctrl := controller.NewInjectionController(s)
|
||||||
|
|
||||||
|
route := v1.Group("/injections")
|
||||||
|
// route.Use(m.Auth(u))
|
||||||
|
|
||||||
|
route.Post("/", ctrl.CreateOne)
|
||||||
|
route.Get("/:id", ctrl.GetOne)
|
||||||
|
route.Patch("/:id", ctrl.UpdateOne)
|
||||||
|
}
|
||||||
@@ -0,0 +1,230 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||||
|
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||||
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
|
m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
||||||
|
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/injections/repositories"
|
||||||
|
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/injections/validations"
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
|
approvalutils "gitlab.com/mbugroup/lti-api.git/internal/utils/approvals"
|
||||||
|
|
||||||
|
"github.com/go-playground/validator/v10"
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type InjectionService interface {
|
||||||
|
GetOne(ctx *fiber.Ctx, id uint) (*entity.Payment, error)
|
||||||
|
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.Payment, error)
|
||||||
|
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.Payment, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type injectionService struct {
|
||||||
|
Log *logrus.Logger
|
||||||
|
Validate *validator.Validate
|
||||||
|
Repository repository.InjectionRepository
|
||||||
|
ApprovalSvc commonSvc.ApprovalService
|
||||||
|
approvalWorkflow approvalutils.ApprovalWorkflowKey
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewInjectionService(
|
||||||
|
repo repository.InjectionRepository,
|
||||||
|
approvalSvc commonSvc.ApprovalService,
|
||||||
|
validate *validator.Validate,
|
||||||
|
) InjectionService {
|
||||||
|
return &injectionService{
|
||||||
|
Log: utils.Log,
|
||||||
|
Validate: validate,
|
||||||
|
Repository: repo,
|
||||||
|
ApprovalSvc: approvalSvc,
|
||||||
|
approvalWorkflow: utils.ApprovalWorkflowInjection,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s injectionService) withRelations(db *gorm.DB) *gorm.DB {
|
||||||
|
return db.Preload("CreatedUser").
|
||||||
|
Preload("BankWarehouse")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s injectionService) GetOne(c *fiber.Ctx, id uint) (*entity.Payment, error) {
|
||||||
|
injection, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, fiber.NewError(fiber.StatusNotFound, "Injection not found")
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Errorf("Failed get injection by id: %+v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !isInjectionTransaction(injection.TransactionType) {
|
||||||
|
return nil, fiber.NewError(fiber.StatusNotFound, "Injection not found")
|
||||||
|
}
|
||||||
|
if s.ApprovalSvc != nil {
|
||||||
|
approval, err := s.ApprovalSvc.LatestByTarget(c.Context(), s.approvalWorkflow, id, s.approvalQueryModifier())
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Warnf("Unable to load latest approval for injection %d: %+v", id, err)
|
||||||
|
} else {
|
||||||
|
injection.LatestApproval = approval
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return injection, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *injectionService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.Payment, error) {
|
||||||
|
if err := s.Validate.Struct(req); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.ensureBankExists(c.Context(), req.BankId); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
adjustmentDate, err := utils.ParseDateString(req.AdjustmentDate)
|
||||||
|
if err != nil {
|
||||||
|
return nil, utils.BadRequest(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
actorID, err := m.ActorIDFromContext(c)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
code, err := s.generateInjectionCode(c.Context())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
createBody := &entity.Payment{
|
||||||
|
PaymentCode: code,
|
||||||
|
TransactionType: string(utils.TransactionTypeInjection),
|
||||||
|
PartyType: string(utils.PaymentPartyCustomer),
|
||||||
|
PartyId: 0,
|
||||||
|
PaymentDate: adjustmentDate,
|
||||||
|
PaymentMethod: string(utils.PaymentMethodSaldo),
|
||||||
|
BankId: req.BankId,
|
||||||
|
Direction: "IN",
|
||||||
|
Nominal: req.Nominal,
|
||||||
|
Notes: req.Notes,
|
||||||
|
CreatedBy: actorID,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
||||||
|
injectionRepoTx := repository.NewInjectionRepository(dbTransaction)
|
||||||
|
if err := injectionRepoTx.CreateOne(c.Context(), createBody, nil); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if s.ApprovalSvc != nil {
|
||||||
|
action := entity.ApprovalActionCreated
|
||||||
|
approvalSvcTx := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(dbTransaction))
|
||||||
|
_, err := approvalSvcTx.CreateApproval(
|
||||||
|
c.Context(),
|
||||||
|
s.approvalWorkflow,
|
||||||
|
createBody.Id,
|
||||||
|
utils.InjectionStepPengajuan,
|
||||||
|
&action,
|
||||||
|
actorID,
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Errorf("Failed to create injection: %+v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.GetOne(c, createBody.Id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s injectionService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.Payment, error) {
|
||||||
|
if err := s.Validate.Struct(req); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
updateBody := make(map[string]any)
|
||||||
|
|
||||||
|
requiresVerification := req.BankId != nil || req.AdjustmentDate != nil || req.Nominal != nil || req.Notes != nil
|
||||||
|
if requiresVerification {
|
||||||
|
current, err := s.Repository.GetByID(c.Context(), id, nil)
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, fiber.NewError(fiber.StatusNotFound, "Injection not found")
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Errorf("Failed get injection by id: %+v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !isInjectionTransaction(current.TransactionType) {
|
||||||
|
return nil, fiber.NewError(fiber.StatusNotFound, "Injection not found")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.BankId != nil {
|
||||||
|
if err := s.ensureBankExists(c.Context(), req.BankId); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
updateBody["bank_id"] = *req.BankId
|
||||||
|
}
|
||||||
|
if req.AdjustmentDate != nil {
|
||||||
|
parsedDate, err := utils.ParseDateString(*req.AdjustmentDate)
|
||||||
|
if err != nil {
|
||||||
|
return nil, utils.BadRequest(err.Error())
|
||||||
|
}
|
||||||
|
updateBody["payment_date"] = parsedDate
|
||||||
|
}
|
||||||
|
if req.Nominal != nil {
|
||||||
|
updateBody["nominal"] = *req.Nominal
|
||||||
|
}
|
||||||
|
if req.Notes != nil {
|
||||||
|
updateBody["notes"] = *req.Notes
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(updateBody) == 0 {
|
||||||
|
return s.GetOne(c, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.Repository.PatchOne(c.Context(), id, updateBody, nil); err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, fiber.NewError(fiber.StatusNotFound, "Injection not found")
|
||||||
|
}
|
||||||
|
s.Log.Errorf("Failed to update injection: %+v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.GetOne(c, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func isInjectionTransaction(transactionType string) bool {
|
||||||
|
return strings.EqualFold(transactionType, string(utils.TransactionTypeInjection))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s injectionService) generateInjectionCode(ctx context.Context) (string, error) {
|
||||||
|
sequence, err := s.Repository.NextPaymentSequence(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("INJ-%05d", sequence), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s injectionService) approvalQueryModifier() func(*gorm.DB) *gorm.DB {
|
||||||
|
return func(db *gorm.DB) *gorm.DB {
|
||||||
|
return db.Preload("ActionUser")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s injectionService) ensureBankExists(ctx context.Context, bankId *uint) error {
|
||||||
|
return commonSvc.EnsureRelations(ctx,
|
||||||
|
commonSvc.RelationCheck{Name: "Bank", ID: bankId, Exists: s.Repository.BankExists},
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package validation
|
||||||
|
|
||||||
|
type Create struct {
|
||||||
|
BankId *uint `json:"bank_id" validate:"required_strict,number,gt=0"`
|
||||||
|
AdjustmentDate string `json:"adjustment_date" validate:"required_strict"`
|
||||||
|
Nominal float64 `json:"nominal" validate:"required_strict,gt=0"`
|
||||||
|
Notes string `json:"notes" validate:"required_strict,max=500"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Update struct {
|
||||||
|
BankId *uint `json:"bank_id,omitempty" validate:"omitempty,number,gt=0"`
|
||||||
|
AdjustmentDate *string `json:"adjustment_date,omitempty" validate:"omitempty"`
|
||||||
|
Nominal *float64 `json:"nominal,omitempty" validate:"omitempty,gt=0"`
|
||||||
|
Notes *string `json:"notes,omitempty" validate:"omitempty,max=500"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Query struct {
|
||||||
|
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
||||||
|
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
|
||||||
|
Search string `query:"search" validate:"omitempty,max=50"`
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package finance
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/go-playground/validator/v10"
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type FinanceModule struct{}
|
||||||
|
|
||||||
|
func (FinanceModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
|
||||||
|
RegisterRoutes(router, db, validate)
|
||||||
|
}
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
package controller
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/modules/finance/payments/dto"
|
||||||
|
service "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/payments/services"
|
||||||
|
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/payments/validations"
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/response"
|
||||||
|
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PaymentController struct {
|
||||||
|
PaymentService service.PaymentService
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPaymentController(paymentService service.PaymentService) *PaymentController {
|
||||||
|
return &PaymentController{
|
||||||
|
PaymentService: paymentService,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *PaymentController) GetOne(c *fiber.Ctx) error {
|
||||||
|
param := c.Params("id")
|
||||||
|
|
||||||
|
id, err := strconv.Atoi(param)
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := u.PaymentService.GetOne(c, uint(id))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusOK).
|
||||||
|
JSON(response.Success{
|
||||||
|
Code: fiber.StatusOK,
|
||||||
|
Status: "success",
|
||||||
|
Message: "Get payment successfully",
|
||||||
|
Data: dto.ToPaymentListDTO(*result),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *PaymentController) CreateOne(c *fiber.Ctx) error {
|
||||||
|
req := new(validation.Create)
|
||||||
|
|
||||||
|
if err := c.BodyParser(req); err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := u.PaymentService.CreateOne(c, req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusCreated).
|
||||||
|
JSON(response.Success{
|
||||||
|
Code: fiber.StatusCreated,
|
||||||
|
Status: "success",
|
||||||
|
Message: "Create payment successfully",
|
||||||
|
Data: dto.ToPaymentListDTO(*result),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *PaymentController) UpdateOne(c *fiber.Ctx) error {
|
||||||
|
req := new(validation.Update)
|
||||||
|
param := c.Params("id")
|
||||||
|
|
||||||
|
id, err := strconv.Atoi(param)
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := c.BodyParser(req); err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := u.PaymentService.UpdateOne(c, req, uint(id))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusOK).
|
||||||
|
JSON(response.Success{
|
||||||
|
Code: fiber.StatusOK,
|
||||||
|
Status: "success",
|
||||||
|
Message: "Update payment successfully",
|
||||||
|
Data: dto.ToPaymentListDTO(*result),
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,189 @@
|
|||||||
|
package dto
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
|
approvalDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/approvals/dto"
|
||||||
|
bankDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/banks/dto"
|
||||||
|
userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
// === DTO Structs ===
|
||||||
|
|
||||||
|
type PaymentRelationDTO struct {
|
||||||
|
Id uint `json:"id"`
|
||||||
|
PaymentCode string `json:"payment_code"`
|
||||||
|
ReferenceNumber *string `json:"reference_number,omitempty"`
|
||||||
|
TransactionType string `json:"transaction_type"`
|
||||||
|
Party Party `json:"party"`
|
||||||
|
PaymentDate time.Time `json:"payment_date"`
|
||||||
|
PaymentMethod string `json:"payment_method"`
|
||||||
|
Bank bankDTO.BankRelationDTO `json:"bank,omitempty"`
|
||||||
|
ExpenseAmount float64 `json:"expense_amount"`
|
||||||
|
IncomeAmount float64 `json:"income_amount"`
|
||||||
|
Nominal float64 `json:"nominal"`
|
||||||
|
Notes string `json:"notes"`
|
||||||
|
CreatedUser userDTO.UserRelationDTO `json:"created_user,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type PaymentListDTO struct {
|
||||||
|
Id uint `json:"id"`
|
||||||
|
PaymentCode string `json:"payment_code"`
|
||||||
|
ReferenceNumber *string `json:"reference_number"`
|
||||||
|
TransactionType string `json:"transaction_type"`
|
||||||
|
Party Party `json:"party"`
|
||||||
|
PaymentDate time.Time `json:"payment_date"`
|
||||||
|
PaymentMethod string `json:"payment_method"`
|
||||||
|
Bank bankDTO.BankRelationDTO `json:"bank"`
|
||||||
|
ExpenseAmount float64 `json:"expense_amount"`
|
||||||
|
IncomeAmount float64 `json:"income_amount"`
|
||||||
|
Nominal float64 `json:"nominal"`
|
||||||
|
Notes string `json:"notes"`
|
||||||
|
CreatedUser userDTO.UserRelationDTO `json:"created_user"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
|
Approval approvalDTO.ApprovalRelationDTO `json:"approval"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type PaymentDetailDTO struct {
|
||||||
|
PaymentListDTO
|
||||||
|
}
|
||||||
|
|
||||||
|
type Party struct {
|
||||||
|
Id uint `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
AccountNumber string `json:"account_number"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// === Mapper Functions ===
|
||||||
|
|
||||||
|
func ToPaymentRelationDTO(e entity.Payment) PaymentRelationDTO {
|
||||||
|
expenseAmount, incomeAmount := paymentAmounts(e.Direction, e.Nominal)
|
||||||
|
|
||||||
|
return PaymentRelationDTO{
|
||||||
|
Id: e.Id,
|
||||||
|
PaymentCode: paymentCodeFromPayment(e),
|
||||||
|
ReferenceNumber: e.ReferenceNumber,
|
||||||
|
TransactionType: transactionTypeFromPayment(e),
|
||||||
|
Party: partyFromPayment(e),
|
||||||
|
PaymentDate: e.PaymentDate,
|
||||||
|
PaymentMethod: e.PaymentMethod,
|
||||||
|
Bank: bankFromPayment(e),
|
||||||
|
ExpenseAmount: expenseAmount,
|
||||||
|
IncomeAmount: incomeAmount,
|
||||||
|
Nominal: e.Nominal,
|
||||||
|
Notes: e.Notes,
|
||||||
|
CreatedUser: userFromPayment(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToPaymentListDTO(e entity.Payment) PaymentListDTO {
|
||||||
|
expenseAmount, incomeAmount := paymentAmounts(e.Direction, e.Nominal)
|
||||||
|
approval := approvalDTO.ApprovalRelationDTO{}
|
||||||
|
if e.LatestApproval != nil {
|
||||||
|
approval = approvalDTO.ToApprovalDTO(*e.LatestApproval)
|
||||||
|
}
|
||||||
|
|
||||||
|
return PaymentListDTO{
|
||||||
|
Id: e.Id,
|
||||||
|
PaymentCode: paymentCodeFromPayment(e),
|
||||||
|
ReferenceNumber: e.ReferenceNumber,
|
||||||
|
TransactionType: transactionTypeFromPayment(e),
|
||||||
|
Party: partyFromPayment(e),
|
||||||
|
PaymentDate: e.PaymentDate,
|
||||||
|
PaymentMethod: e.PaymentMethod,
|
||||||
|
Bank: bankFromPayment(e),
|
||||||
|
ExpenseAmount: expenseAmount,
|
||||||
|
IncomeAmount: incomeAmount,
|
||||||
|
Nominal: e.Nominal,
|
||||||
|
Notes: e.Notes,
|
||||||
|
CreatedUser: userFromPayment(e),
|
||||||
|
CreatedAt: e.CreatedAt,
|
||||||
|
UpdatedAt: e.UpdatedAt,
|
||||||
|
Approval: approval,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToPaymentListDTOs(e []entity.Payment) []PaymentListDTO {
|
||||||
|
result := make([]PaymentListDTO, len(e))
|
||||||
|
for i, r := range e {
|
||||||
|
result[i] = ToPaymentListDTO(r)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToPaymentDetailDTO(e entity.Payment) PaymentDetailDTO {
|
||||||
|
return PaymentDetailDTO{
|
||||||
|
PaymentListDTO: ToPaymentListDTO(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func partyFromPayment(e entity.Payment) Party {
|
||||||
|
party := Party{
|
||||||
|
Id: e.PartyId,
|
||||||
|
Type: e.PartyType,
|
||||||
|
}
|
||||||
|
|
||||||
|
switch utils.PaymentParty(e.PartyType) {
|
||||||
|
case utils.PaymentPartyCustomer:
|
||||||
|
if e.Customer != nil && e.Customer.Id != 0 {
|
||||||
|
party.Name = e.Customer.Name
|
||||||
|
party.AccountNumber = e.Customer.AccountNumber
|
||||||
|
}
|
||||||
|
case utils.PaymentPartySupplier:
|
||||||
|
if e.Supplier != nil && e.Supplier.Id != 0 {
|
||||||
|
party.Name = e.Supplier.Name
|
||||||
|
if e.Supplier.AccountNumber != nil {
|
||||||
|
party.AccountNumber = *e.Supplier.AccountNumber
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return party
|
||||||
|
}
|
||||||
|
|
||||||
|
func bankFromPayment(e entity.Payment) bankDTO.BankRelationDTO {
|
||||||
|
if e.BankWarehouse.Id == 0 {
|
||||||
|
return bankDTO.BankRelationDTO{}
|
||||||
|
}
|
||||||
|
return bankDTO.ToBankRelationDTO(e.BankWarehouse)
|
||||||
|
}
|
||||||
|
|
||||||
|
func userFromPayment(e entity.Payment) userDTO.UserRelationDTO {
|
||||||
|
if e.CreatedUser.Id == 0 {
|
||||||
|
return userDTO.UserRelationDTO{}
|
||||||
|
}
|
||||||
|
return userDTO.ToUserRelationDTO(e.CreatedUser)
|
||||||
|
}
|
||||||
|
|
||||||
|
func paymentCodeFromPayment(e entity.Payment) string {
|
||||||
|
if e.PaymentCode != "" {
|
||||||
|
return e.PaymentCode
|
||||||
|
}
|
||||||
|
if e.ReferenceNumber != nil {
|
||||||
|
return *e.ReferenceNumber
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func transactionTypeFromPayment(e entity.Payment) string {
|
||||||
|
if e.TransactionType != "" {
|
||||||
|
return e.TransactionType
|
||||||
|
}
|
||||||
|
return e.Direction
|
||||||
|
}
|
||||||
|
|
||||||
|
func paymentAmounts(direction string, nominal float64) (float64, float64) {
|
||||||
|
switch strings.ToUpper(direction) {
|
||||||
|
case "IN":
|
||||||
|
return 0, nominal
|
||||||
|
case "OUT":
|
||||||
|
return nominal, 0
|
||||||
|
default:
|
||||||
|
return 0, 0
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package payments
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/go-playground/validator/v10"
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||||
|
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
|
||||||
|
rPayment "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/payments/repositories"
|
||||||
|
sPayment "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/payments/services"
|
||||||
|
utils "gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
|
|
||||||
|
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
|
||||||
|
sUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PaymentModule struct{}
|
||||||
|
|
||||||
|
func (PaymentModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
|
||||||
|
paymentRepo := rPayment.NewPaymentRepository(db)
|
||||||
|
userRepo := rUser.NewUserRepository(db)
|
||||||
|
|
||||||
|
approvalRepo := commonRepo.NewApprovalRepository(db)
|
||||||
|
approvalService := commonSvc.NewApprovalService(approvalRepo)
|
||||||
|
if err := approvalService.RegisterWorkflowSteps(utils.ApprovalWorkflowPayment, utils.PaymentApprovalSteps); err != nil {
|
||||||
|
panic(fmt.Sprintf("failed to register payment approval workflow: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
paymentService := sPayment.NewPaymentService(paymentRepo, approvalService, validate)
|
||||||
|
userService := sUser.NewUserService(userRepo, validate)
|
||||||
|
|
||||||
|
PaymentRoutes(router, userService, paymentService)
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||||
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PaymentRepository interface {
|
||||||
|
repository.BaseRepository[entity.Payment]
|
||||||
|
BankExists(ctx context.Context, bankId uint) (bool, error)
|
||||||
|
CustomerExists(ctx context.Context, customerId uint) (bool, error)
|
||||||
|
SupplierExists(ctx context.Context, supplierId uint) (bool, error)
|
||||||
|
SupplierCategory(ctx context.Context, supplierId uint) (string, error)
|
||||||
|
NextPaymentSequence(ctx context.Context) (int64, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type PaymentRepositoryImpl struct {
|
||||||
|
*repository.BaseRepositoryImpl[entity.Payment]
|
||||||
|
db *gorm.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPaymentRepository(db *gorm.DB) PaymentRepository {
|
||||||
|
return &PaymentRepositoryImpl{
|
||||||
|
BaseRepositoryImpl: repository.NewBaseRepository[entity.Payment](db),
|
||||||
|
db: db,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *PaymentRepositoryImpl) BankExists(ctx context.Context, bankId uint) (bool, error) {
|
||||||
|
return repository.Exists[entity.Bank](ctx, r.db, bankId)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *PaymentRepositoryImpl) CustomerExists(ctx context.Context, customerId uint) (bool, error) {
|
||||||
|
return repository.Exists[entity.Customer](ctx, r.db, customerId)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *PaymentRepositoryImpl) SupplierExists(ctx context.Context, supplierId uint) (bool, error) {
|
||||||
|
return repository.Exists[entity.Supplier](ctx, r.db, supplierId)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *PaymentRepositoryImpl) SupplierCategory(ctx context.Context, supplierId uint) (string, error) {
|
||||||
|
var supplier entity.Supplier
|
||||||
|
if err := r.db.WithContext(ctx).
|
||||||
|
Select("id", "category").
|
||||||
|
First(&supplier, supplierId).Error; err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return supplier.Category, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *PaymentRepositoryImpl) NextPaymentSequence(ctx context.Context) (int64, error) {
|
||||||
|
var next int64
|
||||||
|
if err := r.db.WithContext(ctx).
|
||||||
|
Raw("SELECT nextval('payments_code_seq')").
|
||||||
|
Scan(&next).Error; err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return next, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package payments
|
||||||
|
|
||||||
|
import (
|
||||||
|
// m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
||||||
|
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/payments/controllers"
|
||||||
|
payment "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/payments/services"
|
||||||
|
user "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
||||||
|
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func PaymentRoutes(v1 fiber.Router, u user.UserService, s payment.PaymentService) {
|
||||||
|
ctrl := controller.NewPaymentController(s)
|
||||||
|
|
||||||
|
route := v1.Group("/payments")
|
||||||
|
// route.Use(m.Auth(u))
|
||||||
|
|
||||||
|
route.Post("/", ctrl.CreateOne)
|
||||||
|
route.Get("/:id", ctrl.GetOne)
|
||||||
|
route.Patch("/:id", ctrl.UpdateOne)
|
||||||
|
}
|
||||||
@@ -0,0 +1,362 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||||
|
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||||
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
|
m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
||||||
|
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/payments/repositories"
|
||||||
|
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/payments/validations"
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
|
approvalutils "gitlab.com/mbugroup/lti-api.git/internal/utils/approvals"
|
||||||
|
|
||||||
|
"github.com/go-playground/validator/v10"
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PaymentService interface {
|
||||||
|
GetOne(ctx *fiber.Ctx, id uint) (*entity.Payment, error)
|
||||||
|
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.Payment, error)
|
||||||
|
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.Payment, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type paymentService struct {
|
||||||
|
Log *logrus.Logger
|
||||||
|
Validate *validator.Validate
|
||||||
|
Repository repository.PaymentRepository
|
||||||
|
ApprovalSvc commonSvc.ApprovalService
|
||||||
|
approvalWorkflow approvalutils.ApprovalWorkflowKey
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPaymentService(
|
||||||
|
repo repository.PaymentRepository,
|
||||||
|
approvalSvc commonSvc.ApprovalService,
|
||||||
|
validate *validator.Validate,
|
||||||
|
) PaymentService {
|
||||||
|
return &paymentService{
|
||||||
|
Log: utils.Log,
|
||||||
|
Validate: validate,
|
||||||
|
Repository: repo,
|
||||||
|
ApprovalSvc: approvalSvc,
|
||||||
|
approvalWorkflow: utils.ApprovalWorkflowPayment,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s paymentService) withRelations(db *gorm.DB) *gorm.DB {
|
||||||
|
return db.Preload("CreatedUser").
|
||||||
|
Preload("BankWarehouse").
|
||||||
|
Preload("Customer").
|
||||||
|
Preload("Supplier")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s paymentService) GetOne(c *fiber.Ctx, id uint) (*entity.Payment, error) {
|
||||||
|
payment, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, fiber.NewError(fiber.StatusNotFound, "Payment not found")
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Errorf("Failed get payment by id: %+v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if s.ApprovalSvc != nil {
|
||||||
|
approval, err := s.ApprovalSvc.LatestByTarget(c.Context(), s.approvalWorkflow, id, s.approvalQueryModifier())
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Warnf("Unable to load latest approval for payment %d: %+v", id, err)
|
||||||
|
} else {
|
||||||
|
payment.LatestApproval = approval
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return payment, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *paymentService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.Payment, error) {
|
||||||
|
if err := s.Validate.Struct(req); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
//! CHECK PARTY TYPE
|
||||||
|
party, err := normalizePartyType(req.PartyType)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
//! CHECK EXISTS
|
||||||
|
if err := s.ensurePartyExists(c.Context(), party, req.PartyId); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := s.ensureBankExists(c.Context(), req.BankId); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
//? NORMALIZE
|
||||||
|
paymentDate, err := utils.ParseDateString(req.PaymentDate)
|
||||||
|
if err != nil {
|
||||||
|
return nil, utils.BadRequest(err.Error())
|
||||||
|
}
|
||||||
|
method, err := normalizePaymentMethod(req.PaymentMethod)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
transactionType, err := s.resolveTransactionType(c.Context(), party, req.PartyId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
//? GET CREATED BY
|
||||||
|
actorID, err := m.ActorIDFromContext(c)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
code, err := s.generatePaymentCode(c.Context(), party)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
createBody := &entity.Payment{
|
||||||
|
PaymentCode: code,
|
||||||
|
ReferenceNumber: req.ReferenceNumber,
|
||||||
|
TransactionType: transactionType,
|
||||||
|
PartyType: party,
|
||||||
|
PartyId: req.PartyId,
|
||||||
|
PaymentDate: paymentDate,
|
||||||
|
PaymentMethod: method,
|
||||||
|
BankId: req.BankId,
|
||||||
|
Direction: directionForParty(party),
|
||||||
|
Nominal: req.Nominal,
|
||||||
|
Notes: req.Notes,
|
||||||
|
CreatedBy: actorID,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
||||||
|
paymentRepoTx := repository.NewPaymentRepository(dbTransaction)
|
||||||
|
if err := paymentRepoTx.CreateOne(c.Context(), createBody, nil); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if s.ApprovalSvc != nil {
|
||||||
|
action := entity.ApprovalActionCreated
|
||||||
|
approvalSvcTx := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(dbTransaction))
|
||||||
|
_, err := approvalSvcTx.CreateApproval(
|
||||||
|
c.Context(),
|
||||||
|
s.approvalWorkflow,
|
||||||
|
createBody.Id,
|
||||||
|
utils.PaymentStepPengajuan,
|
||||||
|
&action,
|
||||||
|
actorID,
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Errorf("Failed to create payment: %+v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.GetOne(c, createBody.Id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s paymentService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.Payment, error) {
|
||||||
|
if err := s.Validate.Struct(req); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
updateBody := make(map[string]any)
|
||||||
|
|
||||||
|
if req.PaymentDate != nil {
|
||||||
|
parsedDate, err := utils.ParseDateString(*req.PaymentDate)
|
||||||
|
if err != nil {
|
||||||
|
return nil, utils.BadRequest(err.Error())
|
||||||
|
}
|
||||||
|
updateBody["payment_date"] = parsedDate
|
||||||
|
}
|
||||||
|
if req.Nominal != nil {
|
||||||
|
updateBody["nominal"] = *req.Nominal
|
||||||
|
}
|
||||||
|
if req.ReferenceNumber != nil {
|
||||||
|
updateBody["reference_number"] = *req.ReferenceNumber
|
||||||
|
}
|
||||||
|
if req.PaymentMethod != nil {
|
||||||
|
method, err := normalizePaymentMethod(*req.PaymentMethod)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
updateBody["payment_method"] = method
|
||||||
|
}
|
||||||
|
if req.BankId != nil {
|
||||||
|
if err := s.ensureBankExists(c.Context(), req.BankId); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
updateBody["bank_id"] = *req.BankId
|
||||||
|
}
|
||||||
|
if req.Notes != nil {
|
||||||
|
updateBody["notes"] = *req.Notes
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.PartyType != nil || req.PartyId != nil {
|
||||||
|
existing, err := s.Repository.GetByID(c.Context(), id, nil)
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, fiber.NewError(fiber.StatusNotFound, "Payment not found")
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Errorf("Failed get payment by id: %+v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
partyType := existing.PartyType
|
||||||
|
partyId := existing.PartyId
|
||||||
|
|
||||||
|
if req.PartyType != nil {
|
||||||
|
normalized, err := normalizePartyType(*req.PartyType)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
partyType = normalized
|
||||||
|
updateBody["party_type"] = partyType
|
||||||
|
updateBody["direction"] = directionForParty(partyType)
|
||||||
|
}
|
||||||
|
if req.PartyId != nil {
|
||||||
|
partyId = *req.PartyId
|
||||||
|
updateBody["party_id"] = partyId
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.ensurePartyExists(c.Context(), partyType, partyId); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
transactionType, err := s.resolveTransactionType(c.Context(), partyType, partyId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
updateBody["transaction_type"] = transactionType
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(updateBody) == 0 {
|
||||||
|
return s.GetOne(c, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.Repository.PatchOne(c.Context(), id, updateBody, nil); err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, fiber.NewError(fiber.StatusNotFound, "Payment not found")
|
||||||
|
}
|
||||||
|
s.Log.Errorf("Failed to update payment: %+v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.GetOne(c, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizePartyType(partyType string) (string, error) {
|
||||||
|
party := strings.ToUpper(strings.TrimSpace(partyType))
|
||||||
|
if !utils.IsValidPaymentParty(party) {
|
||||||
|
return "", utils.BadRequest("`party_type` must be `customer` or `supplier`")
|
||||||
|
}
|
||||||
|
return party, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizePaymentMethod(method string) (string, error) {
|
||||||
|
normalized := strings.ToUpper(strings.TrimSpace(method))
|
||||||
|
if !utils.IsValidPaymentMethod(normalized) {
|
||||||
|
return "", utils.BadRequest("Invalid payment_method")
|
||||||
|
}
|
||||||
|
return normalized, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func directionForParty(partyType string) string {
|
||||||
|
if utils.PaymentParty(partyType) == utils.PaymentPartyCustomer {
|
||||||
|
return "IN"
|
||||||
|
}
|
||||||
|
return "OUT"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s paymentService) resolveTransactionType(ctx context.Context, partyType string, partyId uint) (string, error) {
|
||||||
|
switch utils.PaymentParty(partyType) {
|
||||||
|
case utils.PaymentPartyCustomer:
|
||||||
|
return string(utils.TransactionTypePenjualan), nil
|
||||||
|
case utils.PaymentPartySupplier:
|
||||||
|
category, err := s.getSupplierCategory(ctx, partyId)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if isSupplierCategoryBiaya(category) {
|
||||||
|
return string(utils.TransactionTypeBiaya), nil
|
||||||
|
}
|
||||||
|
return string(utils.TransactionTypePembelian), nil
|
||||||
|
default:
|
||||||
|
return "", utils.BadRequest("`party_type` must be `customer` or `supplier`")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s paymentService) generatePaymentCode(ctx context.Context, partyType string) (string, error) {
|
||||||
|
prefix := "PAY"
|
||||||
|
switch utils.PaymentParty(partyType) {
|
||||||
|
case utils.PaymentPartyCustomer:
|
||||||
|
prefix = "PAY-IN"
|
||||||
|
case utils.PaymentPartySupplier:
|
||||||
|
prefix = "PAY-OUT"
|
||||||
|
}
|
||||||
|
sequence, err := s.Repository.NextPaymentSequence(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%s-%05d", prefix, sequence), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s paymentService) approvalQueryModifier() func(*gorm.DB) *gorm.DB {
|
||||||
|
return func(db *gorm.DB) *gorm.DB {
|
||||||
|
return db.Preload("ActionUser")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s paymentService) ensurePartyExists(ctx context.Context, partyType string, partyId uint) error {
|
||||||
|
switch utils.PaymentParty(partyType) {
|
||||||
|
case utils.PaymentPartyCustomer:
|
||||||
|
return commonSvc.EnsureRelations(ctx,
|
||||||
|
commonSvc.RelationCheck{Name: "Customer", ID: &partyId, Exists: s.Repository.CustomerExists},
|
||||||
|
)
|
||||||
|
case utils.PaymentPartySupplier:
|
||||||
|
return commonSvc.EnsureRelations(ctx,
|
||||||
|
commonSvc.RelationCheck{Name: "Supplier", ID: &partyId, Exists: s.Repository.SupplierExists},
|
||||||
|
)
|
||||||
|
default:
|
||||||
|
return utils.BadRequest("`party_type` must be `customer` or `supplier`")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s paymentService) ensureBankExists(ctx context.Context, bankId *uint) error {
|
||||||
|
return commonSvc.EnsureRelations(ctx,
|
||||||
|
commonSvc.RelationCheck{Name: "Bank", ID: bankId, Exists: s.Repository.BankExists},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s paymentService) getSupplierCategory(ctx context.Context, supplierId uint) (string, error) {
|
||||||
|
category, err := s.Repository.SupplierCategory(ctx, supplierId)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return "", utils.NotFound("Supplier not found")
|
||||||
|
}
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return strings.ToUpper(strings.TrimSpace(category)), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func isSupplierCategoryBiaya(category string) bool {
|
||||||
|
switch strings.ToUpper(strings.TrimSpace(category)) {
|
||||||
|
case string(utils.SupplierCategoryBOP), "BIAYA":
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package validation
|
||||||
|
|
||||||
|
type Create struct {
|
||||||
|
PartyType string `json:"party_type" validate:"required_strict,min=1,max=50"`
|
||||||
|
PartyId uint `json:"party_id" validate:"required_strict,number,gt=0"`
|
||||||
|
PaymentDate string `json:"payment_date" validate:"required_strict,datetime=2006-01-02"`
|
||||||
|
Nominal float64 `json:"nominal" validate:"required_strict"`
|
||||||
|
ReferenceNumber *string `json:"reference_number,omitempty"`
|
||||||
|
PaymentMethod string `json:"payment_method" validate:"required_strict,max=20"`
|
||||||
|
BankId *uint `json:"bank_id" validate:"omitempty,number,gt=0"`
|
||||||
|
Notes string `json:"notes" validate:"required_strict,max=500"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Update struct {
|
||||||
|
PartyType *string `json:"party_type,omitempty" validate:"omitempty,max=50"`
|
||||||
|
PartyId *uint `json:"party_id,omitempty" validate:"omitempty,number,gt=0"`
|
||||||
|
PaymentDate *string `json:"payment_date,omitempty" validate:"omitempty,datetime=2006-01-02"`
|
||||||
|
Nominal *float64 `json:"nominal,omitempty" validate:"omitempty,gt=0"`
|
||||||
|
ReferenceNumber *string `json:"reference_number,omitempty"`
|
||||||
|
PaymentMethod *string `json:"payment_method,omitempty" validate:"omitempty,max=20"`
|
||||||
|
BankId *uint `json:"bank_id,omitempty" validate:"omitempty,number,gt=0"`
|
||||||
|
Notes *string `json:"notes,omitempty" validate:"omitempty,max=500"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Query struct {
|
||||||
|
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
||||||
|
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
|
||||||
|
Search string `query:"search" validate:"omitempty,max=50"`
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package finance
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/modules"
|
||||||
|
|
||||||
|
"github.com/go-playground/validator/v10"
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
|
||||||
|
payments "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/payments"
|
||||||
|
initials "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/initials"
|
||||||
|
injections "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/injections"
|
||||||
|
transactions "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/transactions"
|
||||||
|
// MODULE IMPORTS
|
||||||
|
)
|
||||||
|
|
||||||
|
func RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
|
||||||
|
group := router.Group("/finance")
|
||||||
|
|
||||||
|
allModules := []modules.Module{
|
||||||
|
payments.PaymentModule{},
|
||||||
|
initials.InitialModule{},
|
||||||
|
injections.InjectionModule{},
|
||||||
|
transactions.TransactionModule{},
|
||||||
|
// MODULE REGISTRY
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, m := range allModules {
|
||||||
|
m.RegisterRoutes(group, db, validate)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,96 @@
|
|||||||
|
package controller
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/modules/finance/transactions/dto"
|
||||||
|
service "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/transactions/services"
|
||||||
|
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/transactions/validations"
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/response"
|
||||||
|
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TransactionController struct {
|
||||||
|
TransactionService service.TransactionService
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTransactionController(transactionService service.TransactionService) *TransactionController {
|
||||||
|
return &TransactionController{
|
||||||
|
TransactionService: transactionService,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *TransactionController) GetAll(c *fiber.Ctx) error {
|
||||||
|
query := &validation.Query{
|
||||||
|
Page: c.QueryInt("page", 1),
|
||||||
|
Limit: c.QueryInt("limit", 10),
|
||||||
|
Search: c.Query("search", ""),
|
||||||
|
}
|
||||||
|
|
||||||
|
if query.Page < 1 || query.Limit < 1 {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0")
|
||||||
|
}
|
||||||
|
|
||||||
|
result, totalResults, err := u.TransactionService.GetAll(c, query)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusOK).
|
||||||
|
JSON(response.SuccessWithPaginate[dto.TransactionListDTO]{
|
||||||
|
Code: fiber.StatusOK,
|
||||||
|
Status: "success",
|
||||||
|
Message: "Get all transactions successfully",
|
||||||
|
Meta: response.Meta{
|
||||||
|
Page: query.Page,
|
||||||
|
Limit: query.Limit,
|
||||||
|
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
|
||||||
|
TotalResults: totalResults,
|
||||||
|
},
|
||||||
|
Data: dto.ToTransactionListDTOs(result),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *TransactionController) GetOne(c *fiber.Ctx) error {
|
||||||
|
param := c.Params("id")
|
||||||
|
|
||||||
|
id, err := strconv.Atoi(param)
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := u.TransactionService.GetOne(c, uint(id))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusOK).
|
||||||
|
JSON(response.Success{
|
||||||
|
Code: fiber.StatusOK,
|
||||||
|
Status: "success",
|
||||||
|
Message: "Get transaction successfully",
|
||||||
|
Data: dto.ToTransactionListDTO(*result),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *TransactionController) DeleteOne(c *fiber.Ctx) error {
|
||||||
|
param := c.Params("id")
|
||||||
|
|
||||||
|
id, err := strconv.Atoi(param)
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := u.TransactionService.DeleteOne(c, uint(id)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusOK).
|
||||||
|
JSON(response.Common{
|
||||||
|
Code: fiber.StatusOK,
|
||||||
|
Status: "success",
|
||||||
|
Message: "Delete transaction successfully",
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,189 @@
|
|||||||
|
package dto
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
|
approvalDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/approvals/dto"
|
||||||
|
bankDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/banks/dto"
|
||||||
|
userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
// === DTO Structs ===
|
||||||
|
|
||||||
|
type TransactionRelationDTO struct {
|
||||||
|
Id uint `json:"id"`
|
||||||
|
PaymentCode string `json:"payment_code"`
|
||||||
|
ReferenceNumber *string `json:"reference_number,omitempty"`
|
||||||
|
TransactionType string `json:"transaction_type"`
|
||||||
|
Party Party `json:"party"`
|
||||||
|
PaymentDate time.Time `json:"payment_date"`
|
||||||
|
PaymentMethod string `json:"payment_method"`
|
||||||
|
Bank bankDTO.BankRelationDTO `json:"bank,omitempty"`
|
||||||
|
ExpenseAmount float64 `json:"expense_amount"`
|
||||||
|
IncomeAmount float64 `json:"income_amount"`
|
||||||
|
Nominal float64 `json:"nominal"`
|
||||||
|
Notes string `json:"notes"`
|
||||||
|
CreatedUser userDTO.UserRelationDTO `json:"created_user,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type TransactionListDTO struct {
|
||||||
|
Id uint `json:"id"`
|
||||||
|
PaymentCode string `json:"payment_code"`
|
||||||
|
ReferenceNumber *string `json:"reference_number"`
|
||||||
|
TransactionType string `json:"transaction_type"`
|
||||||
|
Party Party `json:"party"`
|
||||||
|
PaymentDate time.Time `json:"payment_date"`
|
||||||
|
PaymentMethod string `json:"payment_method"`
|
||||||
|
Bank bankDTO.BankRelationDTO `json:"bank"`
|
||||||
|
ExpenseAmount float64 `json:"expense_amount"`
|
||||||
|
IncomeAmount float64 `json:"income_amount"`
|
||||||
|
Nominal float64 `json:"nominal"`
|
||||||
|
Notes string `json:"notes"`
|
||||||
|
CreatedUser userDTO.UserRelationDTO `json:"created_user"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
|
Approval approvalDTO.ApprovalRelationDTO `json:"approval"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type TransactionDetailDTO struct {
|
||||||
|
TransactionListDTO
|
||||||
|
}
|
||||||
|
|
||||||
|
type Party struct {
|
||||||
|
Id uint `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
AccountNumber string `json:"account_number"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// === Mapper Functions ===
|
||||||
|
|
||||||
|
func ToTransactionRelationDTO(e entity.Payment) TransactionRelationDTO {
|
||||||
|
expenseAmount, incomeAmount := paymentAmounts(e.Direction, e.Nominal)
|
||||||
|
|
||||||
|
return TransactionRelationDTO{
|
||||||
|
Id: e.Id,
|
||||||
|
PaymentCode: paymentCodeFromPayment(e),
|
||||||
|
ReferenceNumber: e.ReferenceNumber,
|
||||||
|
TransactionType: transactionTypeFromPayment(e),
|
||||||
|
Party: partyFromPayment(e),
|
||||||
|
PaymentDate: e.PaymentDate,
|
||||||
|
PaymentMethod: e.PaymentMethod,
|
||||||
|
Bank: bankFromPayment(e),
|
||||||
|
ExpenseAmount: expenseAmount,
|
||||||
|
IncomeAmount: incomeAmount,
|
||||||
|
Nominal: e.Nominal,
|
||||||
|
Notes: e.Notes,
|
||||||
|
CreatedUser: userFromPayment(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToTransactionListDTO(e entity.Payment) TransactionListDTO {
|
||||||
|
expenseAmount, incomeAmount := paymentAmounts(e.Direction, e.Nominal)
|
||||||
|
approval := approvalDTO.ApprovalRelationDTO{}
|
||||||
|
if e.LatestApproval != nil {
|
||||||
|
approval = approvalDTO.ToApprovalDTO(*e.LatestApproval)
|
||||||
|
}
|
||||||
|
|
||||||
|
return TransactionListDTO{
|
||||||
|
Id: e.Id,
|
||||||
|
PaymentCode: paymentCodeFromPayment(e),
|
||||||
|
ReferenceNumber: e.ReferenceNumber,
|
||||||
|
TransactionType: transactionTypeFromPayment(e),
|
||||||
|
Party: partyFromPayment(e),
|
||||||
|
PaymentDate: e.PaymentDate,
|
||||||
|
PaymentMethod: e.PaymentMethod,
|
||||||
|
Bank: bankFromPayment(e),
|
||||||
|
ExpenseAmount: expenseAmount,
|
||||||
|
IncomeAmount: incomeAmount,
|
||||||
|
Nominal: e.Nominal,
|
||||||
|
Notes: e.Notes,
|
||||||
|
CreatedUser: userFromPayment(e),
|
||||||
|
CreatedAt: e.CreatedAt,
|
||||||
|
UpdatedAt: e.UpdatedAt,
|
||||||
|
Approval: approval,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToTransactionListDTOs(e []entity.Payment) []TransactionListDTO {
|
||||||
|
result := make([]TransactionListDTO, len(e))
|
||||||
|
for i, r := range e {
|
||||||
|
result[i] = ToTransactionListDTO(r)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToTransactionDetailDTO(e entity.Payment) TransactionDetailDTO {
|
||||||
|
return TransactionDetailDTO{
|
||||||
|
TransactionListDTO: ToTransactionListDTO(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func partyFromPayment(e entity.Payment) Party {
|
||||||
|
party := Party{
|
||||||
|
Id: e.PartyId,
|
||||||
|
Type: e.PartyType,
|
||||||
|
}
|
||||||
|
|
||||||
|
switch utils.PaymentParty(e.PartyType) {
|
||||||
|
case utils.PaymentPartyCustomer:
|
||||||
|
if e.Customer != nil && e.Customer.Id != 0 {
|
||||||
|
party.Name = e.Customer.Name
|
||||||
|
party.AccountNumber = e.Customer.AccountNumber
|
||||||
|
}
|
||||||
|
case utils.PaymentPartySupplier:
|
||||||
|
if e.Supplier != nil && e.Supplier.Id != 0 {
|
||||||
|
party.Name = e.Supplier.Name
|
||||||
|
if e.Supplier.AccountNumber != nil {
|
||||||
|
party.AccountNumber = *e.Supplier.AccountNumber
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return party
|
||||||
|
}
|
||||||
|
|
||||||
|
func bankFromPayment(e entity.Payment) bankDTO.BankRelationDTO {
|
||||||
|
if e.BankWarehouse.Id == 0 {
|
||||||
|
return bankDTO.BankRelationDTO{}
|
||||||
|
}
|
||||||
|
return bankDTO.ToBankRelationDTO(e.BankWarehouse)
|
||||||
|
}
|
||||||
|
|
||||||
|
func userFromPayment(e entity.Payment) userDTO.UserRelationDTO {
|
||||||
|
if e.CreatedUser.Id == 0 {
|
||||||
|
return userDTO.UserRelationDTO{}
|
||||||
|
}
|
||||||
|
return userDTO.ToUserRelationDTO(e.CreatedUser)
|
||||||
|
}
|
||||||
|
|
||||||
|
func paymentCodeFromPayment(e entity.Payment) string {
|
||||||
|
if e.PaymentCode != "" {
|
||||||
|
return e.PaymentCode
|
||||||
|
}
|
||||||
|
if e.ReferenceNumber != nil {
|
||||||
|
return *e.ReferenceNumber
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func transactionTypeFromPayment(e entity.Payment) string {
|
||||||
|
if e.TransactionType != "" {
|
||||||
|
return e.TransactionType
|
||||||
|
}
|
||||||
|
return e.Direction
|
||||||
|
}
|
||||||
|
|
||||||
|
func paymentAmounts(direction string, nominal float64) (float64, float64) {
|
||||||
|
switch strings.ToUpper(direction) {
|
||||||
|
case "IN":
|
||||||
|
return 0, nominal
|
||||||
|
case "OUT":
|
||||||
|
return nominal, 0
|
||||||
|
default:
|
||||||
|
return 0, 0
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package transactions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/go-playground/validator/v10"
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||||
|
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
|
||||||
|
rTransaction "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/transactions/repositories"
|
||||||
|
sTransaction "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/transactions/services"
|
||||||
|
|
||||||
|
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
|
||||||
|
sUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TransactionModule struct{}
|
||||||
|
|
||||||
|
func (TransactionModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
|
||||||
|
transactionRepo := rTransaction.NewTransactionRepository(db)
|
||||||
|
userRepo := rUser.NewUserRepository(db)
|
||||||
|
|
||||||
|
approvalRepo := commonRepo.NewApprovalRepository(db)
|
||||||
|
approvalService := commonSvc.NewApprovalService(approvalRepo)
|
||||||
|
if err := approvalService.RegisterWorkflowSteps(utils.ApprovalWorkflowPayment, utils.PaymentApprovalSteps); err != nil {
|
||||||
|
panic(fmt.Sprintf("failed to register payment approval workflow: %v", err))
|
||||||
|
}
|
||||||
|
if err := approvalService.RegisterWorkflowSteps(utils.ApprovalWorkflowInitial, utils.InitialApprovalSteps); err != nil {
|
||||||
|
panic(fmt.Sprintf("failed to register initial approval workflow: %v", err))
|
||||||
|
}
|
||||||
|
if err := approvalService.RegisterWorkflowSteps(utils.ApprovalWorkflowInjection, utils.InjectionApprovalSteps); err != nil {
|
||||||
|
panic(fmt.Sprintf("failed to register injection approval workflow: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
transactionService := sTransaction.NewTransactionService(transactionRepo, approvalService, validate)
|
||||||
|
userService := sUser.NewUserService(userRepo, validate)
|
||||||
|
|
||||||
|
TransactionRoutes(router, userService, transactionService)
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||||
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TransactionRepository interface {
|
||||||
|
repository.BaseRepository[entity.Payment]
|
||||||
|
}
|
||||||
|
|
||||||
|
type TransactionRepositoryImpl struct {
|
||||||
|
*repository.BaseRepositoryImpl[entity.Payment]
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTransactionRepository(db *gorm.DB) TransactionRepository {
|
||||||
|
return &TransactionRepositoryImpl{
|
||||||
|
BaseRepositoryImpl: repository.NewBaseRepository[entity.Payment](db),
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package transactions
|
||||||
|
|
||||||
|
import (
|
||||||
|
// m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
||||||
|
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/transactions/controllers"
|
||||||
|
transaction "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/transactions/services"
|
||||||
|
user "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
||||||
|
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TransactionRoutes(v1 fiber.Router, u user.UserService, s transaction.TransactionService) {
|
||||||
|
ctrl := controller.NewTransactionController(s)
|
||||||
|
|
||||||
|
route := v1.Group("/transactions")
|
||||||
|
// route.Use(m.Auth(u))
|
||||||
|
|
||||||
|
route.Get("/", ctrl.GetAll)
|
||||||
|
route.Get("/:id", ctrl.GetOne)
|
||||||
|
route.Delete("/:id", ctrl.DeleteOne)
|
||||||
|
}
|
||||||
@@ -0,0 +1,175 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||||
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
|
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/transactions/repositories"
|
||||||
|
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/transactions/validations"
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
|
approvalutils "gitlab.com/mbugroup/lti-api.git/internal/utils/approvals"
|
||||||
|
|
||||||
|
"github.com/go-playground/validator/v10"
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TransactionService interface {
|
||||||
|
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.Payment, int64, error)
|
||||||
|
GetOne(ctx *fiber.Ctx, id uint) (*entity.Payment, error)
|
||||||
|
DeleteOne(ctx *fiber.Ctx, id uint) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type transactionService struct {
|
||||||
|
Log *logrus.Logger
|
||||||
|
Validate *validator.Validate
|
||||||
|
Repository repository.TransactionRepository
|
||||||
|
ApprovalSvc commonSvc.ApprovalService
|
||||||
|
approvalWorkflows map[string]approvalutils.ApprovalWorkflowKey
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTransactionService(
|
||||||
|
repo repository.TransactionRepository,
|
||||||
|
approvalSvc commonSvc.ApprovalService,
|
||||||
|
validate *validator.Validate,
|
||||||
|
) TransactionService {
|
||||||
|
return &transactionService{
|
||||||
|
Log: utils.Log,
|
||||||
|
Validate: validate,
|
||||||
|
Repository: repo,
|
||||||
|
ApprovalSvc: approvalSvc,
|
||||||
|
approvalWorkflows: map[string]approvalutils.ApprovalWorkflowKey{
|
||||||
|
string(utils.TransactionTypeSaldoAwal): utils.ApprovalWorkflowInitial,
|
||||||
|
string(utils.TransactionTypeInjection): utils.ApprovalWorkflowInjection,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s transactionService) withRelations(db *gorm.DB) *gorm.DB {
|
||||||
|
return db.Preload("CreatedUser").
|
||||||
|
Preload("BankWarehouse").
|
||||||
|
Preload("Customer").
|
||||||
|
Preload("Supplier")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s transactionService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.Payment, int64, error) {
|
||||||
|
if err := s.Validate.Struct(params); err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
offset := (params.Page - 1) * params.Limit
|
||||||
|
|
||||||
|
transactions, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
||||||
|
db = s.withRelations(db)
|
||||||
|
if params.Search != "" {
|
||||||
|
like := "%" + strings.ToLower(strings.TrimSpace(params.Search)) + "%"
|
||||||
|
return db.Where(
|
||||||
|
`LOWER(payment_code) LIKE ? OR
|
||||||
|
LOWER(COALESCE(reference_number, '')) LIKE ? OR
|
||||||
|
LOWER(COALESCE(transaction_type, '')) LIKE ? OR
|
||||||
|
LOWER(COALESCE(notes, '')) LIKE ?`,
|
||||||
|
like, like, like, like,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return db.Order("payment_date DESC").Order("created_at DESC")
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Errorf("Failed to get transactions: %+v", err)
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
s.attachApprovals(c.Context(), transactions)
|
||||||
|
return transactions, total, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s transactionService) GetOne(c *fiber.Ctx, id uint) (*entity.Payment, error) {
|
||||||
|
transaction, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, fiber.NewError(fiber.StatusNotFound, "Transaction not found")
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Errorf("Failed get transaction by id: %+v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if s.ApprovalSvc != nil {
|
||||||
|
approval, err := s.ApprovalSvc.LatestByTarget(
|
||||||
|
c.Context(),
|
||||||
|
s.workflowForTransaction(transaction),
|
||||||
|
id,
|
||||||
|
s.approvalQueryModifier(),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Warnf("Unable to load latest approval for transaction %d: %+v", id, err)
|
||||||
|
} else {
|
||||||
|
transaction.LatestApproval = approval
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return transaction, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s transactionService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||||
|
if err := s.Repository.DeleteOne(c.Context(), id); err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return fiber.NewError(fiber.StatusNotFound, "Transaction not found")
|
||||||
|
}
|
||||||
|
s.Log.Errorf("Failed to delete transaction: %+v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s transactionService) attachApprovals(ctx context.Context, transactions []entity.Payment) {
|
||||||
|
if s.ApprovalSvc == nil || len(transactions) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
workflowIDs := map[approvalutils.ApprovalWorkflowKey][]uint{}
|
||||||
|
for _, transaction := range transactions {
|
||||||
|
workflow := s.workflowForTransaction(&transaction)
|
||||||
|
workflowIDs[workflow] = append(workflowIDs[workflow], transaction.Id)
|
||||||
|
}
|
||||||
|
|
||||||
|
approvalByWorkflow := make(map[approvalutils.ApprovalWorkflowKey]map[uint]*entity.Approval, len(workflowIDs))
|
||||||
|
for workflow, ids := range workflowIDs {
|
||||||
|
if len(ids) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
approvals, err := s.ApprovalSvc.LatestByTargets(ctx, workflow, ids, s.approvalQueryModifier())
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Warnf("Unable to load latest approvals for transactions: %+v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
approvalByWorkflow[workflow] = approvals
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range transactions {
|
||||||
|
workflow := s.workflowForTransaction(&transactions[i])
|
||||||
|
if approvals, ok := approvalByWorkflow[workflow]; ok {
|
||||||
|
transactions[i].LatestApproval = approvals[transactions[i].Id]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s transactionService) workflowForTransaction(transaction *entity.Payment) approvalutils.ApprovalWorkflowKey {
|
||||||
|
if transaction == nil {
|
||||||
|
return utils.ApprovalWorkflowPayment
|
||||||
|
}
|
||||||
|
transactionType := strings.TrimSpace(strings.ToUpper(transaction.TransactionType))
|
||||||
|
if transactionType == "" {
|
||||||
|
return utils.ApprovalWorkflowPayment
|
||||||
|
}
|
||||||
|
if workflow, ok := s.approvalWorkflows[transactionType]; ok {
|
||||||
|
return workflow
|
||||||
|
}
|
||||||
|
return utils.ApprovalWorkflowPayment
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s transactionService) approvalQueryModifier() func(*gorm.DB) *gorm.DB {
|
||||||
|
return func(db *gorm.DB) *gorm.DB {
|
||||||
|
return db.Preload("ActionUser")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package validation
|
||||||
|
|
||||||
|
type Create struct {
|
||||||
|
Name string `json:"name" validate:"required_strict,min=3"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Update struct {
|
||||||
|
Name *string `json:"name,omitempty" validate:"omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Query struct {
|
||||||
|
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
||||||
|
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
|
||||||
|
Search string `query:"search" validate:"omitempty,max=50"`
|
||||||
|
}
|
||||||
@@ -33,10 +33,8 @@ type ProductWarehouseDTO struct {
|
|||||||
|
|
||||||
type AdjustmentRelationDTO struct {
|
type AdjustmentRelationDTO struct {
|
||||||
Id uint `json:"id"`
|
Id uint `json:"id"`
|
||||||
TransactionType string `json:"transaction_type"`
|
Increase float64 `json:"increase"`
|
||||||
Quantity float64 `json:"quantity"`
|
Decrease float64 `json:"decrease"`
|
||||||
BeforeQuantity float64 `json:"before_quantity"`
|
|
||||||
AfterQuantity float64 `json:"after_quantity"`
|
|
||||||
Note string `json:"note,omitempty"`
|
Note string `json:"note,omitempty"`
|
||||||
ProductWarehouseId uint `json:"product_warehouse_id"`
|
ProductWarehouseId uint `json:"product_warehouse_id"`
|
||||||
ProductWarehouse *ProductWarehouseDTO `json:"product_warehouse,omitempty"`
|
ProductWarehouse *ProductWarehouseDTO `json:"product_warehouse,omitempty"`
|
||||||
@@ -104,12 +102,10 @@ func ToProductWarehouseDTO(e *entity.ProductWarehouse) *ProductWarehouseDTO {
|
|||||||
|
|
||||||
func ToAdjustmentRelationDTO(e *entity.StockLog) AdjustmentRelationDTO {
|
func ToAdjustmentRelationDTO(e *entity.StockLog) AdjustmentRelationDTO {
|
||||||
return AdjustmentRelationDTO{
|
return AdjustmentRelationDTO{
|
||||||
Id: e.Id,
|
Id: e.Id,
|
||||||
// TransactionType: e.LoggableType,
|
|
||||||
// Quantity: e.Q,
|
|
||||||
// BeforeQuantity: e.BeforeQuantity,
|
|
||||||
// AfterQuantity: e.AfterQuantity,
|
|
||||||
Note: e.Notes,
|
Note: e.Notes,
|
||||||
|
Increase: e.Increase,
|
||||||
|
Decrease: e.Decrease,
|
||||||
ProductWarehouseId: e.ProductWarehouseId,
|
ProductWarehouseId: e.ProductWarehouseId,
|
||||||
ProductWarehouse: ToProductWarehouseDTO(e.ProductWarehouse),
|
ProductWarehouse: ToProductWarehouseDTO(e.ProductWarehouse),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ func AdjustmentRoutes(v1 fiber.Router, u user.UserService, s adjustment.Adjustme
|
|||||||
route := v1.Group("/adjustments")
|
route := v1.Group("/adjustments")
|
||||||
route.Use(m.Auth(u))
|
route.Use(m.Auth(u))
|
||||||
// Standard CRUD routes following master data pattern
|
// Standard CRUD routes following master data pattern
|
||||||
route.Get("/", ctrl.AdjustmentHistory) // Get all with pagination and filters
|
route.Get("/",m.RequirePermissions(m.P_AdjustmentGetAll), ctrl.AdjustmentHistory) // Get all with pagination and filters
|
||||||
route.Post("/", ctrl.Adjustment) // Create adjustment
|
route.Post("/",m.RequirePermissions(m.P_AdjustmentCreate), ctrl.Adjustment) // Create adjustment
|
||||||
route.Get("/:id", ctrl.GetOne)
|
route.Get("/:id",m.RequirePermissions(m.P_AdjustmentGetOne), ctrl.GetOne)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ func (s *adjustmentService) GetOne(c *fiber.Ctx, id uint) (*entity.StockLog, err
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if stockLog.LoggableType != entity.LogTypeAdjustment {
|
if stockLog.LoggableType != string(utils.StockLogTypeAdjustment) {
|
||||||
return nil, fiber.NewError(fiber.StatusNotFound, "Adjustment not found")
|
return nil, fiber.NewError(fiber.StatusNotFound, "Adjustment not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,7 +97,7 @@ func (s *adjustmentService) Adjustment(c *fiber.Ctx, req *validation.Create) (*e
|
|||||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Quantity must be greater than zero")
|
return nil, fiber.NewError(fiber.StatusBadRequest, "Quantity must be greater than zero")
|
||||||
}
|
}
|
||||||
transactionType := strings.ToUpper(req.TransactionType)
|
transactionType := strings.ToUpper(req.TransactionType)
|
||||||
if transactionType != entity.TransactionTypeIncrease && transactionType != entity.TransactionTypeDecrease {
|
if transactionType != string(utils.StockLogTransactionTypeIncrease) && transactionType != string(utils.StockLogTransactionTypeDecrease) {
|
||||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid transaction type")
|
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid transaction type")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,7 +141,7 @@ func (s *adjustmentService) Adjustment(c *fiber.Ctx, req *validation.Create) (*e
|
|||||||
if err := common.EnsureProjectFlockNotClosedForProductWarehouses(
|
if err := common.EnsureProjectFlockNotClosedForProductWarehouses(
|
||||||
ctx,
|
ctx,
|
||||||
s.StockLogsRepository.DB(),
|
s.StockLogsRepository.DB(),
|
||||||
[]uint{pw.Id},
|
[]uint{pw.Id},
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -154,14 +154,14 @@ func (s *adjustmentService) Adjustment(c *fiber.Ctx, req *validation.Create) (*e
|
|||||||
|
|
||||||
afterQuantity := productWarehouse.Quantity
|
afterQuantity := productWarehouse.Quantity
|
||||||
newLog := &entity.StockLog{
|
newLog := &entity.StockLog{
|
||||||
// TransactionType: transactionType,
|
|
||||||
LoggableType: entity.LogTypeAdjustment,
|
LoggableType: string(utils.StockLogTypeAdjustment),
|
||||||
LoggableId: 0,
|
LoggableId: 0,
|
||||||
Notes: req.Note,
|
Notes: req.Note,
|
||||||
ProductWarehouseId: productWarehouse.Id,
|
ProductWarehouseId: productWarehouse.Id,
|
||||||
CreatedBy: actorID, // TODO: should Get from auth middleware
|
CreatedBy: actorID, // TODO: should Get from auth middleware
|
||||||
}
|
}
|
||||||
if transactionType == entity.TransactionTypeIncrease {
|
if transactionType == string(utils.StockLogTransactionTypeIncrease) {
|
||||||
afterQuantity += req.Quantity
|
afterQuantity += req.Quantity
|
||||||
newLog.Increase = afterQuantity
|
newLog.Increase = afterQuantity
|
||||||
} else {
|
} else {
|
||||||
@@ -229,7 +229,6 @@ func (s *adjustmentService) AdjustmentHistory(c *fiber.Ctx, query *validation.Qu
|
|||||||
|
|
||||||
isWarehousesExist, err := s.WarehouseRepo.IdExists(c.Context(), uint(query.WarehouseID))
|
isWarehousesExist, err := s.WarehouseRepo.IdExists(c.Context(), uint(query.WarehouseID))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.Log.Errorf("Failed to check warehouse existence: %+v", err)
|
|
||||||
return nil, 0, fiber.NewError(fiber.StatusInternalServerError, "Failed to validate warehouse")
|
return nil, 0, fiber.NewError(fiber.StatusInternalServerError, "Failed to validate warehouse")
|
||||||
}
|
}
|
||||||
if query.WarehouseID > 0 && !isWarehousesExist {
|
if query.WarehouseID > 0 && !isWarehousesExist {
|
||||||
@@ -249,7 +248,7 @@ func (s *adjustmentService) AdjustmentHistory(c *fiber.Ctx, query *validation.Qu
|
|||||||
|
|
||||||
db = s.withRelations(db)
|
db = s.withRelations(db)
|
||||||
|
|
||||||
db = db.Where("loggable_type = ?", entity.LogTypeAdjustment)
|
db = db.Where("loggable_type = ?", string(utils.StockLogTypeAdjustment))
|
||||||
|
|
||||||
if query.TransactionType != "" {
|
if query.TransactionType != "" {
|
||||||
db = db.Where("transaction_type = ?", strings.ToUpper(query.TransactionType))
|
db = db.Where("transaction_type = ?", strings.ToUpper(query.TransactionType))
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package productStocks
|
package productStocks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
||||||
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-stocks/controllers"
|
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-stocks/controllers"
|
||||||
productStock "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-stocks/services"
|
productStock "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-stocks/services"
|
||||||
user "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
user "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
||||||
@@ -13,13 +13,13 @@ func ProductStockRoutes(v1 fiber.Router, u user.UserService, s productStock.Prod
|
|||||||
ctrl := controller.NewProductStockController(s)
|
ctrl := controller.NewProductStockController(s)
|
||||||
|
|
||||||
route := v1.Group("/product-stocks")
|
route := v1.Group("/product-stocks")
|
||||||
|
route.Use(m.Auth(u))
|
||||||
// route.Get("/", m.Auth(u), ctrl.GetAll)
|
// route.Get("/", m.Auth(u), ctrl.GetAll)
|
||||||
// route.Post("/", m.Auth(u), ctrl.CreateOne)
|
// route.Post("/", m.Auth(u), ctrl.CreateOne)
|
||||||
// route.Get("/:id", m.Auth(u), ctrl.GetOne)
|
// route.Get("/:id", m.Auth(u), ctrl.GetOne)
|
||||||
// route.Patch("/:id", m.Auth(u), ctrl.UpdateOne)
|
// route.Patch("/:id", m.Auth(u), ctrl.UpdateOne)
|
||||||
// route.Delete("/:id", m.Auth(u), ctrl.DeleteOne)
|
// route.Delete("/:id", m.Auth(u), ctrl.DeleteOne)
|
||||||
|
|
||||||
route.Get("/", ctrl.GetAll)
|
route.Get("/",m.RequirePermissions(m.P_ProductStockGetAll), ctrl.GetAll)
|
||||||
route.Get("/:id", ctrl.GetOne)
|
route.Get("/:id",m.RequirePermissions(m.P_ProductStockGetOne), ctrl.GetOne)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,11 +64,18 @@ func (s productStockService) GetAll(c *fiber.Ctx, params *validation.Query) ([]e
|
|||||||
offset := (params.Page - 1) * params.Limit
|
offset := (params.Page - 1) * params.Limit
|
||||||
|
|
||||||
productStocks, total, err := s.ProductRepository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
productStocks, total, err := s.ProductRepository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
||||||
|
db = db.Where(`EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM product_warehouses pw
|
||||||
|
WHERE pw.product_id = products.id
|
||||||
|
AND pw.qty > 0
|
||||||
|
)`)
|
||||||
|
|
||||||
db = s.withRelations(db)
|
db = s.withRelations(db)
|
||||||
if params.Search != "" {
|
if params.Search != "" {
|
||||||
return db.Where("name ILIKE ?", "%"+params.Search+"%")
|
db = db.Where("products.name ILIKE ?", "%"+params.Search+"%")
|
||||||
}
|
}
|
||||||
return db.Order("created_at DESC").Order("updated_at DESC")
|
return db.Order("products.created_at DESC").Order("products.updated_at DESC")
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
+1
-1
@@ -93,7 +93,7 @@ func (r *ProductWarehouseRepositoryImpl) GetByCategoryCodeAndWarehouseID(ctx con
|
|||||||
Joins("JOIN products ON products.id = product_warehouses.product_id").
|
Joins("JOIN products ON products.id = product_warehouses.product_id").
|
||||||
Joins("JOIN product_categories ON product_categories.id = products.product_category_id").
|
Joins("JOIN product_categories ON product_categories.id = products.product_category_id").
|
||||||
Where("product_categories.code = ? AND product_warehouses.warehouse_id = ?", categoryCode, warehouseId).
|
Where("product_categories.code = ? AND product_warehouses.warehouse_id = ?", categoryCode, warehouseId).
|
||||||
Order("product_warehouses.created_at DESC")
|
Order("product_warehouses.id DESC")
|
||||||
|
|
||||||
// preload relations so nested Product and Warehouse are populated
|
// preload relations so nested Product and Warehouse are populated
|
||||||
err := q.Preload("Product").Preload("Warehouse").Find(&productWarehouses).Error
|
err := q.Preload("Product").Preload("Warehouse").Find(&productWarehouses).Error
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ func ProductWarehouseRoutes(v1 fiber.Router, u user.UserService, s productWareho
|
|||||||
route := v1.Group("/product-warehouses")
|
route := v1.Group("/product-warehouses")
|
||||||
route.Use(m.Auth(u))
|
route.Use(m.Auth(u))
|
||||||
|
|
||||||
route.Get("/", ctrl.GetAll)
|
route.Get("/",m.RequirePermissions(m.P_ProductWarehousekGetAll), ctrl.GetAll)
|
||||||
route.Get("/:id", ctrl.GetOne)
|
route.Get("/:id",m.RequirePermissions(m.P_ProductWarehouseGetOne), ctrl.GetOne)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ func (u *TransferController) GetOne(c *fiber.Ctx) error {
|
|||||||
Code: fiber.StatusOK,
|
Code: fiber.StatusOK,
|
||||||
Status: "success",
|
Status: "success",
|
||||||
Message: "Get transfer successfully",
|
Message: "Get transfer successfully",
|
||||||
Data: dto.ToTransferListDTO(*result),
|
Data: dto.ToTransferDetailDTO(*result),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,15 +80,19 @@ func (u *TransferController) CreateOne(c *fiber.Ctx) error {
|
|||||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||||
}
|
}
|
||||||
|
|
||||||
// ambil file
|
|
||||||
form, err := c.MultipartForm()
|
form, err := c.MultipartForm()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid multipart form")
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid multipart form")
|
||||||
}
|
}
|
||||||
_ = form.File["documents"]
|
|
||||||
// todo: tunggu ada aws baru proses
|
|
||||||
|
|
||||||
result, err := u.TransferService.CreateOne(c, &req)
|
files := form.File["documents"]
|
||||||
|
|
||||||
|
if len(files) != len(req.Deliveries) {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest,
|
||||||
|
fiber.NewError(fiber.StatusBadRequest, "Jumlah dokumen harus sama dengan jumlah deliveries").Message)
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := u.TransferService.CreateOne(c, &req, files)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -98,6 +102,6 @@ func (u *TransferController) CreateOne(c *fiber.Ctx) error {
|
|||||||
Code: fiber.StatusCreated,
|
Code: fiber.StatusCreated,
|
||||||
Status: "success",
|
Status: "success",
|
||||||
Message: "Create transfer successfully",
|
Message: "Create transfer successfully",
|
||||||
Data: dto.ToTransferListDTO(*result),
|
Data: dto.ToTransferDetailDTO(*result),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,8 +7,6 @@ import (
|
|||||||
userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
|
userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
|
||||||
)
|
)
|
||||||
|
|
||||||
// === DTO Structs ===
|
|
||||||
|
|
||||||
type TransferRelationDTO struct {
|
type TransferRelationDTO struct {
|
||||||
Id uint64 `json:"id"`
|
Id uint64 `json:"id"`
|
||||||
TransferReason string `json:"transfer_reason"`
|
TransferReason string `json:"transfer_reason"`
|
||||||
@@ -17,7 +15,6 @@ type TransferRelationDTO struct {
|
|||||||
DestinationWarehouse *WarehouseDetailDTO `json:"destination_warehouse,omitempty"`
|
DestinationWarehouse *WarehouseDetailDTO `json:"destination_warehouse,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only id and name for warehouse simple view
|
|
||||||
type WarehouseSimpleDTO struct {
|
type WarehouseSimpleDTO struct {
|
||||||
Id uint `json:"id"`
|
Id uint `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
@@ -43,6 +40,14 @@ type SupplierSimpleDTO struct {
|
|||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DocumentDTO struct {
|
||||||
|
Id uint `json:"id"`
|
||||||
|
Path string `json:"path"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Ext string `json:"ext"`
|
||||||
|
Size float64 `json:"size"`
|
||||||
|
}
|
||||||
|
|
||||||
type WarehouseDetailDTO struct {
|
type WarehouseDetailDTO struct {
|
||||||
Id uint `json:"id"`
|
Id uint `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
@@ -65,24 +70,22 @@ type TransferDetailDTO struct {
|
|||||||
Deliveries []TransferDeliveryDTO `json:"deliveries"`
|
Deliveries []TransferDeliveryDTO `json:"deliveries"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Detail produk
|
|
||||||
type TransferDetailItemDTO struct {
|
type TransferDetailItemDTO struct {
|
||||||
Id uint64 `json:"id"`
|
Id uint64 `json:"id"`
|
||||||
Proudct ProductSimpleDTO `json:"product"`
|
Product ProductSimpleDTO `json:"product"`
|
||||||
Quantity float64 `json:"quantity"`
|
Quantity float64 `json:"quantity"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delivery ekspedisi
|
|
||||||
type TransferDeliveryDTO struct {
|
type TransferDeliveryDTO struct {
|
||||||
Id uint64 `json:"id"`
|
Id uint64 `json:"id"`
|
||||||
Supplier SupplierSimpleDTO `json:"supplier"`
|
Supplier SupplierSimpleDTO `json:"supplier"`
|
||||||
VehiclePlate string `json:"vehicle_plate"`
|
VehiclePlate string `json:"vehicle_plate"`
|
||||||
DriverName string `json:"driver_name"`
|
DriverName string `json:"driver_name"`
|
||||||
DocumentNumber string `json:"document_number"`
|
DocumentNumber string `json:"document_number"`
|
||||||
DocumentPath string `json:"document_path"`
|
|
||||||
ShippingCostItem float64 `json:"shipping_cost_item"`
|
ShippingCostItem float64 `json:"shipping_cost_item"`
|
||||||
ShippingCostTotal float64 `json:"shipping_cost_total"`
|
ShippingCostTotal float64 `json:"shipping_cost_total"`
|
||||||
Items []TransferDeliveryItemDTO `json:"items"`
|
Items []TransferDeliveryItemDTO `json:"items"`
|
||||||
|
Document *DocumentDTO `json:"document,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type TransferDeliveryItemDTO struct {
|
type TransferDeliveryItemDTO struct {
|
||||||
@@ -91,10 +94,7 @@ type TransferDeliveryItemDTO struct {
|
|||||||
Quantity float64 `json:"quantity"`
|
Quantity float64 `json:"quantity"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// === Mapper Functions ===
|
|
||||||
|
|
||||||
func ToTransferRelationDTO(e entity.StockTransfer) TransferRelationDTO {
|
func ToTransferRelationDTO(e entity.StockTransfer) TransferRelationDTO {
|
||||||
|
|
||||||
var sourceWarehouse *WarehouseDetailDTO
|
var sourceWarehouse *WarehouseDetailDTO
|
||||||
if e.FromWarehouse != nil && e.FromWarehouse.Id != 0 {
|
if e.FromWarehouse != nil && e.FromWarehouse.Id != 0 {
|
||||||
sourceWarehouse = toWarehouseDetailDTO(e.FromWarehouse)
|
sourceWarehouse = toWarehouseDetailDTO(e.FromWarehouse)
|
||||||
@@ -140,7 +140,7 @@ func toWarehouseDetailDTO(w *entity.Warehouse) *WarehouseDetailDTO {
|
|||||||
Id: w.Id,
|
Id: w.Id,
|
||||||
Name: w.Name,
|
Name: w.Name,
|
||||||
Location: toLocationDTO(w.Location),
|
Location: toLocationDTO(w.Location),
|
||||||
Area: toAreaDTO(&w.Area), // Ambil area langsung dari warehouse (area_id)
|
Area: toAreaDTO(&w.Area),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,22 +150,21 @@ func ToTransferListDTO(e entity.StockTransfer) TransferListDTO {
|
|||||||
mapped := userDTO.ToUserRelationDTO(*e.CreatedUser)
|
mapped := userDTO.ToUserRelationDTO(*e.CreatedUser)
|
||||||
createdUser = &mapped
|
createdUser = &mapped
|
||||||
}
|
}
|
||||||
// Map details
|
|
||||||
var details []TransferDetailItemDTO
|
var details []TransferDetailItemDTO
|
||||||
for _, d := range e.Details {
|
for _, d := range e.Details {
|
||||||
details = append(details, TransferDetailItemDTO{
|
details = append(details, TransferDetailItemDTO{
|
||||||
Id: d.Id,
|
Id: d.Id,
|
||||||
Proudct: ProductSimpleDTO{
|
Product: ProductSimpleDTO{
|
||||||
Id: d.Product.Id,
|
Id: d.Product.Id,
|
||||||
Name: d.Product.Name,
|
Name: d.Product.Name,
|
||||||
},
|
},
|
||||||
Quantity: d.Quantity,
|
Quantity: d.Quantity,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
// Map deliveries
|
|
||||||
var deliveries []TransferDeliveryDTO
|
var deliveries []TransferDeliveryDTO
|
||||||
for _, del := range e.Deliveries {
|
for _, del := range e.Deliveries {
|
||||||
// Map delivery items
|
|
||||||
var items []TransferDeliveryItemDTO
|
var items []TransferDeliveryItemDTO
|
||||||
for _, item := range del.Items {
|
for _, item := range del.Items {
|
||||||
items = append(items, TransferDeliveryItemDTO{
|
items = append(items, TransferDeliveryItemDTO{
|
||||||
@@ -174,6 +173,19 @@ func ToTransferListDTO(e entity.StockTransfer) TransferListDTO {
|
|||||||
Quantity: item.Quantity,
|
Quantity: item.Quantity,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var document *DocumentDTO
|
||||||
|
if len(del.Documents) > 0 {
|
||||||
|
doc := del.Documents[0] // Take first document
|
||||||
|
document = &DocumentDTO{
|
||||||
|
Id: doc.Id,
|
||||||
|
Path: doc.Path,
|
||||||
|
Name: doc.Name,
|
||||||
|
Ext: doc.Ext,
|
||||||
|
Size: doc.Size,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
deliveries = append(deliveries, TransferDeliveryDTO{
|
deliveries = append(deliveries, TransferDeliveryDTO{
|
||||||
Id: del.Id,
|
Id: del.Id,
|
||||||
Supplier: SupplierSimpleDTO{
|
Supplier: SupplierSimpleDTO{
|
||||||
@@ -183,12 +195,13 @@ func ToTransferListDTO(e entity.StockTransfer) TransferListDTO {
|
|||||||
VehiclePlate: del.VehiclePlate,
|
VehiclePlate: del.VehiclePlate,
|
||||||
DriverName: del.DriverName,
|
DriverName: del.DriverName,
|
||||||
DocumentNumber: del.DocumentNumber,
|
DocumentNumber: del.DocumentNumber,
|
||||||
DocumentPath: del.DocumentPath,
|
|
||||||
ShippingCostItem: del.ShippingCostItem,
|
ShippingCostItem: del.ShippingCostItem,
|
||||||
ShippingCostTotal: del.ShippingCostTotal,
|
ShippingCostTotal: del.ShippingCostTotal,
|
||||||
Items: items,
|
Items: items,
|
||||||
|
Document: document,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return TransferListDTO{
|
return TransferListDTO{
|
||||||
TransferRelationDTO: ToTransferRelationDTO(e),
|
TransferRelationDTO: ToTransferRelationDTO(e),
|
||||||
CreatedUser: createdUser,
|
CreatedUser: createdUser,
|
||||||
@@ -208,21 +221,32 @@ func ToTransferListDTOs(e []entity.StockTransfer) []TransferListDTO {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ToTransferDetailDTO(e entity.StockTransfer) TransferDetailDTO {
|
func ToTransferDetailDTO(e entity.StockTransfer) TransferDetailDTO {
|
||||||
// Map details
|
|
||||||
var details []TransferDetailItemDTO
|
var details []TransferDetailItemDTO
|
||||||
for _, d := range e.Details {
|
for _, d := range e.Details {
|
||||||
details = append(details, TransferDetailItemDTO{
|
details = append(details, TransferDetailItemDTO{
|
||||||
Id: d.Id,
|
Id: d.Id,
|
||||||
Proudct: ProductSimpleDTO{
|
Product: ProductSimpleDTO{
|
||||||
Id: d.Product.Id,
|
Id: d.Product.Id,
|
||||||
Name: d.Product.Name,
|
Name: d.Product.Name,
|
||||||
},
|
},
|
||||||
Quantity: d.Quantity,
|
Quantity: d.Quantity,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
// Map deliveries
|
|
||||||
var deliveries []TransferDeliveryDTO
|
var deliveries []TransferDeliveryDTO
|
||||||
for _, del := range e.Deliveries {
|
for _, del := range e.Deliveries {
|
||||||
|
var document *DocumentDTO
|
||||||
|
if len(del.Documents) > 0 {
|
||||||
|
doc := del.Documents[0] // Take first document
|
||||||
|
document = &DocumentDTO{
|
||||||
|
Id: doc.Id,
|
||||||
|
Path: doc.Path,
|
||||||
|
Name: doc.Name,
|
||||||
|
Ext: doc.Ext,
|
||||||
|
Size: doc.Size,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
deliveries = append(deliveries, TransferDeliveryDTO{
|
deliveries = append(deliveries, TransferDeliveryDTO{
|
||||||
Id: del.Id,
|
Id: del.Id,
|
||||||
Supplier: SupplierSimpleDTO{
|
Supplier: SupplierSimpleDTO{
|
||||||
@@ -232,11 +256,12 @@ func ToTransferDetailDTO(e entity.StockTransfer) TransferDetailDTO {
|
|||||||
VehiclePlate: del.VehiclePlate,
|
VehiclePlate: del.VehiclePlate,
|
||||||
DriverName: del.DriverName,
|
DriverName: del.DriverName,
|
||||||
DocumentNumber: del.DocumentNumber,
|
DocumentNumber: del.DocumentNumber,
|
||||||
DocumentPath: del.DocumentPath,
|
|
||||||
ShippingCostItem: del.ShippingCostItem,
|
ShippingCostItem: del.ShippingCostItem,
|
||||||
ShippingCostTotal: del.ShippingCostTotal,
|
ShippingCostTotal: del.ShippingCostTotal,
|
||||||
|
Document: document,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return TransferDetailDTO{
|
return TransferDetailDTO{
|
||||||
TransferListDTO: ToTransferListDTO(e),
|
TransferListDTO: ToTransferListDTO(e),
|
||||||
Details: details,
|
Details: details,
|
||||||
|
|||||||
@@ -1,10 +1,14 @@
|
|||||||
package transfers
|
package transfers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
"github.com/go-playground/validator/v10"
|
"github.com/go-playground/validator/v10"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
|
||||||
|
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||||
|
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||||
rProductWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/repositories"
|
rProductWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/repositories"
|
||||||
rStockTransfer "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/transfers/repositories"
|
rStockTransfer "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/transfers/repositories"
|
||||||
sTransfer "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/transfers/services"
|
sTransfer "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/transfers/services"
|
||||||
@@ -29,8 +33,14 @@ func (TransferModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
|
|||||||
userRepo := rUser.NewUserRepository(db)
|
userRepo := rUser.NewUserRepository(db)
|
||||||
warehouseRepo := rWarehouse.NewWarehouseRepository(db)
|
warehouseRepo := rWarehouse.NewWarehouseRepository(db)
|
||||||
projectFlockKandangRepo := rProjectFlockKandang.NewProjectFlockKandangRepository(db)
|
projectFlockKandangRepo := rProjectFlockKandang.NewProjectFlockKandangRepository(db)
|
||||||
|
documentRepo := commonRepo.NewDocumentRepository(db)
|
||||||
|
|
||||||
transferService := sTransfer.NewTransferService(validate, stockTransferRepo, stockTransferDetailRepo, stockTransferDeliveryRepo, StockTransferDeliveryItemRepo, stockLogsRepo, productWarehouseRepo, supplierRepo, warehouseRepo, projectFlockKandangRepo)
|
documentSvc, err := commonSvc.NewDocumentServiceFromConfig(context.Background(), documentRepo)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
transferService := sTransfer.NewTransferService(validate, stockTransferRepo, stockTransferDetailRepo, stockTransferDeliveryRepo, StockTransferDeliveryItemRepo, stockLogsRepo, productWarehouseRepo, supplierRepo, warehouseRepo, projectFlockKandangRepo, documentSvc)
|
||||||
userService := sUser.NewUserService(userRepo, validate)
|
userService := sUser.NewUserService(userRepo, validate)
|
||||||
|
|
||||||
TransferRoutes(router, userService, transferService)
|
TransferRoutes(router, userService, transferService)
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ func TransferRoutes(v1 fiber.Router, u user.UserService, s transfer.TransferServ
|
|||||||
route := v1.Group("/transfers")
|
route := v1.Group("/transfers")
|
||||||
route.Use(m.Auth(u))
|
route.Use(m.Auth(u))
|
||||||
|
|
||||||
route.Get("/", ctrl.GetAll)
|
route.Get("/",m.RequirePermissions(m.P_TransferGetAll), ctrl.GetAll)
|
||||||
route.Post("/", ctrl.CreateOne)
|
route.Post("/",m.RequirePermissions(m.P_TransferCreateOne), ctrl.CreateOne)
|
||||||
route.Get("/:id", ctrl.GetOne)
|
route.Get("/:id",m.RequirePermissions(m.P_TransferGetOne), ctrl.GetOne)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"mime/multipart"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/go-playground/validator/v10"
|
"github.com/go-playground/validator/v10"
|
||||||
@@ -27,7 +28,7 @@ import (
|
|||||||
type TransferService interface {
|
type TransferService interface {
|
||||||
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.StockTransfer, int64, error)
|
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.StockTransfer, int64, error)
|
||||||
GetOne(ctx *fiber.Ctx, id uint) (*entity.StockTransfer, error)
|
GetOne(ctx *fiber.Ctx, id uint) (*entity.StockTransfer, error)
|
||||||
CreateOne(ctx *fiber.Ctx, req *validation.TransferRequest) (*entity.StockTransfer, error)
|
CreateOne(ctx *fiber.Ctx, req *validation.TransferRequest, files []*multipart.FileHeader) (*entity.StockTransfer, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type transferService struct {
|
type transferService struct {
|
||||||
@@ -42,9 +43,10 @@ type transferService struct {
|
|||||||
SupplierRepo rSupplier.SupplierRepository
|
SupplierRepo rSupplier.SupplierRepository
|
||||||
WarehouseRepo warehouseRepo.WarehouseRepository
|
WarehouseRepo warehouseRepo.WarehouseRepository
|
||||||
ProjectFlockKandangRepo projectFlockKandangRepo.ProjectFlockKandangRepository
|
ProjectFlockKandangRepo projectFlockKandangRepo.ProjectFlockKandangRepository
|
||||||
|
DocumentSvc commonSvc.DocumentService
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTransferService(validate *validator.Validate, stockTransferRepo rStockTransfer.StockTransferRepository, stockTransferDetailRepo rStockTransfer.StockTransferDetailRepository, stockTransferDeliveryRepo rStockTransfer.StockTransferDeliveryRepository, stockTransferDeliveryItemRepo rStockTransfer.StockTransferDeliveryItemRepository, stockLogsRepo rStockLogs.StockLogRepository, productWarehouseRepo rProductWarehouse.ProductWarehouseRepository, supplierRepo rSupplier.SupplierRepository, warehouseRepo warehouseRepo.WarehouseRepository, projectFlockKandangRepo projectFlockKandangRepo.ProjectFlockKandangRepository) TransferService {
|
func NewTransferService(validate *validator.Validate, stockTransferRepo rStockTransfer.StockTransferRepository, stockTransferDetailRepo rStockTransfer.StockTransferDetailRepository, stockTransferDeliveryRepo rStockTransfer.StockTransferDeliveryRepository, stockTransferDeliveryItemRepo rStockTransfer.StockTransferDeliveryItemRepository, stockLogsRepo rStockLogs.StockLogRepository, productWarehouseRepo rProductWarehouse.ProductWarehouseRepository, supplierRepo rSupplier.SupplierRepository, warehouseRepo warehouseRepo.WarehouseRepository, projectFlockKandangRepo projectFlockKandangRepo.ProjectFlockKandangRepository, documentSvc commonSvc.DocumentService) TransferService {
|
||||||
return &transferService{
|
return &transferService{
|
||||||
Log: utils.Log,
|
Log: utils.Log,
|
||||||
Validate: validate,
|
Validate: validate,
|
||||||
@@ -57,8 +59,10 @@ func NewTransferService(validate *validator.Validate, stockTransferRepo rStockTr
|
|||||||
SupplierRepo: supplierRepo,
|
SupplierRepo: supplierRepo,
|
||||||
WarehouseRepo: warehouseRepo,
|
WarehouseRepo: warehouseRepo,
|
||||||
ProjectFlockKandangRepo: projectFlockKandangRepo,
|
ProjectFlockKandangRepo: projectFlockKandangRepo,
|
||||||
|
DocumentSvc: documentSvc,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s transferService) withRelations(db *gorm.DB) *gorm.DB {
|
func (s transferService) withRelations(db *gorm.DB) *gorm.DB {
|
||||||
return db.
|
return db.
|
||||||
Preload("CreatedUser").
|
Preload("CreatedUser").
|
||||||
@@ -71,7 +75,10 @@ func (s transferService) withRelations(db *gorm.DB) *gorm.DB {
|
|||||||
Preload("Details").
|
Preload("Details").
|
||||||
Preload("Details.Product").
|
Preload("Details.Product").
|
||||||
Preload("Deliveries.Items").
|
Preload("Deliveries.Items").
|
||||||
Preload("Deliveries.Supplier")
|
Preload("Deliveries.Supplier").
|
||||||
|
Preload("Deliveries.Documents", func(db *gorm.DB) *gorm.DB {
|
||||||
|
return db.Where("documentable_type = ?", string(utils.DocumentableTypeTransfer))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s transferService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.StockTransfer, int64, error) {
|
func (s transferService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.StockTransfer, int64, error) {
|
||||||
@@ -93,37 +100,34 @@ func (s transferService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entit
|
|||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
s.Log.Infof("Retrieved %d transfers", len(transfers))
|
|
||||||
|
|
||||||
return transfers, total, nil
|
return transfers, total, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s transferService) GetOne(c *fiber.Ctx, id uint) (*entity.StockTransfer, error) {
|
func (s transferService) GetOne(c *fiber.Ctx, id uint) (*entity.StockTransfer, error) {
|
||||||
var transfer entity.StockTransfer
|
s.Log.Infof("Attempting to get StockTransfer with ID: %d", id)
|
||||||
|
|
||||||
// gunakan repo secara langsung
|
|
||||||
transferPtr, err := s.StockTransferRepo.GetByID(c.Context(), id, func(db *gorm.DB) *gorm.DB {
|
transferPtr, err := s.StockTransferRepo.GetByID(c.Context(), id, func(db *gorm.DB) *gorm.DB {
|
||||||
return s.withRelations(db)
|
return s.withRelations(db)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
s.Log.Errorf("Error getting StockTransfer ID %d: %+v", id, err)
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
return nil, fiber.NewError(fiber.StatusNotFound, "Transfer not found")
|
return nil, fiber.NewError(fiber.StatusNotFound, "Transfer not found")
|
||||||
}
|
}
|
||||||
s.Log.Errorf("Failed to get transfer by ID: %+v", err)
|
|
||||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to get transfer")
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to get transfer")
|
||||||
}
|
}
|
||||||
|
|
||||||
s.Log.Infof("Retrieved transfer: %+v", transfer)
|
if transferPtr != nil {
|
||||||
|
s.Log.Infof("StockTransfer %d has %d documents", transferPtr.Id, len(transferPtr.Documents))
|
||||||
|
}
|
||||||
|
|
||||||
return transferPtr, nil
|
return transferPtr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *transferService) CreateOne(c *fiber.Ctx, req *validation.TransferRequest) (*entity.StockTransfer, error) {
|
func (s *transferService) CreateOne(c *fiber.Ctx, req *validation.TransferRequest, files []*multipart.FileHeader) (*entity.StockTransfer, error) {
|
||||||
// Validasi stok di gudang asal harus exist dan mencukupi
|
|
||||||
pwIDs := make([]uint, 0, len(req.Products))
|
pwIDs := make([]uint, 0, len(req.Products))
|
||||||
|
|
||||||
// Validasi stok di gudang asal harus exist dan mencukupi
|
|
||||||
for _, product := range req.Products {
|
for _, product := range req.Products {
|
||||||
sourcePW, err := s.ProductWarehouseRepo.GetProductWarehouseByProductAndWarehouseID(
|
sourcePW, err := s.ProductWarehouseRepo.GetProductWarehouseByProductAndWarehouseID(
|
||||||
c.Context(), uint(product.ProductID), uint(req.SourceWarehouseID),
|
c.Context(), uint(product.ProductID), uint(req.SourceWarehouseID),
|
||||||
@@ -139,6 +143,7 @@ func (s *transferService) CreateOne(c *fiber.Ctx, req *validation.TransferReques
|
|||||||
}
|
}
|
||||||
pwIDs = append(pwIDs, sourcePW.Id)
|
pwIDs = append(pwIDs, sourcePW.Id)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := commonSvc.EnsureProjectFlockNotClosedForProductWarehouses(
|
if err := commonSvc.EnsureProjectFlockNotClosedForProductWarehouses(
|
||||||
c.Context(),
|
c.Context(),
|
||||||
s.StockTransferRepo.DB(),
|
s.StockTransferRepo.DB(),
|
||||||
@@ -152,7 +157,6 @@ func (s *transferService) CreateOne(c *fiber.Ctx, req *validation.TransferReques
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// validasi total qty harus lebih besar dari atau sama dengan total qty di delivery compare berdasarkan productid
|
|
||||||
deliveryQtyMap := make(map[uint]float64)
|
deliveryQtyMap := make(map[uint]float64)
|
||||||
for _, delivery := range req.Deliveries {
|
for _, delivery := range req.Deliveries {
|
||||||
for _, prod := range delivery.Products {
|
for _, prod := range delivery.Products {
|
||||||
@@ -160,7 +164,6 @@ func (s *transferService) CreateOne(c *fiber.Ctx, req *validation.TransferReques
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cek: qty delivery tidak boleh melebihi qty di root
|
|
||||||
for _, product := range req.Products {
|
for _, product := range req.Products {
|
||||||
if deliveryQtyMap[product.ProductID] > product.ProductQty {
|
if deliveryQtyMap[product.ProductID] > product.ProductQty {
|
||||||
return nil, fiber.NewError(fiber.StatusBadRequest,
|
return nil, fiber.NewError(fiber.StatusBadRequest,
|
||||||
@@ -168,7 +171,6 @@ func (s *transferService) CreateOne(c *fiber.Ctx, req *validation.TransferReques
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// cek suplier id caegory BOP cek by id
|
|
||||||
for _, delivery := range req.Deliveries {
|
for _, delivery := range req.Deliveries {
|
||||||
supplier, err := s.SupplierRepo.GetByID(c.Context(), uint(delivery.SupplierID), nil)
|
supplier, err := s.SupplierRepo.GetByID(c.Context(), uint(delivery.SupplierID), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -182,11 +184,8 @@ func (s *transferService) CreateOne(c *fiber.Ctx, req *validation.TransferReques
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate movement number
|
|
||||||
// Format: PND-MBU-00001
|
|
||||||
seqNum, err := s.StockTransferRepo.GetNextMovementNumber(c.Context())
|
seqNum, err := s.StockTransferRepo.GetNextMovementNumber(c.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.Log.Errorf("Failed to get next movement number: %+v", err)
|
|
||||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to generate movement number")
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to generate movement number")
|
||||||
}
|
}
|
||||||
movementNumber := fmt.Sprintf("PND-MBU-%05d", seqNum)
|
movementNumber := fmt.Sprintf("PND-MBU-%05d", seqNum)
|
||||||
@@ -201,17 +200,12 @@ func (s *transferService) CreateOne(c *fiber.Ctx, req *validation.TransferReques
|
|||||||
CreatedBy: uint64(actorID),
|
CreatedBy: uint64(actorID),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save the transfer entity to the database
|
|
||||||
err = s.StockTransferRepo.DB().WithContext(c.Context()).Transaction(func(tx *gorm.DB) error {
|
err = s.StockTransferRepo.DB().WithContext(c.Context()).Transaction(func(tx *gorm.DB) error {
|
||||||
|
|
||||||
// Insert header
|
|
||||||
if err := s.StockTransferRepo.WithTx(tx).CreateOne(c.Context(), entityTransfer, nil); err != nil {
|
if err := s.StockTransferRepo.WithTx(tx).CreateOne(c.Context(), entityTransfer, nil); err != nil {
|
||||||
s.Log.Errorf("Failed to create stock transfer: %+v", err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
s.Log.Infof("Stock transfer created: %+v", entityTransfer.Id)
|
|
||||||
|
|
||||||
// insert ke details
|
|
||||||
var details []*entity.StockTransferDetail
|
var details []*entity.StockTransferDetail
|
||||||
for _, product := range req.Products {
|
for _, product := range req.Products {
|
||||||
details = append(details, &entity.StockTransferDetail{
|
details = append(details, &entity.StockTransferDetail{
|
||||||
@@ -221,12 +215,9 @@ func (s *transferService) CreateOne(c *fiber.Ctx, req *validation.TransferReques
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
if err := s.StockTransferDetailRepo.WithTx(tx).CreateMany(c.Context(), details, nil); err != nil {
|
if err := s.StockTransferDetailRepo.WithTx(tx).CreateMany(c.Context(), details, nil); err != nil {
|
||||||
s.Log.Errorf("Failed to create stock transfer details: %+v", err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
s.Log.Infof("Stock transfer details created for transfer ID: %+v", entityTransfer.Id)
|
|
||||||
|
|
||||||
// Tambahkan proses insert delivery
|
|
||||||
var deliveries []*entity.StockTransferDelivery
|
var deliveries []*entity.StockTransferDelivery
|
||||||
for _, delivery := range req.Deliveries {
|
for _, delivery := range req.Deliveries {
|
||||||
deliveries = append(deliveries, &entity.StockTransferDelivery{
|
deliveries = append(deliveries, &entity.StockTransferDelivery{
|
||||||
@@ -234,16 +225,14 @@ func (s *transferService) CreateOne(c *fiber.Ctx, req *validation.TransferReques
|
|||||||
SupplierId: uint64(delivery.SupplierID),
|
SupplierId: uint64(delivery.SupplierID),
|
||||||
VehiclePlate: delivery.VehiclePlate,
|
VehiclePlate: delivery.VehiclePlate,
|
||||||
DriverName: delivery.DriverName,
|
DriverName: delivery.DriverName,
|
||||||
DocumentPath: "https://tourism.gov.in/sites/default/files/2019-04/dummy-pdf_2.pdf", // todo: tunggu ada aws baru proses
|
|
||||||
ShippingCostItem: delivery.DeliveryCostPerItem,
|
ShippingCostItem: delivery.DeliveryCostPerItem,
|
||||||
ShippingCostTotal: delivery.DeliveryCost,
|
ShippingCostTotal: delivery.DeliveryCost,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if err := s.StockTransferDeliveryRepo.WithTx(tx).CreateMany(c.Context(), deliveries, nil); err != nil {
|
if err := s.StockTransferDeliveryRepo.WithTx(tx).CreateMany(c.Context(), deliveries, nil); err != nil {
|
||||||
s.Log.Errorf("Failed to create stock transfer deliveries: %+v", err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// tambahkan insert ke delivery items sebagai pivot
|
|
||||||
detailMap := make(map[uint64]uint64)
|
detailMap := make(map[uint64]uint64)
|
||||||
for _, d := range details {
|
for _, d := range details {
|
||||||
detailMap[d.ProductId] = d.Id
|
detailMap[d.ProductId] = d.Id
|
||||||
@@ -266,61 +255,63 @@ func (s *transferService) CreateOne(c *fiber.Ctx, req *validation.TransferReques
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err := s.StockTransferDeliveryItemRepo.WithTx(tx).CreateMany(c.Context(), deliveryItems, nil); err != nil {
|
if err := s.StockTransferDeliveryItemRepo.WithTx(tx).CreateMany(c.Context(), deliveryItems, nil); err != nil {
|
||||||
s.Log.Errorf("Failed to create stock transfer delivery items: %+v", err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
s.Log.Infof("Stock transfer delivery items created for transfer ID: %+v", entityTransfer.Id)
|
|
||||||
|
|
||||||
// Proses pengurangan stok di gudang asal dan penambahan stok di gudang tujuan
|
if s.DocumentSvc != nil && len(files) > 0 {
|
||||||
|
|
||||||
|
for idx, file := range files {
|
||||||
|
documentFiles := []commonSvc.DocumentFile{
|
||||||
|
{
|
||||||
|
File: file,
|
||||||
|
Type: string(utils.DocumentTypeTransfer),
|
||||||
|
Index: &idx,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
_, err := s.DocumentSvc.UploadDocuments(c.Context(), commonSvc.DocumentUploadRequest{
|
||||||
|
DocumentableType: string(utils.DocumentableTypeTransfer),
|
||||||
|
DocumentableID: deliveries[idx].Id,
|
||||||
|
CreatedBy: &actorID,
|
||||||
|
Files: documentFiles,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to upload document for delivery %d", idx+1))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for _, product := range req.Products {
|
for _, product := range req.Products {
|
||||||
// Kurangi stok di gudang asal
|
|
||||||
sourcePW, err := s.ProductWarehouseRepo.GetProductWarehouseByProductAndWarehouseID(c.Context(), uint(product.ProductID), uint(req.SourceWarehouseID))
|
sourcePW, err := s.ProductWarehouseRepo.GetProductWarehouseByProductAndWarehouseID(c.Context(), uint(product.ProductID), uint(req.SourceWarehouseID))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.Log.Errorf("Failed to get source product warehouse: %+v", err)
|
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to get source product warehouse")
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to get source product warehouse")
|
||||||
}
|
}
|
||||||
if sourcePW.Quantity < product.ProductQty {
|
if sourcePW.Quantity < product.ProductQty {
|
||||||
s.Log.Errorf("Insufficient stock in source warehouse for product ID: %+v", product.ProductID)
|
|
||||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Insufficient stock in source warehouse for product ID: %d", product.ProductID))
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Insufficient stock in source warehouse for product ID: %d", product.ProductID))
|
||||||
}
|
}
|
||||||
sourcePW.Quantity -= product.ProductQty
|
sourcePW.Quantity -= product.ProductQty
|
||||||
if err := s.ProductWarehouseRepo.WithTx(tx).UpdateOne(c.Context(), sourcePW.Id, sourcePW, nil); err != nil {
|
if err := s.ProductWarehouseRepo.WithTx(tx).UpdateOne(c.Context(), sourcePW.Id, sourcePW, nil); err != nil {
|
||||||
s.Log.Errorf("Failed to update source product warehouse: %+v", err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
s.Log.Infof("Source product warehouse updated: %+v", sourcePW.Id)
|
|
||||||
|
|
||||||
// create stock log for decrease (source)
|
|
||||||
// beforeQty := sourcePW.Quantity + product.ProductQty // sourcePW already decreased
|
|
||||||
decreaseLog := &entity.StockLog{
|
decreaseLog := &entity.StockLog{
|
||||||
// TransactionType: entity.TransactionTypeDecrease,
|
|
||||||
// Quantity: product.ProductQty,
|
|
||||||
// BeforeQuantity: beforeQty,
|
|
||||||
// AfterQuantity: sourcePW.Qty,
|
|
||||||
// LogType: entity.LogTypeTransfer,
|
|
||||||
// LogId: uint(entityTransfer.Id),
|
|
||||||
Decrease: product.ProductQty,
|
Decrease: product.ProductQty,
|
||||||
Notes: "",
|
Notes: "",
|
||||||
LoggableType: entity.LogTypeTransfer,
|
LoggableType: string(utils.StockLogTypeTransfer),
|
||||||
LoggableId: uint(entityTransfer.Id),
|
LoggableId: uint(entityTransfer.Id),
|
||||||
ProductWarehouseId: sourcePW.Id,
|
ProductWarehouseId: sourcePW.Id,
|
||||||
CreatedBy: actorID,
|
CreatedBy: actorID,
|
||||||
}
|
}
|
||||||
if err := s.StockLogsRepository.WithTx(tx).CreateOne(c.Context(), decreaseLog, nil); err != nil {
|
if err := s.StockLogsRepository.WithTx(tx).CreateOne(c.Context(), decreaseLog, nil); err != nil {
|
||||||
s.Log.Errorf("Failed to create stock log decrease: %+v", err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tambah stok di gudang tujuan
|
|
||||||
destPW, err := s.ProductWarehouseRepo.GetProductWarehouseByProductAndWarehouseID(
|
destPW, err := s.ProductWarehouseRepo.GetProductWarehouseByProductAndWarehouseID(
|
||||||
c.Context(), uint(product.ProductID), uint(req.DestinationWarehouseID),
|
c.Context(), uint(product.ProductID), uint(req.DestinationWarehouseID),
|
||||||
)
|
)
|
||||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
s.Log.Errorf("Failed to get destination product warehouse: %+v", err)
|
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to get destination product warehouse")
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to get destination product warehouse")
|
||||||
}
|
}
|
||||||
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
|
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
// Jika belum ada record untuk produk di gudang tujuan, buat baru
|
|
||||||
ctx := c.Context()
|
ctx := c.Context()
|
||||||
projectFlockKandangID, err := s.getActiveProjectFlockKandangID(ctx, uint(req.DestinationWarehouseID))
|
projectFlockKandangID, err := s.getActiveProjectFlockKandangID(ctx, uint(req.DestinationWarehouseID))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -331,41 +322,28 @@ func (s *transferService) CreateOne(c *fiber.Ctx, req *validation.TransferReques
|
|||||||
WarehouseId: uint(req.DestinationWarehouseID),
|
WarehouseId: uint(req.DestinationWarehouseID),
|
||||||
Quantity: 0,
|
Quantity: 0,
|
||||||
ProjectFlockKandangId: &projectFlockKandangID,
|
ProjectFlockKandangId: &projectFlockKandangID,
|
||||||
// CreatedBy: 1, // TODO: should Get from auth middleware
|
|
||||||
}
|
}
|
||||||
if err := s.ProductWarehouseRepo.WithTx(tx).CreateOne(c.Context(), destPW, nil); err != nil {
|
if err := s.ProductWarehouseRepo.WithTx(tx).CreateOne(c.Context(), destPW, nil); err != nil {
|
||||||
s.Log.Errorf("Failed to create destination product warehouse: %+v", err)
|
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create destination product warehouse")
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create destination product warehouse")
|
||||||
}
|
}
|
||||||
s.Log.Infof("Destination product warehouse created: %+v", destPW.Id)
|
|
||||||
}
|
}
|
||||||
// Update stok di gudang tujuan
|
|
||||||
destPW.Quantity += product.ProductQty
|
destPW.Quantity += product.ProductQty
|
||||||
if err := s.ProductWarehouseRepo.WithTx(tx).UpdateOne(c.Context(), destPW.Id, destPW, nil); err != nil {
|
if err := s.ProductWarehouseRepo.WithTx(tx).UpdateOne(c.Context(), destPW.Id, destPW, nil); err != nil {
|
||||||
s.Log.Errorf("Failed to update destination product warehouse: %+v", err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
s.Log.Infof("Destination product warehouse updated: %+v", destPW.Id)
|
|
||||||
|
|
||||||
// create stock log for increase (destination)
|
|
||||||
// beforeDestQty := destPW.Quantity - product.ProductQty
|
|
||||||
increaseLog := &entity.StockLog{
|
increaseLog := &entity.StockLog{
|
||||||
// TransactionType: entity.TransactionTypeIncrease,
|
|
||||||
// Quantity: product.ProductQty,
|
|
||||||
// BeforeQuantity: beforeDestQty,
|
|
||||||
// AfterQuantity: destPW.Qty,
|
|
||||||
Increase: product.ProductQty,
|
Increase: product.ProductQty,
|
||||||
LoggableType: entity.LogTypeTransfer,
|
LoggableType: string(utils.StockLogTypeTransfer),
|
||||||
LoggableId: uint(entityTransfer.Id),
|
LoggableId: uint(entityTransfer.Id),
|
||||||
Notes: "",
|
Notes: "",
|
||||||
ProductWarehouseId: destPW.Id,
|
ProductWarehouseId: destPW.Id,
|
||||||
CreatedBy: actorID,
|
CreatedBy: actorID,
|
||||||
}
|
}
|
||||||
if err := s.StockLogsRepository.WithTx(tx).CreateOne(c.Context(), increaseLog, nil); err != nil {
|
if err := s.StockLogsRepository.WithTx(tx).CreateOne(c.Context(), increaseLog, nil); err != nil {
|
||||||
s.Log.Errorf("Failed to create stock log increase: %+v", err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -373,10 +351,9 @@ func (s *transferService) CreateOne(c *fiber.Ctx, req *validation.TransferReques
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.Log.Errorf("Transaction failed in CreateOne: %+v", err)
|
s.Log.Errorf("Transaction failed in CreateOne: %+v", err)
|
||||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to process transfer transaction")
|
return nil, fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to process transfer transaction: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ambil data lengkap hasil create dengan GetOne (agar preload relasi sama dengan GetOne)
|
|
||||||
result, err := s.GetOne(c, uint(entityTransfer.Id))
|
result, err := s.GetOne(c, uint(entityTransfer.Id))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -390,7 +367,6 @@ func (s *transferService) getActiveProjectFlockKandangID(ctx context.Context, wa
|
|||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
return 0, fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Gudang dengan ID %d tidak ditemukan", warehouseID))
|
return 0, fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Gudang dengan ID %d tidak ditemukan", warehouseID))
|
||||||
}
|
}
|
||||||
s.Log.Errorf("Failed to get warehouse %d: %+v", warehouseID, err)
|
|
||||||
return 0, fiber.NewError(fiber.StatusInternalServerError, "Gagal mengambil data gudang")
|
return 0, fiber.NewError(fiber.StatusInternalServerError, "Gagal mengambil data gudang")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -403,7 +379,6 @@ func (s *transferService) getActiveProjectFlockKandangID(ctx context.Context, wa
|
|||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
return 0, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Kandang %d belum memiliki project flock aktif", *warehouse.KandangId))
|
return 0, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Kandang %d belum memiliki project flock aktif", *warehouse.KandangId))
|
||||||
}
|
}
|
||||||
s.Log.Errorf("Failed to get active project flock for kandang %d: %+v", *warehouse.KandangId, err)
|
|
||||||
return 0, fiber.NewError(fiber.StatusInternalServerError, "Gagal mengambil project flock kandang")
|
return 0, fiber.NewError(fiber.StatusInternalServerError, "Gagal mengambil project flock kandang")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -121,7 +121,7 @@ func ToMarketingDeliveryProductDTO(e entity.MarketingDeliveryProduct) MarketingD
|
|||||||
return MarketingDeliveryProductDTO{
|
return MarketingDeliveryProductDTO{
|
||||||
Id: e.Id,
|
Id: e.Id,
|
||||||
MarketingProductId: e.MarketingProductId,
|
MarketingProductId: e.MarketingProductId,
|
||||||
Qty: e.Qty,
|
Qty: e.UsageQty,
|
||||||
UnitPrice: e.UnitPrice,
|
UnitPrice: e.UnitPrice,
|
||||||
TotalWeight: e.TotalWeight,
|
TotalWeight: e.TotalWeight,
|
||||||
AvgWeight: e.AvgWeight,
|
AvgWeight: e.AvgWeight,
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package marketing
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/go-playground/validator/v10"
|
"github.com/go-playground/validator/v10"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
@@ -13,11 +14,12 @@ import (
|
|||||||
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/repositories"
|
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/repositories"
|
||||||
service "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/services"
|
service "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/services"
|
||||||
rCustomer "gitlab.com/mbugroup/lti-api.git/internal/modules/master/customers/repositories"
|
rCustomer "gitlab.com/mbugroup/lti-api.git/internal/modules/master/customers/repositories"
|
||||||
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
|
|
||||||
sUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
|
||||||
rWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/repositories"
|
rWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/repositories"
|
||||||
rProjectFlockKandang "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
|
rProjectFlockKandang "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
|
||||||
|
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
|
||||||
|
sUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/utils/fifo"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MarketingModule struct{}
|
type MarketingModule struct{}
|
||||||
@@ -31,6 +33,28 @@ func (MarketingModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
|
|||||||
customerRepo := rCustomer.NewCustomerRepository(db)
|
customerRepo := rCustomer.NewCustomerRepository(db)
|
||||||
productWarehouseRepo := rProductWarehouse.NewProductWarehouseRepository(db)
|
productWarehouseRepo := rProductWarehouse.NewProductWarehouseRepository(db)
|
||||||
|
|
||||||
|
// Initialize FIFO service
|
||||||
|
stockAllocationRepo := commonRepo.NewStockAllocationRepository(db)
|
||||||
|
fifoService := commonSvc.NewFifoService(db, stockAllocationRepo, productWarehouseRepo, utils.Log)
|
||||||
|
|
||||||
|
// Register marketing_delivery_products as FIFO Usable
|
||||||
|
// Note: ProductWarehouseID comes from marketing_products table via preload
|
||||||
|
if err := fifoService.RegisterUsable(fifo.UsableConfig{
|
||||||
|
Key: fifo.UsableKeyMarketingDelivery,
|
||||||
|
Table: "marketing_delivery_products",
|
||||||
|
Columns: fifo.UsableColumns{
|
||||||
|
ID: "id",
|
||||||
|
ProductWarehouseID: "product_warehouse_id", // Resolved from marketing_products via preload
|
||||||
|
UsageQuantity: "usage_qty",
|
||||||
|
PendingQuantity: "pending_qty",
|
||||||
|
CreatedAt: "created_at",
|
||||||
|
},
|
||||||
|
}); err != nil {
|
||||||
|
if !strings.Contains(strings.ToLower(err.Error()), "already registered") {
|
||||||
|
panic(fmt.Sprintf("failed to register marketing delivery usable workflow: %v", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize approval service
|
// Initialize approval service
|
||||||
approvalRepo := commonRepo.NewApprovalRepository(db)
|
approvalRepo := commonRepo.NewApprovalRepository(db)
|
||||||
approvalSvc := commonSvc.NewApprovalService(approvalRepo)
|
approvalSvc := commonSvc.NewApprovalService(approvalRepo)
|
||||||
@@ -42,9 +66,10 @@ func (MarketingModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
|
|||||||
|
|
||||||
warehouseRepo := rWarehouse.NewWarehouseRepository(db)
|
warehouseRepo := rWarehouse.NewWarehouseRepository(db)
|
||||||
projectFlockKandangRepo := rProjectFlockKandang.NewProjectFlockKandangRepository(db)
|
projectFlockKandangRepo := rProjectFlockKandang.NewProjectFlockKandangRepository(db)
|
||||||
|
|
||||||
// Initialize services
|
// Initialize services
|
||||||
salesOrdersService := service.NewSalesOrdersService(marketingRepo, customerRepo, productWarehouseRepo, userRepo, approvalSvc,warehouseRepo,projectFlockKandangRepo, validate)
|
salesOrdersService := service.NewSalesOrdersService(marketingRepo, customerRepo, productWarehouseRepo, userRepo, approvalSvc, warehouseRepo, projectFlockKandangRepo, validate)
|
||||||
deliveryOrdersService := service.NewDeliveryOrdersService(marketingRepo, marketingProductRepo, marketingDeliveryProductRepo, approvalSvc, validate)
|
deliveryOrdersService := service.NewDeliveryOrdersService(marketingRepo, marketingProductRepo, marketingDeliveryProductRepo, approvalSvc, fifoService, validate)
|
||||||
userService := sUser.NewUserService(userRepo, validate)
|
userService := sUser.NewUserService(userRepo, validate)
|
||||||
|
|
||||||
// Register routes
|
// Register routes
|
||||||
|
|||||||
@@ -2,9 +2,12 @@ package repository
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
|
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/repports/validations"
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -13,6 +16,10 @@ type MarketingDeliveryProductRepository interface {
|
|||||||
GetDeliveryProductsByProjectFlockID(ctx context.Context, projectFlockID uint, callback func(*gorm.DB) *gorm.DB) ([]entity.MarketingDeliveryProduct, error)
|
GetDeliveryProductsByProjectFlockID(ctx context.Context, projectFlockID uint, callback func(*gorm.DB) *gorm.DB) ([]entity.MarketingDeliveryProduct, error)
|
||||||
GetByMarketingId(ctx context.Context, marketingId uint) ([]entity.MarketingDeliveryProduct, error)
|
GetByMarketingId(ctx context.Context, marketingId uint) ([]entity.MarketingDeliveryProduct, error)
|
||||||
GetByMarketingProductID(ctx context.Context, marketingProductID uint) (*entity.MarketingDeliveryProduct, error)
|
GetByMarketingProductID(ctx context.Context, marketingProductID uint) (*entity.MarketingDeliveryProduct, error)
|
||||||
|
GetAllWithFilters(ctx context.Context, offset, limit int, filters *validation.MarketingQuery) ([]entity.MarketingDeliveryProduct, int64, error)
|
||||||
|
UpdateFifoFields(ctx context.Context, id uint, usageQty, pendingQty float64) error
|
||||||
|
GetUsageQty(ctx context.Context, id uint) (float64, error)
|
||||||
|
ResetFifoFields(ctx context.Context, id uint) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type MarketingDeliveryProductRepositoryImpl struct {
|
type MarketingDeliveryProductRepositoryImpl struct {
|
||||||
@@ -28,8 +35,6 @@ func NewMarketingDeliveryProductRepository(db *gorm.DB) MarketingDeliveryProduct
|
|||||||
func (r *MarketingDeliveryProductRepositoryImpl) GetDeliveryProductsByProjectFlockID(ctx context.Context, projectFlockID uint, callback func(*gorm.DB) *gorm.DB) ([]entity.MarketingDeliveryProduct, error) {
|
func (r *MarketingDeliveryProductRepositoryImpl) GetDeliveryProductsByProjectFlockID(ctx context.Context, projectFlockID uint, callback func(*gorm.DB) *gorm.DB) ([]entity.MarketingDeliveryProduct, error) {
|
||||||
var deliveryProducts []entity.MarketingDeliveryProduct
|
var deliveryProducts []entity.MarketingDeliveryProduct
|
||||||
|
|
||||||
// JOIN digunakan untuk filter WHERE clause ke ProjectFlockID yang berada 3 level relasi atas
|
|
||||||
// Entity relations digunakan di Preload (callback) untuk load data, bukan untuk filter
|
|
||||||
db := r.DB().WithContext(ctx).
|
db := r.DB().WithContext(ctx).
|
||||||
Joins("JOIN marketing_products ON marketing_products.id = marketing_delivery_products.marketing_product_id").
|
Joins("JOIN marketing_products ON marketing_products.id = marketing_delivery_products.marketing_product_id").
|
||||||
Joins("JOIN product_warehouses ON product_warehouses.id = marketing_products.product_warehouse_id").
|
Joins("JOIN product_warehouses ON product_warehouses.id = marketing_products.product_warehouse_id").
|
||||||
@@ -74,3 +79,198 @@ func (r *MarketingDeliveryProductRepositoryImpl) GetByMarketingProductID(ctx con
|
|||||||
|
|
||||||
return &deliveryProduct, nil
|
return &deliveryProduct, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *MarketingDeliveryProductRepositoryImpl) GetAllWithFilters(ctx context.Context, offset, limit int, filters *validation.MarketingQuery) ([]entity.MarketingDeliveryProduct, int64, error) {
|
||||||
|
var deliveryProducts []entity.MarketingDeliveryProduct
|
||||||
|
var total int64
|
||||||
|
|
||||||
|
db := r.DB().WithContext(ctx).
|
||||||
|
Model(&entity.MarketingDeliveryProduct{}).
|
||||||
|
Preload("MarketingProduct", func(db *gorm.DB) *gorm.DB {
|
||||||
|
return db.
|
||||||
|
Preload("Marketing").
|
||||||
|
Preload("Marketing.Customer").
|
||||||
|
Preload("Marketing.SalesPerson").
|
||||||
|
Preload("ProductWarehouse").
|
||||||
|
Preload("ProductWarehouse.Product").
|
||||||
|
Preload("ProductWarehouse.Product.Flags").
|
||||||
|
Preload("ProductWarehouse.Warehouse").
|
||||||
|
Preload("ProductWarehouse.ProjectFlockKandang").
|
||||||
|
Preload("ProductWarehouse.ProjectFlockKandang.ProjectFlock")
|
||||||
|
}).
|
||||||
|
Joins("JOIN marketing_products ON marketing_products.id = marketing_delivery_products.marketing_product_id").
|
||||||
|
Joins("JOIN marketings ON marketings.id = marketing_products.marketing_id")
|
||||||
|
|
||||||
|
if filters.ProductId > 0 || filters.WarehouseId > 0 || filters.Search != "" {
|
||||||
|
db = db.Joins("LEFT JOIN product_warehouses ON product_warehouses.id = marketing_products.product_warehouse_id")
|
||||||
|
}
|
||||||
|
|
||||||
|
if filters.ProductId > 0 || filters.Search != "" {
|
||||||
|
db = db.Joins("LEFT JOIN products ON products.id = product_warehouses.product_id")
|
||||||
|
}
|
||||||
|
|
||||||
|
if filters.WarehouseId > 0 {
|
||||||
|
db = db.Joins("LEFT JOIN warehouses ON warehouses.id = product_warehouses.warehouse_id")
|
||||||
|
}
|
||||||
|
|
||||||
|
if filters.Search != "" {
|
||||||
|
db = db.Joins("LEFT JOIN customers ON customers.id = marketings.customer_id")
|
||||||
|
}
|
||||||
|
|
||||||
|
if filters.Search != "" {
|
||||||
|
searchPattern := "%" + filters.Search + "%"
|
||||||
|
db = db.Where("marketing_delivery_products.vehicle_number ILIKE ? OR marketings.so_number ILIKE ? OR customers.name ILIKE ? OR products.name ILIKE ?",
|
||||||
|
searchPattern, searchPattern, searchPattern, searchPattern)
|
||||||
|
}
|
||||||
|
|
||||||
|
if filters.CustomerId > 0 {
|
||||||
|
db = db.Where("marketings.customer_id = ?", filters.CustomerId)
|
||||||
|
}
|
||||||
|
|
||||||
|
if filters.SalesPersonId > 0 {
|
||||||
|
db = db.Where("marketings.sales_person_id = ?", filters.SalesPersonId)
|
||||||
|
}
|
||||||
|
|
||||||
|
if filters.ProductId > 0 {
|
||||||
|
db = db.Where("product_warehouses.product_id = ?", filters.ProductId)
|
||||||
|
}
|
||||||
|
|
||||||
|
if filters.WarehouseId > 0 {
|
||||||
|
db = db.Where("product_warehouses.warehouse_id = ?", filters.WarehouseId)
|
||||||
|
}
|
||||||
|
|
||||||
|
if filters.FilterBy != "" && (filters.StartDate != "" || filters.EndDate != "") {
|
||||||
|
if filters.FilterBy == "so_date" {
|
||||||
|
if filters.StartDate != "" {
|
||||||
|
if startDate, err := utils.ParseDateString(filters.StartDate); err == nil {
|
||||||
|
db = db.Where("marketings.so_date >= ?", startDate)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if filters.EndDate != "" {
|
||||||
|
if endDate, err := utils.ParseDateString(filters.EndDate); err == nil {
|
||||||
|
nextDate := endDate.AddDate(0, 0, 1)
|
||||||
|
db = db.Where("marketings.so_date < ?", nextDate)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if filters.FilterBy == "realization_date" {
|
||||||
|
if filters.StartDate != "" {
|
||||||
|
if startDate, err := utils.ParseDateString(filters.StartDate); err == nil {
|
||||||
|
db = db.Where("marketing_delivery_products.delivery_date >= ?", startDate)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if filters.EndDate != "" {
|
||||||
|
if endDate, err := utils.ParseDateString(filters.EndDate); err == nil {
|
||||||
|
nextDate := endDate.AddDate(0, 0, 1)
|
||||||
|
db = db.Where("marketing_delivery_products.delivery_date < ?", nextDate)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sortColumn := "marketing_delivery_products.id"
|
||||||
|
sortOrder := "DESC"
|
||||||
|
|
||||||
|
if filters.SortBy != "" {
|
||||||
|
switch filters.SortBy {
|
||||||
|
case "so_date":
|
||||||
|
sortColumn = "marketings.so_date"
|
||||||
|
case "realization_date":
|
||||||
|
sortColumn = "marketing_delivery_products.delivery_date"
|
||||||
|
case "customer":
|
||||||
|
sortColumn = "customers.name"
|
||||||
|
if !containsJoin(db, "customers") {
|
||||||
|
db = db.Joins("LEFT JOIN customers ON customers.id = marketings.customer_id")
|
||||||
|
}
|
||||||
|
case "warehouse":
|
||||||
|
sortColumn = "warehouses.name"
|
||||||
|
if !containsJoin(db, "warehouses") {
|
||||||
|
db = db.Joins("LEFT JOIN product_warehouses ON product_warehouses.id = marketing_products.product_warehouse_id").
|
||||||
|
Joins("LEFT JOIN warehouses ON warehouses.id = product_warehouses.warehouse_id")
|
||||||
|
}
|
||||||
|
case "product":
|
||||||
|
sortColumn = "products.name"
|
||||||
|
if !containsJoin(db, "products") {
|
||||||
|
db = db.Joins("LEFT JOIN product_warehouses ON product_warehouses.id = marketing_products.product_warehouse_id").
|
||||||
|
Joins("LEFT JOIN products ON products.id = product_warehouses.product_id")
|
||||||
|
}
|
||||||
|
case "sales_person":
|
||||||
|
sortColumn = "sales_users.name"
|
||||||
|
if !containsJoin(db, "sales_users") {
|
||||||
|
db = db.Joins("LEFT JOIN users AS sales_users ON sales_users.id = marketings.sales_person_id")
|
||||||
|
}
|
||||||
|
case "vehicle_number":
|
||||||
|
sortColumn = "marketing_delivery_products.vehicle_number"
|
||||||
|
case "sales_amount":
|
||||||
|
sortColumn = "marketing_delivery_products.total_price"
|
||||||
|
case "hpp_amount":
|
||||||
|
sortColumn = "marketing_delivery_products.total_price"
|
||||||
|
case "qty":
|
||||||
|
sortColumn = "marketing_delivery_products.qty"
|
||||||
|
case "average_weight":
|
||||||
|
sortColumn = "marketing_delivery_products.avg_weight"
|
||||||
|
case "total_weight":
|
||||||
|
sortColumn = "marketing_delivery_products.total_weight"
|
||||||
|
case "sales_price":
|
||||||
|
sortColumn = "marketing_delivery_products.unit_price"
|
||||||
|
case "hpp_price":
|
||||||
|
sortColumn = "marketing_delivery_products.unit_price"
|
||||||
|
case "aging_days":
|
||||||
|
sortColumn = "marketing_delivery_products.delivery_date"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if filters.SortOrder != "" && (filters.SortOrder == "asc" || filters.SortOrder == "desc") {
|
||||||
|
sortOrder = strings.ToUpper(filters.SortOrder)
|
||||||
|
}
|
||||||
|
|
||||||
|
db = db.Order(sortColumn + " " + sortOrder)
|
||||||
|
|
||||||
|
if err := db.Count(&total).Error; err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := db.
|
||||||
|
Offset(offset).
|
||||||
|
Limit(limit).
|
||||||
|
Find(&deliveryProducts).Error; err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return deliveryProducts, total, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func containsJoin(db *gorm.DB, tableName string) bool {
|
||||||
|
statement := db.Statement
|
||||||
|
joinSQL := statement.SQL.String()
|
||||||
|
return strings.Contains(joinSQL, "JOIN "+tableName)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *MarketingDeliveryProductRepositoryImpl) UpdateFifoFields(ctx context.Context, id uint, usageQty, pendingQty float64) error {
|
||||||
|
return r.DB().WithContext(ctx).
|
||||||
|
Model(&entity.MarketingDeliveryProduct{}).
|
||||||
|
Where("id = ?", id).
|
||||||
|
Updates(map[string]interface{}{
|
||||||
|
"usage_qty": usageQty,
|
||||||
|
"pending_qty": pendingQty,
|
||||||
|
}).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *MarketingDeliveryProductRepositoryImpl) GetUsageQty(ctx context.Context, id uint) (float64, error) {
|
||||||
|
var usageQty float64
|
||||||
|
err := r.DB().WithContext(ctx).
|
||||||
|
Model(&entity.MarketingDeliveryProduct{}).
|
||||||
|
Where("id = ?", id).
|
||||||
|
Select("usage_qty").
|
||||||
|
Scan(&usageQty).Error
|
||||||
|
return usageQty, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *MarketingDeliveryProductRepositoryImpl) ResetFifoFields(ctx context.Context, id uint) error {
|
||||||
|
return r.DB().WithContext(ctx).
|
||||||
|
Model(&entity.MarketingDeliveryProduct{}).
|
||||||
|
Where("id = ?", id).
|
||||||
|
Updates(map[string]interface{}{
|
||||||
|
"usage_qty": 0,
|
||||||
|
"pending_qty": 0,
|
||||||
|
}).Error
|
||||||
|
}
|
||||||
|
|||||||
@@ -16,16 +16,15 @@ func RegisterRoutes(router fiber.Router, userService user.UserService, salesOrde
|
|||||||
route := router.Group("/marketing")
|
route := router.Group("/marketing")
|
||||||
route.Use(m.Auth(userService))
|
route.Use(m.Auth(userService))
|
||||||
|
|
||||||
route.Get("/", deliveryOrdersCtrl.GetAll)
|
route.Get("/", m.RequirePermissions(m.P_DeliveryGetAll), deliveryOrdersCtrl.GetAll)
|
||||||
route.Get("/:id", deliveryOrdersCtrl.GetOne)
|
route.Get("/:id", m.RequirePermissions(m.P_DeliveryGetOne), deliveryOrdersCtrl.GetOne)
|
||||||
route.Delete("/:id", salesOrdersCtrl.DeleteOne)
|
route.Delete("/:id", m.RequirePermissions(m.P_SalesOrderDelete), salesOrdersCtrl.DeleteOne)
|
||||||
|
|
||||||
route.Post("/sales-orders", salesOrdersCtrl.CreateOne)
|
route.Post("/sales-orders", m.RequirePermissions(m.P_SalesOrderCreateOne), salesOrdersCtrl.CreateOne)
|
||||||
route.Patch("/sales-orders/:id", salesOrdersCtrl.UpdateOne)
|
route.Patch("/sales-orders/:id", m.RequirePermissions(m.P_SalesOrderUpdateOne), salesOrdersCtrl.UpdateOne)
|
||||||
route.Post("/sales-orders/approvals", salesOrdersCtrl.Approval)
|
route.Post("/sales-orders/approvals", m.RequirePermissions(m.P_SalesOrderApproval), salesOrdersCtrl.Approval)
|
||||||
|
|
||||||
|
route.Post("/delivery-orders", m.RequirePermissions(m.P_DeliveryCreateOne), deliveryOrdersCtrl.CreateOne)
|
||||||
|
route.Patch("/delivery-orders/:id", m.RequirePermissions(m.P_DeliveryUpdateOne), deliveryOrdersCtrl.UpdateOne)
|
||||||
|
|
||||||
route.Get("/delivery-orders", deliveryOrdersCtrl.GetAll)
|
|
||||||
route.Get("/delivery-orders/:id", deliveryOrdersCtrl.GetOne)
|
|
||||||
route.Post("/delivery-orders", deliveryOrdersCtrl.CreateOne)
|
|
||||||
route.Patch("/delivery-orders/:id", deliveryOrdersCtrl.UpdateOne)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,10 +15,10 @@ import (
|
|||||||
marketingRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/repositories"
|
marketingRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/repositories"
|
||||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/validations"
|
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/validations"
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/utils/fifo"
|
||||||
|
|
||||||
"github.com/go-playground/validator/v10"
|
"github.com/go-playground/validator/v10"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -30,12 +30,12 @@ type DeliveryOrdersService interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type deliveryOrdersService struct {
|
type deliveryOrdersService struct {
|
||||||
Log *logrus.Logger
|
|
||||||
Validate *validator.Validate
|
Validate *validator.Validate
|
||||||
MarketingRepo marketingRepo.MarketingRepository
|
MarketingRepo marketingRepo.MarketingRepository
|
||||||
MarketingProductRepo marketingRepo.MarketingProductRepository
|
MarketingProductRepo marketingRepo.MarketingProductRepository
|
||||||
MarketingDeliveryProductRepo marketingRepo.MarketingDeliveryProductRepository
|
MarketingDeliveryProductRepo marketingRepo.MarketingDeliveryProductRepository
|
||||||
ApprovalSvc commonSvc.ApprovalService
|
ApprovalSvc commonSvc.ApprovalService
|
||||||
|
FifoSvc commonSvc.FifoService
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDeliveryOrdersService(
|
func NewDeliveryOrdersService(
|
||||||
@@ -43,15 +43,16 @@ func NewDeliveryOrdersService(
|
|||||||
marketingProductRepo marketingRepo.MarketingProductRepository,
|
marketingProductRepo marketingRepo.MarketingProductRepository,
|
||||||
marketingDeliveryProductRepo marketingRepo.MarketingDeliveryProductRepository,
|
marketingDeliveryProductRepo marketingRepo.MarketingDeliveryProductRepository,
|
||||||
approvalSvc commonSvc.ApprovalService,
|
approvalSvc commonSvc.ApprovalService,
|
||||||
|
fifoSvc commonSvc.FifoService,
|
||||||
validate *validator.Validate,
|
validate *validator.Validate,
|
||||||
) DeliveryOrdersService {
|
) DeliveryOrdersService {
|
||||||
return &deliveryOrdersService{
|
return &deliveryOrdersService{
|
||||||
Log: utils.Log,
|
|
||||||
Validate: validate,
|
Validate: validate,
|
||||||
MarketingRepo: marketingRepo,
|
MarketingRepo: marketingRepo,
|
||||||
MarketingProductRepo: marketingProductRepo,
|
MarketingProductRepo: marketingProductRepo,
|
||||||
MarketingDeliveryProductRepo: marketingDeliveryProductRepo,
|
MarketingDeliveryProductRepo: marketingDeliveryProductRepo,
|
||||||
ApprovalSvc: approvalSvc,
|
ApprovalSvc: approvalSvc,
|
||||||
|
FifoSvc: fifoSvc,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,7 +109,6 @@ func (s deliveryOrdersService) GetAll(c *fiber.Ctx, params *validation.DeliveryO
|
|||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.Log.Errorf("Failed to get marketings: %+v", err)
|
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
for i := range marketings {
|
for i := range marketings {
|
||||||
@@ -116,7 +116,7 @@ func (s deliveryOrdersService) GetAll(c *fiber.Ctx, params *validation.DeliveryO
|
|||||||
return db.Preload("ActionUser")
|
return db.Preload("ActionUser")
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.Log.Warnf("Failed to load approval for marketing %d: %+v", marketings[i].Id, err)
|
continue
|
||||||
}
|
}
|
||||||
marketings[i].LatestApproval = latestApproval
|
marketings[i].LatestApproval = latestApproval
|
||||||
}
|
}
|
||||||
@@ -247,7 +247,7 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Delivery
|
|||||||
itemDeliveryDate = &parsedDate
|
itemDeliveryDate = &parsedDate
|
||||||
}
|
}
|
||||||
|
|
||||||
deliveryProduct.Qty = requestedProduct.Qty
|
deliveryProduct.ProductWarehouseId = foundMarketingProduct.ProductWarehouseId
|
||||||
deliveryProduct.UnitPrice = requestedProduct.UnitPrice
|
deliveryProduct.UnitPrice = requestedProduct.UnitPrice
|
||||||
deliveryProduct.AvgWeight = requestedProduct.AvgWeight
|
deliveryProduct.AvgWeight = requestedProduct.AvgWeight
|
||||||
deliveryProduct.TotalWeight = requestedProduct.TotalWeight
|
deliveryProduct.TotalWeight = requestedProduct.TotalWeight
|
||||||
@@ -256,7 +256,8 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Delivery
|
|||||||
deliveryProduct.VehicleNumber = requestedProduct.VehicleNumber
|
deliveryProduct.VehicleNumber = requestedProduct.VehicleNumber
|
||||||
|
|
||||||
if requestedProduct.Qty > 0 {
|
if requestedProduct.Qty > 0 {
|
||||||
if err := s.validateAndReduceProductWarehouse(c.Context(), dbTransaction, foundMarketingProduct, requestedProduct.Qty); err != nil {
|
|
||||||
|
if err := s.consumeDeliveryStock(c.Context(), dbTransaction, deliveryProduct, foundMarketingProduct, requestedProduct.Qty); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -354,8 +355,9 @@ func (s deliveryOrdersService) UpdateOne(c *fiber.Ctx, req *validation.DeliveryO
|
|||||||
itemDeliveryDate = deliveryProduct.DeliveryDate
|
itemDeliveryDate = deliveryProduct.DeliveryDate
|
||||||
}
|
}
|
||||||
|
|
||||||
oldQty := deliveryProduct.Qty
|
oldRequestedQty := deliveryProduct.UsageQty + deliveryProduct.PendingQty
|
||||||
deliveryProduct.Qty = requestedProduct.Qty
|
|
||||||
|
deliveryProduct.ProductWarehouseId = foundMarketingProduct.ProductWarehouseId
|
||||||
deliveryProduct.UnitPrice = requestedProduct.UnitPrice
|
deliveryProduct.UnitPrice = requestedProduct.UnitPrice
|
||||||
deliveryProduct.AvgWeight = requestedProduct.AvgWeight
|
deliveryProduct.AvgWeight = requestedProduct.AvgWeight
|
||||||
deliveryProduct.TotalWeight = requestedProduct.TotalWeight
|
deliveryProduct.TotalWeight = requestedProduct.TotalWeight
|
||||||
@@ -363,14 +365,18 @@ func (s deliveryOrdersService) UpdateOne(c *fiber.Ctx, req *validation.DeliveryO
|
|||||||
deliveryProduct.DeliveryDate = itemDeliveryDate
|
deliveryProduct.DeliveryDate = itemDeliveryDate
|
||||||
deliveryProduct.VehicleNumber = requestedProduct.VehicleNumber
|
deliveryProduct.VehicleNumber = requestedProduct.VehicleNumber
|
||||||
|
|
||||||
qtyChange := requestedProduct.Qty - oldQty
|
if requestedProduct.Qty != oldRequestedQty {
|
||||||
if qtyChange > 0 {
|
|
||||||
if err := s.validateAndReduceProductWarehouse(c.Context(), dbTransaction, foundMarketingProduct, qtyChange); err != nil {
|
if oldRequestedQty > 0 {
|
||||||
return err
|
if err := s.releaseDeliveryStock(c.Context(), dbTransaction, deliveryProduct, foundMarketingProduct); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if qtyChange < 0 {
|
|
||||||
if err := s.restoreProductWarehouseStock(c.Context(), dbTransaction, foundMarketingProduct, -qtyChange); err != nil {
|
if requestedProduct.Qty > 0 {
|
||||||
return err
|
if err := s.consumeDeliveryStock(c.Context(), dbTransaction, deliveryProduct, foundMarketingProduct, requestedProduct.Qty); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -393,50 +399,79 @@ func (s deliveryOrdersService) UpdateOne(c *fiber.Ctx, req *validation.DeliveryO
|
|||||||
return s.getMarketingWithDeliveries(c, id)
|
return s.getMarketingWithDeliveries(c, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s deliveryOrdersService) validateAndReduceProductWarehouse(ctx context.Context, tx *gorm.DB, marketingProduct *entity.MarketingProduct, qtyDeliver float64) error {
|
func (s deliveryOrdersService) consumeDeliveryStock(ctx context.Context, tx *gorm.DB, deliveryProduct *entity.MarketingDeliveryProduct, marketingProduct *entity.MarketingProduct, requestedQty float64) error {
|
||||||
if marketingProduct == nil || marketingProduct.ProductWarehouseId == 0 {
|
if marketingProduct == nil || marketingProduct.ProductWarehouseId == 0 {
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, "Product warehouse not found")
|
return fiber.NewError(fiber.StatusInternalServerError, "Product warehouse not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
pwRepo := productWarehouseRepo.NewProductWarehouseRepository(tx)
|
if deliveryProduct == nil || deliveryProduct.Id == 0 {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Delivery product not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := s.FifoSvc.Consume(ctx, commonSvc.StockConsumeRequest{
|
||||||
|
UsableKey: fifo.UsableKeyMarketingDelivery,
|
||||||
|
UsableID: deliveryProduct.Id,
|
||||||
|
ProductWarehouseID: marketingProduct.ProductWarehouseId,
|
||||||
|
Quantity: requestedQty,
|
||||||
|
AllowPending: false,
|
||||||
|
Tx: tx,
|
||||||
|
})
|
||||||
|
|
||||||
|
deliveryProductRepo := marketingRepo.NewMarketingDeliveryProductRepository(tx)
|
||||||
|
|
||||||
pw, err := pwRepo.GetByID(ctx, marketingProduct.ProductWarehouseId, nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
pwRepo := productWarehouseRepo.NewProductWarehouseRepository(tx)
|
||||||
return fiber.NewError(fiber.StatusNotFound, "Product warehouse not found")
|
pw, err2 := pwRepo.GetByID(ctx, marketingProduct.ProductWarehouseId, nil)
|
||||||
|
if err2 != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to check product warehouse stock")
|
||||||
}
|
}
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to check stock")
|
|
||||||
|
if pw == nil || pw.Quantity < requestedQty {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Insufficient stock. Available: %.2f, Requested: %.2f", func() float64 { if pw != nil { return pw.Quantity } else { return 0 } }(), requestedQty))
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := deliveryProductRepo.UpdateFifoFields(ctx, deliveryProduct.Id, requestedQty, 0); err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update delivery product")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if pw.Quantity < qtyDeliver {
|
if err := deliveryProductRepo.UpdateFifoFields(ctx, deliveryProduct.Id, result.UsageQuantity, result.PendingQuantity); err != nil {
|
||||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Insufficient stock for warehouse - available: %.2f, requested: %.2f", pw.Quantity, qtyDeliver))
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update delivery product")
|
||||||
}
|
}
|
||||||
|
|
||||||
pw.Quantity = pw.Quantity - qtyDeliver
|
|
||||||
if err := pwRepo.UpdateOne(ctx, pw.Id, pw, nil); err != nil {
|
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update stock")
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s deliveryOrdersService) restoreProductWarehouseStock(ctx context.Context, tx *gorm.DB, marketingProduct *entity.MarketingProduct, qtyRestore float64) error {
|
func (s deliveryOrdersService) releaseDeliveryStock(ctx context.Context, tx *gorm.DB, deliveryProduct *entity.MarketingDeliveryProduct, marketingProduct *entity.MarketingProduct) error {
|
||||||
|
if deliveryProduct == nil || deliveryProduct.Id == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
if marketingProduct == nil || marketingProduct.ProductWarehouseId == 0 {
|
if marketingProduct == nil || marketingProduct.ProductWarehouseId == 0 {
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, "Product warehouse not found")
|
return fiber.NewError(fiber.StatusInternalServerError, "Product warehouse not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
pwRepo := productWarehouseRepo.NewProductWarehouseRepository(tx)
|
deliveryProductRepo := marketingRepo.NewMarketingDeliveryProductRepository(tx)
|
||||||
pw, err := pwRepo.GetByID(ctx, marketingProduct.ProductWarehouseId, nil)
|
currentUsage, err := deliveryProductRepo.GetUsageQty(ctx, deliveryProduct.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
currentUsage = 0
|
||||||
return fiber.NewError(fiber.StatusNotFound, "Product warehouse not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to check stock")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pw.Quantity = pw.Quantity + qtyRestore
|
if currentUsage == 0 {
|
||||||
if err := pwRepo.UpdateOne(ctx, pw.Id, pw, nil); err != nil {
|
return nil
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update stock")
|
}
|
||||||
|
|
||||||
|
if err := s.FifoSvc.ReleaseUsage(ctx, commonSvc.StockReleaseRequest{
|
||||||
|
UsableKey: fifo.UsableKeyMarketingDelivery,
|
||||||
|
UsableID: deliveryProduct.Id,
|
||||||
|
Tx: tx,
|
||||||
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := deliveryProductRepo.ResetFifoFields(ctx, deliveryProduct.Id); err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -307,15 +307,17 @@ func (s salesOrdersService) UpdateOne(c *fiber.Ctx, req *validation.Update, id u
|
|||||||
|
|
||||||
if _, err := invDeliveryRepoTx.GetByMarketingProductID(c.Context(), old.Id); err != nil {
|
if _, err := invDeliveryRepoTx.GetByMarketingProductID(c.Context(), old.Id); err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
|
||||||
mdp := &entity.MarketingDeliveryProduct{
|
mdp := &entity.MarketingDeliveryProduct{
|
||||||
MarketingProductId: old.Id,
|
MarketingProductId: old.Id,
|
||||||
Qty: 0,
|
|
||||||
UnitPrice: 0,
|
UnitPrice: 0,
|
||||||
TotalWeight: 0,
|
TotalWeight: 0,
|
||||||
AvgWeight: 0,
|
AvgWeight: 0,
|
||||||
TotalPrice: 0,
|
TotalPrice: 0,
|
||||||
DeliveryDate: nil,
|
DeliveryDate: nil,
|
||||||
VehicleNumber: rp.VehicleNumber,
|
VehicleNumber: rp.VehicleNumber,
|
||||||
|
UsageQty: 0,
|
||||||
|
PendingQty: 0,
|
||||||
}
|
}
|
||||||
if err := invDeliveryRepoTx.CreateOne(c.Context(), mdp, nil); err != nil {
|
if err := invDeliveryRepoTx.CreateOne(c.Context(), mdp, nil); err != nil {
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create marketing delivery product")
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create marketing delivery product")
|
||||||
@@ -340,7 +342,7 @@ func (s salesOrdersService) UpdateOne(c *fiber.Ctx, req *validation.Update, id u
|
|||||||
}
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|
||||||
if deliveryProduct.DeliveryDate != nil || deliveryProduct.Qty > 0 {
|
if deliveryProduct.DeliveryDate != nil || deliveryProduct.UsageQty > 0 || deliveryProduct.PendingQty > 0 {
|
||||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Cannot delete marketing product %d because it has delivery records", old.Id))
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Cannot delete marketing product %d because it has delivery records", old.Id))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -602,13 +604,14 @@ func (s *salesOrdersService) createMarketingProductWithDelivery(ctx context.Cont
|
|||||||
|
|
||||||
marketingDeliveryProduct := &entity.MarketingDeliveryProduct{
|
marketingDeliveryProduct := &entity.MarketingDeliveryProduct{
|
||||||
MarketingProductId: marketingProduct.Id,
|
MarketingProductId: marketingProduct.Id,
|
||||||
Qty: 0,
|
|
||||||
UnitPrice: 0,
|
UnitPrice: 0,
|
||||||
TotalWeight: 0,
|
TotalWeight: 0,
|
||||||
AvgWeight: 0,
|
AvgWeight: 0,
|
||||||
TotalPrice: 0,
|
TotalPrice: 0,
|
||||||
DeliveryDate: nil,
|
DeliveryDate: nil,
|
||||||
VehicleNumber: rp.VehicleNumber,
|
VehicleNumber: rp.VehicleNumber,
|
||||||
|
UsageQty: 0,
|
||||||
|
PendingQty: 0,
|
||||||
}
|
}
|
||||||
if err := invDeliveryRepo.CreateOne(ctx, marketingDeliveryProduct, nil); err != nil {
|
if err := invDeliveryRepo.CreateOne(ctx, marketingDeliveryProduct, nil); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -15,9 +15,9 @@ func AreaRoutes(v1 fiber.Router, u user.UserService, s area.AreaService) {
|
|||||||
route := v1.Group("/areas")
|
route := v1.Group("/areas")
|
||||||
route.Use(m.Auth(u))
|
route.Use(m.Auth(u))
|
||||||
|
|
||||||
route.Get("/", ctrl.GetAll)
|
route.Get("/",m.RequirePermissions(m.P_AreaGetAll), ctrl.GetAll)
|
||||||
route.Post("/", ctrl.CreateOne)
|
route.Post("/",m.RequirePermissions(m.P_AreaCreateOne), ctrl.CreateOne)
|
||||||
route.Get("/:id", ctrl.GetOne)
|
route.Get("/:id",m.RequirePermissions(m.P_AreaGetOne), ctrl.GetOne)
|
||||||
route.Patch("/:id", ctrl.UpdateOne)
|
route.Patch("/:id",m.RequirePermissions(m.P_AreaUpdateOne), ctrl.UpdateOne)
|
||||||
route.Delete("/:id", ctrl.DeleteOne)
|
route.Delete("/:id",m.RequirePermissions(m.P_AreaDeleteOne), ctrl.DeleteOne)
|
||||||
}
|
}
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user