mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-22 14:25:45 +00:00
Compare commits
60 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 34c690956a | |||
| cb9c202b29 | |||
| ec7ec2219c | |||
| b8164e2e08 | |||
| 3c43426fbc | |||
| 131aa4fcb5 | |||
| c1acee7980 | |||
| 810aab1448 | |||
| b93150e8df | |||
| fc532e1202 | |||
| 71c7207bdb | |||
| 96bef248cb | |||
| 1dc1feed03 | |||
| 5ebdc9dfe4 | |||
| 65962f8e2b | |||
| 17ed266131 | |||
| 26e782b212 | |||
| 1fc6cb051d | |||
| e6159e4f90 | |||
| e80874bad7 | |||
| 566567e328 | |||
| 3cb2e15629 | |||
| 1c5b013b9f | |||
| 83650d4486 | |||
| 970332f0be | |||
| f8ca404bbb | |||
| 73dfeb64b0 | |||
| ee216327bb | |||
| 7fc52174e0 | |||
| 735c8e00d0 | |||
| 1a1ea14824 | |||
| ed1fb1b776 | |||
| 9e77d88c83 | |||
| 70db3cfd34 | |||
| 562d8a90c8 | |||
| 68f77a97a5 | |||
| 54e4878406 | |||
| 7677057d7c | |||
| 77ac46a029 | |||
| cdd22cf198 | |||
| 8a006f377e | |||
| 55451a5ea8 | |||
| aafbba199d | |||
| 0206d9dc68 | |||
| e9d1cca294 | |||
| 4d009978ae | |||
| daca97f113 | |||
| 88f1381f4b | |||
| 625642c709 | |||
| f6f4cc5a10 | |||
| 5fb7a78a5a | |||
| bc1dac2a15 | |||
| a3334c6bb0 | |||
| b73f13ee76 | |||
| 9d6a69dc4d | |||
| 0ac174fdc6 | |||
| 4bf9b12680 | |||
| 2028cee274 | |||
| 1c22c0f01c | |||
| 18db58a87b |
+179
-28
@@ -1,35 +1,186 @@
|
||||
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:
|
||||
rules:
|
||||
# MR pipeline
|
||||
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
|
||||
when: always
|
||||
|
||||
# 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
|
||||
# run untuk branch utama & MR
|
||||
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "development"'
|
||||
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "production"'
|
||||
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "production"'
|
||||
- when: never
|
||||
|
||||
include:
|
||||
# khusus MR (notif)
|
||||
- local: "ci/merge_request.yml"
|
||||
rules:
|
||||
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
|
||||
# =========================
|
||||
# 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
|
||||
|
||||
# khusus push ke branch env
|
||||
- local: "ci/development.yml"
|
||||
rules:
|
||||
- if: '$CI_COMMIT_BRANCH == "development"'
|
||||
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"
|
||||
|
||||
- local: "ci/staging.yml"
|
||||
rules:
|
||||
- if: '$CI_COMMIT_BRANCH == "staging"'
|
||||
# =========================
|
||||
# MR
|
||||
# =========================
|
||||
build_mr:
|
||||
stage: build
|
||||
image: public.ecr.aws/docker/library/docker:27
|
||||
tags: [self-hosted-dev]
|
||||
rules:
|
||||
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "production"'
|
||||
variables:
|
||||
IMAGE_TAG: "prod-${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"
|
||||
|
||||
- local: "ci/production.yml"
|
||||
rules:
|
||||
- if: '$CI_COMMIT_BRANCH == "production"'
|
||||
# =========================
|
||||
# DEVELOPMENT (push branch development)
|
||||
# =========================
|
||||
build_push_dev:
|
||||
stage: build
|
||||
image: public.ecr.aws/docker/library/docker:27
|
||||
tags: [self-hosted-dev]
|
||||
rules:
|
||||
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "development"'
|
||||
variables:
|
||||
IMAGE_TAG: "dev-${CI_COMMIT_SHORT_SHA}"
|
||||
before_script:
|
||||
- set -eu
|
||||
- docker version
|
||||
- docker info
|
||||
- *ecr_login
|
||||
script: |
|
||||
set -eu
|
||||
# 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:
|
||||
stage: gitops
|
||||
image: public.ecr.aws/docker/library/alpine:3.20
|
||||
tags: [self-hosted-dev]
|
||||
rules:
|
||||
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "development"'
|
||||
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"
|
||||
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:
|
||||
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "production"'
|
||||
variables:
|
||||
IMAGE_TAG: "prod-${CI_COMMIT_SHORT_SHA}"
|
||||
before_script:
|
||||
- set -eu
|
||||
- docker version
|
||||
- docker info
|
||||
- *ecr_login
|
||||
script: |
|
||||
set -eu
|
||||
# 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"
|
||||
|
||||
+8
-3
@@ -1,7 +1,7 @@
|
||||
# =========================
|
||||
# Builder stage
|
||||
# =========================
|
||||
FROM golang:1.23-alpine AS builder
|
||||
FROM public.ecr.aws/docker/library/golang:1.23-alpine AS builder
|
||||
|
||||
RUN apk add --no-cache git ca-certificates tzdata
|
||||
WORKDIR /app
|
||||
@@ -15,14 +15,17 @@ COPY . .
|
||||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
|
||||
go build -trimpath -ldflags="-s -w" -o lti-api ./cmd/api
|
||||
|
||||
# Build SEED binary (pastikan cmd/seed ada)
|
||||
# Build SEED binary
|
||||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
|
||||
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
|
||||
# =========================
|
||||
FROM alpine:3.20
|
||||
FROM public.ecr.aws/docker/library/alpine:3.20
|
||||
|
||||
RUN apk add --no-cache ca-certificates tzdata curl bash postgresql-client \
|
||||
&& adduser -D -H -u 10001 appuser
|
||||
@@ -31,6 +34,8 @@ WORKDIR /app
|
||||
|
||||
COPY --from=builder /app/lti-api /app/lti-api
|
||||
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
|
||||
EXPOSE 8081
|
||||
|
||||
@@ -111,3 +111,4 @@ IT Development PT Mitra Berlian Unggas Group
|
||||
## 📃 License
|
||||
|
||||
> This project is private. All rights reserved.
|
||||
# mr test Sat 7 Feb 2026 00:14:58 WIB
|
||||
|
||||
+141
-73
@@ -1,91 +1,159 @@
|
||||
stages:
|
||||
- deploy
|
||||
- build
|
||||
- gitops
|
||||
|
||||
deploy-dev:
|
||||
stage: deploy
|
||||
image: alpine:3.20
|
||||
variables:
|
||||
AWS_REGION: ap-southeast-3
|
||||
ECR_REGISTRY: 886436954922.dkr.ecr.ap-southeast-3.amazonaws.com
|
||||
ECR_REPO_NAME: mbugroup/lti-api
|
||||
ECR_REPOSITORY: ${ECR_REGISTRY}/${ECR_REPO_NAME}
|
||||
|
||||
DOCKER_HOST: unix:///var/run/docker.sock
|
||||
DOCKER_TLS_CERTDIR: ""
|
||||
DOCKER_BUILDKIT: "1"
|
||||
|
||||
workflow:
|
||||
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:
|
||||
- if: '$CI_COMMIT_BRANCH == "development"'
|
||||
when: on_success
|
||||
- when: never
|
||||
|
||||
variables:
|
||||
DEPLOY_APP: "LTI-MBUGROUP"
|
||||
GIT_SUBMODULE_STRATEGY: recursive
|
||||
GIT_DEPTH: "1"
|
||||
|
||||
IMAGE_TAG: "dev-${CI_COMMIT_SHORT_SHA}"
|
||||
before_script:
|
||||
- echo "🧰 Installing dependencies..."
|
||||
- apk update && apk add --no-cache openssh git curl bash
|
||||
- set -eu
|
||||
- docker version
|
||||
- docker info
|
||||
- *ecr_login
|
||||
script: |
|
||||
set -eu
|
||||
echo "Build & push: $ECR_REPOSITORY:$IMAGE_TAG"
|
||||
|
||||
# Setup SSH di runner
|
||||
- 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 build \
|
||||
-t "$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
|
||||
docker push "$ECR_REPOSITORY:$IMAGE_TAG"
|
||||
|
||||
script:
|
||||
- echo "🚀 Deploying latest code to $SERVER_USER@$SERVER_IP"
|
||||
- >
|
||||
if ssh -o StrictHostKeyChecking=no "$SERVER_USER@$SERVER_IP" "
|
||||
set -e
|
||||
update_gitops_dev_lti:
|
||||
stage: gitops
|
||||
image: public.ecr.aws/docker/library/alpine:3.20
|
||||
tags: [self-hosted-dev]
|
||||
rules:
|
||||
- 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
|
||||
|
||||
cd /home/devops/docker/deployment/development/lti-api
|
||||
echo "Updating DEV image.tag to $IMAGE_TAG in $VALUES_FILE"
|
||||
yq -i '.image.repository = strenv(ECR_REPOSITORY)' "$VALUES_FILE"
|
||||
yq -i '.image.tag = strenv(IMAGE_TAG)' "$VALUES_FILE"
|
||||
|
||||
# Pastikan remote origin SSH (antisipasi kalau pernah ke-set HTTPS)
|
||||
git remote set-url origin git@gitlab.com:mbugroup/lti-api.git
|
||||
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"
|
||||
|
||||
# Pastikan server percaya gitlab.com juga (untuk git fetch via SSH)
|
||||
mkdir -p ~/.ssh
|
||||
ssh-keyscan -H gitlab.com >> ~/.ssh/known_hosts
|
||||
# =========================
|
||||
# PROD
|
||||
# =========================
|
||||
# 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"
|
||||
|
||||
# Fetch/reset pakai SSH
|
||||
GIT_SSH_COMMAND='ssh -o StrictHostKeyChecking=no' git fetch origin development
|
||||
git reset --hard origin/development
|
||||
# docker build \
|
||||
# -t "$ECR_REPOSITORY:$IMAGE_TAG" \
|
||||
# .
|
||||
|
||||
docker compose restart dev-api-lti || docker compose up -d dev-api-lti
|
||||
"; then
|
||||
STATUS='success';
|
||||
else
|
||||
STATUS='failed';
|
||||
fi;
|
||||
# docker push "$ECR_REPOSITORY:$IMAGE_TAG"
|
||||
|
||||
RUN_URL="${CI_PROJECT_URL}/-/pipelines/${CI_PIPELINE_ID}";
|
||||
# update_gitops_prod_lti:
|
||||
# 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
|
||||
|
||||
if [ "$STATUS" = "success" ]; then
|
||||
COLOR=3066993;
|
||||
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;
|
||||
# echo "Updating PROD image.tag to $IMAGE_TAG in $VALUES_FILE"
|
||||
# yq -i '.image.repository = strenv(ECR_REPOSITORY)' "$VALUES_FILE"
|
||||
# yq -i '.image.tag = strenv(IMAGE_TAG)' "$VALUES_FILE"
|
||||
|
||||
echo "{
|
||||
\"username\": \"CI Bot\",
|
||||
\"embeds\": [{
|
||||
\"title\": \"$TITLE\",
|
||||
\"description\": \"$DESC\",
|
||||
\"color\": $COLOR,
|
||||
\"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
|
||||
# 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"
|
||||
@@ -1,104 +0,0 @@
|
||||
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,9 +220,6 @@ func shouldSkipStockableForUsable(req AllocateRequest, stockableType string) boo
|
||||
if (usableType == "PROJECT_CHICKIN" || functionCode == "CHICKIN_OUT") && stockable == "PROJECT_FLOCK_POPULATION" {
|
||||
return true
|
||||
}
|
||||
if (usableType == "STOCK_TRANSFER_OUT" || functionCode == "STOCK_TRANSFER_OUT") && stockable == "PROJECT_FLOCK_POPULATION" {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
@@ -499,6 +496,10 @@ func (s *fifoStockV2Service) Reflow(ctx context.Context, req ReflowRequest) (*Re
|
||||
if len(rollbackRes.Details) > 0 {
|
||||
result.Rollback.Details = append(result.Rollback.Details, rollbackRes.Details...)
|
||||
}
|
||||
minDesired := rollbackRes.ReleasedQty + usableRow.PendingQuantity
|
||||
if desiredQty < minDesired {
|
||||
desiredQty = minDesired
|
||||
}
|
||||
|
||||
if desiredQty <= 0 {
|
||||
continue
|
||||
@@ -701,17 +702,16 @@ func (s *fifoStockV2Service) resolveRollbackFlagGroup(ctx context.Context, tx *g
|
||||
FlagGroupCode string `gorm:"column:flag_group_code"`
|
||||
}
|
||||
var latest row
|
||||
latestQuery := tx.WithContext(ctx).
|
||||
err := tx.WithContext(ctx).
|
||||
Table("stock_allocations").
|
||||
Select("flag_group_code").
|
||||
Where("usable_type = ? AND usable_id = ?", req.Usable.LegacyTypeKey, req.Usable.ID).
|
||||
Where("engine_version = 'v2'").
|
||||
Where("allocation_purpose = ?", defaultAllocationPurpose()).
|
||||
Where("flag_group_code IS NOT NULL AND flag_group_code <> ''")
|
||||
if code := strings.TrimSpace(req.Usable.FunctionCode); code != "" {
|
||||
latestQuery = latestQuery.Where("function_code = ?", code)
|
||||
}
|
||||
err := latestQuery.Order("id DESC").Limit(1).Take(&latest).Error
|
||||
Where("flag_group_code IS NOT NULL AND flag_group_code <> ''").
|
||||
Order("id DESC").
|
||||
Limit(1).
|
||||
Take(&latest).Error
|
||||
if err == nil && strings.TrimSpace(latest.FlagGroupCode) != "" {
|
||||
return latest.FlagGroupCode, nil
|
||||
}
|
||||
@@ -719,56 +719,19 @@ func (s *fifoStockV2Service) resolveRollbackFlagGroup(ctx context.Context, tx *g
|
||||
return "", err
|
||||
}
|
||||
|
||||
rulesQuery := tx.WithContext(ctx).
|
||||
var rules []routeRule
|
||||
err = tx.WithContext(ctx).
|
||||
Table("fifo_stock_v2_route_rules").
|
||||
Where("is_active = TRUE").
|
||||
Where("lane = ?", string(LaneUsable)).
|
||||
Where("legacy_type_key = ?", req.Usable.LegacyTypeKey)
|
||||
if code := strings.TrimSpace(req.Usable.FunctionCode); code != "" {
|
||||
rulesQuery = rulesQuery.Where("function_code = ?", code)
|
||||
}
|
||||
|
||||
var rules []routeRule
|
||||
err = rulesQuery.Find(&rules).Error
|
||||
Where("legacy_type_key = ?", req.Usable.LegacyTypeKey).
|
||||
Find(&rules).Error
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(rules) == 0 {
|
||||
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 {
|
||||
return "", fmt.Errorf("ambiguous rollback flag group for usable type %s", req.Usable.LegacyTypeKey)
|
||||
}
|
||||
|
||||
@@ -57,6 +57,7 @@ var (
|
||||
SSOPortalURL string
|
||||
SSOClients map[string]SSOClientConfig
|
||||
SSOAccessCookieName string
|
||||
SSOAccessCookieFallback []string
|
||||
SSORefreshCookieName string
|
||||
SSOCookieDomain string
|
||||
SSOCookieSecure bool
|
||||
@@ -107,7 +108,7 @@ func init() {
|
||||
JWTResetPasswordExp = viper.GetInt("JWT_RESET_PASSWORD_EXP_MINUTES")
|
||||
JWTVerifyEmailExp = viper.GetInt("JWT_VERIFY_EMAIL_EXP_MINUTES")
|
||||
|
||||
//Cors
|
||||
// Cors
|
||||
CORSAllowOrigins = parseList("CORS_ALLOW_ORIGINS")
|
||||
CORSAllowMethods = parseListWithDefault("CORS_ALLOW_METHODS", "GET,POST,PUT,PATCH,DELETE,OPTIONS")
|
||||
CORSAllowHeaders = parseListWithDefault("CORS_ALLOW_HEADERS", "Content-Type,Authorization,X-Requested-With")
|
||||
@@ -147,6 +148,7 @@ func init() {
|
||||
SSOGetMeURL = viper.GetString("SSO_GETME_URL")
|
||||
SSOPortalURL = strings.TrimSpace(viper.GetString("SSO_PORTAL_URL"))
|
||||
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")
|
||||
SSOCookieDomain = viper.GetString("SSO_COOKIE_DOMAIN")
|
||||
SSOCookieSecure = viper.GetBool("SSO_COOKIE_SECURE")
|
||||
@@ -259,10 +261,6 @@ func defaultString(v, def string) string {
|
||||
return v
|
||||
}
|
||||
|
||||
func LayingWeekStart() int {
|
||||
return TransferToLayingGrowingMaxWeek
|
||||
}
|
||||
|
||||
func joinPath(parts ...string) string {
|
||||
out := make([]string, 0, len(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{
|
||||
Logger: logger.Default.LogMode(logger.Info),
|
||||
SkipDefaultTransaction: true,
|
||||
PrepareStmt: true,
|
||||
PrepareStmt: false,
|
||||
TranslateError: true,
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -1,250 +1,6 @@
|
||||
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'
|
||||
);
|
||||
-- no-op: moved to 20260306090010_seed_fifo_stock_v2_config_after_core.up.sql
|
||||
-- to ensure FIFO core tables exist before seeding on fresh migrations.
|
||||
|
||||
COMMIT;
|
||||
|
||||
+1
-16
@@ -1,20 +1,5 @@
|
||||
BEGIN;
|
||||
|
||||
-- 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 $$;
|
||||
-- no-op: moved to 20260306090011_disable_chickin_fifo_consumption_after_core.down.sql
|
||||
|
||||
COMMIT;
|
||||
|
||||
+2
-147
@@ -1,151 +1,6 @@
|
||||
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
|
||||
);
|
||||
-- no-op: moved to 20260306090011_disable_chickin_fifo_consumption_after_core.up.sql
|
||||
-- to ensure FIFO core + seed are applied before this data update migration.
|
||||
|
||||
COMMIT;
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
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';
|
||||
-- no-op: moved to 20260306090012_fix_fifo_chickin_out_after_seed.down.sql
|
||||
|
||||
COMMIT;
|
||||
|
||||
@@ -1,34 +1,6 @@
|
||||
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;
|
||||
-- no-op: moved to 20260306090012_fix_fifo_chickin_out_after_seed.up.sql
|
||||
-- to ensure AYAM flag group exists before route-rule upsert.
|
||||
|
||||
COMMIT;
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
BEGIN;
|
||||
|
||||
DELETE FROM fifo_stock_v2_flag_members
|
||||
WHERE flag_name = 'AYAM'
|
||||
AND flag_group_code = 'AYAM';
|
||||
-- no-op: moved to 20260306090013_add_ayam_flag_member_fifo_v2_after_seed.down.sql
|
||||
|
||||
COMMIT;
|
||||
|
||||
@@ -1,13 +1,6 @@
|
||||
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();
|
||||
-- no-op: moved to 20260306090013_add_ayam_flag_member_fifo_v2_after_seed.up.sql
|
||||
-- to ensure AYAM flag group exists before inserting AYAM member.
|
||||
|
||||
COMMIT;
|
||||
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
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
@@ -0,0 +1,250 @@
|
||||
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
@@ -0,0 +1,13 @@
|
||||
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
@@ -0,0 +1,151 @@
|
||||
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;
|
||||
@@ -0,0 +1,9 @@
|
||||
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;
|
||||
@@ -0,0 +1,34 @@
|
||||
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
@@ -0,0 +1,7 @@
|
||||
BEGIN;
|
||||
|
||||
DELETE FROM fifo_stock_v2_flag_members
|
||||
WHERE flag_name = 'AYAM'
|
||||
AND flag_group_code = 'AYAM';
|
||||
|
||||
COMMIT;
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
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;
|
||||
@@ -0,0 +1,28 @@
|
||||
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;
|
||||
@@ -0,0 +1,91 @@
|
||||
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;
|
||||
@@ -0,0 +1,54 @@
|
||||
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;
|
||||
@@ -0,0 +1,41 @@
|
||||
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
@@ -1,118 +0,0 @@
|
||||
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
@@ -1,139 +0,0 @@
|
||||
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
@@ -1,14 +0,0 @@
|
||||
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
@@ -1,86 +0,0 @@
|
||||
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,7 +5,6 @@ import "time"
|
||||
type AdjustmentStock struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
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"`
|
||||
FunctionCode string `gorm:"column:function_code;type:varchar(64)"`
|
||||
TotalQty float64 `gorm:"column:total_qty;default:0"`
|
||||
@@ -19,6 +18,5 @@ type AdjustmentStock struct {
|
||||
AdjNumber string `gorm:"column:adj_number;uniqueIndex;not null"`
|
||||
|
||||
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"`
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ type DailyChecklist struct {
|
||||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
||||
|
||||
Kandang Kandang `gorm:"foreignKey:KandangId;references:Id"`
|
||||
Kandang KandangGroup `gorm:"foreignKey:KandangId;references:Id"`
|
||||
Checklist *Checklist `gorm:"foreignKey:ChecklistId;references:Id"`
|
||||
Creator *User `gorm:"foreignKey:CreatedBy;references:Id"`
|
||||
Tasks []DailyChecklistTask `gorm:"foreignKey:DailyChecklistId;references:Id"`
|
||||
|
||||
@@ -26,6 +26,6 @@ type EmployeeKandang struct {
|
||||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
||||
|
||||
Employee Employee `gorm:"foreignKey:EmployeeId;references:Id"`
|
||||
Kandang Kandang `gorm:"foreignKey:KandangId;references:Id"`
|
||||
Employee Employee `gorm:"foreignKey:EmployeeId;references:Id"`
|
||||
Kandang KandangGroup `gorm:"foreignKey:KandangId;references:Id"`
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ type Kandang struct {
|
||||
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"`
|
||||
LocationId uint `gorm:"not null"`
|
||||
KandangGroupId uint `gorm:"not null"`
|
||||
Capacity float64 `gorm:"not null"`
|
||||
PicId uint `gorm:"not null"`
|
||||
CreatedBy uint `gorm:"not null"`
|
||||
@@ -19,6 +20,7 @@ type Kandang struct {
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||
CreatedUser User `gorm:"foreignKey:CreatedBy;references:Id"`
|
||||
Location Location `gorm:"foreignKey:LocationId;references:Id"`
|
||||
KandangGroup KandangGroup `gorm:"foreignKey:KandangGroupId;references:Id"`
|
||||
Pic User `gorm:"foreignKey:PicId;references:Id"`
|
||||
Warehouses []Warehouse `gorm:"foreignKey:KandangId;references:Id"`
|
||||
ProjectFlockKandangs []ProjectFlockKandang `gorm:"foreignKey:KandangId;references:Id" json:"-"`
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
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"`
|
||||
}
|
||||
@@ -1,12 +1,11 @@
|
||||
package entities
|
||||
|
||||
type ProductWarehouse struct {
|
||||
Id uint `gorm:"primaryKey;column:id"`
|
||||
ProductId uint `gorm:"column:product_id;not null"`
|
||||
WarehouseId uint `gorm:"column:warehouse_id;not null"`
|
||||
ProjectFlockKandangId *uint `gorm:"column:project_flock_kandang_id"`
|
||||
Quantity float64 `gorm:"column:qty;type:numeric(15,3);default:0"`
|
||||
AvailableQty *float64 `gorm:"-"`
|
||||
Id uint `gorm:"primaryKey;column:id"`
|
||||
ProductId uint `gorm:"column:product_id;not null"`
|
||||
WarehouseId uint `gorm:"column:warehouse_id;not null"`
|
||||
ProjectFlockKandangId *uint `gorm:"column:project_flock_kandang_id"`
|
||||
Quantity float64 `gorm:"column:qty;type:numeric(15,3);default:0"`
|
||||
|
||||
// Relations
|
||||
Product Product `gorm:"foreignKey:ProductId;references:Id"`
|
||||
|
||||
@@ -45,6 +45,4 @@ type Recording struct {
|
||||
StandardFcr *float64 `gorm:"-"`
|
||||
PopulationCanChange *bool `gorm:"-"`
|
||||
TransferExecuted *bool `gorm:"-"`
|
||||
IsTransition *bool `gorm:"-"`
|
||||
IsLaying *bool `gorm:"-"`
|
||||
}
|
||||
|
||||
+59
-13
@@ -19,11 +19,11 @@ const (
|
||||
|
||||
// AuthContext keeps authentication details captured by the middleware.
|
||||
type AuthContext struct {
|
||||
Token string
|
||||
Verification *sso.VerificationResult
|
||||
User *entity.User
|
||||
Roles []sso.Role
|
||||
Permissions map[string]struct{}
|
||||
Token string
|
||||
Verification *sso.VerificationResult
|
||||
User *entity.User
|
||||
Roles []sso.Role
|
||||
Permissions map[string]struct{}
|
||||
UserAreaIDs []uint
|
||||
UserLocationIDs []uint
|
||||
UserAllArea bool
|
||||
@@ -36,8 +36,30 @@ type AuthContext struct {
|
||||
func Auth(userService service.UserService, requiredScopes ...string) fiber.Handler {
|
||||
return func(c *fiber.Ctx) error {
|
||||
token := bearerToken(c)
|
||||
if token == "" {
|
||||
token = strings.TrimSpace(c.Cookies(config.SSOAccessCookieName))
|
||||
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 == "" {
|
||||
for _, name := range config.SSOAccessCookieFallback {
|
||||
name = strings.TrimSpace(name)
|
||||
if name == "" || name == primaryName {
|
||||
continue
|
||||
}
|
||||
token = strings.TrimSpace(c.Cookies(name))
|
||||
if token != "" {
|
||||
tokenSource = "cookie:" + name
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if token == "" {
|
||||
return fiber.NewError(fiber.StatusUnauthorized, "Please authenticate")
|
||||
@@ -45,7 +67,11 @@ func Auth(userService service.UserService, requiredScopes ...string) fiber.Handl
|
||||
|
||||
verification, err := sso.VerifyAccessToken(token)
|
||||
if err != nil {
|
||||
utils.Log.WithError(err).Warn("auth: token verification failed")
|
||||
if sso.IsSignatureError(err) {
|
||||
logSignatureError("auth", tokenSource, token, err)
|
||||
} else {
|
||||
utils.Log.WithError(err).Warn("auth: token verification failed")
|
||||
}
|
||||
return fiber.NewError(fiber.StatusUnauthorized, "Please authenticate")
|
||||
}
|
||||
|
||||
@@ -89,11 +115,11 @@ func Auth(userService service.UserService, requiredScopes ...string) fiber.Handl
|
||||
}
|
||||
|
||||
ctx := &AuthContext{
|
||||
Token: token,
|
||||
Verification: verification,
|
||||
User: user,
|
||||
Roles: roles,
|
||||
Permissions: permissions,
|
||||
Token: token,
|
||||
Verification: verification,
|
||||
User: user,
|
||||
Roles: roles,
|
||||
Permissions: permissions,
|
||||
UserAreaIDs: nil,
|
||||
UserLocationIDs: nil,
|
||||
UserAllArea: false,
|
||||
@@ -216,6 +242,26 @@ func hasAllScopes(have, required []string) bool {
|
||||
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.
|
||||
func RequirePermissions(perms ...string) fiber.Handler {
|
||||
required := canonicalPermissions(perms)
|
||||
|
||||
@@ -38,10 +38,9 @@ const (
|
||||
P_ExpenseDocumentRealizations = "lti.expense.document.realization"
|
||||
)
|
||||
const (
|
||||
P_AdjustmentGetAll = "lti.inventory.list"
|
||||
P_AdjustmentCreate = "lti.inventory.create"
|
||||
P_AdjustmentGetOne = "lti.inventory.detail"
|
||||
P_AdjustmentDeleteOne = "lti.inventory.delete"
|
||||
P_AdjustmentGetAll = "lti.inventory.list"
|
||||
P_AdjustmentCreate = "lti.inventory.create"
|
||||
P_AdjustmentGetOne = "lti.inventory.detail"
|
||||
)
|
||||
const (
|
||||
P_ApprovalGetAll = "lti.approval.list"
|
||||
@@ -71,7 +70,6 @@ const (
|
||||
P_TransferGetAll = "lti.inventory.transfer.list"
|
||||
P_TransferGetOne = "lti.inventory.transfer.detail"
|
||||
P_TransferCreateOne = "lti.inventory.transfer.create"
|
||||
P_TransferDeleteOne = "lti.inventory.transfer.delete"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -132,6 +130,8 @@ const (
|
||||
P_KandangsUpdateOne = "lti.master.kandangs.update"
|
||||
P_KandangsDeleteOne = "lti.master.kandangs.delete"
|
||||
|
||||
P_KandangGroups = "lti.daily_checklist.master_data.kandang"
|
||||
|
||||
P_LocationsGetAll = "lti.master.locations.list"
|
||||
P_LocationsGetOne = "lti.master.locations.detail"
|
||||
P_LocationsCreateOne = "lti.master.locations.create"
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
)
|
||||
|
||||
@@ -71,7 +69,7 @@ func ToOverheadDTO(budget *entity.ProjectBudget, realization *entity.ExpenseReal
|
||||
return dto
|
||||
}
|
||||
|
||||
func ToOverheadListDTOs(budgets []entity.ProjectBudget, realizations []entity.ExpenseRealization, totalChickinQty, totalActualPopulation float64, isPerKandang bool, totalKandangCount int, projectFlockKandangCountMap map[uint]int) OverheadListDTO {
|
||||
func ToOverheadListDTOs(budgets []entity.ProjectBudget, realizations []entity.ExpenseRealization, totalChickinQty, totalActualPopulation float64, isPerKandang bool, totalKandangCount int) OverheadListDTO {
|
||||
overheadsByNonstockID := make(map[uint]*OverheadDTO)
|
||||
latestDateByNonstockID := make(map[uint]string)
|
||||
|
||||
@@ -113,35 +111,6 @@ func ToOverheadListDTOs(budgets []entity.ProjectBudget, realizations []entity.Ex
|
||||
qty := realizations[i].Qty
|
||||
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].ActualTotalAmount += totalAmount
|
||||
|
||||
@@ -191,27 +160,6 @@ 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) {
|
||||
if nonstock != nil && nonstock.Id != 0 {
|
||||
return nonstock.Name, nonstock.Uom.Name
|
||||
|
||||
@@ -65,7 +65,7 @@ type SapronakCategoryRowDTO struct {
|
||||
QtyOut float64 `json:"qty_out"`
|
||||
QtyUsed float64 `json:"qty_used"`
|
||||
Description string `json:"description"`
|
||||
ProductCategory []string `json:"product_category"`
|
||||
ProductCategory string `json:"product_category"`
|
||||
UnitPrice float64 `json:"unit_price"`
|
||||
TotalAmount float64 `json:"total_amount"`
|
||||
Notes string `json:"notes"`
|
||||
@@ -183,13 +183,13 @@ func ToSapronakProjectAggregatedFromReport(report *SapronakReportDTO, flag strin
|
||||
"PULLET": 0,
|
||||
}
|
||||
|
||||
buildFlagList := func(productID uint, fallback string) []string {
|
||||
buildFlagList := func(productID uint, fallback string) string {
|
||||
rawFlags := productFlags[productID]
|
||||
if len(rawFlags) == 0 {
|
||||
if fallback == "" {
|
||||
return []string{}
|
||||
return ""
|
||||
}
|
||||
return []string{fallback}
|
||||
return fallback
|
||||
}
|
||||
seen := make(map[string]struct{}, len(rawFlags))
|
||||
ordered := make([]string, 0, len(rawFlags))
|
||||
@@ -220,7 +220,7 @@ func ToSapronakProjectAggregatedFromReport(report *SapronakReportDTO, flag strin
|
||||
}
|
||||
return li < lj
|
||||
})
|
||||
return ordered
|
||||
return strings.Join(ordered, " ")
|
||||
}
|
||||
|
||||
for _, group := range report.Groups {
|
||||
@@ -317,6 +317,27 @@ 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) {
|
||||
if cat == nil {
|
||||
return
|
||||
@@ -345,5 +366,22 @@ func ToSapronakProjectAggregatedFromReport(report *SapronakReportDTO, flag strin
|
||||
buildTotals(result.Doc, "TOTAL DOC")
|
||||
buildTotals(result.Ovk, "TOTAL OVK")
|
||||
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
|
||||
}
|
||||
|
||||
@@ -25,17 +25,17 @@ type ClosingRepository interface {
|
||||
SumMarketingWeightAndQtyByProjectFlockKandangIDsAndFlagNames(ctx context.Context, projectFlockKandangIDs []uint, flagNames []string) (float64, float64, float64, error)
|
||||
SumRecordingEggQtyByProjectFlockKandangIDsAndFlagNames(ctx context.Context, projectFlockKandangIDs []uint, flagNames []string) (float64, error)
|
||||
GetExpeditionHPP(ctx context.Context, projectFlockID uint, projectFlockKandangID *uint) ([]ExpeditionHPPRow, error)
|
||||
FetchSapronakIncoming(ctx context.Context, kandangID uint) ([]SapronakIncomingRow, error)
|
||||
FetchSapronakIncomingDetails(ctx context.Context, kandangID uint) (map[uint][]SapronakDetailRow, error)
|
||||
FetchSapronakUsage(ctx context.Context, pfkID uint) ([]SapronakUsageRow, error)
|
||||
FetchSapronakUsageDetails(ctx context.Context, pfkID uint) (map[uint][]SapronakDetailRow, error)
|
||||
FetchSapronakChickinUsage(ctx context.Context, pfkID uint) ([]SapronakUsageRow, error)
|
||||
FetchSapronakChickinUsageDetails(ctx context.Context, pfkID uint) (map[uint][]SapronakDetailRow, error)
|
||||
FetchSapronakUsageAllocatedDetails(ctx context.Context, projectFlockKandangID uint) (map[uint][]SapronakDetailRow, error)
|
||||
FetchSapronakAdjustments(ctx context.Context, kandangID uint) (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) (map[uint][]SapronakDetailRow, error)
|
||||
FetchSapronakSalesAllocatedDetails(ctx context.Context, projectFlockKandangID uint) (map[uint][]SapronakDetailRow, error)
|
||||
FetchSapronakIncoming(ctx context.Context, kandangID uint, start, end *time.Time) ([]SapronakIncomingRow, error)
|
||||
FetchSapronakIncomingDetails(ctx context.Context, kandangID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, error)
|
||||
FetchSapronakUsage(ctx context.Context, pfkID uint, start, end *time.Time) ([]SapronakUsageRow, error)
|
||||
FetchSapronakUsageDetails(ctx context.Context, pfkID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, error)
|
||||
FetchSapronakChickinUsage(ctx context.Context, pfkID uint, start, end *time.Time) ([]SapronakUsageRow, error)
|
||||
FetchSapronakChickinUsageDetails(ctx context.Context, pfkID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, error)
|
||||
FetchSapronakUsageAllocatedDetails(ctx context.Context, projectFlockKandangID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, error)
|
||||
FetchSapronakAdjustments(ctx context.Context, kandangID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, map[uint][]SapronakDetailRow, error)
|
||||
FetchSapronakTransfers(ctx context.Context, kandangID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, map[uint][]SapronakDetailRow, error)
|
||||
FetchSapronakSales(ctx context.Context, projectFlockKandangID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, error)
|
||||
FetchSapronakSalesAllocatedDetails(ctx context.Context, projectFlockKandangID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, error)
|
||||
GetProductsWithFlagsByIDs(ctx context.Context, productIDs []uint) ([]entity.Product, error)
|
||||
}
|
||||
|
||||
@@ -86,6 +86,8 @@ type SapronakQueryParams struct {
|
||||
Limit int
|
||||
Offset int
|
||||
Search string
|
||||
StartDate *time.Time
|
||||
EndDate *time.Time
|
||||
}
|
||||
|
||||
func (r *ClosingRepositoryImpl) GetSapronak(ctx context.Context, params SapronakQueryParams) ([]SapronakRow, int64, error) {
|
||||
@@ -142,15 +144,33 @@ func (r *ClosingRepositoryImpl) GetSapronak(ctx context.Context, params Sapronak
|
||||
}
|
||||
|
||||
var totalResults int64
|
||||
countSQL := fmt.Sprintf("SELECT COUNT(*) FROM (%s) AS combined%s", unionSQL, searchClause)
|
||||
countArgs := append(append([]any{}, args...), searchArgs...)
|
||||
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
|
||||
}
|
||||
}
|
||||
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 {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
dataArgs := append(append([]any{}, args...), searchArgs...)
|
||||
dataArgs := append(append(append([]any{}, args...), searchArgs...), dateArgs...)
|
||||
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, searchClause)
|
||||
dataSQL := fmt.Sprintf("SELECT * FROM (%s) AS combined%s ORDER BY sort_date ASC, id ASC LIMIT ? OFFSET ?", unionSQL, whereClause)
|
||||
|
||||
var rows []SapronakRow
|
||||
if err := db.Raw(dataSQL, dataArgs...).Scan(&rows).Error; err != nil {
|
||||
@@ -213,6 +233,25 @@ func (r *ClosingRepositoryImpl) GetSapronakSummary(ctx context.Context, params S
|
||||
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(`
|
||||
SELECT
|
||||
product_category AS category,
|
||||
@@ -222,8 +261,8 @@ SELECT
|
||||
FROM (%s) AS combined%s
|
||||
GROUP BY product_category, unit_id, unit
|
||||
ORDER BY product_category ASC, unit ASC
|
||||
`, unionSQL, searchClause)
|
||||
queryArgs := append(append([]any{}, args...), searchArgs...)
|
||||
`, unionSQL, whereClause)
|
||||
queryArgs := append(append(append([]any{}, args...), searchArgs...), dateArgs...)
|
||||
|
||||
var rows []SapronakSummaryRow
|
||||
if err := db.Raw(querySQL, queryArgs...).Scan(&rows).Error; err != nil {
|
||||
@@ -778,6 +817,16 @@ type SapronakDetailRow struct {
|
||||
|
||||
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 {
|
||||
for _, j := range joins {
|
||||
if strings.TrimSpace(j) != "" {
|
||||
@@ -878,6 +927,14 @@ func (r *ClosingRepositoryImpl) fetchSapronakUsage(
|
||||
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(
|
||||
ctx context.Context,
|
||||
table string,
|
||||
@@ -909,11 +966,11 @@ func (r *ClosingRepositoryImpl) fetchSapronakDetails(
|
||||
return scanAndGroupDetails(r.detailQuery(ctx, table, pwJoinCond, joins, selectSQL, where, args...))
|
||||
}
|
||||
|
||||
func (r *ClosingRepositoryImpl) FetchSapronakUsage(ctx context.Context, pfkID uint) ([]SapronakUsageRow, error) {
|
||||
func (r *ClosingRepositoryImpl) FetchSapronakUsage(ctx context.Context, pfkID uint, start, end *time.Time) ([]SapronakUsageRow, error) {
|
||||
if pfkID == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return r.fetchSapronakUsage(
|
||||
db := r.usageQuery(
|
||||
ctx,
|
||||
"recording_stocks rs",
|
||||
"pw.id = rs.product_warehouse_id",
|
||||
@@ -922,13 +979,15 @@ func (r *ClosingRepositoryImpl) FetchSapronakUsage(ctx context.Context, pfkID ui
|
||||
pfkID,
|
||||
sapronakFlagsUsage,
|
||||
)
|
||||
db = applyDateRange(db, "r.record_datetime", start, end)
|
||||
return scanUsage(db)
|
||||
}
|
||||
|
||||
func (r *ClosingRepositoryImpl) FetchSapronakChickinUsage(ctx context.Context, pfkID uint) ([]SapronakUsageRow, error) {
|
||||
func (r *ClosingRepositoryImpl) FetchSapronakChickinUsage(ctx context.Context, pfkID uint, start, end *time.Time) ([]SapronakUsageRow, error) {
|
||||
if pfkID == 0 {
|
||||
return []SapronakUsageRow{}, nil
|
||||
}
|
||||
return r.fetchSapronakUsage(
|
||||
db := r.usageQuery(
|
||||
ctx,
|
||||
"project_chickins pc",
|
||||
"pw.id = pc.product_warehouse_id",
|
||||
@@ -937,10 +996,12 @@ func (r *ClosingRepositoryImpl) FetchSapronakChickinUsage(ctx context.Context, p
|
||||
pfkID,
|
||||
sapronakFlagsChickin,
|
||||
)
|
||||
db = applyDateRange(db, "pc.chick_in_date", start, end)
|
||||
return scanUsage(db)
|
||||
}
|
||||
|
||||
func (r *ClosingRepositoryImpl) FetchSapronakUsageDetails(ctx context.Context, pfkID uint) (map[uint][]SapronakDetailRow, error) {
|
||||
return r.fetchSapronakDetails(
|
||||
func (r *ClosingRepositoryImpl) FetchSapronakUsageDetails(ctx context.Context, pfkID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, error) {
|
||||
db := r.detailQuery(
|
||||
ctx,
|
||||
"recording_stocks rs",
|
||||
"pw.id = rs.product_warehouse_id",
|
||||
@@ -959,10 +1020,12 @@ func (r *ClosingRepositoryImpl) FetchSapronakUsageDetails(ctx context.Context, p
|
||||
pfkID,
|
||||
sapronakFlagsUsage,
|
||||
)
|
||||
db = applyDateRange(db, "r.record_datetime", start, end)
|
||||
return scanAndGroupDetails(db)
|
||||
}
|
||||
|
||||
func (r *ClosingRepositoryImpl) FetchSapronakChickinUsageDetails(ctx context.Context, pfkID uint) (map[uint][]SapronakDetailRow, error) {
|
||||
return r.fetchSapronakDetails(
|
||||
func (r *ClosingRepositoryImpl) FetchSapronakChickinUsageDetails(ctx context.Context, pfkID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, error) {
|
||||
db := r.detailQuery(
|
||||
ctx,
|
||||
"project_chickins pc",
|
||||
"pw.id = pc.product_warehouse_id",
|
||||
@@ -981,13 +1044,16 @@ func (r *ClosingRepositoryImpl) FetchSapronakChickinUsageDetails(ctx context.Con
|
||||
pfkID,
|
||||
sapronakFlagsChickin,
|
||||
)
|
||||
db = applyDateRange(db, "pc.chick_in_date", start, end)
|
||||
return scanAndGroupDetails(db)
|
||||
}
|
||||
|
||||
func (r *ClosingRepositoryImpl) FetchSapronakUsageAllocatedDetails(ctx context.Context, projectFlockKandangID uint) (map[uint][]SapronakDetailRow, error) {
|
||||
func (r *ClosingRepositoryImpl) FetchSapronakUsageAllocatedDetails(ctx context.Context, projectFlockKandangID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, error) {
|
||||
if projectFlockKandangID == 0 {
|
||||
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).
|
||||
Table("stock_allocations AS sa").
|
||||
Select(`
|
||||
@@ -1029,18 +1095,19 @@ 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 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 products p_resolve ON p_resolve.id = COALESCE(pi.product_id, pw_ltt.product_id, pw.product_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_pc.product_id, pw.product_id)").
|
||||
Where("sa.status = ?", entity.StockAllocationStatusActive).
|
||||
Where("sa.allocation_purpose = ?", entity.StockAllocationPurposeConsume).
|
||||
Where("sa.stockable_type <> ?", fifo.StockableKeyProjectFlockPopulation.String()).
|
||||
Where("f.name IN ?", sapronakFlagsAll).
|
||||
Where(`
|
||||
(sa.usable_type = ? AND r.project_flock_kandangs_id = ?)
|
||||
(sa.usable_type = ? AND r.project_flock_kandangs_id = ? AND f.name IN ?)
|
||||
OR
|
||||
(sa.usable_type = ? AND pc_used.project_flock_kandang_id = ?)
|
||||
(sa.usable_type = ? AND pc_used.project_flock_kandang_id = ? AND f.name IN ?)
|
||||
`,
|
||||
fifo.UsableKeyRecordingStock.String(), projectFlockKandangID,
|
||||
fifo.UsableKeyProjectChickin.String(), projectFlockKandangID,
|
||||
fifo.UsableKeyRecordingStock.String(), projectFlockKandangID, sapronakFlagsUsage,
|
||||
fifo.UsableKeyProjectChickin.String(), projectFlockKandangID, sapronakFlagsChickin,
|
||||
)
|
||||
query = r.joinSapronakProductFlag(query, "p_resolve").
|
||||
Group(`
|
||||
@@ -1049,11 +1116,12 @@ func (r *ClosingRepositoryImpl) FetchSapronakUsageAllocatedDetails(ctx context.C
|
||||
po.po_number, st.movement_number, lt.transfer_number, ast.id, pc.id, r.id,
|
||||
pi.price, p_resolve.product_price
|
||||
`)
|
||||
query = applyDateRange(query, dateExpr, start, end)
|
||||
|
||||
return scanAndGroupDetails(query)
|
||||
}
|
||||
|
||||
func (r *ClosingRepositoryImpl) incomingPurchaseBase(ctx context.Context, kandangID uint) *gorm.DB {
|
||||
func (r *ClosingRepositoryImpl) incomingPurchaseBase(ctx context.Context, kandangID uint, start, end *time.Time) *gorm.DB {
|
||||
db := r.withCtx(ctx).
|
||||
Table("purchase_items AS pi").
|
||||
Joins("JOIN purchases po ON po.id = pi.purchase_id AND po.deleted_at IS NULL").
|
||||
@@ -1062,12 +1130,13 @@ func (r *ClosingRepositoryImpl) incomingPurchaseBase(ctx context.Context, kandan
|
||||
Where("w.kandang_id = ?", kandangID).
|
||||
Where("f.name IN ?", sapronakFlagsAll).
|
||||
Where("pi.received_date IS NOT NULL")
|
||||
db = applyDateRange(db, "pi.received_date", start, end)
|
||||
return r.joinSapronakProductFlag(db, "p")
|
||||
}
|
||||
|
||||
func (r *ClosingRepositoryImpl) FetchSapronakIncoming(ctx context.Context, kandangID uint) ([]SapronakIncomingRow, error) {
|
||||
func (r *ClosingRepositoryImpl) FetchSapronakIncoming(ctx context.Context, kandangID uint, start, end *time.Time) ([]SapronakIncomingRow, error) {
|
||||
rows := make([]SapronakIncomingRow, 0)
|
||||
db := r.incomingPurchaseBase(ctx, kandangID).Select(`
|
||||
db := r.incomingPurchaseBase(ctx, kandangID, start, end).Select(`
|
||||
pi.product_id AS product_id,
|
||||
p.name AS product_name,
|
||||
f.name AS flag,
|
||||
@@ -1081,9 +1150,9 @@ func (r *ClosingRepositoryImpl) FetchSapronakIncoming(ctx context.Context, kanda
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
func (r *ClosingRepositoryImpl) FetchSapronakIncomingDetails(ctx context.Context, kandangID uint) (map[uint][]SapronakDetailRow, error) {
|
||||
func (r *ClosingRepositoryImpl) FetchSapronakIncomingDetails(ctx context.Context, kandangID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, error) {
|
||||
return scanAndGroupDetails(
|
||||
r.incomingPurchaseBase(ctx, kandangID).Select(`
|
||||
r.incomingPurchaseBase(ctx, kandangID, start, end).Select(`
|
||||
pi.product_id AS product_id,
|
||||
p.name AS product_name,
|
||||
f.name AS flag,
|
||||
@@ -1178,7 +1247,7 @@ func splitStockLogs(rows []stockLogSapronakRow, refFn func(stockLogSapronakRow)
|
||||
return in, out
|
||||
}
|
||||
|
||||
func (r *ClosingRepositoryImpl) FetchSapronakAdjustments(ctx context.Context, kandangID uint) (map[uint][]SapronakDetailRow, map[uint][]SapronakDetailRow, error) {
|
||||
func (r *ClosingRepositoryImpl) FetchSapronakAdjustments(ctx context.Context, kandangID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, map[uint][]SapronakDetailRow, error) {
|
||||
poByWarehouse := r.DB().
|
||||
Table("purchase_items pi").
|
||||
Select("DISTINCT ON (pi.product_warehouse_id) pi.product_warehouse_id, po.po_number, pi.received_date").
|
||||
@@ -1205,11 +1274,13 @@ func (r *ClosingRepositoryImpl) FetchSapronakAdjustments(ctx context.Context, ka
|
||||
Where("f.name IN ?", sapronakFlagsAll).
|
||||
Where("COALESCE(ast.total_qty, 0) > 0")
|
||||
incomingQuery = r.joinSapronakProductFlag(incomingQuery, "p")
|
||||
incomingQuery = applyDateRange(incomingQuery, "ast.created_at", start, end)
|
||||
incoming, err := scanAndGroupDetails(incomingQuery)
|
||||
if err != nil {
|
||||
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).
|
||||
Table("stock_allocations AS sa").
|
||||
Select(`
|
||||
@@ -1243,6 +1314,7 @@ func (r *ClosingRepositoryImpl) FetchSapronakAdjustments(ctx context.Context, ka
|
||||
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")
|
||||
outgoingQuery = r.joinSapronakProductFlag(outgoingQuery, "p")
|
||||
outgoingQuery = applyDateRange(outgoingQuery, dateExpr, start, end)
|
||||
outgoing, err := scanAndGroupDetails(outgoingQuery)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
@@ -1251,7 +1323,7 @@ func (r *ClosingRepositoryImpl) FetchSapronakAdjustments(ctx context.Context, ka
|
||||
return incoming, outgoing, nil
|
||||
}
|
||||
|
||||
func (r *ClosingRepositoryImpl) FetchSapronakTransfers(ctx context.Context, kandangID uint) (map[uint][]SapronakDetailRow, map[uint][]SapronakDetailRow, error) {
|
||||
func (r *ClosingRepositoryImpl) FetchSapronakTransfers(ctx context.Context, kandangID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, map[uint][]SapronakDetailRow, error) {
|
||||
incomingQuery := r.withCtx(ctx).
|
||||
Table("stock_transfer_details AS std").
|
||||
Select(`
|
||||
@@ -1273,6 +1345,7 @@ func (r *ClosingRepositoryImpl) FetchSapronakTransfers(ctx context.Context, kand
|
||||
Where("(fw.kandang_id IS NULL OR fw.kandang_id <> w.kandang_id)").
|
||||
Where("f.name IN ?", sapronakFlagsAll)
|
||||
incomingQuery = r.joinSapronakProductFlag(incomingQuery, "p")
|
||||
incomingQuery = applyDateRange(incomingQuery, "st.transfer_date", start, end)
|
||||
incoming, err := scanAndGroupDetails(incomingQuery)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
@@ -1300,6 +1373,7 @@ func (r *ClosingRepositoryImpl) FetchSapronakTransfers(ctx context.Context, kand
|
||||
Where("(w_source.kandang_id IS NULL OR w_source.kandang_id <> w.kandang_id)").
|
||||
Where("f.name IN ?", sapronakFlagsAll)
|
||||
incomingLayingQuery = r.joinSapronakProductFlag(incomingLayingQuery, "p")
|
||||
incomingLayingQuery = applyDateRange(incomingLayingQuery, "lt.transfer_date", start, end)
|
||||
incomingLaying, err := scanAndGroupDetails(incomingLayingQuery)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
@@ -1334,6 +1408,7 @@ func (r *ClosingRepositoryImpl) FetchSapronakTransfers(ctx context.Context, kand
|
||||
Where("f.name IN ?", sapronakFlagsAll).
|
||||
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 = applyDateRange(outgoingQuery, "st.transfer_date", start, end)
|
||||
outgoing, err := scanAndGroupDetails(outgoingQuery)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
@@ -1365,6 +1440,7 @@ func (r *ClosingRepositoryImpl) FetchSapronakTransfers(ctx context.Context, kand
|
||||
Where("f.name IN ?", sapronakFlagsAll).
|
||||
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 = applyDateRange(outgoingLayingQuery, "lt.transfer_date", start, end)
|
||||
outgoingLaying, err := scanAndGroupDetails(outgoingLayingQuery)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
@@ -1376,7 +1452,7 @@ func (r *ClosingRepositoryImpl) FetchSapronakTransfers(ctx context.Context, kand
|
||||
return incoming, outgoing, nil
|
||||
}
|
||||
|
||||
func (r *ClosingRepositoryImpl) FetchSapronakSales(ctx context.Context, projectFlockKandangID uint) (map[uint][]SapronakDetailRow, error) {
|
||||
func (r *ClosingRepositoryImpl) FetchSapronakSales(ctx context.Context, projectFlockKandangID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, error) {
|
||||
query := r.withCtx(ctx).
|
||||
Table("stock_allocations AS sa").
|
||||
Select(`
|
||||
@@ -1401,6 +1477,7 @@ 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")
|
||||
|
||||
query = r.joinSapronakProductFlag(query, "p")
|
||||
query = applyDateRange(query, "COALESCE(mdp.delivery_date, mdp.created_at)", start, end)
|
||||
sales, err := scanAndGroupDetails(query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -1434,6 +1511,7 @@ 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")
|
||||
|
||||
nonFifoQuery = r.joinSapronakProductFlag(nonFifoQuery, "p")
|
||||
nonFifoQuery = applyDateRange(nonFifoQuery, "COALESCE(mdp.delivery_date, mdp.created_at)", start, end)
|
||||
nonFifoSales, err := scanAndGroupDetails(nonFifoQuery)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -1446,57 +1524,114 @@ func (r *ClosingRepositoryImpl) FetchSapronakSales(ctx context.Context, projectF
|
||||
return sales, nil
|
||||
}
|
||||
|
||||
func (r *ClosingRepositoryImpl) FetchSapronakSalesAllocatedDetails(ctx context.Context, projectFlockKandangID uint) (map[uint][]SapronakDetailRow, error) {
|
||||
func (r *ClosingRepositoryImpl) FetchSapronakSalesAllocatedDetails(ctx context.Context, projectFlockKandangID uint, start, end *time.Time) (map[uint][]SapronakDetailRow, error) {
|
||||
if projectFlockKandangID == 0 {
|
||||
return map[uint][]SapronakDetailRow{}, nil
|
||||
}
|
||||
|
||||
query := r.withCtx(ctx).
|
||||
Table("stock_allocations AS sa").
|
||||
Select(`
|
||||
pw.product_id AS product_id,
|
||||
p.name AS product_name,
|
||||
f.name AS flag,
|
||||
COALESCE(
|
||||
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
|
||||
) AS date,
|
||||
COALESCE(
|
||||
po.po_number,
|
||||
st.movement_number,
|
||||
lt.transfer_number,
|
||||
CONCAT('ADJ-', ast.id),
|
||||
''
|
||||
) AS reference,
|
||||
)
|
||||
END
|
||||
`, pfpType)
|
||||
|
||||
query := r.withCtx(ctx).
|
||||
Table("stock_allocations AS sa").
|
||||
Select(fmt.Sprintf(`
|
||||
p_resolve.id AS product_id,
|
||||
p_resolve.name AS product_name,
|
||||
f.name AS flag,
|
||||
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 AS date,
|
||||
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,
|
||||
st.movement_number,
|
||||
lt.transfer_number,
|
||||
CASE WHEN ast.id IS NOT NULL THEN CONCAT('ADJ-', ast.id) END,
|
||||
''
|
||||
)
|
||||
END AS reference,
|
||||
0 AS qty_in,
|
||||
COALESCE(SUM(sa.qty), 0) AS qty_out,
|
||||
COALESCE(pi.price, p.product_price, 0) AS price
|
||||
`).
|
||||
Joins("JOIN product_warehouses pw ON pw.id = sa.product_warehouse_id").
|
||||
Joins("JOIN products p ON p.id = pw.product_id").
|
||||
CASE
|
||||
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("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 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 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 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 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.allocation_purpose = ?", entity.StockAllocationPurposeConsume).
|
||||
Where("sa.stockable_type <> ?", fifo.StockableKeyProjectFlockPopulation.String()).
|
||||
Where("pw.project_flock_kandang_id = ?", projectFlockKandangID).
|
||||
Where("f.name IN ?", sapronakFlagsAll).
|
||||
Group(`
|
||||
pw.product_id, p.name, f.name,
|
||||
p_resolve.id, p_resolve.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,
|
||||
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,
|
||||
pi.price, p.product_price
|
||||
pi_pc.price, pi.price, p_resolve.product_price, sa.stockable_type
|
||||
`)
|
||||
|
||||
query = r.joinSapronakProductFlag(query, "p")
|
||||
query = r.joinSapronakProductFlag(query, "p_resolve")
|
||||
query = applyDateRange(query, dateExpr, start, end)
|
||||
return scanAndGroupDetails(query)
|
||||
}
|
||||
|
||||
@@ -1510,7 +1645,6 @@ func (r *ClosingRepositoryImpl) GetProductsWithFlagsByIDs(ctx context.Context, p
|
||||
Preload("Flags").
|
||||
Where("id IN ?", productIDs).
|
||||
Find(&products).Error
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
@@ -33,6 +32,14 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type activeKandangMetric struct {
|
||||
ProjectFlockKandangID uint
|
||||
ProjectFlockID uint
|
||||
KandangID uint
|
||||
Category string
|
||||
Metric float64
|
||||
}
|
||||
|
||||
type ClosingService interface {
|
||||
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]dto.ClosingListItemDTO, int64, error)
|
||||
GetProjectFlockByID(ctx *fiber.Ctx, id uint) (*entity.ProjectFlock, error)
|
||||
@@ -385,6 +392,11 @@ func (s closingService) GetClosingSapronak(c *fiber.Ctx, projectFlockID uint, pa
|
||||
}
|
||||
|
||||
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{
|
||||
Type: params.Type,
|
||||
WarehouseIDs: warehouseIDs,
|
||||
@@ -392,6 +404,8 @@ func (s closingService) GetClosingSapronak(c *fiber.Ctx, projectFlockID uint, pa
|
||||
Limit: params.Limit,
|
||||
Offset: offset,
|
||||
Search: params.Search,
|
||||
StartDate: startDate,
|
||||
EndDate: endDate,
|
||||
})
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to fetch sapronak %s for project flock %d: %+v", params.Type, projectFlockID, err)
|
||||
@@ -468,11 +482,19 @@ 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{
|
||||
Type: params.Type,
|
||||
WarehouseIDs: warehouseIDs,
|
||||
ProjectFlockKandangIDs: projectFlockKandangIDs,
|
||||
Search: params.Search,
|
||||
StartDate: startDate,
|
||||
EndDate: endDate,
|
||||
})
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to fetch sapronak %s summary for project flock %d: %+v", params.Type, projectFlockID, err)
|
||||
@@ -542,6 +564,90 @@ func (s closingService) getProjectFlockKandangIDs(ctx context.Context, projectFl
|
||||
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 {
|
||||
qtyStr := strconv.FormatFloat(qty, 'f', -1, 64)
|
||||
if uom == "" {
|
||||
@@ -616,38 +722,17 @@ func (s closingService) GetOverhead(c *fiber.Ctx, projectFlockID uint, projectFl
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -688,11 +773,197 @@ func (s closingService) GetOverhead(c *fiber.Ctx, projectFlockID uint, projectFl
|
||||
|
||||
totalActualPopulation := totalChickinQty - totalDepletion
|
||||
|
||||
result := dto.ToOverheadListDTOs(budgets, realizations, totalChickinQty, totalActualPopulation, projectFlockKandangID != nil, totalKandangCount, projectFlockKandangCountMap)
|
||||
result := dto.ToOverheadListDTOs(budgets, realizations, totalChickinQty, totalActualPopulation, projectFlockKandangID != nil, totalKandangCount)
|
||||
|
||||
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) {
|
||||
if projectFlockKandangID != nil {
|
||||
if err := m.EnsureProjectFlockKandangAccess(c, s.Repository.DB(), projectFlockID, *projectFlockKandangID); err != nil {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
@@ -123,7 +124,7 @@ func (s sapronakService) computeSapronakReports(ctx context.Context, params *val
|
||||
continue
|
||||
}
|
||||
|
||||
// We no longer filter by date for closing sapronak report; pass nil pointers.
|
||||
// Filter sapronak data by project flock period range.
|
||||
items, groups, totalIncoming, totalUsage, err := s.buildSapronakItems(ctx, pfk, params.Flag)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to build sapronak items for pfk %d: %+v", pfk.Id, err)
|
||||
@@ -379,33 +380,33 @@ func buildSapronakDetails(
|
||||
}
|
||||
|
||||
func (s sapronakService) buildSapronakItems(ctx context.Context, pfk entity.ProjectFlockKandang, flagFilter string) ([]dto.SapronakItemDTO, []dto.SapronakGroupDTO, float64, float64, error) {
|
||||
// For sapronak closing report we intentionally ignore date range
|
||||
// and aggregate all historical transactions for the kandang/project.
|
||||
incomingRows, err := s.Repository.FetchSapronakIncoming(ctx, pfk.KandangId)
|
||||
// Filter by project flock period (start = first chickin or pfk created_at, end = closed_at if any).
|
||||
startDate, endDate := sapronakPeriodRange(pfk)
|
||||
incomingRows, err := s.Repository.FetchSapronakIncoming(ctx, pfk.KandangId, startDate, endDate)
|
||||
if err != nil {
|
||||
return nil, nil, 0, 0, err
|
||||
}
|
||||
incomingDetailsRows, err := s.Repository.FetchSapronakIncomingDetails(ctx, pfk.KandangId)
|
||||
incomingDetailsRows, err := s.Repository.FetchSapronakIncomingDetails(ctx, pfk.KandangId, startDate, endDate)
|
||||
if err != nil {
|
||||
return nil, nil, 0, 0, err
|
||||
}
|
||||
usageRows, err := s.Repository.FetchSapronakUsage(ctx, pfk.Id)
|
||||
usageRows, err := s.Repository.FetchSapronakUsage(ctx, pfk.Id, startDate, endDate)
|
||||
if err != nil {
|
||||
return nil, nil, 0, 0, err
|
||||
}
|
||||
chickinUsageRows, err := s.Repository.FetchSapronakChickinUsage(ctx, pfk.Id)
|
||||
chickinUsageRows, err := s.Repository.FetchSapronakChickinUsage(ctx, pfk.Id, startDate, endDate)
|
||||
if err != nil {
|
||||
return nil, nil, 0, 0, err
|
||||
}
|
||||
usageDetailsRows, err := s.Repository.FetchSapronakUsageDetails(ctx, pfk.Id)
|
||||
usageDetailsRows, err := s.Repository.FetchSapronakUsageDetails(ctx, pfk.Id, startDate, endDate)
|
||||
if err != nil {
|
||||
return nil, nil, 0, 0, err
|
||||
}
|
||||
chickinUsageDetailsRows, err := s.Repository.FetchSapronakChickinUsageDetails(ctx, pfk.Id)
|
||||
chickinUsageDetailsRows, err := s.Repository.FetchSapronakChickinUsageDetails(ctx, pfk.Id, startDate, endDate)
|
||||
if err != nil {
|
||||
return nil, nil, 0, 0, err
|
||||
}
|
||||
usageAllocatedDetails, err := s.Repository.FetchSapronakUsageAllocatedDetails(ctx, pfk.Id)
|
||||
usageAllocatedDetails, err := s.Repository.FetchSapronakUsageAllocatedDetails(ctx, pfk.Id, startDate, endDate)
|
||||
if err != nil {
|
||||
return nil, nil, 0, 0, err
|
||||
}
|
||||
@@ -413,15 +414,15 @@ func (s sapronakService) buildSapronakItems(ctx context.Context, pfk entity.Proj
|
||||
usageDetailsRows = usageAllocatedDetails
|
||||
chickinUsageDetailsRows = map[uint][]repository.SapronakDetailRow{}
|
||||
}
|
||||
adjIncomingRows, adjOutgoingRows, err := s.Repository.FetchSapronakAdjustments(ctx, pfk.KandangId)
|
||||
adjIncomingRows, adjOutgoingRows, err := s.Repository.FetchSapronakAdjustments(ctx, pfk.KandangId, startDate, endDate)
|
||||
if err != nil {
|
||||
return nil, nil, 0, 0, err
|
||||
}
|
||||
transIncomingRows, transOutgoingRows, err := s.Repository.FetchSapronakTransfers(ctx, pfk.KandangId)
|
||||
transIncomingRows, transOutgoingRows, err := s.Repository.FetchSapronakTransfers(ctx, pfk.KandangId, startDate, endDate)
|
||||
if err != nil {
|
||||
return nil, nil, 0, 0, err
|
||||
}
|
||||
salesOutRows, err := s.Repository.FetchSapronakSalesAllocatedDetails(ctx, pfk.Id)
|
||||
salesOutRows, err := s.Repository.FetchSapronakSalesAllocatedDetails(ctx, pfk.Id, startDate, endDate)
|
||||
if err != nil {
|
||||
return nil, nil, 0, 0, err
|
||||
}
|
||||
@@ -470,6 +471,7 @@ func (s sapronakService) buildSapronakItems(ctx context.Context, pfk entity.Proj
|
||||
// should not be counted yet. Only when category is LAYING we allow
|
||||
// pullet usage to contribute to qty_used.
|
||||
isLaying := strings.EqualFold(string(pfk.ProjectFlock.Category), string(utils.ProjectFlockCategoryLaying))
|
||||
hasChickin := len(pfk.Chickins) > 0
|
||||
|
||||
if !isLaying {
|
||||
filteredUsage := make([]repository.SapronakUsageRow, 0, len(chickinUsageRows))
|
||||
@@ -491,11 +493,6 @@ func (s sapronakService) buildSapronakItems(ctx context.Context, pfk entity.Proj
|
||||
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 {
|
||||
if len(rows) == 0 {
|
||||
continue
|
||||
@@ -512,6 +509,11 @@ func (s sapronakService) buildSapronakItems(ctx context.Context, pfk entity.Proj
|
||||
transOutgoing := detailMaps.TransferOut
|
||||
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)
|
||||
transOutgoing = dedupTransfers(transOutgoing)
|
||||
|
||||
@@ -775,6 +777,9 @@ func (s sapronakService) buildSapronakItems(ctx context.Context, pfk entity.Proj
|
||||
if !matchesFlag(flag) {
|
||||
continue
|
||||
}
|
||||
if hasChickin && (strings.EqualFold(flag, "DOC") || strings.EqualFold(flag, "PULLET") || strings.EqualFold(flag, "LAYER")) {
|
||||
continue
|
||||
}
|
||||
group := ensureGroup(flag)
|
||||
for _, d := range details {
|
||||
if d.Flag == "" {
|
||||
@@ -794,6 +799,10 @@ func (s sapronakService) buildSapronakItems(ctx context.Context, pfk entity.Proj
|
||||
if !matchesFlag(flag) {
|
||||
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)
|
||||
for _, d := range details {
|
||||
if d.Flag == "" {
|
||||
@@ -815,3 +824,20 @@ func (s sapronakService) buildSapronakItems(ctx context.Context, pfk entity.Proj
|
||||
|
||||
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,9 +64,11 @@ func (u *DailyChecklistController) GetAll(c *fiber.Ctx) error {
|
||||
status = *item.Status
|
||||
}
|
||||
|
||||
var kandang *kandangDTO.KandangRelationDTO
|
||||
// var kandang *kandangDTO.KandangRelationDTO
|
||||
var kandang *kandangDTO.KandangGroupRelationDTO
|
||||
if item.Kandang.Id != 0 {
|
||||
mapped := kandangDTO.ToKandangRelationDTO(item.Kandang)
|
||||
// mapped := kandangDTO.ToKandangRelationDTO(item.Kandang)x
|
||||
mapped := kandangDTO.ToKandangGroupRelationDTO(item.Kandang)
|
||||
kandang = &mapped
|
||||
}
|
||||
|
||||
|
||||
@@ -19,19 +19,19 @@ type DailyChecklistRelationDTO struct {
|
||||
}
|
||||
|
||||
type DailyChecklistListDTO struct {
|
||||
Id uint `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Status string `json:"status"`
|
||||
Category string `json:"category"`
|
||||
Date time.Time `json:"date"`
|
||||
Kandang *kandangDTO.KandangRelationDTO `json:"kandang,omitempty"`
|
||||
CreatedUser *userDTO.UserRelationDTO `json:"created_user"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
TotalPhase int `json:"total_phase"`
|
||||
TotalActivity int `json:"total_activity"`
|
||||
Progress int `json:"progress"`
|
||||
RejectReason *string `json:"reject_reason"`
|
||||
Id uint `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Status string `json:"status"`
|
||||
Category string `json:"category"`
|
||||
Date time.Time `json:"date"`
|
||||
Kandang *kandangDTO.KandangGroupRelationDTO `json:"kandang,omitempty"`
|
||||
CreatedUser *userDTO.UserRelationDTO `json:"created_user"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
TotalPhase int `json:"total_phase"`
|
||||
TotalActivity int `json:"total_activity"`
|
||||
Progress int `json:"progress"`
|
||||
RejectReason *string `json:"reject_reason"`
|
||||
}
|
||||
|
||||
type DailyChecklistDetailDTO struct {
|
||||
@@ -156,9 +156,9 @@ func ToDailyChecklistListDTO(e entity.DailyChecklist) DailyChecklistListDTO {
|
||||
status = *e.Status
|
||||
}
|
||||
|
||||
var kandang *kandangDTO.KandangRelationDTO
|
||||
var kandang *kandangDTO.KandangGroupRelationDTO
|
||||
if e.Kandang.Id != 0 {
|
||||
mapped := kandangDTO.ToKandangRelationDTO(e.Kandang)
|
||||
mapped := kandangDTO.ToKandangGroupRelationDTO(e.Kandang)
|
||||
kandang = &mapped
|
||||
}
|
||||
|
||||
|
||||
@@ -75,7 +75,7 @@ type DailyChecklistListItem struct {
|
||||
RejectReason *string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
Kandang entity.Kandang
|
||||
Kandang entity.KandangGroup
|
||||
TotalPhase int
|
||||
TotalActivity int
|
||||
Progress int
|
||||
@@ -142,7 +142,7 @@ func (s dailyChecklistService) ensureChecklistAccess(c *fiber.Ctx, checklistID u
|
||||
|
||||
db := s.Repository.DB().WithContext(c.Context()).
|
||||
Table("daily_checklists dc").
|
||||
Joins("JOIN kandangs k ON k.id = dc.kandang_id").
|
||||
Joins("JOIN kandang_groups k ON k.id = dc.kandang_id").
|
||||
Joins("JOIN locations loc ON loc.id = k.location_id").
|
||||
Joins("JOIN areas a ON a.id = loc.area_id").
|
||||
Where("dc.id = ?", checklistID)
|
||||
@@ -168,7 +168,7 @@ func (s dailyChecklistService) ensureKandangAccess(c *fiber.Ctx, kandangID uint)
|
||||
}
|
||||
|
||||
db := s.Repository.DB().WithContext(c.Context()).
|
||||
Table("kandangs k").
|
||||
Table("kandang_groups k").
|
||||
Joins("JOIN locations loc ON loc.id = k.location_id").
|
||||
Joins("JOIN areas a ON a.id = loc.area_id").
|
||||
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()).
|
||||
Table("daily_checklist_activity_tasks t").
|
||||
Joins("JOIN daily_checklists dc ON dc.id = t.checklist_id").
|
||||
Joins("JOIN kandangs k ON k.id = dc.kandang_id").
|
||||
Joins("JOIN kandang_groups k ON k.id = dc.kandang_id").
|
||||
Joins("JOIN locations loc ON loc.id = k.location_id").
|
||||
Joins("JOIN areas a ON a.id = loc.area_id").
|
||||
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()).
|
||||
Table("daily_checklists dc").
|
||||
Joins("JOIN kandangs k ON k.id = dc.kandang_id").
|
||||
Joins("JOIN kandang_groups k ON k.id = dc.kandang_id").
|
||||
Joins("JOIN locations loc ON loc.id = k.location_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.Kandang)
|
||||
kandangMap := make(map[uint]entity.KandangGroup)
|
||||
if len(kandangIDs) > 0 {
|
||||
var kandangs []entity.Kandang
|
||||
var kandangs []entity.KandangGroup
|
||||
if err := s.Repository.DB().WithContext(c.Context()).
|
||||
Where("id IN ?", kandangIDs).
|
||||
Preload("Location").
|
||||
@@ -1019,7 +1019,7 @@ func (s dailyChecklistService) GetSummary(c *fiber.Ctx, params *validation.Summa
|
||||
MAX(a.updated_at) AS last_activity`).
|
||||
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 kandangs k ON k.id = d.kandang_id").
|
||||
Joins("JOIN kandang_groups k ON k.id = d.kandang_id").
|
||||
Joins("JOIN employees e ON e.id = a.employee_id").
|
||||
Joins("JOIN locations loc ON loc.id = k.location_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_checklists dc ON dc.id = dcat.checklist_id").
|
||||
Joins("JOIN employees e ON e.id = dca.employee_id").
|
||||
Joins("JOIN kandangs k ON k.id = dc.kandang_id").
|
||||
Joins("JOIN kandang_groups k ON k.id = dc.kandang_id").
|
||||
Joins("JOIN locations loc ON loc.id = k.location_id").
|
||||
Joins("JOIN areas a ON a.id = loc.area_id").
|
||||
Joins("JOIN phases p ON p.id = dcat.phase_id").
|
||||
|
||||
@@ -69,23 +69,19 @@ func (r *ExpenseRealizationRepositoryImpl) GetClosingOverhead(ctx context.Contex
|
||||
Joins("JOIN expense_nonstocks ON expense_nonstocks.id = expense_realizations.expense_nonstock_id").
|
||||
Joins("JOIN expenses ON expenses.id = expense_nonstocks.expense_id").
|
||||
Joins("LEFT JOIN 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.category = ?", "BOP")
|
||||
|
||||
if projectFlockKandangID != nil {
|
||||
db = db.Where(`(
|
||||
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)
|
||||
)`, *projectFlockKandangID, *projectFlockKandangID, fmt.Sprintf("[%d]", projectFlockID))
|
||||
)`, *projectFlockKandangID, fmt.Sprintf("[%d]", projectFlockID))
|
||||
} else {
|
||||
db = db.Where(`(
|
||||
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)
|
||||
)`, projectFlockID, projectFlockID, fmt.Sprintf("[%d]", projectFlockID))
|
||||
)`, projectFlockID, fmt.Sprintf("[%d]", projectFlockID))
|
||||
}
|
||||
|
||||
err := db.Find(&realizations).Error
|
||||
|
||||
@@ -103,22 +103,3 @@ func (u *AdjustmentController) GetOne(c *fiber.Ctx) error {
|
||||
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",
|
||||
})
|
||||
}
|
||||
|
||||
+25
-214
@@ -2,12 +2,12 @@ package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
@@ -15,19 +15,9 @@ import (
|
||||
type AdjustmentStockRepository interface {
|
||||
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)
|
||||
GetByIDForUpdate(ctx context.Context, id uint) (*entity.AdjustmentStock, 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)
|
||||
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
|
||||
FindOverconsumeRule(ctx context.Context, lane, flagGroupCode, functionCode string) (*bool, error)
|
||||
FindHistory(ctx context.Context, filter AdjustmentHistoryFilter, modifier func(*gorm.DB) *gorm.DB) ([]*entity.AdjustmentStock, int64, error)
|
||||
WithTx(tx *gorm.DB) AdjustmentStockRepository
|
||||
DB() *gorm.DB
|
||||
@@ -54,13 +44,6 @@ type AdjustmentHistoryFilter struct {
|
||||
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 {
|
||||
db *gorm.DB
|
||||
}
|
||||
@@ -90,17 +73,6 @@ func (r *adjustmentStockRepositoryImpl) GetByID(ctx context.Context, id uint, mo
|
||||
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) {
|
||||
type pfkRow struct {
|
||||
KandangID uint `gorm:"column:kandang_id"`
|
||||
@@ -119,21 +91,6 @@ func (r *adjustmentStockRepositoryImpl) FindKandangIDByProjectFlockKandangID(ctx
|
||||
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(
|
||||
ctx context.Context,
|
||||
productID uint,
|
||||
@@ -165,183 +122,37 @@ func (r *adjustmentStockRepositoryImpl) FindRoutesByFunctionCode(
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
func (r *adjustmentStockRepositoryImpl) LoadDownstreamDependencies(
|
||||
func (r *adjustmentStockRepositoryImpl) FindOverconsumeRule(
|
||||
ctx context.Context,
|
||||
stockableType string,
|
||||
stockableIDs []uint,
|
||||
) ([]AdjustmentDownstreamDependency, error) {
|
||||
if strings.TrimSpace(stockableType) == "" || len(stockableIDs) == 0 {
|
||||
return nil, nil
|
||||
lane string,
|
||||
flagGroupCode string,
|
||||
functionCode string,
|
||||
) (*bool, error) {
|
||||
type selectedRow struct {
|
||||
AllowOverconsume bool `gorm:"column:allow_overconsume"`
|
||||
}
|
||||
|
||||
var rows []AdjustmentDownstreamDependency
|
||||
var selected selectedRow
|
||||
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
|
||||
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, err
|
||||
}
|
||||
|
||||
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
|
||||
return &selected.AllowOverconsume, nil
|
||||
}
|
||||
|
||||
func (r *adjustmentStockRepositoryImpl) FindHistory(
|
||||
|
||||
@@ -15,9 +15,8 @@ func AdjustmentRoutes(v1 fiber.Router, u user.UserService, s adjustment.Adjustme
|
||||
route := v1.Group("/adjustments")
|
||||
route.Use(m.Auth(u))
|
||||
// Standard CRUD routes following master data pattern
|
||||
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.Get("/:id", m.RequirePermissions(m.P_AdjustmentGetOne), ctrl.GetOne)
|
||||
route.Delete("/:id", m.RequirePermissions(m.P_AdjustmentDeleteOne), ctrl.DeleteOne)
|
||||
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.Get("/:id",m.RequirePermissions(m.P_AdjustmentGetOne), ctrl.GetOne)
|
||||
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
@@ -30,7 +29,6 @@ import (
|
||||
type AdjustmentService interface {
|
||||
Adjustment(ctx *fiber.Ctx, req *validation.Create) (*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)
|
||||
}
|
||||
|
||||
@@ -50,6 +48,7 @@ type adjustmentService struct {
|
||||
const (
|
||||
adjustmentLaneStockable = "STOCKABLE"
|
||||
adjustmentLaneUsable = "USABLE"
|
||||
flagGroupAyam = "AYAM"
|
||||
)
|
||||
|
||||
func NewAdjustmentService(
|
||||
@@ -77,21 +76,23 @@ func NewAdjustmentService(
|
||||
}
|
||||
}
|
||||
|
||||
func (s *adjustmentService) withRelations(db *gorm.DB) *gorm.DB {
|
||||
return db.
|
||||
Preload("ProductWarehouse").
|
||||
Preload("ProductWarehouse.Product").
|
||||
Preload("ProductWarehouse.Warehouse").
|
||||
Preload("ProductWarehouse.Warehouse.Location").
|
||||
Preload("ProductWarehouse.ProjectFlockKandang").
|
||||
Preload("ProductWarehouse.ProjectFlockKandang.ProjectFlock").
|
||||
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, func(db *gorm.DB) *gorm.DB {
|
||||
return db.
|
||||
Preload("ProductWarehouse").
|
||||
Preload("ProductWarehouse.Product").
|
||||
Preload("ProductWarehouse.Warehouse").
|
||||
Preload("ProductWarehouse.Warehouse.Location").
|
||||
Preload("ProductWarehouse.ProjectFlockKandang").
|
||||
Preload("ProductWarehouse.ProjectFlockKandang.ProjectFlock").
|
||||
Preload("StockLog.CreatedUser")
|
||||
})
|
||||
adjustmentStock, err := s.AdjustmentStockRepository.GetByID(c.Context(), id, s.withRelations)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "Adjustment not found")
|
||||
@@ -103,250 +104,6 @@ func (s *adjustmentService) GetOne(c *fiber.Ctx, id uint) (*entity.AdjustmentSto
|
||||
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) {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
@@ -365,12 +122,12 @@ func (s *adjustmentService) Adjustment(c *fiber.Ctx, req *validation.Create) (*e
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Quantity must be greater than zero")
|
||||
}
|
||||
|
||||
functionCode := utils.NormalizeUpper(req.TransactionSubtype)
|
||||
functionCode := strings.ToUpper(strings.TrimSpace(req.TransactionSubtype))
|
||||
if functionCode == "" {
|
||||
functionCode = utils.NormalizeUpper(req.TransactionSubType)
|
||||
functionCode = strings.ToUpper(strings.TrimSpace(req.TransactionSubType))
|
||||
}
|
||||
if functionCode == "" {
|
||||
functionCode = utils.NormalizeUpper(req.FunctionCode)
|
||||
functionCode = strings.ToUpper(strings.TrimSpace(req.FunctionCode))
|
||||
}
|
||||
if functionCode == "" {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Transaction subtype is required")
|
||||
@@ -387,9 +144,9 @@ func (s *adjustmentService) Adjustment(c *fiber.Ctx, req *validation.Create) (*e
|
||||
return nil, err
|
||||
}
|
||||
|
||||
note := utils.NormalizeTrim(req.Notes)
|
||||
note := strings.TrimSpace(req.Notes)
|
||||
if note == "" {
|
||||
note = utils.NormalizeTrim(req.Note)
|
||||
note = strings.TrimSpace(req.Note)
|
||||
}
|
||||
grandTotal := math.Round((qty*req.Price)*1000) / 1000
|
||||
|
||||
@@ -471,11 +228,8 @@ func (s *adjustmentService) Adjustment(c *fiber.Ctx, req *validation.Create) (*e
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "FIFO v2 service is not available")
|
||||
}
|
||||
|
||||
sourcePW, err := adjustmentStockRepoTX.FindAyamSourceProductWarehouse(ctx, warehouseID, *projectFlockKandangID)
|
||||
sourcePW, err := s.resolveAyamSourceProductWarehouse(ctx, tx, warehouseID, *projectFlockKandangID)
|
||||
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
|
||||
}
|
||||
if err := common.EnsureProjectFlockNotClosedForProductWarehouses(
|
||||
@@ -531,14 +285,6 @@ func (s *adjustmentService) Adjustment(c *fiber.Ctx, req *validation.Create) (*e
|
||||
if err := adjustmentStockRepoTX.CreateOne(ctx, destinationAdjustment, nil); err != nil {
|
||||
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
|
||||
if _, err := s.FifoStockV2Svc.Reflow(ctx, common.FifoStockV2ReflowRequest{
|
||||
@@ -580,7 +326,7 @@ func (s *adjustmentService) Adjustment(c *fiber.Ctx, req *validation.Create) (*e
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := adjustmentStockRepoTX.ResyncProjectFlockPopulationUsage(ctx, *projectFlockKandangID); err != nil {
|
||||
if err := s.resyncProjectFlockPopulationUsage(ctx, tx, *projectFlockKandangID); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to resync project flock population usage")
|
||||
}
|
||||
}
|
||||
@@ -756,80 +502,29 @@ func (s *adjustmentService) resolveRouteByFunctionCode(
|
||||
}
|
||||
}
|
||||
|
||||
func (s *adjustmentService) resolveAdjustmentDependenciesAndPolicy(
|
||||
func (s *adjustmentService) resolveOverconsumePolicy(
|
||||
ctx context.Context,
|
||||
tx *gorm.DB,
|
||||
stockableType string,
|
||||
stockableIDs []uint,
|
||||
) ([]adjustmentStockRepo.AdjustmentDownstreamDependency, bool, error) {
|
||||
deps, err := s.AdjustmentStockRepository.WithTx(tx).LoadDownstreamDependencies(ctx, stockableType, stockableIDs)
|
||||
route *adjustmentStockRepo.AdjustmentRouteResolution,
|
||||
) (bool, error) {
|
||||
if route == nil {
|
||||
return false, fmt.Errorf("route is required")
|
||||
}
|
||||
|
||||
defaultValue := route.AllowPendingDefault
|
||||
selected, err := s.AdjustmentStockRepository.FindOverconsumeRule(
|
||||
ctx,
|
||||
route.Lane,
|
||||
route.FlagGroupCode,
|
||||
route.FunctionCode,
|
||||
)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to load downstream adjustment dependencies: %+v", err)
|
||||
return nil, false, fiber.NewError(fiber.StatusInternalServerError, "Failed to validate downstream adjustment dependencies")
|
||||
return false, err
|
||||
}
|
||||
if len(deps) == 0 {
|
||||
return nil, true, nil
|
||||
if selected == nil {
|
||||
return defaultValue, 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, ", ")
|
||||
return *selected, nil
|
||||
}
|
||||
|
||||
func (s *adjustmentService) getActiveProjectFlockKandangID(ctx context.Context, warehouseID uint) (uint, error) {
|
||||
@@ -858,6 +553,46 @@ func (s *adjustmentService) getActiveProjectFlockKandangID(ctx context.Context,
|
||||
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(
|
||||
ctx context.Context,
|
||||
stockLogRepo stockLogsRepo.StockLogRepository,
|
||||
@@ -941,6 +676,57 @@ 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) {
|
||||
if err := s.Validate.Struct(query); err != nil {
|
||||
return nil, 0, err
|
||||
@@ -953,24 +739,25 @@ func (s *adjustmentService) AdjustmentHistory(c *fiber.Ctx, query *validation.Qu
|
||||
}
|
||||
offset := (query.Page - 1) * query.Limit
|
||||
|
||||
var isProductsExist bool
|
||||
isWarehousesExist, err := s.WarehouseRepo.IdExists(c.Context(), uint(query.WarehouseID))
|
||||
|
||||
if err != nil {
|
||||
return nil, 0, fiber.NewError(fiber.StatusInternalServerError, "Failed to validate warehouse")
|
||||
}
|
||||
if query.WarehouseID > 0 && !isWarehousesExist {
|
||||
return nil, 0, fiber.NewError(fiber.StatusNotFound, "Warehouse not found")
|
||||
if query.WarehouseID > 0 {
|
||||
isWarehouseExist, err := s.WarehouseRepo.IdExists(c.Context(), query.WarehouseID)
|
||||
if err != nil {
|
||||
return nil, 0, fiber.NewError(fiber.StatusInternalServerError, "Failed to validate warehouse")
|
||||
}
|
||||
if !isWarehouseExist {
|
||||
return nil, 0, fiber.NewError(fiber.StatusNotFound, "Warehouse not found")
|
||||
}
|
||||
}
|
||||
|
||||
isProductsExist, err = s.ProductRepo.IdExists(c.Context(), uint(query.ProductID))
|
||||
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to check product existence: %+v", err)
|
||||
return nil, 0, fiber.NewError(fiber.StatusInternalServerError, "Failed to validate product")
|
||||
}
|
||||
if query.ProductID > 0 && !isProductsExist {
|
||||
return nil, 0, fiber.NewError(fiber.StatusNotFound, "Product not found")
|
||||
if query.ProductID > 0 {
|
||||
isProductExist, err := s.ProductRepo.IdExists(c.Context(), query.ProductID)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to check product existence: %+v", err)
|
||||
return nil, 0, fiber.NewError(fiber.StatusInternalServerError, "Failed to validate product")
|
||||
}
|
||||
if !isProductExist {
|
||||
return nil, 0, fiber.NewError(fiber.StatusNotFound, "Product not found")
|
||||
}
|
||||
}
|
||||
|
||||
scope, scopeErr := m.ResolveLocationScope(c, s.AdjustmentStockRepository.DB())
|
||||
@@ -983,11 +770,11 @@ func (s *adjustmentService) AdjustmentHistory(c *fiber.Ctx, query *validation.Qu
|
||||
}
|
||||
}
|
||||
|
||||
functionCode := utils.NormalizeUpper(query.TransactionSubtype)
|
||||
functionCode := strings.ToUpper(strings.TrimSpace(query.TransactionSubtype))
|
||||
if functionCode == "" {
|
||||
functionCode = utils.NormalizeUpper(query.FunctionCode)
|
||||
functionCode = strings.ToUpper(strings.TrimSpace(query.FunctionCode))
|
||||
}
|
||||
transactionType := utils.NormalizeUpper(query.TransactionType)
|
||||
transactionType := strings.ToUpper(strings.TrimSpace(query.TransactionType))
|
||||
|
||||
adjustmentStocks, total, err := s.AdjustmentStockRepository.FindHistory(
|
||||
c.Context(),
|
||||
|
||||
-1
@@ -32,7 +32,6 @@ func (u *ProductWarehouseController) GetAll(c *fiber.Ctx) error {
|
||||
Flags: c.Query("flags", ""),
|
||||
KandangId: uint(c.QueryInt("kandang_id", 0)),
|
||||
TransferContext: c.Query(utils.TransferContextKey, ""),
|
||||
StockMode: c.Query("stock_mode", ""),
|
||||
Type: c.Query("type", ""),
|
||||
}
|
||||
|
||||
|
||||
@@ -12,11 +12,10 @@ import (
|
||||
// === DTO Structs ===
|
||||
|
||||
type ProductWarehouseRelationDTO struct {
|
||||
Id uint `json:"id"`
|
||||
ProductId uint `json:"product_id"`
|
||||
WarehouseId uint `json:"warehouse_id"`
|
||||
Quantity float64 `json:"quantity"`
|
||||
TransferAvailableQty *float64 `json:"transfer_available_qty,omitempty"`
|
||||
Id uint `json:"id"`
|
||||
ProductId uint `json:"product_id"`
|
||||
WarehouseId uint `json:"warehouse_id"`
|
||||
Quantity float64 `json:"quantity"`
|
||||
}
|
||||
|
||||
type ProductWarehouseListDTO struct {
|
||||
@@ -62,11 +61,10 @@ type ProjectFlockRelationDTO struct {
|
||||
|
||||
func ToProductWarehouseRelationDTO(e entity.ProductWarehouse) ProductWarehouseRelationDTO {
|
||||
return ProductWarehouseRelationDTO{
|
||||
Id: e.Id,
|
||||
ProductId: e.ProductId, // Field yang benar dari entity
|
||||
WarehouseId: e.WarehouseId, // Field yang benar dari entity
|
||||
Quantity: e.Quantity,
|
||||
TransferAvailableQty: e.AvailableQty,
|
||||
Id: e.Id,
|
||||
ProductId: e.ProductId, // Field yang benar dari entity
|
||||
WarehouseId: e.WarehouseId, // Field yang benar dari entity
|
||||
Quantity: e.Quantity,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
@@ -28,8 +27,6 @@ type productWarehouseService struct {
|
||||
KandangRepo kandangrepo.KandangRepository
|
||||
}
|
||||
|
||||
const stockModeExcludeChickin = "exclude_chickin"
|
||||
|
||||
func NewProductWarehouseService(repo repository.ProductWarehouseRepository, validate *validator.Validate, kandangRepo kandangrepo.KandangRepository) ProductWarehouseService {
|
||||
return &productWarehouseService{
|
||||
Log: utils.Log,
|
||||
@@ -192,11 +189,6 @@ func (s productWarehouseService) GetAll(c *fiber.Ctx, params *validation.Query)
|
||||
s.Log.Errorf("Failed to get productWarehouses: %+v", err)
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
productWarehouses, err = s.applyTransferAvailableQty(c, params, productWarehouses)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return productWarehouses, total, nil
|
||||
}
|
||||
|
||||
@@ -237,80 +229,3 @@ func (s productWarehouseService) GetOne(c *fiber.Ctx, id uint) (*entity.ProductW
|
||||
}
|
||||
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,6 +20,5 @@ type Query struct {
|
||||
Flags string `query:"flags" validate:"omitempty"`
|
||||
KandangId uint `query:"kandang_id" validate:"omitempty,number,min=1"`
|
||||
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"`
|
||||
}
|
||||
|
||||
@@ -109,23 +109,3 @@ func (u *TransferController) CreateOne(c *fiber.Ctx) error {
|
||||
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,6 +18,5 @@ func TransferRoutes(v1 fiber.Router, u user.UserService, s transfer.TransferServ
|
||||
route.Get("/", m.RequirePermissions(m.P_TransferGetAll), ctrl.GetAll)
|
||||
route.Post("/", m.RequirePermissions(m.P_TransferCreateOne), ctrl.CreateOne)
|
||||
route.Get("/:id", m.RequirePermissions(m.P_TransferGetOne), ctrl.GetOne)
|
||||
route.Delete("/:id", m.RequirePermissions(m.P_TransferDeleteOne), ctrl.DeleteOne)
|
||||
|
||||
}
|
||||
|
||||
@@ -5,14 +5,13 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"mime/multipart"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/sirupsen/logrus"
|
||||
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"
|
||||
m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
||||
rProductWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/repositories"
|
||||
@@ -26,14 +25,12 @@ import (
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils/fifo"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
type TransferService interface {
|
||||
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.StockTransfer, int64, error)
|
||||
GetOne(ctx *fiber.Ctx, id uint) (*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 {
|
||||
@@ -54,15 +51,6 @@ type transferService struct {
|
||||
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 {
|
||||
return &transferService{
|
||||
Log: utils.Log,
|
||||
@@ -118,7 +106,6 @@ 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 {
|
||||
db = s.withRelations(db)
|
||||
db = db.Where("stock_transfers.deleted_at IS NULL")
|
||||
if scope.Restrict {
|
||||
if len(scope.IDs) == 0 {
|
||||
return db.Where("1 = 0")
|
||||
@@ -160,7 +147,6 @@ 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_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 nil, err
|
||||
@@ -171,7 +157,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 {
|
||||
return s.withRelations(db).Where("stock_transfers.deleted_at IS NULL")
|
||||
return s.withRelations(db)
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -527,6 +513,18 @@ 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))
|
||||
}
|
||||
|
||||
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{
|
||||
ProductWarehouseId: uint(*detail.SourceProductWarehouseID),
|
||||
CreatedBy: uint(actorID),
|
||||
@@ -635,208 +633,55 @@ func (s *transferService) CreateOne(c *fiber.Ctx, req *validation.TransferReques
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *transferService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||
if err := s.ensureTransferAccess(c.Context(), id, c); err != nil {
|
||||
return err
|
||||
}
|
||||
if s.FifoStockV2Svc == nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "FIFO v2 service is not available")
|
||||
}
|
||||
|
||||
actorID, err := m.ActorIDFromContext(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
var details []entity.StockTransferDetail
|
||||
if err := tx.WithContext(c.Context()).
|
||||
Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
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 releasedQty > 1e-6 {
|
||||
if err := s.appendStockLog(
|
||||
c.Context(),
|
||||
stockLogRepoTx,
|
||||
uint(*detail.SourceProductWarehouseID),
|
||||
actorID,
|
||||
releasedQty,
|
||||
0,
|
||||
uint(detail.Id),
|
||||
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...)
|
||||
func (s *transferService) allocatePopulationForStockTransferOut(
|
||||
ctx context.Context,
|
||||
tx *gorm.DB,
|
||||
detail *entity.StockTransferDetail,
|
||||
sourceProductWarehouseID uint,
|
||||
consumeQty float64,
|
||||
) error {
|
||||
if consumeQty <= 0 {
|
||||
return nil
|
||||
})
|
||||
}
|
||||
if tx == nil {
|
||||
return errors.New("transaction is required")
|
||||
}
|
||||
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)
|
||||
if err != nil {
|
||||
if fiberErr, ok := err.(*fiber.Error); ok {
|
||||
return fiberErr
|
||||
}
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Gagal menghapus transfer")
|
||||
return err
|
||||
}
|
||||
if pw.ProjectFlockKandangId == nil || *pw.ProjectFlockKandangId == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
populations, err := s.ProjectFlockPopulationRepo.WithTx(tx).GetByProjectFlockKandangIDAndProductWarehouseID(
|
||||
ctx,
|
||||
*pw.ProjectFlockKandangId,
|
||||
sourceProductWarehouseID,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(populations) == 0 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Populasi tidak ditemukan untuk transfer")
|
||||
}
|
||||
|
||||
return nil
|
||||
return fifoV2.AllocatePopulationConsumption(
|
||||
ctx,
|
||||
tx,
|
||||
populations,
|
||||
sourceProductWarehouseID,
|
||||
fifo.UsableKeyStockTransferOut.String(),
|
||||
uint(detail.Id),
|
||||
consumeQty,
|
||||
)
|
||||
}
|
||||
|
||||
func (s *transferService) resolveTransferFlagGroup(
|
||||
@@ -912,264 +757,3 @@ func (s *transferService) getActiveProjectFlockKandangID(ctx context.Context, wa
|
||||
|
||||
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,18 +127,30 @@ func (s deliveryOrdersService) GetAll(c *fiber.Ctx, params *validation.DeliveryO
|
||||
Preload("Products.DeliveryProduct")
|
||||
|
||||
if params.Status != "" {
|
||||
status := strings.TrimSpace(params.Status)
|
||||
latestApprovalSubQuery := s.MarketingRepo.DB().
|
||||
WithContext(c.Context()).
|
||||
Table("approvals").
|
||||
Select("DISTINCT ON (approvable_id) approvable_id, step_name").
|
||||
Select("DISTINCT ON (approvable_id) approvable_id, step_name, action").
|
||||
Where("approvable_type = ?", utils.ApprovalWorkflowMarketing.String()).
|
||||
Order("approvable_id, id DESC")
|
||||
db = db.Where(`EXISTS (
|
||||
SELECT 1
|
||||
FROM (?) AS latest_approval
|
||||
WHERE latest_approval.approvable_id = marketings.id
|
||||
AND LOWER(latest_approval.step_name) = LOWER(?)
|
||||
)`, latestApprovalSubQuery, params.Status)
|
||||
|
||||
if strings.EqualFold(status, "DITOLAK") {
|
||||
db = db.Where(`EXISTS (
|
||||
SELECT 1
|
||||
FROM (?) AS latest_approval
|
||||
WHERE latest_approval.approvable_id = marketings.id
|
||||
AND latest_approval.action = ?
|
||||
)`, latestApprovalSubQuery, string(entity.ApprovalActionRejected))
|
||||
} else {
|
||||
db = db.Where(`EXISTS (
|
||||
SELECT 1
|
||||
FROM (?) AS latest_approval
|
||||
WHERE latest_approval.approvable_id = marketings.id
|
||||
AND LOWER(latest_approval.step_name) = LOWER(?)
|
||||
AND (latest_approval.action IS NULL OR latest_approval.action <> ?)
|
||||
)`, latestApprovalSubQuery, status, string(entity.ApprovalActionRejected))
|
||||
}
|
||||
}
|
||||
|
||||
if params.Search != "" {
|
||||
@@ -213,7 +225,6 @@ func (s deliveryOrdersService) GetAll(c *fiber.Ctx, params *validation.DeliveryO
|
||||
}
|
||||
return db.Order("created_at DESC").Order("updated_at DESC")
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
@@ -258,7 +269,6 @@ func (s deliveryOrdersService) GetOne(c *fiber.Ctx, id uint) (*dto.MarketingDeta
|
||||
return db.Preload("ActionUser")
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
} else if len(approvals) > 0 {
|
||||
if marketing.LatestApproval == nil {
|
||||
latest := approvals[len(approvals)-1]
|
||||
@@ -310,7 +320,6 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Delivery
|
||||
}
|
||||
|
||||
err = s.MarketingRepo.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
||||
|
||||
marketingProductRepositoryTx := marketingRepo.NewMarketingProductRepository(dbTransaction)
|
||||
marketingDeliveryProductRepositoryTx := marketingRepo.NewMarketingDeliveryProductRepository(dbTransaction)
|
||||
approvalSvcTx := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(dbTransaction))
|
||||
@@ -377,7 +386,6 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Delivery
|
||||
deliveryProduct.VehicleNumber = requestedProduct.VehicleNumber
|
||||
|
||||
if requestedProduct.Qty > 0 {
|
||||
|
||||
if err := s.consumeDeliveryStock(c.Context(), dbTransaction, deliveryProduct, foundMarketingProduct, requestedProduct.Qty, actorID); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -404,7 +412,6 @@ func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Delivery
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if fiberErr, ok := err.(*fiber.Error); ok {
|
||||
return nil, fiberErr
|
||||
@@ -436,7 +443,6 @@ func (s deliveryOrdersService) UpdateOne(c *fiber.Ctx, req *validation.DeliveryO
|
||||
}
|
||||
|
||||
err = s.MarketingRepo.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
||||
|
||||
marketingProductRepositoryTx := marketingRepo.NewMarketingProductRepository(dbTransaction)
|
||||
marketingDeliveryProductRepositoryTx := marketingRepo.NewMarketingDeliveryProductRepository(dbTransaction)
|
||||
marketingRepoTx := marketingRepo.NewMarketingRepository(dbTransaction)
|
||||
@@ -525,7 +531,6 @@ func (s deliveryOrdersService) UpdateOne(c *fiber.Ctx, req *validation.DeliveryO
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if fiberErr, ok := err.(*fiber.Error); ok {
|
||||
return nil, fiberErr
|
||||
@@ -558,6 +563,12 @@ func (s deliveryOrdersService) consumeDeliveryStock(ctx context.Context, tx *gor
|
||||
if deliveryProduct == nil || deliveryProduct.Id == 0 {
|
||||
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)
|
||||
previousUsage := deliveryProduct.UsageQty
|
||||
@@ -595,12 +606,12 @@ func (s deliveryOrdersService) consumeDeliveryStock(ctx context.Context, tx *gor
|
||||
Decrease: allocatedDelta,
|
||||
LoggableType: string(utils.StockLogTypeMarketing),
|
||||
LoggableId: deliveryProduct.Id,
|
||||
ProductWarehouseId: marketingProduct.ProductWarehouseId,
|
||||
ProductWarehouseId: deliveryProduct.ProductWarehouseId,
|
||||
CreatedBy: actorID,
|
||||
Notes: fmt.Sprintf("FIFO v2 reflow consume (%.2f)", allocatedDelta),
|
||||
}
|
||||
|
||||
stockLogs, err := s.StockLogRepo.GetByProductWarehouse(ctx, marketingProduct.ProductWarehouseId, 1)
|
||||
stockLogs, err := s.StockLogRepo.GetByProductWarehouse(ctx, deliveryProduct.ProductWarehouseId, 1)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to get stock logs")
|
||||
}
|
||||
|
||||
@@ -151,6 +151,31 @@ 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)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid date format")
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"time"
|
||||
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
kandangDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/dto"
|
||||
kandangGroupDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandang-groups/dto"
|
||||
)
|
||||
|
||||
// === DTO Structs ===
|
||||
@@ -15,12 +15,12 @@ type EmployeesRelationDTO struct {
|
||||
}
|
||||
|
||||
type EmployeesListDTO struct {
|
||||
Id uint `json:"id"`
|
||||
Name string `json:"name"`
|
||||
IsActive bool `json:"is_active"`
|
||||
Kandangs []kandangDTO.KandangRelationDTO `json:"kandangs"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Id uint `json:"id"`
|
||||
Name string `json:"name"`
|
||||
IsActive bool `json:"is_active"`
|
||||
Kandangs []kandangGroupDTO.KandangGroupRelationDTO `json:"kandangs"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
type EmployeesDetailDTO struct {
|
||||
@@ -37,12 +37,12 @@ func ToEmployeesRelationDTO(e entity.Employees) EmployeesRelationDTO {
|
||||
}
|
||||
|
||||
func ToEmployeesListDTO(e entity.Employees) EmployeesListDTO {
|
||||
kandangs := make([]kandangDTO.KandangRelationDTO, 0, len(e.EmployeeKandangs))
|
||||
kandangs := make([]kandangGroupDTO.KandangGroupRelationDTO, 0, len(e.EmployeeKandangs))
|
||||
for _, rel := range e.EmployeeKandangs {
|
||||
if rel.Kandang.Id == 0 {
|
||||
continue
|
||||
}
|
||||
kandangs = append(kandangs, kandangDTO.ToKandangRelationDTO(rel.Kandang))
|
||||
kandangs = append(kandangs, kandangGroupDTO.ToKandangGroupRelationDTO(rel.Kandang))
|
||||
}
|
||||
|
||||
return EmployeesListDTO{
|
||||
|
||||
@@ -52,7 +52,7 @@ func (s employeesService) ensureEmployeeAccess(c *fiber.Ctx, employeeID uint) er
|
||||
db := s.Repository.DB().WithContext(c.Context()).
|
||||
Table("employees e").
|
||||
Joins("JOIN employee_kandangs ek ON ek.employee_id = e.id").
|
||||
Joins("JOIN kandangs k ON k.id = ek.kandang_id").
|
||||
Joins("JOIN kandang_groups k ON k.id = ek.kandang_id").
|
||||
Joins("JOIN locations loc ON loc.id = k.location_id").
|
||||
Joins("JOIN areas a ON a.id = loc.area_id").
|
||||
Where("e.id = ?", employeeID).
|
||||
@@ -79,7 +79,7 @@ func (s employeesService) ensureKandangIDsAccess(c *fiber.Ctx, kandangIDs []uint
|
||||
}
|
||||
|
||||
db := s.Repository.DB().WithContext(c.Context()).
|
||||
Table("kandangs k").
|
||||
Table("kandang_groups k").
|
||||
Joins("JOIN locations loc ON loc.id = k.location_id").
|
||||
Joins("JOIN areas a ON a.id = loc.area_id").
|
||||
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 {
|
||||
db = s.withRelations(db)
|
||||
db = db.Joins("JOIN employee_kandangs ek ON ek.employee_id = employees.id").
|
||||
Joins("JOIN kandangs k ON k.id = ek.kandang_id").
|
||||
Joins("JOIN kandang_groups k ON k.id = ek.kandang_id").
|
||||
Joins("JOIN locations loc ON loc.id = k.location_id").
|
||||
Joins("JOIN areas a ON a.id = loc.area_id")
|
||||
var scopeErr error
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
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",
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
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),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
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)
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
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)
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
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)
|
||||
}
|
||||
@@ -0,0 +1,242 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
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"`
|
||||
}
|
||||
@@ -11,24 +11,32 @@ import (
|
||||
// === DTO Structs ===
|
||||
|
||||
type KandangRelationDTO struct {
|
||||
Id uint `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Status string `json:"status"`
|
||||
Capacity float64 `json:"capacity"`
|
||||
Location *locationDTO.LocationRelationDTO `json:"location,omitempty"`
|
||||
Pic *userDTO.UserRelationDTO `json:"pic,omitempty"`
|
||||
Id uint `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Status string `json:"status"`
|
||||
Capacity float64 `json:"capacity"`
|
||||
KandangGroup *KandangGroupRelationDTO `json:"kandang_group,omitempty"`
|
||||
Location *locationDTO.LocationRelationDTO `json:"location,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 {
|
||||
Id uint `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Status string `json:"status"`
|
||||
Capacity float64 `json:"capacity"`
|
||||
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"`
|
||||
Id uint `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Status string `json:"status"`
|
||||
Capacity float64 `json:"capacity"`
|
||||
KandangGroup KandangGroupRelationDTO `json:"kandang_group"`
|
||||
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"`
|
||||
}
|
||||
|
||||
type KandangDetailDTO struct {
|
||||
@@ -38,6 +46,12 @@ type KandangDetailDTO struct {
|
||||
// === Mapper Functions ===
|
||||
|
||||
func ToKandangRelationDTO(e entity.Kandang) KandangRelationDTO {
|
||||
var kandangGroup *KandangGroupRelationDTO
|
||||
if e.KandangGroup.Id != 0 {
|
||||
mapped := ToKandangGroupRelationDTO(e.KandangGroup)
|
||||
kandangGroup = &mapped
|
||||
}
|
||||
|
||||
var location *locationDTO.LocationRelationDTO
|
||||
if e.Location.Id != 0 {
|
||||
mapped := locationDTO.ToLocationRelationDTO(e.Location)
|
||||
@@ -51,16 +65,31 @@ func ToKandangRelationDTO(e entity.Kandang) KandangRelationDTO {
|
||||
}
|
||||
|
||||
return KandangRelationDTO{
|
||||
Id: e.Id,
|
||||
Name: e.Name,
|
||||
Status: e.Status,
|
||||
Capacity: e.Capacity,
|
||||
Location: location,
|
||||
Pic: pic,
|
||||
Id: e.Id,
|
||||
Name: e.Name,
|
||||
Status: e.Status,
|
||||
Capacity: e.Capacity,
|
||||
KandangGroup: kandangGroup,
|
||||
Location: location,
|
||||
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 {
|
||||
var kandangGroup KandangGroupRelationDTO
|
||||
if e.KandangGroup.Id != 0 {
|
||||
mapped := ToKandangGroupRelationDTO(e.KandangGroup)
|
||||
kandangGroup = mapped
|
||||
}
|
||||
|
||||
var location locationDTO.LocationRelationDTO
|
||||
if e.Location.Id != 0 {
|
||||
mapped := locationDTO.ToLocationRelationDTO(e.Location)
|
||||
@@ -80,15 +109,16 @@ func ToKandangListDTO(e entity.Kandang) KandangListDTO {
|
||||
}
|
||||
|
||||
return KandangListDTO{
|
||||
Id: e.Id,
|
||||
Name: e.Name,
|
||||
Status: e.Status,
|
||||
Location: location,
|
||||
Capacity: e.Capacity,
|
||||
Pic: pic,
|
||||
CreatedAt: e.CreatedAt,
|
||||
UpdatedAt: e.UpdatedAt,
|
||||
CreatedUser: createdUser,
|
||||
Id: e.Id,
|
||||
Name: e.Name,
|
||||
Status: e.Status,
|
||||
Location: location,
|
||||
KandangGroup: kandangGroup,
|
||||
Capacity: e.Capacity,
|
||||
Pic: pic,
|
||||
CreatedAt: e.CreatedAt,
|
||||
UpdatedAt: e.UpdatedAt,
|
||||
CreatedUser: createdUser,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,10 +4,10 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -15,6 +15,7 @@ type KandangRepository interface {
|
||||
repository.BaseRepository[entity.Kandang]
|
||||
LocationExists(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)
|
||||
ProjectFlockExists(ctx context.Context, projectFlockID uint) (bool, error)
|
||||
GetFirstByProjectFlockID(ctx context.Context, projectFlockID uint) (*entity.Kandang, error)
|
||||
@@ -45,6 +46,10 @@ func (r *KandangRepositoryImpl) PicExists(ctx context.Context, picId uint) (bool
|
||||
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) {
|
||||
return repository.ExistsByName[entity.Kandang](ctx, r.db, name, excludeID)
|
||||
}
|
||||
|
||||
@@ -3,13 +3,14 @@ package service
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
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/kandangs/repositories"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/validations"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
"strings"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
@@ -40,7 +41,7 @@ func NewKandangService(repo repository.KandangRepository, validate *validator.Va
|
||||
}
|
||||
|
||||
func (s kandangService) withRelations(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("CreatedUser").Preload("Location").Preload("Pic").Preload("ProjectFlockKandangs.ProjectFlock")
|
||||
return db.Preload("CreatedUser").Preload("Location").Preload("KandangGroup").Preload("Pic").Preload("ProjectFlockKandangs.ProjectFlock")
|
||||
|
||||
}
|
||||
|
||||
@@ -99,6 +100,28 @@ func (s kandangService) GetOne(c *fiber.Ctx, id uint) (*entity.Kandang, error) {
|
||||
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) {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
@@ -116,10 +139,14 @@ func (s *kandangService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
|
||||
|
||||
if err := common.EnsureRelations(c.Context(),
|
||||
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},
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.ensureKandangGroupAccess(c, req.GroupId, &req.LocationId); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
status := strings.ToUpper(strings.TrimSpace(req.Status))
|
||||
if status == "" {
|
||||
@@ -154,12 +181,13 @@ func (s *kandangService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
|
||||
}
|
||||
|
||||
createBody := &entity.Kandang{
|
||||
Name: req.Name,
|
||||
LocationId: req.LocationId,
|
||||
Capacity: req.Capacity,
|
||||
Status: status,
|
||||
PicId: req.PicId,
|
||||
CreatedBy: actorID,
|
||||
Name: req.Name,
|
||||
LocationId: req.LocationId,
|
||||
KandangGroupId: req.GroupId,
|
||||
Capacity: req.Capacity,
|
||||
Status: status,
|
||||
PicId: req.PicId,
|
||||
CreatedBy: actorID,
|
||||
}
|
||||
|
||||
if err := s.Repository.CreateOne(c.Context(), createBody, nil); err != nil {
|
||||
@@ -212,6 +240,7 @@ func (s kandangService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
||||
|
||||
if err := common.EnsureRelations(c.Context(),
|
||||
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},
|
||||
); err != nil {
|
||||
return nil, err
|
||||
@@ -220,6 +249,16 @@ func (s kandangService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
||||
if req.LocationId != nil {
|
||||
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 {
|
||||
updateBody["pic_id"] = *req.PicId
|
||||
|
||||
@@ -5,6 +5,7 @@ type Create struct {
|
||||
Status string `json:"status,omitempty" validate:"omitempty,min=3,max=50"`
|
||||
Capacity float64 `json:"capacity" validate:"required_strict,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"`
|
||||
ProjectFlockId *uint `json:"project_flock_id" validate:"omitempty,number,gt=0"`
|
||||
}
|
||||
@@ -14,6 +15,7 @@ type Update struct {
|
||||
Status *string `json:"status,omitempty" validate:"omitempty,min=3,max=50"`
|
||||
Capacity *float64 `json:"capacity" validate:"omitempty,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"`
|
||||
ProjectFlockId *uint `json:"project_flock_id,omitempty" validate:"omitempty,number,gt=0"`
|
||||
}
|
||||
|
||||
@@ -24,10 +24,11 @@ func NewLocationController(locationService service.LocationService) *LocationCon
|
||||
|
||||
func (u *LocationController) GetAll(c *fiber.Ctx) error {
|
||||
query := &validation.Query{
|
||||
Page: c.QueryInt("page", 1),
|
||||
Limit: c.QueryInt("limit", 10),
|
||||
Search: c.Query("search", ""),
|
||||
AreaId: c.QueryInt("area_id", 0),
|
||||
Page: c.QueryInt("page", 1),
|
||||
Limit: c.QueryInt("limit", 10),
|
||||
Search: c.Query("search", ""),
|
||||
AreaId: c.QueryInt("area_id", 0),
|
||||
HasLaying: c.QueryBool("has_laying", false),
|
||||
}
|
||||
|
||||
if query.Page < 1 || query.Limit < 1 {
|
||||
|
||||
@@ -60,6 +60,17 @@ func (s locationService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entit
|
||||
if params.AreaId != 0 {
|
||||
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")
|
||||
})
|
||||
|
||||
|
||||
@@ -13,8 +13,9 @@ type Update struct {
|
||||
}
|
||||
|
||||
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"`
|
||||
AreaId int `query:"area_id" validate:"omitempty,number,gt=0"`
|
||||
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"`
|
||||
AreaId int `query:"area_id" validate:"omitempty,number,gt=0"`
|
||||
HasLaying bool `query:"has_laying"`
|
||||
}
|
||||
|
||||
+8
-17
@@ -6,7 +6,6 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/config"
|
||||
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/production-standards/repositories"
|
||||
@@ -344,22 +343,17 @@ func (s productionStandardService) EnsureWeekStart(ctx context.Context, standard
|
||||
return nil
|
||||
}
|
||||
|
||||
layingWeekStart := config.LayingWeekStart()
|
||||
|
||||
switch strings.ToUpper(category) {
|
||||
case string(utils.ProjectFlockCategoryLaying):
|
||||
details, err := s.ProductionStandardDetailRepo.GetByProductionStandardID(ctx, standardID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(details) == 0 {
|
||||
return fiber.NewError(
|
||||
fiber.StatusBadRequest,
|
||||
"Standart production tidak tersedia untuk kategori laying",
|
||||
)
|
||||
startWeek := 0
|
||||
if len(details) > 0 {
|
||||
startWeek = details[0].Week
|
||||
}
|
||||
startWeek := details[0].Week
|
||||
if startWeek > layingWeekStart {
|
||||
if startWeek != 18 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Week tidak sesuai dengan standart kategori project flock")
|
||||
}
|
||||
case string(utils.ProjectFlockCategoryGrowing):
|
||||
@@ -367,13 +361,10 @@ func (s productionStandardService) EnsureWeekStart(ctx context.Context, standard
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(details) == 0 {
|
||||
return fiber.NewError(
|
||||
fiber.StatusBadRequest,
|
||||
"Standart production tidak tersedia untuk kategori growing",
|
||||
)
|
||||
startWeek := 0
|
||||
if len(details) > 0 {
|
||||
startWeek = details[0].Week
|
||||
}
|
||||
startWeek := details[0].Week
|
||||
if startWeek != 1 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Week tidak sesuai dengan standart kategori project flock")
|
||||
}
|
||||
@@ -390,7 +381,7 @@ func (s productionStandardService) EnsureWeekAvailable(ctx context.Context, stan
|
||||
upperCategory := strings.ToUpper(category)
|
||||
weekBase := 1
|
||||
if upperCategory == string(utils.ProjectFlockCategoryLaying) {
|
||||
weekBase = config.LayingWeekStart()
|
||||
weekBase = 18
|
||||
}
|
||||
week := ((day - 1) / 7) + weekBase
|
||||
if week <= 0 {
|
||||
|
||||
@@ -38,6 +38,14 @@ func (u *ProductController) GetAll(c *fiber.Ctx) error {
|
||||
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 {
|
||||
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 {
|
||||
db = s.withRelations(db)
|
||||
// Depletion master products are system products and often stored with is_visible = false.
|
||||
// When requested explicitly via is_depletion=true, include hidden records.
|
||||
if params.IsDepletion == nil || !*params.IsDepletion {
|
||||
// Default: show only visible products.
|
||||
// include_all=true can be used to fetch all records (including hidden/system products).
|
||||
if params.IncludeAll == nil || !*params.IncludeAll {
|
||||
db = db.Where("is_visible = ?", true)
|
||||
}
|
||||
if params.Search != "" {
|
||||
|
||||
@@ -45,4 +45,5 @@ type Query struct {
|
||||
Search string `query:"search" validate:"omitempty,max=50"`
|
||||
ProductCategoryID int `query:"product_category_id" validate:"omitempty,number,min=1"`
|
||||
IsDepletion *bool `query:"is_depletion" validate:"omitempty"`
|
||||
IncludeAll *bool `query:"include_all" validate:"omitempty"`
|
||||
}
|
||||
|
||||
@@ -9,10 +9,12 @@ import (
|
||||
|
||||
areas "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas"
|
||||
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"
|
||||
employeess "gitlab.com/mbugroup/lti-api.git/internal/modules/master/employees"
|
||||
fcrs "gitlab.com/mbugroup/lti-api.git/internal/modules/master/fcrs"
|
||||
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"
|
||||
locations "gitlab.com/mbugroup/lti-api.git/internal/modules/master/locations"
|
||||
nonstocks "gitlab.com/mbugroup/lti-api.git/internal/modules/master/nonstocks"
|
||||
@@ -24,7 +26,6 @@ import (
|
||||
suppliers "gitlab.com/mbugroup/lti-api.git/internal/modules/master/suppliers"
|
||||
uoms "gitlab.com/mbugroup/lti-api.git/internal/modules/master/uoms"
|
||||
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
|
||||
)
|
||||
|
||||
@@ -35,6 +36,7 @@ func RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Valida
|
||||
uoms.UomModule{},
|
||||
areas.AreaModule{},
|
||||
locations.LocationModule{},
|
||||
kandanggroups.KandangGroupModule{},
|
||||
kandangs.KandangModule{},
|
||||
warehouses.WarehouseModule{},
|
||||
customers.CustomerModule{},
|
||||
|
||||
@@ -151,25 +151,25 @@ func (u *ChickinController) GetOne(c *fiber.Ctx) error {
|
||||
// })
|
||||
// }
|
||||
|
||||
func (u *ChickinController) DeleteOne(c *fiber.Ctx) error {
|
||||
param := c.Params("id")
|
||||
// func (u *ChickinController) DeleteOne(c *fiber.Ctx) error {
|
||||
// param := c.Params("id")
|
||||
|
||||
id, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||
}
|
||||
// id, err := strconv.Atoi(param)
|
||||
// if err != nil {
|
||||
// return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||
// }
|
||||
|
||||
if err := u.ChickinService.DeleteOne(c, uint(id)); err != nil {
|
||||
return err
|
||||
}
|
||||
// if err := u.ChickinService.DeleteOne(c, uint(id)); err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Common{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Delete chickin successfully",
|
||||
})
|
||||
}
|
||||
// return c.Status(fiber.StatusOK).
|
||||
// JSON(response.Common{
|
||||
// Code: fiber.StatusOK,
|
||||
// Status: "success",
|
||||
// Message: "Delete chickin successfully",
|
||||
// })
|
||||
// }
|
||||
|
||||
func (u *ChickinController) Approval(c *fiber.Ctx) error {
|
||||
req := new(validation.Approve)
|
||||
|
||||
@@ -3,7 +3,6 @@ package dto
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/config"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
areaRelationDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas/dto"
|
||||
flockRelationDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/flocks/dto"
|
||||
@@ -36,13 +35,13 @@ type ChickinRelationDTO struct {
|
||||
}
|
||||
|
||||
type ProjectFlockDTO struct {
|
||||
Id uint `json:"id"`
|
||||
Period int `json:"period"`
|
||||
Category string `json:"category"`
|
||||
Flock *flockRelationDTO.FlockRelationDTO `json:"flock"`
|
||||
Area *areaRelationDTO.AreaRelationDTO `json:"area"`
|
||||
StandardFcr *float64 `json:"standard_fcr"`
|
||||
Location *locationRelationDTO.LocationRelationDTO `json:"location"`
|
||||
Id uint `json:"id"`
|
||||
Period int `json:"period"`
|
||||
Category string `json:"category"`
|
||||
Flock *flockRelationDTO.FlockRelationDTO `json:"flock"`
|
||||
Area *areaRelationDTO.AreaRelationDTO `json:"area"`
|
||||
StandardFcr *float64 `json:"standard_fcr"`
|
||||
Location *locationRelationDTO.LocationRelationDTO `json:"location"`
|
||||
}
|
||||
|
||||
type ProjectFlockKandangDTO struct {
|
||||
@@ -124,13 +123,13 @@ func ToProjectFlockDTO(pfk entity.ProjectFlockKandang) ProjectFlockDTO {
|
||||
location = &mapped
|
||||
}
|
||||
return ProjectFlockDTO{
|
||||
Id: e.Id,
|
||||
Period: pfk.Period,
|
||||
Category: e.Category,
|
||||
Flock: flock,
|
||||
Area: area,
|
||||
Id: e.Id,
|
||||
Period: pfk.Period,
|
||||
Category: e.Category,
|
||||
Flock: flock,
|
||||
Area: area,
|
||||
StandardFcr: resolveProjectFlockStandardFcr(e),
|
||||
Location: location,
|
||||
Location: location,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -220,7 +219,7 @@ func resolveProjectFlockStandardFcr(e entity.ProjectFlock) *float64 {
|
||||
}
|
||||
week := 1
|
||||
if e.Category == string(utils.ProjectFlockCategoryLaying) {
|
||||
week = config.LayingWeekStart()
|
||||
week = 18
|
||||
}
|
||||
for _, detail := range e.ProductionStandard.ProductionStandardDetails {
|
||||
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.Get("/:id",m.RequirePermissions(m.P_ChickinsGetOne), ctrl.GetOne)
|
||||
// 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)
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -7,14 +7,14 @@ import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"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"
|
||||
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||
rExpense "gitlab.com/mbugroup/lti-api.git/internal/modules/expenses/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"
|
||||
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"
|
||||
|
||||
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
|
||||
@@ -33,14 +33,13 @@ func (ProjectFlockKandangModule) RegisterRoutes(router fiber.Router, db *gorm.DB
|
||||
|
||||
approvalRepo := commonRepo.NewApprovalRepository(db)
|
||||
approvalService := commonSvc.NewApprovalService(approvalRepo)
|
||||
fifoStockV2Service := commonSvc.NewFifoStockV2Service(db, utils.Log)
|
||||
// register workflow steps for chickin approvals
|
||||
if err := approvalService.RegisterWorkflowSteps(utils.ApprovalWorkflowProjectFlockKandang, utils.ProjectFlockKandangApprovalSteps); err != nil {
|
||||
panic(fmt.Sprintf("failed to register chickin approval workflow: %v", err))
|
||||
}
|
||||
|
||||
expenseRepo := rExpense.NewExpenseRepository(db)
|
||||
projectFlockKandangService := sProjectFlockKandang.NewProjectFlockKandangService(projectFlockKandangRepo, approvalService, fifoStockV2Service, expenseRepo, warehouseRepo, productWarehouseRepo, projectFlockPopulationRepo, kandangRepo, validate)
|
||||
projectFlockKandangService := sProjectFlockKandang.NewProjectFlockKandangService(projectFlockKandangRepo, approvalService, expenseRepo, warehouseRepo, productWarehouseRepo, projectFlockPopulationRepo,kandangRepo, validate)
|
||||
userService := sUser.NewUserService(userRepo, validate)
|
||||
|
||||
ProjectFlockKandangRoutes(router, userService, projectFlockKandangService)
|
||||
|
||||
+43
-108
@@ -1,10 +1,8 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -37,7 +35,6 @@ type projectFlockKandangService struct {
|
||||
Validate *validator.Validate
|
||||
Repository repository.ProjectFlockKandangRepository
|
||||
ApprovalSvc commonSvc.ApprovalService
|
||||
FifoStockV2Svc commonSvc.FifoStockV2Service
|
||||
ExpenseRepo expenseRepo.ExpenseRepository
|
||||
WarehouseRepo rWarehouse.WarehouseRepository
|
||||
ProductWarehouseRepo rProductWarehouse.ProductWarehouseRepository
|
||||
@@ -72,13 +69,12 @@ type ExpenseSummary struct {
|
||||
Reference string `json:"reference_number"`
|
||||
}
|
||||
|
||||
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 {
|
||||
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 {
|
||||
return &projectFlockKandangService{
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
Repository: repo,
|
||||
ApprovalSvc: approvalSvc,
|
||||
FifoStockV2Svc: fifoStockV2Svc,
|
||||
ExpenseRepo: expenseRepo,
|
||||
WarehouseRepo: warehouseRepo,
|
||||
ProductWarehouseRepo: productWarehouseRepo,
|
||||
@@ -312,25 +308,23 @@ func (s projectFlockKandangService) CheckClosing(c *fiber.Ctx, id uint) (*Closin
|
||||
}
|
||||
|
||||
for _, pw := range productWarehouses {
|
||||
if pw.Quantity > 0 {
|
||||
category := ""
|
||||
if pw.Product.ProductCategory.Id != 0 {
|
||||
category = pw.Product.ProductCategory.Name
|
||||
}
|
||||
uomName := ""
|
||||
if pw.Product.Uom.Id != 0 {
|
||||
uomName = pw.Product.Uom.Name
|
||||
}
|
||||
stockRemain = append(stockRemain, StockRemainingDetail{
|
||||
FlagName: string(flagName),
|
||||
ProductWarehouseId: pw.Id,
|
||||
ProductId: pw.ProductId,
|
||||
ProductName: pw.Product.Name,
|
||||
ProductCategory: category,
|
||||
Uom: uomName,
|
||||
Quantity: pw.Quantity,
|
||||
})
|
||||
category := ""
|
||||
if pw.Product.ProductCategory.Id != 0 {
|
||||
category = pw.Product.ProductCategory.Name
|
||||
}
|
||||
uomName := ""
|
||||
if pw.Product.Uom.Id != 0 {
|
||||
uomName = pw.Product.Uom.Name
|
||||
}
|
||||
stockRemain = append(stockRemain, StockRemainingDetail{
|
||||
FlagName: string(flagName),
|
||||
ProductWarehouseId: pw.Id,
|
||||
ProductId: pw.ProductId,
|
||||
ProductName: pw.Product.Name,
|
||||
ProductCategory: category,
|
||||
Uom: uomName,
|
||||
Quantity: pw.Quantity,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -589,7 +583,7 @@ func (s projectFlockKandangService) Closing(c *fiber.Ctx, id uint, req *validati
|
||||
}
|
||||
}
|
||||
if s.ApprovalSvc != nil {
|
||||
reopenAction := entity.ApprovalActionUpdated
|
||||
reopenAction := entity.ApprovalActionApproved
|
||||
// Hindari duplikasi jika approval terakhir sudah Disetujui + Updated
|
||||
latestPFK, lerr := s.ApprovalSvc.LatestByTarget(c.Context(), utils.ApprovalWorkflowProjectFlockKandang, id, nil)
|
||||
if lerr != nil {
|
||||
@@ -615,6 +609,31 @@ func (s projectFlockKandangService) Closing(c *fiber.Ctx, id uint, req *validati
|
||||
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:
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "action harus close atau unclose")
|
||||
@@ -675,91 +694,7 @@ func (s projectFlockKandangService) calculateAvailableQuantityForProductWarehous
|
||||
if 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
|
||||
}
|
||||
|
||||
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,7 +6,6 @@ import (
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
warehouseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/dto"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/dto"
|
||||
@@ -63,7 +62,6 @@ func (u *ProjectflockController) GetAll(c *fiber.Ctx) error {
|
||||
Search: c.Query("search", ""),
|
||||
SortBy: c.Query("sort_by", ""),
|
||||
SortOrder: c.Query("sort_order", ""),
|
||||
Status: strings.TrimSpace(c.Query("status", "")),
|
||||
}
|
||||
|
||||
if area := c.QueryInt("area_id", 0); area > 0 {
|
||||
@@ -274,20 +272,10 @@ func (u *ProjectflockController) LookupProjectFlockKandang(c *fiber.Ctx) error {
|
||||
projectFlockId := c.QueryInt("project_flock_id", 0)
|
||||
kandangId := c.QueryInt("kandang_id", 0)
|
||||
withPopulation := c.QueryBool("withpopulation", false)
|
||||
recordDateRaw := strings.TrimSpace(c.Query("record_date", ""))
|
||||
var recordDate *time.Time
|
||||
|
||||
if projectFlockId == 0 || kandangId == 0 {
|
||||
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))
|
||||
if err != nil {
|
||||
@@ -312,12 +300,6 @@ func (u *ProjectflockController) LookupProjectFlockKandang(c *fiber.Ctx) error {
|
||||
mapped := warehouseDTO.ToWarehouseRelationDTO(*warehouse)
|
||||
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 {
|
||||
population := dtoResult.AvailableQuantity
|
||||
dtoResult.Population = &population
|
||||
|
||||
@@ -3,7 +3,6 @@ package dto
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/config"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
approvalDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/approvals/dto"
|
||||
areaDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas/dto"
|
||||
@@ -26,23 +25,24 @@ type ProjectFlockRelationDTO struct {
|
||||
|
||||
type ProjectFlockListDTO struct {
|
||||
ProjectFlockRelationDTO
|
||||
Area *areaDTO.AreaRelationDTO `json:"area,omitempty"`
|
||||
Category string `json:"category"`
|
||||
StandardFcr *float64 `json:"standard_fcr,omitempty"`
|
||||
Area *areaDTO.AreaRelationDTO `json:"area,omitempty"`
|
||||
Category string `json:"category"`
|
||||
StandardFcr *float64 `json:"standard_fcr,omitempty"`
|
||||
ProductionStandard *productionStandardDTO.ProductionStandardRelationDTO `json:"production_standard,omitempty"`
|
||||
Location *locationDTO.LocationRelationDTO `json:"location,omitempty"`
|
||||
Kandangs []KandangWithProjectFlockIdDTO `json:"kandangs,omitempty"`
|
||||
ProjectBudgets []ProjectBudgetDTO `json:"project_budgets,omitempty"`
|
||||
CreatedUser *userDTO.UserRelationDTO `json:"created_user"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Approval approvalDTO.ApprovalRelationDTO `json:"approval"`
|
||||
Location *locationDTO.LocationRelationDTO `json:"location,omitempty"`
|
||||
Kandangs []KandangWithProjectFlockIdDTO `json:"kandangs,omitempty"`
|
||||
ProjectBudgets []ProjectBudgetDTO `json:"project_budgets,omitempty"`
|
||||
CreatedUser *userDTO.UserRelationDTO `json:"created_user"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Approval approvalDTO.ApprovalRelationDTO `json:"approval"`
|
||||
}
|
||||
|
||||
type KandangWithProjectFlockIdDTO struct {
|
||||
kandangDTO.KandangRelationDTO
|
||||
ProjectFlockKandangId uint `json:"project_flock_kandang_id"`
|
||||
Period int `json:"period"`
|
||||
ClosedAt *time.Time `json:"closed_at,omitempty"`
|
||||
}
|
||||
|
||||
type ProjectFlockDetailDTO struct {
|
||||
@@ -75,20 +75,28 @@ func ToProjectFlockListDTOWithPeriod(e entity.ProjectFlock, period int) ProjectF
|
||||
for i, kandang := range e.Kandangs {
|
||||
|
||||
var (
|
||||
pfkId uint
|
||||
period int
|
||||
pfkId uint
|
||||
period int
|
||||
closedAt *time.Time
|
||||
)
|
||||
for _, kh := range e.KandangHistory {
|
||||
if kh.KandangId == kandang.Id {
|
||||
pfkId = kh.Id
|
||||
period = kh.Period
|
||||
closedAt = kh.ClosedAt
|
||||
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{
|
||||
KandangRelationDTO: kandangDTO.ToKandangRelationDTO(kandang),
|
||||
KandangRelationDTO: mapped,
|
||||
ProjectFlockKandangId: pfkId,
|
||||
Period: period,
|
||||
ClosedAt: closedAt,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -204,7 +212,7 @@ func resolveProjectFlockStandardFcr(e entity.ProjectFlock) *float64 {
|
||||
}
|
||||
week := 1
|
||||
if e.Category == string(utils.ProjectFlockCategoryLaying) {
|
||||
week = config.LayingWeekStart()
|
||||
week = 18
|
||||
}
|
||||
for _, detail := range e.ProductionStandard.ProductionStandardDetails {
|
||||
if detail.Week == week && detail.StandardFCR != nil {
|
||||
|
||||
@@ -40,8 +40,6 @@ type ProjectFlockKandangDTO struct {
|
||||
AvailableQuantity float64 `json:"available_quantity"`
|
||||
Population *float64 `json:"population,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 {
|
||||
|
||||
@@ -17,7 +17,6 @@ import (
|
||||
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"
|
||||
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"
|
||||
utils "gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
@@ -36,7 +35,6 @@ func (ProjectflockModule) RegisterRoutes(router fiber.Router, db *gorm.DB, valid
|
||||
projectflockKandangRepo := rProjectflock.NewProjectFlockKandangRepository(db)
|
||||
projectFlockPopulationRepo := rProjectflock.NewProjectFlockPopulationRepository(db)
|
||||
recordingRepo := rRecording.NewRecordingRepository(db)
|
||||
transferLayingRepo := rTransferLaying.NewTransferLayingRepository(db)
|
||||
warehouseRepo := rWarehouse.NewWarehouseRepository(db)
|
||||
productWarehouseRepo := rProductWarehouse.NewProductWarehouseRepository(db)
|
||||
projectBudgetRepo := rProjectBudget.NewProjectBudgetRepository(db)
|
||||
@@ -48,7 +46,7 @@ func (ProjectflockModule) RegisterRoutes(router fiber.Router, db *gorm.DB, valid
|
||||
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, transferLayingRepo, approvalService, validate)
|
||||
projectflockService := sProjectflock.NewProjectflockService(projectflockRepo, flockRepo, kandangRepo, projectflockKandangRepo, warehouseRepo, productWarehouseRepo, projectBudgetRepo, nonstockRepo, projectFlockPopulationRepo, recordingRepo, approvalService, validate)
|
||||
userService := sUser.NewUserService(userRepo, validate)
|
||||
|
||||
ProjectflockRoutes(router, userService, projectflockService)
|
||||
|
||||
+5
-16
@@ -51,7 +51,6 @@ func (r *projectFlockPopulationRepositoryImpl) GetByProjectFlockKandangID(ctx co
|
||||
err := r.DB().WithContext(ctx).
|
||||
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.deleted_at IS NULL").
|
||||
Preload("ProjectChickin").
|
||||
Find(&records).Error
|
||||
if err != nil {
|
||||
@@ -88,7 +87,6 @@ func (r *projectFlockPopulationRepositoryImpl) GetByProjectFlockKandangIDAndProd
|
||||
err := r.DB().WithContext(ctx).
|
||||
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.deleted_at IS NULL").
|
||||
Find(&records).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -101,10 +99,8 @@ func (r *projectFlockPopulationRepositoryImpl) GetTotalQtyByProjectFlockKandangI
|
||||
err := r.DB().WithContext(ctx).
|
||||
Table("project_flock_populations").
|
||||
Select("COALESCE(SUM(total_qty - total_used_qty), 0) AS available_qty").
|
||||
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.deleted_at IS NULL").
|
||||
Where("project_flock_populations.deleted_at IS NULL").
|
||||
Joins("JOIN product_warehouses pw ON project_flock_populations.product_warehouse_id = pw.id").
|
||||
Where("pw.project_flock_kandang_id = ?", projectFlockKandangID).
|
||||
Scan(&total).Error
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@@ -115,12 +111,9 @@ func (r *projectFlockPopulationRepositoryImpl) GetTotalQtyByProjectFlockKandangI
|
||||
func (r *projectFlockPopulationRepositoryImpl) GetTotalQtyByProductWarehouseID(ctx context.Context, productWarehouseID uint) (float64, error) {
|
||||
var total float64
|
||||
err := r.DB().WithContext(ctx).
|
||||
Table("project_flock_populations").
|
||||
Select("COALESCE(SUM(project_flock_populations.total_qty - project_flock_populations.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").
|
||||
Model(&entity.ProjectFlockPopulation{}).
|
||||
Where("product_warehouse_id = ?", productWarehouseID).
|
||||
Select("COALESCE(SUM(total_qty - total_used_qty), 0)").
|
||||
Scan(&total).Error
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@@ -135,8 +128,6 @@ func (r *projectFlockPopulationRepositoryImpl) GetAvailableQtyByProjectFlockKand
|
||||
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").
|
||||
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
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@@ -154,8 +145,6 @@ func (r *projectFlockPopulationRepositoryImpl) GetTotalChickInByProjectFlockKand
|
||||
Select("COALESCE(SUM(project_flock_populations.total_qty - project_flock_populations.total_used_qty), 0) AS total_qty").
|
||||
Joins("JOIN project_chickins ON project_chickins.id = project_flock_populations.project_chickin_id").
|
||||
Where("project_chickins.project_flock_kandang_id = ?", projectFlockKandangID).
|
||||
Where("project_chickins.deleted_at IS NULL").
|
||||
Where("project_flock_populations.deleted_at IS NULL").
|
||||
Scan(&total).Error
|
||||
if err != nil {
|
||||
return 0, err
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/validations"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -111,28 +110,6 @@ func (r *ProjectflockRepositoryImpl) applyQueryFilters(db *gorm.DB, params *vali
|
||||
AND pfk.kandang_id IN ?
|
||||
)`, 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)
|
||||
|
||||
|
||||
@@ -23,7 +23,6 @@ import (
|
||||
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"
|
||||
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"
|
||||
purchaseRepository "gitlab.com/mbugroup/lti-api.git/internal/modules/purchases/repositories"
|
||||
utils "gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
@@ -45,8 +44,6 @@ type ProjectflockService interface {
|
||||
GetProjectFlockKandangByProjectAndKandang(ctx *fiber.Ctx, projectFlockID uint, kandangID uint) (*entity.ProjectFlockKandang, float64, error)
|
||||
GetProjectFlockKandangPopulation(ctx *fiber.Ctx, projectFlockKandangID uint) (float64, 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)
|
||||
GetProjectPeriods(ctx *fiber.Ctx, projectIDs []uint) (map[uint]int, error)
|
||||
Approval(ctx *fiber.Ctx, req *validation.Approve) ([]entity.ProjectFlock, error)
|
||||
@@ -67,7 +64,6 @@ type projectflockService struct {
|
||||
PivotRepo repository.ProjectFlockKandangRepository
|
||||
PopulationRepo repository.ProjectFlockPopulationRepository
|
||||
RecordingRepo recordingRepo.RecordingRepository
|
||||
TransferLayingRepo transferLayingRepo.TransferLayingRepository
|
||||
ApprovalSvc commonSvc.ApprovalService
|
||||
approvalWorkflow approvalutils.ApprovalWorkflowKey
|
||||
}
|
||||
@@ -89,7 +85,6 @@ func NewProjectflockService(
|
||||
nonstockRepo nonstockRepository.NonstockRepository,
|
||||
populationRepo repository.ProjectFlockPopulationRepository,
|
||||
recordingRepo recordingRepo.RecordingRepository,
|
||||
transferLayingRepo transferLayingRepo.TransferLayingRepository,
|
||||
approvalSvc commonSvc.ApprovalService,
|
||||
validate *validator.Validate,
|
||||
|
||||
@@ -107,7 +102,6 @@ func NewProjectflockService(
|
||||
PivotRepo: pivotRepo,
|
||||
PopulationRepo: populationRepo,
|
||||
RecordingRepo: recordingRepo,
|
||||
TransferLayingRepo: transferLayingRepo,
|
||||
ApprovalSvc: approvalSvc,
|
||||
approvalWorkflow: utils.ApprovalWorkflowProjectFlock,
|
||||
}
|
||||
@@ -544,70 +538,6 @@ func (s projectflockService) GetProjectFlockKandangChickinDate(ctx *fiber.Ctx, p
|
||||
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) {
|
||||
idStr = strings.TrimSpace(idStr)
|
||||
projectFlockIdStr = strings.TrimSpace(projectFlockIdStr)
|
||||
@@ -649,10 +579,6 @@ func (s projectflockService) GetProjectFlockKandangByParams(ctx *fiber.Ctx, idSt
|
||||
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) {
|
||||
if s.PopulationRepo == nil {
|
||||
return 0, fiber.NewError(fiber.StatusInternalServerError, "Project flock population repository is not configured")
|
||||
|
||||
@@ -20,7 +20,6 @@ type Query struct {
|
||||
LocationId uint `query:"location_id" validate:"omitempty,number,gt=0"`
|
||||
Period int `query:"period" validate:"omitempty,number,gt=0"`
|
||||
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"`
|
||||
TransferContext string `query:"transfer_context" validate:"omitempty,oneof=transfer_to_laying"`
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/config"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
approvalDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/approvals/dto"
|
||||
productWarehouseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/adjustments/dto"
|
||||
@@ -87,8 +86,6 @@ type RecordingRelationDTO struct {
|
||||
EggWeight float64 `json:"egg_weight"`
|
||||
PopulationCanChange bool `json:"population_can_change"`
|
||||
TransferExecuted *bool `json:"transfer_executed,omitempty"`
|
||||
IsTransition bool `json:"is_transition"`
|
||||
IsLaying bool `json:"is_laying"`
|
||||
Approval approvalDTO.ApprovalRelationDTO `json:"approval"`
|
||||
}
|
||||
|
||||
@@ -250,8 +247,6 @@ func toRecordingRelationDTO(e entity.Recording) RecordingRelationDTO {
|
||||
EggWeight: floatValue(e.EggWeight),
|
||||
PopulationCanChange: boolValueDefault(e.PopulationCanChange, true),
|
||||
TransferExecuted: e.TransferExecuted,
|
||||
IsTransition: boolValueDefault(e.IsTransition, false),
|
||||
IsLaying: boolValueDefault(e.IsLaying, false),
|
||||
Approval: latestApproval,
|
||||
}
|
||||
}
|
||||
@@ -309,7 +304,7 @@ func recordingWeekValue(e entity.Recording) int {
|
||||
}
|
||||
weekBase := 1
|
||||
if isLayingRecording(e) {
|
||||
weekBase = config.LayingWeekStart()
|
||||
weekBase = 18
|
||||
}
|
||||
return ((day - 1) / 7) + weekBase
|
||||
}
|
||||
|
||||
@@ -125,7 +125,6 @@ func (RecordingModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
|
||||
nonstockRepo,
|
||||
projectFlockPopulationRepo,
|
||||
recordingRepo,
|
||||
transferLayingRepo,
|
||||
approvalService,
|
||||
validate,
|
||||
)
|
||||
@@ -155,6 +154,7 @@ func (RecordingModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate
|
||||
productWarehouseRepo,
|
||||
warehouseRepo,
|
||||
approvalService,
|
||||
fifoService,
|
||||
fifoStockV2Service,
|
||||
validate,
|
||||
)
|
||||
|
||||
@@ -71,7 +71,6 @@ type RecordingRepository interface {
|
||||
GetLatestAvgWeightByProjectFlockID(ctx context.Context, projectFlockID uint) (avgWeight float64, err error)
|
||||
GetTotalEggProductionWeightByProjectFlockID(ctx context.Context, projectFlockID uint) (totalWeightKg float64, err 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
|
||||
ValidateProductWarehousesByFlags(ctx context.Context, ids []uint, flags []string) (uint, error)
|
||||
}
|
||||
@@ -875,34 +874,6 @@ func (r *RecordingRepositoryImpl) GetAverageTargetMetricsByProjectFlockKandangID
|
||||
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 {
|
||||
if projectFlockKandangID == 0 {
|
||||
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