41 lines
1.3 KiB
Go
41 lines
1.3 KiB
Go
package seeding
|
|
|
|
import (
|
|
"wagfarm-api/assets/seed"
|
|
"wagfarm-api/database"
|
|
"wagfarm-api/logs/base"
|
|
"wagfarm-api/taxonomy/seedingcondition"
|
|
"wagfarm-api/taxonomy/seedingtechnique"
|
|
|
|
"github.com/shopspring/decimal"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type SeedingLog struct {
|
|
base.FieldWorkLog
|
|
SeedID uint `gorm:"not null"`
|
|
Seed seed.Seed `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
|
|
TechniqueID uint `gorm:"not null"`
|
|
Technique seedingtechnique.SeedingTechnique `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
|
|
Amount float64 `gorm:"not null"` // in units/ha
|
|
ConditionsID uint `gorm:"not null"`
|
|
Conditions seedingcondition.SeedingCondition `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
|
|
}
|
|
|
|
func (l *SeedingLog) AfterSave(db *gorm.DB) (err error) {
|
|
// Example: compute cost as Amount * UnitPrice
|
|
if l.SeedID == 0 {
|
|
if err := db.First(&l.Seed, l.SeedID).Error; err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
unitPrice := l.Seed.Cost
|
|
totalCost := unitPrice.Mul(decimal.NewFromFloat(l.Amount))
|
|
l.UpdateCost(db, totalCost)
|
|
|
|
return
|
|
}
|
|
|
|
func RegisterModels() { database.RegisterModel(&SeedingLog{}) }
|