feat(purchasae): added team with category parent and central

This commit is contained in:
efrilm
2026-08-11 21:00:39 +07:00
parent a230199ce0
commit 9ae5be2c33
18 changed files with 566 additions and 32 deletions
@@ -10,6 +10,7 @@ import (
"apskel-pos-be/internal/entities"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type PurchaseOrderRepositoryImpl struct {
@@ -30,6 +31,7 @@ func (r *PurchaseOrderRepositoryImpl) GetByID(ctx context.Context, id uuid.UUID)
var po entities.PurchaseOrder
err := r.db.WithContext(ctx).
Preload("Vendor").
Preload("TeamCategory").
Preload("Items.Ingredient").
Preload("Items.PurchaseCategory").
Preload("Items.Unit").
@@ -45,6 +47,7 @@ func (r *PurchaseOrderRepositoryImpl) GetByIDAndOrganizationID(ctx context.Conte
var po entities.PurchaseOrder
err := r.db.WithContext(ctx).
Preload("Vendor").
Preload("TeamCategory").
Preload("Items.Ingredient").
Preload("Items.PurchaseCategory").
Preload("Items.Unit").
@@ -58,7 +61,10 @@ func (r *PurchaseOrderRepositoryImpl) GetByIDAndOrganizationID(ctx context.Conte
}
func (r *PurchaseOrderRepositoryImpl) Update(ctx context.Context, po *entities.PurchaseOrder) error {
return r.db.WithContext(ctx).Save(po).Error
// Omit associations so preloaded relations are not upserted back. Items and
// attachments are rewritten explicitly by the processor, and without this a
// preloaded TeamCategory would be written over the category row itself.
return r.db.WithContext(ctx).Omit(clause.Associations).Save(po).Error
}
func (r *PurchaseOrderRepositoryImpl) Delete(ctx context.Context, id uuid.UUID) error {
@@ -87,6 +93,14 @@ func (r *PurchaseOrderRepositoryImpl) List(ctx context.Context, organizationID u
if vendorID, ok := value.(uuid.UUID); ok {
query = query.Where("vendor_id = ?", vendorID)
}
case "team_scope":
if teamScope, ok := value.(string); ok && teamScope != "" {
query = query.Where("team_scope = ?", teamScope)
}
case "team_category_id":
if teamCategoryID, ok := value.(uuid.UUID); ok {
query = query.Where("team_category_id = ?", teamCategoryID)
}
case "start_date":
if startDate, ok := value.(time.Time); ok {
query = query.Where("transaction_date >= ?", startDate)
@@ -106,6 +120,7 @@ func (r *PurchaseOrderRepositoryImpl) List(ctx context.Context, organizationID u
err := query.
Preload("Vendor").
Preload("TeamCategory").
Preload("Items.Ingredient").
Preload("Items.PurchaseCategory").
Preload("Items.Unit").
@@ -137,6 +152,14 @@ func (r *PurchaseOrderRepositoryImpl) Count(ctx context.Context, organizationID
if vendorID, ok := value.(uuid.UUID); ok {
query = query.Where("vendor_id = ?", vendorID)
}
case "team_scope":
if teamScope, ok := value.(string); ok && teamScope != "" {
query = query.Where("team_scope = ?", teamScope)
}
case "team_category_id":
if teamCategoryID, ok := value.(uuid.UUID); ok {
query = query.Where("team_category_id = ?", teamCategoryID)
}
case "start_date":
if startDate, ok := value.(time.Time); ok {
query = query.Where("transaction_date >= ?", startDate)
@@ -170,6 +193,7 @@ func (r *PurchaseOrderRepositoryImpl) GetByStatus(ctx context.Context, organizat
err := r.db.WithContext(ctx).
Where("organization_id = ? AND status = ?", organizationID, status).
Preload("Vendor").
Preload("TeamCategory").
Preload("Items.Ingredient").
Preload("Items.PurchaseCategory").
Preload("Items.Unit").
@@ -182,6 +206,7 @@ func (r *PurchaseOrderRepositoryImpl) GetOverdue(ctx context.Context, organizati
err := r.db.WithContext(ctx).
Where("organization_id = ? AND due_date < ? AND status IN (?)", organizationID, time.Now(), []string{"draft", "sent", "approved"}).
Preload("Vendor").
Preload("TeamCategory").
Preload("Items.Ingredient").
Preload("Items.PurchaseCategory").
Preload("Items.Unit").