Passed
Push — master ( fef947...832870 )
by Andreas
12:13
created

render_invoice_actions()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 16
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 29
ccs 9
cts 15
cp 0.6
crap 6.6
rs 9.4222
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
 * Handler addons
11
 *
12
 * @package org.openpsa.invoices
13
 */
14
trait org_openpsa_invoices_handler
15
{
16 5
    public function get_vat_options(string $percentages) : array
17
    {
18 5
        $values = [];
19 5
        if ($vat_array = explode(',', $percentages)) {
20 5
            foreach ($vat_array as $entry) {
21 5
                $values[$entry] = "{$entry}%";
22
            }
23
        }
24 5
        return $values;
25
    }
26
27 1
    public function render_invoice_actions(org_openpsa_invoices_invoice_dba $invoice) : string
28
    {
29 1
        if ($invoice->paid) {
30
            return date('Y-m-d', $invoice->paid);
31
        }
32 1
        if (!$invoice->can_do('midgard:update')) {
33
            return '';
34
        }
35
36
        // unsent invoices
37 1
        if ($invoice->sent == 0) {
38
            // sending per mail enabled in billing data?
39 1
            $billing_data = org_openpsa_invoices_billing_data_dba::get_by_object($invoice);
40
            // only show if mail was chosen as option
41 1
            if ($billing_data->sendingoption === 2) {
42
                $action = 'send_by_mail';
43
                $icon = '<i class="fa fa-paper-plane"></i>';
44
            } else {
45 1
                $action = 'mark_sent';
46 1
                $icon = '<i class="fa fa-paper-plane-o"></i>';
47
            }
48
        } else {
49
            // not paid yet (see above)
50
            $action = 'mark_paid';
51
            $icon = '<i class="fa fa-check"></i>';
52
        }
53
54
        // generate next action buttons
55 1
        return '<button id="invoice_' . $invoice->guid . '" class="' . $action . '">' . $icon . ' ' . $this->_l10n->get($action) . '</button>';
56
    }
57
58 3
    public function prepare_toolbar(bool $show_backlink = true)
59
    {
60 3
        if ($show_backlink) {
61 2
            $this->_view_toolbar->add_item([
62 2
                MIDCOM_TOOLBAR_URL => '',
63 2
                MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('dashboard'),
64 2
                MIDCOM_TOOLBAR_GLYPHICON => 'chevron-left',
65 2
            ]);
66
        }
67 3
        if (midcom::get()->auth->can_user_do('midgard:create', class: org_openpsa_invoices_invoice_dba::class)) {
68 3
            $workflow = $this->get_workflow('datamanager');
0 ignored issues
show
Bug introduced by
It seems like get_workflow() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

68
            /** @scrutinizer ignore-call */ 
69
            $workflow = $this->get_workflow('datamanager');
Loading history...
69 3
            $this->_view_toolbar->add_item($workflow->get_button($this->router->generate('invoice_new_nocustomer'), [
70 3
                MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('create invoice'),
71 3
                MIDCOM_TOOLBAR_GLYPHICON => 'plus',
72 3
            ]));
73
        }
74
    }
75
76 2
    public function add_next_previous(org_openpsa_invoices_invoice_dba $object, string $urlprefix)
77
    {
78 2
        $items = [];
79 2
        $url = '';
80 2
        if ($object->number > 1) {
81
            $mc = org_openpsa_invoices_invoice_dba::new_collector();
82
            $mc->add_constraint('number', '<', $object->number);
83
            $mc->set_limit(1);
84
            $mc->add_order('number', 'DESC');
85
86
            if ($results = $mc->list_keys()) {
87
                $url = $urlprefix . key($results) . '/';
88
            }
89
        }
90 2
        $items[] = [
91 2
            MIDCOM_TOOLBAR_URL => $url,
92 2
            MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('previous'),
93 2
            MIDCOM_TOOLBAR_GLYPHICON => 'chevron-left',
94 2
            MIDCOM_TOOLBAR_ACCESSKEY => 'p',
95 2
            MIDCOM_TOOLBAR_ENABLED => !empty($url)
96 2
        ];
97
98 2
        $url = '';
99 2
        if (($object->number + 1) < $object->generate_invoice_number()) {
100
            $mc = org_openpsa_invoices_invoice_dba::new_collector();
101
            $mc->add_constraint('number', '>', $object->number);
102
            $mc->set_limit(1);
103
            $mc->add_order('number', 'ASC');
104
105
            if ($results = $mc->list_keys()) {
106
                $url = $urlprefix . key($results) . '/';
107
            }
108
        }
109 2
        $items[] = [
110 2
            MIDCOM_TOOLBAR_URL => $url,
111 2
            MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('next'),
112 2
            MIDCOM_TOOLBAR_GLYPHICON => 'chevron-right',
113 2
            MIDCOM_TOOLBAR_ACCESSKEY => 'n',
114 2
            MIDCOM_TOOLBAR_ENABLED => !empty($url)
115 2
        ];
116 2
        org_openpsa_widgets_ui::add_navigation_toolbar($items);
117
    }
118
}
119