2025-04-26 14:04:51 +07:00

172 lines
4.0 KiB
Go

package entity
import (
"enaklo-pos-be/internal/constants/product"
"time"
)
type Product struct {
ID int64 `gorm:"primaryKey;autoIncrement;column:id"`
PartnerID int64 `gorm:"type:int;column:partner_id"`
Name string `gorm:"type:varchar(255);not null;column:name"`
Type string `gorm:"type:varchar;column:type"`
Price float64 `gorm:"type:decimal;column:price"`
Status string `gorm:"type:varchar;column:status"`
Description string `gorm:"type:varchar(255);not null;column:description"`
CreatedAt time.Time `gorm:"autoCreateTime;column:created_at"`
UpdatedAt time.Time `gorm:"autoUpdateTime;column:updated_at"`
DeletedAt *time.Time `gorm:"column:deleted_at"`
CreatedBy int64 `gorm:"type:int;column:created_by"`
UpdatedBy int64 `gorm:"type:int;column:updated_by"`
Image string `gorm:"type:varchar;column:image"`
}
func (Product) TableName() string {
return "products"
}
type ProductSearch struct {
Search string
Name string
Type product.ProductType
BranchID int64
PartnerID int64
Available product.ProductStock
Limit int
Offset int
}
type ProductPOS struct {
PartnerID int64
SiteID int64
}
type ProductList []*ProductDB
type ProductDB struct {
Product
}
func (b *Product) ToProductDB() *ProductDB {
return &ProductDB{
Product: *b,
}
}
func (ProductDB) TableName() string {
return "products"
}
func (e *ProductDB) ToProduct() *Product {
return &Product{
ID: e.ID,
Name: e.Name,
Type: e.Type,
Price: e.Price,
Status: e.Status,
Description: e.Description,
PartnerID: e.PartnerID,
CreatedAt: e.CreatedAt,
UpdatedAt: e.UpdatedAt,
DeletedAt: e.DeletedAt,
CreatedBy: e.CreatedBy,
UpdatedBy: e.UpdatedBy,
Image: e.Image,
}
}
func (b *ProductList) ToProductList() []*Product {
var Products []*Product
for _, p := range *b {
Products = append(Products, p.ToProduct())
}
return Products
}
func (b *ProductList) ToProductListPOS() []*Product {
var Products []*Product
for _, p := range *b {
Products = append(Products, p.ToProduct())
}
return Products
}
func (o *ProductDB) ToUpdatedProduct(updatedby int64, req Product) {
o.UpdatedBy = updatedby
if req.Name != "" {
o.Name = req.Name
}
if req.Image != "" {
o.Image = req.Image
}
if req.Type != "" {
o.Type = req.Type
}
if req.Price > 0 {
o.Price = req.Price
}
if req.Status != "" {
o.Status = req.Status
}
if req.Description != "" {
o.Description = req.Description
}
}
func (o *ProductDB) SetDeleted(updatedby int64) {
currentTime := time.Now()
o.DeletedAt = &currentTime
o.UpdatedBy = updatedby
}
type ProductDetails struct {
Products map[int64]*Product // Map for quick lookups by ID
PartnerID int64 // Common site ID for all products
}
type PaymentMethodBreakdown struct {
PaymentType string `json:"payment_type"`
PaymentProvider string `json:"payment_provider"`
TotalTransactions int64 `json:"total_transactions"`
TotalAmount float64 `json:"total_amount"`
}
type OrderPaymentAnalysis struct {
TotalTransactions int64 `json:"total"`
TotalAmount float64 `json:"total_amount"`
PaymentMethodBreakdown []PaymentMethodBreakdown `json:"payment_method_breakdown"`
}
type RevenueOverviewItem struct {
Period string `json:"period"`
TotalAmount float64 `json:"total_amount"`
OrderCount int64 `json:"order_count"`
}
type SalesByCategoryItem struct {
Category string `json:"category"`
TotalAmount float64 `json:"total_amount"`
TotalQuantity int64 `json:"total_quantity"`
Percentage float64 `json:"percentage"`
}
type PopularProductItem struct {
ProductID int64 `json:"product_id"`
ProductName string `json:"product_name"`
Category string `json:"category"`
TotalSales int64 `json:"total_sales"`
TotalRevenue float64 `json:"total_revenue"`
AveragePrice float64 `json:"average_price"`
Percentage float64 `json:"percentage"`
}