1 | <?php declare(strict_types=1); |
||
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 |
||
147 | |||
148 | public function addStockMovement(StockMovementInterface $stockMovement) : ReportInterface |
||
156 | |||
157 | public function removeStockMovement(StockMovementInterface $stockMovement) : ReportInterface |
||
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 |
||
197 | |||
198 | /** |
||
199 | * @return int |
||
200 | */ |
||
201 | public function getId(): int |
||
205 | |||
206 | /** |
||
207 | * @param int $id |
||
208 | * @return ReportInterface |
||
209 | */ |
||
210 | public function setId(int $id) : ReportInterface |
||
215 | |||
216 | /** |
||
217 | * @return ManufacturerInterface |
||
218 | */ |
||
219 | public function getManufacturer(): ?ManufacturerInterface |
||
223 | |||
224 | /** |
||
225 | * @param ManufacturerInterface $manufacturer |
||
226 | * @return ReportInterface |
||
227 | */ |
||
228 | public function setManufacturer(ManufacturerInterface $manufacturer) : ReportInterface |
||
233 | |||
234 | /** |
||
235 | * @return string |
||
236 | */ |
||
237 | public function getStatus(): ?string |
||
241 | |||
242 | /** |
||
243 | * @param string $status |
||
244 | * @return ReportInterface |
||
245 | */ |
||
246 | public function setStatus(string $status) : ReportInterface |
||
251 | |||
252 | /** |
||
253 | * @return null|string |
||
254 | */ |
||
255 | public function getError(): ?string |
||
259 | |||
260 | /** |
||
261 | * @param null|string $error |
||
262 | * @return ReportInterface |
||
263 | */ |
||
264 | public function setError(?string $error) : ReportInterface |
||
269 | |||
270 | /** |
||
271 | * @return \SplFileInfo |
||
272 | */ |
||
273 | public function getFile(): ?\SplFileInfo |
||
281 | |||
282 | /** |
||
283 | * @param \SplFileInfo $file |
||
284 | * @return Report |
||
285 | */ |
||
286 | public function setFile(\SplFileInfo $file) |
||
291 | |||
292 | /** |
||
293 | * @return StockMovementInterface[]|ArrayCollection |
||
294 | */ |
||
295 | public function getStockMovements() : Collection |
||
299 | |||
300 | /** |
||
301 | * @param StockMovementInterface[]|Collection $stockMovements |
||
302 | * @return ReportInterface |
||
303 | */ |
||
304 | public function setStockMovements(Collection $stockMovements) : ReportInterface |
||
312 | } |
||
313 |