Passed
Push — master ( 9dc093...de7a06 )
by Andreas
24:11
created

org_openpsa_sales_interface::resolve_object_link()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 2
dl 0
loc 9
ccs 0
cts 6
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package org.openpsa.sales
4
 * @author Nemein Oy, http://www.nemein.com/
5
 * @copyright Nemein Oy, http://www.nemein.com/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
/**
10
 * OpenPSA Sales management component
11
 *
12
 * @package org.openpsa.sales
13
 */
14
class org_openpsa_sales_interface extends midcom_baseclasses_components_interface
15
implements midcom_services_permalinks_resolver
16
{
17
    public function resolve_object_link(midcom_db_topic $topic, midcom_core_dbaobject $object) : ?string
18
    {
19
        if ($object instanceof org_openpsa_sales_salesproject_dba) {
20
            return "salesproject/{$object->guid}/";
21
        }
22
        if ($object instanceof org_openpsa_sales_salesproject_deliverable_dba) {
23
            return "deliverable/{$object->guid}/";
24
        }
25
        return null;
26
    }
27
28
    /**
29
     * Used by org_openpsa_relatedto_suspect::find_links_object to find "related to" information
30
     *
31
     * Currently handles persons
32
     */
33 9
    public function org_openpsa_relatedto_find_suspects(midcom_core_dbaobject $object, org_openpsa_relatedto_dba $defaults, array &$links_array)
34
    {
35
        switch (true) {
36 9
            case $object instanceof midcom_db_person:
37
                //List all projects and tasks given person is involved with
38
                $this->_find_suspects_person($object, $defaults, $links_array);
39
                break;
40 9
            case $object instanceof org_openpsa_calendar_event_dba:
41 9
                $this->_find_suspects_event($object, $defaults, $links_array);
42 9
                break;
43
                //TODO: groups ? other objects ?
44
        }
45 9
    }
46
47
    /**
48
     * Used by org_openpsa_relatedto_find_suspects to in case the given object is a person
49
     *
50
     * Current rule: all participants of event must be either manager,contact or resource in task
51
     * that overlaps in time with the event.
52
     */
53 9
    private function _find_suspects_event(midcom_core_dbaobject $object, org_openpsa_relatedto_dba $defaults, array &$links_array)
54
    {
55 9
        if (   !is_array($object->participants)
0 ignored issues
show
Bug Best Practice introduced by
The property participants does not exist on midcom_core_dbaobject. Since you implemented __get, consider adding a @property annotation.
Loading history...
56 9
            || count($object->participants) < 2) {
57
            //We have invalid list or less than two participants, abort
58 9
            return;
59
        }
60
        $mc = org_openpsa_contacts_role_dba::new_collector('role', org_openpsa_sales_salesproject_dba::ROLE_MEMBER);
61
        $mc->add_constraint('person', 'IN', array_keys($object->participants));
62
        $guids = $mc->get_values('objectGuid');
63
64
        $qb = org_openpsa_sales_salesproject_dba::new_query_builder();
65
66
        // Target sales project starts or ends inside given events window or starts before and ends after
67
        $qb->add_constraint('start', '<=', $object->end);
0 ignored issues
show
Bug Best Practice introduced by
The property end does not exist on midcom_core_dbaobject. Since you implemented __get, consider adding a @property annotation.
Loading history...
68
        $qb->begin_group('OR');
69
        $qb->add_constraint('end', '>=', $object->start);
0 ignored issues
show
Bug Best Practice introduced by
The property start does not exist on midcom_core_dbaobject. Since you implemented __get, consider adding a @property annotation.
Loading history...
70
        $qb->add_constraint('end', '=', 0);
71
        $qb->end_group();
72
73
        //Target sales project is active
74
        $qb->add_constraint('state', '=', org_openpsa_sales_salesproject_dba::STATE_ACTIVE);
75
76
        //Each event participant is either manager or member (resource/contact) in task
77
        $qb->begin_group('OR');
78
        $qb->add_constraint('owner', 'IN', array_keys($object->participants));
79
        $qb->add_constraint('guid', 'IN', $guids);
80
        $qb->end_group();
81
82
        org_openpsa_relatedto_suspect::add_links($qb, $this->_component, $defaults, $links_array);
83
    }
84
85
    /**
86
     * Used by org_openpsa_relatedto_find_suspects to in case the given object is a person
87
     */
88
    private function _find_suspects_person(midcom_core_dbaobject $object, org_openpsa_relatedto_dba $defaults, array &$links_array)
89
    {
90
        $qb = org_openpsa_sales_salesproject_dba::new_query_builder();
91
        $qb->add_constraint('state', '=', org_openpsa_sales_salesproject_dba::STATE_ACTIVE);
92
        $qb->begin_group('OR');
93
            $mc = org_openpsa_contacts_role_dba::new_collector('role', org_openpsa_sales_salesproject_dba::ROLE_MEMBER);
94
            $mc->add_constraint('person', '=', $object->id);
95
            $qb->add_constraint('guid', 'IN', $mc->get_values('objectGuid'));
96
            $qb->add_constraint('owner', '=', $object->id);
97
        $qb->end_group();
98
99
        org_openpsa_relatedto_suspect::add_links($qb, $this->_component, $defaults, $links_array);
100
    }
101
102
    /**
103
     * AT handler for handling subscription cycles.
104
     */
105
    public function new_subscription_cycle(array $args, midcom_baseclasses_components_cron_handler $handler)
106
    {
107
        if (!isset($args['deliverable'], $args['cycle'])) {
108
            $handler->print_error('deliverable GUID or cycle number not set, aborting');
109
            return false;
110
        }
111
112
        try {
113
            $deliverable = new org_openpsa_sales_salesproject_deliverable_dba($args['deliverable']);
114
        } catch (midcom_error $e) {
115
            $handler->print_error("Deliverable {$args['deliverable']} not found: " . midcom_connection::get_error_string());
116
            return false;
117
        }
118
        $scheduler = new org_openpsa_invoices_scheduler($deliverable);
119
120
        return $scheduler->run_cycle($args['cycle']);
121
    }
122
123
    /**
124
     * Function to send a notification to owner of the deliverable - guid of deliverable is passed
125
     */
126
    public function new_notification_message(array $args, midcom_baseclasses_components_cron_handler $handler)
127
    {
128
        if (!isset($args['deliverable'])) {
129
            $handler->print_error('deliverable GUID not set, aborting');
130
            return false;
131
        }
132
        try {
133
            $deliverable = new org_openpsa_sales_salesproject_deliverable_dba($args['deliverable']);
134
        } catch (midcom_error $e) {
135
            $handler->print_error('no deliverable with passed GUID: ' . $args['deliverable'] . ', aborting');
136
            return false;
137
        }
138
139
        //get the owner of the salesproject the deliverable belongs to
140
        try {
141
            $project = new org_openpsa_sales_salesproject_dba($deliverable->salesproject);
142
        } catch (midcom_error $e) {
143
            $handler->print_error('Failed to load salesproject: ' . $e->getMessage());
144
            return false;
145
        }
146
147
        $message = [
148
            'title' => sprintf($this->_l10n->get('notification for agreement %s'), $deliverable->title),
149
            'content' => sprintf(
150
                $this->_l10n->get('agreement %s ends on %s. click here: %s'),
151
                $deliverable->title,
152
                $this->_l10n->get_formatter()->date($deliverable->end),
153
                midcom::get()->permalinks->create_permalink($deliverable->guid)
154
            )
155
        ];
156
157
        return org_openpsa_notifications::notify('org.openpsa.sales:new_notification_message', $project->owner, $message);
158
    }
159
}
160