-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathUserPaymentsListing.php
117 lines (97 loc) · 4 KB
/
UserPaymentsListing.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
namespace Crm\PaymentsModule\Components;
use Crm\ApplicationModule\Components\SimpleWidgetFactoryInterface;
use Crm\ApplicationModule\Widget\BaseWidget;
use Crm\ApplicationModule\Widget\WidgetManager;
use Crm\PaymentsModule\MailConfirmation\ParsedMailLogsRepository;
use Crm\PaymentsModule\Repository\PaymentsRepository;
use Crm\PaymentsModule\Repository\RecurrentPaymentsRepository;
use Nette\Application\BadRequestException;
use Nette\Localization\ITranslator;
use Nette\Utils\DateTime;
class UserPaymentsListing extends BaseWidget
{
private $templateName = 'user_payments_listing.latte';
private $paymentsRepository;
private $recurrentPaymentsRepository;
private $parsedMailLogsRepository;
private $translator;
public function __construct(
WidgetManager $widgetManager,
ITranslator $translator,
PaymentsRepository $paymentsRepository,
RecurrentPaymentsRepository $recurrentPaymentsRepository,
ParsedMailLogsRepository $parsedMailLogsRepository
) {
parent::__construct($widgetManager);
$this->paymentsRepository = $paymentsRepository;
$this->recurrentPaymentsRepository = $recurrentPaymentsRepository;
$this->parsedMailLogsRepository = $parsedMailLogsRepository;
$this->translator = $translator;
}
public function header($id = '')
{
$header = $this->translator->translate('payments.admin.component.user_payments_listing.header');
if ($id) {
$header .= ' <small>(' . $this->totalCount($id) . ')</small>';
}
$todayPayments = $this->paymentsRepository->userPayments($id)->where([
'status' => PaymentsRepository::STATUS_PAID,
'paid_at > ?' => DateTime::from(strtotime('today 00:00')),
])->count('*');
if ($todayPayments) {
$header .= ' <span class="label label-warning">' . $this->translator->translate('payments.admin.component.user_payments_listing.today') .'</span>';
}
return $header;
}
public function identifier()
{
return 'userpayments';
}
public function render($id)
{
$this->template->userId = $id;
$payments = $this->paymentsRepository->userPayments($id);
$variableSymbols = [];
foreach ($payments as $payment) {
$variableSymbols[] = $payment->variable_symbol;
}
$this->template->payments = $payments;
$this->template->paymentStatuses = $this->paymentsRepository->getStatusPairs();
$this->template->totalPayments = $this->totalCount($id);
$this->template->parsedEmails = $this->parsedMailLogsRepository->findByVariableSymbols($variableSymbols);
$recurrentPayments = $this->recurrentPaymentsRepository->userRecurrentPayments($id)
->order('id DESC, charge_at DESC');
$this->template->recurrentPayments = $recurrentPayments;
$this->template->totalRecurrentPayments = $recurrentPayments->count('*');
$this->template->setFile(__DIR__ . '/' . $this->templateName);
$this->template->render();
}
public function handleStopRecurrentPayment($recurrentPaymentId)
{
$recurrent = $this->recurrentPaymentsRepository->stoppedByAdmin($recurrentPaymentId);
if (!$recurrent) {
throw new BadRequestException();
}
$user = $recurrent->user;
$this->presenter->redirect(':Users:UsersAdmin:Show', $user->id);
}
private $totalCount = null;
private function totalCount($id)
{
if ($this->totalCount == null) {
$this->totalCount = $this->paymentsRepository->userPayments($id)->count('*');
}
return $this->totalCount;
}
protected function createComponentChangePaymentStatus(ChangePaymentStatusFactoryInterface $factory)
{
$control = $factory->create();
return $control;
}
protected function createComponentSimpleWidget(SimpleWidgetFactoryInterface $factory)
{
$control = $factory->create();
return $control;
}
}