Passed
Push — master ( 3e99a8...27fb69 )
by Andreas
24:27
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 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 (   !is_array($event->participants)
0 ignored issues
show
introduced by
The condition is_array($event->participants) is always true.
Loading history...
50 9
            || count($event->participants) < 2) {
51
            //We have invalid list or less than two participants, abort
52 9
            return;
53
        }
54
        $mc = org_openpsa_contacts_role_dba::new_collector('role', org_openpsa_sales_salesproject_dba::ROLE_MEMBER);
55
        $mc->add_constraint('person', 'IN', array_keys($event->participants));
56
        $guids = $mc->get_values('objectGuid');
57
58
        $qb = org_openpsa_sales_salesproject_dba::new_query_builder();
59
60
        // Target sales project starts or ends inside given events window or starts before and ends after
61
        $qb->add_constraint('start', '<=', $event->end);
62
        $qb->begin_group('OR');
63
        $qb->add_constraint('end', '>=', $event->start);
64
        $qb->add_constraint('end', '=', 0);
65
        $qb->end_group();
66
67
        //Target sales project is active
68
        $qb->add_constraint('state', '=', org_openpsa_sales_salesproject_dba::STATE_ACTIVE);
69
70
        //Each event participant is either manager or member (resource/contact) in task
71
        $qb->begin_group('OR');
72
        $qb->add_constraint('owner', 'IN', array_keys($event->participants));
73
        $qb->add_constraint('guid', 'IN', $guids);
74
        $qb->end_group();
75
76
        org_openpsa_relatedto_suspect::add_links($qb, $this->_component, $defaults, $links_array);
77
    }
78
79
    /**
80
     * AT handler for handling subscription cycles.
81
     */
82
    public function new_subscription_cycle(array $args, midcom_baseclasses_components_cron_handler $handler)
83
    {
84
        if (!isset($args['deliverable'], $args['cycle'])) {
85
            $handler->print_error('deliverable GUID or cycle number not set, aborting');
86
            return false;
87
        }
88
89
        try {
90
            $deliverable = new org_openpsa_sales_salesproject_deliverable_dba($args['deliverable']);
91
        } catch (midcom_error $e) {
92
            $handler->print_error("Deliverable {$args['deliverable']} not found: " . midcom_connection::get_error_string());
93
            return false;
94
        }
95
        $scheduler = new org_openpsa_invoices_scheduler($deliverable);
96
97
        return $scheduler->run_cycle($args['cycle']);
98
    }
99
100
    /**
101
     * Function to send a notification to owner of the deliverable - guid of deliverable is passed
102
     */
103
    public function new_notification_message(array $args, midcom_baseclasses_components_cron_handler $handler)
104
    {
105
        if (!isset($args['deliverable'])) {
106
            $handler->print_error('deliverable GUID not set, aborting');
107
            return false;
108
        }
109
        try {
110
            $deliverable = new org_openpsa_sales_salesproject_deliverable_dba($args['deliverable']);
111
        } catch (midcom_error $e) {
112
            $handler->print_error('no deliverable with passed GUID: ' . $args['deliverable'] . ', aborting');
113
            return false;
114
        }
115
116
        //get the owner of the salesproject the deliverable belongs to
117
        try {
118
            $project = new org_openpsa_sales_salesproject_dba($deliverable->salesproject);
119
        } catch (midcom_error $e) {
120
            $handler->print_error('Failed to load salesproject: ' . $e->getMessage());
121
            return false;
122
        }
123
124
        $message = [
125
            'title' => sprintf($this->_l10n->get('notification for agreement %s'), $deliverable->title),
126
            'content' => sprintf(
127
                $this->_l10n->get('agreement %s ends on %s. click here: %s'),
128
                $deliverable->title,
129
                $this->_l10n->get_formatter()->date($deliverable->end),
130
                midcom::get()->permalinks->create_permalink($deliverable->guid)
131
            )
132
        ];
133
134
        return org_openpsa_notifications::notify('org.openpsa.sales:new_notification_message', $project->owner, $message);
135
    }
136
}
137