aboutsummaryrefslogtreecommitdiffstats
path: root/core.go
blob: 5d76f7193d2edde115d06c107c27f8e073ac2613 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main

import (
	"time"
)

type OfferPrice struct {
	Currency string
	MinQty   uint32
	Price    float32
}

type Offer struct {
	Distributor, Sku, Url, Comment string
	Prices                         []OfferPrice
}

type LineItem struct {
	Mfg, Mpn, Description, Comment, Tag string
	Elements                            []string // TODO: add "circuit element" type
	Offers                              []Offer
}

func (li *LineItem) Id() string {
	return li.Mfg + "::" + li.Mpn
}

// The main anchor of a BOM as a cohesive whole, with a name and permissions.
// Multiple BOMs are associated with a single BomStub; the currently active one
// is the 'head'.
type BomStub struct {
	Name                       string
	Owner                      string
	Description                string
	HeadVersion                string
	Homepage                   *Url
	IsPublicView, IsPublicEdit bool
}

// An actual list of parts/elements. Intended to be immutable once persisted. 
type Bom struct {
	Version   string
	Created   time.Time // TODO: unix timestamp?
	Progeny   string    // where did this BOM come from?
	LineItems []LineItem
}

func NewBom(version string) *Bom {
	return &Bom{Version: version, Created: time.Now()}
}

func (b *Bom) GetLineItem(mfg, mpn string) *LineItem {
	for _, li := range b.LineItems {
		if li.Mfg == mfg && li.Mpn == mpn {
			return &li
		}
	}
	return nil
}

func (b *Bom) AddLineItem(li *LineItem) error {
	if eli := b.GetLineItem(li.Mfg, li.Mpn); eli != nil {
		return Error("This BOM already had an identical LineItem")
	}
	b.LineItems = append(b.LineItems, *li)
	return nil
}

// ---------- testing
func makeTestBom() *Bom {
	op1 := OfferPrice{Currency: "usd", Price: 1.0, MinQty: 1}
	op2 := OfferPrice{Currency: "usd", Price: 0.8, MinQty: 100}
	o := Offer{Sku: "A123", Distributor: "Acme", Prices: []OfferPrice{op1, op2}}
	//o.AddOfferPrice(op1)
	//o.AddOfferPrice(op2)
	li := LineItem{Mfg: "WidgetCo",
		Mpn:      "WIDG0001",
		Elements: []string{"W1", "W2"},
		Offers:   []Offer{o}}
	//li.AddOffer(o)
	b := NewBom("test01")
	b.AddLineItem(&li)
	return b
}