package entities import ( "time" "github.com/google/uuid" "gorm.io/gorm" ) // CashAdvance is money handed to a team up front so it can go shopping — kasbon in // the Indonesian UI. While the cash is out it is still the outlet's, not a cost, so // nothing about what the team bought lives here: that is read back from the purchase // orders and expenses charged to the advance. type CashAdvance struct { 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"` OutletID uuid.UUID `gorm:"type:uuid;not null;index" json:"outlet_id"` CodeNumber string `gorm:"not null;size:50" json:"code_number"` // An advance is always handed to a team, so unlike a purchase order it has no // "not chosen yet" state. TeamCategoryID is set only when the scope is category. TeamScope string `gorm:"not null;size:20;index" json:"team_scope"` TeamCategoryID *uuid.UUID `gorm:"type:uuid;index" json:"team_category_id"` Amount float64 `gorm:"type:decimal(15,2);not null;default:0" json:"amount"` ReturnedAmount float64 `gorm:"type:decimal(15,2);not null;default:0" json:"returned_amount"` IssuedDate time.Time `gorm:"type:date;not null" json:"issued_date"` DueDate *time.Time `gorm:"type:date" json:"due_date"` Status string `gorm:"not null;size:20;default:'draft'" json:"status"` Description *string `gorm:"type:text" json:"description"` CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"` UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"` // SettledAmount is filled in by the read queries from the spending charged to // this advance. It has no column of its own, so it can never drift out of step // with the purchases behind it; the arrow tag keeps writes from touching it. SettledAmount float64 `gorm:"->;-:migration" json:"settled_amount"` Organization *Organization `gorm:"foreignKey:OrganizationID" json:"organization,omitempty"` Outlet *Outlet `gorm:"foreignKey:OutletID" json:"outlet,omitempty"` TeamCategory *Category `gorm:"foreignKey:TeamCategoryID" json:"team_category,omitempty"` } func (k *CashAdvance) BeforeCreate(tx *gorm.DB) error { if k.ID == uuid.Nil { k.ID = uuid.New() } return nil } func (CashAdvance) TableName() string { return "cash_advances" } // CashAdvanceSettlement is one piece of spending charged to an advance. It is read // out of purchase_orders and expenses, so it has no table of its own. type CashAdvanceSettlement struct { Type string `json:"type"` ID uuid.UUID `json:"id"` Number string `json:"number"` Date time.Time `json:"date"` Amount float64 `json:"amount"` Status string `json:"status"` }