Failed Conditions
Push — master ( 4b335d...ccf9db )
by Sylvain
07:33
created

ExportTransactionLinesHandler::writeData()   C

Complexity

Conditions 14
Paths 9

Size

Total Lines 80
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 46
CRAP Score 14

Importance

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

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