Completed
Branch FET/add-copy-promo-line-item-c... (c75a13)
by
unknown
13:36 queued 09:36
created
core/domain/services/registration/CopyRegistrationService.php 2 patches
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -88,7 +88,7 @@  discard block
 block discarded – undo
88 88
         $reindexed_answers = array();
89 89
         foreach ($answers as $answer) {
90 90
             if ($answer instanceof EE_Answer) {
91
-                $reindexed_answers[ $answer->question_ID() ] = $answer->value();
91
+                $reindexed_answers[$answer->question_ID()] = $answer->value();
92 92
             }
93 93
         }
94 94
         return $reindexed_answers;
@@ -108,8 +108,8 @@  discard block
 block discarded – undo
108 108
         EE_Registration $registration,
109 109
         $previous_answers
110 110
     ) {
111
-        $old_answer_value = isset($previous_answers[ $question->ID() ])
112
-            ? $previous_answers[ $question->ID() ]
111
+        $old_answer_value = isset($previous_answers[$question->ID()])
112
+            ? $previous_answers[$question->ID()]
113 113
             : '';
114 114
         $new_answer = EE_Answer::new_instance(
115 115
             array(
@@ -118,7 +118,7 @@  discard block
 block discarded – undo
118 118
                 'ANS_value' => $old_answer_value,
119 119
             )
120 120
         );
121
-        if (! $new_answer instanceof EE_Answer) {
121
+        if ( ! $new_answer instanceof EE_Answer) {
122 122
             throw new UnexpectedEntityException($new_answer, 'EE_Answer');
123 123
         }
124 124
         $new_answer->save();
@@ -155,7 +155,7 @@  discard block
 block discarded – undo
155 155
                         'RPY_amount' => $payment_amount,
156 156
                     )
157 157
                 );
158
-                if (! $new_registration_payment instanceof EE_Registration_Payment) {
158
+                if ( ! $new_registration_payment instanceof EE_Registration_Payment) {
159 159
                     throw new UnexpectedEntityException($new_registration_payment, 'EE_Registration_Payment');
160 160
                 }
161 161
                 $new_registration_payment->save();
@@ -207,7 +207,7 @@  discard block
 block discarded – undo
207 207
 
208 208
             foreach ($promo_line_items as $promo_line_item) {
209 209
                 if ($promo_line_item instanceof \EE_Line_Item) {
210
-                    $promotion =  EED_Promotions::instance()->get_promotion_from_line_item($promo_line_item);
210
+                    $promotion = EED_Promotions::instance()->get_promotion_from_line_item($promo_line_item);
211 211
                     EED_Promotions::instance()->generate_promotion_line_items(
212 212
                         $promotion,
213 213
                         array($new_event_line_item),
Please login to merge, or discard this patch.
Indentation   +178 added lines, -178 removed lines patch added patch discarded remove patch
@@ -27,193 +27,193 @@
 block discarded – undo
27 27
  */
28 28
 class CopyRegistrationService extends DomainService
29 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()->fieldNameForContext(
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
-        foreach ($question_groups as $question_group) {
63
-            if ($question_group instanceof \EE_Question_Group) {
64
-                foreach ($question_group->questions() as $question) {
65
-                    if ($question instanceof EE_Question) {
66
-                        $this->generateNewAnswer(
67
-                            $question,
68
-                            $target_registration,
69
-                            $answers
70
-                        );
71
-                    }
72
-                }
73
-            }
74
-        }
75
-        return true;
76
-    }
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()->fieldNameForContext(
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
+		foreach ($question_groups as $question_group) {
63
+			if ($question_group instanceof \EE_Question_Group) {
64
+				foreach ($question_group->questions() as $question) {
65
+					if ($question instanceof EE_Question) {
66
+						$this->generateNewAnswer(
67
+							$question,
68
+							$target_registration,
69
+							$answers
70
+						);
71
+					}
72
+				}
73
+			}
74
+		}
75
+		return true;
76
+	}
77 77
 
78 78
 
79
-    /**
80
-     * @param EE_Answer[] $answers
81
-     * @return array
82
-     * @throws EE_Error
83
-     */
84
-    protected function reindexAnswersByQuestionId(array $answers)
85
-    {
86
-        $reindexed_answers = array();
87
-        foreach ($answers as $answer) {
88
-            if ($answer instanceof EE_Answer) {
89
-                $reindexed_answers[ $answer->question_ID() ] = $answer->value();
90
-            }
91
-        }
92
-        return $reindexed_answers;
93
-    }
79
+	/**
80
+	 * @param EE_Answer[] $answers
81
+	 * @return array
82
+	 * @throws EE_Error
83
+	 */
84
+	protected function reindexAnswersByQuestionId(array $answers)
85
+	{
86
+		$reindexed_answers = array();
87
+		foreach ($answers as $answer) {
88
+			if ($answer instanceof EE_Answer) {
89
+				$reindexed_answers[ $answer->question_ID() ] = $answer->value();
90
+			}
91
+		}
92
+		return $reindexed_answers;
93
+	}
94 94
 
95 95
 
96
-    /**
97
-     * @param EE_Question      $question
98
-     * @param EE_Registration  $registration
99
-     * @param                  $previous_answers
100
-     * @return EE_Answer
101
-     * @throws UnexpectedEntityException
102
-     * @throws EE_Error
103
-     */
104
-    protected function generateNewAnswer(
105
-        EE_Question $question,
106
-        EE_Registration $registration,
107
-        $previous_answers
108
-    ) {
109
-        $old_answer_value = isset($previous_answers[ $question->ID() ])
110
-            ? $previous_answers[ $question->ID() ]
111
-            : '';
112
-        $new_answer = EE_Answer::new_instance(
113
-            array(
114
-                'QST_ID'    => $question->ID(),
115
-                'REG_ID'    => $registration->ID(),
116
-                'ANS_value' => $old_answer_value,
117
-            )
118
-        );
119
-        if (! $new_answer instanceof EE_Answer) {
120
-            throw new UnexpectedEntityException($new_answer, 'EE_Answer');
121
-        }
122
-        $new_answer->save();
123
-        return $new_answer;
124
-    }
96
+	/**
97
+	 * @param EE_Question      $question
98
+	 * @param EE_Registration  $registration
99
+	 * @param                  $previous_answers
100
+	 * @return EE_Answer
101
+	 * @throws UnexpectedEntityException
102
+	 * @throws EE_Error
103
+	 */
104
+	protected function generateNewAnswer(
105
+		EE_Question $question,
106
+		EE_Registration $registration,
107
+		$previous_answers
108
+	) {
109
+		$old_answer_value = isset($previous_answers[ $question->ID() ])
110
+			? $previous_answers[ $question->ID() ]
111
+			: '';
112
+		$new_answer = EE_Answer::new_instance(
113
+			array(
114
+				'QST_ID'    => $question->ID(),
115
+				'REG_ID'    => $registration->ID(),
116
+				'ANS_value' => $old_answer_value,
117
+			)
118
+		);
119
+		if (! $new_answer instanceof EE_Answer) {
120
+			throw new UnexpectedEntityException($new_answer, 'EE_Answer');
121
+		}
122
+		$new_answer->save();
123
+		return $new_answer;
124
+	}
125 125
 
126 126
 
127
-    /**
128
-     * @param EE_Registration $target_registration
129
-     * @param EE_Registration $registration_to_copy
130
-     * @return bool
131
-     * @throws RuntimeException
132
-     * @throws UnexpectedEntityException
133
-     * @throws EE_Error
134
-     */
135
-    public function copyPaymentDetails(
136
-        EE_Registration $target_registration,
137
-        EE_Registration $registration_to_copy
138
-    ) {
139
-        $save = false;
140
-        $previous_registration_payments = $registration_to_copy->registration_payments();
141
-        $new_registration_payment_total = 0;
142
-        $registration_to_copy_total = $registration_to_copy->paid();
143
-        foreach ($previous_registration_payments as $previous_registration_payment) {
144
-            if (
145
-                $previous_registration_payment instanceof EE_Registration_Payment
146
-                && $previous_registration_payment->payment() instanceof EE_Payment
147
-                && $previous_registration_payment->payment()->is_approved()
148
-            ) {
149
-                $payment_amount = $previous_registration_payment->amount();
150
-                $new_registration_payment = EE_Registration_Payment::new_instance(
151
-                    array(
152
-                        'REG_ID'     => $target_registration->ID(),
153
-                        'PAY_ID'     => $previous_registration_payment->payment()->ID(),
154
-                        'RPY_amount' => $payment_amount,
155
-                    )
156
-                );
157
-                if (! $new_registration_payment instanceof EE_Registration_Payment) {
158
-                    throw new UnexpectedEntityException($new_registration_payment, 'EE_Registration_Payment');
159
-                }
160
-                $new_registration_payment->save();
161
-                // if new reg payment is good, then set old reg payment amount to zero
162
-                $previous_registration_payment->set_amount(0);
163
-                $previous_registration_payment->save();
164
-                // now  increment/decrement payment amounts
165
-                $new_registration_payment_total += $payment_amount;
166
-                $registration_to_copy_total -= $payment_amount;
167
-                $save = true;
168
-            }
169
-        }
170
-        if ($save) {
171
-            $target_registration->set_paid($new_registration_payment_total);
172
-            $target_registration->save();
173
-            $registration_to_copy->set_paid($registration_to_copy_total);
174
-            $registration_to_copy->save();
175
-        }
176
-        return true;
177
-    }
127
+	/**
128
+	 * @param EE_Registration $target_registration
129
+	 * @param EE_Registration $registration_to_copy
130
+	 * @return bool
131
+	 * @throws RuntimeException
132
+	 * @throws UnexpectedEntityException
133
+	 * @throws EE_Error
134
+	 */
135
+	public function copyPaymentDetails(
136
+		EE_Registration $target_registration,
137
+		EE_Registration $registration_to_copy
138
+	) {
139
+		$save = false;
140
+		$previous_registration_payments = $registration_to_copy->registration_payments();
141
+		$new_registration_payment_total = 0;
142
+		$registration_to_copy_total = $registration_to_copy->paid();
143
+		foreach ($previous_registration_payments as $previous_registration_payment) {
144
+			if (
145
+				$previous_registration_payment instanceof EE_Registration_Payment
146
+				&& $previous_registration_payment->payment() instanceof EE_Payment
147
+				&& $previous_registration_payment->payment()->is_approved()
148
+			) {
149
+				$payment_amount = $previous_registration_payment->amount();
150
+				$new_registration_payment = EE_Registration_Payment::new_instance(
151
+					array(
152
+						'REG_ID'     => $target_registration->ID(),
153
+						'PAY_ID'     => $previous_registration_payment->payment()->ID(),
154
+						'RPY_amount' => $payment_amount,
155
+					)
156
+				);
157
+				if (! $new_registration_payment instanceof EE_Registration_Payment) {
158
+					throw new UnexpectedEntityException($new_registration_payment, 'EE_Registration_Payment');
159
+				}
160
+				$new_registration_payment->save();
161
+				// if new reg payment is good, then set old reg payment amount to zero
162
+				$previous_registration_payment->set_amount(0);
163
+				$previous_registration_payment->save();
164
+				// now  increment/decrement payment amounts
165
+				$new_registration_payment_total += $payment_amount;
166
+				$registration_to_copy_total -= $payment_amount;
167
+				$save = true;
168
+			}
169
+		}
170
+		if ($save) {
171
+			$target_registration->set_paid($new_registration_payment_total);
172
+			$target_registration->save();
173
+			$registration_to_copy->set_paid($registration_to_copy_total);
174
+			$registration_to_copy->save();
175
+		}
176
+		return true;
177
+	}
178 178
 
179 179
 
180
-    /**
181
-     * @param EE_Registration $target_registration
182
-     * @param EE_Registration $registration_to_copy
183
-     * @return bool
184
-     * @throws RuntimeException
185
-     * @throws UnexpectedEntityException
186
-     * @throws EE_Error
187
-     */
188
-    public function copyPromotionLineItems(
189
-        EE_Registration $target_registration,
190
-        EE_Registration $registration_to_copy
191
-    ) {
192
-        $transaction = $registration_to_copy->transaction();
193
-        if ($transaction instanceof \EE_Transaction) {
194
-            $total_line_item = $transaction->total_line_item();
195
-            $old_event_line_item = EEH_Line_Item::get_event_line_item_for_ticket($total_line_item, $registration_to_copy->ticket());
196
-            $new_event_line_item = EEH_Line_Item::get_event_line_item_for_ticket($total_line_item, $target_registration->ticket());
197
-            $promo_line_items = EEH_Line_Item::get_line_items_of_object_type($old_event_line_item, \EEM_Line_Item::OBJ_TYPE_PROMOTION);
180
+	/**
181
+	 * @param EE_Registration $target_registration
182
+	 * @param EE_Registration $registration_to_copy
183
+	 * @return bool
184
+	 * @throws RuntimeException
185
+	 * @throws UnexpectedEntityException
186
+	 * @throws EE_Error
187
+	 */
188
+	public function copyPromotionLineItems(
189
+		EE_Registration $target_registration,
190
+		EE_Registration $registration_to_copy
191
+	) {
192
+		$transaction = $registration_to_copy->transaction();
193
+		if ($transaction instanceof \EE_Transaction) {
194
+			$total_line_item = $transaction->total_line_item();
195
+			$old_event_line_item = EEH_Line_Item::get_event_line_item_for_ticket($total_line_item, $registration_to_copy->ticket());
196
+			$new_event_line_item = EEH_Line_Item::get_event_line_item_for_ticket($total_line_item, $target_registration->ticket());
197
+			$promo_line_items = EEH_Line_Item::get_line_items_of_object_type($old_event_line_item, \EEM_Line_Item::OBJ_TYPE_PROMOTION);
198 198
 
199
-            // Bail if we don't have any promotion line items.
200
-            if (empty($promo_line_items)) {
201
-                return;
202
-            }
199
+			// Bail if we don't have any promotion line items.
200
+			if (empty($promo_line_items)) {
201
+				return;
202
+			}
203 203
 
204
-            // Don't increment 'usage' for copied promotions
205
-            add_filter('FHEE__EED_Promotions__add_promotion_line_item__bypass_increment_promotion_scope_uses', '__return_true');
204
+			// Don't increment 'usage' for copied promotions
205
+			add_filter('FHEE__EED_Promotions__add_promotion_line_item__bypass_increment_promotion_scope_uses', '__return_true');
206 206
 
207
-            foreach ($promo_line_items as $promo_line_item) {
208
-                if ($promo_line_item instanceof \EE_Line_Item) {
209
-                    $promotion =  EED_Promotions::instance()->get_promotion_from_line_item($promo_line_item);
210
-                    EED_Promotions::instance()->generate_promotion_line_items(
211
-                        $promotion,
212
-                        array($new_event_line_item),
213
-                        EED_Promotions::instance()->config()->affects_tax()
214
-                    );
215
-                }
216
-            }
217
-        }
218
-    }
207
+			foreach ($promo_line_items as $promo_line_item) {
208
+				if ($promo_line_item instanceof \EE_Line_Item) {
209
+					$promotion =  EED_Promotions::instance()->get_promotion_from_line_item($promo_line_item);
210
+					EED_Promotions::instance()->generate_promotion_line_items(
211
+						$promotion,
212
+						array($new_event_line_item),
213
+						EED_Promotions::instance()->config()->affects_tax()
214
+					);
215
+				}
216
+			}
217
+		}
218
+	}
219 219
 }
Please login to merge, or discard this patch.