-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ByEmail.php
274 lines (235 loc) · 8.59 KB
/
ByEmail.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
declare(strict_types=1);
/**
* This file is part of the AbraFlexi Reminder package
*
* https://github.com/VitexSoftware/abraflexi-reminder
*
* (c) Vítězslav Dvořák <http://vitexsoftware.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace AbraFlexi\Reminder\Notifier;
use AbraFlexi\Bricks\Customer;
use AbraFlexi\FakturaVydana;
use AbraFlexi\Formats;
use AbraFlexi\Functions;
use AbraFlexi\Reminder\RemindMailer;
use AbraFlexi\Reminder\Upominac;
use AbraFlexi\Reminder\Upominka;
use AbraFlexi\RO;
use AbraFlexi\ui\CompanyLogo;
use Ease\Html\DivTag;
use Ease\Html\HrTag;
use Ease\Html\PTag;
use Ease\Html\TableTag;
use Ease\Html\TdTag;
use Ease\Html\TrTag;
use Ease\Sand;
/**
* AbraFlexi - Remind by eMail class.
*
* @author Vítězslav Dvořák <info@vitexsoftware.cz>
* @copyright 2018-2023 Spoje.Net
*/
class ByEmail extends Sand
{
/**
* @var bool status
*/
public bool $result;
public RemindMailer $mailer;
public FakturaVydana $invoicer;
private \AbraFlexi\Adresar $firmer;
/**
* eMail notification.
*/
public function __construct(\AbraFlexi\Reminder\Upominac $reminder, int $score, array $debts)
{
$result = false;
$this->setObjectName();
if ($this->compile($score, $reminder->customer, $debts)) {
$result = $this->send();
if ($score && $result) {
$reminder->customer->adresar->setData([
'id' => $reminder->customer->adresar->getRecordIdent(),
'stitky' => 'UPOMINKA'.$score,
], true);
$reminder->addStatusMessage(
sprintf(
_('Set Label %s '),
'UPOMINKA'.$score,
),
$reminder->customer->adresar->sync() ? 'success' : 'error',
);
}
} else {
$this->addStatusMessage(_('Remind was not sent'), 'warning');
}
$this->result = $result;
}
/**
* Compile Reminder message with its contents.
*
* @param int $score Weeks after due date
* @param Customer $customer
* @param array $clientDebts
*
* @return bool
*/
public function compile($score, $customer, $clientDebts)
{
$result = false;
$email = $customer->adresar->getNotificationEmailAddress();
$nazev = $customer->adresar->getDataValue('nazev');
$this->invoicer = new FakturaVydana();
$this->firmer = &$customer->adresar;
if ($email) {
$upominka = new Upominka();
switch ($score) {
case 1:
$upominka->loadTemplate('prvniUpominka');
break;
case 2:
$upominka->loadTemplate('druhaUpominka');
break;
case 3:
$upominka->loadTemplate('pokusOSmir');
break;
default:
$lastInvDays = Upominac::getDaysToLastInventarization($clientDebts);
if ($lastInvDays < 14) {
$this->addStatusMessage(
sprintf(
_('Last remind / inventarization for %s send before %d days; skipping'),
Functions::uncode((string) $customer),
$lastInvDays,
),
'debug',
);
return false;
}
$upominka->loadTemplate('inventarizace');
}
$invoices = [];
$to = $email;
$dnes = new \DateTime();
$subject = $upominka->getDataValue('hlavicka').' ke dni '.$dnes->format('d.m.Y');
$this->mailer = new RemindMailer($to, $subject);
$heading = new DivTag($upominka->getDataValue('uvod').' '.$nazev);
if (\Ease\Shared::cfg('ADD_LOGO')) {
$headingTableRow = new TrTag();
$headingTableRow->addItem(new TdTag($heading));
$logo = new CompanyLogo([
'align' => 'right',
'id' => 'companylogo',
'height' => '50',
'title' => _('Company logo'),
]);
$headingTableRow->addItem(
new TdTag(
$logo,
['width' => '200px'],
),
);
$headingTable = new TableTag(
$headingTableRow,
['width' => '100%'],
);
$this->mailer->addItem($headingTable);
} else {
$this->mailer->addItem($heading);
}
$this->mailer->addItem(new PTag());
$this->mailer->addItem(new DivTag(nl2br($upominka->getDataValue('textNad'))));
$debtsTable = new TableTag(
null,
['class' => 'greyGridTable'],
);
$debtsTable->addRowHeaderColumns([
_('Code'),
_('var. sym.'),
_('Amount'),
_('Currency'),
_('Due Date'),
_('overdue days'),
]);
foreach ($clientDebts as $debt) {
$currency = Functions::uncode((string) $debt['mena']);
if ($currency === 'CZK') {
$amount = $debt['zbyvaUhradit'];
} else {
$amount = $debt['zbyvaUhraditMen'];
}
$debtsTable->addRowColumns([
$debt['kod'],
$debt['varSym'],
Upominka::formatCurrency($amount),
$currency,
$debt['datSplat']->format('d.m.Y'),
FakturaVydana::overdueDays($debt['datSplat']),
]);
}
$debtsTable->addRowFooterColumns(['', _('Total'), Upominac::formatTotals(Upominka::getSums($clientDebts))]);
$this->mailer->addItem(new PTag('<br clear="all"/>'));
$this->mailer->addItem($debtsTable);
$this->mailer->addItem(new PTag('<br clear="all"/>'));
$this->mailer->addItem(new DivTag(nl2br($upominka->getDataValue('textPod'))));
$this->mailer->addItem(new PTag('<br clear="all"/>'));
$this->mailer->addItem(new HrTag());
$this->mailer->addItem(new DivTag(nl2br($upominka->getDataValue('zapati'))));
if (\Ease\Shared::cfg('QR_PAYMENTS')) {
$this->mailer->addItem(Upominka::qrPayments($clientDebts));
}
$this->addAttachments($clientDebts);
$this->mailer->addItem(new \Ease\Html\PTag(new \Ease\Html\SmallTag(new \Ease\Html\ATag('https://github.com/VitexSoftware/abraflexi-reminder', \Ease\Shared::appName()).' v'.\Ease\Shared::appVersion())));
$result = true;
} else {
$this->addStatusMessage(
sprintf(
_('Client %s without email %s !!!'),
$nazev,
$this->firmer->getApiURL(),
),
'error',
);
}
return $result;
}
/**
* Attach PDF and ISDOC invoices.
*
* @param array $clientDebts
*/
public function addAttachments($clientDebts): void
{
foreach ($clientDebts as $debtCode => $debt) {
if (\Ease\Shared::cfg('MAX_MAIL_SIZE') && ($this->mailer->getCurrentMailSize() > \Ease\Shared::cfg('MAX_MAIL_SIZE', 30000000))) {
$this->mailer->addStatusMessage(sprintf(_('Not enough space in this mail for attaching %s '), $debtCode), 'warning');
continue;
}
if (\array_key_exists('evidence', $debt)) {
$this->invoicer->setEvidence($debt['evidence']);
}
$this->invoicer->setMyKey(RO::code($debt['kod']));
$this->mailer->addFile(
$this->invoicer->downloadInFormat('pdf', '/tmp/'),
Formats::$formats['PDF']['content-type'],
);
$this->mailer->addFile(
$this->invoicer->downloadInFormat('isdocx', '/tmp/'),
Formats::$formats['ISDOCx']['content-type'],
);
}
}
/**
* Send Email Remind.
*
* @return bool
*/
public function send()
{
return $this->mailer->send();
}
}