Files
wagfarm/wagfarm-api/logs/harvest/model.go

35 lines
829 B
Go

package harvest
import (
"wagfarm-api/assets/product"
"wagfarm-api/database"
"wagfarm-api/logs/generic"
"github.com/shopspring/decimal"
"gorm.io/gorm"
)
type HarvestLog struct {
generic.FieldWorkLog
ProductID uint `gorm:"not null"`
Product product.Product `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
Amount float64 `gorm:"not null"` // in units of the product
}
func (l *HarvestLog) AfterSave(db *gorm.DB) (err error) {
// Example: compute cost as Amount * UnitPrice
if l.ProductID == 0 {
if err := db.First(&l.Product, l.ProductID).Error; err != nil {
return err
}
}
unitPrice := l.Product.Cost
totalCost := unitPrice.Mul(decimal.NewFromFloat(l.Amount))
l.UpdateCost(db, totalCost)
return
}
func RegisterModels() { database.RegisterModel(&HarvestLog{}) }