1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Siak\Tontine\Service\Meeting\Pool; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Siak\Tontine\Model\Auction; |
9
|
|
|
use Siak\Tontine\Model\Session; |
10
|
|
|
use Siak\Tontine\Service\LocaleService; |
11
|
|
|
use Siak\Tontine\Service\Meeting\Session\SessionService; |
12
|
|
|
use Siak\Tontine\Service\TenantService; |
13
|
|
|
|
14
|
|
|
class AuctionService |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param TenantService $tenantService |
18
|
|
|
* @param LocaleService $localeService |
19
|
|
|
* @param SessionService $sessionService |
20
|
|
|
*/ |
21
|
|
|
public function __construct(private TenantService $tenantService, |
22
|
|
|
private LocaleService $localeService, private SessionService $sessionService) |
23
|
|
|
{} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param Session $session The session |
27
|
|
|
* @param bool $onlyPaid |
28
|
|
|
* |
29
|
|
|
* @return Builder|Relation |
30
|
|
|
*/ |
31
|
|
|
private function getQuery(Session $session, ?bool $onlyPaid = null): Builder|Relation |
32
|
|
|
{ |
33
|
|
|
return Auction::when($onlyPaid !== null, fn(Builder $qp) => |
|
|
|
|
34
|
|
|
$qp->where('paid', $onlyPaid)) |
35
|
|
|
->where(function(Builder $query) use($session) { |
36
|
|
|
// Take all the auctions in the current session |
37
|
|
|
$query->whereHas('remitment', fn(Builder $qr) => |
38
|
|
|
$qr->whereHas('payable', fn(Builder $qp) => |
39
|
|
|
$qp->where('session_id', $session->id))); |
40
|
|
|
// The auctions in the previous sessions. |
41
|
|
|
$query->orWhere(fn(Builder $qa) => $qa |
42
|
|
|
->whereHas('remitment', fn(Builder $qr) => |
43
|
|
|
$qr->whereHas('payable', fn(Builder $qp) => |
44
|
|
|
$qp->whereHas('session', fn($qs) => $qs->precedes($session, true)))) |
45
|
|
|
->where(function(Builder $query) use($session) { |
46
|
|
|
// The auctions that are not yet paid. |
47
|
|
|
$query->orWhere('paid', false); |
48
|
|
|
// The auctions that are paid in the current session. |
49
|
|
|
$query->orWhere(fn(Builder $qs) => $qs |
50
|
|
|
->where('paid', true) |
51
|
|
|
->where('session_id', $session->id)); |
52
|
|
|
})); |
53
|
|
|
}); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the number of auctions. |
58
|
|
|
* |
59
|
|
|
* @param Session $session The session |
60
|
|
|
* @param bool $onlyPaid |
61
|
|
|
* |
62
|
|
|
* @return int |
63
|
|
|
*/ |
64
|
|
|
public function getAuctionCount(Session $session, ?bool $onlyPaid): int |
65
|
|
|
{ |
66
|
|
|
return $this->getQuery($session, $onlyPaid)->count(); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the auctions. |
71
|
|
|
* |
72
|
|
|
* @param Session $session The session |
73
|
|
|
* @param bool $onlyPaid |
74
|
|
|
* @param int $page |
75
|
|
|
* |
76
|
|
|
* @return Collection |
77
|
|
|
*/ |
78
|
|
|
public function getAuctions(Session $session, ?bool $onlyPaid, int $page = 0): Collection |
79
|
|
|
{ |
80
|
|
|
return $this->getQuery($session, $onlyPaid) |
81
|
|
|
->page($page, $this->tenantService->getLimit()) |
82
|
|
|
->with(['remitment.payable.session', 'remitment.payable.subscription.member']) |
83
|
|
|
->get() |
84
|
|
|
->each(fn(Auction $auction) => |
85
|
|
|
$auction->member = $auction->remitment->payable->subscription->member) |
|
|
|
|
86
|
|
|
->sortBy('member.name', SORT_LOCALE_STRING) |
87
|
|
|
->values(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Set or unset the auction payment. |
92
|
|
|
* |
93
|
|
|
* @param Session $session The session |
94
|
|
|
* @param int $auctionId |
95
|
|
|
* |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
|
|
public function toggleAuctionPayment(Session $session, int $auctionId) |
99
|
|
|
{ |
100
|
|
|
$auction = $this->getQuery($session)->find($auctionId); |
101
|
|
|
if(($auction)) |
102
|
|
|
{ |
103
|
|
|
$auction->update(['paid' => !$auction->paid]); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|