mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-06-09 15:07:49 +00:00
add filter warehouse to marketing;add detail export recording egg; adjust format export marketing
This commit is contained in:
@@ -8,10 +8,12 @@ import (
|
||||
"time"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/common/exportprogress"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/dto"
|
||||
service "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/services"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/validations"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/response"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
@@ -75,6 +77,43 @@ func (u *RecordingController) GetAll(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
listDTO := dto.ToRecordingListDTOs(result)
|
||||
|
||||
recordingIDs := make([]uint, 0, len(result))
|
||||
for i := range result {
|
||||
if result[i].Id != 0 {
|
||||
recordingIDs = append(recordingIDs, result[i].Id)
|
||||
}
|
||||
}
|
||||
if len(recordingIDs) > 0 {
|
||||
eggs, err := u.RecordingService.GetEggsWithFlagsByRecordingIDs(c.Context(), recordingIDs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
eggByRecording := make(map[uint][]entity.RecordingEgg, len(recordingIDs))
|
||||
for _, egg := range eggs {
|
||||
eggByRecording[egg.RecordingId] = append(eggByRecording[egg.RecordingId], egg)
|
||||
}
|
||||
for i := range listDTO {
|
||||
id := listDTO[i].Id
|
||||
if eggList, ok := eggByRecording[id]; ok {
|
||||
breakdown := make(map[string]dto.EggExportBreakdownDTO)
|
||||
for _, egg := range eggList {
|
||||
flagName := eggTypeFromProductName(egg.ProductWarehouse.Product.Name)
|
||||
if flagName == "" {
|
||||
continue
|
||||
}
|
||||
entry := breakdown[flagName]
|
||||
entry.Qty += egg.Qty
|
||||
if egg.Weight != nil {
|
||||
entry.Kg += *egg.Weight
|
||||
}
|
||||
breakdown[flagName] = entry
|
||||
}
|
||||
listDTO[i].EggExportBreakdown = breakdown
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if strings.EqualFold(exportType, "excel") {
|
||||
return exportRecordingListExcel(c, listDTO)
|
||||
}
|
||||
@@ -94,6 +133,33 @@ func (u *RecordingController) GetAll(c *fiber.Ctx) error {
|
||||
})
|
||||
}
|
||||
|
||||
// eggTypeFromProductName maps product name to egg type flag name by keyword matching.
|
||||
// Falls back to empty string if no keyword matches.
|
||||
func eggTypeFromProductName(name string) string {
|
||||
normalized := strings.ToLower(strings.TrimSpace(name))
|
||||
if normalized == "" {
|
||||
return ""
|
||||
}
|
||||
// Ordered longest-first to prefer "papacal" over partial match of "pacal", etc.
|
||||
keywords := []struct {
|
||||
keyword string
|
||||
flag string
|
||||
}{
|
||||
{"papacal", string(utils.FlagTelurPapacal)},
|
||||
{"jumbo", string(utils.FlagTelurJumbo)},
|
||||
{"retak", string(utils.FlagTelurRetak)},
|
||||
{"putih", string(utils.FlagTelurPutih)},
|
||||
{"pecah", string(utils.FlagTelurPecah)},
|
||||
{"utuh", string(utils.FlagTelurUtuh)},
|
||||
}
|
||||
for _, k := range keywords {
|
||||
if strings.Contains(normalized, k.keyword) {
|
||||
return k.flag
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (u *RecordingController) GetOne(c *fiber.Ctx) error {
|
||||
param := c.Params("id")
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/dto"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/xuri/excelize/v2"
|
||||
@@ -79,6 +80,18 @@ func setRecordingExportColumns(file *excelize.File, sheet string) error {
|
||||
"AB": 18,
|
||||
"AC": 24,
|
||||
"AD": 18,
|
||||
"AE": 12,
|
||||
"AF": 10,
|
||||
"AG": 12,
|
||||
"AH": 10,
|
||||
"AI": 12,
|
||||
"AJ": 10,
|
||||
"AK": 12,
|
||||
"AL": 10,
|
||||
"AM": 12,
|
||||
"AN": 10,
|
||||
"AO": 12,
|
||||
"AP": 10,
|
||||
}
|
||||
|
||||
for col, width := range columnWidths {
|
||||
@@ -208,6 +221,31 @@ func setRecordingExportHeaders(file *excelize.File, sheet string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
eggTypes := []struct {
|
||||
col1, col2, label string
|
||||
}{
|
||||
{"AE", "AF", "Telur Utuh"},
|
||||
{"AG", "AH", "Telur Pecah"},
|
||||
{"AI", "AJ", "Telur Putih"},
|
||||
{"AK", "AL", "Telur Retak"},
|
||||
{"AM", "AN", "Telur Papacal"},
|
||||
{"AO", "AP", "Telur Jumbo"},
|
||||
}
|
||||
for _, et := range eggTypes {
|
||||
if err := file.MergeCell(sheet, et.col1+"1", et.col2+"1"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, et.col1+"1", et.label); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, et.col1+"2", "Butir"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellValue(sheet, et.col2+"2", "Kg"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
headerStyle, err := file.NewStyle(&excelize.Style{
|
||||
Font: &excelize.Font{
|
||||
Bold: true,
|
||||
@@ -234,7 +272,7 @@ func setRecordingExportHeaders(file *excelize.File, sheet string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
return file.SetCellStyle(sheet, "A1", "AD2", headerStyle)
|
||||
return file.SetCellStyle(sheet, "A1", "AP2", headerStyle)
|
||||
}
|
||||
|
||||
func setRecordingExportRows(file *excelize.File, sheet string, items []dto.RecordingListDTO) error {
|
||||
@@ -245,7 +283,8 @@ func setRecordingExportRows(file *excelize.File, sheet string, items []dto.Recor
|
||||
columns := []string{
|
||||
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
|
||||
"O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB",
|
||||
"AC", "AD",
|
||||
"AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN",
|
||||
"AO", "AP",
|
||||
}
|
||||
|
||||
currentRow := 3
|
||||
@@ -293,14 +332,14 @@ func setRecordingExportRows(file *excelize.File, sheet string, items []dto.Recor
|
||||
// Expand recordings into one row per sapronak
|
||||
type sapronakRow struct {
|
||||
name string
|
||||
input string
|
||||
input interface{} // float64 for numeric, string "-" for placeholder
|
||||
}
|
||||
sapronaks := make([]sapronakRow, 0)
|
||||
if len(item.FeedUsage) > 0 {
|
||||
for _, fu := range item.FeedUsage {
|
||||
sapronaks = append(sapronaks, sapronakRow{
|
||||
name: safeExportText(fu.ProductName),
|
||||
input: formatNumberID(fu.UsageAmount+fu.PendingQty, 2, true),
|
||||
input: fu.UsageAmount + fu.PendingQty,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
@@ -311,37 +350,66 @@ func setRecordingExportRows(file *excelize.File, sheet string, items []dto.Recor
|
||||
|
||||
for sIdx, s := range sapronaks {
|
||||
if sIdx == 0 {
|
||||
eggQty := func(flagName string) int {
|
||||
if item.EggExportBreakdown != nil {
|
||||
if bd, ok := item.EggExportBreakdown[flagName]; ok {
|
||||
return bd.Qty
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
eggKg := func(flagName string) float64 {
|
||||
if item.EggExportBreakdown != nil {
|
||||
if bd, ok := item.EggExportBreakdown[flagName]; ok {
|
||||
return bd.Kg
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
rowValues := []interface{}{
|
||||
i + 1, // A
|
||||
locationName, // B
|
||||
safeExportText(item.ProjectFlock.FlockName), // C
|
||||
kandangName, // D
|
||||
item.ProjectFlock.Period, // E
|
||||
i + 1, // A
|
||||
locationName, // B
|
||||
safeExportText(item.ProjectFlock.FlockName), // C
|
||||
kandangName, // D
|
||||
item.ProjectFlock.Period, // E
|
||||
formatCategoryLabel(item.ProjectFlock.ProjectFlockCategory), // F
|
||||
formatAgeLabel(item), // G
|
||||
formatDateIndonesian(item.RecordDatetime), // H
|
||||
formatNumberID(item.ProjectFlock.TotalChickQty, 0, false), // I
|
||||
formatNumberID(item.FcrValue, 2, true), // J
|
||||
formatNumberID(fcrStd, 2, true), // K
|
||||
formatNumberID(item.FeedIntake, 2, true), // L
|
||||
formatNumberID(feedIntakeStd, 2, true), // M
|
||||
formatPercentID(item.CumDepletionRate, 2), // N
|
||||
formatPercentID(maxDepletionStd, 2), // O
|
||||
formatNumberID(item.TotalDepletionQty, 2, true), // P
|
||||
formatNumberID(item.EggMass, 2, true), // Q
|
||||
formatNumberID(eggMassStd, 2, true), // R
|
||||
formatNumberID(item.EggWeight, 2, true), // S
|
||||
formatNumberID(eggWeightStd, 2, true), // T
|
||||
formatPercentID(item.HenDay, 2), // U
|
||||
formatPercentID(henDayStd, 2), // V
|
||||
formatPercentID(item.HenHouse, 2), // W
|
||||
formatPercentID(henHouseStd, 2), // X
|
||||
formatApprovalStatus(item), // Y
|
||||
formatAgeLabel(item), // G
|
||||
formatDateIndonesian(item.RecordDatetime), // H
|
||||
item.ProjectFlock.TotalChickQty, // I
|
||||
item.FcrValue, // J
|
||||
fcrStd, // K
|
||||
item.FeedIntake, // L
|
||||
feedIntakeStd, // M
|
||||
item.CumDepletionRate, // N
|
||||
maxDepletionStd, // O
|
||||
item.TotalDepletionQty, // P
|
||||
item.EggMass, // Q
|
||||
eggMassStd, // R
|
||||
item.EggWeight, // S
|
||||
eggWeightStd, // T
|
||||
item.HenDay, // U
|
||||
henDayStd, // V
|
||||
item.HenHouse, // W
|
||||
henHouseStd, // X
|
||||
formatApprovalStatus(item), // Y
|
||||
safeExportText(pointerString(item.Approval.Notes)), // Z
|
||||
createdBy, // AA
|
||||
formatDateIndonesian(item.CreatedAt), // AB
|
||||
s.name, // AC
|
||||
s.input, // AD
|
||||
createdBy, // AA
|
||||
formatDateIndonesian(item.CreatedAt), // AB
|
||||
s.name, // AC
|
||||
s.input, // AD
|
||||
eggQty(string(utils.FlagTelurUtuh)), // AE
|
||||
eggKg(string(utils.FlagTelurUtuh)), // AF
|
||||
eggQty(string(utils.FlagTelurPecah)), // AG
|
||||
eggKg(string(utils.FlagTelurPecah)), // AH
|
||||
eggQty(string(utils.FlagTelurPutih)), // AI
|
||||
eggKg(string(utils.FlagTelurPutih)), // AJ
|
||||
eggQty(string(utils.FlagTelurRetak)), // AK
|
||||
eggKg(string(utils.FlagTelurRetak)), // AL
|
||||
eggQty(string(utils.FlagTelurPapacal)), // AM
|
||||
eggKg(string(utils.FlagTelurPapacal)), // AN
|
||||
eggQty(string(utils.FlagTelurJumbo)), // AO
|
||||
eggKg(string(utils.FlagTelurJumbo)), // AP
|
||||
}
|
||||
|
||||
for idx, col := range columns {
|
||||
@@ -379,7 +447,7 @@ func setRecordingExportRows(file *excelize.File, sheet string, items []dto.Recor
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.SetCellStyle(sheet, "A3", fmt.Sprintf("AD%d", lastRow), dataCenterStyle); err != nil {
|
||||
if err := file.SetCellStyle(sheet, "A3", fmt.Sprintf("AP%d", lastRow), dataCenterStyle); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -445,6 +513,7 @@ func setRecordingExportRows(file *excelize.File, sheet string, items []dto.Recor
|
||||
|
||||
mergeCols := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
|
||||
"O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB",
|
||||
"AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AO", "AP",
|
||||
}
|
||||
for _, rng := range itemRanges {
|
||||
if rng.end > rng.start {
|
||||
@@ -454,6 +523,53 @@ func setRecordingExportRows(file *excelize.File, sheet string, items []dto.Recor
|
||||
}
|
||||
file.SetCellStyle(sheet, fmt.Sprintf("AC%d", rng.end), fmt.Sprintf("AC%d", rng.end), borderBottomLeftStyle)
|
||||
file.SetCellStyle(sheet, fmt.Sprintf("AD%d", rng.end), fmt.Sprintf("AD%d", rng.end), borderBottomCenterStyle)
|
||||
// Egg columns use center + thick bottom border
|
||||
for _, col := range []string{"AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AO", "AP"} {
|
||||
file.SetCellStyle(sheet, fmt.Sprintf("%s%d", col, rng.end), fmt.Sprintf("%s%d", col, rng.end), borderBottomCenterStyle)
|
||||
}
|
||||
}
|
||||
|
||||
numFmtInt := "0"
|
||||
numberIntStyle, err := file.NewStyle(&excelize.Style{
|
||||
CustomNumFmt: &numFmtInt,
|
||||
Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
|
||||
Border: []excelize.Border{
|
||||
{Type: "left", Color: "E6E6E6", Style: 1},
|
||||
{Type: "top", Color: "E6E6E6", Style: 1},
|
||||
{Type: "bottom", Color: "E6E6E6", Style: 1},
|
||||
{Type: "right", Color: "E6E6E6", Style: 1},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
numFmtFloat := "0.00"
|
||||
numberFloatStyle, err := file.NewStyle(&excelize.Style{
|
||||
CustomNumFmt: &numFmtFloat,
|
||||
Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
|
||||
Border: []excelize.Border{
|
||||
{Type: "left", Color: "E6E6E6", Style: 1},
|
||||
{Type: "top", Color: "E6E6E6", Style: 1},
|
||||
{Type: "bottom", Color: "E6E6E6", Style: 1},
|
||||
{Type: "right", Color: "E6E6E6", Style: 1},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
intCols := []string{"E", "I", "AE", "AG", "AI", "AK", "AM", "AO"}
|
||||
for _, col := range intCols {
|
||||
if err := file.SetCellStyle(sheet, col+"3", fmt.Sprintf("%s%d", col, lastRow), numberIntStyle); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
floatCols := []string{"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "AD", "AF", "AH", "AJ", "AL", "AN", "AP"}
|
||||
for _, col := range floatCols {
|
||||
if err := file.SetCellStyle(sheet, col+"3", fmt.Sprintf("%s%d", col, lastRow), numberFloatStyle); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -100,14 +100,20 @@ type RecordingFeedUsageDTO struct {
|
||||
PendingQty float64 `json:"pending_qty"`
|
||||
}
|
||||
|
||||
type EggExportBreakdownDTO struct {
|
||||
Qty int `json:"qty"`
|
||||
Kg float64 `json:"kg"`
|
||||
}
|
||||
|
||||
type RecordingListDTO struct {
|
||||
RecordingRelationDTO
|
||||
CreatedUser *userDTO.UserRelationDTO `json:"created_user,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Kandang *RecordingKandangDTO `json:"kandang,omitempty"`
|
||||
Location *RecordingLocationDTO `json:"location,omitempty"`
|
||||
FeedUsage []RecordingFeedUsageDTO `json:"feed_usage,omitempty"`
|
||||
CreatedUser *userDTO.UserRelationDTO `json:"created_user,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Kandang *RecordingKandangDTO `json:"kandang,omitempty"`
|
||||
Location *RecordingLocationDTO `json:"location,omitempty"`
|
||||
FeedUsage []RecordingFeedUsageDTO `json:"feed_usage,omitempty"`
|
||||
EggExportBreakdown map[string]EggExportBreakdownDTO `json:"egg_breakdown,omitempty"`
|
||||
}
|
||||
|
||||
type RecordingDetailDTO struct {
|
||||
|
||||
@@ -51,6 +51,7 @@ type RecordingRepository interface {
|
||||
UpdateEggTotalQty(tx *gorm.DB, eggID uint, totalQty float64) error
|
||||
UpdateEggWeight(tx *gorm.DB, eggID uint, weight *float64) error
|
||||
GetRecordingEggByID(ctx context.Context, id uint, modifier func(*gorm.DB) *gorm.DB) (*entity.RecordingEgg, error)
|
||||
GetEggsWithFlagsByRecordingIDs(ctx context.Context, recordingIDs []uint) ([]entity.RecordingEgg, error)
|
||||
|
||||
ExistsOnDate(ctx context.Context, projectFlockKandangId uint, recordTime time.Time) (bool, error)
|
||||
|
||||
@@ -581,6 +582,22 @@ func (r *RecordingRepositoryImpl) GetRecordingEggByID(
|
||||
return &egg, nil
|
||||
}
|
||||
|
||||
func (r *RecordingRepositoryImpl) GetEggsWithFlagsByRecordingIDs(ctx context.Context, recordingIDs []uint) ([]entity.RecordingEgg, error) {
|
||||
if len(recordingIDs) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var eggs []entity.RecordingEgg
|
||||
err := r.DB().WithContext(ctx).
|
||||
Preload("ProductWarehouse.Product").
|
||||
Where("recording_eggs.recording_id IN ?", recordingIDs).
|
||||
Find(&eggs).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return eggs, nil
|
||||
}
|
||||
|
||||
func (r *RecordingRepositoryImpl) ExistsOnDate(ctx context.Context, projectFlockKandangId uint, recordTime time.Time) (bool, error) {
|
||||
if projectFlockKandangId == 0 {
|
||||
return false, nil
|
||||
|
||||
@@ -46,6 +46,7 @@ type RecordingService interface {
|
||||
DeleteOne(ctx *fiber.Ctx, id uint) error
|
||||
Approval(ctx *fiber.Ctx, req *validation.Approve) ([]entity.Recording, error)
|
||||
GetProgressRows(ctx *fiber.Ctx, query *exportprogress.Query) ([]exportprogress.Row, error)
|
||||
GetEggsWithFlagsByRecordingIDs(ctx context.Context, recordingIDs []uint) ([]entity.RecordingEgg, error)
|
||||
}
|
||||
|
||||
type recordingService struct {
|
||||
@@ -259,6 +260,10 @@ func (s recordingService) GetProgressRows(c *fiber.Ctx, query *exportprogress.Qu
|
||||
return s.Repository.GetProgressRows(c.Context(), query.StartDate, query.EndDate, scope.IDs, scope.Restrict)
|
||||
}
|
||||
|
||||
func (s recordingService) GetEggsWithFlagsByRecordingIDs(ctx context.Context, recordingIDs []uint) ([]entity.RecordingEgg, error) {
|
||||
return s.Repository.GetEggsWithFlagsByRecordingIDs(ctx, recordingIDs)
|
||||
}
|
||||
|
||||
func (s recordingService) GetOne(c *fiber.Ctx, id uint) (*entity.Recording, error) {
|
||||
if err := m.EnsureRecordingAccess(c, s.Repository.DB(), id); err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user