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