1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Loevgaard\DandomainConsignment\Entity; |
4
|
|
|
|
5
|
|
|
use Assert\Assert; |
6
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
7
|
|
|
use Doctrine\Common\Collections\Collection; |
8
|
|
|
use Doctrine\ORM\Mapping as ORM; |
9
|
|
|
use Knp\DoctrineBehaviors\Model\Timestampable\Timestampable; |
10
|
|
|
use Loevgaard\DandomainConsignment\Entity\Generated\ReportInterface; |
11
|
|
|
use Loevgaard\DandomainConsignment\Entity\Generated\ReportTrait; |
12
|
|
|
use Loevgaard\DandomainFoundation\Entity\Generated\ManufacturerInterface; |
13
|
|
|
use Loevgaard\DandomainStock\Entity\Generated\StockMovementInterface; |
14
|
|
|
use Money\Currency; |
15
|
|
|
use Money\Money; |
16
|
|
|
use Symfony\Component\Validator\Constraints as FormAssert; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @ORM\Entity() |
20
|
|
|
* @ORM\Table(name="ldc_reports") |
21
|
|
|
* @ORM\HasLifecycleCallbacks() |
22
|
|
|
*/ |
23
|
|
|
class Report implements ReportInterface |
24
|
|
|
{ |
25
|
|
|
use ReportTrait; |
26
|
|
|
use Timestampable; |
27
|
|
|
|
28
|
|
|
const STATUS_ERROR = 'error'; |
29
|
|
|
const STATUS_PENDING = 'pending'; |
30
|
|
|
const STATUS_SUCCESSFUL = 'successful'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var int |
34
|
|
|
* |
35
|
|
|
* @ORM\Column(type="integer") |
36
|
|
|
* @ORM\GeneratedValue |
37
|
|
|
* @ORM\Id |
38
|
|
|
*/ |
39
|
|
|
protected $id; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var ManufacturerInterface |
43
|
|
|
* |
44
|
|
|
* @FormAssert\NotBlank() |
45
|
|
|
* |
46
|
|
|
* @ORM\ManyToOne(targetEntity="Loevgaard\DandomainFoundation\Entity\Manufacturer") |
47
|
|
|
* @ORM\JoinColumn(onDelete="CASCADE", nullable=false) |
48
|
|
|
*/ |
49
|
|
|
protected $manufacturer; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string |
53
|
|
|
* |
54
|
|
|
* @FormAssert\Choice(callback="getStatuses") |
55
|
|
|
* |
56
|
|
|
* @ORM\Column(type="string", length=191) |
57
|
|
|
*/ |
58
|
|
|
protected $status; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var string|null |
62
|
|
|
* |
63
|
|
|
* @ORM\Column(type="text", nullable=true) |
64
|
|
|
*/ |
65
|
|
|
protected $error; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var string |
69
|
|
|
* |
70
|
|
|
* @ORM\Column(type="string", length=191, nullable=true) |
71
|
|
|
*/ |
72
|
|
|
protected $file; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var StockMovementInterface[]|ArrayCollection |
76
|
|
|
* |
77
|
|
|
* @ORM\ManyToMany(targetEntity="Loevgaard\DandomainStock\Entity\StockMovement") |
78
|
|
|
* @ORM\JoinTable(name="ldc_reports_stock_movements") |
79
|
|
|
* @ORM\OrderBy({"createdAt" = "ASC"}) |
80
|
|
|
*/ |
81
|
|
|
protected $stockMovements; |
82
|
|
|
|
83
|
|
|
public function __construct() |
84
|
|
|
{ |
85
|
|
|
$this->status = self::STATUS_PENDING; |
86
|
|
|
$this->stockMovements = new ArrayCollection(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function markAsError(?string $error = null) : void |
90
|
|
|
{ |
91
|
|
|
$this->status = self::STATUS_ERROR; |
92
|
|
|
$this->error = $error; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function markAsSuccess() : void |
96
|
|
|
{ |
97
|
|
|
$this->status = self::STATUS_SUCCESSFUL; |
98
|
|
|
$this->error = null; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function isStatus(string $status) : bool |
102
|
|
|
{ |
103
|
|
|
return $this->status === $status; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function isSuccessful() : bool |
107
|
|
|
{ |
108
|
|
|
return $this->isStatus(self::STATUS_SUCCESSFUL); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function isError() : bool |
112
|
|
|
{ |
113
|
|
|
return $this->isStatus(self::STATUS_ERROR); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns true if the report can be delivered to the consignor |
118
|
|
|
* |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
public function isDeliverable() : bool |
122
|
|
|
{ |
123
|
|
|
return $this->isSuccessful(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @ORM\PrePersist() |
128
|
|
|
* @ORM\PreUpdate() |
129
|
|
|
*/ |
130
|
|
|
public function validate() : void |
131
|
|
|
{ |
132
|
|
|
Assert::that($this->manufacturer)->isInstanceOf(ManufacturerInterface::class); |
133
|
|
|
Assert::that($this->status)->choice(self::getStatuses()); |
134
|
|
|
Assert::thatNullOr($this->error)->string(); |
135
|
|
|
Assert::thatNullOr($this->file)->string()->maxLength(191); |
136
|
|
|
Assert::thatAll($this->stockMovements->toArray())->isInstanceOf(StockMovementInterface::class); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public static function getStatuses() : array |
140
|
|
|
{ |
141
|
|
|
return [ |
142
|
|
|
self::STATUS_PENDING => self::STATUS_PENDING, |
143
|
|
|
self::STATUS_SUCCESSFUL => self::STATUS_SUCCESSFUL, |
144
|
|
|
self::STATUS_ERROR => self::STATUS_ERROR |
145
|
|
|
]; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function addStockMovement(StockMovementInterface $stockMovement) : ReportInterface |
149
|
|
|
{ |
150
|
|
|
if (!$this->stockMovements->contains($stockMovement)) { |
151
|
|
|
$this->stockMovements->add($stockMovement); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function removeStockMovement(StockMovementInterface $stockMovement) : ReportInterface |
158
|
|
|
{ |
159
|
|
|
$this->stockMovements->removeElement($stockMovement); |
160
|
|
|
|
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function clearStockMovements() : ReportInterface |
165
|
|
|
{ |
166
|
|
|
$this->stockMovements->clear(); |
167
|
|
|
|
168
|
|
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Returns the total for all stock movements |
173
|
|
|
* Returns a Money object with amount = 0 and $defaultCurrency if there are no stock movements |
174
|
|
|
* |
175
|
|
|
* @param string $defaultCurrency |
176
|
|
|
* @return Money |
177
|
|
|
* @throws \Loevgaard\DandomainStock\Exception\UnsetCurrencyException |
178
|
|
|
*/ |
179
|
|
|
public function getTotal(string $defaultCurrency = 'DKK') : Money |
180
|
|
|
{ |
181
|
|
|
$total = null; |
182
|
|
|
|
183
|
|
|
foreach ($this->stockMovements as $stockMovement) { |
184
|
|
|
if (!$total) { |
185
|
|
|
$total = new Money(0, new Currency($stockMovement->getCurrency())); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$total = $total->add($stockMovement->getTotalPrice()); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
if (!$total) { |
192
|
|
|
$total = new Money(0, new Currency($defaultCurrency)); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return $total; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return int |
200
|
|
|
*/ |
201
|
|
|
public function getId(): int |
202
|
|
|
{ |
203
|
|
|
return (int)$this->id; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param int $id |
208
|
|
|
* @return ReportInterface |
209
|
|
|
*/ |
210
|
|
|
public function setId(int $id) : ReportInterface |
211
|
|
|
{ |
212
|
|
|
$this->id = $id; |
213
|
|
|
return $this; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return ManufacturerInterface |
218
|
|
|
*/ |
219
|
|
|
public function getManufacturer(): ?ManufacturerInterface |
220
|
|
|
{ |
221
|
|
|
return $this->manufacturer; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param ManufacturerInterface $manufacturer |
226
|
|
|
* @return ReportInterface |
227
|
|
|
*/ |
228
|
|
|
public function setManufacturer(ManufacturerInterface $manufacturer) : ReportInterface |
229
|
|
|
{ |
230
|
|
|
$this->manufacturer = $manufacturer; |
231
|
|
|
return $this; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @return string |
236
|
|
|
*/ |
237
|
|
|
public function getStatus(): ?string |
238
|
|
|
{ |
239
|
|
|
return $this->status; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param string $status |
244
|
|
|
* @return ReportInterface |
245
|
|
|
*/ |
246
|
|
|
public function setStatus(string $status) : ReportInterface |
247
|
|
|
{ |
248
|
|
|
$this->status = $status; |
249
|
|
|
return $this; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @return null|string |
254
|
|
|
*/ |
255
|
|
|
public function getError(): ?string |
256
|
|
|
{ |
257
|
|
|
return $this->error; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @param null|string $error |
262
|
|
|
* @return ReportInterface |
263
|
|
|
*/ |
264
|
|
|
public function setError(?string $error) : ReportInterface |
265
|
|
|
{ |
266
|
|
|
$this->error = $error; |
267
|
|
|
return $this; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @return \SplFileInfo |
272
|
|
|
*/ |
273
|
|
|
public function getFile(): ?\SplFileInfo |
274
|
|
|
{ |
275
|
|
|
if(!$this->file) { |
276
|
|
|
return null; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
return new \SplFileInfo($this->file); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @param \SplFileInfo $file |
284
|
|
|
* @return Report |
285
|
|
|
*/ |
286
|
|
|
public function setFile(\SplFileInfo $file) |
287
|
|
|
{ |
288
|
|
|
$this->file = $file->getPathname(); |
289
|
|
|
return $this; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @return StockMovementInterface[]|ArrayCollection |
294
|
|
|
*/ |
295
|
|
|
public function getStockMovements() : Collection |
296
|
|
|
{ |
297
|
|
|
return $this->stockMovements; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @param StockMovementInterface[]|Collection $stockMovements |
302
|
|
|
* @return ReportInterface |
303
|
|
|
*/ |
304
|
|
|
public function setStockMovements(Collection $stockMovements) : ReportInterface |
305
|
|
|
{ |
306
|
|
|
foreach ($stockMovements as $stockMovement) { |
307
|
|
|
$this->addStockMovement($stockMovement); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
return $this; |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|