|
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) |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
68
|
|
|
$qb->begin_group('OR'); |
|
69
|
|
|
$qb->add_constraint('end', '>=', $object->start); |
|
|
|
|
|
|
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
|
|
|
foreach ($qb->execute() as $salesproject) { |
|
83
|
|
|
$to_array = ['other_obj' => false, 'link' => false]; |
|
84
|
|
|
$link = new org_openpsa_relatedto_dba(); |
|
85
|
|
|
org_openpsa_relatedto_suspect::defaults_helper($link, $defaults, $this->_component, $salesproject); |
|
86
|
|
|
$to_array['other_obj'] = $salesproject; |
|
87
|
|
|
$to_array['link'] = $link; |
|
88
|
|
|
|
|
89
|
|
|
$links_array[] = $to_array; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Used by org_openpsa_relatedto_find_suspects to in case the given object is a person |
|
95
|
|
|
*/ |
|
96
|
|
|
private function _find_suspects_person(midcom_core_dbaobject $object, org_openpsa_relatedto_dba $defaults, array &$links_array) |
|
97
|
|
|
{ |
|
98
|
|
|
$qb = org_openpsa_sales_salesproject_dba::new_query_builder(); |
|
99
|
|
|
$qb->add_constraint('state', '=', org_openpsa_sales_salesproject_dba::STATE_ACTIVE); |
|
100
|
|
|
$qb->begin_group('OR'); |
|
101
|
|
|
$mc = org_openpsa_contacts_role_dba::new_collector('role', org_openpsa_sales_salesproject_dba::ROLE_MEMBER); |
|
102
|
|
|
$mc->add_constraint('person', '=', $object->id); |
|
103
|
|
|
$qb->add_constraint('guid', 'IN', $mc->get_values('objectGuid')); |
|
104
|
|
|
$qb->add_constraint('owner', '=', $object->id); |
|
105
|
|
|
$qb->end_group(); |
|
106
|
|
|
|
|
107
|
|
|
foreach ($qb->execute() as $salesproject) { |
|
108
|
|
|
$link = new org_openpsa_relatedto_dba(); |
|
109
|
|
|
org_openpsa_relatedto_suspect::defaults_helper($link, $defaults, $this->_component, $salesproject); |
|
110
|
|
|
|
|
111
|
|
|
$links_array[] = [ |
|
112
|
|
|
'other_obj' => $salesproject, |
|
113
|
|
|
'link' => $link |
|
114
|
|
|
]; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* AT handler for handling subscription cycles. |
|
120
|
|
|
* |
|
121
|
|
|
* @param array $args handler arguments |
|
122
|
|
|
* @param midcom_baseclasses_components_cron_handler $handler cron_handler object calling this method. |
|
123
|
|
|
* @return boolean indicating success/failure |
|
124
|
|
|
*/ |
|
125
|
|
|
public function new_subscription_cycle(array $args, midcom_baseclasses_components_cron_handler $handler) |
|
126
|
|
|
{ |
|
127
|
|
|
if (!isset($args['deliverable'], $args['cycle'])) { |
|
128
|
|
|
$handler->print_error('deliverable GUID or cycle number not set, aborting'); |
|
129
|
|
|
return false; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
try { |
|
133
|
|
|
$deliverable = new org_openpsa_sales_salesproject_deliverable_dba($args['deliverable']); |
|
134
|
|
|
} catch (midcom_error $e) { |
|
135
|
|
|
$handler->print_error("Deliverable {$args['deliverable']} not found: " . midcom_connection::get_error_string()); |
|
136
|
|
|
return false; |
|
137
|
|
|
} |
|
138
|
|
|
$scheduler = new org_openpsa_invoices_scheduler($deliverable); |
|
139
|
|
|
|
|
140
|
|
|
return $scheduler->run_cycle($args['cycle']); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Function to send a notification to owner of the deliverable - guid of deliverable is passed |
|
145
|
|
|
* |
|
146
|
|
|
* @param array $args handler arguments |
|
147
|
|
|
* @param midcom_baseclasses_components_cron_handler $handler cron_handler object calling this method. |
|
148
|
|
|
* @return boolean indicating success/failure |
|
149
|
|
|
*/ |
|
150
|
|
|
public function new_notification_message(array $args, midcom_baseclasses_components_cron_handler $handler) |
|
151
|
|
|
{ |
|
152
|
|
|
if (!isset($args['deliverable'])) { |
|
153
|
|
|
$handler->print_error('deliverable GUID not set, aborting'); |
|
154
|
|
|
return false; |
|
155
|
|
|
} |
|
156
|
|
|
try { |
|
157
|
|
|
$deliverable = new org_openpsa_sales_salesproject_deliverable_dba($args['deliverable']); |
|
158
|
|
|
} catch (midcom_error $e) { |
|
159
|
|
|
$handler->print_error('no deliverable with passed GUID: ' . $args['deliverable'] . ', aborting'); |
|
160
|
|
|
return false; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
//get the owner of the salesproject the deliverable belongs to |
|
164
|
|
|
try { |
|
165
|
|
|
$project = new org_openpsa_sales_salesproject_dba($deliverable->salesproject); |
|
166
|
|
|
} catch (midcom_error $e) { |
|
167
|
|
|
$handler->print_error('Failed to load salesproject: ' . $e->getMessage()); |
|
168
|
|
|
return false; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
$message = [ |
|
172
|
|
|
'title' => sprintf($this->_l10n->get('notification for agreement %s'), $deliverable->title), |
|
173
|
|
|
'content' => sprintf( |
|
174
|
|
|
$this->_l10n->get('agreement %s ends on %s. click here: %s'), |
|
175
|
|
|
$deliverable->title, |
|
176
|
|
|
$this->_l10n->get_formatter()->date($deliverable->end), |
|
177
|
|
|
midcom::get()->permalinks->create_permalink($deliverable->guid) |
|
178
|
|
|
) |
|
179
|
|
|
]; |
|
180
|
|
|
|
|
181
|
|
|
return org_openpsa_notifications::notify('org.openpsa.sales:new_notification_message', $project->owner, $message); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|