-
Notifications
You must be signed in to change notification settings - Fork 0
/
quantities.php
125 lines (106 loc) · 3.83 KB
/
quantities.php
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
<?php
require __DIR__ . '/TestFrameworkInATweet.php';
class SalesOrderLine
{
/**
* @var float
*/
private $quantityInitiallyOrdered;
/**
* @var float
*/
private $quantitySoFarDelivered;
/**
* @var float
*/
private $quantityOpen;
/**
* @var int
*/
private $quantityPrecision = 2;
/**
* @param float $quantityInitiallyOrdered
*/
public function __construct($quantityInitiallyOrdered)
{
$this->quantityInitiallyOrdered = $quantityInitiallyOrdered;
$this->quantitySoFarDelivered = 0.0;
$this->quantityOpen = $quantityInitiallyOrdered;
}
/**
* @param float $quantityInCurrentDelivery
* @return SalesOrderLine
*/
public function processDelivery($quantityInCurrentDelivery)
{
$this->quantitySoFarDelivered = round($this->quantitySoFarDelivered + $quantityInCurrentDelivery, $this->quantityPrecision);
$this->quantityOpen = $this->quantityInitiallyOrdered - $quantityInCurrentDelivery;
if ($this->quantityOpen < 0) {
// over-delivering is fine, but we'll set quantity open to 0
$this->quantityOpen = 0.0;
}
return $this;
}
/**
* @param $quantityInCurrentDelivery
* @return $this
*/
public function undoDelivery($quantityInCurrentDelivery)
{
$this->quantitySoFarDelivered = round($this->quantitySoFarDelivered - $quantityInCurrentDelivery, $this->quantityPrecision);
if ($this->quantitySoFarDelivered < 0) {
$this->quantitySoFarDelivered = 0.0;
}
$this->quantityOpen = round($this->quantityOpen + $quantityInCurrentDelivery, $this->quantityPrecision);
if ($this->quantityOpen > $this->quantityInitiallyOrdered) {
// maybe we have been over-delivered, but we shouldn't expect more than we ordered
$this->quantityOpen = $this->quantityInitiallyOrdered;
}
return $this;
}
public function getQuantityInitiallyOrdered()
{
return $this->quantityInitiallyOrdered;
}
public function getQuantitySoFarDelivered()
{
return $this->quantitySoFarDelivered;
}
public function getQuantityOpen()
{
return $this->quantityOpen;
}
public function getQuantityPrecision()
{
return $this->quantityPrecision;
}
}
$line = new SalesOrderLine(12.5);
it('increases the quantity so far delivered upon processing a delivery',
(new SalesOrderLine(12.5))->processDelivery(10.0)->getQuantitySoFarDelivered() === 10.0
);
it('decreases the quantity still open upon processing a delivery',
(new SalesOrderLine(12.5))->processDelivery(10.0)->getQuantityOpen() === 2.5
);
it('decreases the quantity so far delivered upon undoing a delivery',
(new SalesOrderLine(12.5))->processDelivery(12.5)->undoDelivery(5.0)->getQuantitySoFarDelivered() === 7.5
);
it('increases the quantity open upon undoing a delivery',
(new SalesOrderLine(12.5))->processDelivery(12.5)->undoDelivery(5.0)->getQuantityOpen() === 5.0
);
it('does not let quantity open go below 0',
(new SalesOrderLine(12.5))->processDelivery(15.0)->getQuantityOpen() === 0.0
);
it('does not let quantity remaining to be delivered go over the initially ordered quantity',
(new SalesOrderLine(12.5))->undoDelivery(15.0)->getQuantityOpen() === 12.5
);
done();
/*
* Design issues that should be fixed by introducing value objects:
*
* - Dealing with "quantity precision" (i.e. number of decimals taken into account).
* - Preventing "quantity so far delivered" to end up being less than 0.
* - Preventing "quantity open" to end up being more than "quantity initially ordered".
*
* Question: what is the relation between these different quantities? Which ones do we need to "remember" (i.e. store in a database, etc.), which ones can we derive/compute?
*/