Completed
Push — master ( 561103...c0a37a )
by Andreas
17:13
created

org_openpsa_invoices_handler::get_vat_options()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 1
dl 0
loc 10
ccs 7
cts 7
cp 1
crap 3
rs 10
c 0
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($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)
29
    {
30 1
        if ($invoice->paid) {
31
            return strftime('%Y-%m-%d', $invoice->paid);
32
        }
33 1
        if (!$invoice->can_do('midgard:update')) {
34
            return '';
35
        }
36
37 1
        $action = null;
38 1
        $icon = null;
39
40
        // unsent invoices
41 1
        if ($invoice->sent == 0) {
42
            // sending per mail enabled in billing data?
43 1
            $billing_data = org_openpsa_invoices_billing_data_dba::get_by_object($invoice);
44
            // only show if mail was chosen as option
45 1
            if ((int) $billing_data->sendingoption === 2) {
46
                $action = 'send_by_mail';
47
                $icon = '<i class="fa fa-paper-plane"></i>';
48
            } else {
49 1
                $action = 'mark_sent';
50 1
                $icon = '<i class="fa fa-paper-plane-o"></i>';
51
            }
52
        }
53
        // not paid yet
54
        elseif (!$invoice->paid) {
55
            $action = 'mark_paid';
56
            $icon = '<i class="fa fa-check"></i>';
57
        }
58
59
        // generate next action buttons
60 1
        if ($action !== null) {
61 1
            return '<button id="invoice_' . $invoice->guid . '" class="' . $action . '">' . $icon . ' ' . $this->_l10n->get($action) . '</button>';
62
        }
63
    }
64
65 3
    public function prepare_toolbar($show_backlink = true)
66
    {
67 3
        if ($show_backlink) {
68 2
            $this->_view_toolbar->add_item([
69 2
                MIDCOM_TOOLBAR_URL => '',
70 2
                MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('dashboard'),
71 2
                MIDCOM_TOOLBAR_GLYPHICON => 'chevron-left',
72
            ]);
73
        }
74 3
        if (midcom::get()->auth->can_user_do('midgard:create', null, org_openpsa_invoices_invoice_dba::class)) {
75 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

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