mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-06-09 15:07:49 +00:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 22bf66dbb9 |
@@ -7,20 +7,7 @@ type RecordingStock struct {
|
|||||||
ProjectFlockKandangId *uint `gorm:"column:project_flock_kandang_id;index"`
|
ProjectFlockKandangId *uint `gorm:"column:project_flock_kandang_id;index"`
|
||||||
UsageQty *float64 `gorm:"column:usage_qty"`
|
UsageQty *float64 `gorm:"column:usage_qty"`
|
||||||
PendingQty *float64 `gorm:"column:pending_qty"`
|
PendingQty *float64 `gorm:"column:pending_qty"`
|
||||||
TotalPrice float64 `gorm:"-"`
|
|
||||||
Allocations []RecordingStockAlloc `gorm:"-"`
|
|
||||||
|
|
||||||
Recording Recording `gorm:"foreignKey:RecordingId;references:Id"`
|
Recording Recording `gorm:"foreignKey:RecordingId;references:Id"`
|
||||||
ProductWarehouse ProductWarehouse `gorm:"foreignKey:ProductWarehouseId;references:Id"`
|
ProductWarehouse ProductWarehouse `gorm:"foreignKey:ProductWarehouseId;references:Id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type RecordingStockAlloc struct {
|
|
||||||
SourceType string
|
|
||||||
SourceId uint
|
|
||||||
PrNumber string
|
|
||||||
PoNumber string
|
|
||||||
AdjNumber string
|
|
||||||
Qty float64
|
|
||||||
UnitPrice float64
|
|
||||||
Subtotal float64
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -72,9 +72,9 @@ func (s kandangGroupService) GetAll(c *fiber.Ctx, params *validation.Query) ([]e
|
|||||||
}
|
}
|
||||||
|
|
||||||
if params.OrderBy == "desc" || params.OrderBy == "" {
|
if params.OrderBy == "desc" || params.OrderBy == "" {
|
||||||
db = db.Order(fmt.Sprintf("kandang_groups.%s DESC", params.SortBy))
|
db = db.Order(fmt.Sprintf("kandang_groups.%s DESC, kandang_groups.id ASC", params.SortBy))
|
||||||
} else {
|
} else {
|
||||||
db = db.Order(fmt.Sprintf("kandang_groups.%s ASC", params.SortBy))
|
db = db.Order(fmt.Sprintf("kandang_groups.%s ASC, kandang_groups.id ASC", params.SortBy))
|
||||||
}
|
}
|
||||||
|
|
||||||
return db
|
return db
|
||||||
|
|||||||
@@ -20,6 +20,6 @@ type Query struct {
|
|||||||
Search string `query:"search" validate:"omitempty,max=50"`
|
Search string `query:"search" validate:"omitempty,max=50"`
|
||||||
LocationId int `query:"location_id" validate:"omitempty,number,gt=0"`
|
LocationId int `query:"location_id" validate:"omitempty,number,gt=0"`
|
||||||
PicId int `query:"pic_id" validate:"omitempty,number,gt=0"`
|
PicId int `query:"pic_id" validate:"omitempty,number,gt=0"`
|
||||||
SortBy string `query:"sort_by" validate:"omitempty,max=50,oneof=name created_at updated_at" default:"updated_at"`
|
SortBy string `query:"sort_by" validate:"omitempty,max=50,oneof=name created_at updated_at" default:"name"`
|
||||||
OrderBy string `query:"order_by" validate:"omitempty,oneof=asc desc" default:"desc"`
|
OrderBy string `query:"order_by" validate:"omitempty,oneof=asc desc" default:"asc"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -131,23 +131,10 @@ type RecordingDepletionDTO struct {
|
|||||||
ProductWarehouse productWarehouseDTO.ProductWarehouseDTO `json:"product_warehouse"`
|
ProductWarehouse productWarehouseDTO.ProductWarehouseDTO `json:"product_warehouse"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type RecordingStockAllocDTO struct {
|
|
||||||
SourceType string `json:"source_type"`
|
|
||||||
SourceId uint `json:"source_id"`
|
|
||||||
PrNumber string `json:"pr_number"`
|
|
||||||
PoNumber string `json:"po_number"`
|
|
||||||
AdjNumber string `json:"adj_number"`
|
|
||||||
Qty float64 `json:"qty"`
|
|
||||||
UnitPrice float64 `json:"unit_price"`
|
|
||||||
Subtotal float64 `json:"subtotal"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type RecordingStockDTO struct {
|
type RecordingStockDTO struct {
|
||||||
ProductWarehouseId uint `json:"product_warehouse_id"`
|
ProductWarehouseId uint `json:"product_warehouse_id"`
|
||||||
UsageAmount float64 `json:"usage_amount"`
|
UsageAmount float64 `json:"usage_amount"`
|
||||||
PendingQty float64 `json:"pending_qty"`
|
PendingQty float64 `json:"pending_qty"`
|
||||||
TotalPrice float64 `json:"total_price"`
|
|
||||||
Allocations []RecordingStockAllocDTO `json:"allocations"`
|
|
||||||
ProductWarehouse productWarehouseDTO.ProductWarehouseDTO `json:"product_warehouse"`
|
ProductWarehouse productWarehouseDTO.ProductWarehouseDTO `json:"product_warehouse"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -210,26 +197,10 @@ func ToRecordingStockDTOs(stocks []entity.RecordingStock) []RecordingStockDTO {
|
|||||||
pendingQty = *s.PendingQty
|
pendingQty = *s.PendingQty
|
||||||
}
|
}
|
||||||
|
|
||||||
allocs := make([]RecordingStockAllocDTO, len(s.Allocations))
|
|
||||||
for j, a := range s.Allocations {
|
|
||||||
allocs[j] = RecordingStockAllocDTO{
|
|
||||||
SourceType: a.SourceType,
|
|
||||||
SourceId: a.SourceId,
|
|
||||||
PrNumber: a.PrNumber,
|
|
||||||
PoNumber: a.PoNumber,
|
|
||||||
AdjNumber: a.AdjNumber,
|
|
||||||
Qty: a.Qty,
|
|
||||||
UnitPrice: a.UnitPrice,
|
|
||||||
Subtotal: a.Subtotal,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
result[i] = RecordingStockDTO{
|
result[i] = RecordingStockDTO{
|
||||||
ProductWarehouseId: s.ProductWarehouseId,
|
ProductWarehouseId: s.ProductWarehouseId,
|
||||||
UsageAmount: usageAmount,
|
UsageAmount: usageAmount,
|
||||||
PendingQty: pendingQty,
|
PendingQty: pendingQty,
|
||||||
TotalPrice: s.TotalPrice,
|
|
||||||
Allocations: allocs,
|
|
||||||
ProductWarehouse: mapProductWarehouseDTO(&s.ProductWarehouse),
|
ProductWarehouse: mapProductWarehouseDTO(&s.ProductWarehouse),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,7 +80,6 @@ type RecordingRepository interface {
|
|||||||
ResyncProjectFlockPopulationUsage(ctx context.Context, tx *gorm.DB, projectFlockKandangID uint) error
|
ResyncProjectFlockPopulationUsage(ctx context.Context, tx *gorm.DB, projectFlockKandangID uint) error
|
||||||
ValidateProductWarehousesByFlags(ctx context.Context, ids []uint, flags []string) (uint, error)
|
ValidateProductWarehousesByFlags(ctx context.Context, ids []uint, flags []string) (uint, error)
|
||||||
GetProgressRows(ctx context.Context, startDate, endDate time.Time, allowedLocationIDs []uint, restrict bool) ([]exportprogress.Row, error)
|
GetProgressRows(ctx context.Context, startDate, endDate time.Time, allowedLocationIDs []uint, restrict bool) ([]exportprogress.Row, error)
|
||||||
GetStockAllocationsByIDs(ctx context.Context, stockIDs []uint) (map[uint][]entity.RecordingStockAlloc, error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type RecordingRepositoryImpl struct {
|
type RecordingRepositoryImpl struct {
|
||||||
@@ -1232,71 +1231,3 @@ func (r *RecordingRepositoryImpl) GetTotalWeightProducedFromUniformityByProjectF
|
|||||||
|
|
||||||
return result.TotalWeight, err
|
return result.TotalWeight, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RecordingRepositoryImpl) GetStockAllocationsByIDs(ctx context.Context, stockIDs []uint) (map[uint][]entity.RecordingStockAlloc, error) {
|
|
||||||
if len(stockIDs) == 0 {
|
|
||||||
return map[uint][]entity.RecordingStockAlloc{}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type row struct {
|
|
||||||
RecordingStockId uint
|
|
||||||
SourceType string
|
|
||||||
SourceId uint
|
|
||||||
PrNumber string
|
|
||||||
PoNumber string
|
|
||||||
AdjNumber string
|
|
||||||
Qty float64
|
|
||||||
UnitPrice float64
|
|
||||||
Subtotal float64
|
|
||||||
}
|
|
||||||
|
|
||||||
var rows []row
|
|
||||||
err := r.DB().WithContext(ctx).Raw(`
|
|
||||||
SELECT
|
|
||||||
sa.usable_id AS recording_stock_id,
|
|
||||||
sa.stockable_type AS source_type,
|
|
||||||
sa.stockable_id AS source_id,
|
|
||||||
COALESCE(p.pr_number, '') AS pr_number,
|
|
||||||
COALESCE(p.po_number, '') AS po_number,
|
|
||||||
COALESCE(ast.adj_number, '') AS adj_number,
|
|
||||||
sa.qty AS qty,
|
|
||||||
COALESCE(CASE
|
|
||||||
WHEN sa.stockable_type = 'PURCHASE_ITEMS' THEN pi.price
|
|
||||||
WHEN sa.stockable_type = 'ADJUSTMENT_IN' THEN ast.price
|
|
||||||
END, 0) AS unit_price,
|
|
||||||
sa.qty * COALESCE(CASE
|
|
||||||
WHEN sa.stockable_type = 'PURCHASE_ITEMS' THEN pi.price
|
|
||||||
WHEN sa.stockable_type = 'ADJUSTMENT_IN' THEN ast.price
|
|
||||||
END, 0) AS subtotal
|
|
||||||
FROM stock_allocations sa
|
|
||||||
LEFT JOIN purchase_items pi
|
|
||||||
ON pi.id = sa.stockable_id AND sa.stockable_type = 'PURCHASE_ITEMS'
|
|
||||||
LEFT JOIN purchases p
|
|
||||||
ON p.id = pi.purchase_id
|
|
||||||
LEFT JOIN adjustment_stocks ast
|
|
||||||
ON ast.id = sa.stockable_id AND sa.stockable_type = 'ADJUSTMENT_IN'
|
|
||||||
WHERE sa.usable_type = 'RECORDING_STOCK'
|
|
||||||
AND sa.usable_id IN ?
|
|
||||||
AND sa.status = 'ACTIVE'
|
|
||||||
AND sa.allocation_purpose = 'CONSUME'
|
|
||||||
ORDER BY sa.usable_id, sa.id
|
|
||||||
`, stockIDs).Scan(&rows).Error
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
result := make(map[uint][]entity.RecordingStockAlloc)
|
|
||||||
for _, row := range rows {
|
|
||||||
result[row.RecordingStockId] = append(result[row.RecordingStockId], entity.RecordingStockAlloc{
|
|
||||||
SourceType: row.SourceType,
|
|
||||||
SourceId: row.SourceId,
|
|
||||||
PrNumber: row.PrNumber,
|
|
||||||
PoNumber: row.PoNumber,
|
|
||||||
AdjNumber: row.AdjNumber,
|
|
||||||
Qty: row.Qty,
|
|
||||||
UnitPrice: row.UnitPrice,
|
|
||||||
Subtotal: row.Subtotal,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -278,26 +278,6 @@ func (s recordingService) GetOne(c *fiber.Ctx, id uint) (*entity.Recording, erro
|
|||||||
s.Log.Errorf("Failed get recording by id: %+v", err)
|
s.Log.Errorf("Failed get recording by id: %+v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(recording.Stocks) > 0 {
|
|
||||||
stockIDs := make([]uint, len(recording.Stocks))
|
|
||||||
for i, s := range recording.Stocks {
|
|
||||||
stockIDs[i] = s.Id
|
|
||||||
}
|
|
||||||
if allocMap, err := s.Repository.GetStockAllocationsByIDs(c.Context(), stockIDs); err != nil {
|
|
||||||
s.Log.Warnf("Failed to get stock allocations for recording %d: %+v", id, err)
|
|
||||||
} else {
|
|
||||||
for i := range recording.Stocks {
|
|
||||||
allocs := allocMap[recording.Stocks[i].Id]
|
|
||||||
recording.Stocks[i].Allocations = allocs
|
|
||||||
var total float64
|
|
||||||
for _, a := range allocs {
|
|
||||||
total += a.Subtotal
|
|
||||||
}
|
|
||||||
recording.Stocks[i].TotalPrice = total
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := recordingutil.AttachLatestApproval(c.Context(), recording, s.ApprovalSvc, s.Log); err != nil {
|
if err := recordingutil.AttachLatestApproval(c.Context(), recording, s.ApprovalSvc, s.Log); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user