|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Application\Service\Exporter; |
|
6
|
|
|
|
|
7
|
|
|
use Application\Enum\AccountType; |
|
8
|
|
|
use Application\Model\Account; |
|
9
|
|
|
use Cake\Chronos\ChronosDate; |
|
10
|
|
|
use Ecodev\Felix\Api\Exception; |
|
11
|
|
|
use Ecodev\Felix\Format; |
|
12
|
|
|
use Money\Money; |
|
13
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; |
|
14
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Alignment; |
|
15
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Color; |
|
16
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Fill; |
|
17
|
|
|
use PhpOffice\PhpSpreadsheet\Style\NumberFormat; |
|
18
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @extends AbstractExcel<Account> |
|
22
|
|
|
* |
|
23
|
|
|
* @phpstan-type Data array{ |
|
24
|
|
|
* account: ?Account, |
|
25
|
|
|
* code: int | '', |
|
26
|
|
|
* name: string, |
|
27
|
|
|
* depth: int, |
|
28
|
|
|
* balance: Money, |
|
29
|
|
|
* balancePrevious: ?Money, |
|
30
|
|
|
* budgetAllowed: ?Money, |
|
31
|
|
|
* budgetBalance: ?Money, |
|
32
|
|
|
* format?: array, |
|
33
|
|
|
* formatPrevious?: array, |
|
34
|
|
|
* } |
|
35
|
|
|
*/ |
|
36
|
|
|
class AccountingReport extends AbstractExcel |
|
37
|
|
|
{ |
|
38
|
|
|
private ChronosDate $date; |
|
39
|
|
|
|
|
40
|
|
|
private bool $showBudget = false; |
|
41
|
|
|
|
|
42
|
|
|
private ?ChronosDate $datePrevious = null; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var Data[] |
|
46
|
|
|
*/ |
|
47
|
|
|
private array $assets = []; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var Data[] |
|
51
|
|
|
*/ |
|
52
|
|
|
private array $liabilities = []; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var Data[] |
|
56
|
|
|
*/ |
|
57
|
|
|
private array $expenses = []; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var Data[] |
|
61
|
|
|
*/ |
|
62
|
|
|
private array $revenues = []; |
|
63
|
|
|
|
|
64
|
|
|
private static array $balanceFormat = [ |
|
65
|
|
|
'fill' => [ |
|
66
|
|
|
'fillType' => Fill::FILL_SOLID, |
|
67
|
|
|
'startColor' => [ |
|
68
|
|
|
'argb' => 'FFDDDDDD', |
|
69
|
|
|
], |
|
70
|
|
|
], |
|
71
|
|
|
'numberFormat' => [ |
|
72
|
|
|
'formatCode' => NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1, // eg. 12'345.67 |
|
73
|
|
|
], |
|
74
|
|
|
]; |
|
75
|
|
|
|
|
76
|
|
|
protected static array $ctrlFormat = [ |
|
77
|
|
|
'font' => [ |
|
78
|
|
|
'color' => [ |
|
79
|
|
|
'bold' => false, |
|
80
|
|
|
'argb' => 'FF606060', |
|
81
|
|
|
], |
|
82
|
|
|
], |
|
83
|
|
|
'alignment' => ['wrapText' => true], |
|
84
|
|
|
'fill' => [ |
|
85
|
|
|
'fillType' => Fill::FILL_SOLID, |
|
86
|
|
|
'startColor' => [ |
|
87
|
|
|
'argb' => 'FFDDDDDD', |
|
88
|
|
|
], |
|
89
|
|
|
], |
|
90
|
|
|
]; |
|
91
|
|
|
|
|
92
|
|
|
private static array $wrappedFormat = [ |
|
93
|
|
|
'alignment' => [ |
|
94
|
|
|
'wrapText' => true, |
|
95
|
|
|
], |
|
96
|
|
|
]; |
|
97
|
|
|
|
|
98
|
|
|
private static array $indentFormat = [ |
|
99
|
|
|
'alignment' => [ |
|
100
|
|
|
'horizontal' => Alignment::HORIZONTAL_LEFT, |
|
101
|
|
|
'indent' => 1, |
|
102
|
|
|
], |
|
103
|
|
|
]; |
|
104
|
|
|
|
|
105
|
|
|
private static array $columnWidth = [ |
|
106
|
|
|
'accountCode' => 14, |
|
107
|
|
|
'accountName' => 38, |
|
108
|
|
|
'balance' => 12, |
|
109
|
|
|
]; |
|
110
|
|
|
|
|
111
|
1 |
|
public function __construct( |
|
112
|
|
|
string $hostname, |
|
113
|
|
|
private readonly array $accountingConfig, |
|
114
|
|
|
) { |
|
115
|
1 |
|
parent::__construct($hostname); |
|
116
|
|
|
|
|
117
|
1 |
|
$this->zebra = false; |
|
118
|
1 |
|
$this->autoFilter = false; |
|
119
|
1 |
|
$this->date = ChronosDate::today(); |
|
120
|
|
|
|
|
121
|
1 |
|
$this->sheet->setTitle(_tr('Bilan') . ' & ' . _tr('Résultat')); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
1 |
|
protected function getTitleForFilename(): string |
|
125
|
|
|
{ |
|
126
|
1 |
|
return _tr('compta_rapport_%date%', ['date' => $this->date->format('Y-m-d')]); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
1 |
|
public function setDate(ChronosDate $date): void |
|
130
|
|
|
{ |
|
131
|
1 |
|
$this->date = $date; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
1 |
|
public function setDatePrevious(?ChronosDate $datePrevious): void |
|
135
|
|
|
{ |
|
136
|
1 |
|
$this->datePrevious = $datePrevious; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
1 |
|
public function showBudget(bool $showBudget): void |
|
140
|
|
|
{ |
|
141
|
1 |
|
$this->showBudget = $showBudget; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
1 |
|
protected function writeTitle(): void |
|
145
|
|
|
{ |
|
146
|
1 |
|
$this->column = 1; |
|
147
|
1 |
|
$this->sheet->mergeCells([$this->column, $this->row, $this->column + $this->getColspan(), $this->row]); |
|
148
|
1 |
|
$this->write( |
|
149
|
1 |
|
sprintf($this->hostname . ': rapport comptable au %s', $this->date->format('d.m.Y')), |
|
150
|
1 |
|
self::$titleFormat, |
|
151
|
1 |
|
self::$centerFormat, |
|
152
|
1 |
|
); |
|
153
|
1 |
|
$this->sheet->getRowDimension($this->row)->setRowHeight(35); |
|
154
|
1 |
|
++$this->row; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
1 |
|
private function processAccount(Account $account, int $depth): void |
|
158
|
|
|
{ |
|
159
|
1 |
|
$balance = $account->getBalanceAtDate($this->date); |
|
160
|
1 |
|
$balancePrevious = $this->datePrevious ? $account->getBalanceAtDate($this->datePrevious) : null; |
|
161
|
|
|
// Skip the account if: |
|
162
|
|
|
// - accounting is configured to hide accounts with zero balance |
|
163
|
|
|
// - AND account at report date has zero balance |
|
164
|
|
|
// - AND account at previous date has zero balance (but only if "show previous year" mode is enabled) |
|
165
|
1 |
|
if ($this->accountingConfig['report']['showAccountsWithZeroBalance'] === false && $depth > 1 && $balance->isZero() && (!$this->datePrevious || ($balancePrevious && $balancePrevious->isZero()))) { |
|
166
|
|
|
return; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
1 |
|
if ($account->getType() === AccountType::Equity) { |
|
170
|
|
|
// Don't show special accounts since it's an interim statement, their balance will be computed manually |
|
171
|
1 |
|
return; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
1 |
|
$data = [ |
|
175
|
1 |
|
'code' => $account->getCode(), |
|
176
|
1 |
|
'name' => Format::truncate($account->getName(), 55), |
|
177
|
1 |
|
'depth' => $depth, |
|
178
|
1 |
|
'balance' => $balance, |
|
179
|
1 |
|
'balancePrevious' => $balancePrevious, |
|
180
|
1 |
|
'budgetAllowed' => $this->showBudget ? $account->getBudgetAllowed() : null, |
|
181
|
1 |
|
'budgetBalance' => $this->showBudget ? $account->getBudgetBalance() : null, |
|
182
|
1 |
|
'account' => $account, |
|
183
|
1 |
|
]; |
|
184
|
|
|
|
|
185
|
1 |
|
if ($this->isAccountType($account, AccountType::Asset)) { |
|
186
|
1 |
|
$this->assets[] = $data; |
|
187
|
1 |
|
} elseif ($this->isAccountType($account, AccountType::Liability)) { |
|
188
|
1 |
|
$this->liabilities[] = $data; |
|
189
|
1 |
|
} elseif ($this->isAccountType($account, AccountType::Revenue)) { |
|
190
|
1 |
|
$this->revenues[] = $data; |
|
191
|
1 |
|
} elseif ($this->isAccountType($account, AccountType::Expense)) { |
|
192
|
1 |
|
$this->expenses[] = $data; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
1 |
|
if ($account->getType() === AccountType::Group && $depth <= $this->accountingConfig['report']['maxAccountDepth']) { |
|
196
|
1 |
|
foreach ($account->getChildren() as $child) { |
|
197
|
1 |
|
$this->processAccount($child, $depth + 1); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @param Account $item |
|
204
|
|
|
*/ |
|
205
|
1 |
|
protected function writeItem($item): void |
|
206
|
|
|
{ |
|
207
|
|
|
// This is unusual because we don't write anything but only collect data for later |
|
208
|
1 |
|
$this->processAccount($item, 1); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Compute the profit or loss (at current and previous dates) and insert the result into the list of accounts. |
|
213
|
|
|
*/ |
|
214
|
1 |
|
private function insertProfitOrLoss(): void |
|
215
|
|
|
{ |
|
216
|
1 |
|
$profitOrLoss = $this->getProfitOrLoss(false); |
|
217
|
|
|
|
|
218
|
1 |
|
if ($profitOrLoss->isZero()) { |
|
219
|
|
|
return; // If financial result is balanced, it likely a final accounting report so we don't show the intermediate result |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
1 |
|
$data = [ |
|
223
|
1 |
|
'depth' => 1, |
|
224
|
1 |
|
'code' => '', |
|
225
|
1 |
|
'name' => _tr('Résultat intermédiaire (bénéfice / -perte)'), |
|
226
|
1 |
|
'account' => null, |
|
227
|
1 |
|
'balance' => $profitOrLoss, // can be positive of negative |
|
228
|
1 |
|
'balancePrevious' => null, |
|
229
|
1 |
|
'budgetAllowed' => null, |
|
230
|
1 |
|
'budgetBalance' => null, |
|
231
|
1 |
|
'format' => $this->color($profitOrLoss), |
|
232
|
1 |
|
]; |
|
233
|
|
|
|
|
234
|
1 |
|
if ($this->datePrevious) { |
|
235
|
1 |
|
$profitOrLossPrevious = $this->getProfitOrLoss(true); |
|
236
|
1 |
|
$data['balancePrevious'] = $profitOrLossPrevious; |
|
237
|
1 |
|
$data['formatPrevious'] = $this->color($profitOrLossPrevious); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
// A profit is reported as a POSITIVE green number, and a loss is reported a NEGATIVE red number. |
|
241
|
|
|
// They are identical lines at the end of both LIABILITIES and EXPENSES columns |
|
242
|
1 |
|
$this->liabilities[] = $data; |
|
243
|
1 |
|
$this->expenses[] = $data; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* @param Data $data |
|
248
|
|
|
*/ |
|
249
|
1 |
|
private function maybeBold(array $data, int $untilDepth): array |
|
250
|
|
|
{ |
|
251
|
1 |
|
return $data['depth'] <= $untilDepth ? ['font' => ['bold' => true]] : []; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
1 |
|
private function realWrite(): void |
|
255
|
|
|
{ |
|
256
|
1 |
|
$this->insertProfitOrLoss(); |
|
257
|
|
|
|
|
258
|
|
|
/* |
|
259
|
|
|
* Page 1 |
|
260
|
|
|
* BALANCE SHEET (Asset vs Liabilities) |
|
261
|
|
|
*/ |
|
262
|
|
|
|
|
263
|
1 |
|
$this->lastDataColumn = 1 + $this->getColspan(); |
|
264
|
1 |
|
$this->column = $initialColumn = 1; |
|
265
|
|
|
|
|
266
|
1 |
|
$this->balanceSheetHeaders($initialColumn); |
|
267
|
|
|
|
|
268
|
|
|
// Assets |
|
269
|
1 |
|
$this->column = $initialColumn; |
|
270
|
1 |
|
$initialRow = $this->row; |
|
271
|
1 |
|
$this->lastDataRow = $this->row; |
|
272
|
1 |
|
$this->balanceSheet($initialColumn, $this->assets); |
|
273
|
|
|
|
|
274
|
|
|
// Liabilities |
|
275
|
1 |
|
$this->row = $initialRow; |
|
276
|
1 |
|
$this->column = $initialColumn = $initialColumn + $this->getColspan() / 2 + 1; |
|
277
|
1 |
|
$this->balanceSheet($initialColumn, $this->liabilities); |
|
278
|
|
|
|
|
279
|
1 |
|
$this->row = $this->lastDataRow + 1; |
|
280
|
1 |
|
$this->writeTotals($this->assets, $this->liabilities); |
|
281
|
|
|
|
|
282
|
1 |
|
$this->applyExtraFormatting(5); |
|
283
|
|
|
// set printing area for page 1 |
|
284
|
1 |
|
$this->sheet->getPageSetup()->setPrintAreaByColumnAndRow(1, 1, $this->lastDataColumn, $this->lastDataRow, 0, 'I'); |
|
285
|
|
|
|
|
286
|
|
|
/* |
|
287
|
|
|
* Page 2 |
|
288
|
|
|
* INCOME STATEMENT (Profit vs Loss) |
|
289
|
|
|
*/ |
|
290
|
|
|
|
|
291
|
|
|
// start at the bottom of the balance sheet |
|
292
|
1 |
|
$this->row = $this->lastDataRow + 1; |
|
293
|
1 |
|
$this->column = $initialColumn = 1; |
|
294
|
1 |
|
$this->incomeStatementHeaders($initialColumn); |
|
295
|
|
|
|
|
296
|
|
|
// Expenses |
|
297
|
1 |
|
$initialRow = ++$this->row; |
|
298
|
1 |
|
$this->column = $initialColumn; |
|
299
|
1 |
|
$this->incomeStatement($initialColumn, $this->expenses); |
|
300
|
|
|
|
|
301
|
|
|
// Revenues |
|
302
|
1 |
|
$this->row = $initialRow; |
|
303
|
1 |
|
$this->column = $initialColumn = $initialColumn + $this->getColspan() / 2 + 1; |
|
304
|
1 |
|
$this->incomeStatement($initialColumn, $this->revenues); |
|
305
|
|
|
|
|
306
|
1 |
|
$this->row = $this->lastDataRow + 1; |
|
307
|
1 |
|
$this->writeTotals($this->expenses, $this->revenues); |
|
308
|
|
|
|
|
309
|
1 |
|
$this->applyExtraFormatting($initialRow); |
|
310
|
|
|
// set printing area for page 2 |
|
311
|
1 |
|
$this->sheet->getPageSetup()->setPrintAreaByColumnAndRow(1, $initialRow - 3, $this->lastDataColumn, $this->lastDataRow + 1, 1, 'I'); |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
1 |
|
private function getProfitOrLoss(bool $isPreviousDate): Money |
|
315
|
|
|
{ |
|
316
|
|
|
// Sum the profit and loss root accounts |
|
317
|
1 |
|
$totalRevenues = $this->sumBalance($this->revenues, $isPreviousDate); |
|
318
|
|
|
|
|
319
|
1 |
|
$totalExpenses = $this->sumBalance($this->expenses, $isPreviousDate); |
|
320
|
|
|
|
|
321
|
1 |
|
return $totalRevenues->subtract($totalExpenses); |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
1 |
|
protected function finalize(string $path): void |
|
325
|
|
|
{ |
|
326
|
|
|
// Once we collected all data, we can actually write them all |
|
327
|
1 |
|
$this->realWrite(); |
|
328
|
|
|
|
|
329
|
|
|
// Print on A4 portrait, scale to full page width, variable height (depending on number of accounts) |
|
330
|
1 |
|
$pageSetup = $this->sheet->getPageSetup(); |
|
331
|
1 |
|
$pageSetup->setOrientation(PageSetup::ORIENTATION_PORTRAIT); |
|
332
|
1 |
|
$pageSetup->setPaperSize(PageSetup::PAPERSIZE_A4); |
|
333
|
1 |
|
$pageSetup->setFitToWidth(1); |
|
334
|
1 |
|
$pageSetup->setFitToHeight(0); |
|
335
|
1 |
|
$pageSetup->setHorizontalCentered(true); |
|
336
|
1 |
|
$margins = $this->sheet->getPageMargins(); |
|
337
|
1 |
|
$margins->setTop(0.5); |
|
338
|
1 |
|
$margins->setRight(0.2); |
|
339
|
1 |
|
$margins->setLeft(0.2); |
|
340
|
1 |
|
$margins->setBottom(0.5); |
|
341
|
|
|
|
|
342
|
1 |
|
parent::finalize($path); |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
// Insert a row with the control totals |
|
346
|
1 |
|
protected function writeTotals(array $accountsColumn1, array $accountsColumn2): void |
|
347
|
|
|
{ |
|
348
|
1 |
|
$this->column = $this->firstDataColumn; |
|
349
|
1 |
|
foreach ([$accountsColumn1, $accountsColumn2] as $col => $accounts) { |
|
350
|
|
|
// Account.code |
|
351
|
1 |
|
$this->write(mb_strtoupper(_tr('Contrôle'))); |
|
352
|
|
|
// Account.name |
|
353
|
1 |
|
$this->write(''); |
|
354
|
|
|
// Account.balance |
|
355
|
1 |
|
$cellsToSum = $this->cellsToSum($accounts, 1); |
|
356
|
1 |
|
$this->write($cellsToSum ? '=SUM(' . implode(',', $cellsToSum) . ')' : '', self::$balanceFormat, self::$totalFormat); |
|
357
|
|
|
// Account previous balance (optional) |
|
358
|
1 |
|
if ($this->datePrevious) { |
|
359
|
1 |
|
$cellsToSum = $this->cellsToSum($accounts, 1, 'cellPrevious'); |
|
360
|
1 |
|
$this->write($cellsToSum ? '=SUM(' . implode(',', $cellsToSum) . ')' : '', self::$balanceFormat, self::$totalFormat); |
|
361
|
|
|
} |
|
362
|
|
|
// Budget columns (optional) |
|
363
|
1 |
|
if ($this->showBudget) { |
|
364
|
1 |
|
$cellsToSum = $this->cellsToSum($accounts, 1, 'cellBudgetAllowed'); |
|
365
|
1 |
|
$this->write($cellsToSum ? '=SUM(' . implode(',', $cellsToSum) . ')' : '', self::$balanceFormat, self::$totalFormat); |
|
366
|
1 |
|
$cellsToSum = $this->cellsToSum($accounts, 1, 'cellBudgetBalance'); |
|
367
|
1 |
|
$this->write($cellsToSum ? '=SUM(' . implode(',', $cellsToSum) . ')' : '', self::$balanceFormat, self::$totalFormat); |
|
368
|
|
|
} |
|
369
|
1 |
|
if ($col === 0) { |
|
370
|
|
|
// Inner columns gap |
|
371
|
1 |
|
$this->write(''); |
|
372
|
|
|
} |
|
373
|
|
|
} |
|
374
|
1 |
|
$this->lastDataRow = $this->row; |
|
375
|
|
|
// Apply style |
|
376
|
1 |
|
$range = Coordinate::stringFromColumnIndex($this->firstDataColumn) . $this->row . ':' . Coordinate::stringFromColumnIndex($this->column - 1) . $this->row; |
|
377
|
1 |
|
$this->sheet->getStyle($range)->applyFromArray(self::$ctrlFormat); |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
1 |
|
private function applyExtraFormatting(int $startRow): void |
|
381
|
|
|
{ |
|
382
|
1 |
|
if ($this->datePrevious && !$this->showBudget) { |
|
383
|
|
|
$columnsToFormat = [3, 4, 8, 9]; |
|
384
|
1 |
|
} elseif (!$this->datePrevious && $this->showBudget) { |
|
385
|
|
|
$columnsToFormat = [3, 4, 5, 9, 10, 11]; |
|
386
|
1 |
|
} elseif ($this->datePrevious && $this->showBudget) { |
|
387
|
1 |
|
$columnsToFormat = [3, 4, 5, 6, 10, 11, 12, 13]; |
|
388
|
|
|
} else { |
|
389
|
|
|
// Only current balance cols |
|
390
|
1 |
|
$columnsToFormat = [3, 7]; |
|
391
|
|
|
} |
|
392
|
1 |
|
foreach ($columnsToFormat as $colIndex) { |
|
393
|
|
|
// Format balance numbers |
|
394
|
1 |
|
$range = Coordinate::stringFromColumnIndex($colIndex) . $startRow . ':' . Coordinate::stringFromColumnIndex($colIndex) . $this->lastDataRow; |
|
395
|
1 |
|
$this->sheet->getStyle($range)->applyFromArray(self::$balanceFormat); |
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
|
|
// Increase row height since account names can wrap on multiple lines |
|
399
|
1 |
|
for ($r = $startRow; $r <= $this->lastDataRow; ++$r) { |
|
400
|
1 |
|
$this->sheet->getRowDimension($r)->setRowHeight(30); |
|
401
|
|
|
} |
|
402
|
|
|
} |
|
403
|
|
|
|
|
404
|
1 |
|
private function cellsToSum(array $data, int $depth, string $dataIndex = 'cell'): array |
|
405
|
|
|
{ |
|
406
|
1 |
|
$equityAccountsClasses = $this->accountingConfig['report']['accountClasses']['equity']; |
|
407
|
1 |
|
$cells = array_reduce($data, function (array $carry, $data) use ($equityAccountsClasses, $depth, $dataIndex) { |
|
408
|
|
|
// We only sum accounts at the given depth, plus equity special accounts |
|
409
|
1 |
|
if (isset($data[$dataIndex]) && ($data['depth'] === $depth || in_array(mb_substr((string) $data['code'], 0, 1), $equityAccountsClasses, true))) { |
|
410
|
1 |
|
$carry[] = $data[$dataIndex]; |
|
411
|
|
|
} |
|
412
|
|
|
|
|
413
|
1 |
|
return $carry; |
|
414
|
1 |
|
}, []); |
|
415
|
|
|
|
|
416
|
1 |
|
return $cells; |
|
417
|
|
|
} |
|
418
|
|
|
|
|
419
|
|
|
/** |
|
420
|
|
|
* Sum root or special accounts balance (for the profit and loss calculation) |
|
421
|
|
|
* - Root accounts have depth = 1 |
|
422
|
|
|
* - Special accounts have code 7xxx, 8xxx, 9xxx. |
|
423
|
|
|
* |
|
424
|
|
|
* @param Data[] $data profits or expenses |
|
425
|
|
|
*/ |
|
426
|
1 |
|
private function sumBalance(array $data, bool $isPreviousDate): Money |
|
427
|
|
|
{ |
|
428
|
1 |
|
$sum = array_reduce($data, function (Money $carry, $data) use ($isPreviousDate) { |
|
429
|
1 |
|
if ($data['depth'] === 1 || (int) mb_substr((string) $data['code'], 0, 1) > 6) { |
|
430
|
1 |
|
return $carry->add($isPreviousDate ? $data['balancePrevious'] : $data['balance']); |
|
431
|
|
|
} |
|
432
|
|
|
|
|
433
|
1 |
|
return $carry; |
|
434
|
1 |
|
}, Money::CHF(0)); |
|
435
|
|
|
|
|
436
|
1 |
|
return $sum; |
|
437
|
|
|
} |
|
438
|
|
|
|
|
439
|
1 |
|
private function balanceSheetHeaders(int $initialColumn): void |
|
440
|
|
|
{ |
|
441
|
1 |
|
$this->sheet->mergeCells([$this->column, $this->row, $this->column + $this->getColspan(), $this->row]); |
|
442
|
1 |
|
$this->write( |
|
443
|
1 |
|
_tr('Bilan'), |
|
444
|
1 |
|
self::$titleFormat, |
|
445
|
1 |
|
self::$centerFormat, |
|
446
|
1 |
|
); |
|
447
|
1 |
|
$this->sheet->getRowDimension($this->row)->setRowHeight(40); |
|
448
|
1 |
|
++$this->row; |
|
449
|
|
|
|
|
450
|
|
|
// Header line 1 |
|
451
|
1 |
|
$headers = [ |
|
452
|
1 |
|
['label' => _tr('Actifs'), 'formats' => [self::$headerFormat, self::$centerFormat], 'colspan' => $this->getColspan() / 2], |
|
453
|
1 |
|
['label' => '', 'width' => 3, 'formats' => []], // gap |
|
454
|
1 |
|
['label' => _tr('Passifs'), 'formats' => [self::$headerFormat, self::$centerFormat], 'colspan' => $this->getColspan() / 2], |
|
455
|
1 |
|
]; |
|
456
|
1 |
|
$this->column = $initialColumn; |
|
457
|
1 |
|
$this->writeHeaders($headers); |
|
458
|
1 |
|
++$this->row; |
|
459
|
|
|
|
|
460
|
|
|
// Header line 2: date(s) of balance |
|
461
|
1 |
|
$headers = [ |
|
462
|
1 |
|
['label' => '', 'colspan' => 2], // empty margin |
|
463
|
1 |
|
['label' => $this->date->format('d.m.Y'), 'formats' => [self::$headerFormat, self::$centerFormat]], // current date |
|
464
|
1 |
|
]; |
|
465
|
1 |
|
if ($this->datePrevious) { |
|
466
|
1 |
|
$headers[] = ['label' => $this->datePrevious->format('d.m.Y'), 'formats' => [self::$headerFormat, self::$centerFormat]]; // previous date |
|
467
|
|
|
} |
|
468
|
1 |
|
if ($this->showBudget) { |
|
469
|
1 |
|
$headers[] = ['label' => _tr('Budget prévu'), 'formats' => [self::$headerFormat, self::$centerFormat]]; |
|
470
|
1 |
|
$headers[] = ['label' => _tr('Budget restant'), 'formats' => [self::$headerFormat, self::$centerFormat]]; |
|
471
|
|
|
} |
|
472
|
|
|
|
|
473
|
1 |
|
$headers[] = ['label' => '', 'formats' => []]; // gap |
|
474
|
1 |
|
$headers[] = ['label' => '', 'colspan' => 2]; // empty margin |
|
475
|
1 |
|
$headers[] = ['label' => $this->date->format('d.m.Y'), 'formats' => [self::$headerFormat, self::$centerFormat]]; // current date |
|
476
|
1 |
|
if ($this->datePrevious) { |
|
477
|
1 |
|
$headers[] = ['label' => $this->datePrevious->format('d.m.Y'), 'formats' => [self::$headerFormat, self::$centerFormat]]; // previous date |
|
478
|
|
|
} |
|
479
|
1 |
|
if ($this->showBudget) { |
|
480
|
1 |
|
$headers[] = ['label' => _tr('Budget prévu'), 'formats' => [self::$headerFormat, self::$centerFormat]]; |
|
481
|
1 |
|
$headers[] = ['label' => _tr('Budget restant'), 'formats' => [self::$headerFormat, self::$centerFormat]]; |
|
482
|
|
|
} |
|
483
|
|
|
|
|
484
|
1 |
|
$this->column = $initialColumn; |
|
485
|
1 |
|
$this->writeHeaders($headers); |
|
486
|
1 |
|
++$this->row; |
|
487
|
|
|
} |
|
488
|
|
|
|
|
489
|
|
|
/** |
|
490
|
|
|
* @param Data[] $allData |
|
491
|
|
|
*/ |
|
492
|
1 |
|
private function balanceSheet(int $initialColumn, array &$allData): void |
|
493
|
|
|
{ |
|
494
|
|
|
// Store coordinates (ie. E3) of the 2nd level account budget cells to later use in formula |
|
495
|
1 |
|
$budgetAllowedTotalCells = ''; |
|
496
|
1 |
|
$budgetBalanceTotalCells = ''; |
|
497
|
|
|
|
|
498
|
1 |
|
$firstLine = true; |
|
499
|
|
|
|
|
500
|
1 |
|
foreach ($allData as $index => $data) { |
|
501
|
|
|
// Column: account code |
|
502
|
1 |
|
if ($firstLine) { |
|
503
|
1 |
|
$this->sheet->getColumnDimensionByColumn($this->column)->setWidth(self::$columnWidth['accountCode']); |
|
504
|
|
|
} |
|
505
|
1 |
|
$maybeBold = $this->maybeBold($data, 2); |
|
506
|
1 |
|
if (!$firstLine && $maybeBold) { |
|
507
|
1 |
|
++$this->row; |
|
508
|
|
|
} |
|
509
|
|
|
|
|
510
|
1 |
|
$this->write( |
|
511
|
1 |
|
str_repeat(' ', $data['depth'] - 1) . $data['code'], |
|
512
|
1 |
|
self::$indentFormat, |
|
513
|
1 |
|
$maybeBold, |
|
514
|
1 |
|
); |
|
515
|
|
|
|
|
516
|
|
|
// Column: account name |
|
517
|
1 |
|
if ($firstLine) { |
|
518
|
1 |
|
$this->sheet->getColumnDimensionByColumn($this->column)->setWidth(self::$columnWidth['accountName']); |
|
519
|
|
|
} |
|
520
|
1 |
|
$this->write($data['name'], self::$wrappedFormat, $maybeBold); |
|
521
|
|
|
|
|
522
|
|
|
// Column: balance at date |
|
523
|
1 |
|
if ($firstLine) { |
|
524
|
1 |
|
$this->sheet->getColumnDimensionByColumn($this->column)->setWidth(self::$columnWidth['balance']); |
|
525
|
|
|
} |
|
526
|
|
|
// Store the coordinate of the cell to later compute totals |
|
527
|
1 |
|
$allData[$index]['cell'] = $this->sheet->getCell([$this->column, $this->row])->getCoordinate(); |
|
528
|
1 |
|
$this->write($data['balance'], self::$balanceFormat, $maybeBold, $data['format'] ?? []); |
|
529
|
|
|
|
|
530
|
|
|
// Column: balance at previous date (optional) |
|
531
|
1 |
|
if ($this->datePrevious) { |
|
532
|
1 |
|
$allData[$index]['cellPrevious'] = $this->sheet->getCell([$this->column, $this->row])->getCoordinate(); |
|
533
|
1 |
|
if ($firstLine) { |
|
534
|
1 |
|
$this->sheet->getColumnDimensionByColumn($this->column)->setWidth(self::$columnWidth['balance']); |
|
535
|
|
|
} |
|
536
|
1 |
|
$this->write($data['balancePrevious'], $maybeBold, $data['formatPrevious'] ?? []); |
|
537
|
|
|
} |
|
538
|
|
|
|
|
539
|
|
|
// Budget columns (optional) |
|
540
|
1 |
|
if ($this->showBudget) { |
|
541
|
1 |
|
$allData[$index]['cellBudgetAllowed'] = $this->sheet->getCell([$this->column, $this->row])->getCoordinate(); |
|
542
|
1 |
|
if ($firstLine) { |
|
543
|
1 |
|
$this->sheet->getColumnDimensionByColumn($this->column)->setWidth(self::$columnWidth['balance']); |
|
544
|
1 |
|
$budgetAllowedTotalCells = Coordinate::stringFromColumnIndex($this->column) . $this->row; |
|
545
|
|
|
} |
|
546
|
1 |
|
$this->write($data['budgetAllowed'] ?? '', self::$balanceFormat, $maybeBold); |
|
547
|
|
|
|
|
548
|
1 |
|
$allData[$index]['cellBudgetBalance'] = $this->sheet->getCell([$this->column, $this->row])->getCoordinate(); |
|
549
|
1 |
|
if ($firstLine) { |
|
550
|
1 |
|
$this->sheet->getColumnDimensionByColumn($this->column)->setWidth(self::$columnWidth['balance']); |
|
551
|
1 |
|
$budgetBalanceTotalCells = Coordinate::stringFromColumnIndex($this->column) . $this->row; |
|
552
|
|
|
} |
|
553
|
1 |
|
$this->write($data['budgetBalance'] ?? '', self::$balanceFormat, $maybeBold); |
|
554
|
|
|
} |
|
555
|
|
|
|
|
556
|
1 |
|
$firstLine = false; |
|
557
|
1 |
|
++$this->row; |
|
558
|
1 |
|
$this->column = $initialColumn; |
|
559
|
|
|
|
|
560
|
1 |
|
$this->lastDataRow = max($this->lastDataRow, $this->row); |
|
561
|
|
|
} |
|
562
|
|
|
|
|
563
|
|
|
// Compute the sum of budgets columns using an Excel formula |
|
564
|
|
|
// We don't do it for account balance, since the total of group accounts is computed in DB |
|
565
|
|
|
// Level 2 (= direct child accounts) |
|
566
|
1 |
|
if ($this->showBudget) { |
|
567
|
1 |
|
$cellsToSum = $this->cellsToSum($allData, 2, 'cellBudgetAllowed'); |
|
568
|
1 |
|
if ($cellsToSum) { |
|
569
|
1 |
|
$this->sheet->setCellValue($budgetAllowedTotalCells, '=SUM(' . implode(',', $cellsToSum) . ')'); |
|
570
|
|
|
} |
|
571
|
1 |
|
$cellsToSum = $this->cellsToSum($allData, 2, 'cellBudgetBalance'); |
|
572
|
1 |
|
if ($cellsToSum) { |
|
573
|
1 |
|
$this->sheet->setCellValue($budgetBalanceTotalCells, '=SUM(' . implode(',', $cellsToSum) . ')'); |
|
574
|
|
|
} |
|
575
|
|
|
} |
|
576
|
|
|
} |
|
577
|
|
|
|
|
578
|
1 |
|
private function getColspan(): int |
|
579
|
|
|
{ |
|
580
|
1 |
|
$colspan = 6; |
|
581
|
1 |
|
if ($this->datePrevious) { |
|
582
|
1 |
|
$colspan += 2; |
|
583
|
|
|
} |
|
584
|
1 |
|
if ($this->showBudget) { |
|
585
|
1 |
|
$colspan += 4; |
|
586
|
|
|
} |
|
587
|
|
|
|
|
588
|
1 |
|
return $colspan; |
|
589
|
|
|
} |
|
590
|
|
|
|
|
591
|
1 |
|
private function incomeStatementHeaders(int $initialColumn): void |
|
592
|
|
|
{ |
|
593
|
1 |
|
$this->sheet->mergeCells([$this->column, $this->row, $this->column + $this->getColspan(), $this->row]); |
|
594
|
1 |
|
$this->write( |
|
595
|
1 |
|
_tr('Compte de résultat'), |
|
596
|
1 |
|
self::$titleFormat, |
|
597
|
1 |
|
self::$centerFormat, |
|
598
|
1 |
|
); |
|
599
|
1 |
|
$this->sheet->getRowDimension($this->row)->setRowHeight(40); |
|
600
|
1 |
|
++$this->row; |
|
601
|
|
|
|
|
602
|
|
|
// Header line 1 |
|
603
|
1 |
|
$headers = [ |
|
604
|
1 |
|
['label' => _tr('Charges'), 'formats' => [self::$headerFormat, self::$centerFormat], 'colspan' => $this->getColspan() / 2], |
|
605
|
1 |
|
['label' => '', 'width' => 3, 'formats' => []], // gap |
|
606
|
1 |
|
['label' => _tr('Profits'), 'formats' => [self::$headerFormat, self::$centerFormat], 'colspan' => $this->getColspan() / 2], |
|
607
|
1 |
|
]; |
|
608
|
1 |
|
$this->column = $initialColumn; |
|
609
|
1 |
|
$this->writeHeaders($headers); |
|
610
|
1 |
|
++$this->row; |
|
611
|
|
|
|
|
612
|
|
|
// Header line 2: date(s) of balance |
|
613
|
1 |
|
$headers = [ |
|
614
|
1 |
|
['label' => '', 'colspan' => 2], // empty margin |
|
615
|
1 |
|
['label' => $this->date->format('d.m.Y'), 'formats' => [self::$headerFormat, self::$centerFormat]], // current date |
|
616
|
1 |
|
]; |
|
617
|
1 |
|
if ($this->datePrevious) { |
|
618
|
1 |
|
$headers[] = ['label' => $this->datePrevious->format('d.m.Y'), 'formats' => [self::$headerFormat, self::$centerFormat]]; // previous date |
|
619
|
|
|
} |
|
620
|
1 |
|
if ($this->showBudget) { |
|
621
|
1 |
|
$headers[] = ['label' => _tr('Budget prévu'), 'formats' => [self::$headerFormat, self::$centerFormat]]; |
|
622
|
1 |
|
$headers[] = ['label' => _tr('Budget restant'), 'formats' => [self::$headerFormat, self::$centerFormat]]; |
|
623
|
|
|
} |
|
624
|
|
|
|
|
625
|
1 |
|
$headers[] = ['label' => '', 'formats' => []]; // gap |
|
626
|
1 |
|
$headers[] = ['label' => '', 'colspan' => 2]; // empty margin |
|
627
|
1 |
|
$headers[] = ['label' => $this->date->format('d.m.Y'), 'formats' => [self::$headerFormat, self::$centerFormat]]; // current date |
|
628
|
1 |
|
if ($this->datePrevious) { |
|
629
|
1 |
|
$headers[] = ['label' => $this->datePrevious->format('d.m.Y'), 'formats' => [self::$headerFormat, self::$centerFormat]]; // previous date |
|
630
|
|
|
} |
|
631
|
1 |
|
if ($this->showBudget) { |
|
632
|
1 |
|
$headers[] = ['label' => _tr('Budget prévu'), 'formats' => [self::$headerFormat, self::$centerFormat]]; |
|
633
|
1 |
|
$headers[] = ['label' => _tr('Budget restant'), 'formats' => [self::$headerFormat, self::$centerFormat]]; |
|
634
|
|
|
} |
|
635
|
|
|
|
|
636
|
1 |
|
$this->column = $initialColumn; |
|
637
|
1 |
|
$this->writeHeaders($headers); |
|
638
|
1 |
|
$this->sheet->getRowDimension($this->row)->setRowHeight(1.2, 'cm'); |
|
639
|
|
|
} |
|
640
|
|
|
|
|
641
|
|
|
/** |
|
642
|
|
|
* @param Data[] $allData |
|
643
|
|
|
*/ |
|
644
|
1 |
|
private function incomeStatement(int $initialColumn, array &$allData): void |
|
645
|
|
|
{ |
|
646
|
1 |
|
$firstLine = true; |
|
647
|
1 |
|
foreach ($allData as $index => $data) { |
|
648
|
|
|
// Column: account code |
|
649
|
1 |
|
$maybeBold = $this->maybeBold($data, 1); |
|
650
|
1 |
|
if (!$firstLine && $maybeBold) { |
|
651
|
1 |
|
++$this->row; |
|
652
|
|
|
} |
|
653
|
|
|
|
|
654
|
1 |
|
$this->write( |
|
655
|
1 |
|
str_repeat(' ', $data['depth'] - 1) . $data['code'], |
|
656
|
1 |
|
self::$indentFormat, |
|
657
|
1 |
|
$maybeBold, |
|
658
|
1 |
|
); |
|
659
|
|
|
|
|
660
|
|
|
// Column: account name |
|
661
|
1 |
|
$this->write($data['name'], self::$wrappedFormat, $maybeBold); |
|
662
|
|
|
|
|
663
|
|
|
// Column: balance at date |
|
664
|
|
|
// Store the coordinate of the cell to later compute totals |
|
665
|
1 |
|
$allData[$index]['cell'] = $this->sheet->getCell([$this->column, $this->row])->getCoordinate(); |
|
666
|
1 |
|
$this->write($data['balance'], self::$balanceFormat, $maybeBold, $data['format'] ?? []); |
|
667
|
|
|
|
|
668
|
|
|
// Column: balance at previous date (optional) |
|
669
|
1 |
|
if ($this->datePrevious) { |
|
670
|
1 |
|
$allData[$index]['cellPrevious'] = $this->sheet->getCell([$this->column, $this->row])->getCoordinate(); |
|
671
|
1 |
|
$this->write($data['balancePrevious'], $maybeBold, $data['formatPrevious'] ?? []); |
|
672
|
|
|
} |
|
673
|
|
|
|
|
674
|
|
|
// Budget columns (optional) |
|
675
|
1 |
|
if ($this->showBudget) { |
|
676
|
1 |
|
$allData[$index]['cellBudgetAllowed'] = $this->sheet->getCell([$this->column, $this->row])->getCoordinate(); |
|
677
|
1 |
|
$this->write($data['budgetAllowed'] ?? '', self::$balanceFormat, $maybeBold); |
|
678
|
|
|
|
|
679
|
1 |
|
$allData[$index]['cellBudgetBalance'] = $this->sheet->getCell([$this->column, $this->row])->getCoordinate(); |
|
680
|
1 |
|
$this->write($data['budgetBalance'] ?? '', self::$balanceFormat, $maybeBold); |
|
681
|
|
|
} |
|
682
|
|
|
|
|
683
|
1 |
|
++$this->row; |
|
684
|
1 |
|
$this->column = $initialColumn; |
|
685
|
|
|
|
|
686
|
1 |
|
$this->lastDataRow = max($this->lastDataRow, $this->row); |
|
687
|
1 |
|
$firstLine = false; |
|
688
|
|
|
} |
|
689
|
|
|
} |
|
690
|
|
|
|
|
691
|
1 |
|
private function color(Money $profitOrLoss): array |
|
692
|
|
|
{ |
|
693
|
1 |
|
return [ |
|
694
|
1 |
|
'font' => [ |
|
695
|
1 |
|
'color' => [ |
|
696
|
1 |
|
'argb' => $profitOrLoss->isPositive() ? Color::COLOR_DARKGREEN : Color::COLOR_RED, |
|
697
|
1 |
|
], |
|
698
|
1 |
|
], |
|
699
|
1 |
|
]; |
|
700
|
|
|
} |
|
701
|
|
|
|
|
702
|
1 |
|
private function isAccountType(Account $account, AccountType $accountType): bool |
|
703
|
|
|
{ |
|
704
|
1 |
|
return $account->getType() === $accountType |
|
705
|
1 |
|
|| ( |
|
706
|
1 |
|
$account->getType() === AccountType::Group |
|
707
|
1 |
|
&& $this->isCodeAccountType($account->getCode(), $accountType) |
|
708
|
1 |
|
); |
|
709
|
|
|
} |
|
710
|
|
|
|
|
711
|
1 |
|
private function isCodeAccountType(string|int $code, AccountType $accountType): bool |
|
712
|
|
|
{ |
|
713
|
1 |
|
$accountClasses = $this->accountingConfig['report']['accountClasses']; |
|
714
|
1 |
|
$class = match ($accountType) { |
|
715
|
1 |
|
AccountType::Asset => $accountClasses['assets'], |
|
716
|
1 |
|
AccountType::Liability => $accountClasses['liabilities'], |
|
717
|
1 |
|
AccountType::Revenue => $accountClasses['revenues'], |
|
718
|
1 |
|
AccountType::Expense => $accountClasses['expenses'], |
|
719
|
1 |
|
default => throw new Exception('Non supported related account type: ' . $accountType->value), |
|
720
|
1 |
|
}; |
|
721
|
|
|
|
|
722
|
1 |
|
return in_array(mb_substr((string) $code, 0, 1), $class, true); |
|
723
|
|
|
} |
|
724
|
|
|
} |
|
725
|
|
|
|