Completed
Push — master ( f34725...886fb9 )
by Andreas
15:32
created

render_invoice_actions()   B

Complexity

Conditions 8
Paths 10

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 11.1999

Importance

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

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