Failed Conditions
Push — master ( d2dc84...8e498d )
by Adrien
07:27
created

TransactionLines   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
eloc 83
dl 0
loc 155
ccs 86
cts 86
cp 1
rs 10
c 0
b 0
f 0

5 Methods

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