Passed
Push — master ( 99b2fc...23d1f3 )
by Andreas
13:13
created

org_openpsa_invoices_handler::prepare_toolbar()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

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

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