Compare commits

...

46 Commits

Author SHA1 Message Date
MacBook Air M1 c729067ab5 adjust validate pactch daily checklist 2026-01-08 15:08:40 +07:00
MacBook Air M1 f079bee92a adjust get all phase activity 2026-01-08 13:37:53 +07:00
MacBook Air M1 f1f7edb9ab Adjust limit for get all employee 2026-01-08 11:50:34 +07:00
MacBook Air M1 a4840fc98a add master data config checklist 2026-01-07 21:37:51 +07:00
MacBook Air M1 c3f8ae5887 add api daily checklist report 2026-01-07 17:39:17 +07:00
MacBook Air M1 e545047165 add api summary and update status 2026-01-07 12:02:44 +07:00
MacBook Air M1 42aa6829c5 add api detail daily checklist 2026-01-07 10:36:40 +07:00
MacBook Air M1 dded9e807b add api check uncheck assignment 2026-01-06 23:59:16 +07:00
MacBook Air M1 3bd0602525 add daily checklist module;adjust master data;adjust migration 2026-01-06 17:03:55 +07:00
MacBook Air M1 b1996be24c add module master phase activity 2026-01-05 22:25:46 +07:00
MacBook Air M1 4a08be1f55 add module master data phases 2026-01-05 19:59:03 +07:00
MacBook Air M1 9f840f2650 adjust patch employee 2026-01-05 17:49:44 +07:00
MacBook Air M1 80109b77db adjust api get all employees 2026-01-05 17:32:41 +07:00
MacBook Air M1 df504e3ff0 add migration;add api create employee 2026-01-05 17:17:25 +07:00
Hafizh A. Y. c1a162b4d4 Merge branch 'fix/sapronak-dev' into 'development'
adjust api closing tap sapronak

See merge request mbugroup/lti-api!130
2026-01-03 18:04:50 +00:00
Hafizh A. Y. 1101879039 Merge branch 'fix/BE-Document_s3' into 'development'
feat(BE): fix fifo system recording and uniformity dto

See merge request mbugroup/lti-api!129
2026-01-03 18:04:24 +00:00
ragilap 8de33a0f24 feat(BE): fix delete project flock budget and uniformity, and fix uniformity with update purchase document 2026-01-02 20:43:57 +07:00
MacBook Air M1 1348483b1c adjust api closing tap sapronak 2026-01-02 13:19:11 +07:00
ragilap 2f8f84cb0d Merge branch 'development' of https://gitlab.com/mbugroup/lti-api into fix/BE-Document_s3 2026-01-02 12:05:28 +07:00
ragilap cc5a58b6d1 feat(BE): sso delete and fix response too many request 2026-01-02 12:04:50 +07:00
ragilap fe51f33ab4 feat(BE): fixing fifo system recording 2025-12-31 19:30:04 +07:00
ragilap e0dd2799fc feat(BE): fix fifo system recording and uniformity dto 2025-12-31 15:10:06 +07:00
Hafizh A. Y. 394eb0f363 Merge branch 'dev/fix-route' into 'development'
fix rename route api closing data production

See merge request mbugroup/lti-api!127
2025-12-31 06:16:55 +00:00
MacBook Air M1 47d497d6b0 fix rename route api closing data production 2025-12-31 13:15:02 +07:00
Hafizh A. Y. 3a1a2b436d Merge branch 'fix/BE-Document_s3' into 'development'
feat(BE): add function read and download in document

See merge request mbugroup/lti-api!126
2025-12-31 04:41:53 +00:00
ragilap 9d285869f5 feat(BE): add function read and download in document 2025-12-31 11:39:53 +07:00
Hafizh A. Y. 4e2724a702 Merge branch 'dev/teguh' into 'development'
feat[BE]: add get all nonstock by supplier id on nonstock get all

See merge request mbugroup/lti-api!125
2025-12-31 03:02:48 +00:00
Hafizh A. Y. 953756c15c Merge branch 'fix/BE/US-278-purchase_adjustment_createOne' into 'development'
Fix/be/us 278 purchase adjustment createone

See merge request mbugroup/lti-api!123
2025-12-31 03:01:35 +00:00
Hafizh A. Y. 2749e44439 Merge branch 'feat/BE/US-281-adjustment_recording' into 'development'
feat(BE-281): adjustment recording table with handhouse and deleting weight...

See merge request mbugroup/lti-api!122
2025-12-31 03:01:09 +00:00
ragilap b8c0b0c37d feat(BE-278): add std for max_depletion 2025-12-31 09:44:20 +07:00
Hafizh A. Y. acbf52a5e1 Merge branch 'feat/BE/US-281-uniformity' into 'development'
feat(BE-281): adjustment bug erorr 500 if 404 record projectflock

See merge request mbugroup/lti-api!124
2025-12-31 02:41:42 +00:00
aguhh18 0fc560b91c fix(be): update nonstock query to use SupplierID as a non-pointer type 2025-12-31 09:40:05 +07:00
Hafizh A. Y. d35d0bbe6b Merge branch 'dev/teguh' into 'development'
fix(BE): fix error get location id when only attach to location in expense

See merge request mbugroup/lti-api!119
2025-12-31 02:18:27 +00:00
ragilap d9afd2913e feat(BE-278): adjustment_recording dto 2025-12-31 09:13:55 +07:00
ragilap dbaee73134 feat(BE-278): fix error purchase product warehouse 2025-12-31 07:50:13 +07:00
ragilap bc03c469f2 feat(BE-278): add delete document s3 2025-12-31 04:00:41 +07:00
ragilap fd5f83ca58 feat(BE-278): unrestrict feat warehouse purchase,adding purchase upload document 2025-12-31 03:50:58 +07:00
aguhh18 91fd8a253b feat(BE): update foreign key constraints for project_chickins and adjust service logic for project flock kandang retrieval 2025-12-30 20:16:40 +07:00
aguhh18 d91ff7a4c2 feat(BE): add supplier_id filter to GetAll method and update validation for query parameters 2025-12-30 20:03:23 +07:00
aguhh18 3ecea6741f feat(BE): update DeleteOne method to use uint64 for ID and implement soft delete logic 2025-12-30 19:39:10 +07:00
aguhh18 b988f45a0b feat(BE): update expense DTO and service to directly use location from expense 2025-12-30 19:30:42 +07:00
ragilap 0396aa0255 feat(BE-287):adjustment purchase restrict unfinished 2025-12-30 14:27:50 +07:00
ragilap 756ba223ed feat(BE-281):add standart production into response recording get one 2025-12-30 13:17:01 +07:00
ragilap 16ef73fce3 Merge branch 'feat/BE/Sprint-8' of https://gitlab.com/mbugroup/lti-api into feat/BE/US-281-adjustment_recording 2025-12-30 09:41:42 +07:00
ragilap 8e7e976946 feat(BE-281): adjustment recording table with handhouse and deleting weight unfinished dto:standart fcr,hand house and others 2025-12-29 16:25:08 +07:00
ragilap 411d6fe6a9 feat(BE-281): deleting bw in recording 2025-12-29 09:38:49 +07:00
104 changed files with 6330 additions and 512 deletions
@@ -187,10 +187,11 @@ func (r *BaseRepositoryImpl[T]) PatchOne(
updates map[string]any, updates map[string]any,
modifier func(*gorm.DB) *gorm.DB, modifier func(*gorm.DB) *gorm.DB,
) error { ) error {
q := r.db.WithContext(ctx).Model(new(T)).Where("id = ?", id) q := r.db.WithContext(ctx)
if modifier != nil { if modifier != nil {
q = modifier(q) q = modifier(q)
} }
q = q.Model(new(T)).Where("id = ?", id)
result := q.Updates(updates) result := q.Updates(updates)
if result.Error != nil { if result.Error != nil {
@@ -6,8 +6,10 @@ import (
"fmt" "fmt"
"mime" "mime"
"mime/multipart" "mime/multipart"
"net/url"
"path/filepath" "path/filepath"
"strings" "strings"
"time"
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository" commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
"gitlab.com/mbugroup/lti-api.git/internal/config" "gitlab.com/mbugroup/lti-api.git/internal/config"
@@ -29,6 +31,7 @@ type DocumentService interface {
DeleteDocuments(ctx context.Context, ids []uint, removeFromStorage bool) error DeleteDocuments(ctx context.Context, ids []uint, removeFromStorage bool) error
DeleteByTarget(ctx context.Context, documentableType string, documentableID uint64, removeFromStorage bool) error DeleteByTarget(ctx context.Context, documentableType string, documentableID uint64, removeFromStorage bool) error
PublicURL(document entity.Document) string PublicURL(document entity.Document) string
PresignURL(ctx context.Context, document entity.Document, expires time.Duration) (string, error)
} }
type DocumentUploadRequest struct { type DocumentUploadRequest struct {
@@ -293,6 +296,66 @@ func (s *documentService) PublicURL(document entity.Document) string {
return s.storage.URL(document.Path) return s.storage.URL(document.Path)
} }
func (s *documentService) PresignURL(ctx context.Context, document entity.Document, expires time.Duration) (string, error) {
if s.storage == nil {
return "", errors.New("document storage not configured")
}
if strings.TrimSpace(document.Path) == "" {
return "", errors.New("document path is required")
}
return s.storage.PresignURL(ctx, document.Path, expires)
}
// ResolveDocumentURL normalizes a stored path or URL into a presigned URL.
func ResolveDocumentURL(
ctx context.Context,
svc DocumentService,
rawPath string,
expires time.Duration,
) (string, error) {
if svc == nil {
return "", nil
}
rawPath = strings.TrimSpace(rawPath)
if rawPath == "" {
return "", nil
}
key := rawPath
lower := strings.ToLower(rawPath)
if strings.HasPrefix(lower, "http://") || strings.HasPrefix(lower, "https://") {
key = extractS3KeyFromURL(rawPath)
if key == "" {
return "", nil
}
}
return svc.PresignURL(ctx, entity.Document{Path: key}, expires)
}
func extractS3KeyFromURL(raw string) string {
parsed, err := url.Parse(strings.TrimSpace(raw))
if err != nil {
return ""
}
path := strings.TrimPrefix(parsed.Path, "/")
if path == "" {
return ""
}
host := strings.ToLower(strings.TrimSpace(parsed.Host))
if strings.HasPrefix(host, "s3.") || strings.HasPrefix(host, "s3-") {
parts := strings.SplitN(path, "/", 2)
if len(parts) == 2 {
return parts[1]
}
return ""
}
return path
}
func (s *documentService) generateObjectKey(ext string) (string, error) { func (s *documentService) generateObjectKey(ext string) (string, error) {
normalizedExt := strings.TrimSpace(ext) normalizedExt := strings.TrimSpace(ext)
if normalizedExt != "" && !strings.HasPrefix(normalizedExt, ".") { if normalizedExt != "" && !strings.HasPrefix(normalizedExt, ".") {
@@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"io" "io"
"strings" "strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/aws"
awsconfig "github.com/aws/aws-sdk-go-v2/config" awsconfig "github.com/aws/aws-sdk-go-v2/config"
@@ -17,6 +18,7 @@ type DocumentStorage interface {
Upload(ctx context.Context, key string, body io.Reader, size int64, contentType string) (DocumentStorageUploadResult, error) Upload(ctx context.Context, key string, body io.Reader, size int64, contentType string) (DocumentStorageUploadResult, error)
Delete(ctx context.Context, key string) error Delete(ctx context.Context, key string) error
URL(key string) string URL(key string) string
PresignURL(ctx context.Context, key string, expires time.Duration) (string, error)
} }
type DocumentStorageUploadResult struct { type DocumentStorageUploadResult struct {
@@ -36,9 +38,10 @@ type S3DocumentStorageConfig struct {
} }
type s3DocumentStorage struct { type s3DocumentStorage struct {
client *s3.Client client *s3.Client
bucket string presignClient *s3.PresignClient
base string bucket string
base string
} }
func NewS3DocumentStorage(ctx context.Context, cfg S3DocumentStorageConfig) (DocumentStorage, error) { func NewS3DocumentStorage(ctx context.Context, cfg S3DocumentStorageConfig) (DocumentStorage, error) {
@@ -86,6 +89,7 @@ func NewS3DocumentStorage(ctx context.Context, cfg S3DocumentStorageConfig) (Doc
client := s3.NewFromConfig(awsCfg, func(o *s3.Options) { client := s3.NewFromConfig(awsCfg, func(o *s3.Options) {
o.UsePathStyle = cfg.ForcePathStyle o.UsePathStyle = cfg.ForcePathStyle
}) })
presignClient := s3.NewPresignClient(client)
baseURL := strings.TrimSuffix(strings.TrimSpace(cfg.BaseURL), "/") baseURL := strings.TrimSuffix(strings.TrimSpace(cfg.BaseURL), "/")
if baseURL == "" { if baseURL == "" {
@@ -97,9 +101,10 @@ func NewS3DocumentStorage(ctx context.Context, cfg S3DocumentStorageConfig) (Doc
} }
return &s3DocumentStorage{ return &s3DocumentStorage{
client: client, client: client,
bucket: bucket, presignClient: presignClient,
base: baseURL, bucket: bucket,
base: baseURL,
}, nil }, nil
} }
@@ -158,3 +163,23 @@ func (s *s3DocumentStorage) URL(key string) string {
} }
return fmt.Sprintf("%s/%s", s.base, key) return fmt.Sprintf("%s/%s", s.base, key)
} }
func (s *s3DocumentStorage) PresignURL(ctx context.Context, key string, expires time.Duration) (string, error) {
key = strings.TrimPrefix(strings.TrimSpace(key), "/")
if key == "" {
return "", errors.New("storage key is required")
}
if expires <= 0 {
expires = 15 * time.Minute
}
out, err := s.presignClient.PresignGetObject(ctx, &s3.GetObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(key),
}, s3.WithPresignExpires(expires))
if err != nil {
return "", err
}
return out.URL, nil
}
@@ -192,7 +192,6 @@ func (s *fifoService) Consume(ctx context.Context, req StockConsumeRequest) (*St
if req.Quantity < 0 { if req.Quantity < 0 {
return nil, errors.New("quantity must be zero or greater") return nil, errors.New("quantity must be zero or greater")
} }
cfg, ok := fifo.Usable(req.UsableKey) cfg, ok := fifo.Usable(req.UsableKey)
if !ok { if !ok {
return nil, fmt.Errorf("usable %q is not registered", req.UsableKey) return nil, fmt.Errorf("usable %q is not registered", req.UsableKey)
@@ -220,7 +219,6 @@ func (s *fifoService) Consume(ctx context.Context, req StockConsumeRequest) (*St
currentPending := ctxRow.PendingQty currentPending := ctxRow.PendingQty
currentTotal := currentUsage + currentPending currentTotal := currentUsage + currentPending
delta := req.Quantity - currentTotal delta := req.Quantity - currentTotal
var ( var (
usageDelta float64 usageDelta float64
pendingDelta float64 pendingDelta float64
@@ -285,7 +283,6 @@ func (s *fifoService) Consume(ctx context.Context, req StockConsumeRequest) (*St
result.ReleasedQuantity = releasedAmount result.ReleasedQuantity = releasedAmount
result.UsageQuantity = currentUsage + usageDelta result.UsageQuantity = currentUsage + usageDelta
result.PendingQuantity = currentPending + pendingDelta result.PendingQuantity = currentPending + pendingDelta
return nil return nil
}) })
if err != nil { if err != nil {
@@ -299,7 +296,6 @@ func (s *fifoService) ReleaseUsage(ctx context.Context, req StockReleaseRequest)
if req.UsableID == 0 || strings.TrimSpace(req.UsableKey.String()) == "" { if req.UsableID == 0 || strings.TrimSpace(req.UsableKey.String()) == "" {
return errors.New("usable key and id are required") return errors.New("usable key and id are required")
} }
return s.withTransaction(ctx, req.Tx, func(tx *gorm.DB) error { return s.withTransaction(ctx, req.Tx, func(tx *gorm.DB) error {
cfg, ok := fifo.Usable(req.UsableKey) cfg, ok := fifo.Usable(req.UsableKey)
if !ok { if !ok {
@@ -310,7 +306,6 @@ func (s *fifoService) ReleaseUsage(ctx context.Context, req StockReleaseRequest)
if err != nil { if err != nil {
return err return err
} }
var usageDelta, pendingDelta float64 var usageDelta, pendingDelta float64
if ctxRow.UsageQty > 0 { if ctxRow.UsageQty > 0 {
if _, err := s.releaseUsagePortion(ctx, tx, req.UsableKey, req.UsableID, ctxRow.UsageQty); err != nil { if _, err := s.releaseUsagePortion(ctx, tx, req.UsableKey, req.UsableID, ctxRow.UsageQty); err != nil {
@@ -715,7 +710,7 @@ func (s *fifoService) releaseUsagePortion(
} }
} else { } else {
if err := s.allocations.PatchOne(ctx, allocation.Id, map[string]any{ if err := s.allocations.PatchOne(ctx, allocation.Id, map[string]any{
"quantity": allocation.Qty - releaseAmt, "qty": allocation.Qty - releaseAmt,
}, func(db *gorm.DB) *gorm.DB { }, func(db *gorm.DB) *gorm.DB {
return s.txOrDB(tx, db) return s.txOrDB(tx, db)
}); err != nil { }); err != nil {
@@ -0,0 +1,54 @@
BEGIN;
CREATE TABLE IF NOT EXISTS recording_bws (
id BIGSERIAL PRIMARY KEY,
recording_id BIGINT NOT NULL,
avg_weight NUMERIC(8,2) NOT NULL,
qty NUMERIC(15,3) NOT NULL DEFAULT 1,
total_weight NUMERIC(10,3) NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
CONSTRAINT fk_recording_bws_recording
FOREIGN KEY (recording_id) REFERENCES recordings(id) ON DELETE CASCADE,
CONSTRAINT chk_recording_bws_nonneg
CHECK (avg_weight >= 0 AND qty >= 0 AND total_weight >= 0)
);
CREATE INDEX IF NOT EXISTS idx_recording_bws_recording
ON recording_bws (recording_id);
ALTER TABLE recordings
DROP CONSTRAINT IF EXISTS chk_recordings_nonnegatives_v3;
ALTER TABLE recordings
DROP COLUMN IF EXISTS hand_day,
DROP COLUMN IF EXISTS hand_house,
DROP COLUMN IF EXISTS feed_intake,
DROP COLUMN IF EXISTS egg_mesh,
DROP COLUMN IF EXISTS egg_weight;
ALTER TABLE recordings
ADD CONSTRAINT chk_recordings_nonnegatives_v2 CHECK (
(total_depletion_qty IS NULL OR total_depletion_qty >= 0) AND
(cum_depletion_rate IS NULL OR cum_depletion_rate >= 0) AND
(daily_gain IS NULL OR daily_gain >= 0) AND
(avg_daily_gain IS NULL OR avg_daily_gain >= 0) AND
(cum_intake IS NULL OR cum_intake >= 0) AND
(fcr_value IS NULL OR fcr_value >= 0) AND
(total_chick_qty IS NULL OR total_chick_qty >= 0)
);
ALTER TABLE recording_eggs
DROP CONSTRAINT IF EXISTS chk_recording_eggs_qty;
ALTER TABLE recording_eggs
ALTER COLUMN weight TYPE NUMERIC(10,3) USING weight::NUMERIC(10,3);
ALTER TABLE recording_eggs
ADD CONSTRAINT chk_recording_eggs_qty CHECK (
qty >= 0 AND (weight IS NULL OR weight >= 0)
);
COMMIT;
@@ -0,0 +1,44 @@
BEGIN;
ALTER TABLE recordings
DROP CONSTRAINT IF EXISTS chk_recordings_nonnegatives_v2;
ALTER TABLE recordings
ADD COLUMN IF NOT EXISTS hand_day NUMERIC(15,3),
ADD COLUMN IF NOT EXISTS hand_house NUMERIC(15,3),
ADD COLUMN IF NOT EXISTS feed_intake NUMERIC(15,3),
ADD COLUMN IF NOT EXISTS egg_mesh NUMERIC(15,3),
ADD COLUMN IF NOT EXISTS egg_weight NUMERIC(15,3);
ALTER TABLE recordings
ADD CONSTRAINT chk_recordings_nonnegatives_v3 CHECK (
(total_depletion_qty IS NULL OR total_depletion_qty >= 0) AND
(cum_depletion_rate IS NULL OR cum_depletion_rate >= 0) AND
(daily_gain IS NULL OR daily_gain >= 0) AND
(avg_daily_gain IS NULL OR avg_daily_gain >= 0) AND
(cum_intake IS NULL OR cum_intake >= 0) AND
(fcr_value IS NULL OR fcr_value >= 0) AND
(total_chick_qty IS NULL OR total_chick_qty >= 0) AND
(hand_day IS NULL OR hand_day >= 0) AND
(hand_house IS NULL OR hand_house >= 0) AND
(feed_intake IS NULL OR feed_intake >= 0) AND
(egg_mesh IS NULL OR egg_mesh >= 0) AND
(egg_weight IS NULL OR egg_weight >= 0)
);
ALTER TABLE recording_eggs
ALTER COLUMN weight TYPE NUMERIC(15,3) USING weight::NUMERIC(15,3);
ALTER TABLE recording_eggs
DROP CONSTRAINT IF EXISTS chk_recording_eggs_qty;
ALTER TABLE recording_eggs
ADD CONSTRAINT chk_recording_eggs_qty CHECK (
qty >= 0 AND
(weight IS NULL OR weight >= 0)
);
DROP INDEX IF EXISTS idx_recording_bws_recording;
DROP TABLE IF EXISTS recording_bws;
COMMIT;
@@ -0,0 +1,20 @@
-- Drop CASCADE constraint
DO $$
BEGIN
IF EXISTS (
SELECT 1
FROM pg_constraint
WHERE conname = 'fk_project_chickins_kandang'
AND conrelid = 'project_chickins'::regclass
) THEN
ALTER TABLE project_chickins
DROP CONSTRAINT fk_project_chickins_kandang;
END IF;
END $$;
-- Recreate foreign key constraint with RESTRICT (original behavior)
ALTER TABLE project_chickins
ADD CONSTRAINT fk_project_chickins_kandang
FOREIGN KEY (project_flock_kandang_id)
REFERENCES project_flock_kandangs(id)
ON DELETE RESTRICT ON UPDATE CASCADE;
@@ -0,0 +1,20 @@
-- Drop existing foreign key constraint with RESTRICT
DO $$
BEGIN
IF EXISTS (
SELECT 1
FROM pg_constraint
WHERE conname = 'fk_project_chickins_kandang'
AND conrelid = 'project_chickins'::regclass
) THEN
ALTER TABLE project_chickins
DROP CONSTRAINT fk_project_chickins_kandang;
END IF;
END $$;
-- Add new foreign key constraint with CASCADE delete
ALTER TABLE project_chickins
ADD CONSTRAINT fk_project_chickins_kandang
FOREIGN KEY (project_flock_kandang_id)
REFERENCES project_flock_kandangs(id)
ON DELETE CASCADE ON UPDATE CASCADE;
@@ -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,142 @@
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;
child_type 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;
SELECT format_type(atttypid, atttypmod) INTO child_type
FROM pg_attribute
WHERE attrelid = fk.child_table
AND attname = child_column
AND NOT attisdropped;
IF child_type IS NULL THEN
child_type := 'text';
END IF;
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 %s)',
fk.child_table,
child_column,
child_type,
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 %s',
fk.child_table,
child_column,
child_column,
child_type,
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::%s AND deleted_at IS NULL',
fk.child_table,
child_column,
child_type
);
EXECUTE sql USING parent_value;
ELSE
sql := format(
'DELETE FROM %s WHERE %I = $1::%s',
fk.child_table,
child_column,
child_type
);
EXECUTE sql USING parent_value;
END IF;
ELSIF fk.confdeltype = 'd' THEN
sql := format(
'UPDATE %s SET %I = DEFAULT WHERE %I = $1::%s %s',
fk.child_table,
child_column,
child_column,
child_type,
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,86 @@
BEGIN;
DO $$
BEGIN
IF EXISTS (
SELECT 1
FROM pg_constraint
WHERE conname = 'fk_project_flock_kandang_uniformity_project_flock_kandang'
) THEN
ALTER TABLE project_flock_kandang_uniformity
DROP CONSTRAINT fk_project_flock_kandang_uniformity_project_flock_kandang;
END IF;
END $$;
ALTER TABLE project_flock_kandang_uniformity
ADD CONSTRAINT fk_project_flock_kandang_uniformity_project_flock_kandang
FOREIGN KEY (project_flock_kandang_id)
REFERENCES project_flock_kandangs (id)
ON DELETE RESTRICT ON UPDATE CASCADE;
DO $$
BEGIN
IF EXISTS (
SELECT 1
FROM pg_tables
WHERE tablename = 'project_budgets'
) THEN
IF EXISTS (
SELECT 1
FROM pg_constraint
WHERE conname = 'fk_project_budgets_project_flock_id'
) THEN
ALTER TABLE project_budgets
DROP CONSTRAINT fk_project_budgets_project_flock_id;
END IF;
ALTER TABLE project_budgets
ADD CONSTRAINT fk_project_budgets_project_flock_id
FOREIGN KEY (project_flock_id)
REFERENCES project_flocks(id);
END IF;
END $$;
DO $$
BEGIN
IF EXISTS (
SELECT 1
FROM pg_tables
WHERE tablename = 'project_flock_kandang_uniformity'
) THEN
IF NOT EXISTS (
SELECT 1
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'project_flock_kandang_uniformity'
AND column_name = 'created_at'
) THEN
ALTER TABLE project_flock_kandang_uniformity
ADD COLUMN created_at TIMESTAMPTZ DEFAULT NOW();
END IF;
IF NOT EXISTS (
SELECT 1
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'project_flock_kandang_uniformity'
AND column_name = 'updated_at'
) THEN
ALTER TABLE project_flock_kandang_uniformity
ADD COLUMN updated_at TIMESTAMPTZ DEFAULT NOW();
END IF;
IF NOT EXISTS (
SELECT 1
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'project_flock_kandang_uniformity'
AND column_name = 'deleted_at'
) THEN
ALTER TABLE project_flock_kandang_uniformity
ADD COLUMN deleted_at TIMESTAMPTZ;
END IF;
END IF;
END $$;
COMMIT;
@@ -0,0 +1,90 @@
BEGIN;
DO $$
BEGIN
IF EXISTS (
SELECT 1
FROM pg_constraint
WHERE conname = 'fk_project_flock_kandang_uniformity_project_flock_kandang'
) THEN
ALTER TABLE project_flock_kandang_uniformity
DROP CONSTRAINT fk_project_flock_kandang_uniformity_project_flock_kandang;
END IF;
END $$;
ALTER TABLE project_flock_kandang_uniformity
ADD CONSTRAINT fk_project_flock_kandang_uniformity_project_flock_kandang
FOREIGN KEY (project_flock_kandang_id)
REFERENCES project_flock_kandangs (id)
ON DELETE CASCADE ON UPDATE CASCADE;
DO $$
BEGIN
IF EXISTS (
SELECT 1
FROM pg_tables
WHERE tablename = 'project_budgets'
) THEN
IF EXISTS (
SELECT 1
FROM pg_constraint
WHERE conname = 'fk_project_budgets_project_flock_id'
) THEN
ALTER TABLE project_budgets
DROP CONSTRAINT fk_project_budgets_project_flock_id;
END IF;
ALTER TABLE project_budgets
ADD CONSTRAINT fk_project_budgets_project_flock_id
FOREIGN KEY (project_flock_id)
REFERENCES project_flocks(id)
ON DELETE CASCADE ON UPDATE CASCADE;
END IF;
END $$;
DO $$
BEGIN
IF EXISTS (
SELECT 1
FROM pg_trigger
WHERE tgname = 'trg_soft_delete_fk_project_flock_kandang_uniformity'
) THEN
DROP TRIGGER trg_soft_delete_fk_project_flock_kandang_uniformity
ON project_flock_kandang_uniformity;
END IF;
IF EXISTS (
SELECT 1
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'project_flock_kandang_uniformity'
AND column_name = 'created_at'
) THEN
ALTER TABLE project_flock_kandang_uniformity
DROP COLUMN created_at;
END IF;
IF EXISTS (
SELECT 1
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'project_flock_kandang_uniformity'
AND column_name = 'updated_at'
) THEN
ALTER TABLE project_flock_kandang_uniformity
DROP COLUMN updated_at;
END IF;
IF EXISTS (
SELECT 1
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'project_flock_kandang_uniformity'
AND column_name = 'deleted_at'
) THEN
ALTER TABLE project_flock_kandang_uniformity
DROP COLUMN deleted_at;
END IF;
END $$;
COMMIT;
@@ -0,0 +1,12 @@
DROP TABLE IF EXISTS daily_checklist_tasks;
DROP TABLE IF EXISTS daily_checklist_activity_task_assignees;
DROP TABLE IF EXISTS daily_checklist_activity_tasks;
DROP TABLE IF EXISTS daily_checklist_phases;
DROP TABLE IF EXISTS daily_checklists;
DROP TABLE IF EXISTS checklists;
DROP TABLE IF EXISTS phase_activities;
DROP TABLE IF EXISTS phases;
DROP TABLE IF EXISTS employee_kandangs;
DROP TABLE IF EXISTS employees;
DROP TYPE IF EXISTS category_code;
@@ -0,0 +1,194 @@
CREATE TYPE category_code AS ENUM (
'pullet_open',
'pullet_close',
'produksi_open',
'produksi_close'
);
-- MASTER TABLES
CREATE TABLE employees (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name varchar NOT NULL,
is_active boolean NOT NULL DEFAULT true,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE employee_kandangs (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
employee_id bigint NOT NULL,
kandang_id bigint NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
CONSTRAINT fk_employee_kandangs_employee
FOREIGN KEY (employee_id) REFERENCES employees(id)
ON DELETE CASCADE,
CONSTRAINT fk_employee_kandangs_kandang
FOREIGN KEY (kandang_id) REFERENCES kandangs(id)
ON DELETE CASCADE,
CONSTRAINT uq_employee_kandangs UNIQUE (employee_id, kandang_id)
);
-- PHASE & CHECKLIST
CREATE TABLE phases (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name varchar NOT NULL,
is_active boolean NOT NULL DEFAULT true,
category category_code NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE phase_activities (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
phase_id bigint NOT NULL,
name varchar NOT NULL,
description text,
time_type text,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
CONSTRAINT fk_phase_activities_phase
FOREIGN KEY (phase_id) REFERENCES phases(id)
ON DELETE CASCADE
);
CREATE TABLE checklists (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name varchar NOT NULL,
description text,
phase_id bigint,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
deleted_at timestamptz,
CONSTRAINT fk_checklists_phase
FOREIGN KEY (phase_id) REFERENCES phases(id)
ON DELETE SET NULL
);
-- DAILY CHECKLISTS
CREATE TABLE daily_checklists (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
kandang_id bigint NOT NULL,
checklist_id bigint NOT NULL,
date date NOT NULL,
name varchar,
status varchar,
category category_code NOT NULL,
total_score integer,
document_path varchar,
reject_reason text,
created_by bigint,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
CONSTRAINT fk_daily_checklists_kandang
FOREIGN KEY (kandang_id) REFERENCES kandangs(id)
ON DELETE CASCADE,
CONSTRAINT fk_daily_checklists_checklist
FOREIGN KEY (checklist_id) REFERENCES checklists(id)
ON DELETE RESTRICT,
CONSTRAINT fk_daily_checklists_created_by
FOREIGN KEY (created_by) REFERENCES users(id)
ON DELETE SET NULL
);
--RELASI CHECKLIST ⇄ PHASE
CREATE TABLE daily_checklist_phases (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
checklist_id bigint NOT NULL,
phase_id bigint NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
CONSTRAINT fk_dcp_checklist
FOREIGN KEY (checklist_id) REFERENCES checklists(id)
ON DELETE CASCADE,
CONSTRAINT fk_dcp_phase
FOREIGN KEY (phase_id) REFERENCES phases(id)
ON DELETE CASCADE,
CONSTRAINT uq_daily_checklist_phases UNIQUE (checklist_id, phase_id)
);
--ACTIVITY TASKS & ASSIGNMENT
CREATE TABLE daily_checklist_activity_tasks (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
checklist_id bigint NOT NULL,
phase_id bigint NOT NULL,
phase_activity_id bigint NOT NULL,
time_type text,
notes text,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
CONSTRAINT fk_dcat_checklist
FOREIGN KEY (checklist_id) REFERENCES checklists(id)
ON DELETE CASCADE,
CONSTRAINT fk_dcat_phase
FOREIGN KEY (phase_id) REFERENCES phases(id)
ON DELETE CASCADE,
CONSTRAINT fk_dcat_phase_activity
FOREIGN KEY (phase_activity_id) REFERENCES phase_activities(id)
ON DELETE CASCADE
);
CREATE TABLE daily_checklist_activity_task_assignments (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
task_id bigint NOT NULL,
employee_id bigint NOT NULL,
checked boolean NOT NULL DEFAULT false,
note text,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
CONSTRAINT fk_assignment_task
FOREIGN KEY (task_id) REFERENCES daily_checklist_activity_tasks(id)
ON DELETE CASCADE,
CONSTRAINT fk_assignment_employee
FOREIGN KEY (employee_id) REFERENCES employees(id)
ON DELETE CASCADE
);
--DAILY CHECKLIST TASK RESULT
CREATE TABLE daily_checklist_tasks (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
daily_checklist_id bigint NOT NULL,
checklist_id bigint NOT NULL,
checklist_item_id bigint,
is_completed boolean NOT NULL DEFAULT false,
score_value integer,
notes text,
photo_proof varchar,
status varchar,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
CONSTRAINT fk_dct_daily
FOREIGN KEY (daily_checklist_id) REFERENCES daily_checklists(id)
ON DELETE CASCADE,
CONSTRAINT fk_dct_checklist
FOREIGN KEY (checklist_id) REFERENCES checklists(id)
ON DELETE CASCADE,
CONSTRAINT fk_dct_checklist_item
FOREIGN KEY (checklist_item_id) REFERENCES phase_activities(id)
ON DELETE SET NULL
);
@@ -0,0 +1,2 @@
ALTER TABLE daily_checklists
DROP CONSTRAINT IF EXISTS daily_checklists_date_kandang_category_key;
@@ -0,0 +1,3 @@
ALTER TABLE daily_checklists
ADD CONSTRAINT daily_checklists_date_kandang_category_key
UNIQUE (date, kandang_id, category);
@@ -0,0 +1,2 @@
ALTER TABLE daily_checklists
ALTER COLUMN checklist_id SET NOT NULL;
@@ -0,0 +1,2 @@
ALTER TABLE daily_checklists
ALTER COLUMN checklist_id DROP NOT NULL;
@@ -0,0 +1,4 @@
ALTER TABLE daily_checklist_phases
DROP CONSTRAINT IF EXISTS fk_dcp_daily_checklist,
ADD CONSTRAINT fk_dcp_checklist
FOREIGN KEY (checklist_id) REFERENCES checklists(id) ON DELETE CASCADE;
@@ -0,0 +1,4 @@
ALTER TABLE daily_checklist_phases
DROP CONSTRAINT IF EXISTS fk_dcp_checklist,
ADD CONSTRAINT fk_dcp_daily_checklist
FOREIGN KEY (checklist_id) REFERENCES daily_checklists(id) ON DELETE CASCADE;
@@ -0,0 +1,4 @@
ALTER TABLE daily_checklist_activity_tasks
DROP CONSTRAINT IF EXISTS fk_dcat_daily_checklist,
ADD CONSTRAINT fk_dcat_checklist
FOREIGN KEY (checklist_id) REFERENCES checklists(id) ON DELETE CASCADE;
@@ -0,0 +1,4 @@
ALTER TABLE daily_checklist_activity_tasks
DROP CONSTRAINT IF EXISTS fk_dcat_checklist,
ADD CONSTRAINT fk_dcat_daily_checklist
FOREIGN KEY (checklist_id) REFERENCES daily_checklists(id) ON DELETE CASCADE;
@@ -0,0 +1,2 @@
ALTER TABLE daily_checklist_activity_task_assignments
DROP CONSTRAINT IF EXISTS daily_checklist_activity_task_assignments_task_employee_key;
@@ -0,0 +1,3 @@
ALTER TABLE daily_checklist_activity_task_assignments
ADD CONSTRAINT daily_checklist_activity_task_assignments_task_employee_key
UNIQUE (task_id, employee_id);
@@ -0,0 +1,8 @@
ALTER TABLE phase_activities
DROP COLUMN IF EXISTS deleted_at;
ALTER TABLE phases
DROP COLUMN IF EXISTS deleted_at;
ALTER TABLE employees
DROP COLUMN IF EXISTS deleted_at;
@@ -0,0 +1,8 @@
ALTER TABLE employees
ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ;
ALTER TABLE phases
ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ;
ALTER TABLE phase_activities
ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ;
@@ -0,0 +1 @@
DROP TABLE IF EXISTS config_checklists;
@@ -0,0 +1,9 @@
CREATE TABLE IF NOT EXISTS config_checklists (
id BIGSERIAL PRIMARY KEY,
date DATE NOT NULL,
percentage_threshold_bad INTEGER NOT NULL,
percentage_threshold_enough INTEGER NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
deleted_at TIMESTAMPTZ
);
+17
View File
@@ -0,0 +1,17 @@
package entities
import (
"time"
"gorm.io/gorm"
)
type ConfigChecklist struct {
Id uint `gorm:"primaryKey"`
Date time.Time `gorm:"type:date;not null"`
PercentageThresholdBad int `gorm:"not null"`
PercentageThresholdEnough int `gorm:"not null"`
CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
}
+81
View File
@@ -0,0 +1,81 @@
package entities
import "time"
type DailyChecklist struct {
Id uint `gorm:"primaryKey"`
KandangId uint `gorm:"not null"`
ChecklistId *uint
Date time.Time `gorm:"type:date;not null"`
Name *string `gorm:"type:varchar(255)"`
Status *string `gorm:"type:varchar(255)"`
Category string `gorm:"type:category_code;not null"`
TotalScore *int
DocumentPath *string
RejectReason *string
CreatedBy *uint
CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
Kandang Kandang `gorm:"foreignKey:KandangId;references:Id"`
Checklist *Checklist `gorm:"foreignKey:ChecklistId;references:Id"`
Creator *User `gorm:"foreignKey:CreatedBy;references:Id"`
Tasks []DailyChecklistTask `gorm:"foreignKey:DailyChecklistId;references:Id"`
}
type DailyChecklistPhase struct {
Id uint `gorm:"primaryKey"`
ChecklistId uint `gorm:"not null"`
PhaseId uint `gorm:"not null"`
CreatedAt time.Time `gorm:"autoCreateTime"`
Checklist Checklist `gorm:"foreignKey:ChecklistId;references:Id"`
Phase Phases `gorm:"foreignKey:PhaseId;references:Id"`
}
type DailyChecklistActivityTask struct {
Id uint `gorm:"primaryKey"`
ChecklistId uint `gorm:"not null"`
PhaseId uint `gorm:"not null"`
PhaseActivityId uint `gorm:"not null"`
TimeType *string `gorm:"type:text"`
Notes *string `gorm:"type:text"`
CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
Checklist DailyChecklist `gorm:"foreignKey:ChecklistId;references:Id"`
Phase Phases `gorm:"foreignKey:PhaseId;references:Id"`
PhaseActivity PhaseActivity `gorm:"foreignKey:PhaseActivityId;references:Id"`
Assignments []DailyChecklistActivityTaskAssignment `gorm:"foreignKey:TaskId;references:Id"`
}
type DailyChecklistActivityTaskAssignment struct {
Id uint `gorm:"primaryKey"`
TaskId uint `gorm:"not null"`
EmployeeId uint `gorm:"not null"`
Checked bool `gorm:"not null;default:false"`
Note *string `gorm:"type:text"`
CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
Task DailyChecklistActivityTask `gorm:"foreignKey:TaskId;references:Id"`
Employee Employee `gorm:"foreignKey:EmployeeId;references:Id"`
}
type DailyChecklistTask struct {
Id uint `gorm:"primaryKey"`
DailyChecklistId uint `gorm:"not null"`
ChecklistId uint `gorm:"not null"`
ChecklistItemId *uint
IsCompleted bool `gorm:"not null;default:false"`
ScoreValue *int
Notes *string `gorm:"type:text"`
PhotoProof *string
Status *string
CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
DailyChecklist *DailyChecklist `gorm:"foreignKey:DailyChecklistId;references:Id"`
Checklist Checklist `gorm:"foreignKey:ChecklistId;references:Id"`
ChecklistItem *PhaseActivity `gorm:"foreignKey:ChecklistItemId;references:Id"`
}
+31
View File
@@ -0,0 +1,31 @@
package entities
import (
"time"
"gorm.io/gorm"
)
type Employee struct {
Id uint `gorm:"primaryKey"`
Name string `gorm:"not null"`
IsActive bool `gorm:"not null"`
CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
EmployeeKandangs []EmployeeKandang `gorm:"foreignKey:EmployeeId;references:Id"`
}
type Employees = Employee
type EmployeeKandang struct {
Id uint `gorm:"primaryKey"`
EmployeeId uint `gorm:"not null"`
KandangId uint `gorm:"not null"`
CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
Employee Employee `gorm:"foreignKey:EmployeeId;references:Id"`
Kandang Kandang `gorm:"foreignKey:KandangId;references:Id"`
}
+43
View File
@@ -0,0 +1,43 @@
package entities
import (
"time"
"gorm.io/gorm"
)
type Phases struct {
Id uint `gorm:"primaryKey"`
Name string `gorm:"not null"`
IsActive bool `gorm:"not null;default:true"`
Category string `gorm:"type:category_code;not null"`
CreatedAt time.Time `gorm:"autoCreateTime"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
Activities []PhaseActivity `gorm:"foreignKey:PhaseId;references:Id"`
}
type PhaseActivity struct {
Id uint `gorm:"primaryKey"`
PhaseId uint `gorm:"not null"`
Name string `gorm:"not null"`
Description *string `gorm:"type:text"`
TimeType *string `gorm:"type:text"`
CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
Phase Phases `gorm:"foreignKey:PhaseId;references:Id"`
}
type Checklist struct {
Id uint `gorm:"primaryKey"`
Name string `gorm:"not null"`
Description *string `gorm:"type:text"`
PhaseId *uint
CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
Phase *Phases `gorm:"foreignKey:PhaseId;references:Id"`
}
@@ -1,10 +1,6 @@
package entities package entities
import ( import "time"
"time"
"gorm.io/gorm"
)
type ProjectFlockKandangUniformity struct { type ProjectFlockKandangUniformity struct {
Id uint `gorm:"primaryKey"` Id uint `gorm:"primaryKey"`
@@ -18,9 +14,6 @@ type ProjectFlockKandangUniformity struct {
UniformQty float64 `gorm:"type:numeric(15,3)"` UniformQty float64 `gorm:"type:numeric(15,3)"`
NotUniformQty float64 `gorm:"type:numeric(15,3)"` NotUniformQty float64 `gorm:"type:numeric(15,3)"`
UniformDate *time.Time `gorm:"type:timestamptz"` UniformDate *time.Time `gorm:"type:timestamptz"`
CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
CreatedBy uint `gorm:"not null"` CreatedBy uint `gorm:"not null"`
ProjectFlockKandang ProjectFlockKandang `gorm:"foreignKey:ProjectFlockKandangId;references:Id"` ProjectFlockKandang ProjectFlockKandang `gorm:"foreignKey:ProjectFlockKandangId;references:Id"`
+13 -3
View File
@@ -13,11 +13,14 @@ type Recording struct {
Day *int `gorm:"column:day"` Day *int `gorm:"column:day"`
TotalDepletionQty *float64 `gorm:"column:total_depletion_qty"` TotalDepletionQty *float64 `gorm:"column:total_depletion_qty"`
CumDepletionRate *float64 `gorm:"column:cum_depletion_rate"` CumDepletionRate *float64 `gorm:"column:cum_depletion_rate"`
DailyGain *float64 `gorm:"column:daily_gain"`
AvgDailyGain *float64 `gorm:"column:avg_daily_gain"`
CumIntake *int `gorm:"column:cum_intake"` CumIntake *int `gorm:"column:cum_intake"`
FcrValue *float64 `gorm:"column:fcr_value"` FcrValue *float64 `gorm:"column:fcr_value"`
TotalChickQty *float64 `gorm:"column:total_chick_qty"` TotalChickQty *float64 `gorm:"column:total_chick_qty"`
HandDay *float64 `gorm:"column:hand_day"`
HandHouse *float64 `gorm:"column:hand_house"`
FeedIntake *float64 `gorm:"column:feed_intake"`
EggMesh *float64 `gorm:"column:egg_mesh"`
EggWeight *float64 `gorm:"column:egg_weight"`
CreatedBy uint `gorm:"column:created_by"` CreatedBy uint `gorm:"column:created_by"`
CreatedAt time.Time `gorm:"autoCreateTime"` CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"` UpdatedAt time.Time `gorm:"autoUpdateTime"`
@@ -25,10 +28,17 @@ type Recording struct {
ProjectFlockKandang *ProjectFlockKandang `gorm:"foreignKey:ProjectFlockKandangId;references:Id"` ProjectFlockKandang *ProjectFlockKandang `gorm:"foreignKey:ProjectFlockKandangId;references:Id"`
CreatedUser *User `gorm:"foreignKey:CreatedBy;references:Id"` CreatedUser *User `gorm:"foreignKey:CreatedBy;references:Id"`
BodyWeights []RecordingBW `gorm:"foreignKey:RecordingId;references:Id"`
Depletions []RecordingDepletion `gorm:"foreignKey:RecordingId;references:Id"` Depletions []RecordingDepletion `gorm:"foreignKey:RecordingId;references:Id"`
Stocks []RecordingStock `gorm:"foreignKey:RecordingId;references:Id"` Stocks []RecordingStock `gorm:"foreignKey:RecordingId;references:Id"`
Eggs []RecordingEgg `gorm:"foreignKey:RecordingId;references:Id"` Eggs []RecordingEgg `gorm:"foreignKey:RecordingId;references:Id"`
LatestApproval *Approval `gorm:"-" json:"-"` LatestApproval *Approval `gorm:"-" json:"-"`
StandardHandDay *float64 `gorm:"-"`
StandardHandHouse *float64 `gorm:"-"`
StandardFeedIntake *float64 `gorm:"-"`
StandardMaxDepletion *float64 `gorm:"-"`
StandardEggMesh *float64 `gorm:"-"`
StandardEggWeight *float64 `gorm:"-"`
StandardFcr *float64 `gorm:"-"`
} }
-15
View File
@@ -1,15 +0,0 @@
package entities
import "time"
type RecordingBW struct {
Id uint `gorm:"primaryKey"`
RecordingId uint `gorm:"column:recording_id;not null;index"`
AvgWeight float64 `gorm:"column:avg_weight;not null"`
Qty float64 `gorm:"column:qty;not null"`
TotalWeight float64 `gorm:"column:total_weight;not null"`
CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
Recording Recording `gorm:"foreignKey:RecordingId;references:Id"`
}
+1
View File
@@ -44,6 +44,7 @@ const (
P_ReportExpenseGetAll = "lti.repport.expense.list" P_ReportExpenseGetAll = "lti.repport.expense.list"
P_ReportDeliveryGetAll = "lti.repport.delivery.list" P_ReportDeliveryGetAll = "lti.repport.delivery.list"
P_ReportPurchaseSupplierGetAll = "lti.repport.purchasesupplier.list" P_ReportPurchaseSupplierGetAll = "lti.repport.purchasesupplier.list"
P_ReportHppPerKandangGetAll = "lti.repport.gethppperkandang.list"
) )
const ( const (
@@ -407,7 +407,7 @@ SELECT
COALESCE(fw.name, '') AS source_warehouse, COALESCE(fw.name, '') AS source_warehouse,
COALESCE(tw.name, '') AS destination_warehouse, COALESCE(tw.name, '') AS destination_warehouse,
'' AS destination, '' AS destination,
std.quantity AS quantity, std.usage_qty AS quantity,
u.name AS unit, u.name AS unit,
'Stock Refill' AS notes 'Stock Refill' AS notes
FROM stock_transfer_details std FROM stock_transfer_details std
@@ -456,7 +456,7 @@ SELECT
COALESCE(fw.name, '') AS source_warehouse, COALESCE(fw.name, '') AS source_warehouse,
'' AS destination_warehouse, '' AS destination_warehouse,
COALESCE(tw.name, '') AS destination, COALESCE(tw.name, '') AS destination,
std.quantity AS quantity, std.usage_qty AS quantity,
u.name AS unit, u.name AS unit,
'Transfer to other unit' AS notes 'Transfer to other unit' AS notes
FROM stock_transfer_details std FROM stock_transfer_details std
@@ -927,34 +927,34 @@ func (r *ClosingRepositoryImpl) GetActualUsageCostByProjectFlockID(ctx context.C
COALESCE(SUM( COALESCE(SUM(
CASE CASE
WHEN sa.stockable_type = ? THEN COALESCE(sa.qty, 0) WHEN sa.stockable_type = ? THEN COALESCE(sa.qty, 0)
WHEN sa.stockable_type = ? THEN COALESCE(std.quantity, 0) WHEN sa.stockable_type = ? THEN COALESCE(std.usage_qty, 0)
ELSE 0 ELSE 0
END END
), 0) AS total_qty, ), 0) AS total_qty,
COALESCE(SUM( COALESCE(SUM(
CASE CASE
WHEN sa.stockable_type = ? THEN COALESCE(sa.qty, 0) * COALESCE(pi.price, 0) WHEN sa.stockable_type = ? THEN COALESCE(sa.qty, 0) * COALESCE(pi.price, 0)
WHEN sa.stockable_type = ? THEN COALESCE(std.quantity, 0) * COALESCE(tpi.price, 0) WHEN sa.stockable_type = ? THEN COALESCE(std.usage_qty, 0) * COALESCE(tpi.price, 0)
ELSE 0 ELSE 0
END END
), 0) AS total_price, ), 0) AS total_price,
COALESCE(SUM( COALESCE(SUM(
CASE CASE
WHEN sa.stockable_type = ? THEN COALESCE(sa.qty, 0) WHEN sa.stockable_type = ? THEN COALESCE(sa.qty, 0)
WHEN sa.stockable_type = ? THEN COALESCE(std.quantity, 0) WHEN sa.stockable_type = ? THEN COALESCE(std.usage_qty, 0)
ELSE 0 ELSE 0
END END
), 0) AS qty_divisor, ), 0) AS qty_divisor,
COALESCE(SUM( COALESCE(SUM(
CASE CASE
WHEN sa.stockable_type = ? THEN COALESCE(sa.qty, 0) * COALESCE(pi.price, 0) WHEN sa.stockable_type = ? THEN COALESCE(sa.qty, 0) * COALESCE(pi.price, 0)
WHEN sa.stockable_type = ? THEN COALESCE(std.quantity, 0) * COALESCE(tpi.price, 0) WHEN sa.stockable_type = ? THEN COALESCE(std.usage_qty, 0) * COALESCE(tpi.price, 0)
ELSE 0 ELSE 0
END END
), 0) / NULLIF(COALESCE(SUM( ), 0) / NULLIF(COALESCE(SUM(
CASE CASE
WHEN sa.stockable_type = ? THEN COALESCE(sa.qty, 0) WHEN sa.stockable_type = ? THEN COALESCE(sa.qty, 0)
WHEN sa.stockable_type = ? THEN COALESCE(std.quantity, 0) WHEN sa.stockable_type = ? THEN COALESCE(std.usage_qty, 0)
ELSE 0 ELSE 0
END END
), 0), 0) AS average_price`, ), 0), 0) AS average_price`,
+1 -1
View File
@@ -30,6 +30,6 @@ func ClosingRoutes(v1 fiber.Router, u user.UserService, s closing.ClosingService
route.Get("/:projectFlockId/sapronak", m.RequirePermissions(m.P_ClosingDetail), 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/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("/: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/production-data", m.RequirePermissions(m.P_ClosingDetail), ctrl.GetClosingDataProduksi)
route.Get("/:projectFlockId/keuangan", m.RequirePermissions(m.P_ClosingDetail), ctrl.GetClosingKeuangan) route.Get("/:projectFlockId/keuangan", m.RequirePermissions(m.P_ClosingDetail), ctrl.GetClosingKeuangan)
} }
@@ -0,0 +1,524 @@
package controller
import (
"math"
"strconv"
"gitlab.com/mbugroup/lti-api.git/internal/modules/daily-checklists/dto"
service "gitlab.com/mbugroup/lti-api.git/internal/modules/daily-checklists/services"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/daily-checklists/validations"
kandangDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/dto"
"gitlab.com/mbugroup/lti-api.git/internal/response"
"github.com/gofiber/fiber/v2"
)
type DailyChecklistController struct {
DailyChecklistService service.DailyChecklistService
}
func NewDailyChecklistController(dailyChecklistService service.DailyChecklistService) *DailyChecklistController {
return &DailyChecklistController{
DailyChecklistService: dailyChecklistService,
}
}
func (u *DailyChecklistController) GetAll(c *fiber.Ctx) error {
query := &validation.Query{
Page: c.QueryInt("page", 1),
Limit: c.QueryInt("limit", 10),
Search: c.Query("search", ""),
}
query.DateFrom = c.Query("date_from", "")
query.DateTo = c.Query("date_to", "")
query.Status = c.Query("status", "")
if kandangParam := c.Query("kandang_id", ""); kandangParam != "" {
kandangID, err := strconv.ParseUint(kandangParam, 10, 64)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid kandang_id")
}
value := uint(kandangID)
query.KandangID = &value
}
if query.Page < 1 || query.Limit < 1 {
return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0")
}
result, totalResults, err := u.DailyChecklistService.GetAll(c, query)
if err != nil {
return err
}
responseData := make([]dto.DailyChecklistListDTO, len(result))
for i, item := range result {
var name string
if item.Name != nil {
name = *item.Name
}
var status string
if item.Status != nil {
status = *item.Status
}
var kandang *kandangDTO.KandangRelationDTO
if item.Kandang.Id != 0 {
mapped := kandangDTO.ToKandangRelationDTO(item.Kandang)
kandang = &mapped
}
responseData[i] = dto.DailyChecklistListDTO{
Id: item.ID,
Name: name,
Status: status,
Category: item.Category,
Date: item.Date,
Kandang: kandang,
CreatedUser: nil,
CreatedAt: item.CreatedAt,
UpdatedAt: item.UpdatedAt,
TotalPhase: item.TotalPhase,
TotalActivity: item.TotalActivity,
Progress: item.Progress,
}
}
return c.Status(fiber.StatusOK).
JSON(response.SuccessWithPaginate[dto.DailyChecklistListDTO]{
Code: fiber.StatusOK,
Status: "success",
Message: "Get all dailyChecklists successfully",
Meta: response.Meta{
Page: query.Page,
Limit: query.Limit,
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
TotalResults: totalResults,
},
Data: responseData,
})
}
func (u *DailyChecklistController) GetSummary(c *fiber.Ctx) error {
query := &validation.SummaryQuery{
DateFrom: c.Query("date_from"),
DateTo: c.Query("date_to"),
Category: c.Query("category"),
}
if query.DateFrom == "" || query.DateTo == "" {
return fiber.NewError(fiber.StatusBadRequest, "date_from and date_to are required")
}
if kandangParam := c.Query("kandang_id"); kandangParam != "" {
kandangID, err := strconv.ParseUint(kandangParam, 10, 64)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid kandang_id")
}
value := uint(kandangID)
query.KandangID = &value
}
result, err := u.DailyChecklistService.GetSummary(c, query)
if err != nil {
return err
}
type summaryResponse struct {
PerformanceOverview []dto.DailyChecklistPerformanceOverviewDTO `json:"performance_overview"`
TrackingABK []dto.DailyChecklistSummaryDTO `json:"tracking_abk"`
}
performanceMap := make(map[uint]*dto.DailyChecklistPerformanceOverviewDTO)
tracking := make([]dto.DailyChecklistSummaryDTO, len(result))
for i, summary := range result {
tracking[i] = dto.DailyChecklistSummaryDTO{
EmployeeID: summary.EmployeeID,
EmployeeName: summary.EmployeeName,
KandangID: summary.KandangID,
KandangName: summary.KandangName,
TotalActivity: summary.TotalActivity,
ActivityDone: summary.ActivityDone,
ActivityLeft: summary.ActivityLeft,
CompletionRate: summary.CompletionRate,
LastActivity: summary.LastActivity,
}
if _, ok := performanceMap[summary.EmployeeID]; !ok {
performanceMap[summary.EmployeeID] = &dto.DailyChecklistPerformanceOverviewDTO{
EmployeeID: summary.EmployeeID,
EmployeeName: summary.EmployeeName,
}
}
performanceMap[summary.EmployeeID].TotalActivity += summary.TotalActivity
performanceMap[summary.EmployeeID].ActivityDone += summary.ActivityDone
performanceMap[summary.EmployeeID].ActivityLeft += summary.ActivityLeft
}
performance := make([]dto.DailyChecklistPerformanceOverviewDTO, 0, len(performanceMap))
for _, v := range performanceMap {
performance = append(performance, *v)
}
return c.Status(fiber.StatusOK).
JSON(response.Success{
Code: fiber.StatusOK,
Status: "success",
Message: "Get daily checklist summary successfully",
Data: summaryResponse{
PerformanceOverview: performance,
TrackingABK: tracking,
},
})
}
func (u *DailyChecklistController) GetReport(c *fiber.Ctx) error {
query := &validation.ReportQuery{
Page: c.QueryInt("page", 1),
Limit: c.QueryInt("limit", 10),
Month: c.QueryInt("bulan", 0),
Year: c.QueryInt("tahun", 0),
}
parseUintParam := func(param string) (*uint, error) {
if param == "" {
return nil, nil
}
value, err := strconv.ParseUint(param, 10, 64)
if err != nil {
return nil, err
}
u := uint(value)
return &u, nil
}
if val, err := parseUintParam(c.Query("area_id", "")); err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid area_id")
} else {
query.AreaID = val
}
if val, err := parseUintParam(c.Query("location_id", "")); err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid location_id")
} else {
query.LocationID = val
}
if val, err := parseUintParam(c.Query("kandang_id", "")); err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid kandang_id")
} else {
query.KandangID = val
}
if val, err := parseUintParam(c.Query("employee_id", "")); err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid employee_id")
} else {
query.EmployeeID = val
}
if val, err := parseUintParam(c.Query("phase_id", "")); err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid phase_id")
} else {
query.PhaseID = val
}
if query.Month == 0 || query.Year == 0 {
return fiber.NewError(fiber.StatusBadRequest, "bulan and tahun are required")
}
result, totalResults, err := u.DailyChecklistService.GetReport(c, query)
withoutActivities := func(src map[string]int) map[string]int {
if src == nil {
return map[string]int{}
}
return src
}
if err != nil {
return err
}
responseData := make([]dto.DailyChecklistReportDTO, len(result))
for i, item := range result {
responseData[i] = dto.DailyChecklistReportDTO{
Area: dto.DailyChecklistReportEntityDTO{
Id: item.AreaID,
Name: item.AreaName,
},
Farm: dto.DailyChecklistReportEntityDTO{
Id: item.LocationID,
Name: item.LocationName,
},
Kandang: dto.DailyChecklistReportEntityDTO{
Id: item.KandangID,
Name: item.KandangName,
},
ABK: dto.DailyChecklistReportEntityDTO{
Id: item.EmployeeID,
Name: item.EmployeeName,
},
Phase: item.PhaseName,
DailyActivities: withoutActivities(item.DailyActivities),
Summary: dto.DailyChecklistReportSummaryDTO{
TotalChecklist: item.Summary.TotalChecklist,
JumlahHariEfektif: item.Summary.JumlahHariEfektif,
AbkPercentage: item.Summary.AbkPercentage,
KandangPercentage: item.Summary.KandangPercentage,
Kategori: dto.DailyChecklistReportCategoryDTO{
Kurang: item.Summary.Category.Kurang,
Cukup: item.Summary.Category.Cukup,
Baik: item.Summary.Category.Baik,
},
},
}
}
return c.Status(fiber.StatusOK).
JSON(response.SuccessWithPaginate[dto.DailyChecklistReportDTO]{
Code: fiber.StatusOK,
Status: "success",
Message: "Get daily checklist report successfully",
Meta: response.Meta{
Page: query.Page,
Limit: query.Limit,
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
TotalResults: totalResults,
},
Data: responseData,
})
}
func (u *DailyChecklistController) GetOne(c *fiber.Ctx) error {
param := c.Params("idDailyChecklist")
id, err := strconv.Atoi(param)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
}
detail, err := u.DailyChecklistService.GetDetail(c, uint(id))
if err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.Success{
Code: fiber.StatusOK,
Status: "success",
Message: "Get dailyChecklist successfully",
Data: dto.ToDailyChecklistDetailDTO(detail.Checklist, detail.Phases, detail.Tasks, detail.AssignedEmployees, detail.TotalActivities, detail.Progress),
})
}
func (u *DailyChecklistController) 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.DailyChecklistService.CreateOne(c, req)
if err != nil {
return err
}
return c.Status(fiber.StatusCreated).
JSON(response.Success{
Code: fiber.StatusCreated,
Status: "success",
Message: "Create dailyChecklist successfully",
Data: dto.ToDailyChecklistListDTO(*result),
})
}
func (u *DailyChecklistController) UpdateOne(c *fiber.Ctx) error {
req := new(validation.Update)
param := c.Params("idDailyChecklist")
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.DailyChecklistService.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 dailyChecklist successfully",
Data: dto.ToDailyChecklistListDTO(*result),
})
}
func (u *DailyChecklistController) DeleteOne(c *fiber.Ctx) error {
param := c.Params("idDailyChecklist")
id, err := strconv.Atoi(param)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
}
if err := u.DailyChecklistService.DeleteOne(c, uint(id)); err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.Common{
Code: fiber.StatusOK,
Status: "success",
Message: "Delete dailyChecklist successfully",
})
}
func (u *DailyChecklistController) CreateDailyChecklistPhase(c *fiber.Ctx) error {
param := c.Params("idDailyChecklist")
id, err := strconv.Atoi(param)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid daily checklist id")
}
req := new(validation.AssignPhases)
if err := c.BodyParser(req); err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
}
if err := u.DailyChecklistService.AssignPhases(c, uint(id), req); err != nil {
return err
}
return c.Status(fiber.StatusCreated).
JSON(response.Success{
Code: fiber.StatusCreated,
Status: "success",
Message: "Daily checklist phases saved successfully",
})
}
func (u *DailyChecklistController) CreateAssignment(c *fiber.Ctx) error {
param := c.Params("idDailyChecklist")
id, err := strconv.Atoi(param)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid daily checklist id")
}
req := new(validation.AssignTask)
if err := c.BodyParser(req); err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
}
if err := u.DailyChecklistService.AssignTasks(c, uint(id), req); err != nil {
return err
}
return c.Status(fiber.StatusCreated).
JSON(response.Success{
Code: fiber.StatusCreated,
Status: "success",
Message: "Daily checklist assignments saved successfully",
})
}
func (u *DailyChecklistController) RemoveAssignment(c *fiber.Ctx) error {
dailyChecklistParam := c.Params("idDailyChecklist")
employeeParam := c.Params("idEmployee")
dailyChecklistID, err := strconv.Atoi(dailyChecklistParam)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid daily checklist id")
}
employeeID, err := strconv.Atoi(employeeParam)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid employee id")
}
if err := u.DailyChecklistService.RemoveAssignment(c, uint(dailyChecklistID), uint(employeeID)); err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.Common{
Code: fiber.StatusOK,
Status: "success",
Message: "Assignment removed successfully",
})
}
func (u *DailyChecklistController) GetPhaseByIdChecklist(c *fiber.Ctx) error {
param := c.Params("idDailyChecklist")
id, err := strconv.Atoi(param)
if err != nil || id <= 0 {
return fiber.NewError(fiber.StatusBadRequest, "Invalid daily checklist id")
}
phaseIDs, err := u.DailyChecklistService.GetChecklistPhaseIDs(c, uint(id))
if err != nil {
return err
}
responseData := make([]map[string]uint, len(phaseIDs))
for i, phaseID := range phaseIDs {
responseData[i] = map[string]uint{"phase_id": phaseID}
}
return c.Status(fiber.StatusOK).
JSON(response.Success{
Code: fiber.StatusOK,
Status: "success",
Message: "Get phases successfully",
Data: responseData,
})
}
func (u *DailyChecklistController) GetAllTasks(c *fiber.Ctx) error {
checklistParam := c.Query("checklist_id", "")
if checklistParam == "" {
return fiber.NewError(fiber.StatusBadRequest, "checklist_id is required")
}
checklistID, err := strconv.Atoi(checklistParam)
if err != nil || checklistID <= 0 {
return fiber.NewError(fiber.StatusBadRequest, "Invalid checklist_id")
}
result, err := u.DailyChecklistService.GetTasks(c, uint(checklistID))
if err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.Success{
Code: fiber.StatusOK,
Status: "success",
Message: "Get daily checklist tasks successfully",
Data: result,
})
}
func (u *DailyChecklistController) UpdateAssignment(c *fiber.Ctx) error {
req := new(validation.UpdateAssignment)
if err := c.BodyParser(req); err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
}
if err := u.DailyChecklistService.UpdateAssignment(c, req); err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.Success{
Code: fiber.StatusOK,
Status: "success",
Message: "Assignment updated successfully",
})
}
@@ -0,0 +1,232 @@
package dto
import (
"time"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
employeeDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/employees/dto"
kandangDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/dto"
phaseActivityDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/phase-activities/dto"
phasesDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/phasess/dto"
userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
)
// === DTO Structs ===
type DailyChecklistRelationDTO struct {
Id uint `json:"id"`
Name string `json:"name"`
}
type DailyChecklistListDTO struct {
Id uint `json:"id"`
Name string `json:"name"`
Status string `json:"status"`
Category string `json:"category"`
Date time.Time `json:"date"`
Kandang *kandangDTO.KandangRelationDTO `json:"kandang,omitempty"`
CreatedUser *userDTO.UserRelationDTO `json:"created_user"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
TotalPhase int `json:"total_phase"`
TotalActivity int `json:"total_activity"`
Progress int `json:"progress"`
}
type DailyChecklistDetailDTO struct {
DailyChecklistListDTO
Phases []DailyChecklistPhaseDTO `json:"phases"`
Tasks []DailyChecklistActivityTaskDTO `json:"tasks"`
AssignedEmployees []employeeDTO.EmployeesRelationDTO `json:"assigned_employees"`
TotalActivity int `json:"total_activity"`
Progress float64 `json:"progress"`
}
type DailyChecklistSummaryDTO struct {
EmployeeID uint `json:"employee_id"`
EmployeeName string `json:"employee_name"`
KandangID uint `json:"kandang_id"`
KandangName string `json:"kandang_name"`
TotalActivity int `json:"total_activity"`
ActivityDone int `json:"activity_done"`
ActivityLeft int `json:"activity_left"`
CompletionRate int `json:"completion_rate"`
LastActivity *time.Time `json:"last_activity,omitempty"`
}
type DailyChecklistPerformanceOverviewDTO struct {
EmployeeID uint `json:"employee_id"`
EmployeeName string `json:"employee_name"`
TotalActivity int `json:"total_activity"`
ActivityDone int `json:"activity_done"`
ActivityLeft int `json:"activity_left"`
}
type DailyChecklistReportDTO struct {
Area DailyChecklistReportEntityDTO `json:"area"`
Farm DailyChecklistReportEntityDTO `json:"farm"`
Kandang DailyChecklistReportEntityDTO `json:"kandang"`
ABK DailyChecklistReportEntityDTO `json:"abk"`
Phase string `json:"phase"`
DailyActivities map[string]int `json:"daily_activities"`
Summary DailyChecklistReportSummaryDTO `json:"summary"`
}
type DailyChecklistReportEntityDTO struct {
Id uint `json:"id"`
Name string `json:"name"`
}
type DailyChecklistReportSummaryDTO struct {
TotalChecklist int `json:"total_checklist"`
JumlahHariEfektif int `json:"jumlah_hari_efektif"`
AbkPercentage int `json:"abk_percentage"`
KandangPercentage int `json:"kandang_percentage"`
Kategori DailyChecklistReportCategoryDTO `json:"kategori"`
}
type DailyChecklistReportCategoryDTO struct {
Kurang int `json:"kurang"`
Cukup int `json:"cukup"`
Baik int `json:"baik"`
}
type DailyChecklistPhaseDTO struct {
Id uint `json:"id"`
PhaseId uint `json:"phase_id"`
Phase phasesDTO.PhasesListDTO `json:"phase"`
}
type DailyChecklistActivityTaskDTO struct {
Id uint `json:"id"`
ChecklistId uint `json:"checklist_id"`
PhaseId uint `json:"phase_id"`
PhaseActivityId uint `json:"phase_activity_id"`
TimeType *string `json:"time_type"`
Notes *string `json:"notes"`
Phase phasesDTO.PhasesListDTO `json:"phase"`
PhaseActivity phaseActivityDTO.PhaseActivityListDTO `json:"phase_activity"`
Assignments []DailyChecklistAssignmentDTO `json:"assignments"`
}
type DailyChecklistAssignmentDTO struct {
Employee employeeDTO.EmployeesRelationDTO `json:"employee"`
Checked bool `json:"checked"`
Note *string `json:"note"`
}
// === Mapper Functions ===
func ToDailyChecklistRelationDTO(e entity.DailyChecklist) DailyChecklistRelationDTO {
var name string
if e.Name != nil {
name = *e.Name
}
return DailyChecklistRelationDTO{
Id: e.Id,
Name: name,
}
}
func ToDailyChecklistListDTO(e entity.DailyChecklist) DailyChecklistListDTO {
var createdUser *userDTO.UserRelationDTO
// if e.CreatedUser.Id != 0 {
// mapped := userDTO.ToUserRelationDTO(e.CreatedUser)
// createdUser = &mapped
// }
var name string
if e.Name != nil {
name = *e.Name
}
var status string
if e.Status != nil {
status = *e.Status
}
var kandang *kandangDTO.KandangRelationDTO
if e.Kandang.Id != 0 {
mapped := kandangDTO.ToKandangRelationDTO(e.Kandang)
kandang = &mapped
}
return DailyChecklistListDTO{
Id: e.Id,
Name: name,
Status: status,
Category: e.Category,
Date: e.Date,
Kandang: kandang,
CreatedAt: e.CreatedAt,
UpdatedAt: e.UpdatedAt,
CreatedUser: createdUser,
TotalPhase: 0,
TotalActivity: 0,
Progress: 0,
}
}
func ToDailyChecklistDetailDTO(checklist entity.DailyChecklist, phases []entity.DailyChecklistPhase, tasks []entity.DailyChecklistActivityTask, assignedEmployees []entity.Employee, totalActivities int, progress float64) DailyChecklistDetailDTO {
phaseDTOs := make([]DailyChecklistPhaseDTO, 0, len(phases))
for _, phase := range phases {
phaseDTOs = append(phaseDTOs, DailyChecklistPhaseDTO{
Id: phase.Id,
PhaseId: phase.PhaseId,
Phase: phasesDTO.ToPhasesListDTO(phase.Phase),
})
}
taskDTOs := make([]DailyChecklistActivityTaskDTO, 0, len(tasks))
for _, task := range tasks {
mappedAssignments := make([]DailyChecklistAssignmentDTO, 0, len(task.Assignments))
for _, assignment := range task.Assignments {
if assignment.Employee.Id == 0 {
continue
}
mapped := DailyChecklistAssignmentDTO{
Employee: employeeDTO.ToEmployeesRelationDTO(assignment.Employee),
Checked: assignment.Checked,
Note: assignment.Note,
}
mappedAssignments = append(mappedAssignments, mapped)
}
phaseDTO := phasesDTO.PhasesListDTO{}
if task.Phase.Id != 0 {
phaseDTO = phasesDTO.ToPhasesListDTO(task.Phase)
}
activityDTO := phaseActivityDTO.PhaseActivityListDTO{}
if task.PhaseActivity.Id != 0 {
activityDTO = phaseActivityDTO.ToPhaseActivityListDTO(task.PhaseActivity)
}
taskDTOs = append(taskDTOs, DailyChecklistActivityTaskDTO{
Id: task.Id,
ChecklistId: task.ChecklistId,
PhaseId: task.PhaseId,
PhaseActivityId: task.PhaseActivityId,
TimeType: task.TimeType,
Notes: task.Notes,
Phase: phaseDTO,
PhaseActivity: activityDTO,
Assignments: mappedAssignments,
})
}
assignedDTOs := make([]employeeDTO.EmployeesRelationDTO, 0, len(assignedEmployees))
for _, emp := range assignedEmployees {
assignedDTOs = append(assignedDTOs, employeeDTO.ToEmployeesRelationDTO(emp))
}
return DailyChecklistDetailDTO{
DailyChecklistListDTO: ToDailyChecklistListDTO(checklist),
Phases: phaseDTOs,
Tasks: taskDTOs,
AssignedEmployees: assignedDTOs,
TotalActivity: totalActivities,
Progress: progress,
}
}
@@ -0,0 +1,27 @@
package dailyChecklists
import (
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
rDailyChecklist "gitlab.com/mbugroup/lti-api.git/internal/modules/daily-checklists/repositories"
sDailyChecklist "gitlab.com/mbugroup/lti-api.git/internal/modules/daily-checklists/services"
rPhases "gitlab.com/mbugroup/lti-api.git/internal/modules/master/phasess/repositories"
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
sUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
)
type DailyChecklistModule struct{}
func (DailyChecklistModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
dailyChecklistRepo := rDailyChecklist.NewDailyChecklistRepository(db)
phasesRepo := rPhases.NewPhasesRepository(db)
userRepo := rUser.NewUserRepository(db)
dailyChecklistService := sDailyChecklist.NewDailyChecklistService(dailyChecklistRepo, phasesRepo, validate)
userService := sUser.NewUserService(userRepo, validate)
DailyChecklistRoutes(router, userService, dailyChecklistService)
}
@@ -0,0 +1,21 @@
package repository
import (
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
"gorm.io/gorm"
)
type DailyChecklistRepository interface {
repository.BaseRepository[entity.DailyChecklist]
}
type DailyChecklistRepositoryImpl struct {
*repository.BaseRepositoryImpl[entity.DailyChecklist]
}
func NewDailyChecklistRepository(db *gorm.DB) DailyChecklistRepository {
return &DailyChecklistRepositoryImpl{
BaseRepositoryImpl: repository.NewBaseRepository[entity.DailyChecklist](db),
}
}
@@ -0,0 +1,63 @@
package dailyChecklists
import (
// m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/daily-checklists/controllers"
dailyChecklist "gitlab.com/mbugroup/lti-api.git/internal/modules/daily-checklists/services"
user "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
"github.com/gofiber/fiber/v2"
)
func DailyChecklistRoutes(v1 fiber.Router, u user.UserService, s dailyChecklist.DailyChecklistService) {
ctrl := controller.NewDailyChecklistController(s)
route := v1.Group("/daily-checklists")
// route.Use(m.Auth(u))
route.Get("/", ctrl.GetAll)
route.Get("/report", ctrl.GetReport)
route.Get("/summary", ctrl.GetSummary)
route.Get("/report", ctrl.GetReport)
// create daily checklist
route.Post("/", ctrl.CreateOne)
// get detail data daily checklist by id
route.Get("/relation/:idDailyChecklist", ctrl.GetOne)
// get phases by daily checklist id
route.Get("/phase/:idDailyChecklist", ctrl.GetPhaseByIdChecklist)
// create task
/*
ketika add phase
*/
route.Post("/phase/:idDailyChecklist", ctrl.CreateDailyChecklistPhase)
// create assigment
/*
ketika add ABK
*/
route.Post("/assignment/:idDailyChecklist", ctrl.CreateAssignment)
// remove assignment
/*
ketika remove ABK
*/
route.Delete("/:idDailyChecklist/assignments/:idEmployee", ctrl.RemoveAssignment)
//get all tasks
route.Get("/tasks", ctrl.GetAllTasks)
// update assignment
/*
ketika check dan uncheck tugas oleh ABK
*/
route.Post("/assignment", ctrl.UpdateAssignment)
route.Patch("/:idDailyChecklist", ctrl.UpdateOne)
route.Delete("/:idDailyChecklist", ctrl.DeleteOne)
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,57 @@
package validation
type Create struct {
Date string `json:"date" validate:"required"`
KandangId uint `json:"kandang_id" validate:"required"`
Category string `json:"category" validate:"required"`
Status string `json:"status" validate:"required"`
}
type Update struct {
Status string `json:"status" validate:"required"`
RejectReason *string `json:"reject_reason"`
}
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"`
DateFrom string `query:"date_from" validate:"omitempty"`
DateTo string `query:"date_to" validate:"omitempty"`
Status string `query:"status" validate:"omitempty"`
KandangID *uint `query:"kandang_id" validate:"omitempty"`
}
type AssignPhases struct {
PhaseIDs string `json:"phase_ids" validate:"required"`
}
type AssignTask struct {
EmployeeIDs string `json:"employee_ids" validate:"required"`
}
type UpdateAssignment struct {
TaskID uint `json:"task_id" validate:"required"`
EmployeeID uint `json:"employee_id" validate:"required"`
Checked *bool `json:"checked,omitempty"`
Note *string `json:"note,omitempty"`
}
type SummaryQuery struct {
DateFrom string `query:"date_from" validate:"required"`
DateTo string `query:"date_to" validate:"required"`
Category string `query:"category" validate:"omitempty"`
KandangID *uint `query:"kandang_id" validate:"omitempty"`
}
type ReportQuery struct {
Page int `query:"page" validate:"required,number,min=1,gt=0"`
Limit int `query:"limit" validate:"required,number,min=1,max=100,gt=0"`
Month int `query:"bulan" validate:"required,number,min=1,max=12"`
Year int `query:"tahun" validate:"required,number,min=1900"`
AreaID *uint `query:"area_id" validate:"omitempty"`
LocationID *uint `query:"location_id" validate:"omitempty"`
KandangID *uint `query:"kandang_id" validate:"omitempty"`
EmployeeID *uint `query:"employee_id" validate:"omitempty"`
PhaseID *uint `query:"phase_id" validate:"omitempty"`
}
@@ -203,12 +203,12 @@ func (u *ExpenseController) UpdateOne(c *fiber.Ctx) error {
func (u *ExpenseController) DeleteOne(c *fiber.Ctx) error { func (u *ExpenseController) DeleteOne(c *fiber.Ctx) error {
param := c.Params("id") param := c.Params("id")
id, err := strconv.Atoi(param) id64, err := strconv.ParseUint(param, 10, 64)
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id") return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
} }
if err := u.ExpenseService.DeleteOne(c, uint(id)); err != nil { if err := u.ExpenseService.DeleteOne(c, id64); err != nil {
return err return err
} }
+3 -5
View File
@@ -105,11 +105,9 @@ func ToExpenseBaseDTO(e *entity.Expense) ExpenseBaseDTO {
realizationDate = &e.RealizationDate realizationDate = &e.RealizationDate
} }
if len(e.Nonstocks) > 0 && e.Nonstocks[0].Kandang != nil { if e.Location != nil && e.Location.Id != 0 {
if e.Nonstocks[0].Kandang.Location.Id != 0 { mapped := locationDTO.ToLocationRelationDTO(*e.Location)
mapped := locationDTO.ToLocationRelationDTO(e.Nonstocks[0].Kandang.Location) location = &mapped
location = &mapped
}
} }
if e.Supplier != nil && e.Supplier.Id != 0 { if e.Supplier != nil && e.Supplier.Id != 0 {
@@ -3,6 +3,8 @@ package repository
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"time"
"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"
@@ -17,6 +19,7 @@ type ExpenseRepository interface {
GetWithSupplier(ctx context.Context, id uint64) (*entity.Expense, error) GetWithSupplier(ctx context.Context, id uint64) (*entity.Expense, error)
WithProjectFlockKandangFilter(pfkID, kandangID uint) func(*gorm.DB) *gorm.DB WithProjectFlockKandangFilter(pfkID, kandangID uint) func(*gorm.DB) *gorm.DB
CountUnfinishedByProjectFlockKandang(ctx context.Context, pfkID, kandangID uint, isFinished func(*entity.Approval) bool) (int64, error) CountUnfinishedByProjectFlockKandang(ctx context.Context, pfkID, kandangID uint, isFinished func(*entity.Approval) bool) (int64, error)
DeleteOne(ctx context.Context, id uint) error
} }
type ExpenseRepositoryImpl struct { type ExpenseRepositoryImpl struct {
@@ -107,3 +110,23 @@ func (r *ExpenseRepositoryImpl) CountUnfinishedByProjectFlockKandang(ctx context
} }
return unfinished, nil return unfinished, nil
} }
func (r *ExpenseRepositoryImpl) DeleteOne(ctx context.Context, id uint) error {
// Cast to uint64 to match entity.Id type
id64 := uint64(id)
deletedAt := time.Now()
// Use raw SQL with interpolated integer to avoid type issues
// Interpolate id directly as integer literal (safe because it's uint64)
result := r.DB().WithContext(ctx).
Exec(`UPDATE "expenses" SET "deleted_at" = $1 WHERE "id" = `+fmt.Sprintf("%d", id64)+` AND "deleted_at" IS NULL`,
deletedAt)
if result.Error != nil {
return result.Error
}
if result.RowsAffected == 0 {
return gorm.ErrRecordNotFound
}
return nil
}
+12 -12
View File
@@ -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("/",m.RequirePermissions(m.P_ExpenseGetAll), ctrl.GetAll) route.Get("/", m.RequirePermissions(m.P_ExpenseGetAll), ctrl.GetAll)
route.Post("/",m.RequirePermissions(m.P_ExpenseCreateOne), ctrl.CreateOne) route.Post("/", m.RequirePermissions(m.P_ExpenseCreateOne), ctrl.CreateOne)
route.Get("/:id",m.RequirePermissions(m.P_ExpenseGetOne), ctrl.GetOne) route.Get("/:id", m.RequirePermissions(m.P_ExpenseGetOne), ctrl.GetOne)
route.Patch("/:id",m.RequirePermissions(m.P_ExpenseUpdateOne), ctrl.UpdateOne) route.Patch("/:id", m.RequirePermissions(m.P_ExpenseUpdateOne), ctrl.UpdateOne)
route.Delete("/:id",m.RequirePermissions(m.P_ExpenseDeleteOne), ctrl.DeleteOne) route.Delete("/:id", m.RequirePermissions(m.P_ExpenseDeleteOne), ctrl.DeleteOne)
route.Post("/approvals/manager",m.RequirePermissions(m.P_ExpenseApprovalManager), ctrl.Approval) route.Post("/approvals/manager", m.RequirePermissions(m.P_ExpenseApprovalManager), ctrl.Approval)
route.Post("/approvals/finance",m.RequirePermissions(m.P_ExpenseApprovalFinance), ctrl.Approval) route.Post("/approvals/finance", m.RequirePermissions(m.P_ExpenseApprovalFinance), ctrl.Approval)
route.Post("/:id/realizations",m.RequirePermissions(m.P_ExpenseCreateRealizations), ctrl.CreateRealization) route.Post("/:id/realizations", m.RequirePermissions(m.P_ExpenseCreateRealizations), ctrl.CreateRealization)
route.Patch("/:id/realizations",m.RequirePermissions(m.P_ExpenseUpdateRealizations), ctrl.UpdateRealization) route.Patch("/:id/realizations", m.RequirePermissions(m.P_ExpenseUpdateRealizations), ctrl.UpdateRealization)
route.Post("/:id/complete",m.RequirePermissions(m.P_ExpenseCompleteExpense), ctrl.CompleteExpense) route.Post("/:id/complete", m.RequirePermissions(m.P_ExpenseCompleteExpense), ctrl.CompleteExpense)
route.Delete("/:id/documents/:documentId",m.RequirePermissions(m.P_ExpenseDocument), ctrl.DeleteDocument) route.Delete("/:id/documents/:documentId", m.RequirePermissions(m.P_ExpenseDocument), ctrl.DeleteDocument)
route.Delete("/:id/realization-documents/:documentId",m.RequirePermissions(m.P_ExpenseDocumentRealizations), ctrl.DeleteRealizationDocument) route.Delete("/:id/realization-documents/:documentId", m.RequirePermissions(m.P_ExpenseDocumentRealizations), ctrl.DeleteRealizationDocument)
} }
@@ -30,7 +30,7 @@ type ExpenseService interface {
GetOne(ctx *fiber.Ctx, id uint) (*expenseDto.ExpenseDetailDTO, error) GetOne(ctx *fiber.Ctx, id uint) (*expenseDto.ExpenseDetailDTO, error)
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*expenseDto.ExpenseDetailDTO, error) CreateOne(ctx *fiber.Ctx, req *validation.Create) (*expenseDto.ExpenseDetailDTO, error)
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*expenseDto.ExpenseDetailDTO, error) UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*expenseDto.ExpenseDetailDTO, error)
DeleteOne(ctx *fiber.Ctx, id uint) error DeleteOne(ctx *fiber.Ctx, id uint64) error
CreateRealization(ctx *fiber.Ctx, expenseID uint, req *validation.CreateRealization) (*expenseDto.ExpenseDetailDTO, error) CreateRealization(ctx *fiber.Ctx, expenseID uint, req *validation.CreateRealization) (*expenseDto.ExpenseDetailDTO, error)
CompleteExpense(ctx *fiber.Ctx, id uint, notes *string) (*expenseDto.ExpenseDetailDTO, error) CompleteExpense(ctx *fiber.Ctx, id uint, notes *string) (*expenseDto.ExpenseDetailDTO, error)
UpdateRealization(ctx *fiber.Ctx, expenseID uint, req *validation.UpdateRealization) (*expenseDto.ExpenseDetailDTO, error) UpdateRealization(ctx *fiber.Ctx, expenseID uint, req *validation.UpdateRealization) (*expenseDto.ExpenseDetailDTO, error)
@@ -68,6 +68,7 @@ func (s expenseService) withRelations(db *gorm.DB) *gorm.DB {
return db. return db.
Preload("CreatedUser"). Preload("CreatedUser").
Preload("Supplier"). Preload("Supplier").
Preload("Location").
Preload("Nonstocks.Nonstock"). Preload("Nonstocks.Nonstock").
Preload("Nonstocks.Realization"). Preload("Nonstocks.Realization").
Preload("Nonstocks.ProjectFlockKandang.Kandang.Location"). Preload("Nonstocks.ProjectFlockKandang.Kandang.Location").
@@ -214,21 +215,19 @@ func (s *expenseService) CreateOne(c *fiber.Ctx, req *validation.Create) (*expen
return fiber.NewError(fiber.StatusInternalServerError, "Failed to get active project flocks for location") return fiber.NewError(fiber.StatusInternalServerError, "Failed to get active project flocks for location")
} }
if len(activeProjectFlocks) == 0 { if len(activeProjectFlocks) > 0 {
return fiber.NewError(fiber.StatusBadRequest, "No active project flocks found for this location") projectFlockIDs := make([]uint64, len(activeProjectFlocks))
} for i, pf := range activeProjectFlocks {
projectFlockIDs[i] = uint64(pf.Id)
}
projectFlockIDs := make([]uint64, len(activeProjectFlocks)) projectFlockIdsJSON, err := json.Marshal(projectFlockIDs)
for i, pf := range activeProjectFlocks { if err != nil {
projectFlockIDs[i] = uint64(pf.Id) return fiber.NewError(fiber.StatusInternalServerError, "Failed to marshal project_flock_ids")
}
jsonStr := string(projectFlockIdsJSON)
projectFlockIdJSON = &jsonStr
} }
projectFlockIdsJSON, err := json.Marshal(projectFlockIDs)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, "Failed to marshal project_flock_ids")
}
jsonStr := string(projectFlockIdsJSON)
projectFlockIdJSON = &jsonStr
} }
expense = &entity.Expense{ expense = &entity.Expense{
@@ -621,14 +620,15 @@ func (s expenseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
return responseDTO, nil return responseDTO, nil
} }
func (s expenseService) DeleteOne(c *fiber.Ctx, id uint) error { func (s expenseService) DeleteOne(c *fiber.Ctx, id uint64) error {
idUint := uint(id)
if err := commonSvc.EnsureRelations(c.Context(), if err := commonSvc.EnsureRelations(c.Context(),
commonSvc.RelationCheck{Name: "Expense", ID: &id, Exists: s.Repository.IdExists}, commonSvc.RelationCheck{Name: "Expense", ID: &idUint, Exists: s.Repository.IdExists},
); err != nil { ); err != nil {
return err return err
} }
expense, err := s.Repository.GetByID(c.Context(), id, func(db *gorm.DB) *gorm.DB { expense, err := s.Repository.GetByID(c.Context(), idUint, func(db *gorm.DB) *gorm.DB {
return db.Preload("Nonstocks") return db.Preload("Nonstocks")
}) })
if err != nil { if err != nil {
@@ -643,7 +643,7 @@ func (s expenseService) DeleteOne(c *fiber.Ctx, id uint) error {
if err := s.ensureProjectFlockNotClosedForExpense(c.Context(), expense); err != nil { if err := s.ensureProjectFlockNotClosedForExpense(c.Context(), expense); err != nil {
return err return err
} }
if err := s.Repository.DeleteOne(c.Context(), id); err != nil { if err := s.Repository.DeleteOne(c.Context(), idUint); err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
s.Log.Errorf("Expense not found for ID %d: %+v", id, err) s.Log.Errorf("Expense not found for ID %d: %+v", id, err)
return fiber.NewError(fiber.StatusNotFound, "Expense not found") return fiber.NewError(fiber.StatusNotFound, "Expense not found")
@@ -118,10 +118,9 @@ func (s *adjustmentService) Adjustment(c *fiber.Ctx, req *validation.Create) (*e
var createdLogId uint var createdLogId uint
var projectFlockKandangID *uint var projectFlockKandangID *uint
pfk, err := s.ProjectFlockKandangRepo.GetActiveByKandangID(ctx, uint(req.WarehouseID)) pfkID, err := s.getActiveProjectFlockKandangID(ctx, uint(req.WarehouseID))
if err == nil && pfk != nil { if err == nil && pfkID > 0 {
idCopy := uint(pfk.Id) projectFlockKandangID = &pfkID
projectFlockKandangID = &idCopy
} }
pw, err := s.ProductWarehouseRepo.FindByProductWarehouseAndPfk( pw, err := s.ProductWarehouseRepo.FindByProductWarehouseAndPfk(
@@ -195,13 +194,17 @@ func (s *adjustmentService) Adjustment(c *fiber.Ctx, req *validation.Create) (*e
StockLogId: newLog.Id, StockLogId: newLog.Id,
ProductWarehouseId: productWarehouse.Id, ProductWarehouseId: productWarehouse.Id,
} }
if err := s.AdjustmentStockRepository.WithTx(tx).CreateOne(ctx, adjustmentStock, nil); err != nil {
s.Log.Errorf("Failed to create adjustment stock: %+v", err)
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create adjustment stock record")
}
if transactionType == string(utils.StockLogTransactionTypeIncrease) { if transactionType == string(utils.StockLogTransactionTypeIncrease) {
// Adjustment INCREASE → Replenish stock (Stockable) // Adjustment INCREASE → Replenish stock (Stockable)
note := fmt.Sprintf("Stock Adjustment IN #%d", newLog.Id) note := fmt.Sprintf("Stock Adjustment IN #%d", newLog.Id)
replenishResult, err := s.FifoSvc.Replenish(ctx, common.StockReplenishRequest{ _, err := s.FifoSvc.Replenish(ctx, common.StockReplenishRequest{
StockableKey: "ADJUSTMENT_IN", StockableKey: "ADJUSTMENT_IN",
StockableID: newLog.Id, StockableID: adjustmentStock.Id,
ProductWarehouseID: uint(productWarehouse.Id), ProductWarehouseID: uint(productWarehouse.Id),
Quantity: req.Quantity, Quantity: req.Quantity,
Note: &note, Note: &note,
@@ -211,15 +214,11 @@ func (s *adjustmentService) Adjustment(c *fiber.Ctx, req *validation.Create) (*e
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to replenish stock via FIFO: %v", err)) return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to replenish stock via FIFO: %v", err))
} }
// Update stockable tracking fields
adjustmentStock.TotalQty = replenishResult.AddedQuantity
adjustmentStock.TotalUsed = 0
} else { } else {
// Adjustment DECREASE → Consume stock (Usable) // Adjustment DECREASE → Consume stock (Usable)
consumeResult, err := s.FifoSvc.Consume(ctx, common.StockConsumeRequest{ _, err := s.FifoSvc.Consume(ctx, common.StockConsumeRequest{
UsableKey: "ADJUSTMENT_OUT", UsableKey: "ADJUSTMENT_OUT",
UsableID: newLog.Id, UsableID: adjustmentStock.Id,
ProductWarehouseID: uint(productWarehouse.Id), ProductWarehouseID: uint(productWarehouse.Id),
Quantity: req.Quantity, Quantity: req.Quantity,
AllowPending: false, // Don't allow pending for adjustment AllowPending: false, // Don't allow pending for adjustment
@@ -228,16 +227,6 @@ func (s *adjustmentService) Adjustment(c *fiber.Ctx, req *validation.Create) (*e
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Failed to consume stock via FIFO: %v", err)) return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Failed to consume stock via FIFO: %v", err))
} }
// Update usable tracking fields
adjustmentStock.UsageQty = consumeResult.UsageQuantity
adjustmentStock.PendingQty = consumeResult.PendingQuantity
}
// Save AdjustmentStock record
if err := s.AdjustmentStockRepository.WithTx(tx).CreateOne(ctx, adjustmentStock, nil); err != nil {
s.Log.Errorf("Failed to create adjustment stock: %+v", err)
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create adjustment stock record")
} }
// Update ProductWarehouse quantity (for backward compatibility/reporting) // Update ProductWarehouse quantity (for backward compatibility/reporting)
@@ -216,6 +216,31 @@ func (r *ProductWarehouseRepositoryImpl) CleanupEmpty(ctx context.Context, affec
return nil return nil
} }
var inUseIDs []uint
if err := r.DB().WithContext(ctx).
Model(&entity.PurchaseItem{}).
Where("product_warehouse_id IN ?", emptyIDs).
Distinct().
Pluck("product_warehouse_id", &inUseIDs).Error; err != nil {
return err
}
if len(inUseIDs) > 0 {
inUse := make(map[uint]struct{}, len(inUseIDs))
for _, id := range inUseIDs {
inUse[id] = struct{}{}
}
filtered := make([]uint, 0, len(emptyIDs))
for _, id := range emptyIDs {
if _, exists := inUse[id]; !exists {
filtered = append(filtered, id)
}
}
emptyIDs = filtered
}
if len(emptyIDs) == 0 {
return nil
}
if err := r.DB().WithContext(ctx). if err := r.DB().WithContext(ctx).
Model(&entity.PurchaseItem{}). Model(&entity.PurchaseItem{}).
Where("product_warehouse_id IN ?", emptyIDs). Where("product_warehouse_id IN ?", emptyIDs).
@@ -0,0 +1,144 @@
package controller
import (
"math"
"strconv"
"gitlab.com/mbugroup/lti-api.git/internal/modules/master/config-checklists/dto"
service "gitlab.com/mbugroup/lti-api.git/internal/modules/master/config-checklists/services"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/config-checklists/validations"
"gitlab.com/mbugroup/lti-api.git/internal/response"
"github.com/gofiber/fiber/v2"
)
type ConfigChecklistController struct {
ConfigChecklistService service.ConfigChecklistService
}
func NewConfigChecklistController(configChecklistService service.ConfigChecklistService) *ConfigChecklistController {
return &ConfigChecklistController{
ConfigChecklistService: configChecklistService,
}
}
func (u *ConfigChecklistController) 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.ConfigChecklistService.GetAll(c, query)
if err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.SuccessWithPaginate[dto.ConfigChecklistListDTO]{
Code: fiber.StatusOK,
Status: "success",
Message: "Get all configChecklists successfully",
Meta: response.Meta{
Page: query.Page,
Limit: query.Limit,
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
TotalResults: totalResults,
},
Data: dto.ToConfigChecklistListDTOs(result),
})
}
func (u *ConfigChecklistController) 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.ConfigChecklistService.GetOne(c, uint(id))
if err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.Success{
Code: fiber.StatusOK,
Status: "success",
Message: "Get configChecklist successfully",
Data: dto.ToConfigChecklistListDTO(*result),
})
}
func (u *ConfigChecklistController) 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.ConfigChecklistService.CreateOne(c, req)
if err != nil {
return err
}
return c.Status(fiber.StatusCreated).
JSON(response.Success{
Code: fiber.StatusCreated,
Status: "success",
Message: "Create configChecklist successfully",
Data: dto.ToConfigChecklistListDTO(*result),
})
}
func (u *ConfigChecklistController) 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.ConfigChecklistService.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 configChecklist successfully",
Data: dto.ToConfigChecklistListDTO(*result),
})
}
func (u *ConfigChecklistController) 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.ConfigChecklistService.DeleteOne(c, uint(id)); err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.Common{
Code: fiber.StatusOK,
Status: "success",
Message: "Delete configChecklist successfully",
})
}
@@ -0,0 +1,61 @@
package dto
import (
"time"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
)
// === DTO Structs ===
type ConfigChecklistRelationDTO struct {
Id uint `json:"id"`
Date time.Time `json:"date"`
}
type ConfigChecklistListDTO struct {
Id uint `json:"id"`
Date time.Time `json:"date"`
PercentageThresholdBad int `json:"percentage_threshold_bad"`
PercentageThresholdEnough int `json:"percentage_threshold_enough"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type ConfigChecklistDetailDTO struct {
ConfigChecklistListDTO
}
// === Mapper Functions ===
func ToConfigChecklistRelationDTO(e entity.ConfigChecklist) ConfigChecklistRelationDTO {
return ConfigChecklistRelationDTO{
Id: e.Id,
Date: e.Date,
}
}
func ToConfigChecklistListDTO(e entity.ConfigChecklist) ConfigChecklistListDTO {
return ConfigChecklistListDTO{
Id: e.Id,
Date: e.Date,
PercentageThresholdBad: e.PercentageThresholdBad,
PercentageThresholdEnough: e.PercentageThresholdEnough,
CreatedAt: e.CreatedAt,
UpdatedAt: e.UpdatedAt,
}
}
func ToConfigChecklistListDTOs(e []entity.ConfigChecklist) []ConfigChecklistListDTO {
result := make([]ConfigChecklistListDTO, len(e))
for i, r := range e {
result[i] = ToConfigChecklistListDTO(r)
}
return result
}
func ToConfigChecklistDetailDTO(e entity.ConfigChecklist) ConfigChecklistDetailDTO {
return ConfigChecklistDetailDTO{
ConfigChecklistListDTO: ToConfigChecklistListDTO(e),
}
}
@@ -0,0 +1,25 @@
package configChecklists
import (
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
rConfigChecklist "gitlab.com/mbugroup/lti-api.git/internal/modules/master/config-checklists/repositories"
sConfigChecklist "gitlab.com/mbugroup/lti-api.git/internal/modules/master/config-checklists/services"
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
sUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
)
type ConfigChecklistModule struct{}
func (ConfigChecklistModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
configChecklistRepo := rConfigChecklist.NewConfigChecklistRepository(db)
userRepo := rUser.NewUserRepository(db)
configChecklistService := sConfigChecklist.NewConfigChecklistService(configChecklistRepo, validate)
userService := sUser.NewUserService(userRepo, validate)
ConfigChecklistRoutes(router, userService, configChecklistService)
}
@@ -0,0 +1,21 @@
package repository
import (
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
"gorm.io/gorm"
)
type ConfigChecklistRepository interface {
repository.BaseRepository[entity.ConfigChecklist]
}
type ConfigChecklistRepositoryImpl struct {
*repository.BaseRepositoryImpl[entity.ConfigChecklist]
}
func NewConfigChecklistRepository(db *gorm.DB) ConfigChecklistRepository {
return &ConfigChecklistRepositoryImpl{
BaseRepositoryImpl: repository.NewBaseRepository[entity.ConfigChecklist](db),
}
}
@@ -0,0 +1,23 @@
package configChecklists
import (
m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/master/config-checklists/controllers"
configChecklist "gitlab.com/mbugroup/lti-api.git/internal/modules/master/config-checklists/services"
user "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
"github.com/gofiber/fiber/v2"
)
func ConfigChecklistRoutes(v1 fiber.Router, u user.UserService, s configChecklist.ConfigChecklistService) {
ctrl := controller.NewConfigChecklistController(s)
route := v1.Group("/config-checklists")
route.Use(m.Auth(u))
route.Get("/", ctrl.GetAll)
route.Post("/", ctrl.CreateOne)
route.Get("/:id", ctrl.GetOne)
route.Patch("/:id", ctrl.UpdateOne)
route.Delete("/:id", ctrl.DeleteOne)
}
@@ -0,0 +1,146 @@
package service
import (
"errors"
"time"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/master/config-checklists/repositories"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/config-checklists/validations"
"gitlab.com/mbugroup/lti-api.git/internal/utils"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
)
type ConfigChecklistService interface {
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.ConfigChecklist, int64, error)
GetOne(ctx *fiber.Ctx, id uint) (*entity.ConfigChecklist, error)
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.ConfigChecklist, error)
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.ConfigChecklist, error)
DeleteOne(ctx *fiber.Ctx, id uint) error
}
type configChecklistService struct {
Log *logrus.Logger
Validate *validator.Validate
Repository repository.ConfigChecklistRepository
}
func NewConfigChecklistService(repo repository.ConfigChecklistRepository, validate *validator.Validate) ConfigChecklistService {
return &configChecklistService{
Log: utils.Log,
Validate: validate,
Repository: repo,
}
}
func (s configChecklistService) withRelations(db *gorm.DB) *gorm.DB {
return db
}
func (s configChecklistService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.ConfigChecklist, int64, error) {
if err := s.Validate.Struct(params); err != nil {
return nil, 0, err
}
offset := (params.Page - 1) * params.Limit
configChecklists, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
db = s.withRelations(db)
return db.Order("date DESC").Order("created_at DESC")
})
if err != nil {
s.Log.Errorf("Failed to get configChecklists: %+v", err)
return nil, 0, err
}
return configChecklists, total, nil
}
func (s configChecklistService) GetOne(c *fiber.Ctx, id uint) (*entity.ConfigChecklist, error) {
configChecklist, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "ConfigChecklist not found")
}
if err != nil {
s.Log.Errorf("Failed get configChecklist by id: %+v", err)
return nil, err
}
return configChecklist, nil
}
func (s *configChecklistService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.ConfigChecklist, error) {
if err := s.Validate.Struct(req); err != nil {
return nil, err
}
date, err := time.Parse("2006-01-02", req.Date)
if err != nil {
return nil, fiber.NewError(fiber.StatusBadRequest, "invalid date format, use YYYY-MM-DD")
}
createBody := &entity.ConfigChecklist{
Date: date,
PercentageThresholdBad: req.PercentageThresholdBad,
PercentageThresholdEnough: req.PercentageThresholdEnough,
}
if err := s.Repository.CreateOne(c.Context(), createBody, nil); err != nil {
s.Log.Errorf("Failed to create configChecklist: %+v", err)
return nil, err
}
return s.GetOne(c, createBody.Id)
}
func (s configChecklistService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.ConfigChecklist, error) {
if err := s.Validate.Struct(req); err != nil {
return nil, err
}
updateBody := make(map[string]any)
if req.Date != nil {
date, err := time.Parse("2006-01-02", *req.Date)
if err != nil {
return nil, fiber.NewError(fiber.StatusBadRequest, "invalid date format, use YYYY-MM-DD")
}
updateBody["date"] = date
}
if req.PercentageThresholdBad != nil {
updateBody["percentage_threshold_bad"] = *req.PercentageThresholdBad
}
if req.PercentageThresholdEnough != nil {
updateBody["percentage_threshold_enough"] = *req.PercentageThresholdEnough
}
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, "ConfigChecklist not found")
}
s.Log.Errorf("Failed to update configChecklist: %+v", err)
return nil, err
}
return s.GetOne(c, id)
}
func (s configChecklistService) 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, "ConfigChecklist not found")
}
s.Log.Errorf("Failed to delete configChecklist: %+v", err)
return err
}
return nil
}
@@ -0,0 +1,19 @@
package validation
type Create struct {
Date string `json:"date" validate:"required"`
PercentageThresholdBad int `json:"percentage_threshold_bad" validate:"required"`
PercentageThresholdEnough int `json:"percentage_threshold_enough" validate:"required"`
}
type Update struct {
Date *string `json:"date,omitempty" validate:"omitempty"`
PercentageThresholdBad *int `json:"percentage_threshold_bad,omitempty" validate:"omitempty"`
PercentageThresholdEnough *int `json:"percentage_threshold_enough,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"`
}
@@ -0,0 +1,161 @@
package controller
import (
"math"
"strconv"
"gitlab.com/mbugroup/lti-api.git/internal/modules/master/employees/dto"
service "gitlab.com/mbugroup/lti-api.git/internal/modules/master/employees/services"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/employees/validations"
"gitlab.com/mbugroup/lti-api.git/internal/response"
"github.com/gofiber/fiber/v2"
)
type EmployeesController struct {
EmployeesService service.EmployeesService
}
func NewEmployeesController(employeesService service.EmployeesService) *EmployeesController {
return &EmployeesController{
EmployeesService: employeesService,
}
}
func (u *EmployeesController) 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")
}
if kandangParam := c.Query("kandang_id", ""); kandangParam != "" {
id, err := strconv.Atoi(kandangParam)
if err != nil || id <= 0 {
return fiber.NewError(fiber.StatusBadRequest, "invalid kandang_id")
}
temp := uint(id)
query.KandangId = &temp
}
if activeParam := c.Query("is_active", ""); activeParam != "" {
value, err := strconv.ParseBool(activeParam)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "invalid is_active value")
}
query.IsActive = &value
}
result, totalResults, err := u.EmployeesService.GetAll(c, query)
if err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.SuccessWithPaginate[dto.EmployeesListDTO]{
Code: fiber.StatusOK,
Status: "success",
Message: "Get all employeess successfully",
Meta: response.Meta{
Page: query.Page,
Limit: query.Limit,
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
TotalResults: totalResults,
},
Data: dto.ToEmployeesListDTOs(result),
})
}
func (u *EmployeesController) 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.EmployeesService.GetOne(c, uint(id))
if err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.Success{
Code: fiber.StatusOK,
Status: "success",
Message: "Get employees successfully",
Data: dto.ToEmployeesListDTO(*result),
})
}
func (u *EmployeesController) 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.EmployeesService.CreateOne(c, req)
if err != nil {
return err
}
return c.Status(fiber.StatusCreated).
JSON(response.Success{
Code: fiber.StatusCreated,
Status: "success",
Message: "Create employees successfully",
Data: dto.ToEmployeesListDTO(*result),
})
}
func (u *EmployeesController) 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.EmployeesService.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 employees successfully",
Data: dto.ToEmployeesListDTO(*result),
})
}
func (u *EmployeesController) 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.EmployeesService.DeleteOne(c, uint(id)); err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.Common{
Code: fiber.StatusOK,
Status: "success",
Message: "Delete employees successfully",
})
}
@@ -0,0 +1,70 @@
package dto
import (
"time"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
kandangDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/dto"
)
// === DTO Structs ===
type EmployeesRelationDTO struct {
Id uint `json:"id"`
Name string `json:"name"`
}
type EmployeesListDTO struct {
Id uint `json:"id"`
Name string `json:"name"`
IsActive bool `json:"is_active"`
Kandangs []kandangDTO.KandangRelationDTO `json:"kandangs"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type EmployeesDetailDTO struct {
EmployeesListDTO
}
// === Mapper Functions ===
func ToEmployeesRelationDTO(e entity.Employees) EmployeesRelationDTO {
return EmployeesRelationDTO{
Id: e.Id,
Name: e.Name,
}
}
func ToEmployeesListDTO(e entity.Employees) EmployeesListDTO {
kandangs := make([]kandangDTO.KandangRelationDTO, 0, len(e.EmployeeKandangs))
for _, rel := range e.EmployeeKandangs {
if rel.Kandang.Id == 0 {
continue
}
kandangs = append(kandangs, kandangDTO.ToKandangRelationDTO(rel.Kandang))
}
return EmployeesListDTO{
Id: e.Id,
Name: e.Name,
IsActive: e.IsActive,
Kandangs: kandangs,
CreatedAt: e.CreatedAt,
UpdatedAt: e.UpdatedAt,
}
}
func ToEmployeesListDTOs(e []entity.Employees) []EmployeesListDTO {
result := make([]EmployeesListDTO, len(e))
for i, r := range e {
result[i] = ToEmployeesListDTO(r)
}
return result
}
func ToEmployeesDetailDTO(e entity.Employees) EmployeesDetailDTO {
return EmployeesDetailDTO{
EmployeesListDTO: ToEmployeesListDTO(e),
}
}
@@ -0,0 +1,25 @@
package employeess
import (
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
rEmployees "gitlab.com/mbugroup/lti-api.git/internal/modules/master/employees/repositories"
sEmployees "gitlab.com/mbugroup/lti-api.git/internal/modules/master/employees/services"
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
sUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
)
type EmployeesModule struct{}
func (EmployeesModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
employeesRepo := rEmployees.NewEmployeesRepository(db)
userRepo := rUser.NewUserRepository(db)
employeesService := sEmployees.NewEmployeesService(employeesRepo, validate)
userService := sUser.NewUserService(userRepo, validate)
EmployeesRoutes(router, userService, employeesService)
}
@@ -0,0 +1,21 @@
package repository
import (
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
"gorm.io/gorm"
)
type EmployeesRepository interface {
repository.BaseRepository[entity.Employees]
}
type EmployeesRepositoryImpl struct {
*repository.BaseRepositoryImpl[entity.Employees]
}
func NewEmployeesRepository(db *gorm.DB) EmployeesRepository {
return &EmployeesRepositoryImpl{
BaseRepositoryImpl: repository.NewBaseRepository[entity.Employees](db),
}
}
@@ -0,0 +1,23 @@
package employeess
import (
m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/master/employees/controllers"
employees "gitlab.com/mbugroup/lti-api.git/internal/modules/master/employees/services"
user "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
"github.com/gofiber/fiber/v2"
)
func EmployeesRoutes(v1 fiber.Router, u user.UserService, s employees.EmployeesService) {
ctrl := controller.NewEmployeesController(s)
route := v1.Group("/employees")
route.Use(m.Auth(u))
route.Get("/", ctrl.GetAll)
route.Post("/", ctrl.CreateOne)
route.Get("/:id", ctrl.GetOne)
route.Patch("/:id", ctrl.UpdateOne)
route.Delete("/:id", ctrl.DeleteOne)
}
@@ -0,0 +1,265 @@
package service
import (
"errors"
"strings"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/master/employees/repositories"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/employees/validations"
"gitlab.com/mbugroup/lti-api.git/internal/utils"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
)
type EmployeesService interface {
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.Employees, int64, error)
GetOne(ctx *fiber.Ctx, id uint) (*entity.Employees, error)
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.Employees, error)
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.Employees, error)
DeleteOne(ctx *fiber.Ctx, id uint) error
}
type employeesService struct {
Log *logrus.Logger
Validate *validator.Validate
Repository repository.EmployeesRepository
}
func NewEmployeesService(repo repository.EmployeesRepository, validate *validator.Validate) EmployeesService {
return &employeesService{
Log: utils.Log,
Validate: validate,
Repository: repo,
}
}
func (s employeesService) withRelations(db *gorm.DB) *gorm.DB {
return db.
Preload("EmployeeKandangs.Kandang").
Where("employees.deleted_at IS NULL")
}
func (s employeesService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.Employees, int64, error) {
if err := s.Validate.Struct(params); err != nil {
return nil, 0, err
}
offset := (params.Page - 1) * params.Limit
employeess, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
db = s.withRelations(db)
if params.Search != "" {
db = db.Where("employees.name LIKE ?", "%"+params.Search+"%")
}
if params.KandangId != nil {
db = db.Joins("JOIN employee_kandangs ek ON ek.employee_id = employees.id").
Where("ek.kandang_id = ?", *params.KandangId)
}
if params.IsActive != nil {
db = db.Where("employees.is_active = ?", *params.IsActive)
}
return db.Order("employees.created_at DESC").Order("employees.updated_at DESC")
})
if err != nil {
s.Log.Errorf("Failed to get employeess: %+v", err)
return nil, 0, err
}
return employeess, total, nil
}
func (s employeesService) GetOne(c *fiber.Ctx, id uint) (*entity.Employees, error) {
employees, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "Employees not found")
}
if err != nil {
s.Log.Errorf("Failed get employees by id: %+v", err)
return nil, err
}
return employees, nil
}
func (s *employeesService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.Employees, error) {
if err := s.Validate.Struct(req); err != nil {
return nil, err
}
name := strings.TrimSpace(req.Name)
if name == "" {
return nil, fiber.NewError(fiber.StatusBadRequest, "name cannot be empty")
}
kandangIDs := normalizeKandangIDs(req.KandangIDs)
if len(kandangIDs) == 0 {
return nil, fiber.NewError(fiber.StatusBadRequest, "kandang_ids must contain at least one valid id")
}
if _, err := s.Repository.First(c.Context(), func(db *gorm.DB) *gorm.DB {
return db.Where("LOWER(name) = ?", strings.ToLower(name))
}); err == nil {
return nil, fiber.NewError(fiber.StatusBadRequest, "employee already exists")
} else if !errors.Is(err, gorm.ErrRecordNotFound) {
s.Log.Errorf("Failed checking employee uniqueness: %+v", err)
return nil, err
}
createBody := &entity.Employees{
Name: name,
IsActive: req.IsActive,
}
if err := s.Repository.DB().Transaction(func(tx *gorm.DB) error {
repoTx := s.Repository.WithTx(tx)
if err := repoTx.CreateOne(c.Context(), createBody, nil); err != nil {
return err
}
relations := make([]entity.EmployeeKandang, 0, len(kandangIDs))
for _, kandangID := range kandangIDs {
relations = append(relations, entity.EmployeeKandang{
EmployeeId: createBody.Id,
KandangId: kandangID,
})
}
if len(relations) > 0 {
if err := tx.WithContext(c.Context()).Create(&relations).Error; err != nil {
return err
}
}
return nil
}); err != nil {
s.Log.Errorf("Failed to create employees: %+v", err)
return nil, err
}
return s.GetOne(c, createBody.Id)
}
func (s employeesService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.Employees, error) {
if err := s.Validate.Struct(req); err != nil {
return nil, err
}
updateBody := make(map[string]any)
var (
kandangIDs []uint
needKandangUpdate bool
)
if req.Name != nil {
trimmed := strings.TrimSpace(*req.Name)
if trimmed == "" {
return nil, fiber.NewError(fiber.StatusBadRequest, "name cannot be empty")
}
if _, err := s.Repository.First(c.Context(), func(db *gorm.DB) *gorm.DB {
return db.Where("LOWER(name) = ? AND id <> ?", strings.ToLower(trimmed), id)
}); err == nil {
return nil, fiber.NewError(fiber.StatusBadRequest, "employee already exists")
} else if !errors.Is(err, gorm.ErrRecordNotFound) {
s.Log.Errorf("Failed checking employee uniqueness: %+v", err)
return nil, err
}
updateBody["name"] = trimmed
}
if req.IsActive != nil {
updateBody["is_active"] = *req.IsActive
}
if req.KandangIDs != nil {
ids := normalizeKandangIDs(*req.KandangIDs)
if len(ids) == 0 {
return nil, fiber.NewError(fiber.StatusBadRequest, "kandang_ids must contain at least one valid id")
}
kandangIDs = ids
needKandangUpdate = true
}
if len(updateBody) == 0 && !needKandangUpdate {
return s.GetOne(c, id)
}
if err := s.Repository.DB().Transaction(func(tx *gorm.DB) error {
repoTx := s.Repository.WithTx(tx)
if len(updateBody) > 0 {
if err := repoTx.PatchOne(c.Context(), id, updateBody, nil); err != nil {
return err
}
}
if needKandangUpdate {
if err := tx.WithContext(c.Context()).
Where("employee_id = ?", id).
Delete(&entity.EmployeeKandang{}).Error; err != nil {
return err
}
relations := make([]entity.EmployeeKandang, 0, len(kandangIDs))
for _, kandangID := range kandangIDs {
relations = append(relations, entity.EmployeeKandang{
EmployeeId: id,
KandangId: kandangID,
})
}
if len(relations) > 0 {
if err := tx.WithContext(c.Context()).Create(&relations).Error; err != nil {
return err
}
}
}
return nil
}); err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "Employees not found")
}
s.Log.Errorf("Failed to update employees: %+v", err)
return nil, err
}
return s.GetOne(c, id)
}
func (s employeesService) 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, "Employees not found")
}
s.Log.Errorf("Failed to delete employees: %+v", err)
return err
}
return nil
}
func normalizeKandangIDs(ids []uint) []uint {
result := make([]uint, 0, len(ids))
seen := make(map[uint]struct{})
for _, id := range ids {
if id == 0 {
continue
}
if _, ok := seen[id]; ok {
continue
}
seen[id] = struct{}{}
result = append(result, id)
}
return result
}
@@ -0,0 +1,21 @@
package validation
type Create struct {
Name string `json:"name" validate:"required_strict,min=3"`
KandangIDs []uint `json:"kandang_ids" validate:"required,min=1,dive,required"`
IsActive bool `json:"is_active"`
}
type Update struct {
Name *string `json:"name,omitempty" validate:"omitempty"`
KandangIDs *[]uint `json:"kandang_ids,omitempty" validate:"omitempty,min=1,dive,required"`
IsActive *bool `json:"is_active,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=500,gt=0"`
Search string `query:"search" validate:"omitempty,max=50"`
KandangId *uint `query:"kandang_id" validate:"omitempty"`
IsActive *bool `query:"is_active" validate:"omitempty"`
}
@@ -23,10 +23,12 @@ func NewNonstockController(nonstockService service.NonstockService) *NonstockCon
} }
func (u *NonstockController) GetAll(c *fiber.Ctx) error { func (u *NonstockController) GetAll(c *fiber.Ctx) error {
query := &validation.Query{ query := &validation.Query{
Page: c.QueryInt("page", 1), Page: c.QueryInt("page", 1),
Limit: c.QueryInt("limit", 10), Limit: c.QueryInt("limit", 10),
Search: c.Query("search", ""), Search: c.Query("search", ""),
SupplierID: uint(c.QueryInt("supplier_id", 0)),
} }
if query.Page < 1 || query.Limit < 1 { if query.Page < 1 || query.Limit < 1 {
@@ -58,7 +58,15 @@ func (s nonstockService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entit
offset := (params.Page - 1) * params.Limit offset := (params.Page - 1) * params.Limit
nonstocks, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB { nonstocks, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
if params.SupplierID > 0 {
db = db.Joins("INNER JOIN nonstock_suppliers ON nonstock_suppliers.nonstock_id = nonstocks.id").
Where("nonstock_suppliers.supplier_id = ?", params.SupplierID).
Distinct()
}
db = s.withRelations(db) db = s.withRelations(db)
if params.Search != "" { if params.Search != "" {
return db.Where("name LIKE ?", "%"+params.Search+"%") return db.Where("name LIKE ?", "%"+params.Search+"%")
} }
@@ -15,7 +15,8 @@ type Update struct {
} }
type Query struct { type Query struct {
Page int `query:"page" validate:"omitempty,number,min=1"` Page int `query:"page" validate:"omitempty,number,min=1"`
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"` Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"`
Search string `query:"search" validate:"omitempty,max=50"` Search string `query:"search" validate:"omitempty,max=50"`
SupplierID uint `query:"supplier_id" validate:"omitempty,gt=0"`
} }
@@ -0,0 +1,145 @@
package controller
import (
"math"
"strconv"
"gitlab.com/mbugroup/lti-api.git/internal/modules/master/phase-activities/dto"
service "gitlab.com/mbugroup/lti-api.git/internal/modules/master/phase-activities/services"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/phase-activities/validations"
"gitlab.com/mbugroup/lti-api.git/internal/response"
"github.com/gofiber/fiber/v2"
)
type PhaseActivityController struct {
PhaseActivityService service.PhaseActivityService
}
func NewPhaseActivityController(phaseActivityService service.PhaseActivityService) *PhaseActivityController {
return &PhaseActivityController{
PhaseActivityService: phaseActivityService,
}
}
func (u *PhaseActivityController) GetAll(c *fiber.Ctx) error {
query := &validation.Query{
Page: c.QueryInt("page", 1),
Limit: c.QueryInt("limit", 10),
Search: c.Query("search", ""),
}
query.PhaseIDs = c.Query("phase_ids", "")
if query.Page < 1 || query.Limit < 1 {
return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0")
}
result, totalResults, err := u.PhaseActivityService.GetAll(c, query)
if err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.SuccessWithPaginate[dto.PhaseActivityListDTO]{
Code: fiber.StatusOK,
Status: "success",
Message: "Get all phaseActivitys successfully",
Meta: response.Meta{
Page: query.Page,
Limit: query.Limit,
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
TotalResults: totalResults,
},
Data: dto.ToPhaseActivityListDTOs(result),
})
}
func (u *PhaseActivityController) 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.PhaseActivityService.GetOne(c, uint(id))
if err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.Success{
Code: fiber.StatusOK,
Status: "success",
Message: "Get phaseActivity successfully",
Data: dto.ToPhaseActivityListDTO(*result),
})
}
func (u *PhaseActivityController) 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.PhaseActivityService.CreateOne(c, req)
if err != nil {
return err
}
return c.Status(fiber.StatusCreated).
JSON(response.Success{
Code: fiber.StatusCreated,
Status: "success",
Message: "Create phaseActivity successfully",
Data: dto.ToPhaseActivityListDTO(*result),
})
}
func (u *PhaseActivityController) 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.PhaseActivityService.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 phaseActivity successfully",
Data: dto.ToPhaseActivityListDTO(*result),
})
}
func (u *PhaseActivityController) 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.PhaseActivityService.DeleteOne(c, uint(id)); err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.Common{
Code: fiber.StatusOK,
Status: "success",
Message: "Delete phaseActivity successfully",
})
}
@@ -0,0 +1,72 @@
package dto
import (
"time"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
)
// === DTO Structs ===
type PhaseActivityRelationDTO struct {
Id uint `json:"id"`
Name string `json:"name"`
}
type PhaseActivityListDTO struct {
Id uint `json:"id"`
PhaseId uint `json:"phase_id"`
Name string `json:"name"`
Description *string `json:"description"`
TimeType *string `json:"time_type"`
CreatedUser *userDTO.UserRelationDTO `json:"created_user"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type PhaseActivityDetailDTO struct {
PhaseActivityListDTO
}
// === Mapper Functions ===
func ToPhaseActivityRelationDTO(e entity.PhaseActivity) PhaseActivityRelationDTO {
return PhaseActivityRelationDTO{
Id: e.Id,
Name: e.Name,
}
}
func ToPhaseActivityListDTO(e entity.PhaseActivity) PhaseActivityListDTO {
var createdUser *userDTO.UserRelationDTO
// if e.CreatedUser.Id != 0 {
// mapped := userDTO.ToUserRelationDTO(e.CreatedUser)
// createdUser = &mapped
// }
return PhaseActivityListDTO{
Id: e.Id,
PhaseId: e.PhaseId,
Name: e.Name,
Description: e.Description,
TimeType: e.TimeType,
CreatedAt: e.CreatedAt,
UpdatedAt: e.UpdatedAt,
CreatedUser: createdUser,
}
}
func ToPhaseActivityListDTOs(e []entity.PhaseActivity) []PhaseActivityListDTO {
result := make([]PhaseActivityListDTO, len(e))
for i, r := range e {
result[i] = ToPhaseActivityListDTO(r)
}
return result
}
func ToPhaseActivityDetailDTO(e entity.PhaseActivity) PhaseActivityDetailDTO {
return PhaseActivityDetailDTO{
PhaseActivityListDTO: ToPhaseActivityListDTO(e),
}
}
@@ -0,0 +1,27 @@
package phaseActivity
import (
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
rPhaseActivity "gitlab.com/mbugroup/lti-api.git/internal/modules/master/phase-activities/repositories"
sPhaseActivity "gitlab.com/mbugroup/lti-api.git/internal/modules/master/phase-activities/services"
rPhases "gitlab.com/mbugroup/lti-api.git/internal/modules/master/phasess/repositories"
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
sUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
)
type PhaseActivityModule struct{}
func (PhaseActivityModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
phaseActivityRepo := rPhaseActivity.NewPhaseActivityRepository(db)
phasesRepo := rPhases.NewPhasesRepository(db)
userRepo := rUser.NewUserRepository(db)
phaseActivityService := sPhaseActivity.NewPhaseActivityService(phaseActivityRepo, phasesRepo, validate)
userService := sUser.NewUserService(userRepo, validate)
PhaseActivityRoutes(router, userService, phaseActivityService)
}
@@ -0,0 +1,21 @@
package repository
import (
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
"gorm.io/gorm"
)
type PhaseActivityRepository interface {
repository.BaseRepository[entity.PhaseActivity]
}
type PhaseActivityRepositoryImpl struct {
*repository.BaseRepositoryImpl[entity.PhaseActivity]
}
func NewPhaseActivityRepository(db *gorm.DB) PhaseActivityRepository {
return &PhaseActivityRepositoryImpl{
BaseRepositoryImpl: repository.NewBaseRepository[entity.PhaseActivity](db),
}
}
@@ -0,0 +1,23 @@
package phaseActivity
import (
m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/master/phase-activities/controllers"
phaseActivity "gitlab.com/mbugroup/lti-api.git/internal/modules/master/phase-activities/services"
user "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
"github.com/gofiber/fiber/v2"
)
func PhaseActivityRoutes(v1 fiber.Router, u user.UserService, s phaseActivity.PhaseActivityService) {
ctrl := controller.NewPhaseActivityController(s)
route := v1.Group("/phase-activities")
route.Use(m.Auth(u))
route.Get("/", ctrl.GetAll)
route.Post("/", ctrl.CreateOne)
route.Get("/:id", ctrl.GetOne)
route.Patch("/:id", ctrl.UpdateOne)
route.Delete("/:id", ctrl.DeleteOne)
}
@@ -0,0 +1,187 @@
package service
import (
"errors"
"strconv"
"strings"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/master/phase-activities/repositories"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/phase-activities/validations"
phaseRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/master/phasess/repositories"
"gitlab.com/mbugroup/lti-api.git/internal/utils"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
)
type PhaseActivityService interface {
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.PhaseActivity, int64, error)
GetOne(ctx *fiber.Ctx, id uint) (*entity.PhaseActivity, error)
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.PhaseActivity, error)
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.PhaseActivity, error)
DeleteOne(ctx *fiber.Ctx, id uint) error
}
type phaseActivityService struct {
Log *logrus.Logger
Validate *validator.Validate
Repository repository.PhaseActivityRepository
PhaseRepo phaseRepo.PhasesRepository
}
func NewPhaseActivityService(repo repository.PhaseActivityRepository, phaseRepo phaseRepo.PhasesRepository, validate *validator.Validate) PhaseActivityService {
return &phaseActivityService{
Log: utils.Log,
Validate: validate,
Repository: repo,
PhaseRepo: phaseRepo,
}
}
func (s phaseActivityService) withRelations(db *gorm.DB) *gorm.DB {
return db.Joins("JOIN phases ON phases.id = phase_activities.phase_id").
Where("phases.deleted_at IS NULL")
}
func (s phaseActivityService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.PhaseActivity, int64, error) {
if err := s.Validate.Struct(params); err != nil {
return nil, 0, err
}
offset := (params.Page - 1) * params.Limit
phaseActivitys, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
db = s.withRelations(db)
if params.Search != "" {
db = db.Where("name LIKE ?", "%"+params.Search+"%")
}
if params.PhaseIDs != "" {
ids := parseIDs(params.PhaseIDs)
if len(ids) > 0 {
db = db.Where("phase_id IN ?", ids)
}
}
return db.Order("created_at DESC").Order("updated_at DESC")
})
if err != nil {
s.Log.Errorf("Failed to get phaseActivitys: %+v", err)
return nil, 0, err
}
return phaseActivitys, total, nil
}
func (s phaseActivityService) GetOne(c *fiber.Ctx, id uint) (*entity.PhaseActivity, error) {
phaseActivity, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "PhaseActivity not found")
}
if err != nil {
s.Log.Errorf("Failed get phaseActivity by id: %+v", err)
return nil, err
}
return phaseActivity, nil
}
func (s *phaseActivityService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.PhaseActivity, error) {
if err := s.Validate.Struct(req); err != nil {
return nil, err
}
phase, err := s.PhaseRepo.GetByID(c.Context(), req.PhaseId, nil)
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusBadRequest, "phase not found")
}
if err != nil {
s.Log.Errorf("Failed to get phase: %+v", err)
return nil, err
}
name := strings.TrimSpace(req.Name)
if name == "" {
return nil, fiber.NewError(fiber.StatusBadRequest, "name cannot be empty")
}
timeType := strings.TrimSpace(req.TimeType)
if timeType == "" {
return nil, fiber.NewError(fiber.StatusBadRequest, "time_type cannot be empty")
}
createBody := &entity.PhaseActivity{
PhaseId: phase.Id,
Name: name,
Description: req.Description,
TimeType: &timeType,
}
if err := s.Repository.CreateOne(c.Context(), createBody, nil); err != nil {
s.Log.Errorf("Failed to create phaseActivity: %+v", err)
return nil, err
}
return s.GetOne(c, createBody.Id)
}
func (s phaseActivityService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.PhaseActivity, error) {
if err := s.Validate.Struct(req); err != nil {
return nil, err
}
trimmedName := strings.TrimSpace(req.Name)
if trimmedName == "" {
return nil, fiber.NewError(fiber.StatusBadRequest, "name cannot be empty")
}
trimmedTimeType := strings.TrimSpace(req.TimeType)
if trimmedTimeType == "" {
return nil, fiber.NewError(fiber.StatusBadRequest, "time_type cannot be empty")
}
updateBody := map[string]any{
"name": trimmedName,
"time_type": trimmedTimeType,
}
if req.Description != nil {
updateBody["description"] = *req.Description
}
if err := s.Repository.PatchOne(c.Context(), id, updateBody, nil); err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "PhaseActivity not found")
}
s.Log.Errorf("Failed to update phaseActivity: %+v", err)
return nil, err
}
return s.GetOne(c, id)
}
func (s phaseActivityService) 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, "PhaseActivity not found")
}
s.Log.Errorf("Failed to delete phaseActivity: %+v", err)
return err
}
return nil
}
func parseIDs(raw string) []uint {
parts := strings.Split(raw, ",")
results := make([]uint, 0, len(parts))
for _, part := range parts {
value := strings.TrimSpace(part)
if value == "" {
continue
}
if n, err := strconv.ParseUint(value, 10, 64); err == nil {
results = append(results, uint(n))
}
}
return results
}
@@ -0,0 +1,21 @@
package validation
type Create struct {
PhaseId uint `json:"phase_id" validate:"required"`
Name string `json:"name" validate:"required_strict,min=3"`
Description *string `json:"description,omitempty"`
TimeType string `json:"time_type" validate:"required"`
}
type Update struct {
Name string `json:"name" validate:"required_strict,min=3"`
Description *string `json:"description,omitempty"`
TimeType string `json:"time_type" validate:"required"`
}
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"`
PhaseIDs string `query:"phase_ids" validate:"omitempty"`
}
@@ -0,0 +1,148 @@
package controller
import (
"math"
"strconv"
"gitlab.com/mbugroup/lti-api.git/internal/modules/master/phasess/dto"
service "gitlab.com/mbugroup/lti-api.git/internal/modules/master/phasess/services"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/phasess/validations"
"gitlab.com/mbugroup/lti-api.git/internal/response"
"github.com/gofiber/fiber/v2"
)
type PhasesController struct {
PhasesService service.PhasesService
}
func NewPhasesController(phasesService service.PhasesService) *PhasesController {
return &PhasesController{
PhasesService: phasesService,
}
}
func (u *PhasesController) 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")
}
if category := c.Query("category", ""); category != "" {
query.Category = &category
}
result, totalResults, err := u.PhasesService.GetAll(c, query)
if err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.SuccessWithPaginate[dto.PhasesListDTO]{
Code: fiber.StatusOK,
Status: "success",
Message: "Get all phasess successfully",
Meta: response.Meta{
Page: query.Page,
Limit: query.Limit,
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
TotalResults: totalResults,
},
Data: dto.ToPhasesListDTOs(result),
})
}
func (u *PhasesController) 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.PhasesService.GetOne(c, uint(id))
if err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.Success{
Code: fiber.StatusOK,
Status: "success",
Message: "Get phases successfully",
Data: dto.ToPhasesListDTO(*result),
})
}
func (u *PhasesController) 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.PhasesService.CreateOne(c, req)
if err != nil {
return err
}
return c.Status(fiber.StatusCreated).
JSON(response.Success{
Code: fiber.StatusCreated,
Status: "success",
Message: "Create phases successfully",
Data: dto.ToPhasesListDTO(*result),
})
}
func (u *PhasesController) 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.PhasesService.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 phases successfully",
Data: dto.ToPhasesListDTO(*result),
})
}
func (u *PhasesController) 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.PhasesService.DeleteOne(c, uint(id)); err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.Common{
Code: fiber.StatusOK,
Status: "success",
Message: "Delete phases successfully",
})
}
@@ -0,0 +1,68 @@
package dto
import (
"time"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
)
// === DTO Structs ===
type PhasesRelationDTO struct {
Id uint `json:"id"`
Name string `json:"name"`
}
type PhasesListDTO struct {
Id uint `json:"id"`
Name string `json:"name"`
Category string `json:"category"`
IsActive bool `json:"is_active"`
CreatedUser *userDTO.UserRelationDTO `json:"created_user"`
CreatedAt time.Time `json:"created_at"`
}
type PhasesDetailDTO struct {
PhasesListDTO
}
// === Mapper Functions ===
func ToPhasesRelationDTO(e entity.Phases) PhasesRelationDTO {
return PhasesRelationDTO{
Id: e.Id,
Name: e.Name,
}
}
func ToPhasesListDTO(e entity.Phases) PhasesListDTO {
var createdUser *userDTO.UserRelationDTO
// if e.CreatedUser.Id != 0 {
// mapped := userDTO.ToUserRelationDTO(e.CreatedUser)
// createdUser = &mapped
// }
return PhasesListDTO{
Id: e.Id,
Name: e.Name,
Category: e.Category,
IsActive: e.IsActive,
CreatedAt: e.CreatedAt,
CreatedUser: createdUser,
}
}
func ToPhasesListDTOs(e []entity.Phases) []PhasesListDTO {
result := make([]PhasesListDTO, len(e))
for i, r := range e {
result[i] = ToPhasesListDTO(r)
}
return result
}
func ToPhasesDetailDTO(e entity.Phases) PhasesDetailDTO {
return PhasesDetailDTO{
PhasesListDTO: ToPhasesListDTO(e),
}
}
+25
View File
@@ -0,0 +1,25 @@
package phases
import (
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
rPhases "gitlab.com/mbugroup/lti-api.git/internal/modules/master/phasess/repositories"
sPhases "gitlab.com/mbugroup/lti-api.git/internal/modules/master/phasess/services"
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
sUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
)
type PhasesModule struct{}
func (PhasesModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
phasesRepo := rPhases.NewPhasesRepository(db)
userRepo := rUser.NewUserRepository(db)
phasesService := sPhases.NewPhasesService(phasesRepo, validate)
userService := sUser.NewUserService(userRepo, validate)
PhasesRoutes(router, userService, phasesService)
}
@@ -0,0 +1,21 @@
package repository
import (
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
"gorm.io/gorm"
)
type PhasesRepository interface {
repository.BaseRepository[entity.Phases]
}
type PhasesRepositoryImpl struct {
*repository.BaseRepositoryImpl[entity.Phases]
}
func NewPhasesRepository(db *gorm.DB) PhasesRepository {
return &PhasesRepositoryImpl{
BaseRepositoryImpl: repository.NewBaseRepository[entity.Phases](db),
}
}
+23
View File
@@ -0,0 +1,23 @@
package phases
import (
m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/master/phasess/controllers"
phases "gitlab.com/mbugroup/lti-api.git/internal/modules/master/phasess/services"
user "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
"github.com/gofiber/fiber/v2"
)
func PhasesRoutes(v1 fiber.Router, u user.UserService, s phases.PhasesService) {
ctrl := controller.NewPhasesController(s)
route := v1.Group("/phases")
route.Use(m.Auth(u))
route.Get("/", ctrl.GetAll)
route.Post("/", ctrl.CreateOne)
route.Get("/:id", ctrl.GetOne)
route.Patch("/:id", ctrl.UpdateOne)
route.Delete("/:id", ctrl.DeleteOne)
}
@@ -0,0 +1,158 @@
package service
import (
"errors"
"strings"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/master/phasess/repositories"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/phasess/validations"
"gitlab.com/mbugroup/lti-api.git/internal/utils"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
)
type PhasesService interface {
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.Phases, int64, error)
GetOne(ctx *fiber.Ctx, id uint) (*entity.Phases, error)
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.Phases, error)
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.Phases, error)
DeleteOne(ctx *fiber.Ctx, id uint) error
}
type phasesService struct {
Log *logrus.Logger
Validate *validator.Validate
Repository repository.PhasesRepository
}
func NewPhasesService(repo repository.PhasesRepository, validate *validator.Validate) PhasesService {
return &phasesService{
Log: utils.Log,
Validate: validate,
Repository: repo,
}
}
func (s phasesService) withRelations(db *gorm.DB) *gorm.DB {
return db
}
func (s phasesService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.Phases, int64, error) {
if err := s.Validate.Struct(params); err != nil {
return nil, 0, err
}
offset := (params.Page - 1) * params.Limit
phasess, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
db = s.withRelations(db)
if params.Search != "" {
return db.Where("name LIKE ?", "%"+params.Search+"%")
}
if params.Category != nil {
db = db.Where("category = ?", *params.Category)
}
return db.Order("created_at DESC")
})
if err != nil {
s.Log.Errorf("Failed to get phasess: %+v", err)
return nil, 0, err
}
return phasess, total, nil
}
func (s phasesService) GetOne(c *fiber.Ctx, id uint) (*entity.Phases, error) {
phases, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "Phases not found")
}
if err != nil {
s.Log.Errorf("Failed get phases by id: %+v", err)
return nil, err
}
return phases, nil
}
func (s *phasesService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.Phases, error) {
if err := s.Validate.Struct(req); err != nil {
return nil, err
}
if _, err := s.Repository.First(c.Context(), func(db *gorm.DB) *gorm.DB {
return db.Where("LOWER(name) = ? AND category = ?", strings.ToLower(req.Name), req.Category)
}); err == nil {
return nil, fiber.NewError(fiber.StatusBadRequest, "phase already exists")
} else if !errors.Is(err, gorm.ErrRecordNotFound) {
s.Log.Errorf("Failed checking phase uniqueness: %+v", err)
return nil, err
}
createBody := &entity.Phases{
Name: req.Name,
Category: req.Category,
IsActive: true,
}
if err := s.Repository.CreateOne(c.Context(), createBody, nil); err != nil {
s.Log.Errorf("Failed to create phases: %+v", err)
return nil, err
}
return s.GetOne(c, createBody.Id)
}
func (s phasesService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.Phases, error) {
if err := s.Validate.Struct(req); err != nil {
return nil, err
}
existing, err := s.Repository.GetByID(c.Context(), id, nil)
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "Phases not found")
}
if err != nil {
s.Log.Errorf("Failed get phases by id: %+v", err)
return nil, err
}
updateBody := make(map[string]any)
if req.Name != nil {
if _, err := s.Repository.First(c.Context(), func(db *gorm.DB) *gorm.DB {
return db.Where("LOWER(name) = ? AND category = ? AND id <> ?", strings.ToLower(*req.Name), existing.Category, id)
}); err == nil {
return nil, fiber.NewError(fiber.StatusBadRequest, "phase already exists")
} else if !errors.Is(err, gorm.ErrRecordNotFound) {
s.Log.Errorf("Failed checking phase uniqueness: %+v", err)
return nil, err
}
updateBody["name"] = strings.TrimSpace(*req.Name)
}
if len(updateBody) == 0 {
return s.GetOne(c, id)
}
if err := s.Repository.PatchOne(c.Context(), id, updateBody, nil); err != nil {
s.Log.Errorf("Failed to update phases: %+v", err)
return nil, err
}
return s.GetOne(c, id)
}
func (s phasesService) 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, "Phases not found")
}
s.Log.Errorf("Failed to delete phases: %+v", err)
return err
}
return nil
}
@@ -0,0 +1,17 @@
package validation
type Create struct {
Name string `json:"name" validate:"required_strict,min=3"`
Category string `json:"category" validate:"required"`
}
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"`
Category *string `query:"category" validate:"omitempty"`
}
+9 -1
View File
@@ -10,17 +10,21 @@ import (
areas "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas" areas "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas"
banks "gitlab.com/mbugroup/lti-api.git/internal/modules/master/banks" banks "gitlab.com/mbugroup/lti-api.git/internal/modules/master/banks"
customers "gitlab.com/mbugroup/lti-api.git/internal/modules/master/customers" customers "gitlab.com/mbugroup/lti-api.git/internal/modules/master/customers"
employeess "gitlab.com/mbugroup/lti-api.git/internal/modules/master/employees"
fcrs "gitlab.com/mbugroup/lti-api.git/internal/modules/master/fcrs" fcrs "gitlab.com/mbugroup/lti-api.git/internal/modules/master/fcrs"
flocks "gitlab.com/mbugroup/lti-api.git/internal/modules/master/flocks" flocks "gitlab.com/mbugroup/lti-api.git/internal/modules/master/flocks"
kandangs "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs" kandangs "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs"
locations "gitlab.com/mbugroup/lti-api.git/internal/modules/master/locations" locations "gitlab.com/mbugroup/lti-api.git/internal/modules/master/locations"
nonstocks "gitlab.com/mbugroup/lti-api.git/internal/modules/master/nonstocks" nonstocks "gitlab.com/mbugroup/lti-api.git/internal/modules/master/nonstocks"
phaseActivitys "gitlab.com/mbugroup/lti-api.git/internal/modules/master/phase-activities"
phasess "gitlab.com/mbugroup/lti-api.git/internal/modules/master/phasess"
productcategories "gitlab.com/mbugroup/lti-api.git/internal/modules/master/product-categories" productcategories "gitlab.com/mbugroup/lti-api.git/internal/modules/master/product-categories"
productionStandards "gitlab.com/mbugroup/lti-api.git/internal/modules/master/production-standards"
products "gitlab.com/mbugroup/lti-api.git/internal/modules/master/products" products "gitlab.com/mbugroup/lti-api.git/internal/modules/master/products"
suppliers "gitlab.com/mbugroup/lti-api.git/internal/modules/master/suppliers" suppliers "gitlab.com/mbugroup/lti-api.git/internal/modules/master/suppliers"
uoms "gitlab.com/mbugroup/lti-api.git/internal/modules/master/uoms" uoms "gitlab.com/mbugroup/lti-api.git/internal/modules/master/uoms"
warehouses "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses" warehouses "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses"
productionStandards "gitlab.com/mbugroup/lti-api.git/internal/modules/master/production-standards" configChecklists "gitlab.com/mbugroup/lti-api.git/internal/modules/master/config-checklists"
// MODULE IMPORTS // MODULE IMPORTS
) )
@@ -42,6 +46,10 @@ func RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Valida
banks.BankModule{}, banks.BankModule{},
flocks.FlockModule{}, flocks.FlockModule{},
productionStandards.ProductionStandardModule{}, productionStandards.ProductionStandardModule{},
employeess.EmployeesModule{},
phasess.PhasesModule{},
phaseActivitys.PhaseActivityModule{},
configChecklists.ConfigChecklistModule{},
// MODULE REGISTRY // MODULE REGISTRY
} }
@@ -22,6 +22,7 @@ import (
pfutils "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/utils" pfutils "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/utils"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/validations" validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/validations"
recordingRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/repositories" recordingRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/repositories"
uniformityRepository "gitlab.com/mbugroup/lti-api.git/internal/modules/production/uniformities/repositories"
utils "gitlab.com/mbugroup/lti-api.git/internal/utils" 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"
@@ -866,6 +867,14 @@ func (s projectflockService) detachKandangs(ctx context.Context, dbTransaction *
} }
if len(pfkIDs) > 0 { if len(pfkIDs) > 0 {
uniformityRepo := uniformityRepository.NewUniformityRepository(s.Repository.DB())
if dbTransaction != nil {
uniformityRepo = uniformityRepository.NewUniformityRepository(dbTransaction)
}
if err := uniformityRepo.DeleteByProjectFlockKandangIDs(ctx, pfkIDs); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, "Failed to remove uniformity data for project flock kandang")
}
pwRepo := s.ProductWarehouseRepo pwRepo := s.ProductWarehouseRepo
if dbTransaction != nil { if dbTransaction != nil {
pwRepo = productWarehouseRepository.NewProductWarehouseRepository(dbTransaction) pwRepo = productWarehouseRepository.NewProductWarehouseRepository(dbTransaction)
@@ -22,11 +22,21 @@ type RecordingRelationDTO struct {
ProjectFlockCategory string `json:"project_flock_category"` ProjectFlockCategory string `json:"project_flock_category"`
TotalDepletionQty float64 `json:"total_depletion_qty"` TotalDepletionQty float64 `json:"total_depletion_qty"`
CumDepletionRate float64 `json:"cum_depletion_rate"` CumDepletionRate float64 `json:"cum_depletion_rate"`
DailyGain float64 `json:"daily_gain"`
AvgDailyGain float64 `json:"avg_daily_gain"`
CumIntake int `json:"cum_intake"` CumIntake int `json:"cum_intake"`
FcrValue float64 `json:"fcr_value"` FcrValue float64 `json:"fcr_value"`
TotalChickQty float64 `json:"total_chick_qty"` TotalChickQty float64 `json:"total_chick_qty"`
HandDay float64 `json:"hand_day"`
HandHouse float64 `json:"hand_house"`
FeedIntake float64 `json:"feed_intake"`
EggMesh float64 `json:"egg_mesh"`
EggWeight float64 `json:"egg_weight"`
StandardHandDay *float64 `json:"hand_day_std,omitempty"`
StandardHandHouse *float64 `json:"hand_house_std,omitempty"`
StandardFeedIntake *float64 `json:"feed_intake_std,omitempty"`
StandardMaxDepletion *float64 `json:"max_depletion_std,omitempty"`
StandardEggMesh *float64 `json:"egg_mesh_std,omitempty"`
StandardEggWeight *float64 `json:"egg_weight_std,omitempty"`
StandardFcr *float64 `json:"fcr_std,omitempty"`
Approval approvalDTO.ApprovalRelationDTO `json:"approval"` Approval approvalDTO.ApprovalRelationDTO `json:"approval"`
} }
@@ -39,16 +49,9 @@ type RecordingListDTO struct {
type RecordingDetailDTO struct { type RecordingDetailDTO struct {
RecordingListDTO RecordingListDTO
BodyWeights []RecordingBodyWeightDTO `json:"body_weights"` Depletions []RecordingDepletionDTO `json:"depletions"`
Depletions []RecordingDepletionDTO `json:"depletions"` Stocks []RecordingStockDTO `json:"stocks"`
Stocks []RecordingStockDTO `json:"stocks"` Eggs []RecordingEggDTO `json:"eggs"`
Eggs []RecordingEggDTO `json:"eggs"`
}
type RecordingBodyWeightDTO struct {
AvgWeight float64 `json:"avg_weight"`
Qty float64 `json:"qty"`
TotalWeight float64 `json:"total_weight"`
} }
type RecordingDepletionDTO struct { type RecordingDepletionDTO struct {
@@ -88,11 +91,14 @@ func ToRecordingRelationDTO(e entity.Recording) RecordingRelationDTO {
day int day int
totalDepletionQty float64 totalDepletionQty float64
cumDepletionRate float64 cumDepletionRate float64
dailyGain float64
avgDailyGain float64
cumIntake int cumIntake int
fcrValue float64 fcrValue float64
totalChickQty float64 totalChickQty float64
handDay float64
handHouse float64
feedIntake float64
eggMesh float64
eggWeight float64
) )
if e.Day != nil { if e.Day != nil {
@@ -104,12 +110,6 @@ func ToRecordingRelationDTO(e entity.Recording) RecordingRelationDTO {
if e.CumDepletionRate != nil { if e.CumDepletionRate != nil {
cumDepletionRate = *e.CumDepletionRate cumDepletionRate = *e.CumDepletionRate
} }
if e.DailyGain != nil {
dailyGain = *e.DailyGain
}
if e.AvgDailyGain != nil {
avgDailyGain = *e.AvgDailyGain
}
if e.CumIntake != nil { if e.CumIntake != nil {
cumIntake = *e.CumIntake cumIntake = *e.CumIntake
} }
@@ -119,6 +119,21 @@ func ToRecordingRelationDTO(e entity.Recording) RecordingRelationDTO {
if e.TotalChickQty != nil { if e.TotalChickQty != nil {
totalChickQty = *e.TotalChickQty totalChickQty = *e.TotalChickQty
} }
if e.HandDay != nil {
handDay = *e.HandDay
}
if e.HandHouse != nil {
handHouse = *e.HandHouse
}
if e.FeedIntake != nil {
feedIntake = *e.FeedIntake
}
if e.EggMesh != nil {
eggMesh = *e.EggMesh
}
if e.EggWeight != nil {
eggWeight = *e.EggWeight
}
if e.ProjectFlockKandang != nil && e.ProjectFlockKandang.ProjectFlock.Id != 0 { if e.ProjectFlockKandang != nil && e.ProjectFlockKandang.ProjectFlock.Id != 0 {
category := e.ProjectFlockKandang.ProjectFlock.Category category := e.ProjectFlockKandang.ProjectFlock.Category
@@ -139,11 +154,21 @@ func ToRecordingRelationDTO(e entity.Recording) RecordingRelationDTO {
ProjectFlockCategory: projectFlockCategory, ProjectFlockCategory: projectFlockCategory,
TotalDepletionQty: totalDepletionQty, TotalDepletionQty: totalDepletionQty,
CumDepletionRate: cumDepletionRate, CumDepletionRate: cumDepletionRate,
DailyGain: dailyGain,
AvgDailyGain: avgDailyGain,
CumIntake: cumIntake, CumIntake: cumIntake,
FcrValue: fcrValue, FcrValue: fcrValue,
TotalChickQty: totalChickQty, TotalChickQty: totalChickQty,
HandDay: handDay,
HandHouse: handHouse,
FeedIntake: feedIntake,
EggMesh: eggMesh,
EggWeight: eggWeight,
StandardHandDay: e.StandardHandDay,
StandardHandHouse: e.StandardHandHouse,
StandardFeedIntake: e.StandardFeedIntake,
StandardMaxDepletion: e.StandardMaxDepletion,
StandardEggMesh: e.StandardEggMesh,
StandardEggWeight: e.StandardEggWeight,
StandardFcr: e.StandardFcr,
Approval: latestApproval, Approval: latestApproval,
} }
} }
@@ -183,25 +208,12 @@ func ToRecordingDetailDTO(e entity.Recording) RecordingDetailDTO {
return RecordingDetailDTO{ return RecordingDetailDTO{
RecordingListDTO: listDTO, RecordingListDTO: listDTO,
BodyWeights: ToRecordingBodyWeightDTOs(e.BodyWeights),
Depletions: ToRecordingDepletionDTOs(e.Depletions), Depletions: ToRecordingDepletionDTOs(e.Depletions),
Stocks: ToRecordingStockDTOs(e.Stocks), Stocks: ToRecordingStockDTOs(e.Stocks),
Eggs: eggs, Eggs: eggs,
} }
} }
func ToRecordingBodyWeightDTOs(bodyWeights []entity.RecordingBW) []RecordingBodyWeightDTO {
result := make([]RecordingBodyWeightDTO, len(bodyWeights))
for i, bw := range bodyWeights {
result[i] = RecordingBodyWeightDTO{
AvgWeight: bw.AvgWeight,
Qty: bw.Qty,
TotalWeight: bw.TotalWeight,
}
}
return result
}
func ToRecordingDepletionDTOs(depletions []entity.RecordingDepletion) []RecordingDepletionDTO { func ToRecordingDepletionDTOs(depletions []entity.RecordingDepletion) []RecordingDepletionDTO {
result := make([]RecordingDepletionDTO, len(depletions)) result := make([]RecordingDepletionDTO, len(depletions))
for i, d := range depletions { for i, d := range depletions {
@@ -20,9 +20,6 @@ type RecordingRepository interface {
GetLatestByProjectFlockKandangID(ctx context.Context, projectFlockKandangId uint) (*entity.Recording, error) GetLatestByProjectFlockKandangID(ctx context.Context, projectFlockKandangId uint) (*entity.Recording, error)
GenerateNextDay(tx *gorm.DB, projectFlockKandangId uint) (int, error) GenerateNextDay(tx *gorm.DB, projectFlockKandangId uint) (int, error)
CreateBodyWeights(tx *gorm.DB, bodyWeights []entity.RecordingBW) error
DeleteBodyWeights(tx *gorm.DB, recordingID uint) error
CreateStocks(tx *gorm.DB, stocks []entity.RecordingStock) error CreateStocks(tx *gorm.DB, stocks []entity.RecordingStock) error
DeleteStocks(tx *gorm.DB, recordingID uint) error DeleteStocks(tx *gorm.DB, recordingID uint) error
ListStocks(tx *gorm.DB, recordingID uint) ([]entity.RecordingStock, error) ListStocks(tx *gorm.DB, recordingID uint) ([]entity.RecordingStock, error)
@@ -42,10 +39,11 @@ type RecordingRepository interface {
SumRecordingDepletions(tx *gorm.DB, recordingID uint) (float64, error) SumRecordingDepletions(tx *gorm.DB, recordingID uint) (float64, error)
FindPreviousRecording(tx *gorm.DB, projectFlockKandangId uint, currentDay int) (*entity.Recording, error) FindPreviousRecording(tx *gorm.DB, projectFlockKandangId uint, currentDay int) (*entity.Recording, error)
GetTotalChick(tx *gorm.DB, projectFlockKandangId uint) (int64, error) GetTotalChick(tx *gorm.DB, projectFlockKandangId uint) (int64, error)
GetAverageBodyWeight(tx *gorm.DB, recordingID uint) (float64, error) GetTotalChickinByProjectFlockKandang(tx *gorm.DB, projectFlockKandangId uint) (float64, error)
GetFeedUsageInGrams(tx *gorm.DB, recordingID uint) (float64, error) GetFeedUsageInGrams(tx *gorm.DB, recordingID uint) (float64, error)
GetFcrID(tx *gorm.DB, projectFlockKandangId uint) (uint, error) GetEggSummaryByRecording(tx *gorm.DB, recordingID uint) (totalQty float64, totalWeightGrams float64, err error)
GetFcrStandardWeightKg(tx *gorm.DB, fcrId uint, currentWeightKg float64) (float64, bool, error) GetCumulativeEggQtyByProjectFlockKandang(tx *gorm.DB, projectFlockKandangId uint, recordTime time.Time) (float64, error)
GetFcrStandardNumber(tx *gorm.DB, fcrId uint, currentWeightKg float64) (float64, bool, error)
GetProductionWeightAndQtyByProjectFlockID(ctx context.Context, projectFlockID uint) (totalWeight float64, totalQty float64, err error) GetProductionWeightAndQtyByProjectFlockID(ctx context.Context, projectFlockID uint) (totalWeight float64, totalQty float64, err error)
GetTotalDepletionByProjectFlockID(ctx context.Context, projectFlockID uint) (totalDepletion float64, err error) GetTotalDepletionByProjectFlockID(ctx context.Context, projectFlockID uint) (totalDepletion float64, err error)
GetLatestAvgWeightByProjectFlockID(ctx context.Context, projectFlockID uint) (avgWeight float64, err error) GetLatestAvgWeightByProjectFlockID(ctx context.Context, projectFlockID uint) (avgWeight float64, err error)
@@ -67,7 +65,6 @@ func (r *RecordingRepositoryImpl) WithRelations(db *gorm.DB) *gorm.DB {
Preload("CreatedUser"). Preload("CreatedUser").
Preload("ProjectFlockKandang"). Preload("ProjectFlockKandang").
Preload("ProjectFlockKandang.ProjectFlock"). Preload("ProjectFlockKandang.ProjectFlock").
Preload("BodyWeights").
Preload("Depletions"). Preload("Depletions").
Preload("Depletions.ProductWarehouse"). Preload("Depletions.ProductWarehouse").
Preload("Depletions.ProductWarehouse.Product"). Preload("Depletions.ProductWarehouse.Product").
@@ -114,17 +111,6 @@ func (r *RecordingRepositoryImpl) GenerateNextDay(tx *gorm.DB, projectFlockKanda
return nextRecordingDay(days), nil return nextRecordingDay(days), nil
} }
func (r *RecordingRepositoryImpl) CreateBodyWeights(tx *gorm.DB, bodyWeights []entity.RecordingBW) error {
if len(bodyWeights) == 0 {
return nil
}
return tx.Create(&bodyWeights).Error
}
func (r *RecordingRepositoryImpl) DeleteBodyWeights(tx *gorm.DB, recordingID uint) error {
return tx.Where("recording_id = ?", recordingID).Delete(&entity.RecordingBW{}).Error
}
func (r *RecordingRepositoryImpl) CreateStocks(tx *gorm.DB, stocks []entity.RecordingStock) error { func (r *RecordingRepositoryImpl) CreateStocks(tx *gorm.DB, stocks []entity.RecordingStock) error {
if len(stocks) == 0 { if len(stocks) == 0 {
return nil return nil
@@ -293,35 +279,33 @@ func (r *RecordingRepositoryImpl) GetTotalChick(tx *gorm.DB, projectFlockKandang
return int64(math.Round(total)), nil return int64(math.Round(total)), nil
} }
func (r *RecordingRepositoryImpl) GetAverageBodyWeight(tx *gorm.DB, recordingID uint) (float64, error) { func (r *RecordingRepositoryImpl) GetTotalChickinByProjectFlockKandang(tx *gorm.DB, projectFlockKandangId uint) (float64, error) {
var result struct { if projectFlockKandangId == 0 {
TotalWeight float64
TotalQty float64
}
if err := tx.Model(&entity.RecordingBW{}).
Select("COALESCE(SUM(total_weight), 0) AS total_weight, COALESCE(SUM(qty), 0) AS total_qty").
Where("recording_id = ?", recordingID).
Scan(&result).Error; err != nil {
return 0, err
}
if result.TotalQty == 0 {
return 0, nil return 0, nil
} }
return result.TotalWeight / result.TotalQty, nil
var result float64
err := tx.
Table("project_chickins").
Select("COALESCE(SUM(project_chickins.usage_qty), 0)").
Where("project_chickins.project_flock_kandang_id = ?", projectFlockKandangId).
Scan(&result).Error
return result, err
} }
func (r *RecordingRepositoryImpl) GetFeedUsageInGrams(tx *gorm.DB, recordingID uint) (float64, error) { func (r *RecordingRepositoryImpl) GetFeedUsageInGrams(tx *gorm.DB, recordingID uint) (float64, error) {
var rows []struct { var rows []struct {
UsageQty float64 TotalQty float64
UomName string UomName string
} }
if err := tx. if err := tx.
Table("recording_stocks"). Table("recording_stocks").
Select("COALESCE(recording_stocks.usage_qty, 0) AS usage_qty, LOWER(uoms.name) AS uom_name"). Select("COALESCE(recording_stocks.usage_qty, 0) + COALESCE(recording_stocks.pending_qty, 0) AS total_qty, LOWER(uoms.name) AS uom_name").
Joins("JOIN product_warehouses ON product_warehouses.id = recording_stocks.product_warehouse_id"). Joins("JOIN product_warehouses ON product_warehouses.id = recording_stocks.product_warehouse_id").
Joins("JOIN products ON products.id = product_warehouses.product_id"). Joins("JOIN products ON products.id = product_warehouses.product_id").
Joins("JOIN uoms ON uoms.id = products.uom_id"). Joins("JOIN uoms ON uoms.id = products.uom_id").
Joins("JOIN flags ON flags.flagable_id = products.id AND flags.flagable_type = ? AND UPPER(flags.name) = ?", entity.FlagableTypeProduct, "PAKAN").
Where("recording_stocks.recording_id = ?", recordingID). Where("recording_stocks.recording_id = ?", recordingID).
Scan(&rows).Error; err != nil { Scan(&rows).Error; err != nil {
return 0, err return 0, err
@@ -329,37 +313,63 @@ func (r *RecordingRepositoryImpl) GetFeedUsageInGrams(tx *gorm.DB, recordingID u
var total float64 var total float64
for _, row := range rows { for _, row := range rows {
if row.UsageQty <= 0 { if row.TotalQty <= 0 {
continue continue
} }
switch strings.TrimSpace(row.UomName) { switch strings.TrimSpace(row.UomName) {
case "kilogram", "kg", "kilograms", "kilo": case "kilogram", "kg", "kilograms", "kilo":
total += row.UsageQty * 1000 total += row.TotalQty * 1000
case "gram", "g", "grams": case "gram", "g", "grams":
total += row.UsageQty total += row.TotalQty
default: default:
total += row.UsageQty total += row.TotalQty
} }
} }
return total, nil return total, nil
} }
func (r *RecordingRepositoryImpl) GetFcrID(tx *gorm.DB, projectFlockKandangId uint) (uint, error) { func (r *RecordingRepositoryImpl) GetEggSummaryByRecording(tx *gorm.DB, recordingID uint) (totalQty float64, totalWeightGrams float64, err error) {
if recordingID == 0 {
return 0, 0, nil
}
var result struct { var result struct {
FcrID uint TotalQty float64
TotalWeightGrams float64
} }
if err := tx.Table("project_flock_kandangs"). err = tx.
Select("project_flocks.fcr_id AS fcr_id"). Table("recording_eggs").
Joins("JOIN project_flocks ON project_flocks.id = project_flock_kandangs.project_flock_id"). Select("COALESCE(SUM(recording_eggs.qty), 0) AS total_qty, COALESCE(SUM(recording_eggs.qty * COALESCE(recording_eggs.weight, 0)), 0) AS total_weight_grams").
Where("project_flock_kandangs.id = ?", projectFlockKandangId). Where("recording_eggs.recording_id = ?", recordingID).
Scan(&result).Error; err != nil { Scan(&result).Error
return 0, err if err != nil {
return 0, 0, err
} }
return result.FcrID, nil return result.TotalQty, result.TotalWeightGrams, nil
} }
func (r *RecordingRepositoryImpl) GetFcrStandardWeightKg(tx *gorm.DB, fcrId uint, currentWeightKg float64) (float64, bool, error) { func (r *RecordingRepositoryImpl) GetCumulativeEggQtyByProjectFlockKandang(
if fcrId == 0 { tx *gorm.DB,
projectFlockKandangId uint,
recordTime time.Time,
) (float64, error) {
if projectFlockKandangId == 0 {
return 0, nil
}
var result float64
err := tx.
Table("recording_eggs").
Select("COALESCE(SUM(recording_eggs.qty), 0)").
Joins("JOIN recordings ON recordings.id = recording_eggs.recording_id").
Where("recordings.project_flock_kandangs_id = ?", projectFlockKandangId).
Where("recordings.record_datetime <= ?", recordTime).
Scan(&result).Error
return result, err
}
func (r *RecordingRepositoryImpl) GetFcrStandardNumber(tx *gorm.DB, fcrId uint, currentWeightKg float64) (float64, bool, error) {
if fcrId == 0 || currentWeightKg <= 0 {
return 0, false, nil return 0, false, nil
} }
@@ -382,49 +392,12 @@ func (r *RecordingRepositoryImpl) GetFcrStandardWeightKg(tx *gorm.DB, fcrId uint
return 0, false, err return 0, false, err
} }
weight := standard.Weight return standard.FcrNumber, true, nil
if weight > 10 {
return weight / 1000, true, nil
}
return weight, true, nil
} }
func (r *RecordingRepositoryImpl) GetProductionWeightAndQtyByProjectFlockID(ctx context.Context, projectFlockID uint) (totalWeight float64, totalQty float64, err error) { func (r *RecordingRepositoryImpl) GetProductionWeightAndQtyByProjectFlockID(ctx context.Context, projectFlockID uint) (totalWeight float64, totalQty float64, err error) {
if projectFlockID == 0 { // Body-weight tracking is removed; keep stub for report compatibility.
return 0, 0, nil return 0, 0, nil
}
totalChickinQty, err := r.getTotalChickinQtyByProjectFlockID(ctx, projectFlockID)
if err != nil {
return 0, 0, err
}
totalDepletion, err := r.GetTotalDepletionByProjectFlockID(ctx, projectFlockID)
if err != nil {
return 0, 0, err
}
actualQty := totalChickinQty - totalDepletion
avgWeight, err := r.GetLatestAvgWeightByProjectFlockID(ctx, projectFlockID)
if err != nil {
return 0, 0, err
}
totalWeight = actualQty * avgWeight
return totalWeight, actualQty, nil
}
func (r *RecordingRepositoryImpl) getTotalChickinQtyByProjectFlockID(ctx context.Context, projectFlockID uint) (float64, error) {
var result float64
err := r.DB().WithContext(ctx).
Table("project_chickins").
Select("COALESCE(SUM(project_chickins.usage_qty), 0)").
Joins("JOIN project_flock_kandangs ON project_flock_kandangs.id = project_chickins.project_flock_kandang_id").
Where("project_flock_kandangs.project_flock_id = ?", projectFlockID).
Scan(&result).Error
return result, err
} }
func (r *RecordingRepositoryImpl) GetTotalDepletionByProjectFlockID(ctx context.Context, projectFlockID uint) (float64, error) { func (r *RecordingRepositoryImpl) GetTotalDepletionByProjectFlockID(ctx context.Context, projectFlockID uint) (float64, error) {
@@ -440,16 +413,8 @@ func (r *RecordingRepositoryImpl) GetTotalDepletionByProjectFlockID(ctx context.
} }
func (r *RecordingRepositoryImpl) GetLatestAvgWeightByProjectFlockID(ctx context.Context, projectFlockID uint) (float64, error) { func (r *RecordingRepositoryImpl) GetLatestAvgWeightByProjectFlockID(ctx context.Context, projectFlockID uint) (float64, error) {
var result float64 // Body-weight tracking is removed; keep stub for report compatibility.
err := r.DB().WithContext(ctx). return 0, nil
Table("recording_bws").
Select("COALESCE(AVG(recording_bws.avg_weight), 0)").
Joins("JOIN recordings ON recordings.id = recording_bws.recording_id").
Joins("JOIN project_flock_kandangs ON project_flock_kandangs.id = recordings.project_flock_kandangs_id").
Where("project_flock_kandangs.project_flock_id = ?", projectFlockID).
Where("recordings.record_datetime = (SELECT MAX(record_datetime) FROM recordings r2 WHERE r2.project_flock_kandangs_id IN (SELECT id FROM project_flock_kandangs WHERE project_flock_id = ?))", projectFlockID).
Scan(&result).Error
return result, err
} }
func (r *RecordingRepositoryImpl) GetTotalEggProductionWeightByProjectFlockID(ctx context.Context, projectFlockID uint) (float64, error) { func (r *RecordingRepositoryImpl) GetTotalEggProductionWeightByProjectFlockID(ctx context.Context, projectFlockID uint) (float64, error) {
@@ -9,6 +9,7 @@ import (
entity "gitlab.com/mbugroup/lti-api.git/internal/entities" entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
m "gitlab.com/mbugroup/lti-api.git/internal/middleware" m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
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"
rProductionStandard "gitlab.com/mbugroup/lti-api.git/internal/modules/master/production-standards/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"
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/repositories" repository "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/repositories"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/validations" validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/validations"
@@ -121,6 +122,9 @@ func (s recordingService) GetAll(c *fiber.Ctx, params *validation.Query) ([]enti
if err := s.attachLatestApprovals(c.Context(), recordings); err != nil { if err := s.attachLatestApprovals(c.Context(), recordings); err != nil {
return nil, 0, err return nil, 0, err
} }
if err := s.attachProductionStandards(c.Context(), recordings); err != nil {
return nil, 0, err
}
return recordings, total, nil return recordings, total, nil
} }
@@ -138,6 +142,9 @@ func (s recordingService) GetOne(c *fiber.Ctx, id uint) (*entity.Recording, erro
if err := s.attachLatestApproval(c.Context(), recording); err != nil { if err := s.attachLatestApproval(c.Context(), recording); err != nil {
return nil, err return nil, err
} }
if err := s.attachProductionStandard(c.Context(), recording); err != nil {
return nil, err
}
return recording, nil return recording, nil
} }
@@ -222,7 +229,13 @@ func (s *recordingService) CreateOne(c *fiber.Ctx, req *validation.Create) (*ent
CreatedBy: actorID, CreatedBy: actorID,
} }
if err := s.Repository.CreateOne(ctx, &createdRecording, func(*gorm.DB) *gorm.DB { return tx }); err != nil { createTx := tx.WithContext(ctx).Select(
"ProjectFlockKandangId",
"RecordDatetime",
"Day",
"CreatedBy",
)
if err := createTx.Create(&createdRecording).Error; err != nil {
if errors.Is(err, gorm.ErrDuplicatedKey) { if errors.Is(err, gorm.ErrDuplicatedKey) {
return fiber.NewError( return fiber.NewError(
fiber.StatusBadRequest, fiber.StatusBadRequest,
@@ -233,18 +246,14 @@ func (s *recordingService) CreateOne(c *fiber.Ctx, req *validation.Create) (*ent
return err return err
} }
mappedBodyWeights := recordingutil.MapBodyWeights(createdRecording.Id, req.BodyWeights)
if err := s.Repository.CreateBodyWeights(tx, mappedBodyWeights); err != nil {
s.Log.Errorf("Failed to persist body weights: %+v", err)
return err
}
mappedStocks := recordingutil.MapStocks(createdRecording.Id, req.Stocks) mappedStocks := recordingutil.MapStocks(createdRecording.Id, req.Stocks)
stockDesired := resetStockQuantitiesForFIFO(mappedStocks, s.FifoSvc != nil)
if err := s.Repository.CreateStocks(tx, mappedStocks); err != nil { if err := s.Repository.CreateStocks(tx, mappedStocks); err != nil {
s.Log.Errorf("Failed to persist stocks: %+v", err) s.Log.Errorf("Failed to persist stocks: %+v", err)
return err return err
} }
applyStockDesiredQuantities(mappedStocks, stockDesired, s.FifoSvc != nil)
if err := s.consumeRecordingStocks(ctx, tx, mappedStocks); err != nil { if err := s.consumeRecordingStocks(ctx, tx, mappedStocks); err != nil {
return err return err
} }
@@ -261,7 +270,7 @@ func (s *recordingService) CreateOne(c *fiber.Ctx, req *validation.Create) (*ent
return err return err
} }
if err := s.adjustProductWarehouseQuantities(ctx, tx, buildWarehouseDeltas(nil, mappedDepletions, nil, nil, nil, mappedEggs)); err != nil { if err := s.adjustProductWarehouseQuantities(ctx, tx, buildWarehouseDeltas(nil, mappedDepletions, nil, mappedEggs)); err != nil {
s.Log.Errorf("Failed to adjust product warehouses: %+v", err) s.Log.Errorf("Failed to adjust product warehouses: %+v", err)
return err return err
} }
@@ -291,16 +300,18 @@ func (s recordingService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uin
return nil, err return nil, err
} }
if req.BodyWeights == nil && req.Stocks == nil && req.Depletions == nil && req.Eggs == nil { if req.Stocks == nil && req.Depletions == nil && req.Eggs == nil {
return s.GetOne(c, id) return s.GetOne(c, id)
} }
ctx := c.Context() ctx := c.Context()
var recordingEntity *entity.Recording var recordingEntity *entity.Recording
var updatedRecording *entity.Recording
transactionErr := s.Repository.DB().WithContext(ctx).Transaction(func(tx *gorm.DB) error { transactionErr := s.Repository.DB().WithContext(ctx).Transaction(func(tx *gorm.DB) error {
recording, err := s.Repository.GetByID(ctx, id, func(db *gorm.DB) *gorm.DB { repoTx := s.Repository.WithTx(tx)
return s.Repository.WithRelations(tx) recording, err := repoTx.GetByID(ctx, id, func(db *gorm.DB) *gorm.DB {
return s.Repository.WithRelations(db)
}) })
if err != nil { if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
@@ -311,12 +322,47 @@ func (s recordingService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uin
} }
recordingEntity = recording recordingEntity = recording
hasBodyChanges := req.BodyWeights != nil
hasStockChanges := req.Stocks != nil hasStockChanges := req.Stocks != nil
hasDepletionChanges := req.Depletions != nil hasDepletionChanges := req.Depletions != nil
hasEggChanges := req.Eggs != nil hasEggChanges := req.Eggs != nil
if !hasBodyChanges && !hasStockChanges && !hasDepletionChanges && !hasEggChanges { var existingStocks []entity.RecordingStock
if hasStockChanges {
existingStocks, err = s.Repository.ListStocks(tx, recordingEntity.Id)
if err != nil {
s.Log.Errorf("Failed to list existing stocks: %+v", err)
return err
}
if stocksMatch(existingStocks, req.Stocks) {
hasStockChanges = false
}
}
var existingDepletions []entity.RecordingDepletion
if hasDepletionChanges {
existingDepletions, err = s.Repository.ListDepletions(tx, recordingEntity.Id)
if err != nil {
s.Log.Errorf("Failed to list existing depletions: %+v", err)
return err
}
if depletionsMatch(existingDepletions, req.Depletions) {
hasDepletionChanges = false
}
}
var existingEggs []entity.RecordingEgg
if hasEggChanges {
existingEggs, err = s.Repository.ListEggs(tx, recordingEntity.Id)
if err != nil {
s.Log.Errorf("Failed to list existing eggs: %+v", err)
return err
}
if eggsMatch(existingEggs, req.Eggs) {
hasEggChanges = false
}
}
if !hasStockChanges && !hasDepletionChanges && !hasEggChanges {
return nil return nil
} }
@@ -346,51 +392,13 @@ func (s recordingService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uin
} }
} }
if hasBodyChanges {
if err := s.Repository.DeleteBodyWeights(tx, recordingEntity.Id); err != nil {
s.Log.Errorf("Failed to clear body weights: %+v", err)
return err
}
if err := s.Repository.CreateBodyWeights(tx, recordingutil.MapBodyWeights(recordingEntity.Id, req.BodyWeights)); err != nil {
s.Log.Errorf("Failed to update body weights: %+v", err)
return err
}
}
if hasStockChanges { if hasStockChanges {
existingStocks, err := s.Repository.ListStocks(tx, recordingEntity.Id) if err := s.syncRecordingStocks(ctx, tx, recordingEntity.Id, existingStocks, req.Stocks); err != nil {
if err != nil {
s.Log.Errorf("Failed to list existing stocks: %+v", err)
return err
}
if err := s.releaseRecordingStocks(ctx, tx, existingStocks); err != nil {
return err
}
if err := s.Repository.DeleteStocks(tx, recordingEntity.Id); err != nil {
s.Log.Errorf("Failed to clear stocks: %+v", err)
return err
}
mappedStocks := recordingutil.MapStocks(recordingEntity.Id, req.Stocks)
if err := s.Repository.CreateStocks(tx, mappedStocks); err != nil {
s.Log.Errorf("Failed to update stocks: %+v", err)
return err
}
if err := s.consumeRecordingStocks(ctx, tx, mappedStocks); err != nil {
return err return err
} }
} }
if hasDepletionChanges { if hasDepletionChanges {
existingDepletions, err := s.Repository.ListDepletions(tx, recordingEntity.Id)
if err != nil {
s.Log.Errorf("Failed to list existing depletions: %+v", err)
return err
}
if err := s.Repository.DeleteDepletions(tx, recordingEntity.Id); err != nil { if err := s.Repository.DeleteDepletions(tx, recordingEntity.Id); err != nil {
s.Log.Errorf("Failed to clear depletions: %+v", err) s.Log.Errorf("Failed to clear depletions: %+v", err)
return err return err
@@ -402,19 +410,13 @@ func (s recordingService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uin
return err return err
} }
if err := s.adjustProductWarehouseQuantities(ctx, tx, buildWarehouseDeltas(existingDepletions, mappedDepletions, nil, nil, nil, nil)); err != nil { if err := s.adjustProductWarehouseQuantities(ctx, tx, buildWarehouseDeltas(existingDepletions, mappedDepletions, nil, nil)); err != nil {
s.Log.Errorf("Failed to adjust product warehouses for depletions: %+v", err) s.Log.Errorf("Failed to adjust product warehouses for depletions: %+v", err)
return err return err
} }
} }
if hasEggChanges { if hasEggChanges {
existingEggs, err := s.Repository.ListEggs(tx, recordingEntity.Id)
if err != nil {
s.Log.Errorf("Failed to list existing eggs: %+v", err)
return err
}
if err := s.Repository.DeleteEggs(tx, recordingEntity.Id); err != nil { if err := s.Repository.DeleteEggs(tx, recordingEntity.Id); err != nil {
s.Log.Errorf("Failed to clear eggs: %+v", err) s.Log.Errorf("Failed to clear eggs: %+v", err)
return err return err
@@ -426,13 +428,13 @@ func (s recordingService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uin
return err return err
} }
if err := s.adjustProductWarehouseQuantities(ctx, tx, buildWarehouseDeltas(nil, nil, nil, nil, existingEggs, mappedEggs)); err != nil { if err := s.adjustProductWarehouseQuantities(ctx, tx, buildWarehouseDeltas(nil, nil, existingEggs, mappedEggs)); err != nil {
s.Log.Errorf("Failed to adjust product warehouses for eggs: %+v", err) s.Log.Errorf("Failed to adjust product warehouses for eggs: %+v", err)
return err return err
} }
} }
if hasBodyChanges || hasStockChanges || hasDepletionChanges { if hasStockChanges || hasDepletionChanges || hasEggChanges {
if err := s.computeAndUpdateMetrics(ctx, tx, recordingEntity); err != nil { if err := s.computeAndUpdateMetrics(ctx, tx, recordingEntity); err != nil {
s.Log.Errorf("Failed to recompute recording metrics: %+v", err) s.Log.Errorf("Failed to recompute recording metrics: %+v", err)
return err return err
@@ -481,13 +483,31 @@ func (s recordingService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uin
} }
} }
updated, err := repoTx.GetByID(ctx, recordingEntity.Id, func(db *gorm.DB) *gorm.DB {
return s.Repository.WithRelations(db)
})
if err != nil {
s.Log.Errorf("Failed to reload recording %d after update: %+v", recordingEntity.Id, err)
return err
}
updatedRecording = updated
return nil return nil
}) })
if transactionErr != nil { if transactionErr != nil {
return nil, transactionErr return nil, transactionErr
} }
return s.GetOne(c, id) if updatedRecording == nil {
return s.GetOne(c, id)
}
if err := s.attachLatestApproval(ctx, updatedRecording); err != nil {
return nil, err
}
if err := s.attachProductionStandard(ctx, updatedRecording); err != nil {
return nil, err
}
return updatedRecording, nil
} }
func (s recordingService) Approval(c *fiber.Ctx, req *validation.Approve) ([]entity.Recording, error) { func (s recordingService) Approval(c *fiber.Ctx, req *validation.Approve) ([]entity.Recording, error) {
@@ -596,7 +616,7 @@ func (s recordingService) DeleteOne(c *fiber.Ctx, id uint) error {
return err return err
} }
if err := s.adjustProductWarehouseQuantities(ctx, tx, buildWarehouseDeltas(oldDepletions, nil, nil, nil, oldEggs, nil)); err != nil { if err := s.adjustProductWarehouseQuantities(ctx, tx, buildWarehouseDeltas(oldDepletions, nil, oldEggs, nil)); err != nil {
return err return err
} }
@@ -665,12 +685,17 @@ func (s *recordingService) consumeRecordingStocks(ctx context.Context, tx *gorm.
if stock.UsageQty != nil { if stock.UsageQty != nil {
desired = *stock.UsageQty desired = *stock.UsageQty
} }
var pending float64
if stock.PendingQty != nil {
pending = *stock.PendingQty
}
desiredTotal := desired + pending
result, err := s.FifoSvc.Consume(ctx, commonSvc.StockConsumeRequest{ result, err := s.FifoSvc.Consume(ctx, commonSvc.StockConsumeRequest{
UsableKey: recordingStockUsableKey, UsableKey: recordingStockUsableKey,
UsableID: stock.Id, UsableID: stock.Id,
ProductWarehouseID: stock.ProductWarehouseId, ProductWarehouseID: stock.ProductWarehouseId,
Quantity: desired, Quantity: desiredTotal,
AllowPending: true, AllowPending: true,
Tx: tx, Tx: tx,
}) })
@@ -724,7 +749,6 @@ func (s *recordingService) ReleaseRecordingStocks(ctx context.Context, tx *gorm.
func buildWarehouseDeltas( func buildWarehouseDeltas(
oldDepletions, newDepletions []entity.RecordingDepletion, oldDepletions, newDepletions []entity.RecordingDepletion,
oldStocks, newStocks []entity.RecordingStock,
oldEggs, newEggs []entity.RecordingEgg, oldEggs, newEggs []entity.RecordingEgg,
) map[uint]float64 { ) map[uint]float64 {
deltas := make(map[uint]float64) deltas := make(map[uint]float64)
@@ -757,6 +781,288 @@ func (s *recordingService) adjustProductWarehouseQuantities(ctx context.Context,
return s.ProductWarehouseRepo.AdjustQuantities(ctx, deltas, func(*gorm.DB) *gorm.DB { return tx }) return s.ProductWarehouseRepo.AdjustQuantities(ctx, deltas, func(*gorm.DB) *gorm.DB { return tx })
} }
type desiredStock struct {
Usage float64
Pending float64
}
func resetStockQuantitiesForFIFO(stocks []entity.RecordingStock, enabled bool) []desiredStock {
desired := make([]desiredStock, len(stocks))
for i := range stocks {
if stocks[i].UsageQty != nil {
desired[i].Usage = *stocks[i].UsageQty
}
if stocks[i].PendingQty != nil {
desired[i].Pending = *stocks[i].PendingQty
}
if !enabled {
continue
}
zero := 0.0
stocks[i].UsageQty = &zero
stocks[i].PendingQty = &zero
}
return desired
}
func applyStockDesiredQuantities(stocks []entity.RecordingStock, desired []desiredStock, enabled bool) {
if !enabled {
return
}
for i := range stocks {
if i >= len(desired) {
break
}
usage := desired[i].Usage
pending := desired[i].Pending
stocks[i].UsageQty = &usage
stocks[i].PendingQty = &pending
}
}
func (s *recordingService) syncRecordingStocks(
ctx context.Context,
tx *gorm.DB,
recordingID uint,
existing []entity.RecordingStock,
incoming []validation.Stock,
) error {
if s.FifoSvc == nil {
if err := s.Repository.DeleteStocks(tx, recordingID); err != nil {
return err
}
mapped := recordingutil.MapStocks(recordingID, incoming)
return s.Repository.CreateStocks(tx, mapped)
}
existingByWarehouse := make(map[uint][]entity.RecordingStock)
for _, stock := range existing {
existingByWarehouse[stock.ProductWarehouseId] = append(existingByWarehouse[stock.ProductWarehouseId], stock)
}
stocksToConsume := make([]entity.RecordingStock, 0, len(incoming))
for _, item := range incoming {
list := existingByWarehouse[item.ProductWarehouseId]
var stock entity.RecordingStock
if len(list) > 0 {
stock = list[0]
existingByWarehouse[item.ProductWarehouseId] = list[1:]
} else {
zero := 0.0
stock = entity.RecordingStock{
RecordingId: recordingID,
ProductWarehouseId: item.ProductWarehouseId,
UsageQty: &zero,
PendingQty: &zero,
}
if err := tx.Create(&stock).Error; err != nil {
return err
}
}
desired := item.Qty
stock.UsageQty = &desired
if item.PendingQty != nil {
pending := *item.PendingQty
stock.PendingQty = &pending
}
stocksToConsume = append(stocksToConsume, stock)
}
var leftovers []entity.RecordingStock
for _, list := range existingByWarehouse {
leftovers = append(leftovers, list...)
}
if len(leftovers) > 0 {
if err := s.releaseRecordingStocks(ctx, tx, leftovers); err != nil {
return err
}
ids := make([]uint, 0, len(leftovers))
for _, stock := range leftovers {
if stock.Id != 0 {
ids = append(ids, stock.Id)
}
}
if len(ids) > 0 {
if err := tx.Where("id IN ?", ids).Delete(&entity.RecordingStock{}).Error; err != nil {
return err
}
}
}
if len(stocksToConsume) == 0 {
return nil
}
return s.consumeRecordingStocks(ctx, tx, stocksToConsume)
}
type eggTotals struct {
Qty int
Weight float64
}
type stockTotals struct {
Usage float64
Pending float64
Total float64
}
func summarizeExistingStocks(stocks []entity.RecordingStock) map[uint]stockTotals {
totals := make(map[uint]stockTotals)
for _, stock := range stocks {
var usage float64
var pending float64
if stock.UsageQty != nil {
usage = *stock.UsageQty
}
if stock.PendingQty != nil {
pending = *stock.PendingQty
}
current := totals[stock.ProductWarehouseId]
current.Usage += usage
current.Pending += pending
current.Total += usage + pending
totals[stock.ProductWarehouseId] = current
}
return totals
}
func summarizeIncomingStocks(stocks []validation.Stock) map[uint]stockTotals {
totals := make(map[uint]stockTotals)
for _, stock := range stocks {
var pending float64
if stock.PendingQty != nil {
pending = *stock.PendingQty
}
current := totals[stock.ProductWarehouseId]
current.Usage += stock.Qty
current.Pending += pending
current.Total += stock.Qty + pending
totals[stock.ProductWarehouseId] = current
}
return totals
}
func stocksMatch(existing []entity.RecordingStock, incoming []validation.Stock) bool {
hasPending := false
for _, item := range incoming {
if item.PendingQty != nil {
hasPending = true
break
}
}
existingUsage := make(map[uint]float64)
existingTotal := make(map[uint]float64)
for _, stock := range existing {
var usage float64
var pending float64
if stock.UsageQty != nil {
usage = *stock.UsageQty
}
if stock.PendingQty != nil {
pending = *stock.PendingQty
}
existingUsage[stock.ProductWarehouseId] += usage
existingTotal[stock.ProductWarehouseId] += usage + pending
}
incomingUsage := make(map[uint]float64)
incomingTotal := make(map[uint]float64)
for _, item := range incoming {
var pending float64
if item.PendingQty != nil {
pending = *item.PendingQty
}
incomingUsage[item.ProductWarehouseId] += item.Qty
incomingTotal[item.ProductWarehouseId] += item.Qty + pending
}
if hasPending {
return floatMapsMatch(existingTotal, incomingTotal)
}
return floatMapsMatch(existingUsage, incomingUsage)
}
func depletionsMatch(existing []entity.RecordingDepletion, incoming []validation.Depletion) bool {
existingTotals := make(map[uint]float64)
for _, dep := range existing {
existingTotals[dep.ProductWarehouseId] += dep.Qty
}
incomingTotals := make(map[uint]float64)
for _, dep := range incoming {
incomingTotals[dep.ProductWarehouseId] += dep.Qty
}
return floatMapsMatch(existingTotals, incomingTotals)
}
func eggsMatch(existing []entity.RecordingEgg, incoming []validation.Egg) bool {
existingTotals := make(map[uint]eggTotals)
for _, egg := range existing {
weight := 0.0
if egg.Weight != nil {
weight = *egg.Weight
}
current := existingTotals[egg.ProductWarehouseId]
current.Qty += egg.Qty
current.Weight += float64(egg.Qty) * weight
existingTotals[egg.ProductWarehouseId] = current
}
incomingTotals := make(map[uint]eggTotals)
for _, egg := range incoming {
weight := 0.0
if egg.Weight != nil {
weight = *egg.Weight
}
current := incomingTotals[egg.ProductWarehouseId]
current.Qty += egg.Qty
current.Weight += float64(egg.Qty) * weight
incomingTotals[egg.ProductWarehouseId] = current
}
if len(existingTotals) != len(incomingTotals) {
return false
}
for key, existingTotal := range existingTotals {
incomingTotal, ok := incomingTotals[key]
if !ok {
return false
}
if existingTotal.Qty != incomingTotal.Qty {
return false
}
if !floatNearlyEqual(existingTotal.Weight, incomingTotal.Weight) {
return false
}
}
return true
}
func floatMapsMatch(a, b map[uint]float64) bool {
if len(a) != len(b) {
return false
}
for key, value := range a {
other, ok := b[key]
if !ok {
return false
}
if !floatNearlyEqual(value, other) {
return false
}
}
return true
}
func floatNearlyEqual(a, b float64) bool {
return math.Abs(a-b) <= 0.000001
}
func (s *recordingService) computeAndUpdateMetrics(ctx context.Context, tx *gorm.DB, recording *entity.Recording) error { func (s *recordingService) computeAndUpdateMetrics(ctx context.Context, tx *gorm.DB, recording *entity.Recording) error {
day := 0 day := 0
if recording.Day != nil { if recording.Day != nil {
@@ -775,7 +1081,6 @@ func (s *recordingService) computeAndUpdateMetrics(ctx context.Context, tx *gorm
var prevCumDepletionQty float64 var prevCumDepletionQty float64
var prevCumIntake float64 var prevCumIntake float64
var prevAvgWeight float64
if prevRecording != nil { if prevRecording != nil {
if prevRecording.TotalDepletionQty != nil { if prevRecording.TotalDepletionQty != nil {
prevCumDepletionQty = *prevRecording.TotalDepletionQty prevCumDepletionQty = *prevRecording.TotalDepletionQty
@@ -783,10 +1088,6 @@ func (s *recordingService) computeAndUpdateMetrics(ctx context.Context, tx *gorm
if prevRecording.CumIntake != nil { if prevRecording.CumIntake != nil {
prevCumIntake = float64(*prevRecording.CumIntake) prevCumIntake = float64(*prevRecording.CumIntake)
} }
prevAvgWeight, err = s.Repository.GetAverageBodyWeight(tx, prevRecording.Id)
if err != nil {
return fmt.Errorf("getAverageBodyWeight(prev): %w", err)
}
} }
totalChick, err := s.Repository.GetTotalChick(tx, recording.ProjectFlockKandangId) totalChick, err := s.Repository.GetTotalChick(tx, recording.ProjectFlockKandangId)
@@ -794,20 +1095,25 @@ func (s *recordingService) computeAndUpdateMetrics(ctx context.Context, tx *gorm
return fmt.Errorf("getTotalChick: %w", err) return fmt.Errorf("getTotalChick: %w", err)
} }
currentAvgWeight, err := s.Repository.GetAverageBodyWeight(tx, recording.Id)
if err != nil {
return fmt.Errorf("getAverageBodyWeight(current): %w", err)
}
usageInGrams, err := s.Repository.GetFeedUsageInGrams(tx, recording.Id) usageInGrams, err := s.Repository.GetFeedUsageInGrams(tx, recording.Id)
if err != nil { if err != nil {
return fmt.Errorf("getFeedUsageInGrams: %w", err) return fmt.Errorf("getFeedUsageInGrams: %w", err)
} }
currentAvgGrams := recordingutil.ToGrams(currentAvgWeight) totalEggQty, totalEggWeightGrams, err := s.Repository.GetEggSummaryByRecording(tx, recording.Id)
currentAvgKg := recordingutil.GramsToKg(currentAvgGrams) if err != nil {
prevAvgGrams := recordingutil.ToGrams(prevAvgWeight) return fmt.Errorf("getEggSummaryByRecording: %w", err)
prevAvgKg := recordingutil.GramsToKg(prevAvgGrams) }
cumulativeEggQty, err := s.Repository.GetCumulativeEggQtyByProjectFlockKandang(tx, recording.ProjectFlockKandangId, recording.RecordDatetime)
if err != nil {
return fmt.Errorf("getCumulativeEggQtyByProjectFlockKandang: %w", err)
}
initialChickin, err := s.Repository.GetTotalChickinByProjectFlockKandang(tx, recording.ProjectFlockKandangId)
if err != nil {
return fmt.Errorf("getTotalChickinByProjectFlockKandang: %w", err)
}
currentDepletion := float64(totalDepletionQty) currentDepletion := float64(totalDepletionQty)
cumDepletionQty := prevCumDepletionQty + currentDepletion cumDepletionQty := prevCumDepletionQty + currentDepletion
@@ -840,24 +1146,64 @@ func (s *recordingService) computeAndUpdateMetrics(ctx context.Context, tx *gorm
recording.CumDepletionRate = nil recording.CumDepletionRate = nil
} }
if currentAvgGrams > 0 && prevAvgGrams > 0 { var feedIntake float64
dailyGainKg := (currentAvgGrams - prevAvgGrams) / 1000 if remainingChick > 0 && usageInGrams > 0 {
updates["daily_gain"] = dailyGainKg feedIntake = (usageInGrams / remainingChick) * 1000
recording.DailyGain = &dailyGainKg updates["feed_intake"] = feedIntake
recording.FeedIntake = &feedIntake
} else { } else {
dailyGainKg := 0.0 updates["feed_intake"] = gorm.Expr("NULL")
updates["daily_gain"] = dailyGainKg recording.FeedIntake = nil
recording.DailyGain = &dailyGainKg
} }
if currentAvgKg > 0 && remainingChick > 0 { var handDay float64
avgDailyGain := (currentAvgKg - prevAvgKg) / remainingChick if remainingChick > 0 && totalEggQty >= 0 {
updates["avg_daily_gain"] = avgDailyGain handDay = (totalEggQty / remainingChick) * 100
recording.AvgDailyGain = &avgDailyGain updates["hand_day"] = handDay
recording.HandDay = &handDay
} else { } else {
avgDailyGain := 0.0 updates["hand_day"] = gorm.Expr("NULL")
updates["avg_daily_gain"] = avgDailyGain recording.HandDay = nil
recording.AvgDailyGain = &avgDailyGain }
var handHouse float64
if initialChickin > 0 && cumulativeEggQty >= 0 {
handHouse = cumulativeEggQty / initialChickin
updates["hand_house"] = handHouse
recording.HandHouse = &handHouse
} else {
updates["hand_house"] = gorm.Expr("NULL")
recording.HandHouse = nil
}
var eggMesh float64
if remainingChick > 0 && totalEggWeightGrams > 0 {
eggMesh = (totalEggWeightGrams / remainingChick) * 1000
updates["egg_mesh"] = eggMesh
recording.EggMesh = &eggMesh
} else {
updates["egg_mesh"] = gorm.Expr("NULL")
recording.EggMesh = nil
}
var eggWeight float64
if totalEggQty > 0 && totalEggWeightGrams > 0 {
eggWeight = (totalEggWeightGrams / totalEggQty) * 1000
updates["egg_weight"] = eggWeight
recording.EggWeight = &eggWeight
} else {
updates["egg_weight"] = gorm.Expr("NULL")
recording.EggWeight = nil
}
var fcrValue float64
if usageInGrams > 0 && totalEggWeightGrams > 0 {
fcrValue = totalEggWeightGrams / usageInGrams
updates["fcr_value"] = fcrValue
recording.FcrValue = &fcrValue
} else {
updates["fcr_value"] = gorm.Expr("NULL")
recording.FcrValue = nil
} }
if usageInGrams > 0 && totalChick > 0 { if usageInGrams > 0 && totalChick > 0 {
@@ -882,16 +1228,6 @@ func (s *recordingService) computeAndUpdateMetrics(ctx context.Context, tx *gorm
recording.CumIntake = nil recording.CumIntake = nil
} }
if usageInGrams > 0 && currentAvgKg > 0 {
feedUsageKg := usageInGrams / 1000
fcrValue := feedUsageKg / currentAvgKg
updates["fcr_value"] = fcrValue
recording.FcrValue = &fcrValue
} else {
updates["fcr_value"] = gorm.Expr("NULL")
recording.FcrValue = nil
}
if err := s.Repository.WithTx(tx).PatchOne(ctx, recording.Id, updates, nil); err != nil { if err := s.Repository.WithTx(tx).PatchOne(ctx, recording.Id, updates, nil); err != nil {
return err return err
} }
@@ -997,6 +1333,104 @@ func (s *recordingService) attachLatestApproval(ctx context.Context, item *entit
return nil return nil
} }
type productionStandardValues struct {
HandDay *float64
HandHouse *float64
FeedIntake *float64
MaxDepletion *float64
EggMesh *float64
EggWeight *float64
}
func (s *recordingService) attachProductionStandards(ctx context.Context, items []entity.Recording) error {
if len(items) == 0 {
return nil
}
for i := range items {
if err := s.attachProductionStandard(ctx, &items[i]); err != nil {
s.Log.Warnf("Unable to load production standard for recording %d: %+v", items[i].Id, err)
}
}
return nil
}
func (s *recordingService) attachProductionStandard(ctx context.Context, item *entity.Recording) error {
if item == nil || item.Id == 0 {
return nil
}
if item.Day == nil || *item.Day <= 0 {
return nil
}
if item.ProjectFlockKandang == nil || item.ProjectFlockKandang.ProjectFlock.Id == 0 {
return nil
}
standardID := item.ProjectFlockKandang.ProjectFlock.ProductionStandardId
if standardID == 0 {
return nil
}
week := ((int(*item.Day) - 1) / 7) + 1
if week <= 0 {
return nil
}
category := strings.ToUpper(item.ProjectFlockKandang.ProjectFlock.Category)
db := s.Repository.DB()
standardDetailRepo := rProductionStandard.NewProductionStandardDetailRepository(db)
growthDetailRepo := rProductionStandard.NewStandardGrowthDetailRepository(db)
var standard productionStandardValues
var standardFcr *float64
if category == string(utils.ProjectFlockCategoryLaying) {
detail, err := standardDetailRepo.GetByStandardIDAndWeek(ctx, standardID, week)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
if detail != nil {
standard.HandDay = detail.TargetHenDayProduction
standard.HandHouse = detail.TargetHenHouseProduction
standard.EggWeight = detail.TargetEggWeight
standard.EggMesh = detail.TargetEggMass
}
}
growthDetail, err := growthDetailRepo.GetByStandardIDAndWeek(ctx, standardID, week)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
if growthDetail != nil {
standard.FeedIntake = growthDetail.FeedIntake
standard.MaxDepletion = growthDetail.MaxDepletion
if category == string(utils.ProjectFlockCategoryLaying) && growthDetail.TargetMeanBw != nil && item.ProjectFlockKandang.ProjectFlock.FcrId > 0 {
targetWeight := *growthDetail.TargetMeanBw
if targetWeight > 10 {
targetWeight = targetWeight / 1000
}
if targetWeight > 0 {
fcrStd, ok, err := s.Repository.GetFcrStandardNumber(db, item.ProjectFlockKandang.ProjectFlock.FcrId, targetWeight)
if err != nil {
return err
}
if ok {
standardFcr = &fcrStd
}
}
}
}
item.StandardHandDay = standard.HandDay
item.StandardHandHouse = standard.HandHouse
item.StandardFeedIntake = standard.FeedIntake
item.StandardMaxDepletion = standard.MaxDepletion
item.StandardEggMesh = standard.EggMesh
item.StandardEggWeight = standard.EggWeight
item.StandardFcr = standardFcr
return nil
}
func uniqueUintSlice(values []uint) []uint { func uniqueUintSlice(values []uint) []uint {
if len(values) == 0 { if len(values) == 0 {
return nil return nil
@@ -1,12 +1,6 @@
package validation package validation
type ( type (
BodyWeight struct {
AvgWeight float64 `json:"avg_weight" validate:"required"`
Qty float64 `json:"qty" validate:"required,gt=0"`
TotalWeight *float64 `json:"total_weight,omitempty" validate:"omitempty,gte=0"`
}
Stock struct { Stock struct {
ProductWarehouseId uint `json:"product_warehouse_id" validate:"required,number,min=1"` ProductWarehouseId uint `json:"product_warehouse_id" validate:"required,number,min=1"`
Qty float64 `json:"qty" validate:"required,gte=0"` Qty float64 `json:"qty" validate:"required,gte=0"`
@@ -27,14 +21,12 @@ type (
type Create struct { type Create struct {
ProjectFlockKandangId uint `json:"project_flock_kandang_id" validate:"required,number,min=1"` ProjectFlockKandangId uint `json:"project_flock_kandang_id" validate:"required,number,min=1"`
BodyWeights []BodyWeight `json:"body_weights" validate:"dive"`
Stocks []Stock `json:"stocks" validate:"dive"` Stocks []Stock `json:"stocks" validate:"dive"`
Depletions []Depletion `json:"depletions" validate:"dive"` Depletions []Depletion `json:"depletions" validate:"dive"`
Eggs []Egg `json:"eggs" validate:"omitempty,dive"` Eggs []Egg `json:"eggs" validate:"omitempty,dive"`
} }
type Update struct { type Update struct {
BodyWeights []BodyWeight `json:"body_weights,omitempty" validate:"omitempty,dive"`
Stocks []Stock `json:"stocks,omitempty" validate:"omitempty,dive"` Stocks []Stock `json:"stocks,omitempty" validate:"omitempty,dive"`
Depletions []Depletion `json:"depletions,omitempty" validate:"omitempty,dive"` Depletions []Depletion `json:"depletions,omitempty" validate:"omitempty,dive"`
Eggs []Egg `json:"eggs,omitempty" validate:"omitempty,dive"` Eggs []Egg `json:"eggs,omitempty" validate:"omitempty,dive"`
@@ -71,13 +71,14 @@ func (u *UniformityController) GetOne(c *fiber.Ctx) error {
withDetails := c.QueryBool("with_details", false) withDetails := c.QueryBool("with_details", false)
calculation := service.UniformityCalculation{} calculation := service.UniformityCalculation{}
var document *entity.Document var document *entity.Document
var documentURL string
var meanWeight float64 var meanWeight float64
if result.MeanUp > 0 { if result.MeanUp > 0 {
meanWeight = math.Round(result.MeanUp / 1.10) meanWeight = math.Round(result.MeanUp / 1.10)
} }
if withDetails { if withDetails {
var err error var err error
calculation, document, err = u.UniformityService.CalculateUniformityFromDocument(c, id) calculation, document, documentURL, err = u.UniformityService.CalculateUniformityFromDocument(c, id)
if err != nil { if err != nil {
return err return err
} }
@@ -92,6 +93,10 @@ func (u *UniformityController) GetOne(c *fiber.Ctx) error {
Uniformity: result.Uniformity, Uniformity: result.Uniformity,
Cv: result.Cv, Cv: result.Cv,
} }
document, documentURL, err = u.UniformityService.GetDocumentInfo(c, id)
if err != nil {
return err
}
} }
standard, err := u.UniformityService.GetStandard(c, result) standard, err := u.UniformityService.GetStandard(c, result)
@@ -111,7 +116,7 @@ func (u *UniformityController) GetOne(c *fiber.Ctx) error {
Code: fiber.StatusOK, Code: fiber.StatusOK,
Status: "success", Status: "success",
Message: "Get production uniformity successfully", Message: "Get production uniformity successfully",
Data: dto.ToUniformityDetailDTO(*result, calculation, document, standardDTO), Data: dto.ToUniformityDetailDTO(*result, calculation, document, documentURL, standardDTO),
}) })
} }
@@ -154,7 +159,7 @@ func (u *UniformityController) CreateOne(c *fiber.Ctx) error {
Code: fiber.StatusCreated, Code: fiber.StatusCreated,
Status: "success", Status: "success",
Message: "Create uniformity successfully", Message: "Create uniformity successfully",
Data: dto.ToUniformityDetailDTO(*result, calculation, document, standardDTO), Data: dto.ToUniformityDetailDTO(*result, calculation, document, "", standardDTO),
}) })
} }
@@ -237,7 +242,7 @@ func (u *UniformityController) UpdateOne(c *fiber.Ctx) error {
Code: fiber.StatusOK, Code: fiber.StatusOK,
Status: "success", Status: "success",
Message: "Update uniformity successfully", Message: "Update uniformity successfully",
Data: dto.ToUniformityDetailDTO(*result, calculation, document, standardDTO), Data: dto.ToUniformityDetailDTO(*result, calculation, document, "", standardDTO),
}) })
} }
@@ -45,6 +45,7 @@ type UniformityInfoDTO struct {
ProjectFlock string `json:"project_flock"` ProjectFlock string `json:"project_flock"`
Kandang string `json:"kandang"` Kandang string `json:"kandang"`
FileName string `json:"file_name"` FileName string `json:"file_name"`
FileURL string `json:"file_url"`
} }
type UniformityDetailDTO struct { type UniformityDetailDTO struct {
@@ -73,7 +74,6 @@ type UniformityListDTO struct {
MeanDown float64 `json:"mean_down"` MeanDown float64 `json:"mean_down"`
StandardMeanWeight *float64 `json:"standard_mean_weight"` StandardMeanWeight *float64 `json:"standard_mean_weight"`
StandardUniformity *float64 `json:"standard_uniformity"` StandardUniformity *float64 `json:"standard_uniformity"`
CreatedAt time.Time `json:"created_at"`
CreatedBy uint `json:"created_by"` CreatedBy uint `json:"created_by"`
LatestApproval *approvalDTO.ApprovalRelationDTO `json:"latest_approval"` LatestApproval *approvalDTO.ApprovalRelationDTO `json:"latest_approval"`
} }
@@ -97,6 +97,7 @@ func ToUniformityDetailDTO(
entityData entity.ProjectFlockKandangUniformity, entityData entity.ProjectFlockKandangUniformity,
calc service.UniformityCalculation, calc service.UniformityCalculation,
document *entity.Document, document *entity.Document,
documentURL string,
standard *UniformityStandardDTO, standard *UniformityStandardDTO,
) UniformityDetailDTO { ) UniformityDetailDTO {
info := UniformityInfoDTO{ info := UniformityInfoDTO{
@@ -105,10 +106,14 @@ func ToUniformityDetailDTO(
ProjectFlock: resolveProjectFlockName(entityData.ProjectFlockKandang), ProjectFlock: resolveProjectFlockName(entityData.ProjectFlockKandang),
Kandang: resolveKandangName(entityData.ProjectFlockKandang), Kandang: resolveKandangName(entityData.ProjectFlockKandang),
FileName: "", FileName: "",
FileURL: "",
} }
if document != nil { if document != nil {
info.FileName = document.Name info.FileName = document.Name
} }
if documentURL != "" {
info.FileURL = documentURL
}
return UniformityDetailDTO{ return UniformityDetailDTO{
Id: entityData.Id, Id: entityData.Id,
@@ -148,7 +153,6 @@ func ToUniformityListDTOs(items []entity.ProjectFlockKandangUniformity) []Unifor
UniformQty: item.UniformQty, UniformQty: item.UniformQty,
MeanUp: item.MeanUp, MeanUp: item.MeanUp,
MeanDown: item.MeanDown, MeanDown: item.MeanDown,
CreatedAt: item.CreatedAt,
CreatedBy: item.CreatedBy, CreatedBy: item.CreatedBy,
LatestApproval: latestApproval, LatestApproval: latestApproval,
} }
@@ -1,6 +1,8 @@
package repository package repository
import ( import (
"context"
"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"
"gorm.io/gorm" "gorm.io/gorm"
@@ -8,6 +10,7 @@ import (
type UniformityRepository interface { type UniformityRepository interface {
repository.BaseRepository[entity.ProjectFlockKandangUniformity] repository.BaseRepository[entity.ProjectFlockKandangUniformity]
DeleteByProjectFlockKandangIDs(ctx context.Context, projectFlockKandangIDs []uint) error
} }
type UniformityRepositoryImpl struct { type UniformityRepositoryImpl struct {
@@ -19,3 +22,13 @@ func NewUniformityRepository(db *gorm.DB) UniformityRepository {
BaseRepositoryImpl: repository.NewBaseRepository[entity.ProjectFlockKandangUniformity](db), BaseRepositoryImpl: repository.NewBaseRepository[entity.ProjectFlockKandangUniformity](db),
} }
} }
func (r *UniformityRepositoryImpl) DeleteByProjectFlockKandangIDs(ctx context.Context, projectFlockKandangIDs []uint) error {
if len(projectFlockKandangIDs) == 0 {
return nil
}
return r.DB().WithContext(ctx).
Unscoped().
Where("project_flock_kandang_id IN ?", projectFlockKandangIDs).
Delete(&entity.ProjectFlockKandangUniformity{}).Error
}
@@ -39,7 +39,8 @@ type UniformityService interface {
Approval(ctx *fiber.Ctx, req *validation.Approve) ([]entity.ProjectFlockKandangUniformity, error) Approval(ctx *fiber.Ctx, req *validation.Approve) ([]entity.ProjectFlockKandangUniformity, error)
ParseBodyWeightExcel(ctx *fiber.Ctx, file *multipart.FileHeader) ([]BodyWeightExcelRow, error) ParseBodyWeightExcel(ctx *fiber.Ctx, file *multipart.FileHeader) ([]BodyWeightExcelRow, error)
ComputeUniformity(rows []BodyWeightExcelRow) (UniformityCalculation, error) ComputeUniformity(rows []BodyWeightExcelRow) (UniformityCalculation, error)
CalculateUniformityFromDocument(ctx *fiber.Ctx, uniformityID uint) (UniformityCalculation, *entity.Document, error) GetDocumentInfo(ctx *fiber.Ctx, uniformityID uint) (*entity.Document, string, error)
CalculateUniformityFromDocument(ctx *fiber.Ctx, uniformityID uint) (UniformityCalculation, *entity.Document, string, error)
} }
type uniformityService struct { type uniformityService struct {
@@ -98,7 +99,7 @@ func (s uniformityService) GetAll(c *fiber.Ctx, params *validation.Query) ([]ent
if params.Week != 0 { if params.Week != 0 {
db = db.Where("week = ?", params.Week) db = db.Where("week = ?", params.Week)
} }
return db.Order("uniform_date DESC").Order("created_at DESC") return db.Order("uniform_date DESC").Order("id DESC")
}) })
if err != nil { if err != nil {
@@ -592,50 +593,72 @@ func (s uniformityService) ComputeUniformity(rows []BodyWeightExcelRow) (Uniform
return computeUniformity(rows) return computeUniformity(rows)
} }
func (s uniformityService) CalculateUniformityFromDocument(c *fiber.Ctx, uniformityID uint) (UniformityCalculation, *entity.Document, error) { func (s uniformityService) GetDocumentInfo(c *fiber.Ctx, uniformityID uint) (*entity.Document, string, error) {
if s.DocumentSvc == nil { return s.fetchUniformityDocument(c.Context(), uniformityID, true)
return UniformityCalculation{}, nil, fiber.NewError(fiber.StatusInternalServerError, "Document service not available") }
}
documents, err := s.DocumentSvc.ListByTarget(c.Context(), "UNIFORMITY", uint64(uniformityID)) func (s uniformityService) CalculateUniformityFromDocument(c *fiber.Ctx, uniformityID uint) (UniformityCalculation, *entity.Document, string, error) {
document, url, err := s.fetchUniformityDocument(c.Context(), uniformityID, false)
if err != nil { if err != nil {
return UniformityCalculation{}, nil, err return UniformityCalculation{}, nil, "", err
} }
if len(documents) == 0 { if document == nil || url == "" {
return UniformityCalculation{}, nil, fiber.NewError(fiber.StatusNotFound, "Uniformity document not found") return UniformityCalculation{}, nil, "", fiber.NewError(fiber.StatusNotFound, "Uniformity document not found")
}
document := documents[0]
url := s.DocumentSvc.PublicURL(document)
if url == "" {
return UniformityCalculation{}, nil, fiber.NewError(fiber.StatusBadRequest, "Uniformity document URL not available")
} }
req, err := http.NewRequestWithContext(c.Context(), http.MethodGet, url, nil) req, err := http.NewRequestWithContext(c.Context(), http.MethodGet, url, nil)
if err != nil { if err != nil {
return UniformityCalculation{}, nil, err return UniformityCalculation{}, nil, "", err
} }
resp, err := http.DefaultClient.Do(req) resp, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
return UniformityCalculation{}, nil, err return UniformityCalculation{}, nil, "", err
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 { if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return UniformityCalculation{}, nil, fiber.NewError(fiber.StatusBadRequest, "Failed to download uniformity document") return UniformityCalculation{}, nil, "", fiber.NewError(fiber.StatusBadRequest, "Failed to download uniformity document")
} }
rows, err := parseBodyWeightExcelReader(resp.Body) rows, err := parseBodyWeightExcelReader(resp.Body)
if err != nil { if err != nil {
return UniformityCalculation{}, nil, err return UniformityCalculation{}, nil, "", err
} }
calculation, err := computeUniformity(rows) calculation, err := computeUniformity(rows)
if err != nil { if err != nil {
return UniformityCalculation{}, nil, err return UniformityCalculation{}, nil, "", err
} }
return calculation, &document, nil return calculation, document, url, nil
}
func (s uniformityService) fetchUniformityDocument(ctx context.Context, uniformityID uint, allowMissing bool) (*entity.Document, string, error) {
if s.DocumentSvc == nil {
return nil, "", fiber.NewError(fiber.StatusInternalServerError, "Document service not available")
}
documents, err := s.DocumentSvc.ListByTarget(ctx, "UNIFORMITY", uint64(uniformityID))
if err != nil {
return nil, "", err
}
if len(documents) == 0 {
if allowMissing {
return nil, "", nil
}
return nil, "", fiber.NewError(fiber.StatusNotFound, "Uniformity document not found")
}
document := documents[0]
url, err := s.DocumentSvc.PresignURL(ctx, document, 15*time.Minute)
if err != nil {
return nil, "", err
}
if url == "" {
return nil, "", fiber.NewError(fiber.StatusBadRequest, "Uniformity document URL not available")
}
return &document, url, nil
} }
func (s *uniformityService) createUniformityApproval( func (s *uniformityService) createUniformityApproval(
@@ -1,6 +1,7 @@
package controller package controller
import ( import (
"encoding/json"
"fmt" "fmt"
"math" "math"
"strconv" "strconv"
@@ -24,13 +25,13 @@ func NewPurchaseController(s service.PurchaseService) *PurchaseController {
func (ctrl *PurchaseController) GetAll(c *fiber.Ctx) error { func (ctrl *PurchaseController) GetAll(c *fiber.Ctx) error {
query := &validation.Query{ query := &validation.Query{
Page: c.QueryInt("page", 1), Page: c.QueryInt("page", 1),
Limit: c.QueryInt("limit", 10), Limit: c.QueryInt("limit", 10),
CreatedFrom: strings.TrimSpace(c.Query("created_from")), CreatedFrom: strings.TrimSpace(c.Query("created_from")),
CreatedTo: strings.TrimSpace(c.Query("created_to")), CreatedTo: strings.TrimSpace(c.Query("created_to")),
SupplierID: uint(c.QueryInt("supplier_id", 0)), SupplierID: uint(c.QueryInt("supplier_id", 0)),
AreaID: uint(c.QueryInt("area_id", 0)), AreaID: uint(c.QueryInt("area_id", 0)),
LocationID: uint(c.QueryInt("location_id", 0)), LocationID: uint(c.QueryInt("location_id", 0)),
ProductCategoryID: uint(c.QueryInt("product_category_id", 0)), ProductCategoryID: uint(c.QueryInt("product_category_id", 0)),
} }
@@ -86,7 +87,6 @@ func (ctrl *PurchaseController) CreateOne(c *fiber.Ctx) error {
if err := c.BodyParser(req); err != nil { if err := c.BodyParser(req); err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body") return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
} }
result, err := ctrl.service.CreateOne(c, req) result, err := ctrl.service.CreateOne(c, req)
if err != nil { if err != nil {
return err return err
@@ -161,10 +161,29 @@ func (ctrl *PurchaseController) ReceiveProducts(c *fiber.Ctx) error {
} }
req := new(validation.ReceivePurchaseRequest) req := new(validation.ReceivePurchaseRequest)
if err := c.BodyParser(req); err != nil { form, err := c.MultipartForm()
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body") if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid multipart form")
}
req.Action = c.FormValue("action")
if notes := strings.TrimSpace(c.FormValue("notes")); notes != "" {
req.Notes = &notes
} }
itemsJSON := c.FormValue("items")
if strings.TrimSpace(itemsJSON) != "" {
if err := json.Unmarshal([]byte(itemsJSON), &req.Items); err != nil {
var singleItem validation.ReceivePurchaseItemRequest
if err := json.Unmarshal([]byte(itemsJSON), &singleItem); err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid items JSON")
}
req.Items = []validation.ReceivePurchaseItemRequest{singleItem}
}
}
req.TravelDocuments = form.File["travel_documents"]
if len(req.TravelDocuments) == 0 {
req.TravelDocuments = form.File["documents"]
}
result, err := ctrl.service.ReceiveProducts(c, uint(id), req) result, err := ctrl.service.ReceiveProducts(c, uint(id), req)
if err != nil { if err != nil {
return err return err
+1
View File
@@ -98,6 +98,7 @@ func (PurchaseModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
approvalService, approvalService,
expenseBridge, expenseBridge,
fifoService, fifoService,
documentSvc,
) )
userRepo := rUser.NewUserRepository(db) userRepo := rUser.NewUserRepository(db)
@@ -310,9 +310,6 @@ func (b *expenseBridge) OnItemsReceived(c *fiber.Ctx, purchaseID uint, updates [
return err return err
} }
if cnt == 1 { if cnt == 1 {
if item.Warehouse == nil || item.Warehouse.KandangId == nil || *item.Warehouse.KandangId == 0 {
return fiber.NewError(fiber.StatusBadRequest, "Warehouse not connect to kandangs")
}
newNonstockID, err := b.findExpeditionNonstockID(ctx, supplierID) newNonstockID, err := b.findExpeditionNonstockID(ctx, supplierID)
if err != nil { if err != nil {
return err return err
@@ -332,7 +329,9 @@ func (b *expenseBridge) OnItemsReceived(c *fiber.Ctx, purchaseID uint, updates [
"price": pricePerItem, "price": pricePerItem,
"notes": note, "notes": note,
"nonstock_id": newNonstockID, "nonstock_id": newNonstockID,
"kandang_id": uint64(*item.Warehouse.KandangId), }
if item.Warehouse != nil && item.Warehouse.KandangId != nil && *item.Warehouse.KandangId != 0 {
updateBody["kandang_id"] = uint64(*item.Warehouse.KandangId)
} }
if err := b.db.WithContext(ctx). if err := b.db.WithContext(ctx).
Model(&entity.ExpenseNonstock{}). Model(&entity.ExpenseNonstock{}).
@@ -395,9 +394,13 @@ func (b *expenseBridge) OnItemsReceived(c *fiber.Ctx, purchaseID uint, updates [
} }
if kandangID != nil { if kandangID != nil {
updateBody["kandang_id"] = uint64(*kandangID) updateBody["kandang_id"] = uint64(*kandangID)
} else {
updateBody["kandang_id"] = nil
} }
if projectFK != nil { if projectFK != nil {
updateBody["project_flock_kandang_id"] = uint64(*projectFK) updateBody["project_flock_kandang_id"] = uint64(*projectFK)
} else {
updateBody["project_flock_kandang_id"] = nil
} }
if err := b.db.WithContext(ctx). if err := b.db.WithContext(ctx).
@@ -550,18 +553,27 @@ func (b *expenseBridge) createExpenseViaService(
} }
kandangID := items[0].kandangID kandangID := items[0].kandangID
if kandangID == nil || *kandangID == 0 { var locationID uint64
return nil, fiber.NewError(fiber.StatusBadRequest, "Warehouse not connect to kandangs") var expenseKandangID *uint64
} if kandangID != nil && *kandangID != 0 {
kandang, err := b.kandangRepo.GetByID(ctx, *kandangID, func(db *gorm.DB) *gorm.DB {
kandang, err := b.kandangRepo.GetByID(ctx, *kandangID, func(db *gorm.DB) *gorm.DB { return db.Select("id, location_id")
return db.Select("id, location_id") })
}) if err != nil {
if err != nil { return nil, fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Kandang not found: %d", *kandangID))
return nil, fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Kandang not found: %d", *kandangID)) }
} if kandang == nil {
if kandang == nil { return nil, fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Kandang not found: %d", *kandangID))
return nil, fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Kandang not found: %d", *kandangID)) }
locationID = uint64(kandang.LocationId)
id := uint64(*kandangID)
expenseKandangID = &id
} else {
warehouse := items[0].item.Warehouse
if warehouse == nil || warehouse.LocationId == nil || *warehouse.LocationId == 0 {
return nil, fiber.NewError(fiber.StatusBadRequest, "Warehouse location is required for expense")
}
locationID = uint64(*warehouse.LocationId)
} }
costItems := make([]expenseValidation.CostItem, 0, len(items)) costItems := make([]expenseValidation.CostItem, 0, len(items))
@@ -584,9 +596,9 @@ func (b *expenseBridge) createExpenseViaService(
TransactionDate: utils.FormatDate(expenseDate), TransactionDate: utils.FormatDate(expenseDate),
Category: "BOP", Category: "BOP",
SupplierID: uint64(supplierID), SupplierID: uint64(supplierID),
LocationID: uint64(kandang.LocationId), LocationID: locationID,
ExpenseNonstocks: []expenseValidation.ExpenseNonstock{{ ExpenseNonstocks: []expenseValidation.ExpenseNonstock{{
KandangID: func() *uint64 { id := uint64(*kandangID); return &id }(), KandangID: expenseKandangID,
CostItems: costItems, CostItems: costItems,
}}, }},
} }
@@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"math" "math"
"mime/multipart"
"strings" "strings"
"time" "time"
@@ -57,6 +58,7 @@ type purchaseService struct {
ApprovalSvc commonSvc.ApprovalService ApprovalSvc commonSvc.ApprovalService
ExpenseBridge PurchaseExpenseBridge ExpenseBridge PurchaseExpenseBridge
FifoSvc commonSvc.FifoService FifoSvc commonSvc.FifoService
DocumentSvc commonSvc.DocumentService
approvalWorkflow approvalutils.ApprovalWorkflowKey approvalWorkflow approvalutils.ApprovalWorkflowKey
} }
@@ -76,6 +78,7 @@ func NewPurchaseService(
approvalSvc commonSvc.ApprovalService, approvalSvc commonSvc.ApprovalService,
expenseBridge PurchaseExpenseBridge, expenseBridge PurchaseExpenseBridge,
fifoSvc commonSvc.FifoService, fifoSvc commonSvc.FifoService,
documentSvc commonSvc.DocumentService,
) PurchaseService { ) PurchaseService {
return &purchaseService{ return &purchaseService{
Log: utils.Log, Log: utils.Log,
@@ -89,6 +92,7 @@ func NewPurchaseService(
ApprovalSvc: approvalSvc, ApprovalSvc: approvalSvc,
ExpenseBridge: expenseBridge, ExpenseBridge: expenseBridge,
FifoSvc: fifoSvc, FifoSvc: fifoSvc,
DocumentSvc: documentSvc,
approvalWorkflow: utils.ApprovalWorkflowPurchase, approvalWorkflow: utils.ApprovalWorkflowPurchase,
} }
} }
@@ -246,22 +250,25 @@ func (s *purchaseService) CreateOne(c *fiber.Ctx, req *validation.CreatePurchase
s.Log.Errorf("Failed to get warehouse %d: %+v", id, err) s.Log.Errorf("Failed to get warehouse %d: %+v", id, err)
return nil, nil, utils.Internal("Failed to get warehouse") return nil, nil, utils.Internal("Failed to get warehouse")
} }
if warehouse.KandangId == nil || *warehouse.KandangId == 0 {
return nil, nil, utils.BadRequest(fmt.Sprintf("%s is not linked to a kandang", warehouse.Name))
}
var pfkID *uint var pfkID *uint
if s.ProjectFlockKandangRepo != nil { isKandang := strings.EqualFold(strings.TrimSpace(warehouse.Type), "KANDANG")
if pfk, err := s.ProjectFlockKandangRepo.GetActiveByKandangID(c.Context(), uint(*warehouse.KandangId)); err == nil && pfk != nil { if isKandang {
if pfk.ClosedAt != nil { if warehouse.KandangId == nil || *warehouse.KandangId == 0 {
return nil, nil, utils.BadRequest("Project sudah closing") return nil, nil, utils.BadRequest(fmt.Sprintf("%s is not linked to a kandang", warehouse.Name))
}
if s.ProjectFlockKandangRepo != nil {
if pfk, err := s.ProjectFlockKandangRepo.GetActiveByKandangID(c.Context(), uint(*warehouse.KandangId)); err == nil && pfk != nil {
if pfk.ClosedAt != nil {
return nil, nil, utils.BadRequest("Project sudah closing")
}
idCopy := uint(pfk.Id)
pfkID = &idCopy
} else if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil, utils.BadRequest(fmt.Sprintf("%s has no active project flock", warehouse.Name))
} else if err != nil {
s.Log.Errorf("Failed to validate project flock for warehouse %d: %+v", id, err)
return nil, nil, utils.Internal("Failed to validate project flock")
} }
idCopy := uint(pfk.Id)
pfkID = &idCopy
} else if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil, utils.BadRequest(fmt.Sprintf("%s has no active project flock", warehouse.Name))
} else if err != nil {
s.Log.Errorf("Failed to validate project flock for warehouse %d: %+v", id, err)
return nil, nil, utils.Internal("Failed to validate project flock")
} }
} }
@@ -612,9 +619,7 @@ func (s *purchaseService) ReceiveProducts(c *fiber.Ctx, id uint, req *validation
if err := s.Validate.Struct(req); err != nil { if err := s.Validate.Struct(req); err != nil {
return nil, err return nil, err
} }
ctx := c.Context() ctx := c.Context()
action, err := parseApprovalActionInput(req.Action) action, err := parseApprovalActionInput(req.Action)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -661,6 +666,30 @@ func (s *purchaseService) ReceiveProducts(c *fiber.Ctx, id uint, req *validation
return updated, nil return updated, nil
} }
if action == entity.ApprovalActionApproved && len(req.TravelDocuments) > 0 {
if len(req.TravelDocuments) > len(req.Items) {
return nil, utils.BadRequest("Travel documents exceed total receiving items")
}
for idx, file := range req.TravelDocuments {
if file == nil {
continue
}
if idx >= len(req.Items) {
break
}
itemID := req.Items[idx].PurchaseItemID
if itemID == 0 {
return nil, utils.BadRequest("Purchase item id is required for travel document upload")
}
uploadedURL, err := s.uploadTravelDocument(ctx, actorID, itemID, file)
if err != nil {
s.Log.Errorf("Failed to upload travel document for item %d: %+v", itemID, err)
return nil, utils.Internal("Failed to upload travel document")
}
req.Items[idx].TravelDocumentPath = &uploadedURL
}
}
itemMap := make(map[uint]*entity.PurchaseItem, len(purchase.Items)) itemMap := make(map[uint]*entity.PurchaseItem, len(purchase.Items))
for i := range purchase.Items { for i := range purchase.Items {
itemMap[purchase.Items[i].Id] = &purchase.Items[i] itemMap[purchase.Items[i].Id] = &purchase.Items[i]
@@ -804,32 +833,20 @@ func (s *purchaseService) ReceiveProducts(c *fiber.Ctx, id uint, req *validation
for _, prep := range prepared { for _, prep := range prepared {
item := prep.item item := prep.item
var oldPWID *uint
if item.ProductWarehouseId != nil {
idCopy := uint(*item.ProductWarehouseId)
oldPWID = &idCopy
}
var newPWID *uint var newPWID *uint
clearPW := false
// Always ensure PW when qty > 0 so stockable has target. // Always ensure PW after receiving so linkage stays stable.
if prep.receivedQty > 0 { pwID, err := pwRepoTx.EnsureProductWarehouse(
pwID, err := pwRepoTx.EnsureProductWarehouse( c.Context(),
c.Context(), uint(item.ProductId),
uint(item.ProductId), prep.warehouseID,
prep.warehouseID, item.ProjectFlockKandangId,
item.ProjectFlockKandangId, purchase.CreatedBy,
purchase.CreatedBy, )
) if err != nil {
if err != nil { return err
return err
}
newPWID = &pwID
} else if oldPWID != nil {
newPWID = oldPWID
clearPW = true
} }
newPWID = &pwID
deltaQty := prep.receivedQty - item.TotalQty deltaQty := prep.receivedQty - item.TotalQty
switch { switch {
@@ -854,7 +871,7 @@ func (s *purchaseService) ReceiveProducts(c *fiber.Ctx, id uint, req *validation
VehicleNumber: prep.payload.VehicleNumber, VehicleNumber: prep.payload.VehicleNumber,
ReceivedQty: &qtyCopy, ReceivedQty: &qtyCopy,
ProductWarehouseID: newPWID, ProductWarehouseID: newPWID,
ClearProductWarehouse: clearPW, ClearProductWarehouse: false,
} }
if prep.overrideWarehouse || uint(item.WarehouseId) != prep.warehouseID { if prep.overrideWarehouse || uint(item.WarehouseId) != prep.warehouseID {
@@ -969,6 +986,54 @@ func (s *purchaseService) ReceiveProducts(c *fiber.Ctx, id uint, req *validation
return updated, nil return updated, nil
} }
func (s *purchaseService) uploadTravelDocument(
ctx context.Context,
actorID uint,
itemID uint,
file *multipart.FileHeader,
) (string, error) {
if file == nil {
return "", errors.New("travel document file is required")
}
if s.DocumentSvc == nil {
return "", errors.New("document service not available")
}
documents, err := s.DocumentSvc.ListByTarget(ctx, string(utils.DocumentableTypePurchaseItem), uint64(itemID))
if err != nil {
return "", err
}
if len(documents) > 0 {
var ids []uint
for _, doc := range documents {
if doc.Type == string(utils.DocumentTypePurchaseTravel) {
ids = append(ids, doc.Id)
}
}
if err := s.DocumentSvc.DeleteDocuments(ctx, ids, true); err != nil {
return "", err
}
}
documentFiles := []commonSvc.DocumentFile{{
File: file,
Type: string(utils.DocumentTypePurchaseTravel),
}}
results, err := s.DocumentSvc.UploadDocuments(ctx, commonSvc.DocumentUploadRequest{
DocumentableType: string(utils.DocumentableTypePurchaseItem),
DocumentableID: uint64(itemID),
CreatedBy: &actorID,
Files: documentFiles,
})
if err != nil {
return "", err
}
if len(results) == 0 {
return "", errors.New("upload result is empty")
}
return results[0].Document.Path, nil
}
func (s *purchaseService) DeleteItems(c *fiber.Ctx, id uint, req *validation.DeletePurchaseItemsRequest) (*entity.Purchase, error) { func (s *purchaseService) DeleteItems(c *fiber.Ctx, id uint, req *validation.DeletePurchaseItemsRequest) (*entity.Purchase, error) {
if err := s.Validate.Struct(req); err != nil { if err := s.Validate.Struct(req); err != nil {
return nil, err return nil, err
@@ -1048,6 +1113,10 @@ func (s *purchaseService) DeleteItems(c *fiber.Ctx, id uint, req *validation.Del
return nil, utils.Internal("Failed to delete purchase items") return nil, utils.Internal("Failed to delete purchase items")
} }
if err := s.deletePurchaseItemDocuments(ctx, itemsToDelete); err != nil {
return nil, utils.Internal("Failed to delete purchase documents")
}
if len(itemsToDelete) > 0 { if len(itemsToDelete) > 0 {
if err := s.notifyExpenseItemsDeleted(ctx, purchase.Id, itemsToDelete); err != nil { if err := s.notifyExpenseItemsDeleted(ctx, purchase.Id, itemsToDelete); err != nil {
s.Log.Errorf("Failed to sync expense deletion for purchase %d: %+v", purchase.Id, err) s.Log.Errorf("Failed to sync expense deletion for purchase %d: %+v", purchase.Id, err)
@@ -1107,6 +1176,10 @@ func (s *purchaseService) DeletePurchase(c *fiber.Ctx, id uint) error {
return utils.Internal("Failed to delete purchase") return utils.Internal("Failed to delete purchase")
} }
if err := s.deletePurchaseItemDocuments(ctx, itemsToDelete); err != nil {
return utils.Internal("Failed to delete purchase documents")
}
if len(itemsToDelete) > 0 { if len(itemsToDelete) > 0 {
if err := s.notifyExpenseItemsDeleted(ctx, uint(id), itemsToDelete); err != nil { if err := s.notifyExpenseItemsDeleted(ctx, uint(id), itemsToDelete); err != nil {
s.Log.Errorf("Failed to sync expense deletion for purchase %d: %+v", id, err) s.Log.Errorf("Failed to sync expense deletion for purchase %d: %+v", id, err)
@@ -1190,6 +1263,21 @@ func (s *purchaseService) notifyExpenseItemsDeleted(ctx context.Context, purchas
} }
func (s *purchaseService) deletePurchaseItemDocuments(ctx context.Context, items []entity.PurchaseItem) error {
if s.DocumentSvc == nil || len(items) == 0 {
return nil
}
for _, item := range items {
if item.Id == 0 {
continue
}
if err := s.DocumentSvc.DeleteByTarget(ctx, string(utils.DocumentableTypePurchaseItem), uint64(item.Id), true); err != nil {
return err
}
}
return nil
}
func (s *purchaseService) buildStaffAdjustmentPayload( func (s *purchaseService) buildStaffAdjustmentPayload(
ctx context.Context, ctx context.Context,
purchase *entity.Purchase, purchase *entity.Purchase,
@@ -1427,10 +1515,56 @@ func (s *purchaseService) loadPurchase(
if err := s.attachLatestApproval(ctx, purchase); err != nil { if err := s.attachLatestApproval(ctx, purchase); err != nil {
s.Log.Warnf("Unable to attach latest approval for purchase %d: %+v", id, err) s.Log.Warnf("Unable to attach latest approval for purchase %d: %+v", id, err)
} }
s.applyTravelDocumentURLs(ctx, purchase)
return purchase, nil return purchase, nil
} }
func (s *purchaseService) applyTravelDocumentURLs(ctx context.Context, purchase *entity.Purchase) {
if purchase == nil || s.DocumentSvc == nil {
return
}
for i := range purchase.Items {
item := &purchase.Items[i]
documents, err := s.DocumentSvc.ListByTarget(ctx, string(utils.DocumentableTypePurchaseItem), uint64(item.Id))
if err != nil {
s.Log.Warnf("Unable to load travel documents for purchase item %d: %+v", item.Id, err)
} else {
var targetDoc *entity.Document
for j := len(documents) - 1; j >= 0; j-- {
if documents[j].Type == string(utils.DocumentTypePurchaseTravel) {
targetDoc = &documents[j]
break
}
}
if targetDoc != nil {
url, err := s.DocumentSvc.PresignURL(ctx, *targetDoc, 15*time.Minute)
if err != nil {
s.Log.Warnf("Unable to presign travel document for purchase item %d: %+v", item.Id, err)
} else if url != "" {
item.TravelNumberDocs = &url
continue
}
}
}
path := item.TravelNumberDocs
if path == nil || strings.TrimSpace(*path) == "" {
continue
}
url, err := commonSvc.ResolveDocumentURL(ctx, s.DocumentSvc, *path, 15*time.Minute)
if err != nil {
s.Log.Warnf("Unable to presign travel document for purchase item %d: %+v", item.Id, err)
continue
}
if url == "" {
continue
}
item.TravelNumberDocs = &url
}
}
func collectPFKIDsFromPurchase(p *entity.Purchase) []uint { func collectPFKIDsFromPurchase(p *entity.Purchase) []uint {
seen := make(map[uint]struct{}) seen := make(map[uint]struct{})
ids := make([]uint, 0) ids := make([]uint, 0)
@@ -1462,5 +1596,5 @@ func (s *purchaseService) ensureProjectFlockNotClosedForPurchase(
return utils.Internal("DB not available for project flock validation") return utils.Internal("DB not available for project flock validation")
} }
return commonSvc.EnsureProjectFlockNotClosedForProductWarehouses(ctx, db, pfkIDs) return commonSvc.EnsureProjectFlockNotClosedByProjectFlockKandangID(ctx, db, pfkIDs)
} }
@@ -1,5 +1,7 @@
package validation package validation
import "mime/multipart"
type PurchaseItemPayload struct { type PurchaseItemPayload struct {
WarehouseID uint `json:"warehouse_id" validate:"required,gt=0"` WarehouseID uint `json:"warehouse_id" validate:"required,gt=0"`
ProductID uint `json:"product_id" validate:"required,gt=0"` ProductID uint `json:"product_id" validate:"required,gt=0"`
@@ -26,7 +28,7 @@ type StaffPurchaseApprovalItem struct {
type ApproveStaffPurchaseRequest struct { type ApproveStaffPurchaseRequest struct {
Action string `json:"action" validate:"required,oneof=APPROVED REJECTED"` Action string `json:"action" validate:"required,oneof=APPROVED REJECTED"`
Items []StaffPurchaseApprovalItem `json:"items,omitempty" validate:"omitempty,min=1,dive"` Items []StaffPurchaseApprovalItem `json:"items" validate:"omitempty,min=1,dive"`
Notes *string `json:"notes,omitempty" validate:"omitempty,max=500"` Notes *string `json:"notes,omitempty" validate:"omitempty,max=500"`
} }
@@ -36,21 +38,22 @@ type ApproveManagerPurchaseRequest struct {
} }
type ReceivePurchaseItemRequest struct { type ReceivePurchaseItemRequest struct {
PurchaseItemID uint `json:"purchase_item_id" validate:"required,gt=0"` PurchaseItemID uint `form:"purchase_item_id" json:"purchase_item_id" validate:"required,gt=0"`
WarehouseID *uint `json:"warehouse_id" validate:"omitempty,gt=0"` WarehouseID *uint `form:"warehouse_id" json:"warehouse_id" validate:"omitempty,gt=0"`
ReceivedDate string `json:"received_date" validate:"required,datetime=2006-01-02"` ReceivedDate string `form:"received_date" json:"received_date" validate:"required,datetime=2006-01-02"`
ExpeditionVendorID *uint `json:"expedition_vendor_id,omitempty" validate:"omitempty,gt=0"` ExpeditionVendorID *uint `form:"expedition_vendor_id" json:"expedition_vendor_id,omitempty" validate:"omitempty,gt=0"`
TransportPerItem *float64 `json:"transport_per_item,omitempty" validate:"omitempty,gte=0"` TransportPerItem *float64 `form:"transport_per_item" json:"transport_per_item,omitempty" validate:"omitempty,gte=0"`
TravelNumber *string `json:"travel_number" validate:"omitempty,max=100"` TravelNumber *string `form:"travel_number" json:"travel_number" validate:"omitempty,max=100"`
TravelDocumentPath *string `json:"travel_document_path" validate:"omitempty,max=255"` TravelDocumentPath *string `form:"travel_document_path" json:"travel_document_path" validate:"omitempty,max=1024"`
VehicleNumber *string `json:"vehicle_number" validate:"omitempty,max=100"` VehicleNumber *string `form:"vehicle_number" json:"vehicle_number" validate:"omitempty,max=100"`
ReceivedQty *float64 `json:"received_qty" validate:"omitempty,gte=0"` ReceivedQty *float64 `form:"received_qty" json:"received_qty" validate:"omitempty,gte=0"`
} }
type ReceivePurchaseRequest struct { type ReceivePurchaseRequest struct {
Action string `json:"action" validate:"required,oneof=APPROVED REJECTED"` Action string `form:"action" json:"action" validate:"required,oneof=APPROVED REJECTED"`
Items []ReceivePurchaseItemRequest `json:"items,omitempty" validate:"omitempty,min=1,dive"` Items []ReceivePurchaseItemRequest `form:"items" json:"items" validate:"min=1,dive"`
Notes *string `json:"notes,omitempty" validate:"omitempty,max=500"` TravelDocuments []*multipart.FileHeader `form:"travel_documents" json:"-" validate:"omitempty,dive"`
Notes *string `form:"notes" json:"notes,omitempty" validate:"omitempty,max=500"`
} }
type DeletePurchaseItemsRequest struct { type DeletePurchaseItemsRequest struct {
+1 -1
View File
@@ -18,6 +18,6 @@ func RepportRoutes(v1 fiber.Router, u user.UserService, s repport.RepportService
route.Get("/expense", m.RequirePermissions(m.P_ReportExpenseGetAll), ctrl.GetExpense) route.Get("/expense", m.RequirePermissions(m.P_ReportExpenseGetAll), ctrl.GetExpense)
route.Get("/marketing", m.RequirePermissions(m.P_ReportDeliveryGetAll), ctrl.GetMarketing) route.Get("/marketing", m.RequirePermissions(m.P_ReportDeliveryGetAll), ctrl.GetMarketing)
route.Get("/purchase-supplier", m.RequirePermissions(m.P_ReportPurchaseSupplierGetAll), ctrl.GetPurchaseSupplier) route.Get("/purchase-supplier", m.RequirePermissions(m.P_ReportPurchaseSupplierGetAll), ctrl.GetPurchaseSupplier)
route.Get("/hpp-per-kandang", ctrl.GetHppPerKandang) route.Get("/hpp-per-kandang", m.RequirePermissions(m.P_ReportHppPerKandangGetAll),ctrl.GetHppPerKandang)
} }
@@ -171,6 +171,9 @@ func (h *Controller) Refresh(c *fiber.Ctx) error {
if resp.StatusCode >= 400 { if resp.StatusCode >= 400 {
utils.Log.Warnf("token refresh response status %d", resp.StatusCode) utils.Log.Warnf("token refresh response status %d", resp.StatusCode)
if resp.StatusCode == fiber.StatusTooManyRequests {
return fiber.NewError(fiber.StatusTooManyRequests, "Too many attempts, please slow down")
}
return fiber.NewError(fiber.StatusUnauthorized, "unauthenticated") return fiber.NewError(fiber.StatusUnauthorized, "unauthenticated")
} }

Some files were not shown because too many files have changed in this diff Show More