mirror of
https://github.com/hanzoai/commerce.git
synced 2026-10-30 15:08:42 +00:00
Multi-tenant commerce and billing: orders, subscriptions, invoices, plans, usage metering and a credit ledger. One Go binary. MIT OR Apache-2.0. Claude-Session: https://claude.ai/code/session_01SAtaDFm8VDiWrhYHNNtxMw
69 lines
2.0 KiB
Go
69 lines
2.0 KiB
Go
package balancetransaction
|
|
|
|
import (
|
|
"github.com/hanzoai/commerce/datastore"
|
|
"github.com/hanzoai/commerce/models/mixin"
|
|
"github.com/hanzoai/commerce/models/types/currency"
|
|
"github.com/hanzoai/commerce/util/json"
|
|
"github.com/hanzoai/commerce/util/val"
|
|
"github.com/hanzoai/orm"
|
|
|
|
. "github.com/hanzoai/commerce/types"
|
|
)
|
|
|
|
func init() { orm.Register[BalanceTransaction]("balance-transaction") }
|
|
|
|
// BalanceTransaction records a single change to a customer's balance.
|
|
// Positive amount = credit (adds to balance), negative = debit.
|
|
type BalanceTransaction struct {
|
|
mixin.Model[BalanceTransaction]
|
|
|
|
CustomerId string `json:"customerId"`
|
|
Amount int64 `json:"amount"` // positive = credit, negative = debit
|
|
Currency currency.Type `json:"currency" orm:"default:usd"`
|
|
|
|
// "adjustment" | "credit_note" | "invoice_payment" | "deposit" | "bank_transfer" | "refund"
|
|
Type string `json:"type"`
|
|
|
|
Description string `json:"description,omitempty"`
|
|
InvoiceId string `json:"invoiceId,omitempty"`
|
|
CreditNoteId string `json:"creditNoteId,omitempty"`
|
|
SourceRef string `json:"sourceRef,omitempty"` // external reference
|
|
EndingBalance int64 `json:"endingBalance"` // balance after this transaction
|
|
|
|
Metadata Map `json:"metadata,omitempty" datastore:"-"`
|
|
Metadata_ string `json:"-" datastore:",noindex"`
|
|
}
|
|
|
|
func (bt *BalanceTransaction) Load(ps []datastore.Property) (err error) {
|
|
if err = datastore.LoadStruct(bt, ps); err != nil {
|
|
return err
|
|
}
|
|
|
|
if len(bt.Metadata_) > 0 {
|
|
err = json.DecodeBytes([]byte(bt.Metadata_), &bt.Metadata)
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
func (bt *BalanceTransaction) Save() (ps []datastore.Property, err error) {
|
|
bt.Metadata_ = string(json.EncodeBytes(&bt.Metadata))
|
|
return datastore.SaveStruct(bt)
|
|
}
|
|
|
|
func (bt *BalanceTransaction) Validator() *val.Validator {
|
|
return nil
|
|
}
|
|
|
|
func New(db *datastore.Datastore) *BalanceTransaction {
|
|
bt := new(BalanceTransaction)
|
|
bt.Init(db)
|
|
bt.Parent = db.NewKey("synckey", "", 1, nil)
|
|
return bt
|
|
}
|
|
|
|
func Query(db *datastore.Datastore) datastore.Query {
|
|
return db.Query("balance-transaction")
|
|
}
|