mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
Merge branch 'fix/hpp-harian-feed' into 'development'
[FIX][BE]: add query for feed supplier See merge request mbugroup/lti-api!312
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
|
"gitlab.com/mbugroup/lti-api.git/internal/utils/fifo"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -208,6 +209,75 @@ func (r *hppPerKandangRepository) GetFeedOvkDocCostByPeriod(ctx context.Context,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
feedRows := make([]struct {
|
||||||
|
ProjectFlockKandangID uint
|
||||||
|
FeedCost float64
|
||||||
|
SupplierID *uint
|
||||||
|
SupplierName *string
|
||||||
|
SupplierAlias *string
|
||||||
|
}, 0)
|
||||||
|
|
||||||
|
feedQuery := r.db.WithContext(ctx).
|
||||||
|
Table("recordings AS r").
|
||||||
|
Select(`
|
||||||
|
r.project_flock_kandangs_id AS project_flock_kandang_id,
|
||||||
|
s.id AS supplier_id,
|
||||||
|
s.name AS supplier_name,
|
||||||
|
s.alias AS supplier_alias`).
|
||||||
|
Joins("JOIN recording_stocks AS rs ON rs.recording_id = r.id").
|
||||||
|
Joins("JOIN product_warehouses AS pw ON pw.id = rs.product_warehouse_id").
|
||||||
|
Joins("JOIN flags AS f ON f.flagable_id = pw.product_id AND f.flagable_type = ?", entity.FlagableTypeProduct).
|
||||||
|
Joins("JOIN stock_allocations AS sa ON sa.usable_type = ? AND sa.usable_id = rs.id AND sa.stockable_type = ?", fifo.UsableKeyRecordingStock.String(), fifo.StockableKeyPurchaseItems.String()).
|
||||||
|
Joins("JOIN purchase_items AS pi ON pi.id = sa.stockable_id").
|
||||||
|
Joins("LEFT JOIN purchases AS pur ON pur.id = pi.purchase_id").
|
||||||
|
Joins("LEFT JOIN suppliers AS s ON s.id = pur.supplier_id").
|
||||||
|
Where("r.project_flock_kandangs_id IN ?", projectFlockKandangIDs).
|
||||||
|
Where("r.record_datetime >= ? AND r.record_datetime < ?", start, end).
|
||||||
|
Where("f.name = ?", utils.FlagPakan).
|
||||||
|
Group("r.project_flock_kandangs_id, s.id, s.name, s.alias")
|
||||||
|
|
||||||
|
if err := feedQuery.Scan(&feedRows).Error; err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
feedSuppliers := make([]HppPerKandangSupplierRow, 0)
|
||||||
|
feedSeen := make(map[uint]map[uint]bool)
|
||||||
|
for _, feed := range feedRows {
|
||||||
|
entry, ok := costMap[feed.ProjectFlockKandangID]
|
||||||
|
if !ok {
|
||||||
|
rows = append(rows, HppPerKandangCostRow{
|
||||||
|
ProjectFlockKandangID: feed.ProjectFlockKandangID,
|
||||||
|
})
|
||||||
|
entry = &rows[len(rows)-1]
|
||||||
|
costMap[feed.ProjectFlockKandangID] = entry
|
||||||
|
}
|
||||||
|
entry.FeedCost += feed.FeedCost
|
||||||
|
if feed.SupplierID != nil {
|
||||||
|
if feedSeen[feed.ProjectFlockKandangID] == nil {
|
||||||
|
feedSeen[feed.ProjectFlockKandangID] = make(map[uint]bool)
|
||||||
|
}
|
||||||
|
if !feedSeen[feed.ProjectFlockKandangID][*feed.SupplierID] {
|
||||||
|
feedSeen[feed.ProjectFlockKandangID][*feed.SupplierID] = true
|
||||||
|
supplierName := ""
|
||||||
|
if feed.SupplierName != nil {
|
||||||
|
supplierName = *feed.SupplierName
|
||||||
|
}
|
||||||
|
supplierAlias := ""
|
||||||
|
if feed.SupplierAlias != nil {
|
||||||
|
supplierAlias = *feed.SupplierAlias
|
||||||
|
}
|
||||||
|
feedSuppliers = append(feedSuppliers, HppPerKandangSupplierRow{
|
||||||
|
ProjectFlockKandangID: feed.ProjectFlockKandangID,
|
||||||
|
SupplierID: *feed.SupplierID,
|
||||||
|
SupplierName: supplierName,
|
||||||
|
SupplierAlias: supplierAlias,
|
||||||
|
Category: "FEED",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
docSuppliers = append(docSuppliers, feedSuppliers...)
|
||||||
return rows, docSuppliers, nil
|
return rows, docSuppliers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user