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
@@ -48,6 +48,26 @@ func (r *CategoryRepositoryImpl) GetByOrganization(ctx context.Context, organiza
return categories, err
}
// ListParentCategories returns the top-level categories of an organization. These are
// the buckets the parent category reports roll up to via COALESCE(parent_id, id), so
// the list is deliberately every top-level category, not only those with children —
// otherwise a team could show up in a report but not be selectable on a purchase.
// Categories with no outlet of their own are shared, so they are always included.
func (r *CategoryRepositoryImpl) ListParentCategories(ctx context.Context, organizationID uuid.UUID, outletID *uuid.UUID) ([]*entities.Category, error) {
var categories []*entities.Category
query := r.db.WithContext(ctx).
Where("organization_id = ?", organizationID).
Where("parent_id IS NULL")
if outletID != nil {
query = query.Where("outlet_id = ? OR outlet_id IS NULL", *outletID)
}
err := query.Order("\"order\" ASC, name ASC").Find(&categories).Error
return categories, err
}
func (r *CategoryRepositoryImpl) GetByBusinessType(ctx context.Context, businessType string) ([]*entities.Category, error) {
var categories []*entities.Category
err := r.db.WithContext(ctx).Where("business_type = ?", businessType).Find(&categories).Error
@@ -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").