mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-23 14:55:42 +00:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| febc228115 | |||
| 54e4878406 | |||
| 77ac46a029 | |||
| 8a006f377e | |||
| 18db58a87b |
+135
-28
@@ -1,35 +1,142 @@
|
|||||||
|
stages:
|
||||||
|
- build
|
||||||
|
- gitops
|
||||||
|
|
||||||
|
variables:
|
||||||
|
AWS_REGION: ap-southeast-3
|
||||||
|
ECR_REGISTRY: 886436954922.dkr.ecr.ap-southeast-3.amazonaws.com
|
||||||
|
ECR_REPO_NAME: mbugroup/lti-api
|
||||||
|
ECR_REPOSITORY: ${ECR_REGISTRY}/${ECR_REPO_NAME}
|
||||||
|
|
||||||
|
DOCKER_HOST: unix:///var/run/docker.sock
|
||||||
|
DOCKER_TLS_CERTDIR: ""
|
||||||
|
DOCKER_BUILDKIT: "1"
|
||||||
|
|
||||||
workflow:
|
workflow:
|
||||||
rules:
|
rules:
|
||||||
# MR pipeline
|
# run untuk branch utama & MR
|
||||||
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
|
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "development"'
|
||||||
when: always
|
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "production"'
|
||||||
|
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "production"'
|
||||||
# Push pipeline hanya untuk env branch
|
|
||||||
- if: '$CI_COMMIT_BRANCH == "development"'
|
|
||||||
when: always
|
|
||||||
- if: '$CI_COMMIT_BRANCH == "staging"'
|
|
||||||
when: always
|
|
||||||
- if: '$CI_COMMIT_BRANCH == "production"'
|
|
||||||
when: always
|
|
||||||
|
|
||||||
# Selain itu jangan buat pipeline
|
|
||||||
- when: never
|
- when: never
|
||||||
|
|
||||||
include:
|
# =========================
|
||||||
# khusus MR (notif)
|
# Helper: login ECR
|
||||||
- local: "ci/merge_request.yml"
|
# =========================
|
||||||
rules:
|
.ecr_login: &ecr_login |
|
||||||
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
|
AWS_CLI_ENV_ARGS=""
|
||||||
|
AWS_CLI_ENV_ARGS="$AWS_CLI_ENV_ARGS -e AWS_REGION=$AWS_REGION"
|
||||||
|
AWS_CLI_ENV_ARGS="$AWS_CLI_ENV_ARGS -e AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID:-}"
|
||||||
|
AWS_CLI_ENV_ARGS="$AWS_CLI_ENV_ARGS -e AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY:-}"
|
||||||
|
if [ -n "${AWS_SESSION_TOKEN:-}" ]; then
|
||||||
|
AWS_CLI_ENV_ARGS="$AWS_CLI_ENV_ARGS -e AWS_SESSION_TOKEN=$AWS_SESSION_TOKEN"
|
||||||
|
fi
|
||||||
|
|
||||||
# khusus push ke branch env
|
PASS="$(docker run --rm $AWS_CLI_ENV_ARGS public.ecr.aws/aws-cli/aws-cli:latest \
|
||||||
- local: "ci/development.yml"
|
ecr get-login-password --region "$AWS_REGION" || true)"
|
||||||
rules:
|
if [ -z "$PASS" ]; then
|
||||||
- if: '$CI_COMMIT_BRANCH == "development"'
|
echo "ERROR: Failed to get ECR login password."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
echo "$PASS" | docker login --username AWS --password-stdin "$ECR_REGISTRY"
|
||||||
|
|
||||||
- local: "ci/staging.yml"
|
# =========================
|
||||||
rules:
|
# MR
|
||||||
- if: '$CI_COMMIT_BRANCH == "staging"'
|
# =========================
|
||||||
|
build_mr:
|
||||||
|
stage: build
|
||||||
|
image: public.ecr.aws/docker/library/docker:27
|
||||||
|
tags: [self-hosted-dev]
|
||||||
|
rules:
|
||||||
|
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "production"'
|
||||||
|
variables:
|
||||||
|
IMAGE_TAG: "prod-mr-${CI_COMMIT_SHORT_SHA}"
|
||||||
|
before_script:
|
||||||
|
- set -eu
|
||||||
|
- docker version
|
||||||
|
- docker info
|
||||||
|
- *ecr_login
|
||||||
|
script: |
|
||||||
|
set -eu
|
||||||
|
echo "Build (MR) : $ECR_REPOSITORY:$IMAGE_TAG"
|
||||||
|
docker build -f Dockerfile -t "$ECR_REPOSITORY:$IMAGE_TAG" .
|
||||||
|
echo "Pushing image for MR..."
|
||||||
|
docker push "$ECR_REPOSITORY:$IMAGE_TAG"
|
||||||
|
|
||||||
- local: "ci/production.yml"
|
# =========================
|
||||||
rules:
|
# DEVELOPMENT (push branch development)
|
||||||
- if: '$CI_COMMIT_BRANCH == "production"'
|
# =========================
|
||||||
|
build_push_dev:
|
||||||
|
stage: build
|
||||||
|
image: public.ecr.aws/docker/library/docker:27
|
||||||
|
tags: [self-hosted-dev]
|
||||||
|
rules:
|
||||||
|
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "development"'
|
||||||
|
variables:
|
||||||
|
IMAGE_TAG: "dev-${CI_COMMIT_SHORT_SHA}"
|
||||||
|
before_script:
|
||||||
|
- set -eu
|
||||||
|
- docker version
|
||||||
|
- docker info
|
||||||
|
- *ecr_login
|
||||||
|
script: |
|
||||||
|
set -eu
|
||||||
|
echo "Build & push (dev): $ECR_REPOSITORY:$IMAGE_TAG"
|
||||||
|
docker build -f Dockerfile -t "$ECR_REPOSITORY:$IMAGE_TAG" .
|
||||||
|
docker push "$ECR_REPOSITORY:$IMAGE_TAG"
|
||||||
|
|
||||||
|
# =========================
|
||||||
|
# PRODUCTION (push branch production)
|
||||||
|
# =========================
|
||||||
|
build_push_prod:
|
||||||
|
stage: build
|
||||||
|
image: public.ecr.aws/docker/library/docker:27
|
||||||
|
tags: [self-hosted-dev]
|
||||||
|
rules:
|
||||||
|
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "production"'
|
||||||
|
variables:
|
||||||
|
IMAGE_TAG: "prod-${CI_COMMIT_SHORT_SHA}"
|
||||||
|
before_script:
|
||||||
|
- set -eu
|
||||||
|
- docker version
|
||||||
|
- docker info
|
||||||
|
- *ecr_login
|
||||||
|
script: |
|
||||||
|
set -eu
|
||||||
|
echo "Build & push (prod): $ECR_REPOSITORY:$IMAGE_TAG"
|
||||||
|
docker build -f Dockerfile -t "$ECR_REPOSITORY:$IMAGE_TAG" .
|
||||||
|
docker push "$ECR_REPOSITORY:$IMAGE_TAG"
|
||||||
|
|
||||||
|
update_gitops_prod_lti:
|
||||||
|
stage: gitops
|
||||||
|
image: public.ecr.aws/docker/library/alpine:3.20
|
||||||
|
tags: [self-hosted-dev]
|
||||||
|
rules:
|
||||||
|
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "production"'
|
||||||
|
needs: ["build_push_prod"]
|
||||||
|
variables:
|
||||||
|
IMAGE_TAG: "prod-${CI_COMMIT_SHORT_SHA}"
|
||||||
|
GITOPS_BRANCH: main
|
||||||
|
VALUES_FILE: environments/lti/prod/lti-values-prod.yaml
|
||||||
|
GITOPS_REPO_URL: https://oauth2:${GITOPS_TOKEN}@gitlab.com/cristian.anggita.parjaman/gitops.git
|
||||||
|
before_script:
|
||||||
|
- set -eu
|
||||||
|
- apk add --no-cache git yq
|
||||||
|
- git config --global user.email "ci@gitlab"
|
||||||
|
- git config --global user.name "gitlab-ci"
|
||||||
|
script: |
|
||||||
|
set -eu
|
||||||
|
rm -rf gitops
|
||||||
|
git clone --depth 1 --branch "$GITOPS_BRANCH" "$GITOPS_REPO_URL" gitops
|
||||||
|
cd gitops
|
||||||
|
|
||||||
|
echo "Updating prod image.tag to $IMAGE_TAG"
|
||||||
|
yq -i '.image.tag = strenv(IMAGE_TAG)' "$VALUES_FILE"
|
||||||
|
|
||||||
|
git add "$VALUES_FILE"
|
||||||
|
if git diff --cached --quiet; then
|
||||||
|
echo "No changes to commit"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
git commit -m "lti prod deploy ${IMAGE_TAG}"
|
||||||
|
git push origin "$GITOPS_BRANCH"
|
||||||
|
|||||||
@@ -111,3 +111,4 @@ IT Development PT Mitra Berlian Unggas Group
|
|||||||
## 📃 License
|
## 📃 License
|
||||||
|
|
||||||
> This project is private. All rights reserved.
|
> This project is private. All rights reserved.
|
||||||
|
# mr test Sat 7 Feb 2026 00:14:58 WIB
|
||||||
|
|||||||
@@ -139,11 +139,12 @@ func (r *HppRepositoryImpl) GetOvkUsageCost(ctx context.Context, projectFlockKan
|
|||||||
Select("COALESCE(SUM(rs.usage_qty * COALESCE(pi.price, 0)), 0)").
|
Select("COALESCE(SUM(rs.usage_qty * COALESCE(pi.price, 0)), 0)").
|
||||||
Joins("JOIN recording_stocks AS rs ON rs.recording_id = r.id").
|
Joins("JOIN recording_stocks AS rs ON rs.recording_id = r.id").
|
||||||
Joins("JOIN product_warehouses AS pw ON pw.id = rs.product_warehouse_id").
|
Joins("JOIN product_warehouses AS pw ON pw.id = rs.product_warehouse_id").
|
||||||
|
Joins("JOIN flags AS f ON f.flagable_id = pw.product_id AND f.flagable_type = ?", entity.FlagableTypeProduct).
|
||||||
Joins("JOIN stock_allocations AS sa ON sa.usable_type = ? AND sa.usable_id = rs.id AND sa.stockable_type = ?", fifo.UsableKeyRecordingStock.String(), fifo.StockableKeyPurchaseItems.String()).
|
Joins("JOIN stock_allocations AS sa ON sa.usable_type = ? AND sa.usable_id = rs.id AND sa.stockable_type = ?", fifo.UsableKeyRecordingStock.String(), fifo.StockableKeyPurchaseItems.String()).
|
||||||
Joins("JOIN purchase_items AS pi ON pi.id = sa.stockable_id").
|
Joins("JOIN purchase_items AS pi ON pi.id = sa.stockable_id").
|
||||||
Where("r.project_flock_kandangs_id IN (?)", projectFlockKandangIDs).
|
Where("r.project_flock_kandangs_id IN (?)", projectFlockKandangIDs).
|
||||||
Where("r.record_datetime <= ?", *date).
|
Where("r.record_datetime <= ?", *date).
|
||||||
Where("EXISTS (SELECT 1 FROM flags f WHERE f.flagable_id = pw.product_id AND f.flagable_type = ? AND f.name IN ?)", entity.FlagableTypeProduct, flags).
|
Where("f.name IN ?", flags).
|
||||||
Scan(&total).Error
|
Scan(&total).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ type FifoService interface {
|
|||||||
Consume(ctx context.Context, req StockConsumeRequest) (*StockConsumeResult, error)
|
Consume(ctx context.Context, req StockConsumeRequest) (*StockConsumeResult, error)
|
||||||
ReleaseUsage(ctx context.Context, req StockReleaseRequest) error
|
ReleaseUsage(ctx context.Context, req StockReleaseRequest) error
|
||||||
AdjustStockableQuantity(ctx context.Context, req StockAdjustRequest) error
|
AdjustStockableQuantity(ctx context.Context, req StockAdjustRequest) error
|
||||||
ResolvePending(ctx context.Context, req PendingResolveRequest) ([]PendingResolution, error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type fifoService struct {
|
type fifoService struct {
|
||||||
@@ -112,11 +111,6 @@ type PendingResolution struct {
|
|||||||
Quantity float64
|
Quantity float64
|
||||||
}
|
}
|
||||||
|
|
||||||
type PendingResolveRequest struct {
|
|
||||||
ProductWarehouseID uint
|
|
||||||
Tx *gorm.DB
|
|
||||||
}
|
|
||||||
|
|
||||||
type StockReplenishResult struct {
|
type StockReplenishResult struct {
|
||||||
AddedQuantity float64
|
AddedQuantity float64
|
||||||
PendingResolved []PendingResolution
|
PendingResolved []PendingResolution
|
||||||
@@ -233,23 +227,6 @@ func (s *fifoService) Replenish(ctx context.Context, req StockReplenishRequest)
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *fifoService) ResolvePending(ctx context.Context, req PendingResolveRequest) ([]PendingResolution, error) {
|
|
||||||
if req.ProductWarehouseID == 0 {
|
|
||||||
return nil, errors.New("product warehouse id is required")
|
|
||||||
}
|
|
||||||
|
|
||||||
var resolved []PendingResolution
|
|
||||||
err := s.withTransaction(ctx, req.Tx, func(tx *gorm.DB) error {
|
|
||||||
var err error
|
|
||||||
resolved, err = s.resolvePendingForWarehouse(ctx, tx, req.ProductWarehouseID)
|
|
||||||
return err
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return resolved, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *fifoService) Consume(ctx context.Context, req StockConsumeRequest) (*StockConsumeResult, error) {
|
func (s *fifoService) Consume(ctx context.Context, req StockConsumeRequest) (*StockConsumeResult, error) {
|
||||||
if req.UsableID == 0 || strings.TrimSpace(req.UsableKey.String()) == "" {
|
if req.UsableID == 0 || strings.TrimSpace(req.UsableKey.String()) == "" {
|
||||||
return nil, errors.New("usable key and id are required")
|
return nil, errors.New("usable key and id are required")
|
||||||
@@ -872,8 +849,8 @@ func (s *fifoService) fetchPendingCandidates(ctx context.Context, tx *gorm.DB, p
|
|||||||
if cfg.Columns.CreatedAt == cfg.Columns.ID {
|
if cfg.Columns.CreatedAt == cfg.Columns.ID {
|
||||||
var rows []struct {
|
var rows []struct {
|
||||||
ID uint
|
ID uint
|
||||||
Pending float64 `gorm:"column:pending_qty"`
|
Pending float64
|
||||||
CreatedAt int64 `gorm:"column:created_at"`
|
CreatedAt int64
|
||||||
}
|
}
|
||||||
|
|
||||||
query := tx.Table(cfg.Table).
|
query := tx.Table(cfg.Table).
|
||||||
@@ -890,26 +867,27 @@ func (s *fifoService) fetchPendingCandidates(ctx context.Context, tx *gorm.DB, p
|
|||||||
query = query.Order(order)
|
query = query.Order(order)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := query.Find(&rows).Error; err != nil {
|
if err := query.Find(&rows).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
|
||||||
for _, row := range rows {
|
|
||||||
if row.Pending <= 0 {
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
candidates = append(candidates, pendingCandidate{
|
|
||||||
UsableKey: key,
|
for _, row := range rows {
|
||||||
Config: cfg,
|
if row.Pending <= 0 {
|
||||||
UsableID: row.ID,
|
continue
|
||||||
Pending: row.Pending,
|
}
|
||||||
CreatedAt: time.Unix(0, row.CreatedAt),
|
candidates = append(candidates, pendingCandidate{
|
||||||
})
|
UsableKey: key,
|
||||||
}
|
Config: cfg,
|
||||||
} else {
|
UsableID: row.ID,
|
||||||
|
Pending: row.Pending,
|
||||||
|
CreatedAt: time.Unix(0, row.CreatedAt),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
var rows []struct {
|
var rows []struct {
|
||||||
ID uint
|
ID uint
|
||||||
Pending float64 `gorm:"column:pending_qty"`
|
Pending float64
|
||||||
CreatedAt time.Time `gorm:"column:created_at"`
|
CreatedAt time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
query := tx.Table(cfg.Table).
|
query := tx.Table(cfg.Table).
|
||||||
|
|||||||
-12
@@ -1,12 +0,0 @@
|
|||||||
BEGIN;
|
|
||||||
|
|
||||||
ALTER TABLE recording_depletions
|
|
||||||
DROP CONSTRAINT IF EXISTS chk_recording_depletions_pending_zero;
|
|
||||||
|
|
||||||
ALTER TABLE recording_depletions
|
|
||||||
DROP COLUMN IF EXISTS total_used_qty;
|
|
||||||
|
|
||||||
ALTER TABLE recording_depletions
|
|
||||||
DROP COLUMN IF EXISTS usage_qty;
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
-15
@@ -1,15 +0,0 @@
|
|||||||
BEGIN;
|
|
||||||
|
|
||||||
ALTER TABLE recording_depletions
|
|
||||||
ADD COLUMN IF NOT EXISTS total_used_qty numeric(15, 3) NOT NULL DEFAULT 0,
|
|
||||||
ADD COLUMN IF NOT EXISTS usage_qty numeric(15, 3) NOT NULL DEFAULT 0;
|
|
||||||
|
|
||||||
UPDATE recording_depletions
|
|
||||||
SET pending_qty = 0
|
|
||||||
WHERE pending_qty IS NULL OR pending_qty <> 0;
|
|
||||||
|
|
||||||
ALTER TABLE recording_depletions
|
|
||||||
ADD CONSTRAINT chk_recording_depletions_pending_zero
|
|
||||||
CHECK (pending_qty = 0);
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
@@ -74,7 +74,7 @@ func seedUsers(tx *gorm.DB) (map[string]uint, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func seedUoms(tx *gorm.DB, createdBy uint) (map[string]uint, error) {
|
func seedUoms(tx *gorm.DB, createdBy uint) (map[string]uint, error) {
|
||||||
names := []string{"Kilogram", "Gram", "Liter", "Unit", "Ekor", "Butir"}
|
names := []string{"Kilogram", "Gram", "Liter", "Unit", "Ekor"}
|
||||||
result := make(map[string]uint, len(names))
|
result := make(map[string]uint, len(names))
|
||||||
|
|
||||||
for _, name := range names {
|
for _, name := range names {
|
||||||
@@ -235,7 +235,7 @@ func seedProducts(tx *gorm.DB, createdBy uint, uoms map[string]uint, categories
|
|||||||
Name: "Telur Utuh",
|
Name: "Telur Utuh",
|
||||||
Brand: "-",
|
Brand: "-",
|
||||||
Sku: "4",
|
Sku: "4",
|
||||||
Uom: "Butir",
|
Uom: "Gram",
|
||||||
Category: "Telur",
|
Category: "Telur",
|
||||||
Price: 1,
|
Price: 1,
|
||||||
Flags: []utils.FlagType{utils.FlagTelurUtuh},
|
Flags: []utils.FlagType{utils.FlagTelurUtuh},
|
||||||
@@ -245,7 +245,7 @@ func seedProducts(tx *gorm.DB, createdBy uint, uoms map[string]uint, categories
|
|||||||
Name: "Telur Pecah",
|
Name: "Telur Pecah",
|
||||||
Brand: "-",
|
Brand: "-",
|
||||||
Sku: "5",
|
Sku: "5",
|
||||||
Uom: "Butir",
|
Uom: "Gram",
|
||||||
Category: "Telur",
|
Category: "Telur",
|
||||||
Price: 1,
|
Price: 1,
|
||||||
Flags: []utils.FlagType{utils.FlagTelurPecah},
|
Flags: []utils.FlagType{utils.FlagTelurPecah},
|
||||||
@@ -255,7 +255,7 @@ func seedProducts(tx *gorm.DB, createdBy uint, uoms map[string]uint, categories
|
|||||||
Name: "Telur Putih",
|
Name: "Telur Putih",
|
||||||
Brand: "-",
|
Brand: "-",
|
||||||
Sku: "6",
|
Sku: "6",
|
||||||
Uom: "Butir",
|
Uom: "Gram",
|
||||||
Category: "Telur",
|
Category: "Telur",
|
||||||
Price: 1,
|
Price: 1,
|
||||||
Flags: []utils.FlagType{utils.FlagTelurPutih},
|
Flags: []utils.FlagType{utils.FlagTelurPutih},
|
||||||
@@ -265,32 +265,12 @@ func seedProducts(tx *gorm.DB, createdBy uint, uoms map[string]uint, categories
|
|||||||
Name: "Telur Retak",
|
Name: "Telur Retak",
|
||||||
Brand: "-",
|
Brand: "-",
|
||||||
Sku: "7",
|
Sku: "7",
|
||||||
Uom: "Butir",
|
Uom: "Gram",
|
||||||
Category: "Telur",
|
Category: "Telur",
|
||||||
Price: 1,
|
Price: 1,
|
||||||
Flags: []utils.FlagType{utils.FlagTelurRetak},
|
Flags: []utils.FlagType{utils.FlagTelurRetak},
|
||||||
IsVisible: false,
|
IsVisible: false,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
Name: "Telur Papacal",
|
|
||||||
Brand: "-",
|
|
||||||
Sku: "8",
|
|
||||||
Uom: "Butir",
|
|
||||||
Category: "Telur",
|
|
||||||
Price: 1,
|
|
||||||
Flags: []utils.FlagType{utils.FlagTelur},
|
|
||||||
IsVisible: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "Telur Jumbo",
|
|
||||||
Brand: "-",
|
|
||||||
Sku: "9",
|
|
||||||
Uom: "Butir",
|
|
||||||
Category: "Telur",
|
|
||||||
Price: 1,
|
|
||||||
Flags: []utils.FlagType{utils.FlagTelur},
|
|
||||||
IsVisible: false,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, seed := range seeds {
|
for _, seed := range seeds {
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ type PurchaseItem struct {
|
|||||||
Price float64 `gorm:"type:numeric(15,3);default:0"`
|
Price float64 `gorm:"type:numeric(15,3);default:0"`
|
||||||
TotalPrice float64 `gorm:"type:numeric(15,3);default:0"`
|
TotalPrice float64 `gorm:"type:numeric(15,3);default:0"`
|
||||||
ExpenseNonstockId *uint64
|
ExpenseNonstockId *uint64
|
||||||
HasChickin bool `gorm:"-" json:"-"`
|
|
||||||
|
|
||||||
// Relations
|
// Relations
|
||||||
ExpenseNonstock *ExpenseNonstock `gorm:"foreignKey:ExpenseNonstockId;references:Id"`
|
ExpenseNonstock *ExpenseNonstock `gorm:"foreignKey:ExpenseNonstockId;references:Id"`
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ type RecordingDepletion struct {
|
|||||||
ProductWarehouseId uint `gorm:"column:product_warehouse_id;not null"`
|
ProductWarehouseId uint `gorm:"column:product_warehouse_id;not null"`
|
||||||
SourceProductWarehouseId *uint `gorm:"column:source_product_warehouse_id"`
|
SourceProductWarehouseId *uint `gorm:"column:source_product_warehouse_id"`
|
||||||
Qty float64 `gorm:"column:qty;not null"`
|
Qty float64 `gorm:"column:qty;not null"`
|
||||||
UsageQty float64 `gorm:"column:usage_qty"`
|
|
||||||
PendingQty float64 `gorm:"column:pending_qty"`
|
PendingQty float64 `gorm:"column:pending_qty"`
|
||||||
|
|
||||||
Recording Recording `gorm:"foreignKey:RecordingId;references:Id"`
|
Recording Recording `gorm:"foreignKey:RecordingId;references:Id"`
|
||||||
|
|||||||
@@ -347,12 +347,12 @@ func (u *ClosingController) GetSapronakByProject(c *fiber.Ctx) error {
|
|||||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid project_flock_id")
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid project_flock_id")
|
||||||
}
|
}
|
||||||
|
|
||||||
result, productFlags, err := u.SapronakService.GetSapronakByProject(c, uint(projectID), flag)
|
result, err := u.SapronakService.GetSapronakByProject(c, uint(projectID), flag)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
payload := dto.ToSapronakProjectAggregatedFromReports(result, flag, productFlags)
|
payload := dto.ToSapronakProjectAggregatedFromReports(result, flag)
|
||||||
|
|
||||||
return c.Status(fiber.StatusOK).
|
return c.Status(fiber.StatusOK).
|
||||||
JSON(response.Success{
|
JSON(response.Success{
|
||||||
@@ -377,12 +377,12 @@ func (u *ClosingController) GetSapronakByKandang(c *fiber.Ctx) error {
|
|||||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid project_flock_kandang_id")
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid project_flock_kandang_id")
|
||||||
}
|
}
|
||||||
|
|
||||||
result, productFlags, err := u.SapronakService.GetSapronakByKandang(c, uint(projectID), uint(pfkID), flag)
|
result, err := u.SapronakService.GetSapronakByKandang(c, uint(projectID), uint(pfkID), flag)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
payload := dto.ToSapronakProjectAggregatedFromReport(result, flag, productFlags)
|
payload := dto.ToSapronakProjectAggregatedFromReport(result, flag)
|
||||||
|
|
||||||
return c.Status(fiber.StatusOK).
|
return c.Status(fiber.StatusOK).
|
||||||
JSON(response.Success{
|
JSON(response.Success{
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package dto
|
package dto
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sort"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -128,7 +127,7 @@ type UomSummaryDTO struct {
|
|||||||
|
|
||||||
// === Mapper Functions for Aggregated Sapronak Response ===
|
// === Mapper Functions for Aggregated Sapronak Response ===
|
||||||
|
|
||||||
func ToSapronakProjectAggregatedFromReports(reports []SapronakReportDTO, flag string, productFlags map[uint][]string) SapronakProjectAggregatedDTO {
|
func ToSapronakProjectAggregatedFromReports(reports []SapronakReportDTO, flag string) SapronakProjectAggregatedDTO {
|
||||||
result := SapronakProjectAggregatedDTO{}
|
result := SapronakProjectAggregatedDTO{}
|
||||||
|
|
||||||
if len(reports) == 0 {
|
if len(reports) == 0 {
|
||||||
@@ -136,10 +135,10 @@ func ToSapronakProjectAggregatedFromReports(reports []SapronakReportDTO, flag st
|
|||||||
}
|
}
|
||||||
|
|
||||||
rep := reports[0]
|
rep := reports[0]
|
||||||
return ToSapronakProjectAggregatedFromReport(&rep, flag, productFlags)
|
return ToSapronakProjectAggregatedFromReport(&rep, flag)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ToSapronakProjectAggregatedFromReport(report *SapronakReportDTO, flag string, productFlags map[uint][]string) SapronakProjectAggregatedDTO {
|
func ToSapronakProjectAggregatedFromReport(report *SapronakReportDTO, flag string) SapronakProjectAggregatedDTO {
|
||||||
result := SapronakProjectAggregatedDTO{}
|
result := SapronakProjectAggregatedDTO{}
|
||||||
|
|
||||||
if report == nil {
|
if report == nil {
|
||||||
@@ -176,53 +175,6 @@ func ToSapronakProjectAggregatedFromReport(report *SapronakReportDTO, flag strin
|
|||||||
return t.Format("02-Jan-2006")
|
return t.Format("02-Jan-2006")
|
||||||
}
|
}
|
||||||
|
|
||||||
flagOrder := map[string]int{
|
|
||||||
"DOC": 0,
|
|
||||||
"PAKAN": 0,
|
|
||||||
"OVK": 0,
|
|
||||||
"PULLET": 0,
|
|
||||||
}
|
|
||||||
|
|
||||||
buildFlagList := func(productID uint, fallback string) string {
|
|
||||||
rawFlags := productFlags[productID]
|
|
||||||
if len(rawFlags) == 0 {
|
|
||||||
if fallback == "" {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return fallback
|
|
||||||
}
|
|
||||||
seen := make(map[string]struct{}, len(rawFlags))
|
|
||||||
ordered := make([]string, 0, len(rawFlags))
|
|
||||||
for _, f := range rawFlags {
|
|
||||||
flagName := strings.ToUpper(strings.TrimSpace(f))
|
|
||||||
if flagName == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if _, ok := seen[flagName]; ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
seen[flagName] = struct{}{}
|
|
||||||
ordered = append(ordered, flagName)
|
|
||||||
}
|
|
||||||
sort.SliceStable(ordered, func(i, j int) bool {
|
|
||||||
li := ordered[i]
|
|
||||||
lj := ordered[j]
|
|
||||||
ri, iok := flagOrder[li]
|
|
||||||
rj, jok := flagOrder[lj]
|
|
||||||
if iok != jok {
|
|
||||||
if iok {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if iok && jok && ri != rj {
|
|
||||||
return ri < rj
|
|
||||||
}
|
|
||||||
return li < lj
|
|
||||||
})
|
|
||||||
return strings.Join(ordered, " ")
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, group := range report.Groups {
|
for _, group := range report.Groups {
|
||||||
flagKey := normalizeFlag(group.Flag)
|
flagKey := normalizeFlag(group.Flag)
|
||||||
ptr := byFlag[flagKey]
|
ptr := byFlag[flagKey]
|
||||||
@@ -254,7 +206,7 @@ func ToSapronakProjectAggregatedFromReport(report *SapronakReportDTO, flag strin
|
|||||||
Date: formatDate(item.Tanggal),
|
Date: formatDate(item.Tanggal),
|
||||||
ReferenceNumber: item.NoReferensi,
|
ReferenceNumber: item.NoReferensi,
|
||||||
Description: item.ProductName,
|
Description: item.ProductName,
|
||||||
ProductCategory: buildFlagList(item.ProductID, flagKey),
|
ProductCategory: item.ProductName,
|
||||||
UnitPrice: item.Harga,
|
UnitPrice: item.Harga,
|
||||||
Notes: "-",
|
Notes: "-",
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -991,8 +991,8 @@ func (r *ClosingRepositoryImpl) FetchSapronakUsageAllocatedDetails(ctx context.C
|
|||||||
query := r.withCtx(ctx).
|
query := r.withCtx(ctx).
|
||||||
Table("stock_allocations AS sa").
|
Table("stock_allocations AS sa").
|
||||||
Select(`
|
Select(`
|
||||||
p_resolve.id AS product_id,
|
pw.product_id AS product_id,
|
||||||
p_resolve.name AS product_name,
|
p.name AS product_name,
|
||||||
f.name AS flag,
|
f.name AS flag,
|
||||||
COALESCE(
|
COALESCE(
|
||||||
pi.received_date,
|
pi.received_date,
|
||||||
@@ -1013,9 +1013,10 @@ func (r *ClosingRepositoryImpl) FetchSapronakUsageAllocatedDetails(ctx context.C
|
|||||||
) AS reference,
|
) AS reference,
|
||||||
0 AS qty_in,
|
0 AS qty_in,
|
||||||
COALESCE(SUM(sa.qty), 0) AS qty_out,
|
COALESCE(SUM(sa.qty), 0) AS qty_out,
|
||||||
COALESCE(pi.price, p_resolve.product_price, 0) AS price
|
COALESCE(pi.price, p.product_price, 0) AS price
|
||||||
`).
|
`).
|
||||||
Joins("JOIN product_warehouses pw ON pw.id = sa.product_warehouse_id").
|
Joins("JOIN product_warehouses pw ON pw.id = sa.product_warehouse_id").
|
||||||
|
Joins("JOIN products p ON p.id = pw.product_id").
|
||||||
Joins("LEFT JOIN recording_stocks rs ON rs.id = sa.usable_id AND sa.usable_type = ?", fifo.UsableKeyRecordingStock.String()).
|
Joins("LEFT JOIN recording_stocks rs ON rs.id = sa.usable_id AND sa.usable_type = ?", fifo.UsableKeyRecordingStock.String()).
|
||||||
Joins("LEFT JOIN recordings r ON r.id = rs.recording_id").
|
Joins("LEFT JOIN recordings r ON r.id = rs.recording_id").
|
||||||
Joins("LEFT JOIN project_chickins pc_used ON pc_used.id = sa.usable_id AND sa.usable_type = ?", fifo.UsableKeyProjectChickin.String()).
|
Joins("LEFT JOIN project_chickins pc_used ON pc_used.id = sa.usable_id AND sa.usable_type = ?", fifo.UsableKeyProjectChickin.String()).
|
||||||
@@ -1025,11 +1026,9 @@ func (r *ClosingRepositoryImpl) FetchSapronakUsageAllocatedDetails(ctx context.C
|
|||||||
Joins("LEFT JOIN stock_transfers st ON st.id = std.stock_transfer_id").
|
Joins("LEFT JOIN stock_transfers st ON st.id = std.stock_transfer_id").
|
||||||
Joins("LEFT JOIN laying_transfer_targets ltt ON ltt.id = sa.stockable_id AND sa.stockable_type = ?", fifo.StockableKeyTransferToLayingIn.String()).
|
Joins("LEFT JOIN laying_transfer_targets ltt ON ltt.id = sa.stockable_id AND sa.stockable_type = ?", fifo.StockableKeyTransferToLayingIn.String()).
|
||||||
Joins("LEFT JOIN laying_transfers lt ON lt.id = ltt.laying_transfer_id").
|
Joins("LEFT JOIN laying_transfers lt ON lt.id = ltt.laying_transfer_id").
|
||||||
Joins("LEFT JOIN product_warehouses pw_ltt ON pw_ltt.id = ltt.product_warehouse_id").
|
|
||||||
Joins("LEFT JOIN adjustment_stocks ast ON ast.id = sa.stockable_id AND sa.stockable_type = ?", fifo.StockableKeyAdjustmentIn.String()).
|
Joins("LEFT JOIN adjustment_stocks ast ON ast.id = sa.stockable_id AND sa.stockable_type = ?", fifo.StockableKeyAdjustmentIn.String()).
|
||||||
Joins("LEFT JOIN project_flock_populations pfp ON pfp.id = sa.stockable_id AND sa.stockable_type = ?", fifo.StockableKeyProjectFlockPopulation.String()).
|
Joins("LEFT JOIN project_flock_populations pfp ON pfp.id = sa.stockable_id AND sa.stockable_type = ?", fifo.StockableKeyProjectFlockPopulation.String()).
|
||||||
Joins("LEFT JOIN project_chickins pc ON pc.id = pfp.project_chickin_id").
|
Joins("LEFT JOIN project_chickins pc ON pc.id = pfp.project_chickin_id").
|
||||||
Joins("LEFT JOIN products p_resolve ON p_resolve.id = COALESCE(pi.product_id, pw_ltt.product_id, pw.product_id)").
|
|
||||||
Where("sa.status = ?", entity.StockAllocationStatusActive).
|
Where("sa.status = ?", entity.StockAllocationStatusActive).
|
||||||
Where("sa.stockable_type <> ?", fifo.StockableKeyProjectFlockPopulation.String()).
|
Where("sa.stockable_type <> ?", fifo.StockableKeyProjectFlockPopulation.String()).
|
||||||
Where("f.name IN ?", sapronakFlagsAll).
|
Where("f.name IN ?", sapronakFlagsAll).
|
||||||
@@ -1041,12 +1040,12 @@ func (r *ClosingRepositoryImpl) FetchSapronakUsageAllocatedDetails(ctx context.C
|
|||||||
fifo.UsableKeyRecordingStock.String(), projectFlockKandangID,
|
fifo.UsableKeyRecordingStock.String(), projectFlockKandangID,
|
||||||
fifo.UsableKeyProjectChickin.String(), projectFlockKandangID,
|
fifo.UsableKeyProjectChickin.String(), projectFlockKandangID,
|
||||||
)
|
)
|
||||||
query = r.joinSapronakProductFlag(query, "p_resolve").
|
query = r.joinSapronakProductFlag(query, "p").
|
||||||
Group(`
|
Group(`
|
||||||
p_resolve.id, p_resolve.name, f.name,
|
pw.product_id, p.name, f.name,
|
||||||
pi.received_date, st.transfer_date, lt.transfer_date, ast.created_at, pc.chick_in_date, r.record_datetime,
|
pi.received_date, st.transfer_date, lt.transfer_date, ast.created_at, pc.chick_in_date, r.record_datetime,
|
||||||
po.po_number, st.movement_number, lt.transfer_number, ast.id, pc.id, r.id,
|
po.po_number, st.movement_number, lt.transfer_number, ast.id, pc.id, r.id,
|
||||||
pi.price, p_resolve.product_price
|
pi.price, p.product_price
|
||||||
`)
|
`)
|
||||||
|
|
||||||
return scanAndGroupDetails(query)
|
return scanAndGroupDetails(query)
|
||||||
|
|||||||
@@ -294,9 +294,6 @@ func (s closingKeuanganService) calculateProductionData(c *fiber.Ctx, projectFlo
|
|||||||
func (s closingKeuanganService) buildHPPSection(c *fiber.Ctx, projectFlock *entity.ProjectFlock, projectFlockKandangs []entity.ProjectFlockKandang, costs *CostData, production *ProductionData) dto.HPPSection {
|
func (s closingKeuanganService) buildHPPSection(c *fiber.Ctx, projectFlock *entity.ProjectFlock, projectFlockKandangs []entity.ProjectFlockKandang, costs *CostData, production *ProductionData) dto.HPPSection {
|
||||||
|
|
||||||
actualPopulation := production.TotalPopulationIn - production.TotalDepletion
|
actualPopulation := production.TotalPopulationIn - production.TotalDepletion
|
||||||
if lastPopulation, ok := s.getLastPopulationFromRecordings(c, projectFlockKandangs); ok {
|
|
||||||
actualPopulation = lastPopulation
|
|
||||||
}
|
|
||||||
totalWeightProduced := production.TotalWeightProduced
|
totalWeightProduced := production.TotalWeightProduced
|
||||||
totalEggWeightKg := production.TotalEggWeightKg
|
totalEggWeightKg := production.TotalEggWeightKg
|
||||||
|
|
||||||
@@ -532,35 +529,6 @@ func (s closingKeuanganService) buildProfitLossSection(projectFlock *entity.Proj
|
|||||||
return dto.ToProfitLossSection(plItems, plSummary)
|
return dto.ToProfitLossSection(plItems, plSummary)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s closingKeuanganService) getLastPopulationFromRecordings(c *fiber.Ctx, projectFlockKandangs []entity.ProjectFlockKandang) (float64, bool) {
|
|
||||||
if s.RecordingRepo == nil || len(projectFlockKandangs) == 0 {
|
|
||||||
return 0, false
|
|
||||||
}
|
|
||||||
|
|
||||||
total := 0.0
|
|
||||||
recordedCount := 0
|
|
||||||
for _, kandang := range projectFlockKandangs {
|
|
||||||
latest, err := s.RecordingRepo.GetLatestByProjectFlockKandangID(c.Context(), kandang.Id)
|
|
||||||
if err != nil {
|
|
||||||
s.Log.Errorf("Failed to fetch latest recording for project_flock_kandang_id=%d: %+v", kandang.Id, err)
|
|
||||||
return 0, false
|
|
||||||
}
|
|
||||||
if latest == nil || latest.TotalChickQty == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
recordedCount++
|
|
||||||
if *latest.TotalChickQty > 0 {
|
|
||||||
total += *latest.TotalChickQty
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if recordedCount != len(projectFlockKandangs) {
|
|
||||||
return 0, false
|
|
||||||
}
|
|
||||||
|
|
||||||
return total, true
|
|
||||||
}
|
|
||||||
|
|
||||||
func containsFlag(flags []entity.Flag, name string) bool {
|
func containsFlag(flags []entity.Flag, name string) bool {
|
||||||
for _, flag := range flags {
|
for _, flag := range flags {
|
||||||
if flag.Name == name {
|
if flag.Name == name {
|
||||||
|
|||||||
@@ -18,8 +18,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type SapronakService interface {
|
type SapronakService interface {
|
||||||
GetSapronakByProject(ctx *fiber.Ctx, projectFlockID uint, flag string) ([]dto.SapronakReportDTO, map[uint][]string, error)
|
GetSapronakByProject(ctx *fiber.Ctx, projectFlockID uint, flag string) ([]dto.SapronakReportDTO, error)
|
||||||
GetSapronakByKandang(ctx *fiber.Ctx, projectFlockID uint, pfkID uint, flag string) (*dto.SapronakReportDTO, map[uint][]string, error)
|
GetSapronakByKandang(ctx *fiber.Ctx, projectFlockID uint, pfkID uint, flag string) (*dto.SapronakReportDTO, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type sapronakService struct {
|
type sapronakService struct {
|
||||||
@@ -42,9 +42,9 @@ func NewSapronakService(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s sapronakService) GetSapronakByProject(c *fiber.Ctx, projectFlockID uint, flag string) ([]dto.SapronakReportDTO, map[uint][]string, error) {
|
func (s sapronakService) GetSapronakByProject(c *fiber.Ctx, projectFlockID uint, flag string) ([]dto.SapronakReportDTO, error) {
|
||||||
if projectFlockID == 0 {
|
if projectFlockID == 0 {
|
||||||
return nil, nil, fiber.NewError(fiber.StatusBadRequest, "project_flock_id is required")
|
return nil, fiber.NewError(fiber.StatusBadRequest, "project_flock_id is required")
|
||||||
}
|
}
|
||||||
reports, err := s.computeSapronakReports(c.Context(), &validation.CountSapronakQuery{
|
reports, err := s.computeSapronakReports(c.Context(), &validation.CountSapronakQuery{
|
||||||
ProjectFlockID: projectFlockID,
|
ProjectFlockID: projectFlockID,
|
||||||
@@ -52,27 +52,19 @@ func (s sapronakService) GetSapronakByProject(c *fiber.Ctx, projectFlockID uint,
|
|||||||
Flag: flag,
|
Flag: flag,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(reports) <= 1 {
|
if len(reports) <= 1 {
|
||||||
flags, err := s.collectProductFlags(c.Context(), reports)
|
return reports, nil
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
return reports, flags, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
combined := s.combineSapronakReports(reports, projectFlockID)
|
combined := s.combineSapronakReports(reports, projectFlockID)
|
||||||
flags, err := s.collectProductFlags(c.Context(), []dto.SapronakReportDTO{combined})
|
return []dto.SapronakReportDTO{combined}, nil
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
return []dto.SapronakReportDTO{combined}, flags, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s sapronakService) GetSapronakByKandang(c *fiber.Ctx, projectFlockID uint, pfkID uint, flag string) (*dto.SapronakReportDTO, map[uint][]string, error) {
|
func (s sapronakService) GetSapronakByKandang(c *fiber.Ctx, projectFlockID uint, pfkID uint, flag string) (*dto.SapronakReportDTO, error) {
|
||||||
if projectFlockID == 0 || pfkID == 0 {
|
if projectFlockID == 0 || pfkID == 0 {
|
||||||
return nil, nil, fiber.NewError(fiber.StatusBadRequest, "project_flock_id and project_flock_kandang_id are required")
|
return nil, fiber.NewError(fiber.StatusBadRequest, "project_flock_id and project_flock_kandang_id are required")
|
||||||
}
|
}
|
||||||
|
|
||||||
results, err := s.computeSapronakReports(c.Context(), &validation.CountSapronakQuery{
|
results, err := s.computeSapronakReports(c.Context(), &validation.CountSapronakQuery{
|
||||||
@@ -82,20 +74,16 @@ func (s sapronakService) GetSapronakByKandang(c *fiber.Ctx, projectFlockID uint,
|
|||||||
Flag: flag,
|
Flag: flag,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, res := range results {
|
for _, res := range results {
|
||||||
if res.ProjectFlockID == projectFlockID && res.ProjectFlockKandangID == pfkID {
|
if res.ProjectFlockID == projectFlockID && res.ProjectFlockKandangID == pfkID {
|
||||||
flags, err := s.collectProductFlags(c.Context(), []dto.SapronakReportDTO{res})
|
return &res, nil
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
return &res, flags, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, nil, fiber.NewError(fiber.StatusNotFound, "Sapronak for kandang not found")
|
return nil, fiber.NewError(fiber.StatusNotFound, "Sapronak for kandang not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s sapronakService) computeSapronakReports(ctx context.Context, params *validation.CountSapronakQuery) ([]dto.SapronakReportDTO, error) {
|
func (s sapronakService) computeSapronakReports(ctx context.Context, params *validation.CountSapronakQuery) ([]dto.SapronakReportDTO, error) {
|
||||||
@@ -148,52 +136,6 @@ func (s sapronakService) computeSapronakReports(ctx context.Context, params *val
|
|||||||
return results, nil
|
return results, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s sapronakService) collectProductFlags(ctx context.Context, reports []dto.SapronakReportDTO) (map[uint][]string, error) {
|
|
||||||
productIDs := make(map[uint]struct{})
|
|
||||||
for _, report := range reports {
|
|
||||||
for _, group := range report.Groups {
|
|
||||||
for _, item := range group.Items {
|
|
||||||
if item.ProductID > 0 {
|
|
||||||
productIDs[item.ProductID] = struct{}{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(productIDs) == 0 {
|
|
||||||
return map[uint][]string{}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
ids := make([]uint, 0, len(productIDs))
|
|
||||||
for id := range productIDs {
|
|
||||||
ids = append(ids, id)
|
|
||||||
}
|
|
||||||
|
|
||||||
products, err := s.Repository.GetProductsWithFlagsByIDs(ctx, ids)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
result := make(map[uint][]string, len(products))
|
|
||||||
for _, product := range products {
|
|
||||||
if len(product.Flags) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
flags := make([]string, 0, len(product.Flags))
|
|
||||||
for _, flag := range product.Flags {
|
|
||||||
name := strings.TrimSpace(flag.Name)
|
|
||||||
if name == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
flags = append(flags, strings.ToUpper(name))
|
|
||||||
}
|
|
||||||
if len(flags) > 0 {
|
|
||||||
result[product.Id] = flags
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s sapronakService) loadProjectFlockKandangs(ctx context.Context, params *validation.CountSapronakQuery) ([]entity.ProjectFlockKandang, error) {
|
func (s sapronakService) loadProjectFlockKandangs(ctx context.Context, params *validation.CountSapronakQuery) ([]entity.ProjectFlockKandang, error) {
|
||||||
db := s.ProjectFlockKandangRepo.DB().WithContext(ctx).
|
db := s.ProjectFlockKandangRepo.DB().WithContext(ctx).
|
||||||
Preload("ProjectFlock").
|
Preload("ProjectFlock").
|
||||||
|
|||||||
@@ -175,47 +175,5 @@ func (s productStockService) GetOne(c *fiber.Ctx, id uint) (*entity.Product, err
|
|||||||
s.Log.Errorf("Failed get product by id: %+v", err)
|
s.Log.Errorf("Failed get product by id: %+v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(product.ProductWarehouses) > 0 {
|
|
||||||
ids := make([]uint, 0, len(product.ProductWarehouses))
|
|
||||||
for _, pw := range product.ProductWarehouses {
|
|
||||||
if pw.Id != 0 {
|
|
||||||
ids = append(ids, pw.Id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(ids) > 0 {
|
|
||||||
type pendingUsageRow struct {
|
|
||||||
ProductWarehouseId uint
|
|
||||||
PendingQty float64
|
|
||||||
}
|
|
||||||
|
|
||||||
var rows []pendingUsageRow
|
|
||||||
if err := s.ProductRepository.DB().WithContext(c.Context()).
|
|
||||||
Table("recording_stocks").
|
|
||||||
Select("product_warehouse_id, COALESCE(SUM(pending_qty), 0) AS pending_qty").
|
|
||||||
Where("pending_qty > 0").
|
|
||||||
Where("product_warehouse_id IN ?", ids).
|
|
||||||
Group("product_warehouse_id").
|
|
||||||
Scan(&rows).Error; err != nil {
|
|
||||||
s.Log.Errorf("Failed to load pending usage for product warehouses: %+v", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(rows) > 0 {
|
|
||||||
pendingMap := make(map[uint]float64, len(rows))
|
|
||||||
for _, row := range rows {
|
|
||||||
pendingMap[row.ProductWarehouseId] = row.PendingQty
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := range product.ProductWarehouses {
|
|
||||||
pw := &product.ProductWarehouses[i]
|
|
||||||
if pending, ok := pendingMap[pw.Id]; ok && pending != 0 {
|
|
||||||
pw.Quantity -= pending
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return product, nil
|
return product, nil
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-42
@@ -100,13 +100,9 @@ func (s productWarehouseService) GetAll(c *fiber.Ctx, params *validation.Query)
|
|||||||
|
|
||||||
offset := (params.Page - 1) * params.Limit
|
offset := (params.Page - 1) * params.Limit
|
||||||
|
|
||||||
var marketingTypes []string
|
|
||||||
if params.Type != "" {
|
if params.Type != "" {
|
||||||
marketingTypes = utils.ParseQueryArray(params.Type)
|
if !utils.IsValidMarketingType(params.Type) {
|
||||||
for _, t := range marketingTypes {
|
return nil, 0, fiber.NewError(fiber.StatusBadRequest, "Invalid marketing type")
|
||||||
if !utils.IsValidMarketingType(t) {
|
|
||||||
return nil, 0, fiber.NewError(fiber.StatusBadRequest, "Invalid marketing type")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,42 +135,16 @@ func (s productWarehouseService) GetAll(c *fiber.Ctx, params *validation.Query)
|
|||||||
db = db.Where("warehouse_id = ?", params.WarehouseId)
|
db = db.Where("warehouse_id = ?", params.WarehouseId)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(marketingTypes) > 0 {
|
if params.Type != "" {
|
||||||
flagSet := make(map[string]struct{})
|
switch params.Type {
|
||||||
for _, t := range marketingTypes {
|
case string(utils.MarketingTypeAyamPullet):
|
||||||
switch t {
|
db = s.Repository.ApplyFlagsFilter(db, []string{string(utils.FlagDOC), string(utils.FlagPullet), string(utils.FlagLayer)})
|
||||||
case string(utils.MarketingTypeAyamPullet):
|
case string(utils.MarketingTypeAyam):
|
||||||
flagSet[string(utils.FlagDOC)] = struct{}{}
|
db = s.Repository.ApplyFlagsFilter(db, []string{string(utils.FlagAyamAfkir), string(utils.FlagAyamCulling), string(utils.FlagAyamMati)})
|
||||||
flagSet[string(utils.FlagPullet)] = struct{}{}
|
case string(utils.MarketingTypeTelur):
|
||||||
flagSet[string(utils.FlagLayer)] = struct{}{}
|
db = s.Repository.ApplyFlagsFilter(db, []string{string(utils.FlagTelur), string(utils.FlagTelurUtuh), string(utils.FlagTelurPecah), string(utils.FlagTelurPutih), string(utils.FlagTelurRetak)})
|
||||||
case string(utils.MarketingTypeAyam):
|
case string(utils.MarketingTypeTrading):
|
||||||
flagSet[string(utils.FlagAyamAfkir)] = struct{}{}
|
db = s.Repository.ApplyFlagsFilter(db, []string{string(utils.FlagPakan), string(utils.FlagPreStarter), string(utils.FlagStarter), string(utils.FlagFinisher), string(utils.FlagOVK), string(utils.FlagObat), string(utils.FlagVitamin), string(utils.FlagKimia), string(utils.FlagEkspedisi)})
|
||||||
flagSet[string(utils.FlagAyamCulling)] = struct{}{}
|
|
||||||
flagSet[string(utils.FlagAyamMati)] = struct{}{}
|
|
||||||
case string(utils.MarketingTypeTelur):
|
|
||||||
flagSet[string(utils.FlagTelur)] = struct{}{}
|
|
||||||
flagSet[string(utils.FlagTelurUtuh)] = struct{}{}
|
|
||||||
flagSet[string(utils.FlagTelurPecah)] = struct{}{}
|
|
||||||
flagSet[string(utils.FlagTelurPutih)] = struct{}{}
|
|
||||||
flagSet[string(utils.FlagTelurRetak)] = struct{}{}
|
|
||||||
case string(utils.MarketingTypeTrading):
|
|
||||||
flagSet[string(utils.FlagPakan)] = struct{}{}
|
|
||||||
flagSet[string(utils.FlagPreStarter)] = struct{}{}
|
|
||||||
flagSet[string(utils.FlagStarter)] = struct{}{}
|
|
||||||
flagSet[string(utils.FlagFinisher)] = struct{}{}
|
|
||||||
flagSet[string(utils.FlagOVK)] = struct{}{}
|
|
||||||
flagSet[string(utils.FlagObat)] = struct{}{}
|
|
||||||
flagSet[string(utils.FlagVitamin)] = struct{}{}
|
|
||||||
flagSet[string(utils.FlagKimia)] = struct{}{}
|
|
||||||
flagSet[string(utils.FlagEkspedisi)] = struct{}{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(flagSet) > 0 {
|
|
||||||
flags := make([]string, 0, len(flagSet))
|
|
||||||
for f := range flagSet {
|
|
||||||
flags = append(flags, f)
|
|
||||||
}
|
|
||||||
db = s.Repository.ApplyFlagsFilter(db, flags)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -20,5 +20,5 @@ type Query struct {
|
|||||||
Flags string `query:"flags" validate:"omitempty"`
|
Flags string `query:"flags" validate:"omitempty"`
|
||||||
KandangId uint `query:"kandang_id" validate:"omitempty,number,min=1"`
|
KandangId uint `query:"kandang_id" validate:"omitempty,number,min=1"`
|
||||||
TransferContext string `query:"transfer_context" validate:"omitempty,oneof=inventory_transfer"`
|
TransferContext string `query:"transfer_context" validate:"omitempty,oneof=inventory_transfer"`
|
||||||
Type string `query:"type" validate:"omitempty"`
|
Type string `query:"type" validate:"omitempty,oneof=AYAM TELUR TRADING AYAM_PULLET"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -483,7 +483,7 @@ func (s *transferService) CreateOne(c *fiber.Ctx, req *validation.TransferReques
|
|||||||
}
|
}
|
||||||
if len(stockLogs) > 0 {
|
if len(stockLogs) > 0 {
|
||||||
latestStockLog := stockLogs[0]
|
latestStockLog := stockLogs[0]
|
||||||
stockLogDecrease.Stock = latestStockLog.Stock - stockLogDecrease.Decrease
|
stockLogDecrease.Stock -= latestStockLog.Stock - stockLogDecrease.Decrease
|
||||||
} else {
|
} else {
|
||||||
stockLogDecrease.Stock -= stockLogDecrease.Decrease
|
stockLogDecrease.Stock -= stockLogDecrease.Decrease
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package controller
|
|||||||
import (
|
import (
|
||||||
"math"
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/dto"
|
"gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/dto"
|
||||||
service "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/services"
|
service "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/services"
|
||||||
@@ -24,38 +23,9 @@ func NewDeliveryOrdersController(deliveryOrdersService service.DeliveryOrdersSer
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (u *DeliveryOrdersController) GetAll(c *fiber.Ctx) error {
|
func (u *DeliveryOrdersController) GetAll(c *fiber.Ctx) error {
|
||||||
parseUintListParam := func(param string) ([]uint, error) {
|
|
||||||
if param == "" {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
parts := strings.Split(param, ",")
|
|
||||||
ids := make([]uint, 0, len(parts))
|
|
||||||
for _, part := range parts {
|
|
||||||
trimmed := strings.TrimSpace(part)
|
|
||||||
if trimmed == "" {
|
|
||||||
return nil, strconv.ErrSyntax
|
|
||||||
}
|
|
||||||
parsed, err := strconv.ParseUint(trimmed, 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
ids = append(ids, uint(parsed))
|
|
||||||
}
|
|
||||||
return ids, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
productIDs, err := parseUintListParam(c.Query("product_ids", ""))
|
|
||||||
if err != nil {
|
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid product_ids")
|
|
||||||
}
|
|
||||||
|
|
||||||
query := &validation.DeliveryOrderQuery{
|
query := &validation.DeliveryOrderQuery{
|
||||||
Page: c.QueryInt("page", 1),
|
Page: c.QueryInt("page", 1),
|
||||||
Limit: c.QueryInt("limit", 10),
|
Limit: c.QueryInt("limit", 10),
|
||||||
Search: strings.TrimSpace(c.Query("search", "")),
|
|
||||||
ProductIDs: productIDs,
|
|
||||||
Status: strings.ReplaceAll(strings.TrimSpace(c.Query("status", "")), "_", " "),
|
|
||||||
CustomerId: uint(c.QueryInt("customer_id", 0)),
|
|
||||||
MarketingId: uint(c.QueryInt("marketing_id", 0)),
|
MarketingId: uint(c.QueryInt("marketing_id", 0)),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
@@ -18,7 +17,6 @@ import (
|
|||||||
type MarketingRelationDTO struct {
|
type MarketingRelationDTO struct {
|
||||||
Id uint `json:"id"`
|
Id uint `json:"id"`
|
||||||
SoNumber string `json:"so_number"`
|
SoNumber string `json:"so_number"`
|
||||||
DoNumber *string `json:"do_number"`
|
|
||||||
SoDate time.Time `json:"so_date"`
|
SoDate time.Time `json:"so_date"`
|
||||||
Notes string `json:"notes,omitempty"`
|
Notes string `json:"notes,omitempty"`
|
||||||
}
|
}
|
||||||
@@ -97,16 +95,9 @@ type DeliveryMarketingProductDTO struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ToMarketingRelationDTO(marketing *entity.Marketing) MarketingRelationDTO {
|
func ToMarketingRelationDTO(marketing *entity.Marketing) MarketingRelationDTO {
|
||||||
var doNumber *string
|
|
||||||
if doNumbers := collectDoNumbers(marketing); len(doNumbers) > 0 {
|
|
||||||
value := doNumbers[0]
|
|
||||||
doNumber = &value
|
|
||||||
}
|
|
||||||
|
|
||||||
return MarketingRelationDTO{
|
return MarketingRelationDTO{
|
||||||
Id: marketing.Id,
|
Id: marketing.Id,
|
||||||
SoNumber: marketing.SoNumber,
|
SoNumber: marketing.SoNumber,
|
||||||
DoNumber: doNumber,
|
|
||||||
SoDate: marketing.SoDate,
|
SoDate: marketing.SoDate,
|
||||||
Notes: marketing.Notes,
|
Notes: marketing.Notes,
|
||||||
}
|
}
|
||||||
@@ -191,6 +182,7 @@ func ToMarketingListDTO(marketing *entity.Marketing, deliveryProducts []entity.M
|
|||||||
salesOrderProducts[i] = ToDeliveryMarketingProductDTO(product, marketing.MarketingType)
|
salesOrderProducts[i] = ToDeliveryMarketingProductDTO(product, marketing.MarketingType)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return MarketingListDTO{
|
return MarketingListDTO{
|
||||||
MarketingRelationDTO: ToMarketingRelationDTO(marketing),
|
MarketingRelationDTO: ToMarketingRelationDTO(marketing),
|
||||||
Customer: customer,
|
Customer: customer,
|
||||||
@@ -247,6 +239,7 @@ func ToMarketingDetailDTO(marketing *entity.Marketing, deliveryProducts []entity
|
|||||||
mapped := approvalDTO.ToApprovalDTO(*marketing.LatestApproval)
|
mapped := approvalDTO.ToApprovalDTO(*marketing.LatestApproval)
|
||||||
latestApproval = mapped
|
latestApproval = mapped
|
||||||
}
|
}
|
||||||
|
|
||||||
return MarketingDetailDTO{
|
return MarketingDetailDTO{
|
||||||
MarketingRelationDTO: ToMarketingRelationDTO(marketing),
|
MarketingRelationDTO: ToMarketingRelationDTO(marketing),
|
||||||
SoDocs: marketing.SoDocs,
|
SoDocs: marketing.SoDocs,
|
||||||
@@ -353,46 +346,11 @@ func groupDeliveryProducts(products []MarketingDeliveryProductDTO, soNumber stri
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GenerateDeliveryOrderNumber(soNumber string, deliveryDate *time.Time, warehouseId uint) string {
|
func GenerateDeliveryOrderNumber(soNumber string, deliveryDate *time.Time, warehouseId uint) string {
|
||||||
numberPrefix := soNumber
|
dateStr := ""
|
||||||
if strings.HasPrefix(strings.ToUpper(strings.TrimSpace(soNumber)), "SO-") {
|
if deliveryDate != nil {
|
||||||
numberPrefix = "DO-" + soNumber[3:]
|
dateStr = deliveryDate.Format("20060102")
|
||||||
}
|
}
|
||||||
return numberPrefix
|
return fmt.Sprintf("%s-%s-%d", soNumber, dateStr, warehouseId)
|
||||||
}
|
|
||||||
|
|
||||||
func collectDoNumbers(marketing *entity.Marketing) []string {
|
|
||||||
if marketing == nil || len(marketing.Products) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
seen := make(map[string]struct{})
|
|
||||||
for _, product := range marketing.Products {
|
|
||||||
if product.DeliveryProduct == nil || product.DeliveryProduct.DeliveryDate == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
warehouseID := product.ProductWarehouse.WarehouseId
|
|
||||||
if warehouseID == 0 && product.ProductWarehouse.Warehouse.Id != 0 {
|
|
||||||
warehouseID = product.ProductWarehouse.Warehouse.Id
|
|
||||||
}
|
|
||||||
if warehouseID == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
doNumber := GenerateDeliveryOrderNumber(marketing.SoNumber, product.DeliveryProduct.DeliveryDate, warehouseID)
|
|
||||||
if doNumber != "" {
|
|
||||||
seen[doNumber] = struct{}{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(seen) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
result := make([]string, 0, len(seen))
|
|
||||||
for value := range seen {
|
|
||||||
result = append(result, value)
|
|
||||||
}
|
|
||||||
sort.Strings(result)
|
|
||||||
return result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getVehicleNumber(e entity.MarketingProduct) string {
|
func getVehicleNumber(e entity.MarketingProduct) string {
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||||
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
@@ -116,83 +116,6 @@ func (s deliveryOrdersService) GetAll(c *fiber.Ctx, params *validation.DeliveryO
|
|||||||
Preload("Products.ProductWarehouse.Warehouse").
|
Preload("Products.ProductWarehouse.Warehouse").
|
||||||
Preload("Products.DeliveryProduct")
|
Preload("Products.DeliveryProduct")
|
||||||
|
|
||||||
if params.Status != "" {
|
|
||||||
status := strings.TrimSpace(params.Status)
|
|
||||||
latestApprovalSubQuery := s.MarketingRepo.DB().
|
|
||||||
WithContext(c.Context()).
|
|
||||||
Table("approvals").
|
|
||||||
Select("DISTINCT ON (approvable_id) approvable_id, step_name, action").
|
|
||||||
Where("approvable_type = ?", utils.ApprovalWorkflowMarketing.String()).
|
|
||||||
Order("approvable_id, id DESC")
|
|
||||||
|
|
||||||
if strings.EqualFold(status, "DITOLAK") {
|
|
||||||
db = db.Where(`EXISTS (
|
|
||||||
SELECT 1
|
|
||||||
FROM (?) AS latest_approval
|
|
||||||
WHERE latest_approval.approvable_id = marketings.id
|
|
||||||
AND latest_approval.action = ?
|
|
||||||
)`, latestApprovalSubQuery, string(entity.ApprovalActionRejected))
|
|
||||||
} else {
|
|
||||||
db = db.Where(`EXISTS (
|
|
||||||
SELECT 1
|
|
||||||
FROM (?) AS latest_approval
|
|
||||||
WHERE latest_approval.approvable_id = marketings.id
|
|
||||||
AND LOWER(latest_approval.step_name) = LOWER(?)
|
|
||||||
AND (latest_approval.action IS NULL OR latest_approval.action <> ?)
|
|
||||||
)`, latestApprovalSubQuery, status, string(entity.ApprovalActionRejected))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if params.Search != "" {
|
|
||||||
searchPattern := "%" + params.Search + "%"
|
|
||||||
db = db.Where(`(
|
|
||||||
marketings.so_number ILIKE ? OR
|
|
||||||
EXISTS (
|
|
||||||
SELECT 1
|
|
||||||
FROM customers c
|
|
||||||
WHERE c.id = marketings.customer_id
|
|
||||||
AND c.name ILIKE ?
|
|
||||||
) OR
|
|
||||||
EXISTS (
|
|
||||||
SELECT 1
|
|
||||||
FROM users su
|
|
||||||
WHERE su.id = marketings.sales_person_id
|
|
||||||
AND su.name ILIKE ?
|
|
||||||
) OR
|
|
||||||
EXISTS (
|
|
||||||
SELECT 1
|
|
||||||
FROM marketing_products mp
|
|
||||||
JOIN product_warehouses pw ON pw.id = mp.product_warehouse_id
|
|
||||||
JOIN products p ON p.id = pw.product_id
|
|
||||||
WHERE mp.marketing_id = marketings.id
|
|
||||||
AND p.name ILIKE ?
|
|
||||||
) OR
|
|
||||||
EXISTS (
|
|
||||||
SELECT 1
|
|
||||||
FROM marketing_products mp
|
|
||||||
JOIN product_warehouses pw ON pw.id = mp.product_warehouse_id
|
|
||||||
JOIN warehouses w ON w.id = pw.warehouse_id
|
|
||||||
WHERE mp.marketing_id = marketings.id
|
|
||||||
AND w.name ILIKE ?
|
|
||||||
)
|
|
||||||
)`, searchPattern, searchPattern, searchPattern, searchPattern, searchPattern)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(params.ProductIDs) > 0 {
|
|
||||||
db = db.Where(`EXISTS (
|
|
||||||
SELECT 1
|
|
||||||
FROM marketing_products mp
|
|
||||||
JOIN product_warehouses pw ON pw.id = mp.product_warehouse_id
|
|
||||||
JOIN products p ON p.id = pw.product_id
|
|
||||||
WHERE mp.marketing_id = marketings.id
|
|
||||||
AND p.id IN ?
|
|
||||||
)`, params.ProductIDs)
|
|
||||||
}
|
|
||||||
|
|
||||||
if params.CustomerId != 0 {
|
|
||||||
db = db.Where("marketings.customer_id = ?", params.CustomerId)
|
|
||||||
}
|
|
||||||
|
|
||||||
if scope.Restrict {
|
if scope.Restrict {
|
||||||
if len(scope.IDs) == 0 {
|
if len(scope.IDs) == 0 {
|
||||||
return db.Where("1 = 0")
|
return db.Where("1 = 0")
|
||||||
|
|||||||
@@ -152,31 +152,6 @@ func (s *salesOrdersService) CreateOne(c *fiber.Ctx, req *validation.Create) (*e
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
requestedByWarehouse := make(map[uint]float64)
|
|
||||||
for _, item := range req.MarketingProducts {
|
|
||||||
if item.ProductWarehouseId == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
requestedByWarehouse[item.ProductWarehouseId] += item.Qty
|
|
||||||
}
|
|
||||||
|
|
||||||
for pwID, requestedQty := range requestedByWarehouse {
|
|
||||||
productWarehouse, err := s.ProductWarehouseRepo.GetDetailByID(c.Context(), pwID)
|
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
||||||
return nil, fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Product warehouse %d not found", pwID))
|
|
||||||
}
|
|
||||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check stock availability")
|
|
||||||
}
|
|
||||||
availableQty := productWarehouse.Quantity
|
|
||||||
if availableQty+1e-6 < requestedQty {
|
|
||||||
return nil, fiber.NewError(
|
|
||||||
fiber.StatusBadRequest,
|
|
||||||
fmt.Sprintf("Stok tidak mencukupi untuk gudang %d: diminta %.3f, tersedia %.3f", pwID, requestedQty, availableQty),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
soDate, err := utils.ParseDateString(req.Date)
|
soDate, err := utils.ParseDateString(req.Date)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid date format")
|
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid date format")
|
||||||
|
|||||||
@@ -19,13 +19,9 @@ type DeliveryOrderUpdate struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type DeliveryOrderQuery struct {
|
type DeliveryOrderQuery struct {
|
||||||
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
||||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
|
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
|
||||||
Search string `query:"search" validate:"omitempty,max=100"`
|
MarketingId uint `query:"marketing_id" validate:"omitempty,gt=0"`
|
||||||
ProductIDs []uint `query:"product_ids" validate:"omitempty,dive,gt=0"`
|
|
||||||
Status string `query:"status" validate:"omitempty,max=50"`
|
|
||||||
CustomerId uint `query:"customer_id" validate:"omitempty,gt=0"`
|
|
||||||
MarketingId uint `query:"marketing_id" validate:"omitempty,gt=0"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type DeliveryOrderApprove struct {
|
type DeliveryOrderApprove struct {
|
||||||
|
|||||||
@@ -52,22 +52,7 @@ func (s productCategoryService) GetAll(c *fiber.Ctx, params *validation.Query) (
|
|||||||
productCategories, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
productCategories, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
||||||
db = s.withRelations(db)
|
db = s.withRelations(db)
|
||||||
if params.Search != "" {
|
if params.Search != "" {
|
||||||
terms := splitSearchTerms(params.Search)
|
return db.Where("name ILIKE ?", "%"+params.Search+"%")
|
||||||
if len(terms) == 0 {
|
|
||||||
return db
|
|
||||||
}
|
|
||||||
if len(terms) == 1 {
|
|
||||||
return db.Where("name ILIKE ?", "%"+terms[0]+"%")
|
|
||||||
}
|
|
||||||
for i, term := range terms {
|
|
||||||
like := "%" + term + "%"
|
|
||||||
if i == 0 {
|
|
||||||
db = db.Where("name ILIKE ?", like)
|
|
||||||
} else {
|
|
||||||
db = db.Or("name ILIKE ?", like)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return db
|
|
||||||
}
|
}
|
||||||
return db.Order("created_at DESC").Order("updated_at DESC")
|
return db.Order("created_at DESC").Order("updated_at DESC")
|
||||||
})
|
})
|
||||||
@@ -79,20 +64,6 @@ func (s productCategoryService) GetAll(c *fiber.Ctx, params *validation.Query) (
|
|||||||
return productCategories, total, nil
|
return productCategories, total, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func splitSearchTerms(raw string) []string {
|
|
||||||
parts := strings.FieldsFunc(raw, func(r rune) bool {
|
|
||||||
return r == ',' || r == ';' || r == '|'
|
|
||||||
})
|
|
||||||
terms := make([]string, 0, len(parts))
|
|
||||||
for _, part := range parts {
|
|
||||||
trimmed := strings.TrimSpace(part)
|
|
||||||
if trimmed != "" {
|
|
||||||
terms = append(terms, trimmed)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return terms
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s productCategoryService) GetOne(c *fiber.Ctx, id uint) (*entity.ProductCategory, error) {
|
func (s productCategoryService) GetOne(c *fiber.Ctx, id uint) (*entity.ProductCategory, error) {
|
||||||
productCategory, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
|
productCategory, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
|||||||
@@ -36,7 +36,6 @@ type ChickinService interface {
|
|||||||
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.ProjectChickin, error)
|
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.ProjectChickin, error)
|
||||||
DeleteOne(ctx *fiber.Ctx, id uint) error
|
DeleteOne(ctx *fiber.Ctx, id uint) error
|
||||||
Approval(ctx *fiber.Ctx, req *validation.Approve) ([]entity.ProjectChickin, error)
|
Approval(ctx *fiber.Ctx, req *validation.Approve) ([]entity.ProjectChickin, error)
|
||||||
EnsureChickInExists(ctx context.Context, projectFlockKandangID uint) error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type chickinService struct {
|
type chickinService struct {
|
||||||
@@ -732,30 +731,6 @@ func (s *chickinService) ReleaseChickinStocks(ctx context.Context, tx *gorm.DB,
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s chickinService) EnsureChickInExists(ctx context.Context, projectFlockKandangID uint) error {
|
|
||||||
if projectFlockKandangID == 0 {
|
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "Project flock kandang tidak valid")
|
|
||||||
}
|
|
||||||
|
|
||||||
populations, err := s.ProjectflockPopulationRepo.GetByProjectFlockKandangID(ctx, projectFlockKandangID)
|
|
||||||
if err != nil {
|
|
||||||
s.Log.Errorf("Failed to check project flock population for project_flock_kandang_id=%d: %+v", projectFlockKandangID, err)
|
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, "Gagal memeriksa data chick in")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(populations) == 0 {
|
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "Project flock belum memiliki chick in yang disetujui sehingga belum dapat membuat recording")
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, population := range populations {
|
|
||||||
if population.TotalQty > 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "Chick in project flock belum disetujui sehingga belum dapat membuat recording")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *chickinService) adjustProductWarehouseQuantities(ctx context.Context, tx *gorm.DB, deltas map[uint]float64) error {
|
func (s *chickinService) adjustProductWarehouseQuantities(ctx context.Context, tx *gorm.DB, deltas map[uint]float64) error {
|
||||||
if len(deltas) == 0 {
|
if len(deltas) == 0 {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
+18
-16
@@ -308,23 +308,25 @@ func (s projectFlockKandangService) CheckClosing(c *fiber.Ctx, id uint) (*Closin
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, pw := range productWarehouses {
|
for _, pw := range productWarehouses {
|
||||||
category := ""
|
if pw.Quantity > 0 {
|
||||||
if pw.Product.ProductCategory.Id != 0 {
|
category := ""
|
||||||
category = pw.Product.ProductCategory.Name
|
if pw.Product.ProductCategory.Id != 0 {
|
||||||
|
category = pw.Product.ProductCategory.Name
|
||||||
|
}
|
||||||
|
uomName := ""
|
||||||
|
if pw.Product.Uom.Id != 0 {
|
||||||
|
uomName = pw.Product.Uom.Name
|
||||||
|
}
|
||||||
|
stockRemain = append(stockRemain, StockRemainingDetail{
|
||||||
|
FlagName: string(flagName),
|
||||||
|
ProductWarehouseId: pw.Id,
|
||||||
|
ProductId: pw.ProductId,
|
||||||
|
ProductName: pw.Product.Name,
|
||||||
|
ProductCategory: category,
|
||||||
|
Uom: uomName,
|
||||||
|
Quantity: pw.Quantity,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
uomName := ""
|
|
||||||
if pw.Product.Uom.Id != 0 {
|
|
||||||
uomName = pw.Product.Uom.Name
|
|
||||||
}
|
|
||||||
stockRemain = append(stockRemain, StockRemainingDetail{
|
|
||||||
FlagName: string(flagName),
|
|
||||||
ProductWarehouseId: pw.Id,
|
|
||||||
ProductId: pw.ProductId,
|
|
||||||
ProductName: pw.Product.Name,
|
|
||||||
ProductCategory: category,
|
|
||||||
Uom: uomName,
|
|
||||||
Quantity: pw.Quantity,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
-12
@@ -13,7 +13,6 @@ import (
|
|||||||
|
|
||||||
type ProjectFlockKandangRepository interface {
|
type ProjectFlockKandangRepository interface {
|
||||||
GetByID(ctx context.Context, id uint) (*entity.ProjectFlockKandang, error)
|
GetByID(ctx context.Context, id uint) (*entity.ProjectFlockKandang, error)
|
||||||
GetByIDLight(ctx context.Context, id uint) (*entity.ProjectFlockKandang, error)
|
|
||||||
GetByProjectFlockAndKandang(ctx context.Context, projectFlockID uint, kandangID uint) (*entity.ProjectFlockKandang, error)
|
GetByProjectFlockAndKandang(ctx context.Context, projectFlockID uint, kandangID uint) (*entity.ProjectFlockKandang, error)
|
||||||
GetActiveByKandangID(ctx context.Context, kandangID uint) (*entity.ProjectFlockKandang, error)
|
GetActiveByKandangID(ctx context.Context, kandangID uint) (*entity.ProjectFlockKandang, error)
|
||||||
UpdateClosedAt(ctx context.Context, id uint, t *time.Time) error
|
UpdateClosedAt(ctx context.Context, id uint, t *time.Time) error
|
||||||
@@ -343,17 +342,6 @@ func (r *projectFlockKandangRepositoryImpl) GetByID(ctx context.Context, id uint
|
|||||||
return record, nil
|
return record, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetByIDLight loads only the minimal relations needed for recording flows.
|
|
||||||
func (r *projectFlockKandangRepositoryImpl) GetByIDLight(ctx context.Context, id uint) (*entity.ProjectFlockKandang, error) {
|
|
||||||
record := new(entity.ProjectFlockKandang)
|
|
||||||
if err := r.db.WithContext(ctx).
|
|
||||||
Preload("ProjectFlock").
|
|
||||||
First(record, id).Error; err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return record, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *projectFlockKandangRepositoryImpl) GetByProjectFlockAndKandang(ctx context.Context, projectFlockID uint, kandangID uint) (*entity.ProjectFlockKandang, error) {
|
func (r *projectFlockKandangRepositoryImpl) GetByProjectFlockAndKandang(ctx context.Context, projectFlockID uint, kandangID uint) (*entity.ProjectFlockKandang, error) {
|
||||||
record := new(entity.ProjectFlockKandang)
|
record := new(entity.ProjectFlockKandang)
|
||||||
if err := r.db.WithContext(ctx).
|
if err := r.db.WithContext(ctx).
|
||||||
|
|||||||
@@ -48,7 +48,6 @@ type ProjectflockService interface {
|
|||||||
GetProjectPeriods(ctx *fiber.Ctx, projectIDs []uint) (map[uint]int, error)
|
GetProjectPeriods(ctx *fiber.Ctx, projectIDs []uint) (map[uint]int, error)
|
||||||
Approval(ctx *fiber.Ctx, req *validation.Approve) ([]entity.ProjectFlock, error)
|
Approval(ctx *fiber.Ctx, req *validation.Approve) ([]entity.ProjectFlock, error)
|
||||||
Resubmit(ctx *fiber.Ctx, req *validation.Resubmit, id uint) (*entity.ProjectFlock, error)
|
Resubmit(ctx *fiber.Ctx, req *validation.Resubmit, id uint) (*entity.ProjectFlock, error)
|
||||||
EnsureProjectFlockApproved(ctx context.Context, projectFlockID uint) error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type projectflockService struct {
|
type projectflockService struct {
|
||||||
@@ -113,32 +112,6 @@ func (s projectflockService) approvalQueryModifier() func(*gorm.DB) *gorm.DB {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s projectflockService) EnsureProjectFlockApproved(ctx context.Context, projectFlockID uint) error {
|
|
||||||
if projectFlockID == 0 {
|
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "Project flock kandang tidak valid")
|
|
||||||
}
|
|
||||||
|
|
||||||
approvalSvc := s.ApprovalSvc
|
|
||||||
if approvalSvc == nil {
|
|
||||||
approvalSvc = commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(s.Repository.DB()))
|
|
||||||
}
|
|
||||||
|
|
||||||
latest, err := approvalSvc.LatestByTarget(ctx, s.approvalWorkflow, projectFlockID, nil)
|
|
||||||
if err != nil {
|
|
||||||
s.Log.Errorf("Failed to check project flock %d approval status: %+v", projectFlockID, err)
|
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, "Gagal memeriksa status project flock")
|
|
||||||
}
|
|
||||||
|
|
||||||
if latest == nil {
|
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "Project flock masih dalam status pengajuan sehingga belum dapat membuat recording")
|
|
||||||
}
|
|
||||||
if latest.StepNumber != uint16(utils.ProjectFlockStepAktif) || latest.Action == nil || *latest.Action != entity.ApprovalActionApproved {
|
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "Project flock masih dalam status pengajuan sehingga belum dapat membuat recording")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s projectflockService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.ProjectFlock, int64, map[uint]*flockDTO.FlockRelationDTO, error) {
|
func (s projectflockService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.ProjectFlock, int64, map[uint]*flockDTO.FlockRelationDTO, error) {
|
||||||
if err := s.Validate.Struct(params); err != nil {
|
if err := s.Validate.Struct(params); err != nil {
|
||||||
return nil, 0, nil, err
|
return nil, 0, nil, err
|
||||||
@@ -486,15 +459,6 @@ func (s projectflockService) GetProjectFlockKandangPopulation(ctx *fiber.Ctx, pr
|
|||||||
return 0, fiber.NewError(fiber.StatusBadRequest, "project_flock_kandang_id is required")
|
return 0, fiber.NewError(fiber.StatusBadRequest, "project_flock_kandang_id is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
total, err := s.PopulationRepo.GetAvailableQtyByProjectFlockKandangID(ctx.Context(), projectFlockKandangID)
|
|
||||||
if err != nil {
|
|
||||||
s.Log.Errorf("Failed to fetch project flock kandang population %d: %+v", projectFlockKandangID, err)
|
|
||||||
return 0, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch project flock kandang population")
|
|
||||||
}
|
|
||||||
if total > 0 {
|
|
||||||
return total, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if s.RecordingRepo != nil {
|
if s.RecordingRepo != nil {
|
||||||
latest, err := s.RecordingRepo.GetLatestByProjectFlockKandangID(ctx.Context(), projectFlockKandangID)
|
latest, err := s.RecordingRepo.GetLatestByProjectFlockKandangID(ctx.Context(), projectFlockKandangID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -506,6 +470,12 @@ func (s projectflockService) GetProjectFlockKandangPopulation(ctx *fiber.Ctx, pr
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
total, err := s.PopulationRepo.GetAvailableQtyByProjectFlockKandangID(ctx.Context(), projectFlockKandangID)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Errorf("Failed to fetch project flock kandang population %d: %+v", projectFlockKandangID, err)
|
||||||
|
return 0, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch project flock kandang population")
|
||||||
|
}
|
||||||
|
|
||||||
return total, nil
|
return total, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -580,22 +550,21 @@ func (s projectflockService) GetProjectFlockKandangByParams(ctx *fiber.Ctx, idSt
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s projectflockService) GetAvailableDocQuantity(ctx *fiber.Ctx, kandangID uint) (float64, error) {
|
func (s projectflockService) GetAvailableDocQuantity(ctx *fiber.Ctx, kandangID uint) (float64, error) {
|
||||||
if s.PopulationRepo == nil {
|
|
||||||
return 0, fiber.NewError(fiber.StatusInternalServerError, "Project flock population repository is not configured")
|
|
||||||
}
|
|
||||||
|
|
||||||
pfk, err := s.PivotRepo.GetActiveByKandangID(ctx.Context(), kandangID)
|
wh, err := s.WarehouseRepo.GetByKandangID(ctx.Context(), kandangID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
||||||
return 0, fiber.NewError(fiber.StatusNotFound, "ProjectFlockKandang not found")
|
|
||||||
}
|
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
total, err := s.PopulationRepo.GetAvailableQtyByProjectFlockKandangID(ctx.Context(), pfk.Id)
|
productWarehouses, err := s.ProductWarehouseRepo.GetByCategoryCodeAndWarehouseID(ctx.Context(), "DOC", wh.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
total := 0.0
|
||||||
|
for _, pw := range productWarehouses {
|
||||||
|
total += pw.Quantity
|
||||||
|
}
|
||||||
return total, nil
|
return total, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,14 +27,9 @@ func NewRecordingController(recordingService service.RecordingService) *Recordin
|
|||||||
func (u *RecordingController) GetAll(c *fiber.Ctx) error {
|
func (u *RecordingController) GetAll(c *fiber.Ctx) error {
|
||||||
projectFlockID := c.QueryInt("project_flock_kandang_id", 0)
|
projectFlockID := c.QueryInt("project_flock_kandang_id", 0)
|
||||||
|
|
||||||
page := c.QueryInt("page", 1)
|
|
||||||
limit := c.QueryInt("limit", 10)
|
|
||||||
offset := (page - 1) * limit
|
|
||||||
|
|
||||||
query := &validation.Query{
|
query := &validation.Query{
|
||||||
Page: page,
|
Page: c.QueryInt("page", 1),
|
||||||
Limit: limit,
|
Limit: c.QueryInt("limit", 10),
|
||||||
Offset: offset,
|
|
||||||
Search: c.Query("search"),
|
Search: c.Query("search"),
|
||||||
}
|
}
|
||||||
if projectFlockID > 0 {
|
if projectFlockID > 0 {
|
||||||
@@ -84,27 +79,25 @@ func (u *RecordingController) GetOne(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (u *RecordingController) GetNextDay(c *fiber.Ctx) error {
|
func (u *RecordingController) GetNextDay(c *fiber.Ctx) error {
|
||||||
req := new(validation.GetRecordingNextDay)
|
projectFlockID := c.QueryInt("project_flock_kandang_id", 0)
|
||||||
|
if projectFlockID <= 0 {
|
||||||
if err := c.QueryParser(req); err != nil {
|
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid query params")
|
|
||||||
}
|
|
||||||
if req.ProjectFlockKandangId == 0 {
|
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "project_flock_kandang_id is required")
|
return fiber.NewError(fiber.StatusBadRequest, "project_flock_kandang_id is required")
|
||||||
}
|
}
|
||||||
if req.RecordTime == nil || strings.TrimSpace(*req.RecordTime) == "" {
|
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "record_date is required")
|
recordTime := time.Now().UTC()
|
||||||
|
if recordDate := strings.TrimSpace(c.Query("record_date")); recordDate != "" {
|
||||||
|
parsed, err := time.Parse("2006-01-02", recordDate)
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "record_date must be in YYYY-MM-DD format")
|
||||||
|
}
|
||||||
|
recordTime = parsed.UTC()
|
||||||
}
|
}
|
||||||
recordTime, err := time.Parse("2006-01-02", strings.TrimSpace(*req.RecordTime))
|
|
||||||
if err != nil {
|
nextDay, err := u.RecordingService.GetNextDay(c, uint(projectFlockID), recordTime)
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "record_time must be in YYYY-MM-DD format")
|
|
||||||
}
|
|
||||||
req.RecordTimeValue = &recordTime
|
|
||||||
nextDay, err := u.RecordingService.GetNextDay(c, req)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
projectFlockID := req.ProjectFlockKandangId
|
|
||||||
return c.Status(fiber.StatusOK).
|
return c.Status(fiber.StatusOK).
|
||||||
JSON(response.Success{
|
JSON(response.Success{
|
||||||
Code: fiber.StatusOK,
|
Code: fiber.StatusOK,
|
||||||
|
|||||||
@@ -11,17 +11,9 @@ import (
|
|||||||
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||||
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||||
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"
|
||||||
rFlock "gitlab.com/mbugroup/lti-api.git/internal/modules/master/flocks/repositories"
|
|
||||||
rKandang "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/repositories"
|
|
||||||
rNonstock "gitlab.com/mbugroup/lti-api.git/internal/modules/master/nonstocks/repositories"
|
|
||||||
rProductionStandard "gitlab.com/mbugroup/lti-api.git/internal/modules/master/production-standards/repositories"
|
rProductionStandard "gitlab.com/mbugroup/lti-api.git/internal/modules/master/production-standards/repositories"
|
||||||
sProductionStandard "gitlab.com/mbugroup/lti-api.git/internal/modules/master/production-standards/services"
|
sProductionStandard "gitlab.com/mbugroup/lti-api.git/internal/modules/master/production-standards/services"
|
||||||
rProduct "gitlab.com/mbugroup/lti-api.git/internal/modules/master/products/repositories"
|
|
||||||
rWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/repositories"
|
|
||||||
rChickin "gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins/repositories"
|
|
||||||
sChickin "gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins/services"
|
|
||||||
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"
|
||||||
sProjectFlock "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/services"
|
|
||||||
rRecording "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/repositories"
|
rRecording "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/repositories"
|
||||||
sRecording "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/services"
|
sRecording "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/services"
|
||||||
rStockLogs "gitlab.com/mbugroup/lti-api.git/internal/modules/shared/repositories"
|
rStockLogs "gitlab.com/mbugroup/lti-api.git/internal/modules/shared/repositories"
|
||||||
@@ -36,18 +28,9 @@ type RecordingModule struct{}
|
|||||||
|
|
||||||
func (RecordingModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
|
func (RecordingModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
|
||||||
recordingRepo := rRecording.NewRecordingRepository(db)
|
recordingRepo := rRecording.NewRecordingRepository(db)
|
||||||
projectFlockRepo := rProjectFlock.NewProjectflockRepository(db)
|
|
||||||
projectFlockKandangRepo := rProjectFlock.NewProjectFlockKandangRepository(db)
|
projectFlockKandangRepo := rProjectFlock.NewProjectFlockKandangRepository(db)
|
||||||
projectFlockPopulationRepo := rProjectFlock.NewProjectFlockPopulationRepository(db)
|
projectFlockPopulationRepo := rProjectFlock.NewProjectFlockPopulationRepository(db)
|
||||||
projectBudgetRepo := rProjectFlock.NewProjectBudgetRepository(db)
|
|
||||||
productWarehouseRepo := rProductWarehouse.NewProductWarehouseRepository(db)
|
productWarehouseRepo := rProductWarehouse.NewProductWarehouseRepository(db)
|
||||||
flockRepo := rFlock.NewFlockRepository(db)
|
|
||||||
kandangRepo := rKandang.NewKandangRepository(db)
|
|
||||||
warehouseRepo := rWarehouse.NewWarehouseRepository(db)
|
|
||||||
nonstockRepo := rNonstock.NewNonstockRepository(db)
|
|
||||||
productRepo := rProduct.NewProductRepository(db)
|
|
||||||
chickinRepo := rChickin.NewChickinRepository(db)
|
|
||||||
chickinDetailRepo := rChickin.NewChickinDetailRepository(db)
|
|
||||||
stockAllocationRepo := commonRepo.NewStockAllocationRepository(db)
|
stockAllocationRepo := commonRepo.NewStockAllocationRepository(db)
|
||||||
stockLogRepo := rStockLogs.NewStockLogRepository(db)
|
stockLogRepo := rStockLogs.NewStockLogRepository(db)
|
||||||
productionStandardRepo := rProductionStandard.NewProductionStandardRepository(db)
|
productionStandardRepo := rProductionStandard.NewProductionStandardRepository(db)
|
||||||
@@ -70,30 +53,14 @@ func (RecordingModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
|
|||||||
ProductWarehouseID: "product_warehouse_id",
|
ProductWarehouseID: "product_warehouse_id",
|
||||||
TotalQuantity: "total_qty",
|
TotalQuantity: "total_qty",
|
||||||
TotalUsedQuantity: "total_used",
|
TotalUsedQuantity: "total_used",
|
||||||
CreatedAt: "(SELECT r.record_datetime FROM recordings r WHERE r.id = recording_eggs.recording_id)",
|
CreatedAt: "created_at",
|
||||||
},
|
},
|
||||||
OrderBy: []string{"(SELECT r.record_datetime FROM recordings r WHERE r.id = recording_eggs.recording_id) ASC", "id ASC"},
|
OrderBy: []string{"created_at ASC", "id ASC"},
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
if !strings.Contains(strings.ToLower(err.Error()), "already registered") {
|
if !strings.Contains(strings.ToLower(err.Error()), "already registered") {
|
||||||
panic(fmt.Sprintf("failed to register recording egg stockable workflow: %v", err))
|
panic(fmt.Sprintf("failed to register recording egg stockable workflow: %v", err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err := fifoService.RegisterStockable(fifo.StockableConfig{
|
|
||||||
Key: fifo.StockableKeyRecordingDepletion,
|
|
||||||
Table: "recording_depletions",
|
|
||||||
Columns: fifo.StockableColumns{
|
|
||||||
ID: "id",
|
|
||||||
ProductWarehouseID: "product_warehouse_id",
|
|
||||||
TotalQuantity: "qty",
|
|
||||||
TotalUsedQuantity: "total_used_qty",
|
|
||||||
CreatedAt: "(SELECT r.record_datetime FROM recordings r WHERE r.id = recording_depletions.recording_id)",
|
|
||||||
},
|
|
||||||
OrderBy: []string{"(SELECT r.record_datetime FROM recordings r WHERE r.id = recording_depletions.recording_id) ASC", "id ASC"},
|
|
||||||
}); err != nil {
|
|
||||||
if !strings.Contains(strings.ToLower(err.Error()), "already registered") {
|
|
||||||
panic(fmt.Sprintf("failed to register recording depletion stockable workflow: %v", err))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err := fifoService.RegisterUsable(fifo.UsableConfig{
|
if err := fifoService.RegisterUsable(fifo.UsableConfig{
|
||||||
Key: fifo.UsableKeyRecordingStock,
|
Key: fifo.UsableKeyRecordingStock,
|
||||||
Table: "recording_stocks",
|
Table: "recording_stocks",
|
||||||
@@ -102,7 +69,7 @@ func (RecordingModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
|
|||||||
ProductWarehouseID: "product_warehouse_id",
|
ProductWarehouseID: "product_warehouse_id",
|
||||||
UsageQuantity: "usage_qty",
|
UsageQuantity: "usage_qty",
|
||||||
PendingQuantity: "pending_qty",
|
PendingQuantity: "pending_qty",
|
||||||
CreatedAt: "(SELECT r.record_datetime FROM recordings r WHERE r.id = recording_stocks.recording_id)",
|
CreatedAt: "id",
|
||||||
},
|
},
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
if !strings.Contains(strings.ToLower(err.Error()), "already registered") {
|
if !strings.Contains(strings.ToLower(err.Error()), "already registered") {
|
||||||
@@ -115,9 +82,9 @@ func (RecordingModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
|
|||||||
Columns: fifo.UsableColumns{
|
Columns: fifo.UsableColumns{
|
||||||
ID: "id",
|
ID: "id",
|
||||||
ProductWarehouseID: "source_product_warehouse_id",
|
ProductWarehouseID: "source_product_warehouse_id",
|
||||||
UsageQuantity: "usage_qty",
|
UsageQuantity: "qty",
|
||||||
PendingQuantity: "pending_qty",
|
PendingQuantity: "pending_qty",
|
||||||
CreatedAt: "(SELECT r.record_datetime FROM recordings r WHERE r.id = recording_depletions.recording_id)",
|
CreatedAt: "id",
|
||||||
},
|
},
|
||||||
ExcludedStockables: []fifo.StockableKey{
|
ExcludedStockables: []fifo.StockableKey{
|
||||||
fifo.StockableKeyTransferToLayingIn,
|
fifo.StockableKeyTransferToLayingIn,
|
||||||
@@ -137,41 +104,9 @@ func (RecordingModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
|
|||||||
if err := approvalService.RegisterWorkflowSteps(utils.ApprovalWorkflowRecording, utils.RecordingApprovalSteps); err != nil {
|
if err := approvalService.RegisterWorkflowSteps(utils.ApprovalWorkflowRecording, utils.RecordingApprovalSteps); err != nil {
|
||||||
panic(fmt.Sprintf("failed to register recording approval workflow: %v", err))
|
panic(fmt.Sprintf("failed to register recording approval workflow: %v", err))
|
||||||
}
|
}
|
||||||
if err := approvalService.RegisterWorkflowSteps(utils.ApprovalWorkflowProjectFlock, utils.ProjectFlockApprovalSteps); err != nil {
|
|
||||||
panic(fmt.Sprintf("failed to register project flock approval workflow: %v", err))
|
|
||||||
}
|
|
||||||
|
|
||||||
userRepo := rUser.NewUserRepository(db)
|
userRepo := rUser.NewUserRepository(db)
|
||||||
|
|
||||||
projectFlockService := sProjectFlock.NewProjectflockService(
|
|
||||||
projectFlockRepo,
|
|
||||||
flockRepo,
|
|
||||||
kandangRepo,
|
|
||||||
projectFlockKandangRepo,
|
|
||||||
warehouseRepo,
|
|
||||||
productWarehouseRepo,
|
|
||||||
projectBudgetRepo,
|
|
||||||
nonstockRepo,
|
|
||||||
projectFlockPopulationRepo,
|
|
||||||
recordingRepo,
|
|
||||||
approvalService,
|
|
||||||
validate,
|
|
||||||
)
|
|
||||||
|
|
||||||
chickinService := sChickin.NewChickinService(
|
|
||||||
chickinRepo,
|
|
||||||
kandangRepo,
|
|
||||||
warehouseRepo,
|
|
||||||
productWarehouseRepo,
|
|
||||||
productRepo,
|
|
||||||
projectFlockRepo,
|
|
||||||
projectFlockKandangRepo,
|
|
||||||
projectFlockPopulationRepo,
|
|
||||||
chickinDetailRepo,
|
|
||||||
validate,
|
|
||||||
fifoService,
|
|
||||||
)
|
|
||||||
|
|
||||||
recordingService := sRecording.NewRecordingService(
|
recordingService := sRecording.NewRecordingService(
|
||||||
recordingRepo,
|
recordingRepo,
|
||||||
projectFlockKandangRepo,
|
projectFlockKandangRepo,
|
||||||
@@ -182,8 +117,6 @@ func (RecordingModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
|
|||||||
fifoService,
|
fifoService,
|
||||||
stockLogRepo,
|
stockLogRepo,
|
||||||
productionStandardService,
|
productionStandardService,
|
||||||
projectFlockService,
|
|
||||||
chickinService,
|
|
||||||
validate,
|
validate,
|
||||||
)
|
)
|
||||||
userService := sUser.NewUserService(userRepo, validate)
|
userService := sUser.NewUserService(userRepo, validate)
|
||||||
|
|||||||
@@ -17,13 +17,8 @@ type RecordingRepository interface {
|
|||||||
repository.BaseRepository[entity.Recording]
|
repository.BaseRepository[entity.Recording]
|
||||||
|
|
||||||
WithRelations(db *gorm.DB) *gorm.DB
|
WithRelations(db *gorm.DB) *gorm.DB
|
||||||
WithRelationsList(db *gorm.DB) *gorm.DB
|
|
||||||
ApplyListFilters(db *gorm.DB, search string, projectFlockKandangId uint) *gorm.DB
|
|
||||||
ApplyListCountFilters(db *gorm.DB, search string, projectFlockKandangId uint) *gorm.DB
|
|
||||||
ApplySearchFilters(db *gorm.DB, rawSearch string) *gorm.DB
|
ApplySearchFilters(db *gorm.DB, rawSearch string) *gorm.DB
|
||||||
GetAllWithFilters(ctx context.Context, offset, limit int, search string, projectFlockKandangId uint, modifier func(*gorm.DB) *gorm.DB) ([]entity.Recording, int64, error)
|
|
||||||
GetLatestByProjectFlockKandangID(ctx context.Context, projectFlockKandangId uint) (*entity.Recording, error)
|
GetLatestByProjectFlockKandangID(ctx context.Context, projectFlockKandangId uint) (*entity.Recording, error)
|
||||||
ListByProjectFlockKandangID(ctx context.Context, tx *gorm.DB, projectFlockKandangId uint, from *time.Time) ([]entity.Recording, error)
|
|
||||||
GenerateNextDay(tx *gorm.DB, projectFlockKandangId uint) (int, error)
|
GenerateNextDay(tx *gorm.DB, projectFlockKandangId uint) (int, error)
|
||||||
|
|
||||||
CreateStocks(tx *gorm.DB, stocks []entity.RecordingStock) error
|
CreateStocks(tx *gorm.DB, stocks []entity.RecordingStock) error
|
||||||
@@ -45,13 +40,8 @@ type RecordingRepository interface {
|
|||||||
|
|
||||||
SumRecordingDepletions(tx *gorm.DB, recordingID uint) (float64, error)
|
SumRecordingDepletions(tx *gorm.DB, recordingID uint) (float64, error)
|
||||||
GetCumulativeDepletionByProjectFlockKandangUntil(tx *gorm.DB, projectFlockKandangId uint, recordTime time.Time) (float64, error)
|
GetCumulativeDepletionByProjectFlockKandangUntil(tx *gorm.DB, projectFlockKandangId uint, recordTime time.Time) (float64, error)
|
||||||
GetCumulativeDepletionByRecordingIDs(tx *gorm.DB, recordingIDs []uint) (map[uint]float64, error)
|
|
||||||
GetUniformityMeanBwByWeek(tx *gorm.DB, projectFlockKandangId uint, week int) (float64, bool, error)
|
|
||||||
FindPreviousRecording(tx *gorm.DB, projectFlockKandangId uint, currentDay int) (*entity.Recording, error)
|
FindPreviousRecording(tx *gorm.DB, projectFlockKandangId uint, currentDay int) (*entity.Recording, error)
|
||||||
GetPreviousTotalChickByRecordingIDs(tx *gorm.DB, recordingIDs []uint) (map[uint]*float64, error)
|
|
||||||
GetTotalChick(tx *gorm.DB, projectFlockKandangId uint) (int64, error)
|
GetTotalChick(tx *gorm.DB, projectFlockKandangId uint) (int64, error)
|
||||||
GetRemainingPopulationByProjectFlockKandang(tx *gorm.DB, projectFlockKandangId uint) (float64, error)
|
|
||||||
GetTotalChickByProjectFlockKandangIDs(tx *gorm.DB, projectFlockKandangIds []uint) (map[uint]int64, error)
|
|
||||||
GetTotalChickinByProjectFlockKandang(tx *gorm.DB, projectFlockKandangId 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)
|
||||||
GetEggSummaryByRecording(tx *gorm.DB, recordingID uint) (totalQty float64, totalWeightGrams float64, err error)
|
GetEggSummaryByRecording(tx *gorm.DB, recordingID uint) (totalQty float64, totalWeightGrams float64, err error)
|
||||||
@@ -64,8 +54,6 @@ type RecordingRepository interface {
|
|||||||
GetLatestAvgWeightByProjectFlockID(ctx context.Context, projectFlockID uint) (avgWeight float64, err error)
|
GetLatestAvgWeightByProjectFlockID(ctx context.Context, projectFlockID uint) (avgWeight float64, err error)
|
||||||
GetTotalEggProductionWeightByProjectFlockID(ctx context.Context, projectFlockID uint) (totalWeightKg float64, err error)
|
GetTotalEggProductionWeightByProjectFlockID(ctx context.Context, projectFlockID uint) (totalWeightKg float64, err error)
|
||||||
GetAverageTargetMetricsByProjectFlockKandangID(ctx context.Context, projectFlockKandangID uint, includeTargets bool) (RecordingTargetAverages, error)
|
GetAverageTargetMetricsByProjectFlockKandangID(ctx context.Context, projectFlockKandangID uint, includeTargets bool) (RecordingTargetAverages, error)
|
||||||
ResyncProjectFlockPopulationUsage(ctx context.Context, tx *gorm.DB, projectFlockKandangID uint) error
|
|
||||||
ValidateProductWarehousesByFlags(ctx context.Context, ids []uint, flags []string) (uint, error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type RecordingRepositoryImpl struct {
|
type RecordingRepositoryImpl struct {
|
||||||
@@ -124,64 +112,6 @@ func (r *RecordingRepositoryImpl) WithRelations(db *gorm.DB) *gorm.DB {
|
|||||||
Preload("Eggs.ProductWarehouse.Warehouse.Location")
|
Preload("Eggs.ProductWarehouse.Warehouse.Location")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RecordingRepositoryImpl) WithRelationsList(db *gorm.DB) *gorm.DB {
|
|
||||||
return db.
|
|
||||||
Preload("CreatedUser").
|
|
||||||
Preload("ProjectFlockKandang").
|
|
||||||
Preload("ProjectFlockKandang.Kandang").
|
|
||||||
Preload("ProjectFlockKandang.Kandang.Location").
|
|
||||||
Preload("ProjectFlockKandang.ProjectFlock").
|
|
||||||
Preload("ProjectFlockKandang.ProjectFlock.ProductionStandard")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *RecordingRepositoryImpl) ApplyListFilters(db *gorm.DB, search string, projectFlockKandangId uint) *gorm.DB {
|
|
||||||
db = r.WithRelationsList(db)
|
|
||||||
db = db.
|
|
||||||
Joins("JOIN project_flock_kandangs pfk ON pfk.id = recordings.project_flock_kandangs_id").
|
|
||||||
Joins("JOIN project_flocks pf ON pf.id = pfk.project_flock_id")
|
|
||||||
if projectFlockKandangId != 0 {
|
|
||||||
db = db.Where("recordings.project_flock_kandangs_id = ?", projectFlockKandangId)
|
|
||||||
}
|
|
||||||
db = r.ApplySearchFilters(db, search)
|
|
||||||
return db.Order("recordings.record_datetime DESC").Order("recordings.created_at DESC")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *RecordingRepositoryImpl) ApplyListCountFilters(db *gorm.DB, search string, projectFlockKandangId uint) *gorm.DB {
|
|
||||||
db = db.
|
|
||||||
Joins("JOIN project_flock_kandangs pfk ON pfk.id = recordings.project_flock_kandangs_id").
|
|
||||||
Joins("JOIN project_flocks pf ON pf.id = pfk.project_flock_id")
|
|
||||||
if projectFlockKandangId != 0 {
|
|
||||||
db = db.Where("recordings.project_flock_kandangs_id = ?", projectFlockKandangId)
|
|
||||||
}
|
|
||||||
db = r.ApplySearchFilters(db, search)
|
|
||||||
return db
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *RecordingRepositoryImpl) GetAllWithFilters(ctx context.Context, offset, limit int, search string, projectFlockKandangId uint, modifier func(*gorm.DB) *gorm.DB) ([]entity.Recording, int64, error) {
|
|
||||||
var (
|
|
||||||
records []entity.Recording
|
|
||||||
total int64
|
|
||||||
)
|
|
||||||
|
|
||||||
countQ := r.ApplyListCountFilters(r.DB().WithContext(ctx).Model(&entity.Recording{}), search, projectFlockKandangId)
|
|
||||||
if modifier != nil {
|
|
||||||
countQ = modifier(countQ)
|
|
||||||
}
|
|
||||||
if err := countQ.Count(&total).Error; err != nil {
|
|
||||||
return nil, 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
listQ := r.ApplyListFilters(r.DB().WithContext(ctx).Model(&entity.Recording{}), search, projectFlockKandangId)
|
|
||||||
if modifier != nil {
|
|
||||||
listQ = modifier(listQ)
|
|
||||||
}
|
|
||||||
if err := listQ.Offset(offset).Limit(limit).Find(&records).Error; err != nil {
|
|
||||||
return nil, 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return records, total, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *RecordingRepositoryImpl) ApplySearchFilters(db *gorm.DB, rawSearch string) *gorm.DB {
|
func (r *RecordingRepositoryImpl) ApplySearchFilters(db *gorm.DB, rawSearch string) *gorm.DB {
|
||||||
normalized := strings.ToLower(strings.TrimSpace(rawSearch))
|
normalized := strings.ToLower(strings.TrimSpace(rawSearch))
|
||||||
if normalized == "" {
|
if normalized == "" {
|
||||||
@@ -239,27 +169,6 @@ func (r *RecordingRepositoryImpl) GetLatestByProjectFlockKandangID(ctx context.C
|
|||||||
return &record, nil
|
return &record, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RecordingRepositoryImpl) ListByProjectFlockKandangID(ctx context.Context, tx *gorm.DB, projectFlockKandangId uint, from *time.Time) ([]entity.Recording, error) {
|
|
||||||
if projectFlockKandangId == 0 {
|
|
||||||
return nil, errors.New("project_flock_kandang_id is required")
|
|
||||||
}
|
|
||||||
|
|
||||||
db := tx.WithContext(ctx).
|
|
||||||
Model(&entity.Recording{}).
|
|
||||||
Where("project_flock_kandangs_id = ?", projectFlockKandangId).
|
|
||||||
Where("deleted_at IS NULL")
|
|
||||||
|
|
||||||
if from != nil {
|
|
||||||
db = db.Where("record_datetime >= ?", *from)
|
|
||||||
}
|
|
||||||
|
|
||||||
var records []entity.Recording
|
|
||||||
if err := db.Order("record_datetime ASC").Order("created_at ASC").Find(&records).Error; err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return records, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *RecordingRepositoryImpl) GenerateNextDay(tx *gorm.DB, projectFlockKandangId uint) (int, error) {
|
func (r *RecordingRepositoryImpl) GenerateNextDay(tx *gorm.DB, projectFlockKandangId uint) (int, error) {
|
||||||
var days []int
|
var days []int
|
||||||
if err := tx.Model(&entity.Recording{}).
|
if err := tx.Model(&entity.Recording{}).
|
||||||
@@ -422,68 +331,6 @@ func (r *RecordingRepositoryImpl) GetCumulativeDepletionByProjectFlockKandangUnt
|
|||||||
return total, err
|
return total, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RecordingRepositoryImpl) GetCumulativeDepletionByRecordingIDs(tx *gorm.DB, recordingIDs []uint) (map[uint]float64, error) {
|
|
||||||
result := make(map[uint]float64)
|
|
||||||
if len(recordingIDs) == 0 {
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type row struct {
|
|
||||||
RecordingID uint `gorm:"column:recording_id"`
|
|
||||||
Total float64 `gorm:"column:total_qty"`
|
|
||||||
}
|
|
||||||
var rows []row
|
|
||||||
|
|
||||||
err := tx.
|
|
||||||
Table("recordings r").
|
|
||||||
Select("r.id AS recording_id, COALESCE(SUM(rd.qty), 0) AS total_qty").
|
|
||||||
Joins(`
|
|
||||||
LEFT JOIN recordings r2
|
|
||||||
ON r2.project_flock_kandangs_id = r.project_flock_kandangs_id
|
|
||||||
AND r2.record_datetime <= r.record_datetime
|
|
||||||
AND r2.deleted_at IS NULL`).
|
|
||||||
Joins("LEFT JOIN recording_depletions rd ON rd.recording_id = r2.id").
|
|
||||||
Where("r.id IN ?", recordingIDs).
|
|
||||||
Where("r.deleted_at IS NULL").
|
|
||||||
Group("r.id").
|
|
||||||
Scan(&rows).Error
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, row := range rows {
|
|
||||||
result[row.RecordingID] = row.Total
|
|
||||||
}
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *RecordingRepositoryImpl) GetUniformityMeanBwByWeek(tx *gorm.DB, projectFlockKandangId uint, week int) (float64, bool, error) {
|
|
||||||
if projectFlockKandangId == 0 || week <= 0 {
|
|
||||||
return 0, false, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var row struct {
|
|
||||||
ID uint
|
|
||||||
MeanUp float64
|
|
||||||
}
|
|
||||||
if err := tx.
|
|
||||||
Table("project_flock_kandang_uniformity").
|
|
||||||
Select("id, mean_up").
|
|
||||||
Where("project_flock_kandang_id = ?", projectFlockKandangId).
|
|
||||||
Where("week = ?", week).
|
|
||||||
Order("id DESC").
|
|
||||||
Limit(1).
|
|
||||||
Scan(&row).Error; err != nil {
|
|
||||||
return 0, false, err
|
|
||||||
}
|
|
||||||
if row.ID == 0 {
|
|
||||||
return 0, false, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
meanBw := row.MeanUp / 1.10
|
|
||||||
return meanBw, true, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *RecordingRepositoryImpl) FindPreviousRecording(tx *gorm.DB, projectFlockKandangId uint, currentDay int) (*entity.Recording, error) {
|
func (r *RecordingRepositoryImpl) FindPreviousRecording(tx *gorm.DB, projectFlockKandangId uint, currentDay int) (*entity.Recording, error) {
|
||||||
if currentDay <= 1 {
|
if currentDay <= 1 {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
@@ -506,46 +353,6 @@ func (r *RecordingRepositoryImpl) FindPreviousRecording(tx *gorm.DB, projectFloc
|
|||||||
return &prev, nil
|
return &prev, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RecordingRepositoryImpl) GetPreviousTotalChickByRecordingIDs(tx *gorm.DB, recordingIDs []uint) (map[uint]*float64, error) {
|
|
||||||
result := make(map[uint]*float64)
|
|
||||||
if len(recordingIDs) == 0 {
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type row struct {
|
|
||||||
RecordingID uint `gorm:"column:recording_id"`
|
|
||||||
PrevTotalChickQty *float64 `gorm:"column:prev_total_chick_qty"`
|
|
||||||
}
|
|
||||||
var rows []row
|
|
||||||
|
|
||||||
err := tx.
|
|
||||||
Table("recordings r").
|
|
||||||
Select(`
|
|
||||||
r.id AS recording_id,
|
|
||||||
(
|
|
||||||
SELECT r2.total_chick_qty
|
|
||||||
FROM recordings r2
|
|
||||||
WHERE r2.project_flock_kandangs_id = r.project_flock_kandangs_id
|
|
||||||
AND r2.day IS NOT NULL
|
|
||||||
AND r.day IS NOT NULL
|
|
||||||
AND r2.day < r.day
|
|
||||||
AND r2.deleted_at IS NULL
|
|
||||||
ORDER BY r2.day DESC
|
|
||||||
LIMIT 1
|
|
||||||
) AS prev_total_chick_qty`).
|
|
||||||
Where("r.id IN ?", recordingIDs).
|
|
||||||
Where("r.deleted_at IS NULL").
|
|
||||||
Scan(&rows).Error
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, row := range rows {
|
|
||||||
result[row.RecordingID] = row.PrevTotalChickQty
|
|
||||||
}
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *RecordingRepositoryImpl) GetTotalChick(tx *gorm.DB, projectFlockKandangId uint) (int64, error) {
|
func (r *RecordingRepositoryImpl) GetTotalChick(tx *gorm.DB, projectFlockKandangId uint) (int64, error) {
|
||||||
var total float64
|
var total float64
|
||||||
err := tx.
|
err := tx.
|
||||||
@@ -565,57 +372,6 @@ func (r *RecordingRepositoryImpl) GetTotalChick(tx *gorm.DB, projectFlockKandang
|
|||||||
return int64(math.Round(total)), nil
|
return int64(math.Round(total)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RecordingRepositoryImpl) GetRemainingPopulationByProjectFlockKandang(tx *gorm.DB, projectFlockKandangId uint) (float64, error) {
|
|
||||||
var total float64
|
|
||||||
err := tx.
|
|
||||||
Table("project_flock_populations").
|
|
||||||
Select("COALESCE(SUM(project_flock_populations.total_qty - project_flock_populations.total_used_qty), 0) AS total_qty").
|
|
||||||
Joins("JOIN project_chickins ON project_chickins.id = project_flock_populations.project_chickin_id").
|
|
||||||
Where("project_chickins.project_flock_kandang_id = ?", projectFlockKandangId).
|
|
||||||
Scan(&total).Error
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
if total < 0 {
|
|
||||||
total = 0
|
|
||||||
}
|
|
||||||
return total, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *RecordingRepositoryImpl) GetTotalChickByProjectFlockKandangIDs(tx *gorm.DB, projectFlockKandangIds []uint) (map[uint]int64, error) {
|
|
||||||
result := make(map[uint]int64)
|
|
||||||
if len(projectFlockKandangIds) == 0 {
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type row struct {
|
|
||||||
ProjectFlockKandangId uint `gorm:"column:project_flock_kandang_id"`
|
|
||||||
Total float64 `gorm:"column:total_qty"`
|
|
||||||
}
|
|
||||||
var rows []row
|
|
||||||
|
|
||||||
err := tx.
|
|
||||||
Table("project_flock_populations pfp").
|
|
||||||
Select("project_chickins.project_flock_kandang_id, COALESCE(SUM(pfp.total_qty - pfp.total_used_qty), 0) AS total_qty").
|
|
||||||
Joins("JOIN project_chickins ON project_chickins.id = pfp.project_chickin_id").
|
|
||||||
Where("project_chickins.project_flock_kandang_id IN ?", projectFlockKandangIds).
|
|
||||||
Group("project_chickins.project_flock_kandang_id").
|
|
||||||
Scan(&rows).Error
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, row := range rows {
|
|
||||||
total := math.Round(row.Total)
|
|
||||||
if total < 0 {
|
|
||||||
total = 0
|
|
||||||
}
|
|
||||||
result[row.ProjectFlockKandangId] = int64(total)
|
|
||||||
}
|
|
||||||
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *RecordingRepositoryImpl) GetTotalChickinByProjectFlockKandang(tx *gorm.DB, projectFlockKandangId uint) (float64, error) {
|
func (r *RecordingRepositoryImpl) GetTotalChickinByProjectFlockKandang(tx *gorm.DB, projectFlockKandangId uint) (float64, error) {
|
||||||
if projectFlockKandangId == 0 {
|
if projectFlockKandangId == 0 {
|
||||||
return 0, nil
|
return 0, nil
|
||||||
@@ -801,82 +557,6 @@ func (r *RecordingRepositoryImpl) GetAverageTargetMetricsByProjectFlockKandangID
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RecordingRepositoryImpl) ResyncProjectFlockPopulationUsage(ctx context.Context, tx *gorm.DB, projectFlockKandangID uint) error {
|
|
||||||
if projectFlockKandangID == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
idsSubquery := `
|
|
||||||
SELECT pfp.id
|
|
||||||
FROM project_flock_populations pfp
|
|
||||||
JOIN project_chickins pc ON pc.id = pfp.project_chickin_id
|
|
||||||
WHERE pc.project_flock_kandang_id = ?
|
|
||||||
`
|
|
||||||
|
|
||||||
updateWithAlloc := `
|
|
||||||
UPDATE project_flock_populations p
|
|
||||||
SET total_used_qty = COALESCE(a.used, 0)
|
|
||||||
FROM (
|
|
||||||
SELECT stockable_id, SUM(qty) AS used
|
|
||||||
FROM stock_allocations
|
|
||||||
WHERE stockable_type = 'PROJECT_FLOCK_POPULATION'
|
|
||||||
AND status = 'ACTIVE'
|
|
||||||
GROUP BY stockable_id
|
|
||||||
) a
|
|
||||||
WHERE p.id = a.stockable_id
|
|
||||||
AND p.id IN (` + idsSubquery + `)
|
|
||||||
`
|
|
||||||
|
|
||||||
resetMissing := `
|
|
||||||
UPDATE project_flock_populations p
|
|
||||||
SET total_used_qty = 0
|
|
||||||
WHERE p.id IN (` + idsSubquery + `)
|
|
||||||
AND NOT EXISTS (
|
|
||||||
SELECT 1
|
|
||||||
FROM stock_allocations sa
|
|
||||||
WHERE sa.stockable_type = 'PROJECT_FLOCK_POPULATION'
|
|
||||||
AND sa.status = 'ACTIVE'
|
|
||||||
AND sa.stockable_id = p.id
|
|
||||||
)
|
|
||||||
`
|
|
||||||
|
|
||||||
db := r.DB().WithContext(ctx)
|
|
||||||
if tx != nil {
|
|
||||||
db = tx.WithContext(ctx)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := db.Exec(updateWithAlloc, projectFlockKandangID).Error; err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := db.Exec(resetMissing, projectFlockKandangID).Error; err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *RecordingRepositoryImpl) ValidateProductWarehousesByFlags(ctx context.Context, ids []uint, flags []string) (uint, error) {
|
|
||||||
if len(ids) == 0 {
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
var invalidIDs []uint
|
|
||||||
if err := r.DB().WithContext(ctx).
|
|
||||||
Table("product_warehouses pw").
|
|
||||||
Where("pw.id IN ?", ids).
|
|
||||||
Where(`NOT EXISTS (
|
|
||||||
SELECT 1 FROM flags f
|
|
||||||
WHERE f.flagable_type = 'products'
|
|
||||||
AND f.flagable_id = pw.product_id
|
|
||||||
AND UPPER(f.name) IN ?
|
|
||||||
)`, flags).
|
|
||||||
Pluck("pw.id", &invalidIDs).Error; err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
if len(invalidIDs) > 0 {
|
|
||||||
return invalidIDs[0], nil
|
|
||||||
}
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func nextRecordingDay(days []int) int {
|
func nextRecordingDay(days []int) int {
|
||||||
if len(days) == 0 {
|
if len(days) == 0 {
|
||||||
return 1
|
return 1
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -3,92 +3,43 @@ package service
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"math"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
|
rProductWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/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"
|
||||||
|
rStockLogs "gitlab.com/mbugroup/lti-api.git/internal/modules/shared/repositories"
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/utils/fifo"
|
"gitlab.com/mbugroup/lti-api.git/internal/utils/fifo"
|
||||||
|
recordingutil "gitlab.com/mbugroup/lti-api.git/internal/utils/recording"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type RecordingFIFOIntegrationService interface {
|
||||||
|
ConsumeRecordingStocks(ctx context.Context, tx *gorm.DB, stocks []entity.RecordingStock, note string, actorID uint) error
|
||||||
|
ReleaseRecordingStocks(ctx context.Context, tx *gorm.DB, stocks []entity.RecordingStock, note string, actorID uint) error
|
||||||
|
}
|
||||||
|
|
||||||
var recordingStockUsableKey = fifo.UsableKeyRecordingStock
|
var recordingStockUsableKey = fifo.UsableKeyRecordingStock
|
||||||
var recordingDepletionUsableKey = fifo.UsableKeyRecordingDepletion
|
var recordingDepletionUsableKey = fifo.UsableKeyRecordingDepletion
|
||||||
|
|
||||||
const depletionUsageTolerance = 0.000001
|
func NewRecordingFIFOIntegrationService(
|
||||||
|
repo repository.RecordingRepository,
|
||||||
func (s *recordingService) logStockTrace(action string, stock entity.RecordingStock, extra string) {
|
productWarehouseRepo rProductWarehouse.ProductWarehouseRepository,
|
||||||
if s == nil || s.Log == nil {
|
fifoSvc commonSvc.FifoService,
|
||||||
return
|
stockLogRepo rStockLogs.StockLogRepository,
|
||||||
|
) RecordingFIFOIntegrationService {
|
||||||
|
return &recordingService{
|
||||||
|
Log: utils.Log,
|
||||||
|
Repository: repo,
|
||||||
|
ProductWarehouseRepo: productWarehouseRepo,
|
||||||
|
FifoSvc: fifoSvc,
|
||||||
|
StockLogRepo: stockLogRepo,
|
||||||
}
|
}
|
||||||
usage := 0.0
|
|
||||||
if stock.UsageQty != nil {
|
|
||||||
usage = *stock.UsageQty
|
|
||||||
}
|
|
||||||
pending := 0.0
|
|
||||||
if stock.PendingQty != nil {
|
|
||||||
pending = *stock.PendingQty
|
|
||||||
}
|
|
||||||
s.Log.Infof(
|
|
||||||
"[recording-stock] action=%s recording_id=%d stock_id=%d pw=%d usage=%.3f pending=%.3f %s",
|
|
||||||
action,
|
|
||||||
stock.RecordingId,
|
|
||||||
stock.Id,
|
|
||||||
stock.ProductWarehouseId,
|
|
||||||
usage,
|
|
||||||
pending,
|
|
||||||
extra,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *recordingService) logEggTrace(action string, egg entity.RecordingEgg, extra string) {
|
|
||||||
if s == nil || s.Log == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
weight := 0.0
|
|
||||||
if egg.Weight != nil {
|
|
||||||
weight = *egg.Weight
|
|
||||||
}
|
|
||||||
s.Log.Infof(
|
|
||||||
"[recording-egg] action=%s recording_id=%d egg_id=%d pw=%d qty=%d weight=%.3f total_qty=%.3f total_used=%.3f %s",
|
|
||||||
action,
|
|
||||||
egg.RecordingId,
|
|
||||||
egg.Id,
|
|
||||||
egg.ProductWarehouseId,
|
|
||||||
egg.Qty,
|
|
||||||
weight,
|
|
||||||
egg.TotalQty,
|
|
||||||
egg.TotalUsed,
|
|
||||||
extra,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *recordingService) logDepletionTrace(action string, dep entity.RecordingDepletion, extra string) {
|
|
||||||
if s == nil || s.Log == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
sourceWarehouseID := uint(0)
|
|
||||||
if dep.SourceProductWarehouseId != nil {
|
|
||||||
sourceWarehouseID = *dep.SourceProductWarehouseId
|
|
||||||
}
|
|
||||||
s.Log.Infof(
|
|
||||||
"[recording-depletion] action=%s recording_id=%d depletion_id=%d source_pw=%d dest_pw=%d qty=%.3f usage=%.3f pending=%.3f %s",
|
|
||||||
action,
|
|
||||||
dep.RecordingId,
|
|
||||||
dep.Id,
|
|
||||||
sourceWarehouseID,
|
|
||||||
dep.ProductWarehouseId,
|
|
||||||
dep.Qty,
|
|
||||||
dep.UsageQty,
|
|
||||||
dep.PendingQty,
|
|
||||||
extra,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *recordingService) consumeRecordingStocks(
|
func (s *recordingService) consumeRecordingStocks(
|
||||||
@@ -98,13 +49,9 @@ func (s *recordingService) consumeRecordingStocks(
|
|||||||
note string,
|
note string,
|
||||||
actorID uint,
|
actorID uint,
|
||||||
) error {
|
) error {
|
||||||
if len(stocks) == 0 {
|
if len(stocks) == 0 || s.FifoSvc == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if s.FifoSvc == nil {
|
|
||||||
s.Log.Errorf("FIFO service is not available for consuming recording stocks")
|
|
||||||
return errors.New("fifo service is not available")
|
|
||||||
}
|
|
||||||
if strings.TrimSpace(note) != "" && s.StockLogRepo == nil {
|
if strings.TrimSpace(note) != "" && s.StockLogRepo == nil {
|
||||||
return errors.New("stock log repository is not available")
|
return errors.New("stock log repository is not available")
|
||||||
}
|
}
|
||||||
@@ -113,7 +60,6 @@ func (s *recordingService) consumeRecordingStocks(
|
|||||||
if stock.Id == 0 {
|
if stock.Id == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
s.logStockTrace("consume:start", stock, "")
|
|
||||||
|
|
||||||
var desired float64
|
var desired float64
|
||||||
if stock.UsageQty != nil {
|
if stock.UsageQty != nil {
|
||||||
@@ -141,7 +87,6 @@ func (s *recordingService) consumeRecordingStocks(
|
|||||||
if err := s.Repository.UpdateStockUsage(tx, stock.Id, result.UsageQuantity, result.PendingQuantity); err != nil {
|
if err := s.Repository.UpdateStockUsage(tx, stock.Id, result.UsageQuantity, result.PendingQuantity); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
s.logStockTrace("consume:done", stock, fmt.Sprintf("desired=%.3f used=%.3f pending=%.3f", desiredTotal, result.UsageQuantity, result.PendingQuantity))
|
|
||||||
|
|
||||||
logDecrease := result.UsageQuantity
|
logDecrease := result.UsageQuantity
|
||||||
if result.PendingQuantity > 0 {
|
if result.PendingQuantity > 0 {
|
||||||
@@ -184,13 +129,9 @@ func (s *recordingService) consumeRecordingDepletions(
|
|||||||
note string,
|
note string,
|
||||||
actorID uint,
|
actorID uint,
|
||||||
) error {
|
) error {
|
||||||
if len(depletions) == 0 {
|
if len(depletions) == 0 || s.FifoSvc == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if s.FifoSvc == nil {
|
|
||||||
s.Log.Errorf("FIFO service is not available for consuming recording depletions")
|
|
||||||
return errors.New("fifo service is not available")
|
|
||||||
}
|
|
||||||
if strings.TrimSpace(note) != "" && s.StockLogRepo == nil {
|
if strings.TrimSpace(note) != "" && s.StockLogRepo == nil {
|
||||||
return errors.New("stock log repository is not available")
|
return errors.New("stock log repository is not available")
|
||||||
}
|
}
|
||||||
@@ -199,7 +140,6 @@ func (s *recordingService) consumeRecordingDepletions(
|
|||||||
if depletion.Id == 0 {
|
if depletion.Id == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
s.logDepletionTrace("consume:start", depletion, "")
|
|
||||||
|
|
||||||
sourceWarehouseID := uint(0)
|
sourceWarehouseID := uint(0)
|
||||||
if depletion.SourceProductWarehouseId != nil {
|
if depletion.SourceProductWarehouseId != nil {
|
||||||
@@ -226,7 +166,6 @@ func (s *recordingService) consumeRecordingDepletions(
|
|||||||
if err := s.Repository.UpdateDepletionPending(tx, depletion.Id, result.PendingQuantity); err != nil {
|
if err := s.Repository.UpdateDepletionPending(tx, depletion.Id, result.PendingQuantity); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
s.logDepletionTrace("consume:done", depletion, fmt.Sprintf("desired=%.3f used=%.3f pending=%.3f", desired, result.UsageQuantity, result.PendingQuantity))
|
|
||||||
|
|
||||||
logDecrease := result.UsageQuantity
|
logDecrease := result.UsageQuantity
|
||||||
if result.PendingQuantity > 0 {
|
if result.PendingQuantity > 0 {
|
||||||
@@ -292,6 +231,16 @@ func (s *recordingService) consumeRecordingDepletions(
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *recordingService) ConsumeRecordingStocks(
|
||||||
|
ctx context.Context,
|
||||||
|
tx *gorm.DB,
|
||||||
|
stocks []entity.RecordingStock,
|
||||||
|
note string,
|
||||||
|
actorID uint,
|
||||||
|
) error {
|
||||||
|
return s.consumeRecordingStocks(ctx, tx, stocks, note, actorID)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *recordingService) releaseRecordingStocks(
|
func (s *recordingService) releaseRecordingStocks(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
tx *gorm.DB,
|
tx *gorm.DB,
|
||||||
@@ -299,13 +248,9 @@ func (s *recordingService) releaseRecordingStocks(
|
|||||||
note string,
|
note string,
|
||||||
actorID uint,
|
actorID uint,
|
||||||
) error {
|
) error {
|
||||||
if len(stocks) == 0 {
|
if len(stocks) == 0 || s.FifoSvc == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if s.FifoSvc == nil {
|
|
||||||
s.Log.Errorf("FIFO service is not available for releasing recording stocks")
|
|
||||||
return errors.New("fifo service is not available")
|
|
||||||
}
|
|
||||||
if strings.TrimSpace(note) != "" && s.StockLogRepo == nil {
|
if strings.TrimSpace(note) != "" && s.StockLogRepo == nil {
|
||||||
return errors.New("stock log repository is not available")
|
return errors.New("stock log repository is not available")
|
||||||
}
|
}
|
||||||
@@ -314,26 +259,6 @@ func (s *recordingService) releaseRecordingStocks(
|
|||||||
if stock.Id == 0 {
|
if stock.Id == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if stock.UsageQty != nil && *stock.UsageQty > 0 {
|
|
||||||
activeCount, err := s.countActiveAllocations(ctx, tx, fifo.UsableKeyRecordingStock, stock.Id)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if activeCount == 0 {
|
|
||||||
s.Log.Warnf("recording-stock release: no active allocations, forcing usage/pending to 0 (stock_id=%d)", stock.Id)
|
|
||||||
if err := s.Repository.UpdateStockUsage(tx, stock.Id, 0, 0); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if err := s.resyncStockableUsageFromAllocations(ctx, tx, fifo.UsableKeyRecordingStock, stock.Id); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := s.ensureActiveAllocations(ctx, tx, fifo.UsableKeyRecordingStock, stock.Id); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
s.logStockTrace("release:start", stock, "")
|
|
||||||
if err := s.FifoSvc.ReleaseUsage(ctx, commonSvc.StockReleaseRequest{
|
if err := s.FifoSvc.ReleaseUsage(ctx, commonSvc.StockReleaseRequest{
|
||||||
UsableKey: recordingStockUsableKey,
|
UsableKey: recordingStockUsableKey,
|
||||||
UsableID: stock.Id,
|
UsableID: stock.Id,
|
||||||
@@ -346,7 +271,6 @@ func (s *recordingService) releaseRecordingStocks(
|
|||||||
if err := s.Repository.UpdateStockUsage(tx, stock.Id, 0, 0); err != nil {
|
if err := s.Repository.UpdateStockUsage(tx, stock.Id, 0, 0); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
s.logStockTrace("release:done", stock, "")
|
|
||||||
|
|
||||||
if stock.UsageQty != nil && *stock.UsageQty > 0 && strings.TrimSpace(note) != "" && actorID != 0 {
|
if stock.UsageQty != nil && *stock.UsageQty > 0 && strings.TrimSpace(note) != "" && actorID != 0 {
|
||||||
log := &entity.StockLog{
|
log := &entity.StockLog{
|
||||||
@@ -385,13 +309,9 @@ func (s *recordingService) releaseRecordingDepletions(
|
|||||||
note string,
|
note string,
|
||||||
actorID uint,
|
actorID uint,
|
||||||
) error {
|
) error {
|
||||||
if len(depletions) == 0 {
|
if len(depletions) == 0 || s.FifoSvc == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if s.FifoSvc == nil {
|
|
||||||
s.Log.Errorf("FIFO service is not available for releasing recording depletions")
|
|
||||||
return errors.New("fifo service is not available")
|
|
||||||
}
|
|
||||||
if strings.TrimSpace(note) != "" && s.StockLogRepo == nil {
|
if strings.TrimSpace(note) != "" && s.StockLogRepo == nil {
|
||||||
return errors.New("stock log repository is not available")
|
return errors.New("stock log repository is not available")
|
||||||
}
|
}
|
||||||
@@ -400,36 +320,6 @@ func (s *recordingService) releaseRecordingDepletions(
|
|||||||
if depletion.Id == 0 {
|
if depletion.Id == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if depletion.UsageQty > 0 {
|
|
||||||
activeCount, err := s.countActiveAllocations(ctx, tx, fifo.UsableKeyRecordingDepletion, depletion.Id)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if activeCount == 0 {
|
|
||||||
s.Log.Warnf("recording-depletion release: no active allocations, forcing usage/pending to 0 (depletion_id=%d)", depletion.Id)
|
|
||||||
if err := s.Repository.UpdateDepletionPending(tx, depletion.Id, 0); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := tx.WithContext(ctx).
|
|
||||||
Table("recording_depletions").
|
|
||||||
Where("id = ?", depletion.Id).
|
|
||||||
Update("usage_qty", 0).Error; err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if err := s.resyncStockableUsageFromAllocations(ctx, tx, fifo.UsableKeyRecordingDepletion, depletion.Id); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := s.ensureActiveAllocations(ctx, tx, fifo.UsableKeyRecordingDepletion, depletion.Id); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
s.logDepletionTrace("release:start", depletion, "")
|
|
||||||
if err := validateDepletionUsage(depletion); err != nil {
|
|
||||||
s.Log.Errorf("FIFO depletion mismatch for recording %d (depletion %d): qty=%.3f usage=%.3f pending=%.3f", depletion.RecordingId, depletion.Id, depletion.Qty, depletion.UsageQty, depletion.PendingQty)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
sourceWarehouseID := uint(0)
|
sourceWarehouseID := uint(0)
|
||||||
if depletion.SourceProductWarehouseId != nil {
|
if depletion.SourceProductWarehouseId != nil {
|
||||||
@@ -450,7 +340,6 @@ func (s *recordingService) releaseRecordingDepletions(
|
|||||||
if err := s.Repository.UpdateDepletionPending(tx, depletion.Id, 0); err != nil {
|
if err := s.Repository.UpdateDepletionPending(tx, depletion.Id, 0); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
s.logDepletionTrace("release:done", depletion, "")
|
|
||||||
|
|
||||||
logIncrease := depletion.Qty
|
logIncrease := depletion.Qty
|
||||||
if depletion.PendingQty > 0 {
|
if depletion.PendingQty > 0 {
|
||||||
@@ -516,15 +405,14 @@ func (s *recordingService) releaseRecordingDepletions(
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateDepletionUsage(depletion entity.RecordingDepletion) error {
|
func (s *recordingService) ReleaseRecordingStocks(
|
||||||
desired := depletion.Qty + depletion.PendingQty
|
ctx context.Context,
|
||||||
if math.Abs(depletion.UsageQty-desired) <= depletionUsageTolerance {
|
tx *gorm.DB,
|
||||||
return nil
|
stocks []entity.RecordingStock,
|
||||||
}
|
note string,
|
||||||
return fiber.NewError(
|
actorID uint,
|
||||||
fiber.StatusConflict,
|
) error {
|
||||||
fmt.Sprintf("FIFO depletion mismatch (id=%d): qty=%.3f usage=%.3f pending=%.3f", depletion.Id, depletion.Qty, depletion.UsageQty, depletion.PendingQty),
|
return s.releaseRecordingStocks(ctx, tx, stocks, note, actorID)
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *recordingService) logRecordingEggUsage(
|
func (s *recordingService) logRecordingEggUsage(
|
||||||
@@ -615,13 +503,9 @@ func (s *recordingService) replenishRecordingEggs(
|
|||||||
note string,
|
note string,
|
||||||
actorID uint,
|
actorID uint,
|
||||||
) error {
|
) error {
|
||||||
if len(eggs) == 0 {
|
if len(eggs) == 0 || s.FifoSvc == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if s.FifoSvc == nil {
|
|
||||||
s.Log.Errorf("FIFO service is not available for replenishing recording eggs")
|
|
||||||
return errors.New("fifo service is not available")
|
|
||||||
}
|
|
||||||
if strings.TrimSpace(note) != "" && s.StockLogRepo == nil {
|
if strings.TrimSpace(note) != "" && s.StockLogRepo == nil {
|
||||||
return errors.New("stock log repository is not available")
|
return errors.New("stock log repository is not available")
|
||||||
}
|
}
|
||||||
@@ -630,7 +514,6 @@ func (s *recordingService) replenishRecordingEggs(
|
|||||||
if egg.Id == 0 || egg.ProductWarehouseId == 0 || egg.Qty <= 0 {
|
if egg.Id == 0 || egg.ProductWarehouseId == 0 || egg.Qty <= 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
s.logEggTrace("replenish:start", egg, "")
|
|
||||||
if _, err := s.FifoSvc.Replenish(ctx, commonSvc.StockReplenishRequest{
|
if _, err := s.FifoSvc.Replenish(ctx, commonSvc.StockReplenishRequest{
|
||||||
StockableKey: fifo.StockableKeyRecordingEgg,
|
StockableKey: fifo.StockableKeyRecordingEgg,
|
||||||
StockableID: egg.Id,
|
StockableID: egg.Id,
|
||||||
@@ -641,7 +524,6 @@ func (s *recordingService) replenishRecordingEggs(
|
|||||||
s.Log.Errorf("Failed to replenish FIFO stock for recording egg %d: %+v", egg.Id, err)
|
s.Log.Errorf("Failed to replenish FIFO stock for recording egg %d: %+v", egg.Id, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
s.logEggTrace("replenish:done", egg, "")
|
|
||||||
|
|
||||||
if strings.TrimSpace(note) != "" && actorID != 0 {
|
if strings.TrimSpace(note) != "" && actorID != 0 {
|
||||||
log := &entity.StockLog{
|
log := &entity.StockLog{
|
||||||
@@ -673,210 +555,6 @@ func (s *recordingService) replenishRecordingEggs(
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *recordingService) replenishRecordingDepletions(
|
|
||||||
ctx context.Context,
|
|
||||||
tx *gorm.DB,
|
|
||||||
depletions []entity.RecordingDepletion,
|
|
||||||
) error {
|
|
||||||
if len(depletions) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if s.FifoSvc == nil {
|
|
||||||
s.Log.Errorf("FIFO service is not available for replenishing recording depletions")
|
|
||||||
return errors.New("fifo service is not available")
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, depletion := range depletions {
|
|
||||||
if depletion.Id == 0 || depletion.ProductWarehouseId == 0 || depletion.Qty <= 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
s.logDepletionTrace("replenish:start", depletion, "")
|
|
||||||
if _, err := s.FifoSvc.Replenish(ctx, commonSvc.StockReplenishRequest{
|
|
||||||
StockableKey: fifo.StockableKeyRecordingDepletion,
|
|
||||||
StockableID: depletion.Id,
|
|
||||||
ProductWarehouseID: depletion.ProductWarehouseId,
|
|
||||||
Quantity: depletion.Qty,
|
|
||||||
Tx: tx,
|
|
||||||
}); err != nil {
|
|
||||||
s.Log.Errorf("Failed to replenish FIFO stock for recording depletion %d: %+v", depletion.Id, err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
s.logDepletionTrace("replenish:done", depletion, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *recordingService) reduceRecordingDepletions(
|
|
||||||
ctx context.Context,
|
|
||||||
tx *gorm.DB,
|
|
||||||
depletions []entity.RecordingDepletion,
|
|
||||||
) error {
|
|
||||||
if len(depletions) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if s.FifoSvc == nil {
|
|
||||||
s.Log.Errorf("FIFO service is not available for reducing recording depletions")
|
|
||||||
return errors.New("fifo service is not available")
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, depletion := range depletions {
|
|
||||||
if depletion.Id == 0 || depletion.ProductWarehouseId == 0 || depletion.Qty <= 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
s.logDepletionTrace("reduce:start", depletion, "")
|
|
||||||
if err := s.FifoSvc.AdjustStockableQuantity(ctx, commonSvc.StockAdjustRequest{
|
|
||||||
StockableKey: fifo.StockableKeyRecordingDepletion,
|
|
||||||
StockableID: depletion.Id,
|
|
||||||
ProductWarehouseID: depletion.ProductWarehouseId,
|
|
||||||
Quantity: -depletion.Qty,
|
|
||||||
Tx: tx,
|
|
||||||
}); err != nil {
|
|
||||||
s.Log.Errorf("Failed to reduce FIFO stock for recording depletion %d: %+v", depletion.Id, err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
s.logDepletionTrace("reduce:done", depletion, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *recordingService) reduceRecordingEggs(
|
|
||||||
ctx context.Context,
|
|
||||||
tx *gorm.DB,
|
|
||||||
eggs []entity.RecordingEgg,
|
|
||||||
) error {
|
|
||||||
if len(eggs) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if s.FifoSvc == nil {
|
|
||||||
s.Log.Errorf("FIFO service is not available for reducing recording eggs")
|
|
||||||
return errors.New("fifo service is not available")
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, egg := range eggs {
|
|
||||||
if egg.Id == 0 || egg.ProductWarehouseId == 0 || egg.Qty <= 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
s.logEggTrace("reduce:start", egg, "")
|
|
||||||
if err := s.FifoSvc.AdjustStockableQuantity(ctx, commonSvc.StockAdjustRequest{
|
|
||||||
StockableKey: fifo.StockableKeyRecordingEgg,
|
|
||||||
StockableID: egg.Id,
|
|
||||||
ProductWarehouseID: egg.ProductWarehouseId,
|
|
||||||
Quantity: -float64(egg.Qty),
|
|
||||||
Tx: tx,
|
|
||||||
}); err != nil {
|
|
||||||
s.Log.Errorf("Failed to reduce FIFO stock for recording egg %d: %+v", egg.Id, err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
s.logEggTrace("reduce:done", egg, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *recordingService) ensureActiveAllocations(
|
|
||||||
ctx context.Context,
|
|
||||||
tx *gorm.DB,
|
|
||||||
usableKey fifo.UsableKey,
|
|
||||||
usableID uint,
|
|
||||||
) error {
|
|
||||||
if usableID == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
var count int64
|
|
||||||
if err := tx.WithContext(ctx).
|
|
||||||
Model(&entity.StockAllocation{}).
|
|
||||||
Where("usable_type = ? AND usable_id = ? AND status = ?", usableKey, usableID, entity.StockAllocationStatusActive).
|
|
||||||
Count(&count).Error; err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if count == 0 {
|
|
||||||
return fiber.NewError(fiber.StatusConflict, fmt.Sprintf("no active allocations for usable %s id=%d", usableKey, usableID))
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *recordingService) countActiveAllocations(
|
|
||||||
ctx context.Context,
|
|
||||||
tx *gorm.DB,
|
|
||||||
usableKey fifo.UsableKey,
|
|
||||||
usableID uint,
|
|
||||||
) (int64, error) {
|
|
||||||
if usableID == 0 {
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
var count int64
|
|
||||||
if err := tx.WithContext(ctx).
|
|
||||||
Model(&entity.StockAllocation{}).
|
|
||||||
Where("usable_type = ? AND usable_id = ? AND status = ?", usableKey, usableID, entity.StockAllocationStatusActive).
|
|
||||||
Count(&count).Error; err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
return count, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *recordingService) resyncStockableUsageFromAllocations(
|
|
||||||
ctx context.Context,
|
|
||||||
tx *gorm.DB,
|
|
||||||
usableKey fifo.UsableKey,
|
|
||||||
usableID uint,
|
|
||||||
) error {
|
|
||||||
if usableID == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type stockableRef struct {
|
|
||||||
StockableType string
|
|
||||||
StockableID uint
|
|
||||||
}
|
|
||||||
|
|
||||||
var refs []stockableRef
|
|
||||||
if err := tx.WithContext(ctx).
|
|
||||||
Model(&entity.StockAllocation{}).
|
|
||||||
Select("stockable_type, stockable_id").
|
|
||||||
Where("usable_type = ? AND usable_id = ? AND status = ?", usableKey, usableID, entity.StockAllocationStatusActive).
|
|
||||||
Group("stockable_type, stockable_id").
|
|
||||||
Scan(&refs).Error; err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if len(refs) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, ref := range refs {
|
|
||||||
var total float64
|
|
||||||
if err := tx.WithContext(ctx).
|
|
||||||
Model(&entity.StockAllocation{}).
|
|
||||||
Select("COALESCE(SUM(qty),0)").
|
|
||||||
Where("stockable_type = ? AND stockable_id = ? AND status = ?", ref.StockableType, ref.StockableID, entity.StockAllocationStatusActive).
|
|
||||||
Scan(&total).Error; err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
switch ref.StockableType {
|
|
||||||
case string(fifo.StockableKeyProjectFlockPopulation):
|
|
||||||
if err := tx.WithContext(ctx).
|
|
||||||
Table("project_flock_populations").
|
|
||||||
Where("id = ?", ref.StockableID).
|
|
||||||
Update("total_used_qty", total).Error; err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
case string(fifo.StockableKeyPurchaseItems):
|
|
||||||
if err := tx.WithContext(ctx).
|
|
||||||
Table("purchase_items").
|
|
||||||
Where("id = ?", ref.StockableID).
|
|
||||||
Update("total_used", total).Error; err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
// no-op for other stockables
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type desiredStock struct {
|
type desiredStock struct {
|
||||||
Usage float64
|
Usage float64
|
||||||
Pending float64
|
Pending float64
|
||||||
@@ -887,7 +565,7 @@ type desiredDepletion struct {
|
|||||||
Pending float64
|
Pending float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func resetStockQuantitiesForFIFO(stocks []entity.RecordingStock) []desiredStock {
|
func resetStockQuantitiesForFIFO(stocks []entity.RecordingStock, enabled bool) []desiredStock {
|
||||||
desired := make([]desiredStock, len(stocks))
|
desired := make([]desiredStock, len(stocks))
|
||||||
for i := range stocks {
|
for i := range stocks {
|
||||||
if stocks[i].UsageQty != nil {
|
if stocks[i].UsageQty != nil {
|
||||||
@@ -896,6 +574,9 @@ func resetStockQuantitiesForFIFO(stocks []entity.RecordingStock) []desiredStock
|
|||||||
if stocks[i].PendingQty != nil {
|
if stocks[i].PendingQty != nil {
|
||||||
desired[i].Pending = *stocks[i].PendingQty
|
desired[i].Pending = *stocks[i].PendingQty
|
||||||
}
|
}
|
||||||
|
if !enabled {
|
||||||
|
continue
|
||||||
|
}
|
||||||
zero := 0.0
|
zero := 0.0
|
||||||
stocks[i].UsageQty = &zero
|
stocks[i].UsageQty = &zero
|
||||||
stocks[i].PendingQty = &zero
|
stocks[i].PendingQty = &zero
|
||||||
@@ -903,19 +584,39 @@ func resetStockQuantitiesForFIFO(stocks []entity.RecordingStock) []desiredStock
|
|||||||
return desired
|
return desired
|
||||||
}
|
}
|
||||||
|
|
||||||
func resetDepletionQuantitiesForFIFO(depletions []entity.RecordingDepletion) []desiredDepletion {
|
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 resetDepletionQuantitiesForFIFO(depletions []entity.RecordingDepletion, enabled bool) []desiredDepletion {
|
||||||
desired := make([]desiredDepletion, len(depletions))
|
desired := make([]desiredDepletion, len(depletions))
|
||||||
for i := range depletions {
|
for i := range depletions {
|
||||||
desired[i].Qty = depletions[i].Qty
|
desired[i].Qty = depletions[i].Qty
|
||||||
desired[i].Pending = depletions[i].PendingQty
|
desired[i].Pending = depletions[i].PendingQty
|
||||||
|
if !enabled {
|
||||||
|
continue
|
||||||
|
}
|
||||||
depletions[i].Qty = 0
|
depletions[i].Qty = 0
|
||||||
depletions[i].UsageQty = 0
|
|
||||||
depletions[i].PendingQty = 0
|
depletions[i].PendingQty = 0
|
||||||
}
|
}
|
||||||
return desired
|
return desired
|
||||||
}
|
}
|
||||||
|
|
||||||
func applyDepletionDesiredQuantities(depletions []entity.RecordingDepletion, desired []desiredDepletion) {
|
func applyDepletionDesiredQuantities(depletions []entity.RecordingDepletion, desired []desiredDepletion, enabled bool) {
|
||||||
|
if !enabled {
|
||||||
|
return
|
||||||
|
}
|
||||||
for i := range depletions {
|
for i := range depletions {
|
||||||
if i >= len(desired) {
|
if i >= len(desired) {
|
||||||
break
|
break
|
||||||
@@ -935,8 +636,11 @@ func (s *recordingService) syncRecordingStocks(
|
|||||||
actorID uint,
|
actorID uint,
|
||||||
) error {
|
) error {
|
||||||
if s.FifoSvc == nil {
|
if s.FifoSvc == nil {
|
||||||
s.Log.Errorf("FIFO service is not available for syncing recording stocks")
|
if err := s.Repository.DeleteStocks(tx, recordingID); err != nil {
|
||||||
return errors.New("fifo service is not available")
|
return err
|
||||||
|
}
|
||||||
|
mapped := recordingutil.MapStocks(recordingID, incoming)
|
||||||
|
return s.Repository.CreateStocks(tx, mapped)
|
||||||
}
|
}
|
||||||
|
|
||||||
existingByWarehouse := make(map[uint][]entity.RecordingStock)
|
existingByWarehouse := make(map[uint][]entity.RecordingStock)
|
||||||
@@ -997,137 +701,3 @@ func (s *recordingService) syncRecordingStocks(
|
|||||||
}
|
}
|
||||||
return s.consumeRecordingStocks(ctx, tx, stocksToConsume, note, actorID)
|
return s.consumeRecordingStocks(ctx, tx, stocksToConsume, note, actorID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func sumDepletionQty(items []entity.RecordingDepletion) float64 {
|
|
||||||
var total float64
|
|
||||||
for _, item := range items {
|
|
||||||
if item.Qty > 0 {
|
|
||||||
total += item.Qty
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return total
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *recordingService) ensureDepletionWithinPopulation(ctx context.Context, tx *gorm.DB, projectFlockKandangId uint, newTotal float64, existingTotal float64) error {
|
|
||||||
if projectFlockKandangId == 0 || newTotal <= 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
totalChick, err := s.Repository.GetTotalChick(tx, projectFlockKandangId)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// totalChick already reflects existing depletions; add them back to compare the delta.
|
|
||||||
available := float64(totalChick) + existingTotal
|
|
||||||
if newTotal > available {
|
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "Depletion melebihi populasi yang tersedia")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func ensureRecordingEggsUnused(eggs []entity.RecordingEgg) error {
|
|
||||||
for _, egg := range eggs {
|
|
||||||
if egg.TotalUsed > 0 {
|
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "Recording egg sudah digunakan sehingga tidak dapat diubah")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *recordingService) resolvePopulationWarehouseID(ctx context.Context, projectFlockKandangID uint) (uint, error) {
|
|
||||||
if projectFlockKandangID == 0 {
|
|
||||||
return 0, fiber.NewError(fiber.StatusBadRequest, "Project flock kandang tidak valid")
|
|
||||||
}
|
|
||||||
populations, err := s.ProjectFlockPopulationRepo.GetByProjectFlockKandangID(ctx, projectFlockKandangID)
|
|
||||||
if err != nil {
|
|
||||||
s.Log.Errorf("Failed to fetch populations for project_flock_kandang_id=%d: %+v", projectFlockKandangID, err)
|
|
||||||
return 0, fiber.NewError(fiber.StatusInternalServerError, "Gagal mengambil data populasi")
|
|
||||||
}
|
|
||||||
for _, pop := range populations {
|
|
||||||
if pop.ProductWarehouseId > 0 && pop.TotalQty > 0 {
|
|
||||||
return pop.ProductWarehouseId, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, pop := range populations {
|
|
||||||
if pop.ProductWarehouseId > 0 {
|
|
||||||
return pop.ProductWarehouseId, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0, fiber.NewError(fiber.StatusBadRequest, "Source product warehouse populasi tidak ditemukan")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *recordingService) recalculateFrom(ctx context.Context, tx *gorm.DB, projectFlockKandangId uint, from time.Time) error {
|
|
||||||
if tx == nil || projectFlockKandangId == 0 || from.IsZero() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
fromUTC := from.UTC()
|
|
||||||
records, err := s.Repository.ListByProjectFlockKandangID(ctx, tx, projectFlockKandangId, &fromUTC)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := range records {
|
|
||||||
if err := s.computeAndUpdateMetrics(ctx, tx, &records[i]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *recordingService) rollbackRecordingInventory(ctx context.Context, tx *gorm.DB, recordingID uint, note string, actorID uint) error {
|
|
||||||
if recordingID == 0 || tx == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if err := s.requireFIFO(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
oldDepletions, err := s.Repository.ListDepletions(tx, recordingID)
|
|
||||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
||||||
s.Log.Errorf("Failed to list depletions: %+v", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
oldEggs, err := s.Repository.ListEggs(tx, recordingID)
|
|
||||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
||||||
s.Log.Errorf("Failed to list eggs: %+v", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := ensureRecordingEggsUnused(oldEggs); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := s.releaseRecordingDepletions(ctx, tx, oldDepletions, note, actorID); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
oldStocks, err := s.Repository.ListStocks(tx, recordingID)
|
|
||||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
||||||
s.Log.Errorf("Failed to list stocks: %+v", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := s.releaseRecordingStocks(ctx, tx, oldStocks, note, actorID); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := s.reduceRecordingDepletions(ctx, tx, oldDepletions); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := s.reduceRecordingEggs(ctx, tx, oldEggs); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := s.logRecordingEggRollback(ctx, tx, oldEggs, note, actorID); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *recordingService) requireFIFO() error {
|
|
||||||
if s.FifoSvc == nil {
|
|
||||||
s.Log.Errorf("FIFO service is not available for recording operations")
|
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, "FIFO service is required for recording operations")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
package validation
|
package validation
|
||||||
|
|
||||||
import "time"
|
|
||||||
|
|
||||||
type (
|
type (
|
||||||
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"`
|
||||||
@@ -37,7 +35,6 @@ 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"`
|
||||||
Offset int `query:"-" validate:"omitempty,number,min=0"`
|
|
||||||
ProjectFlockKandangId uint `query:"project_flock_kandang_id" validate:"omitempty,number,min=1"`
|
ProjectFlockKandangId uint `query:"project_flock_kandang_id" validate:"omitempty,number,min=1"`
|
||||||
Search string `query:"search" validate:"omitempty,max=50"`
|
Search string `query:"search" validate:"omitempty,max=50"`
|
||||||
}
|
}
|
||||||
@@ -47,9 +44,3 @@ type Approve struct {
|
|||||||
ApprovableIds []uint `json:"approvable_ids" validate:"required_strict,min=1,dive,gt=0"`
|
ApprovableIds []uint `json:"approvable_ids" validate:"required_strict,min=1,dive,gt=0"`
|
||||||
Notes *string `json:"notes,omitempty" validate:"omitempty,max=500"`
|
Notes *string `json:"notes,omitempty" validate:"omitempty,max=500"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type GetRecordingNextDay struct {
|
|
||||||
ProjectFlockKandangId uint `json:"project_flock_kandang_id" query:"project_flock_kandang_id" validate:"required,number,min=1"`
|
|
||||||
RecordTime *string `json:"record_date" query:"record_date" validate:"required,datetime=2006-01-02"`
|
|
||||||
RecordTimeValue *time.Time `query:"-" validate:"-"`
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -465,16 +465,11 @@ func (s *uniformityService) CreateOne(c *fiber.Ctx, req *validation.Create, file
|
|||||||
); err != nil {
|
); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if strings.EqualFold(category, string(utils.ProjectFlockCategoryGrowing)) {
|
|
||||||
if err := s.updateGrowingFcrForWeek(tx, createBody.ProjectFlockKandangId, createBody.Week, calculation.MeanUp); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
s.Log.Errorf("Failed to create uniformity: %+v", err)
|
s.Log.Errorf("Failed to create uniformity: %+v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.DocumentSvc != nil {
|
if s.DocumentSvc != nil {
|
||||||
actorIDCopy := actorID
|
actorIDCopy := actorID
|
||||||
@@ -638,9 +633,6 @@ func (s uniformityService) UpdateOne(c *fiber.Ctx, req *validation.Update, id ui
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.updateGrowingFcrFromUniformity(c.Context(), id); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return s.GetOne(c, id)
|
return s.GetOne(c, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -702,10 +694,6 @@ func (s uniformityService) UpdateOne(c *fiber.Ctx, req *validation.Update, id ui
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.updateGrowingFcrFromUniformity(c.Context(), id); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return s.GetOne(c, id)
|
return s.GetOne(c, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -736,48 +724,7 @@ func (s uniformityService) DeleteOne(c *fiber.Ctx, id uint) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
type uniformityContext struct {
|
if err := s.Repository.DeleteOne(c.Context(), id); err != nil {
|
||||||
ID uint
|
|
||||||
Week int
|
|
||||||
ProjectFlockKandangId uint
|
|
||||||
Category string
|
|
||||||
}
|
|
||||||
var ctxRow uniformityContext
|
|
||||||
if err := s.Repository.DB().WithContext(c.Context()).
|
|
||||||
Table("project_flock_kandang_uniformity u").
|
|
||||||
Select("u.id, u.week, u.project_flock_kandang_id, pf.category").
|
|
||||||
Joins("JOIN project_flock_kandangs pfk ON pfk.id = u.project_flock_kandang_id").
|
|
||||||
Joins("JOIN project_flocks pf ON pf.id = pfk.project_flock_id").
|
|
||||||
Where("u.id = ?", id).
|
|
||||||
Scan(&ctxRow).Error; err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if ctxRow.ID == 0 {
|
|
||||||
return fiber.NewError(fiber.StatusNotFound, "Uniformity not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := s.Repository.DB().WithContext(c.Context()).Transaction(func(tx *gorm.DB) error {
|
|
||||||
repoTx := s.Repository.WithTx(tx)
|
|
||||||
if err := repoTx.DeleteOne(c.Context(), id); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.EqualFold(ctxRow.Category, string(utils.ProjectFlockCategoryGrowing)) {
|
|
||||||
startDay := (ctxRow.Week-1)*7 + 1
|
|
||||||
endDay := ctxRow.Week * 7
|
|
||||||
if ctxRow.Week > 0 {
|
|
||||||
if err := tx.Model(&entity.Recording{}).
|
|
||||||
Where("project_flock_kandangs_id = ?", ctxRow.ProjectFlockKandangId).
|
|
||||||
Where("day BETWEEN ? AND ?", startDay, endDay).
|
|
||||||
Update("fcr_value", 0).Error; err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}); err != nil {
|
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
return fiber.NewError(fiber.StatusNotFound, "Uniformity not found")
|
return fiber.NewError(fiber.StatusNotFound, "Uniformity not found")
|
||||||
}
|
}
|
||||||
@@ -787,58 +734,6 @@ func (s uniformityService) DeleteOne(c *fiber.Ctx, id uint) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *uniformityService) updateGrowingFcrFromUniformity(ctx context.Context, uniformityID uint) error {
|
|
||||||
if uniformityID == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type uniformityRow struct {
|
|
||||||
ID uint
|
|
||||||
Week int
|
|
||||||
MeanUp float64
|
|
||||||
ProjectFlockKandangId uint
|
|
||||||
Category string
|
|
||||||
}
|
|
||||||
var row uniformityRow
|
|
||||||
if err := s.Repository.DB().WithContext(ctx).
|
|
||||||
Table("project_flock_kandang_uniformity u").
|
|
||||||
Select("u.id, u.week, u.mean_up, u.project_flock_kandang_id, pf.category").
|
|
||||||
Joins("JOIN project_flock_kandangs pfk ON pfk.id = u.project_flock_kandang_id").
|
|
||||||
Joins("JOIN project_flocks pf ON pf.id = pfk.project_flock_id").
|
|
||||||
Where("u.id = ?", uniformityID).
|
|
||||||
Scan(&row).Error; err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if row.ID == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if !strings.EqualFold(row.Category, string(utils.ProjectFlockCategoryGrowing)) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return s.updateGrowingFcrForWeek(s.Repository.DB().WithContext(ctx), row.ProjectFlockKandangId, row.Week, row.MeanUp)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *uniformityService) updateGrowingFcrForWeek(tx *gorm.DB, projectFlockKandangID uint, week int, meanUp float64) error {
|
|
||||||
if tx == nil || projectFlockKandangID == 0 || week <= 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
startDay := (week-1)*7 + 1
|
|
||||||
endDay := week * 7
|
|
||||||
meanBw := meanUp / 1.10
|
|
||||||
if meanBw <= 0 {
|
|
||||||
return tx.Model(&entity.Recording{}).
|
|
||||||
Where("project_flock_kandangs_id = ?", projectFlockKandangID).
|
|
||||||
Where("day BETWEEN ? AND ?", startDay, endDay).
|
|
||||||
Update("fcr_value", 0).Error
|
|
||||||
}
|
|
||||||
|
|
||||||
return tx.Model(&entity.Recording{}).
|
|
||||||
Where("project_flock_kandangs_id = ?", projectFlockKandangID).
|
|
||||||
Where("day BETWEEN ? AND ?", startDay, endDay).
|
|
||||||
Update("fcr_value", gorm.Expr("CASE WHEN feed_intake IS NULL OR feed_intake = 0 THEN 0 ELSE feed_intake / ? END", meanBw)).Error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s uniformityService) Approval(c *fiber.Ctx, req *validation.Approve) ([]entity.ProjectFlockKandangUniformity, error) {
|
func (s uniformityService) Approval(c *fiber.Ctx, req *validation.Approve) ([]entity.ProjectFlockKandangUniformity, error) {
|
||||||
if err := s.Validate.Struct(req); err != nil {
|
if err := s.Validate.Struct(req); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -25,17 +25,17 @@ type PurchaseRelationDTO struct {
|
|||||||
|
|
||||||
type PurchaseListDTO struct {
|
type PurchaseListDTO struct {
|
||||||
PurchaseRelationDTO
|
PurchaseRelationDTO
|
||||||
Supplier *supplierDTO.SupplierRelationDTO `json:"supplier"`
|
Supplier *supplierDTO.SupplierRelationDTO `json:"supplier"`
|
||||||
DueDate *time.Time `json:"due_date"`
|
DueDate *time.Time `json:"due_date"`
|
||||||
CreatedUser *userDTO.UserRelationDTO `json:"created_user"`
|
CreatedUser *userDTO.UserRelationDTO `json:"created_user"`
|
||||||
RequesterName string `json:"requester_name"`
|
RequesterName string `json:"requester_name"`
|
||||||
PoExpedition []PoExpeditionDTO `json:"po_expedition"`
|
PoExpedition []string `json:"po_expedition"`
|
||||||
Products []productDTO.ProductRelationDTO `json:"products"`
|
Products []productDTO.ProductRelationDTO `json:"products"`
|
||||||
Location *locationDTO.LocationRelationDTO `json:"location"`
|
Location *locationDTO.LocationRelationDTO `json:"location"`
|
||||||
Area *areaDTO.AreaRelationDTO `json:"area"`
|
Area *areaDTO.AreaRelationDTO `json:"area"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
LatestApproval *approvalDTO.ApprovalRelationDTO `json:"latest_approval"`
|
LatestApproval *approvalDTO.ApprovalRelationDTO `json:"latest_approval"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type PurchaseDetailDTO struct {
|
type PurchaseDetailDTO struct {
|
||||||
@@ -67,12 +67,6 @@ type PurchaseItemDTO struct {
|
|||||||
VehicleNumber *string `json:"vehicle_number"`
|
VehicleNumber *string `json:"vehicle_number"`
|
||||||
TransportPerItem *float64 `json:"transport_per_item,omitempty"`
|
TransportPerItem *float64 `json:"transport_per_item,omitempty"`
|
||||||
ExpeditionVendor *supplierDTO.SupplierRelationDTO `json:"expedition_vendor,omitempty"`
|
ExpeditionVendor *supplierDTO.SupplierRelationDTO `json:"expedition_vendor,omitempty"`
|
||||||
HasChickin bool `json:"has_chickin"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type PoExpeditionDTO struct {
|
|
||||||
Id uint64 `json:"id"`
|
|
||||||
Refrence string `json:"refrence"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ToPurchaseRelationDTO(p *entity.Purchase) PurchaseRelationDTO {
|
func ToPurchaseRelationDTO(p *entity.Purchase) PurchaseRelationDTO {
|
||||||
@@ -101,7 +95,6 @@ func ToPurchaseItemDTO(item entity.PurchaseItem) PurchaseItemDTO {
|
|||||||
TravelNumber: item.TravelNumber,
|
TravelNumber: item.TravelNumber,
|
||||||
TravelDocumentPath: item.TravelNumberDocs,
|
TravelDocumentPath: item.TravelNumberDocs,
|
||||||
VehicleNumber: item.VehicleNumber,
|
VehicleNumber: item.VehicleNumber,
|
||||||
HasChickin: item.HasChickin,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if item.Product != nil && item.Product.Id != 0 {
|
if item.Product != nil && item.Product.Id != 0 {
|
||||||
@@ -171,12 +164,12 @@ func ToPurchaseListDTO(p entity.Purchase) PurchaseListDTO {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
poExpedition = make([]PoExpeditionDTO, 0)
|
poExpedition []string
|
||||||
location *locationDTO.LocationRelationDTO
|
location *locationDTO.LocationRelationDTO
|
||||||
area *areaDTO.AreaRelationDTO
|
area *areaDTO.AreaRelationDTO
|
||||||
)
|
)
|
||||||
productMap := make(map[uint]productDTO.ProductRelationDTO)
|
productMap := make(map[uint]productDTO.ProductRelationDTO)
|
||||||
expeditionRefSet := make(map[uint64]struct{})
|
expeditionRefSet := make(map[string]struct{})
|
||||||
for i := range p.Items {
|
for i := range p.Items {
|
||||||
item := p.Items[i]
|
item := p.Items[i]
|
||||||
if item.Product != nil && item.Product.Id != 0 {
|
if item.Product != nil && item.Product.Id != 0 {
|
||||||
@@ -185,15 +178,11 @@ func ToPurchaseListDTO(p entity.Purchase) PurchaseListDTO {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if item.ExpenseNonstock != nil && item.ExpenseNonstock.Expense != nil {
|
if item.ExpenseNonstock != nil && item.ExpenseNonstock.Expense != nil {
|
||||||
exp := item.ExpenseNonstock.Expense
|
ref := strings.TrimSpace(item.ExpenseNonstock.Expense.ReferenceNumber)
|
||||||
ref := strings.TrimSpace(exp.ReferenceNumber)
|
if ref != "" {
|
||||||
if exp.Id != 0 && ref != "" {
|
if _, exists := expeditionRefSet[ref]; !exists {
|
||||||
if _, exists := expeditionRefSet[exp.Id]; !exists {
|
expeditionRefSet[ref] = struct{}{}
|
||||||
expeditionRefSet[exp.Id] = struct{}{}
|
poExpedition = append(poExpedition, ref)
|
||||||
poExpedition = append(poExpedition, PoExpeditionDTO{
|
|
||||||
Id: exp.Id,
|
|
||||||
Refrence: ref,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -255,39 +255,6 @@ func (s *purchaseService) GetOne(c *fiber.Ctx, id uint) (*entity.Purchase, error
|
|||||||
if err := s.attachLatestApproval(c.Context(), purchase); err != nil {
|
if err := s.attachLatestApproval(c.Context(), 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)
|
||||||
}
|
}
|
||||||
if len(purchase.Items) > 0 {
|
|
||||||
itemIDs := make([]uint, 0, len(purchase.Items))
|
|
||||||
for i := range purchase.Items {
|
|
||||||
if purchase.Items[i].Id == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
itemIDs = append(itemIDs, purchase.Items[i].Id)
|
|
||||||
}
|
|
||||||
if len(itemIDs) > 0 {
|
|
||||||
var usedIDs []uint
|
|
||||||
if err := s.PurchaseRepo.DB().WithContext(c.Context()).
|
|
||||||
Model(&entity.StockAllocation{}).
|
|
||||||
Distinct("stockable_id").
|
|
||||||
Where("stockable_type = ? AND stockable_id IN ? AND usable_type = ? AND status IN ?",
|
|
||||||
fifo.StockableKeyPurchaseItems.String(),
|
|
||||||
itemIDs,
|
|
||||||
fifo.UsableKeyProjectChickin.String(),
|
|
||||||
[]string{entity.StockAllocationStatusActive, entity.StockAllocationStatusPending},
|
|
||||||
).
|
|
||||||
Pluck("stockable_id", &usedIDs).Error; err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
usedSet := make(map[uint]struct{}, len(usedIDs))
|
|
||||||
for _, id := range usedIDs {
|
|
||||||
usedSet[id] = struct{}{}
|
|
||||||
}
|
|
||||||
for i := range purchase.Items {
|
|
||||||
if _, ok := usedSet[purchase.Items[i].Id]; ok {
|
|
||||||
purchase.Items[i].HasChickin = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
s.applyTravelDocumentURLs(c.Context(), purchase)
|
s.applyTravelDocumentURLs(c.Context(), purchase)
|
||||||
|
|
||||||
return purchase, nil
|
return purchase, nil
|
||||||
@@ -531,54 +498,6 @@ func (s *purchaseService) ApproveStaffPurchase(c *fiber.Ctx, id uint, req *valid
|
|||||||
return nil, utils.BadRequest("Items must not be empty for staff approval")
|
return nil, utils.BadRequest("Items must not be empty for staff approval")
|
||||||
}
|
}
|
||||||
|
|
||||||
if action == entity.ApprovalActionApproved {
|
|
||||||
itemIDs := make([]uint, 0, len(purchase.Items))
|
|
||||||
itemByID := make(map[uint]entity.PurchaseItem, len(purchase.Items))
|
|
||||||
for i := range purchase.Items {
|
|
||||||
if purchase.Items[i].Id == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
itemIDs = append(itemIDs, purchase.Items[i].Id)
|
|
||||||
itemByID[purchase.Items[i].Id] = purchase.Items[i]
|
|
||||||
}
|
|
||||||
if len(itemIDs) > 0 {
|
|
||||||
var usedIDs []uint
|
|
||||||
if err := s.PurchaseRepo.DB().WithContext(ctx).
|
|
||||||
Model(&entity.StockAllocation{}).
|
|
||||||
Distinct("stockable_id").
|
|
||||||
Where("stockable_type = ? AND stockable_id IN ? AND usable_type = ? AND status IN ?",
|
|
||||||
fifo.StockableKeyPurchaseItems.String(),
|
|
||||||
itemIDs,
|
|
||||||
fifo.UsableKeyProjectChickin.String(),
|
|
||||||
[]string{entity.StockAllocationStatusActive, entity.StockAllocationStatusPending},
|
|
||||||
).
|
|
||||||
Pluck("stockable_id", &usedIDs).Error; err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if len(usedIDs) > 0 {
|
|
||||||
usedSet := make(map[uint]struct{}, len(usedIDs))
|
|
||||||
for _, id := range usedIDs {
|
|
||||||
usedSet[id] = struct{}{}
|
|
||||||
}
|
|
||||||
for _, payload := range req.Items {
|
|
||||||
if payload.PurchaseItemID == 0 || payload.Qty == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if _, used := usedSet[payload.PurchaseItemID]; !used {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
item, ok := itemByID[payload.PurchaseItemID]
|
|
||||||
if !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if *payload.Qty != item.SubQty {
|
|
||||||
return nil, utils.BadRequest("Purchase sudah chickin, qty tidak bisa diubah")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
payload, err := s.buildStaffAdjustmentPayload(c.Context(), purchase, req, syncReceiving)
|
payload, err := s.buildStaffAdjustmentPayload(c.Context(), purchase, req, syncReceiving)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -826,54 +745,6 @@ func (s *purchaseService) ReceiveProducts(c *fiber.Ctx, id uint, req *validation
|
|||||||
req.Items[idx].TravelDocumentPath = &uploadedURL
|
req.Items[idx].TravelDocumentPath = &uploadedURL
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if action == entity.ApprovalActionApproved {
|
|
||||||
itemIDs := make([]uint, 0, len(purchase.Items))
|
|
||||||
itemByID := make(map[uint]entity.PurchaseItem, len(purchase.Items))
|
|
||||||
for i := range purchase.Items {
|
|
||||||
if purchase.Items[i].Id == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
itemIDs = append(itemIDs, purchase.Items[i].Id)
|
|
||||||
itemByID[purchase.Items[i].Id] = purchase.Items[i]
|
|
||||||
}
|
|
||||||
if len(itemIDs) > 0 {
|
|
||||||
var usedIDs []uint
|
|
||||||
if err := s.PurchaseRepo.DB().WithContext(ctx).
|
|
||||||
Model(&entity.StockAllocation{}).
|
|
||||||
Distinct("stockable_id").
|
|
||||||
Where("stockable_type = ? AND stockable_id IN ? AND usable_type = ? AND status IN ?",
|
|
||||||
fifo.StockableKeyPurchaseItems.String(),
|
|
||||||
itemIDs,
|
|
||||||
fifo.UsableKeyProjectChickin.String(),
|
|
||||||
[]string{entity.StockAllocationStatusActive, entity.StockAllocationStatusPending},
|
|
||||||
).
|
|
||||||
Pluck("stockable_id", &usedIDs).Error; err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if len(usedIDs) > 0 {
|
|
||||||
usedSet := make(map[uint]struct{}, len(usedIDs))
|
|
||||||
for _, id := range usedIDs {
|
|
||||||
usedSet[id] = struct{}{}
|
|
||||||
}
|
|
||||||
for _, payload := range req.Items {
|
|
||||||
if _, used := usedSet[payload.PurchaseItemID]; !used {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
item, ok := itemByID[payload.PurchaseItemID]
|
|
||||||
if !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
receivedQty := item.SubQty
|
|
||||||
if payload.ReceivedQty != nil {
|
|
||||||
receivedQty = *payload.ReceivedQty
|
|
||||||
}
|
|
||||||
if receivedQty != item.TotalQty {
|
|
||||||
return nil, utils.BadRequest("Purchase sudah chickin, qty penerimaan tidak bisa diubah")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
||||||
@@ -1041,7 +912,6 @@ func (s *purchaseService) ReceiveProducts(c *fiber.Ctx, id uint, req *validation
|
|||||||
pwID uint
|
pwID uint
|
||||||
qty float64
|
qty float64
|
||||||
}, 0, len(prepared))
|
}, 0, len(prepared))
|
||||||
resolvePendingIDs := make(map[uint]struct{})
|
|
||||||
logEntries := make([]struct {
|
logEntries := make([]struct {
|
||||||
itemID uint
|
itemID uint
|
||||||
pwID uint
|
pwID uint
|
||||||
@@ -1064,15 +934,6 @@ func (s *purchaseService) ReceiveProducts(c *fiber.Ctx, id uint, req *validation
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Safety: ensure the PW we got matches the purchase item product.
|
|
||||||
if pwDetail, err := pwRepoTx.GetDetailByID(c.Context(), pwID); err != nil {
|
|
||||||
return err
|
|
||||||
} else if pwDetail.ProductId != uint(item.ProductId) {
|
|
||||||
return fiber.NewError(
|
|
||||||
fiber.StatusBadRequest,
|
|
||||||
fmt.Sprintf("Product warehouse %d belongs to product %d, not purchase item product %d", pwID, pwDetail.ProductId, item.ProductId),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
newPWID = &pwID
|
newPWID = &pwID
|
||||||
|
|
||||||
deltaQty := prep.receivedQty - item.TotalQty
|
deltaQty := prep.receivedQty - item.TotalQty
|
||||||
@@ -1091,7 +952,6 @@ func (s *purchaseService) ReceiveProducts(c *fiber.Ctx, id uint, req *validation
|
|||||||
pwID uint
|
pwID uint
|
||||||
qty float64
|
qty float64
|
||||||
}{itemID: item.Id, pwID: *newPWID, qty: deltaQty})
|
}{itemID: item.Id, pwID: *newPWID, qty: deltaQty})
|
||||||
resolvePendingIDs[*newPWID] = struct{}{}
|
|
||||||
} else {
|
} else {
|
||||||
deltas[*newPWID] += deltaQty
|
deltas[*newPWID] += deltaQty
|
||||||
totalQtyDeltas[item.Id] += deltaQty
|
totalQtyDeltas[item.Id] += deltaQty
|
||||||
@@ -1104,14 +964,11 @@ func (s *purchaseService) ReceiveProducts(c *fiber.Ctx, id uint, req *validation
|
|||||||
qty float64
|
qty float64
|
||||||
}{itemID: item.Id, pwID: *newPWID, qty: deltaQty})
|
}{itemID: item.Id, pwID: *newPWID, qty: deltaQty})
|
||||||
affected[*newPWID] = struct{}{}
|
affected[*newPWID] = struct{}{}
|
||||||
resolvePendingIDs[*newPWID] = struct{}{}
|
|
||||||
} else {
|
} else {
|
||||||
deltas[*newPWID] += deltaQty // negative
|
deltas[*newPWID] += deltaQty // negative
|
||||||
affected[*newPWID] = struct{}{}
|
affected[*newPWID] = struct{}{}
|
||||||
totalQtyDeltas[item.Id] += deltaQty
|
totalQtyDeltas[item.Id] += deltaQty
|
||||||
}
|
}
|
||||||
case newPWID != nil:
|
|
||||||
resolvePendingIDs[*newPWID] = struct{}{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dateCopy := prep.receivedDate
|
dateCopy := prep.receivedDate
|
||||||
@@ -1209,19 +1066,6 @@ func (s *purchaseService) ReceiveProducts(c *fiber.Ctx, id uint, req *validation
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for pwID := range resolvePendingIDs {
|
|
||||||
if pwID == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
resolved, err := s.FifoSvc.ResolvePending(c.Context(), commonSvc.PendingResolveRequest{
|
|
||||||
ProductWarehouseID: pwID,
|
|
||||||
Tx: tx,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
s.Log.Infof("ResolvePending purchase=%d pw=%d resolved=%d", purchase.Id, pwID, len(resolved))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(logEntries) > 0 {
|
if len(logEntries) > 0 {
|
||||||
@@ -1505,30 +1349,6 @@ func (s *purchaseService) DeletePurchase(c *fiber.Ctx, id uint) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
transactionErr := s.PurchaseRepo.DB().WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
transactionErr := s.PurchaseRepo.DB().WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||||
itemIDs := make([]uint, 0, len(itemsToDelete))
|
|
||||||
for _, item := range itemsToDelete {
|
|
||||||
if item.Id == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
itemIDs = append(itemIDs, item.Id)
|
|
||||||
}
|
|
||||||
if len(itemIDs) > 0 {
|
|
||||||
var count int64
|
|
||||||
if err := tx.Model(&entity.StockAllocation{}).
|
|
||||||
Where("stockable_type = ? AND stockable_id IN ? AND usable_type = ? AND status IN ?",
|
|
||||||
fifo.StockableKeyPurchaseItems.String(),
|
|
||||||
itemIDs,
|
|
||||||
fifo.UsableKeyProjectChickin.String(),
|
|
||||||
[]string{entity.StockAllocationStatusActive, entity.StockAllocationStatusPending},
|
|
||||||
).
|
|
||||||
Count(&count).Error; err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if count > 0 {
|
|
||||||
return utils.BadRequest("Purchase already chickin, failed to delete purchase")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := s.rollbackPurchaseStock(ctx, tx, itemsToDelete, note, actorID); err != nil {
|
if err := s.rollbackPurchaseStock(ctx, tx, itemsToDelete, note, actorID); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -1545,10 +1365,6 @@ func (s *purchaseService) DeletePurchase(c *fiber.Ctx, id uint) error {
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if transactionErr != nil {
|
if transactionErr != nil {
|
||||||
var fe *fiber.Error
|
|
||||||
if errors.As(transactionErr, &fe) {
|
|
||||||
return fe
|
|
||||||
}
|
|
||||||
if errors.Is(transactionErr, gorm.ErrRecordNotFound) {
|
if errors.Is(transactionErr, gorm.ErrRecordNotFound) {
|
||||||
return utils.NotFound("Purchase not found")
|
return utils.NotFound("Purchase not found")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,5 +17,4 @@ const (
|
|||||||
StockableKeyPurchaseItems StockableKey = "PURCHASE_ITEMS"
|
StockableKeyPurchaseItems StockableKey = "PURCHASE_ITEMS"
|
||||||
StockableKeyProjectFlockPopulation StockableKey = "PROJECT_FLOCK_POPULATION"
|
StockableKeyProjectFlockPopulation StockableKey = "PROJECT_FLOCK_POPULATION"
|
||||||
StockableKeyRecordingEgg StockableKey = "RECORDING_EGG"
|
StockableKeyRecordingEgg StockableKey = "RECORDING_EGG"
|
||||||
StockableKeyRecordingDepletion StockableKey = "RECORDING_DEPLETION_IN"
|
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,317 +0,0 @@
|
|||||||
package recording
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
|
||||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
|
||||||
rProductionStandard "gitlab.com/mbugroup/lti-api.git/internal/modules/master/production-standards/repositories"
|
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
|
||||||
|
|
||||||
type warnLogger interface {
|
|
||||||
Warnf(format string, args ...any)
|
|
||||||
}
|
|
||||||
|
|
||||||
type productWarehouseExistsRepo interface {
|
|
||||||
ExistsByID(ctx context.Context, id uint) (bool, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type recordingValidationRepo interface {
|
|
||||||
ValidateProductWarehousesByFlags(ctx context.Context, ids []uint, flags []string) (uint, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
func EnsureProductWarehousesExist(ctx context.Context, repo productWarehouseExistsRepo, ids []uint) error {
|
|
||||||
if repo == nil || len(ids) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
for _, id := range ids {
|
|
||||||
ok, err := repo.ExistsByID(ctx, id)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("product warehouse %d not found", id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func EnsureProductWarehousesByFlags(ctx context.Context, repo recordingValidationRepo, ids []uint, flags []string, label string) error {
|
|
||||||
if repo == nil || len(ids) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
invalidID, err := repo.ValidateProductWarehousesByFlags(ctx, ids, flags)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if invalidID != 0 {
|
|
||||||
return fmt.Errorf("product warehouse %d is not a %s warehouse", invalidID, label)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type idGetter[T any] func(T) uint
|
|
||||||
|
|
||||||
func CollectWarehouseIDs[T any](items []T, getID idGetter[T]) []uint {
|
|
||||||
if len(items) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
ids := make([]uint, 0, len(items))
|
|
||||||
for _, item := range items {
|
|
||||||
if id := getID(item); id != 0 {
|
|
||||||
ids = append(ids, id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ids
|
|
||||||
}
|
|
||||||
|
|
||||||
func EnsureProductWarehousesByFlagsForItems[T any](
|
|
||||||
ctx context.Context,
|
|
||||||
repo recordingValidationRepo,
|
|
||||||
items []T,
|
|
||||||
getID idGetter[T],
|
|
||||||
flags []string,
|
|
||||||
label string,
|
|
||||||
) error {
|
|
||||||
ids := CollectWarehouseIDs(items, getID)
|
|
||||||
return EnsureProductWarehousesByFlags(ctx, repo, ids, flags, label)
|
|
||||||
}
|
|
||||||
|
|
||||||
func ComputeDepletionRate(prevRecording *entity.Recording, currentDepletion float64, totalChick int64) float64 {
|
|
||||||
base := 0.0
|
|
||||||
if prevRecording != nil && prevRecording.TotalChickQty != nil && *prevRecording.TotalChickQty > 0 {
|
|
||||||
base = *prevRecording.TotalChickQty
|
|
||||||
} else if totalChick > 0 {
|
|
||||||
base = float64(totalChick) + currentDepletion
|
|
||||||
}
|
|
||||||
if base <= 0 {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
return (currentDepletion / base) * 100
|
|
||||||
}
|
|
||||||
|
|
||||||
func AttachLatestApprovals(ctx context.Context, items []entity.Recording, approvalSvc commonSvc.ApprovalService, logger warnLogger) error {
|
|
||||||
if len(items) == 0 || approvalSvc == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
ids := make([]uint, 0, len(items))
|
|
||||||
visited := make(map[uint]struct{}, len(items))
|
|
||||||
for _, item := range items {
|
|
||||||
if item.Id == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if _, ok := visited[item.Id]; ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
visited[item.Id] = struct{}{}
|
|
||||||
ids = append(ids, item.Id)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(ids) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
latestMap, err := approvalSvc.LatestByTargets(ctx, utils.ApprovalWorkflowRecording, ids, func(db *gorm.DB) *gorm.DB {
|
|
||||||
return db.Preload("ActionUser")
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
if logger != nil {
|
|
||||||
logger.Warnf("Unable to load latest approvals for recordings: %+v", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(latestMap) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := range items {
|
|
||||||
if items[i].Id == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if approval, ok := latestMap[items[i].Id]; ok {
|
|
||||||
items[i].LatestApproval = approval
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func AttachLatestApproval(ctx context.Context, item *entity.Recording, approvalSvc commonSvc.ApprovalService, logger warnLogger) error {
|
|
||||||
if item == nil || item.Id == 0 || approvalSvc == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
latest, err := approvalSvc.LatestByTarget(ctx, utils.ApprovalWorkflowRecording, item.Id, func(db *gorm.DB) *gorm.DB {
|
|
||||||
return db.Preload("ActionUser")
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
if logger != nil {
|
|
||||||
logger.Warnf("Unable to load approvals for recording %d: %+v", item.Id, err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
item.LatestApproval = latest
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type productionStandardValues struct {
|
|
||||||
HenDay *float64
|
|
||||||
HenHouse *float64
|
|
||||||
FeedIntake *float64
|
|
||||||
MaxDepletion *float64
|
|
||||||
EggMass *float64
|
|
||||||
EggWeight *float64
|
|
||||||
}
|
|
||||||
|
|
||||||
func AttachProductionStandards(ctx context.Context, db *gorm.DB, warnOnly bool, logger warnLogger, items ...*entity.Recording) error {
|
|
||||||
if len(items) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type standardKey struct {
|
|
||||||
standardID uint
|
|
||||||
week int
|
|
||||||
}
|
|
||||||
type standardCacheEntry struct {
|
|
||||||
values productionStandardValues
|
|
||||||
fcr *float64
|
|
||||||
}
|
|
||||||
|
|
||||||
if db == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
standardDetailRepo := rProductionStandard.NewProductionStandardDetailRepository(db)
|
|
||||||
growthDetailRepo := rProductionStandard.NewStandardGrowthDetailRepository(db)
|
|
||||||
cache := make(map[standardKey]standardCacheEntry, len(items))
|
|
||||||
|
|
||||||
standardIDs := make(map[uint]struct{}, len(items))
|
|
||||||
for _, item := range items {
|
|
||||||
if item == nil || item.ProjectFlockKandang == nil || item.ProjectFlockKandang.ProjectFlock.Id == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if item.ProjectFlockKandang.ProjectFlock.ProductionStandardId > 0 {
|
|
||||||
standardIDs[item.ProjectFlockKandang.ProjectFlock.ProductionStandardId] = struct{}{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
standardDetailByStd := make(map[uint]map[int]*entity.ProductionStandardDetail, len(standardIDs))
|
|
||||||
growthDetailByStd := make(map[uint]map[int]*entity.StandardGrowthDetail, len(standardIDs))
|
|
||||||
|
|
||||||
for standardID := range standardIDs {
|
|
||||||
details, err := standardDetailRepo.GetByProductionStandardID(ctx, standardID)
|
|
||||||
if err != nil {
|
|
||||||
if warnOnly {
|
|
||||||
if logger != nil {
|
|
||||||
logger.Warnf("Unable to preload production standard detail for standard %d: %+v", standardID, err)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
detailMap := make(map[int]*entity.ProductionStandardDetail, len(details))
|
|
||||||
for i := range details {
|
|
||||||
detail := details[i]
|
|
||||||
detailMap[detail.Week] = &detail
|
|
||||||
}
|
|
||||||
standardDetailByStd[standardID] = detailMap
|
|
||||||
|
|
||||||
growths, err := growthDetailRepo.GetByProductionStandardID(ctx, standardID)
|
|
||||||
if err != nil {
|
|
||||||
if warnOnly {
|
|
||||||
if logger != nil {
|
|
||||||
logger.Warnf("Unable to preload standard growth detail for standard %d: %+v", standardID, err)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
growthMap := make(map[int]*entity.StandardGrowthDetail, len(growths))
|
|
||||||
for i := range growths {
|
|
||||||
growth := growths[i]
|
|
||||||
growthMap[growth.Week] = &growth
|
|
||||||
}
|
|
||||||
growthDetailByStd[standardID] = growthMap
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, item := range items {
|
|
||||||
if item == nil || item.ProjectFlockKandang == nil || item.ProjectFlockKandang.ProjectFlock.Id == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
standardID := item.ProjectFlockKandang.ProjectFlock.ProductionStandardId
|
|
||||||
if standardID == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
week := RecordingWeekValue(*item)
|
|
||||||
cacheKey := standardKey{standardID: standardID, week: week}
|
|
||||||
if cached, ok := cache[cacheKey]; ok {
|
|
||||||
applyProductionStandardValues(item, cached.values, cached.fcr)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
values := productionStandardValues{}
|
|
||||||
var fcr *float64
|
|
||||||
if detailMap, ok := standardDetailByStd[standardID]; ok {
|
|
||||||
if detail, ok := detailMap[week]; ok {
|
|
||||||
values.HenDay = detail.TargetHenDayProduction
|
|
||||||
values.HenHouse = detail.TargetHenHouseProduction
|
|
||||||
values.EggMass = detail.TargetEggMass
|
|
||||||
values.EggWeight = detail.TargetEggWeight
|
|
||||||
fcr = detail.StandardFCR
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if growthMap, ok := growthDetailByStd[standardID]; ok {
|
|
||||||
if growth, ok := growthMap[week]; ok {
|
|
||||||
values.FeedIntake = growth.FeedIntake
|
|
||||||
values.MaxDepletion = growth.MaxDepletion
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cache[cacheKey] = standardCacheEntry{values: values, fcr: fcr}
|
|
||||||
applyProductionStandardValues(item, values, fcr)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func applyProductionStandardValues(item *entity.Recording, values productionStandardValues, fcr *float64) {
|
|
||||||
item.StandardHenDay = values.HenDay
|
|
||||||
item.StandardHenHouse = values.HenHouse
|
|
||||||
item.StandardFeedIntake = values.FeedIntake
|
|
||||||
item.StandardMaxDepletion = values.MaxDepletion
|
|
||||||
item.StandardEggMass = values.EggMass
|
|
||||||
item.StandardEggWeight = values.EggWeight
|
|
||||||
item.StandardFcr = fcr
|
|
||||||
}
|
|
||||||
|
|
||||||
func RecordingWeekValue(e entity.Recording) int {
|
|
||||||
day := intValue(e.Day)
|
|
||||||
if day <= 0 {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
weekBase := 1
|
|
||||||
if IsLayingRecording(e) {
|
|
||||||
weekBase = 18
|
|
||||||
}
|
|
||||||
return ((day - 1) / 7) + weekBase
|
|
||||||
}
|
|
||||||
|
|
||||||
func IsLayingRecording(e entity.Recording) bool {
|
|
||||||
if e.ProjectFlockKandang == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return strings.EqualFold(e.ProjectFlockKandang.ProjectFlock.Category, string(utils.ProjectFlockCategoryLaying))
|
|
||||||
}
|
|
||||||
|
|
||||||
func intValue(value *int) int {
|
|
||||||
if value == nil {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
return *value
|
|
||||||
}
|
|
||||||
@@ -1,9 +1,6 @@
|
|||||||
package recording
|
package recording
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/validations"
|
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/validations"
|
||||||
)
|
)
|
||||||
@@ -73,87 +70,3 @@ func MapEggs(recordingID uint, createdBy uint, items []validation.Egg) []entity.
|
|||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
type EggTotals struct {
|
|
||||||
Qty int
|
|
||||||
Weight float64
|
|
||||||
}
|
|
||||||
|
|
||||||
func StockUsageByWarehouse(items []entity.RecordingStock) map[uint]float64 {
|
|
||||||
return TotalsByWarehouse(items, func(stock entity.RecordingStock) (uint, float64) {
|
|
||||||
var usage float64
|
|
||||||
if stock.UsageQty != nil {
|
|
||||||
usage = *stock.UsageQty
|
|
||||||
}
|
|
||||||
return stock.ProductWarehouseId, usage
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func StockUsageByWarehouseReq(items []validation.Stock) map[uint]float64 {
|
|
||||||
return TotalsByWarehouse(items, func(item validation.Stock) (uint, float64) {
|
|
||||||
return item.ProductWarehouseId, item.Qty
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func FloatMapsEqual(a, b map[uint]float64) bool {
|
|
||||||
if len(a) != len(b) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
for key, value := range a {
|
|
||||||
other, ok := b[key]
|
|
||||||
if !ok || !floatNearlyEqual(value, other) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func EggTotalsEqual(a, b map[uint]EggTotals) bool {
|
|
||||||
if len(a) != len(b) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
for key, value := range a {
|
|
||||||
other, ok := b[key]
|
|
||||||
if !ok || value.Qty != other.Qty || !floatNearlyEqual(value.Weight, other.Weight) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func floatNearlyEqual(a, b float64) bool {
|
|
||||||
return a-b <= 0.000001 && b-a <= 0.000001
|
|
||||||
}
|
|
||||||
|
|
||||||
func TotalsByWarehouse[T any](items []T, get func(T) (uint, float64)) map[uint]float64 {
|
|
||||||
result := make(map[uint]float64)
|
|
||||||
for _, item := range items {
|
|
||||||
warehouseID, qty := get(item)
|
|
||||||
result[warehouseID] += qty
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func EggTotalsByWarehouse[T any](items []T, get func(T) (uint, int, *float64)) map[uint]EggTotals {
|
|
||||||
result := make(map[uint]EggTotals)
|
|
||||||
for _, item := range items {
|
|
||||||
warehouseID, qty, weightPtr := get(item)
|
|
||||||
weight := 0.0
|
|
||||||
if weightPtr != nil {
|
|
||||||
weight = *weightPtr
|
|
||||||
}
|
|
||||||
current := result[warehouseID]
|
|
||||||
current.Qty += qty
|
|
||||||
current.Weight += weight
|
|
||||||
result[warehouseID] = current
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func RecordingNote(action string, id uint) string {
|
|
||||||
action = strings.TrimSpace(action)
|
|
||||||
if action == "" {
|
|
||||||
return fmt.Sprintf("Recording#%d", id)
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("Recording-%s#%d", action, id)
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user