Passed
Push — master ( b4e0ae...1033f8 )
by Sylvain
11:14
created

ExportTransactionLinesAction::writeData()   C

Complexity

Conditions 14
Paths 9

Size

Total Lines 80
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 107.9454

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 14
eloc 47
c 2
b 0
f 0
nc 9
nop 2
dl 0
loc 80
ccs 10
cts 46
cp 0.2174
crap 107.9454
rs 6.2666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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