Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 70 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| StockProductListService | |
0.00% |
0 / 70 |
|
0.00% |
0 / 2 |
156 | |
0.00% |
0 / 1 |
| mapProducts | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
2 | |||
| getAddressForExpedition | |
0.00% |
0 / 59 |
|
0.00% |
0 / 1 |
132 | |||
| 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\User; |
| 8 | use Domain\Address\Enums\AddressCategoryEnum; |
| 9 | use Domain\Address\Exceptions\AddressException; |
| 10 | use Domain\Shared\Enums\SectorEnum; |
| 11 | use Domain\StockIO\DTOs\SearchStockInputDTO; |
| 12 | use Illuminate\Database\Eloquent\Builder; |
| 13 | use Illuminate\Database\Eloquent\Relations\MorphMany; |
| 14 | use Illuminate\Support\Collection; |
| 15 | use Illuminate\Support\Facades\DB; |
| 16 | use Illuminate\Pagination\LengthAwarePaginator; |
| 17 | |
| 18 | readonly class StockProductListService |
| 19 | { |
| 20 | public function mapProducts(SearchStockInputDTO $searchStockInputDTO, User $user): Collection |
| 21 | { |
| 22 | $address = $this->getAddressForExpedition($searchStockInputDTO); |
| 23 | |
| 24 | return $address->items->map(function (Item $item) use ($searchStockInputDTO, $user, $address) { |
| 25 | return [ |
| 26 | "item" => $item->load('integration')->toArray(), |
| 27 | "product" => $item->product->load(['measure', 'integration', 'family.origin', 'unitMeasurement'])->toArray(), |
| 28 | "amount" => $item->getAttribute('amount'), |
| 29 | "transferable_id" => $address->transferable->getKey(), |
| 30 | "created_at" => $item->getAttribute('created_at'), |
| 31 | "updated_at" => $item->getAttribute('updated_at'), |
| 32 | ]; |
| 33 | }); |
| 34 | } |
| 35 | |
| 36 | |
| 37 | private function getAddressForExpedition(SearchStockInputDTO $searchStockInputDTO): Address |
| 38 | { |
| 39 | /** @var ?Address $address */ |
| 40 | $address = Address::query() |
| 41 | ->where('category', AddressCategoryEnum::INPUT->value) |
| 42 | ->where('sector', SectorEnum::EXPEDITION->value) |
| 43 | ->with(['items' => function (MorphMany $query) use ($searchStockInputDTO) { |
| 44 | if ($searchStockInputDTO->integrationCode) { |
| 45 | $query->whereHas('product', function (Builder $query) use ($searchStockInputDTO) { |
| 46 | $query->whereHas('integration', function (Builder $query) use ($searchStockInputDTO) { |
| 47 | $query->where('parameters->integration_code', $searchStockInputDTO->integrationCode); |
| 48 | }); |
| 49 | }); |
| 50 | } |
| 51 | |
| 52 | if ($searchStockInputDTO->integrationBarcode) { |
| 53 | $query->whereHas('product', function (Builder $query) use ($searchStockInputDTO) { |
| 54 | $query->whereHas('integration', function (Builder $query) use ($searchStockInputDTO) { |
| 55 | $query->where('parameters->integration_barcode', $searchStockInputDTO->integrationBarcode); |
| 56 | }); |
| 57 | }); |
| 58 | } |
| 59 | |
| 60 | if ($searchStockInputDTO->integrationDerivation) { |
| 61 | $query->whereHas('product', function (Builder $query) use ($searchStockInputDTO) { |
| 62 | $query->whereHas('integration', function (Builder $query) use ($searchStockInputDTO) { |
| 63 | $query->where('parameters->integration_derivation', $searchStockInputDTO->integrationDerivation); |
| 64 | }); |
| 65 | }); |
| 66 | } |
| 67 | |
| 68 | if ($searchStockInputDTO->integrationProduct) { |
| 69 | $query->whereHas('product', function (Builder $query) use ($searchStockInputDTO) { |
| 70 | $query->whereHas('integration', function (Builder $query) use ($searchStockInputDTO) { |
| 71 | $query->where('parameters->integration_product', $searchStockInputDTO->integrationProduct); |
| 72 | }); |
| 73 | }); |
| 74 | } |
| 75 | |
| 76 | if ($searchStockInputDTO->integrationCompany) { |
| 77 | $query->whereHas('product', function (Builder $query) use ($searchStockInputDTO) { |
| 78 | $query->whereHas('integration', function (Builder $query) use ($searchStockInputDTO) { |
| 79 | $query->where('parameters->integration_company', $searchStockInputDTO->integrationCompany); |
| 80 | }); |
| 81 | }); |
| 82 | } |
| 83 | |
| 84 | if ($searchStockInputDTO->productionOrder) { |
| 85 | $query->whereHas('integration', function (Builder $query) use ($searchStockInputDTO) { |
| 86 | $query->where('parameters->production_order', $searchStockInputDTO->productionOrder); |
| 87 | }); |
| 88 | } |
| 89 | |
| 90 | if ($searchStockInputDTO->movementDate) { |
| 91 | $query->addSelect('items.*'); |
| 92 | $query->join('transactions', function ($join) use ($searchStockInputDTO) { |
| 93 | $join->on(DB::raw("(transactions.item ->> 'id')::bigint"), '=', 'items.id') |
| 94 | ->whereBetween(DB::raw("(transactions.item->'integration'->'parameters'->>'movement_date')::timestamp"), [ |
| 95 | $searchStockInputDTO->movementDate[0]->startOfDay()->format('Y-m-d H:i:s'), |
| 96 | $searchStockInputDTO->movementDate[1]->endOfDay()->format('Y-m-d H:i:s'), |
| 97 | ]); |
| 98 | }); |
| 99 | } |
| 100 | |
| 101 | if ($searchStockInputDTO->description) { |
| 102 | $query->whereHas('product', function (Builder $query) use ($searchStockInputDTO) { |
| 103 | $query->where('description', 'like', '%'.$searchStockInputDTO->description.'%'); |
| 104 | }); |
| 105 | } |
| 106 | |
| 107 | if($searchStockInputDTO->id) { |
| 108 | $query->whereHas('product', function (Builder $query) use ($searchStockInputDTO) { |
| 109 | $query->where('id', $searchStockInputDTO->id); |
| 110 | }); |
| 111 | } |
| 112 | |
| 113 | }])->first(); |
| 114 | |
| 115 | |
| 116 | if (!$address) { |
| 117 | throw new AddressException(__('Input Address not found')); |
| 118 | } |
| 119 | |
| 120 | return $address; |
| 121 | } |
| 122 | |
| 123 | } |