Completed
Branch FET/event-question-group-refac... (7e8a15)
by
unknown
27:06 queued 18:26
created

reindexAnswersByQuestionId()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\services\registration;
4
5
use EE_Answer;
6
use EE_Error;
7
use EE_Payment;
8
use EE_Question;
9
use EE_Registration;
10
use EE_Registration_Payment;
11
use EEM_Event_Question_Group;
12
use EventEspresso\core\domain\services\DomainService;
13
use EventEspresso\core\exceptions\EntityNotFoundException;
14
use EventEspresso\core\exceptions\UnexpectedEntityException;
15
use RuntimeException;
16
17
/**
18
 * Class CopyRegistrationService
19
 * Given two EE_Registrations,
20
 * will copy data from one registration to the target,
21
 *
22
 * @package       Event Espresso
23
 * @author        Brent Christensen
24
 * @since         4.9.1
25
 */
26
class CopyRegistrationService extends DomainService
27
{
28
29
30
    /**
31
     * @param EE_Registration $target_registration
32
     * @param EE_Registration $registration_to_copy
33
     * @return bool
34
     * @throws UnexpectedEntityException
35
     * @throws EntityNotFoundException
36
     * @throws RuntimeException
37
     * @throws EE_Error
38
     */
39
    public function copyRegistrationDetails(
40
        EE_Registration $target_registration,
41
        EE_Registration $registration_to_copy
42
    ) {
43
        // copy attendee
44
        $target_registration->set_attendee_id($registration_to_copy->attendee_ID());
45
        $target_registration->updateStatusBasedOnTotalPaid(false);
46
        $target_registration->save();
47
        // get answers to previous reg questions
48
        $answers = $this->reindexAnswersByQuestionId($registration_to_copy->answers());
49
        // get questions to new event reg form
50
        $new_event = $target_registration->event();
51
        $field_name = 'Event_Question_Group.'
52
            . EEM_Event_Question_Group::instance()->field_name_for_category(
53
                $registration_to_copy->is_primary_registrant()
54
            );
55
        $question_groups = $new_event->question_groups([
56
                [
57
                    'Event.EVT_ID' => $new_event->ID(),
58
                    $field_name => true,
59
                ],
60
                'order_by' => ['QSG_order' => 'ASC'],
61
            ]
62
63
        );
64
        foreach ($question_groups as $question_group) {
65
            if ($question_group instanceof \EE_Question_Group) {
66
                foreach ($question_group->questions() as $question) {
67
                    if ($question instanceof EE_Question) {
68
                        $this->generateNewAnswer(
69
                            $question,
70
                            $target_registration,
71
                            $answers
72
                        );
73
                    }
74
                }
75
            }
76
        }
77
        return true;
78
    }
79
80
81
    /**
82
     * @param EE_Answer[] $answers
83
     * @return array
84
     * @throws EE_Error
85
     */
86
    protected function reindexAnswersByQuestionId(array $answers)
87
    {
88
        $reindexed_answers = array();
89
        foreach ($answers as $answer) {
90
            if ($answer instanceof EE_Answer) {
91
                $reindexed_answers[ $answer->question_ID() ] = $answer->value();
92
            }
93
        }
94
        return $reindexed_answers;
95
    }
96
97
98
    /**
99
     * @param EE_Question      $question
100
     * @param EE_Registration  $registration
101
     * @param                  $previous_answers
102
     * @return EE_Answer
103
     * @throws UnexpectedEntityException
104
     * @throws EE_Error
105
     */
106
    protected function generateNewAnswer(
107
        EE_Question $question,
108
        EE_Registration $registration,
109
        $previous_answers
110
    ) {
111
        $old_answer_value = isset($previous_answers[ $question->ID() ])
112
            ? $previous_answers[ $question->ID() ]
113
            : '';
114
        $new_answer = EE_Answer::new_instance(
115
            array(
116
                'QST_ID'    => $question->ID(),
117
                'REG_ID'    => $registration->ID(),
118
                'ANS_value' => $old_answer_value,
119
            )
120
        );
121
        if (! $new_answer instanceof EE_Answer) {
122
            throw new UnexpectedEntityException($new_answer, 'EE_Answer');
123
        }
124
        $new_answer->save();
125
        return $new_answer;
126
    }
127
128
129
    /**
130
     * @param EE_Registration $target_registration
131
     * @param EE_Registration $registration_to_copy
132
     * @return bool
133
     * @throws RuntimeException
134
     * @throws UnexpectedEntityException
135
     * @throws EE_Error
136
     */
137
    public function copyPaymentDetails(
138
        EE_Registration $target_registration,
139
        EE_Registration $registration_to_copy
140
    ) {
141
        $save = false;
142
        $previous_registration_payments = $registration_to_copy->registration_payments();
143
        $new_registration_payment_total = 0;
144
        $registration_to_copy_total = $registration_to_copy->paid();
145
        foreach ($previous_registration_payments as $previous_registration_payment) {
146
            if ($previous_registration_payment instanceof EE_Registration_Payment
147
                && $previous_registration_payment->payment() instanceof EE_Payment
148
                && $previous_registration_payment->payment()->is_approved()
149
            ) {
150
                $payment_amount = $previous_registration_payment->amount();
151
                $new_registration_payment = EE_Registration_Payment::new_instance(
152
                    array(
153
                        'REG_ID'     => $target_registration->ID(),
154
                        'PAY_ID'     => $previous_registration_payment->payment()->ID(),
155
                        'RPY_amount' => $payment_amount,
156
                    )
157
                );
158
                if (! $new_registration_payment instanceof EE_Registration_Payment) {
159
                    throw new UnexpectedEntityException($new_registration_payment, 'EE_Registration_Payment');
160
                }
161
                $new_registration_payment->save();
162
                // if new reg payment is good, then set old reg payment amount to zero
163
                $previous_registration_payment->set_amount(0);
164
                $previous_registration_payment->save();
165
                // now  increment/decrement payment amounts
166
                $new_registration_payment_total += $payment_amount;
167
                $registration_to_copy_total -= $payment_amount;
168
                $save = true;
169
            }
170
        }
171
        if ($save) {
172
            $target_registration->set_paid($new_registration_payment_total);
173
            $target_registration->save();
174
            $registration_to_copy->set_paid($registration_to_copy_total);
175
            $registration_to_copy->save();
176
        }
177
        return true;
178
    }
179
}
180