mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-22 22:35:43 +00:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c4c414aa94 | |||
| c9dee7d1c4 | |||
| 131949874a | |||
| d0f3392738 | |||
| b2e70fa6eb | |||
| 5ba10113c3 | |||
| 29956528e5 | |||
| 9dcccabc6a | |||
| 333cb9e136 | |||
| 3a8cc47fa0 |
+24
-175
@@ -1,186 +1,35 @@
|
|||||||
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}
|
|
||||||
TARGET_PLATFORM: linux/amd64
|
|
||||||
|
|
||||||
DOCKER_HOST: unix:///var/run/docker.sock
|
|
||||||
DOCKER_TLS_CERTDIR: ""
|
|
||||||
DOCKER_BUILDKIT: "1"
|
|
||||||
|
|
||||||
workflow:
|
workflow:
|
||||||
rules:
|
rules:
|
||||||
# run untuk branch utama & MR
|
# MR pipeline
|
||||||
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "development"'
|
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
|
||||||
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "production"'
|
when: always
|
||||||
- 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:
|
||||||
# Helper: login ECR
|
# khusus MR (notif)
|
||||||
# =========================
|
- local: "ci/merge_request.yml"
|
||||||
.ecr_login: &ecr_login |
|
|
||||||
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
|
|
||||||
|
|
||||||
PASS="$(docker run --rm $AWS_CLI_ENV_ARGS public.ecr.aws/aws-cli/aws-cli:latest \
|
|
||||||
ecr get-login-password --region "$AWS_REGION" || true)"
|
|
||||||
if [ -z "$PASS" ]; then
|
|
||||||
echo "ERROR: Failed to get ECR login password."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
echo "$PASS" | docker login --username AWS --password-stdin "$ECR_REGISTRY"
|
|
||||||
|
|
||||||
# =========================
|
|
||||||
# MR
|
|
||||||
# =========================
|
|
||||||
build_mr:
|
|
||||||
stage: build
|
|
||||||
image: public.ecr.aws/docker/library/docker:27
|
|
||||||
tags: [self-hosted-dev]
|
|
||||||
rules:
|
rules:
|
||||||
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "production"'
|
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
|
||||||
variables:
|
|
||||||
IMAGE_TAG: "prod-${CI_COMMIT_SHORT_SHA}"
|
|
||||||
before_script:
|
|
||||||
- set -eu
|
|
||||||
- docker version
|
|
||||||
- docker info
|
|
||||||
- *ecr_login
|
|
||||||
script: |
|
|
||||||
set -eu
|
|
||||||
# force base image pulls via AWS ECR Public to avoid Docker Hub TLS timeout
|
|
||||||
sed -i 's|^FROM golang:1.23-alpine AS builder$|FROM public.ecr.aws/docker/library/golang:1.23-alpine AS builder|' Dockerfile
|
|
||||||
sed -i 's|^FROM alpine:3.20$|FROM public.ecr.aws/docker/library/alpine:3.20|' Dockerfile
|
|
||||||
echo "Build (MR) : $ECR_REPOSITORY:$IMAGE_TAG"
|
|
||||||
docker build --platform "$TARGET_PLATFORM" -f Dockerfile -t "$ECR_REPOSITORY:$IMAGE_TAG" .
|
|
||||||
echo "Pushing image for MR..."
|
|
||||||
docker push "$ECR_REPOSITORY:$IMAGE_TAG"
|
|
||||||
|
|
||||||
# =========================
|
# khusus push ke branch env
|
||||||
# DEVELOPMENT (push branch development)
|
- local: "ci/development.yml"
|
||||||
# =========================
|
|
||||||
build_push_dev:
|
|
||||||
stage: build
|
|
||||||
image: public.ecr.aws/docker/library/docker:27
|
|
||||||
tags: [self-hosted-dev]
|
|
||||||
rules:
|
rules:
|
||||||
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "development"'
|
- if: '$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
|
|
||||||
# force base image pulls via AWS ECR Public to avoid Docker Hub TLS timeout
|
|
||||||
sed -i 's|^FROM golang:1.23-alpine AS builder$|FROM public.ecr.aws/docker/library/golang:1.23-alpine AS builder|' Dockerfile
|
|
||||||
sed -i 's|^FROM alpine:3.20$|FROM public.ecr.aws/docker/library/alpine:3.20|' Dockerfile
|
|
||||||
echo "Build & push (dev): $ECR_REPOSITORY:$IMAGE_TAG"
|
|
||||||
docker build --platform "$TARGET_PLATFORM" -f Dockerfile -t "$ECR_REPOSITORY:$IMAGE_TAG" .
|
|
||||||
docker push "$ECR_REPOSITORY:$IMAGE_TAG"
|
|
||||||
|
|
||||||
update_gitops_dev_lti:
|
- local: "ci/staging.yml"
|
||||||
stage: gitops
|
|
||||||
image: public.ecr.aws/docker/library/alpine:3.20
|
|
||||||
tags: [self-hosted-dev]
|
|
||||||
rules:
|
rules:
|
||||||
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "development"'
|
- if: '$CI_COMMIT_BRANCH == "staging"'
|
||||||
needs: ["build_push_dev"]
|
|
||||||
variables:
|
|
||||||
IMAGE_TAG: "dev-${CI_COMMIT_SHORT_SHA}"
|
|
||||||
GITOPS_BRANCH: main
|
|
||||||
VALUES_FILE: environments/lti/dev/lti-values-dev.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 dev image.tag to $IMAGE_TAG"
|
- local: "ci/production.yml"
|
||||||
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 dev deploy ${IMAGE_TAG}"
|
|
||||||
git push origin "$GITOPS_BRANCH"
|
|
||||||
|
|
||||||
# =========================
|
|
||||||
# PRODUCTION (push branch production)
|
|
||||||
# =========================
|
|
||||||
build_push_prod:
|
|
||||||
stage: build
|
|
||||||
image: public.ecr.aws/docker/library/docker:27
|
|
||||||
tags: [self-hosted-dev]
|
|
||||||
rules:
|
rules:
|
||||||
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "production"'
|
- if: '$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
|
|
||||||
# force base image pulls via AWS ECR Public to avoid Docker Hub TLS timeout
|
|
||||||
sed -i 's|^FROM golang:1.23-alpine AS builder$|FROM public.ecr.aws/docker/library/golang:1.23-alpine AS builder|' Dockerfile
|
|
||||||
sed -i 's|^FROM alpine:3.20$|FROM public.ecr.aws/docker/library/alpine:3.20|' Dockerfile
|
|
||||||
echo "Build & push (prod): $ECR_REPOSITORY:$IMAGE_TAG"
|
|
||||||
docker build --platform "$TARGET_PLATFORM" -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"
|
|
||||||
|
|||||||
+3
-8
@@ -1,7 +1,7 @@
|
|||||||
# =========================
|
# =========================
|
||||||
# Builder stage
|
# Builder stage
|
||||||
# =========================
|
# =========================
|
||||||
FROM public.ecr.aws/docker/library/golang:1.23-alpine AS builder
|
FROM golang:1.23-alpine AS builder
|
||||||
|
|
||||||
RUN apk add --no-cache git ca-certificates tzdata
|
RUN apk add --no-cache git ca-certificates tzdata
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
@@ -15,17 +15,14 @@ COPY . .
|
|||||||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
|
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
|
||||||
go build -trimpath -ldflags="-s -w" -o lti-api ./cmd/api
|
go build -trimpath -ldflags="-s -w" -o lti-api ./cmd/api
|
||||||
|
|
||||||
# Build SEED binary
|
# Build SEED binary (pastikan cmd/seed ada)
|
||||||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
|
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
|
||||||
go build -trimpath -ldflags="-s -w" -o lti-seed ./cmd/seed
|
go build -trimpath -ldflags="-s -w" -o lti-seed ./cmd/seed
|
||||||
|
|
||||||
# Build migrate CLI with postgres + file drivers
|
|
||||||
RUN GOBIN=/usr/local/bin go install -tags "postgres file" -ldflags="-s -w" github.com/golang-migrate/migrate/v4/cmd/migrate@v4.18.3
|
|
||||||
|
|
||||||
# =========================
|
# =========================
|
||||||
# Runtime stage
|
# Runtime stage
|
||||||
# =========================
|
# =========================
|
||||||
FROM public.ecr.aws/docker/library/alpine:3.20
|
FROM alpine:3.20
|
||||||
|
|
||||||
RUN apk add --no-cache ca-certificates tzdata curl bash postgresql-client \
|
RUN apk add --no-cache ca-certificates tzdata curl bash postgresql-client \
|
||||||
&& adduser -D -H -u 10001 appuser
|
&& adduser -D -H -u 10001 appuser
|
||||||
@@ -34,8 +31,6 @@ WORKDIR /app
|
|||||||
|
|
||||||
COPY --from=builder /app/lti-api /app/lti-api
|
COPY --from=builder /app/lti-api /app/lti-api
|
||||||
COPY --from=builder /app/lti-seed /app/lti-seed
|
COPY --from=builder /app/lti-seed /app/lti-seed
|
||||||
COPY --from=builder /usr/local/bin/migrate /app/migrate
|
|
||||||
COPY --from=builder /app/internal/database/migrations /app/migrations
|
|
||||||
|
|
||||||
USER appuser
|
USER appuser
|
||||||
EXPOSE 8081
|
EXPOSE 8081
|
||||||
|
|||||||
@@ -111,4 +111,3 @@ 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
|
|
||||||
|
|||||||
+73
-141
@@ -1,159 +1,91 @@
|
|||||||
stages:
|
stages:
|
||||||
- build
|
- deploy
|
||||||
- gitops
|
|
||||||
|
|
||||||
variables:
|
deploy-dev:
|
||||||
AWS_REGION: ap-southeast-3
|
stage: deploy
|
||||||
ECR_REGISTRY: 886436954922.dkr.ecr.ap-southeast-3.amazonaws.com
|
image: alpine:3.20
|
||||||
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:
|
|
||||||
rules:
|
|
||||||
- if: '$CI_COMMIT_BRANCH'
|
|
||||||
|
|
||||||
# =========================
|
|
||||||
# Helper: login ECR
|
|
||||||
# =========================
|
|
||||||
.ecr_login: &ecr_login |
|
|
||||||
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
|
|
||||||
|
|
||||||
PASS="$(docker run --rm $AWS_CLI_ENV_ARGS public.ecr.aws/aws-cli/aws-cli:latest \
|
|
||||||
ecr get-login-password --region "$AWS_REGION" || true)"
|
|
||||||
if [ -z "$PASS" ]; then
|
|
||||||
echo "ERROR: Failed to get ECR login password."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
echo "$PASS" | docker login --username AWS --password-stdin "$ECR_REGISTRY"
|
|
||||||
|
|
||||||
# =========================
|
|
||||||
# DEV
|
|
||||||
# =========================
|
|
||||||
build_push_dev_lti:
|
|
||||||
stage: build
|
|
||||||
image: public.ecr.aws/docker/library/docker:27
|
|
||||||
tags: [self-hosted-dev]
|
|
||||||
rules:
|
rules:
|
||||||
- if: '$CI_COMMIT_BRANCH == "development"'
|
- if: '$CI_COMMIT_BRANCH == "development"'
|
||||||
|
when: on_success
|
||||||
|
- when: never
|
||||||
|
|
||||||
variables:
|
variables:
|
||||||
IMAGE_TAG: "dev-${CI_COMMIT_SHORT_SHA}"
|
DEPLOY_APP: "LTI-MBUGROUP"
|
||||||
|
GIT_SUBMODULE_STRATEGY: recursive
|
||||||
|
GIT_DEPTH: "1"
|
||||||
|
|
||||||
before_script:
|
before_script:
|
||||||
- set -eu
|
- echo "🧰 Installing dependencies..."
|
||||||
- docker version
|
- apk update && apk add --no-cache openssh git curl bash
|
||||||
- docker info
|
|
||||||
- *ecr_login
|
|
||||||
script: |
|
|
||||||
set -eu
|
|
||||||
echo "Build & push: $ECR_REPOSITORY:$IMAGE_TAG"
|
|
||||||
|
|
||||||
docker build \
|
# Setup SSH di runner
|
||||||
-t "$ECR_REPOSITORY:$IMAGE_TAG" \
|
- mkdir -p ~/.ssh
|
||||||
.
|
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa
|
||||||
|
- chmod 600 ~/.ssh/id_rsa
|
||||||
|
- eval "$(ssh-agent -s)"
|
||||||
|
- ssh-add ~/.ssh/id_rsa
|
||||||
|
|
||||||
docker push "$ECR_REPOSITORY:$IMAGE_TAG"
|
# Trust host keys (server + gitlab) biar SSH gak nanya interaktif
|
||||||
|
- ssh-keyscan -H "$SERVER_IP" >> ~/.ssh/known_hosts
|
||||||
|
- ssh-keyscan -H gitlab.com >> ~/.ssh/known_hosts
|
||||||
|
|
||||||
update_gitops_dev_lti:
|
script:
|
||||||
stage: gitops
|
- echo "🚀 Deploying latest code to $SERVER_USER@$SERVER_IP"
|
||||||
image: public.ecr.aws/docker/library/alpine:3.20
|
- >
|
||||||
tags: [self-hosted-dev]
|
if ssh -o StrictHostKeyChecking=no "$SERVER_USER@$SERVER_IP" "
|
||||||
rules:
|
set -e
|
||||||
- if: '$CI_COMMIT_BRANCH == "development"'
|
|
||||||
needs: ["build_push_dev_lti"]
|
|
||||||
variables:
|
|
||||||
IMAGE_TAG: "dev-${CI_COMMIT_SHORT_SHA}"
|
|
||||||
GITOPS_BRANCH: main
|
|
||||||
VALUES_FILE: environments/lti/dev/lti-values-dev.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 DEV image.tag to $IMAGE_TAG in $VALUES_FILE"
|
cd /home/devops/docker/deployment/development/lti-api
|
||||||
yq -i '.image.repository = strenv(ECR_REPOSITORY)' "$VALUES_FILE"
|
|
||||||
yq -i '.image.tag = strenv(IMAGE_TAG)' "$VALUES_FILE"
|
|
||||||
|
|
||||||
git add "$VALUES_FILE"
|
# Pastikan remote origin SSH (antisipasi kalau pernah ke-set HTTPS)
|
||||||
if git diff --cached --quiet; then
|
git remote set-url origin git@gitlab.com:mbugroup/lti-api.git
|
||||||
echo "No changes to commit"
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
git commit -m "lti dev deploy ${IMAGE_TAG}"
|
|
||||||
git push origin "$GITOPS_BRANCH"
|
|
||||||
|
|
||||||
# =========================
|
# Pastikan server percaya gitlab.com juga (untuk git fetch via SSH)
|
||||||
# PROD
|
mkdir -p ~/.ssh
|
||||||
# =========================
|
ssh-keyscan -H gitlab.com >> ~/.ssh/known_hosts
|
||||||
# build_push_prod_lti:
|
|
||||||
# stage: build
|
|
||||||
# image: public.ecr.aws/docker/library/docker:27
|
|
||||||
# tags: [self-hosted-dev]
|
|
||||||
# rules:
|
|
||||||
# - if: '$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: $ECR_REPOSITORY:$IMAGE_TAG"
|
|
||||||
|
|
||||||
# docker build \
|
# Fetch/reset pakai SSH
|
||||||
# -t "$ECR_REPOSITORY:$IMAGE_TAG" \
|
GIT_SSH_COMMAND='ssh -o StrictHostKeyChecking=no' git fetch origin development
|
||||||
# .
|
git reset --hard origin/development
|
||||||
|
|
||||||
# docker push "$ECR_REPOSITORY:$IMAGE_TAG"
|
docker compose restart dev-api-lti || docker compose up -d dev-api-lti
|
||||||
|
"; then
|
||||||
|
STATUS='success';
|
||||||
|
else
|
||||||
|
STATUS='failed';
|
||||||
|
fi;
|
||||||
|
|
||||||
# update_gitops_prod_lti:
|
RUN_URL="${CI_PROJECT_URL}/-/pipelines/${CI_PIPELINE_ID}";
|
||||||
# stage: gitops
|
|
||||||
# image: public.ecr.aws/docker/library/alpine:3.20
|
|
||||||
# tags: [self-hosted-dev]
|
|
||||||
# rules:
|
|
||||||
# - if: '$CI_COMMIT_BRANCH == "production"'
|
|
||||||
# needs: ["build_push_prod_lti"]
|
|
||||||
# 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 in $VALUES_FILE"
|
if [ "$STATUS" = "success" ]; then
|
||||||
# yq -i '.image.repository = strenv(ECR_REPOSITORY)' "$VALUES_FILE"
|
COLOR=3066993;
|
||||||
# yq -i '.image.tag = strenv(IMAGE_TAG)' "$VALUES_FILE"
|
TITLE="✅ Deployment API Succeeded";
|
||||||
|
DESC="Deployment job on branch \`${CI_COMMIT_REF_NAME}\` completed successfully.";
|
||||||
|
else
|
||||||
|
COLOR=15158332;
|
||||||
|
TITLE="❌ Deployment API Failed Gaes";
|
||||||
|
DESC="Deployment job on branch \`${CI_COMMIT_REF_NAME}\` failed.";
|
||||||
|
fi;
|
||||||
|
|
||||||
# git add "$VALUES_FILE"
|
echo "{
|
||||||
# if git diff --cached --quiet; then
|
\"username\": \"CI Bot\",
|
||||||
# echo "No changes to commit"
|
\"embeds\": [{
|
||||||
# exit 0
|
\"title\": \"$TITLE\",
|
||||||
# fi
|
\"description\": \"$DESC\",
|
||||||
# git commit -m "lti prod deploy ${IMAGE_TAG}"
|
\"color\": $COLOR,
|
||||||
# git push origin "$GITOPS_BRANCH"
|
\"fields\": [
|
||||||
|
{\"name\": \"Repository\", \"value\": \"${CI_PROJECT_PATH}\", \"inline\": true},
|
||||||
|
{\"name\": \"Actor\", \"value\": \"${GITLAB_USER_LOGIN}\", \"inline\": true},
|
||||||
|
{\"name\": \"Commit\", \"value\": \"${CI_COMMIT_SHA}\", \"inline\": false},
|
||||||
|
{\"name\": \"Pipeline\", \"value\": \"[Open run](${RUN_URL})\", \"inline\": false}
|
||||||
|
]
|
||||||
|
}]
|
||||||
|
}" > payload.json;
|
||||||
|
|
||||||
|
echo "📡 Sending notification to Discord...";
|
||||||
|
curl -sS -H "Content-Type: application/json" \
|
||||||
|
-d @payload.json "$DISCORD_WEBHOOK_URL";
|
||||||
|
|
||||||
|
environment:
|
||||||
|
name: development
|
||||||
|
|||||||
@@ -0,0 +1,104 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type FifoPendingPolicyInput struct {
|
||||||
|
Lane string
|
||||||
|
FlagGroupCode string
|
||||||
|
FunctionCode string
|
||||||
|
LegacyTypeKey string
|
||||||
|
}
|
||||||
|
|
||||||
|
type FifoPendingPolicyResult struct {
|
||||||
|
AllowPending bool
|
||||||
|
RuleSource string
|
||||||
|
Found bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func ResolveFifoPendingPolicy(ctx context.Context, tx *gorm.DB, input FifoPendingPolicyInput) (*FifoPendingPolicyResult, error) {
|
||||||
|
if tx == nil {
|
||||||
|
return nil, gorm.ErrInvalidDB
|
||||||
|
}
|
||||||
|
|
||||||
|
lane := strings.ToUpper(strings.TrimSpace(input.Lane))
|
||||||
|
flagGroupCode := strings.ToUpper(strings.TrimSpace(input.FlagGroupCode))
|
||||||
|
functionCode := strings.ToUpper(strings.TrimSpace(input.FunctionCode))
|
||||||
|
legacyTypeKey := strings.ToUpper(strings.TrimSpace(input.LegacyTypeKey))
|
||||||
|
if lane == "" {
|
||||||
|
return &FifoPendingPolicyResult{
|
||||||
|
AllowPending: false,
|
||||||
|
RuleSource: "SAFE_DEFAULT_BLOCK",
|
||||||
|
Found: false,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type overconsumeRuleRow struct {
|
||||||
|
Allow bool `gorm:"column:allow_overconsume"`
|
||||||
|
}
|
||||||
|
var overconsume overconsumeRuleRow
|
||||||
|
overconsumeErr := tx.WithContext(ctx).
|
||||||
|
Table("fifo_stock_v2_overconsume_rules").
|
||||||
|
Select("allow_overconsume").
|
||||||
|
Where("is_active = TRUE").
|
||||||
|
Where("lane = ?", lane).
|
||||||
|
Where("(flag_group_code IS NULL OR flag_group_code = ?)", flagGroupCode).
|
||||||
|
Where("(function_code IS NULL OR function_code = ?)", functionCode).
|
||||||
|
Order("CASE WHEN flag_group_code IS NULL THEN 1 ELSE 0 END ASC").
|
||||||
|
Order("CASE WHEN function_code IS NULL THEN 1 ELSE 0 END ASC").
|
||||||
|
Order("priority ASC, id ASC").
|
||||||
|
Limit(1).
|
||||||
|
Take(&overconsume).Error
|
||||||
|
if overconsumeErr == nil {
|
||||||
|
return &FifoPendingPolicyResult{
|
||||||
|
AllowPending: overconsume.Allow,
|
||||||
|
RuleSource: "OVERCONSUME_RULE",
|
||||||
|
Found: true,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
if !errors.Is(overconsumeErr, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, overconsumeErr
|
||||||
|
}
|
||||||
|
|
||||||
|
type routeRuleRow struct {
|
||||||
|
AllowPendingDefault bool `gorm:"column:allow_pending_default"`
|
||||||
|
}
|
||||||
|
var routeRule routeRuleRow
|
||||||
|
routeQuery := tx.WithContext(ctx).
|
||||||
|
Table("fifo_stock_v2_route_rules").
|
||||||
|
Select("allow_pending_default").
|
||||||
|
Where("is_active = TRUE").
|
||||||
|
Where("lane = ?", lane).
|
||||||
|
Where("flag_group_code = ?", flagGroupCode)
|
||||||
|
if legacyTypeKey != "" {
|
||||||
|
routeQuery = routeQuery.Where("legacy_type_key = ?", legacyTypeKey)
|
||||||
|
}
|
||||||
|
if functionCode != "" {
|
||||||
|
routeQuery = routeQuery.Where("function_code = ?", functionCode)
|
||||||
|
}
|
||||||
|
routeErr := routeQuery.
|
||||||
|
Order("id ASC").
|
||||||
|
Limit(1).
|
||||||
|
Take(&routeRule).Error
|
||||||
|
if routeErr == nil {
|
||||||
|
return &FifoPendingPolicyResult{
|
||||||
|
AllowPending: routeRule.AllowPendingDefault,
|
||||||
|
RuleSource: "ROUTE_RULE_DEFAULT",
|
||||||
|
Found: true,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
if !errors.Is(routeErr, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, routeErr
|
||||||
|
}
|
||||||
|
|
||||||
|
return &FifoPendingPolicyResult{
|
||||||
|
AllowPending: false,
|
||||||
|
RuleSource: "SAFE_DEFAULT_BLOCK",
|
||||||
|
Found: false,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
@@ -220,6 +220,9 @@ func shouldSkipStockableForUsable(req AllocateRequest, stockableType string) boo
|
|||||||
if (usableType == "PROJECT_CHICKIN" || functionCode == "CHICKIN_OUT") && stockable == "PROJECT_FLOCK_POPULATION" {
|
if (usableType == "PROJECT_CHICKIN" || functionCode == "CHICKIN_OUT") && stockable == "PROJECT_FLOCK_POPULATION" {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
if (usableType == "STOCK_TRANSFER_OUT" || functionCode == "STOCK_TRANSFER_OUT") && stockable == "PROJECT_FLOCK_POPULATION" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -496,10 +499,6 @@ func (s *fifoStockV2Service) Reflow(ctx context.Context, req ReflowRequest) (*Re
|
|||||||
if len(rollbackRes.Details) > 0 {
|
if len(rollbackRes.Details) > 0 {
|
||||||
result.Rollback.Details = append(result.Rollback.Details, rollbackRes.Details...)
|
result.Rollback.Details = append(result.Rollback.Details, rollbackRes.Details...)
|
||||||
}
|
}
|
||||||
minDesired := rollbackRes.ReleasedQty + usableRow.PendingQuantity
|
|
||||||
if desiredQty < minDesired {
|
|
||||||
desiredQty = minDesired
|
|
||||||
}
|
|
||||||
|
|
||||||
if desiredQty <= 0 {
|
if desiredQty <= 0 {
|
||||||
continue
|
continue
|
||||||
@@ -702,16 +701,17 @@ func (s *fifoStockV2Service) resolveRollbackFlagGroup(ctx context.Context, tx *g
|
|||||||
FlagGroupCode string `gorm:"column:flag_group_code"`
|
FlagGroupCode string `gorm:"column:flag_group_code"`
|
||||||
}
|
}
|
||||||
var latest row
|
var latest row
|
||||||
err := tx.WithContext(ctx).
|
latestQuery := tx.WithContext(ctx).
|
||||||
Table("stock_allocations").
|
Table("stock_allocations").
|
||||||
Select("flag_group_code").
|
Select("flag_group_code").
|
||||||
Where("usable_type = ? AND usable_id = ?", req.Usable.LegacyTypeKey, req.Usable.ID).
|
Where("usable_type = ? AND usable_id = ?", req.Usable.LegacyTypeKey, req.Usable.ID).
|
||||||
Where("engine_version = 'v2'").
|
Where("engine_version = 'v2'").
|
||||||
Where("allocation_purpose = ?", defaultAllocationPurpose()).
|
Where("allocation_purpose = ?", defaultAllocationPurpose()).
|
||||||
Where("flag_group_code IS NOT NULL AND flag_group_code <> ''").
|
Where("flag_group_code IS NOT NULL AND flag_group_code <> ''")
|
||||||
Order("id DESC").
|
if code := strings.TrimSpace(req.Usable.FunctionCode); code != "" {
|
||||||
Limit(1).
|
latestQuery = latestQuery.Where("function_code = ?", code)
|
||||||
Take(&latest).Error
|
}
|
||||||
|
err := latestQuery.Order("id DESC").Limit(1).Take(&latest).Error
|
||||||
if err == nil && strings.TrimSpace(latest.FlagGroupCode) != "" {
|
if err == nil && strings.TrimSpace(latest.FlagGroupCode) != "" {
|
||||||
return latest.FlagGroupCode, nil
|
return latest.FlagGroupCode, nil
|
||||||
}
|
}
|
||||||
@@ -719,19 +719,56 @@ func (s *fifoStockV2Service) resolveRollbackFlagGroup(ctx context.Context, tx *g
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
var rules []routeRule
|
rulesQuery := tx.WithContext(ctx).
|
||||||
err = tx.WithContext(ctx).
|
|
||||||
Table("fifo_stock_v2_route_rules").
|
Table("fifo_stock_v2_route_rules").
|
||||||
Where("is_active = TRUE").
|
Where("is_active = TRUE").
|
||||||
Where("lane = ?", string(LaneUsable)).
|
Where("lane = ?", string(LaneUsable)).
|
||||||
Where("legacy_type_key = ?", req.Usable.LegacyTypeKey).
|
Where("legacy_type_key = ?", req.Usable.LegacyTypeKey)
|
||||||
Find(&rules).Error
|
if code := strings.TrimSpace(req.Usable.FunctionCode); code != "" {
|
||||||
|
rulesQuery = rulesQuery.Where("function_code = ?", code)
|
||||||
|
}
|
||||||
|
|
||||||
|
var rules []routeRule
|
||||||
|
err = rulesQuery.Find(&rules).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if len(rules) == 0 {
|
if len(rules) == 0 {
|
||||||
return "", fmt.Errorf("cannot resolve flag group for usable type %s", req.Usable.LegacyTypeKey)
|
return "", fmt.Errorf("cannot resolve flag group for usable type %s", req.Usable.LegacyTypeKey)
|
||||||
}
|
}
|
||||||
|
if len(rules) > 1 && req.ProductWarehouseID != 0 {
|
||||||
|
type candidateRow struct {
|
||||||
|
FlagGroupCode string `gorm:"column:flag_group_code"`
|
||||||
|
}
|
||||||
|
var candidates []candidateRow
|
||||||
|
byProductQuery := tx.WithContext(ctx).
|
||||||
|
Table("fifo_stock_v2_route_rules rr").
|
||||||
|
Select("DISTINCT rr.flag_group_code").
|
||||||
|
Joins("JOIN fifo_stock_v2_flag_groups fg ON fg.code = rr.flag_group_code AND fg.is_active = TRUE").
|
||||||
|
Where("rr.is_active = TRUE").
|
||||||
|
Where("rr.lane = ?", string(LaneUsable)).
|
||||||
|
Where("rr.legacy_type_key = ?", req.Usable.LegacyTypeKey).
|
||||||
|
Where(`
|
||||||
|
EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM product_warehouses pw
|
||||||
|
JOIN flags f ON f.flagable_id = pw.product_id
|
||||||
|
JOIN fifo_stock_v2_flag_members fm ON fm.flag_name = f.name AND fm.is_active = TRUE
|
||||||
|
WHERE pw.id = ?
|
||||||
|
AND f.flagable_type = 'products'
|
||||||
|
AND fm.flag_group_code = rr.flag_group_code
|
||||||
|
)
|
||||||
|
`, req.ProductWarehouseID)
|
||||||
|
if code := strings.TrimSpace(req.Usable.FunctionCode); code != "" {
|
||||||
|
byProductQuery = byProductQuery.Where("rr.function_code = ?", code)
|
||||||
|
}
|
||||||
|
if err := byProductQuery.Order("rr.flag_group_code ASC").Scan(&candidates).Error; err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if len(candidates) == 1 {
|
||||||
|
return strings.TrimSpace(candidates[0].FlagGroupCode), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
if len(rules) > 1 {
|
if len(rules) > 1 {
|
||||||
return "", fmt.Errorf("ambiguous rollback flag group for usable type %s", req.Usable.LegacyTypeKey)
|
return "", fmt.Errorf("ambiguous rollback flag group for usable type %s", req.Usable.LegacyTypeKey)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,7 +57,6 @@ var (
|
|||||||
SSOPortalURL string
|
SSOPortalURL string
|
||||||
SSOClients map[string]SSOClientConfig
|
SSOClients map[string]SSOClientConfig
|
||||||
SSOAccessCookieName string
|
SSOAccessCookieName string
|
||||||
SSOAccessCookieFallback []string
|
|
||||||
SSORefreshCookieName string
|
SSORefreshCookieName string
|
||||||
SSOCookieDomain string
|
SSOCookieDomain string
|
||||||
SSOCookieSecure bool
|
SSOCookieSecure bool
|
||||||
@@ -108,7 +107,7 @@ func init() {
|
|||||||
JWTResetPasswordExp = viper.GetInt("JWT_RESET_PASSWORD_EXP_MINUTES")
|
JWTResetPasswordExp = viper.GetInt("JWT_RESET_PASSWORD_EXP_MINUTES")
|
||||||
JWTVerifyEmailExp = viper.GetInt("JWT_VERIFY_EMAIL_EXP_MINUTES")
|
JWTVerifyEmailExp = viper.GetInt("JWT_VERIFY_EMAIL_EXP_MINUTES")
|
||||||
|
|
||||||
// Cors
|
//Cors
|
||||||
CORSAllowOrigins = parseList("CORS_ALLOW_ORIGINS")
|
CORSAllowOrigins = parseList("CORS_ALLOW_ORIGINS")
|
||||||
CORSAllowMethods = parseListWithDefault("CORS_ALLOW_METHODS", "GET,POST,PUT,PATCH,DELETE,OPTIONS")
|
CORSAllowMethods = parseListWithDefault("CORS_ALLOW_METHODS", "GET,POST,PUT,PATCH,DELETE,OPTIONS")
|
||||||
CORSAllowHeaders = parseListWithDefault("CORS_ALLOW_HEADERS", "Content-Type,Authorization,X-Requested-With")
|
CORSAllowHeaders = parseListWithDefault("CORS_ALLOW_HEADERS", "Content-Type,Authorization,X-Requested-With")
|
||||||
@@ -148,7 +147,6 @@ func init() {
|
|||||||
SSOGetMeURL = viper.GetString("SSO_GETME_URL")
|
SSOGetMeURL = viper.GetString("SSO_GETME_URL")
|
||||||
SSOPortalURL = strings.TrimSpace(viper.GetString("SSO_PORTAL_URL"))
|
SSOPortalURL = strings.TrimSpace(viper.GetString("SSO_PORTAL_URL"))
|
||||||
SSOAccessCookieName = defaultString(viper.GetString("SSO_ACCESS_COOKIE_NAME"), "sso_access")
|
SSOAccessCookieName = defaultString(viper.GetString("SSO_ACCESS_COOKIE_NAME"), "sso_access")
|
||||||
SSOAccessCookieFallback = parseList("SSO_ACCESS_COOKIE_FALLBACK")
|
|
||||||
SSORefreshCookieName = defaultString(viper.GetString("SSO_REFRESH_COOKIE_NAME"), "sso_refresh")
|
SSORefreshCookieName = defaultString(viper.GetString("SSO_REFRESH_COOKIE_NAME"), "sso_refresh")
|
||||||
SSOCookieDomain = viper.GetString("SSO_COOKIE_DOMAIN")
|
SSOCookieDomain = viper.GetString("SSO_COOKIE_DOMAIN")
|
||||||
SSOCookieSecure = viper.GetBool("SSO_COOKIE_SECURE")
|
SSOCookieSecure = viper.GetBool("SSO_COOKIE_SECURE")
|
||||||
@@ -261,6 +259,10 @@ func defaultString(v, def string) string {
|
|||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func LayingWeekStart() int {
|
||||||
|
return TransferToLayingGrowingMaxWeek
|
||||||
|
}
|
||||||
|
|
||||||
func joinPath(parts ...string) string {
|
func joinPath(parts ...string) string {
|
||||||
out := make([]string, 0, len(parts))
|
out := make([]string, 0, len(parts))
|
||||||
for _, part := range parts {
|
for _, part := range parts {
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ func Connect(dbHost, dbName string) *gorm.DB {
|
|||||||
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
|
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
|
||||||
Logger: logger.Default.LogMode(logger.Info),
|
Logger: logger.Default.LogMode(logger.Info),
|
||||||
SkipDefaultTransaction: true,
|
SkipDefaultTransaction: true,
|
||||||
PrepareStmt: false,
|
PrepareStmt: true,
|
||||||
TranslateError: true,
|
TranslateError: true,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -1,6 +1,250 @@
|
|||||||
BEGIN;
|
BEGIN;
|
||||||
|
|
||||||
-- no-op: moved to 20260306090010_seed_fifo_stock_v2_config_after_core.up.sql
|
INSERT INTO fifo_stock_v2_flag_groups(code, name, priority)
|
||||||
-- to ensure FIFO core tables exist before seeding on fresh migrations.
|
VALUES
|
||||||
|
('AYAM', 'AYAM', 10),
|
||||||
|
('AFKIR_CULLING_MATI', 'AFKIR/CULLING/MATI', 20),
|
||||||
|
('PAKAN', 'PAKAN', 30),
|
||||||
|
('OVK', 'OVK', 40),
|
||||||
|
('TELUR', 'TELUR', 50),
|
||||||
|
('TELUR_GRADE', 'UTUH/PUTIH/RETAK/PECAH/PAPACAL/JUMBO', 60)
|
||||||
|
ON CONFLICT (code) DO UPDATE
|
||||||
|
SET
|
||||||
|
name = EXCLUDED.name,
|
||||||
|
priority = EXCLUDED.priority,
|
||||||
|
updated_at = NOW();
|
||||||
|
|
||||||
|
INSERT INTO fifo_stock_v2_flag_members(flag_name, flag_group_code, priority)
|
||||||
|
VALUES
|
||||||
|
('DOC', 'AYAM', 10),
|
||||||
|
('PULLET', 'AYAM', 20),
|
||||||
|
('LAYER', 'AYAM', 30),
|
||||||
|
|
||||||
|
('AYAM-AFKIR', 'AFKIR_CULLING_MATI', 10),
|
||||||
|
('AYAM-CULLING', 'AFKIR_CULLING_MATI', 20),
|
||||||
|
('AYAM-MATI', 'AFKIR_CULLING_MATI', 30),
|
||||||
|
|
||||||
|
('PAKAN', 'PAKAN', 10),
|
||||||
|
('PRE-STARTER', 'PAKAN', 20),
|
||||||
|
('STARTER', 'PAKAN', 30),
|
||||||
|
('FINISHER', 'PAKAN', 40),
|
||||||
|
|
||||||
|
('OVK', 'OVK', 10),
|
||||||
|
('OBAT', 'OVK', 20),
|
||||||
|
('VITAMIN', 'OVK', 30),
|
||||||
|
('KIMIA', 'OVK', 40),
|
||||||
|
|
||||||
|
('TELUR', 'TELUR', 10),
|
||||||
|
|
||||||
|
('TELUR-UTUH', 'TELUR_GRADE', 10),
|
||||||
|
('TELUR-PUTIH', 'TELUR_GRADE', 20),
|
||||||
|
('TELUR-RETAK', 'TELUR_GRADE', 30),
|
||||||
|
('TELUR-PECAH', 'TELUR_GRADE', 40),
|
||||||
|
('TELUR-PAPACAL', 'TELUR_GRADE', 50),
|
||||||
|
('TELUR-JUMBO', 'TELUR_GRADE', 60)
|
||||||
|
ON CONFLICT (flag_name) DO UPDATE
|
||||||
|
SET
|
||||||
|
flag_group_code = EXCLUDED.flag_group_code,
|
||||||
|
priority = EXCLUDED.priority,
|
||||||
|
updated_at = NOW();
|
||||||
|
|
||||||
|
INSERT INTO fifo_stock_v2_traits(
|
||||||
|
source_table,
|
||||||
|
lane,
|
||||||
|
date_table,
|
||||||
|
date_join_left_col,
|
||||||
|
date_join_right_col,
|
||||||
|
date_column,
|
||||||
|
fallback_date_column,
|
||||||
|
sort_priority,
|
||||||
|
id_column
|
||||||
|
)
|
||||||
|
VALUES
|
||||||
|
('purchase_items', 'STOCKABLE', NULL, NULL, NULL, 'received_date', NULL, 10, 'id'),
|
||||||
|
|
||||||
|
('stock_transfer_details', 'STOCKABLE', 'stock_transfers', 'stock_transfer_id', 'id', 'transfer_date', NULL, 20, 'id'),
|
||||||
|
('stock_transfer_details', 'USABLE', 'stock_transfers', 'stock_transfer_id', 'id', 'transfer_date', NULL, 20, 'id'),
|
||||||
|
|
||||||
|
('laying_transfer_targets', 'STOCKABLE', 'laying_transfers', 'laying_transfer_id', 'id', 'transfer_date', NULL, 25, 'id'),
|
||||||
|
('laying_transfer_sources', 'USABLE', 'laying_transfers', 'laying_transfer_id', 'id', 'transfer_date', NULL, 25, 'id'),
|
||||||
|
|
||||||
|
('adjustment_stocks', 'STOCKABLE', NULL, NULL, NULL, 'created_at', NULL, 30, 'id'),
|
||||||
|
('adjustment_stocks', 'USABLE', NULL, NULL, NULL, 'created_at', NULL, 30, 'id'),
|
||||||
|
|
||||||
|
('recording_stocks', 'USABLE', 'recordings', 'recording_id', 'id', 'record_datetime', NULL, 35, 'id'),
|
||||||
|
('recording_depletions', 'USABLE', 'recordings', 'recording_id', 'id', 'record_datetime', NULL, 35, 'id'),
|
||||||
|
('recording_depletions', 'STOCKABLE', 'recordings', 'recording_id', 'id', 'record_datetime', NULL, 35, 'id'),
|
||||||
|
|
||||||
|
('recording_eggs', 'STOCKABLE', 'recordings', 'recording_id', 'id', 'record_datetime', 'created_at', 40, 'id'),
|
||||||
|
|
||||||
|
('marketing_delivery_products', 'USABLE', NULL, NULL, NULL, 'delivery_date', 'created_at', 45, 'id'),
|
||||||
|
|
||||||
|
('project_chickins', 'USABLE', NULL, NULL, NULL, 'chick_in_date', 'created_at', 50, 'id'),
|
||||||
|
('project_flock_populations', 'STOCKABLE', 'project_chickins', 'project_chickin_id', 'id', 'chick_in_date', 'created_at', 55, 'id')
|
||||||
|
ON CONFLICT (source_table, lane) DO UPDATE
|
||||||
|
SET
|
||||||
|
date_table = EXCLUDED.date_table,
|
||||||
|
date_join_left_col = EXCLUDED.date_join_left_col,
|
||||||
|
date_join_right_col = EXCLUDED.date_join_right_col,
|
||||||
|
date_column = EXCLUDED.date_column,
|
||||||
|
fallback_date_column = EXCLUDED.fallback_date_column,
|
||||||
|
sort_priority = EXCLUDED.sort_priority,
|
||||||
|
id_column = EXCLUDED.id_column,
|
||||||
|
is_active = TRUE;
|
||||||
|
|
||||||
|
INSERT INTO fifo_stock_v2_route_rules(
|
||||||
|
flag_group_code,
|
||||||
|
lane,
|
||||||
|
function_code,
|
||||||
|
source_table,
|
||||||
|
source_id_column,
|
||||||
|
product_warehouse_col,
|
||||||
|
quantity_col,
|
||||||
|
used_quantity_col,
|
||||||
|
pending_quantity_col,
|
||||||
|
scope_sql,
|
||||||
|
legacy_type_key,
|
||||||
|
allow_pending_default,
|
||||||
|
is_active
|
||||||
|
)
|
||||||
|
VALUES
|
||||||
|
-- AYAM STOCKABLE
|
||||||
|
('AYAM', 'STOCKABLE', 'ADJUSTMENT_IN', 'adjustment_stocks', 'id', 'product_warehouse_id', 'total_qty', 'total_used', NULL, NULL, 'ADJUSTMENT_IN', TRUE, TRUE),
|
||||||
|
('AYAM', 'STOCKABLE', 'STOCK_TRANSFER_IN', 'stock_transfer_details', 'id', 'dest_product_warehouse_id', 'total_qty', 'total_used', NULL, 'deleted_at IS NULL', 'STOCK_TRANSFER_IN', TRUE, TRUE),
|
||||||
|
('AYAM', 'STOCKABLE', 'PURCHASE_IN', 'purchase_items', 'id', 'product_warehouse_id', 'total_qty', 'total_used', NULL, NULL, 'PURCHASE_ITEMS', TRUE, TRUE),
|
||||||
|
('AYAM', 'STOCKABLE', 'TRANSFER_TO_LAYING_IN', 'laying_transfer_targets', 'id', 'product_warehouse_id', 'total_qty', 'total_used', NULL, 'deleted_at IS NULL', 'TRANSFERTOLAYING_IN', TRUE, TRUE),
|
||||||
|
('AYAM', 'STOCKABLE', 'POPULATION_IN', 'project_flock_populations', 'id', 'product_warehouse_id', 'total_qty', 'total_used_qty', NULL, NULL, 'PROJECT_FLOCK_POPULATION', TRUE, TRUE),
|
||||||
|
|
||||||
|
-- AYAM USABLE
|
||||||
|
('AYAM', 'USABLE', 'ADJUSTMENT_OUT', 'adjustment_stocks', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_qty', NULL, 'ADJUSTMENT_OUT', TRUE, TRUE),
|
||||||
|
('AYAM', 'USABLE', 'STOCK_TRANSFER_OUT', 'stock_transfer_details', 'id', 'source_product_warehouse_id', 'usage_qty', NULL, 'pending_qty', 'deleted_at IS NULL', 'STOCK_TRANSFER_OUT', TRUE, TRUE),
|
||||||
|
('AYAM', 'USABLE', 'CHICKIN_OUT', 'project_chickins', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_usage_qty', 'deleted_at IS NULL', 'PROJECT_CHICKIN', TRUE, TRUE),
|
||||||
|
('AYAM', 'USABLE', 'RECORDING_DEPLETION_OUT', 'recording_depletions', 'id', 'source_product_warehouse_id', 'qty', NULL, 'pending_qty', NULL, 'RECORDING_DEPLETION', TRUE, TRUE),
|
||||||
|
('AYAM', 'USABLE', 'MARKETING_OUT', 'marketing_delivery_products', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_qty', NULL, 'MARKETING_DELIVERY', TRUE, TRUE),
|
||||||
|
('AYAM', 'USABLE', 'TRANSFER_TO_LAYING_OUT', 'laying_transfer_sources', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_usage_qty', 'deleted_at IS NULL', 'TRANSFERTOLAYING_OUT', TRUE, TRUE),
|
||||||
|
|
||||||
|
-- AFKIR/CULLING/MATI STOCKABLE
|
||||||
|
('AFKIR_CULLING_MATI', 'STOCKABLE', 'ADJUSTMENT_IN', 'adjustment_stocks', 'id', 'product_warehouse_id', 'total_qty', 'total_used', NULL, NULL, 'ADJUSTMENT_IN', TRUE, TRUE),
|
||||||
|
('AFKIR_CULLING_MATI', 'STOCKABLE', 'STOCK_TRANSFER_IN', 'stock_transfer_details', 'id', 'dest_product_warehouse_id', 'total_qty', 'total_used', NULL, 'deleted_at IS NULL', 'STOCK_TRANSFER_IN', TRUE, TRUE),
|
||||||
|
('AFKIR_CULLING_MATI', 'STOCKABLE', 'RECORDING_DEPLETION_IN', 'recording_depletions', 'id', 'product_warehouse_id', 'qty', NULL, NULL, NULL, 'RECORDING_DEPLETION', TRUE, TRUE),
|
||||||
|
|
||||||
|
-- AFKIR/CULLING/MATI USABLE
|
||||||
|
('AFKIR_CULLING_MATI', 'USABLE', 'ADJUSTMENT_OUT', 'adjustment_stocks', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_qty', NULL, 'ADJUSTMENT_OUT', TRUE, TRUE),
|
||||||
|
('AFKIR_CULLING_MATI', 'USABLE', 'STOCK_TRANSFER_OUT', 'stock_transfer_details', 'id', 'source_product_warehouse_id', 'usage_qty', NULL, 'pending_qty', 'deleted_at IS NULL', 'STOCK_TRANSFER_OUT', TRUE, TRUE),
|
||||||
|
('AFKIR_CULLING_MATI', 'USABLE', 'MARKETING_OUT', 'marketing_delivery_products', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_qty', NULL, 'MARKETING_DELIVERY', TRUE, TRUE),
|
||||||
|
|
||||||
|
-- PAKAN STOCKABLE
|
||||||
|
('PAKAN', 'STOCKABLE', 'ADJUSTMENT_IN', 'adjustment_stocks', 'id', 'product_warehouse_id', 'total_qty', 'total_used', NULL, NULL, 'ADJUSTMENT_IN', TRUE, TRUE),
|
||||||
|
('PAKAN', 'STOCKABLE', 'STOCK_TRANSFER_IN', 'stock_transfer_details', 'id', 'dest_product_warehouse_id', 'total_qty', 'total_used', NULL, 'deleted_at IS NULL', 'STOCK_TRANSFER_IN', TRUE, TRUE),
|
||||||
|
('PAKAN', 'STOCKABLE', 'PURCHASE_IN', 'purchase_items', 'id', 'product_warehouse_id', 'total_qty', 'total_used', NULL, NULL, 'PURCHASE_ITEMS', TRUE, TRUE),
|
||||||
|
|
||||||
|
-- PAKAN USABLE
|
||||||
|
('PAKAN', 'USABLE', 'ADJUSTMENT_OUT', 'adjustment_stocks', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_qty', NULL, 'ADJUSTMENT_OUT', TRUE, TRUE),
|
||||||
|
('PAKAN', 'USABLE', 'STOCK_TRANSFER_OUT', 'stock_transfer_details', 'id', 'source_product_warehouse_id', 'usage_qty', NULL, 'pending_qty', 'deleted_at IS NULL', 'STOCK_TRANSFER_OUT', TRUE, TRUE),
|
||||||
|
('PAKAN', 'USABLE', 'RECORDING_STOCK_OUT', 'recording_stocks', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_qty', NULL, 'RECORDING_STOCK', TRUE, TRUE),
|
||||||
|
('PAKAN', 'USABLE', 'MARKETING_OUT', 'marketing_delivery_products', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_qty', NULL, 'MARKETING_DELIVERY', TRUE, TRUE),
|
||||||
|
|
||||||
|
-- OVK STOCKABLE
|
||||||
|
('OVK', 'STOCKABLE', 'ADJUSTMENT_IN', 'adjustment_stocks', 'id', 'product_warehouse_id', 'total_qty', 'total_used', NULL, NULL, 'ADJUSTMENT_IN', TRUE, TRUE),
|
||||||
|
('OVK', 'STOCKABLE', 'STOCK_TRANSFER_IN', 'stock_transfer_details', 'id', 'dest_product_warehouse_id', 'total_qty', 'total_used', NULL, 'deleted_at IS NULL', 'STOCK_TRANSFER_IN', TRUE, TRUE),
|
||||||
|
('OVK', 'STOCKABLE', 'PURCHASE_IN', 'purchase_items', 'id', 'product_warehouse_id', 'total_qty', 'total_used', NULL, NULL, 'PURCHASE_ITEMS', TRUE, TRUE),
|
||||||
|
|
||||||
|
-- OVK USABLE
|
||||||
|
('OVK', 'USABLE', 'ADJUSTMENT_OUT', 'adjustment_stocks', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_qty', NULL, 'ADJUSTMENT_OUT', TRUE, TRUE),
|
||||||
|
('OVK', 'USABLE', 'STOCK_TRANSFER_OUT', 'stock_transfer_details', 'id', 'source_product_warehouse_id', 'usage_qty', NULL, 'pending_qty', 'deleted_at IS NULL', 'STOCK_TRANSFER_OUT', TRUE, TRUE),
|
||||||
|
('OVK', 'USABLE', 'RECORDING_STOCK_OUT', 'recording_stocks', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_qty', NULL, 'RECORDING_STOCK', TRUE, TRUE),
|
||||||
|
('OVK', 'USABLE', 'MARKETING_OUT', 'marketing_delivery_products', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_qty', NULL, 'MARKETING_DELIVERY', TRUE, TRUE),
|
||||||
|
|
||||||
|
-- TELUR STOCKABLE
|
||||||
|
('TELUR', 'STOCKABLE', 'ADJUSTMENT_IN', 'adjustment_stocks', 'id', 'product_warehouse_id', 'total_qty', 'total_used', NULL, NULL, 'ADJUSTMENT_IN', TRUE, TRUE),
|
||||||
|
('TELUR', 'STOCKABLE', 'STOCK_TRANSFER_IN', 'stock_transfer_details', 'id', 'dest_product_warehouse_id', 'total_qty', 'total_used', NULL, 'deleted_at IS NULL', 'STOCK_TRANSFER_IN', TRUE, TRUE),
|
||||||
|
('TELUR', 'STOCKABLE', 'RECORDING_EGG_IN', 'recording_eggs', 'id', 'product_warehouse_id', 'total_qty', 'total_used', NULL, NULL, 'RECORDING_EGG', TRUE, TRUE),
|
||||||
|
|
||||||
|
-- TELUR USABLE
|
||||||
|
('TELUR', 'USABLE', 'ADJUSTMENT_OUT', 'adjustment_stocks', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_qty', NULL, 'ADJUSTMENT_OUT', TRUE, TRUE),
|
||||||
|
('TELUR', 'USABLE', 'STOCK_TRANSFER_OUT', 'stock_transfer_details', 'id', 'source_product_warehouse_id', 'usage_qty', NULL, 'pending_qty', 'deleted_at IS NULL', 'STOCK_TRANSFER_OUT', TRUE, TRUE),
|
||||||
|
('TELUR', 'USABLE', 'MARKETING_OUT', 'marketing_delivery_products', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_qty', NULL, 'MARKETING_DELIVERY', TRUE, TRUE),
|
||||||
|
|
||||||
|
-- TELUR_GRADE STOCKABLE
|
||||||
|
('TELUR_GRADE', 'STOCKABLE', 'ADJUSTMENT_IN', 'adjustment_stocks', 'id', 'product_warehouse_id', 'total_qty', 'total_used', NULL, NULL, 'ADJUSTMENT_IN', TRUE, TRUE),
|
||||||
|
('TELUR_GRADE', 'STOCKABLE', 'STOCK_TRANSFER_IN', 'stock_transfer_details', 'id', 'dest_product_warehouse_id', 'total_qty', 'total_used', NULL, 'deleted_at IS NULL', 'STOCK_TRANSFER_IN', TRUE, TRUE),
|
||||||
|
('TELUR_GRADE', 'STOCKABLE', 'RECORDING_EGG_IN', 'recording_eggs', 'id', 'product_warehouse_id', 'total_qty', 'total_used', NULL, NULL, 'RECORDING_EGG', TRUE, TRUE),
|
||||||
|
|
||||||
|
-- TELUR_GRADE USABLE
|
||||||
|
('TELUR_GRADE', 'USABLE', 'ADJUSTMENT_OUT', 'adjustment_stocks', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_qty', NULL, 'ADJUSTMENT_OUT', TRUE, TRUE),
|
||||||
|
('TELUR_GRADE', 'USABLE', 'STOCK_TRANSFER_OUT', 'stock_transfer_details', 'id', 'source_product_warehouse_id', 'usage_qty', NULL, 'pending_qty', 'deleted_at IS NULL', 'STOCK_TRANSFER_OUT', TRUE, TRUE),
|
||||||
|
('TELUR_GRADE', 'USABLE', 'MARKETING_OUT', 'marketing_delivery_products', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_qty', NULL, 'MARKETING_DELIVERY', TRUE, TRUE)
|
||||||
|
ON CONFLICT (flag_group_code, lane, function_code, source_table) DO UPDATE
|
||||||
|
SET
|
||||||
|
source_id_column = EXCLUDED.source_id_column,
|
||||||
|
product_warehouse_col = EXCLUDED.product_warehouse_col,
|
||||||
|
quantity_col = EXCLUDED.quantity_col,
|
||||||
|
used_quantity_col = EXCLUDED.used_quantity_col,
|
||||||
|
pending_quantity_col = EXCLUDED.pending_quantity_col,
|
||||||
|
scope_sql = EXCLUDED.scope_sql,
|
||||||
|
legacy_type_key = EXCLUDED.legacy_type_key,
|
||||||
|
allow_pending_default = EXCLUDED.allow_pending_default,
|
||||||
|
is_active = EXCLUDED.is_active,
|
||||||
|
updated_at = NOW();
|
||||||
|
|
||||||
|
INSERT INTO fifo_stock_v2_overconsume_rules(flag_group_code, function_code, lane, allow_overconsume, priority, reason, is_active)
|
||||||
|
SELECT NULL, NULL, 'USABLE', TRUE, 999, 'fifo_v2_default_allow', TRUE
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1 FROM fifo_stock_v2_overconsume_rules
|
||||||
|
WHERE flag_group_code IS NULL
|
||||||
|
AND function_code IS NULL
|
||||||
|
AND lane = 'USABLE'
|
||||||
|
AND reason = 'fifo_v2_default_allow'
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO fifo_stock_v2_overconsume_rules(flag_group_code, function_code, lane, allow_overconsume, priority, reason, is_active)
|
||||||
|
SELECT 'AYAM', 'RECORDING_DEPLETION_OUT', 'USABLE', FALSE, 10, 'fifo_v2_exception_ayam_depletion_block', TRUE
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1 FROM fifo_stock_v2_overconsume_rules
|
||||||
|
WHERE flag_group_code = 'AYAM'
|
||||||
|
AND function_code = 'RECORDING_DEPLETION_OUT'
|
||||||
|
AND lane = 'USABLE'
|
||||||
|
AND reason = 'fifo_v2_exception_ayam_depletion_block'
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO fifo_stock_v2_overconsume_rules(flag_group_code, function_code, lane, allow_overconsume, priority, reason, is_active)
|
||||||
|
SELECT NULL, 'MARKETING_OUT', 'USABLE', FALSE, 20, 'fifo_v2_exception_marketing_block', TRUE
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1 FROM fifo_stock_v2_overconsume_rules
|
||||||
|
WHERE flag_group_code IS NULL
|
||||||
|
AND function_code = 'MARKETING_OUT'
|
||||||
|
AND lane = 'USABLE'
|
||||||
|
AND reason = 'fifo_v2_exception_marketing_block'
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO fifo_stock_v2_overconsume_rules(flag_group_code, function_code, lane, allow_overconsume, priority, reason, is_active)
|
||||||
|
SELECT NULL, 'STOCK_TRANSFER_OUT', 'USABLE', FALSE, 30, 'fifo_v2_exception_transfer_block', TRUE
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1 FROM fifo_stock_v2_overconsume_rules
|
||||||
|
WHERE flag_group_code IS NULL
|
||||||
|
AND function_code = 'STOCK_TRANSFER_OUT'
|
||||||
|
AND lane = 'USABLE'
|
||||||
|
AND reason = 'fifo_v2_exception_transfer_block'
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO fifo_stock_v2_overconsume_rules(flag_group_code, function_code, lane, allow_overconsume, priority, reason, is_active)
|
||||||
|
SELECT NULL, 'ADJUSTMENT_OUT', 'USABLE', FALSE, 40, 'fifo_v2_exception_adjustment_block', TRUE
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1 FROM fifo_stock_v2_overconsume_rules
|
||||||
|
WHERE flag_group_code IS NULL
|
||||||
|
AND function_code = 'ADJUSTMENT_OUT'
|
||||||
|
AND lane = 'USABLE'
|
||||||
|
AND reason = 'fifo_v2_exception_adjustment_block'
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO fifo_stock_v2_overconsume_rules(flag_group_code, function_code, lane, allow_overconsume, priority, reason, is_active)
|
||||||
|
SELECT NULL, 'TRANSFER_TO_LAYING_OUT', 'USABLE', FALSE, 50, 'fifo_v2_exception_transfer_laying_block', TRUE
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1 FROM fifo_stock_v2_overconsume_rules
|
||||||
|
WHERE flag_group_code IS NULL
|
||||||
|
AND function_code = 'TRANSFER_TO_LAYING_OUT'
|
||||||
|
AND lane = 'USABLE'
|
||||||
|
AND reason = 'fifo_v2_exception_transfer_laying_block'
|
||||||
|
);
|
||||||
|
|
||||||
COMMIT;
|
COMMIT;
|
||||||
|
|||||||
+16
-1
@@ -1,5 +1,20 @@
|
|||||||
BEGIN;
|
BEGIN;
|
||||||
|
|
||||||
-- no-op: moved to 20260306090011_disable_chickin_fifo_consumption_after_core.down.sql
|
-- Restore CHICKIN route if rollback is required.
|
||||||
|
-- NOTE: released PROJECT_CHICKIN allocations are not restored by this down migration.
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF to_regclass('public.fifo_stock_v2_route_rules') IS NOT NULL THEN
|
||||||
|
EXECUTE '
|
||||||
|
UPDATE fifo_stock_v2_route_rules
|
||||||
|
SET is_active = TRUE,
|
||||||
|
updated_at = NOW()
|
||||||
|
WHERE flag_group_code = ''AYAM''
|
||||||
|
AND lane = ''USABLE''
|
||||||
|
AND function_code = ''CHICKIN_OUT''
|
||||||
|
AND source_table = ''project_chickins''
|
||||||
|
';
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
COMMIT;
|
COMMIT;
|
||||||
|
|||||||
+147
-2
@@ -1,6 +1,151 @@
|
|||||||
BEGIN;
|
BEGIN;
|
||||||
|
|
||||||
-- no-op: moved to 20260306090011_disable_chickin_fifo_consumption_after_core.up.sql
|
-- Disable CHICKIN as FIFO USABLE so chick-in acts as business tagging/conversion,
|
||||||
-- to ensure FIFO core + seed are applied before this data update migration.
|
-- not physical stock consumption.
|
||||||
|
UPDATE fifo_stock_v2_route_rules
|
||||||
|
SET is_active = FALSE,
|
||||||
|
updated_at = NOW()
|
||||||
|
WHERE flag_group_code = 'AYAM'
|
||||||
|
AND lane = 'USABLE'
|
||||||
|
AND function_code = 'CHICKIN_OUT'
|
||||||
|
AND source_table = 'project_chickins'
|
||||||
|
AND is_active = TRUE;
|
||||||
|
|
||||||
|
-- Release existing active allocations created by PROJECT_CHICKIN
|
||||||
|
-- and return warehouse qty back.
|
||||||
|
WITH released AS (
|
||||||
|
UPDATE stock_allocations
|
||||||
|
SET status = 'RELEASED',
|
||||||
|
released_at = COALESCE(released_at, NOW()),
|
||||||
|
updated_at = NOW(),
|
||||||
|
note = CASE
|
||||||
|
WHEN COALESCE(note, '') = '' THEN 'fifo_v2_chickin_conversion_release'
|
||||||
|
ELSE note || '; fifo_v2_chickin_conversion_release'
|
||||||
|
END
|
||||||
|
WHERE usable_type = 'PROJECT_CHICKIN'
|
||||||
|
AND status = 'ACTIVE'
|
||||||
|
RETURNING product_warehouse_id, qty
|
||||||
|
),
|
||||||
|
pw_delta AS (
|
||||||
|
SELECT product_warehouse_id, COALESCE(SUM(qty), 0) AS qty_delta
|
||||||
|
FROM released
|
||||||
|
GROUP BY product_warehouse_id
|
||||||
|
)
|
||||||
|
UPDATE product_warehouses pw
|
||||||
|
SET qty = COALESCE(pw.qty, 0) + d.qty_delta
|
||||||
|
FROM pw_delta d
|
||||||
|
WHERE pw.id = d.product_warehouse_id;
|
||||||
|
|
||||||
|
-- Resync stockable total_used columns from remaining ACTIVE allocations.
|
||||||
|
|
||||||
|
-- purchase_items (PURCHASE_ITEMS)
|
||||||
|
UPDATE purchase_items pi
|
||||||
|
SET total_used = COALESCE(a.used, 0)
|
||||||
|
FROM (
|
||||||
|
SELECT stockable_id, SUM(qty) AS used
|
||||||
|
FROM stock_allocations
|
||||||
|
WHERE status = 'ACTIVE'
|
||||||
|
AND stockable_type = 'PURCHASE_ITEMS'
|
||||||
|
GROUP BY stockable_id
|
||||||
|
) a
|
||||||
|
WHERE pi.id = a.stockable_id;
|
||||||
|
|
||||||
|
UPDATE purchase_items pi
|
||||||
|
SET total_used = 0
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM stock_allocations sa
|
||||||
|
WHERE sa.status = 'ACTIVE'
|
||||||
|
AND sa.stockable_type = 'PURCHASE_ITEMS'
|
||||||
|
AND sa.stockable_id = pi.id
|
||||||
|
);
|
||||||
|
|
||||||
|
-- stock_transfer_details (STOCK_TRANSFER_IN)
|
||||||
|
UPDATE stock_transfer_details std
|
||||||
|
SET total_used = COALESCE(a.used, 0)
|
||||||
|
FROM (
|
||||||
|
SELECT stockable_id, SUM(qty) AS used
|
||||||
|
FROM stock_allocations
|
||||||
|
WHERE status = 'ACTIVE'
|
||||||
|
AND stockable_type = 'STOCK_TRANSFER_IN'
|
||||||
|
GROUP BY stockable_id
|
||||||
|
) a
|
||||||
|
WHERE std.id = a.stockable_id;
|
||||||
|
|
||||||
|
UPDATE stock_transfer_details std
|
||||||
|
SET total_used = 0
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM stock_allocations sa
|
||||||
|
WHERE sa.status = 'ACTIVE'
|
||||||
|
AND sa.stockable_type = 'STOCK_TRANSFER_IN'
|
||||||
|
AND sa.stockable_id = std.id
|
||||||
|
);
|
||||||
|
|
||||||
|
-- adjustment_stocks (ADJUSTMENT_IN)
|
||||||
|
UPDATE adjustment_stocks ast
|
||||||
|
SET total_used = COALESCE(a.used, 0)
|
||||||
|
FROM (
|
||||||
|
SELECT stockable_id, SUM(qty) AS used
|
||||||
|
FROM stock_allocations
|
||||||
|
WHERE status = 'ACTIVE'
|
||||||
|
AND stockable_type = 'ADJUSTMENT_IN'
|
||||||
|
GROUP BY stockable_id
|
||||||
|
) a
|
||||||
|
WHERE ast.id = a.stockable_id;
|
||||||
|
|
||||||
|
UPDATE adjustment_stocks ast
|
||||||
|
SET total_used = 0
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM stock_allocations sa
|
||||||
|
WHERE sa.status = 'ACTIVE'
|
||||||
|
AND sa.stockable_type = 'ADJUSTMENT_IN'
|
||||||
|
AND sa.stockable_id = ast.id
|
||||||
|
);
|
||||||
|
|
||||||
|
-- laying_transfer_targets (TRANSFERTOLAYING_IN)
|
||||||
|
UPDATE laying_transfer_targets ltt
|
||||||
|
SET total_used = COALESCE(a.used, 0)
|
||||||
|
FROM (
|
||||||
|
SELECT stockable_id, SUM(qty) AS used
|
||||||
|
FROM stock_allocations
|
||||||
|
WHERE status = 'ACTIVE'
|
||||||
|
AND stockable_type = 'TRANSFERTOLAYING_IN'
|
||||||
|
GROUP BY stockable_id
|
||||||
|
) a
|
||||||
|
WHERE ltt.id = a.stockable_id;
|
||||||
|
|
||||||
|
UPDATE laying_transfer_targets ltt
|
||||||
|
SET total_used = 0
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM stock_allocations sa
|
||||||
|
WHERE sa.status = 'ACTIVE'
|
||||||
|
AND sa.stockable_type = 'TRANSFERTOLAYING_IN'
|
||||||
|
AND sa.stockable_id = ltt.id
|
||||||
|
);
|
||||||
|
|
||||||
|
-- recording_eggs (RECORDING_EGG)
|
||||||
|
UPDATE recording_eggs re
|
||||||
|
SET total_used = COALESCE(a.used, 0)
|
||||||
|
FROM (
|
||||||
|
SELECT stockable_id, SUM(qty) AS used
|
||||||
|
FROM stock_allocations
|
||||||
|
WHERE status = 'ACTIVE'
|
||||||
|
AND stockable_type = 'RECORDING_EGG'
|
||||||
|
GROUP BY stockable_id
|
||||||
|
) a
|
||||||
|
WHERE re.id = a.stockable_id;
|
||||||
|
|
||||||
|
UPDATE recording_eggs re
|
||||||
|
SET total_used = 0
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM stock_allocations sa
|
||||||
|
WHERE sa.status = 'ACTIVE'
|
||||||
|
AND sa.stockable_type = 'RECORDING_EGG'
|
||||||
|
AND sa.stockable_id = re.id
|
||||||
|
);
|
||||||
|
|
||||||
COMMIT;
|
COMMIT;
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
BEGIN;
|
BEGIN;
|
||||||
|
|
||||||
-- no-op: moved to 20260306090012_fix_fifo_chickin_out_after_seed.down.sql
|
DELETE FROM fifo_stock_v2_route_rules
|
||||||
|
WHERE flag_group_code = 'AYAM'
|
||||||
|
AND lane = 'USABLE'
|
||||||
|
AND function_code = 'CHICKIN_OUT'
|
||||||
|
AND source_table = 'project_chickins';
|
||||||
|
|
||||||
COMMIT;
|
COMMIT;
|
||||||
|
|||||||
@@ -1,6 +1,34 @@
|
|||||||
BEGIN;
|
BEGIN;
|
||||||
|
|
||||||
-- no-op: moved to 20260306090012_fix_fifo_chickin_out_after_seed.up.sql
|
INSERT INTO fifo_stock_v2_route_rules(
|
||||||
-- to ensure AYAM flag group exists before route-rule upsert.
|
flag_group_code,
|
||||||
|
lane,
|
||||||
|
function_code,
|
||||||
|
source_table,
|
||||||
|
source_id_column,
|
||||||
|
product_warehouse_col,
|
||||||
|
quantity_col,
|
||||||
|
used_quantity_col,
|
||||||
|
pending_quantity_col,
|
||||||
|
scope_sql,
|
||||||
|
legacy_type_key,
|
||||||
|
allow_pending_default,
|
||||||
|
is_active
|
||||||
|
)
|
||||||
|
VALUES
|
||||||
|
('AYAM', 'USABLE', 'CHICKIN_OUT', 'project_chickins', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_usage_qty', 'deleted_at IS NULL', 'PROJECT_CHICKIN', TRUE, TRUE)
|
||||||
|
ON CONFLICT (flag_group_code, lane, function_code, source_table) DO UPDATE
|
||||||
|
SET
|
||||||
|
source_id_column = EXCLUDED.source_id_column,
|
||||||
|
product_warehouse_col = EXCLUDED.product_warehouse_col,
|
||||||
|
quantity_col = EXCLUDED.quantity_col,
|
||||||
|
used_quantity_col = EXCLUDED.used_quantity_col,
|
||||||
|
pending_quantity_col = EXCLUDED.pending_quantity_col,
|
||||||
|
scope_sql = EXCLUDED.scope_sql,
|
||||||
|
legacy_type_key = EXCLUDED.legacy_type_key,
|
||||||
|
allow_pending_default = EXCLUDED.allow_pending_default,
|
||||||
|
updated_at = NOW(),
|
||||||
|
-- Keep existing is_active (do not override disable migration if it was intentional).
|
||||||
|
is_active = fifo_stock_v2_route_rules.is_active;
|
||||||
|
|
||||||
COMMIT;
|
COMMIT;
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
BEGIN;
|
BEGIN;
|
||||||
|
|
||||||
-- no-op: moved to 20260306090013_add_ayam_flag_member_fifo_v2_after_seed.down.sql
|
DELETE FROM fifo_stock_v2_flag_members
|
||||||
|
WHERE flag_name = 'AYAM'
|
||||||
|
AND flag_group_code = 'AYAM';
|
||||||
|
|
||||||
COMMIT;
|
COMMIT;
|
||||||
|
|||||||
@@ -1,6 +1,13 @@
|
|||||||
BEGIN;
|
BEGIN;
|
||||||
|
|
||||||
-- no-op: moved to 20260306090013_add_ayam_flag_member_fifo_v2_after_seed.up.sql
|
INSERT INTO fifo_stock_v2_flag_members(flag_name, flag_group_code, priority, is_active, created_at, updated_at)
|
||||||
-- to ensure AYAM flag group exists before inserting AYAM member.
|
VALUES
|
||||||
|
('AYAM', 'AYAM', 5, TRUE, NOW(), NOW())
|
||||||
|
ON CONFLICT (flag_name) DO UPDATE
|
||||||
|
SET
|
||||||
|
flag_group_code = EXCLUDED.flag_group_code,
|
||||||
|
priority = EXCLUDED.priority,
|
||||||
|
is_active = TRUE,
|
||||||
|
updated_at = NOW();
|
||||||
|
|
||||||
COMMIT;
|
COMMIT;
|
||||||
|
|||||||
-37
@@ -1,37 +0,0 @@
|
|||||||
BEGIN;
|
|
||||||
|
|
||||||
DELETE FROM fifo_stock_v2_overconsume_rules
|
|
||||||
WHERE reason IN (
|
|
||||||
'fifo_v2_default_allow',
|
|
||||||
'fifo_v2_exception_ayam_depletion_block',
|
|
||||||
'fifo_v2_exception_marketing_block',
|
|
||||||
'fifo_v2_exception_transfer_block',
|
|
||||||
'fifo_v2_exception_adjustment_block',
|
|
||||||
'fifo_v2_exception_transfer_laying_block'
|
|
||||||
);
|
|
||||||
|
|
||||||
DELETE FROM fifo_stock_v2_route_rules
|
|
||||||
WHERE flag_group_code IN ('AYAM', 'AFKIR_CULLING_MATI', 'PAKAN', 'OVK', 'TELUR', 'TELUR_GRADE');
|
|
||||||
|
|
||||||
DELETE FROM fifo_stock_v2_traits
|
|
||||||
WHERE source_table IN (
|
|
||||||
'purchase_items',
|
|
||||||
'stock_transfer_details',
|
|
||||||
'laying_transfer_targets',
|
|
||||||
'laying_transfer_sources',
|
|
||||||
'adjustment_stocks',
|
|
||||||
'recording_stocks',
|
|
||||||
'recording_depletions',
|
|
||||||
'recording_eggs',
|
|
||||||
'marketing_delivery_products',
|
|
||||||
'project_chickins',
|
|
||||||
'project_flock_populations'
|
|
||||||
);
|
|
||||||
|
|
||||||
DELETE FROM fifo_stock_v2_flag_members
|
|
||||||
WHERE flag_group_code IN ('AYAM', 'AFKIR_CULLING_MATI', 'PAKAN', 'OVK', 'TELUR', 'TELUR_GRADE');
|
|
||||||
|
|
||||||
DELETE FROM fifo_stock_v2_flag_groups
|
|
||||||
WHERE code IN ('AYAM', 'AFKIR_CULLING_MATI', 'PAKAN', 'OVK', 'TELUR', 'TELUR_GRADE');
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
-250
@@ -1,250 +0,0 @@
|
|||||||
BEGIN;
|
|
||||||
|
|
||||||
INSERT INTO fifo_stock_v2_flag_groups(code, name, priority)
|
|
||||||
VALUES
|
|
||||||
('AYAM', 'AYAM', 10),
|
|
||||||
('AFKIR_CULLING_MATI', 'AFKIR/CULLING/MATI', 20),
|
|
||||||
('PAKAN', 'PAKAN', 30),
|
|
||||||
('OVK', 'OVK', 40),
|
|
||||||
('TELUR', 'TELUR', 50),
|
|
||||||
('TELUR_GRADE', 'UTUH/PUTIH/RETAK/PECAH/PAPACAL/JUMBO', 60)
|
|
||||||
ON CONFLICT (code) DO UPDATE
|
|
||||||
SET
|
|
||||||
name = EXCLUDED.name,
|
|
||||||
priority = EXCLUDED.priority,
|
|
||||||
updated_at = NOW();
|
|
||||||
|
|
||||||
INSERT INTO fifo_stock_v2_flag_members(flag_name, flag_group_code, priority)
|
|
||||||
VALUES
|
|
||||||
('DOC', 'AYAM', 10),
|
|
||||||
('PULLET', 'AYAM', 20),
|
|
||||||
('LAYER', 'AYAM', 30),
|
|
||||||
|
|
||||||
('AYAM-AFKIR', 'AFKIR_CULLING_MATI', 10),
|
|
||||||
('AYAM-CULLING', 'AFKIR_CULLING_MATI', 20),
|
|
||||||
('AYAM-MATI', 'AFKIR_CULLING_MATI', 30),
|
|
||||||
|
|
||||||
('PAKAN', 'PAKAN', 10),
|
|
||||||
('PRE-STARTER', 'PAKAN', 20),
|
|
||||||
('STARTER', 'PAKAN', 30),
|
|
||||||
('FINISHER', 'PAKAN', 40),
|
|
||||||
|
|
||||||
('OVK', 'OVK', 10),
|
|
||||||
('OBAT', 'OVK', 20),
|
|
||||||
('VITAMIN', 'OVK', 30),
|
|
||||||
('KIMIA', 'OVK', 40),
|
|
||||||
|
|
||||||
('TELUR', 'TELUR', 10),
|
|
||||||
|
|
||||||
('TELUR-UTUH', 'TELUR_GRADE', 10),
|
|
||||||
('TELUR-PUTIH', 'TELUR_GRADE', 20),
|
|
||||||
('TELUR-RETAK', 'TELUR_GRADE', 30),
|
|
||||||
('TELUR-PECAH', 'TELUR_GRADE', 40),
|
|
||||||
('TELUR-PAPACAL', 'TELUR_GRADE', 50),
|
|
||||||
('TELUR-JUMBO', 'TELUR_GRADE', 60)
|
|
||||||
ON CONFLICT (flag_name) DO UPDATE
|
|
||||||
SET
|
|
||||||
flag_group_code = EXCLUDED.flag_group_code,
|
|
||||||
priority = EXCLUDED.priority,
|
|
||||||
updated_at = NOW();
|
|
||||||
|
|
||||||
INSERT INTO fifo_stock_v2_traits(
|
|
||||||
source_table,
|
|
||||||
lane,
|
|
||||||
date_table,
|
|
||||||
date_join_left_col,
|
|
||||||
date_join_right_col,
|
|
||||||
date_column,
|
|
||||||
fallback_date_column,
|
|
||||||
sort_priority,
|
|
||||||
id_column
|
|
||||||
)
|
|
||||||
VALUES
|
|
||||||
('purchase_items', 'STOCKABLE', NULL, NULL, NULL, 'received_date', NULL, 10, 'id'),
|
|
||||||
|
|
||||||
('stock_transfer_details', 'STOCKABLE', 'stock_transfers', 'stock_transfer_id', 'id', 'transfer_date', NULL, 20, 'id'),
|
|
||||||
('stock_transfer_details', 'USABLE', 'stock_transfers', 'stock_transfer_id', 'id', 'transfer_date', NULL, 20, 'id'),
|
|
||||||
|
|
||||||
('laying_transfer_targets', 'STOCKABLE', 'laying_transfers', 'laying_transfer_id', 'id', 'transfer_date', NULL, 25, 'id'),
|
|
||||||
('laying_transfer_sources', 'USABLE', 'laying_transfers', 'laying_transfer_id', 'id', 'transfer_date', NULL, 25, 'id'),
|
|
||||||
|
|
||||||
('adjustment_stocks', 'STOCKABLE', NULL, NULL, NULL, 'created_at', NULL, 30, 'id'),
|
|
||||||
('adjustment_stocks', 'USABLE', NULL, NULL, NULL, 'created_at', NULL, 30, 'id'),
|
|
||||||
|
|
||||||
('recording_stocks', 'USABLE', 'recordings', 'recording_id', 'id', 'record_datetime', NULL, 35, 'id'),
|
|
||||||
('recording_depletions', 'USABLE', 'recordings', 'recording_id', 'id', 'record_datetime', NULL, 35, 'id'),
|
|
||||||
('recording_depletions', 'STOCKABLE', 'recordings', 'recording_id', 'id', 'record_datetime', NULL, 35, 'id'),
|
|
||||||
|
|
||||||
('recording_eggs', 'STOCKABLE', 'recordings', 'recording_id', 'id', 'record_datetime', 'created_at', 40, 'id'),
|
|
||||||
|
|
||||||
('marketing_delivery_products', 'USABLE', NULL, NULL, NULL, 'delivery_date', 'created_at', 45, 'id'),
|
|
||||||
|
|
||||||
('project_chickins', 'USABLE', NULL, NULL, NULL, 'chick_in_date', 'created_at', 50, 'id'),
|
|
||||||
('project_flock_populations', 'STOCKABLE', 'project_chickins', 'project_chickin_id', 'id', 'chick_in_date', 'created_at', 55, 'id')
|
|
||||||
ON CONFLICT (source_table, lane) DO UPDATE
|
|
||||||
SET
|
|
||||||
date_table = EXCLUDED.date_table,
|
|
||||||
date_join_left_col = EXCLUDED.date_join_left_col,
|
|
||||||
date_join_right_col = EXCLUDED.date_join_right_col,
|
|
||||||
date_column = EXCLUDED.date_column,
|
|
||||||
fallback_date_column = EXCLUDED.fallback_date_column,
|
|
||||||
sort_priority = EXCLUDED.sort_priority,
|
|
||||||
id_column = EXCLUDED.id_column,
|
|
||||||
is_active = TRUE;
|
|
||||||
|
|
||||||
INSERT INTO fifo_stock_v2_route_rules(
|
|
||||||
flag_group_code,
|
|
||||||
lane,
|
|
||||||
function_code,
|
|
||||||
source_table,
|
|
||||||
source_id_column,
|
|
||||||
product_warehouse_col,
|
|
||||||
quantity_col,
|
|
||||||
used_quantity_col,
|
|
||||||
pending_quantity_col,
|
|
||||||
scope_sql,
|
|
||||||
legacy_type_key,
|
|
||||||
allow_pending_default,
|
|
||||||
is_active
|
|
||||||
)
|
|
||||||
VALUES
|
|
||||||
-- AYAM STOCKABLE
|
|
||||||
('AYAM', 'STOCKABLE', 'ADJUSTMENT_IN', 'adjustment_stocks', 'id', 'product_warehouse_id', 'total_qty', 'total_used', NULL, NULL, 'ADJUSTMENT_IN', TRUE, TRUE),
|
|
||||||
('AYAM', 'STOCKABLE', 'STOCK_TRANSFER_IN', 'stock_transfer_details', 'id', 'dest_product_warehouse_id', 'total_qty', 'total_used', NULL, 'deleted_at IS NULL', 'STOCK_TRANSFER_IN', TRUE, TRUE),
|
|
||||||
('AYAM', 'STOCKABLE', 'PURCHASE_IN', 'purchase_items', 'id', 'product_warehouse_id', 'total_qty', 'total_used', NULL, NULL, 'PURCHASE_ITEMS', TRUE, TRUE),
|
|
||||||
('AYAM', 'STOCKABLE', 'TRANSFER_TO_LAYING_IN', 'laying_transfer_targets', 'id', 'product_warehouse_id', 'total_qty', 'total_used', NULL, 'deleted_at IS NULL', 'TRANSFERTOLAYING_IN', TRUE, TRUE),
|
|
||||||
('AYAM', 'STOCKABLE', 'POPULATION_IN', 'project_flock_populations', 'id', 'product_warehouse_id', 'total_qty', 'total_used_qty', NULL, NULL, 'PROJECT_FLOCK_POPULATION', TRUE, TRUE),
|
|
||||||
|
|
||||||
-- AYAM USABLE
|
|
||||||
('AYAM', 'USABLE', 'ADJUSTMENT_OUT', 'adjustment_stocks', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_qty', NULL, 'ADJUSTMENT_OUT', TRUE, TRUE),
|
|
||||||
('AYAM', 'USABLE', 'STOCK_TRANSFER_OUT', 'stock_transfer_details', 'id', 'source_product_warehouse_id', 'usage_qty', NULL, 'pending_qty', 'deleted_at IS NULL', 'STOCK_TRANSFER_OUT', TRUE, TRUE),
|
|
||||||
('AYAM', 'USABLE', 'CHICKIN_OUT', 'project_chickins', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_usage_qty', 'deleted_at IS NULL', 'PROJECT_CHICKIN', TRUE, TRUE),
|
|
||||||
('AYAM', 'USABLE', 'RECORDING_DEPLETION_OUT', 'recording_depletions', 'id', 'source_product_warehouse_id', 'qty', NULL, 'pending_qty', NULL, 'RECORDING_DEPLETION', TRUE, TRUE),
|
|
||||||
('AYAM', 'USABLE', 'MARKETING_OUT', 'marketing_delivery_products', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_qty', NULL, 'MARKETING_DELIVERY', TRUE, TRUE),
|
|
||||||
('AYAM', 'USABLE', 'TRANSFER_TO_LAYING_OUT', 'laying_transfer_sources', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_usage_qty', 'deleted_at IS NULL', 'TRANSFERTOLAYING_OUT', TRUE, TRUE),
|
|
||||||
|
|
||||||
-- AFKIR/CULLING/MATI STOCKABLE
|
|
||||||
('AFKIR_CULLING_MATI', 'STOCKABLE', 'ADJUSTMENT_IN', 'adjustment_stocks', 'id', 'product_warehouse_id', 'total_qty', 'total_used', NULL, NULL, 'ADJUSTMENT_IN', TRUE, TRUE),
|
|
||||||
('AFKIR_CULLING_MATI', 'STOCKABLE', 'STOCK_TRANSFER_IN', 'stock_transfer_details', 'id', 'dest_product_warehouse_id', 'total_qty', 'total_used', NULL, 'deleted_at IS NULL', 'STOCK_TRANSFER_IN', TRUE, TRUE),
|
|
||||||
('AFKIR_CULLING_MATI', 'STOCKABLE', 'RECORDING_DEPLETION_IN', 'recording_depletions', 'id', 'product_warehouse_id', 'qty', NULL, NULL, NULL, 'RECORDING_DEPLETION', TRUE, TRUE),
|
|
||||||
|
|
||||||
-- AFKIR/CULLING/MATI USABLE
|
|
||||||
('AFKIR_CULLING_MATI', 'USABLE', 'ADJUSTMENT_OUT', 'adjustment_stocks', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_qty', NULL, 'ADJUSTMENT_OUT', TRUE, TRUE),
|
|
||||||
('AFKIR_CULLING_MATI', 'USABLE', 'STOCK_TRANSFER_OUT', 'stock_transfer_details', 'id', 'source_product_warehouse_id', 'usage_qty', NULL, 'pending_qty', 'deleted_at IS NULL', 'STOCK_TRANSFER_OUT', TRUE, TRUE),
|
|
||||||
('AFKIR_CULLING_MATI', 'USABLE', 'MARKETING_OUT', 'marketing_delivery_products', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_qty', NULL, 'MARKETING_DELIVERY', TRUE, TRUE),
|
|
||||||
|
|
||||||
-- PAKAN STOCKABLE
|
|
||||||
('PAKAN', 'STOCKABLE', 'ADJUSTMENT_IN', 'adjustment_stocks', 'id', 'product_warehouse_id', 'total_qty', 'total_used', NULL, NULL, 'ADJUSTMENT_IN', TRUE, TRUE),
|
|
||||||
('PAKAN', 'STOCKABLE', 'STOCK_TRANSFER_IN', 'stock_transfer_details', 'id', 'dest_product_warehouse_id', 'total_qty', 'total_used', NULL, 'deleted_at IS NULL', 'STOCK_TRANSFER_IN', TRUE, TRUE),
|
|
||||||
('PAKAN', 'STOCKABLE', 'PURCHASE_IN', 'purchase_items', 'id', 'product_warehouse_id', 'total_qty', 'total_used', NULL, NULL, 'PURCHASE_ITEMS', TRUE, TRUE),
|
|
||||||
|
|
||||||
-- PAKAN USABLE
|
|
||||||
('PAKAN', 'USABLE', 'ADJUSTMENT_OUT', 'adjustment_stocks', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_qty', NULL, 'ADJUSTMENT_OUT', TRUE, TRUE),
|
|
||||||
('PAKAN', 'USABLE', 'STOCK_TRANSFER_OUT', 'stock_transfer_details', 'id', 'source_product_warehouse_id', 'usage_qty', NULL, 'pending_qty', 'deleted_at IS NULL', 'STOCK_TRANSFER_OUT', TRUE, TRUE),
|
|
||||||
('PAKAN', 'USABLE', 'RECORDING_STOCK_OUT', 'recording_stocks', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_qty', NULL, 'RECORDING_STOCK', TRUE, TRUE),
|
|
||||||
('PAKAN', 'USABLE', 'MARKETING_OUT', 'marketing_delivery_products', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_qty', NULL, 'MARKETING_DELIVERY', TRUE, TRUE),
|
|
||||||
|
|
||||||
-- OVK STOCKABLE
|
|
||||||
('OVK', 'STOCKABLE', 'ADJUSTMENT_IN', 'adjustment_stocks', 'id', 'product_warehouse_id', 'total_qty', 'total_used', NULL, NULL, 'ADJUSTMENT_IN', TRUE, TRUE),
|
|
||||||
('OVK', 'STOCKABLE', 'STOCK_TRANSFER_IN', 'stock_transfer_details', 'id', 'dest_product_warehouse_id', 'total_qty', 'total_used', NULL, 'deleted_at IS NULL', 'STOCK_TRANSFER_IN', TRUE, TRUE),
|
|
||||||
('OVK', 'STOCKABLE', 'PURCHASE_IN', 'purchase_items', 'id', 'product_warehouse_id', 'total_qty', 'total_used', NULL, NULL, 'PURCHASE_ITEMS', TRUE, TRUE),
|
|
||||||
|
|
||||||
-- OVK USABLE
|
|
||||||
('OVK', 'USABLE', 'ADJUSTMENT_OUT', 'adjustment_stocks', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_qty', NULL, 'ADJUSTMENT_OUT', TRUE, TRUE),
|
|
||||||
('OVK', 'USABLE', 'STOCK_TRANSFER_OUT', 'stock_transfer_details', 'id', 'source_product_warehouse_id', 'usage_qty', NULL, 'pending_qty', 'deleted_at IS NULL', 'STOCK_TRANSFER_OUT', TRUE, TRUE),
|
|
||||||
('OVK', 'USABLE', 'RECORDING_STOCK_OUT', 'recording_stocks', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_qty', NULL, 'RECORDING_STOCK', TRUE, TRUE),
|
|
||||||
('OVK', 'USABLE', 'MARKETING_OUT', 'marketing_delivery_products', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_qty', NULL, 'MARKETING_DELIVERY', TRUE, TRUE),
|
|
||||||
|
|
||||||
-- TELUR STOCKABLE
|
|
||||||
('TELUR', 'STOCKABLE', 'ADJUSTMENT_IN', 'adjustment_stocks', 'id', 'product_warehouse_id', 'total_qty', 'total_used', NULL, NULL, 'ADJUSTMENT_IN', TRUE, TRUE),
|
|
||||||
('TELUR', 'STOCKABLE', 'STOCK_TRANSFER_IN', 'stock_transfer_details', 'id', 'dest_product_warehouse_id', 'total_qty', 'total_used', NULL, 'deleted_at IS NULL', 'STOCK_TRANSFER_IN', TRUE, TRUE),
|
|
||||||
('TELUR', 'STOCKABLE', 'RECORDING_EGG_IN', 'recording_eggs', 'id', 'product_warehouse_id', 'total_qty', 'total_used', NULL, NULL, 'RECORDING_EGG', TRUE, TRUE),
|
|
||||||
|
|
||||||
-- TELUR USABLE
|
|
||||||
('TELUR', 'USABLE', 'ADJUSTMENT_OUT', 'adjustment_stocks', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_qty', NULL, 'ADJUSTMENT_OUT', TRUE, TRUE),
|
|
||||||
('TELUR', 'USABLE', 'STOCK_TRANSFER_OUT', 'stock_transfer_details', 'id', 'source_product_warehouse_id', 'usage_qty', NULL, 'pending_qty', 'deleted_at IS NULL', 'STOCK_TRANSFER_OUT', TRUE, TRUE),
|
|
||||||
('TELUR', 'USABLE', 'MARKETING_OUT', 'marketing_delivery_products', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_qty', NULL, 'MARKETING_DELIVERY', TRUE, TRUE),
|
|
||||||
|
|
||||||
-- TELUR_GRADE STOCKABLE
|
|
||||||
('TELUR_GRADE', 'STOCKABLE', 'ADJUSTMENT_IN', 'adjustment_stocks', 'id', 'product_warehouse_id', 'total_qty', 'total_used', NULL, NULL, 'ADJUSTMENT_IN', TRUE, TRUE),
|
|
||||||
('TELUR_GRADE', 'STOCKABLE', 'STOCK_TRANSFER_IN', 'stock_transfer_details', 'id', 'dest_product_warehouse_id', 'total_qty', 'total_used', NULL, 'deleted_at IS NULL', 'STOCK_TRANSFER_IN', TRUE, TRUE),
|
|
||||||
('TELUR_GRADE', 'STOCKABLE', 'RECORDING_EGG_IN', 'recording_eggs', 'id', 'product_warehouse_id', 'total_qty', 'total_used', NULL, NULL, 'RECORDING_EGG', TRUE, TRUE),
|
|
||||||
|
|
||||||
-- TELUR_GRADE USABLE
|
|
||||||
('TELUR_GRADE', 'USABLE', 'ADJUSTMENT_OUT', 'adjustment_stocks', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_qty', NULL, 'ADJUSTMENT_OUT', TRUE, TRUE),
|
|
||||||
('TELUR_GRADE', 'USABLE', 'STOCK_TRANSFER_OUT', 'stock_transfer_details', 'id', 'source_product_warehouse_id', 'usage_qty', NULL, 'pending_qty', 'deleted_at IS NULL', 'STOCK_TRANSFER_OUT', TRUE, TRUE),
|
|
||||||
('TELUR_GRADE', 'USABLE', 'MARKETING_OUT', 'marketing_delivery_products', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_qty', NULL, 'MARKETING_DELIVERY', TRUE, TRUE)
|
|
||||||
ON CONFLICT (flag_group_code, lane, function_code, source_table) DO UPDATE
|
|
||||||
SET
|
|
||||||
source_id_column = EXCLUDED.source_id_column,
|
|
||||||
product_warehouse_col = EXCLUDED.product_warehouse_col,
|
|
||||||
quantity_col = EXCLUDED.quantity_col,
|
|
||||||
used_quantity_col = EXCLUDED.used_quantity_col,
|
|
||||||
pending_quantity_col = EXCLUDED.pending_quantity_col,
|
|
||||||
scope_sql = EXCLUDED.scope_sql,
|
|
||||||
legacy_type_key = EXCLUDED.legacy_type_key,
|
|
||||||
allow_pending_default = EXCLUDED.allow_pending_default,
|
|
||||||
is_active = EXCLUDED.is_active,
|
|
||||||
updated_at = NOW();
|
|
||||||
|
|
||||||
INSERT INTO fifo_stock_v2_overconsume_rules(flag_group_code, function_code, lane, allow_overconsume, priority, reason, is_active)
|
|
||||||
SELECT NULL, NULL, 'USABLE', TRUE, 999, 'fifo_v2_default_allow', TRUE
|
|
||||||
WHERE NOT EXISTS (
|
|
||||||
SELECT 1 FROM fifo_stock_v2_overconsume_rules
|
|
||||||
WHERE flag_group_code IS NULL
|
|
||||||
AND function_code IS NULL
|
|
||||||
AND lane = 'USABLE'
|
|
||||||
AND reason = 'fifo_v2_default_allow'
|
|
||||||
);
|
|
||||||
|
|
||||||
INSERT INTO fifo_stock_v2_overconsume_rules(flag_group_code, function_code, lane, allow_overconsume, priority, reason, is_active)
|
|
||||||
SELECT 'AYAM', 'RECORDING_DEPLETION_OUT', 'USABLE', FALSE, 10, 'fifo_v2_exception_ayam_depletion_block', TRUE
|
|
||||||
WHERE NOT EXISTS (
|
|
||||||
SELECT 1 FROM fifo_stock_v2_overconsume_rules
|
|
||||||
WHERE flag_group_code = 'AYAM'
|
|
||||||
AND function_code = 'RECORDING_DEPLETION_OUT'
|
|
||||||
AND lane = 'USABLE'
|
|
||||||
AND reason = 'fifo_v2_exception_ayam_depletion_block'
|
|
||||||
);
|
|
||||||
|
|
||||||
INSERT INTO fifo_stock_v2_overconsume_rules(flag_group_code, function_code, lane, allow_overconsume, priority, reason, is_active)
|
|
||||||
SELECT NULL, 'MARKETING_OUT', 'USABLE', FALSE, 20, 'fifo_v2_exception_marketing_block', TRUE
|
|
||||||
WHERE NOT EXISTS (
|
|
||||||
SELECT 1 FROM fifo_stock_v2_overconsume_rules
|
|
||||||
WHERE flag_group_code IS NULL
|
|
||||||
AND function_code = 'MARKETING_OUT'
|
|
||||||
AND lane = 'USABLE'
|
|
||||||
AND reason = 'fifo_v2_exception_marketing_block'
|
|
||||||
);
|
|
||||||
|
|
||||||
INSERT INTO fifo_stock_v2_overconsume_rules(flag_group_code, function_code, lane, allow_overconsume, priority, reason, is_active)
|
|
||||||
SELECT NULL, 'STOCK_TRANSFER_OUT', 'USABLE', FALSE, 30, 'fifo_v2_exception_transfer_block', TRUE
|
|
||||||
WHERE NOT EXISTS (
|
|
||||||
SELECT 1 FROM fifo_stock_v2_overconsume_rules
|
|
||||||
WHERE flag_group_code IS NULL
|
|
||||||
AND function_code = 'STOCK_TRANSFER_OUT'
|
|
||||||
AND lane = 'USABLE'
|
|
||||||
AND reason = 'fifo_v2_exception_transfer_block'
|
|
||||||
);
|
|
||||||
|
|
||||||
INSERT INTO fifo_stock_v2_overconsume_rules(flag_group_code, function_code, lane, allow_overconsume, priority, reason, is_active)
|
|
||||||
SELECT NULL, 'ADJUSTMENT_OUT', 'USABLE', FALSE, 40, 'fifo_v2_exception_adjustment_block', TRUE
|
|
||||||
WHERE NOT EXISTS (
|
|
||||||
SELECT 1 FROM fifo_stock_v2_overconsume_rules
|
|
||||||
WHERE flag_group_code IS NULL
|
|
||||||
AND function_code = 'ADJUSTMENT_OUT'
|
|
||||||
AND lane = 'USABLE'
|
|
||||||
AND reason = 'fifo_v2_exception_adjustment_block'
|
|
||||||
);
|
|
||||||
|
|
||||||
INSERT INTO fifo_stock_v2_overconsume_rules(flag_group_code, function_code, lane, allow_overconsume, priority, reason, is_active)
|
|
||||||
SELECT NULL, 'TRANSFER_TO_LAYING_OUT', 'USABLE', FALSE, 50, 'fifo_v2_exception_transfer_laying_block', TRUE
|
|
||||||
WHERE NOT EXISTS (
|
|
||||||
SELECT 1 FROM fifo_stock_v2_overconsume_rules
|
|
||||||
WHERE flag_group_code IS NULL
|
|
||||||
AND function_code = 'TRANSFER_TO_LAYING_OUT'
|
|
||||||
AND lane = 'USABLE'
|
|
||||||
AND reason = 'fifo_v2_exception_transfer_laying_block'
|
|
||||||
);
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
-13
@@ -1,13 +0,0 @@
|
|||||||
BEGIN;
|
|
||||||
|
|
||||||
-- Restore CHICKIN route if rollback is required.
|
|
||||||
-- NOTE: released PROJECT_CHICKIN allocations are not restored by this down migration.
|
|
||||||
UPDATE fifo_stock_v2_route_rules
|
|
||||||
SET is_active = TRUE,
|
|
||||||
updated_at = NOW()
|
|
||||||
WHERE flag_group_code = 'AYAM'
|
|
||||||
AND lane = 'USABLE'
|
|
||||||
AND function_code = 'CHICKIN_OUT'
|
|
||||||
AND source_table = 'project_chickins';
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
-151
@@ -1,151 +0,0 @@
|
|||||||
BEGIN;
|
|
||||||
|
|
||||||
-- Disable CHICKIN as FIFO USABLE so chick-in acts as business tagging/conversion,
|
|
||||||
-- not physical stock consumption.
|
|
||||||
UPDATE fifo_stock_v2_route_rules
|
|
||||||
SET is_active = FALSE,
|
|
||||||
updated_at = NOW()
|
|
||||||
WHERE flag_group_code = 'AYAM'
|
|
||||||
AND lane = 'USABLE'
|
|
||||||
AND function_code = 'CHICKIN_OUT'
|
|
||||||
AND source_table = 'project_chickins'
|
|
||||||
AND is_active = TRUE;
|
|
||||||
|
|
||||||
-- Release existing active allocations created by PROJECT_CHICKIN
|
|
||||||
-- and return warehouse qty back.
|
|
||||||
WITH released AS (
|
|
||||||
UPDATE stock_allocations
|
|
||||||
SET status = 'RELEASED',
|
|
||||||
released_at = COALESCE(released_at, NOW()),
|
|
||||||
updated_at = NOW(),
|
|
||||||
note = CASE
|
|
||||||
WHEN COALESCE(note, '') = '' THEN 'fifo_v2_chickin_conversion_release'
|
|
||||||
ELSE note || '; fifo_v2_chickin_conversion_release'
|
|
||||||
END
|
|
||||||
WHERE usable_type = 'PROJECT_CHICKIN'
|
|
||||||
AND status = 'ACTIVE'
|
|
||||||
RETURNING product_warehouse_id, qty
|
|
||||||
),
|
|
||||||
pw_delta AS (
|
|
||||||
SELECT product_warehouse_id, COALESCE(SUM(qty), 0) AS qty_delta
|
|
||||||
FROM released
|
|
||||||
GROUP BY product_warehouse_id
|
|
||||||
)
|
|
||||||
UPDATE product_warehouses pw
|
|
||||||
SET qty = COALESCE(pw.qty, 0) + d.qty_delta
|
|
||||||
FROM pw_delta d
|
|
||||||
WHERE pw.id = d.product_warehouse_id;
|
|
||||||
|
|
||||||
-- Resync stockable total_used columns from remaining ACTIVE allocations.
|
|
||||||
|
|
||||||
-- purchase_items (PURCHASE_ITEMS)
|
|
||||||
UPDATE purchase_items pi
|
|
||||||
SET total_used = COALESCE(a.used, 0)
|
|
||||||
FROM (
|
|
||||||
SELECT stockable_id, SUM(qty) AS used
|
|
||||||
FROM stock_allocations
|
|
||||||
WHERE status = 'ACTIVE'
|
|
||||||
AND stockable_type = 'PURCHASE_ITEMS'
|
|
||||||
GROUP BY stockable_id
|
|
||||||
) a
|
|
||||||
WHERE pi.id = a.stockable_id;
|
|
||||||
|
|
||||||
UPDATE purchase_items pi
|
|
||||||
SET total_used = 0
|
|
||||||
WHERE NOT EXISTS (
|
|
||||||
SELECT 1
|
|
||||||
FROM stock_allocations sa
|
|
||||||
WHERE sa.status = 'ACTIVE'
|
|
||||||
AND sa.stockable_type = 'PURCHASE_ITEMS'
|
|
||||||
AND sa.stockable_id = pi.id
|
|
||||||
);
|
|
||||||
|
|
||||||
-- stock_transfer_details (STOCK_TRANSFER_IN)
|
|
||||||
UPDATE stock_transfer_details std
|
|
||||||
SET total_used = COALESCE(a.used, 0)
|
|
||||||
FROM (
|
|
||||||
SELECT stockable_id, SUM(qty) AS used
|
|
||||||
FROM stock_allocations
|
|
||||||
WHERE status = 'ACTIVE'
|
|
||||||
AND stockable_type = 'STOCK_TRANSFER_IN'
|
|
||||||
GROUP BY stockable_id
|
|
||||||
) a
|
|
||||||
WHERE std.id = a.stockable_id;
|
|
||||||
|
|
||||||
UPDATE stock_transfer_details std
|
|
||||||
SET total_used = 0
|
|
||||||
WHERE NOT EXISTS (
|
|
||||||
SELECT 1
|
|
||||||
FROM stock_allocations sa
|
|
||||||
WHERE sa.status = 'ACTIVE'
|
|
||||||
AND sa.stockable_type = 'STOCK_TRANSFER_IN'
|
|
||||||
AND sa.stockable_id = std.id
|
|
||||||
);
|
|
||||||
|
|
||||||
-- adjustment_stocks (ADJUSTMENT_IN)
|
|
||||||
UPDATE adjustment_stocks ast
|
|
||||||
SET total_used = COALESCE(a.used, 0)
|
|
||||||
FROM (
|
|
||||||
SELECT stockable_id, SUM(qty) AS used
|
|
||||||
FROM stock_allocations
|
|
||||||
WHERE status = 'ACTIVE'
|
|
||||||
AND stockable_type = 'ADJUSTMENT_IN'
|
|
||||||
GROUP BY stockable_id
|
|
||||||
) a
|
|
||||||
WHERE ast.id = a.stockable_id;
|
|
||||||
|
|
||||||
UPDATE adjustment_stocks ast
|
|
||||||
SET total_used = 0
|
|
||||||
WHERE NOT EXISTS (
|
|
||||||
SELECT 1
|
|
||||||
FROM stock_allocations sa
|
|
||||||
WHERE sa.status = 'ACTIVE'
|
|
||||||
AND sa.stockable_type = 'ADJUSTMENT_IN'
|
|
||||||
AND sa.stockable_id = ast.id
|
|
||||||
);
|
|
||||||
|
|
||||||
-- laying_transfer_targets (TRANSFERTOLAYING_IN)
|
|
||||||
UPDATE laying_transfer_targets ltt
|
|
||||||
SET total_used = COALESCE(a.used, 0)
|
|
||||||
FROM (
|
|
||||||
SELECT stockable_id, SUM(qty) AS used
|
|
||||||
FROM stock_allocations
|
|
||||||
WHERE status = 'ACTIVE'
|
|
||||||
AND stockable_type = 'TRANSFERTOLAYING_IN'
|
|
||||||
GROUP BY stockable_id
|
|
||||||
) a
|
|
||||||
WHERE ltt.id = a.stockable_id;
|
|
||||||
|
|
||||||
UPDATE laying_transfer_targets ltt
|
|
||||||
SET total_used = 0
|
|
||||||
WHERE NOT EXISTS (
|
|
||||||
SELECT 1
|
|
||||||
FROM stock_allocations sa
|
|
||||||
WHERE sa.status = 'ACTIVE'
|
|
||||||
AND sa.stockable_type = 'TRANSFERTOLAYING_IN'
|
|
||||||
AND sa.stockable_id = ltt.id
|
|
||||||
);
|
|
||||||
|
|
||||||
-- recording_eggs (RECORDING_EGG)
|
|
||||||
UPDATE recording_eggs re
|
|
||||||
SET total_used = COALESCE(a.used, 0)
|
|
||||||
FROM (
|
|
||||||
SELECT stockable_id, SUM(qty) AS used
|
|
||||||
FROM stock_allocations
|
|
||||||
WHERE status = 'ACTIVE'
|
|
||||||
AND stockable_type = 'RECORDING_EGG'
|
|
||||||
GROUP BY stockable_id
|
|
||||||
) a
|
|
||||||
WHERE re.id = a.stockable_id;
|
|
||||||
|
|
||||||
UPDATE recording_eggs re
|
|
||||||
SET total_used = 0
|
|
||||||
WHERE NOT EXISTS (
|
|
||||||
SELECT 1
|
|
||||||
FROM stock_allocations sa
|
|
||||||
WHERE sa.status = 'ACTIVE'
|
|
||||||
AND sa.stockable_type = 'RECORDING_EGG'
|
|
||||||
AND sa.stockable_id = re.id
|
|
||||||
);
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
BEGIN;
|
|
||||||
|
|
||||||
DELETE FROM fifo_stock_v2_route_rules
|
|
||||||
WHERE flag_group_code = 'AYAM'
|
|
||||||
AND lane = 'USABLE'
|
|
||||||
AND function_code = 'CHICKIN_OUT'
|
|
||||||
AND source_table = 'project_chickins';
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
BEGIN;
|
|
||||||
|
|
||||||
INSERT INTO fifo_stock_v2_route_rules(
|
|
||||||
flag_group_code,
|
|
||||||
lane,
|
|
||||||
function_code,
|
|
||||||
source_table,
|
|
||||||
source_id_column,
|
|
||||||
product_warehouse_col,
|
|
||||||
quantity_col,
|
|
||||||
used_quantity_col,
|
|
||||||
pending_quantity_col,
|
|
||||||
scope_sql,
|
|
||||||
legacy_type_key,
|
|
||||||
allow_pending_default,
|
|
||||||
is_active
|
|
||||||
)
|
|
||||||
VALUES
|
|
||||||
('AYAM', 'USABLE', 'CHICKIN_OUT', 'project_chickins', 'id', 'product_warehouse_id', 'usage_qty', NULL, 'pending_usage_qty', 'deleted_at IS NULL', 'PROJECT_CHICKIN', TRUE, TRUE)
|
|
||||||
ON CONFLICT (flag_group_code, lane, function_code, source_table) DO UPDATE
|
|
||||||
SET
|
|
||||||
source_id_column = EXCLUDED.source_id_column,
|
|
||||||
product_warehouse_col = EXCLUDED.product_warehouse_col,
|
|
||||||
quantity_col = EXCLUDED.quantity_col,
|
|
||||||
used_quantity_col = EXCLUDED.used_quantity_col,
|
|
||||||
pending_quantity_col = EXCLUDED.pending_quantity_col,
|
|
||||||
scope_sql = EXCLUDED.scope_sql,
|
|
||||||
legacy_type_key = EXCLUDED.legacy_type_key,
|
|
||||||
allow_pending_default = EXCLUDED.allow_pending_default,
|
|
||||||
updated_at = NOW(),
|
|
||||||
-- Keep existing is_active (do not override disable migration if it was intentional).
|
|
||||||
is_active = fifo_stock_v2_route_rules.is_active;
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
-7
@@ -1,7 +0,0 @@
|
|||||||
BEGIN;
|
|
||||||
|
|
||||||
DELETE FROM fifo_stock_v2_flag_members
|
|
||||||
WHERE flag_name = 'AYAM'
|
|
||||||
AND flag_group_code = 'AYAM';
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
-13
@@ -1,13 +0,0 @@
|
|||||||
BEGIN;
|
|
||||||
|
|
||||||
INSERT INTO fifo_stock_v2_flag_members(flag_name, flag_group_code, priority, is_active, created_at, updated_at)
|
|
||||||
VALUES
|
|
||||||
('AYAM', 'AYAM', 5, TRUE, NOW(), NOW())
|
|
||||||
ON CONFLICT (flag_name) DO UPDATE
|
|
||||||
SET
|
|
||||||
flag_group_code = EXCLUDED.flag_group_code,
|
|
||||||
priority = EXCLUDED.priority,
|
|
||||||
is_active = TRUE,
|
|
||||||
updated_at = NOW();
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
BEGIN;
|
|
||||||
|
|
||||||
ALTER TABLE daily_checklists
|
|
||||||
DROP CONSTRAINT IF EXISTS fk_daily_checklists_kandang;
|
|
||||||
|
|
||||||
UPDATE daily_checklists dc
|
|
||||||
SET kandang_id = k.id
|
|
||||||
FROM kandangs k
|
|
||||||
WHERE
|
|
||||||
dc.kandang_id = k.kandang_group_id;
|
|
||||||
|
|
||||||
ALTER TABLE daily_checklists
|
|
||||||
ADD CONSTRAINT fk_daily_checklists_kandang
|
|
||||||
FOREIGN KEY (kandang_id) REFERENCES kandangs (id) ON DELETE CASCADE;
|
|
||||||
|
|
||||||
DROP INDEX IF EXISTS idx_kandangs_kandang_group_id;
|
|
||||||
|
|
||||||
ALTER TABLE kandangs
|
|
||||||
DROP CONSTRAINT IF EXISTS fk_kandangs_kandang_group;
|
|
||||||
|
|
||||||
ALTER TABLE kandangs
|
|
||||||
DROP COLUMN IF EXISTS kandang_group_id;
|
|
||||||
|
|
||||||
DROP INDEX IF EXISTS kandang_groups_name_unique;
|
|
||||||
|
|
||||||
DROP TABLE IF EXISTS kandang_groups;
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
@@ -1,91 +0,0 @@
|
|||||||
BEGIN;
|
|
||||||
|
|
||||||
CREATE TABLE kandang_groups (
|
|
||||||
id BIGSERIAL PRIMARY KEY,
|
|
||||||
name VARCHAR(50) NOT NULL,
|
|
||||||
status VARCHAR(20) NOT NULL,
|
|
||||||
location_id BIGINT NOT NULL REFERENCES locations (id) ON DELETE RESTRICT ON UPDATE CASCADE,
|
|
||||||
pic_id BIGINT REFERENCES users (id) ON DELETE SET NULL ON UPDATE CASCADE,
|
|
||||||
created_at TIMESTAMPTZ DEFAULT NOW (),
|
|
||||||
updated_at TIMESTAMPTZ DEFAULT NOW (),
|
|
||||||
deleted_at TIMESTAMPTZ,
|
|
||||||
created_by BIGINT REFERENCES users (id) ON DELETE SET NULL ON UPDATE CASCADE
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE UNIQUE INDEX kandang_groups_name_unique ON kandang_groups (name)
|
|
||||||
WHERE
|
|
||||||
deleted_at IS NULL;
|
|
||||||
|
|
||||||
ALTER TABLE kandangs
|
|
||||||
ADD COLUMN kandang_group_id BIGINT;
|
|
||||||
|
|
||||||
CREATE TEMP TABLE tmp_kandang_group_map (
|
|
||||||
kandang_id BIGINT PRIMARY KEY,
|
|
||||||
kandang_group_id BIGINT NOT NULL
|
|
||||||
) ON COMMIT DROP;
|
|
||||||
|
|
||||||
INSERT INTO tmp_kandang_group_map (kandang_id, kandang_group_id)
|
|
||||||
SELECT
|
|
||||||
k.id,
|
|
||||||
nextval(pg_get_serial_sequence('kandang_groups', 'id'))
|
|
||||||
FROM kandangs k
|
|
||||||
ORDER BY
|
|
||||||
k.id;
|
|
||||||
|
|
||||||
INSERT INTO kandang_groups (
|
|
||||||
id,
|
|
||||||
name,
|
|
||||||
status,
|
|
||||||
location_id,
|
|
||||||
pic_id,
|
|
||||||
created_at,
|
|
||||||
updated_at,
|
|
||||||
deleted_at,
|
|
||||||
created_by
|
|
||||||
)
|
|
||||||
SELECT
|
|
||||||
m.kandang_group_id,
|
|
||||||
k.name,
|
|
||||||
k.status,
|
|
||||||
k.location_id,
|
|
||||||
CASE WHEN pic.id IS NOT NULL THEN k.pic_id ELSE NULL END,
|
|
||||||
k.created_at,
|
|
||||||
k.updated_at,
|
|
||||||
k.deleted_at,
|
|
||||||
CASE WHEN creator.id IS NOT NULL THEN k.created_by ELSE NULL END
|
|
||||||
FROM kandangs k
|
|
||||||
JOIN tmp_kandang_group_map m ON m.kandang_id = k.id
|
|
||||||
LEFT JOIN users pic ON pic.id = k.pic_id
|
|
||||||
LEFT JOIN users creator ON creator.id = k.created_by
|
|
||||||
ORDER BY
|
|
||||||
k.id;
|
|
||||||
|
|
||||||
UPDATE kandangs k
|
|
||||||
SET kandang_group_id = m.kandang_group_id
|
|
||||||
FROM tmp_kandang_group_map m
|
|
||||||
WHERE
|
|
||||||
m.kandang_id = k.id;
|
|
||||||
|
|
||||||
ALTER TABLE daily_checklists
|
|
||||||
DROP CONSTRAINT IF EXISTS fk_daily_checklists_kandang;
|
|
||||||
|
|
||||||
UPDATE daily_checklists dc
|
|
||||||
SET kandang_id = m.kandang_group_id
|
|
||||||
FROM tmp_kandang_group_map m
|
|
||||||
WHERE
|
|
||||||
dc.kandang_id = m.kandang_id;
|
|
||||||
|
|
||||||
ALTER TABLE daily_checklists
|
|
||||||
ADD CONSTRAINT fk_daily_checklists_kandang
|
|
||||||
FOREIGN KEY (kandang_id) REFERENCES kandang_groups (id) ON DELETE CASCADE;
|
|
||||||
|
|
||||||
ALTER TABLE kandangs
|
|
||||||
ALTER COLUMN kandang_group_id SET NOT NULL;
|
|
||||||
|
|
||||||
ALTER TABLE kandangs
|
|
||||||
ADD CONSTRAINT fk_kandangs_kandang_group
|
|
||||||
FOREIGN KEY (kandang_group_id) REFERENCES kandang_groups (id) ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
||||||
|
|
||||||
CREATE INDEX idx_kandangs_kandang_group_id ON kandangs (kandang_group_id);
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
BEGIN;
|
|
||||||
|
|
||||||
ALTER TABLE employee_kandangs
|
|
||||||
DROP CONSTRAINT IF EXISTS fk_employee_kandangs_kandang;
|
|
||||||
|
|
||||||
ALTER TABLE employee_kandangs
|
|
||||||
DROP CONSTRAINT IF EXISTS uq_employee_kandangs;
|
|
||||||
|
|
||||||
CREATE TEMP TABLE tmp_kandang_group_to_kandang_map (
|
|
||||||
kandang_group_id BIGINT PRIMARY KEY,
|
|
||||||
kandang_id BIGINT NOT NULL
|
|
||||||
) ON COMMIT DROP;
|
|
||||||
|
|
||||||
INSERT INTO tmp_kandang_group_to_kandang_map (kandang_group_id, kandang_id)
|
|
||||||
SELECT
|
|
||||||
k.kandang_group_id,
|
|
||||||
MIN(k.id) AS kandang_id
|
|
||||||
FROM kandangs k
|
|
||||||
WHERE k.kandang_group_id IS NOT NULL
|
|
||||||
GROUP BY k.kandang_group_id;
|
|
||||||
|
|
||||||
DO $$
|
|
||||||
BEGIN
|
|
||||||
IF EXISTS (
|
|
||||||
SELECT 1
|
|
||||||
FROM employee_kandangs ek
|
|
||||||
LEFT JOIN tmp_kandang_group_to_kandang_map m ON m.kandang_group_id = ek.kandang_id
|
|
||||||
WHERE m.kandang_id IS NULL
|
|
||||||
) THEN
|
|
||||||
RAISE EXCEPTION 'Cannot rollback employee_kandangs migration: kandang_group_id has no kandang mapping';
|
|
||||||
END IF;
|
|
||||||
END $$;
|
|
||||||
|
|
||||||
UPDATE employee_kandangs ek
|
|
||||||
SET kandang_id = m.kandang_id,
|
|
||||||
updated_at = NOW()
|
|
||||||
FROM tmp_kandang_group_to_kandang_map m
|
|
||||||
WHERE m.kandang_group_id = ek.kandang_id;
|
|
||||||
|
|
||||||
DELETE FROM employee_kandangs ek1
|
|
||||||
USING employee_kandangs ek2
|
|
||||||
WHERE ek1.id > ek2.id
|
|
||||||
AND ek1.employee_id = ek2.employee_id
|
|
||||||
AND ek1.kandang_id = ek2.kandang_id;
|
|
||||||
|
|
||||||
ALTER TABLE employee_kandangs
|
|
||||||
ADD CONSTRAINT fk_employee_kandangs_kandang
|
|
||||||
FOREIGN KEY (kandang_id) REFERENCES kandangs (id)
|
|
||||||
ON DELETE CASCADE;
|
|
||||||
|
|
||||||
ALTER TABLE employee_kandangs
|
|
||||||
ADD CONSTRAINT uq_employee_kandangs UNIQUE (employee_id, kandang_id);
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
BEGIN;
|
|
||||||
|
|
||||||
ALTER TABLE employee_kandangs
|
|
||||||
DROP CONSTRAINT IF EXISTS fk_employee_kandangs_kandang;
|
|
||||||
|
|
||||||
ALTER TABLE employee_kandangs
|
|
||||||
DROP CONSTRAINT IF EXISTS uq_employee_kandangs;
|
|
||||||
|
|
||||||
DO $$
|
|
||||||
BEGIN
|
|
||||||
IF EXISTS (
|
|
||||||
SELECT 1
|
|
||||||
FROM employee_kandangs ek
|
|
||||||
LEFT JOIN kandangs k ON k.id = ek.kandang_id
|
|
||||||
WHERE k.kandang_group_id IS NULL
|
|
||||||
) THEN
|
|
||||||
RAISE EXCEPTION 'Cannot migrate employee_kandangs: kandang_id has no kandang_group_id mapping';
|
|
||||||
END IF;
|
|
||||||
END $$;
|
|
||||||
|
|
||||||
UPDATE employee_kandangs ek
|
|
||||||
SET kandang_id = k.kandang_group_id,
|
|
||||||
updated_at = NOW()
|
|
||||||
FROM kandangs k
|
|
||||||
WHERE k.id = ek.kandang_id;
|
|
||||||
|
|
||||||
DELETE FROM employee_kandangs ek1
|
|
||||||
USING employee_kandangs ek2
|
|
||||||
WHERE ek1.id > ek2.id
|
|
||||||
AND ek1.employee_id = ek2.employee_id
|
|
||||||
AND ek1.kandang_id = ek2.kandang_id;
|
|
||||||
|
|
||||||
ALTER TABLE employee_kandangs
|
|
||||||
ADD CONSTRAINT fk_employee_kandangs_kandang
|
|
||||||
FOREIGN KEY (kandang_id) REFERENCES kandang_groups (id)
|
|
||||||
ON DELETE CASCADE;
|
|
||||||
|
|
||||||
ALTER TABLE employee_kandangs
|
|
||||||
ADD CONSTRAINT uq_employee_kandangs UNIQUE (employee_id, kandang_id);
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
+118
@@ -0,0 +1,118 @@
|
|||||||
|
BEGIN;
|
||||||
|
|
||||||
|
-- MARKETING_OUT: if AYAM-only rule exists, convert back to global rule.
|
||||||
|
UPDATE fifo_stock_v2_overconsume_rules
|
||||||
|
SET
|
||||||
|
flag_group_code = NULL,
|
||||||
|
allow_overconsume = FALSE,
|
||||||
|
priority = 20,
|
||||||
|
reason = 'fifo_v2_exception_marketing_block',
|
||||||
|
is_active = TRUE
|
||||||
|
WHERE lane = 'USABLE'
|
||||||
|
AND function_code = 'MARKETING_OUT'
|
||||||
|
AND flag_group_code = 'AYAM'
|
||||||
|
AND reason = 'fifo_v2_exception_marketing_block_ayam_only';
|
||||||
|
|
||||||
|
-- MARKETING_OUT: if global row already exists, keep it active.
|
||||||
|
UPDATE fifo_stock_v2_overconsume_rules
|
||||||
|
SET
|
||||||
|
allow_overconsume = FALSE,
|
||||||
|
priority = 20,
|
||||||
|
is_active = TRUE
|
||||||
|
WHERE lane = 'USABLE'
|
||||||
|
AND function_code = 'MARKETING_OUT'
|
||||||
|
AND flag_group_code IS NULL
|
||||||
|
AND reason = 'fifo_v2_exception_marketing_block';
|
||||||
|
|
||||||
|
-- MARKETING_OUT: insert global rule if still missing.
|
||||||
|
INSERT INTO fifo_stock_v2_overconsume_rules(
|
||||||
|
flag_group_code,
|
||||||
|
function_code,
|
||||||
|
lane,
|
||||||
|
allow_overconsume,
|
||||||
|
priority,
|
||||||
|
reason,
|
||||||
|
is_active
|
||||||
|
)
|
||||||
|
SELECT NULL, 'MARKETING_OUT', 'USABLE', FALSE, 20, 'fifo_v2_exception_marketing_block', TRUE
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM fifo_stock_v2_overconsume_rules
|
||||||
|
WHERE lane = 'USABLE'
|
||||||
|
AND function_code = 'MARKETING_OUT'
|
||||||
|
AND flag_group_code IS NULL
|
||||||
|
AND reason = 'fifo_v2_exception_marketing_block'
|
||||||
|
);
|
||||||
|
|
||||||
|
-- MARKETING_OUT: deactivate AYAM-only duplicates if any remain.
|
||||||
|
UPDATE fifo_stock_v2_overconsume_rules
|
||||||
|
SET
|
||||||
|
is_active = FALSE
|
||||||
|
WHERE lane = 'USABLE'
|
||||||
|
AND function_code = 'MARKETING_OUT'
|
||||||
|
AND flag_group_code = 'AYAM'
|
||||||
|
AND reason = 'fifo_v2_exception_marketing_block_ayam_only';
|
||||||
|
|
||||||
|
-- STOCK_TRANSFER_OUT: if AYAM-only rule exists, convert back to global rule.
|
||||||
|
UPDATE fifo_stock_v2_overconsume_rules
|
||||||
|
SET
|
||||||
|
flag_group_code = NULL,
|
||||||
|
allow_overconsume = FALSE,
|
||||||
|
priority = 30,
|
||||||
|
reason = 'fifo_v2_exception_transfer_block',
|
||||||
|
is_active = TRUE
|
||||||
|
WHERE lane = 'USABLE'
|
||||||
|
AND function_code = 'STOCK_TRANSFER_OUT'
|
||||||
|
AND flag_group_code = 'AYAM'
|
||||||
|
AND reason = 'fifo_v2_exception_transfer_block_ayam_only';
|
||||||
|
|
||||||
|
-- STOCK_TRANSFER_OUT: if global row already exists, keep it active.
|
||||||
|
UPDATE fifo_stock_v2_overconsume_rules
|
||||||
|
SET
|
||||||
|
allow_overconsume = FALSE,
|
||||||
|
priority = 30,
|
||||||
|
is_active = TRUE
|
||||||
|
WHERE lane = 'USABLE'
|
||||||
|
AND function_code = 'STOCK_TRANSFER_OUT'
|
||||||
|
AND flag_group_code IS NULL
|
||||||
|
AND reason = 'fifo_v2_exception_transfer_block';
|
||||||
|
|
||||||
|
-- STOCK_TRANSFER_OUT: insert global rule if still missing.
|
||||||
|
INSERT INTO fifo_stock_v2_overconsume_rules(
|
||||||
|
flag_group_code,
|
||||||
|
function_code,
|
||||||
|
lane,
|
||||||
|
allow_overconsume,
|
||||||
|
priority,
|
||||||
|
reason,
|
||||||
|
is_active
|
||||||
|
)
|
||||||
|
SELECT NULL, 'STOCK_TRANSFER_OUT', 'USABLE', FALSE, 30, 'fifo_v2_exception_transfer_block', TRUE
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM fifo_stock_v2_overconsume_rules
|
||||||
|
WHERE lane = 'USABLE'
|
||||||
|
AND function_code = 'STOCK_TRANSFER_OUT'
|
||||||
|
AND flag_group_code IS NULL
|
||||||
|
AND reason = 'fifo_v2_exception_transfer_block'
|
||||||
|
);
|
||||||
|
|
||||||
|
-- STOCK_TRANSFER_OUT: deactivate AYAM-only duplicates if any remain.
|
||||||
|
UPDATE fifo_stock_v2_overconsume_rules
|
||||||
|
SET
|
||||||
|
is_active = FALSE
|
||||||
|
WHERE lane = 'USABLE'
|
||||||
|
AND function_code = 'STOCK_TRANSFER_OUT'
|
||||||
|
AND flag_group_code = 'AYAM'
|
||||||
|
AND reason = 'fifo_v2_exception_transfer_block_ayam_only';
|
||||||
|
|
||||||
|
-- CHICKIN_OUT: rollback AYAM-only hard-block added by up migration.
|
||||||
|
UPDATE fifo_stock_v2_overconsume_rules
|
||||||
|
SET
|
||||||
|
is_active = FALSE
|
||||||
|
WHERE lane = 'USABLE'
|
||||||
|
AND function_code = 'CHICKIN_OUT'
|
||||||
|
AND flag_group_code = 'AYAM'
|
||||||
|
AND reason = 'fifo_v2_exception_chickin_block_ayam_only';
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
+139
@@ -0,0 +1,139 @@
|
|||||||
|
BEGIN;
|
||||||
|
|
||||||
|
-- MARKETING_OUT: if global rule exists, convert to AYAM-specific.
|
||||||
|
UPDATE fifo_stock_v2_overconsume_rules
|
||||||
|
SET
|
||||||
|
flag_group_code = 'AYAM',
|
||||||
|
allow_overconsume = FALSE,
|
||||||
|
priority = 20,
|
||||||
|
reason = 'fifo_v2_exception_marketing_block_ayam_only',
|
||||||
|
is_active = TRUE
|
||||||
|
WHERE lane = 'USABLE'
|
||||||
|
AND function_code = 'MARKETING_OUT'
|
||||||
|
AND flag_group_code IS NULL
|
||||||
|
AND reason = 'fifo_v2_exception_marketing_block';
|
||||||
|
|
||||||
|
-- MARKETING_OUT: if AYAM-specific row already exists, enforce desired value.
|
||||||
|
UPDATE fifo_stock_v2_overconsume_rules
|
||||||
|
SET
|
||||||
|
allow_overconsume = FALSE,
|
||||||
|
priority = 20,
|
||||||
|
is_active = TRUE
|
||||||
|
WHERE lane = 'USABLE'
|
||||||
|
AND function_code = 'MARKETING_OUT'
|
||||||
|
AND flag_group_code = 'AYAM'
|
||||||
|
AND reason = 'fifo_v2_exception_marketing_block_ayam_only';
|
||||||
|
|
||||||
|
-- MARKETING_OUT: insert AYAM-specific if no suitable row exists.
|
||||||
|
INSERT INTO fifo_stock_v2_overconsume_rules(
|
||||||
|
flag_group_code,
|
||||||
|
function_code,
|
||||||
|
lane,
|
||||||
|
allow_overconsume,
|
||||||
|
priority,
|
||||||
|
reason,
|
||||||
|
is_active
|
||||||
|
)
|
||||||
|
SELECT 'AYAM', 'MARKETING_OUT', 'USABLE', FALSE, 20, 'fifo_v2_exception_marketing_block_ayam_only', TRUE
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM fifo_stock_v2_overconsume_rules
|
||||||
|
WHERE lane = 'USABLE'
|
||||||
|
AND function_code = 'MARKETING_OUT'
|
||||||
|
AND flag_group_code = 'AYAM'
|
||||||
|
AND reason = 'fifo_v2_exception_marketing_block_ayam_only'
|
||||||
|
);
|
||||||
|
|
||||||
|
-- MARKETING_OUT: deactivate remaining global rule (if any duplicate row exists).
|
||||||
|
UPDATE fifo_stock_v2_overconsume_rules
|
||||||
|
SET
|
||||||
|
is_active = FALSE
|
||||||
|
WHERE lane = 'USABLE'
|
||||||
|
AND function_code = 'MARKETING_OUT'
|
||||||
|
AND flag_group_code IS NULL
|
||||||
|
AND reason = 'fifo_v2_exception_marketing_block';
|
||||||
|
|
||||||
|
-- STOCK_TRANSFER_OUT: if global rule exists, convert to AYAM-specific.
|
||||||
|
UPDATE fifo_stock_v2_overconsume_rules
|
||||||
|
SET
|
||||||
|
flag_group_code = 'AYAM',
|
||||||
|
allow_overconsume = FALSE,
|
||||||
|
priority = 30,
|
||||||
|
reason = 'fifo_v2_exception_transfer_block_ayam_only',
|
||||||
|
is_active = TRUE
|
||||||
|
WHERE lane = 'USABLE'
|
||||||
|
AND function_code = 'STOCK_TRANSFER_OUT'
|
||||||
|
AND flag_group_code IS NULL
|
||||||
|
AND reason = 'fifo_v2_exception_transfer_block';
|
||||||
|
|
||||||
|
-- STOCK_TRANSFER_OUT: if AYAM-specific row already exists, enforce desired value.
|
||||||
|
UPDATE fifo_stock_v2_overconsume_rules
|
||||||
|
SET
|
||||||
|
allow_overconsume = FALSE,
|
||||||
|
priority = 30,
|
||||||
|
is_active = TRUE
|
||||||
|
WHERE lane = 'USABLE'
|
||||||
|
AND function_code = 'STOCK_TRANSFER_OUT'
|
||||||
|
AND flag_group_code = 'AYAM'
|
||||||
|
AND reason = 'fifo_v2_exception_transfer_block_ayam_only';
|
||||||
|
|
||||||
|
-- STOCK_TRANSFER_OUT: insert AYAM-specific if no suitable row exists.
|
||||||
|
INSERT INTO fifo_stock_v2_overconsume_rules(
|
||||||
|
flag_group_code,
|
||||||
|
function_code,
|
||||||
|
lane,
|
||||||
|
allow_overconsume,
|
||||||
|
priority,
|
||||||
|
reason,
|
||||||
|
is_active
|
||||||
|
)
|
||||||
|
SELECT 'AYAM', 'STOCK_TRANSFER_OUT', 'USABLE', FALSE, 30, 'fifo_v2_exception_transfer_block_ayam_only', TRUE
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM fifo_stock_v2_overconsume_rules
|
||||||
|
WHERE lane = 'USABLE'
|
||||||
|
AND function_code = 'STOCK_TRANSFER_OUT'
|
||||||
|
AND flag_group_code = 'AYAM'
|
||||||
|
AND reason = 'fifo_v2_exception_transfer_block_ayam_only'
|
||||||
|
);
|
||||||
|
|
||||||
|
-- STOCK_TRANSFER_OUT: deactivate remaining global rule (if any duplicate row exists).
|
||||||
|
UPDATE fifo_stock_v2_overconsume_rules
|
||||||
|
SET
|
||||||
|
is_active = FALSE
|
||||||
|
WHERE lane = 'USABLE'
|
||||||
|
AND function_code = 'STOCK_TRANSFER_OUT'
|
||||||
|
AND flag_group_code IS NULL
|
||||||
|
AND reason = 'fifo_v2_exception_transfer_block';
|
||||||
|
|
||||||
|
-- CHICKIN_OUT: enforce AYAM-specific hard-block (cannot pending).
|
||||||
|
UPDATE fifo_stock_v2_overconsume_rules
|
||||||
|
SET
|
||||||
|
allow_overconsume = FALSE,
|
||||||
|
priority = 25,
|
||||||
|
is_active = TRUE
|
||||||
|
WHERE lane = 'USABLE'
|
||||||
|
AND function_code = 'CHICKIN_OUT'
|
||||||
|
AND flag_group_code = 'AYAM'
|
||||||
|
AND reason = 'fifo_v2_exception_chickin_block_ayam_only';
|
||||||
|
|
||||||
|
INSERT INTO fifo_stock_v2_overconsume_rules(
|
||||||
|
flag_group_code,
|
||||||
|
function_code,
|
||||||
|
lane,
|
||||||
|
allow_overconsume,
|
||||||
|
priority,
|
||||||
|
reason,
|
||||||
|
is_active
|
||||||
|
)
|
||||||
|
SELECT 'AYAM', 'CHICKIN_OUT', 'USABLE', FALSE, 25, 'fifo_v2_exception_chickin_block_ayam_only', TRUE
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM fifo_stock_v2_overconsume_rules
|
||||||
|
WHERE lane = 'USABLE'
|
||||||
|
AND function_code = 'CHICKIN_OUT'
|
||||||
|
AND flag_group_code = 'AYAM'
|
||||||
|
AND reason = 'fifo_v2_exception_chickin_block_ayam_only'
|
||||||
|
);
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
BEGIN;
|
||||||
|
|
||||||
|
ALTER TABLE adjustment_stocks
|
||||||
|
DROP CONSTRAINT IF EXISTS chk_adjustment_stocks_paired_not_self;
|
||||||
|
|
||||||
|
ALTER TABLE adjustment_stocks
|
||||||
|
DROP CONSTRAINT IF EXISTS fk_adjustment_stocks_paired_adjustment_id;
|
||||||
|
|
||||||
|
DROP INDEX IF EXISTS idx_adjustment_stocks_paired_adjustment_id;
|
||||||
|
|
||||||
|
ALTER TABLE adjustment_stocks
|
||||||
|
DROP COLUMN IF EXISTS paired_adjustment_id;
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
+86
@@ -0,0 +1,86 @@
|
|||||||
|
BEGIN;
|
||||||
|
|
||||||
|
ALTER TABLE adjustment_stocks
|
||||||
|
ADD COLUMN IF NOT EXISTS paired_adjustment_id BIGINT NULL;
|
||||||
|
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF NOT EXISTS (
|
||||||
|
SELECT 1 FROM pg_constraint WHERE conname = 'fk_adjustment_stocks_paired_adjustment_id'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE adjustment_stocks
|
||||||
|
ADD CONSTRAINT fk_adjustment_stocks_paired_adjustment_id
|
||||||
|
FOREIGN KEY (paired_adjustment_id)
|
||||||
|
REFERENCES adjustment_stocks(id)
|
||||||
|
ON DELETE SET NULL
|
||||||
|
ON UPDATE CASCADE;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
ALTER TABLE adjustment_stocks
|
||||||
|
DROP CONSTRAINT IF EXISTS chk_adjustment_stocks_paired_not_self;
|
||||||
|
|
||||||
|
ALTER TABLE adjustment_stocks
|
||||||
|
ADD CONSTRAINT chk_adjustment_stocks_paired_not_self
|
||||||
|
CHECK (paired_adjustment_id IS NULL OR paired_adjustment_id <> id);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_adjustment_stocks_paired_adjustment_id
|
||||||
|
ON adjustment_stocks(paired_adjustment_id);
|
||||||
|
|
||||||
|
-- Backfill pairing untuk depletion-out <-> depletion-in existing records.
|
||||||
|
WITH candidates AS (
|
||||||
|
SELECT
|
||||||
|
src.id AS src_id,
|
||||||
|
dst.id AS dst_id,
|
||||||
|
ABS(EXTRACT(EPOCH FROM (dst.created_at - src.created_at))) AS ts_diff,
|
||||||
|
ABS(dst.id - src.id) AS id_diff,
|
||||||
|
ROW_NUMBER() OVER (
|
||||||
|
PARTITION BY src.id
|
||||||
|
ORDER BY ABS(EXTRACT(EPOCH FROM (dst.created_at - src.created_at))) ASC,
|
||||||
|
ABS(dst.id - src.id) ASC,
|
||||||
|
dst.id ASC
|
||||||
|
) AS rn_src,
|
||||||
|
ROW_NUMBER() OVER (
|
||||||
|
PARTITION BY dst.id
|
||||||
|
ORDER BY ABS(EXTRACT(EPOCH FROM (dst.created_at - src.created_at))) ASC,
|
||||||
|
ABS(dst.id - src.id) ASC,
|
||||||
|
src.id ASC
|
||||||
|
) AS rn_dst
|
||||||
|
FROM adjustment_stocks src
|
||||||
|
JOIN adjustment_stocks dst
|
||||||
|
ON dst.id <> src.id
|
||||||
|
AND dst.transaction_type = src.transaction_type
|
||||||
|
AND dst.function_code = 'RECORDING_DEPLETION_IN'
|
||||||
|
AND src.function_code = 'RECORDING_DEPLETION_OUT'
|
||||||
|
AND dst.paired_adjustment_id IS NULL
|
||||||
|
AND src.paired_adjustment_id IS NULL
|
||||||
|
AND ABS((COALESCE(src.usage_qty, 0) + COALESCE(src.pending_qty, 0)) - COALESCE(dst.total_qty, 0)) < 0.0001
|
||||||
|
AND COALESCE(src.price, 0) = COALESCE(dst.price, 0)
|
||||||
|
AND COALESCE(src.grand_total, 0) = COALESCE(dst.grand_total, 0)
|
||||||
|
AND ABS(EXTRACT(EPOCH FROM (dst.created_at - src.created_at))) <= 120
|
||||||
|
),
|
||||||
|
chosen AS (
|
||||||
|
SELECT src_id, dst_id
|
||||||
|
FROM candidates
|
||||||
|
WHERE rn_src = 1
|
||||||
|
AND rn_dst = 1
|
||||||
|
)
|
||||||
|
UPDATE adjustment_stocks src
|
||||||
|
SET paired_adjustment_id = c.dst_id
|
||||||
|
FROM chosen c
|
||||||
|
WHERE src.id = c.src_id
|
||||||
|
AND src.paired_adjustment_id IS NULL;
|
||||||
|
|
||||||
|
WITH chosen AS (
|
||||||
|
SELECT a.id AS src_id, a.paired_adjustment_id AS dst_id
|
||||||
|
FROM adjustment_stocks a
|
||||||
|
WHERE a.function_code = 'RECORDING_DEPLETION_OUT'
|
||||||
|
AND a.paired_adjustment_id IS NOT NULL
|
||||||
|
)
|
||||||
|
UPDATE adjustment_stocks dst
|
||||||
|
SET paired_adjustment_id = c.src_id
|
||||||
|
FROM chosen c
|
||||||
|
WHERE dst.id = c.dst_id
|
||||||
|
AND dst.paired_adjustment_id IS NULL;
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
@@ -5,6 +5,7 @@ import "time"
|
|||||||
type AdjustmentStock struct {
|
type AdjustmentStock struct {
|
||||||
Id uint `gorm:"primaryKey"`
|
Id uint `gorm:"primaryKey"`
|
||||||
ProductWarehouseId uint `gorm:"column:product_warehouse_id;not null"`
|
ProductWarehouseId uint `gorm:"column:product_warehouse_id;not null"`
|
||||||
|
PairedAdjustmentId *uint `gorm:"column:paired_adjustment_id"`
|
||||||
TransactionType string `gorm:"column:transaction_type;type:varchar(100);not null;default:LEGACY"`
|
TransactionType string `gorm:"column:transaction_type;type:varchar(100);not null;default:LEGACY"`
|
||||||
FunctionCode string `gorm:"column:function_code;type:varchar(64)"`
|
FunctionCode string `gorm:"column:function_code;type:varchar(64)"`
|
||||||
TotalQty float64 `gorm:"column:total_qty;default:0"`
|
TotalQty float64 `gorm:"column:total_qty;default:0"`
|
||||||
@@ -18,5 +19,6 @@ type AdjustmentStock struct {
|
|||||||
AdjNumber string `gorm:"column:adj_number;uniqueIndex;not null"`
|
AdjNumber string `gorm:"column:adj_number;uniqueIndex;not null"`
|
||||||
|
|
||||||
ProductWarehouse *ProductWarehouse `gorm:"foreignKey:ProductWarehouseId;references:Id"`
|
ProductWarehouse *ProductWarehouse `gorm:"foreignKey:ProductWarehouseId;references:Id"`
|
||||||
|
PairedAdjustment *AdjustmentStock `gorm:"foreignKey:PairedAdjustmentId;references:Id"`
|
||||||
StockLog *StockLog `gorm:"polymorphic:Loggable;polymorphicType:LoggableType;polymorphicId:LoggableId;polymorphicValue:ADJUSTMENT"`
|
StockLog *StockLog `gorm:"polymorphic:Loggable;polymorphicType:LoggableType;polymorphicId:LoggableId;polymorphicValue:ADJUSTMENT"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ type DailyChecklist struct {
|
|||||||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||||
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
||||||
|
|
||||||
Kandang KandangGroup `gorm:"foreignKey:KandangId;references:Id"`
|
Kandang Kandang `gorm:"foreignKey:KandangId;references:Id"`
|
||||||
Checklist *Checklist `gorm:"foreignKey:ChecklistId;references:Id"`
|
Checklist *Checklist `gorm:"foreignKey:ChecklistId;references:Id"`
|
||||||
Creator *User `gorm:"foreignKey:CreatedBy;references:Id"`
|
Creator *User `gorm:"foreignKey:CreatedBy;references:Id"`
|
||||||
Tasks []DailyChecklistTask `gorm:"foreignKey:DailyChecklistId;references:Id"`
|
Tasks []DailyChecklistTask `gorm:"foreignKey:DailyChecklistId;references:Id"`
|
||||||
|
|||||||
@@ -27,5 +27,5 @@ type EmployeeKandang struct {
|
|||||||
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
||||||
|
|
||||||
Employee Employee `gorm:"foreignKey:EmployeeId;references:Id"`
|
Employee Employee `gorm:"foreignKey:EmployeeId;references:Id"`
|
||||||
Kandang KandangGroup `gorm:"foreignKey:KandangId;references:Id"`
|
Kandang Kandang `gorm:"foreignKey:KandangId;references:Id"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ type Kandang struct {
|
|||||||
Name string `gorm:"type:varchar(50);not null;uniqueIndex:kandangs_name_unique,where:deleted_at IS NULL"`
|
Name string `gorm:"type:varchar(50);not null;uniqueIndex:kandangs_name_unique,where:deleted_at IS NULL"`
|
||||||
Status string `gorm:"type:varchar(50);not null"`
|
Status string `gorm:"type:varchar(50);not null"`
|
||||||
LocationId uint `gorm:"not null"`
|
LocationId uint `gorm:"not null"`
|
||||||
KandangGroupId uint `gorm:"not null"`
|
|
||||||
Capacity float64 `gorm:"not null"`
|
Capacity float64 `gorm:"not null"`
|
||||||
PicId uint `gorm:"not null"`
|
PicId uint `gorm:"not null"`
|
||||||
CreatedBy uint `gorm:"not null"`
|
CreatedBy uint `gorm:"not null"`
|
||||||
@@ -20,7 +19,6 @@ type Kandang struct {
|
|||||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||||
CreatedUser User `gorm:"foreignKey:CreatedBy;references:Id"`
|
CreatedUser User `gorm:"foreignKey:CreatedBy;references:Id"`
|
||||||
Location Location `gorm:"foreignKey:LocationId;references:Id"`
|
Location Location `gorm:"foreignKey:LocationId;references:Id"`
|
||||||
KandangGroup KandangGroup `gorm:"foreignKey:KandangGroupId;references:Id"`
|
|
||||||
Pic User `gorm:"foreignKey:PicId;references:Id"`
|
Pic User `gorm:"foreignKey:PicId;references:Id"`
|
||||||
Warehouses []Warehouse `gorm:"foreignKey:KandangId;references:Id"`
|
Warehouses []Warehouse `gorm:"foreignKey:KandangId;references:Id"`
|
||||||
ProjectFlockKandangs []ProjectFlockKandang `gorm:"foreignKey:KandangId;references:Id" json:"-"`
|
ProjectFlockKandangs []ProjectFlockKandang `gorm:"foreignKey:KandangId;references:Id" json:"-"`
|
||||||
|
|||||||
@@ -1,24 +0,0 @@
|
|||||||
package entities
|
|
||||||
|
|
||||||
import (
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
|
||||||
|
|
||||||
type KandangGroup struct {
|
|
||||||
Id uint `gorm:"primaryKey"`
|
|
||||||
Name string `gorm:"type:varchar(50);not null;uniqueIndex:kandang_groups_name_unique,where:deleted_at IS NULL"`
|
|
||||||
Status string `gorm:"type:varchar(50);not null"`
|
|
||||||
LocationId uint `gorm:"not null"`
|
|
||||||
PicId uint `gorm:"not null"`
|
|
||||||
CreatedBy uint `gorm:"not null"`
|
|
||||||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
|
||||||
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
|
||||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
|
||||||
|
|
||||||
CreatedUser User `gorm:"foreignKey:CreatedBy;references:Id"`
|
|
||||||
Location Location `gorm:"foreignKey:LocationId;references:Id"`
|
|
||||||
Pic User `gorm:"foreignKey:PicId;references:Id"`
|
|
||||||
Kandangs []Kandang `gorm:"foreignKey:KandangGroupId;references:Id"`
|
|
||||||
}
|
|
||||||
@@ -6,6 +6,7 @@ type ProductWarehouse struct {
|
|||||||
WarehouseId uint `gorm:"column:warehouse_id;not null"`
|
WarehouseId uint `gorm:"column:warehouse_id;not null"`
|
||||||
ProjectFlockKandangId *uint `gorm:"column:project_flock_kandang_id"`
|
ProjectFlockKandangId *uint `gorm:"column:project_flock_kandang_id"`
|
||||||
Quantity float64 `gorm:"column:qty;type:numeric(15,3);default:0"`
|
Quantity float64 `gorm:"column:qty;type:numeric(15,3);default:0"`
|
||||||
|
AvailableQty *float64 `gorm:"-"`
|
||||||
|
|
||||||
// Relations
|
// Relations
|
||||||
Product Product `gorm:"foreignKey:ProductId;references:Id"`
|
Product Product `gorm:"foreignKey:ProductId;references:Id"`
|
||||||
|
|||||||
@@ -45,4 +45,6 @@ type Recording struct {
|
|||||||
StandardFcr *float64 `gorm:"-"`
|
StandardFcr *float64 `gorm:"-"`
|
||||||
PopulationCanChange *bool `gorm:"-"`
|
PopulationCanChange *bool `gorm:"-"`
|
||||||
TransferExecuted *bool `gorm:"-"`
|
TransferExecuted *bool `gorm:"-"`
|
||||||
|
IsTransition *bool `gorm:"-"`
|
||||||
|
IsLaying *bool `gorm:"-"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,30 +36,8 @@ type AuthContext struct {
|
|||||||
func Auth(userService service.UserService, requiredScopes ...string) fiber.Handler {
|
func Auth(userService service.UserService, requiredScopes ...string) fiber.Handler {
|
||||||
return func(c *fiber.Ctx) error {
|
return func(c *fiber.Ctx) error {
|
||||||
token := bearerToken(c)
|
token := bearerToken(c)
|
||||||
tokenSource := ""
|
|
||||||
if token != "" {
|
|
||||||
tokenSource = "header"
|
|
||||||
} else {
|
|
||||||
primaryName := strings.TrimSpace(config.SSOAccessCookieName)
|
|
||||||
if primaryName != "" {
|
|
||||||
token = strings.TrimSpace(c.Cookies(primaryName))
|
|
||||||
if token != "" {
|
|
||||||
tokenSource = "cookie:" + primaryName
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if token == "" {
|
if token == "" {
|
||||||
for _, name := range config.SSOAccessCookieFallback {
|
token = strings.TrimSpace(c.Cookies(config.SSOAccessCookieName))
|
||||||
name = strings.TrimSpace(name)
|
|
||||||
if name == "" || name == primaryName {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
token = strings.TrimSpace(c.Cookies(name))
|
|
||||||
if token != "" {
|
|
||||||
tokenSource = "cookie:" + name
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if token == "" {
|
if token == "" {
|
||||||
return fiber.NewError(fiber.StatusUnauthorized, "Please authenticate")
|
return fiber.NewError(fiber.StatusUnauthorized, "Please authenticate")
|
||||||
@@ -67,11 +45,7 @@ func Auth(userService service.UserService, requiredScopes ...string) fiber.Handl
|
|||||||
|
|
||||||
verification, err := sso.VerifyAccessToken(token)
|
verification, err := sso.VerifyAccessToken(token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if sso.IsSignatureError(err) {
|
|
||||||
logSignatureError("auth", tokenSource, token, err)
|
|
||||||
} else {
|
|
||||||
utils.Log.WithError(err).Warn("auth: token verification failed")
|
utils.Log.WithError(err).Warn("auth: token verification failed")
|
||||||
}
|
|
||||||
return fiber.NewError(fiber.StatusUnauthorized, "Please authenticate")
|
return fiber.NewError(fiber.StatusUnauthorized, "Please authenticate")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -242,26 +216,6 @@ func hasAllScopes(have, required []string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func logSignatureError(ctxLabel, tokenSource, token string, err error) {
|
|
||||||
info := sso.ExtractTokenInfo(token)
|
|
||||||
aud := strings.Join(info.Aud, ",")
|
|
||||||
utils.Log.Errorf(
|
|
||||||
"access token verification failed: %v | ctx=%s source=%s iss=%s kid=%s aud=%s sub=%s exp=%d iat=%d nbf=%d expected_iss=%s expected_aud=%v",
|
|
||||||
err,
|
|
||||||
ctxLabel,
|
|
||||||
tokenSource,
|
|
||||||
info.Iss,
|
|
||||||
info.Kid,
|
|
||||||
aud,
|
|
||||||
info.Sub,
|
|
||||||
info.Exp,
|
|
||||||
info.Iat,
|
|
||||||
info.Nbf,
|
|
||||||
config.SSOIssuer,
|
|
||||||
config.SSOAllowedAudiences,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// RequirePermissions ensures the authenticated user possesses all specified permissions.
|
// RequirePermissions ensures the authenticated user possesses all specified permissions.
|
||||||
func RequirePermissions(perms ...string) fiber.Handler {
|
func RequirePermissions(perms ...string) fiber.Handler {
|
||||||
required := canonicalPermissions(perms)
|
required := canonicalPermissions(perms)
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ const (
|
|||||||
P_AdjustmentGetAll = "lti.inventory.list"
|
P_AdjustmentGetAll = "lti.inventory.list"
|
||||||
P_AdjustmentCreate = "lti.inventory.create"
|
P_AdjustmentCreate = "lti.inventory.create"
|
||||||
P_AdjustmentGetOne = "lti.inventory.detail"
|
P_AdjustmentGetOne = "lti.inventory.detail"
|
||||||
|
P_AdjustmentDeleteOne = "lti.inventory.delete"
|
||||||
)
|
)
|
||||||
const (
|
const (
|
||||||
P_ApprovalGetAll = "lti.approval.list"
|
P_ApprovalGetAll = "lti.approval.list"
|
||||||
@@ -70,6 +71,7 @@ const (
|
|||||||
P_TransferGetAll = "lti.inventory.transfer.list"
|
P_TransferGetAll = "lti.inventory.transfer.list"
|
||||||
P_TransferGetOne = "lti.inventory.transfer.detail"
|
P_TransferGetOne = "lti.inventory.transfer.detail"
|
||||||
P_TransferCreateOne = "lti.inventory.transfer.create"
|
P_TransferCreateOne = "lti.inventory.transfer.create"
|
||||||
|
P_TransferDeleteOne = "lti.inventory.transfer.delete"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -130,8 +132,6 @@ const (
|
|||||||
P_KandangsUpdateOne = "lti.master.kandangs.update"
|
P_KandangsUpdateOne = "lti.master.kandangs.update"
|
||||||
P_KandangsDeleteOne = "lti.master.kandangs.delete"
|
P_KandangsDeleteOne = "lti.master.kandangs.delete"
|
||||||
|
|
||||||
P_KandangGroups = "lti.daily_checklist.master_data.kandang"
|
|
||||||
|
|
||||||
P_LocationsGetAll = "lti.master.locations.list"
|
P_LocationsGetAll = "lti.master.locations.list"
|
||||||
P_LocationsGetOne = "lti.master.locations.detail"
|
P_LocationsGetOne = "lti.master.locations.detail"
|
||||||
P_LocationsCreateOne = "lti.master.locations.create"
|
P_LocationsCreateOne = "lti.master.locations.create"
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package dto
|
package dto
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -69,7 +71,7 @@ func ToOverheadDTO(budget *entity.ProjectBudget, realization *entity.ExpenseReal
|
|||||||
return dto
|
return dto
|
||||||
}
|
}
|
||||||
|
|
||||||
func ToOverheadListDTOs(budgets []entity.ProjectBudget, realizations []entity.ExpenseRealization, totalChickinQty, totalActualPopulation float64, isPerKandang bool, totalKandangCount int) OverheadListDTO {
|
func ToOverheadListDTOs(budgets []entity.ProjectBudget, realizations []entity.ExpenseRealization, totalChickinQty, totalActualPopulation float64, isPerKandang bool, totalKandangCount int, projectFlockKandangCountMap map[uint]int) OverheadListDTO {
|
||||||
overheadsByNonstockID := make(map[uint]*OverheadDTO)
|
overheadsByNonstockID := make(map[uint]*OverheadDTO)
|
||||||
latestDateByNonstockID := make(map[uint]string)
|
latestDateByNonstockID := make(map[uint]string)
|
||||||
|
|
||||||
@@ -111,6 +113,35 @@ func ToOverheadListDTOs(budgets []entity.ProjectBudget, realizations []entity.Ex
|
|||||||
qty := realizations[i].Qty
|
qty := realizations[i].Qty
|
||||||
totalAmount := calculateTotal(realizations[i].Qty, realizations[i].Price)
|
totalAmount := calculateTotal(realizations[i].Qty, realizations[i].Price)
|
||||||
|
|
||||||
|
// Farm-level expense division
|
||||||
|
if realizations[i].ExpenseNonstock.Expense != nil &&
|
||||||
|
realizations[i].ExpenseNonstock.Expense.ProjectFlockId != nil {
|
||||||
|
projectFlockIDs := parseProjectFlockIDsFromJSON(*realizations[i].ExpenseNonstock.Expense.ProjectFlockId)
|
||||||
|
|
||||||
|
if len(projectFlockIDs) > 0 {
|
||||||
|
totalKandangInAllProjects := 0
|
||||||
|
for _, pfID := range projectFlockIDs {
|
||||||
|
if count, exists := projectFlockKandangCountMap[pfID]; exists {
|
||||||
|
totalKandangInAllProjects += count
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if totalKandangInAllProjects > 0 {
|
||||||
|
if isPerKandang {
|
||||||
|
qty = qty / float64(totalKandangInAllProjects)
|
||||||
|
totalAmount = totalAmount / float64(totalKandangInAllProjects)
|
||||||
|
} else {
|
||||||
|
// Overhead ALL: divide by total kandang then multiply by this project's kandang count
|
||||||
|
perKandangAmount := totalAmount / float64(totalKandangInAllProjects)
|
||||||
|
perKandangQty := qty / float64(totalKandangInAllProjects)
|
||||||
|
|
||||||
|
qty = perKandangQty * float64(totalKandangCount)
|
||||||
|
totalAmount = perKandangAmount * float64(totalKandangCount)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
overheadsByNonstockID[nonstockID].ActualQuantity += qty
|
overheadsByNonstockID[nonstockID].ActualQuantity += qty
|
||||||
overheadsByNonstockID[nonstockID].ActualTotalAmount += totalAmount
|
overheadsByNonstockID[nonstockID].ActualTotalAmount += totalAmount
|
||||||
|
|
||||||
@@ -160,6 +191,27 @@ func ToOverheadListDTOs(budgets []entity.ProjectBudget, realizations []entity.Ex
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseProjectFlockIDsFromJSON(projectFlockJSON string) []uint {
|
||||||
|
if projectFlockJSON == "" {
|
||||||
|
return []uint{}
|
||||||
|
}
|
||||||
|
|
||||||
|
var projectFlocks []uint
|
||||||
|
if err := json.Unmarshal([]byte(projectFlockJSON), &projectFlocks); err != nil {
|
||||||
|
return []uint{}
|
||||||
|
}
|
||||||
|
|
||||||
|
return projectFlocks
|
||||||
|
}
|
||||||
|
|
||||||
|
func countProjectFlocksInJSON(projectFlockJSON string) int {
|
||||||
|
projectFlocks := parseProjectFlockIDsFromJSON(projectFlockJSON)
|
||||||
|
if len(projectFlocks) == 0 {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return len(projectFlocks)
|
||||||
|
}
|
||||||
|
|
||||||
func getItemInfo(nonstock *entity.Nonstock) (string, string) {
|
func getItemInfo(nonstock *entity.Nonstock) (string, string) {
|
||||||
if nonstock != nil && nonstock.Id != 0 {
|
if nonstock != nil && nonstock.Id != 0 {
|
||||||
return nonstock.Name, nonstock.Uom.Name
|
return nonstock.Name, nonstock.Uom.Name
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ type SapronakCategoryRowDTO struct {
|
|||||||
QtyOut float64 `json:"qty_out"`
|
QtyOut float64 `json:"qty_out"`
|
||||||
QtyUsed float64 `json:"qty_used"`
|
QtyUsed float64 `json:"qty_used"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
ProductCategory string `json:"product_category"`
|
ProductCategory []string `json:"product_category"`
|
||||||
UnitPrice float64 `json:"unit_price"`
|
UnitPrice float64 `json:"unit_price"`
|
||||||
TotalAmount float64 `json:"total_amount"`
|
TotalAmount float64 `json:"total_amount"`
|
||||||
Notes string `json:"notes"`
|
Notes string `json:"notes"`
|
||||||
@@ -183,13 +183,13 @@ func ToSapronakProjectAggregatedFromReport(report *SapronakReportDTO, flag strin
|
|||||||
"PULLET": 0,
|
"PULLET": 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
buildFlagList := func(productID uint, fallback string) string {
|
buildFlagList := func(productID uint, fallback string) []string {
|
||||||
rawFlags := productFlags[productID]
|
rawFlags := productFlags[productID]
|
||||||
if len(rawFlags) == 0 {
|
if len(rawFlags) == 0 {
|
||||||
if fallback == "" {
|
if fallback == "" {
|
||||||
return ""
|
return []string{}
|
||||||
}
|
}
|
||||||
return fallback
|
return []string{fallback}
|
||||||
}
|
}
|
||||||
seen := make(map[string]struct{}, len(rawFlags))
|
seen := make(map[string]struct{}, len(rawFlags))
|
||||||
ordered := make([]string, 0, len(rawFlags))
|
ordered := make([]string, 0, len(rawFlags))
|
||||||
@@ -220,7 +220,7 @@ func ToSapronakProjectAggregatedFromReport(report *SapronakReportDTO, flag strin
|
|||||||
}
|
}
|
||||||
return li < lj
|
return li < lj
|
||||||
})
|
})
|
||||||
return strings.Join(ordered, " ")
|
return ordered
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, group := range report.Groups {
|
for _, group := range report.Groups {
|
||||||
@@ -317,27 +317,6 @@ func ToSapronakProjectAggregatedFromReport(report *SapronakReportDTO, flag strin
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// For chicken categories, keep qty_used aligned with qty_in - qty_out.
|
|
||||||
// Sales are excluded; usage represents remaining after transfers.
|
|
||||||
adjustChicken := func(cat *SapronakCategoryDTO) {
|
|
||||||
if cat == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for i := range cat.Rows {
|
|
||||||
row := &cat.Rows[i]
|
|
||||||
remaining := row.QtyIn - row.QtyOut
|
|
||||||
if remaining < 0 {
|
|
||||||
remaining = 0
|
|
||||||
}
|
|
||||||
row.QtyUsed = remaining
|
|
||||||
if row.UnitPrice > 0 {
|
|
||||||
row.TotalAmount = row.QtyUsed * row.UnitPrice
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
adjustChicken(result.Doc)
|
|
||||||
adjustChicken(result.Pullet)
|
|
||||||
|
|
||||||
buildTotals := func(cat *SapronakCategoryDTO, label string) {
|
buildTotals := func(cat *SapronakCategoryDTO, label string) {
|
||||||
if cat == nil {
|
if cat == nil {
|
||||||
return
|
return
|
||||||
@@ -366,22 +345,5 @@ func ToSapronakProjectAggregatedFromReport(report *SapronakReportDTO, flag strin
|
|||||||
buildTotals(result.Doc, "TOTAL DOC")
|
buildTotals(result.Doc, "TOTAL DOC")
|
||||||
buildTotals(result.Ovk, "TOTAL OVK")
|
buildTotals(result.Ovk, "TOTAL OVK")
|
||||||
buildTotals(result.Pakan, "TOTAL PAKAN")
|
buildTotals(result.Pakan, "TOTAL PAKAN")
|
||||||
|
|
||||||
// For chicken categories, enforce total qty_used = qty_in - qty_out.
|
|
||||||
adjustChickenTotal := func(cat *SapronakCategoryDTO) {
|
|
||||||
if cat == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
remaining := cat.Total.QtyIn - cat.Total.QtyOut
|
|
||||||
if remaining < 0 {
|
|
||||||
remaining = 0
|
|
||||||
}
|
|
||||||
cat.Total.QtyUsed = remaining
|
|
||||||
if cat.Total.AvgUnitPrice > 0 {
|
|
||||||
cat.Total.TotalAmount = cat.Total.AvgUnitPrice * remaining
|
|
||||||
}
|
|
||||||
}
|
|
||||||
adjustChickenTotal(result.Doc)
|
|
||||||
adjustChickenTotal(result.Pullet)
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,17 +25,17 @@ type ClosingRepository interface {
|
|||||||
SumMarketingWeightAndQtyByProjectFlockKandangIDsAndFlagNames(ctx context.Context, projectFlockKandangIDs []uint, flagNames []string) (float64, float64, float64, error)
|
SumMarketingWeightAndQtyByProjectFlockKandangIDsAndFlagNames(ctx context.Context, projectFlockKandangIDs []uint, flagNames []string) (float64, float64, float64, error)
|
||||||
SumRecordingEggQtyByProjectFlockKandangIDsAndFlagNames(ctx context.Context, projectFlockKandangIDs []uint, flagNames []string) (float64, error)
|
SumRecordingEggQtyByProjectFlockKandangIDsAndFlagNames(ctx context.Context, projectFlockKandangIDs []uint, flagNames []string) (float64, error)
|
||||||
GetExpeditionHPP(ctx context.Context, projectFlockID uint, projectFlockKandangID *uint) ([]ExpeditionHPPRow, error)
|
GetExpeditionHPP(ctx context.Context, projectFlockID uint, projectFlockKandangID *uint) ([]ExpeditionHPPRow, error)
|
||||||
FetchSapronakIncoming(ctx context.Context, kandangID uint, start, end *time.Time) ([]SapronakIncomingRow, error)
|
FetchSapronakIncoming(ctx context.Context, kandangID uint) ([]SapronakIncomingRow, error)
|
||||||
FetchSapronakIncomingDetails(ctx context.Context, kandangID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, error)
|
FetchSapronakIncomingDetails(ctx context.Context, kandangID uint) (map[uint][]SapronakDetailRow, error)
|
||||||
FetchSapronakUsage(ctx context.Context, pfkID uint, start, end *time.Time) ([]SapronakUsageRow, error)
|
FetchSapronakUsage(ctx context.Context, pfkID uint) ([]SapronakUsageRow, error)
|
||||||
FetchSapronakUsageDetails(ctx context.Context, pfkID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, error)
|
FetchSapronakUsageDetails(ctx context.Context, pfkID uint) (map[uint][]SapronakDetailRow, error)
|
||||||
FetchSapronakChickinUsage(ctx context.Context, pfkID uint, start, end *time.Time) ([]SapronakUsageRow, error)
|
FetchSapronakChickinUsage(ctx context.Context, pfkID uint) ([]SapronakUsageRow, error)
|
||||||
FetchSapronakChickinUsageDetails(ctx context.Context, pfkID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, error)
|
FetchSapronakChickinUsageDetails(ctx context.Context, pfkID uint) (map[uint][]SapronakDetailRow, error)
|
||||||
FetchSapronakUsageAllocatedDetails(ctx context.Context, projectFlockKandangID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, error)
|
FetchSapronakUsageAllocatedDetails(ctx context.Context, projectFlockKandangID uint) (map[uint][]SapronakDetailRow, error)
|
||||||
FetchSapronakAdjustments(ctx context.Context, kandangID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, map[uint][]SapronakDetailRow, error)
|
FetchSapronakAdjustments(ctx context.Context, kandangID uint) (map[uint][]SapronakDetailRow, map[uint][]SapronakDetailRow, error)
|
||||||
FetchSapronakTransfers(ctx context.Context, kandangID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, map[uint][]SapronakDetailRow, error)
|
FetchSapronakTransfers(ctx context.Context, kandangID uint) (map[uint][]SapronakDetailRow, map[uint][]SapronakDetailRow, error)
|
||||||
FetchSapronakSales(ctx context.Context, projectFlockKandangID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, error)
|
FetchSapronakSales(ctx context.Context, projectFlockKandangID uint) (map[uint][]SapronakDetailRow, error)
|
||||||
FetchSapronakSalesAllocatedDetails(ctx context.Context, projectFlockKandangID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, error)
|
FetchSapronakSalesAllocatedDetails(ctx context.Context, projectFlockKandangID uint) (map[uint][]SapronakDetailRow, error)
|
||||||
GetProductsWithFlagsByIDs(ctx context.Context, productIDs []uint) ([]entity.Product, error)
|
GetProductsWithFlagsByIDs(ctx context.Context, productIDs []uint) ([]entity.Product, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,8 +86,6 @@ type SapronakQueryParams struct {
|
|||||||
Limit int
|
Limit int
|
||||||
Offset int
|
Offset int
|
||||||
Search string
|
Search string
|
||||||
StartDate *time.Time
|
|
||||||
EndDate *time.Time
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *ClosingRepositoryImpl) GetSapronak(ctx context.Context, params SapronakQueryParams) ([]SapronakRow, int64, error) {
|
func (r *ClosingRepositoryImpl) GetSapronak(ctx context.Context, params SapronakQueryParams) ([]SapronakRow, int64, error) {
|
||||||
@@ -144,33 +142,15 @@ func (r *ClosingRepositoryImpl) GetSapronak(ctx context.Context, params Sapronak
|
|||||||
}
|
}
|
||||||
|
|
||||||
var totalResults int64
|
var totalResults int64
|
||||||
dateClause := ""
|
countSQL := fmt.Sprintf("SELECT COUNT(*) FROM (%s) AS combined%s", unionSQL, searchClause)
|
||||||
var dateArgs []any
|
countArgs := append(append([]any{}, args...), searchArgs...)
|
||||||
if params.StartDate != nil {
|
|
||||||
dateClause += " AND sort_date::date >= ?"
|
|
||||||
dateArgs = append(dateArgs, params.StartDate)
|
|
||||||
}
|
|
||||||
if params.EndDate != nil {
|
|
||||||
dateClause += " AND sort_date::date <= ?"
|
|
||||||
dateArgs = append(dateArgs, params.EndDate)
|
|
||||||
}
|
|
||||||
whereClause := searchClause
|
|
||||||
if dateClause != "" {
|
|
||||||
if whereClause == "" {
|
|
||||||
whereClause = " WHERE " + strings.TrimPrefix(dateClause, " AND ")
|
|
||||||
} else {
|
|
||||||
whereClause += dateClause
|
|
||||||
}
|
|
||||||
}
|
|
||||||
countSQL := fmt.Sprintf("SELECT COUNT(*) FROM (%s) AS combined%s", unionSQL, whereClause)
|
|
||||||
countArgs := append(append(append([]any{}, args...), searchArgs...), dateArgs...)
|
|
||||||
if err := db.Raw(countSQL, countArgs...).Scan(&totalResults).Error; err != nil {
|
if err := db.Raw(countSQL, countArgs...).Scan(&totalResults).Error; err != nil {
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
dataArgs := append(append(append([]any{}, args...), searchArgs...), dateArgs...)
|
dataArgs := append(append([]any{}, args...), searchArgs...)
|
||||||
dataArgs = append(dataArgs, params.Limit, params.Offset)
|
dataArgs = append(dataArgs, params.Limit, params.Offset)
|
||||||
dataSQL := fmt.Sprintf("SELECT * FROM (%s) AS combined%s ORDER BY sort_date ASC, id ASC LIMIT ? OFFSET ?", unionSQL, whereClause)
|
dataSQL := fmt.Sprintf("SELECT * FROM (%s) AS combined%s ORDER BY sort_date ASC, id ASC LIMIT ? OFFSET ?", unionSQL, searchClause)
|
||||||
|
|
||||||
var rows []SapronakRow
|
var rows []SapronakRow
|
||||||
if err := db.Raw(dataSQL, dataArgs...).Scan(&rows).Error; err != nil {
|
if err := db.Raw(dataSQL, dataArgs...).Scan(&rows).Error; err != nil {
|
||||||
@@ -233,25 +213,6 @@ func (r *ClosingRepositoryImpl) GetSapronakSummary(ctx context.Context, params S
|
|||||||
searchArgs = append(searchArgs, like, like, like, like, like, like, like, like, like)
|
searchArgs = append(searchArgs, like, like, like, like, like, like, like, like, like)
|
||||||
}
|
}
|
||||||
|
|
||||||
dateClause := ""
|
|
||||||
var dateArgs []any
|
|
||||||
if params.StartDate != nil {
|
|
||||||
dateClause += " AND sort_date::date >= ?"
|
|
||||||
dateArgs = append(dateArgs, params.StartDate)
|
|
||||||
}
|
|
||||||
if params.EndDate != nil {
|
|
||||||
dateClause += " AND sort_date::date <= ?"
|
|
||||||
dateArgs = append(dateArgs, params.EndDate)
|
|
||||||
}
|
|
||||||
whereClause := searchClause
|
|
||||||
if dateClause != "" {
|
|
||||||
if whereClause == "" {
|
|
||||||
whereClause = " WHERE " + strings.TrimPrefix(dateClause, " AND ")
|
|
||||||
} else {
|
|
||||||
whereClause += dateClause
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
querySQL := fmt.Sprintf(`
|
querySQL := fmt.Sprintf(`
|
||||||
SELECT
|
SELECT
|
||||||
product_category AS category,
|
product_category AS category,
|
||||||
@@ -261,8 +222,8 @@ SELECT
|
|||||||
FROM (%s) AS combined%s
|
FROM (%s) AS combined%s
|
||||||
GROUP BY product_category, unit_id, unit
|
GROUP BY product_category, unit_id, unit
|
||||||
ORDER BY product_category ASC, unit ASC
|
ORDER BY product_category ASC, unit ASC
|
||||||
`, unionSQL, whereClause)
|
`, unionSQL, searchClause)
|
||||||
queryArgs := append(append(append([]any{}, args...), searchArgs...), dateArgs...)
|
queryArgs := append(append([]any{}, args...), searchArgs...)
|
||||||
|
|
||||||
var rows []SapronakSummaryRow
|
var rows []SapronakSummaryRow
|
||||||
if err := db.Raw(querySQL, queryArgs...).Scan(&rows).Error; err != nil {
|
if err := db.Raw(querySQL, queryArgs...).Scan(&rows).Error; err != nil {
|
||||||
@@ -817,16 +778,6 @@ type SapronakDetailRow struct {
|
|||||||
|
|
||||||
func (r *ClosingRepositoryImpl) withCtx(ctx context.Context) *gorm.DB { return r.DB().WithContext(ctx) }
|
func (r *ClosingRepositoryImpl) withCtx(ctx context.Context) *gorm.DB { return r.DB().WithContext(ctx) }
|
||||||
|
|
||||||
func applyDateRange(db *gorm.DB, column string, start, end *time.Time) *gorm.DB {
|
|
||||||
if start != nil {
|
|
||||||
db = db.Where(column+"::date >= ?", start)
|
|
||||||
}
|
|
||||||
if end != nil {
|
|
||||||
db = db.Where(column+"::date <= ?", end)
|
|
||||||
}
|
|
||||||
return db
|
|
||||||
}
|
|
||||||
|
|
||||||
func applyJoins(db *gorm.DB, joins ...string) *gorm.DB {
|
func applyJoins(db *gorm.DB, joins ...string) *gorm.DB {
|
||||||
for _, j := range joins {
|
for _, j := range joins {
|
||||||
if strings.TrimSpace(j) != "" {
|
if strings.TrimSpace(j) != "" {
|
||||||
@@ -927,14 +878,6 @@ func (r *ClosingRepositoryImpl) fetchSapronakUsage(
|
|||||||
return rows, nil
|
return rows, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func scanUsage(db *gorm.DB) ([]SapronakUsageRow, error) {
|
|
||||||
rows := make([]SapronakUsageRow, 0)
|
|
||||||
if err := db.Group("pw.product_id, p.name, f.name, p.product_price").Scan(&rows).Error; err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return rows, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *ClosingRepositoryImpl) detailQuery(
|
func (r *ClosingRepositoryImpl) detailQuery(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
table string,
|
table string,
|
||||||
@@ -966,11 +909,11 @@ func (r *ClosingRepositoryImpl) fetchSapronakDetails(
|
|||||||
return scanAndGroupDetails(r.detailQuery(ctx, table, pwJoinCond, joins, selectSQL, where, args...))
|
return scanAndGroupDetails(r.detailQuery(ctx, table, pwJoinCond, joins, selectSQL, where, args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *ClosingRepositoryImpl) FetchSapronakUsage(ctx context.Context, pfkID uint, start, end *time.Time) ([]SapronakUsageRow, error) {
|
func (r *ClosingRepositoryImpl) FetchSapronakUsage(ctx context.Context, pfkID uint) ([]SapronakUsageRow, error) {
|
||||||
if pfkID == 0 {
|
if pfkID == 0 {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
db := r.usageQuery(
|
return r.fetchSapronakUsage(
|
||||||
ctx,
|
ctx,
|
||||||
"recording_stocks rs",
|
"recording_stocks rs",
|
||||||
"pw.id = rs.product_warehouse_id",
|
"pw.id = rs.product_warehouse_id",
|
||||||
@@ -979,15 +922,13 @@ func (r *ClosingRepositoryImpl) FetchSapronakUsage(ctx context.Context, pfkID ui
|
|||||||
pfkID,
|
pfkID,
|
||||||
sapronakFlagsUsage,
|
sapronakFlagsUsage,
|
||||||
)
|
)
|
||||||
db = applyDateRange(db, "r.record_datetime", start, end)
|
|
||||||
return scanUsage(db)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *ClosingRepositoryImpl) FetchSapronakChickinUsage(ctx context.Context, pfkID uint, start, end *time.Time) ([]SapronakUsageRow, error) {
|
func (r *ClosingRepositoryImpl) FetchSapronakChickinUsage(ctx context.Context, pfkID uint) ([]SapronakUsageRow, error) {
|
||||||
if pfkID == 0 {
|
if pfkID == 0 {
|
||||||
return []SapronakUsageRow{}, nil
|
return []SapronakUsageRow{}, nil
|
||||||
}
|
}
|
||||||
db := r.usageQuery(
|
return r.fetchSapronakUsage(
|
||||||
ctx,
|
ctx,
|
||||||
"project_chickins pc",
|
"project_chickins pc",
|
||||||
"pw.id = pc.product_warehouse_id",
|
"pw.id = pc.product_warehouse_id",
|
||||||
@@ -996,12 +937,10 @@ func (r *ClosingRepositoryImpl) FetchSapronakChickinUsage(ctx context.Context, p
|
|||||||
pfkID,
|
pfkID,
|
||||||
sapronakFlagsChickin,
|
sapronakFlagsChickin,
|
||||||
)
|
)
|
||||||
db = applyDateRange(db, "pc.chick_in_date", start, end)
|
|
||||||
return scanUsage(db)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *ClosingRepositoryImpl) FetchSapronakUsageDetails(ctx context.Context, pfkID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, error) {
|
func (r *ClosingRepositoryImpl) FetchSapronakUsageDetails(ctx context.Context, pfkID uint) (map[uint][]SapronakDetailRow, error) {
|
||||||
db := r.detailQuery(
|
return r.fetchSapronakDetails(
|
||||||
ctx,
|
ctx,
|
||||||
"recording_stocks rs",
|
"recording_stocks rs",
|
||||||
"pw.id = rs.product_warehouse_id",
|
"pw.id = rs.product_warehouse_id",
|
||||||
@@ -1020,12 +959,10 @@ func (r *ClosingRepositoryImpl) FetchSapronakUsageDetails(ctx context.Context, p
|
|||||||
pfkID,
|
pfkID,
|
||||||
sapronakFlagsUsage,
|
sapronakFlagsUsage,
|
||||||
)
|
)
|
||||||
db = applyDateRange(db, "r.record_datetime", start, end)
|
|
||||||
return scanAndGroupDetails(db)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *ClosingRepositoryImpl) FetchSapronakChickinUsageDetails(ctx context.Context, pfkID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, error) {
|
func (r *ClosingRepositoryImpl) FetchSapronakChickinUsageDetails(ctx context.Context, pfkID uint) (map[uint][]SapronakDetailRow, error) {
|
||||||
db := r.detailQuery(
|
return r.fetchSapronakDetails(
|
||||||
ctx,
|
ctx,
|
||||||
"project_chickins pc",
|
"project_chickins pc",
|
||||||
"pw.id = pc.product_warehouse_id",
|
"pw.id = pc.product_warehouse_id",
|
||||||
@@ -1044,16 +981,13 @@ func (r *ClosingRepositoryImpl) FetchSapronakChickinUsageDetails(ctx context.Con
|
|||||||
pfkID,
|
pfkID,
|
||||||
sapronakFlagsChickin,
|
sapronakFlagsChickin,
|
||||||
)
|
)
|
||||||
db = applyDateRange(db, "pc.chick_in_date", start, end)
|
|
||||||
return scanAndGroupDetails(db)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *ClosingRepositoryImpl) FetchSapronakUsageAllocatedDetails(ctx context.Context, projectFlockKandangID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, error) {
|
func (r *ClosingRepositoryImpl) FetchSapronakUsageAllocatedDetails(ctx context.Context, projectFlockKandangID uint) (map[uint][]SapronakDetailRow, error) {
|
||||||
if projectFlockKandangID == 0 {
|
if projectFlockKandangID == 0 {
|
||||||
return map[uint][]SapronakDetailRow{}, nil
|
return map[uint][]SapronakDetailRow{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
dateExpr := "COALESCE(pi.received_date, st.transfer_date, lt.transfer_date, ast.created_at, pc.chick_in_date, r.record_datetime)"
|
|
||||||
query := r.withCtx(ctx).
|
query := r.withCtx(ctx).
|
||||||
Table("stock_allocations AS sa").
|
Table("stock_allocations AS sa").
|
||||||
Select(`
|
Select(`
|
||||||
@@ -1095,19 +1029,18 @@ func (r *ClosingRepositoryImpl) FetchSapronakUsageAllocatedDetails(ctx context.C
|
|||||||
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 product_warehouses pw_pc ON pw_pc.id = pc.product_warehouse_id").
|
Joins("LEFT JOIN products p_resolve ON p_resolve.id = COALESCE(pi.product_id, pw_ltt.product_id, pw.product_id)").
|
||||||
Joins("LEFT JOIN products p_resolve ON p_resolve.id = COALESCE(pi.product_id, pw_ltt.product_id, pw_pc.product_id, pw.product_id)").
|
|
||||||
Where("sa.status = ?", entity.StockAllocationStatusActive).
|
Where("sa.status = ?", entity.StockAllocationStatusActive).
|
||||||
Where("sa.allocation_purpose = ?", entity.StockAllocationPurposeConsume).
|
Where("sa.allocation_purpose = ?", entity.StockAllocationPurposeConsume).
|
||||||
Where("sa.stockable_type <> ?", fifo.StockableKeyProjectFlockPopulation.String()).
|
Where("sa.stockable_type <> ?", fifo.StockableKeyProjectFlockPopulation.String()).
|
||||||
Where("f.name IN ?", sapronakFlagsAll).
|
Where("f.name IN ?", sapronakFlagsAll).
|
||||||
Where(`
|
Where(`
|
||||||
(sa.usable_type = ? AND r.project_flock_kandangs_id = ? AND f.name IN ?)
|
(sa.usable_type = ? AND r.project_flock_kandangs_id = ?)
|
||||||
OR
|
OR
|
||||||
(sa.usable_type = ? AND pc_used.project_flock_kandang_id = ? AND f.name IN ?)
|
(sa.usable_type = ? AND pc_used.project_flock_kandang_id = ?)
|
||||||
`,
|
`,
|
||||||
fifo.UsableKeyRecordingStock.String(), projectFlockKandangID, sapronakFlagsUsage,
|
fifo.UsableKeyRecordingStock.String(), projectFlockKandangID,
|
||||||
fifo.UsableKeyProjectChickin.String(), projectFlockKandangID, sapronakFlagsChickin,
|
fifo.UsableKeyProjectChickin.String(), projectFlockKandangID,
|
||||||
)
|
)
|
||||||
query = r.joinSapronakProductFlag(query, "p_resolve").
|
query = r.joinSapronakProductFlag(query, "p_resolve").
|
||||||
Group(`
|
Group(`
|
||||||
@@ -1116,12 +1049,11 @@ func (r *ClosingRepositoryImpl) FetchSapronakUsageAllocatedDetails(ctx context.C
|
|||||||
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_resolve.product_price
|
||||||
`)
|
`)
|
||||||
query = applyDateRange(query, dateExpr, start, end)
|
|
||||||
|
|
||||||
return scanAndGroupDetails(query)
|
return scanAndGroupDetails(query)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *ClosingRepositoryImpl) incomingPurchaseBase(ctx context.Context, kandangID uint, start, end *time.Time) *gorm.DB {
|
func (r *ClosingRepositoryImpl) incomingPurchaseBase(ctx context.Context, kandangID uint) *gorm.DB {
|
||||||
db := r.withCtx(ctx).
|
db := r.withCtx(ctx).
|
||||||
Table("purchase_items AS pi").
|
Table("purchase_items AS pi").
|
||||||
Joins("JOIN purchases po ON po.id = pi.purchase_id AND po.deleted_at IS NULL").
|
Joins("JOIN purchases po ON po.id = pi.purchase_id AND po.deleted_at IS NULL").
|
||||||
@@ -1130,13 +1062,12 @@ func (r *ClosingRepositoryImpl) incomingPurchaseBase(ctx context.Context, kandan
|
|||||||
Where("w.kandang_id = ?", kandangID).
|
Where("w.kandang_id = ?", kandangID).
|
||||||
Where("f.name IN ?", sapronakFlagsAll).
|
Where("f.name IN ?", sapronakFlagsAll).
|
||||||
Where("pi.received_date IS NOT NULL")
|
Where("pi.received_date IS NOT NULL")
|
||||||
db = applyDateRange(db, "pi.received_date", start, end)
|
|
||||||
return r.joinSapronakProductFlag(db, "p")
|
return r.joinSapronakProductFlag(db, "p")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *ClosingRepositoryImpl) FetchSapronakIncoming(ctx context.Context, kandangID uint, start, end *time.Time) ([]SapronakIncomingRow, error) {
|
func (r *ClosingRepositoryImpl) FetchSapronakIncoming(ctx context.Context, kandangID uint) ([]SapronakIncomingRow, error) {
|
||||||
rows := make([]SapronakIncomingRow, 0)
|
rows := make([]SapronakIncomingRow, 0)
|
||||||
db := r.incomingPurchaseBase(ctx, kandangID, start, end).Select(`
|
db := r.incomingPurchaseBase(ctx, kandangID).Select(`
|
||||||
pi.product_id AS product_id,
|
pi.product_id AS product_id,
|
||||||
p.name AS product_name,
|
p.name AS product_name,
|
||||||
f.name AS flag,
|
f.name AS flag,
|
||||||
@@ -1150,9 +1081,9 @@ func (r *ClosingRepositoryImpl) FetchSapronakIncoming(ctx context.Context, kanda
|
|||||||
return rows, nil
|
return rows, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *ClosingRepositoryImpl) FetchSapronakIncomingDetails(ctx context.Context, kandangID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, error) {
|
func (r *ClosingRepositoryImpl) FetchSapronakIncomingDetails(ctx context.Context, kandangID uint) (map[uint][]SapronakDetailRow, error) {
|
||||||
return scanAndGroupDetails(
|
return scanAndGroupDetails(
|
||||||
r.incomingPurchaseBase(ctx, kandangID, start, end).Select(`
|
r.incomingPurchaseBase(ctx, kandangID).Select(`
|
||||||
pi.product_id AS product_id,
|
pi.product_id AS product_id,
|
||||||
p.name AS product_name,
|
p.name AS product_name,
|
||||||
f.name AS flag,
|
f.name AS flag,
|
||||||
@@ -1247,7 +1178,7 @@ func splitStockLogs(rows []stockLogSapronakRow, refFn func(stockLogSapronakRow)
|
|||||||
return in, out
|
return in, out
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *ClosingRepositoryImpl) FetchSapronakAdjustments(ctx context.Context, kandangID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, map[uint][]SapronakDetailRow, error) {
|
func (r *ClosingRepositoryImpl) FetchSapronakAdjustments(ctx context.Context, kandangID uint) (map[uint][]SapronakDetailRow, map[uint][]SapronakDetailRow, error) {
|
||||||
poByWarehouse := r.DB().
|
poByWarehouse := r.DB().
|
||||||
Table("purchase_items pi").
|
Table("purchase_items pi").
|
||||||
Select("DISTINCT ON (pi.product_warehouse_id) pi.product_warehouse_id, po.po_number, pi.received_date").
|
Select("DISTINCT ON (pi.product_warehouse_id) pi.product_warehouse_id, po.po_number, pi.received_date").
|
||||||
@@ -1274,13 +1205,11 @@ func (r *ClosingRepositoryImpl) FetchSapronakAdjustments(ctx context.Context, ka
|
|||||||
Where("f.name IN ?", sapronakFlagsAll).
|
Where("f.name IN ?", sapronakFlagsAll).
|
||||||
Where("COALESCE(ast.total_qty, 0) > 0")
|
Where("COALESCE(ast.total_qty, 0) > 0")
|
||||||
incomingQuery = r.joinSapronakProductFlag(incomingQuery, "p")
|
incomingQuery = r.joinSapronakProductFlag(incomingQuery, "p")
|
||||||
incomingQuery = applyDateRange(incomingQuery, "ast.created_at", start, end)
|
|
||||||
incoming, err := scanAndGroupDetails(incomingQuery)
|
incoming, err := scanAndGroupDetails(incomingQuery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
dateExpr := "COALESCE(pi.received_date, st.transfer_date, lt.transfer_date, pfp_po.received_date, pc.chick_in_date, ast_in.created_at, ast.created_at)"
|
|
||||||
outgoingQuery := r.withCtx(ctx).
|
outgoingQuery := r.withCtx(ctx).
|
||||||
Table("stock_allocations AS sa").
|
Table("stock_allocations AS sa").
|
||||||
Select(`
|
Select(`
|
||||||
@@ -1314,7 +1243,6 @@ func (r *ClosingRepositoryImpl) FetchSapronakAdjustments(ctx context.Context, ka
|
|||||||
Where("f.name NOT IN ?", sapronakFlags(utils.FlagDOC, utils.FlagPullet)).
|
Where("f.name NOT IN ?", sapronakFlags(utils.FlagDOC, utils.FlagPullet)).
|
||||||
Group("pw.product_id, p.name, f.name, pi.received_date, st.transfer_date, lt.transfer_date, pfp_po.received_date, pc.chick_in_date, ast_in.created_at, ast.created_at, po.po_number, st.movement_number, lt.transfer_number, pfp_po.po_number, pc.id, ast_in.id, ast.id, p.product_price")
|
Group("pw.product_id, p.name, f.name, pi.received_date, st.transfer_date, lt.transfer_date, pfp_po.received_date, pc.chick_in_date, ast_in.created_at, ast.created_at, po.po_number, st.movement_number, lt.transfer_number, pfp_po.po_number, pc.id, ast_in.id, ast.id, p.product_price")
|
||||||
outgoingQuery = r.joinSapronakProductFlag(outgoingQuery, "p")
|
outgoingQuery = r.joinSapronakProductFlag(outgoingQuery, "p")
|
||||||
outgoingQuery = applyDateRange(outgoingQuery, dateExpr, start, end)
|
|
||||||
outgoing, err := scanAndGroupDetails(outgoingQuery)
|
outgoing, err := scanAndGroupDetails(outgoingQuery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
@@ -1323,7 +1251,7 @@ func (r *ClosingRepositoryImpl) FetchSapronakAdjustments(ctx context.Context, ka
|
|||||||
return incoming, outgoing, nil
|
return incoming, outgoing, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *ClosingRepositoryImpl) FetchSapronakTransfers(ctx context.Context, kandangID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, map[uint][]SapronakDetailRow, error) {
|
func (r *ClosingRepositoryImpl) FetchSapronakTransfers(ctx context.Context, kandangID uint) (map[uint][]SapronakDetailRow, map[uint][]SapronakDetailRow, error) {
|
||||||
incomingQuery := r.withCtx(ctx).
|
incomingQuery := r.withCtx(ctx).
|
||||||
Table("stock_transfer_details AS std").
|
Table("stock_transfer_details AS std").
|
||||||
Select(`
|
Select(`
|
||||||
@@ -1345,7 +1273,6 @@ func (r *ClosingRepositoryImpl) FetchSapronakTransfers(ctx context.Context, kand
|
|||||||
Where("(fw.kandang_id IS NULL OR fw.kandang_id <> w.kandang_id)").
|
Where("(fw.kandang_id IS NULL OR fw.kandang_id <> w.kandang_id)").
|
||||||
Where("f.name IN ?", sapronakFlagsAll)
|
Where("f.name IN ?", sapronakFlagsAll)
|
||||||
incomingQuery = r.joinSapronakProductFlag(incomingQuery, "p")
|
incomingQuery = r.joinSapronakProductFlag(incomingQuery, "p")
|
||||||
incomingQuery = applyDateRange(incomingQuery, "st.transfer_date", start, end)
|
|
||||||
incoming, err := scanAndGroupDetails(incomingQuery)
|
incoming, err := scanAndGroupDetails(incomingQuery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
@@ -1373,7 +1300,6 @@ func (r *ClosingRepositoryImpl) FetchSapronakTransfers(ctx context.Context, kand
|
|||||||
Where("(w_source.kandang_id IS NULL OR w_source.kandang_id <> w.kandang_id)").
|
Where("(w_source.kandang_id IS NULL OR w_source.kandang_id <> w.kandang_id)").
|
||||||
Where("f.name IN ?", sapronakFlagsAll)
|
Where("f.name IN ?", sapronakFlagsAll)
|
||||||
incomingLayingQuery = r.joinSapronakProductFlag(incomingLayingQuery, "p")
|
incomingLayingQuery = r.joinSapronakProductFlag(incomingLayingQuery, "p")
|
||||||
incomingLayingQuery = applyDateRange(incomingLayingQuery, "lt.transfer_date", start, end)
|
|
||||||
incomingLaying, err := scanAndGroupDetails(incomingLayingQuery)
|
incomingLaying, err := scanAndGroupDetails(incomingLayingQuery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
@@ -1408,7 +1334,6 @@ func (r *ClosingRepositoryImpl) FetchSapronakTransfers(ctx context.Context, kand
|
|||||||
Where("f.name IN ?", sapronakFlagsAll).
|
Where("f.name IN ?", sapronakFlagsAll).
|
||||||
Group("std.id, std.product_id, p.name, f.name, st.transfer_date, st.movement_number, p.product_price")
|
Group("std.id, std.product_id, p.name, f.name, st.transfer_date, st.movement_number, p.product_price")
|
||||||
outgoingQuery = r.joinSapronakProductFlag(outgoingQuery, "p")
|
outgoingQuery = r.joinSapronakProductFlag(outgoingQuery, "p")
|
||||||
outgoingQuery = applyDateRange(outgoingQuery, "st.transfer_date", start, end)
|
|
||||||
outgoing, err := scanAndGroupDetails(outgoingQuery)
|
outgoing, err := scanAndGroupDetails(outgoingQuery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
@@ -1440,7 +1365,6 @@ func (r *ClosingRepositoryImpl) FetchSapronakTransfers(ctx context.Context, kand
|
|||||||
Where("f.name IN ?", sapronakFlagsAll).
|
Where("f.name IN ?", sapronakFlagsAll).
|
||||||
Group("lt.id, pw.product_id, p.name, f.name, lt.transfer_date, lt.transfer_number, p.product_price")
|
Group("lt.id, pw.product_id, p.name, f.name, lt.transfer_date, lt.transfer_number, p.product_price")
|
||||||
outgoingLayingQuery = r.joinSapronakProductFlag(outgoingLayingQuery, "p")
|
outgoingLayingQuery = r.joinSapronakProductFlag(outgoingLayingQuery, "p")
|
||||||
outgoingLayingQuery = applyDateRange(outgoingLayingQuery, "lt.transfer_date", start, end)
|
|
||||||
outgoingLaying, err := scanAndGroupDetails(outgoingLayingQuery)
|
outgoingLaying, err := scanAndGroupDetails(outgoingLayingQuery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
@@ -1452,7 +1376,7 @@ func (r *ClosingRepositoryImpl) FetchSapronakTransfers(ctx context.Context, kand
|
|||||||
return incoming, outgoing, nil
|
return incoming, outgoing, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *ClosingRepositoryImpl) FetchSapronakSales(ctx context.Context, projectFlockKandangID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, error) {
|
func (r *ClosingRepositoryImpl) FetchSapronakSales(ctx context.Context, projectFlockKandangID uint) (map[uint][]SapronakDetailRow, error) {
|
||||||
query := r.withCtx(ctx).
|
query := r.withCtx(ctx).
|
||||||
Table("stock_allocations AS sa").
|
Table("stock_allocations AS sa").
|
||||||
Select(`
|
Select(`
|
||||||
@@ -1477,7 +1401,6 @@ func (r *ClosingRepositoryImpl) FetchSapronakSales(ctx context.Context, projectF
|
|||||||
Group("mdp.id, pw.product_id, p.name, f.name, mdp.delivery_date, mdp.created_at, m.so_number, mdp.unit_price, mp.unit_price")
|
Group("mdp.id, pw.product_id, p.name, f.name, mdp.delivery_date, mdp.created_at, m.so_number, mdp.unit_price, mp.unit_price")
|
||||||
|
|
||||||
query = r.joinSapronakProductFlag(query, "p")
|
query = r.joinSapronakProductFlag(query, "p")
|
||||||
query = applyDateRange(query, "COALESCE(mdp.delivery_date, mdp.created_at)", start, end)
|
|
||||||
sales, err := scanAndGroupDetails(query)
|
sales, err := scanAndGroupDetails(query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -1511,7 +1434,6 @@ func (r *ClosingRepositoryImpl) FetchSapronakSales(ctx context.Context, projectF
|
|||||||
Group("mdp.id, pw.product_id, p.name, f.name, mdp.delivery_date, mdp.created_at, m.so_number, mdp.unit_price, mp.unit_price")
|
Group("mdp.id, pw.product_id, p.name, f.name, mdp.delivery_date, mdp.created_at, m.so_number, mdp.unit_price, mp.unit_price")
|
||||||
|
|
||||||
nonFifoQuery = r.joinSapronakProductFlag(nonFifoQuery, "p")
|
nonFifoQuery = r.joinSapronakProductFlag(nonFifoQuery, "p")
|
||||||
nonFifoQuery = applyDateRange(nonFifoQuery, "COALESCE(mdp.delivery_date, mdp.created_at)", start, end)
|
|
||||||
nonFifoSales, err := scanAndGroupDetails(nonFifoQuery)
|
nonFifoSales, err := scanAndGroupDetails(nonFifoQuery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -1524,114 +1446,57 @@ func (r *ClosingRepositoryImpl) FetchSapronakSales(ctx context.Context, projectF
|
|||||||
return sales, nil
|
return sales, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *ClosingRepositoryImpl) FetchSapronakSalesAllocatedDetails(ctx context.Context, projectFlockKandangID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, error) {
|
func (r *ClosingRepositoryImpl) FetchSapronakSalesAllocatedDetails(ctx context.Context, projectFlockKandangID uint) (map[uint][]SapronakDetailRow, error) {
|
||||||
if projectFlockKandangID == 0 {
|
if projectFlockKandangID == 0 {
|
||||||
return map[uint][]SapronakDetailRow{}, nil
|
return map[uint][]SapronakDetailRow{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
pfpType := fifo.StockableKeyProjectFlockPopulation.String()
|
|
||||||
dateExpr := fmt.Sprintf(`
|
|
||||||
CASE
|
|
||||||
WHEN sa.stockable_type = '%s' THEN COALESCE(
|
|
||||||
pi_pc.received_date,
|
|
||||||
st_pc.transfer_date,
|
|
||||||
lt_pc.transfer_date,
|
|
||||||
ast_pc.created_at,
|
|
||||||
pc.chick_in_date
|
|
||||||
)
|
|
||||||
ELSE COALESCE(
|
|
||||||
pi.received_date,
|
|
||||||
st.transfer_date,
|
|
||||||
lt.transfer_date,
|
|
||||||
ast.created_at
|
|
||||||
)
|
|
||||||
END
|
|
||||||
`, pfpType)
|
|
||||||
|
|
||||||
query := r.withCtx(ctx).
|
query := r.withCtx(ctx).
|
||||||
Table("stock_allocations AS sa").
|
Table("stock_allocations AS sa").
|
||||||
Select(fmt.Sprintf(`
|
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,
|
||||||
CASE
|
COALESCE(
|
||||||
WHEN sa.stockable_type = '%s' THEN COALESCE(
|
|
||||||
pi_pc.received_date,
|
|
||||||
st_pc.transfer_date,
|
|
||||||
lt_pc.transfer_date,
|
|
||||||
ast_pc.created_at,
|
|
||||||
pc.chick_in_date
|
|
||||||
)
|
|
||||||
ELSE COALESCE(
|
|
||||||
pi.received_date,
|
pi.received_date,
|
||||||
st.transfer_date,
|
st.transfer_date,
|
||||||
lt.transfer_date,
|
lt.transfer_date,
|
||||||
ast.created_at
|
ast.created_at
|
||||||
)
|
) AS date,
|
||||||
END AS date,
|
COALESCE(
|
||||||
CASE
|
|
||||||
WHEN sa.stockable_type = '%s' THEN COALESCE(
|
|
||||||
po_pc.po_number,
|
|
||||||
st_pc.movement_number,
|
|
||||||
lt_pc.transfer_number,
|
|
||||||
CASE WHEN ast_pc.id IS NOT NULL THEN CONCAT('ADJ-', ast_pc.id) END,
|
|
||||||
CONCAT('CHICKIN-', pc.id),
|
|
||||||
''
|
|
||||||
)
|
|
||||||
ELSE COALESCE(
|
|
||||||
po.po_number,
|
po.po_number,
|
||||||
st.movement_number,
|
st.movement_number,
|
||||||
lt.transfer_number,
|
lt.transfer_number,
|
||||||
CASE WHEN ast.id IS NOT NULL THEN CONCAT('ADJ-', ast.id) END,
|
CONCAT('ADJ-', ast.id),
|
||||||
''
|
''
|
||||||
)
|
) AS reference,
|
||||||
END 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,
|
||||||
CASE
|
COALESCE(pi.price, p.product_price, 0) AS price
|
||||||
WHEN sa.stockable_type = '%s' THEN COALESCE(pi_pc.price, p_resolve.product_price, 0)
|
`).
|
||||||
ELSE COALESCE(pi.price, p_resolve.product_price, 0)
|
|
||||||
END AS price
|
|
||||||
`, pfpType, pfpType, pfpType)).
|
|
||||||
Joins("JOIN marketing_delivery_products mdp ON mdp.id = sa.usable_id AND sa.usable_type = ?", fifo.UsableKeyMarketingDelivery.String()).
|
|
||||||
Joins("JOIN product_warehouses pw_sales ON pw_sales.id = mdp.product_warehouse_id").
|
|
||||||
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("JOIN marketing_delivery_products mdp ON mdp.id = sa.usable_id AND sa.usable_type = ?", fifo.UsableKeyMarketingDelivery.String()).
|
||||||
Joins("LEFT JOIN purchase_items pi ON pi.id = sa.stockable_id AND sa.stockable_type = ?", fifo.StockableKeyPurchaseItems.String()).
|
Joins("LEFT JOIN purchase_items pi ON pi.id = sa.stockable_id AND sa.stockable_type = ?", fifo.StockableKeyPurchaseItems.String()).
|
||||||
Joins("LEFT JOIN purchases po ON po.id = pi.purchase_id").
|
Joins("LEFT JOIN purchases po ON po.id = pi.purchase_id").
|
||||||
Joins("LEFT JOIN stock_transfer_details std ON std.id = sa.stockable_id AND sa.stockable_type = ?", fifo.StockableKeyStockTransferIn.String()).
|
Joins("LEFT JOIN stock_transfer_details std ON std.id = sa.stockable_id AND sa.stockable_type = ?", fifo.StockableKeyStockTransferIn.String()).
|
||||||
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_chickins pc ON pc.id = pfp.project_chickin_id").
|
|
||||||
Joins("LEFT JOIN stock_allocations sa_pc ON sa_pc.usable_type = ? AND sa_pc.usable_id = pc.id", fifo.UsableKeyProjectChickin.String()).
|
|
||||||
Joins("LEFT JOIN purchase_items pi_pc ON pi_pc.id = sa_pc.stockable_id AND sa_pc.stockable_type = ?", fifo.StockableKeyPurchaseItems.String()).
|
|
||||||
Joins("LEFT JOIN purchases po_pc ON po_pc.id = pi_pc.purchase_id").
|
|
||||||
Joins("LEFT JOIN stock_transfer_details std_pc ON std_pc.id = sa_pc.stockable_id AND sa_pc.stockable_type = ?", fifo.StockableKeyStockTransferIn.String()).
|
|
||||||
Joins("LEFT JOIN stock_transfers st_pc ON st_pc.id = std_pc.stock_transfer_id").
|
|
||||||
Joins("LEFT JOIN laying_transfer_targets ltt_pc ON ltt_pc.id = sa_pc.stockable_id AND sa_pc.stockable_type = ?", fifo.StockableKeyTransferToLayingIn.String()).
|
|
||||||
Joins("LEFT JOIN laying_transfers lt_pc ON lt_pc.id = ltt_pc.laying_transfer_id").
|
|
||||||
Joins("LEFT JOIN adjustment_stocks ast_pc ON ast_pc.id = sa_pc.stockable_id AND sa_pc.stockable_type = ?", fifo.StockableKeyAdjustmentIn.String()).
|
|
||||||
Joins("LEFT JOIN product_warehouses pw_pc ON pw_pc.id = pc.product_warehouse_id").
|
|
||||||
Joins(fmt.Sprintf("LEFT JOIN products p_resolve ON p_resolve.id = CASE WHEN sa.stockable_type = '%s' THEN pw_pc.product_id ELSE COALESCE(pi.product_id, pw_ltt.product_id, pw.product_id) END", pfpType)).
|
|
||||||
Where("sa.status = ?", entity.StockAllocationStatusActive).
|
Where("sa.status = ?", entity.StockAllocationStatusActive).
|
||||||
Where("sa.allocation_purpose = ?", entity.StockAllocationPurposeConsume).
|
Where("sa.allocation_purpose = ?", entity.StockAllocationPurposeConsume).
|
||||||
Where("sa.stockable_type <> ?", fifo.StockableKeyProjectFlockPopulation.String()).
|
Where("sa.stockable_type <> ?", fifo.StockableKeyProjectFlockPopulation.String()).
|
||||||
Where("pw.project_flock_kandang_id = ?", projectFlockKandangID).
|
Where("pw.project_flock_kandang_id = ?", projectFlockKandangID).
|
||||||
Where("f.name IN ?", sapronakFlagsAll).
|
Where("f.name IN ?", sapronakFlagsAll).
|
||||||
Group(`
|
Group(`
|
||||||
p_resolve.id, p_resolve.name, f.name,
|
pw.product_id, p.name, f.name,
|
||||||
pi_pc.received_date, st_pc.transfer_date, lt_pc.transfer_date, ast_pc.created_at, pc.chick_in_date,
|
|
||||||
pi.received_date, st.transfer_date, lt.transfer_date, ast.created_at,
|
pi.received_date, st.transfer_date, lt.transfer_date, ast.created_at,
|
||||||
po_pc.po_number, st_pc.movement_number, lt_pc.transfer_number, ast_pc.id, pc.id,
|
|
||||||
po.po_number, st.movement_number, lt.transfer_number, ast.id,
|
po.po_number, st.movement_number, lt.transfer_number, ast.id,
|
||||||
pi_pc.price, pi.price, p_resolve.product_price, sa.stockable_type
|
pi.price, p.product_price
|
||||||
`)
|
`)
|
||||||
|
|
||||||
query = r.joinSapronakProductFlag(query, "p_resolve")
|
query = r.joinSapronakProductFlag(query, "p")
|
||||||
query = applyDateRange(query, dateExpr, start, end)
|
|
||||||
return scanAndGroupDetails(query)
|
return scanAndGroupDetails(query)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1645,6 +1510,7 @@ func (r *ClosingRepositoryImpl) GetProductsWithFlagsByIDs(ctx context.Context, p
|
|||||||
Preload("Flags").
|
Preload("Flags").
|
||||||
Where("id IN ?", productIDs).
|
Where("id IN ?", productIDs).
|
||||||
Find(&products).Error
|
Find(&products).Error
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
@@ -32,14 +33,6 @@ import (
|
|||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type activeKandangMetric struct {
|
|
||||||
ProjectFlockKandangID uint
|
|
||||||
ProjectFlockID uint
|
|
||||||
KandangID uint
|
|
||||||
Category string
|
|
||||||
Metric float64
|
|
||||||
}
|
|
||||||
|
|
||||||
type ClosingService interface {
|
type ClosingService interface {
|
||||||
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]dto.ClosingListItemDTO, int64, error)
|
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]dto.ClosingListItemDTO, int64, error)
|
||||||
GetProjectFlockByID(ctx *fiber.Ctx, id uint) (*entity.ProjectFlock, error)
|
GetProjectFlockByID(ctx *fiber.Ctx, id uint) (*entity.ProjectFlock, error)
|
||||||
@@ -392,11 +385,6 @@ func (s closingService) GetClosingSapronak(c *fiber.Ctx, projectFlockID uint, pa
|
|||||||
}
|
}
|
||||||
|
|
||||||
offset := (params.Page - 1) * params.Limit
|
offset := (params.Page - 1) * params.Limit
|
||||||
startDate, endDate, err := s.getSapronakDateRange(c.Context(), projectFlockID, params.KandangID)
|
|
||||||
if err != nil {
|
|
||||||
s.Log.Errorf("Failed to resolve sapronak date range for project flock %d: %+v", projectFlockID, err)
|
|
||||||
return nil, 0, fiber.NewError(fiber.StatusInternalServerError, "Failed to resolve sapronak date range")
|
|
||||||
}
|
|
||||||
rows, totalResults, err := s.Repository.GetSapronak(c.Context(), repository.SapronakQueryParams{
|
rows, totalResults, err := s.Repository.GetSapronak(c.Context(), repository.SapronakQueryParams{
|
||||||
Type: params.Type,
|
Type: params.Type,
|
||||||
WarehouseIDs: warehouseIDs,
|
WarehouseIDs: warehouseIDs,
|
||||||
@@ -404,8 +392,6 @@ func (s closingService) GetClosingSapronak(c *fiber.Ctx, projectFlockID uint, pa
|
|||||||
Limit: params.Limit,
|
Limit: params.Limit,
|
||||||
Offset: offset,
|
Offset: offset,
|
||||||
Search: params.Search,
|
Search: params.Search,
|
||||||
StartDate: startDate,
|
|
||||||
EndDate: endDate,
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.Log.Errorf("Failed to fetch sapronak %s for project flock %d: %+v", params.Type, projectFlockID, err)
|
s.Log.Errorf("Failed to fetch sapronak %s for project flock %d: %+v", params.Type, projectFlockID, err)
|
||||||
@@ -482,19 +468,11 @@ func (s closingService) GetClosingSapronakSummary(c *fiber.Ctx, projectFlockID u
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
startDate, endDate, err := s.getSapronakDateRange(c.Context(), projectFlockID, params.KandangID)
|
|
||||||
if err != nil {
|
|
||||||
s.Log.Errorf("Failed to resolve sapronak date range for project flock %d: %+v", projectFlockID, err)
|
|
||||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to resolve sapronak date range")
|
|
||||||
}
|
|
||||||
|
|
||||||
rows, err := s.Repository.GetSapronakSummary(c.Context(), repository.SapronakQueryParams{
|
rows, err := s.Repository.GetSapronakSummary(c.Context(), repository.SapronakQueryParams{
|
||||||
Type: params.Type,
|
Type: params.Type,
|
||||||
WarehouseIDs: warehouseIDs,
|
WarehouseIDs: warehouseIDs,
|
||||||
ProjectFlockKandangIDs: projectFlockKandangIDs,
|
ProjectFlockKandangIDs: projectFlockKandangIDs,
|
||||||
Search: params.Search,
|
Search: params.Search,
|
||||||
StartDate: startDate,
|
|
||||||
EndDate: endDate,
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.Log.Errorf("Failed to fetch sapronak %s summary for project flock %d: %+v", params.Type, projectFlockID, err)
|
s.Log.Errorf("Failed to fetch sapronak %s summary for project flock %d: %+v", params.Type, projectFlockID, err)
|
||||||
@@ -564,90 +542,6 @@ func (s closingService) getProjectFlockKandangIDs(ctx context.Context, projectFl
|
|||||||
return ids, nil
|
return ids, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s closingService) getSapronakDateRange(ctx context.Context, projectFlockID uint, kandangID *uint) (*time.Time, *time.Time, error) {
|
|
||||||
db := s.Repository.DB().WithContext(ctx)
|
|
||||||
|
|
||||||
if kandangID != nil && *kandangID > 0 {
|
|
||||||
var pfk entity.ProjectFlockKandang
|
|
||||||
if err := db.Select("id, created_at, closed_at").First(&pfk, *kandangID).Error; err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var minChickin *time.Time
|
|
||||||
if err := db.Table("project_chickins").
|
|
||||||
Select("MIN(chick_in_date)").
|
|
||||||
Where("project_flock_kandang_id = ?", pfk.Id).
|
|
||||||
Scan(&minChickin).Error; err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
start := pfk.CreatedAt
|
|
||||||
if minChickin != nil && !minChickin.IsZero() {
|
|
||||||
start = *minChickin
|
|
||||||
}
|
|
||||||
startDate := dateOnlyUTC(start)
|
|
||||||
|
|
||||||
var endDate *time.Time
|
|
||||||
if pfk.ClosedAt != nil {
|
|
||||||
d := dateOnlyUTC(*pfk.ClosedAt)
|
|
||||||
endDate = &d
|
|
||||||
}
|
|
||||||
|
|
||||||
return &startDate, endDate, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var minCreated time.Time
|
|
||||||
if err := db.Model(&entity.ProjectFlockKandang{}).
|
|
||||||
Select("MIN(created_at)").
|
|
||||||
Where("project_flock_id = ?", projectFlockID).
|
|
||||||
Scan(&minCreated).Error; err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var minChickin *time.Time
|
|
||||||
if err := db.Table("project_chickins pc").
|
|
||||||
Select("MIN(pc.chick_in_date)").
|
|
||||||
Joins("JOIN project_flock_kandangs pfk ON pfk.id = pc.project_flock_kandang_id").
|
|
||||||
Where("pfk.project_flock_id = ?", projectFlockID).
|
|
||||||
Scan(&minChickin).Error; err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
start := minCreated
|
|
||||||
if minChickin != nil && !minChickin.IsZero() {
|
|
||||||
start = *minChickin
|
|
||||||
}
|
|
||||||
startDate := dateOnlyUTC(start)
|
|
||||||
|
|
||||||
var endDate *time.Time
|
|
||||||
var openCount int64
|
|
||||||
if err := db.Model(&entity.ProjectFlockKandang{}).
|
|
||||||
Where("project_flock_id = ? AND closed_at IS NULL", projectFlockID).
|
|
||||||
Count(&openCount).Error; err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
if openCount == 0 {
|
|
||||||
var maxClosed *time.Time
|
|
||||||
if err := db.Model(&entity.ProjectFlockKandang{}).
|
|
||||||
Select("MAX(closed_at)").
|
|
||||||
Where("project_flock_id = ?", projectFlockID).
|
|
||||||
Scan(&maxClosed).Error; err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
if maxClosed != nil && !maxClosed.IsZero() {
|
|
||||||
d := dateOnlyUTC(*maxClosed)
|
|
||||||
endDate = &d
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return &startDate, endDate, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func dateOnlyUTC(t time.Time) time.Time {
|
|
||||||
u := t.UTC()
|
|
||||||
return time.Date(u.Year(), u.Month(), u.Day(), 0, 0, 0, 0, time.UTC)
|
|
||||||
}
|
|
||||||
|
|
||||||
func formatQuantity(qty float64, uom string) string {
|
func formatQuantity(qty float64, uom string) string {
|
||||||
qtyStr := strconv.FormatFloat(qty, 'f', -1, 64)
|
qtyStr := strconv.FormatFloat(qty, 'f', -1, 64)
|
||||||
if uom == "" {
|
if uom == "" {
|
||||||
@@ -722,17 +616,38 @@ func (s closingService) GetOverhead(c *fiber.Ctx, projectFlockID uint, projectFl
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
realizations, err = s.allocateFarmOverheadRealizations(c.Context(), projectFlockID, projectFlockKandangID, realizations)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
projectFlockKandangs, err := s.ProjectFlockKandangRepo.GetByProjectFlockID(c.Context(), projectFlockID)
|
projectFlockKandangs, err := s.ProjectFlockKandangRepo.GetByProjectFlockID(c.Context(), projectFlockID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
totalKandangCount := len(projectFlockKandangs)
|
totalKandangCount := len(projectFlockKandangs)
|
||||||
|
|
||||||
|
// Build kandang count map for farm expense division
|
||||||
|
projectFlockKandangCountMap := make(map[uint]int)
|
||||||
|
projectFlockKandangCountMap[projectFlockID] = totalKandangCount
|
||||||
|
|
||||||
|
involvedProjectFlocks := make(map[uint]bool)
|
||||||
|
for _, realization := range realizations {
|
||||||
|
if realization.ExpenseNonstock != nil &&
|
||||||
|
realization.ExpenseNonstock.Expense != nil &&
|
||||||
|
realization.ExpenseNonstock.Expense.ProjectFlockId != nil {
|
||||||
|
var projectFlockIDs []uint
|
||||||
|
if err := json.Unmarshal([]byte(*realization.ExpenseNonstock.Expense.ProjectFlockId), &projectFlockIDs); err == nil {
|
||||||
|
for _, pfID := range projectFlockIDs {
|
||||||
|
if pfID != projectFlockID {
|
||||||
|
involvedProjectFlocks[pfID] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for pfID := range involvedProjectFlocks {
|
||||||
|
if pfKandangs, err := s.ProjectFlockKandangRepo.GetByProjectFlockID(c.Context(), pfID); err == nil {
|
||||||
|
projectFlockKandangCountMap[pfID] = len(pfKandangs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
chickins, err := s.ChickinRepo.GetByProjectFlockID(c.Context(), projectFlockID)
|
chickins, err := s.ChickinRepo.GetByProjectFlockID(c.Context(), projectFlockID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -773,197 +688,11 @@ func (s closingService) GetOverhead(c *fiber.Ctx, projectFlockID uint, projectFl
|
|||||||
|
|
||||||
totalActualPopulation := totalChickinQty - totalDepletion
|
totalActualPopulation := totalChickinQty - totalDepletion
|
||||||
|
|
||||||
result := dto.ToOverheadListDTOs(budgets, realizations, totalChickinQty, totalActualPopulation, projectFlockKandangID != nil, totalKandangCount)
|
result := dto.ToOverheadListDTOs(budgets, realizations, totalChickinQty, totalActualPopulation, projectFlockKandangID != nil, totalKandangCount, projectFlockKandangCountMap)
|
||||||
|
|
||||||
return &result, nil
|
return &result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type activeKandangMetricRow struct {
|
|
||||||
ProjectFlockKandangID uint `gorm:"column:project_flock_kandang_id"`
|
|
||||||
ProjectFlockID uint `gorm:"column:project_flock_id"`
|
|
||||||
KandangID uint `gorm:"column:kandang_id"`
|
|
||||||
Category string `gorm:"column:category"`
|
|
||||||
ChickinQty float64 `gorm:"column:chickin_qty"`
|
|
||||||
DepletionQty float64 `gorm:"column:depletion_qty"`
|
|
||||||
EggQty float64 `gorm:"column:egg_qty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s closingService) getActiveKandangMetrics(ctx context.Context, locationID uint, transactionDate time.Time) ([]activeKandangMetric, error) {
|
|
||||||
db := s.Repository.DB().WithContext(ctx)
|
|
||||||
|
|
||||||
rows := []activeKandangMetricRow{}
|
|
||||||
rawSQL := `
|
|
||||||
SELECT
|
|
||||||
pfk.id AS project_flock_kandang_id,
|
|
||||||
pfk.project_flock_id AS project_flock_id,
|
|
||||||
pfk.kandang_id AS kandang_id,
|
|
||||||
pf.category AS category,
|
|
||||||
COALESCE((
|
|
||||||
SELECT SUM(pc.usage_qty)
|
|
||||||
FROM project_chickins pc
|
|
||||||
WHERE pc.project_flock_kandang_id = pfk.id
|
|
||||||
AND pc.chick_in_date::date <= ?
|
|
||||||
), 0) AS chickin_qty,
|
|
||||||
COALESCE((
|
|
||||||
SELECT SUM(rd.qty)
|
|
||||||
FROM recording_depletions rd
|
|
||||||
JOIN recordings r ON r.id = rd.recording_id
|
|
||||||
WHERE r.project_flock_kandangs_id = pfk.id
|
|
||||||
AND r.record_datetime::date <= ?
|
|
||||||
), 0) AS depletion_qty,
|
|
||||||
COALESCE((
|
|
||||||
SELECT SUM(re.qty)
|
|
||||||
FROM recording_eggs re
|
|
||||||
JOIN recordings r2 ON r2.id = re.recording_id
|
|
||||||
WHERE r2.project_flock_kandangs_id = pfk.id
|
|
||||||
AND r2.record_datetime::date <= ?
|
|
||||||
), 0) AS egg_qty
|
|
||||||
FROM project_flock_kandangs pfk
|
|
||||||
JOIN project_flocks pf ON pf.id = pfk.project_flock_id
|
|
||||||
WHERE pf.location_id = ?
|
|
||||||
AND (pfk.closed_at IS NULL OR pfk.closed_at::date > ?)
|
|
||||||
AND EXISTS (
|
|
||||||
SELECT 1
|
|
||||||
FROM project_chickins pc2
|
|
||||||
WHERE pc2.project_flock_kandang_id = pfk.id
|
|
||||||
AND pc2.chick_in_date::date <= ?
|
|
||||||
)
|
|
||||||
`
|
|
||||||
if err := db.Raw(rawSQL, transactionDate, transactionDate, transactionDate, locationID, transactionDate, transactionDate).Scan(&rows).Error; err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
result := make([]activeKandangMetric, 0, len(rows))
|
|
||||||
for _, row := range rows {
|
|
||||||
metric := 0.0
|
|
||||||
switch strings.ToLower(strings.TrimSpace(row.Category)) {
|
|
||||||
case "growing":
|
|
||||||
metric = row.ChickinQty
|
|
||||||
case "laying":
|
|
||||||
metric = row.EggQty
|
|
||||||
default:
|
|
||||||
s.Log.Warnf("Unknown project flock category for overhead allocation: %s (pfk=%d)", row.Category, row.ProjectFlockKandangID)
|
|
||||||
}
|
|
||||||
|
|
||||||
result = append(result, activeKandangMetric{
|
|
||||||
ProjectFlockKandangID: row.ProjectFlockKandangID,
|
|
||||||
ProjectFlockID: row.ProjectFlockID,
|
|
||||||
KandangID: row.KandangID,
|
|
||||||
Category: row.Category,
|
|
||||||
Metric: metric,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func round2(value float64) float64 {
|
|
||||||
return math.Round(value*100) / 100
|
|
||||||
}
|
|
||||||
|
|
||||||
func allocateFarmLevelQty(totalQty float64, metrics []activeKandangMetric) map[uint]float64 {
|
|
||||||
allocations := make(map[uint]float64, len(metrics))
|
|
||||||
if totalQty == 0 || len(metrics) == 0 {
|
|
||||||
return allocations
|
|
||||||
}
|
|
||||||
|
|
||||||
totalMetric := 0.0
|
|
||||||
var maxMetric float64
|
|
||||||
var maxMetricID uint
|
|
||||||
for _, m := range metrics {
|
|
||||||
if m.Metric <= 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
totalMetric += m.Metric
|
|
||||||
if m.Metric > maxMetric || maxMetricID == 0 {
|
|
||||||
maxMetric = m.Metric
|
|
||||||
maxMetricID = m.ProjectFlockKandangID
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if totalMetric == 0 {
|
|
||||||
return allocations
|
|
||||||
}
|
|
||||||
|
|
||||||
sumRounded := 0.0
|
|
||||||
for _, m := range metrics {
|
|
||||||
if m.Metric <= 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
portion := totalQty * (m.Metric / totalMetric)
|
|
||||||
rounded := round2(portion)
|
|
||||||
allocations[m.ProjectFlockKandangID] = rounded
|
|
||||||
sumRounded += rounded
|
|
||||||
}
|
|
||||||
|
|
||||||
diff := totalQty - sumRounded
|
|
||||||
if maxMetricID != 0 && diff != 0 {
|
|
||||||
allocations[maxMetricID] = round2(allocations[maxMetricID] + diff)
|
|
||||||
}
|
|
||||||
|
|
||||||
return allocations
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s closingService) allocateFarmOverheadRealizations(ctx context.Context, projectFlockID uint, projectFlockKandangID *uint, realizations []entity.ExpenseRealization) ([]entity.ExpenseRealization, error) {
|
|
||||||
if len(realizations) == 0 {
|
|
||||||
return realizations, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
cache := make(map[string][]activeKandangMetric)
|
|
||||||
allocated := make([]entity.ExpenseRealization, 0, len(realizations))
|
|
||||||
|
|
||||||
for _, realization := range realizations {
|
|
||||||
expenseNonstock := realization.ExpenseNonstock
|
|
||||||
if expenseNonstock == nil || expenseNonstock.Expense == nil {
|
|
||||||
allocated = append(allocated, realization)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// If already bound to a specific project flock kandang, don't re-allocate.
|
|
||||||
if expenseNonstock.ProjectFlockKandangId != nil {
|
|
||||||
allocated = append(allocated, realization)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
expense := expenseNonstock.Expense
|
|
||||||
locationID := uint(expense.LocationId)
|
|
||||||
txDate := expense.RealizationDate
|
|
||||||
|
|
||||||
cacheKey := fmt.Sprintf("%d|%s", locationID, txDate.Format("2006-01-02"))
|
|
||||||
metrics, exists := cache[cacheKey]
|
|
||||||
if !exists {
|
|
||||||
var err error
|
|
||||||
metrics, err = s.getActiveKandangMetrics(ctx, locationID, txDate)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
cache[cacheKey] = metrics
|
|
||||||
}
|
|
||||||
|
|
||||||
allocations := allocateFarmLevelQty(realization.Qty, metrics)
|
|
||||||
allocatedQty := 0.0
|
|
||||||
if projectFlockKandangID != nil {
|
|
||||||
allocatedQty = allocations[*projectFlockKandangID]
|
|
||||||
} else {
|
|
||||||
for _, m := range metrics {
|
|
||||||
if m.ProjectFlockID == projectFlockID {
|
|
||||||
allocatedQty += allocations[m.ProjectFlockKandangID]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
allocatedQty = round2(allocatedQty)
|
|
||||||
}
|
|
||||||
|
|
||||||
adj := realization
|
|
||||||
adj.Qty = allocatedQty
|
|
||||||
if adj.Qty == 0 {
|
|
||||||
adj.Price = realization.Price
|
|
||||||
}
|
|
||||||
|
|
||||||
allocated = append(allocated, adj)
|
|
||||||
}
|
|
||||||
|
|
||||||
return allocated, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s closingService) GetExpeditionHPP(c *fiber.Ctx, projectFlockID uint, projectFlockKandangID *uint) (*dto.ExpeditionHPPDTO, error) {
|
func (s closingService) GetExpeditionHPP(c *fiber.Ctx, projectFlockID uint, projectFlockKandangID *uint) (*dto.ExpeditionHPPDTO, error) {
|
||||||
if projectFlockKandangID != nil {
|
if projectFlockKandangID != nil {
|
||||||
if err := m.EnsureProjectFlockKandangAccess(c, s.Repository.DB(), projectFlockID, *projectFlockKandangID); err != nil {
|
if err := m.EnsureProjectFlockKandangAccess(c, s.Repository.DB(), projectFlockID, *projectFlockKandangID); err != nil {
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/go-playground/validator/v10"
|
"github.com/go-playground/validator/v10"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
@@ -124,7 +123,7 @@ func (s sapronakService) computeSapronakReports(ctx context.Context, params *val
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter sapronak data by project flock period range.
|
// We no longer filter by date for closing sapronak report; pass nil pointers.
|
||||||
items, groups, totalIncoming, totalUsage, err := s.buildSapronakItems(ctx, pfk, params.Flag)
|
items, groups, totalIncoming, totalUsage, err := s.buildSapronakItems(ctx, pfk, params.Flag)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.Log.Errorf("Failed to build sapronak items for pfk %d: %+v", pfk.Id, err)
|
s.Log.Errorf("Failed to build sapronak items for pfk %d: %+v", pfk.Id, err)
|
||||||
@@ -380,33 +379,33 @@ func buildSapronakDetails(
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s sapronakService) buildSapronakItems(ctx context.Context, pfk entity.ProjectFlockKandang, flagFilter string) ([]dto.SapronakItemDTO, []dto.SapronakGroupDTO, float64, float64, error) {
|
func (s sapronakService) buildSapronakItems(ctx context.Context, pfk entity.ProjectFlockKandang, flagFilter string) ([]dto.SapronakItemDTO, []dto.SapronakGroupDTO, float64, float64, error) {
|
||||||
// Filter by project flock period (start = first chickin or pfk created_at, end = closed_at if any).
|
// For sapronak closing report we intentionally ignore date range
|
||||||
startDate, endDate := sapronakPeriodRange(pfk)
|
// and aggregate all historical transactions for the kandang/project.
|
||||||
incomingRows, err := s.Repository.FetchSapronakIncoming(ctx, pfk.KandangId, startDate, endDate)
|
incomingRows, err := s.Repository.FetchSapronakIncoming(ctx, pfk.KandangId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, 0, 0, err
|
return nil, nil, 0, 0, err
|
||||||
}
|
}
|
||||||
incomingDetailsRows, err := s.Repository.FetchSapronakIncomingDetails(ctx, pfk.KandangId, startDate, endDate)
|
incomingDetailsRows, err := s.Repository.FetchSapronakIncomingDetails(ctx, pfk.KandangId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, 0, 0, err
|
return nil, nil, 0, 0, err
|
||||||
}
|
}
|
||||||
usageRows, err := s.Repository.FetchSapronakUsage(ctx, pfk.Id, startDate, endDate)
|
usageRows, err := s.Repository.FetchSapronakUsage(ctx, pfk.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, 0, 0, err
|
return nil, nil, 0, 0, err
|
||||||
}
|
}
|
||||||
chickinUsageRows, err := s.Repository.FetchSapronakChickinUsage(ctx, pfk.Id, startDate, endDate)
|
chickinUsageRows, err := s.Repository.FetchSapronakChickinUsage(ctx, pfk.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, 0, 0, err
|
return nil, nil, 0, 0, err
|
||||||
}
|
}
|
||||||
usageDetailsRows, err := s.Repository.FetchSapronakUsageDetails(ctx, pfk.Id, startDate, endDate)
|
usageDetailsRows, err := s.Repository.FetchSapronakUsageDetails(ctx, pfk.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, 0, 0, err
|
return nil, nil, 0, 0, err
|
||||||
}
|
}
|
||||||
chickinUsageDetailsRows, err := s.Repository.FetchSapronakChickinUsageDetails(ctx, pfk.Id, startDate, endDate)
|
chickinUsageDetailsRows, err := s.Repository.FetchSapronakChickinUsageDetails(ctx, pfk.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, 0, 0, err
|
return nil, nil, 0, 0, err
|
||||||
}
|
}
|
||||||
usageAllocatedDetails, err := s.Repository.FetchSapronakUsageAllocatedDetails(ctx, pfk.Id, startDate, endDate)
|
usageAllocatedDetails, err := s.Repository.FetchSapronakUsageAllocatedDetails(ctx, pfk.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, 0, 0, err
|
return nil, nil, 0, 0, err
|
||||||
}
|
}
|
||||||
@@ -414,15 +413,15 @@ func (s sapronakService) buildSapronakItems(ctx context.Context, pfk entity.Proj
|
|||||||
usageDetailsRows = usageAllocatedDetails
|
usageDetailsRows = usageAllocatedDetails
|
||||||
chickinUsageDetailsRows = map[uint][]repository.SapronakDetailRow{}
|
chickinUsageDetailsRows = map[uint][]repository.SapronakDetailRow{}
|
||||||
}
|
}
|
||||||
adjIncomingRows, adjOutgoingRows, err := s.Repository.FetchSapronakAdjustments(ctx, pfk.KandangId, startDate, endDate)
|
adjIncomingRows, adjOutgoingRows, err := s.Repository.FetchSapronakAdjustments(ctx, pfk.KandangId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, 0, 0, err
|
return nil, nil, 0, 0, err
|
||||||
}
|
}
|
||||||
transIncomingRows, transOutgoingRows, err := s.Repository.FetchSapronakTransfers(ctx, pfk.KandangId, startDate, endDate)
|
transIncomingRows, transOutgoingRows, err := s.Repository.FetchSapronakTransfers(ctx, pfk.KandangId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, 0, 0, err
|
return nil, nil, 0, 0, err
|
||||||
}
|
}
|
||||||
salesOutRows, err := s.Repository.FetchSapronakSalesAllocatedDetails(ctx, pfk.Id, startDate, endDate)
|
salesOutRows, err := s.Repository.FetchSapronakSalesAllocatedDetails(ctx, pfk.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, 0, 0, err
|
return nil, nil, 0, 0, err
|
||||||
}
|
}
|
||||||
@@ -471,7 +470,6 @@ func (s sapronakService) buildSapronakItems(ctx context.Context, pfk entity.Proj
|
|||||||
// should not be counted yet. Only when category is LAYING we allow
|
// should not be counted yet. Only when category is LAYING we allow
|
||||||
// pullet usage to contribute to qty_used.
|
// pullet usage to contribute to qty_used.
|
||||||
isLaying := strings.EqualFold(string(pfk.ProjectFlock.Category), string(utils.ProjectFlockCategoryLaying))
|
isLaying := strings.EqualFold(string(pfk.ProjectFlock.Category), string(utils.ProjectFlockCategoryLaying))
|
||||||
hasChickin := len(pfk.Chickins) > 0
|
|
||||||
|
|
||||||
if !isLaying {
|
if !isLaying {
|
||||||
filteredUsage := make([]repository.SapronakUsageRow, 0, len(chickinUsageRows))
|
filteredUsage := make([]repository.SapronakUsageRow, 0, len(chickinUsageRows))
|
||||||
@@ -493,6 +491,11 @@ func (s sapronakService) buildSapronakItems(ctx context.Context, pfk entity.Proj
|
|||||||
chickinUsageDetailsRows = filteredDetail
|
chickinUsageDetailsRows = filteredDetail
|
||||||
}
|
}
|
||||||
|
|
||||||
|
allUsageRows := append(usageRows, chickinUsageRows...)
|
||||||
|
incoming, usage := mapIncomingUsage(incomingRows, allUsageRows)
|
||||||
|
itemMap := make(map[uint]dto.SapronakItemDTO, len(incoming)+len(usage))
|
||||||
|
groupMap := make(map[string]*dto.SapronakGroupDTO)
|
||||||
|
|
||||||
for pid, rows := range chickinUsageDetailsRows {
|
for pid, rows := range chickinUsageDetailsRows {
|
||||||
if len(rows) == 0 {
|
if len(rows) == 0 {
|
||||||
continue
|
continue
|
||||||
@@ -509,11 +512,6 @@ func (s sapronakService) buildSapronakItems(ctx context.Context, pfk entity.Proj
|
|||||||
transOutgoing := detailMaps.TransferOut
|
transOutgoing := detailMaps.TransferOut
|
||||||
salesOutgoing := detailMaps.SalesOut
|
salesOutgoing := detailMaps.SalesOut
|
||||||
|
|
||||||
allUsageRows := append(usageRows, chickinUsageRows...)
|
|
||||||
incoming, usage := mapIncomingUsage(incomingRows, allUsageRows)
|
|
||||||
itemMap := make(map[uint]dto.SapronakItemDTO, len(incoming)+len(usage))
|
|
||||||
groupMap := make(map[string]*dto.SapronakGroupDTO)
|
|
||||||
|
|
||||||
transIncoming = dedupTransfers(transIncoming)
|
transIncoming = dedupTransfers(transIncoming)
|
||||||
transOutgoing = dedupTransfers(transOutgoing)
|
transOutgoing = dedupTransfers(transOutgoing)
|
||||||
|
|
||||||
@@ -777,9 +775,6 @@ func (s sapronakService) buildSapronakItems(ctx context.Context, pfk entity.Proj
|
|||||||
if !matchesFlag(flag) {
|
if !matchesFlag(flag) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if hasChickin && (strings.EqualFold(flag, "DOC") || strings.EqualFold(flag, "PULLET") || strings.EqualFold(flag, "LAYER")) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
group := ensureGroup(flag)
|
group := ensureGroup(flag)
|
||||||
for _, d := range details {
|
for _, d := range details {
|
||||||
if d.Flag == "" {
|
if d.Flag == "" {
|
||||||
@@ -799,10 +794,6 @@ func (s sapronakService) buildSapronakItems(ctx context.Context, pfk entity.Proj
|
|||||||
if !matchesFlag(flag) {
|
if !matchesFlag(flag) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// For chicken, we don't count sales as sapronak outflow.
|
|
||||||
if strings.EqualFold(flag, "DOC") || strings.EqualFold(flag, "PULLET") || strings.EqualFold(flag, "LAYER") {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
group := ensureGroup(flag)
|
group := ensureGroup(flag)
|
||||||
for _, d := range details {
|
for _, d := range details {
|
||||||
if d.Flag == "" {
|
if d.Flag == "" {
|
||||||
@@ -824,20 +815,3 @@ func (s sapronakService) buildSapronakItems(ctx context.Context, pfk entity.Proj
|
|||||||
|
|
||||||
return items, groups, totalIncoming, totalUsage, nil
|
return items, groups, totalIncoming, totalUsage, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func sapronakPeriodRange(pfk entity.ProjectFlockKandang) (*time.Time, *time.Time) {
|
|
||||||
if len(pfk.Chickins) == 0 {
|
|
||||||
start := dateOnlyUTC(pfk.CreatedAt)
|
|
||||||
return &start, pfk.ClosedAt
|
|
||||||
}
|
|
||||||
|
|
||||||
minDate := pfk.Chickins[0].ChickInDate
|
|
||||||
for _, c := range pfk.Chickins[1:] {
|
|
||||||
if c.ChickInDate.Before(minDate) {
|
|
||||||
minDate = c.ChickInDate
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
start := dateOnlyUTC(minDate)
|
|
||||||
return &start, pfk.ClosedAt
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -64,11 +64,9 @@ func (u *DailyChecklistController) GetAll(c *fiber.Ctx) error {
|
|||||||
status = *item.Status
|
status = *item.Status
|
||||||
}
|
}
|
||||||
|
|
||||||
// var kandang *kandangDTO.KandangRelationDTO
|
var kandang *kandangDTO.KandangRelationDTO
|
||||||
var kandang *kandangDTO.KandangGroupRelationDTO
|
|
||||||
if item.Kandang.Id != 0 {
|
if item.Kandang.Id != 0 {
|
||||||
// mapped := kandangDTO.ToKandangRelationDTO(item.Kandang)x
|
mapped := kandangDTO.ToKandangRelationDTO(item.Kandang)
|
||||||
mapped := kandangDTO.ToKandangGroupRelationDTO(item.Kandang)
|
|
||||||
kandang = &mapped
|
kandang = &mapped
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ type DailyChecklistListDTO struct {
|
|||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
Category string `json:"category"`
|
Category string `json:"category"`
|
||||||
Date time.Time `json:"date"`
|
Date time.Time `json:"date"`
|
||||||
Kandang *kandangDTO.KandangGroupRelationDTO `json:"kandang,omitempty"`
|
Kandang *kandangDTO.KandangRelationDTO `json:"kandang,omitempty"`
|
||||||
CreatedUser *userDTO.UserRelationDTO `json:"created_user"`
|
CreatedUser *userDTO.UserRelationDTO `json:"created_user"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
@@ -156,9 +156,9 @@ func ToDailyChecklistListDTO(e entity.DailyChecklist) DailyChecklistListDTO {
|
|||||||
status = *e.Status
|
status = *e.Status
|
||||||
}
|
}
|
||||||
|
|
||||||
var kandang *kandangDTO.KandangGroupRelationDTO
|
var kandang *kandangDTO.KandangRelationDTO
|
||||||
if e.Kandang.Id != 0 {
|
if e.Kandang.Id != 0 {
|
||||||
mapped := kandangDTO.ToKandangGroupRelationDTO(e.Kandang)
|
mapped := kandangDTO.ToKandangRelationDTO(e.Kandang)
|
||||||
kandang = &mapped
|
kandang = &mapped
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ type DailyChecklistListItem struct {
|
|||||||
RejectReason *string
|
RejectReason *string
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
UpdatedAt time.Time
|
UpdatedAt time.Time
|
||||||
Kandang entity.KandangGroup
|
Kandang entity.Kandang
|
||||||
TotalPhase int
|
TotalPhase int
|
||||||
TotalActivity int
|
TotalActivity int
|
||||||
Progress int
|
Progress int
|
||||||
@@ -142,7 +142,7 @@ func (s dailyChecklistService) ensureChecklistAccess(c *fiber.Ctx, checklistID u
|
|||||||
|
|
||||||
db := s.Repository.DB().WithContext(c.Context()).
|
db := s.Repository.DB().WithContext(c.Context()).
|
||||||
Table("daily_checklists dc").
|
Table("daily_checklists dc").
|
||||||
Joins("JOIN kandang_groups k ON k.id = dc.kandang_id").
|
Joins("JOIN kandangs k ON k.id = dc.kandang_id").
|
||||||
Joins("JOIN locations loc ON loc.id = k.location_id").
|
Joins("JOIN locations loc ON loc.id = k.location_id").
|
||||||
Joins("JOIN areas a ON a.id = loc.area_id").
|
Joins("JOIN areas a ON a.id = loc.area_id").
|
||||||
Where("dc.id = ?", checklistID)
|
Where("dc.id = ?", checklistID)
|
||||||
@@ -168,7 +168,7 @@ func (s dailyChecklistService) ensureKandangAccess(c *fiber.Ctx, kandangID uint)
|
|||||||
}
|
}
|
||||||
|
|
||||||
db := s.Repository.DB().WithContext(c.Context()).
|
db := s.Repository.DB().WithContext(c.Context()).
|
||||||
Table("kandang_groups k").
|
Table("kandangs k").
|
||||||
Joins("JOIN locations loc ON loc.id = k.location_id").
|
Joins("JOIN locations loc ON loc.id = k.location_id").
|
||||||
Joins("JOIN areas a ON a.id = loc.area_id").
|
Joins("JOIN areas a ON a.id = loc.area_id").
|
||||||
Where("k.id = ?", kandangID)
|
Where("k.id = ?", kandangID)
|
||||||
@@ -196,7 +196,7 @@ func (s dailyChecklistService) ensureTaskAccess(c *fiber.Ctx, taskID uint) error
|
|||||||
db := s.Repository.DB().WithContext(c.Context()).
|
db := s.Repository.DB().WithContext(c.Context()).
|
||||||
Table("daily_checklist_activity_tasks t").
|
Table("daily_checklist_activity_tasks t").
|
||||||
Joins("JOIN daily_checklists dc ON dc.id = t.checklist_id").
|
Joins("JOIN daily_checklists dc ON dc.id = t.checklist_id").
|
||||||
Joins("JOIN kandang_groups k ON k.id = dc.kandang_id").
|
Joins("JOIN kandangs k ON k.id = dc.kandang_id").
|
||||||
Joins("JOIN locations loc ON loc.id = k.location_id").
|
Joins("JOIN locations loc ON loc.id = k.location_id").
|
||||||
Joins("JOIN areas a ON a.id = loc.area_id").
|
Joins("JOIN areas a ON a.id = loc.area_id").
|
||||||
Where("t.id = ?", taskID)
|
Where("t.id = ?", taskID)
|
||||||
@@ -225,7 +225,7 @@ func (s dailyChecklistService) GetAll(c *fiber.Ctx, params *validation.Query) ([
|
|||||||
|
|
||||||
db := s.Repository.DB().WithContext(c.Context()).
|
db := s.Repository.DB().WithContext(c.Context()).
|
||||||
Table("daily_checklists dc").
|
Table("daily_checklists dc").
|
||||||
Joins("JOIN kandang_groups k ON k.id = dc.kandang_id").
|
Joins("JOIN kandangs k ON k.id = dc.kandang_id").
|
||||||
Joins("JOIN locations loc ON loc.id = k.location_id").
|
Joins("JOIN locations loc ON loc.id = k.location_id").
|
||||||
Joins("JOIN areas a ON a.id = loc.area_id")
|
Joins("JOIN areas a ON a.id = loc.area_id")
|
||||||
|
|
||||||
@@ -341,9 +341,9 @@ func (s dailyChecklistService) GetAll(c *fiber.Ctx, params *validation.Query) ([
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
kandangMap := make(map[uint]entity.KandangGroup)
|
kandangMap := make(map[uint]entity.Kandang)
|
||||||
if len(kandangIDs) > 0 {
|
if len(kandangIDs) > 0 {
|
||||||
var kandangs []entity.KandangGroup
|
var kandangs []entity.Kandang
|
||||||
if err := s.Repository.DB().WithContext(c.Context()).
|
if err := s.Repository.DB().WithContext(c.Context()).
|
||||||
Where("id IN ?", kandangIDs).
|
Where("id IN ?", kandangIDs).
|
||||||
Preload("Location").
|
Preload("Location").
|
||||||
@@ -1019,7 +1019,7 @@ func (s dailyChecklistService) GetSummary(c *fiber.Ctx, params *validation.Summa
|
|||||||
MAX(a.updated_at) AS last_activity`).
|
MAX(a.updated_at) AS last_activity`).
|
||||||
Joins("JOIN daily_checklist_activity_tasks t ON t.id = a.task_id").
|
Joins("JOIN daily_checklist_activity_tasks t ON t.id = a.task_id").
|
||||||
Joins("JOIN daily_checklists d ON d.id = t.checklist_id").
|
Joins("JOIN daily_checklists d ON d.id = t.checklist_id").
|
||||||
Joins("JOIN kandang_groups k ON k.id = d.kandang_id").
|
Joins("JOIN kandangs k ON k.id = d.kandang_id").
|
||||||
Joins("JOIN employees e ON e.id = a.employee_id").
|
Joins("JOIN employees e ON e.id = a.employee_id").
|
||||||
Joins("JOIN locations loc ON loc.id = k.location_id").
|
Joins("JOIN locations loc ON loc.id = k.location_id").
|
||||||
Joins("JOIN areas ar ON ar.id = loc.area_id").
|
Joins("JOIN areas ar ON ar.id = loc.area_id").
|
||||||
@@ -1092,7 +1092,7 @@ func (s dailyChecklistService) GetReport(c *fiber.Ctx, params *validation.Report
|
|||||||
Joins("JOIN daily_checklist_activity_tasks dcat ON dcat.id = dca.task_id").
|
Joins("JOIN daily_checklist_activity_tasks dcat ON dcat.id = dca.task_id").
|
||||||
Joins("JOIN daily_checklists dc ON dc.id = dcat.checklist_id").
|
Joins("JOIN daily_checklists dc ON dc.id = dcat.checklist_id").
|
||||||
Joins("JOIN employees e ON e.id = dca.employee_id").
|
Joins("JOIN employees e ON e.id = dca.employee_id").
|
||||||
Joins("JOIN kandang_groups k ON k.id = dc.kandang_id").
|
Joins("JOIN kandangs k ON k.id = dc.kandang_id").
|
||||||
Joins("JOIN locations loc ON loc.id = k.location_id").
|
Joins("JOIN locations loc ON loc.id = k.location_id").
|
||||||
Joins("JOIN areas a ON a.id = loc.area_id").
|
Joins("JOIN areas a ON a.id = loc.area_id").
|
||||||
Joins("JOIN phases p ON p.id = dcat.phase_id").
|
Joins("JOIN phases p ON p.id = dcat.phase_id").
|
||||||
|
|||||||
@@ -69,19 +69,23 @@ func (r *ExpenseRealizationRepositoryImpl) GetClosingOverhead(ctx context.Contex
|
|||||||
Joins("JOIN expense_nonstocks ON expense_nonstocks.id = expense_realizations.expense_nonstock_id").
|
Joins("JOIN expense_nonstocks ON expense_nonstocks.id = expense_realizations.expense_nonstock_id").
|
||||||
Joins("JOIN expenses ON expenses.id = expense_nonstocks.expense_id").
|
Joins("JOIN expenses ON expenses.id = expense_nonstocks.expense_id").
|
||||||
Joins("LEFT JOIN project_flock_kandangs ON project_flock_kandangs.id = expense_nonstocks.project_flock_kandang_id").
|
Joins("LEFT JOIN project_flock_kandangs ON project_flock_kandangs.id = expense_nonstocks.project_flock_kandang_id").
|
||||||
|
Joins("LEFT JOIN kandangs ON kandangs.id = expense_nonstocks.kandang_id").
|
||||||
Where("expenses.realization_date IS NOT NULL").
|
Where("expenses.realization_date IS NOT NULL").
|
||||||
Where("expenses.category = ?", "BOP")
|
Where("expenses.category = ?", "BOP")
|
||||||
|
|
||||||
if projectFlockKandangID != nil {
|
if projectFlockKandangID != nil {
|
||||||
db = db.Where(`(
|
db = db.Where(`(
|
||||||
expense_nonstocks.project_flock_kandang_id = ? OR
|
expense_nonstocks.project_flock_kandang_id = ? OR
|
||||||
|
(expense_nonstocks.kandang_id = (SELECT kandang_id FROM project_flock_kandangs WHERE id = ?) AND
|
||||||
|
expense_nonstocks.project_flock_kandang_id IS NULL) OR
|
||||||
(expenses.project_flock_id IS NOT NULL AND expenses.project_flock_id::jsonb @> ?::jsonb)
|
(expenses.project_flock_id IS NOT NULL AND expenses.project_flock_id::jsonb @> ?::jsonb)
|
||||||
)`, *projectFlockKandangID, fmt.Sprintf("[%d]", projectFlockID))
|
)`, *projectFlockKandangID, *projectFlockKandangID, fmt.Sprintf("[%d]", projectFlockID))
|
||||||
} else {
|
} else {
|
||||||
db = db.Where(`(
|
db = db.Where(`(
|
||||||
project_flock_kandangs.project_flock_id = ? OR
|
project_flock_kandangs.project_flock_id = ? OR
|
||||||
|
kandangs.id IN (SELECT kandang_id FROM project_flock_kandangs WHERE project_flock_id = ?) OR
|
||||||
(expenses.project_flock_id IS NOT NULL AND expenses.project_flock_id::jsonb @> ?::jsonb)
|
(expenses.project_flock_id IS NOT NULL AND expenses.project_flock_id::jsonb @> ?::jsonb)
|
||||||
)`, projectFlockID, fmt.Sprintf("[%d]", projectFlockID))
|
)`, projectFlockID, projectFlockID, fmt.Sprintf("[%d]", projectFlockID))
|
||||||
}
|
}
|
||||||
|
|
||||||
err := db.Find(&realizations).Error
|
err := db.Find(&realizations).Error
|
||||||
|
|||||||
@@ -103,3 +103,22 @@ func (u *AdjustmentController) GetOne(c *fiber.Ctx) error {
|
|||||||
Data: dto.ToAdjustmentDetailDTO(stockLog),
|
Data: dto.ToAdjustmentDetailDTO(stockLog),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *AdjustmentController) DeleteOne(c *fiber.Ctx) error {
|
||||||
|
param := c.Params("id")
|
||||||
|
id, err := strconv.Atoi(param)
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := u.AdjustmentService.DeleteOne(c, uint(id)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusOK).
|
||||||
|
JSON(response.Common{
|
||||||
|
Code: fiber.StatusOK,
|
||||||
|
Status: "success",
|
||||||
|
Message: "Delete adjustment successfully",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
+216
-27
@@ -2,12 +2,12 @@ package repositories
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"gorm.io/gorm/clause"
|
"gorm.io/gorm/clause"
|
||||||
)
|
)
|
||||||
@@ -15,9 +15,19 @@ import (
|
|||||||
type AdjustmentStockRepository interface {
|
type AdjustmentStockRepository interface {
|
||||||
CreateOne(ctx context.Context, data *entity.AdjustmentStock, modifier func(*gorm.DB) *gorm.DB) error
|
CreateOne(ctx context.Context, data *entity.AdjustmentStock, modifier func(*gorm.DB) *gorm.DB) error
|
||||||
GetByID(ctx context.Context, id uint, modifier func(*gorm.DB) *gorm.DB) (*entity.AdjustmentStock, error)
|
GetByID(ctx context.Context, id uint, modifier func(*gorm.DB) *gorm.DB) (*entity.AdjustmentStock, error)
|
||||||
|
GetByIDForUpdate(ctx context.Context, id uint) (*entity.AdjustmentStock, error)
|
||||||
FindKandangIDByProjectFlockKandangID(ctx context.Context, projectFlockKandangID uint) (uint, error)
|
FindKandangIDByProjectFlockKandangID(ctx context.Context, projectFlockKandangID uint) (uint, error)
|
||||||
|
FindProductIDByProductWarehouseID(ctx context.Context, productWarehouseID uint) (uint, error)
|
||||||
FindRoutesByFunctionCode(ctx context.Context, productID uint, functionCode string) ([]AdjustmentRouteResolution, error)
|
FindRoutesByFunctionCode(ctx context.Context, productID uint, functionCode string) ([]AdjustmentRouteResolution, error)
|
||||||
FindOverconsumeRule(ctx context.Context, lane, flagGroupCode, functionCode string) (*bool, error)
|
LoadDownstreamDependencies(ctx context.Context, stockableType string, stockableIDs []uint) ([]AdjustmentDownstreamDependency, error)
|
||||||
|
FindAyamSourceProductWarehouse(ctx context.Context, warehouseID uint, projectFlockKandangID uint) (*entity.ProductWarehouse, error)
|
||||||
|
IsAyamProduct(ctx context.Context, productID uint) (bool, error)
|
||||||
|
CountActiveConsumeAllocationsByUsable(ctx context.Context, usableType string, usableID uint) (int64, error)
|
||||||
|
UpdateTotalQty(ctx context.Context, id uint, qty float64) error
|
||||||
|
UpdatePairedAdjustmentID(ctx context.Context, id uint, pairedID uint) error
|
||||||
|
DeleteStockLogsByAdjustmentID(ctx context.Context, adjustmentID uint) error
|
||||||
|
DeleteAdjustmentByID(ctx context.Context, id uint) error
|
||||||
|
ResyncProjectFlockPopulationUsage(ctx context.Context, projectFlockKandangID uint) error
|
||||||
FindHistory(ctx context.Context, filter AdjustmentHistoryFilter, modifier func(*gorm.DB) *gorm.DB) ([]*entity.AdjustmentStock, int64, error)
|
FindHistory(ctx context.Context, filter AdjustmentHistoryFilter, modifier func(*gorm.DB) *gorm.DB) ([]*entity.AdjustmentStock, int64, error)
|
||||||
WithTx(tx *gorm.DB) AdjustmentStockRepository
|
WithTx(tx *gorm.DB) AdjustmentStockRepository
|
||||||
DB() *gorm.DB
|
DB() *gorm.DB
|
||||||
@@ -44,6 +54,13 @@ type AdjustmentHistoryFilter struct {
|
|||||||
Limit int
|
Limit int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AdjustmentDownstreamDependency struct {
|
||||||
|
UsableType string `gorm:"column:usable_type"`
|
||||||
|
UsableID uint64 `gorm:"column:usable_id"`
|
||||||
|
FunctionCode string `gorm:"column:function_code"`
|
||||||
|
FlagGroupCode string `gorm:"column:flag_group_code"`
|
||||||
|
}
|
||||||
|
|
||||||
type adjustmentStockRepositoryImpl struct {
|
type adjustmentStockRepositoryImpl struct {
|
||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
}
|
}
|
||||||
@@ -73,6 +90,17 @@ func (r *adjustmentStockRepositoryImpl) GetByID(ctx context.Context, id uint, mo
|
|||||||
return &record, nil
|
return &record, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *adjustmentStockRepositoryImpl) GetByIDForUpdate(ctx context.Context, id uint) (*entity.AdjustmentStock, error) {
|
||||||
|
var record entity.AdjustmentStock
|
||||||
|
if err := r.db.WithContext(ctx).
|
||||||
|
Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||||
|
Where("id = ?", id).
|
||||||
|
Take(&record).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &record, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *adjustmentStockRepositoryImpl) FindKandangIDByProjectFlockKandangID(ctx context.Context, projectFlockKandangID uint) (uint, error) {
|
func (r *adjustmentStockRepositoryImpl) FindKandangIDByProjectFlockKandangID(ctx context.Context, projectFlockKandangID uint) (uint, error) {
|
||||||
type pfkRow struct {
|
type pfkRow struct {
|
||||||
KandangID uint `gorm:"column:kandang_id"`
|
KandangID uint `gorm:"column:kandang_id"`
|
||||||
@@ -91,6 +119,21 @@ func (r *adjustmentStockRepositoryImpl) FindKandangIDByProjectFlockKandangID(ctx
|
|||||||
return pfk.KandangID, nil
|
return pfk.KandangID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *adjustmentStockRepositoryImpl) FindProductIDByProductWarehouseID(ctx context.Context, productWarehouseID uint) (uint, error) {
|
||||||
|
type productRow struct {
|
||||||
|
ProductID uint `gorm:"column:product_id"`
|
||||||
|
}
|
||||||
|
var row productRow
|
||||||
|
if err := r.db.WithContext(ctx).
|
||||||
|
Table("product_warehouses").
|
||||||
|
Select("product_id").
|
||||||
|
Where("id = ?", productWarehouseID).
|
||||||
|
Take(&row).Error; err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return row.ProductID, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *adjustmentStockRepositoryImpl) FindRoutesByFunctionCode(
|
func (r *adjustmentStockRepositoryImpl) FindRoutesByFunctionCode(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
productID uint,
|
productID uint,
|
||||||
@@ -122,37 +165,183 @@ func (r *adjustmentStockRepositoryImpl) FindRoutesByFunctionCode(
|
|||||||
return rows, nil
|
return rows, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *adjustmentStockRepositoryImpl) FindOverconsumeRule(
|
func (r *adjustmentStockRepositoryImpl) LoadDownstreamDependencies(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
lane string,
|
stockableType string,
|
||||||
flagGroupCode string,
|
stockableIDs []uint,
|
||||||
functionCode string,
|
) ([]AdjustmentDownstreamDependency, error) {
|
||||||
) (*bool, error) {
|
if strings.TrimSpace(stockableType) == "" || len(stockableIDs) == 0 {
|
||||||
type selectedRow struct {
|
|
||||||
AllowOverconsume bool `gorm:"column:allow_overconsume"`
|
|
||||||
}
|
|
||||||
|
|
||||||
var selected selectedRow
|
|
||||||
err := r.db.WithContext(ctx).
|
|
||||||
Table("fifo_stock_v2_overconsume_rules").
|
|
||||||
Select("allow_overconsume").
|
|
||||||
Where("is_active = TRUE").
|
|
||||||
Where("lane = ?", lane).
|
|
||||||
Where("(flag_group_code IS NULL OR flag_group_code = ?)", flagGroupCode).
|
|
||||||
Where("(function_code IS NULL OR function_code = ?)", functionCode).
|
|
||||||
Order("CASE WHEN flag_group_code IS NULL THEN 1 ELSE 0 END ASC").
|
|
||||||
Order("CASE WHEN function_code IS NULL THEN 1 ELSE 0 END ASC").
|
|
||||||
Order("priority ASC, id ASC").
|
|
||||||
Limit(1).
|
|
||||||
Take(&selected).Error
|
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var rows []AdjustmentDownstreamDependency
|
||||||
|
err := r.db.WithContext(ctx).
|
||||||
|
Table("stock_allocations").
|
||||||
|
Select("usable_type, usable_id, COALESCE(function_code,'') AS function_code, COALESCE(flag_group_code,'') AS flag_group_code").
|
||||||
|
Where("stockable_type = ?", strings.ToUpper(strings.TrimSpace(stockableType))).
|
||||||
|
Where("stockable_id IN ?", stockableIDs).
|
||||||
|
Where("status = ?", entity.StockAllocationStatusActive).
|
||||||
|
Where("allocation_purpose = ?", entity.StockAllocationPurposeConsume).
|
||||||
|
Where("deleted_at IS NULL").
|
||||||
|
Where(
|
||||||
|
"(usable_type <> ? OR EXISTS (SELECT 1 FROM project_chickins pc WHERE pc.id = stock_allocations.usable_id AND pc.deleted_at IS NULL))",
|
||||||
|
"PROJECT_CHICKIN",
|
||||||
|
).
|
||||||
|
Group("usable_type, usable_id, function_code, flag_group_code").
|
||||||
|
Scan(&rows).Error
|
||||||
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &selected.AllowOverconsume, nil
|
return rows, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *adjustmentStockRepositoryImpl) FindAyamSourceProductWarehouse(
|
||||||
|
ctx context.Context,
|
||||||
|
warehouseID uint,
|
||||||
|
projectFlockKandangID uint,
|
||||||
|
) (*entity.ProductWarehouse, error) {
|
||||||
|
var sourcePW entity.ProductWarehouse
|
||||||
|
err := r.db.WithContext(ctx).
|
||||||
|
Model(&entity.ProductWarehouse{}).
|
||||||
|
Where("project_flock_kandang_id = ?", projectFlockKandangID).
|
||||||
|
Where(`
|
||||||
|
EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM flags f
|
||||||
|
JOIN fifo_stock_v2_flag_members fm ON fm.flag_name = f.name AND fm.is_active = TRUE
|
||||||
|
WHERE f.flagable_type = ?
|
||||||
|
AND f.flagable_id = product_warehouses.product_id
|
||||||
|
AND fm.flag_group_code = ?
|
||||||
|
)
|
||||||
|
`, entity.FlagableTypeProduct, "AYAM").
|
||||||
|
Order(gorm.Expr("CASE WHEN warehouse_id = ? THEN 0 ELSE 1 END ASC", warehouseID)).
|
||||||
|
Order("id ASC").
|
||||||
|
Take(&sourcePW).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &sourcePW, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *adjustmentStockRepositoryImpl) IsAyamProduct(ctx context.Context, productID uint) (bool, error) {
|
||||||
|
if productID == 0 {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var count int64
|
||||||
|
if err := r.db.WithContext(ctx).
|
||||||
|
Table("flags f").
|
||||||
|
Joins("JOIN fifo_stock_v2_flag_members fm ON fm.flag_name = f.name AND fm.flag_group_code = ? AND fm.is_active = TRUE", "AYAM").
|
||||||
|
Where("f.flagable_type = ?", entity.FlagableTypeProduct).
|
||||||
|
Where("f.flagable_id = ?", productID).
|
||||||
|
Count(&count).Error; err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return count > 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *adjustmentStockRepositoryImpl) CountActiveConsumeAllocationsByUsable(
|
||||||
|
ctx context.Context,
|
||||||
|
usableType string,
|
||||||
|
usableID uint,
|
||||||
|
) (int64, error) {
|
||||||
|
if strings.TrimSpace(usableType) == "" || usableID == 0 {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var count int64
|
||||||
|
err := r.db.WithContext(ctx).
|
||||||
|
Table("stock_allocations").
|
||||||
|
Where("usable_type = ?", strings.ToUpper(strings.TrimSpace(usableType))).
|
||||||
|
Where("usable_id = ?", usableID).
|
||||||
|
Where("status = ?", entity.StockAllocationStatusActive).
|
||||||
|
Where("allocation_purpose = ?", entity.StockAllocationPurposeConsume).
|
||||||
|
Where("deleted_at IS NULL").
|
||||||
|
Count(&count).Error
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return count, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *adjustmentStockRepositoryImpl) UpdateTotalQty(ctx context.Context, id uint, qty float64) error {
|
||||||
|
return r.db.WithContext(ctx).
|
||||||
|
Model(&entity.AdjustmentStock{}).
|
||||||
|
Where("id = ?", id).
|
||||||
|
Update("total_qty", qty).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *adjustmentStockRepositoryImpl) UpdatePairedAdjustmentID(ctx context.Context, id uint, pairedID uint) error {
|
||||||
|
return r.db.WithContext(ctx).
|
||||||
|
Model(&entity.AdjustmentStock{}).
|
||||||
|
Where("id = ?", id).
|
||||||
|
Update("paired_adjustment_id", pairedID).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *adjustmentStockRepositoryImpl) DeleteStockLogsByAdjustmentID(ctx context.Context, adjustmentID uint) error {
|
||||||
|
return r.db.WithContext(ctx).
|
||||||
|
Where("loggable_type = ? AND loggable_id = ?", string(utils.StockLogTypeAdjustment), adjustmentID).
|
||||||
|
Delete(&entity.StockLog{}).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *adjustmentStockRepositoryImpl) DeleteAdjustmentByID(ctx context.Context, id uint) error {
|
||||||
|
return r.db.WithContext(ctx).
|
||||||
|
Where("id = ?", id).
|
||||||
|
Delete(&entity.AdjustmentStock{}).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *adjustmentStockRepositoryImpl) ResyncProjectFlockPopulationUsage(ctx context.Context, 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'
|
||||||
|
AND allocation_purpose = 'CONSUME'
|
||||||
|
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.allocation_purpose = 'CONSUME'
|
||||||
|
AND sa.stockable_id = p.id
|
||||||
|
)
|
||||||
|
`
|
||||||
|
|
||||||
|
db := r.db.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 *adjustmentStockRepositoryImpl) FindHistory(
|
func (r *adjustmentStockRepositoryImpl) FindHistory(
|
||||||
|
|||||||
@@ -15,8 +15,9 @@ func AdjustmentRoutes(v1 fiber.Router, u user.UserService, s adjustment.Adjustme
|
|||||||
route := v1.Group("/adjustments")
|
route := v1.Group("/adjustments")
|
||||||
route.Use(m.Auth(u))
|
route.Use(m.Auth(u))
|
||||||
// Standard CRUD routes following master data pattern
|
// Standard CRUD routes following master data pattern
|
||||||
route.Get("/",m.RequirePermissions(m.P_AdjustmentGetAll), ctrl.AdjustmentHistory) // Get all with pagination and filters
|
route.Get("/", m.RequirePermissions(m.P_AdjustmentGetAll), ctrl.AdjustmentHistory) // Get all with pagination and filters
|
||||||
route.Post("/",m.RequirePermissions(m.P_AdjustmentCreate), ctrl.Adjustment) // Create adjustment
|
route.Post("/", m.RequirePermissions(m.P_AdjustmentCreate), ctrl.Adjustment) // Create adjustment
|
||||||
route.Get("/:id",m.RequirePermissions(m.P_AdjustmentGetOne), ctrl.GetOne)
|
route.Get("/:id", m.RequirePermissions(m.P_AdjustmentGetOne), ctrl.GetOne)
|
||||||
|
route.Delete("/:id", m.RequirePermissions(m.P_AdjustmentDeleteOne), ctrl.DeleteOne)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/go-playground/validator/v10"
|
"github.com/go-playground/validator/v10"
|
||||||
@@ -29,6 +30,7 @@ import (
|
|||||||
type AdjustmentService interface {
|
type AdjustmentService interface {
|
||||||
Adjustment(ctx *fiber.Ctx, req *validation.Create) (*entity.AdjustmentStock, error)
|
Adjustment(ctx *fiber.Ctx, req *validation.Create) (*entity.AdjustmentStock, error)
|
||||||
GetOne(ctx *fiber.Ctx, id uint) (*entity.AdjustmentStock, error)
|
GetOne(ctx *fiber.Ctx, id uint) (*entity.AdjustmentStock, error)
|
||||||
|
DeleteOne(ctx *fiber.Ctx, id uint) error
|
||||||
AdjustmentHistory(ctx *fiber.Ctx, query *validation.Query) ([]*entity.AdjustmentStock, int64, error)
|
AdjustmentHistory(ctx *fiber.Ctx, query *validation.Query) ([]*entity.AdjustmentStock, int64, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,7 +50,6 @@ type adjustmentService struct {
|
|||||||
const (
|
const (
|
||||||
adjustmentLaneStockable = "STOCKABLE"
|
adjustmentLaneStockable = "STOCKABLE"
|
||||||
adjustmentLaneUsable = "USABLE"
|
adjustmentLaneUsable = "USABLE"
|
||||||
flagGroupAyam = "AYAM"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewAdjustmentService(
|
func NewAdjustmentService(
|
||||||
@@ -76,7 +77,12 @@ func NewAdjustmentService(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *adjustmentService) withRelations(db *gorm.DB) *gorm.DB {
|
func (s *adjustmentService) GetOne(c *fiber.Ctx, id uint) (*entity.AdjustmentStock, error) {
|
||||||
|
if err := m.EnsureStockLogAccess(c, s.StockLogsRepository.DB(), id); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
adjustmentStock, err := s.AdjustmentStockRepository.GetByID(c.Context(), id, func(db *gorm.DB) *gorm.DB {
|
||||||
return db.
|
return db.
|
||||||
Preload("ProductWarehouse").
|
Preload("ProductWarehouse").
|
||||||
Preload("ProductWarehouse.Product").
|
Preload("ProductWarehouse.Product").
|
||||||
@@ -85,14 +91,7 @@ func (s *adjustmentService) withRelations(db *gorm.DB) *gorm.DB {
|
|||||||
Preload("ProductWarehouse.ProjectFlockKandang").
|
Preload("ProductWarehouse.ProjectFlockKandang").
|
||||||
Preload("ProductWarehouse.ProjectFlockKandang.ProjectFlock").
|
Preload("ProductWarehouse.ProjectFlockKandang.ProjectFlock").
|
||||||
Preload("StockLog.CreatedUser")
|
Preload("StockLog.CreatedUser")
|
||||||
}
|
})
|
||||||
|
|
||||||
func (s *adjustmentService) GetOne(c *fiber.Ctx, id uint) (*entity.AdjustmentStock, error) {
|
|
||||||
if err := m.EnsureStockLogAccess(c, s.StockLogsRepository.DB(), id); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
adjustmentStock, err := s.AdjustmentStockRepository.GetByID(c.Context(), id, s.withRelations)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
return nil, fiber.NewError(fiber.StatusNotFound, "Adjustment not found")
|
return nil, fiber.NewError(fiber.StatusNotFound, "Adjustment not found")
|
||||||
@@ -104,6 +103,250 @@ func (s *adjustmentService) GetOne(c *fiber.Ctx, id uint) (*entity.AdjustmentSto
|
|||||||
return adjustmentStock, nil
|
return adjustmentStock, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *adjustmentService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||||
|
if id == 0 {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid adjustment id")
|
||||||
|
}
|
||||||
|
if s.FifoStockV2Svc == nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "FIFO v2 service is not available")
|
||||||
|
}
|
||||||
|
if err := m.EnsureStockLogAccess(c, s.StockLogsRepository.DB(), id); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := c.Context()
|
||||||
|
actorID, err := m.ActorIDFromContext(c)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.StockLogsRepository.DB().WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||||
|
adjustments, err := s.collectAdjustmentsForDelete(ctx, tx, id)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, item := range adjustments {
|
||||||
|
if err := s.deleteSingleAdjustmentInTx(ctx, tx, item, actorID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *adjustmentService) collectAdjustmentsForDelete(ctx context.Context, tx *gorm.DB, id uint) ([]entity.AdjustmentStock, error) {
|
||||||
|
repoTx := s.AdjustmentStockRepository.WithTx(tx)
|
||||||
|
adjustment, err := repoTx.GetByIDForUpdate(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, fiber.NewError(fiber.StatusNotFound, "Adjustment not found")
|
||||||
|
}
|
||||||
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to load adjustment")
|
||||||
|
}
|
||||||
|
|
||||||
|
adjustments := []entity.AdjustmentStock{*adjustment}
|
||||||
|
leftPairCode := utils.NormalizeUpper(adjustment.FunctionCode)
|
||||||
|
isDepletionCode := leftPairCode == string(utils.AdjustmentTransactionSubtypeRecordingDepletionIn) ||
|
||||||
|
leftPairCode == string(utils.AdjustmentTransactionSubtypeRecordingDepletionOut)
|
||||||
|
if !isDepletionCode {
|
||||||
|
return adjustments, nil
|
||||||
|
}
|
||||||
|
if adjustment.PairedAdjustmentId == nil || *adjustment.PairedAdjustmentId == 0 {
|
||||||
|
return nil, fiber.NewError(
|
||||||
|
fiber.StatusBadRequest,
|
||||||
|
"Adjustment depletion tidak memiliki pasangan valid. Data harus diperbaiki terlebih dahulu untuk mencegah orphan.",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pair, err := repoTx.GetByIDForUpdate(ctx, *adjustment.PairedAdjustmentId)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, fiber.NewError(
|
||||||
|
fiber.StatusBadRequest,
|
||||||
|
fmt.Sprintf("Pasangan adjustment depletion (%d) tidak ditemukan. Data harus diperbaiki terlebih dahulu untuk mencegah orphan.", *adjustment.PairedAdjustmentId),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to load paired adjustment")
|
||||||
|
}
|
||||||
|
rightPairCode := utils.NormalizeUpper(pair.FunctionCode)
|
||||||
|
isPairDepletionCode := rightPairCode == string(utils.AdjustmentTransactionSubtypeRecordingDepletionIn) ||
|
||||||
|
rightPairCode == string(utils.AdjustmentTransactionSubtypeRecordingDepletionOut)
|
||||||
|
if !isPairDepletionCode {
|
||||||
|
return nil, fiber.NewError(
|
||||||
|
fiber.StatusBadRequest,
|
||||||
|
fmt.Sprintf("Pasangan adjustment %d bukan depletion pair yang valid", pair.Id),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if pair.PairedAdjustmentId == nil || *pair.PairedAdjustmentId != adjustment.Id {
|
||||||
|
return nil, fiber.NewError(
|
||||||
|
fiber.StatusBadRequest,
|
||||||
|
fmt.Sprintf("Pasangan adjustment depletion tidak konsisten (%d <-> %d). Perbaiki pairing terlebih dahulu.", adjustment.Id, pair.Id),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
isValidPair := (leftPairCode == string(utils.AdjustmentTransactionSubtypeRecordingDepletionIn) &&
|
||||||
|
rightPairCode == string(utils.AdjustmentTransactionSubtypeRecordingDepletionOut)) ||
|
||||||
|
(leftPairCode == string(utils.AdjustmentTransactionSubtypeRecordingDepletionOut) &&
|
||||||
|
rightPairCode == string(utils.AdjustmentTransactionSubtypeRecordingDepletionIn))
|
||||||
|
if !isValidPair {
|
||||||
|
return nil, fiber.NewError(
|
||||||
|
fiber.StatusBadRequest,
|
||||||
|
fmt.Sprintf("Pasangan function_code depletion tidak valid (%s <-> %s)", adjustment.FunctionCode, pair.FunctionCode),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
adjustments = append(adjustments, *pair)
|
||||||
|
sort.Slice(adjustments, func(i, j int) bool {
|
||||||
|
return adjustments[i].Id < adjustments[j].Id
|
||||||
|
})
|
||||||
|
return adjustments, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *adjustmentService) deleteSingleAdjustmentInTx(
|
||||||
|
ctx context.Context,
|
||||||
|
tx *gorm.DB,
|
||||||
|
adjustment entity.AdjustmentStock,
|
||||||
|
actorID uint,
|
||||||
|
) error {
|
||||||
|
repoTx := s.AdjustmentStockRepository.WithTx(tx)
|
||||||
|
productID, err := repoTx.FindProductIDByProductWarehouseID(ctx, adjustment.ProductWarehouseId)
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to load product warehouse context")
|
||||||
|
}
|
||||||
|
|
||||||
|
routeMeta, err := s.resolveRouteByFunctionCode(ctx, productID, adjustment.FunctionCode)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
isAyamProduct, err := repoTx.IsAyamProduct(ctx, productID)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Errorf("Failed to resolve AYAM flag for product %d: %+v", productID, err)
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to validate product flag")
|
||||||
|
}
|
||||||
|
|
||||||
|
stockLogRepoTx := stockLogsRepo.NewStockLogRepository(tx)
|
||||||
|
notes := fmt.Sprintf("ADJUSTMENT DELETE#%s", utils.NormalizeTrim(adjustment.AdjNumber))
|
||||||
|
|
||||||
|
switch routeMeta.Lane {
|
||||||
|
case adjustmentLaneStockable:
|
||||||
|
deps, allowPending, err := s.resolveAdjustmentDependenciesAndPolicy(
|
||||||
|
ctx,
|
||||||
|
tx,
|
||||||
|
fifo.StockableKeyAdjustmentIn.String(),
|
||||||
|
[]uint{adjustment.Id},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(deps) > 0 && isAyamProduct {
|
||||||
|
return fiber.NewError(
|
||||||
|
fiber.StatusBadRequest,
|
||||||
|
fmt.Sprintf(
|
||||||
|
"Adjustment tidak dapat dihapus karena produk AYAM sudah dipakai transaksi turunan. Dependensi aktif: %s. Alasan block: produk AYAM yang sudah terpakai tidak dapat dihapus.",
|
||||||
|
formatAdjustmentDependencySummary(deps),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if len(deps) > 0 && !allowPending {
|
||||||
|
return fiber.NewError(
|
||||||
|
fiber.StatusBadRequest,
|
||||||
|
fmt.Sprintf(
|
||||||
|
"Adjustment tidak dapat dihapus karena stok adjustment sudah dipakai transaksi turunan. Dependensi aktif: %s. Alasan block: pending disabled by config.",
|
||||||
|
formatAdjustmentDependencySummary(deps),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
oldQty := adjustment.TotalQty
|
||||||
|
if oldQty > 0 {
|
||||||
|
if err := repoTx.UpdateTotalQty(ctx, adjustment.Id, 0); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
asOf := adjustment.CreatedAt
|
||||||
|
if _, err := s.FifoStockV2Svc.Reflow(ctx, common.FifoStockV2ReflowRequest{
|
||||||
|
FlagGroupCode: routeMeta.FlagGroupCode,
|
||||||
|
ProductWarehouseID: adjustment.ProductWarehouseId,
|
||||||
|
AsOf: &asOf,
|
||||||
|
Tx: tx,
|
||||||
|
}); err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Failed to reflow stock via FIFO v2: %v", err))
|
||||||
|
}
|
||||||
|
if err := s.createAdjustmentStockLog(
|
||||||
|
ctx,
|
||||||
|
stockLogRepoTx,
|
||||||
|
adjustment.Id,
|
||||||
|
adjustment.ProductWarehouseId,
|
||||||
|
notes,
|
||||||
|
actorID,
|
||||||
|
0,
|
||||||
|
oldQty,
|
||||||
|
); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case adjustmentLaneUsable:
|
||||||
|
activeBeforeRollback, err := repoTx.CountActiveConsumeAllocationsByUsable(ctx, fifo.UsableKeyAdjustmentOut.String(), adjustment.Id)
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to validate adjustment allocations before rollback")
|
||||||
|
}
|
||||||
|
rollbackRes, err := s.FifoStockV2Svc.Rollback(ctx, common.FifoStockV2RollbackRequest{
|
||||||
|
ProductWarehouseID: adjustment.ProductWarehouseId,
|
||||||
|
Usable: common.FifoStockV2Ref{
|
||||||
|
ID: adjustment.Id,
|
||||||
|
LegacyTypeKey: fifo.UsableKeyAdjustmentOut.String(),
|
||||||
|
},
|
||||||
|
Reason: notes,
|
||||||
|
Tx: tx,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Failed to rollback FIFO v2 adjustment: %v", err))
|
||||||
|
}
|
||||||
|
activeAfterRollback, err := repoTx.CountActiveConsumeAllocationsByUsable(ctx, fifo.UsableKeyAdjustmentOut.String(), adjustment.Id)
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to validate adjustment allocations after rollback")
|
||||||
|
}
|
||||||
|
if activeAfterRollback > 0 {
|
||||||
|
return fiber.NewError(
|
||||||
|
fiber.StatusBadRequest,
|
||||||
|
fmt.Sprintf(
|
||||||
|
"Adjustment tidak dapat dihapus karena masih ada alokasi aktif ADJUSTMENT_OUT=%d (sebelum rollback=%d, sesudah rollback=%d).",
|
||||||
|
adjustment.Id,
|
||||||
|
activeBeforeRollback,
|
||||||
|
activeAfterRollback,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
releasedQty := 0.0
|
||||||
|
if rollbackRes != nil {
|
||||||
|
releasedQty = rollbackRes.ReleasedQty
|
||||||
|
}
|
||||||
|
if releasedQty > 0 {
|
||||||
|
if err := s.createAdjustmentStockLog(
|
||||||
|
ctx,
|
||||||
|
stockLogRepoTx,
|
||||||
|
adjustment.Id,
|
||||||
|
adjustment.ProductWarehouseId,
|
||||||
|
notes,
|
||||||
|
actorID,
|
||||||
|
releasedQty,
|
||||||
|
0,
|
||||||
|
); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "Unsupported adjustment lane")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := repoTx.DeleteStockLogsByAdjustmentID(ctx, adjustment.Id); err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to delete adjustment stock logs")
|
||||||
|
}
|
||||||
|
if err := repoTx.DeleteAdjustmentByID(ctx, adjustment.Id); err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to delete adjustment")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *adjustmentService) Adjustment(c *fiber.Ctx, req *validation.Create) (*entity.AdjustmentStock, error) {
|
func (s *adjustmentService) Adjustment(c *fiber.Ctx, req *validation.Create) (*entity.AdjustmentStock, error) {
|
||||||
if err := s.Validate.Struct(req); err != nil {
|
if err := s.Validate.Struct(req); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -122,12 +365,12 @@ func (s *adjustmentService) Adjustment(c *fiber.Ctx, req *validation.Create) (*e
|
|||||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Quantity must be greater than zero")
|
return nil, fiber.NewError(fiber.StatusBadRequest, "Quantity must be greater than zero")
|
||||||
}
|
}
|
||||||
|
|
||||||
functionCode := strings.ToUpper(strings.TrimSpace(req.TransactionSubtype))
|
functionCode := utils.NormalizeUpper(req.TransactionSubtype)
|
||||||
if functionCode == "" {
|
if functionCode == "" {
|
||||||
functionCode = strings.ToUpper(strings.TrimSpace(req.TransactionSubType))
|
functionCode = utils.NormalizeUpper(req.TransactionSubType)
|
||||||
}
|
}
|
||||||
if functionCode == "" {
|
if functionCode == "" {
|
||||||
functionCode = strings.ToUpper(strings.TrimSpace(req.FunctionCode))
|
functionCode = utils.NormalizeUpper(req.FunctionCode)
|
||||||
}
|
}
|
||||||
if functionCode == "" {
|
if functionCode == "" {
|
||||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Transaction subtype is required")
|
return nil, fiber.NewError(fiber.StatusBadRequest, "Transaction subtype is required")
|
||||||
@@ -144,9 +387,9 @@ func (s *adjustmentService) Adjustment(c *fiber.Ctx, req *validation.Create) (*e
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
note := strings.TrimSpace(req.Notes)
|
note := utils.NormalizeTrim(req.Notes)
|
||||||
if note == "" {
|
if note == "" {
|
||||||
note = strings.TrimSpace(req.Note)
|
note = utils.NormalizeTrim(req.Note)
|
||||||
}
|
}
|
||||||
grandTotal := math.Round((qty*req.Price)*1000) / 1000
|
grandTotal := math.Round((qty*req.Price)*1000) / 1000
|
||||||
|
|
||||||
@@ -228,8 +471,11 @@ func (s *adjustmentService) Adjustment(c *fiber.Ctx, req *validation.Create) (*e
|
|||||||
return fiber.NewError(fiber.StatusInternalServerError, "FIFO v2 service is not available")
|
return fiber.NewError(fiber.StatusInternalServerError, "FIFO v2 service is not available")
|
||||||
}
|
}
|
||||||
|
|
||||||
sourcePW, err := s.resolveAyamSourceProductWarehouse(ctx, tx, warehouseID, *projectFlockKandangID)
|
sourcePW, err := adjustmentStockRepoTX.FindAyamSourceProductWarehouse(ctx, warehouseID, *projectFlockKandangID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "Produk sumber AYAM pada project flock kandang yang sama tidak ditemukan")
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := common.EnsureProjectFlockNotClosedForProductWarehouses(
|
if err := common.EnsureProjectFlockNotClosedForProductWarehouses(
|
||||||
@@ -285,6 +531,14 @@ func (s *adjustmentService) Adjustment(c *fiber.Ctx, req *validation.Create) (*e
|
|||||||
if err := adjustmentStockRepoTX.CreateOne(ctx, destinationAdjustment, nil); err != nil {
|
if err := adjustmentStockRepoTX.CreateOne(ctx, destinationAdjustment, nil); err != nil {
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create depletion destination adjustment stock record")
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create depletion destination adjustment stock record")
|
||||||
}
|
}
|
||||||
|
if err := adjustmentStockRepoTX.UpdatePairedAdjustmentID(ctx, sourceAdjustment.Id, destinationAdjustment.Id); err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to link depletion source adjustment pair")
|
||||||
|
}
|
||||||
|
if err := adjustmentStockRepoTX.UpdatePairedAdjustmentID(ctx, destinationAdjustment.Id, sourceAdjustment.Id); err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to link depletion destination adjustment pair")
|
||||||
|
}
|
||||||
|
sourceAdjustment.PairedAdjustmentId = &destinationAdjustment.Id
|
||||||
|
destinationAdjustment.PairedAdjustmentId = &sourceAdjustment.Id
|
||||||
|
|
||||||
sourceAsOf := sourceAdjustment.CreatedAt
|
sourceAsOf := sourceAdjustment.CreatedAt
|
||||||
if _, err := s.FifoStockV2Svc.Reflow(ctx, common.FifoStockV2ReflowRequest{
|
if _, err := s.FifoStockV2Svc.Reflow(ctx, common.FifoStockV2ReflowRequest{
|
||||||
@@ -326,7 +580,7 @@ func (s *adjustmentService) Adjustment(c *fiber.Ctx, req *validation.Create) (*e
|
|||||||
); err != nil {
|
); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := s.resyncProjectFlockPopulationUsage(ctx, tx, *projectFlockKandangID); err != nil {
|
if err := adjustmentStockRepoTX.ResyncProjectFlockPopulationUsage(ctx, *projectFlockKandangID); err != nil {
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to resync project flock population usage")
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to resync project flock population usage")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -502,29 +756,80 @@ func (s *adjustmentService) resolveRouteByFunctionCode(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *adjustmentService) resolveOverconsumePolicy(
|
func (s *adjustmentService) resolveAdjustmentDependenciesAndPolicy(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
route *adjustmentStockRepo.AdjustmentRouteResolution,
|
tx *gorm.DB,
|
||||||
) (bool, error) {
|
stockableType string,
|
||||||
if route == nil {
|
stockableIDs []uint,
|
||||||
return false, fmt.Errorf("route is required")
|
) ([]adjustmentStockRepo.AdjustmentDownstreamDependency, bool, error) {
|
||||||
}
|
deps, err := s.AdjustmentStockRepository.WithTx(tx).LoadDownstreamDependencies(ctx, stockableType, stockableIDs)
|
||||||
|
|
||||||
defaultValue := route.AllowPendingDefault
|
|
||||||
selected, err := s.AdjustmentStockRepository.FindOverconsumeRule(
|
|
||||||
ctx,
|
|
||||||
route.Lane,
|
|
||||||
route.FlagGroupCode,
|
|
||||||
route.FunctionCode,
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
s.Log.Errorf("Failed to load downstream adjustment dependencies: %+v", err)
|
||||||
|
return nil, false, fiber.NewError(fiber.StatusInternalServerError, "Failed to validate downstream adjustment dependencies")
|
||||||
}
|
}
|
||||||
if selected == nil {
|
if len(deps) == 0 {
|
||||||
return defaultValue, nil
|
return nil, true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return *selected, nil
|
allowPending := true
|
||||||
|
for _, dep := range deps {
|
||||||
|
policy, policyErr := common.ResolveFifoPendingPolicy(ctx, tx, common.FifoPendingPolicyInput{
|
||||||
|
Lane: adjustmentLaneUsable,
|
||||||
|
FlagGroupCode: dep.FlagGroupCode,
|
||||||
|
FunctionCode: dep.FunctionCode,
|
||||||
|
LegacyTypeKey: dep.UsableType,
|
||||||
|
})
|
||||||
|
if policyErr != nil {
|
||||||
|
s.Log.Errorf("Failed to resolve FIFO pending policy for adjustment dependency: %+v", policyErr)
|
||||||
|
return nil, false, fiber.NewError(fiber.StatusInternalServerError, "Failed to read FIFO v2 configuration")
|
||||||
|
}
|
||||||
|
if !policy.Found || !policy.AllowPending {
|
||||||
|
allowPending = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return deps, allowPending, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatAdjustmentDependencySummary(rows []adjustmentStockRepo.AdjustmentDownstreamDependency) string {
|
||||||
|
if len(rows) == 0 {
|
||||||
|
return "-"
|
||||||
|
}
|
||||||
|
|
||||||
|
grouped := make(map[string]map[uint64]struct{})
|
||||||
|
for _, row := range rows {
|
||||||
|
label := utils.NormalizeUpper(row.UsableType)
|
||||||
|
if label == "" {
|
||||||
|
label = "UNKNOWN"
|
||||||
|
}
|
||||||
|
if _, ok := grouped[label]; !ok {
|
||||||
|
grouped[label] = make(map[uint64]struct{})
|
||||||
|
}
|
||||||
|
grouped[label][row.UsableID] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
labels := make([]string, 0, len(grouped))
|
||||||
|
for label := range grouped {
|
||||||
|
labels = append(labels, label)
|
||||||
|
}
|
||||||
|
sort.Strings(labels)
|
||||||
|
|
||||||
|
parts := make([]string, 0, len(labels))
|
||||||
|
for _, label := range labels {
|
||||||
|
ids := make([]uint64, 0, len(grouped[label]))
|
||||||
|
for id := range grouped[label] {
|
||||||
|
ids = append(ids, id)
|
||||||
|
}
|
||||||
|
sort.Slice(ids, func(i, j int) bool { return ids[i] < ids[j] })
|
||||||
|
idParts := make([]string, 0, len(ids))
|
||||||
|
for _, id := range ids {
|
||||||
|
idParts = append(idParts, fmt.Sprintf("%d", id))
|
||||||
|
}
|
||||||
|
parts = append(parts, fmt.Sprintf("%s=%s", label, strings.Join(idParts, "|")))
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Join(parts, ", ")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *adjustmentService) getActiveProjectFlockKandangID(ctx context.Context, warehouseID uint) (uint, error) {
|
func (s *adjustmentService) getActiveProjectFlockKandangID(ctx context.Context, warehouseID uint) (uint, error) {
|
||||||
@@ -553,46 +858,6 @@ func (s *adjustmentService) getActiveProjectFlockKandangID(ctx context.Context,
|
|||||||
return uint(projectFlockKandang.Id), nil
|
return uint(projectFlockKandang.Id), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *adjustmentService) resolveAyamSourceProductWarehouse(
|
|
||||||
ctx context.Context,
|
|
||||||
tx *gorm.DB,
|
|
||||||
warehouseID uint,
|
|
||||||
projectFlockKandangID uint,
|
|
||||||
) (*entity.ProductWarehouse, error) {
|
|
||||||
if tx == nil {
|
|
||||||
return nil, fmt.Errorf("transaction is required")
|
|
||||||
}
|
|
||||||
if projectFlockKandangID == 0 {
|
|
||||||
return nil, fiber.NewError(fiber.StatusBadRequest, "project_flock_kandang_id tidak valid untuk depletion conversion")
|
|
||||||
}
|
|
||||||
|
|
||||||
var sourcePW entity.ProductWarehouse
|
|
||||||
err := tx.WithContext(ctx).
|
|
||||||
Model(&entity.ProductWarehouse{}).
|
|
||||||
Where("project_flock_kandang_id = ?", projectFlockKandangID).
|
|
||||||
Where(`
|
|
||||||
EXISTS (
|
|
||||||
SELECT 1
|
|
||||||
FROM flags f
|
|
||||||
JOIN fifo_stock_v2_flag_members fm ON fm.flag_name = f.name AND fm.is_active = TRUE
|
|
||||||
WHERE f.flagable_type = ?
|
|
||||||
AND f.flagable_id = product_warehouses.product_id
|
|
||||||
AND fm.flag_group_code = ?
|
|
||||||
)
|
|
||||||
`, entity.FlagableTypeProduct, flagGroupAyam).
|
|
||||||
Order(gorm.Expr("CASE WHEN warehouse_id = ? THEN 0 ELSE 1 END ASC", warehouseID)).
|
|
||||||
Order("id ASC").
|
|
||||||
Take(&sourcePW).Error
|
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
||||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Produk sumber AYAM pada project flock kandang yang sama tidak ditemukan")
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &sourcePW, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *adjustmentService) createAdjustmentStockLog(
|
func (s *adjustmentService) createAdjustmentStockLog(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
stockLogRepo stockLogsRepo.StockLogRepository,
|
stockLogRepo stockLogsRepo.StockLogRepository,
|
||||||
@@ -676,57 +941,6 @@ func (s *adjustmentService) allocatePopulationForDepletionAdjustment(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *adjustmentService) resyncProjectFlockPopulationUsage(ctx context.Context, tx *gorm.DB, projectFlockKandangID uint) error {
|
|
||||||
if tx == nil || 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'
|
|
||||||
AND allocation_purpose = 'CONSUME'
|
|
||||||
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.allocation_purpose = 'CONSUME'
|
|
||||||
AND sa.stockable_id = p.id
|
|
||||||
)
|
|
||||||
`
|
|
||||||
|
|
||||||
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 (s *adjustmentService) AdjustmentHistory(c *fiber.Ctx, query *validation.Query) ([]*entity.AdjustmentStock, int64, error) {
|
func (s *adjustmentService) AdjustmentHistory(c *fiber.Ctx, query *validation.Query) ([]*entity.AdjustmentStock, int64, error) {
|
||||||
if err := s.Validate.Struct(query); err != nil {
|
if err := s.Validate.Struct(query); err != nil {
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
@@ -739,26 +953,25 @@ func (s *adjustmentService) AdjustmentHistory(c *fiber.Ctx, query *validation.Qu
|
|||||||
}
|
}
|
||||||
offset := (query.Page - 1) * query.Limit
|
offset := (query.Page - 1) * query.Limit
|
||||||
|
|
||||||
if query.WarehouseID > 0 {
|
var isProductsExist bool
|
||||||
isWarehouseExist, err := s.WarehouseRepo.IdExists(c.Context(), query.WarehouseID)
|
isWarehousesExist, err := s.WarehouseRepo.IdExists(c.Context(), uint(query.WarehouseID))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, fiber.NewError(fiber.StatusInternalServerError, "Failed to validate warehouse")
|
return nil, 0, fiber.NewError(fiber.StatusInternalServerError, "Failed to validate warehouse")
|
||||||
}
|
}
|
||||||
if !isWarehouseExist {
|
if query.WarehouseID > 0 && !isWarehousesExist {
|
||||||
return nil, 0, fiber.NewError(fiber.StatusNotFound, "Warehouse not found")
|
return nil, 0, fiber.NewError(fiber.StatusNotFound, "Warehouse not found")
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if query.ProductID > 0 {
|
isProductsExist, err = s.ProductRepo.IdExists(c.Context(), uint(query.ProductID))
|
||||||
isProductExist, err := s.ProductRepo.IdExists(c.Context(), query.ProductID)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.Log.Errorf("Failed to check product existence: %+v", err)
|
s.Log.Errorf("Failed to check product existence: %+v", err)
|
||||||
return nil, 0, fiber.NewError(fiber.StatusInternalServerError, "Failed to validate product")
|
return nil, 0, fiber.NewError(fiber.StatusInternalServerError, "Failed to validate product")
|
||||||
}
|
}
|
||||||
if !isProductExist {
|
if query.ProductID > 0 && !isProductsExist {
|
||||||
return nil, 0, fiber.NewError(fiber.StatusNotFound, "Product not found")
|
return nil, 0, fiber.NewError(fiber.StatusNotFound, "Product not found")
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
scope, scopeErr := m.ResolveLocationScope(c, s.AdjustmentStockRepository.DB())
|
scope, scopeErr := m.ResolveLocationScope(c, s.AdjustmentStockRepository.DB())
|
||||||
if scopeErr != nil {
|
if scopeErr != nil {
|
||||||
@@ -770,11 +983,11 @@ func (s *adjustmentService) AdjustmentHistory(c *fiber.Ctx, query *validation.Qu
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
functionCode := strings.ToUpper(strings.TrimSpace(query.TransactionSubtype))
|
functionCode := utils.NormalizeUpper(query.TransactionSubtype)
|
||||||
if functionCode == "" {
|
if functionCode == "" {
|
||||||
functionCode = strings.ToUpper(strings.TrimSpace(query.FunctionCode))
|
functionCode = utils.NormalizeUpper(query.FunctionCode)
|
||||||
}
|
}
|
||||||
transactionType := strings.ToUpper(strings.TrimSpace(query.TransactionType))
|
transactionType := utils.NormalizeUpper(query.TransactionType)
|
||||||
|
|
||||||
adjustmentStocks, total, err := s.AdjustmentStockRepository.FindHistory(
|
adjustmentStocks, total, err := s.AdjustmentStockRepository.FindHistory(
|
||||||
c.Context(),
|
c.Context(),
|
||||||
|
|||||||
+1
@@ -32,6 +32,7 @@ func (u *ProductWarehouseController) GetAll(c *fiber.Ctx) error {
|
|||||||
Flags: c.Query("flags", ""),
|
Flags: c.Query("flags", ""),
|
||||||
KandangId: uint(c.QueryInt("kandang_id", 0)),
|
KandangId: uint(c.QueryInt("kandang_id", 0)),
|
||||||
TransferContext: c.Query(utils.TransferContextKey, ""),
|
TransferContext: c.Query(utils.TransferContextKey, ""),
|
||||||
|
StockMode: c.Query("stock_mode", ""),
|
||||||
Type: c.Query("type", ""),
|
Type: c.Query("type", ""),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ type ProductWarehouseRelationDTO struct {
|
|||||||
ProductId uint `json:"product_id"`
|
ProductId uint `json:"product_id"`
|
||||||
WarehouseId uint `json:"warehouse_id"`
|
WarehouseId uint `json:"warehouse_id"`
|
||||||
Quantity float64 `json:"quantity"`
|
Quantity float64 `json:"quantity"`
|
||||||
|
TransferAvailableQty *float64 `json:"transfer_available_qty,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ProductWarehouseListDTO struct {
|
type ProductWarehouseListDTO struct {
|
||||||
@@ -65,6 +66,7 @@ func ToProductWarehouseRelationDTO(e entity.ProductWarehouse) ProductWarehouseRe
|
|||||||
ProductId: e.ProductId, // Field yang benar dari entity
|
ProductId: e.ProductId, // Field yang benar dari entity
|
||||||
WarehouseId: e.WarehouseId, // Field yang benar dari entity
|
WarehouseId: e.WarehouseId, // Field yang benar dari entity
|
||||||
Quantity: e.Quantity,
|
Quantity: e.Quantity,
|
||||||
|
TransferAvailableQty: e.AvailableQty,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/go-playground/validator/v10"
|
"github.com/go-playground/validator/v10"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
@@ -27,6 +28,8 @@ type productWarehouseService struct {
|
|||||||
KandangRepo kandangrepo.KandangRepository
|
KandangRepo kandangrepo.KandangRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const stockModeExcludeChickin = "exclude_chickin"
|
||||||
|
|
||||||
func NewProductWarehouseService(repo repository.ProductWarehouseRepository, validate *validator.Validate, kandangRepo kandangrepo.KandangRepository) ProductWarehouseService {
|
func NewProductWarehouseService(repo repository.ProductWarehouseRepository, validate *validator.Validate, kandangRepo kandangrepo.KandangRepository) ProductWarehouseService {
|
||||||
return &productWarehouseService{
|
return &productWarehouseService{
|
||||||
Log: utils.Log,
|
Log: utils.Log,
|
||||||
@@ -189,6 +192,11 @@ func (s productWarehouseService) GetAll(c *fiber.Ctx, params *validation.Query)
|
|||||||
s.Log.Errorf("Failed to get productWarehouses: %+v", err)
|
s.Log.Errorf("Failed to get productWarehouses: %+v", err)
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
productWarehouses, err = s.applyTransferAvailableQty(c, params, productWarehouses)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
return productWarehouses, total, nil
|
return productWarehouses, total, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,3 +237,80 @@ func (s productWarehouseService) GetOne(c *fiber.Ctx, id uint) (*entity.ProductW
|
|||||||
}
|
}
|
||||||
return productWarehouse, nil
|
return productWarehouse, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s productWarehouseService) applyTransferAvailableQty(c *fiber.Ctx, params *validation.Query, rows []entity.ProductWarehouse) ([]entity.ProductWarehouse, error) {
|
||||||
|
if len(rows) == 0 {
|
||||||
|
return rows, nil
|
||||||
|
}
|
||||||
|
if params == nil ||
|
||||||
|
params.TransferContext != utils.TransferContextInventoryTransfer ||
|
||||||
|
params.StockMode != stockModeExcludeChickin {
|
||||||
|
return rows, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ayamPWIDs := make([]uint, 0)
|
||||||
|
for i := range rows {
|
||||||
|
if isAyamProductByFlags(rows[i].Product.Flags) {
|
||||||
|
ayamPWIDs = append(ayamPWIDs, rows[i].Id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(ayamPWIDs) == 0 {
|
||||||
|
return rows, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type populationRemainingRow struct {
|
||||||
|
ProductWarehouseID uint `gorm:"column:product_warehouse_id"`
|
||||||
|
RemainingQty float64 `gorm:"column:remaining_qty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var populationRows []populationRemainingRow
|
||||||
|
if err := s.Repository.DB().WithContext(c.Context()).
|
||||||
|
Table("project_flock_populations pfp").
|
||||||
|
Select("pfp.product_warehouse_id, COALESCE(SUM(GREATEST(pfp.total_qty - pfp.total_used_qty, 0)), 0) AS remaining_qty").
|
||||||
|
Joins("JOIN project_chickins pc ON pc.id = pfp.project_chickin_id").
|
||||||
|
Where("pfp.product_warehouse_id IN ?", ayamPWIDs).
|
||||||
|
Where("pfp.deleted_at IS NULL").
|
||||||
|
Where("pc.deleted_at IS NULL").
|
||||||
|
Group("pfp.product_warehouse_id").
|
||||||
|
Scan(&populationRows).Error; err != nil {
|
||||||
|
s.Log.Errorf("Failed to resolve chickin population remaining for transfer stock filter: %+v", err)
|
||||||
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to resolve transfer stock availability")
|
||||||
|
}
|
||||||
|
|
||||||
|
populationRemainingByPW := make(map[uint]float64, len(populationRows))
|
||||||
|
for _, row := range populationRows {
|
||||||
|
populationRemainingByPW[row.ProductWarehouseID] = row.RemainingQty
|
||||||
|
}
|
||||||
|
|
||||||
|
filtered := make([]entity.ProductWarehouse, 0, len(rows))
|
||||||
|
for i := range rows {
|
||||||
|
row := rows[i]
|
||||||
|
if !isAyamProductByFlags(row.Product.Flags) {
|
||||||
|
filtered = append(filtered, row)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
available := row.Quantity - populationRemainingByPW[row.Id]
|
||||||
|
if available < 0 {
|
||||||
|
available = 0
|
||||||
|
}
|
||||||
|
row.AvailableQty = &available
|
||||||
|
|
||||||
|
if available <= 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
filtered = append(filtered, row)
|
||||||
|
}
|
||||||
|
|
||||||
|
return filtered, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func isAyamProductByFlags(flags []entity.Flag) bool {
|
||||||
|
for _, flag := range flags {
|
||||||
|
if utils.CanonicalFlagType(strings.TrimSpace(flag.Name)) == utils.FlagAyam {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|||||||
+1
@@ -20,5 +20,6 @@ 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"`
|
||||||
|
StockMode string `query:"stock_mode" validate:"omitempty,oneof=exclude_chickin"`
|
||||||
Type string `query:"type" validate:"omitempty"`
|
Type string `query:"type" validate:"omitempty"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -109,3 +109,23 @@ func (u *TransferController) CreateOne(c *fiber.Ctx) error {
|
|||||||
Data: dto.ToTransferDetailDTO(*result),
|
Data: dto.ToTransferDetailDTO(*result),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *TransferController) DeleteOne(c *fiber.Ctx) error {
|
||||||
|
param := c.Params("id")
|
||||||
|
|
||||||
|
id, err := strconv.Atoi(param)
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := u.TransferService.DeleteOne(c, uint(id)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusOK).
|
||||||
|
JSON(response.Common{
|
||||||
|
Code: fiber.StatusOK,
|
||||||
|
Status: "success",
|
||||||
|
Message: "Delete transfer successfully",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -18,5 +18,6 @@ func TransferRoutes(v1 fiber.Router, u user.UserService, s transfer.TransferServ
|
|||||||
route.Get("/", m.RequirePermissions(m.P_TransferGetAll), ctrl.GetAll)
|
route.Get("/", m.RequirePermissions(m.P_TransferGetAll), ctrl.GetAll)
|
||||||
route.Post("/", m.RequirePermissions(m.P_TransferCreateOne), ctrl.CreateOne)
|
route.Post("/", m.RequirePermissions(m.P_TransferCreateOne), ctrl.CreateOne)
|
||||||
route.Get("/:id", m.RequirePermissions(m.P_TransferGetOne), ctrl.GetOne)
|
route.Get("/:id", m.RequirePermissions(m.P_TransferGetOne), ctrl.GetOne)
|
||||||
|
route.Delete("/:id", m.RequirePermissions(m.P_TransferDeleteOne), ctrl.DeleteOne)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,13 +5,14 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/go-playground/validator/v10"
|
"github.com/go-playground/validator/v10"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||||
fifoV2 "gitlab.com/mbugroup/lti-api.git/internal/common/service/fifo_stock_v2"
|
|
||||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
||||||
rProductWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/repositories"
|
rProductWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/repositories"
|
||||||
@@ -25,12 +26,14 @@ import (
|
|||||||
"gitlab.com/mbugroup/lti-api.git/internal/utils/fifo"
|
"gitlab.com/mbugroup/lti-api.git/internal/utils/fifo"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
"gorm.io/gorm/clause"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TransferService interface {
|
type TransferService interface {
|
||||||
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.StockTransfer, int64, error)
|
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.StockTransfer, int64, error)
|
||||||
GetOne(ctx *fiber.Ctx, id uint) (*entity.StockTransfer, error)
|
GetOne(ctx *fiber.Ctx, id uint) (*entity.StockTransfer, error)
|
||||||
CreateOne(ctx *fiber.Ctx, req *validation.TransferRequest, files []*multipart.FileHeader) (*entity.StockTransfer, error)
|
CreateOne(ctx *fiber.Ctx, req *validation.TransferRequest, files []*multipart.FileHeader) (*entity.StockTransfer, error)
|
||||||
|
DeleteOne(ctx *fiber.Ctx, id uint) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type transferService struct {
|
type transferService struct {
|
||||||
@@ -51,6 +54,15 @@ type transferService struct {
|
|||||||
ExpenseBridge TransferExpenseBridge
|
ExpenseBridge TransferExpenseBridge
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const transferDeleteDownstreamGuardMessage = "Transfer stock tidak dapat dihapus karena stok transfer sudah dipakai transaksi turunan. Hapus dependensi terkait secara manual terlebih dahulu."
|
||||||
|
|
||||||
|
type downstreamDependency struct {
|
||||||
|
UsableType string `gorm:"column:usable_type"`
|
||||||
|
UsableID uint64 `gorm:"column:usable_id"`
|
||||||
|
FunctionCode string `gorm:"column:function_code"`
|
||||||
|
FlagGroupCode string `gorm:"column:flag_group_code"`
|
||||||
|
}
|
||||||
|
|
||||||
func NewTransferService(validate *validator.Validate, stockTransferRepo rStockTransfer.StockTransferRepository, stockTransferDetailRepo rStockTransfer.StockTransferDetailRepository, stockTransferDeliveryRepo rStockTransfer.StockTransferDeliveryRepository, stockTransferDeliveryItemRepo rStockTransfer.StockTransferDeliveryItemRepository, stockLogsRepo rStockLogs.StockLogRepository, productWarehouseRepo rProductWarehouse.ProductWarehouseRepository, supplierRepo rSupplier.SupplierRepository, warehouseRepo warehouseRepo.WarehouseRepository, projectFlockKandangRepo projectFlockKandangRepo.ProjectFlockKandangRepository, projectFlockPopulationRepo projectFlockKandangRepo.ProjectFlockPopulationRepository, documentSvc commonSvc.DocumentService, fifoStockV2Svc commonSvc.FifoStockV2Service, expenseBridge TransferExpenseBridge) TransferService {
|
func NewTransferService(validate *validator.Validate, stockTransferRepo rStockTransfer.StockTransferRepository, stockTransferDetailRepo rStockTransfer.StockTransferDetailRepository, stockTransferDeliveryRepo rStockTransfer.StockTransferDeliveryRepository, stockTransferDeliveryItemRepo rStockTransfer.StockTransferDeliveryItemRepository, stockLogsRepo rStockLogs.StockLogRepository, productWarehouseRepo rProductWarehouse.ProductWarehouseRepository, supplierRepo rSupplier.SupplierRepository, warehouseRepo warehouseRepo.WarehouseRepository, projectFlockKandangRepo projectFlockKandangRepo.ProjectFlockKandangRepository, projectFlockPopulationRepo projectFlockKandangRepo.ProjectFlockPopulationRepository, documentSvc commonSvc.DocumentService, fifoStockV2Svc commonSvc.FifoStockV2Service, expenseBridge TransferExpenseBridge) TransferService {
|
||||||
return &transferService{
|
return &transferService{
|
||||||
Log: utils.Log,
|
Log: utils.Log,
|
||||||
@@ -106,6 +118,7 @@ func (s transferService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entit
|
|||||||
|
|
||||||
transfers, total, err := s.StockTransferRepo.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
transfers, total, err := s.StockTransferRepo.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
||||||
db = s.withRelations(db)
|
db = s.withRelations(db)
|
||||||
|
db = db.Where("stock_transfers.deleted_at IS NULL")
|
||||||
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")
|
||||||
@@ -147,6 +160,7 @@ func (s transferService) GetOne(c *fiber.Ctx, id uint) (*entity.StockTransfer, e
|
|||||||
Joins("JOIN warehouses w_from ON w_from.id = stock_transfers.from_warehouse_id").
|
Joins("JOIN warehouses w_from ON w_from.id = stock_transfers.from_warehouse_id").
|
||||||
Joins("JOIN warehouses w_to ON w_to.id = stock_transfers.to_warehouse_id").
|
Joins("JOIN warehouses w_to ON w_to.id = stock_transfers.to_warehouse_id").
|
||||||
Where("stock_transfers.id = ?", id).
|
Where("stock_transfers.id = ?", id).
|
||||||
|
Where("stock_transfers.deleted_at IS NULL").
|
||||||
Where("w_from.location_id IN ? OR w_to.location_id IN ?", scope.IDs, scope.IDs).
|
Where("w_from.location_id IN ? OR w_to.location_id IN ?", scope.IDs, scope.IDs).
|
||||||
Count(&count).Error; err != nil {
|
Count(&count).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -157,7 +171,7 @@ func (s transferService) GetOne(c *fiber.Ctx, id uint) (*entity.StockTransfer, e
|
|||||||
}
|
}
|
||||||
|
|
||||||
transferPtr, err := s.StockTransferRepo.GetByID(c.Context(), id, func(db *gorm.DB) *gorm.DB {
|
transferPtr, err := s.StockTransferRepo.GetByID(c.Context(), id, func(db *gorm.DB) *gorm.DB {
|
||||||
return s.withRelations(db)
|
return s.withRelations(db).Where("stock_transfers.deleted_at IS NULL")
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
@@ -513,18 +527,6 @@ func (s *transferService) CreateOne(c *fiber.Ctx, req *validation.TransferReques
|
|||||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Stok tidak mencukupi untuk produk %d di gudang asal", product.ProductID))
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Stok tidak mencukupi untuk produk %d di gudang asal", product.ProductID))
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.EqualFold(flagGroupCode, "AYAM") && outUsageQty > 0 {
|
|
||||||
if err := s.allocatePopulationForStockTransferOut(
|
|
||||||
c.Context(),
|
|
||||||
tx,
|
|
||||||
detail,
|
|
||||||
uint(*detail.SourceProductWarehouseID),
|
|
||||||
outUsageQty,
|
|
||||||
); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
stockLogDecrease := &entity.StockLog{
|
stockLogDecrease := &entity.StockLog{
|
||||||
ProductWarehouseId: uint(*detail.SourceProductWarehouseID),
|
ProductWarehouseId: uint(*detail.SourceProductWarehouseID),
|
||||||
CreatedBy: uint(actorID),
|
CreatedBy: uint(actorID),
|
||||||
@@ -633,55 +635,208 @@ func (s *transferService) CreateOne(c *fiber.Ctx, req *validation.TransferReques
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *transferService) allocatePopulationForStockTransferOut(
|
func (s *transferService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||||
ctx context.Context,
|
if err := s.ensureTransferAccess(c.Context(), id, c); err != nil {
|
||||||
tx *gorm.DB,
|
return err
|
||||||
detail *entity.StockTransferDetail,
|
|
||||||
sourceProductWarehouseID uint,
|
|
||||||
consumeQty float64,
|
|
||||||
) error {
|
|
||||||
if consumeQty <= 0 {
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
if tx == nil {
|
if s.FifoStockV2Svc == nil {
|
||||||
return errors.New("transaction is required")
|
return fiber.NewError(fiber.StatusInternalServerError, "FIFO v2 service is not available")
|
||||||
}
|
|
||||||
if detail == nil || detail.Id == 0 {
|
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "Data transfer detail tidak valid")
|
|
||||||
}
|
|
||||||
if sourceProductWarehouseID == 0 {
|
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "Gudang sumber tidak valid")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pw, err := s.ProductWarehouseRepo.WithTx(tx).GetByID(ctx, sourceProductWarehouseID, nil)
|
actorID, err := m.ActorIDFromContext(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if pw.ProjectFlockKandangId == nil || *pw.ProjectFlockKandangId == 0 {
|
|
||||||
return nil
|
var deletedDetails []entity.StockTransferDetail
|
||||||
|
err = s.StockTransferRepo.DB().WithContext(c.Context()).Transaction(func(tx *gorm.DB) error {
|
||||||
|
stockLogRepoTx := rStockLogs.NewStockLogRepository(tx)
|
||||||
|
|
||||||
|
var transfer entity.StockTransfer
|
||||||
|
if err := tx.WithContext(c.Context()).
|
||||||
|
Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||||
|
Where("id = ?", uint64(id)).
|
||||||
|
Where("deleted_at IS NULL").
|
||||||
|
Take(&transfer).Error; err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Transfer dengan ID %d tidak ditemukan", id))
|
||||||
|
}
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Gagal mengambil data transfer")
|
||||||
}
|
}
|
||||||
|
|
||||||
populations, err := s.ProjectFlockPopulationRepo.WithTx(tx).GetByProjectFlockKandangIDAndProductWarehouseID(
|
var details []entity.StockTransferDetail
|
||||||
ctx,
|
if err := tx.WithContext(c.Context()).
|
||||||
*pw.ProjectFlockKandangId,
|
Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||||
sourceProductWarehouseID,
|
Where("stock_transfer_id = ?", transfer.Id).
|
||||||
|
Where("deleted_at IS NULL").
|
||||||
|
Order("id ASC").
|
||||||
|
Find(&details).Error; err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Gagal mengambil detail transfer")
|
||||||
|
}
|
||||||
|
if len(details) == 0 {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "Transfer tidak memiliki detail produk")
|
||||||
|
}
|
||||||
|
|
||||||
|
detailIDs := make([]uint64, 0, len(details))
|
||||||
|
for _, detail := range details {
|
||||||
|
detailIDs = append(detailIDs, detail.Id)
|
||||||
|
}
|
||||||
|
if err := s.ensureDeletePolicyForDownstreamConsumption(c.Context(), tx, detailIDs); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
type reflowKey struct {
|
||||||
|
flagGroupCode string
|
||||||
|
productWarehouseID uint
|
||||||
|
}
|
||||||
|
destReflows := make(map[reflowKey]struct{})
|
||||||
|
|
||||||
|
for _, detail := range details {
|
||||||
|
if detail.SourceProductWarehouseID == nil || *detail.SourceProductWarehouseID == 0 {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Detail transfer %d tidak memiliki source product warehouse valid", detail.Id))
|
||||||
|
}
|
||||||
|
if detail.DestProductWarehouseID == nil || *detail.DestProductWarehouseID == 0 {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Detail transfer %d tidak memiliki destination product warehouse valid", detail.Id))
|
||||||
|
}
|
||||||
|
|
||||||
|
flagGroupCode, err := s.resolveTransferFlagGroup(c.Context(), tx, uint(detail.ProductId))
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("FIFO v2 route tidak ditemukan untuk produk %d: %v", detail.ProductId, err))
|
||||||
|
}
|
||||||
|
|
||||||
|
rollbackRes, err := s.FifoStockV2Svc.Rollback(c.Context(), commonSvc.FifoStockV2RollbackRequest{
|
||||||
|
ProductWarehouseID: uint(*detail.SourceProductWarehouseID),
|
||||||
|
Usable: commonSvc.FifoStockV2Ref{
|
||||||
|
ID: uint(detail.Id),
|
||||||
|
LegacyTypeKey: fifo.UsableKeyStockTransferOut.String(),
|
||||||
|
FunctionCode: "STOCK_TRANSFER_OUT",
|
||||||
|
},
|
||||||
|
Reason: fmt.Sprintf("transfer delete #%s", transfer.MovementNumber),
|
||||||
|
Tx: tx,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Gagal rollback FIFO v2 transfer detail %d: %v", detail.Id, err))
|
||||||
|
}
|
||||||
|
|
||||||
|
releasedQty := 0.0
|
||||||
|
if rollbackRes != nil {
|
||||||
|
releasedQty = rollbackRes.ReleasedQty
|
||||||
|
}
|
||||||
|
if detail.UsageQty > 1e-6 && releasedQty < detail.UsageQty-1e-6 {
|
||||||
|
return fiber.NewError(
|
||||||
|
fiber.StatusBadRequest,
|
||||||
|
fmt.Sprintf("Rollback FIFO v2 source transfer detail %d tidak lengkap. Dibutuhkan %.3f, terlepas %.3f", detail.Id, detail.UsageQty, releasedQty),
|
||||||
)
|
)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if len(populations) == 0 {
|
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "Populasi tidak ditemukan untuk transfer")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return fifoV2.AllocatePopulationConsumption(
|
if releasedQty > 1e-6 {
|
||||||
ctx,
|
if err := s.appendStockLog(
|
||||||
tx,
|
c.Context(),
|
||||||
populations,
|
stockLogRepoTx,
|
||||||
sourceProductWarehouseID,
|
uint(*detail.SourceProductWarehouseID),
|
||||||
fifo.UsableKeyStockTransferOut.String(),
|
actorID,
|
||||||
|
releasedQty,
|
||||||
|
0,
|
||||||
uint(detail.Id),
|
uint(detail.Id),
|
||||||
consumeQty,
|
fmt.Sprintf("TRANSFER DELETE #%s", transfer.MovementNumber),
|
||||||
)
|
); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
destDecreaseQty := detail.TotalQty
|
||||||
|
if destDecreaseQty <= 1e-6 {
|
||||||
|
destDecreaseQty = detail.UsageQty
|
||||||
|
}
|
||||||
|
if destDecreaseQty > 1e-6 {
|
||||||
|
if err := s.appendStockLog(
|
||||||
|
c.Context(),
|
||||||
|
stockLogRepoTx,
|
||||||
|
uint(*detail.DestProductWarehouseID),
|
||||||
|
actorID,
|
||||||
|
0,
|
||||||
|
destDecreaseQty,
|
||||||
|
uint(detail.Id),
|
||||||
|
fmt.Sprintf("TRANSFER DELETE #%s", transfer.MovementNumber),
|
||||||
|
); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
destReflows[reflowKey{
|
||||||
|
flagGroupCode: flagGroupCode,
|
||||||
|
productWarehouseID: uint(*detail.DestProductWarehouseID),
|
||||||
|
}] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
now := time.Now().UTC()
|
||||||
|
if err := tx.WithContext(c.Context()).
|
||||||
|
Where("stock_transfer_detail_id IN ?", detailIDs).
|
||||||
|
Delete(&entity.StockTransferDeliveryItem{}).Error; err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Gagal menghapus item delivery transfer")
|
||||||
|
}
|
||||||
|
if err := tx.WithContext(c.Context()).
|
||||||
|
Model(&entity.StockTransferDelivery{}).
|
||||||
|
Where("stock_transfer_id = ?", transfer.Id).
|
||||||
|
Where("deleted_at IS NULL").
|
||||||
|
Updates(map[string]any{
|
||||||
|
"deleted_at": now,
|
||||||
|
"updated_at": now,
|
||||||
|
}).Error; err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Gagal menghapus delivery transfer")
|
||||||
|
}
|
||||||
|
if err := tx.WithContext(c.Context()).
|
||||||
|
Model(&entity.StockTransferDetail{}).
|
||||||
|
Where("id IN ?", detailIDs).
|
||||||
|
Where("deleted_at IS NULL").
|
||||||
|
Updates(map[string]any{
|
||||||
|
"deleted_at": now,
|
||||||
|
"updated_at": now,
|
||||||
|
}).Error; err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Gagal menghapus detail transfer")
|
||||||
|
}
|
||||||
|
|
||||||
|
asOf := transfer.TransferDate
|
||||||
|
for key := range destReflows {
|
||||||
|
if _, err := s.FifoStockV2Svc.Reflow(c.Context(), commonSvc.FifoStockV2ReflowRequest{
|
||||||
|
FlagGroupCode: key.flagGroupCode,
|
||||||
|
ProductWarehouseID: key.productWarehouseID,
|
||||||
|
AsOf: &asOf,
|
||||||
|
Tx: tx,
|
||||||
|
}); err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Gagal reflow stok tujuan saat delete transfer: %v", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tx.WithContext(c.Context()).
|
||||||
|
Model(&entity.StockTransfer{}).
|
||||||
|
Where("id = ?", transfer.Id).
|
||||||
|
Where("deleted_at IS NULL").
|
||||||
|
Updates(map[string]any{
|
||||||
|
"deleted_at": now,
|
||||||
|
"updated_at": now,
|
||||||
|
}).Error; err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Gagal menghapus transfer")
|
||||||
|
}
|
||||||
|
|
||||||
|
deletedDetails = append(deletedDetails, details...)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
if fiberErr, ok := err.(*fiber.Error); ok {
|
||||||
|
return fiberErr
|
||||||
|
}
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Gagal menghapus transfer")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(deletedDetails) > 0 && s.ExpenseBridge != nil {
|
||||||
|
if err := s.ExpenseBridge.OnItemsDeleted(c.Context(), uint64(id), deletedDetails); err != nil {
|
||||||
|
s.Log.Errorf("Failed to cleanup transfer expense link for transfer_id=%d: %+v", id, err)
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Transfer berhasil dihapus, namun sinkronisasi expense gagal. Silakan cek modul expense")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *transferService) resolveTransferFlagGroup(
|
func (s *transferService) resolveTransferFlagGroup(
|
||||||
@@ -757,3 +912,264 @@ func (s *transferService) getActiveProjectFlockKandangID(ctx context.Context, wa
|
|||||||
|
|
||||||
return uint(projectFlockKandang.Id), nil
|
return uint(projectFlockKandang.Id), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *transferService) ensureTransferAccess(ctx context.Context, id uint, c *fiber.Ctx) error {
|
||||||
|
scope, err := m.ResolveLocationScope(c, s.StockTransferRepo.DB())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !scope.Restrict {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if len(scope.IDs) == 0 {
|
||||||
|
return fiber.NewError(fiber.StatusNotFound, "Transfer not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
var count int64
|
||||||
|
if err := s.StockTransferRepo.DB().WithContext(ctx).
|
||||||
|
Table("stock_transfers").
|
||||||
|
Joins("JOIN warehouses w_from ON w_from.id = stock_transfers.from_warehouse_id").
|
||||||
|
Joins("JOIN warehouses w_to ON w_to.id = stock_transfers.to_warehouse_id").
|
||||||
|
Where("stock_transfers.id = ?", id).
|
||||||
|
Where("stock_transfers.deleted_at IS NULL").
|
||||||
|
Where("w_from.location_id IN ? OR w_to.location_id IN ?", scope.IDs, scope.IDs).
|
||||||
|
Count(&count).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if count == 0 {
|
||||||
|
return fiber.NewError(fiber.StatusNotFound, "Transfer not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *transferService) ensureDeletePolicyForDownstreamConsumption(ctx context.Context, tx *gorm.DB, detailIDs []uint64) error {
|
||||||
|
dependencies, err := s.loadActiveTransferDownstreamDependencies(ctx, tx, detailIDs)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Errorf("Failed to load downstream stock transfer consumption: %+v", err)
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Gagal memvalidasi transaksi turunan transfer stock")
|
||||||
|
}
|
||||||
|
if len(dependencies) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
ayamDependency, err := s.hasAyamDownstreamConsumption(ctx, tx, detailIDs)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Errorf("Failed to validate AYAM downstream dependency for transfer delete: %+v", err)
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Gagal memvalidasi dependensi AYAM pada transfer stock")
|
||||||
|
}
|
||||||
|
if ayamDependency {
|
||||||
|
return fiber.NewError(
|
||||||
|
fiber.StatusBadRequest,
|
||||||
|
fmt.Sprintf(
|
||||||
|
"%s Dependensi aktif: %s. Alasan block: produk AYAM yang sudah terpakai tidak dapat dihapus.",
|
||||||
|
transferDeleteDownstreamGuardMessage,
|
||||||
|
formatDownstreamDependencySummary(dependencies),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
denyReason := ""
|
||||||
|
for _, dep := range dependencies {
|
||||||
|
policy, policyErr := commonSvc.ResolveFifoPendingPolicy(ctx, tx, commonSvc.FifoPendingPolicyInput{
|
||||||
|
Lane: "USABLE",
|
||||||
|
FlagGroupCode: dep.FlagGroupCode,
|
||||||
|
FunctionCode: dep.FunctionCode,
|
||||||
|
LegacyTypeKey: dep.UsableType,
|
||||||
|
})
|
||||||
|
if policyErr != nil {
|
||||||
|
s.Log.Errorf("Failed to resolve FIFO pending policy for transfer dependency: %+v", policyErr)
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Gagal membaca konfigurasi FIFO v2")
|
||||||
|
}
|
||||||
|
if !policy.Found || !policy.AllowPending {
|
||||||
|
denyReason = "pending disabled by config"
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if denyReason == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return fiber.NewError(
|
||||||
|
fiber.StatusBadRequest,
|
||||||
|
fmt.Sprintf(
|
||||||
|
"%s Dependensi aktif: %s. Alasan block: %s.",
|
||||||
|
transferDeleteDownstreamGuardMessage,
|
||||||
|
formatDownstreamDependencySummary(dependencies),
|
||||||
|
denyReason,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *transferService) loadActiveTransferDownstreamDependencies(
|
||||||
|
ctx context.Context,
|
||||||
|
tx *gorm.DB,
|
||||||
|
detailIDs []uint64,
|
||||||
|
) ([]downstreamDependency, error) {
|
||||||
|
if len(detailIDs) == 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
db := s.StockTransferRepo.DB().WithContext(ctx)
|
||||||
|
if tx != nil {
|
||||||
|
db = tx.WithContext(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
var rows []downstreamDependency
|
||||||
|
err := db.Table("stock_allocations").
|
||||||
|
Select("usable_type, usable_id, COALESCE(function_code,'') AS function_code, COALESCE(flag_group_code,'') AS flag_group_code").
|
||||||
|
Where("stockable_type = ?", fifo.StockableKeyStockTransferIn.String()).
|
||||||
|
Where("stockable_id IN ?", detailIDs).
|
||||||
|
Where("status = ?", entity.StockAllocationStatusActive).
|
||||||
|
Where("allocation_purpose = ?", entity.StockAllocationPurposeConsume).
|
||||||
|
Where("deleted_at IS NULL").
|
||||||
|
Group("usable_type, usable_id, function_code, flag_group_code").
|
||||||
|
Scan(&rows).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return rows, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatDownstreamDependencySummary(rows []downstreamDependency) string {
|
||||||
|
if len(rows) == 0 {
|
||||||
|
return "-"
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencyMap := make(map[string]map[uint64]struct{})
|
||||||
|
for _, row := range rows {
|
||||||
|
label := mapTransferDownstreamUsableLabel(row.UsableType)
|
||||||
|
if _, ok := dependencyMap[label]; !ok {
|
||||||
|
dependencyMap[label] = make(map[uint64]struct{})
|
||||||
|
}
|
||||||
|
dependencyMap[label][row.UsableID] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
labels := make([]string, 0, len(dependencyMap))
|
||||||
|
for label := range dependencyMap {
|
||||||
|
labels = append(labels, label)
|
||||||
|
}
|
||||||
|
sort.Strings(labels)
|
||||||
|
|
||||||
|
details := make([]string, 0, len(labels))
|
||||||
|
for _, label := range labels {
|
||||||
|
ids := sortedUint64Keys(dependencyMap[label])
|
||||||
|
details = append(details, fmt.Sprintf("%s=%s", label, joinUint64(ids)))
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Join(details, ", ")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *transferService) hasAyamDownstreamConsumption(ctx context.Context, tx *gorm.DB, detailIDs []uint64) (bool, error) {
|
||||||
|
if len(detailIDs) == 0 {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
db := s.StockTransferRepo.DB().WithContext(ctx)
|
||||||
|
if tx != nil {
|
||||||
|
db = tx.WithContext(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
var found int64
|
||||||
|
err := db.Table("stock_allocations sa").
|
||||||
|
Joins("JOIN stock_transfer_details std ON std.id = sa.stockable_id AND std.deleted_at IS NULL").
|
||||||
|
Joins("JOIN flags f ON f.flagable_type = ? AND f.flagable_id = std.product_id", entity.FlagableTypeProduct).
|
||||||
|
Joins("JOIN fifo_stock_v2_flag_members fm ON fm.flag_name = f.name AND fm.flag_group_code = ? AND fm.is_active = TRUE", "AYAM").
|
||||||
|
Where("sa.stockable_type = ?", fifo.StockableKeyStockTransferIn.String()).
|
||||||
|
Where("sa.stockable_id IN ?", detailIDs).
|
||||||
|
Where("sa.status = ?", entity.StockAllocationStatusActive).
|
||||||
|
Where("sa.allocation_purpose = ?", entity.StockAllocationPurposeConsume).
|
||||||
|
Where("sa.deleted_at IS NULL").
|
||||||
|
Count(&found).Error
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return found > 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func mapTransferDownstreamUsableLabel(usableType string) string {
|
||||||
|
switch strings.ToUpper(strings.TrimSpace(usableType)) {
|
||||||
|
case fifo.UsableKeyRecordingStock.String(), fifo.UsableKeyRecordingDepletion.String():
|
||||||
|
return "Recording"
|
||||||
|
case fifo.UsableKeyProjectChickin.String():
|
||||||
|
return "Chickin"
|
||||||
|
case fifo.UsableKeyMarketingDelivery.String():
|
||||||
|
return "Marketing"
|
||||||
|
case fifo.UsableKeyTransferToLayingOut.String():
|
||||||
|
return "TransferToLaying"
|
||||||
|
case fifo.UsableKeyStockTransferOut.String():
|
||||||
|
return "TransferStock"
|
||||||
|
case fifo.UsableKeyAdjustmentOut.String():
|
||||||
|
return "Adjustment"
|
||||||
|
default:
|
||||||
|
return strings.ToUpper(strings.TrimSpace(usableType))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func sortedUint64Keys(input map[uint64]struct{}) []uint64 {
|
||||||
|
if len(input) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
out := make([]uint64, 0, len(input))
|
||||||
|
for id := range input {
|
||||||
|
if id == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
out = append(out, id)
|
||||||
|
}
|
||||||
|
sort.Slice(out, func(i, j int) bool { return out[i] < out[j] })
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func joinUint64(values []uint64) string {
|
||||||
|
if len(values) == 0 {
|
||||||
|
return "-"
|
||||||
|
}
|
||||||
|
parts := make([]string, 0, len(values))
|
||||||
|
for _, value := range values {
|
||||||
|
parts = append(parts, fmt.Sprintf("%d", value))
|
||||||
|
}
|
||||||
|
return strings.Join(parts, "|")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *transferService) appendStockLog(
|
||||||
|
ctx context.Context,
|
||||||
|
stockLogRepo rStockLogs.StockLogRepository,
|
||||||
|
productWarehouseID uint,
|
||||||
|
actorID uint,
|
||||||
|
increase float64,
|
||||||
|
decrease float64,
|
||||||
|
loggableID uint,
|
||||||
|
notes string,
|
||||||
|
) error {
|
||||||
|
if productWarehouseID == 0 || (increase <= 1e-6 && decrease <= 1e-6) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
stockLog := &entity.StockLog{
|
||||||
|
ProductWarehouseId: productWarehouseID,
|
||||||
|
CreatedBy: actorID,
|
||||||
|
Increase: increase,
|
||||||
|
Decrease: decrease,
|
||||||
|
LoggableType: string(utils.StockLogTypeTransfer),
|
||||||
|
LoggableId: loggableID,
|
||||||
|
Notes: notes,
|
||||||
|
}
|
||||||
|
|
||||||
|
stockLogs, err := stockLogRepo.GetByProductWarehouse(ctx, productWarehouseID, 1)
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to get stock logs")
|
||||||
|
}
|
||||||
|
if len(stockLogs) > 0 {
|
||||||
|
latestStockLog := stockLogs[0]
|
||||||
|
stockLog.Stock = latestStockLog.Stock + increase - decrease
|
||||||
|
} else {
|
||||||
|
stockLog.Stock = increase - decrease
|
||||||
|
}
|
||||||
|
if err := stockLogRepo.CreateOne(ctx, stockLog, nil); err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, "Gagal membuat stock log saat delete transfer")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -127,30 +127,18 @@ func (s deliveryOrdersService) GetAll(c *fiber.Ctx, params *validation.DeliveryO
|
|||||||
Preload("Products.DeliveryProduct")
|
Preload("Products.DeliveryProduct")
|
||||||
|
|
||||||
if params.Status != "" {
|
if params.Status != "" {
|
||||||
status := strings.TrimSpace(params.Status)
|
|
||||||
latestApprovalSubQuery := s.MarketingRepo.DB().
|
latestApprovalSubQuery := s.MarketingRepo.DB().
|
||||||
WithContext(c.Context()).
|
WithContext(c.Context()).
|
||||||
Table("approvals").
|
Table("approvals").
|
||||||
Select("DISTINCT ON (approvable_id) approvable_id, step_name, action").
|
Select("DISTINCT ON (approvable_id) approvable_id, step_name").
|
||||||
Where("approvable_type = ?", utils.ApprovalWorkflowMarketing.String()).
|
Where("approvable_type = ?", utils.ApprovalWorkflowMarketing.String()).
|
||||||
Order("approvable_id, id DESC")
|
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 (
|
db = db.Where(`EXISTS (
|
||||||
SELECT 1
|
SELECT 1
|
||||||
FROM (?) AS latest_approval
|
FROM (?) AS latest_approval
|
||||||
WHERE latest_approval.approvable_id = marketings.id
|
WHERE latest_approval.approvable_id = marketings.id
|
||||||
AND LOWER(latest_approval.step_name) = LOWER(?)
|
AND LOWER(latest_approval.step_name) = LOWER(?)
|
||||||
AND (latest_approval.action IS NULL OR latest_approval.action <> ?)
|
)`, latestApprovalSubQuery, params.Status)
|
||||||
)`, latestApprovalSubQuery, status, string(entity.ApprovalActionRejected))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.Search != "" {
|
if params.Search != "" {
|
||||||
@@ -225,6 +213,7 @@ func (s deliveryOrdersService) GetAll(c *fiber.Ctx, params *validation.DeliveryO
|
|||||||
}
|
}
|
||||||
return db.Order("created_at DESC").Order("updated_at DESC")
|
return db.Order("created_at DESC").Order("updated_at DESC")
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
@@ -269,6 +258,7 @@ func (s deliveryOrdersService) GetOne(c *fiber.Ctx, id uint) (*dto.MarketingDeta
|
|||||||
return db.Preload("ActionUser")
|
return db.Preload("ActionUser")
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
||||||
} else if len(approvals) > 0 {
|
} else if len(approvals) > 0 {
|
||||||
if marketing.LatestApproval == nil {
|
if marketing.LatestApproval == nil {
|
||||||
latest := approvals[len(approvals)-1]
|
latest := approvals[len(approvals)-1]
|
||||||
@@ -320,6 +310,7 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Delivery
|
|||||||
}
|
}
|
||||||
|
|
||||||
err = s.MarketingRepo.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
err = s.MarketingRepo.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
||||||
|
|
||||||
marketingProductRepositoryTx := marketingRepo.NewMarketingProductRepository(dbTransaction)
|
marketingProductRepositoryTx := marketingRepo.NewMarketingProductRepository(dbTransaction)
|
||||||
marketingDeliveryProductRepositoryTx := marketingRepo.NewMarketingDeliveryProductRepository(dbTransaction)
|
marketingDeliveryProductRepositoryTx := marketingRepo.NewMarketingDeliveryProductRepository(dbTransaction)
|
||||||
approvalSvcTx := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(dbTransaction))
|
approvalSvcTx := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(dbTransaction))
|
||||||
@@ -386,6 +377,7 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Delivery
|
|||||||
deliveryProduct.VehicleNumber = requestedProduct.VehicleNumber
|
deliveryProduct.VehicleNumber = requestedProduct.VehicleNumber
|
||||||
|
|
||||||
if requestedProduct.Qty > 0 {
|
if requestedProduct.Qty > 0 {
|
||||||
|
|
||||||
if err := s.consumeDeliveryStock(c.Context(), dbTransaction, deliveryProduct, foundMarketingProduct, requestedProduct.Qty, actorID); err != nil {
|
if err := s.consumeDeliveryStock(c.Context(), dbTransaction, deliveryProduct, foundMarketingProduct, requestedProduct.Qty, actorID); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -412,6 +404,7 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Delivery
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if fiberErr, ok := err.(*fiber.Error); ok {
|
if fiberErr, ok := err.(*fiber.Error); ok {
|
||||||
return nil, fiberErr
|
return nil, fiberErr
|
||||||
@@ -443,6 +436,7 @@ func (s deliveryOrdersService) UpdateOne(c *fiber.Ctx, req *validation.DeliveryO
|
|||||||
}
|
}
|
||||||
|
|
||||||
err = s.MarketingRepo.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
err = s.MarketingRepo.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
||||||
|
|
||||||
marketingProductRepositoryTx := marketingRepo.NewMarketingProductRepository(dbTransaction)
|
marketingProductRepositoryTx := marketingRepo.NewMarketingProductRepository(dbTransaction)
|
||||||
marketingDeliveryProductRepositoryTx := marketingRepo.NewMarketingDeliveryProductRepository(dbTransaction)
|
marketingDeliveryProductRepositoryTx := marketingRepo.NewMarketingDeliveryProductRepository(dbTransaction)
|
||||||
marketingRepoTx := marketingRepo.NewMarketingRepository(dbTransaction)
|
marketingRepoTx := marketingRepo.NewMarketingRepository(dbTransaction)
|
||||||
@@ -531,6 +525,7 @@ func (s deliveryOrdersService) UpdateOne(c *fiber.Ctx, req *validation.DeliveryO
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if fiberErr, ok := err.(*fiber.Error); ok {
|
if fiberErr, ok := err.(*fiber.Error); ok {
|
||||||
return nil, fiberErr
|
return nil, fiberErr
|
||||||
@@ -563,12 +558,6 @@ func (s deliveryOrdersService) consumeDeliveryStock(ctx context.Context, tx *gor
|
|||||||
if deliveryProduct == nil || deliveryProduct.Id == 0 {
|
if deliveryProduct == nil || deliveryProduct.Id == 0 {
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, "Delivery product not found")
|
return fiber.NewError(fiber.StatusInternalServerError, "Delivery product not found")
|
||||||
}
|
}
|
||||||
if deliveryProduct.ProductWarehouseId == 0 {
|
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, "Delivery product warehouse not found")
|
|
||||||
}
|
|
||||||
if deliveryProduct.ProductWarehouseId != marketingProduct.ProductWarehouseId {
|
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "Delivery product warehouse mismatch with marketing product")
|
|
||||||
}
|
|
||||||
|
|
||||||
deliveryProductRepo := marketingRepo.NewMarketingDeliveryProductRepository(tx)
|
deliveryProductRepo := marketingRepo.NewMarketingDeliveryProductRepository(tx)
|
||||||
previousUsage := deliveryProduct.UsageQty
|
previousUsage := deliveryProduct.UsageQty
|
||||||
@@ -606,12 +595,12 @@ func (s deliveryOrdersService) consumeDeliveryStock(ctx context.Context, tx *gor
|
|||||||
Decrease: allocatedDelta,
|
Decrease: allocatedDelta,
|
||||||
LoggableType: string(utils.StockLogTypeMarketing),
|
LoggableType: string(utils.StockLogTypeMarketing),
|
||||||
LoggableId: deliveryProduct.Id,
|
LoggableId: deliveryProduct.Id,
|
||||||
ProductWarehouseId: deliveryProduct.ProductWarehouseId,
|
ProductWarehouseId: marketingProduct.ProductWarehouseId,
|
||||||
CreatedBy: actorID,
|
CreatedBy: actorID,
|
||||||
Notes: fmt.Sprintf("FIFO v2 reflow consume (%.2f)", allocatedDelta),
|
Notes: fmt.Sprintf("FIFO v2 reflow consume (%.2f)", allocatedDelta),
|
||||||
}
|
}
|
||||||
|
|
||||||
stockLogs, err := s.StockLogRepo.GetByProductWarehouse(ctx, deliveryProduct.ProductWarehouseId, 1)
|
stockLogs, err := s.StockLogRepo.GetByProductWarehouse(ctx, marketingProduct.ProductWarehouseId, 1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to get stock logs")
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to get stock logs")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -151,31 +151,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")
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
kandangGroupDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandang-groups/dto"
|
kandangDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/dto"
|
||||||
)
|
)
|
||||||
|
|
||||||
// === DTO Structs ===
|
// === DTO Structs ===
|
||||||
@@ -18,7 +18,7 @@ type EmployeesListDTO struct {
|
|||||||
Id uint `json:"id"`
|
Id uint `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
IsActive bool `json:"is_active"`
|
IsActive bool `json:"is_active"`
|
||||||
Kandangs []kandangGroupDTO.KandangGroupRelationDTO `json:"kandangs"`
|
Kandangs []kandangDTO.KandangRelationDTO `json:"kandangs"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
}
|
}
|
||||||
@@ -37,12 +37,12 @@ func ToEmployeesRelationDTO(e entity.Employees) EmployeesRelationDTO {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ToEmployeesListDTO(e entity.Employees) EmployeesListDTO {
|
func ToEmployeesListDTO(e entity.Employees) EmployeesListDTO {
|
||||||
kandangs := make([]kandangGroupDTO.KandangGroupRelationDTO, 0, len(e.EmployeeKandangs))
|
kandangs := make([]kandangDTO.KandangRelationDTO, 0, len(e.EmployeeKandangs))
|
||||||
for _, rel := range e.EmployeeKandangs {
|
for _, rel := range e.EmployeeKandangs {
|
||||||
if rel.Kandang.Id == 0 {
|
if rel.Kandang.Id == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
kandangs = append(kandangs, kandangGroupDTO.ToKandangGroupRelationDTO(rel.Kandang))
|
kandangs = append(kandangs, kandangDTO.ToKandangRelationDTO(rel.Kandang))
|
||||||
}
|
}
|
||||||
|
|
||||||
return EmployeesListDTO{
|
return EmployeesListDTO{
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ func (s employeesService) ensureEmployeeAccess(c *fiber.Ctx, employeeID uint) er
|
|||||||
db := s.Repository.DB().WithContext(c.Context()).
|
db := s.Repository.DB().WithContext(c.Context()).
|
||||||
Table("employees e").
|
Table("employees e").
|
||||||
Joins("JOIN employee_kandangs ek ON ek.employee_id = e.id").
|
Joins("JOIN employee_kandangs ek ON ek.employee_id = e.id").
|
||||||
Joins("JOIN kandang_groups k ON k.id = ek.kandang_id").
|
Joins("JOIN kandangs k ON k.id = ek.kandang_id").
|
||||||
Joins("JOIN locations loc ON loc.id = k.location_id").
|
Joins("JOIN locations loc ON loc.id = k.location_id").
|
||||||
Joins("JOIN areas a ON a.id = loc.area_id").
|
Joins("JOIN areas a ON a.id = loc.area_id").
|
||||||
Where("e.id = ?", employeeID).
|
Where("e.id = ?", employeeID).
|
||||||
@@ -79,7 +79,7 @@ func (s employeesService) ensureKandangIDsAccess(c *fiber.Ctx, kandangIDs []uint
|
|||||||
}
|
}
|
||||||
|
|
||||||
db := s.Repository.DB().WithContext(c.Context()).
|
db := s.Repository.DB().WithContext(c.Context()).
|
||||||
Table("kandang_groups k").
|
Table("kandangs k").
|
||||||
Joins("JOIN locations loc ON loc.id = k.location_id").
|
Joins("JOIN locations loc ON loc.id = k.location_id").
|
||||||
Joins("JOIN areas a ON a.id = loc.area_id").
|
Joins("JOIN areas a ON a.id = loc.area_id").
|
||||||
Where("k.id IN ?", kandangIDs)
|
Where("k.id IN ?", kandangIDs)
|
||||||
@@ -109,7 +109,7 @@ func (s employeesService) GetAll(c *fiber.Ctx, params *validation.Query) ([]enti
|
|||||||
employeess, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
employeess, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
||||||
db = s.withRelations(db)
|
db = s.withRelations(db)
|
||||||
db = db.Joins("JOIN employee_kandangs ek ON ek.employee_id = employees.id").
|
db = db.Joins("JOIN employee_kandangs ek ON ek.employee_id = employees.id").
|
||||||
Joins("JOIN kandang_groups k ON k.id = ek.kandang_id").
|
Joins("JOIN kandangs k ON k.id = ek.kandang_id").
|
||||||
Joins("JOIN locations loc ON loc.id = k.location_id").
|
Joins("JOIN locations loc ON loc.id = k.location_id").
|
||||||
Joins("JOIN areas a ON a.id = loc.area_id")
|
Joins("JOIN areas a ON a.id = loc.area_id")
|
||||||
var scopeErr error
|
var scopeErr error
|
||||||
|
|||||||
@@ -1,146 +0,0 @@
|
|||||||
package controller
|
|
||||||
|
|
||||||
import (
|
|
||||||
"math"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
|
||||||
|
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandang-groups/dto"
|
|
||||||
service "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandang-groups/services"
|
|
||||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandang-groups/validations"
|
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/response"
|
|
||||||
)
|
|
||||||
|
|
||||||
type KandangGroupController struct {
|
|
||||||
KandangGroupService service.KandangGroupService
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewKandangGroupController(kandangGroupService service.KandangGroupService) *KandangGroupController {
|
|
||||||
return &KandangGroupController{
|
|
||||||
KandangGroupService: kandangGroupService,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *KandangGroupController) GetAll(c *fiber.Ctx) error {
|
|
||||||
query := &validation.Query{
|
|
||||||
Page: c.QueryInt("page", 1),
|
|
||||||
Limit: c.QueryInt("limit", 10),
|
|
||||||
Search: c.Query("search", ""),
|
|
||||||
LocationId: c.QueryInt("location_id", 0),
|
|
||||||
PicId: c.QueryInt("pic_id", 0),
|
|
||||||
}
|
|
||||||
|
|
||||||
if query.Page < 1 || query.Limit < 1 {
|
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0")
|
|
||||||
}
|
|
||||||
|
|
||||||
result, totalResults, err := u.KandangGroupService.GetAll(c, query)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.Status(fiber.StatusOK).
|
|
||||||
JSON(response.SuccessWithPaginate[dto.KandangGroupListDTO]{
|
|
||||||
Code: fiber.StatusOK,
|
|
||||||
Status: "success",
|
|
||||||
Message: "Get all kandang groups successfully",
|
|
||||||
Meta: response.Meta{
|
|
||||||
Page: query.Page,
|
|
||||||
Limit: query.Limit,
|
|
||||||
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
|
|
||||||
TotalResults: totalResults,
|
|
||||||
},
|
|
||||||
Data: dto.ToKandangGroupListDTOs(result),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *KandangGroupController) GetOne(c *fiber.Ctx) error {
|
|
||||||
param := c.Params("id")
|
|
||||||
|
|
||||||
id, err := strconv.Atoi(param)
|
|
||||||
if err != nil {
|
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
|
||||||
}
|
|
||||||
|
|
||||||
result, err := u.KandangGroupService.GetOne(c, uint(id))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.Status(fiber.StatusOK).
|
|
||||||
JSON(response.Success{
|
|
||||||
Code: fiber.StatusOK,
|
|
||||||
Status: "success",
|
|
||||||
Message: "Get kandang group successfully",
|
|
||||||
Data: dto.ToKandangGroupListDTO(*result),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *KandangGroupController) CreateOne(c *fiber.Ctx) error {
|
|
||||||
req := new(validation.Create)
|
|
||||||
|
|
||||||
if err := c.BodyParser(req); err != nil {
|
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
|
||||||
}
|
|
||||||
|
|
||||||
result, err := u.KandangGroupService.CreateOne(c, req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.Status(fiber.StatusCreated).
|
|
||||||
JSON(response.Success{
|
|
||||||
Code: fiber.StatusCreated,
|
|
||||||
Status: "success",
|
|
||||||
Message: "Create kandang group successfully",
|
|
||||||
Data: dto.ToKandangGroupListDTO(*result),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *KandangGroupController) UpdateOne(c *fiber.Ctx) error {
|
|
||||||
req := new(validation.Update)
|
|
||||||
param := c.Params("id")
|
|
||||||
|
|
||||||
id, err := strconv.Atoi(param)
|
|
||||||
if err != nil {
|
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := c.BodyParser(req); err != nil {
|
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
|
||||||
}
|
|
||||||
|
|
||||||
result, err := u.KandangGroupService.UpdateOne(c, req, uint(id))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.Status(fiber.StatusOK).
|
|
||||||
JSON(response.Success{
|
|
||||||
Code: fiber.StatusOK,
|
|
||||||
Status: "success",
|
|
||||||
Message: "Update kandang group successfully",
|
|
||||||
Data: dto.ToKandangGroupListDTO(*result),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *KandangGroupController) DeleteOne(c *fiber.Ctx) error {
|
|
||||||
param := c.Params("id")
|
|
||||||
|
|
||||||
id, err := strconv.Atoi(param)
|
|
||||||
if err != nil {
|
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := u.KandangGroupService.DeleteOne(c, uint(id)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.Status(fiber.StatusOK).
|
|
||||||
JSON(response.Common{
|
|
||||||
Code: fiber.StatusOK,
|
|
||||||
Status: "success",
|
|
||||||
Message: "Delete kandang group successfully",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -1,114 +0,0 @@
|
|||||||
package dto
|
|
||||||
|
|
||||||
import (
|
|
||||||
"time"
|
|
||||||
|
|
||||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
|
||||||
locationDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/locations/dto"
|
|
||||||
userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
|
|
||||||
)
|
|
||||||
|
|
||||||
type KandangGroupRelationDTO struct {
|
|
||||||
Id uint `json:"id"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
Status string `json:"status"`
|
|
||||||
Location *locationDTO.LocationRelationDTO `json:"location,omitempty"`
|
|
||||||
Pic *userDTO.UserRelationDTO `json:"pic,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type RecordingKandangDTO struct {
|
|
||||||
Id uint `json:"id"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type KandangGroupListDTO struct {
|
|
||||||
Id uint `json:"id"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
Status string `json:"status"`
|
|
||||||
Location locationDTO.LocationRelationDTO `json:"location"`
|
|
||||||
Pic userDTO.UserRelationDTO `json:"pic"`
|
|
||||||
CreatedUser userDTO.UserRelationDTO `json:"created_user"`
|
|
||||||
CreatedAt time.Time `json:"created_at"`
|
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
|
||||||
RecordingKandangs []RecordingKandangDTO `json:"recording_kandangs"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type KandangGroupDetailDTO struct {
|
|
||||||
KandangGroupListDTO
|
|
||||||
}
|
|
||||||
|
|
||||||
func ToKandangGroupRelationDTO(e entity.KandangGroup) KandangGroupRelationDTO {
|
|
||||||
var location *locationDTO.LocationRelationDTO
|
|
||||||
if e.Location.Id != 0 {
|
|
||||||
mapped := locationDTO.ToLocationRelationDTO(e.Location)
|
|
||||||
location = &mapped
|
|
||||||
}
|
|
||||||
|
|
||||||
var pic *userDTO.UserRelationDTO
|
|
||||||
if e.Pic.Id != 0 {
|
|
||||||
mapped := userDTO.ToUserRelationDTO(e.Pic)
|
|
||||||
pic = &mapped
|
|
||||||
}
|
|
||||||
|
|
||||||
return KandangGroupRelationDTO{
|
|
||||||
Id: e.Id,
|
|
||||||
Name: e.Name,
|
|
||||||
Status: e.Status,
|
|
||||||
Location: location,
|
|
||||||
Pic: pic,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ToKandangGroupListDTO(e entity.KandangGroup) KandangGroupListDTO {
|
|
||||||
var location locationDTO.LocationRelationDTO
|
|
||||||
if e.Location.Id != 0 {
|
|
||||||
mapped := locationDTO.ToLocationRelationDTO(e.Location)
|
|
||||||
location = mapped
|
|
||||||
}
|
|
||||||
|
|
||||||
var pic userDTO.UserRelationDTO
|
|
||||||
if e.Pic.Id != 0 {
|
|
||||||
mapped := userDTO.ToUserRelationDTO(e.Pic)
|
|
||||||
pic = mapped
|
|
||||||
}
|
|
||||||
|
|
||||||
var createdUser userDTO.UserRelationDTO
|
|
||||||
if e.CreatedUser.Id != 0 {
|
|
||||||
mapped := userDTO.ToUserRelationDTO(e.CreatedUser)
|
|
||||||
createdUser = mapped
|
|
||||||
}
|
|
||||||
|
|
||||||
recordingKandangs := make([]RecordingKandangDTO, 0, len(e.Kandangs))
|
|
||||||
for _, kandang := range e.Kandangs {
|
|
||||||
recordingKandangs = append(recordingKandangs, RecordingKandangDTO{
|
|
||||||
Id: kandang.Id,
|
|
||||||
Name: kandang.Name,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return KandangGroupListDTO{
|
|
||||||
Id: e.Id,
|
|
||||||
Name: e.Name,
|
|
||||||
Status: e.Status,
|
|
||||||
Location: location,
|
|
||||||
Pic: pic,
|
|
||||||
CreatedAt: e.CreatedAt,
|
|
||||||
UpdatedAt: e.UpdatedAt,
|
|
||||||
CreatedUser: createdUser,
|
|
||||||
RecordingKandangs: recordingKandangs,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ToKandangGroupListDTOs(e []entity.KandangGroup) []KandangGroupListDTO {
|
|
||||||
result := make([]KandangGroupListDTO, len(e))
|
|
||||||
for i, r := range e {
|
|
||||||
result[i] = ToKandangGroupListDTO(r)
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func ToKandangGroupDetailDTO(e entity.KandangGroup) KandangGroupDetailDTO {
|
|
||||||
return KandangGroupDetailDTO{
|
|
||||||
KandangGroupListDTO: ToKandangGroupListDTO(e),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
package kandanggroups
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/go-playground/validator/v10"
|
|
||||||
"github.com/gofiber/fiber/v2"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
|
|
||||||
rKandangGroup "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandang-groups/repositories"
|
|
||||||
sKandangGroup "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandang-groups/services"
|
|
||||||
|
|
||||||
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
|
|
||||||
sUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
|
||||||
)
|
|
||||||
|
|
||||||
type KandangGroupModule struct{}
|
|
||||||
|
|
||||||
func (KandangGroupModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
|
|
||||||
kandangGroupRepo := rKandangGroup.NewKandangGroupRepository(db)
|
|
||||||
userRepo := rUser.NewUserRepository(db)
|
|
||||||
|
|
||||||
kandangGroupService := sKandangGroup.NewKandangGroupService(kandangGroupRepo, validate)
|
|
||||||
userService := sUser.NewUserService(userRepo, validate)
|
|
||||||
|
|
||||||
KandangGroupRoutes(router, userService, kandangGroupService)
|
|
||||||
}
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
package repository
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"gorm.io/gorm"
|
|
||||||
|
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
|
||||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
|
||||||
)
|
|
||||||
|
|
||||||
type KandangGroupRepository interface {
|
|
||||||
repository.BaseRepository[entity.KandangGroup]
|
|
||||||
LocationExists(ctx context.Context, locationId uint) (bool, error)
|
|
||||||
PicExists(ctx context.Context, picId uint) (bool, error)
|
|
||||||
NameExists(ctx context.Context, name string, excludeID *uint) (bool, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type KandangGroupRepositoryImpl struct {
|
|
||||||
*repository.BaseRepositoryImpl[entity.KandangGroup]
|
|
||||||
db *gorm.DB
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewKandangGroupRepository(db *gorm.DB) KandangGroupRepository {
|
|
||||||
return &KandangGroupRepositoryImpl{
|
|
||||||
BaseRepositoryImpl: repository.NewBaseRepository[entity.KandangGroup](db),
|
|
||||||
db: db,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *KandangGroupRepositoryImpl) LocationExists(ctx context.Context, locationId uint) (bool, error) {
|
|
||||||
return repository.Exists[entity.Location](ctx, r.db, locationId)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *KandangGroupRepositoryImpl) PicExists(ctx context.Context, picId uint) (bool, error) {
|
|
||||||
return repository.Exists[entity.User](ctx, r.db, picId)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *KandangGroupRepositoryImpl) NameExists(ctx context.Context, name string, excludeID *uint) (bool, error) {
|
|
||||||
return repository.ExistsByName[entity.KandangGroup](ctx, r.db, name, excludeID)
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
package kandanggroups
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/gofiber/fiber/v2"
|
|
||||||
|
|
||||||
m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
|
||||||
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandang-groups/controllers"
|
|
||||||
kandanggroup "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandang-groups/services"
|
|
||||||
user "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
|
||||||
)
|
|
||||||
|
|
||||||
func KandangGroupRoutes(v1 fiber.Router, u user.UserService, s kandanggroup.KandangGroupService) {
|
|
||||||
ctrl := controller.NewKandangGroupController(s)
|
|
||||||
|
|
||||||
route := v1.Group("/kandang-groups")
|
|
||||||
route.Use(m.Auth(u))
|
|
||||||
|
|
||||||
route.Get("/", ctrl.GetAll)
|
|
||||||
route.Post("/", m.RequirePermissions(m.P_KandangGroups), ctrl.CreateOne)
|
|
||||||
route.Get("/:id", m.RequirePermissions(m.P_KandangGroups), ctrl.GetOne)
|
|
||||||
route.Patch("/:id", m.RequirePermissions(m.P_KandangGroups), ctrl.UpdateOne)
|
|
||||||
route.Delete("/:id", m.RequirePermissions(m.P_KandangGroups), ctrl.DeleteOne)
|
|
||||||
}
|
|
||||||
@@ -1,242 +0,0 @@
|
|||||||
package service
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/go-playground/validator/v10"
|
|
||||||
"github.com/gofiber/fiber/v2"
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
|
|
||||||
common "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
|
||||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
|
||||||
m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
|
||||||
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandang-groups/repositories"
|
|
||||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandang-groups/validations"
|
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
|
||||||
)
|
|
||||||
|
|
||||||
type KandangGroupService interface {
|
|
||||||
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.KandangGroup, int64, error)
|
|
||||||
GetOne(ctx *fiber.Ctx, id uint) (*entity.KandangGroup, error)
|
|
||||||
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.KandangGroup, error)
|
|
||||||
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.KandangGroup, error)
|
|
||||||
DeleteOne(ctx *fiber.Ctx, id uint) error
|
|
||||||
}
|
|
||||||
|
|
||||||
type kandangGroupService struct {
|
|
||||||
Log *logrus.Logger
|
|
||||||
Validate *validator.Validate
|
|
||||||
Repository repository.KandangGroupRepository
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewKandangGroupService(repo repository.KandangGroupRepository, validate *validator.Validate) KandangGroupService {
|
|
||||||
return &kandangGroupService{
|
|
||||||
Log: utils.Log,
|
|
||||||
Validate: validate,
|
|
||||||
Repository: repo,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s kandangGroupService) withRelations(db *gorm.DB) *gorm.DB {
|
|
||||||
return db.
|
|
||||||
Preload("CreatedUser").
|
|
||||||
Preload("Location").
|
|
||||||
Preload("Pic").
|
|
||||||
Preload("Kandangs", func(tx *gorm.DB) *gorm.DB {
|
|
||||||
return tx.Select("id", "name", "kandang_group_id").Order("name ASC")
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s kandangGroupService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.KandangGroup, int64, error) {
|
|
||||||
if err := s.Validate.Struct(params); err != nil {
|
|
||||||
return nil, 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var scopeErr error
|
|
||||||
offset := (params.Page - 1) * params.Limit
|
|
||||||
|
|
||||||
kandangGroups, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
|
||||||
db = s.withRelations(db)
|
|
||||||
db, scopeErr = m.ApplyLocationScope(c, db, "kandang_groups.location_id")
|
|
||||||
if params.Search != "" {
|
|
||||||
db = db.Where("kandang_groups.name ILIKE ?", "%"+params.Search+"%")
|
|
||||||
}
|
|
||||||
if params.LocationId != 0 {
|
|
||||||
db = db.Where("kandang_groups.location_id = ?", params.LocationId)
|
|
||||||
}
|
|
||||||
if params.PicId != 0 {
|
|
||||||
db = db.Where("kandang_groups.pic_id = ?", params.PicId)
|
|
||||||
}
|
|
||||||
return db.Order("kandang_groups.created_at DESC").Order("kandang_groups.updated_at DESC")
|
|
||||||
})
|
|
||||||
|
|
||||||
if scopeErr != nil {
|
|
||||||
return nil, 0, scopeErr
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
s.Log.Errorf("Failed to get kandang groups: %+v", err)
|
|
||||||
return nil, 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return kandangGroups, total, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s kandangGroupService) GetOne(c *fiber.Ctx, id uint) (*entity.KandangGroup, error) {
|
|
||||||
var scopeErr error
|
|
||||||
|
|
||||||
kandangGroup, err := s.Repository.GetByID(c.Context(), id, func(db *gorm.DB) *gorm.DB {
|
|
||||||
db = s.withRelations(db)
|
|
||||||
db, scopeErr = m.ApplyLocationScope(c, db, "kandang_groups.location_id")
|
|
||||||
return db
|
|
||||||
})
|
|
||||||
if scopeErr != nil {
|
|
||||||
return nil, scopeErr
|
|
||||||
}
|
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
||||||
return nil, fiber.NewError(fiber.StatusNotFound, "Kandang group not found")
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
s.Log.Errorf("Failed to get kandang group by id: %+v", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return kandangGroup, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *kandangGroupService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.KandangGroup, error) {
|
|
||||||
if err := s.Validate.Struct(req); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if err := m.EnsureLocationAccess(c, s.Repository.DB(), req.LocationId); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if exists, err := s.Repository.NameExists(c.Context(), req.Name, nil); err != nil {
|
|
||||||
s.Log.Errorf("Failed to check kandang group name: %+v", err)
|
|
||||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check kandang group name")
|
|
||||||
} else if exists {
|
|
||||||
return nil, fiber.NewError(fiber.StatusConflict, fmt.Sprintf("Kandang group with name %s already exists", req.Name))
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := common.EnsureRelations(c.Context(),
|
|
||||||
common.RelationCheck{Name: "Location", ID: &req.LocationId, Exists: s.Repository.LocationExists},
|
|
||||||
common.RelationCheck{Name: "Pic", ID: &req.PicId, Exists: s.Repository.PicExists},
|
|
||||||
); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
status := strings.ToUpper(strings.TrimSpace(req.Status))
|
|
||||||
if status == "" {
|
|
||||||
status = string(utils.KandangStatusNonActive)
|
|
||||||
}
|
|
||||||
if !utils.IsValidKandangStatus(status) {
|
|
||||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid kandang group status")
|
|
||||||
}
|
|
||||||
|
|
||||||
actorID, err := m.ActorIDFromContext(c)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
createBody := &entity.KandangGroup{
|
|
||||||
Name: req.Name,
|
|
||||||
Status: status,
|
|
||||||
LocationId: req.LocationId,
|
|
||||||
PicId: req.PicId,
|
|
||||||
CreatedBy: actorID,
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := s.Repository.CreateOne(c.Context(), createBody, nil); err != nil {
|
|
||||||
s.Log.Errorf("Failed to create kandang group: %+v", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return s.GetOne(c, createBody.Id)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s kandangGroupService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.KandangGroup, error) {
|
|
||||||
if err := s.Validate.Struct(req); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
existing, err := s.GetOne(c, id)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if req.LocationId != nil {
|
|
||||||
if err := m.EnsureLocationAccess(c, s.Repository.DB(), *req.LocationId); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
updateBody := make(map[string]any)
|
|
||||||
|
|
||||||
if req.Name != nil {
|
|
||||||
if exists, err := s.Repository.NameExists(c.Context(), *req.Name, &id); err != nil {
|
|
||||||
s.Log.Errorf("Failed to check kandang group name: %+v", err)
|
|
||||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check kandang group name")
|
|
||||||
} else if exists {
|
|
||||||
return nil, fiber.NewError(fiber.StatusConflict, fmt.Sprintf("Kandang group with name %s already exists", *req.Name))
|
|
||||||
}
|
|
||||||
updateBody["name"] = *req.Name
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := common.EnsureRelations(c.Context(),
|
|
||||||
common.RelationCheck{Name: "Location", ID: req.LocationId, Exists: s.Repository.LocationExists},
|
|
||||||
common.RelationCheck{Name: "Pic", ID: req.PicId, Exists: s.Repository.PicExists},
|
|
||||||
); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if req.LocationId != nil {
|
|
||||||
updateBody["location_id"] = *req.LocationId
|
|
||||||
}
|
|
||||||
if req.PicId != nil {
|
|
||||||
updateBody["pic_id"] = *req.PicId
|
|
||||||
}
|
|
||||||
if req.Status != nil {
|
|
||||||
status := strings.ToUpper(strings.TrimSpace(*req.Status))
|
|
||||||
if !utils.IsValidKandangStatus(status) {
|
|
||||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid kandang group status")
|
|
||||||
}
|
|
||||||
updateBody["status"] = status
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(updateBody) == 0 {
|
|
||||||
return existing, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := s.Repository.PatchOne(c.Context(), id, updateBody, nil); err != nil {
|
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
||||||
return nil, fiber.NewError(fiber.StatusNotFound, "Kandang group not found")
|
|
||||||
}
|
|
||||||
s.Log.Errorf("Failed to update kandang group: %+v", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return s.GetOne(c, id)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s kandangGroupService) DeleteOne(c *fiber.Ctx, id uint) error {
|
|
||||||
kandangGroup, err := s.GetOne(c, id)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if len(kandangGroup.Kandangs) > 0 {
|
|
||||||
return fiber.NewError(fiber.StatusConflict, "Kandang group tidak boleh dihapus karena masih memiliki relasi kandang")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := s.Repository.DeleteOne(c.Context(), id); err != nil {
|
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
||||||
return fiber.NewError(fiber.StatusNotFound, "Kandang group not found")
|
|
||||||
}
|
|
||||||
s.Log.Errorf("Failed to delete kandang group: %+v", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
package validation
|
|
||||||
|
|
||||||
type Create struct {
|
|
||||||
Name string `json:"name" validate:"required_strict,min=3,max=50"`
|
|
||||||
Status string `json:"status,omitempty" validate:"omitempty,min=3,max=50"`
|
|
||||||
LocationId uint `json:"location_id" validate:"required_strict,number,gt=0"`
|
|
||||||
PicId uint `json:"pic_id" validate:"required_strict,number,gt=0"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Update struct {
|
|
||||||
Name *string `json:"name,omitempty" validate:"omitempty,max=50"`
|
|
||||||
Status *string `json:"status,omitempty" validate:"omitempty,min=3,max=50"`
|
|
||||||
LocationId *uint `json:"location_id,omitempty" validate:"omitempty,number,gt=0"`
|
|
||||||
PicId *uint `json:"pic_id,omitempty" validate:"omitempty,number,gt=0"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Query struct {
|
|
||||||
Page int `query:"page" validate:"omitempty,number,min=1"`
|
|
||||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=500"`
|
|
||||||
Search string `query:"search" validate:"omitempty,max=50"`
|
|
||||||
LocationId int `query:"location_id" validate:"omitempty,number,gt=0"`
|
|
||||||
PicId int `query:"pic_id" validate:"omitempty,number,gt=0"`
|
|
||||||
}
|
|
||||||
@@ -15,23 +15,15 @@ type KandangRelationDTO struct {
|
|||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
Capacity float64 `json:"capacity"`
|
Capacity float64 `json:"capacity"`
|
||||||
KandangGroup *KandangGroupRelationDTO `json:"kandang_group,omitempty"`
|
|
||||||
Location *locationDTO.LocationRelationDTO `json:"location,omitempty"`
|
Location *locationDTO.LocationRelationDTO `json:"location,omitempty"`
|
||||||
Pic *userDTO.UserRelationDTO `json:"pic,omitempty"`
|
Pic *userDTO.UserRelationDTO `json:"pic,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type KandangGroupRelationDTO struct {
|
|
||||||
Id uint `json:"id"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
Status string `json:"status"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type KandangListDTO struct {
|
type KandangListDTO struct {
|
||||||
Id uint `json:"id"`
|
Id uint `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
Capacity float64 `json:"capacity"`
|
Capacity float64 `json:"capacity"`
|
||||||
KandangGroup KandangGroupRelationDTO `json:"kandang_group"`
|
|
||||||
Location locationDTO.LocationRelationDTO `json:"location"`
|
Location locationDTO.LocationRelationDTO `json:"location"`
|
||||||
Pic userDTO.UserRelationDTO `json:"pic"`
|
Pic userDTO.UserRelationDTO `json:"pic"`
|
||||||
CreatedUser userDTO.UserRelationDTO `json:"created_user"`
|
CreatedUser userDTO.UserRelationDTO `json:"created_user"`
|
||||||
@@ -46,12 +38,6 @@ type KandangDetailDTO struct {
|
|||||||
// === Mapper Functions ===
|
// === Mapper Functions ===
|
||||||
|
|
||||||
func ToKandangRelationDTO(e entity.Kandang) KandangRelationDTO {
|
func ToKandangRelationDTO(e entity.Kandang) KandangRelationDTO {
|
||||||
var kandangGroup *KandangGroupRelationDTO
|
|
||||||
if e.KandangGroup.Id != 0 {
|
|
||||||
mapped := ToKandangGroupRelationDTO(e.KandangGroup)
|
|
||||||
kandangGroup = &mapped
|
|
||||||
}
|
|
||||||
|
|
||||||
var location *locationDTO.LocationRelationDTO
|
var location *locationDTO.LocationRelationDTO
|
||||||
if e.Location.Id != 0 {
|
if e.Location.Id != 0 {
|
||||||
mapped := locationDTO.ToLocationRelationDTO(e.Location)
|
mapped := locationDTO.ToLocationRelationDTO(e.Location)
|
||||||
@@ -69,27 +55,12 @@ func ToKandangRelationDTO(e entity.Kandang) KandangRelationDTO {
|
|||||||
Name: e.Name,
|
Name: e.Name,
|
||||||
Status: e.Status,
|
Status: e.Status,
|
||||||
Capacity: e.Capacity,
|
Capacity: e.Capacity,
|
||||||
KandangGroup: kandangGroup,
|
|
||||||
Location: location,
|
Location: location,
|
||||||
Pic: pic,
|
Pic: pic,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ToKandangGroupRelationDTO(e entity.KandangGroup) KandangGroupRelationDTO {
|
|
||||||
return KandangGroupRelationDTO{
|
|
||||||
Id: e.Id,
|
|
||||||
Name: e.Name,
|
|
||||||
Status: e.Status,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ToKandangListDTO(e entity.Kandang) KandangListDTO {
|
func ToKandangListDTO(e entity.Kandang) KandangListDTO {
|
||||||
var kandangGroup KandangGroupRelationDTO
|
|
||||||
if e.KandangGroup.Id != 0 {
|
|
||||||
mapped := ToKandangGroupRelationDTO(e.KandangGroup)
|
|
||||||
kandangGroup = mapped
|
|
||||||
}
|
|
||||||
|
|
||||||
var location locationDTO.LocationRelationDTO
|
var location locationDTO.LocationRelationDTO
|
||||||
if e.Location.Id != 0 {
|
if e.Location.Id != 0 {
|
||||||
mapped := locationDTO.ToLocationRelationDTO(e.Location)
|
mapped := locationDTO.ToLocationRelationDTO(e.Location)
|
||||||
@@ -113,7 +84,6 @@ func ToKandangListDTO(e entity.Kandang) KandangListDTO {
|
|||||||
Name: e.Name,
|
Name: e.Name,
|
||||||
Status: e.Status,
|
Status: e.Status,
|
||||||
Location: location,
|
Location: location,
|
||||||
KandangGroup: kandangGroup,
|
|
||||||
Capacity: e.Capacity,
|
Capacity: e.Capacity,
|
||||||
Pic: pic,
|
Pic: pic,
|
||||||
CreatedAt: e.CreatedAt,
|
CreatedAt: e.CreatedAt,
|
||||||
|
|||||||
@@ -4,10 +4,10 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -15,7 +15,6 @@ type KandangRepository interface {
|
|||||||
repository.BaseRepository[entity.Kandang]
|
repository.BaseRepository[entity.Kandang]
|
||||||
LocationExists(ctx context.Context, areaId uint) (bool, error)
|
LocationExists(ctx context.Context, areaId uint) (bool, error)
|
||||||
PicExists(ctx context.Context, areaId uint) (bool, error)
|
PicExists(ctx context.Context, areaId uint) (bool, error)
|
||||||
KandangGroupExists(ctx context.Context, kandangGroupId uint) (bool, error)
|
|
||||||
NameExists(ctx context.Context, name string, excludeID *uint) (bool, error)
|
NameExists(ctx context.Context, name string, excludeID *uint) (bool, error)
|
||||||
ProjectFlockExists(ctx context.Context, projectFlockID uint) (bool, error)
|
ProjectFlockExists(ctx context.Context, projectFlockID uint) (bool, error)
|
||||||
GetFirstByProjectFlockID(ctx context.Context, projectFlockID uint) (*entity.Kandang, error)
|
GetFirstByProjectFlockID(ctx context.Context, projectFlockID uint) (*entity.Kandang, error)
|
||||||
@@ -46,10 +45,6 @@ func (r *KandangRepositoryImpl) PicExists(ctx context.Context, picId uint) (bool
|
|||||||
return repository.Exists[entity.User](ctx, r.db, picId)
|
return repository.Exists[entity.User](ctx, r.db, picId)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *KandangRepositoryImpl) KandangGroupExists(ctx context.Context, kandangGroupId uint) (bool, error) {
|
|
||||||
return repository.Exists[entity.KandangGroup](ctx, r.db, kandangGroupId)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *KandangRepositoryImpl) NameExists(ctx context.Context, name string, excludeID *uint) (bool, error) {
|
func (r *KandangRepositoryImpl) NameExists(ctx context.Context, name string, excludeID *uint) (bool, error) {
|
||||||
return repository.ExistsByName[entity.Kandang](ctx, r.db, name, excludeID)
|
return repository.ExistsByName[entity.Kandang](ctx, r.db, name, excludeID)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,14 +3,13 @@ package service
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
|
|
||||||
common "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
common "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"
|
||||||
m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
||||||
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/repositories"
|
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/repositories"
|
||||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/validations"
|
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/validations"
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/go-playground/validator/v10"
|
"github.com/go-playground/validator/v10"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
@@ -41,7 +40,7 @@ func NewKandangService(repo repository.KandangRepository, validate *validator.Va
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s kandangService) withRelations(db *gorm.DB) *gorm.DB {
|
func (s kandangService) withRelations(db *gorm.DB) *gorm.DB {
|
||||||
return db.Preload("CreatedUser").Preload("Location").Preload("KandangGroup").Preload("Pic").Preload("ProjectFlockKandangs.ProjectFlock")
|
return db.Preload("CreatedUser").Preload("Location").Preload("Pic").Preload("ProjectFlockKandangs.ProjectFlock")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,28 +99,6 @@ func (s kandangService) GetOne(c *fiber.Ctx, id uint) (*entity.Kandang, error) {
|
|||||||
return kandang, nil
|
return kandang, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s kandangService) ensureKandangGroupAccess(c *fiber.Ctx, groupID uint, expectedLocationID *uint) error {
|
|
||||||
var kandangGroup entity.KandangGroup
|
|
||||||
if err := s.Repository.DB().WithContext(c.Context()).
|
|
||||||
Select("id", "location_id").
|
|
||||||
First(&kandangGroup, groupID).Error; err != nil {
|
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
||||||
return fiber.NewError(fiber.StatusNotFound, "Kandang group not found")
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := m.EnsureLocationAccess(c, s.Repository.DB(), kandangGroup.LocationId); err != nil {
|
|
||||||
return fiber.NewError(fiber.StatusNotFound, "Kandang group not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
if expectedLocationID != nil && kandangGroup.LocationId != *expectedLocationID {
|
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "Kandang group location must match kandang location")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *kandangService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.Kandang, error) {
|
func (s *kandangService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.Kandang, error) {
|
||||||
if err := s.Validate.Struct(req); err != nil {
|
if err := s.Validate.Struct(req); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -139,14 +116,10 @@ func (s *kandangService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
|
|||||||
|
|
||||||
if err := common.EnsureRelations(c.Context(),
|
if err := common.EnsureRelations(c.Context(),
|
||||||
common.RelationCheck{Name: "Location", ID: &req.LocationId, Exists: s.Repository.LocationExists},
|
common.RelationCheck{Name: "Location", ID: &req.LocationId, Exists: s.Repository.LocationExists},
|
||||||
common.RelationCheck{Name: "KandangGroup", ID: &req.GroupId, Exists: s.Repository.KandangGroupExists},
|
|
||||||
common.RelationCheck{Name: "Pic", ID: &req.PicId, Exists: s.Repository.PicExists},
|
common.RelationCheck{Name: "Pic", ID: &req.PicId, Exists: s.Repository.PicExists},
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err := s.ensureKandangGroupAccess(c, req.GroupId, &req.LocationId); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
status := strings.ToUpper(strings.TrimSpace(req.Status))
|
status := strings.ToUpper(strings.TrimSpace(req.Status))
|
||||||
if status == "" {
|
if status == "" {
|
||||||
@@ -183,7 +156,6 @@ func (s *kandangService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
|
|||||||
createBody := &entity.Kandang{
|
createBody := &entity.Kandang{
|
||||||
Name: req.Name,
|
Name: req.Name,
|
||||||
LocationId: req.LocationId,
|
LocationId: req.LocationId,
|
||||||
KandangGroupId: req.GroupId,
|
|
||||||
Capacity: req.Capacity,
|
Capacity: req.Capacity,
|
||||||
Status: status,
|
Status: status,
|
||||||
PicId: req.PicId,
|
PicId: req.PicId,
|
||||||
@@ -240,7 +212,6 @@ func (s kandangService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
|||||||
|
|
||||||
if err := common.EnsureRelations(c.Context(),
|
if err := common.EnsureRelations(c.Context(),
|
||||||
common.RelationCheck{Name: "Location", ID: req.LocationId, Exists: s.Repository.LocationExists},
|
common.RelationCheck{Name: "Location", ID: req.LocationId, Exists: s.Repository.LocationExists},
|
||||||
common.RelationCheck{Name: "KandangGroup", ID: req.GroupId, Exists: s.Repository.KandangGroupExists},
|
|
||||||
common.RelationCheck{Name: "Pic", ID: req.PicId, Exists: s.Repository.PicExists},
|
common.RelationCheck{Name: "Pic", ID: req.PicId, Exists: s.Repository.PicExists},
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -249,16 +220,6 @@ func (s kandangService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
|||||||
if req.LocationId != nil {
|
if req.LocationId != nil {
|
||||||
updateBody["location_id"] = *req.LocationId
|
updateBody["location_id"] = *req.LocationId
|
||||||
}
|
}
|
||||||
if req.GroupId != nil {
|
|
||||||
targetLocationID := existing.LocationId
|
|
||||||
if req.LocationId != nil {
|
|
||||||
targetLocationID = *req.LocationId
|
|
||||||
}
|
|
||||||
if err := s.ensureKandangGroupAccess(c, *req.GroupId, &targetLocationID); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
updateBody["kandang_group_id"] = *req.GroupId
|
|
||||||
}
|
|
||||||
|
|
||||||
if req.PicId != nil {
|
if req.PicId != nil {
|
||||||
updateBody["pic_id"] = *req.PicId
|
updateBody["pic_id"] = *req.PicId
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ type Create struct {
|
|||||||
Status string `json:"status,omitempty" validate:"omitempty,min=3,max=50"`
|
Status string `json:"status,omitempty" validate:"omitempty,min=3,max=50"`
|
||||||
Capacity float64 `json:"capacity" validate:"required_strict,gt=0"`
|
Capacity float64 `json:"capacity" validate:"required_strict,gt=0"`
|
||||||
LocationId uint `json:"location_id" validate:"required_strict,number,gt=0"`
|
LocationId uint `json:"location_id" validate:"required_strict,number,gt=0"`
|
||||||
GroupId uint `json:"group_id" validate:"required_strict,number,gt=0"`
|
|
||||||
PicId uint `json:"pic_id" validate:"required_strict,number,gt=0"`
|
PicId uint `json:"pic_id" validate:"required_strict,number,gt=0"`
|
||||||
ProjectFlockId *uint `json:"project_flock_id" validate:"omitempty,number,gt=0"`
|
ProjectFlockId *uint `json:"project_flock_id" validate:"omitempty,number,gt=0"`
|
||||||
}
|
}
|
||||||
@@ -15,7 +14,6 @@ type Update struct {
|
|||||||
Status *string `json:"status,omitempty" validate:"omitempty,min=3,max=50"`
|
Status *string `json:"status,omitempty" validate:"omitempty,min=3,max=50"`
|
||||||
Capacity *float64 `json:"capacity" validate:"omitempty,gt=0"`
|
Capacity *float64 `json:"capacity" validate:"omitempty,gt=0"`
|
||||||
LocationId *uint `json:"location_id,omitempty" validate:"omitempty,number,gt=0"`
|
LocationId *uint `json:"location_id,omitempty" validate:"omitempty,number,gt=0"`
|
||||||
GroupId *uint `json:"group_id" validate:"required_strict,number,gt=0"`
|
|
||||||
PicId *uint `json:"pic_id,omitempty" validate:"omitempty,number,gt=0"`
|
PicId *uint `json:"pic_id,omitempty" validate:"omitempty,number,gt=0"`
|
||||||
ProjectFlockId *uint `json:"project_flock_id,omitempty" validate:"omitempty,number,gt=0"`
|
ProjectFlockId *uint `json:"project_flock_id,omitempty" validate:"omitempty,number,gt=0"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,6 @@ func (u *LocationController) GetAll(c *fiber.Ctx) error {
|
|||||||
Limit: c.QueryInt("limit", 10),
|
Limit: c.QueryInt("limit", 10),
|
||||||
Search: c.Query("search", ""),
|
Search: c.Query("search", ""),
|
||||||
AreaId: c.QueryInt("area_id", 0),
|
AreaId: c.QueryInt("area_id", 0),
|
||||||
HasLaying: c.QueryBool("has_laying", false),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if query.Page < 1 || query.Limit < 1 {
|
if query.Page < 1 || query.Limit < 1 {
|
||||||
|
|||||||
@@ -60,17 +60,6 @@ func (s locationService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entit
|
|||||||
if params.AreaId != 0 {
|
if params.AreaId != 0 {
|
||||||
db = db.Where("area_id = ?", params.AreaId)
|
db = db.Where("area_id = ?", params.AreaId)
|
||||||
}
|
}
|
||||||
if params.HasLaying {
|
|
||||||
db = db.Where(`
|
|
||||||
EXISTS (
|
|
||||||
SELECT 1
|
|
||||||
FROM project_flocks pf
|
|
||||||
WHERE pf.location_id = locations.id
|
|
||||||
AND pf.category = ?
|
|
||||||
AND pf.deleted_at IS NULL
|
|
||||||
)
|
|
||||||
`, utils.ProjectFlockCategoryLaying)
|
|
||||||
}
|
|
||||||
return db.Order("created_at DESC").Order("updated_at DESC")
|
return db.Order("created_at DESC").Order("updated_at DESC")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -17,5 +17,4 @@ type Query struct {
|
|||||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=500"`
|
Limit int `query:"limit" validate:"omitempty,number,min=1,max=500"`
|
||||||
Search string `query:"search" validate:"omitempty,max=50"`
|
Search string `query:"search" validate:"omitempty,max=50"`
|
||||||
AreaId int `query:"area_id" validate:"omitempty,number,gt=0"`
|
AreaId int `query:"area_id" validate:"omitempty,number,gt=0"`
|
||||||
HasLaying bool `query:"has_laying"`
|
|
||||||
}
|
}
|
||||||
|
|||||||
+17
-8
@@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/config"
|
||||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
||||||
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/master/production-standards/repositories"
|
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/master/production-standards/repositories"
|
||||||
@@ -343,17 +344,22 @@ func (s productionStandardService) EnsureWeekStart(ctx context.Context, standard
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
layingWeekStart := config.LayingWeekStart()
|
||||||
|
|
||||||
switch strings.ToUpper(category) {
|
switch strings.ToUpper(category) {
|
||||||
case string(utils.ProjectFlockCategoryLaying):
|
case string(utils.ProjectFlockCategoryLaying):
|
||||||
details, err := s.ProductionStandardDetailRepo.GetByProductionStandardID(ctx, standardID)
|
details, err := s.ProductionStandardDetailRepo.GetByProductionStandardID(ctx, standardID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
startWeek := 0
|
if len(details) == 0 {
|
||||||
if len(details) > 0 {
|
return fiber.NewError(
|
||||||
startWeek = details[0].Week
|
fiber.StatusBadRequest,
|
||||||
|
"Standart production tidak tersedia untuk kategori laying",
|
||||||
|
)
|
||||||
}
|
}
|
||||||
if startWeek != 18 {
|
startWeek := details[0].Week
|
||||||
|
if startWeek > layingWeekStart {
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "Week tidak sesuai dengan standart kategori project flock")
|
return fiber.NewError(fiber.StatusBadRequest, "Week tidak sesuai dengan standart kategori project flock")
|
||||||
}
|
}
|
||||||
case string(utils.ProjectFlockCategoryGrowing):
|
case string(utils.ProjectFlockCategoryGrowing):
|
||||||
@@ -361,10 +367,13 @@ func (s productionStandardService) EnsureWeekStart(ctx context.Context, standard
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
startWeek := 0
|
if len(details) == 0 {
|
||||||
if len(details) > 0 {
|
return fiber.NewError(
|
||||||
startWeek = details[0].Week
|
fiber.StatusBadRequest,
|
||||||
|
"Standart production tidak tersedia untuk kategori growing",
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
startWeek := details[0].Week
|
||||||
if startWeek != 1 {
|
if startWeek != 1 {
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "Week tidak sesuai dengan standart kategori project flock")
|
return fiber.NewError(fiber.StatusBadRequest, "Week tidak sesuai dengan standart kategori project flock")
|
||||||
}
|
}
|
||||||
@@ -381,7 +390,7 @@ func (s productionStandardService) EnsureWeekAvailable(ctx context.Context, stan
|
|||||||
upperCategory := strings.ToUpper(category)
|
upperCategory := strings.ToUpper(category)
|
||||||
weekBase := 1
|
weekBase := 1
|
||||||
if upperCategory == string(utils.ProjectFlockCategoryLaying) {
|
if upperCategory == string(utils.ProjectFlockCategoryLaying) {
|
||||||
weekBase = 18
|
weekBase = config.LayingWeekStart()
|
||||||
}
|
}
|
||||||
week := ((day - 1) / 7) + weekBase
|
week := ((day - 1) / 7) + weekBase
|
||||||
if week <= 0 {
|
if week <= 0 {
|
||||||
|
|||||||
@@ -38,14 +38,6 @@ func (u *ProductController) GetAll(c *fiber.Ctx) error {
|
|||||||
query.IsDepletion = &value
|
query.IsDepletion = &value
|
||||||
}
|
}
|
||||||
|
|
||||||
if includeAllParam := c.Query("include_all", ""); includeAllParam != "" {
|
|
||||||
value, err := strconv.ParseBool(includeAllParam)
|
|
||||||
if err != nil {
|
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "invalid include_all value")
|
|
||||||
}
|
|
||||||
query.IncludeAll = &value
|
|
||||||
}
|
|
||||||
|
|
||||||
if query.Page < 1 || query.Limit < 1 {
|
if query.Page < 1 || query.Limit < 1 {
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0")
|
return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -229,9 +229,9 @@ func (s productService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity
|
|||||||
|
|
||||||
products, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
products, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
||||||
db = s.withRelations(db)
|
db = s.withRelations(db)
|
||||||
// Default: show only visible products.
|
// Depletion master products are system products and often stored with is_visible = false.
|
||||||
// include_all=true can be used to fetch all records (including hidden/system products).
|
// When requested explicitly via is_depletion=true, include hidden records.
|
||||||
if params.IncludeAll == nil || !*params.IncludeAll {
|
if params.IsDepletion == nil || !*params.IsDepletion {
|
||||||
db = db.Where("is_visible = ?", true)
|
db = db.Where("is_visible = ?", true)
|
||||||
}
|
}
|
||||||
if params.Search != "" {
|
if params.Search != "" {
|
||||||
|
|||||||
@@ -45,5 +45,4 @@ type Query struct {
|
|||||||
Search string `query:"search" validate:"omitempty,max=50"`
|
Search string `query:"search" validate:"omitempty,max=50"`
|
||||||
ProductCategoryID int `query:"product_category_id" validate:"omitempty,number,min=1"`
|
ProductCategoryID int `query:"product_category_id" validate:"omitempty,number,min=1"`
|
||||||
IsDepletion *bool `query:"is_depletion" validate:"omitempty"`
|
IsDepletion *bool `query:"is_depletion" validate:"omitempty"`
|
||||||
IncludeAll *bool `query:"include_all" validate:"omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,12 +9,10 @@ import (
|
|||||||
|
|
||||||
areas "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas"
|
areas "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas"
|
||||||
banks "gitlab.com/mbugroup/lti-api.git/internal/modules/master/banks"
|
banks "gitlab.com/mbugroup/lti-api.git/internal/modules/master/banks"
|
||||||
configChecklists "gitlab.com/mbugroup/lti-api.git/internal/modules/master/config-checklists"
|
|
||||||
customers "gitlab.com/mbugroup/lti-api.git/internal/modules/master/customers"
|
customers "gitlab.com/mbugroup/lti-api.git/internal/modules/master/customers"
|
||||||
employeess "gitlab.com/mbugroup/lti-api.git/internal/modules/master/employees"
|
employeess "gitlab.com/mbugroup/lti-api.git/internal/modules/master/employees"
|
||||||
fcrs "gitlab.com/mbugroup/lti-api.git/internal/modules/master/fcrs"
|
fcrs "gitlab.com/mbugroup/lti-api.git/internal/modules/master/fcrs"
|
||||||
flocks "gitlab.com/mbugroup/lti-api.git/internal/modules/master/flocks"
|
flocks "gitlab.com/mbugroup/lti-api.git/internal/modules/master/flocks"
|
||||||
kandanggroups "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandang-groups"
|
|
||||||
kandangs "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs"
|
kandangs "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs"
|
||||||
locations "gitlab.com/mbugroup/lti-api.git/internal/modules/master/locations"
|
locations "gitlab.com/mbugroup/lti-api.git/internal/modules/master/locations"
|
||||||
nonstocks "gitlab.com/mbugroup/lti-api.git/internal/modules/master/nonstocks"
|
nonstocks "gitlab.com/mbugroup/lti-api.git/internal/modules/master/nonstocks"
|
||||||
@@ -26,6 +24,7 @@ import (
|
|||||||
suppliers "gitlab.com/mbugroup/lti-api.git/internal/modules/master/suppliers"
|
suppliers "gitlab.com/mbugroup/lti-api.git/internal/modules/master/suppliers"
|
||||||
uoms "gitlab.com/mbugroup/lti-api.git/internal/modules/master/uoms"
|
uoms "gitlab.com/mbugroup/lti-api.git/internal/modules/master/uoms"
|
||||||
warehouses "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses"
|
warehouses "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses"
|
||||||
|
configChecklists "gitlab.com/mbugroup/lti-api.git/internal/modules/master/config-checklists"
|
||||||
// MODULE IMPORTS
|
// MODULE IMPORTS
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -36,7 +35,6 @@ func RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Valida
|
|||||||
uoms.UomModule{},
|
uoms.UomModule{},
|
||||||
areas.AreaModule{},
|
areas.AreaModule{},
|
||||||
locations.LocationModule{},
|
locations.LocationModule{},
|
||||||
kandanggroups.KandangGroupModule{},
|
|
||||||
kandangs.KandangModule{},
|
kandangs.KandangModule{},
|
||||||
warehouses.WarehouseModule{},
|
warehouses.WarehouseModule{},
|
||||||
customers.CustomerModule{},
|
customers.CustomerModule{},
|
||||||
|
|||||||
@@ -151,25 +151,25 @@ func (u *ChickinController) GetOne(c *fiber.Ctx) error {
|
|||||||
// })
|
// })
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// func (u *ChickinController) DeleteOne(c *fiber.Ctx) error {
|
func (u *ChickinController) DeleteOne(c *fiber.Ctx) error {
|
||||||
// param := c.Params("id")
|
param := c.Params("id")
|
||||||
|
|
||||||
// id, err := strconv.Atoi(param)
|
id, err := strconv.Atoi(param)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||||
// }
|
}
|
||||||
|
|
||||||
// if err := u.ChickinService.DeleteOne(c, uint(id)); err != nil {
|
if err := u.ChickinService.DeleteOne(c, uint(id)); err != nil {
|
||||||
// return err
|
return err
|
||||||
// }
|
}
|
||||||
|
|
||||||
// return c.Status(fiber.StatusOK).
|
return c.Status(fiber.StatusOK).
|
||||||
// JSON(response.Common{
|
JSON(response.Common{
|
||||||
// Code: fiber.StatusOK,
|
Code: fiber.StatusOK,
|
||||||
// Status: "success",
|
Status: "success",
|
||||||
// Message: "Delete chickin successfully",
|
Message: "Delete chickin successfully",
|
||||||
// })
|
})
|
||||||
// }
|
}
|
||||||
|
|
||||||
func (u *ChickinController) Approval(c *fiber.Ctx) error {
|
func (u *ChickinController) Approval(c *fiber.Ctx) error {
|
||||||
req := new(validation.Approve)
|
req := new(validation.Approve)
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package dto
|
|||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/config"
|
||||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
areaRelationDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas/dto"
|
areaRelationDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas/dto"
|
||||||
flockRelationDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/flocks/dto"
|
flockRelationDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/flocks/dto"
|
||||||
@@ -219,7 +220,7 @@ func resolveProjectFlockStandardFcr(e entity.ProjectFlock) *float64 {
|
|||||||
}
|
}
|
||||||
week := 1
|
week := 1
|
||||||
if e.Category == string(utils.ProjectFlockCategoryLaying) {
|
if e.Category == string(utils.ProjectFlockCategoryLaying) {
|
||||||
week = 18
|
week = config.LayingWeekStart()
|
||||||
}
|
}
|
||||||
for _, detail := range e.ProductionStandard.ProductionStandardDetails {
|
for _, detail := range e.ProductionStandard.ProductionStandardDetails {
|
||||||
if detail.Week == week && detail.StandardFCR != nil {
|
if detail.Week == week && detail.StandardFCR != nil {
|
||||||
|
|||||||
@@ -19,6 +19,6 @@ func ChickinRoutes(v1 fiber.Router, u user.UserService, s chickin.ChickinService
|
|||||||
route.Post("/",m.RequirePermissions(m.P_ChickinsCreateOne), ctrl.CreateOne)
|
route.Post("/",m.RequirePermissions(m.P_ChickinsCreateOne), ctrl.CreateOne)
|
||||||
route.Get("/:id",m.RequirePermissions(m.P_ChickinsGetOne), ctrl.GetOne)
|
route.Get("/:id",m.RequirePermissions(m.P_ChickinsGetOne), ctrl.GetOne)
|
||||||
// route.Patch("/:id", ctrl.UpdateOne)
|
// route.Patch("/:id", ctrl.UpdateOne)
|
||||||
// route.Delete("/:id", ctrl.DeleteOne)
|
route.Delete("/:id", ctrl.DeleteOne)
|
||||||
route.Post("/approvals",m.RequirePermissions(m.P_ChickinsApproval), ctrl.Approval)
|
route.Post("/approvals",m.RequirePermissions(m.P_ChickinsApproval), ctrl.Approval)
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -7,14 +7,14 @@ import (
|
|||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
|
||||||
sProjectFlockKandang "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project-flock-kandangs/services"
|
|
||||||
rProjectFlockKandang "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
|
|
||||||
rKandang "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/repositories"
|
|
||||||
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"
|
||||||
rExpense "gitlab.com/mbugroup/lti-api.git/internal/modules/expenses/repositories"
|
rExpense "gitlab.com/mbugroup/lti-api.git/internal/modules/expenses/repositories"
|
||||||
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"
|
||||||
|
rKandang "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/repositories"
|
||||||
rWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/repositories"
|
rWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/repositories"
|
||||||
|
sProjectFlockKandang "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project-flock-kandangs/services"
|
||||||
|
rProjectFlockKandang "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
|
|
||||||
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
|
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
|
||||||
@@ -33,13 +33,14 @@ func (ProjectFlockKandangModule) RegisterRoutes(router fiber.Router, db *gorm.DB
|
|||||||
|
|
||||||
approvalRepo := commonRepo.NewApprovalRepository(db)
|
approvalRepo := commonRepo.NewApprovalRepository(db)
|
||||||
approvalService := commonSvc.NewApprovalService(approvalRepo)
|
approvalService := commonSvc.NewApprovalService(approvalRepo)
|
||||||
|
fifoStockV2Service := commonSvc.NewFifoStockV2Service(db, utils.Log)
|
||||||
// register workflow steps for chickin approvals
|
// register workflow steps for chickin approvals
|
||||||
if err := approvalService.RegisterWorkflowSteps(utils.ApprovalWorkflowProjectFlockKandang, utils.ProjectFlockKandangApprovalSteps); err != nil {
|
if err := approvalService.RegisterWorkflowSteps(utils.ApprovalWorkflowProjectFlockKandang, utils.ProjectFlockKandangApprovalSteps); err != nil {
|
||||||
panic(fmt.Sprintf("failed to register chickin approval workflow: %v", err))
|
panic(fmt.Sprintf("failed to register chickin approval workflow: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
expenseRepo := rExpense.NewExpenseRepository(db)
|
expenseRepo := rExpense.NewExpenseRepository(db)
|
||||||
projectFlockKandangService := sProjectFlockKandang.NewProjectFlockKandangService(projectFlockKandangRepo, approvalService, expenseRepo, warehouseRepo, productWarehouseRepo, projectFlockPopulationRepo,kandangRepo, validate)
|
projectFlockKandangService := sProjectFlockKandang.NewProjectFlockKandangService(projectFlockKandangRepo, approvalService, fifoStockV2Service, expenseRepo, warehouseRepo, productWarehouseRepo, projectFlockPopulationRepo, kandangRepo, validate)
|
||||||
userService := sUser.NewUserService(userRepo, validate)
|
userService := sUser.NewUserService(userRepo, validate)
|
||||||
|
|
||||||
ProjectFlockKandangRoutes(router, userService, projectFlockKandangService)
|
ProjectFlockKandangRoutes(router, userService, projectFlockKandangService)
|
||||||
|
|||||||
+92
-27
@@ -1,8 +1,10 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -35,6 +37,7 @@ type projectFlockKandangService struct {
|
|||||||
Validate *validator.Validate
|
Validate *validator.Validate
|
||||||
Repository repository.ProjectFlockKandangRepository
|
Repository repository.ProjectFlockKandangRepository
|
||||||
ApprovalSvc commonSvc.ApprovalService
|
ApprovalSvc commonSvc.ApprovalService
|
||||||
|
FifoStockV2Svc commonSvc.FifoStockV2Service
|
||||||
ExpenseRepo expenseRepo.ExpenseRepository
|
ExpenseRepo expenseRepo.ExpenseRepository
|
||||||
WarehouseRepo rWarehouse.WarehouseRepository
|
WarehouseRepo rWarehouse.WarehouseRepository
|
||||||
ProductWarehouseRepo rProductWarehouse.ProductWarehouseRepository
|
ProductWarehouseRepo rProductWarehouse.ProductWarehouseRepository
|
||||||
@@ -69,12 +72,13 @@ type ExpenseSummary struct {
|
|||||||
Reference string `json:"reference_number"`
|
Reference string `json:"reference_number"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewProjectFlockKandangService(repo repository.ProjectFlockKandangRepository, approvalSvc commonSvc.ApprovalService, expenseRepo expenseRepo.ExpenseRepository, warehouseRepo rWarehouse.WarehouseRepository, productWarehouseRepo rProductWarehouse.ProductWarehouseRepository, populationRepo repository.ProjectFlockPopulationRepository, kandangRepo kandangRepo.KandangRepository, validate *validator.Validate) ProjectFlockKandangService {
|
func NewProjectFlockKandangService(repo repository.ProjectFlockKandangRepository, approvalSvc commonSvc.ApprovalService, fifoStockV2Svc commonSvc.FifoStockV2Service, expenseRepo expenseRepo.ExpenseRepository, warehouseRepo rWarehouse.WarehouseRepository, productWarehouseRepo rProductWarehouse.ProductWarehouseRepository, populationRepo repository.ProjectFlockPopulationRepository, kandangRepo kandangRepo.KandangRepository, validate *validator.Validate) ProjectFlockKandangService {
|
||||||
return &projectFlockKandangService{
|
return &projectFlockKandangService{
|
||||||
Log: utils.Log,
|
Log: utils.Log,
|
||||||
Validate: validate,
|
Validate: validate,
|
||||||
Repository: repo,
|
Repository: repo,
|
||||||
ApprovalSvc: approvalSvc,
|
ApprovalSvc: approvalSvc,
|
||||||
|
FifoStockV2Svc: fifoStockV2Svc,
|
||||||
ExpenseRepo: expenseRepo,
|
ExpenseRepo: expenseRepo,
|
||||||
WarehouseRepo: warehouseRepo,
|
WarehouseRepo: warehouseRepo,
|
||||||
ProductWarehouseRepo: productWarehouseRepo,
|
ProductWarehouseRepo: productWarehouseRepo,
|
||||||
@@ -308,6 +312,7 @@ func (s projectFlockKandangService) CheckClosing(c *fiber.Ctx, id uint) (*Closin
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, pw := range productWarehouses {
|
for _, pw := range productWarehouses {
|
||||||
|
if pw.Quantity > 0 {
|
||||||
category := ""
|
category := ""
|
||||||
if pw.Product.ProductCategory.Id != 0 {
|
if pw.Product.ProductCategory.Id != 0 {
|
||||||
category = pw.Product.ProductCategory.Name
|
category = pw.Product.ProductCategory.Name
|
||||||
@@ -328,6 +333,7 @@ func (s projectFlockKandangService) CheckClosing(c *fiber.Ctx, id uint) (*Closin
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
expenseSummaries := make([]ExpenseSummary, 0)
|
expenseSummaries := make([]ExpenseSummary, 0)
|
||||||
if s.ExpenseRepo != nil {
|
if s.ExpenseRepo != nil {
|
||||||
@@ -583,7 +589,7 @@ func (s projectFlockKandangService) Closing(c *fiber.Ctx, id uint, req *validati
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if s.ApprovalSvc != nil {
|
if s.ApprovalSvc != nil {
|
||||||
reopenAction := entity.ApprovalActionApproved
|
reopenAction := entity.ApprovalActionUpdated
|
||||||
// Hindari duplikasi jika approval terakhir sudah Disetujui + Updated
|
// Hindari duplikasi jika approval terakhir sudah Disetujui + Updated
|
||||||
latestPFK, lerr := s.ApprovalSvc.LatestByTarget(c.Context(), utils.ApprovalWorkflowProjectFlockKandang, id, nil)
|
latestPFK, lerr := s.ApprovalSvc.LatestByTarget(c.Context(), utils.ApprovalWorkflowProjectFlockKandang, id, nil)
|
||||||
if lerr != nil {
|
if lerr != nil {
|
||||||
@@ -609,31 +615,6 @@ func (s projectFlockKandangService) Closing(c *fiber.Ctx, id uint, req *validati
|
|||||||
return nil, aerr
|
return nil, aerr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pastikan approval project flock kembali ke Aktif
|
|
||||||
latestPF, lerr := s.ApprovalSvc.LatestByTarget(c.Context(), utils.ApprovalWorkflowProjectFlock, pfk.ProjectFlockId, nil)
|
|
||||||
if lerr != nil {
|
|
||||||
return nil, lerr
|
|
||||||
}
|
|
||||||
shouldCreatePF := true
|
|
||||||
if latestPF != nil &&
|
|
||||||
latestPF.StepNumber == uint16(utils.ProjectFlockStepAktif) &&
|
|
||||||
latestPF.Action != nil && *latestPF.Action == reopenAction {
|
|
||||||
shouldCreatePF = false
|
|
||||||
}
|
|
||||||
if shouldCreatePF {
|
|
||||||
if _, aerr := s.ApprovalSvc.CreateApproval(
|
|
||||||
c.Context(),
|
|
||||||
utils.ApprovalWorkflowProjectFlock,
|
|
||||||
pfk.ProjectFlockId,
|
|
||||||
utils.ProjectFlockStepAktif,
|
|
||||||
&reopenAction,
|
|
||||||
actorID,
|
|
||||||
nil,
|
|
||||||
); aerr != nil && !errors.Is(aerr, gorm.ErrDuplicatedKey) {
|
|
||||||
return nil, aerr
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return nil, fiber.NewError(fiber.StatusBadRequest, "action harus close atau unclose")
|
return nil, fiber.NewError(fiber.StatusBadRequest, "action harus close atau unclose")
|
||||||
@@ -694,7 +675,91 @@ func (s projectFlockKandangService) calculateAvailableQuantityForProductWarehous
|
|||||||
if availableQty < 0 {
|
if availableQty < 0 {
|
||||||
availableQty = 0
|
availableQty = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sourceAvailable, err := s.resolveLayingSourceAvailableQty(c.Context(), nil, productWarehouse.Id, nil)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if sourceAvailable < availableQty {
|
||||||
|
availableQty = sourceAvailable
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return availableQty, nil
|
return availableQty, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s projectFlockKandangService) resolveLayingSourceAvailableQty(ctx context.Context, tx *gorm.DB, productWarehouseID uint, asOf *time.Time) (float64, error) {
|
||||||
|
if productWarehouseID == 0 || s.FifoStockV2Svc == nil {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
flagGroupCode, err := s.resolveFlagGroupByProductWarehouse(ctx, tx, productWarehouseID)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(flagGroupCode) == "" {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
gatherRows, err := s.FifoStockV2Svc.Gather(ctx, commonSvc.FifoStockV2GatherRequest{
|
||||||
|
FlagGroupCode: flagGroupCode,
|
||||||
|
Lane: commonSvc.FifoStockV2Lane("STOCKABLE"),
|
||||||
|
AllocationPurpose: entity.StockAllocationPurposeConsume,
|
||||||
|
ProductWarehouseID: productWarehouseID,
|
||||||
|
AsOf: asOf,
|
||||||
|
Limit: 10000,
|
||||||
|
Tx: tx,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
total := 0.0
|
||||||
|
for _, row := range gatherRows {
|
||||||
|
if row.AvailableQuantity <= 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
total += row.AvailableQuantity
|
||||||
|
}
|
||||||
|
return math.Max(total, 0), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s projectFlockKandangService) resolveFlagGroupByProductWarehouse(ctx context.Context, tx *gorm.DB, productWarehouseID uint) (string, error) {
|
||||||
|
type row struct {
|
||||||
|
FlagGroupCode string `gorm:"column:flag_group_code"`
|
||||||
|
}
|
||||||
|
selected := row{}
|
||||||
|
|
||||||
|
db := s.Repository.DB()
|
||||||
|
if tx != nil {
|
||||||
|
db = tx
|
||||||
|
}
|
||||||
|
|
||||||
|
err := db.WithContext(ctx).
|
||||||
|
Table("fifo_stock_v2_route_rules rr").
|
||||||
|
Select("rr.flag_group_code").
|
||||||
|
Joins("JOIN fifo_stock_v2_flag_groups fg ON fg.code = rr.flag_group_code AND fg.is_active = TRUE").
|
||||||
|
Where("rr.is_active = TRUE").
|
||||||
|
Where("rr.lane = 'STOCKABLE'").
|
||||||
|
Where(`
|
||||||
|
EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM product_warehouses pw
|
||||||
|
JOIN flags f ON f.flagable_id = pw.product_id
|
||||||
|
JOIN fifo_stock_v2_flag_members fm ON fm.flag_name = f.name AND fm.is_active = TRUE
|
||||||
|
WHERE pw.id = ?
|
||||||
|
AND f.flagable_type = ?
|
||||||
|
AND fm.flag_group_code = rr.flag_group_code
|
||||||
|
)
|
||||||
|
`, productWarehouseID, entity.FlagableTypeProduct).
|
||||||
|
Order("fg.priority ASC, rr.id ASC").
|
||||||
|
Limit(1).
|
||||||
|
Take(&selected).Error
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return strings.TrimSpace(selected.FlagGroupCode), nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
warehouseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/dto"
|
warehouseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/dto"
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/dto"
|
"gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/dto"
|
||||||
@@ -62,6 +63,7 @@ func (u *ProjectflockController) GetAll(c *fiber.Ctx) error {
|
|||||||
Search: c.Query("search", ""),
|
Search: c.Query("search", ""),
|
||||||
SortBy: c.Query("sort_by", ""),
|
SortBy: c.Query("sort_by", ""),
|
||||||
SortOrder: c.Query("sort_order", ""),
|
SortOrder: c.Query("sort_order", ""),
|
||||||
|
Status: strings.TrimSpace(c.Query("status", "")),
|
||||||
}
|
}
|
||||||
|
|
||||||
if area := c.QueryInt("area_id", 0); area > 0 {
|
if area := c.QueryInt("area_id", 0); area > 0 {
|
||||||
@@ -272,10 +274,20 @@ func (u *ProjectflockController) LookupProjectFlockKandang(c *fiber.Ctx) error {
|
|||||||
projectFlockId := c.QueryInt("project_flock_id", 0)
|
projectFlockId := c.QueryInt("project_flock_id", 0)
|
||||||
kandangId := c.QueryInt("kandang_id", 0)
|
kandangId := c.QueryInt("kandang_id", 0)
|
||||||
withPopulation := c.QueryBool("withpopulation", false)
|
withPopulation := c.QueryBool("withpopulation", false)
|
||||||
|
recordDateRaw := strings.TrimSpace(c.Query("record_date", ""))
|
||||||
|
var recordDate *time.Time
|
||||||
|
|
||||||
if projectFlockId == 0 || kandangId == 0 {
|
if projectFlockId == 0 || kandangId == 0 {
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid project_flock_id or kandang_id")
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid project_flock_id or kandang_id")
|
||||||
}
|
}
|
||||||
|
if recordDateRaw != "" {
|
||||||
|
parsed, err := time.Parse("2006-01-02", recordDateRaw)
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, "record_date must be in YYYY-MM-DD format")
|
||||||
|
}
|
||||||
|
utc := parsed.UTC()
|
||||||
|
recordDate = &utc
|
||||||
|
}
|
||||||
|
|
||||||
result, availableStock, err := u.ProjectflockService.GetProjectFlockKandangByProjectAndKandang(c, uint(projectFlockId), uint(kandangId))
|
result, availableStock, err := u.ProjectflockService.GetProjectFlockKandangByProjectAndKandang(c, uint(projectFlockId), uint(kandangId))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -300,6 +312,12 @@ func (u *ProjectflockController) LookupProjectFlockKandang(c *fiber.Ctx) error {
|
|||||||
mapped := warehouseDTO.ToWarehouseRelationDTO(*warehouse)
|
mapped := warehouseDTO.ToWarehouseRelationDTO(*warehouse)
|
||||||
dtoResult.Warehouse = &mapped
|
dtoResult.Warehouse = &mapped
|
||||||
}
|
}
|
||||||
|
if isTransition, isLaying, serr := u.ProjectflockService.GetProjectFlockKandangTransferStateAtDate(c, result.Id, recordDate); serr != nil {
|
||||||
|
return serr
|
||||||
|
} else {
|
||||||
|
dtoResult.IsTransition = isTransition
|
||||||
|
dtoResult.IsLaying = isLaying
|
||||||
|
}
|
||||||
if withPopulation {
|
if withPopulation {
|
||||||
population := dtoResult.AvailableQuantity
|
population := dtoResult.AvailableQuantity
|
||||||
dtoResult.Population = &population
|
dtoResult.Population = &population
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package dto
|
|||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/config"
|
||||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
approvalDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/approvals/dto"
|
approvalDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/approvals/dto"
|
||||||
areaDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas/dto"
|
areaDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas/dto"
|
||||||
@@ -42,7 +43,6 @@ type KandangWithProjectFlockIdDTO struct {
|
|||||||
kandangDTO.KandangRelationDTO
|
kandangDTO.KandangRelationDTO
|
||||||
ProjectFlockKandangId uint `json:"project_flock_kandang_id"`
|
ProjectFlockKandangId uint `json:"project_flock_kandang_id"`
|
||||||
Period int `json:"period"`
|
Period int `json:"period"`
|
||||||
ClosedAt *time.Time `json:"closed_at,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type ProjectFlockDetailDTO struct {
|
type ProjectFlockDetailDTO struct {
|
||||||
@@ -77,26 +77,18 @@ func ToProjectFlockListDTOWithPeriod(e entity.ProjectFlock, period int) ProjectF
|
|||||||
var (
|
var (
|
||||||
pfkId uint
|
pfkId uint
|
||||||
period int
|
period int
|
||||||
closedAt *time.Time
|
|
||||||
)
|
)
|
||||||
for _, kh := range e.KandangHistory {
|
for _, kh := range e.KandangHistory {
|
||||||
if kh.KandangId == kandang.Id {
|
if kh.KandangId == kandang.Id {
|
||||||
pfkId = kh.Id
|
pfkId = kh.Id
|
||||||
period = kh.Period
|
period = kh.Period
|
||||||
closedAt = kh.ClosedAt
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mapped := kandangDTO.ToKandangRelationDTO(kandang)
|
|
||||||
if closedAt != nil {
|
|
||||||
// Jangan ubah tabel kandang, hanya override status di response.
|
|
||||||
mapped.Status = string(utils.KandangStatusNonActive)
|
|
||||||
}
|
|
||||||
kandangSummaries[i] = KandangWithProjectFlockIdDTO{
|
kandangSummaries[i] = KandangWithProjectFlockIdDTO{
|
||||||
KandangRelationDTO: mapped,
|
KandangRelationDTO: kandangDTO.ToKandangRelationDTO(kandang),
|
||||||
ProjectFlockKandangId: pfkId,
|
ProjectFlockKandangId: pfkId,
|
||||||
Period: period,
|
Period: period,
|
||||||
ClosedAt: closedAt,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -212,7 +204,7 @@ func resolveProjectFlockStandardFcr(e entity.ProjectFlock) *float64 {
|
|||||||
}
|
}
|
||||||
week := 1
|
week := 1
|
||||||
if e.Category == string(utils.ProjectFlockCategoryLaying) {
|
if e.Category == string(utils.ProjectFlockCategoryLaying) {
|
||||||
week = 18
|
week = config.LayingWeekStart()
|
||||||
}
|
}
|
||||||
for _, detail := range e.ProductionStandard.ProductionStandardDetails {
|
for _, detail := range e.ProductionStandard.ProductionStandardDetails {
|
||||||
if detail.Week == week && detail.StandardFCR != nil {
|
if detail.Week == week && detail.StandardFCR != nil {
|
||||||
|
|||||||
@@ -40,6 +40,8 @@ type ProjectFlockKandangDTO struct {
|
|||||||
AvailableQuantity float64 `json:"available_quantity"`
|
AvailableQuantity float64 `json:"available_quantity"`
|
||||||
Population *float64 `json:"population,omitempty"`
|
Population *float64 `json:"population,omitempty"`
|
||||||
ChickInDate *time.Time `json:"chick_in_date,omitempty"`
|
ChickInDate *time.Time `json:"chick_in_date,omitempty"`
|
||||||
|
IsTransition bool `json:"is_transition"`
|
||||||
|
IsLaying bool `json:"is_laying"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func ToProjectFlockKandangDTO(e entity.ProjectFlockKandang) ProjectFlockKandangDTO {
|
func ToProjectFlockKandangDTO(e entity.ProjectFlockKandang) ProjectFlockKandangDTO {
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import (
|
|||||||
rProjectBudget "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
|
rProjectBudget "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"
|
rProjectflock "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
|
||||||
rRecording "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/repositories"
|
rRecording "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/repositories"
|
||||||
|
rTransferLaying "gitlab.com/mbugroup/lti-api.git/internal/modules/production/transfer_layings/repositories"
|
||||||
|
|
||||||
sProjectflock "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/services"
|
sProjectflock "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/services"
|
||||||
utils "gitlab.com/mbugroup/lti-api.git/internal/utils"
|
utils "gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
@@ -35,6 +36,7 @@ func (ProjectflockModule) RegisterRoutes(router fiber.Router, db *gorm.DB, valid
|
|||||||
projectflockKandangRepo := rProjectflock.NewProjectFlockKandangRepository(db)
|
projectflockKandangRepo := rProjectflock.NewProjectFlockKandangRepository(db)
|
||||||
projectFlockPopulationRepo := rProjectflock.NewProjectFlockPopulationRepository(db)
|
projectFlockPopulationRepo := rProjectflock.NewProjectFlockPopulationRepository(db)
|
||||||
recordingRepo := rRecording.NewRecordingRepository(db)
|
recordingRepo := rRecording.NewRecordingRepository(db)
|
||||||
|
transferLayingRepo := rTransferLaying.NewTransferLayingRepository(db)
|
||||||
warehouseRepo := rWarehouse.NewWarehouseRepository(db)
|
warehouseRepo := rWarehouse.NewWarehouseRepository(db)
|
||||||
productWarehouseRepo := rProductWarehouse.NewProductWarehouseRepository(db)
|
productWarehouseRepo := rProductWarehouse.NewProductWarehouseRepository(db)
|
||||||
projectBudgetRepo := rProjectBudget.NewProjectBudgetRepository(db)
|
projectBudgetRepo := rProjectBudget.NewProjectBudgetRepository(db)
|
||||||
@@ -46,7 +48,7 @@ func (ProjectflockModule) RegisterRoutes(router fiber.Router, db *gorm.DB, valid
|
|||||||
panic(fmt.Sprintf("failed to register project flock approval workflow: %v", err))
|
panic(fmt.Sprintf("failed to register project flock approval workflow: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
projectflockService := sProjectflock.NewProjectflockService(projectflockRepo, flockRepo, kandangRepo, projectflockKandangRepo, warehouseRepo, productWarehouseRepo, projectBudgetRepo, nonstockRepo, projectFlockPopulationRepo, recordingRepo, approvalService, validate)
|
projectflockService := sProjectflock.NewProjectflockService(projectflockRepo, flockRepo, kandangRepo, projectflockKandangRepo, warehouseRepo, productWarehouseRepo, projectBudgetRepo, nonstockRepo, projectFlockPopulationRepo, recordingRepo, transferLayingRepo, approvalService, validate)
|
||||||
userService := sUser.NewUserService(userRepo, validate)
|
userService := sUser.NewUserService(userRepo, validate)
|
||||||
|
|
||||||
ProjectflockRoutes(router, userService, projectflockService)
|
ProjectflockRoutes(router, userService, projectflockService)
|
||||||
|
|||||||
+16
-5
@@ -51,6 +51,7 @@ func (r *projectFlockPopulationRepositoryImpl) GetByProjectFlockKandangID(ctx co
|
|||||||
err := r.DB().WithContext(ctx).
|
err := r.DB().WithContext(ctx).
|
||||||
Joins("JOIN project_chickins ON project_chickins.id = project_flock_populations.project_chickin_id").
|
Joins("JOIN project_chickins ON project_chickins.id = project_flock_populations.project_chickin_id").
|
||||||
Where("project_chickins.project_flock_kandang_id = ?", projectFlockKandangID).
|
Where("project_chickins.project_flock_kandang_id = ?", projectFlockKandangID).
|
||||||
|
Where("project_chickins.deleted_at IS NULL").
|
||||||
Preload("ProjectChickin").
|
Preload("ProjectChickin").
|
||||||
Find(&records).Error
|
Find(&records).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -87,6 +88,7 @@ func (r *projectFlockPopulationRepositoryImpl) GetByProjectFlockKandangIDAndProd
|
|||||||
err := r.DB().WithContext(ctx).
|
err := r.DB().WithContext(ctx).
|
||||||
Joins("JOIN project_chickins ON project_chickins.id = project_flock_populations.project_chickin_id").
|
Joins("JOIN project_chickins ON project_chickins.id = project_flock_populations.project_chickin_id").
|
||||||
Where("project_chickins.project_flock_kandang_id = ? AND project_flock_populations.product_warehouse_id = ?", projectFlockKandangID, productWarehouseID).
|
Where("project_chickins.project_flock_kandang_id = ? AND project_flock_populations.product_warehouse_id = ?", projectFlockKandangID, productWarehouseID).
|
||||||
|
Where("project_chickins.deleted_at IS NULL").
|
||||||
Find(&records).Error
|
Find(&records).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -99,8 +101,10 @@ func (r *projectFlockPopulationRepositoryImpl) GetTotalQtyByProjectFlockKandangI
|
|||||||
err := r.DB().WithContext(ctx).
|
err := r.DB().WithContext(ctx).
|
||||||
Table("project_flock_populations").
|
Table("project_flock_populations").
|
||||||
Select("COALESCE(SUM(total_qty - total_used_qty), 0) AS available_qty").
|
Select("COALESCE(SUM(total_qty - total_used_qty), 0) AS available_qty").
|
||||||
Joins("JOIN product_warehouses pw ON project_flock_populations.product_warehouse_id = pw.id").
|
Joins("JOIN project_chickins ON project_chickins.id = project_flock_populations.project_chickin_id").
|
||||||
Where("pw.project_flock_kandang_id = ?", projectFlockKandangID).
|
Where("project_chickins.project_flock_kandang_id = ?", projectFlockKandangID).
|
||||||
|
Where("project_chickins.deleted_at IS NULL").
|
||||||
|
Where("project_flock_populations.deleted_at IS NULL").
|
||||||
Scan(&total).Error
|
Scan(&total).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
@@ -111,9 +115,12 @@ func (r *projectFlockPopulationRepositoryImpl) GetTotalQtyByProjectFlockKandangI
|
|||||||
func (r *projectFlockPopulationRepositoryImpl) GetTotalQtyByProductWarehouseID(ctx context.Context, productWarehouseID uint) (float64, error) {
|
func (r *projectFlockPopulationRepositoryImpl) GetTotalQtyByProductWarehouseID(ctx context.Context, productWarehouseID uint) (float64, error) {
|
||||||
var total float64
|
var total float64
|
||||||
err := r.DB().WithContext(ctx).
|
err := r.DB().WithContext(ctx).
|
||||||
Model(&entity.ProjectFlockPopulation{}).
|
Table("project_flock_populations").
|
||||||
Where("product_warehouse_id = ?", productWarehouseID).
|
Select("COALESCE(SUM(project_flock_populations.total_qty - project_flock_populations.total_used_qty), 0)").
|
||||||
Select("COALESCE(SUM(total_qty - total_used_qty), 0)").
|
Joins("JOIN project_chickins ON project_chickins.id = project_flock_populations.project_chickin_id").
|
||||||
|
Where("project_flock_populations.product_warehouse_id = ?", productWarehouseID).
|
||||||
|
Where("project_chickins.deleted_at IS NULL").
|
||||||
|
Where("project_flock_populations.deleted_at IS NULL").
|
||||||
Scan(&total).Error
|
Scan(&total).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
@@ -128,6 +135,8 @@ func (r *projectFlockPopulationRepositoryImpl) GetAvailableQtyByProjectFlockKand
|
|||||||
Select("COALESCE(SUM(total_qty - total_used_qty), 0) AS total_qty").
|
Select("COALESCE(SUM(total_qty - total_used_qty), 0) AS total_qty").
|
||||||
Joins("JOIN project_chickins ON project_chickins.id = project_flock_populations.project_chickin_id").
|
Joins("JOIN project_chickins ON project_chickins.id = project_flock_populations.project_chickin_id").
|
||||||
Where("project_chickins.project_flock_kandang_id = ?", projectFlockKandangID).
|
Where("project_chickins.project_flock_kandang_id = ?", projectFlockKandangID).
|
||||||
|
Where("project_chickins.deleted_at IS NULL").
|
||||||
|
Where("project_flock_populations.deleted_at IS NULL").
|
||||||
Scan(&total).Error
|
Scan(&total).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
@@ -145,6 +154,8 @@ func (r *projectFlockPopulationRepositoryImpl) GetTotalChickInByProjectFlockKand
|
|||||||
Select("COALESCE(SUM(project_flock_populations.total_qty - project_flock_populations.total_used_qty), 0) AS total_qty").
|
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").
|
Joins("JOIN project_chickins ON project_chickins.id = project_flock_populations.project_chickin_id").
|
||||||
Where("project_chickins.project_flock_kandang_id = ?", projectFlockKandangID).
|
Where("project_chickins.project_flock_kandang_id = ?", projectFlockKandangID).
|
||||||
|
Where("project_chickins.deleted_at IS NULL").
|
||||||
|
Where("project_flock_populations.deleted_at IS NULL").
|
||||||
Scan(&total).Error
|
Scan(&total).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/validations"
|
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/validations"
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -110,6 +111,28 @@ func (r *ProjectflockRepositoryImpl) applyQueryFilters(db *gorm.DB, params *vali
|
|||||||
AND pfk.kandang_id IN ?
|
AND pfk.kandang_id IN ?
|
||||||
)`, params.KandangIds)
|
)`, params.KandangIds)
|
||||||
}
|
}
|
||||||
|
if params.Status != "" {
|
||||||
|
db = db.Where(`
|
||||||
|
EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM approvals latest_approval
|
||||||
|
WHERE latest_approval.approvable_type = ?
|
||||||
|
AND latest_approval.approvable_id = project_flocks.id
|
||||||
|
AND latest_approval.id = (
|
||||||
|
SELECT a2.id
|
||||||
|
FROM approvals a2
|
||||||
|
WHERE a2.approvable_type = ?
|
||||||
|
AND a2.approvable_id = project_flocks.id
|
||||||
|
ORDER BY a2.id DESC
|
||||||
|
LIMIT 1
|
||||||
|
)
|
||||||
|
AND LOWER(latest_approval.step_name) = LOWER(?)
|
||||||
|
)`,
|
||||||
|
utils.ApprovalWorkflowProjectFlock.String(),
|
||||||
|
utils.ApprovalWorkflowProjectFlock.String(),
|
||||||
|
params.Status,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
db = r.applySearchFilters(db, params.Search)
|
db = r.applySearchFilters(db, params.Search)
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import (
|
|||||||
pfutils "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/utils"
|
pfutils "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/utils"
|
||||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/validations"
|
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/validations"
|
||||||
recordingRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/repositories"
|
recordingRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/repositories"
|
||||||
|
transferLayingRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/production/transfer_layings/repositories"
|
||||||
uniformityRepository "gitlab.com/mbugroup/lti-api.git/internal/modules/production/uniformities/repositories"
|
uniformityRepository "gitlab.com/mbugroup/lti-api.git/internal/modules/production/uniformities/repositories"
|
||||||
purchaseRepository "gitlab.com/mbugroup/lti-api.git/internal/modules/purchases/repositories"
|
purchaseRepository "gitlab.com/mbugroup/lti-api.git/internal/modules/purchases/repositories"
|
||||||
utils "gitlab.com/mbugroup/lti-api.git/internal/utils"
|
utils "gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
@@ -44,6 +45,8 @@ type ProjectflockService interface {
|
|||||||
GetProjectFlockKandangByProjectAndKandang(ctx *fiber.Ctx, projectFlockID uint, kandangID uint) (*entity.ProjectFlockKandang, float64, error)
|
GetProjectFlockKandangByProjectAndKandang(ctx *fiber.Ctx, projectFlockID uint, kandangID uint) (*entity.ProjectFlockKandang, float64, error)
|
||||||
GetProjectFlockKandangPopulation(ctx *fiber.Ctx, projectFlockKandangID uint) (float64, error)
|
GetProjectFlockKandangPopulation(ctx *fiber.Ctx, projectFlockKandangID uint) (float64, error)
|
||||||
GetProjectFlockKandangChickinDate(ctx *fiber.Ctx, projectFlockKandangID uint) (*time.Time, error)
|
GetProjectFlockKandangChickinDate(ctx *fiber.Ctx, projectFlockKandangID uint) (*time.Time, error)
|
||||||
|
GetProjectFlockKandangTransferState(ctx *fiber.Ctx, projectFlockKandangID uint) (bool, bool, error)
|
||||||
|
GetProjectFlockKandangTransferStateAtDate(ctx *fiber.Ctx, projectFlockKandangID uint, referenceDate *time.Time) (bool, bool, error)
|
||||||
GetPeriodSummary(ctx *fiber.Ctx, locationID uint) ([]KandangPeriodSummary, error)
|
GetPeriodSummary(ctx *fiber.Ctx, locationID uint) ([]KandangPeriodSummary, error)
|
||||||
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)
|
||||||
@@ -64,6 +67,7 @@ type projectflockService struct {
|
|||||||
PivotRepo repository.ProjectFlockKandangRepository
|
PivotRepo repository.ProjectFlockKandangRepository
|
||||||
PopulationRepo repository.ProjectFlockPopulationRepository
|
PopulationRepo repository.ProjectFlockPopulationRepository
|
||||||
RecordingRepo recordingRepo.RecordingRepository
|
RecordingRepo recordingRepo.RecordingRepository
|
||||||
|
TransferLayingRepo transferLayingRepo.TransferLayingRepository
|
||||||
ApprovalSvc commonSvc.ApprovalService
|
ApprovalSvc commonSvc.ApprovalService
|
||||||
approvalWorkflow approvalutils.ApprovalWorkflowKey
|
approvalWorkflow approvalutils.ApprovalWorkflowKey
|
||||||
}
|
}
|
||||||
@@ -85,6 +89,7 @@ func NewProjectflockService(
|
|||||||
nonstockRepo nonstockRepository.NonstockRepository,
|
nonstockRepo nonstockRepository.NonstockRepository,
|
||||||
populationRepo repository.ProjectFlockPopulationRepository,
|
populationRepo repository.ProjectFlockPopulationRepository,
|
||||||
recordingRepo recordingRepo.RecordingRepository,
|
recordingRepo recordingRepo.RecordingRepository,
|
||||||
|
transferLayingRepo transferLayingRepo.TransferLayingRepository,
|
||||||
approvalSvc commonSvc.ApprovalService,
|
approvalSvc commonSvc.ApprovalService,
|
||||||
validate *validator.Validate,
|
validate *validator.Validate,
|
||||||
|
|
||||||
@@ -102,6 +107,7 @@ func NewProjectflockService(
|
|||||||
PivotRepo: pivotRepo,
|
PivotRepo: pivotRepo,
|
||||||
PopulationRepo: populationRepo,
|
PopulationRepo: populationRepo,
|
||||||
RecordingRepo: recordingRepo,
|
RecordingRepo: recordingRepo,
|
||||||
|
TransferLayingRepo: transferLayingRepo,
|
||||||
ApprovalSvc: approvalSvc,
|
ApprovalSvc: approvalSvc,
|
||||||
approvalWorkflow: utils.ApprovalWorkflowProjectFlock,
|
approvalWorkflow: utils.ApprovalWorkflowProjectFlock,
|
||||||
}
|
}
|
||||||
@@ -538,6 +544,70 @@ func (s projectflockService) GetProjectFlockKandangChickinDate(ctx *fiber.Ctx, p
|
|||||||
return earliest, nil
|
return earliest, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s projectflockService) GetProjectFlockKandangTransferState(ctx *fiber.Ctx, projectFlockKandangID uint) (bool, bool, error) {
|
||||||
|
return s.GetProjectFlockKandangTransferStateAtDate(ctx, projectFlockKandangID, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s projectflockService) GetProjectFlockKandangTransferStateAtDate(ctx *fiber.Ctx, projectFlockKandangID uint, referenceDate *time.Time) (bool, bool, error) {
|
||||||
|
if projectFlockKandangID == 0 || s.TransferLayingRepo == nil || s.PivotRepo == nil {
|
||||||
|
return false, false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
pfk, err := s.PivotRepo.GetByIDLight(ctx.Context(), projectFlockKandangID)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return false, false, nil
|
||||||
|
}
|
||||||
|
s.Log.Errorf("Failed to resolve project flock kandang %d for transfer state: %+v", projectFlockKandangID, err)
|
||||||
|
return false, false, fiber.NewError(fiber.StatusInternalServerError, "Failed to resolve transfer state")
|
||||||
|
}
|
||||||
|
|
||||||
|
category := strings.ToUpper(strings.TrimSpace(pfk.ProjectFlock.Category))
|
||||||
|
var transfer *entity.LayingTransfer
|
||||||
|
switch category {
|
||||||
|
case strings.ToUpper(string(utils.ProjectFlockCategoryGrowing)):
|
||||||
|
transfer, err = s.TransferLayingRepo.GetLatestApprovedBySourceKandang(ctx.Context(), projectFlockKandangID)
|
||||||
|
case strings.ToUpper(string(utils.ProjectFlockCategoryLaying)):
|
||||||
|
transfer, err = s.TransferLayingRepo.GetLatestApprovedByTargetKandang(ctx.Context(), projectFlockKandangID)
|
||||||
|
default:
|
||||||
|
return false, false, nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return false, false, nil
|
||||||
|
}
|
||||||
|
s.Log.Errorf("Failed to resolve transfer state for project flock kandang %d: %+v", projectFlockKandangID, err)
|
||||||
|
return false, false, fiber.NewError(fiber.StatusInternalServerError, "Failed to resolve transfer state")
|
||||||
|
}
|
||||||
|
if transfer == nil {
|
||||||
|
return false, false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
physicalMoveDate := normalizeDateOnlyUTC(transfer.TransferDate)
|
||||||
|
if physicalMoveDate.IsZero() {
|
||||||
|
return false, false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
economicCutoffDate := physicalMoveDate
|
||||||
|
if transfer.EconomicCutoffDate != nil && !transfer.EconomicCutoffDate.IsZero() {
|
||||||
|
economicCutoffDate = normalizeDateOnlyUTC(*transfer.EconomicCutoffDate)
|
||||||
|
} else if transfer.EffectiveMoveDate != nil && !transfer.EffectiveMoveDate.IsZero() {
|
||||||
|
economicCutoffDate = normalizeDateOnlyUTC(*transfer.EffectiveMoveDate)
|
||||||
|
}
|
||||||
|
if economicCutoffDate.Before(physicalMoveDate) {
|
||||||
|
economicCutoffDate = physicalMoveDate
|
||||||
|
}
|
||||||
|
|
||||||
|
reference := normalizeDateOnlyUTC(time.Now().UTC())
|
||||||
|
if referenceDate != nil && !referenceDate.IsZero() {
|
||||||
|
reference = normalizeDateOnlyUTC(referenceDate.UTC())
|
||||||
|
}
|
||||||
|
isTransition := !reference.Before(physicalMoveDate) && reference.Before(economicCutoffDate)
|
||||||
|
isLaying := !reference.Before(economicCutoffDate)
|
||||||
|
|
||||||
|
return isTransition, isLaying, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s projectflockService) GetProjectFlockKandangByParams(ctx *fiber.Ctx, idStr string, projectFlockIdStr string, kandangIdStr string) (*entity.ProjectFlockKandang, float64, error) {
|
func (s projectflockService) GetProjectFlockKandangByParams(ctx *fiber.Ctx, idStr string, projectFlockIdStr string, kandangIdStr string) (*entity.ProjectFlockKandang, float64, error) {
|
||||||
idStr = strings.TrimSpace(idStr)
|
idStr = strings.TrimSpace(idStr)
|
||||||
projectFlockIdStr = strings.TrimSpace(projectFlockIdStr)
|
projectFlockIdStr = strings.TrimSpace(projectFlockIdStr)
|
||||||
@@ -579,6 +649,10 @@ func (s projectflockService) GetProjectFlockKandangByParams(ctx *fiber.Ctx, idSt
|
|||||||
return s.GetProjectFlockKandangByProjectAndKandang(ctx, uint(pfid), uint(kid))
|
return s.GetProjectFlockKandangByProjectAndKandang(ctx, uint(pfid), uint(kid))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func normalizeDateOnlyUTC(value time.Time) time.Time {
|
||||||
|
return time.Date(value.UTC().Year(), value.UTC().Month(), value.UTC().Day(), 0, 0, 0, 0, time.UTC)
|
||||||
|
}
|
||||||
|
|
||||||
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 {
|
if s.PopulationRepo == nil {
|
||||||
return 0, fiber.NewError(fiber.StatusInternalServerError, "Project flock population repository is not configured")
|
return 0, fiber.NewError(fiber.StatusInternalServerError, "Project flock population repository is not configured")
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ type Query struct {
|
|||||||
LocationId uint `query:"location_id" validate:"omitempty,number,gt=0"`
|
LocationId uint `query:"location_id" validate:"omitempty,number,gt=0"`
|
||||||
Period int `query:"period" validate:"omitempty,number,gt=0"`
|
Period int `query:"period" validate:"omitempty,number,gt=0"`
|
||||||
Category string `query:"category" validate:"omitempty"`
|
Category string `query:"category" validate:"omitempty"`
|
||||||
|
Status string `query:"status" validate:"omitempty,oneof=Pengajuan Aktif Selesai"`
|
||||||
KandangIds []uint `query:"kandang_id" validate:"omitempty,dive,gt=0"`
|
KandangIds []uint `query:"kandang_id" validate:"omitempty,dive,gt=0"`
|
||||||
TransferContext string `query:"transfer_context" validate:"omitempty,oneof=transfer_to_laying"`
|
TransferContext string `query:"transfer_context" validate:"omitempty,oneof=transfer_to_laying"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/config"
|
||||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
approvalDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/approvals/dto"
|
approvalDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/approvals/dto"
|
||||||
productWarehouseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/adjustments/dto"
|
productWarehouseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/adjustments/dto"
|
||||||
@@ -86,6 +87,8 @@ type RecordingRelationDTO struct {
|
|||||||
EggWeight float64 `json:"egg_weight"`
|
EggWeight float64 `json:"egg_weight"`
|
||||||
PopulationCanChange bool `json:"population_can_change"`
|
PopulationCanChange bool `json:"population_can_change"`
|
||||||
TransferExecuted *bool `json:"transfer_executed,omitempty"`
|
TransferExecuted *bool `json:"transfer_executed,omitempty"`
|
||||||
|
IsTransition bool `json:"is_transition"`
|
||||||
|
IsLaying bool `json:"is_laying"`
|
||||||
Approval approvalDTO.ApprovalRelationDTO `json:"approval"`
|
Approval approvalDTO.ApprovalRelationDTO `json:"approval"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -247,6 +250,8 @@ func toRecordingRelationDTO(e entity.Recording) RecordingRelationDTO {
|
|||||||
EggWeight: floatValue(e.EggWeight),
|
EggWeight: floatValue(e.EggWeight),
|
||||||
PopulationCanChange: boolValueDefault(e.PopulationCanChange, true),
|
PopulationCanChange: boolValueDefault(e.PopulationCanChange, true),
|
||||||
TransferExecuted: e.TransferExecuted,
|
TransferExecuted: e.TransferExecuted,
|
||||||
|
IsTransition: boolValueDefault(e.IsTransition, false),
|
||||||
|
IsLaying: boolValueDefault(e.IsLaying, false),
|
||||||
Approval: latestApproval,
|
Approval: latestApproval,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -304,7 +309,7 @@ func recordingWeekValue(e entity.Recording) int {
|
|||||||
}
|
}
|
||||||
weekBase := 1
|
weekBase := 1
|
||||||
if isLayingRecording(e) {
|
if isLayingRecording(e) {
|
||||||
weekBase = 18
|
weekBase = config.LayingWeekStart()
|
||||||
}
|
}
|
||||||
return ((day - 1) / 7) + weekBase
|
return ((day - 1) / 7) + weekBase
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -125,6 +125,7 @@ func (RecordingModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
|
|||||||
nonstockRepo,
|
nonstockRepo,
|
||||||
projectFlockPopulationRepo,
|
projectFlockPopulationRepo,
|
||||||
recordingRepo,
|
recordingRepo,
|
||||||
|
transferLayingRepo,
|
||||||
approvalService,
|
approvalService,
|
||||||
validate,
|
validate,
|
||||||
)
|
)
|
||||||
@@ -154,7 +155,6 @@ func (RecordingModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
|
|||||||
productWarehouseRepo,
|
productWarehouseRepo,
|
||||||
warehouseRepo,
|
warehouseRepo,
|
||||||
approvalService,
|
approvalService,
|
||||||
fifoService,
|
|
||||||
fifoStockV2Service,
|
fifoStockV2Service,
|
||||||
validate,
|
validate,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -71,6 +71,7 @@ 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)
|
||||||
|
GetProjectFlockKandangIDsByPopulationWarehouseIDs(ctx context.Context, tx *gorm.DB, productWarehouseIDs []uint) ([]uint, error)
|
||||||
ResyncProjectFlockPopulationUsage(ctx context.Context, tx *gorm.DB, projectFlockKandangID uint) error
|
ResyncProjectFlockPopulationUsage(ctx context.Context, tx *gorm.DB, projectFlockKandangID uint) error
|
||||||
ValidateProductWarehousesByFlags(ctx context.Context, ids []uint, flags []string) (uint, error)
|
ValidateProductWarehousesByFlags(ctx context.Context, ids []uint, flags []string) (uint, error)
|
||||||
}
|
}
|
||||||
@@ -874,6 +875,34 @@ func (r *RecordingRepositoryImpl) GetAverageTargetMetricsByProjectFlockKandangID
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *RecordingRepositoryImpl) GetProjectFlockKandangIDsByPopulationWarehouseIDs(
|
||||||
|
ctx context.Context,
|
||||||
|
tx *gorm.DB,
|
||||||
|
productWarehouseIDs []uint,
|
||||||
|
) ([]uint, error) {
|
||||||
|
if len(productWarehouseIDs) == 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
db := r.DB().WithContext(ctx)
|
||||||
|
if tx != nil {
|
||||||
|
db = tx.WithContext(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
var kandangIDs []uint
|
||||||
|
if err := db.Table("project_flock_populations pfp").
|
||||||
|
Select("DISTINCT pc.project_flock_kandang_id").
|
||||||
|
Joins("JOIN project_chickins pc ON pc.id = pfp.project_chickin_id").
|
||||||
|
Where("pfp.product_warehouse_id IN ?", productWarehouseIDs).
|
||||||
|
Where("pfp.deleted_at IS NULL").
|
||||||
|
Where("pc.deleted_at IS NULL").
|
||||||
|
Pluck("pc.project_flock_kandang_id", &kandangIDs).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return kandangIDs, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *RecordingRepositoryImpl) ResyncProjectFlockPopulationUsage(ctx context.Context, tx *gorm.DB, projectFlockKandangID uint) error {
|
func (r *RecordingRepositoryImpl) ResyncProjectFlockPopulationUsage(ctx context.Context, tx *gorm.DB, projectFlockKandangID uint) error {
|
||||||
if projectFlockKandangID == 0 {
|
if projectFlockKandangID == 0 {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user