Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.12% |
16 / 17 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| StockIOProductMissingAmountService | |
94.12% |
16 / 17 |
|
75.00% |
3 / 4 |
7.01 | |
0.00% |
0 / 1 |
| resolve | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| resolveStockAmount | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
2.01 | |||
| resolveRequestedAmount | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| resolveDeliveredAmount | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\StockIO\Services; |
| 4 | |
| 5 | use App\Models\Address\Address; |
| 6 | use App\Models\Item\Item; |
| 7 | use App\Models\StockIO\StockIOProduct; |
| 8 | |
| 9 | class StockIOProductMissingAmountService |
| 10 | { |
| 11 | public function resolve(StockIOProduct $stockIOProduct): float |
| 12 | { |
| 13 | $requestedAmount = $this->resolveRequestedAmount($stockIOProduct); |
| 14 | $deliveredAmount = $this->resolveDeliveredAmount($stockIOProduct); |
| 15 | $availableAmount = $this->resolveStockAmount($stockIOProduct); |
| 16 | |
| 17 | return max(round($requestedAmount - $deliveredAmount - $availableAmount, 2), 0); |
| 18 | } |
| 19 | |
| 20 | public function resolveStockAmount(StockIOProduct $stockIOProduct): float |
| 21 | { |
| 22 | $productId = $stockIOProduct->getAttribute('product_id'); |
| 23 | |
| 24 | if (!$productId) { |
| 25 | return 0; |
| 26 | } |
| 27 | |
| 28 | return round((float) Item::query() |
| 29 | ->where('product_id', $productId) |
| 30 | ->where('itemable_type', Address::class) |
| 31 | ->sum('amount'), 2); |
| 32 | } |
| 33 | |
| 34 | private function resolveRequestedAmount(StockIOProduct $stockIOProduct): float |
| 35 | { |
| 36 | if ($stockIOProduct->relationLoaded('requestItem')) { |
| 37 | return (float) ($stockIOProduct->requestItem?->getAttribute('amount') ?? $stockIOProduct->getAttribute('amount')); |
| 38 | } |
| 39 | |
| 40 | return (float) ($stockIOProduct->requestItem()?->first()?->getAttribute('amount') ?? $stockIOProduct->getAttribute('amount')); |
| 41 | } |
| 42 | |
| 43 | private function resolveDeliveredAmount(StockIOProduct $stockIOProduct): float |
| 44 | { |
| 45 | if ($stockIOProduct->relationLoaded('requestDeliveries')) { |
| 46 | return round((float) $stockIOProduct->requestDeliveries->sum('amount'), 2); |
| 47 | } |
| 48 | |
| 49 | return round((float) $stockIOProduct->requestDeliveries()->sum('amount'), 2); |
| 50 | } |
| 51 | |
| 52 | } |