mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
Compare commits
518 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cad9328e5d | |||
| b43979bbba | |||
| 1a56b37e4e | |||
| 4340828fec | |||
| f74b6476de | |||
| 3011735458 | |||
| d40aac8960 | |||
| 18672f541e | |||
| 58aed76bbb | |||
| 77ec805931 | |||
| 505db703d8 | |||
| cc19b626e1 | |||
| fc157dfd79 | |||
| f407ef6a0c | |||
| 248ca1d522 | |||
| d41f1b9495 | |||
| 6efab80686 | |||
| 6253ca46bc | |||
| aa1fd1c35b | |||
| 1f10e96288 | |||
| ae69b138bf | |||
| b2dd9a6e13 | |||
| 9bc66798d4 | |||
| 1d95976360 | |||
| 114f1a7c24 | |||
| 14cc7ef2ae | |||
| 357b5709f5 | |||
| 474c42770b | |||
| 90de167fcd | |||
| 7183df6938 | |||
| b862fc4113 | |||
| f59cdd821a | |||
| 22038533d7 | |||
| 5641cadeec | |||
| 4eacdd543a | |||
| f75225b81b | |||
| a0221bb79c | |||
| 82f0db107a | |||
| d2ce0918b5 | |||
| 8f4fce1219 | |||
| b7ecf1dfcf | |||
| 9a328ae1e4 | |||
| 58ae03a090 | |||
| e406b20ca7 | |||
| 1c1f2f03aa | |||
| ce108da847 | |||
| e968b8ed9c | |||
| 96627e964f | |||
| 6e0ff557a8 | |||
| f4790e56ea | |||
| 760b37449e | |||
| fb2a7a6676 | |||
| a89c2edb99 | |||
| 5b80081d05 | |||
| 9bf33d2bae | |||
| 2cc89b31be | |||
| 3d92c4eb54 | |||
| 75b822eb19 | |||
| 3b661919c0 | |||
| 74146e90c6 | |||
| 1a9936eaa1 | |||
| c257a2cb28 | |||
| 8a403e803c | |||
| 0707876a81 | |||
| c191776e6c | |||
| 4bdcadb898 | |||
| 4f9cd5131f | |||
| 703c1548c9 | |||
| 616250d38b | |||
| a312b32aa2 | |||
| 9b6637066e | |||
| 4496d211a1 | |||
| 2c93558d05 | |||
| d5b3120ea3 | |||
| 4a85643aef | |||
| 22bd0e5891 | |||
| d1e3234f73 | |||
| 3d1d9c418b | |||
| 3669f20f4a | |||
| a21b554fc7 | |||
| 86caa6c4d4 | |||
| ef286949f8 | |||
| 16157b135f | |||
| a2012aa145 | |||
| 478487870b | |||
| 392b2f51ad | |||
| 20a45bb71e | |||
| 9c9cd1dd1b | |||
| bb8e4198f4 | |||
| 6b58eb0c65 | |||
| 97a807f494 | |||
| bee9feafaf | |||
| d0dd12776e | |||
| ac3623fa97 | |||
| 705939bbf5 | |||
| 4d6f731f80 | |||
| a26ccb7811 | |||
| 24289796b9 | |||
| 9dd4a93476 | |||
| b468ba3df9 | |||
| 2127e9d1f4 | |||
| 1dae84101f | |||
| 595ad6ed17 | |||
| 8c71e7f2a3 | |||
| 43f9b660ce | |||
| 641b7ebd38 | |||
| 396c1b5e45 | |||
| 2c169e7f83 | |||
| 150f8e8606 | |||
| 1a0261ed36 | |||
| c3fe6b0463 | |||
| 207e8ee3c4 | |||
| f2195ae208 | |||
| 8086d2fb9e | |||
| 3d39d6d31e | |||
| 3d70701d03 | |||
| 8410573ee6 | |||
| 095080320c | |||
| e503a84660 | |||
| 6cac4f0243 | |||
| 958dd4f241 | |||
| 37f0324e2a | |||
| 77043005dd | |||
| 8302345b30 | |||
| 461877f75c | |||
| f3ddd79974 | |||
| 8848a50e3b | |||
| ae7e53ac1f | |||
| ba20394a10 | |||
| b8de42b6fa | |||
| aa94c7cc02 | |||
| 5621c63e9a | |||
| 0920b91271 | |||
| 00cdfb692b | |||
| eef3c0f759 | |||
| 3c77aff413 | |||
| f8d42dbdb3 | |||
| e881c2b952 | |||
| 52ebcc5c2d | |||
| 3e0291c2ba | |||
| 7a704c4ec4 | |||
| bd0f89c521 | |||
| b83ebc0ff9 | |||
| 1572dfd0b8 | |||
| 258fd1d7e0 | |||
| f44ddef79b | |||
| 9339e1e9f0 | |||
| 798dd7f9a3 | |||
| 7a8f813e1f | |||
| d656eedfbe | |||
| 571f6b4bf3 | |||
| 76d980878d | |||
| fdd8e3ec31 | |||
| 0f6cd3a054 | |||
| c6d087eeab | |||
| 437cd3beda | |||
| 8fbce5a01e | |||
| f4b2408698 | |||
| 8c84981812 | |||
| 458c8e0a91 | |||
| 4646bf5577 | |||
| e60c08e09e | |||
| 33d3b18468 | |||
| 8b1831fc73 | |||
| 919dc5c2e8 | |||
| 129d253683 | |||
| f69321d9cd | |||
| 74158138c0 | |||
| 699a6e9289 | |||
| 507d6c4293 | |||
| 43286cead1 | |||
| 1216e65419 | |||
| 42f030a780 | |||
| cf475d678e | |||
| a42c201ac6 | |||
| d4699fba5b | |||
| e1ab5a90cb | |||
| f060da1cd3 | |||
| f82ac01e7c | |||
| fd2f773806 | |||
| 7db3afe985 | |||
| 8dc88b97a4 | |||
| f1787d3375 | |||
| 6b4eb758e4 | |||
| d54911f8b4 | |||
| 1edd071a8a | |||
| fb565ef728 | |||
| 9928b4c970 | |||
| 8c58cc4103 | |||
| 0d585a99a6 | |||
| 302147787e | |||
| 19a268adfe | |||
| b1b50c3c01 | |||
| c085888ca9 | |||
| 41d3c21fef | |||
| ad5e832d41 | |||
| 774c9b1d58 | |||
| 12ed9cd753 | |||
| 4ab1553340 | |||
| 3fa50b6344 | |||
| 80424dee17 | |||
| a9b33eaf28 | |||
| 551534d02a | |||
| 202a8ffc66 | |||
| 6bc5e7d293 | |||
| 2e0827dec5 | |||
| 87973a6c9f | |||
| ebac5f5a98 | |||
| 1ca6c6a104 | |||
| a1832a6144 | |||
| 78a45b11e7 | |||
| 58b29501c0 | |||
| bac0361df5 | |||
| c8912e503e | |||
| 645a97b460 | |||
| eb2479cc41 | |||
| 04ec8560a7 | |||
| 12240a9e2d | |||
| f2a46843c8 | |||
| 06e92d1c77 | |||
| d7ed768d14 | |||
| f04cbd24bd | |||
| ec4b849778 | |||
| 132e043597 | |||
| e99af36796 | |||
| f6bdb17699 | |||
| 8f7fc622f6 | |||
| 30f5ed417c | |||
| 1d726afa6f | |||
| 96ba947952 | |||
| ad0504f49e | |||
| 0b708cd57b | |||
| 32153f02b8 | |||
| cf37822a07 | |||
| 7fb6c3c7bf | |||
| fd689919b0 | |||
| 2ad0c17fbe | |||
| e8c7b3f2a8 | |||
| ce09c2473c | |||
| 3493d1d7b2 | |||
| 9fff954857 | |||
| c7c1e4b335 | |||
| 5dbe9bb989 | |||
| e555cfa950 | |||
| 3a457ceee5 | |||
| 32a8557a3b | |||
| baa49b7cf1 | |||
| 1818f5a295 | |||
| a73b44808f | |||
| 69d9137e78 | |||
| e32b231c6c | |||
| ca6d0b160b | |||
| e8a89f0f17 | |||
| d96a12776a | |||
| adabb4e035 | |||
| 16a0b848bc | |||
| c2d2701d72 | |||
| 894efa7aa5 | |||
| d0625e7d21 | |||
| 67ecdbc1dd | |||
| d50ab7cc97 | |||
| ad3bb0e29a | |||
| dd4dcc1c39 | |||
| aa4da68680 | |||
| 95965cb26a | |||
| e4e17f16f9 | |||
| edd77c5265 | |||
| dab692a0c1 | |||
| e6244dea8a | |||
| d1e4bf060e | |||
| fc06b3e4db | |||
| cf42e8c130 | |||
| 8ff97cb647 | |||
| 1b7ce3c62c | |||
| 21de17b18a | |||
| 2aaaab91f7 | |||
| 4227152979 | |||
| ec020ac17c | |||
| adc30ad5cd | |||
| 9fb5395469 | |||
| bc771660be | |||
| b615570036 | |||
| c793c3cf9a | |||
| b3e0410f5a | |||
| a882d5a687 | |||
| c0848b6d2d | |||
| b240478ed5 | |||
| 3052497fc0 | |||
| 71c62c5e02 | |||
| 768961d7d6 | |||
| 8cd9627a51 | |||
| 378d633ea4 | |||
| fb193fc61f | |||
| af7aabdec8 | |||
| bac36b4f00 | |||
| 687d02313b | |||
| 7d3602d829 | |||
| 533e9aca6f | |||
| dcfb5e10b4 | |||
| fbeccf4cdc | |||
| f2ae2cc731 | |||
| eda50930e7 | |||
| 0bc5480a1d | |||
| ef482dd1b9 | |||
| 302f0ed877 | |||
| 31c48ee1da | |||
| 8ad11af9c9 | |||
| 688d3fa757 | |||
| 409652c15e | |||
| 08be60c229 | |||
| 50b19dc1c3 | |||
| e770526c1a | |||
| 77af262662 | |||
| 9d611b7492 | |||
| 21ff1c8ab7 | |||
| 2ca84ecffe | |||
| f13e4f907c | |||
| 4d334e8d5c | |||
| 62522a751f | |||
| 62ccc2e5d6 | |||
| 13fc246f21 | |||
| 89293a843e | |||
| a5af469865 | |||
| 8efe9b668b | |||
| 89480deeb0 | |||
| 4146342120 | |||
| b375fb964e | |||
| fe002c9602 | |||
| f1032b44d1 | |||
| a3e9017e29 | |||
| 7f2401311b | |||
| 3f4d6c630a | |||
| 8792161c02 | |||
| 37c26d5877 | |||
| c316a6d7a9 | |||
| 3a89e18b16 | |||
| c6dc94a4e1 | |||
| aeb5433346 | |||
| 549e283757 | |||
| cad15bcd78 | |||
| e004354420 | |||
| e0ff6e6d79 | |||
| 804ff45dbd | |||
| 9f1c153841 | |||
| 2a884a8d09 | |||
| dbf72c7248 | |||
| 7daa509cd0 | |||
| 894fa0b22a | |||
| d680196919 | |||
| 9fc2d0556e | |||
| c2a89910fb | |||
| 32772a63c8 | |||
| 26bf7f165e | |||
| dff9e73ab1 | |||
| 1847e5590a | |||
| 57094f664c | |||
| f8b6e12d16 | |||
| e12c34db13 | |||
| 3012d260ec | |||
| f6e872c0aa | |||
| 8a639f127c | |||
| 2acaa10b60 | |||
| bd9d41e161 | |||
| 06d8d0b795 | |||
| 1d5e7b6e1a | |||
| da190f1b05 | |||
| c8905eb715 | |||
| 7f1d796b65 | |||
| 6c7ff3f415 | |||
| 03fbf7f4b7 | |||
| 7545e9b37d | |||
| 69f38bf16a | |||
| 5730053e04 | |||
| b087a703ef | |||
| 00edcb6add | |||
| de6580d11c | |||
| dcd6008946 | |||
| 711f58abae | |||
| ffd3c905fe | |||
| 6e4a8617da | |||
| 8a57d5d675 | |||
| 5de81f6315 | |||
| e50dd096a4 | |||
| 7551d11888 | |||
| 7444cfac31 | |||
| 1b5b5bc847 | |||
| 5d7b613ffc | |||
| 33e89d65ab | |||
| 0f4cc6e379 | |||
| 590df26a1f | |||
| ce7ce778fd | |||
| eaa208f733 | |||
| b088eebac5 | |||
| 3c10866208 | |||
| cfbe431222 | |||
| f7a392be52 | |||
| 4c434899aa | |||
| 7fd90f3268 | |||
| d26c2dba3f | |||
| f8415ea15d | |||
| 64fe845128 | |||
| 4bd8319e3b | |||
| bba2dec8c6 | |||
| f7b70d4b14 | |||
| 9f28294dc3 | |||
| 6a166ceb86 | |||
| f0b4fe916c | |||
| f37bf4d22d | |||
| ac5edb36e7 | |||
| 8fab5d7d91 | |||
| 5ddfb2c745 | |||
| 5cfa4d4a59 | |||
| 80b2cafd2f | |||
| b47f26d448 | |||
| 2f22182605 | |||
| e2d352721c | |||
| 068fe4329e | |||
| 15be8dcbea | |||
| 041e8763ac | |||
| 644e9911e4 | |||
| bb04cb53d9 | |||
| 048e607290 | |||
| 18441eb19f | |||
| 526e14f26e | |||
| 539081ce99 | |||
| d568b87e01 | |||
| 9515848d8f | |||
| c15ff8a211 | |||
| d1d94357cf | |||
| 67f5165bfb | |||
| 1217f34dcd | |||
| ae41422776 | |||
| 3978951d8f | |||
| 3422fceec7 | |||
| 09f1b29359 | |||
| 167d18fe87 | |||
| 473f4504ea | |||
| dc7dc0ba47 | |||
| a54129866e | |||
| d40243be4b | |||
| 525ff650f2 | |||
| c1e9b5a975 | |||
| 272367d8ef | |||
| d3c7d65bf5 | |||
| 944fd860a3 | |||
| af79db8726 | |||
| b42ca5e6fb | |||
| 3b2c6f16c3 | |||
| 359e982e76 | |||
| bc0bf7fe16 | |||
| 70a7b1b888 | |||
| 17d55bd2c0 | |||
| 9cc86df1ed | |||
| b7914e8294 | |||
| d33119661a | |||
| 8a57d439dc | |||
| 3d76854273 | |||
| b7a3882f20 | |||
| 29933a5df9 | |||
| f8aee4be7b | |||
| 4ee5bf3628 | |||
| 0f06dff761 | |||
| 0629c5ccf6 | |||
| 43eb1df118 | |||
| 338312edd1 | |||
| f7522636e2 | |||
| b11f03dfda | |||
| 76e65704d7 | |||
| 857a3c284b | |||
| 5606b9c4a3 | |||
| 7af78d04dd | |||
| 2650e919e7 | |||
| 7b2d3ae025 | |||
| 64e8de2344 | |||
| 2be9ae36c1 | |||
| 6c08fe23ca | |||
| 8a64300ddd | |||
| 9164550263 | |||
| a2d2c4269a | |||
| 90f363bfdb | |||
| a7a784970d | |||
| 18b0663dc6 | |||
| 375e057e7c | |||
| 9336289573 | |||
| 76d5b6b69a | |||
| 0a84e427c1 | |||
| cad91957b3 | |||
| fca2d63c6e | |||
| f5a016b74b | |||
| 82a7bada05 | |||
| c6626cb6f5 | |||
| ebfa88e721 | |||
| 705138795c | |||
| 538372a43a | |||
| 7a26ca5fe5 | |||
| a08466a28e | |||
| 1bdaf63763 | |||
| d8fb427734 | |||
| c9ebd88e9d | |||
| 0c6d42070a | |||
| 8725d79f8f | |||
| 39909d1c2e | |||
| 556540e97f | |||
| e421307965 | |||
| 1b5437bc01 | |||
| 7d6573fabd | |||
| ce083bccdc | |||
| dc4729c3b9 | |||
| bec6a93152 | |||
| 42853aaac0 | |||
| 610555c3cf | |||
| c60c40af03 | |||
| 2d098cb6b1 | |||
| 30231fabe9 | |||
| e738a97e4c | |||
| 81f4a5e33e | |||
| 1e9fdd2b0d | |||
| b6a60d5009 |
+4
-1
@@ -9,11 +9,13 @@ main
|
|||||||
bin/
|
bin/
|
||||||
*.exe
|
*.exe
|
||||||
*.out
|
*.out
|
||||||
|
.air.toml
|
||||||
Makefile
|
Makefile
|
||||||
docker-compose.local.yml
|
docker-compose.local.yml
|
||||||
docker-compose.yaml
|
docker-compose.yaml
|
||||||
|
Dockerfile
|
||||||
Dockerfile.local
|
Dockerfile.local
|
||||||
|
.gitlab-ci.yml
|
||||||
# Go build cache
|
# Go build cache
|
||||||
.gocache/
|
.gocache/
|
||||||
vendor
|
vendor
|
||||||
@@ -27,3 +29,4 @@ coverage/
|
|||||||
.vscode/
|
.vscode/
|
||||||
.idea/
|
.idea/
|
||||||
*.swp
|
*.swp
|
||||||
|
.DS_Store
|
||||||
|
|||||||
+29
-84
@@ -1,90 +1,35 @@
|
|||||||
stages:
|
workflow:
|
||||||
- deploy
|
rules:
|
||||||
|
# MR pipeline
|
||||||
|
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
|
||||||
|
when: always
|
||||||
|
|
||||||
deploy-dev:
|
# Push pipeline hanya untuk env branch
|
||||||
stage: deploy
|
- if: '$CI_COMMIT_BRANCH == "development"'
|
||||||
image: alpine:3.20
|
when: always
|
||||||
variables:
|
- if: '$CI_COMMIT_BRANCH == "staging"'
|
||||||
DEPLOY_APP: "LTI-MBUGROUP"
|
when: always
|
||||||
# Opsional: kalau pakai submodule, ini bikin clone submodule pakai SSH juga
|
- if: '$CI_COMMIT_BRANCH == "production"'
|
||||||
GIT_SUBMODULE_STRATEGY: recursive
|
when: always
|
||||||
GIT_DEPTH: "1"
|
|
||||||
|
|
||||||
before_script:
|
# Selain itu jangan buat pipeline
|
||||||
- echo "🧰 Installing dependencies..."
|
- when: never
|
||||||
- apk update && apk add --no-cache openssh git curl bash
|
|
||||||
|
|
||||||
# Setup SSH di runner
|
include:
|
||||||
- mkdir -p ~/.ssh
|
# khusus MR (notif)
|
||||||
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa
|
- local: "ci/merge_request.yml"
|
||||||
- chmod 600 ~/.ssh/id_rsa
|
rules:
|
||||||
- eval "$(ssh-agent -s)"
|
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
|
||||||
- ssh-add ~/.ssh/id_rsa
|
|
||||||
|
|
||||||
# Trust host keys (server + gitlab) biar SSH gak nanya interaktif
|
# khusus push ke branch env
|
||||||
- ssh-keyscan -H "$SERVER_IP" >> ~/.ssh/known_hosts
|
- local: "ci/development.yml"
|
||||||
- ssh-keyscan -H gitlab.com >> ~/.ssh/known_hosts
|
rules:
|
||||||
|
- if: '$CI_COMMIT_BRANCH == "development"'
|
||||||
|
|
||||||
script:
|
- local: "ci/staging.yml"
|
||||||
- echo "🚀 Deploying latest code to $SERVER_USER@$SERVER_IP"
|
rules:
|
||||||
|
- if: '$CI_COMMIT_BRANCH == "staging"'
|
||||||
|
|
||||||
- >
|
- local: "ci/production.yml"
|
||||||
if ssh -o StrictHostKeyChecking=no "$SERVER_USER@$SERVER_IP" "
|
rules:
|
||||||
set -e
|
- if: '$CI_COMMIT_BRANCH == "production"'
|
||||||
|
|
||||||
cd /home/devops/docker/deployment/development/lti-api
|
|
||||||
|
|
||||||
# Pastikan remote origin SSH (antisipasi kalau pernah ke-set HTTPS)
|
|
||||||
git remote set-url origin git@gitlab.com:mbugroup/lti-api.git
|
|
||||||
|
|
||||||
# Pastikan server percaya gitlab.com juga (untuk git fetch via SSH)
|
|
||||||
mkdir -p ~/.ssh
|
|
||||||
ssh-keyscan -H gitlab.com >> ~/.ssh/known_hosts
|
|
||||||
|
|
||||||
# Fetch/reset pakai SSH
|
|
||||||
GIT_SSH_COMMAND='ssh -o StrictHostKeyChecking=no' git fetch origin development
|
|
||||||
git reset --hard origin/development
|
|
||||||
|
|
||||||
docker compose restart dev-api-lti || docker compose up -d dev-api-lti
|
|
||||||
"; then
|
|
||||||
STATUS='success';
|
|
||||||
else
|
|
||||||
STATUS='failed';
|
|
||||||
fi;
|
|
||||||
|
|
||||||
RUN_URL="${CI_PROJECT_URL}/-/pipelines/${CI_PIPELINE_ID}";
|
|
||||||
|
|
||||||
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 "{
|
|
||||||
\"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";
|
|
||||||
|
|
||||||
only:
|
|
||||||
- development
|
|
||||||
|
|
||||||
environment:
|
|
||||||
name: development
|
|
||||||
|
|||||||
+29
-11
@@ -1,20 +1,38 @@
|
|||||||
FROM golang:1.23-alpine
|
# =========================
|
||||||
|
# Builder stage
|
||||||
|
# =========================
|
||||||
|
FROM golang:1.23-alpine AS builder
|
||||||
|
|
||||||
# Install dependensi dasar
|
RUN apk add --no-cache git ca-certificates tzdata
|
||||||
RUN apk add --no-cache git curl bash build-base
|
WORKDIR /app
|
||||||
|
|
||||||
# Install Air (pakai repo baru air-verse)
|
|
||||||
RUN go install github.com/air-verse/air@v1.52.3
|
|
||||||
|
|
||||||
WORKDIR /lti-api
|
|
||||||
|
|
||||||
# Cache dependencies
|
|
||||||
COPY go.mod go.sum ./
|
COPY go.mod go.sum ./
|
||||||
RUN go mod download
|
RUN go mod download
|
||||||
|
|
||||||
# Copy source code
|
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
|
# Build API binary
|
||||||
|
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)
|
||||||
|
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
|
||||||
|
go build -trimpath -ldflags="-s -w" -o lti-seed ./cmd/seed
|
||||||
|
|
||||||
|
# =========================
|
||||||
|
# Runtime stage
|
||||||
|
# =========================
|
||||||
|
FROM alpine:3.20
|
||||||
|
|
||||||
|
RUN apk add --no-cache ca-certificates tzdata curl bash postgresql-client \
|
||||||
|
&& adduser -D -H -u 10001 appuser
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY --from=builder /app/lti-api /app/lti-api
|
||||||
|
COPY --from=builder /app/lti-seed /app/lti-seed
|
||||||
|
|
||||||
|
USER appuser
|
||||||
EXPOSE 8081
|
EXPOSE 8081
|
||||||
|
|
||||||
CMD ["air", "-c", ".air.toml"]
|
CMD ["/app/lti-api"]
|
||||||
|
|||||||
@@ -110,4 +110,4 @@ IT Development PT Mitra Berlian Unggas Group
|
|||||||
|
|
||||||
## 📃 License
|
## 📃 License
|
||||||
|
|
||||||
This project is private. All rights reserved.
|
> This project is private. All rights reserved.
|
||||||
|
|||||||
@@ -0,0 +1,91 @@
|
|||||||
|
stages:
|
||||||
|
- deploy
|
||||||
|
|
||||||
|
deploy-dev:
|
||||||
|
stage: deploy
|
||||||
|
image: alpine:3.20
|
||||||
|
|
||||||
|
rules:
|
||||||
|
- if: '$CI_COMMIT_BRANCH == "development"'
|
||||||
|
when: on_success
|
||||||
|
- when: never
|
||||||
|
|
||||||
|
variables:
|
||||||
|
DEPLOY_APP: "LTI-MBUGROUP"
|
||||||
|
GIT_SUBMODULE_STRATEGY: recursive
|
||||||
|
GIT_DEPTH: "1"
|
||||||
|
|
||||||
|
before_script:
|
||||||
|
- echo "🧰 Installing dependencies..."
|
||||||
|
- apk update && apk add --no-cache openssh git curl bash
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
script:
|
||||||
|
- echo "🚀 Deploying latest code to $SERVER_USER@$SERVER_IP"
|
||||||
|
- >
|
||||||
|
if ssh -o StrictHostKeyChecking=no "$SERVER_USER@$SERVER_IP" "
|
||||||
|
set -e
|
||||||
|
|
||||||
|
cd /home/devops/docker/deployment/development/lti-api
|
||||||
|
|
||||||
|
# Pastikan remote origin SSH (antisipasi kalau pernah ke-set HTTPS)
|
||||||
|
git remote set-url origin git@gitlab.com:mbugroup/lti-api.git
|
||||||
|
|
||||||
|
# Pastikan server percaya gitlab.com juga (untuk git fetch via SSH)
|
||||||
|
mkdir -p ~/.ssh
|
||||||
|
ssh-keyscan -H gitlab.com >> ~/.ssh/known_hosts
|
||||||
|
|
||||||
|
# Fetch/reset pakai SSH
|
||||||
|
GIT_SSH_COMMAND='ssh -o StrictHostKeyChecking=no' git fetch origin development
|
||||||
|
git reset --hard origin/development
|
||||||
|
|
||||||
|
docker compose restart dev-api-lti || docker compose up -d dev-api-lti
|
||||||
|
"; then
|
||||||
|
STATUS='success';
|
||||||
|
else
|
||||||
|
STATUS='failed';
|
||||||
|
fi;
|
||||||
|
|
||||||
|
RUN_URL="${CI_PROJECT_URL}/-/pipelines/${CI_PIPELINE_ID}";
|
||||||
|
|
||||||
|
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 "{
|
||||||
|
\"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
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
stages:
|
||||||
|
- notify
|
||||||
|
|
||||||
|
notify_discord_on_mr_request_main_dev:
|
||||||
|
stage: notify
|
||||||
|
image: alpine:3.20
|
||||||
|
rules:
|
||||||
|
# hanya MR yang target ke main atau development
|
||||||
|
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && ($CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "main" || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "development")'
|
||||||
|
when: on_success
|
||||||
|
- when: never
|
||||||
|
|
||||||
|
script:
|
||||||
|
- apk add --no-cache curl jq coreutils
|
||||||
|
- |
|
||||||
|
TIME_HUMAN="$(date '+%d/%m/%y, %H.%M')"
|
||||||
|
TIME_ISO="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
||||||
|
|
||||||
|
TITLE="${CI_MERGE_REQUEST_TITLE}"
|
||||||
|
IID="!${CI_MERGE_REQUEST_IID}"
|
||||||
|
USER_LINE="${GITLAB_USER_NAME} (${GITLAB_USER_LOGIN})"
|
||||||
|
PROJECT_PATH="${CI_PROJECT_PATH}"
|
||||||
|
USERNAME="${GITLAB_USER_LOGIN}"
|
||||||
|
MR_URL="${CI_PROJECT_URL}/-/merge_requests/${CI_MERGE_REQUEST_IID}"
|
||||||
|
|
||||||
|
DESC="$(printf "**%s**\n\n%s opened merge request %s %s\n%s" \
|
||||||
|
"$USERNAME" "$USER_LINE" "$IID" "$TITLE" "$TIME_HUMAN")"
|
||||||
|
|
||||||
|
payload=$(jq -n \
|
||||||
|
--arg desc "$DESC" \
|
||||||
|
--arg project "$PROJECT_PATH" \
|
||||||
|
--arg timeiso "$TIME_ISO" \
|
||||||
|
--arg mrurl "$MR_URL" \
|
||||||
|
'{
|
||||||
|
"username": "Mock-api - Merge Requests",
|
||||||
|
"embeds": [
|
||||||
|
{
|
||||||
|
"description": ($desc + "\n" + $mrurl),
|
||||||
|
"color": 15105570,
|
||||||
|
"footer": { "text": $project },
|
||||||
|
"timestamp": $timeiso
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}')
|
||||||
|
|
||||||
|
curl -sS -H "Content-Type: application/json" \
|
||||||
|
-d "$payload" \
|
||||||
|
"$DISCORD_WEBHOOK_URL"
|
||||||
@@ -0,0 +1,155 @@
|
|||||||
|
stages:
|
||||||
|
- build
|
||||||
|
- migrate
|
||||||
|
- deploy
|
||||||
|
- seed
|
||||||
|
|
||||||
|
default:
|
||||||
|
tags:
|
||||||
|
- self-hosted-prod
|
||||||
|
|
||||||
|
variables:
|
||||||
|
DOCKER_BUILDKIT: "1"
|
||||||
|
|
||||||
|
IMAGE_TAG: "production_${CI_COMMIT_SHORT_SHA}"
|
||||||
|
IMAGE_NAME: "${CI_REGISTRY_IMAGE}:${IMAGE_TAG}"
|
||||||
|
IMAGE_LATEST: "${CI_REGISTRY_IMAGE}:production_latest"
|
||||||
|
|
||||||
|
DEPLOY_DIR: "/opt/deploy/lti"
|
||||||
|
COMPOSE_FILE: "docker-compose.yaml"
|
||||||
|
|
||||||
|
# =========================
|
||||||
|
# BUILD (AUTO)
|
||||||
|
# =========================
|
||||||
|
build_production:
|
||||||
|
stage: build
|
||||||
|
rules:
|
||||||
|
- if: '$CI_COMMIT_BRANCH == "production"'
|
||||||
|
when: on_success
|
||||||
|
- when: never
|
||||||
|
script: |
|
||||||
|
set -e
|
||||||
|
docker info
|
||||||
|
|
||||||
|
echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin "$CI_REGISTRY"
|
||||||
|
|
||||||
|
echo "✅ Build image: $IMAGE_NAME"
|
||||||
|
docker build -t "$IMAGE_NAME" -f Dockerfile .
|
||||||
|
|
||||||
|
echo "✅ Push image: $IMAGE_NAME"
|
||||||
|
docker push "$IMAGE_NAME"
|
||||||
|
|
||||||
|
echo "✅ Tag latest: $IMAGE_LATEST"
|
||||||
|
docker tag "$IMAGE_NAME" "$IMAGE_LATEST"
|
||||||
|
docker push "$IMAGE_LATEST"
|
||||||
|
|
||||||
|
# =========================
|
||||||
|
# MIGRATE (PRODUCTION)
|
||||||
|
# =========================
|
||||||
|
migrate_production:
|
||||||
|
stage: migrate
|
||||||
|
rules:
|
||||||
|
- if: '$CI_COMMIT_BRANCH == "production"'
|
||||||
|
when: on_success
|
||||||
|
- when: never
|
||||||
|
needs:
|
||||||
|
- job: build_production
|
||||||
|
artifacts: false
|
||||||
|
script: |
|
||||||
|
set -e
|
||||||
|
echo "✅ Running migrations (production) ..."
|
||||||
|
|
||||||
|
cd "$DEPLOY_DIR"
|
||||||
|
test -f "$COMPOSE_FILE" || (echo "❌ $COMPOSE_FILE not found in $DEPLOY_DIR" && exit 1)
|
||||||
|
test -f .env || (echo "❌ .env not found in $DEPLOY_DIR" && exit 1)
|
||||||
|
|
||||||
|
set -a
|
||||||
|
. ./.env
|
||||||
|
set +a
|
||||||
|
|
||||||
|
test -n "$DB_HOST" || (echo "❌ DB_HOST empty" && exit 1)
|
||||||
|
test -n "$DB_PORT" || (echo "❌ DB_PORT empty" && exit 1)
|
||||||
|
test -n "$DB_USER" || (echo "❌ DB_USER empty" && exit 1)
|
||||||
|
test -n "$DB_PASSWORD" || (echo "❌ DB_PASSWORD empty" && exit 1)
|
||||||
|
test -n "$DB_NAME" || (echo "❌ DB_NAME empty" && exit 1)
|
||||||
|
|
||||||
|
export DATABASE_URL="postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}?sslmode=${DB_SSLMODE:-disable}"
|
||||||
|
echo "✅ DATABASE_URL=$DATABASE_URL"
|
||||||
|
|
||||||
|
# NOTE: pastikan nama servicenya benar untuk production (ini sebelumnya masih stg-*)
|
||||||
|
docker compose -f "$COMPOSE_FILE" up -d stg-postgres-lti stg-redis-lti || true
|
||||||
|
|
||||||
|
COMPOSE_NETWORK_KEY="$(docker compose -f "$COMPOSE_FILE" config | awk '/networks:/ {getline; print $1}' | tr -d ':')"
|
||||||
|
NETWORK_NAME="$(docker network ls --format '{{.Name}}' | grep "_${COMPOSE_NETWORK_KEY}$" | head -n 1)"
|
||||||
|
test -n "$NETWORK_NAME" || (echo "❌ Cannot find docker network for compose ($COMPOSE_NETWORK_KEY)" && exit 1)
|
||||||
|
|
||||||
|
echo "✅ Checking migrations from repo..."
|
||||||
|
ls -lah "$CI_PROJECT_DIR/internal/database/migrations"
|
||||||
|
|
||||||
|
echo "✅ Running migrations via migrate/migrate container"
|
||||||
|
set +e
|
||||||
|
out=$(docker run --rm \
|
||||||
|
--network "$NETWORK_NAME" \
|
||||||
|
-v "$CI_PROJECT_DIR/internal/database/migrations:/migrations:ro" \
|
||||||
|
migrate/migrate:v4.15.2 \
|
||||||
|
-path=/migrations -database "$DATABASE_URL" up 2>&1)
|
||||||
|
code=$?
|
||||||
|
set -e
|
||||||
|
|
||||||
|
echo "$out"
|
||||||
|
|
||||||
|
if echo "$out" | grep -qi "no change"; then
|
||||||
|
echo "✅ No change (already up to date)"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ $code -ne 0 ]; then
|
||||||
|
echo "❌ Migration failed with exit code $code"
|
||||||
|
exit $code
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "✅ Migration applied successfully"
|
||||||
|
|
||||||
|
# =========================
|
||||||
|
# DEPLOY (AUTO)
|
||||||
|
# =========================
|
||||||
|
deploy_production:
|
||||||
|
stage: deploy
|
||||||
|
rules:
|
||||||
|
- if: '$CI_COMMIT_BRANCH == "production"'
|
||||||
|
when: on_success
|
||||||
|
- when: never
|
||||||
|
needs:
|
||||||
|
- job: build_production
|
||||||
|
artifacts: false
|
||||||
|
script: |
|
||||||
|
set -e
|
||||||
|
docker info
|
||||||
|
echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin "$CI_REGISTRY"
|
||||||
|
|
||||||
|
cd "$DEPLOY_DIR"
|
||||||
|
test -f "$COMPOSE_FILE" || (echo "❌ $COMPOSE_FILE not found in $DEPLOY_DIR" && exit 1)
|
||||||
|
test -f .env || (echo "❌ .env not found in $DEPLOY_DIR" && exit 1)
|
||||||
|
|
||||||
|
docker compose -f "$COMPOSE_FILE" pull
|
||||||
|
docker compose -f "$COMPOSE_FILE" up -d --force-recreate
|
||||||
|
docker image prune -f
|
||||||
|
|
||||||
|
# =========================
|
||||||
|
# SEED (MANUAL)
|
||||||
|
# =========================
|
||||||
|
seed_production:
|
||||||
|
stage: seed
|
||||||
|
rules:
|
||||||
|
- if: '$CI_COMMIT_BRANCH == "production"'
|
||||||
|
when: manual
|
||||||
|
- when: never
|
||||||
|
script: |
|
||||||
|
set -e
|
||||||
|
cd "$DEPLOY_DIR"
|
||||||
|
test -f .env || (echo "❌ .env not found" && exit 1)
|
||||||
|
|
||||||
|
echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin "$CI_REGISTRY"
|
||||||
|
|
||||||
|
docker compose --env-file .env pull seed
|
||||||
|
docker compose --env-file .env run --rm seed
|
||||||
+164
@@ -0,0 +1,164 @@
|
|||||||
|
stages:
|
||||||
|
- build
|
||||||
|
- migrate
|
||||||
|
- deploy
|
||||||
|
- seed
|
||||||
|
|
||||||
|
default:
|
||||||
|
tags:
|
||||||
|
- self-hosted-stg
|
||||||
|
|
||||||
|
variables:
|
||||||
|
DOCKER_BUILDKIT: "1"
|
||||||
|
|
||||||
|
IMAGE_TAG: "staging_${CI_COMMIT_SHORT_SHA}"
|
||||||
|
IMAGE_NAME: "${CI_REGISTRY_IMAGE}:${IMAGE_TAG}"
|
||||||
|
IMAGE_LATEST: "${CI_REGISTRY_IMAGE}:staging_latest"
|
||||||
|
|
||||||
|
DEPLOY_DIR: "/opt/deploy/stg-lti-api"
|
||||||
|
COMPOSE_FILE: "docker-compose.yaml"
|
||||||
|
|
||||||
|
# =========================
|
||||||
|
# BUILD (AUTO)
|
||||||
|
# =========================
|
||||||
|
build_staging:
|
||||||
|
stage: build
|
||||||
|
rules:
|
||||||
|
- if: '$CI_COMMIT_BRANCH == "staging"'
|
||||||
|
when: on_success
|
||||||
|
- when: never
|
||||||
|
script: |
|
||||||
|
set -e
|
||||||
|
docker info
|
||||||
|
|
||||||
|
echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin "$CI_REGISTRY"
|
||||||
|
|
||||||
|
echo "✅ Build image: $IMAGE_NAME"
|
||||||
|
docker build -t "$IMAGE_NAME" -f Dockerfile .
|
||||||
|
|
||||||
|
echo "✅ Push image: $IMAGE_NAME"
|
||||||
|
docker push "$IMAGE_NAME"
|
||||||
|
|
||||||
|
echo "✅ Tag latest: $IMAGE_LATEST"
|
||||||
|
docker tag "$IMAGE_NAME" "$IMAGE_LATEST"
|
||||||
|
docker push "$IMAGE_LATEST"
|
||||||
|
|
||||||
|
# =========================
|
||||||
|
# MIGRATE (AUTO)
|
||||||
|
# =========================
|
||||||
|
migrate_staging:
|
||||||
|
stage: migrate
|
||||||
|
rules:
|
||||||
|
- if: '$CI_COMMIT_BRANCH == "staging"'
|
||||||
|
when: on_success
|
||||||
|
- when: never
|
||||||
|
needs:
|
||||||
|
- job: build_staging
|
||||||
|
artifacts: false
|
||||||
|
script: |
|
||||||
|
set -e
|
||||||
|
echo "✅ Running migrations (staging) ..."
|
||||||
|
|
||||||
|
cd "$DEPLOY_DIR"
|
||||||
|
test -f "$COMPOSE_FILE" || (echo "❌ $COMPOSE_FILE not found in $DEPLOY_DIR" && exit 1)
|
||||||
|
test -f .env || (echo "❌ .env not found in $DEPLOY_DIR" && exit 1)
|
||||||
|
|
||||||
|
set -a
|
||||||
|
. ./.env
|
||||||
|
set +a
|
||||||
|
|
||||||
|
test -n "$DB_HOST" || (echo "❌ DB_HOST empty" && exit 1)
|
||||||
|
test -n "$DB_PORT" || (echo "❌ DB_PORT empty" && exit 1)
|
||||||
|
test -n "$DB_USER" || (echo "❌ DB_USER empty" && exit 1)
|
||||||
|
test -n "$DB_PASSWORD" || (echo "❌ DB_PASSWORD empty" && exit 1)
|
||||||
|
test -n "$DB_NAME" || (echo "❌ DB_NAME empty" && exit 1)
|
||||||
|
|
||||||
|
export DATABASE_URL="postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}?sslmode=${DB_SSLMODE:-disable}"
|
||||||
|
echo "✅ DATABASE_URL=$DATABASE_URL"
|
||||||
|
|
||||||
|
echo "✅ Ensuring postgres & redis running ..."
|
||||||
|
docker compose -f "$COMPOSE_FILE" up -d stg-postgres-lti stg-redis-lti || true
|
||||||
|
|
||||||
|
COMPOSE_NETWORK_KEY="$(docker compose -f "$COMPOSE_FILE" config | awk '/networks:/ {getline; print $1}' | tr -d ':')"
|
||||||
|
echo "✅ Compose network key: $COMPOSE_NETWORK_KEY"
|
||||||
|
|
||||||
|
NETWORK_NAME="$(docker network ls --format '{{.Name}}' | grep "_${COMPOSE_NETWORK_KEY}$" | head -n 1)"
|
||||||
|
test -n "$NETWORK_NAME" || (echo "❌ Cannot find docker network for compose ($COMPOSE_NETWORK_KEY)" && exit 1)
|
||||||
|
|
||||||
|
echo "✅ Docker network detected: $NETWORK_NAME"
|
||||||
|
|
||||||
|
echo "✅ Checking migrations from repo..."
|
||||||
|
ls -lah "$CI_PROJECT_DIR/internal/database/migrations"
|
||||||
|
|
||||||
|
echo "✅ Running migrations via migrate/migrate container"
|
||||||
|
set +e
|
||||||
|
out=$(docker run --rm \
|
||||||
|
--network "$NETWORK_NAME" \
|
||||||
|
-v "$CI_PROJECT_DIR/internal/database/migrations:/migrations:ro" \
|
||||||
|
migrate/migrate:v4.15.2 \
|
||||||
|
-path=/migrations -database "$DATABASE_URL" up 2>&1)
|
||||||
|
code=$?
|
||||||
|
set -e
|
||||||
|
|
||||||
|
echo "$out"
|
||||||
|
|
||||||
|
if echo "$out" | grep -qi "no change"; then
|
||||||
|
echo "✅ No change (already up to date)"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ $code -ne 0 ]; then
|
||||||
|
echo "❌ Migration failed with exit code $code"
|
||||||
|
exit $code
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "✅ Migration applied successfully"
|
||||||
|
|
||||||
|
# =========================
|
||||||
|
# DEPLOY (AUTO)
|
||||||
|
# =========================
|
||||||
|
deploy_staging:
|
||||||
|
stage: deploy
|
||||||
|
rules:
|
||||||
|
- if: '$CI_COMMIT_BRANCH == "staging"'
|
||||||
|
when: on_success
|
||||||
|
- when: never
|
||||||
|
needs:
|
||||||
|
- job: migrate_staging
|
||||||
|
artifacts: false
|
||||||
|
- job: build_staging
|
||||||
|
artifacts: false
|
||||||
|
script: |
|
||||||
|
set -e
|
||||||
|
docker info
|
||||||
|
echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin "$CI_REGISTRY"
|
||||||
|
|
||||||
|
cd "$DEPLOY_DIR"
|
||||||
|
test -f "$COMPOSE_FILE" || (echo "❌ $COMPOSE_FILE not found in $DEPLOY_DIR" && exit 1)
|
||||||
|
test -f .env || (echo "❌ .env not found in $DEPLOY_DIR" && exit 1)
|
||||||
|
|
||||||
|
docker compose -f "$COMPOSE_FILE" pull
|
||||||
|
docker compose -f "$COMPOSE_FILE" up -d --force-recreate
|
||||||
|
docker image prune -f
|
||||||
|
|
||||||
|
# =========================
|
||||||
|
# SEED (MANUAL)
|
||||||
|
# =========================
|
||||||
|
seed_staging:
|
||||||
|
stage: seed
|
||||||
|
rules:
|
||||||
|
- if: '$CI_COMMIT_BRANCH == "staging"'
|
||||||
|
when: manual
|
||||||
|
- when: never
|
||||||
|
needs:
|
||||||
|
- job: deploy_staging
|
||||||
|
artifacts: false
|
||||||
|
allow_failure: false
|
||||||
|
script: |
|
||||||
|
set -e
|
||||||
|
cd "$DEPLOY_DIR"
|
||||||
|
test -f "$COMPOSE_FILE" || (echo "❌ $COMPOSE_FILE not found" && exit 1)
|
||||||
|
test -f .env || (echo "❌ .env not found" && exit 1)
|
||||||
|
|
||||||
|
docker compose -f "$COMPOSE_FILE" pull seed || true
|
||||||
|
docker compose -f "$COMPOSE_FILE" run --rm seed
|
||||||
+1
-1
@@ -14,8 +14,8 @@ import (
|
|||||||
"gitlab.com/mbugroup/lti-api.git/internal/database"
|
"gitlab.com/mbugroup/lti-api.git/internal/database"
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
"gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/modules/sso/session"
|
"gitlab.com/mbugroup/lti-api.git/internal/modules/sso/session"
|
||||||
|
sso "gitlab.com/mbugroup/lti-api.git/internal/modules/sso/verifier"
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/route"
|
"gitlab.com/mbugroup/lti-api.git/internal/route"
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/sso"
|
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
|
|||||||
@@ -1,77 +0,0 @@
|
|||||||
services:
|
|
||||||
postgresdb:
|
|
||||||
image: postgres:alpine
|
|
||||||
restart: always
|
|
||||||
ports:
|
|
||||||
- "${DB_PORT_HOST:-5542}:5432"
|
|
||||||
environment:
|
|
||||||
POSTGRES_USER: ${DB_USER:-postgres}
|
|
||||||
POSTGRES_PASSWORD: ${DB_PASSWORD:-postgres}
|
|
||||||
POSTGRES_DB: ${DB_NAME:-db_lti_erp}
|
|
||||||
volumes:
|
|
||||||
- dbdata:/var/lib/postgresql/data
|
|
||||||
- ./internal/database/init:/docker-entrypoint-initdb.d
|
|
||||||
networks: [go-network]
|
|
||||||
healthcheck:
|
|
||||||
test:
|
|
||||||
[
|
|
||||||
"CMD-SHELL",
|
|
||||||
"pg_isready -U ${DB_USER:-postgres} -d ${DB_NAME:-db_lti_erp}",
|
|
||||||
]
|
|
||||||
interval: 10s
|
|
||||||
timeout: 5s
|
|
||||||
retries: 5
|
|
||||||
redis:
|
|
||||||
image: redis:7-alpine
|
|
||||||
restart: unless-stopped
|
|
||||||
ports:
|
|
||||||
- "${REDIS_PORT_HOST:-6381}:6379"
|
|
||||||
healthcheck:
|
|
||||||
test: ["CMD-SHELL", "redis-cli ping | grep PONG"]
|
|
||||||
interval: 5s
|
|
||||||
timeout: 3s
|
|
||||||
retries: 10
|
|
||||||
networks: [go-network]
|
|
||||||
|
|
||||||
app:
|
|
||||||
build:
|
|
||||||
context: .
|
|
||||||
dockerfile: Dockerfile.local
|
|
||||||
image: cosmtrek/air:v1.52.3
|
|
||||||
working_dir: /lti-api
|
|
||||||
volumes:
|
|
||||||
- .:/lti-api
|
|
||||||
- ./internal/config/jwtRS256.key:/run/keys/jwtRS256.key
|
|
||||||
- ./internal/config/jwtRS256.key.pub:/run/keys/jwtRS256.key.pub
|
|
||||||
command: air -c .air.toml
|
|
||||||
env_file:
|
|
||||||
- .env
|
|
||||||
environment:
|
|
||||||
DB_HOST: postgresdb
|
|
||||||
DB_PORT: 5432
|
|
||||||
DB_USER: ${DB_USER:-postgres}
|
|
||||||
DB_PASSWORD: ${DB_PASSWORD:-postgres}
|
|
||||||
DB_NAME: ${DB_NAME:-db_lti_erp}
|
|
||||||
REDIS_URL: ${REDIS_URL:-redis://redis:6379/0}
|
|
||||||
ports:
|
|
||||||
- "${APP_PORT:-8081}:8081"
|
|
||||||
depends_on:
|
|
||||||
postgresdb:
|
|
||||||
condition: service_healthy
|
|
||||||
networks: [go-network]
|
|
||||||
healthcheck:
|
|
||||||
test: ["CMD-SHELL", "wget -qO- http://localhost:8081/healthz || exit 1"]
|
|
||||||
interval: 10s
|
|
||||||
timeout: 3s
|
|
||||||
retries: 10
|
|
||||||
start_period: 10s
|
|
||||||
|
|
||||||
volumes:
|
|
||||||
dbdata:
|
|
||||||
go-mod-cache:
|
|
||||||
go-build-cache:
|
|
||||||
|
|
||||||
networks:
|
|
||||||
go-network:
|
|
||||||
name: lti-api_go-network
|
|
||||||
driver: bridge
|
|
||||||
@@ -1,98 +0,0 @@
|
|||||||
services:
|
|
||||||
dev-api-lti:
|
|
||||||
build:
|
|
||||||
context: .
|
|
||||||
dockerfile: Dockerfile
|
|
||||||
container_name: dev-api-lti
|
|
||||||
working_dir: /lti-api
|
|
||||||
command: ["/bin/sh", "scripts/entrypoint.sh"]
|
|
||||||
ports:
|
|
||||||
- "8081:8081"
|
|
||||||
env_file:
|
|
||||||
- .env
|
|
||||||
environment:
|
|
||||||
# override agar koneksi ke container internal
|
|
||||||
DB_HOST: dev-postgres-lti
|
|
||||||
DB_PORT: 5432
|
|
||||||
REDIS_URL: redis://dev-redis-lti:6379/0
|
|
||||||
volumes:
|
|
||||||
- .:/lti-api
|
|
||||||
- ./.air.toml:/lti-api/.air.toml:ro
|
|
||||||
- ./internal/config/jwtRS256.key:/run/keys/jwtRS256.key
|
|
||||||
- ./internal/config/jwtRS256.key.pub:/run/keys/jwtRS256.key.pub
|
|
||||||
depends_on:
|
|
||||||
- dev-postgres-lti
|
|
||||||
- dev-redis-lti
|
|
||||||
networks:
|
|
||||||
- lti-network
|
|
||||||
healthcheck:
|
|
||||||
test: ["CMD-SHELL", "wget -qO- http://localhost:8081/healthz || exit 1"]
|
|
||||||
interval: 10s
|
|
||||||
timeout: 3s
|
|
||||||
retries: 10
|
|
||||||
start_period: 10s
|
|
||||||
deploy:
|
|
||||||
resources:
|
|
||||||
limits:
|
|
||||||
cpus: "2.0"
|
|
||||||
memory: 2G
|
|
||||||
reservations:
|
|
||||||
cpus: "1.0"
|
|
||||||
memory: 512M
|
|
||||||
|
|
||||||
dev-postgres-lti:
|
|
||||||
image: postgres:15-alpine
|
|
||||||
container_name: dev-postgres-lti
|
|
||||||
restart: always
|
|
||||||
env_file:
|
|
||||||
- credential/.env.db
|
|
||||||
ports:
|
|
||||||
- "5433:5432"
|
|
||||||
volumes:
|
|
||||||
- dev-postgres-lti-data:/var/lib/postgresql/data
|
|
||||||
- ./credential:/docker-entrypoint-initdb.d:ro
|
|
||||||
networks:
|
|
||||||
- lti-network
|
|
||||||
healthcheck:
|
|
||||||
test: ["CMD-SHELL", "pg_isready -U ${DB_USER:-postgres} -d ${DB_NAME:-db_lti_erp}"]
|
|
||||||
interval: 10s
|
|
||||||
timeout: 5s
|
|
||||||
retries: 5
|
|
||||||
start_period: 5s
|
|
||||||
deploy:
|
|
||||||
resources:
|
|
||||||
limits:
|
|
||||||
cpus: "1.0"
|
|
||||||
memory: 2G
|
|
||||||
reservations:
|
|
||||||
cpus: "0.5"
|
|
||||||
memory: 512M
|
|
||||||
|
|
||||||
dev-redis-lti:
|
|
||||||
image: redis:7-alpine
|
|
||||||
container_name: dev-redis-lti
|
|
||||||
restart: always
|
|
||||||
ports:
|
|
||||||
- "6380:6379"
|
|
||||||
networks:
|
|
||||||
- lti-network
|
|
||||||
healthcheck:
|
|
||||||
test: ["CMD", "redis-cli", "ping"]
|
|
||||||
interval: 10s
|
|
||||||
timeout: 3s
|
|
||||||
retries: 10
|
|
||||||
deploy:
|
|
||||||
resources:
|
|
||||||
limits:
|
|
||||||
cpus: "0.5"
|
|
||||||
memory: 512M
|
|
||||||
reservations:
|
|
||||||
cpus: "0.2"
|
|
||||||
memory: 256M
|
|
||||||
|
|
||||||
networks:
|
|
||||||
lti-network:
|
|
||||||
driver: bridge
|
|
||||||
|
|
||||||
volumes:
|
|
||||||
dev-postgres-lti-data:
|
|
||||||
@@ -16,6 +16,7 @@ require (
|
|||||||
github.com/golang-jwt/jwt/v5 v5.2.1
|
github.com/golang-jwt/jwt/v5 v5.2.1
|
||||||
github.com/google/uuid v1.6.0
|
github.com/google/uuid v1.6.0
|
||||||
github.com/jackc/pgconn v1.14.1
|
github.com/jackc/pgconn v1.14.1
|
||||||
|
github.com/jackc/pgx/v5 v5.5.5
|
||||||
github.com/redis/go-redis/v9 v9.14.0
|
github.com/redis/go-redis/v9 v9.14.0
|
||||||
github.com/sirupsen/logrus v1.9.3
|
github.com/sirupsen/logrus v1.9.3
|
||||||
github.com/spf13/viper v1.19.0
|
github.com/spf13/viper v1.19.0
|
||||||
@@ -60,7 +61,6 @@ require (
|
|||||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||||
github.com/jackc/pgproto3/v2 v2.3.2 // indirect
|
github.com/jackc/pgproto3/v2 v2.3.2 // indirect
|
||||||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
|
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
|
||||||
github.com/jackc/pgx/v5 v5.5.5 // indirect
|
|
||||||
github.com/jackc/puddle/v2 v2.2.1 // indirect
|
github.com/jackc/puddle/v2 v2.2.1 // indirect
|
||||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||||
github.com/jinzhu/now v1.1.5 // indirect
|
github.com/jinzhu/now v1.1.5 // indirect
|
||||||
|
|||||||
@@ -262,14 +262,10 @@ github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVS
|
|||||||
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
|
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
|
||||||
github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d h1:llb0neMWDQe87IzJLS4Ci7psK/lVsjIS2otl+1WyRyY=
|
github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d h1:llb0neMWDQe87IzJLS4Ci7psK/lVsjIS2otl+1WyRyY=
|
||||||
github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
|
github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
|
||||||
github.com/xuri/efp v0.0.1 h1:fws5Rv3myXyYni8uwj2qKjVaRP30PdjeYe2Y6FDsCL8=
|
|
||||||
github.com/xuri/efp v0.0.1/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
|
|
||||||
github.com/xuri/excelize/v2 v2.9.0 h1:1tgOaEq92IOEumR1/JfYS/eR0KHOCsRv/rYXXh6YJQE=
|
github.com/xuri/excelize/v2 v2.9.0 h1:1tgOaEq92IOEumR1/JfYS/eR0KHOCsRv/rYXXh6YJQE=
|
||||||
github.com/xuri/excelize/v2 v2.9.0/go.mod h1:uqey4QBZ9gdMeWApPLdhm9x+9o2lq4iVmjiLfBS5hdE=
|
github.com/xuri/excelize/v2 v2.9.0/go.mod h1:uqey4QBZ9gdMeWApPLdhm9x+9o2lq4iVmjiLfBS5hdE=
|
||||||
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 h1:hPVCafDV85blFTabnqKgNhDCkJX25eik94Si9cTER4A=
|
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 h1:hPVCafDV85blFTabnqKgNhDCkJX25eik94Si9cTER4A=
|
||||||
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
|
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
|
||||||
github.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9 h1:+C0TIdyyYmzadGaL/HBLbf3WdLgC29pgyhTjAT/0nuE=
|
|
||||||
github.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
|
|
||||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||||
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
|
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
|
||||||
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
|
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
|
||||||
|
|||||||
@@ -0,0 +1,309 @@
|
|||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/utils/fifo"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type HppCostRepository interface {
|
||||||
|
GetProjectFlockKandangIDs(ctx context.Context, projectFlockId uint) ([]uint, error)
|
||||||
|
GetDocCost(ctx context.Context, projectFlockKandangIDs []uint) (float64, error)
|
||||||
|
GetBudgetCostByProjectFlockId(ctx context.Context, projectFlockId uint) (float64, error)
|
||||||
|
GetExpedisionCost(ctx context.Context, projectFlockKandangIDs []uint) (float64, error)
|
||||||
|
GetFeedUsageCost(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (float64, error)
|
||||||
|
GetOvkUsageCost(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (float64, error)
|
||||||
|
GetTotalPopulation(ctx context.Context, projectFlockKandangIDs []uint) (float64, error)
|
||||||
|
GetPulletCost(ctx context.Context, projectFlockKandangId uint) (float64, error)
|
||||||
|
GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (float64, float64, error)
|
||||||
|
GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, startDate *time.Time, endDate *time.Time) (float64, float64, error)
|
||||||
|
GetProjectFlockIDByProjectFlockKandangID(ctx context.Context, projectFlockKandangId uint) (uint, error)
|
||||||
|
GetTransferSourceSummary(ctx context.Context, projectFlockKandangId uint) (uint, float64, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type HppRepositoryImpl struct {
|
||||||
|
db *gorm.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHppCostRepository(db *gorm.DB) HppCostRepository {
|
||||||
|
return &HppRepositoryImpl{db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *HppRepositoryImpl) GetProjectFlockKandangIDs(ctx context.Context, projectFlockId uint) ([]uint, error) {
|
||||||
|
var ids []uint
|
||||||
|
err := r.db.WithContext(ctx).
|
||||||
|
Table("project_flock_kandangs").
|
||||||
|
Select("id").
|
||||||
|
Where("project_flock_id = ?", projectFlockId).
|
||||||
|
Scan(&ids).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return ids, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *HppRepositoryImpl) GetDocCost(ctx context.Context, projectFlockKandangIDs []uint) (float64, error) {
|
||||||
|
var total float64
|
||||||
|
err := r.db.WithContext(ctx).
|
||||||
|
Table("project_chickins AS pc").
|
||||||
|
Select("COALESCE(SUM(pc.usage_qty * COALESCE(pi.price, 0)), 0)").
|
||||||
|
Joins("JOIN stock_allocations AS sa ON sa.usable_type = ? AND sa.usable_id = pc.id AND sa.stockable_type = ?", fifo.UsableKeyProjectChickin.String(), fifo.StockableKeyPurchaseItems.String()).
|
||||||
|
Joins("JOIN purchase_items AS pi ON pi.id = sa.stockable_id").
|
||||||
|
Where("pc.project_flock_kandang_id IN (?)", projectFlockKandangIDs).
|
||||||
|
Scan(&total).Error
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return total, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *HppRepositoryImpl) GetBudgetCostByProjectFlockId(ctx context.Context, projectFlockId uint) (float64, error) {
|
||||||
|
var total float64
|
||||||
|
err := r.db.WithContext(ctx).
|
||||||
|
Table("project_budgets AS pb").
|
||||||
|
Select("COALESCE(SUM(pb.qty * pb.price), 0)").
|
||||||
|
Where("pb.project_flock_id = ?", projectFlockId).
|
||||||
|
Scan(&total).Error
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return total, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *HppRepositoryImpl) GetExpedisionCost(ctx context.Context, projectFlockKandangIDs []uint) (float64, error) {
|
||||||
|
var total float64
|
||||||
|
err := r.db.WithContext(ctx).
|
||||||
|
Table("expense_nonstocks AS en").
|
||||||
|
Select("COALESCE(SUM(er.qty * er.price), 0)").
|
||||||
|
Joins("JOIN expense_realizations AS er ON er.expense_nonstock_id = en.id").
|
||||||
|
Joins("JOIN flags AS f ON f.flagable_id = en.nonstock_id AND f.flagable_type = ?", entity.FlagableTypeNonstock).
|
||||||
|
Where("en.project_flock_kandang_id IN (?)", projectFlockKandangIDs).
|
||||||
|
Where("f.name = ?", utils.FlagEkspedisi).
|
||||||
|
Scan(&total).Error
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return total, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *HppRepositoryImpl) GetFeedUsageCost(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (float64, error) {
|
||||||
|
if date == nil {
|
||||||
|
now := time.Now()
|
||||||
|
date = &now
|
||||||
|
}
|
||||||
|
|
||||||
|
var total float64
|
||||||
|
err := r.db.WithContext(ctx).
|
||||||
|
Table("recordings AS r").
|
||||||
|
Select("COALESCE(SUM(rs.usage_qty * COALESCE(pi.price, 0)), 0)").
|
||||||
|
Joins("JOIN recording_stocks AS rs ON rs.recording_id = r.id").
|
||||||
|
Joins("JOIN product_warehouses AS pw ON pw.id = rs.product_warehouse_id").
|
||||||
|
Joins("JOIN flags AS f ON f.flagable_id = pw.product_id AND f.flagable_type = ?", entity.FlagableTypeProduct).
|
||||||
|
Joins("JOIN stock_allocations AS sa ON sa.usable_type = ? AND sa.usable_id = rs.id AND sa.stockable_type = ?", fifo.UsableKeyRecordingStock.String(), fifo.StockableKeyPurchaseItems.String()).
|
||||||
|
Joins("JOIN purchase_items AS pi ON pi.id = sa.stockable_id").
|
||||||
|
Where("r.project_flock_kandangs_id IN (?)", projectFlockKandangIDs).
|
||||||
|
Where("r.record_datetime <= ?", *date).
|
||||||
|
Where("f.name = ?", utils.FlagPakan).
|
||||||
|
Scan(&total).Error
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return total, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *HppRepositoryImpl) GetOvkUsageCost(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (float64, error) {
|
||||||
|
if date == nil {
|
||||||
|
now := time.Now()
|
||||||
|
date = &now
|
||||||
|
}
|
||||||
|
|
||||||
|
flags := []utils.FlagType{
|
||||||
|
utils.FlagOVK,
|
||||||
|
utils.FlagObat,
|
||||||
|
utils.FlagVitamin,
|
||||||
|
utils.FlagKimia,
|
||||||
|
}
|
||||||
|
|
||||||
|
var total float64
|
||||||
|
err := r.db.WithContext(ctx).
|
||||||
|
Table("recordings AS r").
|
||||||
|
Select("COALESCE(SUM(rs.usage_qty * COALESCE(pi.price, 0)), 0)").
|
||||||
|
Joins("JOIN recording_stocks AS rs ON rs.recording_id = r.id").
|
||||||
|
Joins("JOIN product_warehouses AS pw ON pw.id = rs.product_warehouse_id").
|
||||||
|
Joins("JOIN flags AS f ON f.flagable_id = pw.product_id AND f.flagable_type = ?", entity.FlagableTypeProduct).
|
||||||
|
Joins("JOIN stock_allocations AS sa ON sa.usable_type = ? AND sa.usable_id = rs.id AND sa.stockable_type = ?", fifo.UsableKeyRecordingStock.String(), fifo.StockableKeyPurchaseItems.String()).
|
||||||
|
Joins("JOIN purchase_items AS pi ON pi.id = sa.stockable_id").
|
||||||
|
Where("r.project_flock_kandangs_id IN (?)", projectFlockKandangIDs).
|
||||||
|
Where("r.record_datetime <= ?", *date).
|
||||||
|
Where("f.name IN ?", flags).
|
||||||
|
Scan(&total).Error
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return total, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *HppRepositoryImpl) GetTotalPopulation(ctx context.Context, projectFlockKandangIDs []uint) (float64, error) {
|
||||||
|
var total float64
|
||||||
|
err := r.db.WithContext(ctx).
|
||||||
|
Table("project_chickins AS pc").
|
||||||
|
Select("COALESCE(SUM(pc.usage_qty), 0)").
|
||||||
|
Where("pc.project_flock_kandang_id IN (?)", projectFlockKandangIDs).
|
||||||
|
Scan(&total).Error
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return total, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *HppRepositoryImpl) GetPulletCost(ctx context.Context, projectFlockKandangId uint) (float64, error) {
|
||||||
|
stockablePurchase := fifo.StockableKeyPurchaseItems.String()
|
||||||
|
stockableTransferIn := fifo.StockableKeyStockTransferIn.String()
|
||||||
|
usableProjectChickin := fifo.UsableKeyProjectChickin.String()
|
||||||
|
|
||||||
|
var total float64
|
||||||
|
err := r.db.WithContext(ctx).
|
||||||
|
Table("project_chickins AS pc").
|
||||||
|
Select(`
|
||||||
|
COALESCE(SUM(pc.usage_qty * CASE
|
||||||
|
WHEN sa.stockable_type = ? THEN COALESCE(pi.price, 0)
|
||||||
|
WHEN sa.stockable_type = ? THEN COALESCE(tpi.price, 0)
|
||||||
|
ELSE 0
|
||||||
|
END), 0)`,
|
||||||
|
stockablePurchase, stockableTransferIn).
|
||||||
|
Joins("JOIN stock_allocations AS sa ON sa.usable_type = ? AND sa.usable_id = pc.id", usableProjectChickin).
|
||||||
|
Joins("LEFT JOIN purchase_items AS pi ON pi.id = sa.stockable_id AND sa.stockable_type = ?", stockablePurchase).
|
||||||
|
Joins("LEFT JOIN stock_allocations AS tsa ON tsa.usable_type = ? AND tsa.usable_id = sa.stockable_id AND sa.stockable_type = ? AND tsa.stockable_type = ?", stockableTransferIn, stockableTransferIn, stockablePurchase).
|
||||||
|
Joins("LEFT JOIN purchase_items AS tpi ON tpi.id = tsa.stockable_id").
|
||||||
|
Where("pc.project_flock_kandang_id = ?", projectFlockKandangId).
|
||||||
|
Scan(&total).Error
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return total, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *HppRepositoryImpl) GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (float64, float64, error) {
|
||||||
|
if date == nil {
|
||||||
|
now := time.Now()
|
||||||
|
date = &now
|
||||||
|
}
|
||||||
|
|
||||||
|
var totals struct {
|
||||||
|
TotalPieces float64
|
||||||
|
TotalWeightKg float64
|
||||||
|
}
|
||||||
|
err := r.db.WithContext(ctx).
|
||||||
|
Table("recordings AS r").
|
||||||
|
Select("COALESCE(SUM(re.qty), 0) AS total_pieces, COALESCE(SUM(re.weight), 0)AS total_weight_kg").
|
||||||
|
Joins("JOIN recording_eggs AS re ON re.recording_id = r.id").
|
||||||
|
Where("r.project_flock_kandangs_id IN (?)", projectFlockKandangIDs).
|
||||||
|
Where("r.record_datetime <= ?", *date).
|
||||||
|
Scan(&totals).Error
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return totals.TotalPieces, totals.TotalWeightKg, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *HppRepositoryImpl) GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(
|
||||||
|
ctx context.Context,
|
||||||
|
projectFlockKandangIDs []uint,
|
||||||
|
startDate *time.Time,
|
||||||
|
endDate *time.Time,
|
||||||
|
) (float64, float64, error) {
|
||||||
|
|
||||||
|
if endDate == nil {
|
||||||
|
now := time.Now()
|
||||||
|
endDate = &now
|
||||||
|
}
|
||||||
|
|
||||||
|
type subResult struct {
|
||||||
|
UsableID uint
|
||||||
|
MdpUsageQty float64
|
||||||
|
MdpWeight float64
|
||||||
|
}
|
||||||
|
|
||||||
|
subQuery := r.db.WithContext(ctx).
|
||||||
|
Table("recordings AS r").
|
||||||
|
Select(`
|
||||||
|
DISTINCT sa.usable_id,
|
||||||
|
mdp.usage_qty AS mdp_usage_qty,
|
||||||
|
mdp.total_weight AS mdp_weight
|
||||||
|
`).
|
||||||
|
Joins("JOIN recording_eggs re ON re.recording_id = r.id").
|
||||||
|
Joins(
|
||||||
|
"JOIN stock_allocations sa ON sa.stockable_type = ? AND sa.stockable_id = re.id AND sa.usable_type = ?",
|
||||||
|
fifo.StockableKeyRecordingEgg.String(),
|
||||||
|
fifo.UsableKeyMarketingDelivery.String(),
|
||||||
|
).
|
||||||
|
Joins("JOIN marketing_delivery_products mdp ON mdp.id = sa.usable_id").
|
||||||
|
Where("r.project_flock_kandangs_id IN (?)", projectFlockKandangIDs).
|
||||||
|
Where("r.record_datetime <= ?", *endDate).
|
||||||
|
Where("mdp.delivery_date <= ?", *startDate)
|
||||||
|
|
||||||
|
var totals struct {
|
||||||
|
TotalPieces float64
|
||||||
|
TotalWeight float64
|
||||||
|
}
|
||||||
|
|
||||||
|
err := r.db.WithContext(ctx).
|
||||||
|
Table("(?) AS x", subQuery).
|
||||||
|
Select(`
|
||||||
|
COALESCE(SUM(x.mdp_usage_qty), 0) AS total_pieces,
|
||||||
|
COALESCE(SUM(x.mdp_weight), 0) AS total_weight
|
||||||
|
`).
|
||||||
|
Scan(&totals).Error
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return totals.TotalPieces, totals.TotalWeight, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *HppRepositoryImpl) GetProjectFlockIDByProjectFlockKandangID(ctx context.Context, projectFlockKandangId uint) (uint, error) {
|
||||||
|
var projectFlockID uint
|
||||||
|
err := r.db.WithContext(ctx).
|
||||||
|
Table("project_flock_kandangs").
|
||||||
|
Select("project_flock_id").
|
||||||
|
Where("id = ?", projectFlockKandangId).
|
||||||
|
Scan(&projectFlockID).Error
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return projectFlockID, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *HppRepositoryImpl) GetTransferSourceSummary(ctx context.Context, projectFlockKandangId uint) (uint, float64, error) {
|
||||||
|
var summary struct {
|
||||||
|
ProjectFlockID uint
|
||||||
|
TotalQty float64
|
||||||
|
}
|
||||||
|
err := r.db.WithContext(ctx).
|
||||||
|
Table("laying_transfer_targets AS ltt").
|
||||||
|
Select("lt.from_project_flock_id AS project_flock_id, COALESCE(SUM(ltt.total_qty), 0) AS total_qty").
|
||||||
|
Joins("JOIN laying_transfers AS lt ON lt.id = ltt.laying_transfer_id").
|
||||||
|
Where("ltt.target_project_flock_kandang_id = ?", projectFlockKandangId).
|
||||||
|
Group("lt.from_project_flock_id").
|
||||||
|
Scan(&summary).Error
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return summary.ProjectFlockID, summary.TotalQty, nil
|
||||||
|
}
|
||||||
@@ -15,7 +15,7 @@ type ApprovalService interface {
|
|||||||
WorkflowSteps(workflow approvalutils.ApprovalWorkflowKey) map[approvalutils.ApprovalStep]string
|
WorkflowSteps(workflow approvalutils.ApprovalWorkflowKey) map[approvalutils.ApprovalStep]string
|
||||||
WorkflowStepName(workflow approvalutils.ApprovalWorkflowKey, step approvalutils.ApprovalStep) (string, bool)
|
WorkflowStepName(workflow approvalutils.ApprovalWorkflowKey, step approvalutils.ApprovalStep) (string, bool)
|
||||||
CreateApproval(ctx context.Context, workflow approvalutils.ApprovalWorkflowKey, approvableID uint, step approvalutils.ApprovalStep, action *entity.ApprovalAction, actorID uint, note *string) (*entity.Approval, error)
|
CreateApproval(ctx context.Context, workflow approvalutils.ApprovalWorkflowKey, approvableID uint, step approvalutils.ApprovalStep, action *entity.ApprovalAction, actorID uint, note *string) (*entity.Approval, error)
|
||||||
List(ctx context.Context, module string, approvableID *uint, page, limit int, search string) ([]entity.Approval, int64, error)
|
List(ctx context.Context, module string, approvableID *uint, page, limit int, search string, orderByDate string) ([]entity.Approval, int64, error)
|
||||||
ListByTarget(ctx context.Context, workflow approvalutils.ApprovalWorkflowKey, approvableID uint, modifier func(*gorm.DB) *gorm.DB) ([]entity.Approval, error)
|
ListByTarget(ctx context.Context, workflow approvalutils.ApprovalWorkflowKey, approvableID uint, modifier func(*gorm.DB) *gorm.DB) ([]entity.Approval, error)
|
||||||
LatestByTarget(ctx context.Context, workflow approvalutils.ApprovalWorkflowKey, approvableID uint, modifier func(*gorm.DB) *gorm.DB) (*entity.Approval, error)
|
LatestByTarget(ctx context.Context, workflow approvalutils.ApprovalWorkflowKey, approvableID uint, modifier func(*gorm.DB) *gorm.DB) (*entity.Approval, error)
|
||||||
LatestByTargets(ctx context.Context, workflow approvalutils.ApprovalWorkflowKey, approvableIDs []uint, modifier func(*gorm.DB) *gorm.DB) (map[uint]*entity.Approval, error)
|
LatestByTargets(ctx context.Context, workflow approvalutils.ApprovalWorkflowKey, approvableIDs []uint, modifier func(*gorm.DB) *gorm.DB) (map[uint]*entity.Approval, error)
|
||||||
@@ -70,9 +70,14 @@ func (s *approvalService) List(
|
|||||||
approvableID *uint,
|
approvableID *uint,
|
||||||
page, limit int,
|
page, limit int,
|
||||||
search string,
|
search string,
|
||||||
|
orderByDate string,
|
||||||
) ([]entity.Approval, int64, error) {
|
) ([]entity.Approval, int64, error) {
|
||||||
module = strings.TrimSpace(strings.ToUpper(module))
|
module = strings.TrimSpace(strings.ToUpper(module))
|
||||||
search = strings.TrimSpace(search)
|
search = strings.TrimSpace(search)
|
||||||
|
orderByDate = strings.TrimSpace(strings.ToUpper(orderByDate))
|
||||||
|
if orderByDate != "ASC" && orderByDate != "DESC" {
|
||||||
|
orderByDate = "DESC"
|
||||||
|
}
|
||||||
|
|
||||||
if limit <= 0 {
|
if limit <= 0 {
|
||||||
limit = 10
|
limit = 10
|
||||||
@@ -90,7 +95,7 @@ func (s *approvalService) List(
|
|||||||
func(db *gorm.DB) *gorm.DB {
|
func(db *gorm.DB) *gorm.DB {
|
||||||
query := db.
|
query := db.
|
||||||
Where("approvable_type = ?", module).
|
Where("approvable_type = ?", module).
|
||||||
Order("action_at DESC").
|
Order("action_at " + orderByDate).
|
||||||
Preload("ActionUser")
|
Preload("ActionUser")
|
||||||
|
|
||||||
if approvableID != nil {
|
if approvableID != nil {
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultDocumentPathLimit = 50
|
defaultDocumentPathLimit = 255
|
||||||
defaultDocumentKeyPrefix = "docs"
|
defaultDocumentKeyPrefix = "docs"
|
||||||
maxDocumentNameLength = 50
|
maxDocumentNameLength = 50
|
||||||
)
|
)
|
||||||
@@ -363,13 +363,19 @@ func (s *documentService) generateObjectKey(ext string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
u := uuid.New().String()
|
u := uuid.New().String()
|
||||||
key := fmt.Sprintf("%s/%s%s", strings.Trim(s.keyPrefix, "/"), u, normalizedExt)
|
keyPrefix := strings.Trim(s.keyPrefix, "/")
|
||||||
if s.keyPrefix == "" {
|
key := fmt.Sprintf("%s%s", u, normalizedExt)
|
||||||
key = fmt.Sprintf("%s%s", u, normalizedExt)
|
if keyPrefix != "" {
|
||||||
|
key = fmt.Sprintf("%s/%s%s", keyPrefix, u, normalizedExt)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(key) > s.maxPathLength {
|
if len(key) > s.maxPathLength {
|
||||||
key = fmt.Sprintf("%s%s", u, normalizedExt)
|
compact := strings.ReplaceAll(u, "-", "")
|
||||||
|
if keyPrefix != "" {
|
||||||
|
key = fmt.Sprintf("%s/%s%s", keyPrefix, compact, normalizedExt)
|
||||||
|
} else {
|
||||||
|
key = fmt.Sprintf("%s%s", compact, normalizedExt)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(key) > s.maxPathLength {
|
if len(key) > s.maxPathLength {
|
||||||
|
|||||||
@@ -25,6 +25,8 @@ type FifoService interface {
|
|||||||
Replenish(ctx context.Context, req StockReplenishRequest) (*StockReplenishResult, error)
|
Replenish(ctx context.Context, req StockReplenishRequest) (*StockReplenishResult, error)
|
||||||
Consume(ctx context.Context, req StockConsumeRequest) (*StockConsumeResult, error)
|
Consume(ctx context.Context, req StockConsumeRequest) (*StockConsumeResult, error)
|
||||||
ReleaseUsage(ctx context.Context, req StockReleaseRequest) error
|
ReleaseUsage(ctx context.Context, req StockReleaseRequest) error
|
||||||
|
AdjustStockableQuantity(ctx context.Context, req StockAdjustRequest) error
|
||||||
|
ResolvePending(ctx context.Context, req PendingResolveRequest) ([]PendingResolution, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type fifoService struct {
|
type fifoService struct {
|
||||||
@@ -95,12 +97,26 @@ type StockReplenishRequest struct {
|
|||||||
Tx *gorm.DB
|
Tx *gorm.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type StockAdjustRequest struct {
|
||||||
|
StockableKey fifo.StockableKey
|
||||||
|
StockableID uint
|
||||||
|
ProductWarehouseID uint
|
||||||
|
Quantity float64
|
||||||
|
Note *string
|
||||||
|
Tx *gorm.DB
|
||||||
|
}
|
||||||
|
|
||||||
type PendingResolution struct {
|
type PendingResolution struct {
|
||||||
UsableKey fifo.UsableKey
|
UsableKey fifo.UsableKey
|
||||||
UsableID uint
|
UsableID uint
|
||||||
Quantity float64
|
Quantity float64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PendingResolveRequest struct {
|
||||||
|
ProductWarehouseID uint
|
||||||
|
Tx *gorm.DB
|
||||||
|
}
|
||||||
|
|
||||||
type StockReplenishResult struct {
|
type StockReplenishResult struct {
|
||||||
AddedQuantity float64
|
AddedQuantity float64
|
||||||
PendingResolved []PendingResolution
|
PendingResolved []PendingResolution
|
||||||
@@ -138,6 +154,38 @@ type StockReleaseRequest struct {
|
|||||||
Tx *gorm.DB
|
Tx *gorm.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *fifoService) AdjustStockableQuantity(ctx context.Context, req StockAdjustRequest) error {
|
||||||
|
if req.StockableID == 0 || strings.TrimSpace(req.StockableKey.String()) == "" {
|
||||||
|
return errors.New("stockable key and id are required")
|
||||||
|
}
|
||||||
|
if req.ProductWarehouseID == 0 {
|
||||||
|
return errors.New("product warehouse id is required")
|
||||||
|
}
|
||||||
|
if req.Quantity == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if req.Quantity > 0 {
|
||||||
|
return errors.New("quantity must be negative")
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg, ok := fifo.Stockable(req.StockableKey)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("stockable %q is not registered", req.StockableKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.withTransaction(ctx, req.Tx, func(tx *gorm.DB) error {
|
||||||
|
if err := s.incrementStockableQty(ctx, tx, cfg, req.StockableID, req.Quantity); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.productWarehouseRepo.AdjustQuantities(ctx, map[uint]float64{
|
||||||
|
req.ProductWarehouseID: req.Quantity,
|
||||||
|
}, func(db *gorm.DB) *gorm.DB {
|
||||||
|
return s.txOrDB(tx, db)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func (s *fifoService) Replenish(ctx context.Context, req StockReplenishRequest) (*StockReplenishResult, error) {
|
func (s *fifoService) Replenish(ctx context.Context, req StockReplenishRequest) (*StockReplenishResult, error) {
|
||||||
if req.StockableID == 0 || strings.TrimSpace(req.StockableKey.String()) == "" {
|
if req.StockableID == 0 || strings.TrimSpace(req.StockableKey.String()) == "" {
|
||||||
return nil, errors.New("stockable key and id are required")
|
return nil, errors.New("stockable key and id are required")
|
||||||
@@ -185,6 +233,23 @@ func (s *fifoService) Replenish(ctx context.Context, req StockReplenishRequest)
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *fifoService) ResolvePending(ctx context.Context, req PendingResolveRequest) ([]PendingResolution, error) {
|
||||||
|
if req.ProductWarehouseID == 0 {
|
||||||
|
return nil, errors.New("product warehouse id is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
var resolved []PendingResolution
|
||||||
|
err := s.withTransaction(ctx, req.Tx, func(tx *gorm.DB) error {
|
||||||
|
var err error
|
||||||
|
resolved, err = s.resolvePendingForWarehouse(ctx, tx, req.ProductWarehouseID)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return resolved, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *fifoService) Consume(ctx context.Context, req StockConsumeRequest) (*StockConsumeResult, error) {
|
func (s *fifoService) Consume(ctx context.Context, req StockConsumeRequest) (*StockConsumeResult, error) {
|
||||||
if req.UsableID == 0 || strings.TrimSpace(req.UsableKey.String()) == "" {
|
if req.UsableID == 0 || strings.TrimSpace(req.UsableKey.String()) == "" {
|
||||||
return nil, errors.New("usable key and id are required")
|
return nil, errors.New("usable key and id are required")
|
||||||
@@ -228,7 +293,13 @@ func (s *fifoService) Consume(ctx context.Context, req StockConsumeRequest) (*St
|
|||||||
|
|
||||||
switch {
|
switch {
|
||||||
case delta > 0:
|
case delta > 0:
|
||||||
allocationRes, err := s.allocateFromStock(ctx, tx, productWarehouseID, req.UsableKey, req.UsableID, delta)
|
|
||||||
|
var excludedStockables []fifo.StockableKey
|
||||||
|
if cfg.ExcludedStockables != nil {
|
||||||
|
excludedStockables = cfg.ExcludedStockables
|
||||||
|
}
|
||||||
|
|
||||||
|
allocationRes, err := s.allocateFromStock(ctx, tx, productWarehouseID, req.UsableKey, req.UsableID, delta, excludedStockables)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -261,7 +332,7 @@ func (s *fifoService) Consume(ctx context.Context, req StockConsumeRequest) (*St
|
|||||||
}
|
}
|
||||||
|
|
||||||
if reductionTarget > 0 {
|
if reductionTarget > 0 {
|
||||||
released, err := s.releaseUsagePortion(ctx, tx, req.UsableKey, req.UsableID, reductionTarget)
|
released, err := s.releaseUsagePortion(ctx, tx, req.UsableKey, req.UsableID, reductionTarget, productWarehouseID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -308,7 +379,7 @@ func (s *fifoService) ReleaseUsage(ctx context.Context, req StockReleaseRequest)
|
|||||||
}
|
}
|
||||||
var usageDelta, pendingDelta float64
|
var usageDelta, pendingDelta float64
|
||||||
if ctxRow.UsageQty > 0 {
|
if ctxRow.UsageQty > 0 {
|
||||||
if _, err := s.releaseUsagePortion(ctx, tx, req.UsableKey, req.UsableID, ctxRow.UsageQty); err != nil {
|
if _, err := s.releaseUsagePortion(ctx, tx, req.UsableKey, req.UsableID, ctxRow.UsageQty, ctxRow.ProductWarehouseID); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
usageDelta -= ctxRow.UsageQty
|
usageDelta -= ctxRow.UsageQty
|
||||||
@@ -410,8 +481,9 @@ func (s *fifoService) allocateFromStock(
|
|||||||
usableKey fifo.UsableKey,
|
usableKey fifo.UsableKey,
|
||||||
usableID uint,
|
usableID uint,
|
||||||
requestQty float64,
|
requestQty float64,
|
||||||
|
excludedStockables []fifo.StockableKey,
|
||||||
) (*allocationOutcome, error) {
|
) (*allocationOutcome, error) {
|
||||||
lots, err := s.fetchStockLots(ctx, tx, productWarehouseID)
|
lots, err := s.fetchStockLots(ctx, tx, productWarehouseID, excludedStockables)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -492,14 +564,24 @@ func (s *fifoService) allocateFromStock(
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *fifoService) fetchStockLots(ctx context.Context, tx *gorm.DB, productWarehouseID uint) ([]stockLot, error) {
|
func (s *fifoService) fetchStockLots(ctx context.Context, tx *gorm.DB, productWarehouseID uint, excludedStockables []fifo.StockableKey) ([]stockLot, error) {
|
||||||
configs := fifo.Stockables()
|
configs := fifo.Stockables()
|
||||||
if len(configs) == 0 {
|
if len(configs) == 0 {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create exclusion set for faster lookup
|
||||||
|
excludedSet := make(map[fifo.StockableKey]bool)
|
||||||
|
for _, key := range excludedStockables {
|
||||||
|
excludedSet[key] = true
|
||||||
|
}
|
||||||
|
|
||||||
var lots []stockLot
|
var lots []stockLot
|
||||||
for key, cfg := range configs {
|
for key, cfg := range configs {
|
||||||
|
// Skip excluded stockables
|
||||||
|
if excludedSet[key] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
usesNumericTime := cfg.Columns.CreatedAt == cfg.Columns.ID
|
usesNumericTime := cfg.Columns.CreatedAt == cfg.Columns.ID
|
||||||
|
|
||||||
@@ -616,7 +698,13 @@ func (s *fifoService) resolvePendingForWarehouse(ctx context.Context, tx *gorm.D
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
outcome, err := s.allocateFromStock(ctx, tx, productWarehouseID, candidate.UsableKey, candidate.UsableID, candidate.Pending)
|
// Get excluded stockables from candidate usable config
|
||||||
|
var excludedStockables []fifo.StockableKey
|
||||||
|
if candidate.Config.ExcludedStockables != nil {
|
||||||
|
excludedStockables = candidate.Config.ExcludedStockables
|
||||||
|
}
|
||||||
|
|
||||||
|
outcome, err := s.allocateFromStock(ctx, tx, productWarehouseID, candidate.UsableKey, candidate.UsableID, candidate.Pending, excludedStockables)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -657,6 +745,7 @@ func (s *fifoService) releaseUsagePortion(
|
|||||||
usableKey fifo.UsableKey,
|
usableKey fifo.UsableKey,
|
||||||
usableID uint,
|
usableID uint,
|
||||||
target float64,
|
target float64,
|
||||||
|
expectedWarehouseID uint,
|
||||||
) (float64, error) {
|
) (float64, error) {
|
||||||
if target <= 0 {
|
if target <= 0 {
|
||||||
return 0, nil
|
return 0, nil
|
||||||
@@ -672,6 +761,18 @@ func (s *fifoService) releaseUsagePortion(
|
|||||||
if len(allocations) == 0 {
|
if len(allocations) == 0 {
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
for i := range allocations {
|
||||||
|
alloc := &allocations[i]
|
||||||
|
if expectedWarehouseID == 0 || alloc.ProductWarehouseId == expectedWarehouseID {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if err := tx.Model(&entities.StockAllocation{}).
|
||||||
|
Where("id = ?", alloc.Id).
|
||||||
|
Update("product_warehouse_id", expectedWarehouseID).Error; err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
alloc.ProductWarehouseId = expectedWarehouseID
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
remaining = target
|
remaining = target
|
||||||
@@ -768,30 +869,30 @@ func (s *fifoService) fetchPendingCandidates(ctx context.Context, tx *gorm.DB, p
|
|||||||
cfg.Columns.CreatedAt,
|
cfg.Columns.CreatedAt,
|
||||||
)
|
)
|
||||||
|
|
||||||
var rows []struct {
|
if cfg.Columns.CreatedAt == cfg.Columns.ID {
|
||||||
ID uint
|
var rows []struct {
|
||||||
Pending float64
|
ID uint
|
||||||
CreatedAt time.Time
|
Pending float64 `gorm:"column:pending_qty"`
|
||||||
}
|
CreatedAt int64 `gorm:"column:created_at"`
|
||||||
|
}
|
||||||
|
|
||||||
query := tx.Table(cfg.Table).
|
query := tx.Table(cfg.Table).
|
||||||
Select(selectStmt).
|
Select(selectStmt).
|
||||||
Where(fmt.Sprintf("%s = ?", cfg.Columns.ProductWarehouseID), productWarehouseID).
|
Where(fmt.Sprintf("%s = ?", cfg.Columns.ProductWarehouseID), productWarehouseID).
|
||||||
Where(fmt.Sprintf("%s > 0", cfg.Columns.PendingQuantity)).
|
Where(fmt.Sprintf("%s > 0", cfg.Columns.PendingQuantity)).
|
||||||
Limit(s.pendingBatchPerUsable)
|
Limit(s.pendingBatchPerUsable)
|
||||||
|
|
||||||
if cfg.Scope != nil {
|
if cfg.Scope != nil {
|
||||||
query = cfg.Scope(query)
|
query = cfg.Scope(query)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, order := range s.orderClauses(cfg.OrderBy) {
|
for _, order := range s.orderClauses(cfg.OrderBy) {
|
||||||
query = query.Order(order)
|
query = query.Order(order)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := query.Find(&rows).Error; err != nil {
|
if err := query.Find(&rows).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, row := range rows {
|
for _, row := range rows {
|
||||||
if row.Pending <= 0 {
|
if row.Pending <= 0 {
|
||||||
continue
|
continue
|
||||||
@@ -801,9 +902,47 @@ func (s *fifoService) fetchPendingCandidates(ctx context.Context, tx *gorm.DB, p
|
|||||||
Config: cfg,
|
Config: cfg,
|
||||||
UsableID: row.ID,
|
UsableID: row.ID,
|
||||||
Pending: row.Pending,
|
Pending: row.Pending,
|
||||||
CreatedAt: row.CreatedAt,
|
CreatedAt: time.Unix(0, row.CreatedAt),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
var rows []struct {
|
||||||
|
ID uint
|
||||||
|
Pending float64 `gorm:"column:pending_qty"`
|
||||||
|
CreatedAt time.Time `gorm:"column:created_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
query := tx.Table(cfg.Table).
|
||||||
|
Select(selectStmt).
|
||||||
|
Where(fmt.Sprintf("%s = ?", cfg.Columns.ProductWarehouseID), productWarehouseID).
|
||||||
|
Where(fmt.Sprintf("%s > 0", cfg.Columns.PendingQuantity)).
|
||||||
|
Limit(s.pendingBatchPerUsable)
|
||||||
|
|
||||||
|
if cfg.Scope != nil {
|
||||||
|
query = cfg.Scope(query)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, order := range s.orderClauses(cfg.OrderBy) {
|
||||||
|
query = query.Order(order)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := query.Find(&rows).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, row := range rows {
|
||||||
|
if row.Pending <= 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
candidates = append(candidates, pendingCandidate{
|
||||||
|
UsableKey: key,
|
||||||
|
Config: cfg,
|
||||||
|
UsableID: row.ID,
|
||||||
|
Pending: row.Pending,
|
||||||
|
CreatedAt: row.CreatedAt,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(candidates) == 0 {
|
if len(candidates) == 0 {
|
||||||
|
|||||||
@@ -0,0 +1,272 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"math"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||||
|
)
|
||||||
|
|
||||||
|
type HppService interface {
|
||||||
|
CalculateHppCost(projectFlockKandangId uint, date *time.Time) (*HppCostResponse, error)
|
||||||
|
GetTotalDepresiasiFlockGrowing(sourceProjectFlockID uint, date *time.Time) (float64, error)
|
||||||
|
GetTotalProductionCost(projectFlockKandangId uint, endDate *time.Time, depresiasiTransfer float64) (float64, error)
|
||||||
|
GetBudgetKandangLaying(projectFlockKandangId uint, endDate *time.Time) (float64, error)
|
||||||
|
GetDepresiasiTransfer(projectFlockKandangId uint, date *time.Time) (float64, error)
|
||||||
|
GetHppEstimationDanRealisasi(totalProductionCost float64, projectFlockKandangId uint, startDate *time.Time, endDate *time.Time) (*HppCostResponse, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type HppCostResponse struct {
|
||||||
|
Estimation HppCostDetail `json:"estimation"`
|
||||||
|
Real HppCostDetail `json:"real"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type HppCostDetail struct {
|
||||||
|
HargaKg float64 `json:"harga_kg"`
|
||||||
|
HargaButir float64 `json:"harga_butir"`
|
||||||
|
Total float64 `json:"total"`
|
||||||
|
Kg float64 `json:"kg"`
|
||||||
|
Butir float64 `json:"butir"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type hppService struct {
|
||||||
|
hppRepo commonRepo.HppCostRepository
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHppService(hppRepo commonRepo.HppCostRepository) HppService {
|
||||||
|
return &hppService{hppRepo: hppRepo}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *hppService) CalculateHppCost(projectFlockKandangId uint, date *time.Time) (*HppCostResponse, error) {
|
||||||
|
if date == nil {
|
||||||
|
now := time.Now()
|
||||||
|
date = &now
|
||||||
|
}
|
||||||
|
|
||||||
|
location, err := time.LoadLocation("Asia/Jakarta")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
startOfDay := time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, location)
|
||||||
|
endOfDay := startOfDay.Add(24 * time.Hour)
|
||||||
|
|
||||||
|
depresiasiTransfer, err := s.GetDepresiasiTransfer(projectFlockKandangId, &endOfDay)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
totalProductionCost, err := s.GetTotalProductionCost(projectFlockKandangId, &endOfDay, depresiasiTransfer)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.GetHppEstimationDanRealisasi(totalProductionCost, projectFlockKandangId, &startOfDay, &endOfDay)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *hppService) GetTotalDepresiasiFlockGrowing(sourceProjectFlockID uint, date *time.Time) (float64, error) {
|
||||||
|
if date == nil {
|
||||||
|
now := time.Now()
|
||||||
|
date = &now
|
||||||
|
}
|
||||||
|
|
||||||
|
if s.hppRepo == nil {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
kandangIDs, err := s.hppRepo.GetProjectFlockKandangIDs(context.Background(), sourceProjectFlockID)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
docCost, err := s.hppRepo.GetDocCost(context.Background(), kandangIDs)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
budgetCost, err := s.hppRepo.GetBudgetCostByProjectFlockId(context.Background(), sourceProjectFlockID)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
expedisionCost, err := s.hppRepo.GetExpedisionCost(context.Background(), kandangIDs)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
feedCost, err := s.hppRepo.GetFeedUsageCost(context.Background(), kandangIDs, date)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
ovkCost, err := s.hppRepo.GetOvkUsageCost(context.Background(), kandangIDs, date)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return docCost + budgetCost + expedisionCost + feedCost + ovkCost, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *hppService) GetTotalProductionCost(projectFlockKandangId uint, endDate *time.Time, depresiasiTransfer float64) (float64, error) {
|
||||||
|
// if date == nil {
|
||||||
|
// now := time.Now()
|
||||||
|
// date = &now
|
||||||
|
// }
|
||||||
|
|
||||||
|
costPullet, err := s.hppRepo.GetPulletCost(context.Background(), projectFlockKandangId)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
costFeed, err := s.hppRepo.GetFeedUsageCost(context.Background(), []uint{projectFlockKandangId}, endDate)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
costOvk, err := s.hppRepo.GetOvkUsageCost(context.Background(), []uint{projectFlockKandangId}, endDate)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
costExpedision, err := s.hppRepo.GetExpedisionCost(context.Background(), []uint{projectFlockKandangId})
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
costBudget, err := s.GetBudgetKandangLaying(projectFlockKandangId, endDate)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return depresiasiTransfer + costPullet + costFeed + costOvk + costExpedision + costBudget, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *hppService) GetBudgetKandangLaying(projectFlockKandangId uint, endDate *time.Time) (float64, error) {
|
||||||
|
// if date == nil {
|
||||||
|
// now := time.Now()
|
||||||
|
// date = &now
|
||||||
|
// }
|
||||||
|
|
||||||
|
if s.hppRepo == nil {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
projectFlockId, err := s.hppRepo.GetProjectFlockIDByProjectFlockKandangID(context.Background(), projectFlockKandangId)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
projectFlockKandangIds, err := s.hppRepo.GetProjectFlockKandangIDs(context.Background(), projectFlockId)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
eggProduksiPiecesFlock, _, err := s.hppRepo.GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(context.Background(), projectFlockKandangIds, endDate)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
eggProduksiPiecesKandang, _, err := s.hppRepo.GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(context.Background(), []uint{projectFlockKandangId}, endDate)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
totalBudgetCost, err := s.hppRepo.GetBudgetCostByProjectFlockId(context.Background(), projectFlockId)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if eggProduksiPiecesFlock == 0 {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return (totalBudgetCost * eggProduksiPiecesKandang) / eggProduksiPiecesFlock, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *hppService) GetDepresiasiTransfer(projectFlockKandangId uint, endDate *time.Time) (float64, error) {
|
||||||
|
// if endDate == nil {
|
||||||
|
// now := time.Now()
|
||||||
|
// endDate = &now
|
||||||
|
// }
|
||||||
|
|
||||||
|
if s.hppRepo == nil {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceProjectFlockID, transferTotalQty, err := s.hppRepo.GetTransferSourceSummary(context.Background(), projectFlockKandangId)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
kandangIDsGrowing, err := s.hppRepo.GetProjectFlockKandangIDs(context.Background(), sourceProjectFlockID)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
totalPopulationFlockGrowing, err := s.hppRepo.GetTotalPopulation(context.Background(), kandangIDsGrowing)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if totalPopulationFlockGrowing == 0 {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
totalDepresiasiFlockGrowing, err := s.GetTotalDepresiasiFlockGrowing(sourceProjectFlockID, endDate)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return (totalDepresiasiFlockGrowing * transferTotalQty) / totalPopulationFlockGrowing, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *hppService) GetHppEstimationDanRealisasi(totalProductionCost float64, projectFlockKandangId uint, startDate *time.Time, endDate *time.Time) (*HppCostResponse, error) {
|
||||||
|
|
||||||
|
if s.hppRepo == nil {
|
||||||
|
return &HppCostResponse{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
estimPieces, estimWeightKg, err := s.hppRepo.GetEggProduksiPiecesAndWeightKgByProjectFlockKandangIds(context.Background(), []uint{projectFlockKandangId}, endDate)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
realPieces, realWeightKg, err := s.hppRepo.GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(context.Background(), []uint{projectFlockKandangId}, startDate, endDate)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
estimation := HppCostDetail{
|
||||||
|
Total: totalProductionCost,
|
||||||
|
Kg: estimWeightKg,
|
||||||
|
Butir: estimPieces,
|
||||||
|
}
|
||||||
|
if estimWeightKg > 0 {
|
||||||
|
estimation.HargaKg = roundToTwoDecimals(totalProductionCost / estimWeightKg)
|
||||||
|
}
|
||||||
|
if estimPieces > 0 {
|
||||||
|
estimation.HargaButir = roundToTwoDecimals(totalProductionCost / estimPieces)
|
||||||
|
}
|
||||||
|
|
||||||
|
real := HppCostDetail{
|
||||||
|
Total: totalProductionCost,
|
||||||
|
Kg: realWeightKg,
|
||||||
|
Butir: realPieces,
|
||||||
|
}
|
||||||
|
if realWeightKg > 0 {
|
||||||
|
real.HargaKg = roundToTwoDecimals(totalProductionCost / realWeightKg)
|
||||||
|
}
|
||||||
|
if realPieces > 0 {
|
||||||
|
real.HargaButir = roundToTwoDecimals(totalProductionCost / realPieces)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &HppCostResponse{
|
||||||
|
Estimation: estimation,
|
||||||
|
Real: real,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func roundToTwoDecimals(value float64) float64 {
|
||||||
|
return math.Round(value*100) / 100
|
||||||
|
}
|
||||||
@@ -54,12 +54,14 @@ var (
|
|||||||
SSOAuthorizeURL string
|
SSOAuthorizeURL string
|
||||||
SSOTokenURL string
|
SSOTokenURL string
|
||||||
SSOGetMeURL string
|
SSOGetMeURL string
|
||||||
|
SSOPortalURL string
|
||||||
SSOClients map[string]SSOClientConfig
|
SSOClients map[string]SSOClientConfig
|
||||||
SSOAccessCookieName string
|
SSOAccessCookieName string
|
||||||
SSORefreshCookieName string
|
SSORefreshCookieName string
|
||||||
SSOCookieDomain string
|
SSOCookieDomain string
|
||||||
SSOCookieSecure bool
|
SSOCookieSecure bool
|
||||||
SSOCookieSameSite string
|
SSOCookieSameSite string
|
||||||
|
SSOAccessTokenMaxBytes int
|
||||||
SSOTokenBlacklistPrefix string
|
SSOTokenBlacklistPrefix string
|
||||||
SSOPKCETTL time.Duration
|
SSOPKCETTL time.Duration
|
||||||
SSOUserSyncDrift time.Duration
|
SSOUserSyncDrift time.Duration
|
||||||
@@ -72,6 +74,7 @@ var (
|
|||||||
S3SecretKey string
|
S3SecretKey string
|
||||||
S3ForcePathStyle bool
|
S3ForcePathStyle bool
|
||||||
S3PublicBaseURL string
|
S3PublicBaseURL string
|
||||||
|
S3EnvPrefix string
|
||||||
S3DocumentKeyPrefix string
|
S3DocumentKeyPrefix string
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -122,7 +125,12 @@ func init() {
|
|||||||
S3SecretKey = strings.TrimSpace(viper.GetString("S3_SECRET_KEY"))
|
S3SecretKey = strings.TrimSpace(viper.GetString("S3_SECRET_KEY"))
|
||||||
S3ForcePathStyle = viper.GetBool("S3_FORCE_PATH_STYLE")
|
S3ForcePathStyle = viper.GetBool("S3_FORCE_PATH_STYLE")
|
||||||
S3PublicBaseURL = strings.TrimSuffix(strings.TrimSpace(viper.GetString("S3_PUBLIC_BASE_URL")), "/")
|
S3PublicBaseURL = strings.TrimSuffix(strings.TrimSpace(viper.GetString("S3_PUBLIC_BASE_URL")), "/")
|
||||||
S3DocumentKeyPrefix = defaultString(strings.Trim(strings.TrimSpace(viper.GetString("S3_DOCUMENT_PREFIX")), "/"), "docs")
|
S3EnvPrefix = defaultString(strings.Trim(strings.TrimSpace(viper.GetString("S3_ENV_PREFIX")), "/"), "local")
|
||||||
|
docPrefix := strings.Trim(strings.TrimSpace(viper.GetString("S3_DOCUMENT_PREFIX")), "/")
|
||||||
|
if docPrefix == "" {
|
||||||
|
docPrefix = "docs"
|
||||||
|
}
|
||||||
|
S3DocumentKeyPrefix = joinPath(S3EnvPrefix, docPrefix)
|
||||||
|
|
||||||
// SSO integration
|
// SSO integration
|
||||||
SSOIssuer = viper.GetString("SSO_ISSUER")
|
SSOIssuer = viper.GetString("SSO_ISSUER")
|
||||||
@@ -131,11 +139,16 @@ func init() {
|
|||||||
SSOAuthorizeURL = viper.GetString("SSO_AUTHORIZE_URL")
|
SSOAuthorizeURL = viper.GetString("SSO_AUTHORIZE_URL")
|
||||||
SSOTokenURL = viper.GetString("SSO_TOKEN_URL")
|
SSOTokenURL = viper.GetString("SSO_TOKEN_URL")
|
||||||
SSOGetMeURL = viper.GetString("SSO_GETME_URL")
|
SSOGetMeURL = viper.GetString("SSO_GETME_URL")
|
||||||
|
SSOPortalURL = strings.TrimSpace(viper.GetString("SSO_PORTAL_URL"))
|
||||||
SSOAccessCookieName = defaultString(viper.GetString("SSO_ACCESS_COOKIE_NAME"), "sso_access")
|
SSOAccessCookieName = defaultString(viper.GetString("SSO_ACCESS_COOKIE_NAME"), "sso_access")
|
||||||
SSORefreshCookieName = defaultString(viper.GetString("SSO_REFRESH_COOKIE_NAME"), "sso_refresh")
|
SSORefreshCookieName = defaultString(viper.GetString("SSO_REFRESH_COOKIE_NAME"), "sso_refresh")
|
||||||
SSOCookieDomain = viper.GetString("SSO_COOKIE_DOMAIN")
|
SSOCookieDomain = viper.GetString("SSO_COOKIE_DOMAIN")
|
||||||
SSOCookieSecure = viper.GetBool("SSO_COOKIE_SECURE")
|
SSOCookieSecure = viper.GetBool("SSO_COOKIE_SECURE")
|
||||||
SSOCookieSameSite = defaultString(viper.GetString("SSO_COOKIE_SAMESITE"), "Lax")
|
SSOCookieSameSite = defaultString(viper.GetString("SSO_COOKIE_SAMESITE"), "Lax")
|
||||||
|
SSOAccessTokenMaxBytes = viper.GetInt("SSO_ACCESS_TOKEN_MAX_BYTES")
|
||||||
|
if SSOAccessTokenMaxBytes <= 0 {
|
||||||
|
SSOAccessTokenMaxBytes = 4096
|
||||||
|
}
|
||||||
SSOTokenBlacklistPrefix = defaultString(viper.GetString("SSO_TOKEN_BLACKLIST_PREFIX"), "sso:blacklist")
|
SSOTokenBlacklistPrefix = defaultString(viper.GetString("SSO_TOKEN_BLACKLIST_PREFIX"), "sso:blacklist")
|
||||||
if ttl := viper.GetInt("SSO_PKCE_TTL_SECONDS"); ttl > 0 {
|
if ttl := viper.GetInt("SSO_PKCE_TTL_SECONDS"); ttl > 0 {
|
||||||
SSOPKCETTL = time.Duration(ttl) * time.Second
|
SSOPKCETTL = time.Duration(ttl) * time.Second
|
||||||
@@ -240,6 +253,17 @@ func defaultString(v, def string) string {
|
|||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func joinPath(parts ...string) string {
|
||||||
|
out := make([]string, 0, len(parts))
|
||||||
|
for _, part := range parts {
|
||||||
|
part = strings.Trim(part, "/")
|
||||||
|
if part != "" {
|
||||||
|
out = append(out, part)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return strings.Join(out, "/")
|
||||||
|
}
|
||||||
|
|
||||||
func ensureProdConfig() {
|
func ensureProdConfig() {
|
||||||
if SSOAuthorizeURL == "" || !strings.HasPrefix(SSOAuthorizeURL, "https://") {
|
if SSOAuthorizeURL == "" || !strings.HasPrefix(SSOAuthorizeURL, "https://") {
|
||||||
panic("SSO_AUTHORIZE_URL must be https in production")
|
panic("SSO_AUTHORIZE_URL must be https in production")
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ func FiberConfig() fiber.Config {
|
|||||||
CaseSensitive: true,
|
CaseSensitive: true,
|
||||||
ServerHeader: "Fiber",
|
ServerHeader: "Fiber",
|
||||||
AppName: "Fiber API",
|
AppName: "Fiber API",
|
||||||
|
BodyLimit: 8 * 1024 * 1024,
|
||||||
ErrorHandler: utils.ErrorHandler,
|
ErrorHandler: utils.ErrorHandler,
|
||||||
JSONEncoder: sonic.Marshal,
|
JSONEncoder: sonic.Marshal,
|
||||||
JSONDecoder: sonic.Unmarshal,
|
JSONDecoder: sonic.Unmarshal,
|
||||||
|
|||||||
@@ -1 +1,2 @@
|
|||||||
DROP TABLE IF EXISTS expenses;
|
DROP SEQUENCE IF EXISTS expenses_ref_seq;
|
||||||
|
DROP TABLE IF EXISTS expenses;
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
-- Drop function and sequence for sales order numbers
|
-- Drop function and sequence for sales order numbers
|
||||||
DROP FUNCTION IF EXISTS generate_so_number();
|
|
||||||
DROP SEQUENCE IF EXISTS so_number_seq;
|
DROP SEQUENCE IF EXISTS so_number_seq;
|
||||||
|
DROP FUNCTION IF EXISTS generate_so_number();
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
DROP TABLE IF EXISTS daily_checklist_tasks;
|
-- Drop tables in correct order (child tables before parent tables)
|
||||||
|
DROP TABLE IF EXISTS daily_checklist_activity_task_assignments; -- Child table with FK to daily_checklist_activity_tasks
|
||||||
DROP TABLE IF EXISTS daily_checklist_activity_task_assignees;
|
DROP TABLE IF EXISTS daily_checklist_activity_task_assignees;
|
||||||
DROP TABLE IF EXISTS daily_checklist_activity_tasks;
|
DROP TABLE IF EXISTS daily_checklist_activity_tasks;
|
||||||
|
DROP TABLE IF EXISTS daily_checklist_tasks;
|
||||||
DROP TABLE IF EXISTS daily_checklist_phases;
|
DROP TABLE IF EXISTS daily_checklist_phases;
|
||||||
DROP TABLE IF EXISTS daily_checklists;
|
DROP TABLE IF EXISTS daily_checklists;
|
||||||
DROP TABLE IF EXISTS checklists;
|
DROP TABLE IF EXISTS checklists;
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
BEGIN;
|
||||||
|
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM pg_constraint
|
||||||
|
WHERE conname = 'fk_recordings_project_flock_kandang'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE recordings
|
||||||
|
DROP CONSTRAINT fk_recordings_project_flock_kandang;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
ALTER TABLE recordings
|
||||||
|
ADD CONSTRAINT fk_recordings_project_flock_kandang
|
||||||
|
FOREIGN KEY (project_flock_kandangs_id)
|
||||||
|
REFERENCES project_flock_kandangs (id)
|
||||||
|
ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
BEGIN;
|
||||||
|
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM pg_constraint
|
||||||
|
WHERE conname = 'fk_recordings_project_flock_kandang'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE recordings
|
||||||
|
DROP CONSTRAINT fk_recordings_project_flock_kandang;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
ALTER TABLE recordings
|
||||||
|
ADD CONSTRAINT fk_recordings_project_flock_kandang
|
||||||
|
FOREIGN KEY (project_flock_kandangs_id)
|
||||||
|
REFERENCES project_flock_kandangs (id)
|
||||||
|
ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
-- Revert back to NO ACTION (RESTRICT behavior)
|
||||||
|
ALTER TABLE expense_nonstocks DROP CONSTRAINT IF EXISTS fk_expense_nonstocks_expense_id;
|
||||||
|
|
||||||
|
ALTER TABLE expense_nonstocks
|
||||||
|
ADD CONSTRAINT fk_expense_nonstocks_expense_id
|
||||||
|
FOREIGN KEY (expense_id) REFERENCES expenses(id)
|
||||||
|
ON DELETE NO ACTION;
|
||||||
|
|
||||||
|
-- Revert expense_realizations FK
|
||||||
|
ALTER TABLE expense_realizations DROP CONSTRAINT IF EXISTS fk_expense_realizations_nonstock_id;
|
||||||
|
|
||||||
|
ALTER TABLE expense_realizations
|
||||||
|
ADD CONSTRAINT fk_expense_realizations_nonstock_id
|
||||||
|
FOREIGN KEY (expense_nonstock_id) REFERENCES expense_nonstocks(id)
|
||||||
|
ON DELETE NO ACTION;
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
-- Drop existing FK constraints
|
||||||
|
ALTER TABLE expense_nonstocks DROP CONSTRAINT IF EXISTS fk_expense_nonstocks_expense_id;
|
||||||
|
|
||||||
|
-- Recreate with ON DELETE CASCADE
|
||||||
|
ALTER TABLE expense_nonstocks
|
||||||
|
ADD CONSTRAINT fk_expense_nonstocks_expense_id
|
||||||
|
FOREIGN KEY (expense_id) REFERENCES expenses(id)
|
||||||
|
ON DELETE CASCADE;
|
||||||
|
|
||||||
|
-- Drop and recreate expense_realizations FK
|
||||||
|
ALTER TABLE expense_realizations DROP CONSTRAINT IF EXISTS fk_expense_realizations_nonstock_id;
|
||||||
|
|
||||||
|
ALTER TABLE expense_realizations
|
||||||
|
ADD CONSTRAINT fk_expense_realizations_nonstock_id
|
||||||
|
FOREIGN KEY (expense_nonstock_id) REFERENCES expense_nonstocks(id)
|
||||||
|
ON DELETE CASCADE;
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
-- Revert back to NO ACTION (for rollback safety)
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
ALTER TABLE marketing_products DROP CONSTRAINT IF EXISTS fk_marketing_products_marketing_id;
|
||||||
|
|
||||||
|
ALTER TABLE marketing_products
|
||||||
|
ADD CONSTRAINT fk_marketing_products_marketing_id
|
||||||
|
FOREIGN KEY (marketing_id) REFERENCES marketings(id)
|
||||||
|
ON DELETE NO ACTION;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
ALTER TABLE marketing_delivery_products DROP CONSTRAINT IF EXISTS fk_marketing_delivery_products_marketing_product_id;
|
||||||
|
|
||||||
|
ALTER TABLE marketing_delivery_products
|
||||||
|
ADD CONSTRAINT fk_marketing_delivery_products_marketing_product_id
|
||||||
|
FOREIGN KEY (marketing_product_id) REFERENCES marketing_products(id)
|
||||||
|
ON DELETE NO ACTION;
|
||||||
|
END $$;
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
-- Ensure marketing_products FK is CASCADE (it should already be, but let's make sure)
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
-- Drop existing FK if exists
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1 FROM pg_constraint
|
||||||
|
WHERE conname = 'fk_marketing_products_marketing_id'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE marketing_products DROP CONSTRAINT fk_marketing_products_marketing_id;
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
-- Recreate with ON DELETE CASCADE
|
||||||
|
ALTER TABLE marketing_products
|
||||||
|
ADD CONSTRAINT fk_marketing_products_marketing_id
|
||||||
|
FOREIGN KEY (marketing_id) REFERENCES marketings(id)
|
||||||
|
ON DELETE CASCADE;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
-- Ensure marketing_delivery_products FK is CASCADE
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
-- Drop existing FK if exists
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1 FROM pg_constraint
|
||||||
|
WHERE conname = 'fk_marketing_delivery_products_marketing_product_id'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE marketing_delivery_products DROP CONSTRAINT fk_marketing_delivery_products_marketing_product_id;
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
-- Recreate with ON DELETE CASCADE
|
||||||
|
ALTER TABLE marketing_delivery_products
|
||||||
|
ADD CONSTRAINT fk_marketing_delivery_products_marketing_product_id
|
||||||
|
FOREIGN KEY (marketing_product_id) REFERENCES marketing_products(id)
|
||||||
|
ON DELETE CASCADE;
|
||||||
|
END $$;
|
||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
-- Drop foreign key and column
|
||||||
|
ALTER TABLE laying_transfers
|
||||||
|
DROP CONSTRAINT IF EXISTS fk_laying_transfers_product_warehouse_id;
|
||||||
|
|
||||||
|
ALTER TABLE laying_transfers
|
||||||
|
DROP COLUMN IF EXISTS product_warehouse_id;
|
||||||
|
|
||||||
|
-- Drop index
|
||||||
|
DROP INDEX IF EXISTS idx_laying_transfers_product_warehouse_id;
|
||||||
+19
@@ -0,0 +1,19 @@
|
|||||||
|
-- Add product_warehouse_id to laying_transfers for FIFO support
|
||||||
|
ALTER TABLE laying_transfers
|
||||||
|
ADD COLUMN product_warehouse_id BIGINT;
|
||||||
|
|
||||||
|
-- Add foreign key
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'product_warehouses') THEN
|
||||||
|
ALTER TABLE laying_transfers
|
||||||
|
ADD CONSTRAINT fk_laying_transfers_product_warehouse_id
|
||||||
|
FOREIGN KEY (product_warehouse_id)
|
||||||
|
REFERENCES product_warehouses(id)
|
||||||
|
ON DELETE SET NULL;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
-- Add index
|
||||||
|
CREATE INDEX idx_laying_transfers_product_warehouse_id
|
||||||
|
ON laying_transfers(product_warehouse_id);
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
-- Rollback: Remove STOCKABLE fields from laying_transfers
|
||||||
|
|
||||||
|
-- Drop index
|
||||||
|
DROP INDEX IF EXISTS idx_laying_transfers_dest_product_warehouse_id;
|
||||||
|
|
||||||
|
-- Drop foreign key constraint
|
||||||
|
ALTER TABLE laying_transfers
|
||||||
|
DROP CONSTRAINT IF EXISTS fk_laying_transfers_dest_product_warehouse_id;
|
||||||
|
|
||||||
|
-- Drop columns
|
||||||
|
ALTER TABLE laying_transfers
|
||||||
|
DROP COLUMN IF EXISTS dest_product_warehouse_id,
|
||||||
|
DROP COLUMN IF EXISTS total_qty,
|
||||||
|
DROP COLUMN IF EXISTS total_used;
|
||||||
+30
@@ -0,0 +1,30 @@
|
|||||||
|
-- Add STOCKABLE fields to laying_transfers for destination warehouse
|
||||||
|
-- This enables Transfer to Laying to work as DUAL ROLE (Stockable + Usable)
|
||||||
|
|
||||||
|
-- Add columns for STOCKABLE role (destination warehouse)
|
||||||
|
ALTER TABLE laying_transfers
|
||||||
|
ADD COLUMN dest_product_warehouse_id BIGINT,
|
||||||
|
ADD COLUMN total_qty NUMERIC(15, 3) DEFAULT 0,
|
||||||
|
ADD COLUMN total_used NUMERIC(15, 3) DEFAULT 0;
|
||||||
|
|
||||||
|
-- Add foreign key constraint
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'product_warehouses') THEN
|
||||||
|
ALTER TABLE laying_transfers
|
||||||
|
ADD CONSTRAINT fk_laying_transfers_dest_product_warehouse_id
|
||||||
|
FOREIGN KEY (dest_product_warehouse_id)
|
||||||
|
REFERENCES product_warehouses(id)
|
||||||
|
ON DELETE SET NULL;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
-- Add index for performance
|
||||||
|
CREATE INDEX idx_laying_transfers_dest_product_warehouse_id
|
||||||
|
ON laying_transfers(dest_product_warehouse_id);
|
||||||
|
|
||||||
|
-- Add comment for documentation
|
||||||
|
COMMENT ON COLUMN laying_transfers.product_warehouse_id IS 'Product warehouse at source (Growing flock) - for USABLE role';
|
||||||
|
COMMENT ON COLUMN laying_transfers.dest_product_warehouse_id IS 'Product warehouse at destination (Laying flock) - for STOCKABLE role';
|
||||||
|
COMMENT ON COLUMN laying_transfers.total_qty IS 'Total lot quantity introduced to destination warehouse - for STOCKABLE role';
|
||||||
|
COMMENT ON COLUMN laying_transfers.total_used IS 'Quantity already consumed from this lot at destination - for STOCKABLE role';
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
-- Remove chart_data, uniform_date, and related indexes
|
||||||
|
DROP INDEX IF EXISTS idx_project_flock_kandang_uniformity_uniform_date;
|
||||||
|
DROP INDEX IF EXISTS idx_project_flock_kandang_uniformity_unique;
|
||||||
|
|
||||||
|
ALTER TABLE project_flock_kandang_uniformity
|
||||||
|
DROP COLUMN IF EXISTS chart_data,
|
||||||
|
DROP COLUMN IF EXISTS uniform_date;
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
-- Add uniform_date (if missing), chart_data, and unique constraint for uniformity records
|
||||||
|
ALTER TABLE project_flock_kandang_uniformity
|
||||||
|
ADD COLUMN IF NOT EXISTS uniform_date TIMESTAMPTZ,
|
||||||
|
ADD COLUMN IF NOT EXISTS chart_data JSONB;
|
||||||
|
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM information_schema.columns
|
||||||
|
WHERE table_schema = 'public'
|
||||||
|
AND table_name = 'project_flock_kandang_uniformity'
|
||||||
|
AND column_name = 'deleted_at'
|
||||||
|
) THEN
|
||||||
|
CREATE UNIQUE INDEX IF NOT EXISTS idx_project_flock_kandang_uniformity_unique
|
||||||
|
ON project_flock_kandang_uniformity (project_flock_kandang_id, week, uniform_date)
|
||||||
|
WHERE deleted_at IS NULL;
|
||||||
|
ELSE
|
||||||
|
CREATE UNIQUE INDEX IF NOT EXISTS idx_project_flock_kandang_uniformity_unique
|
||||||
|
ON project_flock_kandang_uniformity (project_flock_kandang_id, week, uniform_date);
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_project_flock_kandang_uniformity_uniform_date
|
||||||
|
ON project_flock_kandang_uniformity (uniform_date);
|
||||||
+4
@@ -0,0 +1,4 @@
|
|||||||
|
-- Remove expense_nonstock_id from stock_transfer_details
|
||||||
|
ALTER TABLE stock_transfer_details DROP CONSTRAINT IF EXISTS fk_stock_transfer_details_expense_nonstock;
|
||||||
|
ALTER TABLE stock_transfer_details DROP COLUMN IF EXISTS expense_nonstock_id;
|
||||||
|
DROP INDEX IF EXISTS idx_stock_transfer_details_expense_nonstock_id;
|
||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
-- Add expense_nonstock_id to stock_transfer_details
|
||||||
|
-- This allows tracking expedition/transport costs for stock transfers (same as purchase)
|
||||||
|
|
||||||
|
ALTER TABLE stock_transfer_details
|
||||||
|
ADD COLUMN expense_nonstock_id BIGINT,
|
||||||
|
ADD CONSTRAINT fk_stock_transfer_details_expense_nonstock
|
||||||
|
FOREIGN KEY (expense_nonstock_id) REFERENCES expense_nonstocks(id) ON DELETE SET NULL;
|
||||||
|
|
||||||
|
-- Create index for better query performance
|
||||||
|
CREATE INDEX idx_stock_transfer_details_expense_nonstock_id ON stock_transfer_details(expense_nonstock_id);
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
BEGIN;
|
||||||
|
|
||||||
|
ALTER TABLE payments
|
||||||
|
DROP COLUMN IF EXISTS party_account_number;
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
BEGIN;
|
||||||
|
|
||||||
|
ALTER TABLE payments
|
||||||
|
ADD COLUMN IF NOT EXISTS party_account_number VARCHAR(50);
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
DROP TABLE IF EXISTS projects;
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
DROP TABLE IF EXISTS projects;
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
-- Revert master data foreign keys to CASCADE delete (except FCR)
|
||||||
|
ALTER TABLE nonstock_suppliers
|
||||||
|
DROP CONSTRAINT IF EXISTS nonstock_suppliers_nonstock_id_fkey,
|
||||||
|
DROP CONSTRAINT IF EXISTS nonstock_suppliers_supplier_id_fkey;
|
||||||
|
|
||||||
|
ALTER TABLE nonstock_suppliers
|
||||||
|
ADD CONSTRAINT nonstock_suppliers_nonstock_id_fkey FOREIGN KEY (nonstock_id)
|
||||||
|
REFERENCES nonstocks (id) ON DELETE CASCADE ON UPDATE CASCADE,
|
||||||
|
ADD CONSTRAINT nonstock_suppliers_supplier_id_fkey FOREIGN KEY (supplier_id)
|
||||||
|
REFERENCES suppliers (id) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
ALTER TABLE product_suppliers
|
||||||
|
DROP CONSTRAINT IF EXISTS product_suppliers_product_id_fkey,
|
||||||
|
DROP CONSTRAINT IF EXISTS product_suppliers_supplier_id_fkey;
|
||||||
|
|
||||||
|
ALTER TABLE product_suppliers
|
||||||
|
ADD CONSTRAINT product_suppliers_product_id_fkey FOREIGN KEY (product_id)
|
||||||
|
REFERENCES products (id) ON DELETE CASCADE ON UPDATE CASCADE,
|
||||||
|
ADD CONSTRAINT product_suppliers_supplier_id_fkey FOREIGN KEY (supplier_id)
|
||||||
|
REFERENCES suppliers (id) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
-- Update master data foreign keys to RESTRICT delete (except FCR)
|
||||||
|
ALTER TABLE nonstock_suppliers
|
||||||
|
DROP CONSTRAINT IF EXISTS nonstock_suppliers_nonstock_id_fkey,
|
||||||
|
DROP CONSTRAINT IF EXISTS nonstock_suppliers_supplier_id_fkey;
|
||||||
|
|
||||||
|
ALTER TABLE nonstock_suppliers
|
||||||
|
ADD CONSTRAINT nonstock_suppliers_nonstock_id_fkey FOREIGN KEY (nonstock_id)
|
||||||
|
REFERENCES nonstocks (id) ON DELETE RESTRICT ON UPDATE CASCADE,
|
||||||
|
ADD CONSTRAINT nonstock_suppliers_supplier_id_fkey FOREIGN KEY (supplier_id)
|
||||||
|
REFERENCES suppliers (id) ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
ALTER TABLE product_suppliers
|
||||||
|
DROP CONSTRAINT IF EXISTS product_suppliers_product_id_fkey,
|
||||||
|
DROP CONSTRAINT IF EXISTS product_suppliers_supplier_id_fkey;
|
||||||
|
|
||||||
|
ALTER TABLE product_suppliers
|
||||||
|
ADD CONSTRAINT product_suppliers_product_id_fkey FOREIGN KEY (product_id)
|
||||||
|
REFERENCES products (id) ON DELETE RESTRICT ON UPDATE CASCADE,
|
||||||
|
ADD CONSTRAINT product_suppliers_supplier_id_fkey FOREIGN KEY (supplier_id)
|
||||||
|
REFERENCES suppliers (id) ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||||
+79
@@ -0,0 +1,79 @@
|
|||||||
|
-- Rollback: Revert FIFO fields back to laying_transfers from detail tables
|
||||||
|
|
||||||
|
-- ============================================================================
|
||||||
|
-- PART 1: Remove FIFO columns from detail tables
|
||||||
|
-- ============================================================================
|
||||||
|
|
||||||
|
-- Add back old qty column first
|
||||||
|
ALTER TABLE laying_transfer_sources
|
||||||
|
ADD COLUMN IF NOT EXISTS qty NUMERIC(15, 3) NOT NULL DEFAULT 0;
|
||||||
|
|
||||||
|
ALTER TABLE laying_transfer_targets
|
||||||
|
ADD COLUMN IF NOT EXISTS qty NUMERIC(15, 3) NOT NULL DEFAULT 0;
|
||||||
|
|
||||||
|
-- Now drop FIFO columns
|
||||||
|
ALTER TABLE laying_transfer_sources
|
||||||
|
DROP COLUMN IF EXISTS usage_qty,
|
||||||
|
DROP COLUMN IF EXISTS pending_usage_qty;
|
||||||
|
|
||||||
|
ALTER TABLE laying_transfer_targets
|
||||||
|
DROP COLUMN IF EXISTS total_qty,
|
||||||
|
DROP COLUMN IF EXISTS total_used;
|
||||||
|
|
||||||
|
-- ============================================================================
|
||||||
|
-- PART 2: Add back FIFO columns to laying_transfers table
|
||||||
|
-- ============================================================================
|
||||||
|
|
||||||
|
-- Add columns back for USABLE role (source warehouse)
|
||||||
|
ALTER TABLE laying_transfers
|
||||||
|
ADD COLUMN product_warehouse_id BIGINT,
|
||||||
|
ADD COLUMN pending_usage_qty NUMERIC(15, 3),
|
||||||
|
ADD COLUMN usage_qty NUMERIC(15, 3);
|
||||||
|
|
||||||
|
-- Add columns back for STOCKABLE role (destination warehouse)
|
||||||
|
ALTER TABLE laying_transfers
|
||||||
|
ADD COLUMN dest_product_warehouse_id BIGINT,
|
||||||
|
ADD COLUMN total_qty NUMERIC(15, 3) DEFAULT 0 NOT NULL,
|
||||||
|
ADD COLUMN total_used NUMERIC(15, 3) DEFAULT 0 NOT NULL;
|
||||||
|
|
||||||
|
-- ============================================================================
|
||||||
|
-- PART 3: Recreate foreign key constraints
|
||||||
|
-- ============================================================================
|
||||||
|
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'product_warehouses') THEN
|
||||||
|
-- Add source product warehouse FK
|
||||||
|
ALTER TABLE laying_transfers
|
||||||
|
ADD CONSTRAINT fk_laying_transfers_product_warehouse_id
|
||||||
|
FOREIGN KEY (product_warehouse_id)
|
||||||
|
REFERENCES product_warehouses(id)
|
||||||
|
ON DELETE SET NULL;
|
||||||
|
|
||||||
|
-- Add destination product warehouse FK
|
||||||
|
ALTER TABLE laying_transfers
|
||||||
|
ADD CONSTRAINT fk_laying_transfers_dest_product_warehouse_id
|
||||||
|
FOREIGN KEY (dest_product_warehouse_id)
|
||||||
|
REFERENCES product_warehouses(id)
|
||||||
|
ON DELETE SET NULL;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
-- ============================================================================
|
||||||
|
-- PART 4: Recreate indexes for performance
|
||||||
|
-- ============================================================================
|
||||||
|
|
||||||
|
CREATE INDEX idx_laying_transfers_product_warehouse_id
|
||||||
|
ON laying_transfers(product_warehouse_id);
|
||||||
|
|
||||||
|
CREATE INDEX idx_laying_transfers_dest_product_warehouse_id
|
||||||
|
ON laying_transfers(dest_product_warehouse_id);
|
||||||
|
|
||||||
|
-- ============================================================================
|
||||||
|
-- PART 5: Recreate comments for documentation
|
||||||
|
-- ============================================================================
|
||||||
|
|
||||||
|
COMMENT ON COLUMN laying_transfers.product_warehouse_id IS 'Product warehouse at source (Growing flock) - for USABLE role';
|
||||||
|
COMMENT ON COLUMN laying_transfers.dest_product_warehouse_id IS 'Product warehouse at destination (Laying flock) - for STOCKABLE role';
|
||||||
|
COMMENT ON COLUMN laying_transfers.total_qty IS 'Total lot quantity introduced to destination warehouse - for STOCKABLE role';
|
||||||
|
COMMENT ON COLUMN laying_transfers.total_used IS 'Quantity already consumed from this lot at destination - for FIFO STOCKABLE role';
|
||||||
+73
@@ -0,0 +1,73 @@
|
|||||||
|
-- Move FIFO fields from laying_transfers to detail tables (sources & targets)
|
||||||
|
-- This enables proper FIFO integration for transfer laying with multiple sources and targets
|
||||||
|
|
||||||
|
-- ============================================================================
|
||||||
|
-- PART 1: Remove FIFO-related columns from laying_transfers table
|
||||||
|
-- ============================================================================
|
||||||
|
|
||||||
|
-- Drop foreign key constraints first
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
-- Drop source product warehouse FK
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1 FROM pg_constraint
|
||||||
|
WHERE conname = 'fk_laying_transfers_product_warehouse_id'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE laying_transfers
|
||||||
|
DROP CONSTRAINT fk_laying_transfers_product_warehouse_id;
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
-- Drop destination product warehouse FK
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1 FROM pg_constraint
|
||||||
|
WHERE conname = 'fk_laying_transfers_dest_product_warehouse_id'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE laying_transfers
|
||||||
|
DROP CONSTRAINT fk_laying_transfers_dest_product_warehouse_id;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
-- Drop indexes
|
||||||
|
DROP INDEX IF EXISTS idx_laying_transfers_product_warehouse_id;
|
||||||
|
DROP INDEX IF EXISTS idx_laying_transfers_dest_product_warehouse_id;
|
||||||
|
|
||||||
|
-- Remove columns from laying_transfers
|
||||||
|
ALTER TABLE laying_transfers
|
||||||
|
DROP COLUMN IF EXISTS product_warehouse_id,
|
||||||
|
DROP COLUMN IF EXISTS dest_product_warehouse_id,
|
||||||
|
DROP COLUMN IF EXISTS pending_usage_qty,
|
||||||
|
DROP COLUMN IF EXISTS usage_qty,
|
||||||
|
DROP COLUMN IF EXISTS total_qty,
|
||||||
|
DROP COLUMN IF EXISTS total_used;
|
||||||
|
|
||||||
|
-- ============================================================================
|
||||||
|
-- PART 2: Add FIFO columns to laying_transfer_sources (USABLE role)
|
||||||
|
-- ============================================================================
|
||||||
|
|
||||||
|
ALTER TABLE laying_transfer_sources
|
||||||
|
ADD COLUMN usage_qty NUMERIC(15, 3) DEFAULT 0 NOT NULL,
|
||||||
|
ADD COLUMN pending_usage_qty NUMERIC(15, 3) DEFAULT 0 NOT NULL;
|
||||||
|
|
||||||
|
-- Add comments for documentation
|
||||||
|
COMMENT ON COLUMN laying_transfer_sources.usage_qty IS 'Quantity consumed from this source - for FIFO USABLE role';
|
||||||
|
COMMENT ON COLUMN laying_transfer_sources.pending_usage_qty IS 'Quantity pending to consume from this source - for FIFO USABLE role';
|
||||||
|
|
||||||
|
-- Drop old qty column as it's replaced by usage_qty
|
||||||
|
ALTER TABLE laying_transfer_sources
|
||||||
|
DROP COLUMN IF EXISTS qty;
|
||||||
|
|
||||||
|
-- ============================================================================
|
||||||
|
-- PART 3: Add FIFO columns to laying_transfer_targets (STOCKABLE role)
|
||||||
|
-- ============================================================================
|
||||||
|
|
||||||
|
ALTER TABLE laying_transfer_targets
|
||||||
|
ADD COLUMN total_qty NUMERIC(15, 3) DEFAULT 0 NOT NULL,
|
||||||
|
ADD COLUMN total_used NUMERIC(15, 3) DEFAULT 0 NOT NULL;
|
||||||
|
|
||||||
|
-- Add comments for documentation
|
||||||
|
COMMENT ON COLUMN laying_transfer_targets.total_qty IS 'Total lot quantity introduced to this target warehouse - for FIFO STOCKABLE role';
|
||||||
|
COMMENT ON COLUMN laying_transfer_targets.total_used IS 'Quantity already consumed from this lot at target warehouse - for FIFO STOCKABLE role';
|
||||||
|
|
||||||
|
-- Drop old qty column as it's replaced by total_qty
|
||||||
|
ALTER TABLE laying_transfer_targets
|
||||||
|
DROP COLUMN IF EXISTS qty;
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
BEGIN;
|
||||||
|
|
||||||
|
ALTER TABLE recordings
|
||||||
|
DROP CONSTRAINT IF EXISTS chk_recordings_nonnegatives_v4;
|
||||||
|
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM information_schema.columns
|
||||||
|
WHERE table_name = 'recordings' AND column_name = 'hen_day'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE recordings RENAME COLUMN hen_day TO hand_day;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM information_schema.columns
|
||||||
|
WHERE table_name = 'recordings' AND column_name = 'hen_house'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE recordings RENAME COLUMN hen_house TO hand_house;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM information_schema.columns
|
||||||
|
WHERE table_name = 'recordings' AND column_name = 'egg_mass'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE recordings RENAME COLUMN egg_mass TO egg_mesh;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
ALTER TABLE recordings
|
||||||
|
ADD COLUMN IF NOT EXISTS daily_gain NUMERIC(7,3),
|
||||||
|
ADD COLUMN IF NOT EXISTS avg_daily_gain NUMERIC(7,3);
|
||||||
|
|
||||||
|
ALTER TABLE recordings
|
||||||
|
ADD CONSTRAINT chk_recordings_nonnegatives_v3 CHECK (
|
||||||
|
(total_depletion_qty IS NULL OR total_depletion_qty >= 0) AND
|
||||||
|
(cum_depletion_rate IS NULL OR cum_depletion_rate >= 0) AND
|
||||||
|
(daily_gain IS NULL OR daily_gain >= 0) AND
|
||||||
|
(avg_daily_gain IS NULL OR avg_daily_gain >= 0) AND
|
||||||
|
(cum_intake IS NULL OR cum_intake >= 0) AND
|
||||||
|
(fcr_value IS NULL OR fcr_value >= 0) AND
|
||||||
|
(total_chick_qty IS NULL OR total_chick_qty >= 0) AND
|
||||||
|
(hand_day IS NULL OR hand_day >= 0) AND
|
||||||
|
(hand_house IS NULL OR hand_house >= 0) AND
|
||||||
|
(feed_intake IS NULL OR feed_intake >= 0) AND
|
||||||
|
(egg_mesh IS NULL OR egg_mesh >= 0) AND
|
||||||
|
(egg_weight IS NULL OR egg_weight >= 0)
|
||||||
|
);
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
BEGIN;
|
||||||
|
|
||||||
|
ALTER TABLE recordings
|
||||||
|
DROP CONSTRAINT IF EXISTS chk_recordings_nonnegatives_v3;
|
||||||
|
|
||||||
|
ALTER TABLE recordings
|
||||||
|
DROP COLUMN IF EXISTS daily_gain,
|
||||||
|
DROP COLUMN IF EXISTS avg_daily_gain;
|
||||||
|
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM information_schema.columns
|
||||||
|
WHERE table_name = 'recordings' AND column_name = 'hand_day'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE recordings RENAME COLUMN hand_day TO hen_day;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM information_schema.columns
|
||||||
|
WHERE table_name = 'recordings' AND column_name = 'hand_house'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE recordings RENAME COLUMN hand_house TO hen_house;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM information_schema.columns
|
||||||
|
WHERE table_name = 'recordings' AND column_name = 'egg_mesh'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE recordings RENAME COLUMN egg_mesh TO egg_mass;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
ALTER TABLE recordings
|
||||||
|
ADD CONSTRAINT chk_recordings_nonnegatives_v4 CHECK (
|
||||||
|
(total_depletion_qty IS NULL OR total_depletion_qty >= 0) AND
|
||||||
|
(cum_depletion_rate IS NULL OR cum_depletion_rate >= 0) AND
|
||||||
|
(cum_intake IS NULL OR cum_intake >= 0) AND
|
||||||
|
(fcr_value IS NULL OR fcr_value >= 0) AND
|
||||||
|
(total_chick_qty IS NULL OR total_chick_qty >= 0) AND
|
||||||
|
(hen_day IS NULL OR hen_day >= 0) AND
|
||||||
|
(hen_house IS NULL OR hen_house >= 0) AND
|
||||||
|
(feed_intake IS NULL OR feed_intake >= 0) AND
|
||||||
|
(egg_mass IS NULL OR egg_mass >= 0) AND
|
||||||
|
(egg_weight IS NULL OR egg_weight >= 0)
|
||||||
|
);
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
-- Rollback: remove price from supplier relations
|
||||||
|
ALTER TABLE product_suppliers
|
||||||
|
DROP COLUMN IF EXISTS price;
|
||||||
|
|
||||||
|
ALTER TABLE nonstock_suppliers
|
||||||
|
DROP COLUMN IF EXISTS price;
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
-- Migration: add price to supplier relations
|
||||||
|
ALTER TABLE product_suppliers
|
||||||
|
ADD COLUMN IF NOT EXISTS price NUMERIC(15, 3) NOT NULL DEFAULT 0;
|
||||||
|
|
||||||
|
ALTER TABLE nonstock_suppliers
|
||||||
|
ADD COLUMN IF NOT EXISTS price NUMERIC(15, 3) NOT NULL DEFAULT 0;
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
ALTER TABLE recording_eggs
|
||||||
|
DROP COLUMN IF EXISTS total_used,
|
||||||
|
DROP COLUMN IF EXISTS total_qty;
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
ALTER TABLE recording_eggs
|
||||||
|
ADD COLUMN total_qty NUMERIC(15, 3) DEFAULT 0 NOT NULL,
|
||||||
|
ADD COLUMN total_used NUMERIC(15, 3) DEFAULT 0 NOT NULL;
|
||||||
|
|
||||||
|
UPDATE recording_eggs
|
||||||
|
SET total_qty = qty
|
||||||
|
WHERE total_qty = 0;
|
||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
-- Rollback: add price back to nonstock_suppliers
|
||||||
|
ALTER TABLE nonstock_suppliers
|
||||||
|
ADD COLUMN IF NOT EXISTS price NUMERIC(15, 3) NOT NULL DEFAULT 0;
|
||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
-- Migration: remove price from nonstock_suppliers
|
||||||
|
ALTER TABLE nonstock_suppliers
|
||||||
|
DROP COLUMN IF EXISTS price;
|
||||||
+4
@@ -0,0 +1,4 @@
|
|||||||
|
-- Rollback: Remove requested_qty column from laying_transfer_sources table
|
||||||
|
|
||||||
|
ALTER TABLE laying_transfer_sources
|
||||||
|
DROP COLUMN IF EXISTS requested_qty;
|
||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
-- Add requested_qty column to laying_transfer_sources table
|
||||||
|
-- This field stores the quantity requested by user during create/update
|
||||||
|
-- Separate from UsageQty (FIFO consumed) and PendingUsageQty (FIFO pending)
|
||||||
|
|
||||||
|
ALTER TABLE laying_transfer_sources
|
||||||
|
ADD COLUMN requested_qty NUMERIC(15,3) DEFAULT 0 NOT NULL;
|
||||||
|
|
||||||
|
-- Add comment for documentation
|
||||||
|
COMMENT ON COLUMN laying_transfer_sources.requested_qty IS 'Quantity requested by user during create/update';
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
ALTER TABLE recording_depletions
|
||||||
|
DROP COLUMN IF EXISTS pending_qty,
|
||||||
|
DROP COLUMN IF EXISTS source_product_warehouse_id;
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
ALTER TABLE recording_depletions
|
||||||
|
ADD COLUMN IF NOT EXISTS pending_qty numeric(15,3) NOT NULL DEFAULT 0,
|
||||||
|
ADD COLUMN IF NOT EXISTS source_product_warehouse_id bigint;
|
||||||
|
|
||||||
|
UPDATE recording_depletions rd
|
||||||
|
SET source_product_warehouse_id = src.product_warehouse_id
|
||||||
|
FROM recordings r
|
||||||
|
JOIN LATERAL (
|
||||||
|
SELECT pfp.product_warehouse_id
|
||||||
|
FROM project_chickins pc
|
||||||
|
JOIN project_flock_populations pfp ON pfp.project_chickin_id = pc.id
|
||||||
|
WHERE pc.project_flock_kandang_id = r.project_flock_kandangs_id
|
||||||
|
ORDER BY pfp.created_at ASC, pfp.id ASC
|
||||||
|
LIMIT 1
|
||||||
|
) AS src ON true
|
||||||
|
WHERE r.id = rd.recording_id
|
||||||
|
AND rd.source_product_warehouse_id IS NULL;
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
BEGIN;
|
||||||
|
|
||||||
|
ALTER TABLE payments
|
||||||
|
ALTER COLUMN bank_id SET NOT NULL;
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
BEGIN;
|
||||||
|
|
||||||
|
ALTER TABLE payments
|
||||||
|
ALTER COLUMN bank_id DROP NOT NULL;
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
ALTER TABLE adjustment_stocks ADD COLUMN stock_log_id INTEGER;
|
||||||
|
|
||||||
|
CREATE INDEX idx_adjustment_stocks_stock_log_id ON adjustment_stocks (stock_log_id);
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
ALTER TABLE adjustment_stocks DROP COLUMN IF EXISTS stock_log_id;
|
||||||
+56
@@ -0,0 +1,56 @@
|
|||||||
|
BEGIN;
|
||||||
|
|
||||||
|
DO $$
|
||||||
|
DECLARE
|
||||||
|
t text;
|
||||||
|
seq_name text;
|
||||||
|
BEGIN
|
||||||
|
FOREACH t IN ARRAY ARRAY[
|
||||||
|
'daily_checklist_activity_task_assignments',
|
||||||
|
'daily_checklist_activity_tasks',
|
||||||
|
'daily_checklist_phases',
|
||||||
|
'daily_checklist_tasks',
|
||||||
|
'daily_checklists',
|
||||||
|
'employee_kandangs',
|
||||||
|
'employees',
|
||||||
|
'phase_activities',
|
||||||
|
'phases'
|
||||||
|
]
|
||||||
|
LOOP
|
||||||
|
-- Sequence name convention
|
||||||
|
seq_name := format('public.%I_id_seq', t);
|
||||||
|
|
||||||
|
-- 1) Drop default nextval (bigserial behavior)
|
||||||
|
EXECUTE format(
|
||||||
|
'ALTER TABLE public.%I ALTER COLUMN id DROP DEFAULT',
|
||||||
|
t
|
||||||
|
);
|
||||||
|
|
||||||
|
-- 2) Add IDENTITY back (BY DEFAULT is safer for rollback)
|
||||||
|
EXECUTE format(
|
||||||
|
'ALTER TABLE public.%I ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY',
|
||||||
|
t
|
||||||
|
);
|
||||||
|
|
||||||
|
-- 3) Detach & optionally drop sequence (safe)
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1 FROM pg_class
|
||||||
|
WHERE relkind = 'S'
|
||||||
|
AND relname = t || '_id_seq'
|
||||||
|
) THEN
|
||||||
|
EXECUTE format(
|
||||||
|
'ALTER SEQUENCE %s OWNED BY NONE',
|
||||||
|
seq_name
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Optional: drop sequence (comment if you want to keep it)
|
||||||
|
EXECUTE format(
|
||||||
|
'DROP SEQUENCE IF EXISTS %s',
|
||||||
|
seq_name
|
||||||
|
);
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
END LOOP;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
+59
@@ -0,0 +1,59 @@
|
|||||||
|
BEGIN;
|
||||||
|
|
||||||
|
DO $$
|
||||||
|
DECLARE
|
||||||
|
t text;
|
||||||
|
seq_name text;
|
||||||
|
max_id bigint;
|
||||||
|
BEGIN
|
||||||
|
FOREACH t IN ARRAY ARRAY[
|
||||||
|
'daily_checklist_activity_task_assignments',
|
||||||
|
'daily_checklist_activity_tasks',
|
||||||
|
'daily_checklist_phases',
|
||||||
|
'daily_checklist_tasks',
|
||||||
|
'daily_checklists',
|
||||||
|
'employee_kandangs',
|
||||||
|
'employees',
|
||||||
|
'phase_activities',
|
||||||
|
'phases'
|
||||||
|
]
|
||||||
|
LOOP
|
||||||
|
-- Sequence name convention: public.<table>_id_seq
|
||||||
|
seq_name := format('public.%I_id_seq', t);
|
||||||
|
|
||||||
|
-- Drop IDENTITY only if the column is identity (safe to re-run)
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM information_schema.columns
|
||||||
|
WHERE table_schema = 'public'
|
||||||
|
AND table_name = t
|
||||||
|
AND column_name = 'id'
|
||||||
|
AND is_identity = 'YES'
|
||||||
|
) THEN
|
||||||
|
EXECUTE format('ALTER TABLE public.%I ALTER COLUMN id DROP IDENTITY', t);
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
-- Ensure sequence exists
|
||||||
|
EXECUTE format('CREATE SEQUENCE IF NOT EXISTS %s', seq_name);
|
||||||
|
|
||||||
|
-- Set default like bigserial
|
||||||
|
EXECUTE format(
|
||||||
|
'ALTER TABLE public.%I ALTER COLUMN id SET DEFAULT nextval(''%s'')',
|
||||||
|
t, seq_name
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Own the sequence by the column
|
||||||
|
EXECUTE format(
|
||||||
|
'ALTER SEQUENCE %s OWNED BY public.%I.id',
|
||||||
|
seq_name, t
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Sync sequence to MAX(id) + 1 to avoid duplicate key
|
||||||
|
EXECUTE format('SELECT COALESCE(MAX(id), 0) FROM public.%I', t) INTO max_id;
|
||||||
|
|
||||||
|
EXECUTE format('SELECT setval(''%s'', $1, false)', seq_name)
|
||||||
|
USING (max_id + 1);
|
||||||
|
END LOOP;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
ALTER TABLE stock_logs
|
||||||
|
DROP COLUMN stock;
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
ALTER TABLE stock_logs
|
||||||
|
ADD COLUMN stock NUMERIC(15, 3) NOT NULL DEFAULT 0;
|
||||||
|
|
||||||
|
WITH calc AS (
|
||||||
|
SELECT
|
||||||
|
id,
|
||||||
|
SUM(COALESCE(increase, 0) - COALESCE(decrease, 0))
|
||||||
|
OVER (
|
||||||
|
PARTITION BY product_warehouse_id
|
||||||
|
ORDER BY id
|
||||||
|
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
|
||||||
|
) AS running_stock
|
||||||
|
FROM stock_logs
|
||||||
|
)
|
||||||
|
UPDATE stock_logs t
|
||||||
|
SET stock = c.running_stock
|
||||||
|
FROM calc c
|
||||||
|
WHERE t.id = c.id;
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
BEGIN;
|
||||||
|
|
||||||
|
-- Migration: revert documents.path length
|
||||||
|
ALTER TABLE documents
|
||||||
|
ALTER COLUMN path TYPE VARCHAR(50);
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
BEGIN;
|
||||||
|
|
||||||
|
-- Migration: extend documents.path length for environment prefixes
|
||||||
|
ALTER TABLE documents
|
||||||
|
ALTER COLUMN path TYPE VARCHAR(255);
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
-- Drop transfer laying sequence
|
||||||
|
DROP SEQUENCE IF EXISTS transfer_laying_seq;
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
-- Create sequence for transfer laying movement number
|
||||||
|
CREATE SEQUENCE IF NOT EXISTS transfer_laying_seq START
|
||||||
|
WITH
|
||||||
|
1 INCREMENT BY 1 MINVALUE 1 MAXVALUE 99999 NO CYCLE;
|
||||||
|
|
||||||
|
-- Set sequence starting value based on existing data (if any)
|
||||||
|
-- This prevents duplicate movement numbers if there's already data
|
||||||
|
DO $$ DECLARE max_existing INTEGER;
|
||||||
|
|
||||||
|
BEGIN
|
||||||
|
-- Check if table exists and has data
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM information_schema.tables
|
||||||
|
WHERE
|
||||||
|
table_schema = 'public'
|
||||||
|
AND table_name = 'transfer_to_layings'
|
||||||
|
) THEN
|
||||||
|
-- Get max ID from existing records
|
||||||
|
SELECT COALESCE(MAX(id), 0) INTO max_existing
|
||||||
|
FROM transfer_to_layings;
|
||||||
|
|
||||||
|
-- Set sequence to start after the highest existing ID
|
||||||
|
IF max_existing > 0 THEN PERFORM setval (
|
||||||
|
'transfer_laying_seq',
|
||||||
|
max_existing
|
||||||
|
);
|
||||||
|
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
END $$;
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
BEGIN;
|
||||||
|
|
||||||
|
ALTER TABLE adjustment_stocks
|
||||||
|
DROP COLUMN adj_number;
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
BEGIN;
|
||||||
|
|
||||||
|
ALTER TABLE adjustment_stocks
|
||||||
|
ADD COLUMN adj_number VARCHAR(255);
|
||||||
|
|
||||||
|
UPDATE adjustment_stocks
|
||||||
|
SET adj_number = CONCAT('ADJ-', LPAD(id::text, 5, '0'))
|
||||||
|
WHERE adj_number IS NULL;
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
-- Remove columns from marketing_products
|
||||||
|
ALTER TABLE marketing_products
|
||||||
|
DROP COLUMN IF EXISTS week,
|
||||||
|
DROP COLUMN IF EXISTS weight_per_convertion,
|
||||||
|
DROP COLUMN IF EXISTS convertion_unit;
|
||||||
|
|
||||||
|
-- Remove column from marketings
|
||||||
|
ALTER TABLE marketings DROP COLUMN IF EXISTS marketing_type;
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
-- Add marketing_type to marketings table
|
||||||
|
ALTER TABLE marketings
|
||||||
|
ADD COLUMN IF NOT EXISTS marketing_type VARCHAR(50);
|
||||||
|
|
||||||
|
-- Add convertion fields to marketing_products table
|
||||||
|
ALTER TABLE marketing_products
|
||||||
|
ADD COLUMN IF NOT EXISTS convertion_unit VARCHAR(20),
|
||||||
|
ADD COLUMN IF NOT EXISTS weight_per_convertion NUMERIC(15, 3),
|
||||||
|
ADD COLUMN IF NOT EXISTS week INTEGER;
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
BEGIN;
|
||||||
|
|
||||||
|
DO $$
|
||||||
|
DECLARE
|
||||||
|
fallback_fcr_id BIGINT;
|
||||||
|
BEGIN
|
||||||
|
IF NOT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM information_schema.columns
|
||||||
|
WHERE table_schema = 'public'
|
||||||
|
AND table_name = 'project_flocks'
|
||||||
|
AND column_name = 'fcr_id'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE project_flocks
|
||||||
|
ADD COLUMN fcr_id BIGINT;
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
SELECT id INTO fallback_fcr_id
|
||||||
|
FROM fcrs
|
||||||
|
ORDER BY id ASC
|
||||||
|
LIMIT 1;
|
||||||
|
|
||||||
|
IF fallback_fcr_id IS NOT NULL THEN
|
||||||
|
UPDATE project_flocks
|
||||||
|
SET fcr_id = fallback_fcr_id
|
||||||
|
WHERE fcr_id IS NULL;
|
||||||
|
|
||||||
|
ALTER TABLE project_flocks
|
||||||
|
ALTER COLUMN fcr_id SET NOT NULL;
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM pg_constraint
|
||||||
|
WHERE conname = 'project_flocks_fcr_id_fkey'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE project_flocks
|
||||||
|
DROP CONSTRAINT project_flocks_fcr_id_fkey;
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
ALTER TABLE project_flocks
|
||||||
|
ADD CONSTRAINT project_flocks_fcr_id_fkey
|
||||||
|
FOREIGN KEY (fcr_id) REFERENCES fcrs(id)
|
||||||
|
ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
BEGIN;
|
||||||
|
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM information_schema.columns
|
||||||
|
WHERE table_schema = 'public'
|
||||||
|
AND table_name = 'project_flocks'
|
||||||
|
AND column_name = 'fcr_id'
|
||||||
|
) THEN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM pg_constraint
|
||||||
|
WHERE conname = 'project_flocks_fcr_id_fkey'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE project_flocks
|
||||||
|
DROP CONSTRAINT project_flocks_fcr_id_fkey;
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
ALTER TABLE project_flocks
|
||||||
|
DROP COLUMN fcr_id;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
@@ -74,7 +74,7 @@ func seedUsers(tx *gorm.DB) (map[string]uint, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func seedUoms(tx *gorm.DB, createdBy uint) (map[string]uint, error) {
|
func seedUoms(tx *gorm.DB, createdBy uint) (map[string]uint, error) {
|
||||||
names := []string{"Kilogram", "Gram", "Liter", "Unit", "Ekor"}
|
names := []string{"Kilogram", "Gram", "Liter", "Unit", "Ekor", "Butir"}
|
||||||
result := make(map[string]uint, len(names))
|
result := make(map[string]uint, len(names))
|
||||||
|
|
||||||
for _, name := range names {
|
for _, name := range names {
|
||||||
@@ -235,7 +235,7 @@ func seedProducts(tx *gorm.DB, createdBy uint, uoms map[string]uint, categories
|
|||||||
Name: "Telur Utuh",
|
Name: "Telur Utuh",
|
||||||
Brand: "-",
|
Brand: "-",
|
||||||
Sku: "4",
|
Sku: "4",
|
||||||
Uom: "Gram",
|
Uom: "Butir",
|
||||||
Category: "Telur",
|
Category: "Telur",
|
||||||
Price: 1,
|
Price: 1,
|
||||||
Flags: []utils.FlagType{utils.FlagTelurUtuh},
|
Flags: []utils.FlagType{utils.FlagTelurUtuh},
|
||||||
@@ -245,7 +245,7 @@ func seedProducts(tx *gorm.DB, createdBy uint, uoms map[string]uint, categories
|
|||||||
Name: "Telur Pecah",
|
Name: "Telur Pecah",
|
||||||
Brand: "-",
|
Brand: "-",
|
||||||
Sku: "5",
|
Sku: "5",
|
||||||
Uom: "Gram",
|
Uom: "Butir",
|
||||||
Category: "Telur",
|
Category: "Telur",
|
||||||
Price: 1,
|
Price: 1,
|
||||||
Flags: []utils.FlagType{utils.FlagTelurPecah},
|
Flags: []utils.FlagType{utils.FlagTelurPecah},
|
||||||
@@ -255,7 +255,7 @@ func seedProducts(tx *gorm.DB, createdBy uint, uoms map[string]uint, categories
|
|||||||
Name: "Telur Putih",
|
Name: "Telur Putih",
|
||||||
Brand: "-",
|
Brand: "-",
|
||||||
Sku: "6",
|
Sku: "6",
|
||||||
Uom: "Gram",
|
Uom: "Butir",
|
||||||
Category: "Telur",
|
Category: "Telur",
|
||||||
Price: 1,
|
Price: 1,
|
||||||
Flags: []utils.FlagType{utils.FlagTelurPutih},
|
Flags: []utils.FlagType{utils.FlagTelurPutih},
|
||||||
@@ -265,12 +265,32 @@ func seedProducts(tx *gorm.DB, createdBy uint, uoms map[string]uint, categories
|
|||||||
Name: "Telur Retak",
|
Name: "Telur Retak",
|
||||||
Brand: "-",
|
Brand: "-",
|
||||||
Sku: "7",
|
Sku: "7",
|
||||||
Uom: "Gram",
|
Uom: "Butir",
|
||||||
Category: "Telur",
|
Category: "Telur",
|
||||||
Price: 1,
|
Price: 1,
|
||||||
Flags: []utils.FlagType{utils.FlagTelurRetak},
|
Flags: []utils.FlagType{utils.FlagTelurRetak},
|
||||||
IsVisible: false,
|
IsVisible: false,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "Telur Papacal",
|
||||||
|
Brand: "-",
|
||||||
|
Sku: "8",
|
||||||
|
Uom: "Butir",
|
||||||
|
Category: "Telur",
|
||||||
|
Price: 1,
|
||||||
|
Flags: []utils.FlagType{utils.FlagTelur},
|
||||||
|
IsVisible: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "Telur Jumbo",
|
||||||
|
Brand: "-",
|
||||||
|
Sku: "9",
|
||||||
|
Uom: "Butir",
|
||||||
|
Category: "Telur",
|
||||||
|
Price: 1,
|
||||||
|
Flags: []utils.FlagType{utils.FlagTelur},
|
||||||
|
IsVisible: false,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, seed := range seeds {
|
for _, seed := range seeds {
|
||||||
@@ -299,6 +319,7 @@ func seedProducts(tx *gorm.DB, createdBy uint, uoms map[string]uint, categories
|
|||||||
Tax: tax,
|
Tax: tax,
|
||||||
ExpiryPeriod: seed.Expiry,
|
ExpiryPeriod: seed.Expiry,
|
||||||
CreatedBy: createdBy,
|
CreatedBy: createdBy,
|
||||||
|
IsVisible: seed.IsVisible,
|
||||||
}
|
}
|
||||||
if err := tx.Create(&product).Error; err != nil {
|
if err := tx.Create(&product).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -2,28 +2,17 @@ package entities
|
|||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
// AdjustmentStock tracks FIFO allocation for stock adjustments
|
|
||||||
// - For INCREASE adjustments (Stockable): Tracks stock added to warehouse
|
|
||||||
// - For DECREASE adjustments (Usable): Tracks stock consumed from warehouse
|
|
||||||
type AdjustmentStock struct {
|
type AdjustmentStock struct {
|
||||||
Id uint `gorm:"primaryKey"`
|
Id uint `gorm:"primaryKey"`
|
||||||
StockLogId uint `gorm:"column:stock_log_id;not null;index"`
|
ProductWarehouseId uint `gorm:"column:product_warehouse_id;not null"`
|
||||||
ProductWarehouseId uint `gorm:"column:product_warehouse_id;not null"`
|
TotalQty float64 `gorm:"column:total_qty;default:0"`
|
||||||
|
TotalUsed float64 `gorm:"column:total_used;default:0"`
|
||||||
|
UsageQty float64 `gorm:"column:usage_qty;default:0"`
|
||||||
|
PendingQty float64 `gorm:"column:pending_qty;default:0"`
|
||||||
|
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime"`
|
||||||
|
UpdatedAt time.Time `gorm:"column:updated_at;autoUpdateTime"`
|
||||||
|
AdjNumber string `gorm:"column:adj_number;uniqueIndex;not null"`
|
||||||
|
|
||||||
// === FIFO FIELDS FOR INCREASE ADJUSTMENT (Stockable) ===
|
|
||||||
// Tracks stock added to warehouse via adjustment INCREASE
|
|
||||||
TotalQty float64 `gorm:"column:total_qty;default:0"` // Total lot quantity available
|
|
||||||
TotalUsed float64 `gorm:"column:total_used;default:0"` // Quantity already used from this lot
|
|
||||||
|
|
||||||
// === FIFO FIELDS FOR DECREASE ADJUSTMENT (Usable) ===
|
|
||||||
// Tracks stock consumed from warehouse via adjustment DECREASE
|
|
||||||
UsageQty float64 `gorm:"column:usage_qty;default:0"` // Actual quantity consumed
|
|
||||||
PendingQty float64 `gorm:"column:pending_qty;default:0"` // Pending quantity (waiting for stock)
|
|
||||||
|
|
||||||
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime"`
|
|
||||||
UpdatedAt time.Time `gorm:"column:updated_at;autoUpdateTime"`
|
|
||||||
|
|
||||||
// Relations
|
|
||||||
StockLog *StockLog `gorm:"foreignKey:StockLogId;references:Id"`
|
|
||||||
ProductWarehouse *ProductWarehouse `gorm:"foreignKey:ProductWarehouseId;references:Id"`
|
ProductWarehouse *ProductWarehouse `gorm:"foreignKey:ProductWarehouseId;references:Id"`
|
||||||
|
StockLog *StockLog `gorm:"polymorphic:Loggable;polymorphicType:LoggableType;polymorphicId:LoggableId;polymorphicValue:ADJUSTMENT"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package entities
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Dashboard struct {
|
||||||
|
Id uint `gorm:"primaryKey"`
|
||||||
|
Name string `gorm:"not null;uniqueIndex:idx_name,where:deleted_at IS NULL"`
|
||||||
|
CreatedBy uint `gorm:"not null"`
|
||||||
|
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||||
|
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
||||||
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||||
|
|
||||||
|
CreatedUser User `gorm:"foreignKey:CreatedBy;references:Id"`
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@ type Document struct {
|
|||||||
DocumentableType string `gorm:"size:50;not null;index:documents_documentable_polymorphic,priority:1"`
|
DocumentableType string `gorm:"size:50;not null;index:documents_documentable_polymorphic,priority:1"`
|
||||||
DocumentableId uint64 `gorm:"not null;index:documents_documentable_polymorphic,priority:2"`
|
DocumentableId uint64 `gorm:"not null;index:documents_documentable_polymorphic,priority:2"`
|
||||||
Type string `gorm:"size:50;not null"`
|
Type string `gorm:"size:50;not null"`
|
||||||
Path string `gorm:"size:50;not null"`
|
Path string `gorm:"size:255;not null"`
|
||||||
Name string `gorm:"size:50;not null"`
|
Name string `gorm:"size:50;not null"`
|
||||||
Ext string `gorm:"size:50;not null"`
|
Ext string `gorm:"size:50;not null"`
|
||||||
Size float64 `gorm:"type:numeric(15,3);not null"`
|
Size float64 `gorm:"type:numeric(15,3);not null"`
|
||||||
|
|||||||
@@ -5,15 +5,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ExpenseNonstock struct {
|
type ExpenseNonstock struct {
|
||||||
Id uint64 `gorm:"primaryKey;autoIncrement"`
|
Id uint64 `gorm:"primaryKey;autoIncrement"`
|
||||||
ExpenseId *uint64 `gorm:""`
|
ExpenseId *uint64 `gorm:""`
|
||||||
ProjectFlockKandangId *uint64 `gorm:""`
|
ProjectFlockKandangId *uint64 `gorm:""`
|
||||||
KandangId *uint64 `gorm:""`
|
KandangId *uint64 `gorm:""`
|
||||||
NonstockId *uint64 `gorm:""`
|
NonstockId *uint64 `gorm:""`
|
||||||
Qty float64 `gorm:"type:numeric(15,3);not null"`
|
Qty float64 `gorm:"type:numeric(15,3);not null"`
|
||||||
Price float64 `gorm:"type:numeric(15,3);not null;column:price"`
|
Price float64 `gorm:"type:numeric(15,3);not null;column:price"`
|
||||||
Notes string `gorm:"type:text;column:notes"`
|
Notes string `gorm:"type:text;column:notes"`
|
||||||
CreatedAt time.Time `gorm:"type:timestamptz;default:CURRENT_TIMESTAMP"`
|
CreatedAt time.Time `gorm:"type:timestamptz;default:CURRENT_TIMESTAMP"`
|
||||||
|
|
||||||
Expense *Expense `gorm:"foreignKey:ExpenseId;references:Id"`
|
Expense *Expense `gorm:"foreignKey:ExpenseId;references:Id"`
|
||||||
ProjectFlockKandang *ProjectFlockKandang `gorm:"foreignKey:ProjectFlockKandangId;references:Id"`
|
ProjectFlockKandang *ProjectFlockKandang `gorm:"foreignKey:ProjectFlockKandangId;references:Id"`
|
||||||
|
|||||||
@@ -12,18 +12,16 @@ type LayingTransfer struct {
|
|||||||
FromProjectFlockId uint `gorm:"not null"`
|
FromProjectFlockId uint `gorm:"not null"`
|
||||||
ToProjectFlockId uint `gorm:"not null"`
|
ToProjectFlockId uint `gorm:"not null"`
|
||||||
TransferDate time.Time `gorm:"type:date;not null"`
|
TransferDate time.Time `gorm:"type:date;not null"`
|
||||||
PendingUsageQty *float64 `gorm:"type:numeric(15,3)"`
|
|
||||||
UsageQty *float64 `gorm:"type:numeric(15,3)"`
|
|
||||||
Notes string `gorm:"type:text"`
|
Notes string `gorm:"type:text"`
|
||||||
CreatedBy uint `gorm:"not null"`
|
CreatedBy uint `gorm:"not null"`
|
||||||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||||
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
||||||
DeletedAt gorm.DeletedAt `gorm:"index"`
|
DeletedAt gorm.DeletedAt `gorm:"index"`
|
||||||
|
|
||||||
FromProjectFlock *ProjectFlock `gorm:"foreignKey:FromProjectFlockId;references:Id"`
|
FromProjectFlock *ProjectFlock `gorm:"foreignKey:FromProjectFlockId;references:Id"`
|
||||||
ToProjectFlock *ProjectFlock `gorm:"foreignKey:ToProjectFlockId;references:Id"`
|
ToProjectFlock *ProjectFlock `gorm:"foreignKey:ToProjectFlockId;references:Id"`
|
||||||
CreatedUser *User `gorm:"foreignKey:CreatedBy;references:Id"`
|
CreatedUser *User `gorm:"foreignKey:CreatedBy;references:Id"`
|
||||||
Sources []LayingTransferSource `gorm:"foreignKey:LayingTransferId;constraint:OnDelete:CASCADE"`
|
Sources []LayingTransferSource `gorm:"foreignKey:LayingTransferId;constraint:OnDelete:CASCADE"`
|
||||||
Targets []LayingTransferTarget `gorm:"foreignKey:LayingTransferId;constraint:OnDelete:CASCADE"`
|
Targets []LayingTransferTarget `gorm:"foreignKey:LayingTransferId;constraint:OnDelete:CASCADE"`
|
||||||
LatestApproval *Approval `gorm:"-" json:"-"`
|
LatestApproval *Approval `gorm:"-" json:"-"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,9 @@ type LayingTransferSource struct {
|
|||||||
LayingTransferId uint `gorm:"index;not null"`
|
LayingTransferId uint `gorm:"index;not null"`
|
||||||
SourceProjectFlockKandangId uint `gorm:"not null"`
|
SourceProjectFlockKandangId uint `gorm:"not null"`
|
||||||
ProductWarehouseId *uint `gorm:""`
|
ProductWarehouseId *uint `gorm:""`
|
||||||
Qty float64 `gorm:"type:numeric(15,3);not null"`
|
RequestedQty float64 `gorm:"type:numeric(15,3);default:0;not null"` // Quantity requested by user
|
||||||
|
UsageQty float64 `gorm:"type:numeric(15,3);default:0;not null"` // FIFO USABLE field
|
||||||
|
PendingUsageQty float64 `gorm:"type:numeric(15,3);default:0;not null"` // FIFO USABLE field
|
||||||
Note string `gorm:"type:text"`
|
Note string `gorm:"type:text"`
|
||||||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||||
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
||||||
|
|||||||
@@ -10,7 +10,8 @@ type LayingTransferTarget struct {
|
|||||||
Id uint `gorm:"primaryKey"`
|
Id uint `gorm:"primaryKey"`
|
||||||
LayingTransferId uint `gorm:"index;not null"`
|
LayingTransferId uint `gorm:"index;not null"`
|
||||||
TargetProjectFlockKandangId uint `gorm:"not null"`
|
TargetProjectFlockKandangId uint `gorm:"not null"`
|
||||||
Qty float64 `gorm:"type:numeric(15,3);not null"`
|
TotalQty float64 `gorm:"type:numeric(15,3);default:0;not null"` // FIFO STOCKABLE field
|
||||||
|
TotalUsed float64 `gorm:"type:numeric(15,3);default:0;not null"` // FIFO STOCKABLE field
|
||||||
ProductWarehouseId *uint `gorm:""`
|
ProductWarehouseId *uint `gorm:""`
|
||||||
Note string `gorm:"type:text"`
|
Note string `gorm:"type:text"`
|
||||||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ type Marketing struct {
|
|||||||
SoDate time.Time `gorm:"type:date;not null"`
|
SoDate time.Time `gorm:"type:date;not null"`
|
||||||
SalesPersonId uint `gorm:"not null"`
|
SalesPersonId uint `gorm:"not null"`
|
||||||
Notes string `gorm:"type:text"`
|
Notes string `gorm:"type:text"`
|
||||||
|
MarketingType string `gorm:"type:varchar(50)"`
|
||||||
CreatedBy uint `gorm:"not null"`
|
CreatedBy uint `gorm:"not null"`
|
||||||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||||
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
||||||
|
|||||||
@@ -1,14 +1,17 @@
|
|||||||
package entities
|
package entities
|
||||||
|
|
||||||
type MarketingProduct struct {
|
type MarketingProduct struct {
|
||||||
Id uint `gorm:"primaryKey;autoIncrement"`
|
Id uint `gorm:"primaryKey;autoIncrement"`
|
||||||
MarketingId uint `gorm:"not null"`
|
MarketingId uint `gorm:"not null"`
|
||||||
ProductWarehouseId uint `gorm:"not null"`
|
ProductWarehouseId uint `gorm:"not null"`
|
||||||
Qty float64 `gorm:"type:numeric(15,3);not null"`
|
Qty float64 `gorm:"type:numeric(15,3);not null"`
|
||||||
UnitPrice float64 `gorm:"type:numeric(15,3);not null"`
|
ConvertionUnit *string `gorm:"type:varchar(20)"`
|
||||||
AvgWeight float64 `gorm:"type:numeric(15,3);not null"`
|
WeightPerConvertion *float64 `gorm:"type:numeric(15,3)"`
|
||||||
TotalWeight float64 `gorm:"type:numeric(15,3);not null"`
|
Week *int `gorm:"type:integer"`
|
||||||
TotalPrice float64 `gorm:"type:numeric(15,3);not null"`
|
UnitPrice float64 `gorm:"type:numeric(15,3);not null"`
|
||||||
|
AvgWeight float64 `gorm:"type:numeric(15,3);not null"`
|
||||||
|
TotalWeight float64 `gorm:"type:numeric(15,3);not null"`
|
||||||
|
TotalPrice float64 `gorm:"type:numeric(15,3);not null"`
|
||||||
|
|
||||||
Marketing Marketing `gorm:"foreignKey:MarketingId;references:Id"`
|
Marketing Marketing `gorm:"foreignKey:MarketingId;references:Id"`
|
||||||
ProductWarehouse ProductWarehouse `gorm:"foreignKey:ProductWarehouseId;references:Id"`
|
ProductWarehouse ProductWarehouse `gorm:"foreignKey:ProductWarehouseId;references:Id"`
|
||||||
|
|||||||
@@ -7,22 +7,23 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Payment struct {
|
type Payment struct {
|
||||||
Id uint `gorm:"primaryKey;autoIncrement"`
|
Id uint `gorm:"primaryKey;autoIncrement"`
|
||||||
PaymentCode string `gorm:"type:varchar(50);not null"`
|
PaymentCode string `gorm:"type:varchar(50);not null"`
|
||||||
ReferenceNumber *string `gorm:"type:varchar(100)"`
|
ReferenceNumber *string `gorm:"type:varchar(100)"`
|
||||||
TransactionType string `gorm:"type:varchar(50)"`
|
TransactionType string `gorm:"type:varchar(50)"`
|
||||||
PartyType string `gorm:"type:varchar(50);not null;index:payments_party_polymorphic,priority:1"`
|
PartyType string `gorm:"type:varchar(50);not null;index:payments_party_polymorphic,priority:1"`
|
||||||
PartyId uint `gorm:"not null;index:payments_party_polymorphic,priority:2"`
|
PartyId uint `gorm:"not null;index:payments_party_polymorphic,priority:2"`
|
||||||
PaymentDate time.Time `gorm:"not null"`
|
PartyAccountNumber *string `gorm:"type:varchar(50)"`
|
||||||
PaymentMethod string `gorm:"type:varchar(20);not null"`
|
PaymentDate time.Time `gorm:"not null"`
|
||||||
BankId *uint `gorm:"not null;index:idx_payments_bank_id"`
|
PaymentMethod string `gorm:"type:varchar(20);not null"`
|
||||||
Direction string `gorm:"type:varchar(5);not null"`
|
BankId *uint `gorm:"not null;index:idx_payments_bank_id"`
|
||||||
Nominal float64 `gorm:"type:numeric(15,3);not null"`
|
Direction string `gorm:"type:varchar(5);not null"`
|
||||||
Notes string `gorm:"type:text;not null"`
|
Nominal float64 `gorm:"type:numeric(15,3);not null"`
|
||||||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
Notes string `gorm:"type:text;not null"`
|
||||||
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
||||||
CreatedBy uint `gorm:"index" json:"-"`
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||||
|
CreatedBy uint `gorm:"index" json:"-"`
|
||||||
|
|
||||||
BankWarehouse Bank `gorm:"foreignKey:BankId;references:Id"`
|
BankWarehouse Bank `gorm:"foreignKey:BankId;references:Id"`
|
||||||
CreatedUser User `gorm:"foreignKey:CreatedBy;references:Id"`
|
CreatedUser User `gorm:"foreignKey:CreatedBy;references:Id"`
|
||||||
|
|||||||
@@ -7,12 +7,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Phases struct {
|
type Phases struct {
|
||||||
Id uint `gorm:"primaryKey"`
|
Id uint `gorm:"primaryKey"`
|
||||||
Name string `gorm:"not null"`
|
Name string `gorm:"not null"`
|
||||||
IsActive bool `gorm:"not null;default:true"`
|
IsActive bool `gorm:"not null;default:true"`
|
||||||
Category string `gorm:"type:category_code;not null"`
|
Category string `gorm:"type:category_code;not null"`
|
||||||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||||
|
ActivityCount int `gorm:"-" json:"-"`
|
||||||
|
|
||||||
Activities []PhaseActivity `gorm:"foreignKey:PhaseId;references:Id"`
|
Activities []PhaseActivity `gorm:"foreignKey:PhaseId;references:Id"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ type Product struct {
|
|||||||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||||
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
||||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||||
IsVisible bool `gorm:"column:is_visible;default:true"`
|
IsVisible bool ``
|
||||||
|
|
||||||
CreatedUser User `gorm:"foreignKey:CreatedBy;references:Id"`
|
CreatedUser User `gorm:"foreignKey:CreatedBy;references:Id"`
|
||||||
Uom Uom `gorm:"foreignKey:UomId;references:Id"`
|
Uom Uom `gorm:"foreignKey:UomId;references:Id"`
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import "time"
|
|||||||
type ProductSupplier struct {
|
type ProductSupplier struct {
|
||||||
ProductId uint `gorm:"not null"`
|
ProductId uint `gorm:"not null"`
|
||||||
SupplierId uint `gorm:"not null"`
|
SupplierId uint `gorm:"not null"`
|
||||||
|
Price float64 `gorm:"type:numeric(15,3);not null;default:0"`
|
||||||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||||
|
|
||||||
Product Product `gorm:"foreignKey:ProductId;references:Id"`
|
Product Product `gorm:"foreignKey:ProductId;references:Id"`
|
||||||
|
|||||||
@@ -1,20 +1,24 @@
|
|||||||
package entities
|
package entities
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
type ProjectFlockKandangUniformity struct {
|
type ProjectFlockKandangUniformity struct {
|
||||||
Id uint `gorm:"primaryKey"`
|
Id uint `gorm:"primaryKey"`
|
||||||
Uniformity float64 `gorm:"type:numeric(15,3)"`
|
Uniformity float64 `gorm:"type:numeric(15,3)"`
|
||||||
Week int `gorm:"not null"`
|
Week int `gorm:"not null"`
|
||||||
Cv float64 `gorm:"type:numeric(15,3)"`
|
Cv float64 `gorm:"type:numeric(15,3)"`
|
||||||
ChickQtyOfWeight float64 `gorm:"type:numeric(15,3)"`
|
ChickQtyOfWeight float64 `gorm:"type:numeric(15,3)"`
|
||||||
MeanUp float64 `gorm:"type:numeric(15,3)"`
|
MeanUp float64 `gorm:"type:numeric(15,3)"`
|
||||||
MeanDown float64 `gorm:"type:numeric(15,3)"`
|
MeanDown float64 `gorm:"type:numeric(15,3)"`
|
||||||
ProjectFlockKandangId uint `gorm:"not null"`
|
ProjectFlockKandangId uint `gorm:"not null"`
|
||||||
UniformQty float64 `gorm:"type:numeric(15,3)"`
|
UniformQty float64 `gorm:"type:numeric(15,3)"`
|
||||||
NotUniformQty float64 `gorm:"type:numeric(15,3)"`
|
NotUniformQty float64 `gorm:"type:numeric(15,3)"`
|
||||||
UniformDate *time.Time `gorm:"type:timestamptz"`
|
ChartData json.RawMessage `gorm:"type:jsonb"`
|
||||||
CreatedBy uint `gorm:"not null"`
|
UniformDate *time.Time `gorm:"type:timestamptz"`
|
||||||
|
CreatedBy uint `gorm:"not null"`
|
||||||
|
|
||||||
ProjectFlockKandang ProjectFlockKandang `gorm:"foreignKey:ProjectFlockKandangId;references:Id"`
|
ProjectFlockKandang ProjectFlockKandang `gorm:"foreignKey:ProjectFlockKandangId;references:Id"`
|
||||||
CreatedUser User `gorm:"foreignKey:CreatedBy;references:Id"`
|
CreatedUser User `gorm:"foreignKey:CreatedBy;references:Id"`
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ type ProjectFlock struct {
|
|||||||
FlockName string `gorm:"type:varchar(255);not null;uniqueIndex"`
|
FlockName string `gorm:"type:varchar(255);not null;uniqueIndex"`
|
||||||
AreaId uint `gorm:"not null"`
|
AreaId uint `gorm:"not null"`
|
||||||
Category string `gorm:"type:varchar(20);not null"`
|
Category string `gorm:"type:varchar(20);not null"`
|
||||||
FcrId uint `gorm:"not null"`
|
|
||||||
ProductionStandardId uint `gorm:"column:production_standard_id"`
|
ProductionStandardId uint `gorm:"column:production_standard_id"`
|
||||||
LocationId uint `gorm:"not null"`
|
LocationId uint `gorm:"not null"`
|
||||||
CreatedBy uint `gorm:"not null"`
|
CreatedBy uint `gorm:"not null"`
|
||||||
@@ -20,7 +19,6 @@ type ProjectFlock struct {
|
|||||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||||
|
|
||||||
Area Area `gorm:"foreignKey:AreaId;references:Id"`
|
Area Area `gorm:"foreignKey:AreaId;references:Id"`
|
||||||
Fcr Fcr `gorm:"foreignKey:FcrId;references:Id"`
|
|
||||||
ProductionStandard ProductionStandard `gorm:"foreignKey:ProductionStandardId;references:Id"`
|
ProductionStandard ProductionStandard `gorm:"foreignKey:ProductionStandardId;references:Id"`
|
||||||
Location Location `gorm:"foreignKey:LocationId;references:Id"`
|
Location Location `gorm:"foreignKey:LocationId;references:Id"`
|
||||||
CreatedUser User `gorm:"foreignKey:CreatedBy;references:Id"`
|
CreatedUser User `gorm:"foreignKey:CreatedBy;references:Id"`
|
||||||
|
|||||||
@@ -10,8 +10,9 @@ type ProjectFlockKandang struct {
|
|||||||
ClosedAt *time.Time `gorm:"index"`
|
ClosedAt *time.Time `gorm:"index"`
|
||||||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||||
|
|
||||||
ProjectFlock ProjectFlock `gorm:"foreignKey:ProjectFlockId;references:Id"`
|
ProjectFlock ProjectFlock `gorm:"foreignKey:ProjectFlockId;references:Id"`
|
||||||
Kandang Kandang `gorm:"foreignKey:KandangId;references:Id"`
|
Kandang Kandang `gorm:"foreignKey:KandangId;references:Id"`
|
||||||
Chickins []ProjectChickin `gorm:"foreignKey:ProjectFlockKandangId;references:Id"`
|
Chickins []ProjectChickin `gorm:"foreignKey:ProjectFlockKandangId;references:Id"`
|
||||||
LatestApproval *Approval `gorm:"-" json:"-"`
|
LatestProjectFlockApproval *Approval `gorm:"-" json:"-"`
|
||||||
|
LatestChickinApproval *Approval `gorm:"-" json:"-"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ type PurchaseItem struct {
|
|||||||
Price float64 `gorm:"type:numeric(15,3);default:0"`
|
Price float64 `gorm:"type:numeric(15,3);default:0"`
|
||||||
TotalPrice float64 `gorm:"type:numeric(15,3);default:0"`
|
TotalPrice float64 `gorm:"type:numeric(15,3);default:0"`
|
||||||
ExpenseNonstockId *uint64
|
ExpenseNonstockId *uint64
|
||||||
|
HasChickin bool `gorm:"-" json:"-"`
|
||||||
|
|
||||||
// Relations
|
// Relations
|
||||||
ExpenseNonstock *ExpenseNonstock `gorm:"foreignKey:ExpenseNonstockId;references:Id"`
|
ExpenseNonstock *ExpenseNonstock `gorm:"foreignKey:ExpenseNonstockId;references:Id"`
|
||||||
|
|||||||
@@ -12,14 +12,16 @@ type Recording struct {
|
|||||||
RecordDatetime time.Time `gorm:"column:record_datetime;not null"`
|
RecordDatetime time.Time `gorm:"column:record_datetime;not null"`
|
||||||
Day *int `gorm:"column:day"`
|
Day *int `gorm:"column:day"`
|
||||||
TotalDepletionQty *float64 `gorm:"column:total_depletion_qty"`
|
TotalDepletionQty *float64 `gorm:"column:total_depletion_qty"`
|
||||||
|
TotalDepletionCumQty *float64 `gorm:"-"`
|
||||||
CumDepletionRate *float64 `gorm:"column:cum_depletion_rate"`
|
CumDepletionRate *float64 `gorm:"column:cum_depletion_rate"`
|
||||||
|
DepletionRate *float64 `gorm:"-"`
|
||||||
CumIntake *int `gorm:"column:cum_intake"`
|
CumIntake *int `gorm:"column:cum_intake"`
|
||||||
FcrValue *float64 `gorm:"column:fcr_value"`
|
FcrValue *float64 `gorm:"column:fcr_value"`
|
||||||
TotalChickQty *float64 `gorm:"column:total_chick_qty"`
|
TotalChickQty *float64 `gorm:"column:total_chick_qty"`
|
||||||
HandDay *float64 `gorm:"column:hand_day"`
|
HenDay *float64 `gorm:"column:hen_day"`
|
||||||
HandHouse *float64 `gorm:"column:hand_house"`
|
HenHouse *float64 `gorm:"column:hen_house"`
|
||||||
FeedIntake *float64 `gorm:"column:feed_intake"`
|
FeedIntake *float64 `gorm:"column:feed_intake"`
|
||||||
EggMesh *float64 `gorm:"column:egg_mesh"`
|
EggMass *float64 `gorm:"column:egg_mass"`
|
||||||
EggWeight *float64 `gorm:"column:egg_weight"`
|
EggWeight *float64 `gorm:"column:egg_weight"`
|
||||||
CreatedBy uint `gorm:"column:created_by"`
|
CreatedBy uint `gorm:"column:created_by"`
|
||||||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||||
@@ -34,11 +36,11 @@ type Recording struct {
|
|||||||
|
|
||||||
LatestApproval *Approval `gorm:"-" json:"-"`
|
LatestApproval *Approval `gorm:"-" json:"-"`
|
||||||
|
|
||||||
StandardHandDay *float64 `gorm:"-"`
|
StandardHenDay *float64 `gorm:"-"`
|
||||||
StandardHandHouse *float64 `gorm:"-"`
|
StandardHenHouse *float64 `gorm:"-"`
|
||||||
StandardFeedIntake *float64 `gorm:"-"`
|
StandardFeedIntake *float64 `gorm:"-"`
|
||||||
StandardMaxDepletion *float64 `gorm:"-"`
|
StandardMaxDepletion *float64 `gorm:"-"`
|
||||||
StandardEggMesh *float64 `gorm:"-"`
|
StandardEggMass *float64 `gorm:"-"`
|
||||||
StandardEggWeight *float64 `gorm:"-"`
|
StandardEggWeight *float64 `gorm:"-"`
|
||||||
StandardFcr *float64 `gorm:"-"`
|
StandardFcr *float64 `gorm:"-"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
package entities
|
package entities
|
||||||
|
|
||||||
type RecordingDepletion struct {
|
type RecordingDepletion struct {
|
||||||
Id uint `gorm:"primaryKey"`
|
Id uint `gorm:"primaryKey"`
|
||||||
RecordingId uint `gorm:"column:recording_id;not null;index"`
|
RecordingId uint `gorm:"column:recording_id;not null;index"`
|
||||||
ProductWarehouseId uint `gorm:"column:product_warehouse_id;not null"`
|
ProductWarehouseId uint `gorm:"column:product_warehouse_id;not null"`
|
||||||
Qty float64 `gorm:"column:qty;not null"`
|
SourceProductWarehouseId *uint `gorm:"column:source_product_warehouse_id"`
|
||||||
|
Qty float64 `gorm:"column:qty;not null"`
|
||||||
|
PendingQty float64 `gorm:"column:pending_qty"`
|
||||||
|
|
||||||
Recording Recording `gorm:"foreignKey:RecordingId;references:Id"`
|
Recording Recording `gorm:"foreignKey:RecordingId;references:Id"`
|
||||||
ProductWarehouse ProductWarehouse `gorm:"foreignKey:ProductWarehouseId;references:Id"`
|
ProductWarehouse ProductWarehouse `gorm:"foreignKey:ProductWarehouseId;references:Id"`
|
||||||
|
|||||||
@@ -7,11 +7,14 @@ type RecordingEgg struct {
|
|||||||
RecordingId uint `gorm:"column:recording_id;not null;index"`
|
RecordingId uint `gorm:"column:recording_id;not null;index"`
|
||||||
ProductWarehouseId uint `gorm:"column:product_warehouse_id;not null"`
|
ProductWarehouseId uint `gorm:"column:product_warehouse_id;not null"`
|
||||||
Qty int `gorm:"column:qty;not null"`
|
Qty int `gorm:"column:qty;not null"`
|
||||||
|
TotalQty float64 `gorm:"column:total_qty"`
|
||||||
|
TotalUsed float64 `gorm:"column:total_used"`
|
||||||
Weight *float64 `gorm:"column:weight"`
|
Weight *float64 `gorm:"column:weight"`
|
||||||
CreatedBy uint `gorm:"column:created_by"`
|
CreatedBy uint `gorm:"column:created_by"`
|
||||||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||||
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
||||||
ProductWarehouse ProductWarehouse `gorm:"foreignKey:ProductWarehouseId;references:Id"`
|
ProductWarehouse ProductWarehouse `gorm:"foreignKey:ProductWarehouseId;references:Id"`
|
||||||
|
ProductFlagName *string `gorm:"->;column:product_flag_name" json:"-"`
|
||||||
CreatedUser *User `gorm:"foreignKey:CreatedBy;references:Id"`
|
CreatedUser *User `gorm:"foreignKey:CreatedBy;references:Id"`
|
||||||
Recording Recording `gorm:"foreignKey:RecordingId;references:Id"`
|
Recording Recording `gorm:"foreignKey:RecordingId;references:Id"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ type StockLog struct {
|
|||||||
|
|
||||||
Increase float64 `gorm:"column:increase;type:numeric(15,3);default:0"`
|
Increase float64 `gorm:"column:increase;type:numeric(15,3);default:0"`
|
||||||
Decrease float64 `gorm:"column:decrease;type:numeric(15,3);default:0"`
|
Decrease float64 `gorm:"column:decrease;type:numeric(15,3);default:0"`
|
||||||
|
Stock float64 `gorm:"column:stock;type:numeric(15,3);not null;default:0"`
|
||||||
|
|
||||||
LoggableType string `gorm:"column:loggable_type;type:varchar(50);not null"`
|
LoggableType string `gorm:"column:loggable_type;type:varchar(50);not null"`
|
||||||
LoggableId uint `gorm:"column:loggable_id;not null"`
|
LoggableId uint `gorm:"column:loggable_id;not null"`
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import "time"
|
|||||||
type StockTransferDelivery struct {
|
type StockTransferDelivery struct {
|
||||||
Id uint64 `gorm:"primaryKey;autoIncrement"`
|
Id uint64 `gorm:"primaryKey;autoIncrement"`
|
||||||
StockTransferId uint64
|
StockTransferId uint64
|
||||||
SupplierId uint64
|
SupplierId *uint64
|
||||||
VehiclePlate string
|
VehiclePlate string
|
||||||
DriverName string
|
DriverName string
|
||||||
DocumentNumber string
|
DocumentNumber string
|
||||||
|
|||||||
@@ -8,27 +8,22 @@ type StockTransferDetail struct {
|
|||||||
StockTransferId uint64
|
StockTransferId uint64
|
||||||
ProductId uint64
|
ProductId uint64
|
||||||
|
|
||||||
// === FIFO FIELDS - SOURCE WAREHOUSE (Usable) ===
|
|
||||||
// Tracking stock yang DIAMBIL dari source warehouse
|
|
||||||
SourceProductWarehouseID *uint64 `gorm:"column:source_product_warehouse_id"`
|
SourceProductWarehouseID *uint64 `gorm:"column:source_product_warehouse_id"`
|
||||||
UsageQty float64 `gorm:"column:usage_qty;default:0"` // Actual yang berhasil diambil
|
UsageQty float64 `gorm:"column:usage_qty;default:0"` // Actual yang berhasil diambil
|
||||||
PendingQty float64 `gorm:"column:pending_qty;default:0"` // Yang pending (nunggu stock)
|
PendingQty float64 `gorm:"column:pending_qty;default:0"` // Yang pending (nunggu stock)
|
||||||
|
DestProductWarehouseID *uint64 `gorm:"column:dest_product_warehouse_id"`
|
||||||
// === FIFO FIELDS - DESTINATION WAREHOUSE (Stockable) ===
|
TotalQty float64 `gorm:"column:total_qty;default:0"` // Total lot yang tersedia
|
||||||
// Tracking stock yang DITAMBAHKAN ke destination warehouse
|
TotalUsed float64 `gorm:"column:total_used;default:0"` // Yang sudah dipakai dari lot ini
|
||||||
DestProductWarehouseID *uint64 `gorm:"column:dest_product_warehouse_id"`
|
ExpenseNonstockId *uint64 `gorm:"column:expense_nonstock_id"`
|
||||||
TotalQty float64 `gorm:"column:total_qty;default:0"` // Total lot yang tersedia
|
CreatedAt time.Time
|
||||||
TotalUsed float64 `gorm:"column:total_used;default:0"` // Yang sudah dipakai dari lot ini
|
UpdatedAt time.Time
|
||||||
|
DeletedAt *time.Time `gorm:"index"`
|
||||||
// === METADATA ===
|
|
||||||
CreatedAt time.Time
|
|
||||||
UpdatedAt time.Time
|
|
||||||
DeletedAt *time.Time `gorm:"index"`
|
|
||||||
|
|
||||||
// === RELATIONS ===
|
// === RELATIONS ===
|
||||||
StockTransfer *StockTransfer `gorm:"foreignKey:StockTransferId"`
|
StockTransfer *StockTransfer `gorm:"foreignKey:StockTransferId"`
|
||||||
Product *Product `gorm:"foreignKey:ProductId"`
|
Product *Product `gorm:"foreignKey:ProductId"`
|
||||||
SourceProductWarehouse *ProductWarehouse `gorm:"foreignKey:SourceProductWarehouseID"`
|
SourceProductWarehouse *ProductWarehouse `gorm:"foreignKey:SourceProductWarehouseID"`
|
||||||
DestProductWarehouse *ProductWarehouse `gorm:"foreignKey:DestProductWarehouseID"`
|
DestProductWarehouse *ProductWarehouse `gorm:"foreignKey:DestProductWarehouseID"`
|
||||||
|
ExpenseNonstock *ExpenseNonstock `gorm:"foreignKey:ExpenseNonstockId;references:Id"`
|
||||||
DeliveryItems []StockTransferDeliveryItem `gorm:"foreignKey:StockTransferDetailId"`
|
DeliveryItems []StockTransferDeliveryItem `gorm:"foreignKey:StockTransferDetailId"`
|
||||||
}
|
}
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user