Failed Conditions
Push — master ( 5a6ec0...06e1a1 )
by Sylvain
11:24 queued 11s
created

ExportTransactionLinesAction::getHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 16
ccs 14
cts 14
cp 1
crap 1
rs 9.7998
c 0
b 0
f 0
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
            if (!$currentTransactionId) {
69
                $currentTransactionId = $line->getTransaction()->getId();
70
                $currentTransactionRowStart = $this->row;
71
            } elseif ($line->getTransaction()->getId() !== $currentTransactionId) {
72
                // Current line is the first of a new transaction
73
                // Merge cells that have the same value because from the same transaction
74
                foreach ($mergeRowsFromColumns as $column) {
75
                    $sheet->mergeCellsByColumnAndRow($column, $currentTransactionRowStart, $column, $this->row - 1);
76
                }
77
                $currentTransactionRowStart = $this->row;
78
                $currentTransactionId = $line->getTransaction()->getId();
79
            }
80
81
            // Date
82
            $this->write($sheet, $line->getTransactionDate()->format('d.m.Y'));
83
84
            // Transaction.ID
85
            $this->write($sheet, $line->getTransaction()->getId());
86
            $url = 'https://' . $this->hostname . '/admin/transaction/%u';
87
            $sheet->getCellByColumnAndRow($this->column - 1, $this->row)->getHyperlink()->setUrl(sprintf($url, $line->getTransaction()->getId()));
88
            // Transaction.name
89
            $this->write($sheet, $line->getTransaction()->getName());
90
            // Transaction.remarks
91
            $this->write($sheet, $line->getTransaction()->getRemarks());
92
            // Transaction.internalRemarks
93
            $this->write($sheet, $line->getTransaction()->getInternalRemarks());
94
95
            // TransactionLine.name
96
            $this->write($sheet, $line->getName());
97
            // TransactionLine.remarks
98
            $this->write($sheet, $line->getRemarks());
99
            // Bookable.name
100
            $this->write($sheet, $line->getBookable() ? $line->getBookable()->getName() : '');
101
            // Debit account
102
            $this->write($sheet, $line->getDebit() ? implode(' ', [$line->getDebit()->getCode(), $line->getDebit()->getName()]) : '');
103
            // Credit account
104
            $this->write($sheet, $line->getCredit() ? implode(' ', [$line->getCredit()->getCode(), $line->getCredit()->getName()]) : '');
105
            // Debit amount
106
            $this->write($sheet, $line->getDebit() ? $this->moneyFormatter->format($line->getBalance()) : '');
107
            // Credit amount
108
            $this->write($sheet, $line->getCredit() ? $this->moneyFormatter->format($line->getBalance()) : '');
109
            // Reconciled
110
            $this->write($sheet, $line->isReconciled() ? '✔️' : '');
111
112
            $maxColumn = $this->column - 1;
113
114
            $range = Coordinate::stringFromColumnIndex($initialColumn) . $this->row . ':' . Coordinate::stringFromColumnIndex($maxColumn) . $this->row;
115
            // Thicker horizontal delimiter between lines of different transactions
116
            if ($index === max(array_keys($items)) || $line->getTransaction()->getId() !== $items[$index + 1]->getTransaction()->getId()) {
117
                $borderBottom = self::$bordersBottom;
118
            } else {
119
                $borderBottom = self::$bordersBottomLight;
120
            }
121
            $sheet->getStyle($range)->applyFromArray($borderBottom);
122
123
            $this->column = $initialColumn;
124
            ++$this->row;
125
        }
126 1
    }
127
128 1
    protected function getHeaders(): array
129
    {
130
        return [
131 1
            ['label' => 'Date', 'width' => 12, 'formats' => [self::$dateFormat]],
132 1
            ['label' => 'ID', 'width' => 5, 'formats' => [self::$headerFormat, self::$centerFormat]],
133 1
            ['label' => 'Transaction', 'width' => 'auto', 'formats' => [self::$wrapFormat]],
134 1
            ['label' => 'Remarques', 'width' => 'auto', 'formats' => [self::$wrapFormat]],
135 1
            ['label' => 'Remarques internes', 'width' => 'auto', 'formats' => [self::$wrapFormat]],
136 1
            ['label' => 'Écriture', 'width' => 'auto', 'formats' => [self::$wrapFormat]],
137 1
            ['label' => 'Remarques', 'width' => 'auto', 'formats' => [self::$wrapFormat]],
138 1
            ['label' => 'Réservable', 'width' => 'auto', 'formats' => [self::$wrapFormat]],
139 1
            ['label' => 'Compte débit', 'width' => 30, 'formats' => [self::$wrapFormat]],
140 1
            ['label' => 'Compte crédit', 'width' => 30, 'formats' => [self::$wrapFormat]],
141 1
            ['label' => 'Montant débit', 'width' => 20, 'formats' => [self::$headerFormat]],
142 1
            ['label' => 'Montant crédit', 'width' => 20, 'formats' => [self::$headerFormat]],
143 1
            ['label' => 'Pointé', 'width' => 15, 'formats' => [self::$centerFormat]],
144
        ];
145
    }
146
147
    /**
148
     * @param Worksheet $sheet
149
     * @param TransactionLine[] $items
150
     */
151 1
    protected function writeFooter(Worksheet $sheet, array $items): void
152
    {
153 1
        $initialColumn = $this->column;
154
155
        // Date
156 1
        $this->write($sheet, '');
157
        // ID
158 1
        $this->write($sheet, '');
159
        // Transaction.name
160 1
        $this->write($sheet, '');
161
        // Transaction.remarks
162 1
        $this->write($sheet, '');
163
        // Internal remarks
164 1
        $this->write($sheet, '');
165
        // TransactionLine.name
166 1
        $this->write($sheet, '');
167
        // Remarks
168 1
        $this->write($sheet, '');
169
        // Bookable.name
170 1
        $this->write($sheet, '');
171
        // Debit account
172 1
        $this->write($sheet, '');
173
        // Credit account
174 1
        $this->write($sheet, '');
175
        // Debit amount
176 1
        $this->write($sheet, '=SUBTOTAL(9,' . Coordinate::stringFromColumnIndex($this->column) . '2:' . Coordinate::stringFromColumnIndex($this->column) . ($this->row - 1) . ')');
177
        // Credit amount
178 1
        $this->write($sheet, '=SUBTOTAL(9,' . Coordinate::stringFromColumnIndex($this->column) . '2:' . Coordinate::stringFromColumnIndex($this->column) . ($this->row - 1) . ')');
179
        // Reconciled
180 1
        $this->write($sheet, '');
181
182
        // Apply style
183 1
        $range = Coordinate::stringFromColumnIndex($initialColumn) . $this->row . ':' . Coordinate::stringFromColumnIndex($this->column - 1) . $this->row;
184 1
        $sheet->getStyle($range)->applyFromArray(self::$totalFormat);
185 1
    }
186
}
187