mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-25 07:45:44 +00:00
feat/BE/US-284/TASK-,299-Create API (GET ONE in tab Perhitungan Sapronak)
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/modules/closings/dto"
|
||||
)
|
||||
|
||||
type SapronakFormatter interface {
|
||||
ProjectPayload(reports []dto.SapronakReportDTO) dto.SapronakProjectAggregatedDTO
|
||||
KandangPayload(report *dto.SapronakReportDTO) dto.SapronakProjectAggregatedDTO
|
||||
}
|
||||
|
||||
type sapronakFormatter struct{}
|
||||
|
||||
func NewSapronakFormatter() SapronakFormatter {
|
||||
return &sapronakFormatter{}
|
||||
}
|
||||
|
||||
func (f *sapronakFormatter) ProjectPayload(reports []dto.SapronakReportDTO) dto.SapronakProjectAggregatedDTO {
|
||||
result := dto.SapronakProjectAggregatedDTO{
|
||||
Doc: dto.SapronakCategoryDTO{},
|
||||
Ovk: dto.SapronakCategoryDTO{},
|
||||
Pakan: dto.SapronakCategoryDTO{},
|
||||
}
|
||||
|
||||
if len(reports) == 0 {
|
||||
return result
|
||||
}
|
||||
|
||||
rep := reports[0]
|
||||
return f.mapFromReport(&rep)
|
||||
}
|
||||
|
||||
func (f *sapronakFormatter) KandangPayload(report *dto.SapronakReportDTO) dto.SapronakProjectAggregatedDTO {
|
||||
return f.mapFromReport(report)
|
||||
}
|
||||
|
||||
func (f *sapronakFormatter) mapFromReport(report *dto.SapronakReportDTO) dto.SapronakProjectAggregatedDTO {
|
||||
result := dto.SapronakProjectAggregatedDTO{
|
||||
Doc: dto.SapronakCategoryDTO{},
|
||||
Ovk: dto.SapronakCategoryDTO{},
|
||||
Pakan: dto.SapronakCategoryDTO{},
|
||||
}
|
||||
|
||||
if report == nil {
|
||||
return result
|
||||
}
|
||||
|
||||
byFlag := map[string]*dto.SapronakCategoryDTO{
|
||||
"DOC": &result.Doc,
|
||||
"OVK": &result.Ovk,
|
||||
"PAKAN": &result.Pakan,
|
||||
}
|
||||
|
||||
formatDate := func(t *time.Time) string {
|
||||
if t == nil {
|
||||
return ""
|
||||
}
|
||||
return t.Format("02-Jan-2006")
|
||||
}
|
||||
|
||||
for _, group := range report.Groups {
|
||||
flag := strings.ToUpper(group.Flag)
|
||||
target := byFlag[flag]
|
||||
if target == nil {
|
||||
continue
|
||||
}
|
||||
for idx, item := range group.Items {
|
||||
qtyUsed := item.QtyKeluar
|
||||
if qtyUsed == 0 {
|
||||
qtyUsed = item.QtyMasuk
|
||||
}
|
||||
|
||||
target.Rows = append(target.Rows, dto.SapronakCategoryRowDTO{
|
||||
ID: idx + 1,
|
||||
Date: formatDate(item.Tanggal),
|
||||
ReferenceNumber: item.NoReferensi,
|
||||
QtyIn: item.QtyMasuk,
|
||||
QtyOut: item.QtyKeluar,
|
||||
QtyUsed: qtyUsed,
|
||||
Description: item.ProductName,
|
||||
ProductCategory: item.ProductName,
|
||||
UnitPrice: item.Harga,
|
||||
TotalAmount: item.Nilai,
|
||||
Notes: "-",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
buildTotals := func(cat *dto.SapronakCategoryDTO, label string) {
|
||||
var qtyIn, qtyOut, qtyUsed, total float64
|
||||
for _, r := range cat.Rows {
|
||||
qtyIn += r.QtyIn
|
||||
qtyOut += r.QtyOut
|
||||
qtyUsed += r.QtyUsed
|
||||
total += r.TotalAmount
|
||||
}
|
||||
avg := 0.0
|
||||
if qtyIn > 0 {
|
||||
avg = total / qtyIn
|
||||
}
|
||||
cat.Total = dto.SapronakCategoryTotalDTO{
|
||||
Label: label,
|
||||
QtyIn: qtyIn,
|
||||
QtyOut: qtyOut,
|
||||
QtyUsed: qtyUsed,
|
||||
AvgUnitPrice: avg,
|
||||
TotalAmount: total,
|
||||
}
|
||||
}
|
||||
|
||||
buildTotals(&result.Doc, "TOTAL DOC")
|
||||
buildTotals(&result.Ovk, "TOTAL OVK")
|
||||
buildTotals(&result.Pakan, "TOTAL PAKAN")
|
||||
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user