-
Notifications
You must be signed in to change notification settings - Fork 0
/
stock.go
329 lines (274 loc) · 7.17 KB
/
stock.go
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package app
import (
"errors"
"time"
"github.com/shopspring/decimal"
)
type stockType int
// MEDICINE, FEED and ACCESSORY are the default stock types in madmin
const (
MEDICINE stockType = iota
FEED
ACCESSORY
)
// StockTyper is an interface that wraps the StockType method.
// StockType returns the type of a stock item
type StockTyper interface {
StockType() stockType
}
func (st stockType) StockType() stockType {
return st
}
// Stock is a stock item interface
type Stock interface {
ID() string
Type() stockType
Name() string
SetName(string)
IsExpirable() bool
// IsExpirable() should always be chacked before trying to call ExpirationDate()
// or SetExpitrationDate(). Trying to get or set expiration date of an unexpirable
// stock causes panic.
ExpirationDate() time.Time
SetExpirationDate(time.Time)
MinQuantity() decimal.Decimal
SetMinQuantity(decimal.Decimal)
Quantity() decimal.Decimal
SetQuantity(decimal.Decimal)
DistributorID() string
SetDistributorID(string)
Update(StockDTO) error
}
// NewStock creates a new valid Stock object.
// For now it only works for stock items of MEDICINE, FEED or ACCESSORY type.
func NewStock(dto *NewStockDTO) (Stock, error) {
switch dto.Type {
case MEDICINE:
m, err := NewMedicine(dto)
return m, err
case FEED:
f, err := NewFeed(dto)
return f, err
case ACCESSORY:
a, err := NewAccessory(dto)
return a, err
default:
return nil, errors.New("invalid stock type")
}
}
type defaultStock struct {
id string
name string
minQuantity decimal.Decimal
quantity decimal.Decimal
expirationDate time.Time
distributorID string
}
func (ds *defaultStock) ID() string {
return ds.id
}
func (ds *defaultStock) Name() string {
return ds.name
}
func (ds *defaultStock) SetName(name string) {
ds.name = name
}
func (ds *defaultStock) IsExpirable() bool {
return true
}
func (ds *defaultStock) ExpirationDate() time.Time {
return ds.expirationDate
}
func (ds *defaultStock) SetExpirationDate(expirationDate time.Time) {
ds.expirationDate = expirationDate
}
func (ds *defaultStock) MinQuantity() decimal.Decimal {
return ds.minQuantity
}
func (ds *defaultStock) SetMinQuantity(quantity decimal.Decimal) {
ds.minQuantity = quantity
}
func (ds *defaultStock) Quantity() decimal.Decimal {
return ds.quantity
}
func (ds *defaultStock) SetQuantity(quantity decimal.Decimal) {
ds.quantity = quantity
}
func (ds *defaultStock) DistributorID() string {
return ds.distributorID
}
func (ds *defaultStock) SetDistributorID(id string) {
ds.distributorID = id
}
func (ds *defaultStock) Update(dto StockDTO) error {
if ds.ID() != dto.ID {
return errors.New("trying to update stock with different id")
}
ds.SetName(dto.Name)
if ds.IsExpirable() {
date, err := validDateFromString(dto.ExpirationDate)
if err != nil {
return err
}
ds.SetExpirationDate(date)
}
quantity, err := validQuantityFromString(dto.Quantity)
if err != nil {
return err
}
ds.SetQuantity(quantity)
if dto.MinQuantity == "" {
ds.SetMinQuantity(decimal.Zero)
} else {
minQuantity, err := validQuantityFromString(dto.MinQuantity)
if err != nil {
return err
}
ds.SetMinQuantity(minQuantity)
}
ds.SetDistributorID(dto.DistributorID)
return nil
}
type medicine struct {
defaultStock
}
// NewMedicine creates a new stock item of medicine type (expirable stock item)
func NewMedicine(dto *NewStockDTO) (Stock, error) {
id, err := newUUID()
if err != nil {
return nil, err
}
ds := &defaultStock{id: id}
ds.SetName(dto.Name)
date, err := validDateFromString(dto.ExpirationDate)
if err != nil {
return nil, err
}
ds.SetExpirationDate(date)
quantity, err := validQuantityFromString(dto.Quantity)
if err != nil {
return nil, err
}
ds.SetQuantity(quantity)
if dto.MinQuantity == "" {
ds.SetMinQuantity(decimal.Zero)
} else {
minQuantity, err := validQuantityFromString(dto.MinQuantity)
if err != nil {
return nil, err
}
ds.SetMinQuantity(minQuantity)
}
ds.SetDistributorID(dto.DistributorID)
return &medicine{defaultStock: *ds}, err
}
func (m *medicine) Type() stockType {
return MEDICINE
}
type feed struct {
defaultStock
}
// NewFeed creates a new stock item of feed type (expirable stock item)
func NewFeed(dto *NewStockDTO) (Stock, error) {
id, err := newUUID()
if err != nil {
return nil, err
}
name := dto.Name
quantityString := dto.Quantity
if quantityString == "" {
return nil, errors.New("no quantity set for stock")
}
quantity, err := decimal.NewFromString(quantityString)
if err != nil {
return nil, err
}
var (
minQuantityString = dto.MinQuantity
minQuantity decimal.Decimal
)
if minQuantityString != "" {
minQuantity, err = decimal.NewFromString(minQuantityString)
if err != nil {
return nil, err
}
} else {
minQuantity = decimal.New(0, 0)
}
dateString := dto.ExpirationDate
if dateString == "" {
return nil, errors.New("no expiration date set for feed")
}
layout := "2006-01-02T15:04:05.000Z"
date, err := time.Parse(layout, dateString)
if err != nil {
return nil, err
}
distributorID := dto.DistributorID
return &feed{defaultStock{id: id, name: name, quantity: quantity, minQuantity: minQuantity, expirationDate: date, distributorID: distributorID}}, err
}
func (f *feed) Type() stockType {
return FEED
}
type accessory struct {
defaultStock
}
// NewAccessory creates a new stock item of accessory type (unexpirable stock item).
// TODO: minimum quantity should be an integer, represented as decimal
// (should not have non-zero values after floating point)
func NewAccessory(dto *NewStockDTO) (Stock, error) {
id, err := newUUID()
if err != nil {
return nil, err
}
name := dto.Name
quantityString := dto.Quantity
if quantityString == "" {
return nil, errors.New("no quantity set for stock")
}
quantity, err := decimal.NewFromString(quantityString)
if err != nil {
return nil, err
}
var (
minQuantityString = dto.MinQuantity
minQuantity decimal.Decimal
)
if minQuantityString != "" {
minQuantity, err = decimal.NewFromString(minQuantityString)
if err != nil {
return nil, err
}
} else {
minQuantity = decimal.New(0, 0)
}
dateString := dto.ExpirationDate
if dateString != "" {
if dateString != "" {
return nil, errors.New("error in creating stock item: expiration date set for an accessory")
}
}
distributorID := dto.DistributorID
return &accessory{defaultStock{id: id, name: name, quantity: quantity, minQuantity: minQuantity, distributorID: distributorID}}, err
}
func (a *accessory) Type() stockType {
return ACCESSORY
}
func (a *accessory) IsExpirable() bool {
return false
}
func (a *accessory) ExpirationDate() time.Time {
panic("Error - trying to read accessory's expiration date. Accessories do not expire.")
}
func (a *accessory) SetExpirationDate(time.Time) {
panic("Error - trying to set accessory's expiration date. Accessories do not expire.")
}
func compareStock(first, second Stock) bool {
return first.ID() == second.ID() &&
first.Type() == second.Type() &&
first.Name() == second.Name() &&
first.ExpirationDate() == second.ExpirationDate() &&
first.Quantity().Cmp(second.Quantity()) == 0 &&
first.MinQuantity().Cmp(second.MinQuantity()) == 0 &&
first.DistributorID() == second.DistributorID()
}