Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
TeamTasksService
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
30
0.00% covered (danger)
0.00%
0 / 1
 list
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3namespace Domain\Team\Services;
4
5use App\Models\PickingOrder\PickingOrder;
6use App\Models\StockIO\StockIO;
7use App\Models\Team\Team;
8use Domain\StockIO\Enums\StockProductStatusEnum;
9use Domain\Team\DTOs\TeamTaskSearchDTO;
10use Illuminate\Database\Eloquent\Builder;
11use Illuminate\Support\Collection;
12use Illuminate\Support\Facades\DB;
13
14readonly class TeamTasksService
15{
16    /**
17     * @param TeamTaskSearchDTO $teamTaskSearchDTO
18     * @param Team $team
19     * @return Collection
20     */
21    public function list(TeamTaskSearchDTO $teamTaskSearchDTO, Team $team): Collection
22    {
23        $teamID = $team->getKey();
24        $statusCanceledStockProduct = StockProductStatusEnum::CANCELED->value;
25
26        $filters = '';
27
28        if ($teamTaskSearchDTO->startDate) {
29            $dateStart = $teamTaskSearchDTO->startDate[0]->startOfDay()->format('Y-m-d\TH:i:s');
30            $dateEnd   = $teamTaskSearchDTO->startDate[1]->endOfDay()->format('Y-m-d\TH:i:s');
31
32            $filters   = "and picking_orders.start_date between '$dateStart' and '$dateEnd' ";
33        }
34
35        if ($teamTaskSearchDTO->endDate) {
36            $dateStart = $teamTaskSearchDTO->endDate[0]->startOfDay()->format('Y-m-d\TH:i:s');
37            $dateEnd   = $teamTaskSearchDTO->endDate[1]->endOfDay()->format('Y-m-d\TH:i:s');
38
39            $filters   = $filters . "and picking_orders.end_date between '$dateStart' and '$dateEnd' ";
40        }
41
42        if ($teamTaskSearchDTO->createdAt) {
43            $dateStart = $teamTaskSearchDTO->createdAt[0]->startOfDay()->format('Y-m-d\TH:i:s');
44            $dateEnd   = $teamTaskSearchDTO->createdAt[1]->endOfDay()->format('Y-m-d\TH:i:s');
45
46            $filters   = $filters . "and picking_orders.created_at between '$dateStart' and '$dateEnd' ";
47        }
48
49        if ($teamTaskSearchDTO->prvFat){
50            $dateStart = $teamTaskSearchDTO->prvFat[0]->startOfDay()->format('Y-m-d\TH:i:s');
51            $dateEnd   = $teamTaskSearchDTO->prvFat[1]->startOfDay()->format('Y-m-d\TH:i:s');
52
53            $filters   = $filters . "and integrations.parameters->>'PRVFAT' between '$dateStart' and '$dateEnd' ";
54        }
55
56        $status = implode(', ', $teamTaskSearchDTO->status);
57
58        $query = DB::select("with tarefa as (
59                                select picking_orders.responsible_id,
60                                       stock_io_products.id stock_io_product_id,
61                                       product_measures.gross_weight,
62                                       sum(stock_io_products.amount * product_measures.gross_weight) over(partition by picking_orders.responsible_id) total_weight
63                                  from picking_orders
64                                 inner join stock_ios
65                                    on stock_ios.id = picking_orders.stock_io_id
66                                   and stock_ios.team_id = $teamID
67                                 inner join picking_order_products
68                                    on picking_order_products.picking_order_id = picking_orders.id
69                                 inner join stock_io_products
70                                    on stock_io_products.stock_io_id = stock_ios.id
71                                   and stock_io_products.id          = picking_order_products.stock_io_product_id
72                                   and stock_io_products.status      <> $statusCanceledStockProduct
73                                 inner join product_measures
74                                    on product_measures.product_id = stock_io_products.product_id
75                                 where picking_orders.status in ($status)
76                                 $filters
77                            )
78
79                                select responsible_id,
80                                       min(total_weight) total_weight,
81                                       sum(coalesce(gross_weight * picked_items.amount, 0)) total_separeted,
82                                       min(total_weight) - sum(coalesce(gross_weight * picked_items.amount, 0)) weight_difference
83                                  from tarefa
84                                  left join picked_items
85                                    on picked_items.stock_io_product_id = tarefa.stock_io_product_id
86                                 group by responsible_id");
87
88        return collect($query);
89    }
90}