Completed
Push — master ( 3c1e03...ef9dbd )
by Andreas
20:30
created

org_openpsa_invoices_status::get_journal_entries()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.2559

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 9
nc 2
nop 0
dl 0
loc 15
ccs 6
cts 10
cp 0.6
crap 2.2559
rs 9.9666
c 1
b 1
f 0
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
    /**
15
     * @var org_openpsa_invoices_invoice_dba
16
     */
17
    private $invoice;
18
19
    /**
20
     * @var midcom_services_i18n_l10n
21
     */
22
    private $l10n;
23
24
    /**
25
     * @var midcom_services_i18n_l10n
26
     */
27
    private $l10n_midcom;
28
29
    /**
30
     *
31
     * @param org_openpsa_invoices_invoice_dba $invoice
32
     */
33 1
    public function __construct(org_openpsa_invoices_invoice_dba $invoice)
34
    {
35 1
        $this->l10n = midcom::get()->i18n->get_l10n('org.openpsa.invoices');
36 1
        $this->l10n_midcom = midcom::get()->i18n->get_l10n('midcom');
37 1
        $this->invoice = $invoice;
38 1
    }
39
40
    /**
41
     *
42
     * @return string
43
     */
44 1
    public function get_current_status()
45
    {
46 1
        switch ($this->invoice->get_status()) {
47 1
            case 'unsent':
48 1
                return $this->l10n->get('unsent');
49
50
            case 'open':
51
                return sprintf($this->l10n->get('due on %s'), $this->l10n->get_formatter()->date($this->invoice->due));
52
53
            case 'overdue':
54
                return '<span class="bad">' . sprintf($this->l10n->get('overdue since %s'), $this->l10n->get_formatter()->date($this->invoice->due)) . '</span>';
55
56
            case 'paid':
57
                return sprintf($this->l10n->get('paid on %s'), $this->l10n->get_formatter()->date($this->invoice->paid));
58
59
            case 'canceled':
60
                return sprintf($this->l10n->get('invoice canceled on %s'), $this->l10n->get_formatter()->date($this->invoice->paid));
61
        }
62
    }
63
64 1
    public function get_status_class()
65
    {
66 1
        return $this->invoice->get_status();
67
    }
68
69 1
    public function get_button()
70
    {
71 1
        $tooltip = midcom::get()->i18n->get_l10n('org.openpsa.relatedto')->get('add journal entry');
72 1
        $save_label = $this->l10n_midcom->get('save');
73 1
        $cancel_label = $this->l10n_midcom->get('cancel');
74 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" .
75 1
            '<i class="fa fa-plus" title="' . $tooltip . "\"></i></a>\n";
76
    }
77
78
    /**
79
     * @return array
80
     */
81 1
    public function get_history()
82
    {
83 1
        $entries = array_merge($this->get_status_entries(), $this->get_journal_entries());
84
85 1
        usort($entries, function ($a, $b) {
86
            if ($a['timestamp'] == $b['timestamp']) {
87
                return $b['order'] <=> $a['order'];
88
            }
89
            return $b['timestamp'] <=> $a['timestamp'];
90 1
        });
91
92 1
        return $entries;
93
    }
94
95
    /**
96
     *
97
     * @return array
98
     */
99 1
    private function get_status_entries()
100
    {
101 1
        $entries = [];
102 1
        if ($this->invoice->cancelationInvoice) {
103
            $prefix = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX);
104
            $cancelation_invoice = new org_openpsa_invoices_invoice_dba($this->invoice->cancelationInvoice);
105
            $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

105
            $cancelation_invoice_link = /** @scrutinizer ignore-type */ $prefix . 'invoice/' . $cancelation_invoice->guid . '/';
Loading history...
106
            $cancelation_invoice_link = "<a href=\"" . $cancelation_invoice_link . "\">" . $this->l10n->get('invoice') . " " . $cancelation_invoice->get_label() . "</a>";
107
            $entries[] = [
108
                'timestamp' => $cancelation_invoice->metadata->created,
109
                'message' => sprintf($this->l10n->get('invoice got canceled by %s'), $cancelation_invoice_link),
110
                'order' => 4
111
            ];
112 1
        } elseif ($this->invoice->paid) {
113
            $entries[] = [
114
                'timestamp' => $this->invoice->paid,
115
                'message' => sprintf($this->l10n->get('marked invoice %s paid'), ''),
116
                'order' => 3
117
            ];
118
        }
119 1
        if (   $this->invoice->due
120
            && (   (   $this->invoice->due < time()
121
                    && $this->invoice->paid == 0)
122 1
                || $this->invoice->due < $this->invoice->paid)) {
123
            $entries[] = [
124
                'timestamp' => $this->invoice->due,
125
                'message' => $this->l10n->get('overdue'),
126
                'order' => 2
127
            ];
128
        }
129
130 1
        if ($this->invoice->sent) {
131
            if ($mail_time = $this->invoice->get_parameter('org.openpsa.invoices', 'sent_by_mail')) {
132
                $entries[] = [
133
                    'timestamp' => $mail_time,
134
                    'message' => sprintf($this->l10n->get('marked invoice %s sent per mail'), ''),
135
                    'order' => 1
136
                ];
137
            } else {
138
                $entries[] = [
139
                    'timestamp' => $this->invoice->sent,
140
                    'message' => sprintf($this->l10n->get('marked invoice %s sent'), ''),
141
                    'order' => 1
142
                ];
143
            }
144
        }
145 1
        $entries[] = [
146 1
            'timestamp' => $this->invoice->metadata->created,
147 1
            'message' => sprintf($this->l10n->get('invoice %s created'), ''),
148 1
            'order' => 0
149
        ];
150 1
        return $entries;
151
    }
152
153 1
    private function get_journal_entries()
154
    {
155 1
        $entries = [];
156
157 1
        $mc = org_openpsa_relatedto_journal_entry_dba::new_collector('linkGuid', $this->invoice->guid);
158 1
        $rows = $mc->get_rows(['title', 'metadata.created']);
159
160 1
        foreach ($rows as $row) {
161
            $entries[] = [
162
                'timestamp' => strtotime((string) $row['created']),
163
                'message' => $row['title'],
164
                'order' => 0
165
            ];
166
        }
167 1
        return $entries;
168
    }
169
}
170