Passed
Push — master ( dc34cc...293021 )
by Andreas
27:55
created

org_openpsa_invoices_status   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Test Coverage

Coverage 46.24%

Importance

Changes 0
Metric Value
eloc 81
dl 0
loc 135
ccs 43
cts 93
cp 0.4624
rs 10
c 0
b 0
f 0
wmc 19

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get_history() 0 12 2
A get_status_class() 0 3 1
A get_button() 0 7 1
A get_current_status() 0 8 1
A get_journal_entries() 0 15 2
B get_status_entries() 0 64 11
1
<?php
2
/**
3
 * @package org.openpsa.invoices
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
/**
10
 * @package org.openpsa.invoices
11
 */
12
class org_openpsa_invoices_status extends org_openpsa_widgets_status
13
{
14
    private org_openpsa_invoices_invoice_dba $invoice;
15
16
    private midcom_services_i18n_l10n $l10n;
17
18
    private midcom_services_i18n_l10n $l10n_midcom;
19
20 1
    public function __construct(org_openpsa_invoices_invoice_dba $invoice)
21
    {
22 1
        $this->l10n = midcom::get()->i18n->get_l10n('org.openpsa.invoices');
23 1
        $this->l10n_midcom = midcom::get()->i18n->get_l10n('midcom');
24 1
        $this->invoice = $invoice;
25
    }
26
27 1
    public function get_current_status() : string
28
    {
29 1
        return match ($this->invoice->get_status()) {
30 1
            'unsent' => $this->l10n->get('unsent'),
31
            'open' => sprintf($this->l10n->get('due on %s'), $this->l10n->get_formatter()->date($this->invoice->due)),
32
            'overdue' => '<span class="bad">' . sprintf($this->l10n->get('overdue since %s'), $this->l10n->get_formatter()->date($this->invoice->due)) . '</span>',
33
            'paid' => sprintf($this->l10n->get('paid on %s'), $this->l10n->get_formatter()->date($this->invoice->paid)),
34 1
            'canceled' => sprintf($this->l10n->get('invoice canceled on %s'), $this->l10n->get_formatter()->date($this->invoice->paid))
35 1
        };
36
    }
37
38 1
    public function get_status_class() : string
39
    {
40 1
        return $this->invoice->get_status();
41
    }
42
43 1
    public function get_button() : string
44
    {
45 1
        $tooltip = midcom::get()->i18n->get_string('add journal entry', 'org.openpsa.relatedto');
46 1
        $save_label = $this->l10n_midcom->get('save');
47 1
        $cancel_label = $this->l10n_midcom->get('cancel');
48 1
        return '<a id="add-journal-entry" data-guid="' . $this->invoice->guid . '" data-dialog-submit-label="' . $save_label . '" data-dialog-cancel-label="' . $cancel_label . '" title="' . $tooltip . "\">\n" .
49 1
            '<i class="fa fa-plus" title="' . $tooltip . "\"></i></a>\n";
50
    }
51
52 1
    public function get_history() : array
53
    {
54 1
        $entries = array_merge($this->get_status_entries(), $this->get_journal_entries());
55
56 1
        usort($entries, function (array $a, array $b) {
57
            if ($a['timestamp'] == $b['timestamp']) {
58
                return $b['order'] <=> $a['order'];
59
            }
60
            return $b['timestamp'] <=> $a['timestamp'];
61 1
        });
62
63 1
        return $entries;
64
    }
65
66 1
    private function get_status_entries() : array
67
    {
68 1
        $entries = [];
69 1
        if ($this->invoice->cancelationInvoice) {
70
            $prefix = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX);
71
            $cancelation_invoice = new org_openpsa_invoices_invoice_dba($this->invoice->cancelationInvoice);
72
            $cancelation_invoice_link = $prefix . 'invoice/' . $cancelation_invoice->guid . '/';
0 ignored issues
show
Bug introduced by
Are you sure $prefix of type false|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
            $cancelation_invoice_link = /** @scrutinizer ignore-type */ $prefix . 'invoice/' . $cancelation_invoice->guid . '/';
Loading history...
73
            $cancelation_invoice_link = "<a href=\"" . $cancelation_invoice_link . "\">" . $this->l10n->get('invoice') . " " . $cancelation_invoice->get_label() . "</a>";
74
            $entries[] = [
75
                'timestamp' => $cancelation_invoice->metadata->created,
76
                'message' => sprintf($this->l10n->get('invoice got canceled by %s'), $cancelation_invoice_link),
77
                'order' => 4
78
            ];
79 1
        } elseif ($this->invoice->paid) {
80
            $entries[] = [
81
                'timestamp' => $this->invoice->paid,
82
                'message' => sprintf($this->l10n->get('marked invoice %s paid'), ''),
83
                'order' => 3
84
            ];
85
        }
86 1
        if (   $this->invoice->due
87 1
            && (   (   $this->invoice->due < time()
88 1
                    && $this->invoice->paid == 0)
89 1
                || $this->invoice->due < $this->invoice->paid)) {
90
            $entries[] = [
91
                'timestamp' => $this->invoice->due,
92
                'message' => $this->l10n->get('overdue'),
93
                'order' => 2
94
            ];
95
        }
96
97 1
        if ($this->invoice->sent) {
98
            if ($mail_time = $this->invoice->get_parameter('org.openpsa.invoices', 'sent_by_mail')) {
99
                $entries[] = [
100
                    'timestamp' => $mail_time,
101
                    'message' => sprintf($this->l10n->get('marked invoice %s sent per mail'), ''),
102
                    'order' => 1
103
                ];
104
            } else {
105
                $entries[] = [
106
                    'timestamp' => $this->invoice->sent,
107
                    'message' => sprintf($this->l10n->get('marked invoice %s sent'), ''),
108
                    'order' => 1
109
                ];
110
            }
111
112
            $reminders = $this->invoice->get_parameter('org.openpsa.invoices', 'sent_payment_reminder');
113
            if ($reminders) {
114
                $reminder_times = json_decode($reminders, true);
115
                foreach ($reminder_times as $reminder_time) {
116
                    $entries[] = [
117
                        'timestamp' => $reminder_time,
118
                        'message' => sprintf($this->l10n->get('marked invoice %s payment reminder sent'), ''),
119
                        'order' => 1
120
                    ];
121
                }
122
            }
123
        }
124 1
        $entries[] = [
125 1
            'timestamp' => $this->invoice->metadata->created,
126 1
            'message' => sprintf($this->l10n->get('invoice %s created'), ''),
127 1
            'order' => 0
128 1
        ];
129 1
        return $entries;
130
    }
131
132 1
    private function get_journal_entries() : array
133
    {
134 1
        $entries = [];
135
136 1
        $mc = org_openpsa_relatedto_journal_entry_dba::new_collector('linkGuid', $this->invoice->guid);
137 1
        $rows = $mc->get_rows(['title', 'metadata.created']);
138
139 1
        foreach ($rows as $row) {
140
            $entries[] = [
141
                'timestamp' => strtotime((string) $row['created']),
142
                'message' => $row['title'],
143
                'order' => 0
144
            ];
145
        }
146 1
        return $entries;
147
    }
148
}
149