2025-07-18 20:10:29 +07:00
|
|
|
package entities
|
|
|
|
|
|
|
|
|
|
import (
|
2026-10-01 22:26:44 +07:00
|
|
|
"strings"
|
2025-07-18 20:10:29 +07:00
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Product struct {
|
2026-10-01 22:26:44 +07:00
|
|
|
ID uuid.UUID `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
|
|
|
|
|
OrganizationID uuid.UUID `gorm:"type:uuid;not null;index" json:"organization_id" validate:"required"`
|
|
|
|
|
CategoryID uuid.UUID `gorm:"type:uuid;not null;index" json:"category_id" validate:"required"`
|
|
|
|
|
SKU *string `gorm:"size:100;index" json:"sku"`
|
|
|
|
|
Name string `gorm:"not null;size:255" json:"name" validate:"required,min=1,max=255"`
|
|
|
|
|
Description *string `gorm:"type:text" json:"description"`
|
|
|
|
|
Price float64 `gorm:"type:decimal(10,2);not null" json:"price" validate:"required,min=0"`
|
|
|
|
|
Cost float64 `gorm:"type:decimal(10,2);default:0.00" json:"cost" validate:"min=0"`
|
|
|
|
|
BusinessType string `gorm:"size:50;default:'restaurant'" json:"business_type"`
|
|
|
|
|
ImageURL *string `gorm:"size:500" json:"image_url"`
|
|
|
|
|
// PrinterTypes are the stations the product is printed at. Set them through
|
|
|
|
|
// SetPrinterTypes.
|
|
|
|
|
PrinterTypes StringSlice `gorm:"type:jsonb;not null;default:'[\"kitchen\"]'" json:"printer_types"`
|
|
|
|
|
UnitID *uuid.UUID `gorm:"type:uuid;index" json:"unit_id"`
|
|
|
|
|
SellBy string `gorm:"size:20;default:'unit'" json:"sell_by"`
|
|
|
|
|
HasIngredients bool `gorm:"default:false" json:"has_ingredients"`
|
|
|
|
|
Metadata Metadata `gorm:"type:jsonb;default:'{}'" json:"metadata"`
|
|
|
|
|
IsActive bool `gorm:"default:true" json:"is_active"`
|
|
|
|
|
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
|
|
|
|
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
2025-07-18 20:10:29 +07:00
|
|
|
|
2026-05-28 15:30:18 +07:00
|
|
|
Organization Organization `gorm:"foreignKey:OrganizationID" json:"organization,omitempty"`
|
|
|
|
|
Category Category `gorm:"foreignKey:CategoryID" json:"category,omitempty"`
|
|
|
|
|
Unit *Unit `gorm:"foreignKey:UnitID" json:"unit,omitempty"`
|
|
|
|
|
ProductVariants []ProductVariant `gorm:"foreignKey:ProductID" json:"variants,omitempty"`
|
|
|
|
|
ProductRecipes []ProductRecipe `gorm:"foreignKey:ProductID" json:"product_recipes,omitempty"`
|
|
|
|
|
Inventory []Inventory `gorm:"foreignKey:ProductID" json:"inventory,omitempty"`
|
|
|
|
|
OrderItems []OrderItem `gorm:"foreignKey:ProductID" json:"order_items,omitempty"`
|
|
|
|
|
ProductOutletPrices []ProductOutletPrice `gorm:"foreignKey:ProductID" json:"product_outlet_prices,omitempty"`
|
2025-07-18 20:10:29 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *Product) BeforeCreate(tx *gorm.DB) error {
|
|
|
|
|
if p.ID == uuid.Nil {
|
|
|
|
|
p.ID = uuid.New()
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (Product) TableName() string {
|
|
|
|
|
return "products"
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-01 22:26:44 +07:00
|
|
|
// SetPrinterTypes records the stations the product is printed at, in order, dropping
|
|
|
|
|
// blanks and repeats.
|
|
|
|
|
func (p *Product) SetPrinterTypes(printerTypes []string) {
|
|
|
|
|
cleaned := StringSlice{}
|
|
|
|
|
seen := make(map[string]bool, len(printerTypes))
|
|
|
|
|
for _, printerType := range printerTypes {
|
|
|
|
|
printerType = strings.TrimSpace(printerType)
|
|
|
|
|
if printerType == "" || seen[printerType] {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
seen[printerType] = true
|
|
|
|
|
cleaned = append(cleaned, printerType)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p.PrinterTypes = cleaned
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetPrinterTypes returns the stations the product is printed at, never nil, so a
|
|
|
|
|
// response always carries a list. A product that was not loaded has none.
|
|
|
|
|
func (p *Product) GetPrinterTypes() []string {
|
|
|
|
|
if p.PrinterTypes == nil {
|
|
|
|
|
return []string{}
|
|
|
|
|
}
|
|
|
|
|
return []string(p.PrinterTypes)
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-18 20:10:29 +07:00
|
|
|
type ProductVariant struct {
|
|
|
|
|
ID uuid.UUID `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
|
|
|
|
|
ProductID uuid.UUID `gorm:"type:uuid;not null;index" json:"product_id" validate:"required"`
|
|
|
|
|
Name string `gorm:"not null;size:255" json:"name" validate:"required,min=1,max=255"`
|
|
|
|
|
PriceModifier float64 `gorm:"type:decimal(10,2);default:0.00" json:"price_modifier"`
|
|
|
|
|
Cost float64 `gorm:"type:decimal(10,2);default:0.00" json:"cost" validate:"min=0"`
|
|
|
|
|
Metadata Metadata `gorm:"type:jsonb;default:'{}'" json:"metadata"`
|
|
|
|
|
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
|
|
|
|
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
|
|
|
|
|
|
|
|
|
Product Product `gorm:"foreignKey:ProductID" json:"product,omitempty"`
|
|
|
|
|
OrderItems []OrderItem `gorm:"foreignKey:ProductVariantID" json:"order_items,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (pv *ProductVariant) BeforeCreate(tx *gorm.DB) error {
|
|
|
|
|
if pv.ID == uuid.Nil {
|
|
|
|
|
pv.ID = uuid.New()
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ProductVariant) TableName() string {
|
|
|
|
|
return "product_variants"
|
|
|
|
|
}
|