Completed
Push — master ( 886fb9...3f4d38 )
by Andreas
14:36
created

org_openpsa_invoices_handler   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 66.15%

Importance

Changes 0
Metric Value
eloc 62
dl 0
loc 100
ccs 43
cts 65
cp 0.6615
rs 10
c 0
b 0
f 0
wmc 16

3 Methods

Rating   Name   Duplication   Size   Complexity  
B render_invoice_actions() 0 34 8
A prepare_toolbar() 0 14 3
A add_next_previous() 0 43 5
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
        $url = '';
75 2
        if ($object->number > 1) {
76
            $mc = org_openpsa_invoices_invoice_dba::new_collector();
77
            $mc->add_constraint('number', '<', $object->number);
78
            $mc->set_limit(1);
79
            $mc->add_order('number', 'DESC');
80
            $results = $mc->list_keys();
81
82
            if (!empty($results)) {
83
                $url = $urlprefix . key($results) . '/';
84
            }
85
        }
86 2
        $items[] = [
87 2
            MIDCOM_TOOLBAR_URL => $url,
88 2
            MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('previous'),
89 2
            MIDCOM_TOOLBAR_GLYPHICON => 'chevron-left',
90 2
            MIDCOM_TOOLBAR_ACCESSKEY => 'p',
91 2
            MIDCOM_TOOLBAR_ENABLED => !empty($url)
92
        ];
93
94 2
        $url = '';
95 2
        if (($object->number + 1) < $object->generate_invoice_number()) {
96
            $mc = org_openpsa_invoices_invoice_dba::new_collector();
97
            $mc->add_constraint('number', '>', $object->number);
98
            $mc->set_limit(1);
99
            $mc->add_order('number', 'ASC');
100
            $results = $mc->list_keys();
101
102
            if (!empty($results)) {
103
                $url = $urlprefix . key($results) . '/';
104
            }
105
        }
106 2
        $items[] = [
107 2
            MIDCOM_TOOLBAR_URL => $url,
108 2
            MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('next'),
109 2
            MIDCOM_TOOLBAR_GLYPHICON => 'chevron-right',
110 2
            MIDCOM_TOOLBAR_ACCESSKEY => 'n',
111 2
            MIDCOM_TOOLBAR_ENABLED => !empty($url)
112
        ];
113 2
        org_openpsa_widgets_ui::add_navigation_toolbar($items);
114 2
    }
115
}
116