Completed
Push — master ( e88a10...327e61 )
by Andreas
17:39
created

org_openpsa_sales_handler::process_notify_date()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 7.6513

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 19
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 27
ccs 7
cts 18
cp 0.3889
crap 7.6513
rs 9.6333
1
<?php
2
/**
3
 * @package org.openpsa.sales
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.sales
13
 */
14
trait org_openpsa_sales_handler
15
{
16
    /**
17
     * Function to process the notify date
18
     * creates/edits/deletes the corresponding at_entry if needed
19
     *
20
     * @param integer $notify The notify date
21
     * @param org_openpsa_sales_salesproject_deliverable_dba $deliverable The current deliverable
22
     */
23 3
    public function process_notify_date($notify, org_openpsa_sales_salesproject_deliverable_dba $deliverable)
24
    {
25
        //check if there is already an at_entry
26 3
        $mc = new org_openpsa_relatedto_collector($deliverable->guid, midcom_services_at_entry_dba::class);
27 3
        $mc->add_constraint('toClass', '=', org_openpsa_sales_salesproject_deliverable_dba::class);
28 3
        $mc->add_constraint('toExtra', '=', 'notify_at_entry');
29 3
        $at_entries = $mc->get_related_objects();
30
        //check date
31 3
        if ($notify) {
32
            if (empty($at_entries)) {
33
                $at_entry = new midcom_services_at_entry_dba;
34
                $at_entry->method = 'new_notification_message';
35
                $at_entry->component = 'org.openpsa.sales';
36
                $at_entry->arguments = ['deliverable' => $deliverable->guid];
37
                $at_entry->create();
38
                //relatedto from notification to deliverable
39
                org_openpsa_relatedto_plugin::create($at_entry, 'midcom.services.at', $deliverable, 'org.openpsa.sales', null, ['toExtra' => 'notify_at_entry']);
40
            } else {
41
                $at_entry = end($at_entries);
42
            }
43
            $at_entry->start = $notify;
44
            $at_entry->update();
45
        } else {
46
            //void date - so delete existing at_entries for this notify_date
47 3
            foreach ($at_entries as $at_entry) {
48
                //check if related at_entry exists & delete it
49
                $at_entry->delete();
50
            }
51
        }
52 3
    }
53
}
54