|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Application\Handler; |
|
6
|
|
|
|
|
7
|
|
|
use Application\Model\TransactionLine; |
|
8
|
|
|
use Money\Currencies\ISOCurrencies; |
|
9
|
|
|
use Money\Formatter\DecimalMoneyFormatter; |
|
10
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; |
|
11
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
|
12
|
|
|
|
|
13
|
|
|
class ExportTransactionLinesHandler extends AbstractExcel |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var DecimalMoneyFormatter |
|
17
|
|
|
*/ |
|
18
|
|
|
private $moneyFormatter; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* ReportTransactionsAction constructor. |
|
22
|
|
|
*/ |
|
23
|
1 |
|
public function __construct(string $hostname) |
|
24
|
|
|
{ |
|
25
|
1 |
|
parent::__construct($hostname, 'transactionLines'); |
|
26
|
|
|
|
|
27
|
1 |
|
$currencies = new ISOCurrencies(); |
|
28
|
1 |
|
$this->moneyFormatter = new DecimalMoneyFormatter($currencies); |
|
29
|
|
|
|
|
30
|
1 |
|
$this->outputFileName = sprintf('Ichtus_compta_ecritures_%s.xlsx', date('Y-m-d-H:i:s')); |
|
31
|
|
|
|
|
32
|
1 |
|
$sheet = $this->workbook->getActiveSheet(); |
|
33
|
1 |
|
$sheet->setTitle('Compta Écritures'); |
|
34
|
1 |
|
$sheet->getDefaultRowDimension()->setRowHeight(20); |
|
35
|
1 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The model class name |
|
39
|
|
|
*/ |
|
40
|
|
|
protected function getModelClass(): string |
|
41
|
|
|
{ |
|
42
|
|
|
return TransactionLine::class; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param TransactionLine[] $items |
|
47
|
|
|
*/ |
|
48
|
1 |
|
protected function writeData(Worksheet $sheet, array $items): void |
|
49
|
|
|
{ |
|
50
|
1 |
|
$sheet->setShowGridlines(false); |
|
51
|
1 |
|
$initialColumn = $maxColumn = $this->column; |
|
|
|
|
|
|
52
|
1 |
|
$currentTransactionRowStart = null; |
|
53
|
1 |
|
$currentTransactionId = null; |
|
54
|
|
|
|
|
55
|
|
|
$mergeRowsFromColumns = [ |
|
56
|
1 |
|
$initialColumn + 1, // Transaction.ID |
|
57
|
1 |
|
$initialColumn + 2, // Transaction.name |
|
58
|
1 |
|
$initialColumn + 3, // Transaction.remarks |
|
59
|
1 |
|
$initialColumn + 4, // Transaction.internalRemarks |
|
60
|
|
|
]; |
|
61
|
|
|
|
|
62
|
1 |
|
foreach ($items as $index => $line) { |
|
63
|
|
|
$transaction = $line->getTransaction(); |
|
64
|
|
|
$transactionId = $transaction->getId(); |
|
65
|
|
|
|
|
66
|
|
|
if (!$currentTransactionId) { |
|
67
|
|
|
$currentTransactionId = $transactionId; |
|
68
|
|
|
$currentTransactionRowStart = $this->row; |
|
69
|
|
|
} elseif ($transactionId !== $currentTransactionId) { |
|
70
|
|
|
// Current line starts a new transaction |
|
71
|
|
|
if ($currentTransactionRowStart < $this->row - 1) { |
|
72
|
|
|
// Multi-line transaction |
|
73
|
|
|
// Merge cells that have the same value because from the same transaction |
|
74
|
|
|
foreach ($mergeRowsFromColumns as $column) { |
|
75
|
|
|
$sheet->mergeCellsByColumnAndRow($column, $currentTransactionRowStart, $column, $this->row - 1); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
$currentTransactionRowStart = $this->row; |
|
79
|
|
|
$currentTransactionId = $transactionId; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
// Date |
|
83
|
|
|
$this->write($sheet, $line->getTransactionDate()->format('d.m.Y')); |
|
84
|
|
|
|
|
85
|
|
|
// Transaction.ID |
|
86
|
|
|
$this->write($sheet, $transactionId); |
|
87
|
|
|
$url = 'https://' . $this->hostname . '/admin/transaction/%u'; |
|
88
|
|
|
$sheet->getCellByColumnAndRow($this->column - 1, $this->row)->getHyperlink()->setUrl(sprintf($url, $transactionId)); |
|
89
|
|
|
// Transaction.name |
|
90
|
|
|
$this->write($sheet, $transaction->getName()); |
|
91
|
|
|
// Transaction.remarks |
|
92
|
|
|
$this->write($sheet, $transaction->getRemarks()); |
|
93
|
|
|
// Transaction.internalRemarks |
|
94
|
|
|
$this->write($sheet, $transaction->getInternalRemarks()); |
|
95
|
|
|
|
|
96
|
|
|
// TransactionLine.name |
|
97
|
|
|
$this->write($sheet, $line->getName()); |
|
98
|
|
|
// TransactionLine.remarks |
|
99
|
|
|
$this->write($sheet, $line->getRemarks()); |
|
100
|
|
|
// Bookable.name |
|
101
|
|
|
$this->write($sheet, $line->getBookable() ? $line->getBookable()->getName() : ''); |
|
102
|
|
|
// Debit account |
|
103
|
|
|
$debit = $line->getDebit(); |
|
104
|
|
|
$this->write($sheet, $debit ? implode(' ', [$debit->getCode(), $debit->getName()]) : ''); |
|
105
|
|
|
// Credit account |
|
106
|
|
|
$credit = $line->getCredit(); |
|
107
|
|
|
$this->write($sheet, $credit ? implode(' ', [$credit->getCode(), $credit->getName()]) : ''); |
|
108
|
|
|
// Debit amount |
|
109
|
|
|
$this->write($sheet, $debit ? $this->moneyFormatter->format($line->getBalance()) : ''); |
|
110
|
|
|
// Credit amount |
|
111
|
|
|
$this->write($sheet, $credit ? $this->moneyFormatter->format($line->getBalance()) : ''); |
|
112
|
|
|
// Reconciled |
|
113
|
|
|
$this->write($sheet, $line->isReconciled() ? '✔️' : ''); |
|
114
|
|
|
|
|
115
|
|
|
$maxColumn = $this->column - 1; |
|
116
|
|
|
|
|
117
|
|
|
$range = Coordinate::stringFromColumnIndex($initialColumn) . $this->row . ':' . Coordinate::stringFromColumnIndex($maxColumn) . $this->row; |
|
118
|
|
|
// Thicker horizontal delimiter between lines of different transactions |
|
119
|
|
|
if ($index === max(array_keys($items)) || $line->getTransaction()->getId() !== $items[$index + 1]->getTransaction()->getId()) { |
|
120
|
|
|
$borderBottom = self::$bordersBottom; |
|
121
|
|
|
} else { |
|
122
|
|
|
$borderBottom = self::$bordersBottomLight; |
|
123
|
|
|
} |
|
124
|
|
|
$sheet->getStyle($range)->applyFromArray($borderBottom); |
|
125
|
|
|
|
|
126
|
|
|
$this->column = $initialColumn; |
|
127
|
|
|
++$this->row; |
|
128
|
|
|
} |
|
129
|
1 |
|
} |
|
130
|
|
|
|
|
131
|
1 |
|
protected function getHeaders(): array |
|
132
|
|
|
{ |
|
133
|
|
|
return [ |
|
134
|
1 |
|
['label' => 'Date', 'width' => 12, 'formats' => [self::$dateFormat]], |
|
135
|
1 |
|
['label' => 'ID', 'width' => 7, 'formats' => [self::$headerFormat, self::$centerFormat]], |
|
136
|
1 |
|
['label' => 'Transaction', 'width' => 25, 'formats' => [self::$wrapFormat]], |
|
137
|
1 |
|
['label' => 'Remarques', 'width' => 25, 'formats' => [self::$wrapFormat]], |
|
138
|
1 |
|
['label' => 'Remarques internes', 'width' => 25, 'formats' => [self::$wrapFormat]], |
|
139
|
1 |
|
['label' => 'Écriture', 'width' => 25, 'formats' => [self::$wrapFormat]], |
|
140
|
1 |
|
['label' => 'Remarques', 'width' => 25, 'formats' => [self::$wrapFormat]], |
|
141
|
1 |
|
['label' => 'Réservable', 'width' => 30, 'formats' => [self::$wrapFormat]], |
|
142
|
1 |
|
['label' => 'Compte débit', 'width' => 30, 'formats' => [self::$wrapFormat]], |
|
143
|
1 |
|
['label' => 'Compte crédit', 'width' => 30, 'formats' => [self::$wrapFormat]], |
|
144
|
1 |
|
['label' => 'Montant débit', 'width' => 20, 'formats' => [self::$headerFormat]], |
|
145
|
1 |
|
['label' => 'Montant crédit', 'width' => 20, 'formats' => [self::$headerFormat]], |
|
146
|
1 |
|
['label' => 'Pointé', 'width' => 15, 'formats' => [self::$centerFormat]], |
|
147
|
|
|
]; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @param TransactionLine[] $items |
|
152
|
|
|
*/ |
|
153
|
1 |
|
protected function writeFooter(Worksheet $sheet, array $items): void |
|
154
|
|
|
{ |
|
155
|
1 |
|
$initialColumn = $this->column; |
|
156
|
|
|
|
|
157
|
|
|
// Date |
|
158
|
1 |
|
$this->write($sheet, ''); |
|
159
|
|
|
// ID |
|
160
|
1 |
|
$this->write($sheet, ''); |
|
161
|
|
|
// Transaction.name |
|
162
|
1 |
|
$this->write($sheet, ''); |
|
163
|
|
|
// Transaction.remarks |
|
164
|
1 |
|
$this->write($sheet, ''); |
|
165
|
|
|
// Internal remarks |
|
166
|
1 |
|
$this->write($sheet, ''); |
|
167
|
|
|
// TransactionLine.name |
|
168
|
1 |
|
$this->write($sheet, ''); |
|
169
|
|
|
// Remarks |
|
170
|
1 |
|
$this->write($sheet, ''); |
|
171
|
|
|
// Bookable.name |
|
172
|
1 |
|
$this->write($sheet, ''); |
|
173
|
|
|
// Debit account |
|
174
|
1 |
|
$this->write($sheet, ''); |
|
175
|
|
|
// Credit account |
|
176
|
1 |
|
$this->write($sheet, ''); |
|
177
|
|
|
// Debit amount |
|
178
|
1 |
|
$this->write($sheet, '=SUBTOTAL(9,' . Coordinate::stringFromColumnIndex($this->column) . '2:' . Coordinate::stringFromColumnIndex($this->column) . ($this->row - 1) . ')'); |
|
179
|
|
|
// Credit amount |
|
180
|
1 |
|
$this->write($sheet, '=SUBTOTAL(9,' . Coordinate::stringFromColumnIndex($this->column) . '2:' . Coordinate::stringFromColumnIndex($this->column) . ($this->row - 1) . ')'); |
|
181
|
|
|
// Reconciled |
|
182
|
1 |
|
$this->write($sheet, ''); |
|
183
|
|
|
|
|
184
|
|
|
// Apply style |
|
185
|
1 |
|
$range = Coordinate::stringFromColumnIndex($initialColumn) . $this->row . ':' . Coordinate::stringFromColumnIndex($this->column - 1) . $this->row; |
|
186
|
1 |
|
$sheet->getStyle($range)->applyFromArray(self::$totalFormat); |
|
187
|
1 |
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|