mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 21:41:55 +00:00
Merge branch 'feat/BE/US-159,160/marketing' into 'feat/BE/Sprint-5'
Feat/be/us 159,160/marketing See merge request mbugroup/lti-api!66
This commit is contained in:
@@ -3,13 +3,9 @@ root = "."
|
||||
tmp_dir = "tmp"
|
||||
|
||||
[build]
|
||||
# Build binary utama
|
||||
cmd = "go build -o /lti-api/tmp/main ./cmd/api"
|
||||
# Lokasi binary hasil build
|
||||
bin = "/lti-api/tmp/main"
|
||||
# Jalankan binary langsung dengan environment dev
|
||||
full_bin = "APP_ENV=dev /lti-api/tmp/main"
|
||||
# File yang dipantau oleh Air
|
||||
cmd = "go build -o ./tmp/main ./cmd/api"
|
||||
bin = "tmp/main"
|
||||
full_bin = "APP_ENV=dev ./tmp/main"
|
||||
include_ext = ["go", "tpl", "tmpl", "html"]
|
||||
exclude_dir = ["vendor", "tmp"]
|
||||
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
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
|
||||
@@ -10,6 +10,7 @@ require (
|
||||
github.com/gofiber/contrib/jwt v1.0.10
|
||||
github.com/gofiber/fiber/v2 v2.52.5
|
||||
github.com/golang-jwt/jwt/v5 v5.2.1
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/jackc/pgconn v1.14.1
|
||||
github.com/redis/go-redis/v9 v9.14.0
|
||||
github.com/sirupsen/logrus v1.9.3
|
||||
@@ -33,7 +34,6 @@ require (
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/google/go-cmp v0.6.0 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
|
||||
github.com/jackc/pgio v1.0.0 // indirect
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
DROP TABLE IF EXISTS marketing_delivery_products CASCADE;
|
||||
|
||||
DROP TABLE IF EXISTS marketing_products CASCADE;
|
||||
|
||||
DROP TABLE IF EXISTS marketings CASCADE;
|
||||
@@ -0,0 +1,44 @@
|
||||
CREATE TABLE marketings (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
so_number VARCHAR(255) UNIQUE NOT NULL,
|
||||
customer_id BIGINT NOT NULL,
|
||||
so_docs VARCHAR(20),
|
||||
so_date DATE NOT NULL,
|
||||
sales_person_id BIGINT NOT NULL,
|
||||
notes TEXT,
|
||||
created_by BIGINT NOT NULL,
|
||||
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
deleted_at TIMESTAMPTZ
|
||||
);
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'customers') THEN
|
||||
ALTER TABLE marketings
|
||||
ADD CONSTRAINT fk_marketings_customer_id
|
||||
FOREIGN KEY (customer_id) REFERENCES customers(id) ON DELETE RESTRICT;
|
||||
END IF;
|
||||
|
||||
IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'users') THEN
|
||||
ALTER TABLE marketings
|
||||
ADD CONSTRAINT fk_marketings_sales_person_id
|
||||
FOREIGN KEY (sales_person_id) REFERENCES users(id) ON DELETE RESTRICT;
|
||||
END IF;
|
||||
|
||||
IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'users') THEN
|
||||
ALTER TABLE marketings
|
||||
ADD CONSTRAINT fk_marketings_created_by
|
||||
FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE RESTRICT;
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
CREATE INDEX idx_marketings_customer_id ON marketings (customer_id);
|
||||
|
||||
CREATE INDEX idx_marketings_sales_person_id ON marketings (sales_person_id);
|
||||
|
||||
CREATE INDEX idx_marketings_created_by ON marketings (created_by);
|
||||
|
||||
CREATE INDEX idx_marketings_so_date ON marketings (so_date);
|
||||
|
||||
CREATE INDEX idx_marketings_deleted_at ON marketings (deleted_at);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS marketing_products CASCADE;
|
||||
@@ -0,0 +1,34 @@
|
||||
CREATE TABLE marketing_products (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
marketing_id BIGINT NOT NULL,
|
||||
product_warehouse_id BIGINT NOT NULL,
|
||||
qty NUMERIC(15, 3) NOT NULL,
|
||||
unit_price NUMERIC(15, 3) NOT NULL,
|
||||
avg_weight NUMERIC(15, 3) NOT NULL,
|
||||
total_weight NUMERIC(15, 3) NOT NULL,
|
||||
total_price NUMERIC(15, 3) NOT NULL,
|
||||
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
deleted_at TIMESTAMPTZ
|
||||
);
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'marketings') THEN
|
||||
ALTER TABLE marketing_products
|
||||
ADD CONSTRAINT fk_marketing_products_marketing_id
|
||||
FOREIGN KEY (marketing_id) REFERENCES marketings(id) ON DELETE CASCADE;
|
||||
END IF;
|
||||
|
||||
IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'product_warehouses') THEN
|
||||
ALTER TABLE marketing_products
|
||||
ADD CONSTRAINT fk_marketing_products_product_warehouse_id
|
||||
FOREIGN KEY (product_warehouse_id) REFERENCES product_warehouses(id) ON DELETE RESTRICT;
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
CREATE INDEX idx_marketing_products_marketing_id ON marketing_products (marketing_id);
|
||||
|
||||
CREATE INDEX idx_marketing_products_product_warehouse_id ON marketing_products (product_warehouse_id);
|
||||
|
||||
CREATE INDEX idx_marketing_products_deleted_at ON marketing_products (deleted_at);
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
|
||||
DROP TABLE IF EXISTS marketing_delivery_products CASCADE;
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
CREATE TABLE marketing_delivery_products (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
marketing_product_id BIGINT UNIQUE NOT NULL,
|
||||
qty NUMERIC(15, 3) NOT NULL,
|
||||
unit_price NUMERIC(15, 3) NOT NULL,
|
||||
total_weight NUMERIC(15, 3) NOT NULL,
|
||||
avg_weight NUMERIC(15, 3) NOT NULL,
|
||||
total_price NUMERIC(15, 3) NOT NULL,
|
||||
delivery_date DATE,
|
||||
vehicle_number VARCHAR(50),
|
||||
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
deleted_at TIMESTAMPTZ
|
||||
);
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'marketing_products') THEN
|
||||
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 IF;
|
||||
END $$;
|
||||
|
||||
CREATE INDEX idx_marketing_delivery_products_marketing_product_id ON marketing_delivery_products (marketing_product_id);
|
||||
|
||||
CREATE INDEX idx_marketing_delivery_products_delivery_date ON marketing_delivery_products (delivery_date);
|
||||
|
||||
CREATE INDEX idx_marketing_delivery_products_deleted_at ON marketing_delivery_products (deleted_at);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS expenses;
|
||||
@@ -0,0 +1,44 @@
|
||||
CREATE TABLE expenses (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
reference_number VARCHAR, -- format => BOP-LTI-0001 = 0001 is increment
|
||||
supplier_id BIGINT NULL,
|
||||
category VARCHAR(50) NOT NULL CHECK (
|
||||
category IN ('BOP', 'NON-BOP')
|
||||
),
|
||||
po_number VARCHAR(50) UNIQUE NOT NULL,
|
||||
document_path JSON,
|
||||
expense_date DATE NOT NULL,
|
||||
grand_total NUMERIC(15, 3) DEFAULT 0,
|
||||
note TEXT,
|
||||
created_by BIGINT,
|
||||
created_at TIMESTAMPTZ DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ DEFAULT now(),
|
||||
deleted_at TIMESTAMPTZ
|
||||
);
|
||||
|
||||
-- Tambahkan Foreign Key ke suppliers
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'suppliers') THEN
|
||||
ALTER TABLE expenses
|
||||
ADD CONSTRAINT fk_expenses_supplier_id
|
||||
FOREIGN KEY (supplier_id) REFERENCES suppliers(id);
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
-- Tambahkan Foreign Key ke users (created_by)
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'users') THEN
|
||||
ALTER TABLE expenses
|
||||
ADD CONSTRAINT fk_expenses_created_by
|
||||
FOREIGN KEY (created_by) REFERENCES users(id);
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
-- Index
|
||||
CREATE INDEX idx_expenses_supplier_id ON expenses (supplier_id);
|
||||
|
||||
CREATE INDEX idx_expenses_expense_date ON expenses (expense_date);
|
||||
|
||||
CREATE INDEX idx_expenses_deleted_at ON expenses (deleted_at);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS expense_nonstocks;
|
||||
@@ -0,0 +1,50 @@
|
||||
CREATE TABLE expense_nonstocks (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
expense_id BIGINT,
|
||||
project_flock_kandang_id BIGINT,
|
||||
nonstock_id BIGINT,
|
||||
qty NUMERIC(15, 3) NOT NULL,
|
||||
unit_price NUMERIC(15, 3) NOT NULL,
|
||||
total_price NUMERIC(15, 3) NOT NULL,
|
||||
note TEXT NULL,
|
||||
created_at TIMESTAMPTZ DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ DEFAULT now(),
|
||||
deleted_at TIMESTAMPTZ
|
||||
);
|
||||
|
||||
-- Tambahkan Foreign Key ke expenses
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'expenses') THEN
|
||||
ALTER TABLE expense_nonstocks
|
||||
ADD CONSTRAINT fk_expense_nonstocks_expense_id
|
||||
FOREIGN KEY (expense_id) REFERENCES expenses(id);
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
-- Tambahkan Foreign Key ke project_flock_kandangs
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'project_flock_kandangs') THEN
|
||||
ALTER TABLE expense_nonstocks
|
||||
ADD CONSTRAINT fk_expense_nonstocks_kandang_id
|
||||
FOREIGN KEY (project_flock_kandang_id) REFERENCES project_flock_kandangs(id);
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
-- Tambahkan Foreign Key ke nonstocks
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'nonstocks') THEN
|
||||
ALTER TABLE expense_nonstocks
|
||||
ADD CONSTRAINT fk_expense_nonstocks_nonstock_id
|
||||
FOREIGN KEY (nonstock_id) REFERENCES nonstocks(id);
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
-- Index
|
||||
CREATE INDEX idx_expense_nonstocks_expense_id ON expense_nonstocks (expense_id);
|
||||
|
||||
CREATE INDEX idx_expense_nonstocks_nonstock_id ON expense_nonstocks (nonstock_id);
|
||||
|
||||
CREATE INDEX idx_expense_nonstocks_deleted_at ON expense_nonstocks (deleted_at);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS expense_realizations;
|
||||
@@ -0,0 +1,40 @@
|
||||
CREATE TABLE expense_realizations (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
expense_nonstock_id BIGINT,
|
||||
realization_qty NUMERIC(15, 3) NOT NULL,
|
||||
realization_unit_price NUMERIC(15, 3) NOT NULL,
|
||||
realization_total_price NUMERIC(15, 3) NOT NULL,
|
||||
realization_date DATE NOT NULL,
|
||||
note TEXT,
|
||||
created_by BIGINT,
|
||||
created_at TIMESTAMPTZ DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ DEFAULT now(),
|
||||
deleted_at TIMESTAMPTZ
|
||||
);
|
||||
|
||||
-- Tambahkan Foreign Key ke expense_nonstocks
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'expense_nonstocks') THEN
|
||||
ALTER TABLE expense_realizations
|
||||
ADD CONSTRAINT fk_expense_realizations_nonstock_id
|
||||
FOREIGN KEY (expense_nonstock_id) REFERENCES expense_nonstocks(id);
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
-- Tambahkan Foreign Key ke users (created_by)
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'users') THEN
|
||||
ALTER TABLE expense_realizations
|
||||
ADD CONSTRAINT fk_expense_realizations_created_by
|
||||
FOREIGN KEY (created_by) REFERENCES users(id);
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
-- Index
|
||||
CREATE INDEX idx_expense_realizations_nonstock_id ON expense_realizations (expense_nonstock_id);
|
||||
|
||||
CREATE INDEX idx_expense_realizations_date ON expense_realizations (realization_date);
|
||||
|
||||
CREATE INDEX idx_expense_realizations_deleted_at ON expense_realizations (deleted_at);
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
-- Add back timestamp columns to marketing_products table
|
||||
ALTER TABLE marketing_products
|
||||
ADD COLUMN IF NOT EXISTS created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ;
|
||||
|
||||
-- Add back timestamp columns to marketing_delivery_products table
|
||||
ALTER TABLE marketing_delivery_products
|
||||
ADD COLUMN IF NOT EXISTS created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ;
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
-- Drop timestamp columns from marketing_products table if it exists
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'marketing_products' AND column_name = 'created_at') THEN
|
||||
ALTER TABLE marketing_products DROP COLUMN created_at;
|
||||
END IF;
|
||||
IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'marketing_products' AND column_name = 'updated_at') THEN
|
||||
ALTER TABLE marketing_products DROP COLUMN updated_at;
|
||||
END IF;
|
||||
IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'marketing_products' AND column_name = 'deleted_at') THEN
|
||||
ALTER TABLE marketing_products DROP COLUMN deleted_at;
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
-- Drop timestamp columns from marketing_delivery_products table if it exists
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'marketing_delivery_products' AND column_name = 'created_at') THEN
|
||||
ALTER TABLE marketing_delivery_products DROP COLUMN created_at;
|
||||
END IF;
|
||||
IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'marketing_delivery_products' AND column_name = 'updated_at') THEN
|
||||
ALTER TABLE marketing_delivery_products DROP COLUMN updated_at;
|
||||
END IF;
|
||||
IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'marketing_delivery_products' AND column_name = 'deleted_at') THEN
|
||||
ALTER TABLE marketing_delivery_products DROP COLUMN deleted_at;
|
||||
END IF;
|
||||
END $$;
|
||||
@@ -589,6 +589,14 @@ func seedProducts(tx *gorm.DB, createdBy uint, uoms map[string]uint, categories
|
||||
Category: "Day Old Chick",
|
||||
Price: 1,
|
||||
},
|
||||
{
|
||||
Name: "Ayam Mati",
|
||||
Brand: "-",
|
||||
Sku: "2",
|
||||
Uom: "Ekor",
|
||||
Category: "Day Old Chick",
|
||||
Price: 1,
|
||||
},
|
||||
{
|
||||
Name: "Ayam Culling",
|
||||
Brand: "-",
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package entities
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Expense struct {
|
||||
Id uint64 `gorm:"primaryKey;autoIncrement"`
|
||||
ReferenceNumber *string `gorm:"type:varchar(50)"`
|
||||
SupplierId *uint64 `gorm:""`
|
||||
Category string `gorm:"type:varchar(50);not null"`
|
||||
PoNumber string `gorm:"uniqueIndex;not null;type:varchar(50)"`
|
||||
DocumentPath sql.NullString `gorm:"type:json"`
|
||||
ExpenseDate time.Time `gorm:"type:date;not null"`
|
||||
GrandTotal float64 `gorm:"type:numeric(15,3);default:0"`
|
||||
Note *string `gorm:"type:text"`
|
||||
CreatedBy *uint64 `gorm:""`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||
|
||||
// Relations
|
||||
Supplier *Supplier `gorm:"foreignKey:SupplierId;references:Id"`
|
||||
CreatedUser *User `gorm:"foreignKey:CreatedBy;references:Id"`
|
||||
Nonstocks []ExpenseNonstock `gorm:"foreignKey:ExpenseId;references:Id"`
|
||||
LatestApproval *Approval `gorm:"-" json:"latest_approval,omitempty"`
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package entities
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ExpenseNonstock struct {
|
||||
Id uint64 `gorm:"primaryKey;autoIncrement"`
|
||||
ExpenseId *uint64 `gorm:""`
|
||||
ProjectFlockKandangId *uint64 `gorm:""`
|
||||
NonstockId *uint64 `gorm:""`
|
||||
Qty float64 `gorm:"type:numeric(15,3);not null"`
|
||||
UnitPrice float64 `gorm:"type:numeric(15,3);not null"`
|
||||
TotalPrice float64 `gorm:"type:numeric(15,3);not null"`
|
||||
Note *string `gorm:"type:text"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||
|
||||
// Relations
|
||||
Expense *Expense `gorm:"foreignKey:ExpenseId;references:Id"`
|
||||
ProjectFlockKandang *ProjectFlockKandang `gorm:"foreignKey:ProjectFlockKandangId;references:Id"`
|
||||
Nonstock *Nonstock `gorm:"foreignKey:NonstockId;references:Id"`
|
||||
Realizations []ExpenseRealization `gorm:"foreignKey:ExpenseNonstockId;references:Id"`
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package entities
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ExpenseRealization struct {
|
||||
Id uint64 `gorm:"primaryKey;autoIncrement"`
|
||||
ExpenseNonstockId *uint64 `gorm:""`
|
||||
RealizationQty float64 `gorm:"type:numeric(15,3);not null"`
|
||||
RealizationUnitPrice float64 `gorm:"type:numeric(15,3);not null"`
|
||||
RealizationTotalPrice float64 `gorm:"type:numeric(15,3);not null"`
|
||||
RealizationDate time.Time `gorm:"type:date;not null"`
|
||||
Note *string `gorm:"type:text"`
|
||||
CreatedBy *uint64 `gorm:""`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||
|
||||
// Relations
|
||||
ExpenseNonstock *ExpenseNonstock `gorm:"foreignKey:ExpenseNonstockId;references:Id"`
|
||||
CreatedUser *User `gorm:"foreignKey:CreatedBy;references:Id"`
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package entities
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Marketing struct {
|
||||
Id uint `gorm:"primaryKey;autoIncrement"`
|
||||
SoNumber string `gorm:"uniqueIndex;not null"`
|
||||
CustomerId uint `gorm:"not null"`
|
||||
SoDocs string `gorm:"type:varchar(20)"`
|
||||
SoDate time.Time `gorm:"type:date;not null"`
|
||||
SalesPersonId uint `gorm:"not null"`
|
||||
Notes string `gorm:"type:text"`
|
||||
CreatedBy uint `gorm:"not null"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||
|
||||
Customer Customer `gorm:"foreignKey:CustomerId;references:Id"`
|
||||
SalesPerson User `gorm:"foreignKey:SalesPersonId;references:Id"`
|
||||
CreatedUser User `gorm:"foreignKey:CreatedBy;references:Id"`
|
||||
Products []MarketingProduct `gorm:"foreignKey:MarketingId;references:Id"`
|
||||
LatestApproval *Approval `gorm:"-" json:"latest_approval,omitempty"`
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package entities
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type MarketingDeliveryProduct struct {
|
||||
Id uint `gorm:"primaryKey;autoIncrement"`
|
||||
MarketingProductId uint `gorm:"uniqueIndex;not null"`
|
||||
Qty float64 `gorm:"type:numeric(15,3)"`
|
||||
UnitPrice float64 `gorm:"type:numeric(15,3)"`
|
||||
TotalWeight float64 `gorm:"type:numeric(15,3)"`
|
||||
AvgWeight float64 `gorm:"type:numeric(15,3)"`
|
||||
TotalPrice float64 `gorm:"type:numeric(15,3)"`
|
||||
DeliveryDate *time.Time `gorm:"type:timestamptz"`
|
||||
VehicleNumber string `gorm:"type:varchar(50)"`
|
||||
|
||||
MarketingProduct MarketingProduct `gorm:"foreignKey:MarketingProductId;references:Id"`
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package entities
|
||||
|
||||
type MarketingProduct struct {
|
||||
Id uint `gorm:"primaryKey;autoIncrement"`
|
||||
MarketingId uint `gorm:"not null"`
|
||||
ProductWarehouseId uint `gorm:"not null"`
|
||||
Qty 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"`
|
||||
ProductWarehouse ProductWarehouse `gorm:"foreignKey:ProductWarehouseId;references:Id"`
|
||||
DeliveryProduct *MarketingDeliveryProduct `gorm:"foreignKey:MarketingProductId;references:Id"`
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"math"
|
||||
"strconv"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/modules/expenses/dto"
|
||||
service "gitlab.com/mbugroup/lti-api.git/internal/modules/expenses/services"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/expenses/validations"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/response"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type ExpenseController struct {
|
||||
ExpenseService service.ExpenseService
|
||||
}
|
||||
|
||||
func NewExpenseController(expenseService service.ExpenseService) *ExpenseController {
|
||||
return &ExpenseController{
|
||||
ExpenseService: expenseService,
|
||||
}
|
||||
}
|
||||
|
||||
func (u *ExpenseController) GetAll(c *fiber.Ctx) error {
|
||||
query := &validation.Query{
|
||||
Page: c.QueryInt("page", 1),
|
||||
Limit: c.QueryInt("limit", 10),
|
||||
Search: c.Query("search", ""),
|
||||
}
|
||||
|
||||
if query.Page < 1 || query.Limit < 1 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0")
|
||||
}
|
||||
|
||||
result, totalResults, err := u.ExpenseService.GetAll(c, query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.SuccessWithPaginate[dto.ExpenseListDTO]{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Get all expenses successfully",
|
||||
Meta: response.Meta{
|
||||
Page: query.Page,
|
||||
Limit: query.Limit,
|
||||
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
|
||||
TotalResults: totalResults,
|
||||
},
|
||||
Data: dto.ToExpenseListDTOs(result),
|
||||
})
|
||||
}
|
||||
|
||||
func (u *ExpenseController) GetOne(c *fiber.Ctx) error {
|
||||
param := c.Params("id")
|
||||
|
||||
id, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||
}
|
||||
|
||||
result, err := u.ExpenseService.GetOne(c, uint(id))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Get expense successfully",
|
||||
Data: dto.ToExpenseListDTO(*result),
|
||||
})
|
||||
}
|
||||
|
||||
func (u *ExpenseController) CreateOne(c *fiber.Ctx) error {
|
||||
req := new(validation.Create)
|
||||
|
||||
if err := c.BodyParser(req); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
}
|
||||
|
||||
result, err := u.ExpenseService.CreateOne(c, req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusCreated).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusCreated,
|
||||
Status: "success",
|
||||
Message: "Create expense successfully",
|
||||
Data: dto.ToExpenseListDTO(*result),
|
||||
})
|
||||
}
|
||||
|
||||
func (u *ExpenseController) UpdateOne(c *fiber.Ctx) error {
|
||||
req := new(validation.Update)
|
||||
param := c.Params("id")
|
||||
|
||||
id, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||
}
|
||||
|
||||
if err := c.BodyParser(req); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
}
|
||||
|
||||
result, err := u.ExpenseService.UpdateOne(c, req, uint(id))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Update expense successfully",
|
||||
Data: dto.ToExpenseListDTO(*result),
|
||||
})
|
||||
}
|
||||
|
||||
func (u *ExpenseController) DeleteOne(c *fiber.Ctx) error {
|
||||
param := c.Params("id")
|
||||
|
||||
id, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||
}
|
||||
|
||||
if err := u.ExpenseService.DeleteOne(c, uint(id)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Common{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Delete expense successfully",
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
|
||||
)
|
||||
|
||||
// === DTO Structs ===
|
||||
|
||||
type ExpenseBaseDTO struct {
|
||||
Id uint64 `json:"id"`
|
||||
PoNumber string `json:"po_number"`
|
||||
ExpenseDate time.Time `json:"expense_date"`
|
||||
GrandTotal float64 `json:"grand_total"`
|
||||
}
|
||||
|
||||
type ExpenseListDTO struct {
|
||||
Id uint64 `json:"id"`
|
||||
ReferenceNumber string `json:"reference_number"`
|
||||
PoNumber string `json:"po_number"`
|
||||
Category string `json:"category"`
|
||||
ExpenseDate time.Time `json:"expense_date"`
|
||||
GrandTotal float64 `json:"grand_total"`
|
||||
CreatedUser *userDTO.UserBaseDTO `json:"created_user,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// === Mapper Functions ===
|
||||
|
||||
func ToExpenseBaseDTO(e entity.Expense) ExpenseBaseDTO {
|
||||
return ExpenseBaseDTO{
|
||||
Id: e.Id,
|
||||
PoNumber: e.PoNumber,
|
||||
ExpenseDate: e.ExpenseDate,
|
||||
GrandTotal: e.GrandTotal,
|
||||
}
|
||||
}
|
||||
|
||||
func ToExpenseListDTO(e entity.Expense) ExpenseListDTO {
|
||||
var createdUser *userDTO.UserBaseDTO
|
||||
if e.CreatedUser.Id != 0 {
|
||||
mapped := userDTO.ToUserBaseDTO(*e.CreatedUser)
|
||||
createdUser = &mapped
|
||||
}
|
||||
|
||||
return ExpenseListDTO{
|
||||
Id: e.Id,
|
||||
ReferenceNumber: *e.ReferenceNumber,
|
||||
PoNumber: e.PoNumber,
|
||||
Category: e.Category,
|
||||
ExpenseDate: e.ExpenseDate,
|
||||
GrandTotal: e.GrandTotal,
|
||||
CreatedAt: e.CreatedAt,
|
||||
UpdatedAt: e.UpdatedAt,
|
||||
CreatedUser: createdUser,
|
||||
}
|
||||
}
|
||||
|
||||
func ToExpenseListDTOs(e []entity.Expense) []ExpenseListDTO {
|
||||
result := make([]ExpenseListDTO, len(e))
|
||||
for i, r := range e {
|
||||
result[i] = ToExpenseListDTO(r)
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package expenses
|
||||
|
||||
import (
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gorm.io/gorm"
|
||||
|
||||
rExpense "gitlab.com/mbugroup/lti-api.git/internal/modules/expenses/repositories"
|
||||
sExpense "gitlab.com/mbugroup/lti-api.git/internal/modules/expenses/services"
|
||||
|
||||
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
|
||||
sUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
||||
)
|
||||
|
||||
type ExpenseModule struct{}
|
||||
|
||||
func (ExpenseModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
|
||||
expenseRepo := rExpense.NewExpenseRepository(db)
|
||||
userRepo := rUser.NewUserRepository(db)
|
||||
|
||||
expenseService := sExpense.NewExpenseService(expenseRepo, validate)
|
||||
userService := sUser.NewUserService(userRepo, validate)
|
||||
|
||||
ExpenseRoutes(router, userService, expenseService)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ExpenseRepository interface {
|
||||
repository.BaseRepository[entity.Expense]
|
||||
IdExists(ctx context.Context, id uint64) (bool, error)
|
||||
}
|
||||
|
||||
type ExpenseRepositoryImpl struct {
|
||||
*repository.BaseRepositoryImpl[entity.Expense]
|
||||
}
|
||||
|
||||
func NewExpenseRepository(db *gorm.DB) ExpenseRepository {
|
||||
return &ExpenseRepositoryImpl{
|
||||
BaseRepositoryImpl: repository.NewBaseRepository[entity.Expense](db),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *ExpenseRepositoryImpl) IdExists(ctx context.Context, id uint64) (bool, error) {
|
||||
return repository.Exists[entity.Expense](ctx, r.DB(), uint(id))
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ExpenseNonstockRepository interface {
|
||||
repository.BaseRepository[entity.ExpenseNonstock]
|
||||
IdExists(ctx context.Context, id uint64) (bool, error)
|
||||
}
|
||||
|
||||
type ExpenseNonstockRepositoryImpl struct {
|
||||
*repository.BaseRepositoryImpl[entity.ExpenseNonstock]
|
||||
}
|
||||
|
||||
func NewExpenseNonstockRepository(db *gorm.DB) ExpenseNonstockRepository {
|
||||
return &ExpenseNonstockRepositoryImpl{
|
||||
BaseRepositoryImpl: repository.NewBaseRepository[entity.ExpenseNonstock](db),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *ExpenseNonstockRepositoryImpl) IdExists(ctx context.Context, id uint64) (bool, error) {
|
||||
return repository.Exists[entity.ExpenseNonstock](ctx, r.DB(), uint(id))
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ExpenseRealizationRepository interface {
|
||||
repository.BaseRepository[entity.ExpenseRealization]
|
||||
IdExists(ctx context.Context, id uint64) (bool, error)
|
||||
}
|
||||
|
||||
type ExpenseRealizationRepositoryImpl struct {
|
||||
*repository.BaseRepositoryImpl[entity.ExpenseRealization]
|
||||
}
|
||||
|
||||
func NewExpenseRealizationRepository(db *gorm.DB) ExpenseRealizationRepository {
|
||||
return &ExpenseRealizationRepositoryImpl{
|
||||
BaseRepositoryImpl: repository.NewBaseRepository[entity.ExpenseRealization](db),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *ExpenseRealizationRepositoryImpl) IdExists(ctx context.Context, id uint64) (bool, error) {
|
||||
return repository.Exists[entity.ExpenseRealization](ctx, r.DB(), uint(id))
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package expenses
|
||||
|
||||
import (
|
||||
// m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
||||
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/expenses/controllers"
|
||||
expense "gitlab.com/mbugroup/lti-api.git/internal/modules/expenses/services"
|
||||
user "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func ExpenseRoutes(v1 fiber.Router, u user.UserService, s expense.ExpenseService) {
|
||||
ctrl := controller.NewExpenseController(s)
|
||||
|
||||
route := v1.Group("/expenses")
|
||||
|
||||
// route.Get("/", m.Auth(u), ctrl.GetAll)
|
||||
// route.Post("/", m.Auth(u), ctrl.CreateOne)
|
||||
// route.Get("/:id", m.Auth(u), ctrl.GetOne)
|
||||
// route.Patch("/:id", m.Auth(u), ctrl.UpdateOne)
|
||||
// route.Delete("/:id", m.Auth(u), ctrl.DeleteOne)
|
||||
|
||||
route.Get("/", ctrl.GetAll)
|
||||
route.Post("/", ctrl.CreateOne)
|
||||
route.Get("/:id", ctrl.GetOne)
|
||||
route.Patch("/:id", ctrl.UpdateOne)
|
||||
route.Delete("/:id", ctrl.DeleteOne)
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/expenses/repositories"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/expenses/validations"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ExpenseService interface {
|
||||
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.Expense, int64, error)
|
||||
GetOne(ctx *fiber.Ctx, id uint) (*entity.Expense, error)
|
||||
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.Expense, error)
|
||||
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.Expense, error)
|
||||
DeleteOne(ctx *fiber.Ctx, id uint) error
|
||||
}
|
||||
|
||||
type expenseService struct {
|
||||
Log *logrus.Logger
|
||||
Validate *validator.Validate
|
||||
Repository repository.ExpenseRepository
|
||||
}
|
||||
|
||||
func NewExpenseService(repo repository.ExpenseRepository, validate *validator.Validate) ExpenseService {
|
||||
return &expenseService{
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
Repository: repo,
|
||||
}
|
||||
}
|
||||
|
||||
func (s expenseService) withRelations(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("CreatedUser")
|
||||
}
|
||||
|
||||
func (s expenseService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.Expense, int64, error) {
|
||||
if err := s.Validate.Struct(params); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
offset := (params.Page - 1) * params.Limit
|
||||
|
||||
expenses, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
||||
db = s.withRelations(db)
|
||||
if params.Search != "" {
|
||||
return db.Where("name LIKE ?", "%"+params.Search+"%")
|
||||
}
|
||||
return db.Order("created_at DESC").Order("updated_at DESC")
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get expenses: %+v", err)
|
||||
return nil, 0, err
|
||||
}
|
||||
return expenses, total, nil
|
||||
}
|
||||
|
||||
func (s expenseService) GetOne(c *fiber.Ctx, id uint) (*entity.Expense, error) {
|
||||
expense, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "Expense not found")
|
||||
}
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed get expense by id: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
return expense, nil
|
||||
}
|
||||
|
||||
func (s *expenseService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.Expense, error) {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
createdBy := uint64(1)
|
||||
createBody := &entity.Expense{
|
||||
PoNumber: req.PoNumber,
|
||||
Category: req.Category,
|
||||
CreatedBy: &createdBy,
|
||||
}
|
||||
|
||||
if err := s.Repository.CreateOne(c.Context(), createBody, nil); err != nil {
|
||||
s.Log.Errorf("Failed to create expense: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.GetOne(c, uint(createBody.Id))
|
||||
}
|
||||
|
||||
func (s expenseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.Expense, error) {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
updateBody := make(map[string]any)
|
||||
|
||||
if req.PoNumber != nil {
|
||||
updateBody["po_number"] = *req.PoNumber
|
||||
}
|
||||
if req.Category != nil {
|
||||
updateBody["category"] = *req.Category
|
||||
}
|
||||
|
||||
if len(updateBody) == 0 {
|
||||
return s.GetOne(c, id)
|
||||
}
|
||||
|
||||
if err := s.Repository.PatchOne(c.Context(), id, updateBody, nil); err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "Expense not found")
|
||||
}
|
||||
s.Log.Errorf("Failed to update expense: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.GetOne(c, id)
|
||||
}
|
||||
|
||||
func (s expenseService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||
if err := s.Repository.DeleteOne(c.Context(), id); err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, "Expense not found")
|
||||
}
|
||||
s.Log.Errorf("Failed to delete expense: %+v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package validation
|
||||
|
||||
type Create struct {
|
||||
PoNumber string `json:"po_number" validate:"required,max=50"`
|
||||
Category string `json:"category" validate:"required,max=50"`
|
||||
}
|
||||
|
||||
type Update struct {
|
||||
PoNumber *string `json:"po_number,omitempty" validate:"omitempty,max=50"`
|
||||
Category *string `json:"category,omitempty" validate:"omitempty,max=50"`
|
||||
}
|
||||
|
||||
type Query struct {
|
||||
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
|
||||
Search string `query:"search" validate:"omitempty,max=50"`
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type MarketingDeliveryProductRepository interface {
|
||||
repository.BaseRepository[entity.MarketingDeliveryProduct]
|
||||
GetByMarketingProductID(ctx context.Context, marketingProductID uint) (*entity.MarketingDeliveryProduct, error)
|
||||
GetByMarketingId(ctx context.Context, marketingId uint) ([]entity.MarketingDeliveryProduct, error)
|
||||
}
|
||||
|
||||
type MarketingDeliveryProductRepositoryImpl struct {
|
||||
*repository.BaseRepositoryImpl[entity.MarketingDeliveryProduct]
|
||||
}
|
||||
|
||||
func NewMarketingDeliveryProductRepository(db *gorm.DB) MarketingDeliveryProductRepository {
|
||||
return &MarketingDeliveryProductRepositoryImpl{
|
||||
BaseRepositoryImpl: repository.NewBaseRepository[entity.MarketingDeliveryProduct](db),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *MarketingDeliveryProductRepositoryImpl) GetByMarketingProductID(ctx context.Context, marketingProductID uint) (*entity.MarketingDeliveryProduct, error) {
|
||||
var deliveryProduct entity.MarketingDeliveryProduct
|
||||
if err := r.DB().WithContext(ctx).Where("marketing_product_id = ?", marketingProductID).First(&deliveryProduct).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &deliveryProduct, nil
|
||||
}
|
||||
|
||||
func (r *MarketingDeliveryProductRepositoryImpl) GetByMarketingId(ctx context.Context, marketingId uint) ([]entity.MarketingDeliveryProduct, error) {
|
||||
var deliveryProducts []entity.MarketingDeliveryProduct
|
||||
|
||||
// Raw query untuk mengambil delivery products berdasarkan marketing ID dengan preload MarketingProduct
|
||||
// Filter: hanya ambil yang sudah memiliki delivery_date (delivery date tidak null)
|
||||
if err := r.DB().WithContext(ctx).
|
||||
Preload("MarketingProduct").
|
||||
Joins("INNER JOIN marketing_products mp ON marketing_delivery_products.marketing_product_id = mp.id").
|
||||
Where("mp.marketing_id = ?", marketingId).
|
||||
Where("marketing_delivery_products.delivery_date IS NOT NULL").
|
||||
Order("marketing_delivery_products.id ASC").
|
||||
Find(&deliveryProducts).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return deliveryProducts, nil
|
||||
}
|
||||
+1
@@ -29,6 +29,7 @@ func (u *ProductWarehouseController) GetAll(c *fiber.Ctx) error {
|
||||
ProductId: uint(c.QueryInt("product_id", 0)),
|
||||
WarehouseId: uint(c.QueryInt("warehouse_id", 0)),
|
||||
Flags: c.Query("flags", ""),
|
||||
KandangId: uint(c.QueryInt("kandang_id", 0)),
|
||||
}
|
||||
|
||||
if query.Page < 1 || query.Limit < 1 {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"time"
|
||||
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
productDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/products/dto"
|
||||
)
|
||||
|
||||
// === DTO Structs ===
|
||||
@@ -15,13 +16,19 @@ type ProductWarehouseBaseDTO struct {
|
||||
Quantity float64 `json:"quantity"`
|
||||
}
|
||||
|
||||
type ProductWarehousNestedDTO struct {
|
||||
Id uint `json:"id"`
|
||||
Product *productDTO.ProductBaseDTO `json:"product,omitempty"`
|
||||
Warehouse *WarehouseBaseDTO `json:"warehouse,omitempty"`
|
||||
}
|
||||
|
||||
type ProductWarehouseListDTO struct {
|
||||
ProductWarehouseBaseDTO
|
||||
Product *ProductBaseDTO `json:"product,omitempty"`
|
||||
Warehouse *WarehouseBaseDTO `json:"warehouse,omitempty"`
|
||||
CreatedUser *UserBaseDTO `json:"created_user,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Product *productDTO.ProductBaseDTO `json:"product,omitempty"`
|
||||
Warehouse *WarehouseBaseDTO `json:"warehouse,omitempty"`
|
||||
CreatedUser *UserBaseDTO `json:"created_user,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
type UserBaseDTO struct {
|
||||
@@ -75,6 +82,19 @@ func ToProductWarehouseBaseDTO(e entity.ProductWarehouse) ProductWarehouseBaseDT
|
||||
}
|
||||
}
|
||||
|
||||
func ToProductWarehouseNestedDTO(e entity.ProductWarehouse) ProductWarehousNestedDTO {
|
||||
product := productDTO.ToProductBaseDTO(e.Product)
|
||||
|
||||
return ProductWarehousNestedDTO{
|
||||
Id: e.Id,
|
||||
Product: &product,
|
||||
Warehouse: &WarehouseBaseDTO{
|
||||
Id: e.Warehouse.Id,
|
||||
Name: e.Warehouse.Name,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func ToProductWarehouseListDTO(e entity.ProductWarehouse) ProductWarehouseListDTO {
|
||||
dto := ProductWarehouseListDTO{
|
||||
ProductWarehouseBaseDTO: ToProductWarehouseBaseDTO(e),
|
||||
@@ -84,18 +104,7 @@ func ToProductWarehouseListDTO(e entity.ProductWarehouse) ProductWarehouseListDT
|
||||
|
||||
// Map Product relation jika ada
|
||||
if e.Product.Id != 0 {
|
||||
product := ProductBaseDTO{
|
||||
Id: e.Product.Id,
|
||||
Name: e.Product.Name,
|
||||
}
|
||||
if e.Product.Sku != nil {
|
||||
product.Sku = *e.Product.Sku
|
||||
}
|
||||
if len(e.Product.Flags) > 0 {
|
||||
for _, f := range e.Product.Flags {
|
||||
product.Flags = append(product.Flags, f.Name)
|
||||
}
|
||||
}
|
||||
product := productDTO.ToProductBaseDTO(e.Product)
|
||||
dto.Product = &product
|
||||
}
|
||||
|
||||
@@ -120,7 +129,7 @@ func ToProductWarehouseListDTO(e entity.ProductWarehouse) ProductWarehouseListDT
|
||||
}
|
||||
}
|
||||
|
||||
if &e.Warehouse.Area != nil && e.Warehouse.Area.Id != 0 {
|
||||
if e.Warehouse.Area.Id != 0 {
|
||||
warehouse.Area = &AreaBaseDTO{
|
||||
Id: e.Warehouse.Area.Id,
|
||||
Name: e.Warehouse.Area.Name,
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
rProductWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/repositories"
|
||||
sProductWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/services"
|
||||
rKandang "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/repositories"
|
||||
|
||||
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
|
||||
sUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
||||
@@ -17,8 +18,9 @@ type ProductWarehouseModule struct{}
|
||||
func (ProductWarehouseModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
|
||||
productWarehouseRepo := rProductWarehouse.NewProductWarehouseRepository(db)
|
||||
userRepo := rUser.NewUserRepository(db)
|
||||
kandangRepo := rKandang.NewKandangRepository(db)
|
||||
|
||||
productWarehouseService := sProductWarehouse.NewProductWarehouseService(productWarehouseRepo, validate)
|
||||
productWarehouseService := sProductWarehouse.NewProductWarehouseService(productWarehouseRepo, validate, kandangRepo)
|
||||
userService := sUser.NewUserService(userRepo, validate)
|
||||
|
||||
ProductWarehouseRoutes(router, userService, productWarehouseService)
|
||||
|
||||
+7
-2
@@ -25,6 +25,7 @@ type ProductWarehouseRepository interface {
|
||||
ApplyFlagsFilter(db *gorm.DB, flags []string) *gorm.DB
|
||||
AdjustQuantities(ctx context.Context, deltas map[uint]float64, modifier func(*gorm.DB) *gorm.DB) error
|
||||
GetDetailByID(ctx context.Context, id uint) (*entity.ProductWarehouse, error)
|
||||
IdExists(ctx context.Context, id uint) (bool, error)
|
||||
CleanupEmpty(ctx context.Context, affected map[uint]struct{}) error
|
||||
EnsureProductWarehouse(ctx context.Context, productID, warehouseID uint, createdBy uint64) (uint, error)
|
||||
}
|
||||
@@ -50,6 +51,10 @@ func (r *ProductWarehouseRepositoryImpl) ExistsByID(ctx context.Context, id uint
|
||||
return repository.Exists[entity.ProductWarehouse](ctx, r.DB(), id)
|
||||
}
|
||||
|
||||
func (r *ProductWarehouseRepositoryImpl) IdExists(ctx context.Context, id uint) (bool, error) {
|
||||
return repository.Exists[entity.ProductWarehouse](ctx, r.DB(), id)
|
||||
}
|
||||
|
||||
func (r *ProductWarehouseRepositoryImpl) ProductWarehouseExists(ctx context.Context, productId, warehouseId uint, excludeID *uint) (bool, error) {
|
||||
var count int64
|
||||
query := r.DB().WithContext(ctx).Model(&entity.ProductWarehouse{}).
|
||||
@@ -250,7 +255,7 @@ func (r *ProductWarehouseRepositoryImpl) GetByFlagAndWarehouseID(ctx context.Con
|
||||
var productWarehouses []entity.ProductWarehouse
|
||||
err := r.DB().WithContext(ctx).Model(&entity.ProductWarehouse{}).
|
||||
Joins("JOIN products ON products.id = product_warehouses.product_id").
|
||||
Joins("JOIN flags ON flags.flagable_id = products.id AND flags.flagable_type = ?", "products").
|
||||
Joins("JOIN flags ON flags.flagable_id = products.id AND flags.flagable_type = 'products'").
|
||||
Where("flags.name = ? AND product_warehouses.warehouse_id = ?", flagName, warehouseId).
|
||||
Order("product_warehouses.created_at DESC").
|
||||
Preload("Product").Preload("Warehouse").
|
||||
@@ -264,7 +269,7 @@ func (r *ProductWarehouseRepositoryImpl) GetByFlagAndWarehouseID(ctx context.Con
|
||||
func (r *ProductWarehouseRepositoryImpl) GetFirstProductByFlag(ctx context.Context, flagName string) (*entity.Product, error) {
|
||||
var product entity.Product
|
||||
err := r.DB().WithContext(ctx).
|
||||
Joins("JOIN flags ON flags.flagable_id = products.id AND flags.flagable_type = ?", "products").
|
||||
Joins("JOIN flags ON flags.flagable_id = products.id AND flags.flagable_type = 'products'").
|
||||
Where("flags.name = ?", flagName).
|
||||
First(&product).Error
|
||||
if err != nil {
|
||||
|
||||
+25
-7
@@ -6,6 +6,7 @@ import (
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/repositories"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/validations"
|
||||
kandangrepo "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/repositories"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
@@ -20,16 +21,18 @@ type ProductWarehouseService interface {
|
||||
}
|
||||
|
||||
type productWarehouseService struct {
|
||||
Log *logrus.Logger
|
||||
Validate *validator.Validate
|
||||
Repository repository.ProductWarehouseRepository
|
||||
Log *logrus.Logger
|
||||
Validate *validator.Validate
|
||||
Repository repository.ProductWarehouseRepository
|
||||
KandangRepo kandangrepo.KandangRepository
|
||||
}
|
||||
|
||||
func NewProductWarehouseService(repo repository.ProductWarehouseRepository, validate *validator.Validate) ProductWarehouseService {
|
||||
func NewProductWarehouseService(repo repository.ProductWarehouseRepository, validate *validator.Validate, kandangRepo kandangrepo.KandangRepository) ProductWarehouseService {
|
||||
return &productWarehouseService{
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
Repository: repo,
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
Repository: repo,
|
||||
KandangRepo: kandangRepo,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,6 +72,16 @@ func (s productWarehouseService) GetAll(c *fiber.Ctx, params *validation.Query)
|
||||
}
|
||||
}
|
||||
|
||||
if params.KandangId > 0 {
|
||||
isKandangExist, err := s.KandangRepo.IdExists(c.Context(), params.KandangId)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if !isKandangExist {
|
||||
return nil, 0, fiber.NewError(fiber.StatusNotFound, "Kandang not found")
|
||||
}
|
||||
}
|
||||
|
||||
offset := (params.Page - 1) * params.Limit
|
||||
|
||||
cleanFlags := utils.ParseFlags(params.Flags)
|
||||
@@ -80,6 +93,11 @@ func (s productWarehouseService) GetAll(c *fiber.Ctx, params *validation.Query)
|
||||
db = db.Where("product_id = ?", params.ProductId)
|
||||
}
|
||||
|
||||
if params.KandangId != 0 {
|
||||
db = db.Joins("JOIN warehouses ON product_warehouses.warehouse_id = warehouses.id").
|
||||
Where("warehouses.kandang_id = ?", params.KandangId)
|
||||
}
|
||||
|
||||
if params.WarehouseId != 0 {
|
||||
db = db.Where("warehouse_id = ?", params.WarehouseId)
|
||||
}
|
||||
|
||||
+1
@@ -18,4 +18,5 @@ type Query struct {
|
||||
ProductId uint `query:"product_id" validate:"omitempty,number,min=1"`
|
||||
WarehouseId uint `query:"warehouse_id" validate:"omitempty,number,min=1"`
|
||||
Flags string `query:"flags" validate:"omitempty"`
|
||||
KandangId uint `query:"kandang_id" validate:"omitempty,number,min=1"`
|
||||
}
|
||||
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"math"
|
||||
"strconv"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/delivery-orderss/dto"
|
||||
service "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/delivery-orderss/services"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/delivery-orderss/validations"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/response"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type DeliveryOrdersController struct {
|
||||
DeliveryOrdersService service.DeliveryOrdersService
|
||||
}
|
||||
|
||||
func NewDeliveryOrdersController(deliveryOrdersService service.DeliveryOrdersService) *DeliveryOrdersController {
|
||||
return &DeliveryOrdersController{
|
||||
DeliveryOrdersService: deliveryOrdersService,
|
||||
}
|
||||
}
|
||||
|
||||
func (u *DeliveryOrdersController) GetAll(c *fiber.Ctx) error {
|
||||
query := &validation.Query{
|
||||
Page: c.QueryInt("page", 1),
|
||||
Limit: c.QueryInt("limit", 10),
|
||||
MarketingId: uint(c.QueryInt("marketing_id", 0)),
|
||||
}
|
||||
|
||||
if query.Page < 1 || query.Limit < 1 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0")
|
||||
}
|
||||
|
||||
result, totalResults, err := u.DeliveryOrdersService.GetAll(c, query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.SuccessWithPaginate[dto.MarketingListDTO]{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Get all deliveryOrderss successfully",
|
||||
Meta: response.Meta{
|
||||
Page: query.Page,
|
||||
Limit: query.Limit,
|
||||
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
|
||||
TotalResults: totalResults,
|
||||
},
|
||||
Data: result,
|
||||
})
|
||||
}
|
||||
|
||||
func (u *DeliveryOrdersController) GetOne(c *fiber.Ctx) error {
|
||||
param := c.Params("id")
|
||||
|
||||
id, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||
}
|
||||
|
||||
result, err := u.DeliveryOrdersService.GetOne(c, uint(id))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Get deliveryOrders successfully",
|
||||
Data: *result,
|
||||
})
|
||||
}
|
||||
|
||||
func (u *DeliveryOrdersController) CreateOne(c *fiber.Ctx) error {
|
||||
req := new(validation.Create)
|
||||
|
||||
if err := c.BodyParser(req); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
}
|
||||
|
||||
result, err := u.DeliveryOrdersService.CreateOne(c, req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusCreated).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusCreated,
|
||||
Status: "success",
|
||||
Message: "Create delivery products successfully",
|
||||
Data: result,
|
||||
})
|
||||
}
|
||||
|
||||
func (u *DeliveryOrdersController) UpdateOne(c *fiber.Ctx) error {
|
||||
req := new(validation.Update)
|
||||
param := c.Params("id")
|
||||
|
||||
id, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||
}
|
||||
|
||||
if err := c.BodyParser(req); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
}
|
||||
|
||||
result, err := u.DeliveryOrdersService.UpdateOne(c, req, uint(id))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Update deliveryOrders successfully",
|
||||
Data: result,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,336 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
approvalDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/approvals/dto"
|
||||
productwarehouseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/dto"
|
||||
customerDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/customers/dto"
|
||||
userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
|
||||
)
|
||||
|
||||
type MarketingBaseDTO struct {
|
||||
Id uint `json:"id"`
|
||||
SoNumber string `json:"so_number"`
|
||||
SoDate time.Time `json:"so_date"`
|
||||
Notes string `json:"notes,omitempty"`
|
||||
}
|
||||
|
||||
type MarketingListDTO struct {
|
||||
MarketingBaseDTO
|
||||
Customer *customerDTO.CustomerBaseDTO `json:"customer,omitempty"`
|
||||
SalesPerson *userDTO.UserBaseDTO `json:"sales_person,omitempty"`
|
||||
SoDocs string `json:"so_docs,omitempty"`
|
||||
SalesOrder []MarketingProductDTO `json:"sales_order,omitempty"`
|
||||
CreatedUser *userDTO.UserBaseDTO `json:"created_user"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
LatestApproval *approvalDTO.ApprovalBaseDTO `json:"latest_approval,omitempty"`
|
||||
}
|
||||
|
||||
type MarketingDetailDTO struct {
|
||||
MarketingBaseDTO
|
||||
Customer *customerDTO.CustomerBaseDTO `json:"customer,omitempty"`
|
||||
SalesPerson *userDTO.UserBaseDTO `json:"sales_person,omitempty"`
|
||||
SoDocs string `json:"so_docs,omitempty"`
|
||||
SalesOrder []MarketingProductDTO `json:"sales_order,omitempty"`
|
||||
DeliveryOrder []DeliveryGroupDTO `json:"delivery_order,omitempty"`
|
||||
CreatedUser *userDTO.UserBaseDTO `json:"created_user"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
LatestApproval *approvalDTO.ApprovalBaseDTO `json:"latest_approval,omitempty"`
|
||||
}
|
||||
type MarketingDeliveryProductDTO struct {
|
||||
Id uint `json:"id"`
|
||||
MarketingProductId uint `json:"marketing_product_id"`
|
||||
Qty float64 `json:"qty"`
|
||||
UnitPrice float64 `json:"unit_price"`
|
||||
TotalWeight float64 `json:"total_weight"`
|
||||
AvgWeight float64 `json:"avg_weight"`
|
||||
TotalPrice float64 `json:"total_price"`
|
||||
DeliveryDate *time.Time `json:"delivery_date"`
|
||||
VehicleNumber string `json:"vehicle_number"`
|
||||
ProductWarehouse *productwarehouseDTO.ProductWarehousNestedDTO `json:"product_warehouse,omitempty"`
|
||||
}
|
||||
|
||||
type DeliveryItemDTO struct {
|
||||
ProductWarehouse *productwarehouseDTO.ProductWarehousNestedDTO `json:"product_warehouse"`
|
||||
Qty float64 `json:"qty"`
|
||||
UnitPrice float64 `json:"unit_price"`
|
||||
TotalWeight float64 `json:"total_weight"`
|
||||
AvgWeight float64 `json:"avg_weight"`
|
||||
TotalPrice float64 `json:"total_price"`
|
||||
VehicleNumber string `json:"vehicle_number"`
|
||||
}
|
||||
|
||||
type DeliveryGroupDTO struct {
|
||||
DoNumber string `json:"do_number"`
|
||||
DeliveryDate *time.Time `json:"delivery_date"`
|
||||
Warehouse *productwarehouseDTO.WarehouseBaseDTO `json:"warehouse,omitempty"`
|
||||
Deliveries []DeliveryItemDTO `json:"deliveries"`
|
||||
}
|
||||
|
||||
type MarketingProductDTO struct {
|
||||
Id uint `json:"id"`
|
||||
MarketingId uint `json:"marketing_id"`
|
||||
ProductWarehouseId uint `json:"product_warehouse_id"`
|
||||
Qty float64 `json:"qty"`
|
||||
UnitPrice float64 `json:"unit_price"`
|
||||
AvgWeight float64 `json:"avg_weight"`
|
||||
TotalWeight float64 `json:"total_weight"`
|
||||
TotalPrice float64 `json:"total_price"`
|
||||
ProductWarehouse *productwarehouseDTO.ProductWarehousNestedDTO `json:"product_warehouse,omitempty"`
|
||||
VehicleNumber string `json:"vehicle_number,omitempty"`
|
||||
}
|
||||
|
||||
func ToMarketingBaseDTO(marketing *entity.Marketing) MarketingBaseDTO {
|
||||
return MarketingBaseDTO{
|
||||
Id: marketing.Id,
|
||||
SoNumber: marketing.SoNumber,
|
||||
SoDate: marketing.SoDate,
|
||||
Notes: marketing.Notes,
|
||||
}
|
||||
}
|
||||
|
||||
func ToMarketingProductDTO(e entity.MarketingProduct) MarketingProductDTO {
|
||||
var productWarehouse *productwarehouseDTO.ProductWarehousNestedDTO
|
||||
if e.ProductWarehouse.Id != 0 {
|
||||
mapped := productwarehouseDTO.ToProductWarehouseNestedDTO(e.ProductWarehouse)
|
||||
productWarehouse = &mapped
|
||||
}
|
||||
|
||||
return MarketingProductDTO{
|
||||
Id: e.Id,
|
||||
MarketingId: e.MarketingId,
|
||||
ProductWarehouseId: e.ProductWarehouseId,
|
||||
Qty: e.Qty,
|
||||
UnitPrice: e.UnitPrice,
|
||||
AvgWeight: e.AvgWeight,
|
||||
TotalWeight: e.TotalWeight,
|
||||
TotalPrice: e.TotalPrice,
|
||||
ProductWarehouse: productWarehouse,
|
||||
VehicleNumber: getVehicleNumber(e),
|
||||
}
|
||||
}
|
||||
|
||||
func ToMarketingDeliveryProductDTO(e entity.MarketingDeliveryProduct) MarketingDeliveryProductDTO {
|
||||
return MarketingDeliveryProductDTO{
|
||||
Id: e.Id,
|
||||
MarketingProductId: e.MarketingProductId,
|
||||
Qty: e.Qty,
|
||||
UnitPrice: e.UnitPrice,
|
||||
TotalWeight: e.TotalWeight,
|
||||
AvgWeight: e.AvgWeight,
|
||||
TotalPrice: e.TotalPrice,
|
||||
DeliveryDate: e.DeliveryDate,
|
||||
VehicleNumber: e.VehicleNumber,
|
||||
}
|
||||
}
|
||||
|
||||
func ToMarketingListDTO(marketing *entity.Marketing, deliveryProducts []entity.MarketingDeliveryProduct) MarketingListDTO {
|
||||
var createdUser *userDTO.UserBaseDTO
|
||||
if marketing.CreatedUser.Id != 0 {
|
||||
mapped := userDTO.ToUserBaseDTO(marketing.CreatedUser)
|
||||
createdUser = &mapped
|
||||
}
|
||||
|
||||
var customer *customerDTO.CustomerBaseDTO
|
||||
if marketing.Customer.Id != 0 {
|
||||
mapped := customerDTO.ToCustomerBaseDTO(marketing.Customer)
|
||||
customer = &mapped
|
||||
}
|
||||
|
||||
var salesPerson *userDTO.UserBaseDTO
|
||||
if marketing.SalesPerson.Id != 0 {
|
||||
mapped := userDTO.ToUserBaseDTO(marketing.SalesPerson)
|
||||
salesPerson = &mapped
|
||||
}
|
||||
|
||||
var latestApproval *approvalDTO.ApprovalBaseDTO
|
||||
if marketing.LatestApproval != nil {
|
||||
mapped := approvalDTO.ToApprovalDTO(*marketing.LatestApproval)
|
||||
latestApproval = &mapped
|
||||
}
|
||||
|
||||
var salesOrderProducts []MarketingProductDTO
|
||||
if len(marketing.Products) > 0 {
|
||||
salesOrderProducts = make([]MarketingProductDTO, len(marketing.Products))
|
||||
for i, product := range marketing.Products {
|
||||
salesOrderProducts[i] = ToMarketingProductDTO(product)
|
||||
}
|
||||
}
|
||||
|
||||
return MarketingListDTO{
|
||||
MarketingBaseDTO: ToMarketingBaseDTO(marketing),
|
||||
Customer: customer,
|
||||
SalesPerson: salesPerson,
|
||||
SoDocs: marketing.SoDocs,
|
||||
SalesOrder: salesOrderProducts,
|
||||
CreatedUser: createdUser,
|
||||
CreatedAt: marketing.CreatedAt,
|
||||
UpdatedAt: marketing.UpdatedAt,
|
||||
LatestApproval: latestApproval,
|
||||
}
|
||||
}
|
||||
|
||||
func ToMarketingDetailDTO(marketing *entity.Marketing, deliveryProducts []entity.MarketingDeliveryProduct) MarketingDetailDTO {
|
||||
var createdUser *userDTO.UserBaseDTO
|
||||
if marketing.CreatedUser.Id != 0 {
|
||||
mapped := userDTO.ToUserBaseDTO(marketing.CreatedUser)
|
||||
createdUser = &mapped
|
||||
}
|
||||
|
||||
var customer *customerDTO.CustomerBaseDTO
|
||||
if marketing.Customer.Id != 0 {
|
||||
mapped := customerDTO.ToCustomerBaseDTO(marketing.Customer)
|
||||
customer = &mapped
|
||||
}
|
||||
|
||||
var salesPerson *userDTO.UserBaseDTO
|
||||
if marketing.SalesPerson.Id != 0 {
|
||||
mapped := userDTO.ToUserBaseDTO(marketing.SalesPerson)
|
||||
salesPerson = &mapped
|
||||
}
|
||||
|
||||
var salesOrderProducts []MarketingProductDTO
|
||||
if len(marketing.Products) > 0 {
|
||||
salesOrderProducts = make([]MarketingProductDTO, len(marketing.Products))
|
||||
for i, product := range marketing.Products {
|
||||
salesOrderProducts[i] = ToMarketingProductDTO(product)
|
||||
}
|
||||
}
|
||||
|
||||
var deliveryProductsDTOs []MarketingDeliveryProductDTO
|
||||
if len(deliveryProducts) > 0 {
|
||||
deliveryProductsDTOs = make([]MarketingDeliveryProductDTO, len(deliveryProducts))
|
||||
for i, dp := range deliveryProducts {
|
||||
deliveryProductsDTOs[i] = ToMarketingDeliveryProductDTO(dp)
|
||||
}
|
||||
deliveryProductsDTOs = enrichDeliveryProductDTOsWithWarehouse(deliveryProductsDTOs, marketing)
|
||||
}
|
||||
|
||||
deliveryGroups := groupDeliveryProducts(deliveryProductsDTOs, marketing.SoNumber)
|
||||
|
||||
var latestApproval *approvalDTO.ApprovalBaseDTO
|
||||
if marketing.LatestApproval != nil {
|
||||
mapped := approvalDTO.ToApprovalDTO(*marketing.LatestApproval)
|
||||
latestApproval = &mapped
|
||||
}
|
||||
|
||||
return MarketingDetailDTO{
|
||||
MarketingBaseDTO: ToMarketingBaseDTO(marketing),
|
||||
SoDocs: marketing.SoDocs,
|
||||
Customer: customer,
|
||||
SalesPerson: salesPerson,
|
||||
SalesOrder: salesOrderProducts,
|
||||
DeliveryOrder: deliveryGroups,
|
||||
CreatedUser: createdUser,
|
||||
CreatedAt: marketing.CreatedAt,
|
||||
UpdatedAt: marketing.UpdatedAt,
|
||||
LatestApproval: latestApproval,
|
||||
}
|
||||
}
|
||||
|
||||
func ToMarketingListDTOs(marketings []entity.Marketing) []MarketingListDTO {
|
||||
result := make([]MarketingListDTO, len(marketings))
|
||||
for i, m := range marketings {
|
||||
result[i] = ToMarketingListDTO(&m, []entity.MarketingDeliveryProduct{})
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func enrichDeliveryProductDTOsWithWarehouse(deliveryProductDTOs []MarketingDeliveryProductDTO, marketing *entity.Marketing) []MarketingDeliveryProductDTO {
|
||||
if len(deliveryProductDTOs) == 0 || marketing == nil || len(marketing.Products) == 0 {
|
||||
return deliveryProductDTOs
|
||||
}
|
||||
|
||||
productMap := make(map[uint]*entity.MarketingProduct)
|
||||
for i := range marketing.Products {
|
||||
productMap[marketing.Products[i].Id] = &marketing.Products[i]
|
||||
}
|
||||
|
||||
for i := range deliveryProductDTOs {
|
||||
if product, exists := productMap[deliveryProductDTOs[i].MarketingProductId]; exists {
|
||||
if product.ProductWarehouse.Id != 0 {
|
||||
mapped := productwarehouseDTO.ToProductWarehouseNestedDTO(product.ProductWarehouse)
|
||||
deliveryProductDTOs[i].ProductWarehouse = &mapped
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return deliveryProductDTOs
|
||||
}
|
||||
|
||||
func groupDeliveryProducts(products []MarketingDeliveryProductDTO, soNumber string) []DeliveryGroupDTO {
|
||||
groupMap := make(map[string]*DeliveryGroupDTO)
|
||||
|
||||
for _, product := range products {
|
||||
if product.DeliveryDate == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
var warehouseId uint
|
||||
var warehouseName string
|
||||
if product.ProductWarehouse != nil {
|
||||
warehouseId = product.ProductWarehouse.Warehouse.Id
|
||||
warehouseName = product.ProductWarehouse.Warehouse.Name
|
||||
}
|
||||
|
||||
key := fmt.Sprintf("%d_%s", warehouseId, product.DeliveryDate.Format("2006-01-02"))
|
||||
|
||||
group, exists := groupMap[key]
|
||||
if !exists {
|
||||
group = &DeliveryGroupDTO{
|
||||
DeliveryDate: product.DeliveryDate,
|
||||
Warehouse: &productwarehouseDTO.WarehouseBaseDTO{
|
||||
Id: warehouseId,
|
||||
Name: warehouseName,
|
||||
},
|
||||
Deliveries: []DeliveryItemDTO{},
|
||||
}
|
||||
groupMap[key] = group
|
||||
}
|
||||
|
||||
deliveryItem := DeliveryItemDTO{
|
||||
ProductWarehouse: product.ProductWarehouse,
|
||||
Qty: product.Qty,
|
||||
UnitPrice: product.UnitPrice,
|
||||
TotalWeight: product.TotalWeight,
|
||||
AvgWeight: product.AvgWeight,
|
||||
TotalPrice: product.TotalPrice,
|
||||
VehicleNumber: product.VehicleNumber,
|
||||
}
|
||||
group.Deliveries = append(group.Deliveries, deliveryItem)
|
||||
}
|
||||
|
||||
var groups []DeliveryGroupDTO
|
||||
for _, group := range groupMap {
|
||||
groups = append(groups, *group)
|
||||
}
|
||||
|
||||
sort.Slice(groups, func(i, j int) bool {
|
||||
if groups[i].DeliveryDate == nil || groups[j].DeliveryDate == nil {
|
||||
return false
|
||||
}
|
||||
return groups[i].DeliveryDate.Before(*groups[j].DeliveryDate)
|
||||
})
|
||||
|
||||
for i := range groups {
|
||||
if groups[i].DeliveryDate != nil {
|
||||
dateStr := groups[i].DeliveryDate.Format("20060102")
|
||||
groups[i].DoNumber = fmt.Sprintf("%s-%s-%d", soNumber, dateStr, groups[i].Warehouse.Id)
|
||||
}
|
||||
}
|
||||
|
||||
return groups
|
||||
}
|
||||
|
||||
func getVehicleNumber(e entity.MarketingProduct) string {
|
||||
if e.DeliveryProduct != nil && e.DeliveryProduct.VehicleNumber != "" {
|
||||
return e.DeliveryProduct.VehicleNumber
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package delivery_orderss
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gorm.io/gorm"
|
||||
|
||||
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||
rMarketingDeliveryProduct "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/marketing-delivery-products/repositories"
|
||||
sDeliveryOrders "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/delivery-orderss/services"
|
||||
rMarketing "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/sales-orders/repositories"
|
||||
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
|
||||
sUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
)
|
||||
|
||||
type DeliveryOrdersModule struct{}
|
||||
|
||||
func (DeliveryOrdersModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
|
||||
marketingRepo := rMarketing.NewMarketingRepository(db)
|
||||
marketingProductRepo := rMarketing.NewMarketingProductRepository(db)
|
||||
marketingDeliveryProductRepo := rMarketingDeliveryProduct.NewMarketingDeliveryProductRepository(db)
|
||||
userRepo := rUser.NewUserRepository(db)
|
||||
approvalRepo := commonRepo.NewApprovalRepository(db)
|
||||
approvalSvc := commonSvc.NewApprovalService(approvalRepo)
|
||||
|
||||
// Register workflow steps for MARKETINGS approval
|
||||
if err := approvalSvc.RegisterWorkflowSteps(utils.ApprovalWorkflowMarketing, utils.MarketingApprovalSteps); err != nil {
|
||||
panic(fmt.Sprintf("failed to register marketing approval workflow: %v", err))
|
||||
}
|
||||
|
||||
deliveryOrdersService := sDeliveryOrders.NewDeliveryOrdersService(marketingRepo, marketingProductRepo, marketingDeliveryProductRepo, approvalSvc, validate)
|
||||
userService := sUser.NewUserService(userRepo, validate)
|
||||
|
||||
DeliveryOrdersRoutes(router, userService, deliveryOrdersService)
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package delivery_orderss
|
||||
|
||||
import (
|
||||
// m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
||||
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/delivery-orderss/controllers"
|
||||
deliveryOrders "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/delivery-orderss/services"
|
||||
user "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func DeliveryOrdersRoutes(v1 fiber.Router, u user.UserService, s deliveryOrders.DeliveryOrdersService) {
|
||||
ctrl := controller.NewDeliveryOrdersController(s)
|
||||
|
||||
v1.Get("/", ctrl.GetAll)
|
||||
v1.Get("/:id", ctrl.GetOne)
|
||||
|
||||
// Sisanya di group /delivery-orders
|
||||
route := v1.Group("/delivery-orders")
|
||||
|
||||
// route.Get("/", m.Auth(u), ctrl.GetAll)
|
||||
// route.Post("/", m.Auth(u), ctrl.CreateOne)
|
||||
// route.Get("/:id", m.Auth(u), ctrl.GetOne)
|
||||
// route.Patch("/:id", m.Auth(u), ctrl.UpdateOne)
|
||||
// route.Delete("/:id", m.Auth(u), ctrl.DeleteOne)
|
||||
|
||||
route.Post("/", ctrl.CreateOne)
|
||||
route.Patch("/:id", ctrl.UpdateOne)
|
||||
|
||||
}
|
||||
@@ -0,0 +1,427 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
marketingDeliveryProductRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/marketing-delivery-products/repositories"
|
||||
productWarehouseRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/repositories"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/delivery-orderss/dto"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/delivery-orderss/validations"
|
||||
marketingRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/sales-orders/repositories"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type DeliveryOrdersService interface {
|
||||
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]dto.MarketingListDTO, int64, error)
|
||||
GetOne(ctx *fiber.Ctx, id uint) (*dto.MarketingDetailDTO, error)
|
||||
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*dto.MarketingDetailDTO, error)
|
||||
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*dto.MarketingDetailDTO, error)
|
||||
}
|
||||
|
||||
type deliveryOrdersService struct {
|
||||
Log *logrus.Logger
|
||||
Validate *validator.Validate
|
||||
MarketingRepo marketingRepo.MarketingRepository
|
||||
MarketingProductRepo marketingRepo.MarketingProductRepository
|
||||
MarketingDeliveryProductRepo marketingDeliveryProductRepo.MarketingDeliveryProductRepository
|
||||
ApprovalSvc commonSvc.ApprovalService
|
||||
}
|
||||
|
||||
func NewDeliveryOrdersService(
|
||||
marketingRepo marketingRepo.MarketingRepository,
|
||||
marketingProductRepo marketingRepo.MarketingProductRepository,
|
||||
marketingDeliveryProductRepo marketingDeliveryProductRepo.MarketingDeliveryProductRepository,
|
||||
approvalSvc commonSvc.ApprovalService,
|
||||
validate *validator.Validate,
|
||||
) DeliveryOrdersService {
|
||||
return &deliveryOrdersService{
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
MarketingRepo: marketingRepo,
|
||||
MarketingProductRepo: marketingProductRepo,
|
||||
MarketingDeliveryProductRepo: marketingDeliveryProductRepo,
|
||||
ApprovalSvc: approvalSvc,
|
||||
}
|
||||
}
|
||||
|
||||
func (s deliveryOrdersService) withRelations(db *gorm.DB) *gorm.DB {
|
||||
return db.
|
||||
Preload("CreatedUser").
|
||||
Preload("Customer").
|
||||
Preload("SalesPerson").
|
||||
Preload("Products.ProductWarehouse.Product").
|
||||
Preload("Products.ProductWarehouse.Warehouse").
|
||||
Preload("Products.DeliveryProduct")
|
||||
}
|
||||
|
||||
func (s deliveryOrdersService) getMarketingWithDeliveries(c *fiber.Ctx, marketingId uint) (*dto.MarketingDetailDTO, error) {
|
||||
marketing, err := s.MarketingRepo.GetByID(c.Context(), marketingId, s.withRelations)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch marketing")
|
||||
}
|
||||
|
||||
latestApproval, err := s.ApprovalSvc.LatestByTarget(c.Context(), utils.ApprovalWorkflowMarketing, marketingId, nil)
|
||||
if err != nil {
|
||||
}
|
||||
marketing.LatestApproval = latestApproval
|
||||
|
||||
allDeliveryProducts, err := s.MarketingDeliveryProductRepo.GetByMarketingId(c.Context(), marketingId)
|
||||
if err != nil {
|
||||
allDeliveryProducts = []entity.MarketingDeliveryProduct{}
|
||||
}
|
||||
|
||||
responseDTO := dto.ToMarketingDetailDTO(marketing, allDeliveryProducts)
|
||||
return &responseDTO, nil
|
||||
}
|
||||
|
||||
func (s deliveryOrdersService) GetAll(c *fiber.Ctx, params *validation.Query) ([]dto.MarketingListDTO, int64, error) {
|
||||
if err := s.Validate.Struct(params); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
offset := (params.Page - 1) * params.Limit
|
||||
|
||||
marketings, total, err := s.MarketingRepo.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
||||
db = db.
|
||||
Preload("CreatedUser").
|
||||
Preload("Customer").
|
||||
Preload("SalesPerson").
|
||||
Preload("Products.ProductWarehouse.Product").
|
||||
Preload("Products.ProductWarehouse.Warehouse").
|
||||
Preload("Products.DeliveryProduct")
|
||||
|
||||
if params.MarketingId != 0 {
|
||||
return db.Where("id = ?", params.MarketingId)
|
||||
}
|
||||
return db.Order("created_at DESC").Order("updated_at DESC")
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get marketings: %+v", err)
|
||||
return nil, 0, err
|
||||
}
|
||||
for i := range marketings {
|
||||
latestApproval, err := s.ApprovalSvc.LatestByTarget(c.Context(), utils.ApprovalWorkflowMarketing, marketings[i].Id, func(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("ActionUser")
|
||||
})
|
||||
if err != nil {
|
||||
s.Log.Warnf("Failed to load approval for marketing %d: %+v", marketings[i].Id, err)
|
||||
}
|
||||
marketings[i].LatestApproval = latestApproval
|
||||
}
|
||||
|
||||
result := make([]dto.MarketingListDTO, len(marketings))
|
||||
for i, marketing := range marketings {
|
||||
result[i] = dto.ToMarketingListDTO(&marketing, []entity.MarketingDeliveryProduct{})
|
||||
}
|
||||
|
||||
return result, total, nil
|
||||
}
|
||||
|
||||
func (s deliveryOrdersService) GetOne(c *fiber.Ctx, id uint) (*dto.MarketingDetailDTO, error) {
|
||||
|
||||
marketing, err := s.MarketingRepo.GetByID(c.Context(), id, s.withRelations)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "Marketing not found")
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
allDeliveryProducts, err := s.MarketingDeliveryProductRepo.GetByMarketingId(c.Context(), id)
|
||||
if err != nil {
|
||||
allDeliveryProducts = []entity.MarketingDeliveryProduct{}
|
||||
}
|
||||
|
||||
if s.ApprovalSvc != nil {
|
||||
approvals, err := s.ApprovalSvc.ListByTarget(c.Context(), utils.ApprovalWorkflowMarketing, marketing.Id, func(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("ActionUser")
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
} else if len(approvals) > 0 {
|
||||
if marketing.LatestApproval == nil {
|
||||
latest := approvals[len(approvals)-1]
|
||||
marketing.LatestApproval = &latest
|
||||
}
|
||||
} else {
|
||||
marketing.LatestApproval = nil
|
||||
}
|
||||
}
|
||||
|
||||
responseDTO := dto.ToMarketingDetailDTO(marketing, allDeliveryProducts)
|
||||
return &responseDTO, nil
|
||||
}
|
||||
|
||||
func (s *deliveryOrdersService) CreateOne(c *fiber.Ctx, req *validation.Create) (*dto.MarketingDetailDTO, error) {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := commonSvc.EnsureRelations(c.Context(),
|
||||
commonSvc.RelationCheck{Name: "Marketing", ID: &req.MarketingId, Exists: s.MarketingRepo.IdExists},
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
approvalSvc := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(s.MarketingRepo.DB()))
|
||||
|
||||
latestApproval, err := approvalSvc.LatestByTarget(c.Context(), utils.ApprovalWorkflowMarketing, req.MarketingId, nil)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check approval status")
|
||||
}
|
||||
if latestApproval == nil {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Marketing has not been submitted for approval")
|
||||
}
|
||||
if latestApproval.StepNumber < uint16(utils.MarketingStepSalesOrder) {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Marketing must be approved to Sales Order step before creating delivery order")
|
||||
}
|
||||
if latestApproval.StepNumber >= uint16(utils.MarketingDeliveryOrder) {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Delivery order already exists for this marketing")
|
||||
}
|
||||
if latestApproval.Action == nil || *latestApproval.Action != entity.ApprovalActionApproved {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Marketing is not approved - current status: %v", *latestApproval.Action))
|
||||
}
|
||||
|
||||
err = s.MarketingRepo.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
||||
|
||||
marketingProductRepositoryTx := marketingRepo.NewMarketingProductRepository(dbTransaction)
|
||||
marketingDeliveryProductRepositoryTx := marketingDeliveryProductRepo.NewMarketingDeliveryProductRepository(dbTransaction)
|
||||
approvalSvcTx := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(dbTransaction))
|
||||
|
||||
allMarketingProducts, err := marketingProductRepositoryTx.GetByMarketingID(c.Context(), req.MarketingId)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("No marketing products found for marketing %d", req.MarketingId))
|
||||
}
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch marketing products")
|
||||
}
|
||||
|
||||
for _, requestedProduct := range req.DeliveryProducts {
|
||||
var foundMarketingProduct *entity.MarketingProduct
|
||||
for i := range allMarketingProducts {
|
||||
if allMarketingProducts[i].Id == requestedProduct.MarketingProductId {
|
||||
foundMarketingProduct = &allMarketingProducts[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
if foundMarketingProduct == nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Marketing product %d not found for this marketing", requestedProduct.MarketingProductId))
|
||||
}
|
||||
|
||||
deliveryProduct, err := marketingDeliveryProductRepositoryTx.GetByMarketingProductID(c.Context(), foundMarketingProduct.Id)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Delivery product for marketing product %d not found", requestedProduct.MarketingProductId))
|
||||
}
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch delivery product")
|
||||
}
|
||||
|
||||
var itemDeliveryDate *time.Time
|
||||
if requestedProduct.DeliveryDate != "" {
|
||||
parsedDate, err := utils.ParseDateString(requestedProduct.DeliveryDate)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Invalid delivery date format for product %d: %v", requestedProduct.MarketingProductId, err))
|
||||
}
|
||||
itemDeliveryDate = &parsedDate
|
||||
}
|
||||
|
||||
deliveryProduct.Qty = requestedProduct.Qty
|
||||
deliveryProduct.UnitPrice = requestedProduct.UnitPrice
|
||||
deliveryProduct.AvgWeight = requestedProduct.AvgWeight
|
||||
deliveryProduct.TotalWeight = requestedProduct.TotalWeight
|
||||
deliveryProduct.TotalPrice = requestedProduct.TotalPrice
|
||||
deliveryProduct.DeliveryDate = itemDeliveryDate
|
||||
deliveryProduct.VehicleNumber = requestedProduct.VehicleNumber
|
||||
|
||||
if requestedProduct.Qty > 0 {
|
||||
if err := s.validateAndReduceProductWarehouse(c.Context(), dbTransaction, foundMarketingProduct, requestedProduct.Qty); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := marketingDeliveryProductRepositoryTx.UpdateOne(c.Context(), deliveryProduct.Id, deliveryProduct, nil); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update delivery product")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
actorID := uint(1) // TODO: ambil dari auth context
|
||||
approvalAction := entity.ApprovalActionApproved
|
||||
if _, err := approvalSvcTx.CreateApproval(
|
||||
c.Context(),
|
||||
utils.ApprovalWorkflowMarketing,
|
||||
req.MarketingId,
|
||||
utils.MarketingDeliveryOrder,
|
||||
&approvalAction,
|
||||
actorID,
|
||||
nil); err != nil {
|
||||
if !errors.Is(err, gorm.ErrDuplicatedKey) {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create delivery order approval")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if fiberErr, ok := err.(*fiber.Error); ok {
|
||||
return nil, fiberErr
|
||||
}
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to create delivery order")
|
||||
}
|
||||
|
||||
return s.getMarketingWithDeliveries(c, req.MarketingId)
|
||||
}
|
||||
|
||||
func (s deliveryOrdersService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*dto.MarketingDetailDTO, error) {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := commonSvc.EnsureRelations(c.Context(),
|
||||
commonSvc.RelationCheck{Name: "Marketing", ID: &id, Exists: s.MarketingRepo.IdExists},
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err := s.MarketingRepo.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
||||
|
||||
marketingProductRepositoryTx := marketingRepo.NewMarketingProductRepository(dbTransaction)
|
||||
marketingDeliveryProductRepositoryTx := marketingDeliveryProductRepo.NewMarketingDeliveryProductRepository(dbTransaction)
|
||||
|
||||
allMarketingProducts, err := marketingProductRepositoryTx.GetByMarketingID(c.Context(), id)
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch marketing products")
|
||||
}
|
||||
|
||||
if len(req.DeliveryProducts) > 0 {
|
||||
for _, requestedProduct := range req.DeliveryProducts {
|
||||
|
||||
var foundMarketingProduct *entity.MarketingProduct
|
||||
for i := range allMarketingProducts {
|
||||
if allMarketingProducts[i].Id == requestedProduct.MarketingProductId {
|
||||
foundMarketingProduct = &allMarketingProducts[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
if foundMarketingProduct == nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Marketing product %d not found for this marketing", requestedProduct.MarketingProductId))
|
||||
}
|
||||
|
||||
deliveryProduct, err := marketingDeliveryProductRepositoryTx.GetByMarketingProductID(c.Context(), foundMarketingProduct.Id)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Delivery product for marketing product %d not found", requestedProduct.MarketingProductId))
|
||||
}
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch delivery product")
|
||||
}
|
||||
|
||||
var itemDeliveryDate *time.Time
|
||||
if requestedProduct.DeliveryDate != "" {
|
||||
parsedDate, err := utils.ParseDateString(requestedProduct.DeliveryDate)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Invalid delivery date format for product %d: %v", requestedProduct.MarketingProductId, err))
|
||||
}
|
||||
itemDeliveryDate = &parsedDate
|
||||
} else if deliveryProduct.DeliveryDate != nil {
|
||||
itemDeliveryDate = deliveryProduct.DeliveryDate
|
||||
}
|
||||
|
||||
oldQty := deliveryProduct.Qty
|
||||
deliveryProduct.Qty = requestedProduct.Qty
|
||||
deliveryProduct.UnitPrice = requestedProduct.UnitPrice
|
||||
deliveryProduct.AvgWeight = requestedProduct.AvgWeight
|
||||
deliveryProduct.TotalWeight = requestedProduct.TotalWeight
|
||||
deliveryProduct.TotalPrice = requestedProduct.TotalPrice
|
||||
deliveryProduct.DeliveryDate = itemDeliveryDate
|
||||
deliveryProduct.VehicleNumber = requestedProduct.VehicleNumber
|
||||
|
||||
qtyChange := requestedProduct.Qty - oldQty
|
||||
if qtyChange > 0 {
|
||||
if err := s.validateAndReduceProductWarehouse(c.Context(), dbTransaction, foundMarketingProduct, qtyChange); err != nil {
|
||||
return err
|
||||
}
|
||||
} else if qtyChange < 0 {
|
||||
if err := s.restoreProductWarehouseStock(c.Context(), dbTransaction, foundMarketingProduct, -qtyChange); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := marketingDeliveryProductRepositoryTx.UpdateOne(c.Context(), deliveryProduct.Id, deliveryProduct, nil); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update delivery product")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if fiberErr, ok := err.(*fiber.Error); ok {
|
||||
return nil, fiberErr
|
||||
}
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to update delivery order")
|
||||
}
|
||||
|
||||
return s.getMarketingWithDeliveries(c, id)
|
||||
}
|
||||
|
||||
func (s deliveryOrdersService) validateAndReduceProductWarehouse(ctx context.Context, tx *gorm.DB, marketingProduct *entity.MarketingProduct, qtyDeliver float64) error {
|
||||
if marketingProduct == nil || marketingProduct.ProductWarehouseId == 0 {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Product warehouse not found")
|
||||
}
|
||||
|
||||
pwRepo := productWarehouseRepo.NewProductWarehouseRepository(tx)
|
||||
|
||||
pw, err := pwRepo.GetByID(ctx, marketingProduct.ProductWarehouseId, nil)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, "Product warehouse not found")
|
||||
}
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to check stock")
|
||||
}
|
||||
|
||||
if pw.Quantity < qtyDeliver {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Insufficient stock for warehouse - available: %.2f, requested: %.2f", pw.Quantity, qtyDeliver))
|
||||
}
|
||||
|
||||
pw.Quantity = pw.Quantity - qtyDeliver
|
||||
if err := pwRepo.UpdateOne(ctx, pw.Id, pw, nil); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update stock")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s deliveryOrdersService) restoreProductWarehouseStock(ctx context.Context, tx *gorm.DB, marketingProduct *entity.MarketingProduct, qtyRestore float64) error {
|
||||
if marketingProduct == nil || marketingProduct.ProductWarehouseId == 0 {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Product warehouse not found")
|
||||
}
|
||||
|
||||
pwRepo := productWarehouseRepo.NewProductWarehouseRepository(tx)
|
||||
pw, err := pwRepo.GetByID(ctx, marketingProduct.ProductWarehouseId, nil)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, "Product warehouse not found")
|
||||
}
|
||||
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to check stock")
|
||||
}
|
||||
|
||||
pw.Quantity = pw.Quantity + qtyRestore
|
||||
if err := pwRepo.UpdateOne(ctx, pw.Id, pw, nil); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update stock")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package validation
|
||||
|
||||
type DeliveryProduct struct {
|
||||
MarketingProductId uint `json:"marketing_product_id" validate:"required,gt=0"`
|
||||
Qty float64 `json:"qty" validate:"omitempty,gte=0"`
|
||||
UnitPrice float64 `json:"unit_price" validate:"omitempty,gte=0"`
|
||||
AvgWeight float64 `json:"avg_weight" validate:"omitempty,gte=0"`
|
||||
TotalWeight float64 `json:"total_weight" validate:"omitempty,gte=0"`
|
||||
TotalPrice float64 `json:"total_price" validate:"omitempty,gte=0"`
|
||||
DeliveryDate string `json:"delivery_date" validate:"omitempty,datetime=2006-01-02"`
|
||||
VehicleNumber string `json:"vehicle_number" validate:"omitempty,max=50"`
|
||||
}
|
||||
|
||||
type Create struct {
|
||||
MarketingId uint `json:"marketing_id" validate:"required,gt=0"`
|
||||
DeliveryProducts []DeliveryProduct `json:"delivery_products" validate:"required,min=1,dive"`
|
||||
}
|
||||
|
||||
type Update struct {
|
||||
DeliveryProducts []DeliveryProduct `json:"delivery_products" validate:"omitempty,min=1,dive"`
|
||||
}
|
||||
|
||||
type Query struct {
|
||||
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
|
||||
MarketingId uint `query:"marketing_id" validate:"omitempty,gt=0"`
|
||||
}
|
||||
|
||||
type Approve struct {
|
||||
Action string `json:"action" validate:"required_strict"`
|
||||
ApprovableIds []uint `json:"approvable_ids" validate:"required_strict,min=1,dive,gt=0"`
|
||||
Notes *string `json:"notes,omitempty" validate:"omitempty,max=500"`
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package marketing
|
||||
|
||||
import (
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type MarketingModule struct{}
|
||||
|
||||
func (MarketingModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
|
||||
RegisterRoutes(router, db, validate)
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package marketing
|
||||
|
||||
import (
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/modules"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gorm.io/gorm"
|
||||
|
||||
salesOrderss "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/sales-orders"
|
||||
deliveryOrderss "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/delivery-orderss"
|
||||
// MODULE IMPORTS
|
||||
)
|
||||
|
||||
func RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
|
||||
group := router.Group("/marketing")
|
||||
|
||||
allModules := []modules.Module{
|
||||
salesOrderss.SalesOrdersModule{},
|
||||
deliveryOrderss.DeliveryOrdersModule{},
|
||||
// MODULE REGISTRY
|
||||
}
|
||||
|
||||
for _, m := range allModules {
|
||||
m.RegisterRoutes(group, db, validate)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/sales-orders/dto"
|
||||
service "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/sales-orders/services"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/sales-orders/validations"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/response"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type SalesOrdersController struct {
|
||||
SalesOrdersService service.SalesOrdersService
|
||||
}
|
||||
|
||||
func NewSalesOrdersController(salesOrdersService service.SalesOrdersService) *SalesOrdersController {
|
||||
return &SalesOrdersController{
|
||||
SalesOrdersService: salesOrdersService,
|
||||
}
|
||||
}
|
||||
|
||||
func (u *SalesOrdersController) CreateOne(c *fiber.Ctx) error {
|
||||
req := new(validation.Create)
|
||||
|
||||
if err := c.BodyParser(req); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
}
|
||||
|
||||
result, err := u.SalesOrdersService.CreateOne(c, req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusCreated).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusCreated,
|
||||
Status: "success",
|
||||
Message: "Create salesOrders successfully",
|
||||
Data: dto.ToSalesOrdersListDTOFromMarketing(*result),
|
||||
})
|
||||
}
|
||||
|
||||
func (u *SalesOrdersController) UpdateOne(c *fiber.Ctx) error {
|
||||
req := new(validation.Update)
|
||||
param := c.Params("id")
|
||||
|
||||
id, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||
}
|
||||
|
||||
if err := c.BodyParser(req); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
}
|
||||
|
||||
result, err := u.SalesOrdersService.UpdateOne(c, req, uint(id))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Update salesOrders successfully",
|
||||
Data: dto.ToSalesOrdersListDTOFromMarketing(*result),
|
||||
})
|
||||
}
|
||||
|
||||
func (u *SalesOrdersController) DeleteOne(c *fiber.Ctx) error {
|
||||
param := c.Params("id")
|
||||
|
||||
id, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||
}
|
||||
|
||||
if err := u.SalesOrdersService.DeleteOne(c, uint(id)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Common{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Delete salesOrders successfully",
|
||||
})
|
||||
}
|
||||
|
||||
func (u *SalesOrdersController) Approval(c *fiber.Ctx) error {
|
||||
req := new(validation.Approve)
|
||||
|
||||
if err := c.BodyParser(req); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
}
|
||||
|
||||
results, err := u.SalesOrdersService.Approval(c, req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var (
|
||||
data interface{}
|
||||
message = "Submit sales order approval successfully"
|
||||
)
|
||||
if len(results) == 1 {
|
||||
data = dto.ToSalesOrdersListDTOFromMarketing(results[0])
|
||||
} else {
|
||||
message = "Submit sales order approvals successfully"
|
||||
data = dto.ToSalesOrdersListDTOsFromMarketing(results)
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: message,
|
||||
Data: data,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
productWarehouseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/dto"
|
||||
)
|
||||
|
||||
// === DTO Structs ===
|
||||
|
||||
type MarketingProductDTO struct {
|
||||
Id uint `json:"id"`
|
||||
Qty float64 `json:"qty"`
|
||||
UnitPrice float64 `json:"unit_price"`
|
||||
AvgWeight float64 `json:"avg_weight"`
|
||||
TotalWeight float64 `json:"total_weight"`
|
||||
TotalPrice float64 `json:"total_price"`
|
||||
ProductWarehouse *productWarehouseDTO.ProductWarehousNestedDTO `json:"product_warehouse,omitempty"`
|
||||
}
|
||||
|
||||
type SalesOrdersListDTO struct {
|
||||
Id uint `json:"id"`
|
||||
SoNumber string `json:"so_number"`
|
||||
SoDate time.Time `json:"so_date"`
|
||||
Notes string `json:"notes,omitempty"`
|
||||
SalesOrder []MarketingProductDTO `json:"sales_order,omitempty"`
|
||||
}
|
||||
|
||||
// === Mapper Functions ===
|
||||
|
||||
func ToMarketingProductDTO(e entity.MarketingProduct) MarketingProductDTO {
|
||||
var productWarehouse *productWarehouseDTO.ProductWarehousNestedDTO
|
||||
|
||||
if e.ProductWarehouse.Id != 0 {
|
||||
mapped := productWarehouseDTO.ToProductWarehouseNestedDTO(e.ProductWarehouse)
|
||||
productWarehouse = &mapped
|
||||
}
|
||||
|
||||
return MarketingProductDTO{
|
||||
Id: e.Id,
|
||||
Qty: e.Qty,
|
||||
UnitPrice: e.UnitPrice,
|
||||
AvgWeight: e.AvgWeight,
|
||||
TotalWeight: e.TotalWeight,
|
||||
TotalPrice: e.TotalPrice,
|
||||
ProductWarehouse: productWarehouse,
|
||||
}
|
||||
}
|
||||
|
||||
func ToSalesOrdersListDTO(e entity.Marketing) SalesOrdersListDTO {
|
||||
products := make([]MarketingProductDTO, len(e.Products))
|
||||
for i, p := range e.Products {
|
||||
products[i] = ToMarketingProductDTO(p)
|
||||
}
|
||||
|
||||
return SalesOrdersListDTO{
|
||||
Id: e.Id,
|
||||
SoNumber: e.SoNumber,
|
||||
SoDate: e.SoDate,
|
||||
Notes: e.Notes,
|
||||
SalesOrder: products,
|
||||
}
|
||||
}
|
||||
|
||||
func ToSalesOrdersListDTOFromMarketing(e entity.Marketing) SalesOrdersListDTO {
|
||||
var salesOrder []MarketingProductDTO
|
||||
if len(e.Products) > 0 {
|
||||
salesOrder = make([]MarketingProductDTO, len(e.Products))
|
||||
for i, product := range e.Products {
|
||||
salesOrder[i] = ToMarketingProductDTO(product)
|
||||
}
|
||||
}
|
||||
|
||||
return SalesOrdersListDTO{
|
||||
Id: e.Id,
|
||||
SoNumber: e.SoNumber,
|
||||
SoDate: e.SoDate,
|
||||
Notes: e.Notes,
|
||||
SalesOrder: salesOrder,
|
||||
}
|
||||
}
|
||||
|
||||
func ToSalesOrdersListDTOsFromMarketing(e []entity.Marketing) []SalesOrdersListDTO {
|
||||
result := make([]SalesOrdersListDTO, len(e))
|
||||
for i, r := range e {
|
||||
result[i] = ToSalesOrdersListDTOFromMarketing(r)
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package sales_orders
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gorm.io/gorm"
|
||||
|
||||
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||
rProductWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/repositories"
|
||||
rSalesOrders "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/sales-orders/repositories"
|
||||
sSalesOrders "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/sales-orders/services"
|
||||
rCustomer "gitlab.com/mbugroup/lti-api.git/internal/modules/master/customers/repositories"
|
||||
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
|
||||
sUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
)
|
||||
|
||||
type SalesOrdersModule struct{}
|
||||
|
||||
func (SalesOrdersModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
|
||||
marketingRepo := rSalesOrders.NewMarketingRepository(db)
|
||||
userRepo := rUser.NewUserRepository(db)
|
||||
customerRepo := rCustomer.NewCustomerRepository(db)
|
||||
productWarehouseRepo := rProductWarehouse.NewProductWarehouseRepository(db)
|
||||
|
||||
approvalSvc := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(db))
|
||||
|
||||
if err := approvalSvc.RegisterWorkflowSteps(utils.ApprovalWorkflowMarketing, utils.MarketingApprovalSteps); err != nil {
|
||||
panic(fmt.Sprintf("failed to register marketing approval workflow: %v", err))
|
||||
}
|
||||
|
||||
salesOrdersService := sSalesOrders.NewSalesOrdersService(marketingRepo, customerRepo, productWarehouseRepo, userRepo, approvalSvc, validate)
|
||||
userService := sUser.NewUserService(userRepo, validate)
|
||||
|
||||
SalesOrdersRoutes(router, userService, salesOrdersService)
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type MarketingDeliveryProductRepository interface {
|
||||
repository.BaseRepository[entity.MarketingDeliveryProduct]
|
||||
}
|
||||
|
||||
type MarketingDeliveryProductRepositoryImpl struct {
|
||||
*repository.BaseRepositoryImpl[entity.MarketingDeliveryProduct]
|
||||
}
|
||||
|
||||
func NewMarketingDeliveryProductRepository(db *gorm.DB) MarketingDeliveryProductRepository {
|
||||
return &MarketingDeliveryProductRepositoryImpl{
|
||||
BaseRepositoryImpl: repository.NewBaseRepository[entity.MarketingDeliveryProduct](db),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type MarketingProductRepository interface {
|
||||
repository.BaseRepository[entity.MarketingProduct]
|
||||
GetByMarketingID(ctx context.Context, marketingID uint) ([]entity.MarketingProduct, error)
|
||||
IdExists(ctx context.Context, id uint) (bool, error)
|
||||
}
|
||||
|
||||
type MarketingProductRepositoryImpl struct {
|
||||
*repository.BaseRepositoryImpl[entity.MarketingProduct]
|
||||
}
|
||||
|
||||
func NewMarketingProductRepository(db *gorm.DB) MarketingProductRepository {
|
||||
return &MarketingProductRepositoryImpl{
|
||||
BaseRepositoryImpl: repository.NewBaseRepository[entity.MarketingProduct](db),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *MarketingProductRepositoryImpl) GetByMarketingID(ctx context.Context, marketingID uint) ([]entity.MarketingProduct, error) {
|
||||
var products []entity.MarketingProduct
|
||||
if err := r.DB().WithContext(ctx).Where("marketing_id = ?", marketingID).Find(&products).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(products) == 0 {
|
||||
return products, gorm.ErrRecordNotFound
|
||||
}
|
||||
return products, nil
|
||||
}
|
||||
|
||||
func (r *MarketingProductRepositoryImpl) IdExists(ctx context.Context, id uint) (bool, error) {
|
||||
return repository.Exists[entity.MarketingProduct](ctx, r.DB(), id)
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type MarketingRepository interface {
|
||||
repository.BaseRepository[entity.Marketing]
|
||||
IdExists(ctx context.Context, id uint) (bool, error)
|
||||
GetNextSequence(ctx context.Context) (uint, error)
|
||||
}
|
||||
|
||||
type MarketingRepositoryImpl struct {
|
||||
*repository.BaseRepositoryImpl[entity.Marketing]
|
||||
}
|
||||
|
||||
func NewMarketingRepository(db *gorm.DB) MarketingRepository {
|
||||
return &MarketingRepositoryImpl{
|
||||
BaseRepositoryImpl: repository.NewBaseRepository[entity.Marketing](db),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *MarketingRepositoryImpl) IdExists(ctx context.Context, id uint) (bool, error) {
|
||||
return repository.Exists[entity.Marketing](ctx, r.DB(), id)
|
||||
}
|
||||
|
||||
func (r *MarketingRepositoryImpl) GetNextSequence(ctx context.Context) (uint, error) {
|
||||
var maxID uint
|
||||
if err := r.DB().WithContext(ctx).Model(&entity.Marketing{}).Select("COALESCE(MAX(id), 0)").Scan(&maxID).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return maxID + 1, nil
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package sales_orders
|
||||
|
||||
import (
|
||||
// m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
||||
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/sales-orders/controllers"
|
||||
salesOrders "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/sales-orders/services"
|
||||
user "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func SalesOrdersRoutes(v1 fiber.Router, u user.UserService, s salesOrders.SalesOrdersService) {
|
||||
ctrl := controller.NewSalesOrdersController(s)
|
||||
|
||||
v1.Delete("/:id", ctrl.DeleteOne)
|
||||
route := v1.Group("/sales-orders")
|
||||
|
||||
// route.Post("/", m.Auth(u), ctrl.CreateOne)
|
||||
// route.Patch("/:id", m.Auth(u), ctrl.UpdateOne)
|
||||
// route.Delete("/:id", m.Auth(u), ctrl.DeleteOne)
|
||||
|
||||
route.Post("/", ctrl.CreateOne)
|
||||
route.Patch("/:id", ctrl.UpdateOne)
|
||||
|
||||
route.Post("/approvals", ctrl.Approval)
|
||||
}
|
||||
@@ -0,0 +1,548 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
rInvMarketingDeliveryProduct "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/marketing-delivery-products/repositories"
|
||||
productWarehouseRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/repositories"
|
||||
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/sales-orders/repositories"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/sales-orders/validations"
|
||||
customerRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/master/customers/repositories"
|
||||
userRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
approvalutils "gitlab.com/mbugroup/lti-api.git/internal/utils/approvals"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type SalesOrdersService interface {
|
||||
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.Marketing, error)
|
||||
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.Marketing, error)
|
||||
DeleteOne(ctx *fiber.Ctx, id uint) error
|
||||
Approval(ctx *fiber.Ctx, req *validation.Approve) ([]entity.Marketing, error)
|
||||
}
|
||||
|
||||
type salesOrdersService struct {
|
||||
Log *logrus.Logger
|
||||
Validate *validator.Validate
|
||||
MarketingRepo repository.MarketingRepository
|
||||
CustomerRepo customerRepo.CustomerRepository
|
||||
ProductWarehouseRepo productWarehouseRepo.ProductWarehouseRepository
|
||||
UserRepo userRepo.UserRepository
|
||||
ApprovalSvc commonSvc.ApprovalService
|
||||
}
|
||||
|
||||
func NewSalesOrdersService(marketingRepo repository.MarketingRepository, customerRepo customerRepo.CustomerRepository, productWarehouseRepo productWarehouseRepo.ProductWarehouseRepository, userRepo userRepo.UserRepository, approvalSvc commonSvc.ApprovalService, validate *validator.Validate) SalesOrdersService {
|
||||
return &salesOrdersService{
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
MarketingRepo: marketingRepo,
|
||||
CustomerRepo: customerRepo,
|
||||
ProductWarehouseRepo: productWarehouseRepo,
|
||||
UserRepo: userRepo,
|
||||
ApprovalSvc: approvalSvc,
|
||||
}
|
||||
}
|
||||
|
||||
func (s salesOrdersService) withRelations(db *gorm.DB) *gorm.DB {
|
||||
return db.
|
||||
Preload("CreatedUser").
|
||||
Preload("Customer").
|
||||
Preload("SalesPerson").
|
||||
Preload("Products.ProductWarehouse.Product").
|
||||
Preload("Products.ProductWarehouse.Warehouse")
|
||||
}
|
||||
|
||||
func (s salesOrdersService) getOne(c *fiber.Ctx, id uint) (*entity.Marketing, error) {
|
||||
marketing, err := s.MarketingRepo.GetByID(c.Context(), id, s.withRelations)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "SalesOrders not found")
|
||||
}
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed get marketing by id: %+v", err)
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch sales order")
|
||||
}
|
||||
|
||||
if s.ApprovalSvc != nil {
|
||||
approvals, err := s.ApprovalSvc.ListByTarget(c.Context(), utils.ApprovalWorkflowMarketing, id, func(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("ActionUser")
|
||||
})
|
||||
if err == nil && len(approvals) > 0 {
|
||||
latest := approvals[len(approvals)-1]
|
||||
marketing.LatestApproval = &latest
|
||||
}
|
||||
}
|
||||
|
||||
return marketing, nil
|
||||
}
|
||||
|
||||
func (s *salesOrdersService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.Marketing, error) {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := commonSvc.EnsureRelations(c.Context(),
|
||||
commonSvc.RelationCheck{Name: "Customer", ID: &req.CustomerId, Exists: s.CustomerRepo.IdExists},
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, item := range req.MarketingProducts {
|
||||
if err := commonSvc.EnsureRelations(c.Context(),
|
||||
commonSvc.RelationCheck{Name: "ProductWarehouse", ID: &item.ProductWarehouseId, Exists: s.ProductWarehouseRepo.IdExists},
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
soDate, err := utils.ParseDateString(req.Date)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid date format")
|
||||
}
|
||||
|
||||
nextSeq, err := s.MarketingRepo.GetNextSequence(c.Context())
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to generate SO number")
|
||||
}
|
||||
soNumber := fmt.Sprintf("SO-%05d", nextSeq)
|
||||
|
||||
var marketing *entity.Marketing
|
||||
err = s.MarketingRepo.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
||||
|
||||
marketingRepoTx := repository.NewMarketingRepository(dbTransaction)
|
||||
marketingProductRepoTx := repository.NewMarketingProductRepository(dbTransaction)
|
||||
invDeliveryRepoTx := rInvMarketingDeliveryProduct.NewMarketingDeliveryProductRepository(dbTransaction)
|
||||
approvalSvcTx := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(dbTransaction))
|
||||
|
||||
marketing = &entity.Marketing{
|
||||
CustomerId: req.CustomerId,
|
||||
SoNumber: soNumber,
|
||||
SoDate: soDate,
|
||||
SalesPersonId: req.SalesPersonId,
|
||||
Notes: req.Notes,
|
||||
CreatedBy: 1,
|
||||
}
|
||||
if err := marketingRepoTx.CreateOne(c.Context(), marketing, nil); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create salesOrders")
|
||||
}
|
||||
|
||||
if len(req.MarketingProducts) > 0 {
|
||||
for _, product := range req.MarketingProducts {
|
||||
if err := s.createMarketingProductWithDelivery(c.Context(), marketing.Id, product, marketingProductRepoTx, invDeliveryRepoTx); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create marketing product")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
actorID := uint(1) // TODO: ambil dari auth context
|
||||
approvalAction := entity.ApprovalActionCreated
|
||||
if _, err := approvalSvcTx.CreateApproval(
|
||||
c.Context(),
|
||||
utils.ApprovalWorkflowMarketing,
|
||||
marketing.Id,
|
||||
utils.MarketingStepPengajuan,
|
||||
&approvalAction,
|
||||
actorID,
|
||||
nil); err != nil {
|
||||
if !errors.Is(err, gorm.ErrDuplicatedKey) {
|
||||
fiber.NewError(fiber.StatusInternalServerError, "Failed to create approval")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
if fiberErr, ok := err.(*fiber.Error); ok {
|
||||
return nil, fiberErr
|
||||
}
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to create salesOrders")
|
||||
}
|
||||
|
||||
marketing, err = s.MarketingRepo.GetByID(c.Context(), marketing.Id, s.withRelations)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch created sales order")
|
||||
}
|
||||
|
||||
return marketing, nil
|
||||
}
|
||||
|
||||
func (s salesOrdersService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.Marketing, error) {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := commonSvc.EnsureRelations(c.Context(),
|
||||
commonSvc.RelationCheck{Name: "Marketing", ID: &id, Exists: s.MarketingRepo.IdExists},
|
||||
commonSvc.RelationCheck{Name: "Customer", ID: &req.CustomerId, Exists: s.CustomerRepo.IdExists},
|
||||
commonSvc.RelationCheck{Name: "SalesPerson", ID: &req.SalesPersonId, Exists: s.UserRepo.IdExists},
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
latestApproval, err := s.ApprovalSvc.LatestByTarget(c.Context(), utils.ApprovalWorkflowMarketing, id, nil)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check approval status")
|
||||
}
|
||||
if latestApproval != nil && latestApproval.StepNumber >= 3 {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Cannot update sales order after delivery order approval")
|
||||
}
|
||||
|
||||
if len(req.MarketingProducts) > 0 {
|
||||
for _, item := range req.MarketingProducts {
|
||||
if err := commonSvc.EnsureRelations(c.Context(),
|
||||
commonSvc.RelationCheck{Name: "ProductWarehouse", ID: &item.ProductWarehouseId, Exists: s.ProductWarehouseRepo.IdExists},
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
err = s.MarketingRepo.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
||||
|
||||
marketingRepoTx := repository.NewMarketingRepository(dbTransaction)
|
||||
marketingProductRepoTx := repository.NewMarketingProductRepository(dbTransaction)
|
||||
approvalSvcTx := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(dbTransaction))
|
||||
invDeliveryRepoTx := rInvMarketingDeliveryProduct.NewMarketingDeliveryProductRepository(dbTransaction)
|
||||
|
||||
updateBody := make(map[string]any)
|
||||
if req.CustomerId != 0 {
|
||||
updateBody["customer_id"] = req.CustomerId
|
||||
}
|
||||
if req.SalesPersonId != 0 {
|
||||
updateBody["sales_person_id"] = req.SalesPersonId
|
||||
}
|
||||
if req.Date != "" {
|
||||
soDate, err := utils.ParseDateString(req.Date)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid date format")
|
||||
}
|
||||
updateBody["so_date"] = soDate
|
||||
}
|
||||
if req.Notes != "" {
|
||||
updateBody["notes"] = req.Notes
|
||||
}
|
||||
|
||||
if len(updateBody) > 0 {
|
||||
if err := marketingRepoTx.PatchOne(c.Context(), id, updateBody, nil); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update sales order")
|
||||
}
|
||||
}
|
||||
|
||||
if len(req.MarketingProducts) > 0 {
|
||||
|
||||
oldProducts, err := marketingProductRepoTx.GetByMarketingID(c.Context(), id)
|
||||
if err != nil && err != gorm.ErrRecordNotFound {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch existing products")
|
||||
}
|
||||
|
||||
oldByPW := make(map[uint]*entity.MarketingProduct)
|
||||
for i := range oldProducts {
|
||||
p := oldProducts[i]
|
||||
oldByPW[p.ProductWarehouseId] = &p
|
||||
}
|
||||
|
||||
reqByPW := make(map[uint]validation.CreateMarketingProduct)
|
||||
for _, rp := range req.MarketingProducts {
|
||||
reqByPW[rp.ProductWarehouseId] = rp
|
||||
}
|
||||
|
||||
for _, rp := range req.MarketingProducts {
|
||||
if old, ok := oldByPW[rp.ProductWarehouseId]; ok {
|
||||
|
||||
updateBody := map[string]any{
|
||||
"product_warehouse_id": rp.ProductWarehouseId,
|
||||
"qty": rp.Qty,
|
||||
"unit_price": rp.UnitPrice,
|
||||
"avg_weight": rp.AvgWeight,
|
||||
"total_weight": rp.TotalWeight,
|
||||
"total_price": rp.TotalPrice,
|
||||
}
|
||||
if err := marketingProductRepoTx.PatchOne(c.Context(), old.Id, updateBody, nil); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update marketing product")
|
||||
}
|
||||
|
||||
if _, err := invDeliveryRepoTx.GetByMarketingProductID(c.Context(), old.Id); err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
mdp := &entity.MarketingDeliveryProduct{
|
||||
MarketingProductId: old.Id,
|
||||
Qty: 0,
|
||||
UnitPrice: 0,
|
||||
TotalWeight: 0,
|
||||
AvgWeight: 0,
|
||||
TotalPrice: 0,
|
||||
DeliveryDate: nil,
|
||||
VehicleNumber: rp.VehicleNumber,
|
||||
}
|
||||
if err := invDeliveryRepoTx.CreateOne(c.Context(), mdp, nil); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create marketing delivery product")
|
||||
}
|
||||
} else {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to check delivery product")
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if err := s.createMarketingProductWithDelivery(c.Context(), id, rp, marketingProductRepoTx, invDeliveryRepoTx); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create marketing product")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, old := range oldProducts {
|
||||
if _, ok := reqByPW[old.ProductWarehouseId]; !ok {
|
||||
|
||||
deliveryProduct, err := invDeliveryRepoTx.GetByMarketingProductID(c.Context(), old.Id)
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to check existing delivery product")
|
||||
}
|
||||
if err == nil {
|
||||
|
||||
if deliveryProduct.DeliveryDate != nil || deliveryProduct.Qty > 0 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Cannot delete marketing product %d because it has delivery records", old.Id))
|
||||
}
|
||||
|
||||
if err := invDeliveryRepoTx.DeleteOne(c.Context(), deliveryProduct.Id); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to delete marketing delivery product")
|
||||
}
|
||||
}
|
||||
|
||||
if err := marketingProductRepoTx.DeleteOne(c.Context(), old.Id); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to delete marketing product")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if latestApproval != nil {
|
||||
actorID := uint(1) // todo: ambil dari auth context
|
||||
action := entity.ApprovalActionUpdated
|
||||
_, err := approvalSvcTx.CreateApproval(
|
||||
c.Context(),
|
||||
utils.ApprovalWorkflowMarketing,
|
||||
id,
|
||||
approvalutils.ApprovalStep(latestApproval.StepNumber),
|
||||
&action,
|
||||
actorID,
|
||||
nil)
|
||||
if err != nil {
|
||||
if !errors.Is(err, gorm.ErrDuplicatedKey) {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create update approval")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if fiberErr, ok := err.(*fiber.Error); ok {
|
||||
return nil, fiberErr
|
||||
}
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to update sales order")
|
||||
}
|
||||
|
||||
return s.getOne(c, id)
|
||||
}
|
||||
|
||||
func (s salesOrdersService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||
marketing, err := s.MarketingRepo.GetByID(c.Context(), id, s.withRelations)
|
||||
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, "SalesOrders not found")
|
||||
}
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch sales order")
|
||||
}
|
||||
|
||||
err = s.MarketingRepo.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
||||
|
||||
marketingProductRepoTx := repository.NewMarketingProductRepository(dbTransaction)
|
||||
marketingDeliveryProductRepoTx := repository.NewMarketingDeliveryProductRepository(dbTransaction)
|
||||
marketingRepoTx := repository.NewMarketingRepository(dbTransaction)
|
||||
|
||||
if len(marketing.Products) > 0 {
|
||||
for _, product := range marketing.Products {
|
||||
if err := marketingDeliveryProductRepoTx.DeleteMany(c.Context(), func(db *gorm.DB) *gorm.DB {
|
||||
return db.Where("marketing_product_id = ?", product.Id).Unscoped()
|
||||
}); err != nil && err != gorm.ErrRecordNotFound {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to delete sales order products")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := marketingProductRepoTx.DeleteMany(c.Context(), func(db *gorm.DB) *gorm.DB {
|
||||
return db.Where("marketing_id = ?", id).Unscoped()
|
||||
}); err != nil && err != gorm.ErrRecordNotFound {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to delete sales order products")
|
||||
}
|
||||
|
||||
if err := marketingRepoTx.DeleteOne(c.Context(), id); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to delete sales order")
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if fiberErr, ok := err.(*fiber.Error); ok {
|
||||
return fiberErr
|
||||
}
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to delete sales order")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s salesOrdersService) Approval(c *fiber.Ctx, req *validation.Approve) ([]entity.Marketing, error) {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
approvalSvc := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(s.MarketingRepo.DB()))
|
||||
|
||||
var action entity.ApprovalAction
|
||||
switch strings.ToUpper(strings.TrimSpace(req.Action)) {
|
||||
case string(entity.ApprovalActionRejected):
|
||||
action = entity.ApprovalActionRejected
|
||||
case string(entity.ApprovalActionApproved):
|
||||
action = entity.ApprovalActionApproved
|
||||
default:
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "action must be APPROVED or REJECTED")
|
||||
}
|
||||
|
||||
approvableIDs := utils.UniqueUintSlice(req.ApprovableIds)
|
||||
if len(approvableIDs) == 0 {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "approvable_ids must contain at least one id")
|
||||
}
|
||||
|
||||
for _, id := range approvableIDs {
|
||||
if err := commonSvc.EnsureRelations(c.Context(),
|
||||
commonSvc.RelationCheck{Name: "Marketing", ID: &id, Exists: s.MarketingRepo.IdExists},
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
latestApproval, err := approvalSvc.LatestByTarget(c.Context(), utils.ApprovalWorkflowMarketing, id, nil)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check approval status")
|
||||
}
|
||||
if latestApproval == nil {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("No approval found for Marketing %d - sales orders must be created first", id))
|
||||
}
|
||||
|
||||
if action == entity.ApprovalActionApproved {
|
||||
switch latestApproval.StepNumber {
|
||||
case uint16(utils.MarketingStepPengajuan):
|
||||
case uint16(utils.MarketingStepSalesOrder):
|
||||
default:
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest,
|
||||
fmt.Sprintf("Marketing %d cannot be approved - current step is %d", id, latestApproval.StepNumber))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
err := s.MarketingRepo.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
||||
|
||||
approvalSvc := commonSvc.NewApprovalService(commonRepo.NewApprovalRepository(dbTransaction))
|
||||
|
||||
for _, approvableID := range approvableIDs {
|
||||
latestApproval, err := approvalSvc.LatestByTarget(c.Context(), utils.ApprovalWorkflowMarketing, approvableID, nil)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to check current approval step")
|
||||
}
|
||||
|
||||
if latestApproval == nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("No approval found for Marketing %d", approvableID))
|
||||
}
|
||||
|
||||
var nextStep approvalutils.ApprovalStep
|
||||
currentStep := latestApproval.StepNumber
|
||||
|
||||
if action == entity.ApprovalActionApproved {
|
||||
|
||||
if currentStep == uint16(utils.MarketingStepPengajuan) {
|
||||
nextStep = utils.MarketingStepSalesOrder
|
||||
} else if currentStep == uint16(utils.MarketingStepSalesOrder) {
|
||||
nextStep = utils.MarketingDeliveryOrder
|
||||
} else {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Marketing %d already completed all approval steps", approvableID))
|
||||
}
|
||||
} else {
|
||||
|
||||
nextStep = approvalutils.ApprovalStep(currentStep)
|
||||
}
|
||||
|
||||
actorID := uint(1) // todo ambil dari auth context
|
||||
if _, err := approvalSvc.CreateApproval(
|
||||
c.Context(),
|
||||
utils.ApprovalWorkflowMarketing,
|
||||
approvableID,
|
||||
nextStep,
|
||||
&action,
|
||||
actorID,
|
||||
req.Notes,
|
||||
); err != nil {
|
||||
s.Log.Errorf("Failed to create approval for %d: %+v", approvableID, err)
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create approval")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if fiberErr, ok := err.(*fiber.Error); ok {
|
||||
return nil, fiberErr
|
||||
}
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to record approval")
|
||||
}
|
||||
|
||||
updated := make([]entity.Marketing, 0, len(approvableIDs))
|
||||
for _, id := range approvableIDs {
|
||||
marketing, err := s.getOne(c, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
updated = append(updated, *marketing)
|
||||
}
|
||||
|
||||
return updated, nil
|
||||
}
|
||||
|
||||
func (s *salesOrdersService) createMarketingProductWithDelivery(ctx context.Context, marketingId uint, rp validation.CreateMarketingProduct, marketingProductRepo repository.MarketingProductRepository, invDeliveryRepo rInvMarketingDeliveryProduct.MarketingDeliveryProductRepository) error {
|
||||
|
||||
marketingProduct := &entity.MarketingProduct{
|
||||
MarketingId: marketingId,
|
||||
ProductWarehouseId: rp.ProductWarehouseId,
|
||||
Qty: rp.Qty,
|
||||
UnitPrice: rp.UnitPrice,
|
||||
AvgWeight: rp.AvgWeight,
|
||||
TotalWeight: rp.TotalWeight,
|
||||
TotalPrice: rp.TotalPrice,
|
||||
}
|
||||
if err := marketingProductRepo.CreateOne(ctx, marketingProduct, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
marketingDeliveryProduct := &entity.MarketingDeliveryProduct{
|
||||
MarketingProductId: marketingProduct.Id,
|
||||
Qty: 0,
|
||||
UnitPrice: 0,
|
||||
TotalWeight: 0,
|
||||
AvgWeight: 0,
|
||||
TotalPrice: 0,
|
||||
DeliveryDate: nil,
|
||||
VehicleNumber: rp.VehicleNumber,
|
||||
}
|
||||
if err := invDeliveryRepo.CreateOne(ctx, marketingDeliveryProduct, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package validation
|
||||
|
||||
type Create struct {
|
||||
CustomerId uint `json:"customer_id" validate:"required,gt=0"`
|
||||
SalesPersonId uint `json:"sales_person_id" validate:"required,gt=0"`
|
||||
Date string `json:"date" validate:"required,datetime=2006-01-02"`
|
||||
Notes string `json:"notes" validate:"omitempty,max=500"`
|
||||
MarketingProducts []CreateMarketingProduct `json:"marketing_products" validate:"required,min=1,dive"`
|
||||
}
|
||||
|
||||
type CreateMarketingProduct struct {
|
||||
VehicleNumber string `json:"vehicle_number" validate:"required,min=1,max=50"`
|
||||
ProductWarehouseId uint `json:"product_warehouse_id" validate:"required,gt=0"`
|
||||
UnitPrice float64 `json:"unit_price" validate:"required,gt=0"`
|
||||
TotalWeight float64 `json:"total_weight" validate:"required,gt=0"`
|
||||
Qty float64 `json:"qty" validate:"required,gt=0"`
|
||||
AvgWeight float64 `json:"avg_weight" validate:"required,gt=0"`
|
||||
TotalPrice float64 `json:"total_price" validate:"required,gt=0"`
|
||||
}
|
||||
|
||||
type Update struct {
|
||||
CustomerId uint `json:"customer_id" validate:"omitempty,gt=0"`
|
||||
SalesPersonId uint `json:"sales_person_id" validate:"omitempty,gt=0"`
|
||||
Date string `json:"date" validate:"omitempty,datetime=2006-01-02"`
|
||||
Notes string `json:"notes" validate:"omitempty,max=500"`
|
||||
MarketingProducts []CreateMarketingProduct `json:"marketing_products" validate:"omitempty,min=1,dive"`
|
||||
}
|
||||
|
||||
type Approve struct {
|
||||
Action string `json:"action" validate:"required_strict"`
|
||||
ApprovableIds []uint `json:"approvable_ids" validate:"required_strict,min=1,dive,gt=0"`
|
||||
Notes *string `json:"notes,omitempty" validate:"omitempty,max=500"`
|
||||
}
|
||||
@@ -12,6 +12,7 @@ type CustomerRepository interface {
|
||||
repository.BaseRepository[entity.Customer]
|
||||
PicExists(ctx context.Context, areaId uint) (bool, error)
|
||||
NameExists(ctx context.Context, name string, excludeID *uint) (bool, error)
|
||||
IdExists(ctx context.Context, id uint) (bool, error)
|
||||
}
|
||||
|
||||
type CustomerRepositoryImpl struct {
|
||||
@@ -33,3 +34,7 @@ func (r *CustomerRepositoryImpl) PicExists(ctx context.Context, picId uint) (boo
|
||||
func (r *CustomerRepositoryImpl) NameExists(ctx context.Context, name string, excludeID *uint) (bool, error) {
|
||||
return repository.ExistsByName[entity.Customer](ctx, r.db, name, excludeID)
|
||||
}
|
||||
|
||||
func (r *CustomerRepositoryImpl) IdExists(ctx context.Context, id uint) (bool, error) {
|
||||
return repository.Exists[entity.Customer](ctx, r.db, id)
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ type KandangRepository interface {
|
||||
UpdateStatusByProjectFlockID(ctx context.Context, projectFlockID uint, status utils.KandangStatus) error
|
||||
UpsertProjectFlockKandang(ctx context.Context, projectFlockID, kandangID uint) error
|
||||
UpdateStatusByIDs(ctx context.Context, kandangIDs []uint, status utils.KandangStatus) error
|
||||
IdExists(ctx context.Context, id uint) (bool, error)
|
||||
}
|
||||
|
||||
type KandangRepositoryImpl struct {
|
||||
@@ -60,6 +61,10 @@ func (r *KandangRepositoryImpl) ProjectFlockExists(ctx context.Context, projectF
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
func (r *KandangRepositoryImpl) IdExists(ctx context.Context, id uint) (bool, error) {
|
||||
return repository.Exists[entity.Kandang](ctx, r.db, id)
|
||||
}
|
||||
|
||||
func (r *KandangRepositoryImpl) HasActiveKandangForProjectFlock(ctx context.Context, projectFlockID uint, excludeID *uint) (bool, error) {
|
||||
var count int64
|
||||
q := r.db.WithContext(ctx).
|
||||
|
||||
@@ -9,20 +9,29 @@ import (
|
||||
flockBaseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/flocks/dto"
|
||||
kandangBaseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/dto"
|
||||
locationBaseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/locations/dto"
|
||||
productDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/products/dto"
|
||||
warehouseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/dto"
|
||||
pfutils "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/utils"
|
||||
userBaseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
|
||||
)
|
||||
|
||||
// === DTO Structs (ordered) ===
|
||||
|
||||
type ProductWarehouseDTO struct {
|
||||
Id uint `json:"id"`
|
||||
Product *productDTO.ProductBaseDTO `json:"product,omitempty"`
|
||||
Warehouse *warehouseDTO.WarehouseBaseDTO `json:"warehouse,omitempty"`
|
||||
}
|
||||
|
||||
type ChickinBaseDTO struct {
|
||||
Id uint `json:"id"`
|
||||
ProjectFlockKandangId uint `json:"project_flock_kandang_id"`
|
||||
ChickInDate time.Time `json:"chick_in_date"`
|
||||
ProductWarehouseId uint `json:"product_warehouse_id"`
|
||||
UsageQty float64 `json:"usage_qty"`
|
||||
PendingUsageQty float64 `json:"pending_usage_qty"`
|
||||
Notes string `json:"notes"`
|
||||
Id uint `json:"id"`
|
||||
ProjectFlockKandangId uint `json:"project_flock_kandang_id"`
|
||||
ChickInDate time.Time `json:"chick_in_date"`
|
||||
ProductWarehouseId uint `json:"product_warehouse_id"`
|
||||
ProductWarehouse *ProductWarehouseDTO `json:"product_warehouse,omitempty"`
|
||||
UsageQty float64 `json:"usage_qty"`
|
||||
PendingUsageQty float64 `json:"pending_usage_qty"`
|
||||
Notes string `json:"notes"`
|
||||
}
|
||||
|
||||
type ProjectFlockDTO struct {
|
||||
@@ -160,11 +169,18 @@ func ToChickinBaseDTO(e entity.ProjectChickin) ChickinBaseDTO {
|
||||
// If relation is not loaded but ID is available, use the ID
|
||||
projectFlockKandangId = e.ProjectFlockKandangId
|
||||
}
|
||||
|
||||
var productWarehouse *ProductWarehouseDTO
|
||||
if e.ProductWarehouse != nil && e.ProductWarehouse.Id != 0 {
|
||||
productWarehouse = toProductWarehouseDTO(e.ProductWarehouse)
|
||||
}
|
||||
|
||||
return ChickinBaseDTO{
|
||||
Id: e.Id,
|
||||
ProjectFlockKandangId: projectFlockKandangId,
|
||||
ChickInDate: e.ChickInDate,
|
||||
ProductWarehouseId: e.ProductWarehouseId,
|
||||
ProductWarehouse: productWarehouse,
|
||||
UsageQty: e.UsageQty,
|
||||
PendingUsageQty: e.PendingUsageQty,
|
||||
Notes: e.Notes,
|
||||
@@ -243,3 +259,25 @@ func ToChickinDetailDTOs(e []entity.ProjectChickin) []ChickinDetailDTO {
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// === Helper Functions ===
|
||||
|
||||
// ToProductWarehouseDTO adalah exported helper untuk mapping ProductWarehouse (shared logic)
|
||||
func ToProductWarehouseDTO(pw *entity.ProductWarehouse) *ProductWarehouseDTO {
|
||||
if pw == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
product := productDTO.ToProductBaseDTO(pw.Product)
|
||||
warehouse := warehouseDTO.ToWarehouseBaseDTO(pw.Warehouse)
|
||||
|
||||
return &ProductWarehouseDTO{
|
||||
Id: pw.Id,
|
||||
Product: &product,
|
||||
Warehouse: &warehouse,
|
||||
}
|
||||
}
|
||||
|
||||
func toProductWarehouseDTO(pw *entity.ProductWarehouse) *ProductWarehouseDTO {
|
||||
return ToProductWarehouseDTO(pw)
|
||||
}
|
||||
|
||||
@@ -123,39 +123,34 @@ func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) ([]enti
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "Warehouse for Kandang not found")
|
||||
}
|
||||
|
||||
var productWarehouses []entity.ProductWarehouse
|
||||
category := strings.ToUpper(strings.TrimSpace(projectFlockKandang.ProjectFlock.Category))
|
||||
|
||||
var productCategoryCode string
|
||||
switch category {
|
||||
case string(utils.ProjectFlockCategoryGrowing):
|
||||
productCategoryCode = "DOC"
|
||||
case string(utils.ProjectFlockCategoryLaying):
|
||||
productCategoryCode = "PULLET"
|
||||
default:
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Unknown category: %s", category))
|
||||
}
|
||||
|
||||
productWarehouses, err = s.ProductWarehouseRepo.GetByCategoryCodeAndWarehouseID(c.Context(), productCategoryCode, warehouse.Id)
|
||||
if err != nil || len(productWarehouses) == 0 {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Product for %s category in the Kandang's warehouse not found", strings.ToLower(category)))
|
||||
}
|
||||
|
||||
chickinDate, err := utils.ParseDateString(req.ChickInDate)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid ChickInDate format")
|
||||
}
|
||||
|
||||
actorID := uint(1) // todo nanti ambil dari auth context
|
||||
newChikins := make([]*entity.ProjectChickin, 0)
|
||||
for _, productWarehouse := range productWarehouses {
|
||||
availableQty, err := s.calculateAvailableQuantity(c, req.ProjectFlockKandangId, &productWarehouse, category)
|
||||
|
||||
for _, chickinReq := range req.ChickinRequests {
|
||||
|
||||
productWarehouse, err := s.ProductWarehouseRepo.GetByID(c.Context(), chickinReq.ProductWarehouseId, nil)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to calculate available quantity for product warehouse %d", productWarehouse.Id))
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("Product warehouse %d not found", chickinReq.ProductWarehouseId))
|
||||
}
|
||||
|
||||
if productWarehouse.WarehouseId != warehouse.Id {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Product warehouse %d is not bound to kandang's warehouse", chickinReq.ProductWarehouseId))
|
||||
}
|
||||
|
||||
chickinDate, err := utils.ParseDateString(chickinReq.ChickInDate)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Invalid ChickInDate format for product warehouse %d", chickinReq.ProductWarehouseId))
|
||||
}
|
||||
|
||||
availableQty, err := s.calculateAvailableQuantity(c, req.ProjectFlockKandangId, productWarehouse, category)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to calculate available quantity for product warehouse %d", chickinReq.ProductWarehouseId))
|
||||
}
|
||||
|
||||
if availableQty <= 0 {
|
||||
continue
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("No available quantity for product warehouse %d", chickinReq.ProductWarehouseId))
|
||||
}
|
||||
|
||||
newChickin := &entity.ProjectChickin{
|
||||
@@ -163,8 +158,8 @@ func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) ([]enti
|
||||
ChickInDate: chickinDate,
|
||||
UsageQty: 0,
|
||||
PendingUsageQty: availableQty,
|
||||
ProductWarehouseId: productWarehouse.Id,
|
||||
Notes: req.Note,
|
||||
ProductWarehouseId: chickinReq.ProductWarehouseId,
|
||||
Notes: chickinReq.Note,
|
||||
CreatedBy: actorID,
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
package validation
|
||||
|
||||
type Create struct {
|
||||
ProjectFlockKandangId uint `json:"project_flock_kandang_id" validate:"required,number,min=1"`
|
||||
ChickInDate string `json:"chick_in_date" validate:"required,datetime=2006-01-02"`
|
||||
Note string `json:"note" validate:"omitempty"`
|
||||
ProjectFlockKandangId uint `json:"project_flock_kandang_id" validate:"required,number,min=1"`
|
||||
ChickinRequests []ChickinRequestItem `json:"chickin_requests" validate:"required,min=1,dive"`
|
||||
}
|
||||
|
||||
type ChickinRequestItem struct {
|
||||
ChickInDate string `json:"chick_in_date" validate:"required,datetime=2006-01-02"`
|
||||
ProductWarehouseId uint `json:"product_warehouse_id" validate:"required,number,min=1"`
|
||||
Note string `json:"note" validate:"omitempty"`
|
||||
}
|
||||
|
||||
type Update struct {
|
||||
|
||||
+24
-7
@@ -4,6 +4,7 @@ import (
|
||||
"math"
|
||||
"strconv"
|
||||
|
||||
flockDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/flocks/dto"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/modules/production/project-flock-kandangs/dto"
|
||||
service "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project-flock-kandangs/services"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project-flock-kandangs/validations"
|
||||
@@ -24,20 +25,36 @@ func NewProjectFlockKandangController(projectFlockKandangService service.Project
|
||||
|
||||
func (u *ProjectFlockKandangController) GetAll(c *fiber.Ctx) error {
|
||||
query := &validation.Query{
|
||||
Page: c.QueryInt("page", 1),
|
||||
Limit: c.QueryInt("limit", 10),
|
||||
Search: c.Query("search", ""),
|
||||
Page: c.QueryInt("page", 1),
|
||||
Limit: c.QueryInt("limit", 10),
|
||||
Search: c.Query("search", ""),
|
||||
ProjectFlockId: uint(c.QueryInt("project_flock_id", 0)),
|
||||
KandangId: uint(c.QueryInt("kandang_id", 0)),
|
||||
Category: c.Query("category", ""),
|
||||
AreaId: uint(c.QueryInt("area_id", 0)),
|
||||
SortBy: c.Query("sort_by", ""),
|
||||
SortOrder: c.Query("sort_order", ""),
|
||||
StepName: c.Query("step_name", ""),
|
||||
}
|
||||
|
||||
if query.Page < 1 || query.Limit < 1 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0")
|
||||
}
|
||||
|
||||
result, totalResults, err := u.ProjectFlockKandangService.GetAll(c, query)
|
||||
results, totalResults, flockMap, err := u.ProjectFlockKandangService.GetAll(c, query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data := make([]dto.ProjectFlockKandangListDTO, 0)
|
||||
for _, result := range results {
|
||||
var flock *flockDTO.FlockBaseDTO
|
||||
if flockMap != nil {
|
||||
flock = flockMap[result.ProjectFlock.Id]
|
||||
}
|
||||
data = append(data, dto.ToProjectFlockKandangListDTOWithFlock(result, flock))
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.SuccessWithPaginate[dto.ProjectFlockKandangListDTO]{
|
||||
Code: fiber.StatusOK,
|
||||
@@ -49,7 +66,7 @@ func (u *ProjectFlockKandangController) GetAll(c *fiber.Ctx) error {
|
||||
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
|
||||
TotalResults: totalResults,
|
||||
},
|
||||
Data: dto.ToProjectFlockKandangListDTOs(result),
|
||||
Data: data,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -61,7 +78,7 @@ func (u *ProjectFlockKandangController) GetOne(c *fiber.Ctx) error {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||
}
|
||||
|
||||
result, availableQtys, err := u.ProjectFlockKandangService.GetOne(c, uint(id))
|
||||
result, availableQtys, productWarehouses, flock, err := u.ProjectFlockKandangService.GetOne(c, uint(id))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -71,6 +88,6 @@ func (u *ProjectFlockKandangController) GetOne(c *fiber.Ctx) error {
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Get projectFlockKandang successfully",
|
||||
Data: dto.ToProjectFlockKandangListDTOWithAvailableQty(*result, availableQtys),
|
||||
Data: dto.ToProjectFlockKandangDetailDTOWithAvailableQtyAndFlock(*result, availableQtys, productWarehouses, flock),
|
||||
})
|
||||
}
|
||||
|
||||
+85
-147
@@ -19,11 +19,14 @@ import (
|
||||
// === DTO Structs (ordered) ===
|
||||
|
||||
type ProjectFlockKandangBaseDTO struct {
|
||||
Id uint `json:"id"`
|
||||
Id uint `json:"id"`
|
||||
KandangId uint `json:"kandang_id"`
|
||||
ProjectFlockId uint `json:"project_flock_id"`
|
||||
}
|
||||
|
||||
type ProjectFlockDTO struct {
|
||||
Id uint `json:"id"`
|
||||
Name string `json:"flock_name,omitempty"`
|
||||
Period int `json:"period"`
|
||||
Flock *flockDTO.FlockBaseDTO `json:"flock,omitempty"`
|
||||
Area *areaDTO.AreaBaseDTO `json:"area,omitempty"`
|
||||
@@ -54,24 +57,26 @@ type AvailableQtyDTO struct {
|
||||
|
||||
type ProjectFlockKandangListDTO struct {
|
||||
ProjectFlockKandangBaseDTO
|
||||
ProjectFlock *ProjectFlockDTO `json:"project_flock,omitempty"`
|
||||
Kandang *KandangDTO `json:"kandang,omitempty"`
|
||||
Chickins []chickinDTO.ChickinBaseDTO `json:"chickins,omitempty"`
|
||||
AvailableQtys []AvailableQtyDTO `json:"available_qtys,omitempty"`
|
||||
CreatedUser *userDTO.UserBaseDTO `json:"created_user,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Approval *approvalDTO.ApprovalBaseDTO `json:"approval,omitempty"`
|
||||
ProjectFlock *ProjectFlockDTO `json:"project_flock,omitempty"`
|
||||
Kandang *KandangDTO `json:"kandang,omitempty"`
|
||||
CreatedUser *userDTO.UserBaseDTO `json:"created_user,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Approval *approvalDTO.ApprovalBaseDTO `json:"approval,omitempty"`
|
||||
}
|
||||
|
||||
type ProjectFlockKandangDetailDTO struct {
|
||||
ProjectFlockKandangListDTO
|
||||
Chickins []chickinDTO.ChickinBaseDTO `json:"chickins,omitempty"`
|
||||
AvailableQtys []AvailableQtyDTO `json:"available_qtys,omitempty"`
|
||||
}
|
||||
|
||||
// === Mapper Functions (ordered) ===
|
||||
|
||||
func ToProjectFlockKandangBaseDTO(e entity.ProjectFlockKandang) ProjectFlockKandangBaseDTO {
|
||||
return ProjectFlockKandangBaseDTO{
|
||||
Id: e.Id,
|
||||
Id: e.Id,
|
||||
KandangId: e.KandangId,
|
||||
ProjectFlockId: e.ProjectFlockId,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,7 +87,9 @@ func toProjectFlockDTO(pf *projectFlockDTO.ProjectFlockListDTO) *ProjectFlockDTO
|
||||
|
||||
return &ProjectFlockDTO{
|
||||
Id: pf.Id,
|
||||
Name: pf.FlockName,
|
||||
Period: pf.Period,
|
||||
Flock: pf.Flock,
|
||||
Area: pf.Area,
|
||||
Category: pf.Category,
|
||||
Fcr: pf.Fcr,
|
||||
@@ -93,23 +100,31 @@ func toProjectFlockDTO(pf *projectFlockDTO.ProjectFlockListDTO) *ProjectFlockDTO
|
||||
}
|
||||
}
|
||||
|
||||
func ToProjectFlockKandangListDTOWithAvailableQty(e entity.ProjectFlockKandang, availableQtysRaw []map[string]interface{}) ProjectFlockKandangListDTO {
|
||||
func ToProjectFlockKandangDetailDTOWithAvailableQty(e entity.ProjectFlockKandang, availableQtyMap map[uint]float64, productWarehouses []entity.ProductWarehouse) ProjectFlockKandangDetailDTO {
|
||||
return ToProjectFlockKandangDetailDTOWithAvailableQtyAndFlock(e, availableQtyMap, productWarehouses, nil)
|
||||
}
|
||||
|
||||
func ToProjectFlockKandangDetailDTOWithAvailableQtyAndFlock(e entity.ProjectFlockKandang, availableQtyMap map[uint]float64, productWarehouses []entity.ProductWarehouse, flock *flockDTO.FlockBaseDTO) ProjectFlockKandangDetailDTO {
|
||||
var projectFlockSummary *projectFlockDTO.ProjectFlockListDTO
|
||||
if e.ProjectFlock.Id != 0 {
|
||||
mapped := projectFlockDTO.ToProjectFlockListDTO(e.ProjectFlock)
|
||||
mapped := projectFlockDTO.ToProjectFlockListDTO(e.ProjectFlock, flock)
|
||||
projectFlockSummary = &mapped
|
||||
}
|
||||
|
||||
return ProjectFlockKandangListDTO{
|
||||
listDTO := ProjectFlockKandangListDTO{
|
||||
ProjectFlockKandangBaseDTO: ToProjectFlockKandangBaseDTO(e),
|
||||
ProjectFlock: toProjectFlockDTO(projectFlockSummary),
|
||||
Kandang: toKandangDTO(e.Kandang),
|
||||
Chickins: toChickinDTOs(e.Chickins),
|
||||
AvailableQtys: toAvailableQtyDTOsFromRaw(availableQtysRaw),
|
||||
CreatedAt: e.CreatedAt,
|
||||
CreatedUser: toCreatedUserDTO(e.ProjectFlock),
|
||||
Approval: toApprovalDTO(e),
|
||||
}
|
||||
|
||||
return ProjectFlockKandangDetailDTO{
|
||||
ProjectFlockKandangListDTO: listDTO,
|
||||
Chickins: toChickinDTOs(e.Chickins),
|
||||
AvailableQtys: toAvailableQtyDTOsFromMap(e.Chickins, availableQtyMap, productWarehouses),
|
||||
}
|
||||
}
|
||||
|
||||
func toKandangDTO(kandang entity.Kandang) *KandangDTO {
|
||||
@@ -124,6 +139,17 @@ func toKandangDTO(kandang entity.Kandang) *KandangDTO {
|
||||
}
|
||||
}
|
||||
|
||||
func toFlockDTO(flock *entity.Flock) *flockDTO.FlockBaseDTO {
|
||||
if flock == nil || flock.Id == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &flockDTO.FlockBaseDTO{
|
||||
Id: flock.Id,
|
||||
Name: flock.Name,
|
||||
}
|
||||
}
|
||||
|
||||
func toApprovalDTO(e entity.ProjectFlockKandang) *approvalDTO.ApprovalBaseDTO {
|
||||
if e.LatestApproval != nil {
|
||||
mapped := approvalDTO.ToApprovalDTO(*e.LatestApproval)
|
||||
@@ -133,9 +159,13 @@ func toApprovalDTO(e entity.ProjectFlockKandang) *approvalDTO.ApprovalBaseDTO {
|
||||
}
|
||||
|
||||
func ToProjectFlockKandangListDTO(e entity.ProjectFlockKandang) ProjectFlockKandangListDTO {
|
||||
return ToProjectFlockKandangListDTOWithFlock(e, nil)
|
||||
}
|
||||
|
||||
func ToProjectFlockKandangListDTOWithFlock(e entity.ProjectFlockKandang, flock *flockDTO.FlockBaseDTO) ProjectFlockKandangListDTO {
|
||||
var projectFlockSummary *projectFlockDTO.ProjectFlockListDTO
|
||||
if e.ProjectFlock.Id != 0 {
|
||||
mapped := projectFlockDTO.ToProjectFlockListDTO(e.ProjectFlock)
|
||||
mapped := projectFlockDTO.ToProjectFlockListDTOWithFlock(e.ProjectFlock, flock)
|
||||
projectFlockSummary = &mapped
|
||||
}
|
||||
|
||||
@@ -143,8 +173,6 @@ func ToProjectFlockKandangListDTO(e entity.ProjectFlockKandang) ProjectFlockKand
|
||||
ProjectFlockKandangBaseDTO: ToProjectFlockKandangBaseDTO(e),
|
||||
ProjectFlock: toProjectFlockDTO(projectFlockSummary),
|
||||
Kandang: toKandangDTO(e.Kandang),
|
||||
Chickins: toChickinDTOs(e.Chickins),
|
||||
AvailableQtys: toAvailableQtyDTOs(e.Chickins),
|
||||
CreatedAt: e.CreatedAt,
|
||||
CreatedUser: toCreatedUserDTO(e.ProjectFlock),
|
||||
Approval: toApprovalDTO(e),
|
||||
@@ -159,75 +187,6 @@ func ToProjectFlockKandangListDTOs(e []entity.ProjectFlockKandang) []ProjectFloc
|
||||
return result
|
||||
}
|
||||
|
||||
func ToProjectFlockKandangDetailDTO(e entity.ProjectFlockKandang) ProjectFlockKandangDetailDTO {
|
||||
return ProjectFlockKandangDetailDTO{
|
||||
ProjectFlockKandangListDTO: ToProjectFlockKandangListDTO(e),
|
||||
}
|
||||
}
|
||||
|
||||
// === Helper Functions (ordered) ===
|
||||
|
||||
func toProductWarehouseDTO(pwData map[string]interface{}) *ProductWarehouseDTO {
|
||||
if pwData == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
dto := &ProductWarehouseDTO{}
|
||||
|
||||
if id, ok := pwData["id"].(float64); ok {
|
||||
dto.Id = uint(id)
|
||||
} else if id, ok := pwData["id"].(uint); ok {
|
||||
dto.Id = id
|
||||
}
|
||||
|
||||
if pData, ok := pwData["product"].(map[string]interface{}); ok {
|
||||
dto.Product = toProductDTO(pData)
|
||||
}
|
||||
|
||||
if wData, ok := pwData["warehouse"].(map[string]interface{}); ok {
|
||||
dto.Warehouse = toWarehouseDTO(wData)
|
||||
}
|
||||
|
||||
return dto
|
||||
}
|
||||
|
||||
func toProductDTO(pData map[string]interface{}) *productDTO.ProductBaseDTO {
|
||||
if pData == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
product := &productDTO.ProductBaseDTO{}
|
||||
if id, ok := pData["id"].(float64); ok {
|
||||
product.Id = uint(id)
|
||||
} else if id, ok := pData["id"].(uint); ok {
|
||||
product.Id = id
|
||||
}
|
||||
if name, ok := pData["name"].(string); ok {
|
||||
product.Name = name
|
||||
}
|
||||
return product
|
||||
}
|
||||
|
||||
func toWarehouseDTO(wData map[string]interface{}) *warehouseDTO.WarehouseBaseDTO {
|
||||
if wData == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
warehouse := &warehouseDTO.WarehouseBaseDTO{}
|
||||
if id, ok := wData["id"].(float64); ok {
|
||||
warehouse.Id = uint(id)
|
||||
} else if id, ok := wData["id"].(uint); ok {
|
||||
warehouse.Id = id
|
||||
}
|
||||
if name, ok := wData["name"].(string); ok {
|
||||
warehouse.Name = name
|
||||
}
|
||||
if wType, ok := wData["type"].(string); ok {
|
||||
warehouse.Type = wType
|
||||
}
|
||||
return warehouse
|
||||
}
|
||||
|
||||
func toCreatedUserDTO(pf entity.ProjectFlock) *userDTO.UserBaseDTO {
|
||||
if pf.CreatedUser.Id != 0 {
|
||||
mapped := userDTO.ToUserBaseDTO(pf.CreatedUser)
|
||||
@@ -253,78 +212,57 @@ func toChickinDTOs(chickins []entity.ProjectChickin) []chickinDTO.ChickinBaseDTO
|
||||
return result
|
||||
}
|
||||
|
||||
func toAvailableQtyDTOs(chickins []entity.ProjectChickin) []AvailableQtyDTO {
|
||||
if len(chickins) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
availableQtyMap := make(map[uint]AvailableQtyDTO)
|
||||
for _, ch := range chickins {
|
||||
if ch.ProductWarehouse == nil || ch.ProductWarehouse.Quantity <= 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
if _, exists := availableQtyMap[ch.ProductWarehouseId]; exists {
|
||||
continue
|
||||
}
|
||||
|
||||
pwDTO := &ProductWarehouseDTO{
|
||||
Id: ch.ProductWarehouse.Id,
|
||||
}
|
||||
|
||||
if ch.ProductWarehouse.Product.Id != 0 {
|
||||
pwDTO.Product = &productDTO.ProductBaseDTO{
|
||||
Id: ch.ProductWarehouse.Product.Id,
|
||||
Name: ch.ProductWarehouse.Product.Name,
|
||||
}
|
||||
}
|
||||
|
||||
if ch.ProductWarehouse.Warehouse.Id != 0 {
|
||||
pwDTO.Warehouse = &warehouseDTO.WarehouseBaseDTO{
|
||||
Id: ch.ProductWarehouse.Warehouse.Id,
|
||||
Name: ch.ProductWarehouse.Warehouse.Name,
|
||||
Type: ch.ProductWarehouse.Warehouse.Type,
|
||||
}
|
||||
}
|
||||
|
||||
availableQtyMap[ch.ProductWarehouseId] = AvailableQtyDTO{
|
||||
ProductWarehouse: pwDTO,
|
||||
}
|
||||
}
|
||||
|
||||
func toAvailableQtyDTOsFromMap(chickins []entity.ProjectChickin, availableQtyMap map[uint]float64, productWarehouses []entity.ProductWarehouse) []AvailableQtyDTO {
|
||||
if len(availableQtyMap) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// First, build map from chickins
|
||||
pwMap := make(map[uint]*entity.ProductWarehouse)
|
||||
for _, chickin := range chickins {
|
||||
if chickin.ProductWarehouse != nil && chickin.ProductWarehouse.Id != 0 {
|
||||
pwMap[chickin.ProductWarehouseId] = chickin.ProductWarehouse
|
||||
}
|
||||
}
|
||||
|
||||
// Then, add productWarehouses that are not in chickins yet
|
||||
for i := range productWarehouses {
|
||||
if _, exists := pwMap[productWarehouses[i].Id]; !exists {
|
||||
pwMap[productWarehouses[i].Id] = &productWarehouses[i]
|
||||
}
|
||||
}
|
||||
|
||||
result := make([]AvailableQtyDTO, 0, len(availableQtyMap))
|
||||
for _, v := range availableQtyMap {
|
||||
result = append(result, v)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func toAvailableQtyDTOsFromRaw(availableQtysRaw []map[string]interface{}) []AvailableQtyDTO {
|
||||
if len(availableQtysRaw) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
result := make([]AvailableQtyDTO, len(availableQtysRaw))
|
||||
for i, v := range availableQtysRaw {
|
||||
pwData, ok := v["product_warehouse"].(map[string]interface{})
|
||||
if !ok {
|
||||
for pwId, availableQty := range availableQtyMap {
|
||||
pw, exists := pwMap[pwId]
|
||||
if !exists || pw == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
pwDTO := toProductWarehouseDTO(pwData)
|
||||
availableQty := 0.0
|
||||
if qty, ok := v["available_qty"].(float64); ok {
|
||||
availableQty = qty
|
||||
}
|
||||
pwDTO := ToProductWarehouseDTO(pw)
|
||||
|
||||
result[i] = AvailableQtyDTO{
|
||||
result = append(result, AvailableQtyDTO{
|
||||
AvailableQty: availableQty,
|
||||
ProductWarehouse: pwDTO,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func ToProductWarehouseDTO(pw *entity.ProductWarehouse) *ProductWarehouseDTO {
|
||||
if pw == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
chickinPwDTO := chickinDTO.ToProductWarehouseDTO(pw)
|
||||
if chickinPwDTO == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &ProductWarehouseDTO{
|
||||
Id: chickinPwDTO.Id,
|
||||
Product: chickinPwDTO.Product,
|
||||
Warehouse: chickinPwDTO.Warehouse,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||
rProductWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/repositories"
|
||||
rFlock "gitlab.com/mbugroup/lti-api.git/internal/modules/master/flocks/repositories"
|
||||
rWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/repositories"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
|
||||
@@ -28,6 +29,7 @@ func (ProjectFlockKandangModule) RegisterRoutes(router fiber.Router, db *gorm.DB
|
||||
userRepo := rUser.NewUserRepository(db)
|
||||
warehouseRepo := rWarehouse.NewWarehouseRepository(db)
|
||||
productWarehouseRepo := rProductWarehouse.NewProductWarehouseRepository(db)
|
||||
flockRepo := rFlock.NewFlockRepository(db)
|
||||
|
||||
approvalRepo := commonRepo.NewApprovalRepository(db)
|
||||
approvalService := commonSvc.NewApprovalService(approvalRepo)
|
||||
@@ -36,7 +38,7 @@ func (ProjectFlockKandangModule) RegisterRoutes(router fiber.Router, db *gorm.DB
|
||||
panic(fmt.Sprintf("failed to register project flock kandang approval workflow: %v", err))
|
||||
}
|
||||
|
||||
projectFlockKandangService := sProjectFlockKandang.NewProjectFlockKandangService(projectFlockKandangRepo, approvalService, warehouseRepo, productWarehouseRepo, projectFlockPopulationRepo, validate)
|
||||
projectFlockKandangService := sProjectFlockKandang.NewProjectFlockKandangService(projectFlockKandangRepo, approvalService, warehouseRepo, productWarehouseRepo, projectFlockPopulationRepo, flockRepo, validate)
|
||||
userService := sUser.NewUserService(userRepo, validate)
|
||||
|
||||
ProjectFlockKandangRoutes(router, userService, projectFlockKandangService)
|
||||
|
||||
+98
-45
@@ -6,9 +6,12 @@ import (
|
||||
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
rProductWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/repositories"
|
||||
flockDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/flocks/dto"
|
||||
flockRepository "gitlab.com/mbugroup/lti-api.git/internal/modules/master/flocks/repositories"
|
||||
rWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/repositories"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project-flock-kandangs/validations"
|
||||
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
|
||||
pfutils "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/utils"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
@@ -18,10 +21,12 @@ import (
|
||||
)
|
||||
|
||||
type ProjectFlockKandangService interface {
|
||||
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.ProjectFlockKandang, int64, error)
|
||||
GetOne(ctx *fiber.Ctx, id uint) (*entity.ProjectFlockKandang, []map[string]interface{}, error)
|
||||
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.ProjectFlockKandang, int64, map[uint]*flockDTO.FlockBaseDTO, error)
|
||||
GetOne(ctx *fiber.Ctx, id uint) (*entity.ProjectFlockKandang, map[uint]float64, []entity.ProductWarehouse, *flockDTO.FlockBaseDTO, error)
|
||||
}
|
||||
|
||||
// Note: map[uint]float64 adalah mapping dari ProductWarehouse ID ke calculated available quantity
|
||||
|
||||
type projectFlockKandangService struct {
|
||||
Log *logrus.Logger
|
||||
Validate *validator.Validate
|
||||
@@ -30,9 +35,10 @@ type projectFlockKandangService struct {
|
||||
WarehouseRepo rWarehouse.WarehouseRepository
|
||||
ProductWarehouseRepo rProductWarehouse.ProductWarehouseRepository
|
||||
PopulationRepo repository.ProjectFlockPopulationRepository
|
||||
FlockRepo flockRepository.FlockRepository
|
||||
}
|
||||
|
||||
func NewProjectFlockKandangService(repo repository.ProjectFlockKandangRepository, approvalSvc commonSvc.ApprovalService, warehouseRepo rWarehouse.WarehouseRepository, productWarehouseRepo rProductWarehouse.ProductWarehouseRepository, populationRepo repository.ProjectFlockPopulationRepository, validate *validator.Validate) ProjectFlockKandangService {
|
||||
func NewProjectFlockKandangService(repo repository.ProjectFlockKandangRepository, approvalSvc commonSvc.ApprovalService, warehouseRepo rWarehouse.WarehouseRepository, productWarehouseRepo rProductWarehouse.ProductWarehouseRepository, populationRepo repository.ProjectFlockPopulationRepository, flockRepo flockRepository.FlockRepository, validate *validator.Validate) ProjectFlockKandangService {
|
||||
return &projectFlockKandangService{
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
@@ -41,32 +47,73 @@ func NewProjectFlockKandangService(repo repository.ProjectFlockKandangRepository
|
||||
WarehouseRepo: warehouseRepo,
|
||||
ProductWarehouseRepo: productWarehouseRepo,
|
||||
PopulationRepo: populationRepo,
|
||||
FlockRepo: flockRepo,
|
||||
}
|
||||
}
|
||||
|
||||
func (s projectFlockKandangService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.ProjectFlockKandang, int64, error) {
|
||||
func (s projectFlockKandangService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.ProjectFlockKandang, int64, map[uint]*flockDTO.FlockBaseDTO, error) {
|
||||
if err := s.Validate.Struct(params); err != nil {
|
||||
return nil, 0, err
|
||||
return nil, 0, nil, err
|
||||
}
|
||||
|
||||
projectFlockKandangs, err := s.Repository.GetAll(c.Context())
|
||||
offset := (params.Page - 1) * params.Limit
|
||||
|
||||
projectFlockKandangs, total, err := s.Repository.GetAllWithFilters(c.Context(), offset, params.Limit, params)
|
||||
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get projectFlockKandangs: %+v", err)
|
||||
return nil, 0, err
|
||||
return nil, 0, nil, err
|
||||
}
|
||||
total := int64(len(projectFlockKandangs))
|
||||
|
||||
return projectFlockKandangs, total, nil
|
||||
if s.ApprovalSvc != nil {
|
||||
projectFlockKandangIDs := make([]uint, len(projectFlockKandangs))
|
||||
for i, pfk := range projectFlockKandangs {
|
||||
projectFlockKandangIDs[i] = pfk.Id
|
||||
}
|
||||
|
||||
approvalMap, err := s.ApprovalSvc.LatestByTargets(c.Context(), utils.ApprovalWorkflowProjectFlockKandang, projectFlockKandangIDs, func(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("ActionUser")
|
||||
})
|
||||
if err != nil {
|
||||
s.Log.Warnf("Failed to fetch approvals for projectFlockKandangs: %+v", err)
|
||||
} else {
|
||||
for i := range projectFlockKandangs {
|
||||
if approval, ok := approvalMap[projectFlockKandangs[i].Id]; ok {
|
||||
projectFlockKandangs[i].LatestApproval = approval
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
flockMap := make(map[uint]*flockDTO.FlockBaseDTO)
|
||||
for i := range projectFlockKandangs {
|
||||
if projectFlockKandangs[i].ProjectFlock.Id != 0 {
|
||||
baseName := pfutils.DeriveBaseName(projectFlockKandangs[i].ProjectFlock.FlockName)
|
||||
if baseName != "" {
|
||||
flock, err := s.FlockRepo.GetByName(c.Context(), baseName)
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
s.Log.Warnf("Failed to fetch flock %q: %+v", baseName, err)
|
||||
} else if flock != nil {
|
||||
|
||||
flockMap[projectFlockKandangs[i].ProjectFlock.Id] = &flockDTO.FlockBaseDTO{
|
||||
Id: flock.Id,
|
||||
Name: flock.Name,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return projectFlockKandangs, total, flockMap, nil
|
||||
}
|
||||
|
||||
func (s projectFlockKandangService) GetOne(c *fiber.Ctx, id uint) (*entity.ProjectFlockKandang, []map[string]interface{}, error) {
|
||||
func (s projectFlockKandangService) GetOne(c *fiber.Ctx, id uint) (*entity.ProjectFlockKandang, map[uint]float64, []entity.ProductWarehouse, *flockDTO.FlockBaseDTO, error) {
|
||||
projectFlockKandang, err := s.Repository.GetByID(c.Context(), id)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil, fiber.NewError(fiber.StatusNotFound, "ProjectFlockKandang not found")
|
||||
return nil, nil, nil, nil, fiber.NewError(fiber.StatusNotFound, "ProjectFlockKandang not found")
|
||||
}
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed get projectFlockKandang by id: %+v", err)
|
||||
return nil, nil, err
|
||||
return nil, nil, nil, nil, err
|
||||
}
|
||||
|
||||
if len(projectFlockKandang.Chickins) > 0 && s.ApprovalSvc != nil {
|
||||
@@ -80,16 +127,46 @@ func (s projectFlockKandangService) GetOne(c *fiber.Ctx, id uint) (*entity.Proje
|
||||
}
|
||||
}
|
||||
|
||||
availableQtys, err := s.getAvailableQuantities(c, projectFlockKandang)
|
||||
availableQtyMap, err := s.getAvailableQuantities(c, projectFlockKandang)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to fetch available quantities for kandang %d: %+v", projectFlockKandang.Kandang.Id, err)
|
||||
availableQtys = nil
|
||||
availableQtyMap = nil
|
||||
}
|
||||
|
||||
return projectFlockKandang, availableQtys, nil
|
||||
var productWarehouses []entity.ProductWarehouse
|
||||
if len(availableQtyMap) > 0 {
|
||||
pwIds := make([]uint, 0, len(availableQtyMap))
|
||||
for pwId := range availableQtyMap {
|
||||
pwIds = append(pwIds, pwId)
|
||||
}
|
||||
productWarehouses, err = s.ProductWarehouseRepo.GetByIDs(c.Context(), pwIds, func(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("Product").Preload("Warehouse")
|
||||
})
|
||||
if err != nil {
|
||||
s.Log.Warnf("Failed to fetch product warehouses: %+v", err)
|
||||
productWarehouses = []entity.ProductWarehouse{}
|
||||
}
|
||||
}
|
||||
var flockResult *flockDTO.FlockBaseDTO
|
||||
if projectFlockKandang.ProjectFlock.Id != 0 {
|
||||
baseName := pfutils.DeriveBaseName(projectFlockKandang.ProjectFlock.FlockName)
|
||||
if baseName != "" {
|
||||
flock, err := s.FlockRepo.GetByName(c.Context(), baseName)
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
s.Log.Warnf("Failed to fetch flock %q: %+v", baseName, err)
|
||||
} else if flock != nil {
|
||||
flockResult = &flockDTO.FlockBaseDTO{
|
||||
Id: flock.Id,
|
||||
Name: flock.Name,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return projectFlockKandang, availableQtyMap, productWarehouses, flockResult, nil
|
||||
}
|
||||
|
||||
func (s projectFlockKandangService) getAvailableQuantities(c *fiber.Ctx, projectFlockKandang *entity.ProjectFlockKandang) ([]map[string]interface{}, error) {
|
||||
func (s projectFlockKandangService) getAvailableQuantities(c *fiber.Ctx, projectFlockKandang *entity.ProjectFlockKandang) (map[uint]float64, error) {
|
||||
if projectFlockKandang.Kandang.Id == 0 || s.WarehouseRepo == nil || s.ProductWarehouseRepo == nil {
|
||||
return nil, nil
|
||||
}
|
||||
@@ -105,48 +182,24 @@ func (s projectFlockKandangService) getAvailableQuantities(c *fiber.Ctx, project
|
||||
} else if projectFlockKandang.ProjectFlock.Category == string(utils.ProjectFlockCategoryLaying) {
|
||||
productCategoryCode = "PULLET"
|
||||
} else {
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
products, err := s.ProductWarehouseRepo.GetByCategoryCodeAndWarehouseID(c.Context(), productCategoryCode, warehouse.Id)
|
||||
products, err := s.ProductWarehouseRepo.GetByFlagAndWarehouseID(c.Context(), productCategoryCode, warehouse.Id)
|
||||
if err != nil || len(products) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var result []map[string]interface{}
|
||||
result := make(map[uint]float64)
|
||||
for _, pw := range products {
|
||||
availableQty, err := s.calculateAvailableQuantityForProductWarehouse(c, projectFlockKandang, &pw)
|
||||
if err != nil {
|
||||
s.Log.Warnf("Failed to calculate available quantity for product warehouse %d: %v", pw.Id, err)
|
||||
}
|
||||
|
||||
// Only include product warehouse if available_qty > 0
|
||||
if availableQty <= 0 {
|
||||
continue
|
||||
if availableQty > 0 {
|
||||
result[pw.Id] = availableQty
|
||||
}
|
||||
|
||||
productData := map[string]interface{}{
|
||||
"id": pw.Product.Id,
|
||||
"name": pw.Product.Name,
|
||||
}
|
||||
|
||||
warehouseData := map[string]interface{}{
|
||||
"id": pw.Warehouse.Id,
|
||||
"name": pw.Warehouse.Name,
|
||||
"type": pw.Warehouse.Type,
|
||||
}
|
||||
|
||||
productWarehouseData := map[string]interface{}{
|
||||
"id": pw.Id,
|
||||
"product": productData,
|
||||
"warehouse": warehouseData,
|
||||
}
|
||||
|
||||
result = append(result, map[string]interface{}{
|
||||
"available_qty": availableQty,
|
||||
"product_warehouse": productWarehouseData,
|
||||
})
|
||||
}
|
||||
|
||||
return result, nil
|
||||
|
||||
+10
-3
@@ -11,7 +11,14 @@ type Update struct {
|
||||
}
|
||||
|
||||
type Query struct {
|
||||
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
|
||||
Search string `query:"search" validate:"omitempty,max=50"`
|
||||
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
|
||||
Search string `query:"search" validate:"omitempty,max=50"`
|
||||
ProjectFlockId uint `query:"project_flock_id" validate:"omitempty"`
|
||||
KandangId uint `query:"kandang_id" validate:"omitempty"`
|
||||
Category string `query:"category" validate:"omitempty,oneof=Growing Laying"`
|
||||
AreaId uint `query:"area_id" validate:"omitempty"`
|
||||
SortBy string `query:"sort_by" validate:"omitempty,oneof=created_at period"`
|
||||
SortOrder string `query:"sort_order" validate:"omitempty,oneof=ASC DESC"`
|
||||
StepName string `query:"step_name" validate:"omitempty,max=50"`
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
flockDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/flocks/dto"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/dto"
|
||||
service "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/services"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/validations"
|
||||
@@ -71,6 +72,10 @@ func (u *ProjectflockController) GetAll(c *fiber.Ctx) error {
|
||||
if period := c.QueryInt("period", 0); period > 0 {
|
||||
query.Period = period
|
||||
}
|
||||
if category := c.Query("category", ""); category != "" {
|
||||
query.Category = category
|
||||
|
||||
}
|
||||
|
||||
if kandangRaw := c.Query("kandang_id", c.Query("kandang_ids", "")); kandangRaw != "" {
|
||||
ids, err := parseUintList(kandangRaw)
|
||||
@@ -80,7 +85,7 @@ func (u *ProjectflockController) GetAll(c *fiber.Ctx) error {
|
||||
query.KandangIds = ids
|
||||
}
|
||||
|
||||
result, totalResults, err := u.ProjectflockService.GetAll(c, query)
|
||||
result, totalResults, flockMap, err := u.ProjectflockService.GetAll(c, query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -119,7 +124,7 @@ func (u *ProjectflockController) GetOne(c *fiber.Ctx) error {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||
}
|
||||
|
||||
result, err := u.ProjectflockService.GetOne(c, uint(id))
|
||||
result, flock, err := u.ProjectflockService.GetOne(c, uint(id))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -157,7 +162,7 @@ func (u *ProjectflockController) CreateOne(c *fiber.Ctx) error {
|
||||
Code: fiber.StatusCreated,
|
||||
Status: "success",
|
||||
Message: "Create projectflock successfully",
|
||||
Data: dto.ToProjectFlockListDTO(*result),
|
||||
Data: dto.ToProjectFlockListDTO(*result, nil),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -184,7 +189,7 @@ func (u *ProjectflockController) UpdateOne(c *fiber.Ctx) error {
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Update projectflock successfully",
|
||||
Data: dto.ToProjectFlockListDTO(*result),
|
||||
Data: dto.ToProjectFlockListDTO(*result, nil),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -23,25 +23,23 @@ type ProjectFlockBaseDTO struct {
|
||||
FlockName string `json:"flock_name"`
|
||||
}
|
||||
|
||||
type KandangWithProjectFlockIdDTO struct {
|
||||
Id uint `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Status string `json:"status"`
|
||||
ProjectFlockKandangId uint `json:"project_flock_kandang_id"`
|
||||
}
|
||||
|
||||
type ProjectFlockListDTO struct {
|
||||
ProjectFlockBaseDTO
|
||||
// Flock *flockDTO.FlockBaseDTO `json:"flock,omitempty"`
|
||||
Area *areaDTO.AreaBaseDTO `json:"area,omitempty"`
|
||||
Category string `json:"category"`
|
||||
Fcr *fcrDTO.FcrBaseDTO `json:"fcr,omitempty"`
|
||||
Location *locationDTO.LocationBaseDTO `json:"location,omitempty"`
|
||||
Kandangs []kandangDTO.KandangBaseDTO `json:"kandangs,omitempty"`
|
||||
CreatedUser *userDTO.UserBaseDTO `json:"created_user"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Approval approvalDTO.ApprovalBaseDTO `json:"approval"`
|
||||
Flock *flockDTO.FlockBaseDTO `json:"flock,omitempty"`
|
||||
Area *areaDTO.AreaBaseDTO `json:"area,omitempty"`
|
||||
Category string `json:"category"`
|
||||
Fcr *fcrDTO.FcrBaseDTO `json:"fcr,omitempty"`
|
||||
Location *locationDTO.LocationBaseDTO `json:"location,omitempty"`
|
||||
Kandangs []KandangWithProjectFlockIdDTO `json:"kandangs,omitempty"`
|
||||
CreatedUser *userDTO.UserBaseDTO `json:"created_user"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Approval approvalDTO.ApprovalBaseDTO `json:"approval"`
|
||||
}
|
||||
|
||||
type KandangWithProjectFlockIdDTO struct {
|
||||
kandangDTO.KandangBaseDTO
|
||||
ProjectFlockKandangId uint `json:"project_flock_kandang_id"`
|
||||
}
|
||||
|
||||
type ProjectFlockDetailDTO struct {
|
||||
@@ -66,20 +64,25 @@ func ToProjectFlockListDTOWithPeriod(e entity.ProjectFlock, period int) ProjectF
|
||||
createdUser = &mapped
|
||||
}
|
||||
|
||||
var kandangSummaries []kandangDTO.KandangBaseDTO
|
||||
var kandangSummaries []KandangWithProjectFlockIdDTO
|
||||
if len(e.Kandangs) > 0 {
|
||||
kandangSummaries = make([]kandangDTO.KandangBaseDTO, len(e.Kandangs))
|
||||
kandangSummaries = make([]KandangWithProjectFlockIdDTO, len(e.Kandangs))
|
||||
for i, kandang := range e.Kandangs {
|
||||
kandangSummaries[i] = kandangDTO.ToKandangBaseDTO(kandang)
|
||||
|
||||
var pfkId uint
|
||||
for _, kh := range e.KandangHistory {
|
||||
if kh.KandangId == kandang.Id {
|
||||
pfkId = kh.Id
|
||||
break
|
||||
}
|
||||
}
|
||||
kandangSummaries[i] = KandangWithProjectFlockIdDTO{
|
||||
KandangBaseDTO: kandangDTO.ToKandangBaseDTO(kandang),
|
||||
ProjectFlockKandangId: pfkId,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// var flockSummary *flockDTO.FlockBaseDTO
|
||||
// if baseName := pfutils.DeriveBaseName(e.FlockName); baseName != "" {
|
||||
// summary := flockDTO.FlockBaseDTO{Id: 0, Name: baseName}
|
||||
// flockSummary = &summary
|
||||
// }
|
||||
|
||||
var areaSummary *areaDTO.AreaBaseDTO
|
||||
if e.Area.Id != 0 {
|
||||
mapped := areaDTO.ToAreaBaseDTO(e.Area)
|
||||
@@ -98,6 +101,11 @@ func ToProjectFlockListDTOWithPeriod(e entity.ProjectFlock, period int) ProjectF
|
||||
locationSummary = &mapped
|
||||
}
|
||||
|
||||
var flockSummary *flockDTO.FlockBaseDTO
|
||||
if flock != nil && flock.Id != 0 {
|
||||
flockSummary = flock
|
||||
}
|
||||
|
||||
latestApproval := defaultProjectFlockLatestApproval(e)
|
||||
if e.LatestApproval != nil {
|
||||
snapshot := approvalDTO.ToApprovalDTO(*e.LatestApproval)
|
||||
@@ -119,6 +127,10 @@ func ToProjectFlockListDTOWithPeriod(e entity.ProjectFlock, period int) ProjectF
|
||||
}
|
||||
}
|
||||
|
||||
func ToProjectFlockListDTOWithFlock(e entity.ProjectFlock, flock *flockDTO.FlockBaseDTO) ProjectFlockListDTO {
|
||||
return ToProjectFlockListDTO(e, flock)
|
||||
}
|
||||
|
||||
func ToProjectFlockListDTOs(items []entity.ProjectFlock) []ProjectFlockListDTO {
|
||||
result := make([]ProjectFlockListDTO, len(items))
|
||||
for i, item := range items {
|
||||
@@ -145,7 +157,24 @@ func ToProjectFlockListDTOsWithPeriods(items []entity.ProjectFlock, periods map[
|
||||
return result
|
||||
}
|
||||
|
||||
func ToProjectFlockDetailDTO(e entity.ProjectFlock) ProjectFlockDetailDTO {
|
||||
func ToProjectFlockListDTOsWithFlocks(items []entity.ProjectFlock, flocks map[uint]*entity.Flock) []ProjectFlockListDTO {
|
||||
result := make([]ProjectFlockListDTO, len(items))
|
||||
for i, item := range items {
|
||||
var flock *flockDTO.FlockBaseDTO
|
||||
if flocks != nil {
|
||||
if f := flocks[item.Id]; f != nil {
|
||||
flock = &flockDTO.FlockBaseDTO{
|
||||
Id: f.Id,
|
||||
Name: f.Name,
|
||||
}
|
||||
}
|
||||
}
|
||||
result[i] = ToProjectFlockListDTOWithFlock(item, flock)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func ToProjectFlockDetailDTO(e entity.ProjectFlock, flock *flockDTO.FlockBaseDTO) ProjectFlockDetailDTO {
|
||||
return ProjectFlockDetailDTO{
|
||||
ProjectFlockListDTO: ToProjectFlockListDTOWithPeriod(e, 0),
|
||||
}
|
||||
|
||||
+15
@@ -14,6 +14,7 @@ type ProjectFlockPopulationRepository interface {
|
||||
ExistsByProjectChickinID(ctx context.Context, projectChickinID uint) (bool, error)
|
||||
GetByProjectChickinIDAndProductWarehouseID(ctx context.Context, projectChickinID uint, productWarehouseID uint) ([]entity.ProjectFlockPopulation, error)
|
||||
GetByProjectFlockKandangIDAndProductWarehouseID(ctx context.Context, projectFlockKandangID uint, productWarehouseID uint) ([]entity.ProjectFlockPopulation, error)
|
||||
GetTotalQtyByProjectFlockKandangID(ctx context.Context, projectFlockKandangID uint) (float64, error)
|
||||
|
||||
// subset of base repository methods used by services
|
||||
CreateOne(ctx context.Context, entity *entity.ProjectFlockPopulation, modifier func(*gorm.DB) *gorm.DB) error
|
||||
@@ -91,3 +92,17 @@ func (r *projectFlockPopulationRepositoryImpl) GetByProjectFlockKandangIDAndProd
|
||||
}
|
||||
return records, nil
|
||||
}
|
||||
|
||||
func (r *projectFlockPopulationRepositoryImpl) GetTotalQtyByProjectFlockKandangID(ctx context.Context, projectFlockKandangID uint) (float64, error) {
|
||||
var total float64
|
||||
err := r.DB().WithContext(ctx).
|
||||
Table("project_flock_populations").
|
||||
Select("COALESCE(SUM(total_qty), 0) AS total_qty").
|
||||
Joins("JOIN project_chickins ON project_chickins.id = project_flock_populations.project_chickin_id").
|
||||
Where("project_chickins.project_flock_kandang_id = ?", projectFlockKandangID).
|
||||
Scan(&total).Error
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return total, nil
|
||||
}
|
||||
|
||||
@@ -52,7 +52,9 @@ func (r *ProjectflockRepositoryImpl) withDefaultRelations(db *gorm.DB) *gorm.DB
|
||||
Preload("Area").
|
||||
Preload("Fcr").
|
||||
Preload("Location").
|
||||
Preload("Kandangs")
|
||||
Preload("Kandangs").
|
||||
Preload("KandangHistory").
|
||||
Preload("KandangHistory.Kandang")
|
||||
}
|
||||
|
||||
func (r *ProjectflockRepositoryImpl) applyQueryFilters(db *gorm.DB, params *validation.Query) *gorm.DB {
|
||||
@@ -60,6 +62,9 @@ func (r *ProjectflockRepositoryImpl) applyQueryFilters(db *gorm.DB, params *vali
|
||||
return db
|
||||
}
|
||||
|
||||
if params.Category != "" {
|
||||
db = db.Where("project_flocks.category = ?", params.Category)
|
||||
}
|
||||
if params.AreaId > 0 {
|
||||
db = db.Where("project_flocks.area_id = ?", params.AreaId)
|
||||
}
|
||||
|
||||
+105
-8
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project-flock-kandangs/validations"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -14,7 +15,8 @@ type ProjectFlockKandangRepository interface {
|
||||
GetByProjectFlockAndKandang(ctx context.Context, projectFlockID uint, kandangID uint) (*entity.ProjectFlockKandang, error)
|
||||
CreateMany(ctx context.Context, records []*entity.ProjectFlockKandang) error
|
||||
DeleteMany(ctx context.Context, projectFlockID uint, kandangIDs []uint) error
|
||||
GetAll(ctx context.Context) ([]entity.ProjectFlockKandang, error)
|
||||
GetAll(ctx context.Context, offset int, limit int, modifier func(*gorm.DB) *gorm.DB) ([]entity.ProjectFlockKandang, int64, error)
|
||||
GetAllWithFilters(ctx context.Context, offset int, limit int, params interface{}) ([]entity.ProjectFlockKandang, int64, error)
|
||||
ListExistingKandangIDs(ctx context.Context, projectFlockID uint, kandangIDs []uint) ([]uint, error)
|
||||
HasKandangsLinkedToOtherProject(ctx context.Context, kandangIDs []uint, exceptProjectID *uint) (bool, error)
|
||||
FindKandangsWithRecordings(ctx context.Context, projectFlockID uint, kandangIDs []uint) ([]entity.Kandang, error)
|
||||
@@ -51,9 +53,36 @@ func (r *projectFlockKandangRepositoryImpl) DeleteMany(ctx context.Context, proj
|
||||
Delete(&entity.ProjectFlockKandang{}).Error
|
||||
}
|
||||
|
||||
func (r *projectFlockKandangRepositoryImpl) GetAll(ctx context.Context) ([]entity.ProjectFlockKandang, error) {
|
||||
func (r *projectFlockKandangRepositoryImpl) GetAll(ctx context.Context, offset int, limit int, modifier func(*gorm.DB) *gorm.DB) ([]entity.ProjectFlockKandang, int64, error) {
|
||||
var records []entity.ProjectFlockKandang
|
||||
if err := r.db.WithContext(ctx).
|
||||
var total int64
|
||||
|
||||
q := r.db.WithContext(ctx)
|
||||
|
||||
if modifier != nil {
|
||||
q = modifier(q)
|
||||
}
|
||||
|
||||
if err := q.Model(&entity.ProjectFlockKandang{}).Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
if err := q.Offset(offset).Limit(limit).Find(&records).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return records, total, nil
|
||||
}
|
||||
|
||||
func (r *projectFlockKandangRepositoryImpl) GetAllWithFilters(ctx context.Context, offset int, limit int, params interface{}) ([]entity.ProjectFlockKandang, int64, error) {
|
||||
var records []entity.ProjectFlockKandang
|
||||
var total int64
|
||||
|
||||
query, ok := params.(*validation.Query)
|
||||
|
||||
q := r.db.WithContext(ctx).
|
||||
Joins("JOIN \"kandangs\" ON \"project_flock_kandangs\".\"kandang_id\" = \"kandangs\".\"id\"").
|
||||
Joins("JOIN \"project_flocks\" ON \"project_flock_kandangs\".\"project_flock_id\" = \"project_flocks\".\"id\"").
|
||||
Preload("ProjectFlock").
|
||||
Preload("ProjectFlock.Fcr").
|
||||
Preload("ProjectFlock.Area").
|
||||
@@ -64,12 +93,76 @@ func (r *projectFlockKandangRepositoryImpl) GetAll(ctx context.Context) ([]entit
|
||||
Preload("Kandang").
|
||||
Preload("Chickins").
|
||||
Preload("Chickins.CreatedUser").
|
||||
Preload("Chickins.ProductWarehouse").
|
||||
Order("project_flock_id ASC, created_at ASC").
|
||||
Find(&records).Error; err != nil {
|
||||
return nil, err
|
||||
Preload("Chickins.ProductWarehouse")
|
||||
|
||||
if ok && query != nil && query.StepName != "" {
|
||||
q = q.Where(`
|
||||
EXISTS (
|
||||
SELECT 1 FROM "approvals"
|
||||
WHERE "approvals"."approvable_id" = "project_flock_kandangs"."id"
|
||||
AND "approvals"."approvable_type" = ?
|
||||
AND LOWER("approvals"."step_name") = LOWER(?)
|
||||
AND "approvals"."id" IN (
|
||||
SELECT "id" FROM "approvals"
|
||||
WHERE "approvable_id" = "project_flock_kandangs"."id"
|
||||
AND "approvable_type" = ?
|
||||
ORDER BY "action_at" DESC
|
||||
LIMIT 1
|
||||
)
|
||||
)
|
||||
`, "PROJECT_FLOCK_KANDANGS", query.StepName, "PROJECT_FLOCK_KANDANGS")
|
||||
}
|
||||
return records, nil
|
||||
|
||||
if ok && query != nil {
|
||||
if query.Search != "" {
|
||||
escapedSearch := strings.NewReplacer("\\", "\\\\", "%", "\\%", "_", "\\_").Replace(query.Search)
|
||||
q = q.Where(
|
||||
r.db.Where("LOWER(\"kandangs\".\"name\") LIKE LOWER(?) ESCAPE '\\'", "%"+escapedSearch+"%").
|
||||
Or("LOWER(\"project_flocks\".\"flock_name\") LIKE LOWER(?) ESCAPE '\\'", "%"+escapedSearch+"%"),
|
||||
)
|
||||
}
|
||||
|
||||
if query.ProjectFlockId > 0 {
|
||||
q = q.Where("\"project_flock_kandangs\".\"project_flock_id\" = ?", query.ProjectFlockId)
|
||||
}
|
||||
|
||||
if query.KandangId > 0 {
|
||||
q = q.Where("\"project_flock_kandangs\".\"kandang_id\" = ?", query.KandangId)
|
||||
}
|
||||
|
||||
if query.Category != "" {
|
||||
q = q.Where("\"project_flocks\".\"category\" = ?", query.Category)
|
||||
}
|
||||
|
||||
if query.AreaId > 0 {
|
||||
q = q.Where("\"project_flocks\".\"area_id\" = ?", query.AreaId)
|
||||
}
|
||||
}
|
||||
|
||||
if err := q.Model(&entity.ProjectFlockKandang{}).Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
sortBy := "\"project_flock_kandangs\".\"created_at\" DESC"
|
||||
if ok && query != nil && query.SortBy != "" {
|
||||
sortOrder := "DESC"
|
||||
if query.SortOrder == "ASC" {
|
||||
sortOrder = "ASC"
|
||||
}
|
||||
|
||||
switch query.SortBy {
|
||||
case "created_at":
|
||||
sortBy = "\"project_flock_kandangs\".\"created_at\" " + sortOrder
|
||||
case "period":
|
||||
sortBy = "\"project_flocks\".\"period\" " + sortOrder
|
||||
}
|
||||
}
|
||||
|
||||
if err := q.Order(sortBy).Offset(offset).Limit(limit).Find(&records).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return records, total, nil
|
||||
}
|
||||
|
||||
func (r *projectFlockKandangRepositoryImpl) WithTx(tx *gorm.DB) ProjectFlockKandangRepository {
|
||||
@@ -97,6 +190,8 @@ func (r *projectFlockKandangRepositoryImpl) GetByID(ctx context.Context, id uint
|
||||
Preload("Chickins").
|
||||
Preload("Chickins.CreatedUser").
|
||||
Preload("Chickins.ProductWarehouse").
|
||||
Preload("Chickins.ProductWarehouse.Product").
|
||||
Preload("Chickins.ProductWarehouse.Warehouse").
|
||||
First(record, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -118,6 +213,8 @@ func (r *projectFlockKandangRepositoryImpl) GetByProjectFlockAndKandang(ctx cont
|
||||
Preload("Chickins").
|
||||
Preload("Chickins.CreatedUser").
|
||||
Preload("Chickins.ProductWarehouse").
|
||||
Preload("Chickins.ProductWarehouse.Product").
|
||||
Preload("Chickins.ProductWarehouse.Warehouse").
|
||||
First(record).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ func ProjectflockRoutes(v1 fiber.Router, u user.UserService, s projectflock.Proj
|
||||
route.Get("/:id", ctrl.GetOne)
|
||||
route.Patch("/:id", ctrl.UpdateOne)
|
||||
route.Delete("/:id", ctrl.DeleteOne)
|
||||
route.Get("/kandangs/:project_flock_kandang_id/periods", ctrl.GetFlockPeriodSummary)
|
||||
route.Get("/kandangs/lookup", ctrl.LookupProjectFlockKandang)
|
||||
route.Post("/approvals", ctrl.Approval)
|
||||
route.Get("/kandangs/:location_id/periods", ctrl.GetFlockPeriodSummary)
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
authmiddleware "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
||||
productWarehouseRepository "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/repositories"
|
||||
flockDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/flocks/dto"
|
||||
flockRepository "gitlab.com/mbugroup/lti-api.git/internal/modules/master/flocks/repositories"
|
||||
kandangRepository "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/repositories"
|
||||
warehouseRepository "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/repositories"
|
||||
@@ -28,8 +29,8 @@ import (
|
||||
)
|
||||
|
||||
type ProjectflockService interface {
|
||||
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.ProjectFlock, int64, error)
|
||||
GetOne(ctx *fiber.Ctx, id uint) (*entity.ProjectFlock, error)
|
||||
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.ProjectFlock, int64, map[uint]*flockDTO.FlockBaseDTO, error)
|
||||
GetOne(ctx *fiber.Ctx, id uint) (*entity.ProjectFlock, *flockDTO.FlockBaseDTO, error)
|
||||
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.ProjectFlock, error)
|
||||
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.ProjectFlock, error)
|
||||
GetAvailableDocQuantity(ctx *fiber.Ctx, kandangID uint) (float64, error)
|
||||
@@ -95,9 +96,9 @@ func (s projectflockService) withRelations(db *gorm.DB) *gorm.DB {
|
||||
Preload("KandangHistory.Kandang")
|
||||
}
|
||||
|
||||
func (s projectflockService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.ProjectFlock, int64, error) {
|
||||
func (s projectflockService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.ProjectFlock, int64, map[uint]*flockDTO.FlockBaseDTO, error) {
|
||||
if err := s.Validate.Struct(params); err != nil {
|
||||
return nil, 0, err
|
||||
return nil, 0, nil, err
|
||||
}
|
||||
|
||||
if params.Page <= 0 {
|
||||
@@ -113,7 +114,7 @@ func (s projectflockService) GetAll(c *fiber.Ctx, params *validation.Query) ([]e
|
||||
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get projectflocks: %+v", err)
|
||||
return nil, 0, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch project flocks")
|
||||
return nil, 0, nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch project flocks")
|
||||
}
|
||||
|
||||
if s.ApprovalSvc != nil && len(projectflocks) > 0 {
|
||||
@@ -136,10 +137,28 @@ func (s projectflockService) GetAll(c *fiber.Ctx, params *validation.Query) ([]e
|
||||
}
|
||||
}
|
||||
|
||||
return projectflocks, total, nil
|
||||
flockMap := make(map[uint]*flockDTO.FlockBaseDTO)
|
||||
for i := range projectflocks {
|
||||
if projectflocks[i].FlockName != "" {
|
||||
baseName := pfutils.DeriveBaseName(projectflocks[i].FlockName)
|
||||
if baseName != "" {
|
||||
flock, err := s.FlockRepo.GetByName(c.Context(), baseName)
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
s.Log.Warnf("Failed to fetch flock %q: %+v", baseName, err)
|
||||
} else if flock != nil {
|
||||
flockMap[projectflocks[i].Id] = &flockDTO.FlockBaseDTO{
|
||||
Id: flock.Id,
|
||||
Name: flock.Name,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return projectflocks, total, flockMap, nil
|
||||
}
|
||||
|
||||
func (s projectflockService) GetOne(c *fiber.Ctx, id uint) (*entity.ProjectFlock, error) {
|
||||
func (s projectflockService) getOneEntityOnly(c *fiber.Ctx, id uint) (*entity.ProjectFlock, error) {
|
||||
projectflock, err := s.Repository.GetByID(c.Context(), id, s.Repository.WithDefaultRelations())
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "Projectflock not found")
|
||||
@@ -168,6 +187,52 @@ func (s projectflockService) GetOne(c *fiber.Ctx, id uint) (*entity.ProjectFlock
|
||||
return projectflock, nil
|
||||
}
|
||||
|
||||
func (s projectflockService) GetOne(c *fiber.Ctx, id uint) (*entity.ProjectFlock, *flockDTO.FlockBaseDTO, error) {
|
||||
projectflock, err := s.Repository.GetByID(c.Context(), id, s.Repository.WithDefaultRelations())
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil, fiber.NewError(fiber.StatusNotFound, "Projectflock not found")
|
||||
}
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed get projectflock by id: %+v", err)
|
||||
return nil, nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch project flock")
|
||||
}
|
||||
|
||||
if s.ApprovalSvc != nil {
|
||||
approvals, err := s.ApprovalSvc.ListByTarget(c.Context(), s.approvalWorkflow, id, func(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("ActionUser")
|
||||
})
|
||||
if err != nil {
|
||||
s.Log.Warnf("Unable to load approvals for projectflock %d: %+v", id, err)
|
||||
} else if len(approvals) > 0 {
|
||||
if projectflock.LatestApproval == nil {
|
||||
latest := approvals[len(approvals)-1]
|
||||
projectflock.LatestApproval = &latest
|
||||
}
|
||||
} else {
|
||||
projectflock.LatestApproval = nil
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch Flock master data for this ProjectFlock
|
||||
var flockResult *flockDTO.FlockBaseDTO
|
||||
if projectflock.FlockName != "" {
|
||||
baseName := pfutils.DeriveBaseName(projectflock.FlockName)
|
||||
if baseName != "" {
|
||||
flock, err := s.FlockRepo.GetByName(c.Context(), baseName)
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
s.Log.Warnf("Failed to fetch flock %q: %+v", baseName, err)
|
||||
} else if flock != nil {
|
||||
flockResult = &flockDTO.FlockBaseDTO{
|
||||
Id: flock.Id,
|
||||
Name: flock.Name,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return projectflock, flockResult, nil
|
||||
}
|
||||
|
||||
func (s *projectflockService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.ProjectFlock, error) {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
@@ -289,7 +354,7 @@ func (s *projectflockService) CreateOne(c *fiber.Ctx, req *validation.Create) (*
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to create project flock")
|
||||
}
|
||||
|
||||
return s.GetOne(c, createBody.Id)
|
||||
return s.getOneEntityOnly(c, createBody.Id)
|
||||
}
|
||||
|
||||
func (s projectflockService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.ProjectFlock, error) {
|
||||
@@ -415,7 +480,7 @@ func (s projectflockService) UpdateOne(c *fiber.Ctx, req *validation.Update, id
|
||||
|
||||
hasChanges := hasBodyChanges || hasKandangChanges
|
||||
if !hasChanges {
|
||||
return s.GetOne(c, id)
|
||||
return s.getOneEntityOnly(c, id)
|
||||
}
|
||||
|
||||
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
||||
@@ -542,7 +607,7 @@ func (s projectflockService) UpdateOne(c *fiber.Ctx, req *validation.Update, id
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to update project flock")
|
||||
}
|
||||
|
||||
return s.GetOne(c, id)
|
||||
return s.getOneEntityOnly(c, id)
|
||||
}
|
||||
|
||||
func (s projectflockService) Approval(c *fiber.Ctx, req *validation.Approve) ([]entity.ProjectFlock, error) {
|
||||
@@ -636,7 +701,7 @@ func (s projectflockService) Approval(c *fiber.Ctx, req *validation.Approve) ([]
|
||||
|
||||
updated := make([]entity.ProjectFlock, 0, len(approvableIDs))
|
||||
for _, approvableID := range approvableIDs {
|
||||
project, err := s.GetOne(c, approvableID)
|
||||
project, err := s.getOneEntityOnly(c, approvableID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ type Query struct {
|
||||
AreaId uint `query:"area_id" validate:"omitempty,number,gt=0"`
|
||||
LocationId uint `query:"location_id" validate:"omitempty,number,gt=0"`
|
||||
Period int `query:"period" validate:"omitempty,number,gt=0"`
|
||||
Category string `query:"category" validate:"omitempty"`
|
||||
KandangIds []uint `query:"kandang_id" validate:"omitempty,dive,gt=0"`
|
||||
}
|
||||
|
||||
|
||||
@@ -69,13 +69,21 @@ type RecordingStockDTO struct {
|
||||
}
|
||||
|
||||
type RecordingEggDTO struct {
|
||||
Id uint `json:"id"`
|
||||
Id uint `json:"id"`
|
||||
ProductWarehouseId uint `json:"product_warehouse_id"`
|
||||
Qty int `json:"qty"`
|
||||
ProductWarehouse productWarehouseDTO.ProductWarehouseDTO `json:"product_warehouse"`
|
||||
Gradings []RecordingEggGradingDTO `json:"gradings,omitempty"`
|
||||
}
|
||||
|
||||
type RecordingProductWarehouseDTO struct {
|
||||
Id uint `json:"id"`
|
||||
ProductId uint `json:"product_id"`
|
||||
ProductName string `json:"product_name"`
|
||||
WarehouseId uint `json:"warehouse_id"`
|
||||
WarehouseName string `json:"warehouse_name"`
|
||||
}
|
||||
|
||||
type RecordingEggGradingDTO struct {
|
||||
Grade string `json:"grade,omitempty"`
|
||||
Qty float64 `json:"qty"`
|
||||
|
||||
+49
-14
@@ -1,6 +1,7 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
|
||||
@@ -24,16 +25,8 @@ func NewTransferLayingController(transferLayingService service.TransferLayingSer
|
||||
|
||||
func (u *TransferLayingController) GetAll(c *fiber.Ctx) error {
|
||||
query := &validation.Query{
|
||||
Page: c.QueryInt("page", 1),
|
||||
Limit: c.QueryInt("limit", 10),
|
||||
SourceProjectFlockId: uint(c.QueryInt("source_project_flock_id", 0)),
|
||||
TargetProjectFlockId: uint(c.QueryInt("target_project_flock_id", 0)),
|
||||
TransferDateFrom: c.Query("transfer_date_from", ""),
|
||||
TransferDateTo: c.Query("transfer_date_to", ""),
|
||||
ApprovalStatus: c.Query("approval_status", ""),
|
||||
TransferNumber: c.Query("transfer_number", ""),
|
||||
Sort: c.Query("sort", "created_at"),
|
||||
Order: c.Query("order", "desc"),
|
||||
Page: c.QueryInt("page", 1),
|
||||
Limit: c.QueryInt("limit", 10),
|
||||
}
|
||||
|
||||
if query.Page < 1 || query.Limit < 1 {
|
||||
@@ -45,8 +38,13 @@ func (u *TransferLayingController) GetAll(c *fiber.Ctx) error {
|
||||
return err
|
||||
}
|
||||
|
||||
data := make([]dto.TransferLayingDetailDTO, len(result))
|
||||
for i, transfer := range result {
|
||||
data[i] = dto.ToTransferLayingDetailDTOWithSingleApproval(transfer, transfer.LatestApproval)
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.SuccessWithPaginate[dto.TransferLayingListDTO]{
|
||||
JSON(response.SuccessWithPaginate[dto.TransferLayingDetailDTO]{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Get all transferLayings successfully",
|
||||
@@ -56,7 +54,7 @@ func (u *TransferLayingController) GetAll(c *fiber.Ctx) error {
|
||||
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
|
||||
TotalResults: totalResults,
|
||||
},
|
||||
Data: dto.ToTransferLayingListDTOs(result),
|
||||
Data: data,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -113,7 +111,7 @@ func (u *TransferLayingController) UpdateOne(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
if err := c.BodyParser(req); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Invalid request body: %s", err.Error()))
|
||||
}
|
||||
|
||||
result, err := u.TransferLayingService.UpdateOne(c, req, uint(id))
|
||||
@@ -126,7 +124,7 @@ func (u *TransferLayingController) UpdateOne(c *fiber.Ctx) error {
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Update transferLaying successfully",
|
||||
Data: dto.ToTransferLayingListDTO(*result),
|
||||
Data: dto.ToTransferLayingDetailDTOWithSingleApproval(*result, result.LatestApproval),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -180,3 +178,40 @@ func (u *TransferLayingController) Approval(c *fiber.Ctx) error {
|
||||
Data: data,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
func (u *TransferLayingController) GetAvailableQtyPerKandang(c *fiber.Ctx) error {
|
||||
projectFlockID, err := strconv.ParseUint(c.Params("project_flock_id"), 10, 32)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid project_flock_id")
|
||||
}
|
||||
|
||||
pf, kandangQtyMap, err := u.TransferLayingService.GetAvailableQtyPerKandang(c, uint(projectFlockID))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Build kandang list response
|
||||
kandangs := make([]dto.KandangAvailableQtyDTO, 0, len(kandangQtyMap))
|
||||
for kandangPFID, qty := range kandangQtyMap {
|
||||
kandangs = append(kandangs, dto.KandangAvailableQtyDTO{
|
||||
ProjectFlockKandangId: kandangPFID,
|
||||
AvailableQty: qty,
|
||||
})
|
||||
}
|
||||
|
||||
resp := dto.AvailableQtyForTransferDTO{
|
||||
ProjectFlockId: pf.Id,
|
||||
ProjectFlockCode: pf.FlockName,
|
||||
Category: pf.Category,
|
||||
Kandangs: kandangs,
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Get available quantity successfully",
|
||||
Data: resp,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -18,8 +18,9 @@ type TransferLayingBaseDTO struct {
|
||||
}
|
||||
|
||||
type ProjectFlockSummaryDTO struct {
|
||||
Id uint `json:"id"`
|
||||
Category string `json:"category"`
|
||||
Id uint `json:"id"`
|
||||
FlockName string `json:"flock_name"`
|
||||
Category string `json:"category"`
|
||||
}
|
||||
|
||||
type ProductSummaryDTO struct {
|
||||
@@ -39,8 +40,13 @@ type ProductWarehouseSummaryDTO struct {
|
||||
}
|
||||
|
||||
type ProjectFlockKandangSummaryDTO struct {
|
||||
Id uint `json:"id"`
|
||||
KandangId uint `json:"kandang_id"`
|
||||
Id uint `json:"id"`
|
||||
Kandang *KandangSummaryDTO `json:"kandang,omitempty"`
|
||||
}
|
||||
|
||||
type KandangSummaryDTO struct {
|
||||
Id uint `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type LayingTransferSourceDTO struct {
|
||||
@@ -76,6 +82,20 @@ type TransferLayingDetailDTO struct {
|
||||
Approval *approvalDTO.ApprovalBaseDTO `json:"approval,omitempty"`
|
||||
}
|
||||
|
||||
// === Available Quantity DTOs ===
|
||||
|
||||
type KandangAvailableQtyDTO struct {
|
||||
ProjectFlockKandangId uint `json:"project_flock_kandang_id"`
|
||||
AvailableQty float64 `json:"available_qty"`
|
||||
}
|
||||
|
||||
type AvailableQtyForTransferDTO struct {
|
||||
ProjectFlockId uint `json:"project_flock_id"`
|
||||
ProjectFlockCode string `json:"flock_name"`
|
||||
Category string `json:"category"`
|
||||
Kandangs []KandangAvailableQtyDTO `json:"kandangs"`
|
||||
}
|
||||
|
||||
// === Mapper Functions ===
|
||||
|
||||
func ToProjectFlockSummaryDTO(pf *entity.ProjectFlock) *ProjectFlockSummaryDTO {
|
||||
@@ -84,8 +104,9 @@ func ToProjectFlockSummaryDTO(pf *entity.ProjectFlock) *ProjectFlockSummaryDTO {
|
||||
}
|
||||
|
||||
return &ProjectFlockSummaryDTO{
|
||||
Id: pf.Id,
|
||||
Category: pf.Category,
|
||||
Id: pf.Id,
|
||||
FlockName: pf.FlockName,
|
||||
Category: pf.Category,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,9 +115,17 @@ func ToProjectFlockKandangSummaryDTO(pfk *entity.ProjectFlockKandang) *ProjectFl
|
||||
return nil
|
||||
}
|
||||
|
||||
var kandang *KandangSummaryDTO
|
||||
if pfk.Kandang.Id != 0 {
|
||||
kandang = &KandangSummaryDTO{
|
||||
Id: pfk.Kandang.Id,
|
||||
Name: pfk.Kandang.Name,
|
||||
}
|
||||
}
|
||||
|
||||
return &ProjectFlockKandangSummaryDTO{
|
||||
Id: pfk.Id,
|
||||
KandangId: pfk.KandangId,
|
||||
Id: pfk.Id,
|
||||
Kandang: kandang,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -205,7 +234,6 @@ func ToTransferLayingListDTO(e entity.LayingTransfer) TransferLayingListDTO {
|
||||
func ToTransferLayingDetailDTO(e entity.LayingTransfer, approvals []entity.Approval) TransferLayingDetailDTO {
|
||||
var latestApproval *approvalDTO.ApprovalBaseDTO
|
||||
|
||||
// Use LatestApproval from entity if available
|
||||
if e.LatestApproval != nil {
|
||||
mapped := approvalDTO.ToApprovalDTO(*e.LatestApproval)
|
||||
latestApproval = &mapped
|
||||
|
||||
@@ -27,4 +27,5 @@ func TransferLayingRoutes(v1 fiber.Router, u user.UserService, s transferLaying.
|
||||
route.Patch("/:id", ctrl.UpdateOne)
|
||||
route.Delete("/:id", ctrl.DeleteOne)
|
||||
route.Post("/approvals", ctrl.Approval)
|
||||
route.Get("/project-flocks/:project_flock_id/available-qty", ctrl.GetAvailableQtyPerKandang)
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ type TransferLayingService interface {
|
||||
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.LayingTransfer, error)
|
||||
DeleteOne(ctx *fiber.Ctx, id uint) error
|
||||
Approval(ctx *fiber.Ctx, req *validation.Approve) ([]entity.LayingTransfer, error)
|
||||
GetAvailableQtyPerKandang(ctx *fiber.Ctx, projectFlockID uint) (*entity.ProjectFlock, map[uint]float64, error)
|
||||
}
|
||||
|
||||
type transferLayingService struct {
|
||||
@@ -71,13 +72,17 @@ func NewTransferLayingService(
|
||||
func (s transferLayingService) withRelations(db *gorm.DB) *gorm.DB {
|
||||
return db.
|
||||
Preload("CreatedUser").
|
||||
Preload("FromProjectFlock").
|
||||
Preload("ToProjectFlock").
|
||||
Preload("Sources").
|
||||
Preload("Sources.SourceProjectFlockKandang").
|
||||
Preload("Sources.SourceProjectFlockKandang.Kandang").
|
||||
Preload("Sources.ProductWarehouse").
|
||||
Preload("Sources.ProductWarehouse.Product").
|
||||
Preload("Sources.ProductWarehouse.Warehouse").
|
||||
Preload("Targets").
|
||||
Preload("Targets.TargetProjectFlockKandang").
|
||||
Preload("Targets.TargetProjectFlockKandang.Kandang").
|
||||
Preload("Targets.ProductWarehouse").
|
||||
Preload("Targets.ProductWarehouse.Product").
|
||||
Preload("Targets.ProductWarehouse.Warehouse")
|
||||
@@ -92,32 +97,7 @@ func (s transferLayingService) GetAll(c *fiber.Ctx, params *validation.Query) ([
|
||||
|
||||
transferLayings, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
||||
db = s.withRelations(db)
|
||||
if params.SourceProjectFlockId != 0 {
|
||||
db = db.Where("from_project_flock_id = ?", params.SourceProjectFlockId)
|
||||
}
|
||||
if params.TargetProjectFlockId != 0 {
|
||||
db = db.Where("to_project_flock_id = ?", params.TargetProjectFlockId)
|
||||
}
|
||||
if params.TransferDateFrom != "" {
|
||||
db = db.Where("transfer_date >= ?", params.TransferDateFrom)
|
||||
}
|
||||
if params.TransferDateTo != "" {
|
||||
db = db.Where("transfer_date <= ?", params.TransferDateTo)
|
||||
}
|
||||
if params.TransferNumber != "" {
|
||||
db = db.Where("transfer_number ILIKE ?", "%"+params.TransferNumber+"%")
|
||||
}
|
||||
|
||||
sortField := "created_at"
|
||||
if params.Sort != "" {
|
||||
sortField = params.Sort
|
||||
}
|
||||
sortOrder := "DESC"
|
||||
if params.Order == "asc" {
|
||||
sortOrder = "ASC"
|
||||
}
|
||||
db = db.Order(fmt.Sprintf("%s %s", sortField, sortOrder))
|
||||
|
||||
db = db.Order("created_at DESC")
|
||||
return db
|
||||
})
|
||||
|
||||
@@ -126,19 +106,14 @@ func (s transferLayingService) GetAll(c *fiber.Ctx, params *validation.Query) ([
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
if params.ApprovalStatus != "" {
|
||||
var filtered []entity.LayingTransfer
|
||||
approvalRepo := commonRepo.NewApprovalRepository(s.Repository.DB())
|
||||
|
||||
for _, transfer := range transferLayings {
|
||||
latestApproval, err := approvalRepo.LatestByTarget(c.Context(), string(utils.ApprovalWorkflowTransferToLaying), transfer.Id, nil)
|
||||
if err == nil && latestApproval != nil && latestApproval.Action != nil {
|
||||
if string(*latestApproval.Action) == params.ApprovalStatus {
|
||||
filtered = append(filtered, transfer)
|
||||
}
|
||||
}
|
||||
approvalRepo := commonRepo.NewApprovalRepository(s.Repository.DB())
|
||||
for i, transfer := range transferLayings {
|
||||
latestApproval, err := approvalRepo.LatestByTarget(c.Context(), string(utils.ApprovalWorkflowTransferToLaying), transfer.Id, func(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("ActionUser")
|
||||
})
|
||||
if err == nil && latestApproval != nil {
|
||||
transferLayings[i].LatestApproval = latestApproval
|
||||
}
|
||||
transferLayings = filtered
|
||||
}
|
||||
|
||||
return transferLayings, total, nil
|
||||
@@ -155,7 +130,9 @@ func (s transferLayingService) GetOne(c *fiber.Ctx, id uint) (*entity.LayingTran
|
||||
}
|
||||
|
||||
approvalRepo := commonRepo.NewApprovalRepository(s.Repository.DB())
|
||||
latestApproval, err := approvalRepo.LatestByTarget(c.Context(), string(utils.ApprovalWorkflowTransferToLaying), transferLaying.Id, nil)
|
||||
latestApproval, err := approvalRepo.LatestByTarget(c.Context(), string(utils.ApprovalWorkflowTransferToLaying), transferLaying.Id, func(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("ActionUser")
|
||||
})
|
||||
if err == nil && latestApproval != nil {
|
||||
transferLaying.LatestApproval = latestApproval
|
||||
}
|
||||
@@ -361,7 +338,9 @@ func (s transferLayingService) UpdateOne(c *fiber.Ctx, req *validation.Update, i
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err := s.Repository.GetByID(c.Context(), id, nil)
|
||||
existingTransfer, err := s.Repository.GetByID(c.Context(), id, func(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("Sources.ProductWarehouse").Preload("Targets")
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "TransferLaying not found")
|
||||
@@ -382,26 +361,140 @@ func (s transferLayingService) UpdateOne(c *fiber.Ctx, req *validation.Update, i
|
||||
}
|
||||
}
|
||||
|
||||
updateBody := make(map[string]any)
|
||||
|
||||
if req.TransferDate != nil {
|
||||
updateBody["transfer_date"] = *req.TransferDate
|
||||
if _, err := s.ProjectFlockRepo.GetByID(c.Context(), req.SourceProjectFlockId, nil); err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Source project flock not found")
|
||||
}
|
||||
|
||||
if req.Reason != nil {
|
||||
updateBody["notes"] = *req.Reason
|
||||
if _, err := s.ProjectFlockRepo.GetByID(c.Context(), req.TargetProjectFlockId, nil); err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Target project flock not found")
|
||||
}
|
||||
|
||||
if len(updateBody) == 0 {
|
||||
return s.GetOne(c, id)
|
||||
transferDate, err := time.Parse("2006-01-02", req.TransferDate)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid transfer date format")
|
||||
}
|
||||
|
||||
if err := s.Repository.PatchOne(c.Context(), id, updateBody, nil); err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "TransferLaying not found")
|
||||
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
||||
projectFlockPopulationRepoTx := s.ProjectFlockPopulationRepo.WithTx(dbTransaction)
|
||||
productWarehouseRepoTx := s.ProductWarehouseRepo.WithTx(dbTransaction)
|
||||
|
||||
for _, oldSource := range existingTransfer.Sources {
|
||||
if oldSource.ProductWarehouseId != nil && oldSource.Qty > 0 {
|
||||
|
||||
if err := productWarehouseRepoTx.PatchOne(c.Context(), *oldSource.ProductWarehouseId, map[string]any{
|
||||
"quantity": gorm.Expr("quantity + ?", oldSource.Qty),
|
||||
}, nil); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to restore warehouse quantity")
|
||||
}
|
||||
|
||||
if err := s.restoreProjectFlockPopulation(c.Context(), projectFlockPopulationRepoTx, oldSource.SourceProjectFlockKandangId, oldSource.Qty); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
s.Log.Errorf("Failed to update transferLaying: %+v", err)
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to update transfer laying")
|
||||
|
||||
for _, oldSource := range existingTransfer.Sources {
|
||||
if err := dbTransaction.Delete(&oldSource).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to delete old source")
|
||||
}
|
||||
}
|
||||
|
||||
for _, oldTarget := range existingTransfer.Targets {
|
||||
if err := dbTransaction.Delete(&oldTarget).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to delete old target")
|
||||
}
|
||||
}
|
||||
|
||||
totalSourceQty := 0.0
|
||||
for _, source := range req.SourceKandangs {
|
||||
totalSourceQty += source.Quantity
|
||||
}
|
||||
|
||||
if err := s.Repository.WithTx(dbTransaction).PatchOne(c.Context(), id, map[string]any{
|
||||
"transfer_date": transferDate,
|
||||
"notes": req.Reason,
|
||||
"pending_usage_qty": &totalSourceQty,
|
||||
}, nil); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update transfer header")
|
||||
}
|
||||
|
||||
sourceWarehouseMap := make(map[uint]uint)
|
||||
for _, sourceDetail := range req.SourceKandangs {
|
||||
|
||||
populations, err := projectFlockPopulationRepoTx.GetByProjectFlockKandangID(c.Context(), sourceDetail.ProjectFlockKandangId)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to get populations")
|
||||
}
|
||||
|
||||
if len(populations) == 0 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Source kandang %d has no population available", sourceDetail.ProjectFlockKandangId))
|
||||
}
|
||||
|
||||
var totalPopulation float64
|
||||
var productWarehouseId uint
|
||||
|
||||
for _, pop := range populations {
|
||||
totalPopulation += pop.TotalQty
|
||||
if pop.ProductWarehouseId > 0 {
|
||||
productWarehouseId = pop.ProductWarehouseId
|
||||
}
|
||||
}
|
||||
|
||||
if totalPopulation < sourceDetail.Quantity {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Source kandang %d has insufficient quantity. Available: %.0f, Requested: %.0f", sourceDetail.ProjectFlockKandangId, totalPopulation, sourceDetail.Quantity))
|
||||
}
|
||||
|
||||
sourceWarehouseMap[sourceDetail.ProjectFlockKandangId] = productWarehouseId
|
||||
|
||||
source := entity.LayingTransferSource{
|
||||
LayingTransferId: id,
|
||||
SourceProjectFlockKandangId: sourceDetail.ProjectFlockKandangId,
|
||||
Qty: sourceDetail.Quantity,
|
||||
ProductWarehouseId: &productWarehouseId,
|
||||
}
|
||||
if err := dbTransaction.Create(&source).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create transfer source")
|
||||
}
|
||||
|
||||
if err := s.reduceProjectFlockPopulation(c.Context(), projectFlockPopulationRepoTx, sourceDetail.ProjectFlockKandangId, sourceDetail.Quantity); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := productWarehouseRepoTx.PatchOne(c.Context(), productWarehouseId, map[string]any{"quantity": gorm.Expr("quantity - ?", sourceDetail.Quantity)}, nil); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update source warehouse quantity")
|
||||
}
|
||||
}
|
||||
|
||||
for _, targetDetail := range req.TargetKandangs {
|
||||
targetPFK, err := s.ProjectFlockKandangRepo.GetByID(c.Context(), targetDetail.ProjectFlockKandangId)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to get target project flock kandang")
|
||||
}
|
||||
|
||||
targetWarehouse, err := s.WarehouseRepo.GetLatestByKandangID(c.Context(), targetPFK.KandangId)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("No warehouse found for target kandang %d", targetDetail.ProjectFlockKandangId))
|
||||
}
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to get target warehouse")
|
||||
}
|
||||
|
||||
target := entity.LayingTransferTarget{
|
||||
LayingTransferId: id,
|
||||
TargetProjectFlockKandangId: targetDetail.ProjectFlockKandangId,
|
||||
Qty: targetDetail.Quantity,
|
||||
ProductWarehouseId: &targetWarehouse.Id,
|
||||
}
|
||||
if err := dbTransaction.Create(&target).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create transfer target")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.GetOne(c, id)
|
||||
@@ -431,7 +524,6 @@ func (s transferLayingService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Cannot delete transfer laying with status %s", action))
|
||||
}
|
||||
}
|
||||
|
||||
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
||||
|
||||
productWarehouseRepoTx := s.ProductWarehouseRepo.WithTx(dbTransaction)
|
||||
@@ -464,9 +556,8 @@ func (s transferLayingService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||
for i := len(populations) - 1; i >= 0 && remainingToRestore > 0; i-- {
|
||||
pop := populations[i]
|
||||
restoreAmount := remainingToRestore
|
||||
if remainingToRestore < pop.TotalQty {
|
||||
|
||||
restoreAmount = remainingToRestore
|
||||
if pop.TotalQty < remainingToRestore {
|
||||
restoreAmount = pop.TotalQty
|
||||
}
|
||||
|
||||
newQty := pop.TotalQty + restoreAmount
|
||||
@@ -725,3 +816,58 @@ func (s *transferLayingService) reduceProjectFlockPopulation(ctx context.Context
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *transferLayingService) restoreProjectFlockPopulation(ctx context.Context, populationRepo ProjectFlockRepository.ProjectFlockPopulationRepository, projectFlockKandangID uint, quantityToRestore float64) error {
|
||||
populations, err := populationRepo.GetByProjectFlockKandangID(ctx, projectFlockKandangID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(populations) == 0 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "No populations found for restoration")
|
||||
}
|
||||
|
||||
if len(populations) > 0 {
|
||||
lastPop := populations[len(populations)-1]
|
||||
newQty := lastPop.TotalQty + quantityToRestore
|
||||
if err := populationRepo.PatchOne(ctx, lastPop.Id, map[string]any{"total_qty": newQty}, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s transferLayingService) GetAvailableQtyPerKandang(ctx *fiber.Ctx, projectFlockID uint) (*entity.ProjectFlock, map[uint]float64, error) {
|
||||
|
||||
pf, err := s.ProjectFlockRepo.GetByID(ctx.Context(), projectFlockID, func(db *gorm.DB) *gorm.DB {
|
||||
return db
|
||||
})
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get project flock %d: %+v", projectFlockID, err)
|
||||
return nil, nil, fiber.NewError(fiber.StatusNotFound, "Project flock not found")
|
||||
}
|
||||
|
||||
kandangs, _, err := s.ProjectFlockKandangRepo.GetAll(ctx.Context(), 0, 1000, func(db *gorm.DB) *gorm.DB {
|
||||
return db.Where("project_flock_id = ?", projectFlockID).Order("kandang_id ASC")
|
||||
})
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get kandangs for project flock %d: %+v", projectFlockID, err)
|
||||
return nil, nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch kandangs")
|
||||
}
|
||||
|
||||
kandangAvailableQty := make(map[uint]float64)
|
||||
for _, kandang := range kandangs {
|
||||
|
||||
totalQty, err := s.ProjectFlockPopulationRepo.GetTotalQtyByProjectFlockKandangID(ctx.Context(), kandang.Id)
|
||||
if err != nil {
|
||||
s.Log.Warnf("Failed to get total qty for kandang %d: %+v", kandang.Id, err)
|
||||
kandangAvailableQty[kandang.Id] = 0
|
||||
continue
|
||||
}
|
||||
|
||||
kandangAvailableQty[kandang.Id] = totalQty
|
||||
}
|
||||
|
||||
return pf, kandangAvailableQty, nil
|
||||
}
|
||||
|
||||
+8
-12
@@ -20,21 +20,17 @@ type Create struct {
|
||||
}
|
||||
|
||||
type Update struct {
|
||||
TransferDate *string `json:"transfer_date,omitempty" validate:"omitempty,datetime=2006-01-02"`
|
||||
Reason *string `json:"reason,omitempty" validate:"omitempty,max=1000"`
|
||||
TransferDate string `json:"transfer_date" validate:"required,datetime=2006-01-02"`
|
||||
SourceProjectFlockId uint `json:"source_project_flock_id" validate:"required"`
|
||||
TargetProjectFlockId uint `json:"target_project_flock_id" validate:"required"`
|
||||
SourceKandangs []SourceKandangDetail `json:"source_kandangs" validate:"required,min=1,dive,required"`
|
||||
TargetKandangs []TargetKandangDetail `json:"target_kandangs" validate:"required,min=1,dive,required"`
|
||||
Reason string `json:"reason" validate:"omitempty,max=1000"`
|
||||
}
|
||||
|
||||
type Query struct {
|
||||
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
|
||||
SourceProjectFlockId uint `query:"source_project_flock_id" validate:"omitempty"`
|
||||
TargetProjectFlockId uint `query:"target_project_flock_id" validate:"omitempty"`
|
||||
TransferDateFrom string `query:"transfer_date_from" validate:"omitempty,datetime=2006-01-02"`
|
||||
TransferDateTo string `query:"transfer_date_to" validate:"omitempty,datetime=2006-01-02"`
|
||||
ApprovalStatus string `query:"approval_status" validate:"omitempty,oneof=PENDING APPROVED REJECTED"` // Filter by latest approval status
|
||||
TransferNumber string `query:"transfer_number" validate:"omitempty"` // Search by transfer number
|
||||
Sort string `query:"sort" validate:"omitempty,oneof=created_at transfer_date"` // Sort by field
|
||||
Order string `query:"order" validate:"omitempty,oneof=asc desc"` // Sort order
|
||||
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
|
||||
}
|
||||
|
||||
type Approve struct {
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
|
||||
type UserRepository interface {
|
||||
commonrepo.BaseRepository[entity.User]
|
||||
IdExists(ctx context.Context, id uint) (bool, error)
|
||||
GetByIdUser(ctx context.Context, idUser int64, modifier func(*gorm.DB) *gorm.DB) (*entity.User, error)
|
||||
UpsertByIdUser(ctx context.Context, user *entity.User) error
|
||||
SoftDeleteByIdUser(ctx context.Context, idUser int64) error
|
||||
@@ -21,14 +22,20 @@ type UserRepository interface {
|
||||
|
||||
type UserRepositoryImpl struct {
|
||||
*commonrepo.BaseRepositoryImpl[entity.User]
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewUserRepository(db *gorm.DB) UserRepository {
|
||||
return &UserRepositoryImpl{
|
||||
BaseRepositoryImpl: commonrepo.NewBaseRepository[entity.User](db),
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *UserRepositoryImpl) IdExists(ctx context.Context, id uint) (bool, error) {
|
||||
return commonrepo.Exists[entity.User](ctx, r.db, id)
|
||||
}
|
||||
|
||||
func (r *UserRepositoryImpl) GetByIdUser(
|
||||
ctx context.Context,
|
||||
idUser int64,
|
||||
|
||||
@@ -10,11 +10,13 @@ import (
|
||||
|
||||
approvals "gitlab.com/mbugroup/lti-api.git/internal/modules/approvals"
|
||||
constants "gitlab.com/mbugroup/lti-api.git/internal/modules/constants"
|
||||
expenses "gitlab.com/mbugroup/lti-api.git/internal/modules/expenses"
|
||||
inventory "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory"
|
||||
marketing "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing"
|
||||
master "gitlab.com/mbugroup/lti-api.git/internal/modules/master"
|
||||
ssoModule "gitlab.com/mbugroup/lti-api.git/internal/modules/sso"
|
||||
production "gitlab.com/mbugroup/lti-api.git/internal/modules/production"
|
||||
purchases "gitlab.com/mbugroup/lti-api.git/internal/modules/purchases"
|
||||
ssoModule "gitlab.com/mbugroup/lti-api.git/internal/modules/sso"
|
||||
users "gitlab.com/mbugroup/lti-api.git/internal/modules/users"
|
||||
// MODULE IMPORTS
|
||||
)
|
||||
@@ -33,6 +35,10 @@ func Routes(app *fiber.App, db *gorm.DB) {
|
||||
purchases.PurchaseModule{},
|
||||
production.ProductionModule{},
|
||||
approvals.ApprovalModule{},
|
||||
purchases.PurchaseModule{},
|
||||
marketing.MarketingModule{},
|
||||
ssoModule.Module{},
|
||||
expenses.ExpenseModule{},
|
||||
ssoModule.Module{},
|
||||
// MODULE REGISTRY
|
||||
}
|
||||
|
||||
@@ -234,6 +234,40 @@ var PurchaseApprovalSteps = map[approvalutils.ApprovalStep]string{
|
||||
PurchaseStepCompleted: "Selesai",
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// Marketings Approval
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
const (
|
||||
ApprovalWorkflowMarketing approvalutils.ApprovalWorkflowKey = approvalutils.ApprovalWorkflowKey("MARKETINGS")
|
||||
MarketingStepPengajuan approvalutils.ApprovalStep = 1
|
||||
MarketingStepSalesOrder approvalutils.ApprovalStep = 2
|
||||
MarketingDeliveryOrder approvalutils.ApprovalStep = 3
|
||||
)
|
||||
|
||||
var MarketingApprovalSteps = map[approvalutils.ApprovalStep]string{
|
||||
MarketingStepPengajuan: "Pengajuan",
|
||||
MarketingStepSalesOrder: "Sales Order",
|
||||
MarketingDeliveryOrder: "Delivery Order",
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// Expense Approval
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
const (
|
||||
ApprovalWorkflowExpense approvalutils.ApprovalWorkflowKey = approvalutils.ApprovalWorkflowKey("EXPENSES")
|
||||
ExpenseStepPengajuan approvalutils.ApprovalStep = 1
|
||||
ExpenseStepManager approvalutils.ApprovalStep = 2
|
||||
ExpenseStepFinance approvalutils.ApprovalStep = 3
|
||||
)
|
||||
|
||||
var ExpenseApprovalSteps = map[approvalutils.ApprovalStep]string{
|
||||
ExpenseStepPengajuan: "Pengajuan",
|
||||
ExpenseStepManager: "Manager",
|
||||
ExpenseStepFinance: "Finance",
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// Validators
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user