Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 121
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
TaskProgressReportService
0.00% covered (danger)
0.00%
0 / 121
0.00% covered (danger)
0.00%
0 / 4
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 adminUser
0.00% covered (danger)
0.00%
0 / 53
0.00% covered (danger)
0.00%
0 / 1
2
 separatorUser
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
2
 storerUser
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Domain\Shared\Services;
4
5use App\Helper\AppHelper;
6use App\Models\PickingOrder\PickingOrder;
7use App\Models\StockIO\StockIO;
8use App\Models\Transaction\Transaction;
9use App\Models\TransferTask\TransferTask;
10use App\Models\User;
11use Carbon\Carbon;
12use Domain\PickingOrder\DTOs\PickingOrderSearchDTO;
13use Domain\Shared\Concerns\FiltersExecutionDateRange;
14use Domain\Shared\DTOs\TaskResumedProgressSearchDTO;
15use Domain\Shared\Resources\TasksProgressResource;
16use Domain\Transaction\Enums\TransactionTypeEnum;
17use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
18use Illuminate\Support\Facades\DB;
19
20class TaskProgressReportService
21{
22    use FiltersExecutionDateRange;
23
24    private Carbon $startOfDay;
25    private Carbon $endOfDay;
26
27    public function __construct(TaskResumedProgressSearchDTO $taskResumedProgressSearchDTO) {
28        if($taskResumedProgressSearchDTO->date_range <> []){
29            $this->startOfDay = $taskResumedProgressSearchDTO->date_range[0]->startOfDay();
30            $this->endOfDay = $taskResumedProgressSearchDTO->date_range[1]->endOfDay();
31        } else {
32            $this->startOfDay = Carbon::now()->startOfDay();
33            $this->endOfDay = Carbon::now()->endOfDay();
34        }
35
36    }
37
38    /**
39     * @param PickingOrderSearchDTO $pickingOrderSearchDTO
40     * @param User $user
41     * @return
42     */
43    public function adminUser(User $user): AnonymousResourceCollection
44    {
45        $pickingOrder = PickingOrder::visibleToCurrentUser()
46            ->join('picking_order_products', 'picking_orders.id', '=', 'picking_order_products.picking_order_id')
47            ->join('stock_io_products', 'stock_io_products.id', '=', 'picking_order_products.stock_io_product_id')
48            ->join('product_measures', 'stock_io_products.product_id', '=', 'product_measures.product_id')
49            ->join('users', 'users.id', '=', 'picking_orders.responsible_id')
50            ->orderBy('picking_orders.start_date')
51            ->select('picking_orders.id', 'picking_orders.created_at', 'users.id as user_id', 'users.name')
52            ->selectRaw('SUM(stock_io_products.amount) as totalAmount')
53            ->selectRaw('SUM(product_measures.gross_weight) as totalWeight')
54            ->selectRaw("'picking_order' as typeReturn")
55            ->where('stock_io_products.picked', 'true')
56            ->where(function ($query) {
57                $this->applyExecutionWindowFilter($query, 'picking_orders', $this->startOfDay, $this->endOfDay);
58            })
59            ->groupBy('picking_orders.id', 'picking_orders.created_at', 'users.id', 'users.name')
60            ->orderBy('users.id')
61            ->get();
62
63        $transferTasks = TransferTask::visibleToCurrentUser()
64            ->join('transfer_task_products', 'transfer_tasks.id', '=', 'transfer_task_products.transfer_task_id')
65            ->join('product_measures', 'transfer_task_products.product_id', '=', 'product_measures.product_id')
66            ->join('users', 'users.id', '=', 'transfer_tasks.responsible_id')
67            ->select('transfer_tasks.id', 'transfer_tasks.created_at', 'users.id as user_id', 'users.name')
68            ->selectRaw('SUM(transfer_task_products.suggestion_amount) as totalAmount')
69            ->selectRaw('SUM(product_measures.gross_weight) as totalWeight')
70            ->selectRaw("'transfer_tasks' as typeReturn")
71            ->where('transfer_task_products.picked', 'true')
72            ->where(function ($query) {
73                $this->applyExecutionWindowFilter($query, 'transfer_tasks', $this->startOfDay, $this->endOfDay);
74            })
75            ->groupBy('transfer_tasks.id', 'transfer_tasks.created_at', 'users.id', 'users.name')
76            ->orderBy('users.id')
77            ->get();
78
79        $abilities = $user->currentAccessToken()->abilities;
80        $sector    = (int) array_key_first($abilities);
81
82        $input = Transaction::query()
83            ->join('users', 'users.id', 'transactions.user_id')
84            ->join('product_measures', function ($join) {
85                $join->on('product_measures.product_id', '=', DB::raw("(((transactions.item->>'product')::jsonb)->>'id')::bigint"));
86            })
87            ->select('transactions.id', 'transactions.created_at', 'users.id as user_id', 'users.name')
88            ->selectRaw('SUM(transactions.amount) as totalAmount')
89            ->selectRaw('SUM(product_measures.gross_weight) as totalWeight')
90            ->selectRaw("'stock_input' as typeReturn")
91            ->where('transactions.type', TransactionTypeEnum::INPUT)
92            ->whereBetween('transactions.created_at', [$this->startOfDay, $this->endOfDay])
93            ->where('transactions.sector', $sector)
94            ->where('users.id', '<>', AppHelper::systemUser()->getKey())
95            ->groupBy('transactions.id', 'transactions.created_at', 'users.id', 'users.name')
96            ->get();
97
98        $unifiedDates = $pickingOrder->merge($transferTasks)->merge($input)->sortByDesc('created_at');
99
100        return TasksProgressResource::collection(
101            $unifiedDates
102        );
103    }
104
105    /**
106     * @param User $user
107     * @return
108     */
109    public function separatorUser(User $user)
110    {
111        $pickingOrder = PickingOrder::visibleToCurrentUser()
112            ->join('picking_order_products', 'picking_orders.id', '=', 'picking_order_products.picking_order_id')
113            ->join('stock_io_products', 'stock_io_products.id', '=', 'picking_order_products.stock_io_product_id')
114            ->join('product_measures', 'stock_io_products.product_id', '=', 'product_measures.product_id')
115            ->orderBy('picking_orders.start_date')
116            ->select('picking_orders.id', 'picking_orders.created_at')
117            ->selectRaw('SUM(stock_io_products.amount) as totalAmount')
118            ->selectRaw('SUM(product_measures.gross_weight) as totalWeight')
119            ->selectRaw("'picking_order' as typeReturn")
120            ->where('stock_io_products.picked', 'true')
121            ->where(function ($query) {
122                $this->applyExecutionWindowFilter($query, 'picking_orders', $this->startOfDay, $this->endOfDay);
123            })
124            ->groupBy('picking_orders.id', 'picking_orders.created_at')
125            ->get();
126
127        $transferTasks = TransferTask::visibleToCurrentUser()
128            ->join('transfer_task_products', 'transfer_tasks.id', '=', 'transfer_task_products.transfer_task_id')
129            ->join('product_measures', 'transfer_task_products.product_id', '=', 'product_measures.product_id')
130            ->select('transfer_tasks.id', 'transfer_tasks.created_at')
131            ->selectRaw('SUM(transfer_task_products.suggestion_amount) as totalAmount')
132            ->selectRaw('SUM(product_measures.gross_weight) as totalWeight')
133            ->selectRaw("'transfer_tasks' as typeReturn")
134            ->where('transfer_task_products.picked', 'true')
135            ->where(function ($query) {
136                $this->applyExecutionWindowFilter($query, 'transfer_tasks', $this->startOfDay, $this->endOfDay);
137            })
138            ->groupBy('transfer_tasks.id', 'transfer_tasks.created_at')
139            ->get();
140
141        $unifiedDates = $pickingOrder->merge($transferTasks)->sortByDesc('created_at');
142
143        return TasksProgressResource::collection(
144            $unifiedDates
145        );
146    }
147
148    /**
149     * @param PickingOrderSearchDTO $pickingOrderSearchDTO
150     * @param User $user
151     * @return
152     */
153    public function storerUser(User $user)
154    {
155        $input = Transaction::query()
156            ->join('users', 'users.id', 'transactions.user_id')
157            ->join('product_measures', function ($join) {
158                $join->on('product_measures.product_id', '=', DB::raw("(((transactions.item->>'product')::jsonb)->>'id')::bigint"));
159            })
160            ->select('transactions.id', 'transactions.created_at')
161            ->selectRaw('SUM(transactions.amount) as totalAmount')
162            ->selectRaw('SUM(product_measures.gross_weight) as totalWeight')
163            ->selectRaw("'stock_input' as typeReturn")
164            ->where('transactions.type', TransactionTypeEnum::INPUT)
165            ->whereBetween('transactions.created_at', [$this->startOfDay, $this->endOfDay])
166            ->where('users.id', $user->getKey())
167            ->groupBy('transactions.id', 'transactions.created_at')
168            ->get();
169
170        $transferTasks = TransferTask::visibleToCurrentUser()
171            ->join('transfer_task_products', 'transfer_tasks.id', '=', 'transfer_task_products.transfer_task_id')
172            ->join('product_measures', 'transfer_task_products.product_id', '=', 'product_measures.product_id')
173            ->select('transfer_tasks.id', 'transfer_tasks.created_at')
174            ->selectRaw('SUM(transfer_task_products.suggestion_amount) as totalAmount')
175            ->selectRaw('SUM(product_measures.gross_weight) as totalWeight')
176            ->selectRaw("'transfer_tasks' as typeReturn")
177            ->where('transfer_task_products.picked', 'true')
178            ->where(function ($query) {
179                $this->applyExecutionWindowFilter($query, 'transfer_tasks', $this->startOfDay, $this->endOfDay);
180            })
181            ->groupBy('transfer_tasks.id', 'transfer_tasks.created_at')
182            ->get();
183
184        $unifiedDates = $input->merge($transferTasks)->sortByDesc('created_at');
185
186        return TasksProgressResource::collection(
187            $unifiedDates
188        );
189    }
190
191   
192}