Passed
Push — master ( 27fb69...02ae83 )
by Andreas
17:11
created

org_openpsa_sales_interface   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Test Coverage

Coverage 12.07%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 57
c 4
b 0
f 0
dl 0
loc 120
ccs 7
cts 58
cp 0.1207
rs 10
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve_object_link() 0 9 3
A org_openpsa_relatedto_find_suspects() 0 4 2
A new_notification_message() 0 32 4
A new_subscription_cycle() 0 16 3
A _find_suspects_event() 0 29 2
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 events
32
     */
33 9
    public function org_openpsa_relatedto_find_suspects(midcom_core_dbaobject $object, org_openpsa_relatedto_dba $defaults, array &$links_array)
34
    {
35 9
        if ($object instanceof org_openpsa_calendar_event_dba) {
36 9
            $this->_find_suspects_event($object, $defaults, $links_array);
37
            //TODO: groups ? other objects ?
38
        }
39 9
    }
40
41
    /**
42
     * Used by org_openpsa_relatedto_find_suspects to in case the given object is an event
43
     *
44
     * Current rule: all participants of event must be either manager,contact or resource in task
45
     * that overlaps in time with the event.
46
     */
47 9
    private function _find_suspects_event(org_openpsa_calendar_event_dba $event, org_openpsa_relatedto_dba $defaults, array &$links_array)
48
    {
49 9
        if (count($event->participants) < 2) {
50
            //We have less than two participants, abort
51 9
            return;
52
        }
53
        $mc = org_openpsa_contacts_role_dba::new_collector('role', org_openpsa_sales_salesproject_dba::ROLE_MEMBER);
54
        $mc->add_constraint('person', 'IN', array_keys($event->participants));
55
        $guids = $mc->get_values('objectGuid');
56
57
        $qb = org_openpsa_sales_salesproject_dba::new_query_builder();
58
59
        // Target sales project starts or ends inside given events window or starts before and ends after
60
        $qb->add_constraint('start', '<=', $event->end);
61
        $qb->begin_group('OR');
62
        $qb->add_constraint('end', '>=', $event->start);
63
        $qb->add_constraint('end', '=', 0);
64
        $qb->end_group();
65
66
        //Target sales project is active
67
        $qb->add_constraint('state', '=', org_openpsa_sales_salesproject_dba::STATE_ACTIVE);
68
69
        //Each event participant is either manager or member (resource/contact) in task
70
        $qb->begin_group('OR');
71
        $qb->add_constraint('owner', 'IN', array_keys($event->participants));
72
        $qb->add_constraint('guid', 'IN', $guids);
73
        $qb->end_group();
74
75
        org_openpsa_relatedto_suspect::add_links($qb, $this->_component, $defaults, $links_array);
76
    }
77
78
    /**
79
     * AT handler for handling subscription cycles.
80
     */
81
    public function new_subscription_cycle(array $args, midcom_baseclasses_components_cron_handler $handler)
82
    {
83
        if (!isset($args['deliverable'], $args['cycle'])) {
84
            $handler->print_error('deliverable GUID or cycle number not set, aborting');
85
            return false;
86
        }
87
88
        try {
89
            $deliverable = new org_openpsa_sales_salesproject_deliverable_dba($args['deliverable']);
90
        } catch (midcom_error $e) {
91
            $handler->print_error("Deliverable {$args['deliverable']} not found: " . midcom_connection::get_error_string());
92
            return false;
93
        }
94
        $scheduler = new org_openpsa_invoices_scheduler($deliverable);
95
96
        return $scheduler->run_cycle($args['cycle']);
97
    }
98
99
    /**
100
     * Function to send a notification to owner of the deliverable - guid of deliverable is passed
101
     */
102
    public function new_notification_message(array $args, midcom_baseclasses_components_cron_handler $handler)
103
    {
104
        if (!isset($args['deliverable'])) {
105
            $handler->print_error('deliverable GUID not set, aborting');
106
            return false;
107
        }
108
        try {
109
            $deliverable = new org_openpsa_sales_salesproject_deliverable_dba($args['deliverable']);
110
        } catch (midcom_error $e) {
111
            $handler->print_error('no deliverable with passed GUID: ' . $args['deliverable'] . ', aborting');
112
            return false;
113
        }
114
115
        //get the owner of the salesproject the deliverable belongs to
116
        try {
117
            $project = new org_openpsa_sales_salesproject_dba($deliverable->salesproject);
118
        } catch (midcom_error $e) {
119
            $handler->print_error('Failed to load salesproject: ' . $e->getMessage());
120
            return false;
121
        }
122
123
        $message = [
124
            'title' => sprintf($this->_l10n->get('notification for agreement %s'), $deliverable->title),
125
            'content' => sprintf(
126
                $this->_l10n->get('agreement %s ends on %s. click here: %s'),
127
                $deliverable->title,
128
                $this->_l10n->get_formatter()->date($deliverable->end),
129
                midcom::get()->permalinks->create_permalink($deliverable->guid)
130
            )
131
        ];
132
133
        return org_openpsa_notifications::notify('org.openpsa.sales:new_notification_message', $project->owner, $message);
134
    }
135
}
136