mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-22 06:15:44 +00:00
feat(BE): fix delete project flock budget and uniformity, and fix uniformity with update purchase document
This commit is contained in:
@@ -22,6 +22,7 @@ import (
|
||||
pfutils "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/utils"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/validations"
|
||||
recordingRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/repositories"
|
||||
uniformityRepository "gitlab.com/mbugroup/lti-api.git/internal/modules/production/uniformities/repositories"
|
||||
utils "gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
approvalutils "gitlab.com/mbugroup/lti-api.git/internal/utils/approvals"
|
||||
|
||||
@@ -866,6 +867,14 @@ func (s projectflockService) detachKandangs(ctx context.Context, dbTransaction *
|
||||
}
|
||||
|
||||
if len(pfkIDs) > 0 {
|
||||
uniformityRepo := uniformityRepository.NewUniformityRepository(s.Repository.DB())
|
||||
if dbTransaction != nil {
|
||||
uniformityRepo = uniformityRepository.NewUniformityRepository(dbTransaction)
|
||||
}
|
||||
if err := uniformityRepo.DeleteByProjectFlockKandangIDs(ctx, pfkIDs); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to remove uniformity data for project flock kandang")
|
||||
}
|
||||
|
||||
pwRepo := s.ProductWarehouseRepo
|
||||
if dbTransaction != nil {
|
||||
pwRepo = productWarehouseRepository.NewProductWarehouseRepository(dbTransaction)
|
||||
|
||||
@@ -333,13 +333,6 @@ func (s recordingService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uin
|
||||
s.Log.Errorf("Failed to list existing stocks: %+v", err)
|
||||
return err
|
||||
}
|
||||
if s.Log != nil && s.Log.IsLevelEnabled(logrus.DebugLevel) {
|
||||
s.Log.WithFields(logrus.Fields{
|
||||
"recording_id": recordingEntity.Id,
|
||||
"existing": summarizeExistingStocks(existingStocks),
|
||||
"incoming": summarizeIncomingStocks(req.Stocks),
|
||||
}).Debug("recording update stock comparison")
|
||||
}
|
||||
if stocksMatch(existingStocks, req.Stocks) {
|
||||
hasStockChanges = false
|
||||
}
|
||||
@@ -698,16 +691,6 @@ func (s *recordingService) consumeRecordingStocks(ctx context.Context, tx *gorm.
|
||||
}
|
||||
desiredTotal := desired + pending
|
||||
|
||||
if s.Log != nil && s.Log.IsLevelEnabled(logrus.DebugLevel) {
|
||||
s.Log.WithFields(logrus.Fields{
|
||||
"recording_stock_id": stock.Id,
|
||||
"product_warehouse_id": stock.ProductWarehouseId,
|
||||
"desired_usage_qty": desired,
|
||||
"desired_pending_qty": pending,
|
||||
"desired_total_qty": desiredTotal,
|
||||
}).Debug("recording fifo consume start")
|
||||
}
|
||||
|
||||
result, err := s.FifoSvc.Consume(ctx, commonSvc.StockConsumeRequest{
|
||||
UsableKey: recordingStockUsableKey,
|
||||
UsableID: stock.Id,
|
||||
@@ -721,17 +704,6 @@ func (s *recordingService) consumeRecordingStocks(ctx context.Context, tx *gorm.
|
||||
return err
|
||||
}
|
||||
|
||||
if s.Log != nil && s.Log.IsLevelEnabled(logrus.DebugLevel) {
|
||||
s.Log.WithFields(logrus.Fields{
|
||||
"recording_stock_id": stock.Id,
|
||||
"product_warehouse_id": stock.ProductWarehouseId,
|
||||
"result_usage_qty": result.UsageQuantity,
|
||||
"result_pending_qty": result.PendingQuantity,
|
||||
"released_qty": result.ReleasedQuantity,
|
||||
"added_allocations": len(result.AddedAllocations),
|
||||
}).Debug("recording fifo consume result")
|
||||
}
|
||||
|
||||
if err := s.Repository.UpdateStockUsage(tx, stock.Id, result.UsageQuantity, result.PendingQuantity); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -754,23 +726,6 @@ func (s *recordingService) releaseRecordingStocks(ctx context.Context, tx *gorm.
|
||||
continue
|
||||
}
|
||||
|
||||
var usage float64
|
||||
var pending float64
|
||||
if stock.UsageQty != nil {
|
||||
usage = *stock.UsageQty
|
||||
}
|
||||
if stock.PendingQty != nil {
|
||||
pending = *stock.PendingQty
|
||||
}
|
||||
if s.Log != nil && s.Log.IsLevelEnabled(logrus.DebugLevel) {
|
||||
s.Log.WithFields(logrus.Fields{
|
||||
"recording_stock_id": stock.Id,
|
||||
"product_warehouse_id": stock.ProductWarehouseId,
|
||||
"current_usage_qty": usage,
|
||||
"current_pending_qty": pending,
|
||||
}).Debug("recording fifo release start")
|
||||
}
|
||||
|
||||
if err := s.FifoSvc.ReleaseUsage(ctx, commonSvc.StockReleaseRequest{
|
||||
UsableKey: recordingStockUsableKey,
|
||||
UsableID: stock.Id,
|
||||
|
||||
@@ -74,7 +74,6 @@ type UniformityListDTO struct {
|
||||
MeanDown float64 `json:"mean_down"`
|
||||
StandardMeanWeight *float64 `json:"standard_mean_weight"`
|
||||
StandardUniformity *float64 `json:"standard_uniformity"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
CreatedBy uint `json:"created_by"`
|
||||
LatestApproval *approvalDTO.ApprovalRelationDTO `json:"latest_approval"`
|
||||
}
|
||||
@@ -154,7 +153,6 @@ func ToUniformityListDTOs(items []entity.ProjectFlockKandangUniformity) []Unifor
|
||||
UniformQty: item.UniformQty,
|
||||
MeanUp: item.MeanUp,
|
||||
MeanDown: item.MeanDown,
|
||||
CreatedAt: item.CreatedAt,
|
||||
CreatedBy: item.CreatedBy,
|
||||
LatestApproval: latestApproval,
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
"gorm.io/gorm"
|
||||
@@ -8,6 +10,7 @@ import (
|
||||
|
||||
type UniformityRepository interface {
|
||||
repository.BaseRepository[entity.ProjectFlockKandangUniformity]
|
||||
DeleteByProjectFlockKandangIDs(ctx context.Context, projectFlockKandangIDs []uint) error
|
||||
}
|
||||
|
||||
type UniformityRepositoryImpl struct {
|
||||
@@ -19,3 +22,13 @@ func NewUniformityRepository(db *gorm.DB) UniformityRepository {
|
||||
BaseRepositoryImpl: repository.NewBaseRepository[entity.ProjectFlockKandangUniformity](db),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *UniformityRepositoryImpl) DeleteByProjectFlockKandangIDs(ctx context.Context, projectFlockKandangIDs []uint) error {
|
||||
if len(projectFlockKandangIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
return r.DB().WithContext(ctx).
|
||||
Unscoped().
|
||||
Where("project_flock_kandang_id IN ?", projectFlockKandangIDs).
|
||||
Delete(&entity.ProjectFlockKandangUniformity{}).Error
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ func (s uniformityService) GetAll(c *fiber.Ctx, params *validation.Query) ([]ent
|
||||
if params.Week != 0 {
|
||||
db = db.Where("week = ?", params.Week)
|
||||
}
|
||||
return db.Order("uniform_date DESC").Order("created_at DESC")
|
||||
return db.Order("uniform_date DESC").Order("id DESC")
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
|
||||
@@ -180,7 +180,10 @@ func (ctrl *PurchaseController) ReceiveProducts(c *fiber.Ctx) error {
|
||||
req.Items = []validation.ReceivePurchaseItemRequest{singleItem}
|
||||
}
|
||||
}
|
||||
req.TravelDocuments = form.File["documents"]
|
||||
req.TravelDocuments = form.File["travel_documents"]
|
||||
if len(req.TravelDocuments) == 0 {
|
||||
req.TravelDocuments = form.File["documents"]
|
||||
}
|
||||
result, err := ctrl.service.ReceiveProducts(c, uint(id), req)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -999,6 +999,22 @@ func (s *purchaseService) uploadTravelDocument(
|
||||
return "", errors.New("document service not available")
|
||||
}
|
||||
|
||||
documents, err := s.DocumentSvc.ListByTarget(ctx, string(utils.DocumentableTypePurchaseItem), uint64(itemID))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(documents) > 0 {
|
||||
var ids []uint
|
||||
for _, doc := range documents {
|
||||
if doc.Type == string(utils.DocumentTypePurchaseTravel) {
|
||||
ids = append(ids, doc.Id)
|
||||
}
|
||||
}
|
||||
if err := s.DocumentSvc.DeleteDocuments(ctx, ids, true); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
documentFiles := []commonSvc.DocumentFile{{
|
||||
File: file,
|
||||
Type: string(utils.DocumentTypePurchaseTravel),
|
||||
@@ -1015,7 +1031,7 @@ func (s *purchaseService) uploadTravelDocument(
|
||||
if len(results) == 0 {
|
||||
return "", errors.New("upload result is empty")
|
||||
}
|
||||
return results[0].URL, nil
|
||||
return results[0].Document.Path, nil
|
||||
}
|
||||
|
||||
func (s *purchaseService) DeleteItems(c *fiber.Ctx, id uint, req *validation.DeletePurchaseItemsRequest) (*entity.Purchase, error) {
|
||||
@@ -1499,10 +1515,56 @@ func (s *purchaseService) loadPurchase(
|
||||
if err := s.attachLatestApproval(ctx, purchase); err != nil {
|
||||
s.Log.Warnf("Unable to attach latest approval for purchase %d: %+v", id, err)
|
||||
}
|
||||
s.applyTravelDocumentURLs(ctx, purchase)
|
||||
|
||||
return purchase, nil
|
||||
}
|
||||
|
||||
func (s *purchaseService) applyTravelDocumentURLs(ctx context.Context, purchase *entity.Purchase) {
|
||||
if purchase == nil || s.DocumentSvc == nil {
|
||||
return
|
||||
}
|
||||
|
||||
for i := range purchase.Items {
|
||||
item := &purchase.Items[i]
|
||||
documents, err := s.DocumentSvc.ListByTarget(ctx, string(utils.DocumentableTypePurchaseItem), uint64(item.Id))
|
||||
if err != nil {
|
||||
s.Log.Warnf("Unable to load travel documents for purchase item %d: %+v", item.Id, err)
|
||||
} else {
|
||||
var targetDoc *entity.Document
|
||||
for j := len(documents) - 1; j >= 0; j-- {
|
||||
if documents[j].Type == string(utils.DocumentTypePurchaseTravel) {
|
||||
targetDoc = &documents[j]
|
||||
break
|
||||
}
|
||||
}
|
||||
if targetDoc != nil {
|
||||
url, err := s.DocumentSvc.PresignURL(ctx, *targetDoc, 15*time.Minute)
|
||||
if err != nil {
|
||||
s.Log.Warnf("Unable to presign travel document for purchase item %d: %+v", item.Id, err)
|
||||
} else if url != "" {
|
||||
item.TravelNumberDocs = &url
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
path := item.TravelNumberDocs
|
||||
if path == nil || strings.TrimSpace(*path) == "" {
|
||||
continue
|
||||
}
|
||||
url, err := commonSvc.ResolveDocumentURL(ctx, s.DocumentSvc, *path, 15*time.Minute)
|
||||
if err != nil {
|
||||
s.Log.Warnf("Unable to presign travel document for purchase item %d: %+v", item.Id, err)
|
||||
continue
|
||||
}
|
||||
if url == "" {
|
||||
continue
|
||||
}
|
||||
item.TravelNumberDocs = &url
|
||||
}
|
||||
}
|
||||
|
||||
func collectPFKIDsFromPurchase(p *entity.Purchase) []uint {
|
||||
seen := make(map[uint]struct{})
|
||||
ids := make([]uint, 0)
|
||||
|
||||
Reference in New Issue
Block a user