mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
126 lines
3.0 KiB
Go
126 lines
3.0 KiB
Go
package service
|
|
|
|
import (
|
|
"strings"
|
|
"time"
|
|
|
|
"gitlab.com/mbugroup/lti-api.git/internal/modules/closings/dto"
|
|
)
|
|
|
|
type SapronakFormatter interface {
|
|
ProjectPayload(reports []dto.SapronakReportDTO, flag string) dto.SapronakProjectAggregatedDTO
|
|
KandangPayload(report *dto.SapronakReportDTO, flag string) dto.SapronakProjectAggregatedDTO
|
|
}
|
|
|
|
type sapronakFormatter struct{}
|
|
|
|
func NewSapronakFormatter() SapronakFormatter {
|
|
return &sapronakFormatter{}
|
|
}
|
|
|
|
func (f *sapronakFormatter) ProjectPayload(reports []dto.SapronakReportDTO, flag string) dto.SapronakProjectAggregatedDTO {
|
|
result := dto.SapronakProjectAggregatedDTO{}
|
|
|
|
if len(reports) == 0 {
|
|
return result
|
|
}
|
|
|
|
rep := reports[0]
|
|
return f.mapFromReport(&rep, flag)
|
|
}
|
|
|
|
func (f *sapronakFormatter) KandangPayload(report *dto.SapronakReportDTO, flag string) dto.SapronakProjectAggregatedDTO {
|
|
return f.mapFromReport(report, flag)
|
|
}
|
|
|
|
func (f *sapronakFormatter) mapFromReport(report *dto.SapronakReportDTO, flag string) dto.SapronakProjectAggregatedDTO {
|
|
result := dto.SapronakProjectAggregatedDTO{}
|
|
|
|
if report == nil {
|
|
return result
|
|
}
|
|
|
|
filter := strings.ToUpper(strings.TrimSpace(flag))
|
|
|
|
byFlag := map[string]**dto.SapronakCategoryDTO{}
|
|
if filter == "" || filter == "DOC" {
|
|
result.Doc = &dto.SapronakCategoryDTO{}
|
|
byFlag["DOC"] = &result.Doc
|
|
}
|
|
if filter == "" || filter == "OVK" {
|
|
result.Ovk = &dto.SapronakCategoryDTO{}
|
|
byFlag["OVK"] = &result.Ovk
|
|
}
|
|
if filter == "" || filter == "PAKAN" {
|
|
result.Pakan = &dto.SapronakCategoryDTO{}
|
|
byFlag["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)
|
|
ptr := byFlag[flag]
|
|
if ptr == nil || *ptr == nil {
|
|
continue
|
|
}
|
|
target := *ptr
|
|
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) {
|
|
if cat == nil {
|
|
return
|
|
}
|
|
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
|
|
}
|