|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Application\Service\Exporter; |
|
6
|
|
|
|
|
7
|
|
|
use Application\Model\TransactionLine; |
|
8
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; |
|
9
|
|
|
|
|
10
|
|
|
class TransactionLines extends AbstractExcel |
|
11
|
|
|
{ |
|
12
|
1 |
|
public function __construct(string $hostname) |
|
13
|
|
|
{ |
|
14
|
1 |
|
parent::__construct($hostname); |
|
15
|
|
|
|
|
16
|
1 |
|
$this->sheet->setTitle('Compta Écritures'); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
private int $currentTransactionRowStart = 0; |
|
20
|
|
|
|
|
21
|
|
|
private ?int $currentTransactionId = null; |
|
22
|
|
|
|
|
23
|
1 |
|
protected function getTitleForFilename(): string |
|
24
|
|
|
{ |
|
25
|
1 |
|
return 'compta_ecritures_' . date('Y-m-d-H:i:s'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
1 |
|
protected function getHeaders(): array |
|
29
|
|
|
{ |
|
30
|
1 |
|
return [ |
|
31
|
1 |
|
['label' => 'Date', 'width' => 12, 'formats' => [self::$headerFormat, self::$dateFormat], 'autofilter' => true], |
|
32
|
1 |
|
['label' => 'ID', 'width' => 7, 'formats' => [self::$headerFormat, self::$centerFormat], 'autofilter' => true], |
|
33
|
1 |
|
['label' => 'Transaction', 'width' => 25, 'formats' => [self::$headerFormat], 'autofilter' => true], |
|
34
|
1 |
|
['label' => 'Remarques', 'width' => 25, 'formats' => [self::$headerFormat], 'autofilter' => true], |
|
35
|
1 |
|
['label' => 'Remarques internes', 'width' => 25, 'formats' => [self::$headerFormat], 'autofilter' => true], |
|
36
|
1 |
|
['label' => 'Écriture', 'width' => 25, 'formats' => [self::$headerFormat], 'autofilter' => true], |
|
37
|
1 |
|
['label' => 'Remarques', 'width' => 25, 'formats' => [self::$headerFormat], 'autofilter' => true], |
|
38
|
1 |
|
['label' => 'Réservable', 'width' => 30, 'formats' => [self::$headerFormat], 'autofilter' => true], |
|
39
|
1 |
|
['label' => 'Compte débit', 'width' => 30, 'formats' => [self::$headerFormat], 'autofilter' => true], |
|
40
|
1 |
|
['label' => 'Compte crédit', 'width' => 30, 'formats' => [self::$headerFormat], 'autofilter' => true], |
|
41
|
1 |
|
['label' => 'Montant débit', 'width' => 20, 'formats' => [self::$headerFormat], 'autofilter' => true], |
|
42
|
1 |
|
['label' => 'Montant crédit', 'width' => 20, 'formats' => [self::$headerFormat], 'autofilter' => true], |
|
43
|
1 |
|
['label' => 'Pointé', 'width' => 12, 'formats' => [self::$headerFormat, self::$centerFormat], 'autofilter' => true], |
|
44
|
1 |
|
['label' => 'Tag', 'width' => 30, 'formats' => [self::$headerFormat], 'autofilter' => true], |
|
45
|
1 |
|
]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
protected function initialize(string $path): void |
|
49
|
|
|
{ |
|
50
|
1 |
|
parent::initialize($path); |
|
51
|
1 |
|
$this->writeHeaders($this->getHeaders()); |
|
52
|
1 |
|
$this->firstDataRow = ++$this->row; |
|
53
|
1 |
|
$this->column = $this->firstDataColumn; |
|
54
|
1 |
|
$this->sheet->setShowGridlines(false); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param TransactionLine $line |
|
59
|
|
|
*/ |
|
60
|
1 |
|
protected function writeItem($line): void |
|
61
|
|
|
{ |
|
62
|
1 |
|
$mergeRowsFromColumns = [ |
|
63
|
1 |
|
$this->firstDataColumn + 1, // Transaction.ID |
|
64
|
1 |
|
$this->firstDataColumn + 2, // Transaction.name |
|
65
|
1 |
|
$this->firstDataColumn + 3, // Transaction.remarks |
|
66
|
1 |
|
$this->firstDataColumn + 4, // Transaction.internalRemarks |
|
67
|
1 |
|
]; |
|
68
|
|
|
|
|
69
|
1 |
|
$transaction = $line->getTransaction(); |
|
70
|
1 |
|
$transactionId = $transaction->getId(); |
|
71
|
|
|
|
|
72
|
1 |
|
if (!$this->currentTransactionId) { |
|
|
|
|
|
|
73
|
1 |
|
$this->currentTransactionId = $transactionId; |
|
74
|
1 |
|
$this->currentTransactionRowStart = $this->row; |
|
75
|
1 |
|
} elseif ($transactionId !== $this->currentTransactionId) { |
|
76
|
|
|
// Current line starts a new transaction |
|
77
|
1 |
|
if ($this->currentTransactionRowStart < $this->row - 1) { |
|
78
|
|
|
// Multi-line transaction |
|
79
|
|
|
// Merge cells that have the same value because from the same transaction |
|
80
|
1 |
|
foreach ($mergeRowsFromColumns as $column) { |
|
81
|
1 |
|
$this->sheet->mergeCells([$column, $this->currentTransactionRowStart, $column, $this->row - 1]); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
1 |
|
$this->currentTransactionRowStart = $this->row; |
|
85
|
1 |
|
$this->currentTransactionId = $transactionId; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
// Date |
|
89
|
1 |
|
$this->write($line->getTransactionDate()->format('d.m.Y')); |
|
90
|
|
|
|
|
91
|
|
|
// Transaction.ID |
|
92
|
1 |
|
$this->write($transactionId); |
|
93
|
1 |
|
$url = 'https://' . $this->hostname . '/admin/transaction/%u'; |
|
94
|
1 |
|
$this->sheet->getCell([$this->column - 1, $this->row])->getHyperlink()->setUrl(sprintf($url, $transactionId)); |
|
95
|
|
|
// Transaction.name |
|
96
|
1 |
|
$this->write($transaction->getName()); |
|
97
|
|
|
// Transaction.remarks |
|
98
|
1 |
|
$this->write($transaction->getRemarks()); |
|
99
|
|
|
// Transaction.internalRemarks |
|
100
|
1 |
|
$this->write($transaction->getInternalRemarks()); |
|
101
|
|
|
|
|
102
|
|
|
// TransactionLine.name |
|
103
|
1 |
|
$this->write($line->getName()); |
|
104
|
|
|
|
|
105
|
|
|
// TransactionLine.remarks |
|
106
|
1 |
|
$this->write($line->getRemarks()); |
|
107
|
|
|
// Bookable.name |
|
108
|
1 |
|
$bookable = $line->getBookable(); |
|
109
|
1 |
|
$this->write($bookable ? $bookable->getName() : ''); |
|
110
|
|
|
// Debit account |
|
111
|
1 |
|
$debit = $line->getDebit(); |
|
112
|
1 |
|
$this->write($debit ? implode(' ', [$debit->getCode(), $debit->getName()]) : ''); |
|
113
|
|
|
// Credit account |
|
114
|
1 |
|
$credit = $line->getCredit(); |
|
115
|
1 |
|
$this->write($credit ? implode(' ', [$credit->getCode(), $credit->getName()]) : ''); |
|
116
|
|
|
// Debit amount |
|
117
|
1 |
|
$this->write($debit ? $this->moneyFormatter->format($line->getBalance()) : ''); |
|
118
|
|
|
// Credit amount |
|
119
|
1 |
|
$this->write($credit ? $this->moneyFormatter->format($line->getBalance()) : ''); |
|
120
|
|
|
// Reconciled |
|
121
|
1 |
|
$this->sheet->getStyle([$this->column, $this->row])->applyFromArray(self::$centerFormat); |
|
122
|
1 |
|
$this->write($line->isReconciled() ? '✔️' : ''); |
|
123
|
|
|
// Tag |
|
124
|
1 |
|
$tag = $line->getTransactionTag(); |
|
125
|
1 |
|
$this->write($tag ? $tag->getName() : ''); |
|
126
|
|
|
|
|
127
|
1 |
|
$this->lastDataColumn = $this->column - 1; |
|
128
|
|
|
|
|
129
|
1 |
|
$range = Coordinate::stringFromColumnIndex($this->firstDataColumn) . $this->row . ':' . Coordinate::stringFromColumnIndex($this->lastDataColumn) . $this->row; |
|
130
|
|
|
// Thicker horizontal delimiter between lines of different transactions |
|
131
|
1 |
|
$this->sheet->getStyle($range)->applyFromArray(self::$bordersBottomLight); |
|
132
|
|
|
|
|
133
|
1 |
|
$this->column = $this->firstDataColumn; |
|
134
|
1 |
|
++$this->row; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
1 |
|
protected function writeFooter(): void |
|
138
|
|
|
{ |
|
139
|
|
|
// Date |
|
140
|
1 |
|
$this->write(''); |
|
141
|
|
|
// ID |
|
142
|
1 |
|
$this->write(''); |
|
143
|
|
|
// Transaction.name |
|
144
|
1 |
|
$this->write(''); |
|
145
|
|
|
// Transaction.remarks |
|
146
|
1 |
|
$this->write(''); |
|
147
|
|
|
// Internal remarks |
|
148
|
1 |
|
$this->write(''); |
|
149
|
|
|
// TransactionLine.name |
|
150
|
1 |
|
$this->write(''); |
|
151
|
|
|
// Remarks |
|
152
|
1 |
|
$this->write(''); |
|
153
|
|
|
// Bookable.name |
|
154
|
1 |
|
$this->write(''); |
|
155
|
|
|
// Debit account |
|
156
|
1 |
|
$this->write(''); |
|
157
|
|
|
// Credit account |
|
158
|
1 |
|
$this->write(''); |
|
159
|
|
|
// Debit amount |
|
160
|
1 |
|
$this->write('=SUBTOTAL(9,' . Coordinate::stringFromColumnIndex($this->column) . '2:' . Coordinate::stringFromColumnIndex($this->column) . ($this->row - 1) . ')'); |
|
161
|
|
|
// Credit amount |
|
162
|
1 |
|
$this->write('=SUBTOTAL(9,' . Coordinate::stringFromColumnIndex($this->column) . '2:' . Coordinate::stringFromColumnIndex($this->column) . ($this->row - 1) . ')'); |
|
163
|
|
|
// Reconciled |
|
164
|
1 |
|
$this->write(''); |
|
165
|
|
|
|
|
166
|
|
|
// Apply style |
|
167
|
1 |
|
$range = Coordinate::stringFromColumnIndex($this->firstDataColumn) . $this->row . ':' . Coordinate::stringFromColumnIndex($this->column - 1) . $this->row; |
|
168
|
1 |
|
$this->sheet->getStyle($range)->applyFromArray(self::$totalFormat); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: