Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
29 / 29 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| PayrollHelper | |
100.00% |
29 / 29 |
|
100.00% |
2 / 2 |
7 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getData | |
100.00% |
28 / 28 |
|
100.00% |
1 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Service; |
| 6 | |
| 7 | use App\Entity\Store; |
| 8 | use App\Entity\Transaction; |
| 9 | use App\Repository\StoreRepository; |
| 10 | use App\Repository\TransactionRepository; |
| 11 | |
| 12 | class PayrollHelper |
| 13 | { |
| 14 | public function __construct( |
| 15 | private readonly StoreRepository $storeRepository, |
| 16 | private readonly TransactionRepository $transactionRepository, |
| 17 | ) {} |
| 18 | |
| 19 | /** |
| 20 | * @return array{factDate: string, prevDate: string, stores: Store[], storeData: array<int, array{saldoIni: mixed, transactions: Transaction[]}>} |
| 21 | */ |
| 22 | public function getData( |
| 23 | int $year, |
| 24 | int $month, |
| 25 | int $storeId = 0 |
| 26 | ): array |
| 27 | { |
| 28 | $stores = $this->storeRepository->findAll(); |
| 29 | |
| 30 | $factDate = $year.'-'.$month.'-1'; |
| 31 | |
| 32 | if (1 === $month) { |
| 33 | $prevYear = $year - 1; |
| 34 | $prevMonth = 12; |
| 35 | } else { |
| 36 | $prevYear = $year; |
| 37 | $prevMonth = $month - 1; |
| 38 | } |
| 39 | |
| 40 | $prevDate = $prevYear.'-'.$prevMonth.'-01'; |
| 41 | |
| 42 | $selectedStores = []; |
| 43 | foreach ($stores as $store) { |
| 44 | if ($storeId && $store->getId() !== $storeId) { |
| 45 | continue; |
| 46 | } |
| 47 | |
| 48 | $selectedStores[] = $store; |
| 49 | } |
| 50 | |
| 51 | $selectedIds = array_map(static fn (Store $s): int => (int) $s->getId(), $selectedStores); |
| 52 | $prevDateStr = $prevYear.'-'.$prevMonth.'-01'; |
| 53 | |
| 54 | $saldos = $this->transactionRepository->getSaldoALaFechaByStores($selectedIds, $prevDateStr); |
| 55 | $payments = $this->transactionRepository->findMonthPaymentsByStores($selectedIds, $prevMonth, $prevYear); |
| 56 | |
| 57 | $storeData = []; |
| 58 | foreach ($selectedStores as $store) { |
| 59 | $id = (int) $store->getId(); |
| 60 | $storeData[$id]['saldoIni'] = $saldos[$id] ?? null; |
| 61 | $storeData[$id]['transactions'] = $payments[$id] ?? []; |
| 62 | } |
| 63 | |
| 64 | return [ |
| 65 | 'factDate' => $factDate, |
| 66 | 'prevDate' => $prevDate, |
| 67 | 'stores' => $selectedStores, |
| 68 | 'storeData' => $storeData, |
| 69 | ]; |
| 70 | } |
| 71 | } |