Passed
Push — master ( 19e3b7...6548a2 )
by Andreas
23:32
created

org_openpsa_invoices_handler::add_next_previous()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 41
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 6.2017

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 30
nc 9
nop 2
dl 0
loc 41
ccs 21
cts 33
cp 0.6364
crap 6.2017
rs 9.1288
c 1
b 0
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
 * 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
        $vat_array = explode(',', $percentages);
20 5
        if (!empty($vat_array)) {
21 5
            foreach ($vat_array as $entry) {
22 5
                $values[$entry] = "{$entry}%";
23
            }
24
        }
25 5
        return $values;
26
    }
27
28 1
    public function render_invoice_actions(org_openpsa_invoices_invoice_dba $invoice) : string
29
    {
30 1
        if ($invoice->paid) {
31
            return date('Y-m-d', $invoice->paid);
32
        }
33 1
        if (!$invoice->can_do('midgard:update')) {
34
            return '';
35
        }
36
37
        // unsent invoices
38 1
        if ($invoice->sent == 0) {
39
            // sending per mail enabled in billing data?
40 1
            $billing_data = org_openpsa_invoices_billing_data_dba::get_by_object($invoice);
41
            // only show if mail was chosen as option
42 1
            if ($billing_data->sendingoption === 2) {
43
                $action = 'send_by_mail';
44
                $icon = '<i class="fa fa-paper-plane"></i>';
45
            } else {
46 1
                $action = 'mark_sent';
47 1
                $icon = '<i class="fa fa-paper-plane-o"></i>';
48
            }
49
        } else {
50
            // not paid yet (see above)
51
            $action = 'mark_paid';
52
            $icon = '<i class="fa fa-check"></i>';
53
        }
54
55
        // generate next action buttons
56 1
        return '<button id="invoice_' . $invoice->guid . '" class="' . $action . '">' . $icon . ' ' . $this->_l10n->get($action) . '</button>';
57
    }
58
59 3
    public function prepare_toolbar(bool $show_backlink = true)
60
    {
61 3
        if ($show_backlink) {
62 2
            $this->_view_toolbar->add_item([
63 2
                MIDCOM_TOOLBAR_URL => '',
64 2
                MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('dashboard'),
65 2
                MIDCOM_TOOLBAR_GLYPHICON => 'chevron-left',
66 2
            ]);
67
        }
68 3
        if (midcom::get()->auth->can_user_do('midgard:create', null, org_openpsa_invoices_invoice_dba::class)) {
69 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

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