Failed Conditions
Push — master ( c4208e...4b96fe )
by Sylvain
09:02
created

ExportTransactionLinesAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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