Completed
Branch fix-dummy-related-question-qst... (e5efcf)
by
unknown
07:49 queued 03:45
created
data_migration_scripts/4_1_0_stages/EE_DMS_4_1_0_attendees.dmsstage.php 2 patches
Indentation   +636 added lines, -636 removed lines patch added patch discarded remove patch
@@ -183,647 +183,647 @@
 block discarded – undo
183 183
  */
184 184
 class EE_DMS_4_1_0_attendees extends EE_Data_Migration_Script_Stage_Table
185 185
 {
186
-    private $_new_attendee_cpt_table;
187
-    private $_new_attendee_meta_table;
188
-    private $_new_reg_table;
189
-    private $_new_transaction_table;
190
-    private $_new_payment_table;
191
-    private $_new_line_table;
192
-    private $_old_mer_table;
193
-    private $_new_ticket_table;
194
-    private $_new_ticket_datetime_table;
195
-    private $_new_datetime_table;
196
-    private $_new_datetime_ticket_table;
197
-    private $_new_price_table;
198
-    private $_new_ticket_price_table;
199
-    /**
200
-     * Rememebrs whether or not the mer table exists
201
-     * @var boolean
202
-     */
203
-    private $_mer_tables_exist = null;
204
-
205
-    public function __construct()
206
-    {
207
-        global $wpdb;
208
-        $this->_pretty_name = esc_html__("Attendees", "event_espresso");
209
-        $this->_old_table = $wpdb->prefix . "events_attendee";
210
-        // Only select the event status column from the event table.
211
-        $this->select_expression = 'att.*, e.event_status';
212
-        // Only select attendees for events that aren't deleted.
213
-        $this->_extra_where_sql = 'AS att
186
+	private $_new_attendee_cpt_table;
187
+	private $_new_attendee_meta_table;
188
+	private $_new_reg_table;
189
+	private $_new_transaction_table;
190
+	private $_new_payment_table;
191
+	private $_new_line_table;
192
+	private $_old_mer_table;
193
+	private $_new_ticket_table;
194
+	private $_new_ticket_datetime_table;
195
+	private $_new_datetime_table;
196
+	private $_new_datetime_ticket_table;
197
+	private $_new_price_table;
198
+	private $_new_ticket_price_table;
199
+	/**
200
+	 * Rememebrs whether or not the mer table exists
201
+	 * @var boolean
202
+	 */
203
+	private $_mer_tables_exist = null;
204
+
205
+	public function __construct()
206
+	{
207
+		global $wpdb;
208
+		$this->_pretty_name = esc_html__("Attendees", "event_espresso");
209
+		$this->_old_table = $wpdb->prefix . "events_attendee";
210
+		// Only select the event status column from the event table.
211
+		$this->select_expression = 'att.*, e.event_status';
212
+		// Only select attendees for events that aren't deleted.
213
+		$this->_extra_where_sql = 'AS att
214 214
             INNER JOIN ' . $wpdb->prefix . 'events_detail AS e ON att.event_id=e.id
215 215
             WHERE e.event_status!="D"';
216
-        $this->_old_mer_table = $wpdb->prefix . "events_multi_event_registration_id_group";
217
-        $this->_new_attendee_cpt_table = $wpdb->posts;
218
-        $this->_new_attendee_meta_table = $wpdb->prefix . "esp_attendee_meta";
219
-        $this->_new_reg_table = $wpdb->prefix . "esp_registration";
220
-        $this->_new_transaction_table = $wpdb->prefix . "esp_transaction";
221
-        $this->_new_payment_table = $wpdb->prefix . "esp_payment";
222
-        $this->_new_line_table = $wpdb->prefix . "esp_line_item";
223
-        $this->_new_ticket_table = $wpdb->prefix . "esp_ticket";
224
-        $this->_new_ticket_datetime_table = $wpdb->prefix . "esp_datetime_ticket";
225
-        $this->_new_datetime_table = $wpdb->prefix . "esp_datetime";
226
-        $this->_new_datetime_ticket_table = $wpdb->prefix . "esp_datetime_ticket";
227
-        $this->_new_price_table = $wpdb->prefix . "esp_price";
228
-        $this->_new_ticket_price_table = $wpdb->prefix . "esp_ticket_price";
229
-        parent::__construct();
230
-    }
231
-
232
-    protected function _migrate_old_row($old_row)
233
-    {
234
-        // first check if there's already a new attendee with similar characteristics
235
-        $new_att_id = $this->_find_attendee_cpt_matching($old_row);
236
-        if (!$new_att_id) {
237
-            $new_att_id = $this->_insert_new_attendee_cpt($old_row);
238
-            if (!$new_att_id) {
239
-                // if we couldnt even make an attendee, abandon all hope
240
-                return false;
241
-            }
242
-            $new_att_meta_id = $this->_insert_attendee_meta_row($old_row, $new_att_id);
243
-            if ($new_att_meta_id) {
244
-                $this->get_migration_script()->set_mapping($this->_old_table, $old_row['id'], $this->_new_attendee_meta_table, $new_att_meta_id);
245
-            }
246
-        }
247
-        $this->get_migration_script()->set_mapping($this->_old_table, $old_row['id'], $this->_new_attendee_cpt_table, $new_att_id);
248
-
249
-        $txn_id = $this->_insert_new_transaction($old_row);
250
-        if (!$txn_id) {
251
-            // if we couldnt make the transaction, also abandon all hope
252
-            return false;
253
-        }
254
-        $this->get_migration_script()->set_mapping($this->_old_table, $old_row['id'], $this->_new_transaction_table, $txn_id);
255
-        $pay_id = $this->_insert_new_payment($old_row, $txn_id);
256
-        if ($pay_id) {
257
-            $this->get_migration_script()->set_mapping($this->_old_table, $old_row['id'], $this->_new_payment_table, $pay_id);
258
-        }
259
-
260
-
261
-        // even if there was no payment, we can go ahead with adding the reg
262
-        $new_regs = $this->_insert_new_registrations($old_row, $new_att_id, $txn_id);
263
-        if ($new_regs) {
264
-            $this->get_migration_script()->set_mapping($this->_old_table, $old_row['id'], $this->_new_reg_table, $new_regs);
265
-        }
266
-    }
267
-
268
-    /**
269
-     * Checks if there's already an attendee CPT in the db that has the same
270
-     * first and last name, and email. If so, returns its ID as an int.
271
-     * @global type $wpdb
272
-     * @param array $old_attendee
273
-     * @return int
274
-     */
275
-    private function _find_attendee_cpt_matching($old_attendee)
276
-    {
277
-        global $wpdb;
278
-        $existing_attendee_id = $wpdb->get_var($wpdb->prepare("SELECT id FROM " . $this->_new_attendee_cpt_table . " AS cpt INNER JOIN " . $this->_new_attendee_meta_table . " AS meta ON cpt.ID = meta.ATT_ID WHERE meta.ATT_fname = %s AND meta.ATT_lname = %s AND meta.ATT_email = %s LIMIT 1", $old_attendee['fname'], $old_attendee['lname'], $old_attendee['email']));
279
-        return intval($existing_attendee_id);
280
-    }
281
-
282
-    private function _insert_new_attendee_cpt($old_attendee)
283
-    {
284
-        global $wpdb;
285
-        $cols_n_values = array(
286
-            'post_title' => stripslashes($old_attendee['fname'] . " " . $old_attendee['lname']),// ATT_full_name
287
-            'post_content' => '',// ATT_bio
288
-            'post_name' => sanitize_title($old_attendee['fname'] . "-" . $old_attendee['lname']),// ATT_slug
289
-            'post_date' => $this->get_migration_script()->convert_date_string_to_utc($this, $old_attendee, $old_attendee['date']),// ATT_created
290
-            'post_excerpt' => '',// ATT_short_bio
291
-            'post_modified' => $this->get_migration_script()->convert_date_string_to_utc($this, $old_attendee, $old_attendee['date']),// ATT_modified
292
-            'post_author' => 0,// ATT_author
293
-            'post_parent' => 0,// ATT_parent
294
-            'post_type' => 'espresso_attendees',// post_type
295
-            'post_status' => 'publish'// status
296
-        );
297
-        $datatypes = array(
298
-            '%s',// ATT_full_name
299
-            '%s',// ATT_bio
300
-            '%s',// ATT_slug
301
-            '%s',// ATT_created
302
-            '%s',// ATT_short_bio
303
-            '%s',// ATT_modified
304
-            '%d',// ATT_author
305
-            '%d',// ATT_parent
306
-            '%s',// post_type
307
-            '%s',// status
308
-        );
309
-        $success = $wpdb->insert($this->_new_attendee_cpt_table, $cols_n_values, $datatypes);
310
-        if (!$success) {
311
-            $this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, $old_attendee, $this->_new_attendee_cpt_table, $cols_n_values, $datatypes));
312
-            return 0;
313
-        }
314
-        $new_id = $wpdb->insert_id;
315
-        return $new_id;
316
-    }
317
-
318
-    private function _insert_attendee_meta_row($old_attendee, $new_attendee_cpt_id)
319
-    {
320
-        global $wpdb;
321
-        // get the state and country ids from the old row
322
-        try {
323
-            $new_country = $this->get_migration_script()->get_or_create_country(stripslashes($old_attendee['country_id']));
324
-            $new_country_iso = $new_country['CNT_ISO'];
325
-        } catch (EE_Error $exception) {
326
-            $new_country_iso = $this->get_migration_script()->get_default_country_iso();
327
-        }
328
-        try {
329
-            $new_state = $this->get_migration_script()->get_or_create_state(stripslashes($old_attendee['state']), $new_country_iso);
330
-            $new_state_id = $new_state['STA_ID'];
331
-        } catch (EE_Error $exception) {
332
-            $new_state_id = 0;
333
-        }
334
-        $cols_n_values = array(
335
-            'ATT_ID' => $new_attendee_cpt_id,
336
-            'ATT_fname' => stripslashes($old_attendee['fname']),
337
-            'ATT_lname' => stripslashes($old_attendee['lname']),
338
-            'ATT_address' => stripslashes($old_attendee['address']),
339
-            'ATT_address2' => stripslashes($old_attendee['address2']),
340
-            'ATT_city' => stripslashes($old_attendee['city']),
341
-            'STA_ID' => $new_state_id,
342
-            'CNT_ISO' => $new_country_iso,
343
-            'ATT_zip' => stripslashes($old_attendee['zip']),
344
-            'ATT_email' => stripslashes($old_attendee['email']),
345
-            'ATT_phone' => stripslashes($old_attendee['phone']),
346
-        );
347
-        $datatypes = array(
348
-            '%d',// ATT_ID
349
-            '%s',// ATT_fname
350
-            '%s',// ATT_lname
351
-            '%s',// ATT_address
352
-            '%s',// ATT_address2
353
-            '%s',// ATT_city
354
-            '%d',// STA_ID
355
-            '%s',// CNT_ISO
356
-            '%s',// ATT_zip
357
-            '%s',// ATT_email
358
-            '%s',// ATT_phone
359
-        );
360
-        $success = $wpdb->insert($this->_new_attendee_meta_table, $cols_n_values, $datatypes);
361
-        if (!$success) {
362
-            $this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, $old_attendee, $this->_new_attendee_meta_table, $cols_n_values, $datatypes));
363
-            return 0;
364
-        }
365
-        $new_id = $wpdb->insert_id;
366
-        return $new_id;
367
-    }
368
-
369
-    /**
370
-     * Note: we don't necessarily create a new transaction for each attendee row.
371
-     * Only if the old attendee 'is_primary' is true; otherwise we find the old attendee row that
372
-     * 'is_primary' and has the same 'txn_id', then we return ITS new transaction id
373
-     * @global type $wpdb
374
-     * @param type $old_attendee
375
-     * @return int new transaction id
376
-     */
377
-    private function _insert_new_transaction($old_attendee)
378
-    {
379
-        global $wpdb;
380
-
381
-        // first: let's check for an existing transaction for this old attendee
382
-        if (intval($old_attendee['is_primary'])) {// primary attendee, so create txn
383
-            $txn_id = $this->get_migration_script()->get_mapping_new_pk($this->_old_table, intval($old_attendee['id']), $this->_new_transaction_table);
384
-        } else { // non-primary attendee, so find its primary attendee's transaction
385
-            $primary_attendee_old_id = $wpdb->get_var($wpdb->prepare("SELECT id FROM " . $this->_old_table . " WHERE is_primary=1 and registration_id=%s", $old_attendee['registration_id']));
386
-            if (!$primary_attendee_old_id) {
387
-                $primary_attendee = $this->_find_mer_primary_attendee_using_mer_tables($old_attendee['registration_id']);
388
-                $primary_attendee_old_id = is_array($primary_attendee) ? $primary_attendee['id'] : null;
389
-            }
390
-            $txn_id = $this->get_migration_script()->get_mapping_new_pk($this->_old_table, intval($primary_attendee_old_id), $this->_new_transaction_table);
391
-            if (!$txn_id) {
392
-                $this->add_error(sprintf(esc_html__("Could not find primary attendee's new transaction. Current attendee is: %s, we think the 3.1 primary attendee for it has id %d, but there's no 4.1 transaction for that primary attendee id.", "event_espresso"), $this->_json_encode($old_attendee), $primary_attendee_old_id));
393
-                $txn_id = 0;
394
-            }
395
-        }
396
-        // if there isn't yet a transaction row for this, create one
397
-        // (so even if it was a non-primary attendee with no EE3 primary attendee,
398
-        // it ought to have SOME transaction, so we'll make one)
399
-        if (!$txn_id) {
400
-            // maps 3.1 payment stati onto 4.1 transaction stati
401
-            $txn_status_mapping = array(
402
-                'Completed' => 'TCM',
403
-                'Pending' => 'TIN',
404
-                'Payment Declined' => 'TIN',
405
-                'Incomplete' => 'TIN',
406
-                'Not Completed' => 'TIN',
407
-                'Cancelled' => 'TIN',
408
-                'Declined' => 'TIN'
409
-            );
410
-            $STS_ID = isset($txn_status_mapping[ $old_attendee['payment_status'] ]) ? $txn_status_mapping[ $old_attendee['payment_status'] ] : 'TIN';
411
-            $cols_n_values = array(
412
-                'TXN_timestamp' => $this->get_migration_script()->convert_date_string_to_utc($this, $old_attendee, $old_attendee['date']),
413
-                'TXN_total' => floatval($old_attendee['total_cost']),
414
-                'TXN_paid' => floatval($old_attendee['amount_pd']),
415
-                'STS_ID' => $STS_ID,
416
-                'TXN_hash_salt' => $old_attendee['hashSalt']
417
-            );
418
-            $datatypes = array(
419
-                '%s',// TXN_timestamp
420
-                '%f',// TXN_total
421
-                '%f',// TXN_paid
422
-                '%s',// STS_ID
423
-                '%s',// TXN_hash_salt
424
-            );
425
-            $success = $wpdb->insert($this->_new_transaction_table, $cols_n_values, $datatypes);
426
-            if (!$success) {
427
-                $this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, $old_attendee, $this->_new_transaction_table, $cols_n_values, $datatypes));
428
-                return 0;
429
-            }
430
-            $txn_id = $wpdb->insert_id;
431
-        }
432
-
433
-        return $txn_id;
434
-    }
435
-
436
-    /**
437
-     * Detects if the MER tables exist
438
-     * @global type $wpdb
439
-     * @return boolean
440
-     */
441
-    private function _mer_tables_exist()
442
-    {
443
-        if ($this->_mer_tables_exist === null) {
444
-            global $wpdb;
445
-
446
-            if ($wpdb->get_var("SHOW TABLES LIKE '{$this->_old_mer_table}'") != $this->_old_mer_table) {
447
-                $this->_mer_tables_exist = false;
448
-            } else {
449
-                $this->_mer_tables_exist = true;
450
-            }
451
-        }
452
-        return $this->_mer_tables_exist;
453
-    }
454
-
455
-    /**
456
-     * Gets the 4.1 registration's status given the 3.1 attendee row. We consider
457
-     * whether the event required pre-approval or not,a dn the 4.1 payment status.
458
-     * @global type $wpdb
459
-     * @param type $old_attendee_row
460
-     * @return string
461
-     */
462
-    private function _get_reg_status_for_old_payment_status($old_attendee_row)
463
-    {
464
-        // need event default reg status and if pre_approval was required
465
-        global $wpdb;
466
-        $event_required_pre_approval = $wpdb->get_var($wpdb->prepare("SELECT require_pre_approval FROM " . $wpdb->prefix . "events_detail WHERE id = %d", $old_attendee_row['event_id']));
467
-        return $this->get_migration_script()->convert_3_1_payment_status_to_4_1_STS_ID(
468
-            $old_attendee_row['payment_status'],
469
-            intval($event_required_pre_approval) && intval($old_attendee_row['pre_approve'])
470
-        );
471
-    }
472
-
473
-    /**
474
-     * Adds however many rgistrations are indicated by the old attendee's QUANTITY field,
475
-     * and returns an array of their IDs
476
-     * @global type $wpdb
477
-     * @param array $old_attendee
478
-     * @param int $new_attendee_id
479
-     * @param int $new_txn_id
480
-     * @return array of new registratio ids
481
-     */
482
-    private function _insert_new_registrations($old_attendee, $new_attendee_id, $new_txn_id)
483
-    {
484
-        global $wpdb;
485
-
486
-        $STS_ID = $this->_get_reg_status_for_old_payment_status($old_attendee);
487
-        $new_event_id = $this->get_migration_script()->get_mapping_new_pk($wpdb->prefix . 'events_detail', $old_attendee['event_id'], $wpdb->posts);
488
-        if (!$new_event_id) {
489
-            $this->add_error(sprintf(esc_html__("Could not find NEW event CPT ID for old event '%d' on old attendee %s", "event_espresso"), $old_attendee['event_id'], $this->_json_encode($old_attendee)));
490
-        }
491
-
492
-        $ticket_id = $this->_try_to_find_new_ticket_id($old_attendee, $new_event_id);
493
-        if (!$ticket_id) {
494
-            $ticket_id = $this->_insert_new_ticket_because_none_found($old_attendee, $new_event_id);
495
-            $this->add_error(sprintf(esc_html__('Could not find a ticket for old attendee with id %d for new event %d, so created a new ticket with id %d', 'event_espresso'), $old_attendee['id'], $new_event_id, $ticket_id));
496
-        }
497
-        $regs_on_this_row = intval($old_attendee['quantity']);
498
-        $new_regs = array();
499
-        // 4 cases we need to account for:
500
-        // 1 old attendee_details row with a quantity of X (no mer)
501
-        // Y old attendee_details rows with a quantity of 1 (no mer) joined by their common registration_id
502
-        // Y old attendee_details rows with a quantity of x (because of mer)
503
-        // Y old attendee_details rows with a quantity of 1 (because of mer) joined by wp_events_multi_event_registration_id_group
504
-        for ($count = 1; $count <= $regs_on_this_row; $count++) {
505
-            // sum regs on older rows
506
-            $regs_on_this_event_and_txn = $this->_sum_old_attendees_on_old_txn($old_attendee, true);
507
-            $cols_n_values = array(
508
-                'EVT_ID' => $new_event_id,
509
-                'ATT_ID' => $new_attendee_id,
510
-                'TXN_ID' => $new_txn_id,
511
-                'TKT_ID' => $ticket_id,
512
-                'STS_ID' => $STS_ID,
513
-                'REG_date' => $this->get_migration_script()->convert_date_string_to_utc($this, $old_attendee, $old_attendee['date']),
514
-                'REG_final_price' => $old_attendee['final_price'],
515
-                'REG_session' => substr($old_attendee['attendee_session'], 0, 44),
516
-                'REG_code' => sanitize_key($old_attendee['registration_id']),
517
-                'REG_url_link' => sanitize_key($old_attendee['registration_id'] . '-' . $count),
518
-                'REG_count' => $regs_on_this_event_and_txn + $count,
519
-                'REG_group_size' => $this->_sum_old_attendees_on_old_txn($old_attendee, false),
520
-                'REG_att_is_going' => true,
521
-                'REG_deleted' => false
522
-            );
523
-            $datatypes = array(
524
-                '%d',// EVT_ID
525
-                '%d',// ATT_ID
526
-                '%d',// TXN_ID
527
-                '%d',// TKT_ID
528
-                '%s',// STS_ID
529
-                '%s',// REG_date
530
-                '%f',// REG_final_price
531
-                '%s',// REG_session
532
-                '%s',// REG_code
533
-                '%s',// REG_url_link
534
-                '%d',// REG_count
535
-                '%d',// REG_group_size
536
-                '%d',// REG_att_is_going
537
-                '%d',// REG_deleted
538
-            );
539
-            $success = $wpdb->insert($this->_new_reg_table, $cols_n_values, $datatypes);
540
-            if (!$success) {
541
-                $this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, $old_attendee, $this->_new_reg_table, $cols_n_values, $datatypes));
542
-                return 0;
543
-            }
544
-            $cols_n_values['REG_ID'] = $wpdb->insert_id;
545
-            $new_regs[] = $wpdb->insert_id;
546
-        }
547
-        $this->_add_regs_to_ticket_and_datetimes($ticket_id, count($new_regs), $STS_ID);
548
-        return $new_regs;
549
-    }
550
-
551
-    /**
552
-     * Increments the sold values on the ticket and its related datetimes by the amount sold,
553
-     * which should be done directly after adding the rows. Yes this means we're constantly incrementing
554
-     * the sold amounts as we go, and is less efficient than a single big query,
555
-     * but its safer because we KNOW these regs have been added, rather than inferring
556
-     * that they WILL be added (because the attendees stage runs nearly last during
557
-     * the migration script)
558
-     * @param type $new_ticket_id
559
-     * @param type $sold
560
-     * @param type $STS_ID
561
-     * @return boolean whether they were successfully updated or not
562
-     */
563
-    protected function _add_regs_to_ticket_and_datetimes($new_ticket_id, $quantity_sold, $STS_ID)
564
-    {
565
-        if ($STS_ID != 'RAP') {
566
-            return true;
567
-        }
568
-        global $wpdb;
569
-        $success = $wpdb->query($wpdb->prepare("UPDATE {$this->_new_ticket_table} SET TKT_sold=TKT_sold+%d WHERE TKT_ID=%d", $quantity_sold, $new_ticket_id));
570
-        if ($success) {
571
-            // get the ticket's datetimes, and increment them too
572
-            $success_update_dateimtes = $wpdb->query($wpdb->prepare("UPDATE {$this->_new_ticket_table} TKT
216
+		$this->_old_mer_table = $wpdb->prefix . "events_multi_event_registration_id_group";
217
+		$this->_new_attendee_cpt_table = $wpdb->posts;
218
+		$this->_new_attendee_meta_table = $wpdb->prefix . "esp_attendee_meta";
219
+		$this->_new_reg_table = $wpdb->prefix . "esp_registration";
220
+		$this->_new_transaction_table = $wpdb->prefix . "esp_transaction";
221
+		$this->_new_payment_table = $wpdb->prefix . "esp_payment";
222
+		$this->_new_line_table = $wpdb->prefix . "esp_line_item";
223
+		$this->_new_ticket_table = $wpdb->prefix . "esp_ticket";
224
+		$this->_new_ticket_datetime_table = $wpdb->prefix . "esp_datetime_ticket";
225
+		$this->_new_datetime_table = $wpdb->prefix . "esp_datetime";
226
+		$this->_new_datetime_ticket_table = $wpdb->prefix . "esp_datetime_ticket";
227
+		$this->_new_price_table = $wpdb->prefix . "esp_price";
228
+		$this->_new_ticket_price_table = $wpdb->prefix . "esp_ticket_price";
229
+		parent::__construct();
230
+	}
231
+
232
+	protected function _migrate_old_row($old_row)
233
+	{
234
+		// first check if there's already a new attendee with similar characteristics
235
+		$new_att_id = $this->_find_attendee_cpt_matching($old_row);
236
+		if (!$new_att_id) {
237
+			$new_att_id = $this->_insert_new_attendee_cpt($old_row);
238
+			if (!$new_att_id) {
239
+				// if we couldnt even make an attendee, abandon all hope
240
+				return false;
241
+			}
242
+			$new_att_meta_id = $this->_insert_attendee_meta_row($old_row, $new_att_id);
243
+			if ($new_att_meta_id) {
244
+				$this->get_migration_script()->set_mapping($this->_old_table, $old_row['id'], $this->_new_attendee_meta_table, $new_att_meta_id);
245
+			}
246
+		}
247
+		$this->get_migration_script()->set_mapping($this->_old_table, $old_row['id'], $this->_new_attendee_cpt_table, $new_att_id);
248
+
249
+		$txn_id = $this->_insert_new_transaction($old_row);
250
+		if (!$txn_id) {
251
+			// if we couldnt make the transaction, also abandon all hope
252
+			return false;
253
+		}
254
+		$this->get_migration_script()->set_mapping($this->_old_table, $old_row['id'], $this->_new_transaction_table, $txn_id);
255
+		$pay_id = $this->_insert_new_payment($old_row, $txn_id);
256
+		if ($pay_id) {
257
+			$this->get_migration_script()->set_mapping($this->_old_table, $old_row['id'], $this->_new_payment_table, $pay_id);
258
+		}
259
+
260
+
261
+		// even if there was no payment, we can go ahead with adding the reg
262
+		$new_regs = $this->_insert_new_registrations($old_row, $new_att_id, $txn_id);
263
+		if ($new_regs) {
264
+			$this->get_migration_script()->set_mapping($this->_old_table, $old_row['id'], $this->_new_reg_table, $new_regs);
265
+		}
266
+	}
267
+
268
+	/**
269
+	 * Checks if there's already an attendee CPT in the db that has the same
270
+	 * first and last name, and email. If so, returns its ID as an int.
271
+	 * @global type $wpdb
272
+	 * @param array $old_attendee
273
+	 * @return int
274
+	 */
275
+	private function _find_attendee_cpt_matching($old_attendee)
276
+	{
277
+		global $wpdb;
278
+		$existing_attendee_id = $wpdb->get_var($wpdb->prepare("SELECT id FROM " . $this->_new_attendee_cpt_table . " AS cpt INNER JOIN " . $this->_new_attendee_meta_table . " AS meta ON cpt.ID = meta.ATT_ID WHERE meta.ATT_fname = %s AND meta.ATT_lname = %s AND meta.ATT_email = %s LIMIT 1", $old_attendee['fname'], $old_attendee['lname'], $old_attendee['email']));
279
+		return intval($existing_attendee_id);
280
+	}
281
+
282
+	private function _insert_new_attendee_cpt($old_attendee)
283
+	{
284
+		global $wpdb;
285
+		$cols_n_values = array(
286
+			'post_title' => stripslashes($old_attendee['fname'] . " " . $old_attendee['lname']),// ATT_full_name
287
+			'post_content' => '',// ATT_bio
288
+			'post_name' => sanitize_title($old_attendee['fname'] . "-" . $old_attendee['lname']),// ATT_slug
289
+			'post_date' => $this->get_migration_script()->convert_date_string_to_utc($this, $old_attendee, $old_attendee['date']),// ATT_created
290
+			'post_excerpt' => '',// ATT_short_bio
291
+			'post_modified' => $this->get_migration_script()->convert_date_string_to_utc($this, $old_attendee, $old_attendee['date']),// ATT_modified
292
+			'post_author' => 0,// ATT_author
293
+			'post_parent' => 0,// ATT_parent
294
+			'post_type' => 'espresso_attendees',// post_type
295
+			'post_status' => 'publish'// status
296
+		);
297
+		$datatypes = array(
298
+			'%s',// ATT_full_name
299
+			'%s',// ATT_bio
300
+			'%s',// ATT_slug
301
+			'%s',// ATT_created
302
+			'%s',// ATT_short_bio
303
+			'%s',// ATT_modified
304
+			'%d',// ATT_author
305
+			'%d',// ATT_parent
306
+			'%s',// post_type
307
+			'%s',// status
308
+		);
309
+		$success = $wpdb->insert($this->_new_attendee_cpt_table, $cols_n_values, $datatypes);
310
+		if (!$success) {
311
+			$this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, $old_attendee, $this->_new_attendee_cpt_table, $cols_n_values, $datatypes));
312
+			return 0;
313
+		}
314
+		$new_id = $wpdb->insert_id;
315
+		return $new_id;
316
+	}
317
+
318
+	private function _insert_attendee_meta_row($old_attendee, $new_attendee_cpt_id)
319
+	{
320
+		global $wpdb;
321
+		// get the state and country ids from the old row
322
+		try {
323
+			$new_country = $this->get_migration_script()->get_or_create_country(stripslashes($old_attendee['country_id']));
324
+			$new_country_iso = $new_country['CNT_ISO'];
325
+		} catch (EE_Error $exception) {
326
+			$new_country_iso = $this->get_migration_script()->get_default_country_iso();
327
+		}
328
+		try {
329
+			$new_state = $this->get_migration_script()->get_or_create_state(stripslashes($old_attendee['state']), $new_country_iso);
330
+			$new_state_id = $new_state['STA_ID'];
331
+		} catch (EE_Error $exception) {
332
+			$new_state_id = 0;
333
+		}
334
+		$cols_n_values = array(
335
+			'ATT_ID' => $new_attendee_cpt_id,
336
+			'ATT_fname' => stripslashes($old_attendee['fname']),
337
+			'ATT_lname' => stripslashes($old_attendee['lname']),
338
+			'ATT_address' => stripslashes($old_attendee['address']),
339
+			'ATT_address2' => stripslashes($old_attendee['address2']),
340
+			'ATT_city' => stripslashes($old_attendee['city']),
341
+			'STA_ID' => $new_state_id,
342
+			'CNT_ISO' => $new_country_iso,
343
+			'ATT_zip' => stripslashes($old_attendee['zip']),
344
+			'ATT_email' => stripslashes($old_attendee['email']),
345
+			'ATT_phone' => stripslashes($old_attendee['phone']),
346
+		);
347
+		$datatypes = array(
348
+			'%d',// ATT_ID
349
+			'%s',// ATT_fname
350
+			'%s',// ATT_lname
351
+			'%s',// ATT_address
352
+			'%s',// ATT_address2
353
+			'%s',// ATT_city
354
+			'%d',// STA_ID
355
+			'%s',// CNT_ISO
356
+			'%s',// ATT_zip
357
+			'%s',// ATT_email
358
+			'%s',// ATT_phone
359
+		);
360
+		$success = $wpdb->insert($this->_new_attendee_meta_table, $cols_n_values, $datatypes);
361
+		if (!$success) {
362
+			$this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, $old_attendee, $this->_new_attendee_meta_table, $cols_n_values, $datatypes));
363
+			return 0;
364
+		}
365
+		$new_id = $wpdb->insert_id;
366
+		return $new_id;
367
+	}
368
+
369
+	/**
370
+	 * Note: we don't necessarily create a new transaction for each attendee row.
371
+	 * Only if the old attendee 'is_primary' is true; otherwise we find the old attendee row that
372
+	 * 'is_primary' and has the same 'txn_id', then we return ITS new transaction id
373
+	 * @global type $wpdb
374
+	 * @param type $old_attendee
375
+	 * @return int new transaction id
376
+	 */
377
+	private function _insert_new_transaction($old_attendee)
378
+	{
379
+		global $wpdb;
380
+
381
+		// first: let's check for an existing transaction for this old attendee
382
+		if (intval($old_attendee['is_primary'])) {// primary attendee, so create txn
383
+			$txn_id = $this->get_migration_script()->get_mapping_new_pk($this->_old_table, intval($old_attendee['id']), $this->_new_transaction_table);
384
+		} else { // non-primary attendee, so find its primary attendee's transaction
385
+			$primary_attendee_old_id = $wpdb->get_var($wpdb->prepare("SELECT id FROM " . $this->_old_table . " WHERE is_primary=1 and registration_id=%s", $old_attendee['registration_id']));
386
+			if (!$primary_attendee_old_id) {
387
+				$primary_attendee = $this->_find_mer_primary_attendee_using_mer_tables($old_attendee['registration_id']);
388
+				$primary_attendee_old_id = is_array($primary_attendee) ? $primary_attendee['id'] : null;
389
+			}
390
+			$txn_id = $this->get_migration_script()->get_mapping_new_pk($this->_old_table, intval($primary_attendee_old_id), $this->_new_transaction_table);
391
+			if (!$txn_id) {
392
+				$this->add_error(sprintf(esc_html__("Could not find primary attendee's new transaction. Current attendee is: %s, we think the 3.1 primary attendee for it has id %d, but there's no 4.1 transaction for that primary attendee id.", "event_espresso"), $this->_json_encode($old_attendee), $primary_attendee_old_id));
393
+				$txn_id = 0;
394
+			}
395
+		}
396
+		// if there isn't yet a transaction row for this, create one
397
+		// (so even if it was a non-primary attendee with no EE3 primary attendee,
398
+		// it ought to have SOME transaction, so we'll make one)
399
+		if (!$txn_id) {
400
+			// maps 3.1 payment stati onto 4.1 transaction stati
401
+			$txn_status_mapping = array(
402
+				'Completed' => 'TCM',
403
+				'Pending' => 'TIN',
404
+				'Payment Declined' => 'TIN',
405
+				'Incomplete' => 'TIN',
406
+				'Not Completed' => 'TIN',
407
+				'Cancelled' => 'TIN',
408
+				'Declined' => 'TIN'
409
+			);
410
+			$STS_ID = isset($txn_status_mapping[ $old_attendee['payment_status'] ]) ? $txn_status_mapping[ $old_attendee['payment_status'] ] : 'TIN';
411
+			$cols_n_values = array(
412
+				'TXN_timestamp' => $this->get_migration_script()->convert_date_string_to_utc($this, $old_attendee, $old_attendee['date']),
413
+				'TXN_total' => floatval($old_attendee['total_cost']),
414
+				'TXN_paid' => floatval($old_attendee['amount_pd']),
415
+				'STS_ID' => $STS_ID,
416
+				'TXN_hash_salt' => $old_attendee['hashSalt']
417
+			);
418
+			$datatypes = array(
419
+				'%s',// TXN_timestamp
420
+				'%f',// TXN_total
421
+				'%f',// TXN_paid
422
+				'%s',// STS_ID
423
+				'%s',// TXN_hash_salt
424
+			);
425
+			$success = $wpdb->insert($this->_new_transaction_table, $cols_n_values, $datatypes);
426
+			if (!$success) {
427
+				$this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, $old_attendee, $this->_new_transaction_table, $cols_n_values, $datatypes));
428
+				return 0;
429
+			}
430
+			$txn_id = $wpdb->insert_id;
431
+		}
432
+
433
+		return $txn_id;
434
+	}
435
+
436
+	/**
437
+	 * Detects if the MER tables exist
438
+	 * @global type $wpdb
439
+	 * @return boolean
440
+	 */
441
+	private function _mer_tables_exist()
442
+	{
443
+		if ($this->_mer_tables_exist === null) {
444
+			global $wpdb;
445
+
446
+			if ($wpdb->get_var("SHOW TABLES LIKE '{$this->_old_mer_table}'") != $this->_old_mer_table) {
447
+				$this->_mer_tables_exist = false;
448
+			} else {
449
+				$this->_mer_tables_exist = true;
450
+			}
451
+		}
452
+		return $this->_mer_tables_exist;
453
+	}
454
+
455
+	/**
456
+	 * Gets the 4.1 registration's status given the 3.1 attendee row. We consider
457
+	 * whether the event required pre-approval or not,a dn the 4.1 payment status.
458
+	 * @global type $wpdb
459
+	 * @param type $old_attendee_row
460
+	 * @return string
461
+	 */
462
+	private function _get_reg_status_for_old_payment_status($old_attendee_row)
463
+	{
464
+		// need event default reg status and if pre_approval was required
465
+		global $wpdb;
466
+		$event_required_pre_approval = $wpdb->get_var($wpdb->prepare("SELECT require_pre_approval FROM " . $wpdb->prefix . "events_detail WHERE id = %d", $old_attendee_row['event_id']));
467
+		return $this->get_migration_script()->convert_3_1_payment_status_to_4_1_STS_ID(
468
+			$old_attendee_row['payment_status'],
469
+			intval($event_required_pre_approval) && intval($old_attendee_row['pre_approve'])
470
+		);
471
+	}
472
+
473
+	/**
474
+	 * Adds however many rgistrations are indicated by the old attendee's QUANTITY field,
475
+	 * and returns an array of their IDs
476
+	 * @global type $wpdb
477
+	 * @param array $old_attendee
478
+	 * @param int $new_attendee_id
479
+	 * @param int $new_txn_id
480
+	 * @return array of new registratio ids
481
+	 */
482
+	private function _insert_new_registrations($old_attendee, $new_attendee_id, $new_txn_id)
483
+	{
484
+		global $wpdb;
485
+
486
+		$STS_ID = $this->_get_reg_status_for_old_payment_status($old_attendee);
487
+		$new_event_id = $this->get_migration_script()->get_mapping_new_pk($wpdb->prefix . 'events_detail', $old_attendee['event_id'], $wpdb->posts);
488
+		if (!$new_event_id) {
489
+			$this->add_error(sprintf(esc_html__("Could not find NEW event CPT ID for old event '%d' on old attendee %s", "event_espresso"), $old_attendee['event_id'], $this->_json_encode($old_attendee)));
490
+		}
491
+
492
+		$ticket_id = $this->_try_to_find_new_ticket_id($old_attendee, $new_event_id);
493
+		if (!$ticket_id) {
494
+			$ticket_id = $this->_insert_new_ticket_because_none_found($old_attendee, $new_event_id);
495
+			$this->add_error(sprintf(esc_html__('Could not find a ticket for old attendee with id %d for new event %d, so created a new ticket with id %d', 'event_espresso'), $old_attendee['id'], $new_event_id, $ticket_id));
496
+		}
497
+		$regs_on_this_row = intval($old_attendee['quantity']);
498
+		$new_regs = array();
499
+		// 4 cases we need to account for:
500
+		// 1 old attendee_details row with a quantity of X (no mer)
501
+		// Y old attendee_details rows with a quantity of 1 (no mer) joined by their common registration_id
502
+		// Y old attendee_details rows with a quantity of x (because of mer)
503
+		// Y old attendee_details rows with a quantity of 1 (because of mer) joined by wp_events_multi_event_registration_id_group
504
+		for ($count = 1; $count <= $regs_on_this_row; $count++) {
505
+			// sum regs on older rows
506
+			$regs_on_this_event_and_txn = $this->_sum_old_attendees_on_old_txn($old_attendee, true);
507
+			$cols_n_values = array(
508
+				'EVT_ID' => $new_event_id,
509
+				'ATT_ID' => $new_attendee_id,
510
+				'TXN_ID' => $new_txn_id,
511
+				'TKT_ID' => $ticket_id,
512
+				'STS_ID' => $STS_ID,
513
+				'REG_date' => $this->get_migration_script()->convert_date_string_to_utc($this, $old_attendee, $old_attendee['date']),
514
+				'REG_final_price' => $old_attendee['final_price'],
515
+				'REG_session' => substr($old_attendee['attendee_session'], 0, 44),
516
+				'REG_code' => sanitize_key($old_attendee['registration_id']),
517
+				'REG_url_link' => sanitize_key($old_attendee['registration_id'] . '-' . $count),
518
+				'REG_count' => $regs_on_this_event_and_txn + $count,
519
+				'REG_group_size' => $this->_sum_old_attendees_on_old_txn($old_attendee, false),
520
+				'REG_att_is_going' => true,
521
+				'REG_deleted' => false
522
+			);
523
+			$datatypes = array(
524
+				'%d',// EVT_ID
525
+				'%d',// ATT_ID
526
+				'%d',// TXN_ID
527
+				'%d',// TKT_ID
528
+				'%s',// STS_ID
529
+				'%s',// REG_date
530
+				'%f',// REG_final_price
531
+				'%s',// REG_session
532
+				'%s',// REG_code
533
+				'%s',// REG_url_link
534
+				'%d',// REG_count
535
+				'%d',// REG_group_size
536
+				'%d',// REG_att_is_going
537
+				'%d',// REG_deleted
538
+			);
539
+			$success = $wpdb->insert($this->_new_reg_table, $cols_n_values, $datatypes);
540
+			if (!$success) {
541
+				$this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, $old_attendee, $this->_new_reg_table, $cols_n_values, $datatypes));
542
+				return 0;
543
+			}
544
+			$cols_n_values['REG_ID'] = $wpdb->insert_id;
545
+			$new_regs[] = $wpdb->insert_id;
546
+		}
547
+		$this->_add_regs_to_ticket_and_datetimes($ticket_id, count($new_regs), $STS_ID);
548
+		return $new_regs;
549
+	}
550
+
551
+	/**
552
+	 * Increments the sold values on the ticket and its related datetimes by the amount sold,
553
+	 * which should be done directly after adding the rows. Yes this means we're constantly incrementing
554
+	 * the sold amounts as we go, and is less efficient than a single big query,
555
+	 * but its safer because we KNOW these regs have been added, rather than inferring
556
+	 * that they WILL be added (because the attendees stage runs nearly last during
557
+	 * the migration script)
558
+	 * @param type $new_ticket_id
559
+	 * @param type $sold
560
+	 * @param type $STS_ID
561
+	 * @return boolean whether they were successfully updated or not
562
+	 */
563
+	protected function _add_regs_to_ticket_and_datetimes($new_ticket_id, $quantity_sold, $STS_ID)
564
+	{
565
+		if ($STS_ID != 'RAP') {
566
+			return true;
567
+		}
568
+		global $wpdb;
569
+		$success = $wpdb->query($wpdb->prepare("UPDATE {$this->_new_ticket_table} SET TKT_sold=TKT_sold+%d WHERE TKT_ID=%d", $quantity_sold, $new_ticket_id));
570
+		if ($success) {
571
+			// get the ticket's datetimes, and increment them too
572
+			$success_update_dateimtes = $wpdb->query($wpdb->prepare("UPDATE {$this->_new_ticket_table} TKT
573 573
 				INNER JOIN {$this->_new_ticket_datetime_table} as DTK ON TKT.TKT_ID = DTK.TKT_ID
574 574
 				INNER JOIN {$this->_new_datetime_table} as DTT ON DTK.DTT_ID = DTT.DTT_ID
575 575
 				SET DTT.DTT_sold = DTT.DTT_sold + %d WHERE TKT.TKT_ID = %d", $quantity_sold, $new_ticket_id));
576
-            if (!$success_update_dateimtes) {
577
-                $this->add_error(sprintf(esc_html__("Could not update datetimes related to ticket with ID %d's TKT_sold by %d because %s", "event_espresso"), $new_ticket_id, $quantity_sold, $wpdb->last_error));
578
-            }
579
-        } else {
580
-            $this->add_error(sprintf(esc_html__("Could not update ticket with ID %d's TKT_sold by %d because %s", "event_espresso"), $new_ticket_id, $quantity_sold, $wpdb->last_error));
581
-        }
582
-        return true;
583
-    }
584
-
585
-    /**
586
-     * Makes a best guess at which ticket is the one the attendee purchased.
587
-     * Obviously, the old attendee's event_id narrows it down quite a bit;
588
-     * then the old attendee's orig_price and event_time, and price_option can uniquely identify the ticket
589
-     * however, if we don't find an exact match, see if any of those conditions match;
590
-     * and lastly if none of that works, just use the first ticket for the event we find
591
-     * @param array $old_attendee
592
-     */
593
-    private function _try_to_find_new_ticket_id($old_attendee, $new_event_id)
594
-    {
595
-        global $wpdb;
596
-        $tickets_table = $this->_new_ticket_table;
597
-        $datetime_tickets_table = $this->_new_ticket_datetime_table;
598
-        $datetime_table = $this->_new_datetime_table;
599
-
600
-        $old_att_price_option = $old_attendee['price_option'];
601
-        $old_att_price = floatval($old_attendee['orig_price']);
602
-
603
-        $old_att_start_date = $old_attendee['start_date'];
604
-        $old_att_start_time = $this->get_migration_script()->convertTimeFromAMPM($old_attendee['event_time']);
605
-        $old_att_datetime = $this->get_migration_script()->convert_date_string_to_utc($this, $old_attendee, "$old_att_start_date $old_att_start_time:00");
606
-        // add all conditions to an array from which we can SHIFT conditions off in order to widen our search
607
-        // the most important condition should be last, as it will be array_shift'ed off last
608
-        $conditions = array(
609
-            $wpdb->prepare("$datetime_table.DTT_EVT_start = %s", $old_att_datetime),// times match?
610
-            $wpdb->prepare("$tickets_table.TKT_price = %f", $old_att_price),// prices match?
611
-            $wpdb->prepare("$tickets_table.TKT_name = %s", $old_att_price_option),// names match?
612
-            $wpdb->prepare("$datetime_table.EVT_ID = %d", $new_event_id),// events match?
613
-        );
614
-        $select_and_join_part = "SELECT $tickets_table.TKT_ID FROM $tickets_table INNER JOIN
576
+			if (!$success_update_dateimtes) {
577
+				$this->add_error(sprintf(esc_html__("Could not update datetimes related to ticket with ID %d's TKT_sold by %d because %s", "event_espresso"), $new_ticket_id, $quantity_sold, $wpdb->last_error));
578
+			}
579
+		} else {
580
+			$this->add_error(sprintf(esc_html__("Could not update ticket with ID %d's TKT_sold by %d because %s", "event_espresso"), $new_ticket_id, $quantity_sold, $wpdb->last_error));
581
+		}
582
+		return true;
583
+	}
584
+
585
+	/**
586
+	 * Makes a best guess at which ticket is the one the attendee purchased.
587
+	 * Obviously, the old attendee's event_id narrows it down quite a bit;
588
+	 * then the old attendee's orig_price and event_time, and price_option can uniquely identify the ticket
589
+	 * however, if we don't find an exact match, see if any of those conditions match;
590
+	 * and lastly if none of that works, just use the first ticket for the event we find
591
+	 * @param array $old_attendee
592
+	 */
593
+	private function _try_to_find_new_ticket_id($old_attendee, $new_event_id)
594
+	{
595
+		global $wpdb;
596
+		$tickets_table = $this->_new_ticket_table;
597
+		$datetime_tickets_table = $this->_new_ticket_datetime_table;
598
+		$datetime_table = $this->_new_datetime_table;
599
+
600
+		$old_att_price_option = $old_attendee['price_option'];
601
+		$old_att_price = floatval($old_attendee['orig_price']);
602
+
603
+		$old_att_start_date = $old_attendee['start_date'];
604
+		$old_att_start_time = $this->get_migration_script()->convertTimeFromAMPM($old_attendee['event_time']);
605
+		$old_att_datetime = $this->get_migration_script()->convert_date_string_to_utc($this, $old_attendee, "$old_att_start_date $old_att_start_time:00");
606
+		// add all conditions to an array from which we can SHIFT conditions off in order to widen our search
607
+		// the most important condition should be last, as it will be array_shift'ed off last
608
+		$conditions = array(
609
+			$wpdb->prepare("$datetime_table.DTT_EVT_start = %s", $old_att_datetime),// times match?
610
+			$wpdb->prepare("$tickets_table.TKT_price = %f", $old_att_price),// prices match?
611
+			$wpdb->prepare("$tickets_table.TKT_name = %s", $old_att_price_option),// names match?
612
+			$wpdb->prepare("$datetime_table.EVT_ID = %d", $new_event_id),// events match?
613
+		);
614
+		$select_and_join_part = "SELECT $tickets_table.TKT_ID FROM $tickets_table INNER JOIN
615 615
 			$datetime_tickets_table ON $tickets_table.TKT_ID = $datetime_tickets_table.TKT_ID INNER JOIN
616 616
 			$datetime_table ON $datetime_tickets_table.DTT_ID = $datetime_table.DTT_ID";
617
-        // start running queries, widening search each time by removing a condition
618
-        do {
619
-            $full_query = $select_and_join_part . " WHERE " . implode(" AND ", $conditions) . " LIMIT 1";
620
-            $ticket_id_found = $wpdb->get_var($full_query);
621
-            array_shift($conditions);
622
-        } while (!$ticket_id_found && $conditions);
623
-        return $ticket_id_found;
624
-    }
625
-
626
-    /**
627
-     * If we couldn't find a 4.1 ticket for a 3.1 attendee row, this function creates one;
628
-     * and it also tries to find a datetime that works, and a inserts a price, and associates
629
-     * the new ticket to that datetime and price.
630
-     * @return int ticket id
631
-     */
632
-    private function _insert_new_ticket_because_none_found($old_attendee, $new_event_id)
633
-    {
634
-        global $wpdb;
635
-        $old_att_price_option = $old_attendee['price_option'];
636
-        $old_att_price = floatval($old_attendee['orig_price']);
637
-
638
-        $old_att_start_date = $old_attendee['start_date'];
639
-        $old_att_start_time = $this->get_migration_script()->convertTimeFromAMPM($old_attendee['event_time']);
640
-        $old_att_start_datetime = $this->get_migration_script()->convert_date_string_to_utc($this, $old_attendee, "$old_att_start_date $old_att_start_time:00");
641
-
642
-
643
-        // insert new datetime unless we find one
644
-        $datetime_id = $wpdb->get_var($wpdb->prepare("SELECT DTT_ID FROM " . $this->_new_datetime_table . " WHERE DTT_EVT_start=%s AND EVT_ID=%d LIMIT 1", $old_att_start_datetime, $new_event_id), ARRAY_A);
645
-        if (!$datetime_id) {
646
-            $old_att_end_date = $old_attendee['start_date'];
647
-            $old_att_end_time = $this->get_migration_script()->convertTimeFromAMPM($old_attendee['event_time']);
648
-            $old_att_end_datetime = $this->get_migration_script()->convert_date_string_to_utc($this, $old_attendee, "$old_att_end_date $old_att_end_time:00");
649
-            $wpdb->insert(
650
-                $this->_new_datetime_table,
651
-                array(
652
-                    'EVT_ID' => $new_event_id,
653
-                    'DTT_EVT_start' => $old_att_start_datetime,
654
-                    'DTT_EVT_end' => $old_att_end_datetime,
655
-                    'DTT_deleted' => true
656
-                ),
657
-                array(
658
-                    '%d',// EVT_ID
659
-                    '%s',// DTT_EVT_start
660
-                    '%s',// DTT_EVT_end
661
-                    '%d',// DTT_deleted
662
-                )
663
-            );
664
-            $datetime_id = $wpdb->insert_id;
665
-        }
666
-
667
-        // insert new ticket
668
-        $success = $wpdb->insert(
669
-            $wpdb->prefix . 'esp_ticket',
670
-            array(
671
-                'TKT_name' => $old_att_price_option,
672
-                'TKT_qty' => -1,
673
-                'TKT_price' => $old_att_price,
674
-                'TKT_start_date' => $old_att_start_datetime,// we really have no clue what the time should be, but at least it was available when they attended
675
-                'TKT_end_date' => $old_att_end_datetime,
676
-
677
-            ),
678
-            array(
679
-                '%s',// name
680
-                '%d',// qty
681
-                '%d',// price
682
-                '%s',// start_date
683
-                '%s',// end_date
684
-            )
685
-        );
686
-        $ticket_id = $wpdb->insert_id;
687
-        // associate the ticket with the datetime we found earlier
688
-        $wpdb->insert(
689
-            $this->_new_datetime_ticket_table,
690
-            array(
691
-                'DTT_ID' => $datetime_id,
692
-                'TKT_ID' => $ticket_id
693
-            ),
694
-            array(
695
-                '%d',// DTT_ID
696
-                '%d',// TKT_ID
697
-            )
698
-        );
699
-        // insert new price
700
-        $wpdb->insert(
701
-            $this->_new_price_table,
702
-            array(
703
-                'PRC_amount' => $old_att_price,
704
-                'PRT_ID' => EE_DMS_4_1_0_prices::price_type_base,
705
-                'PRC_name' => $old_att_price_option,
706
-                'PRC_deleted' => true
707
-            ),
708
-            array(
709
-                '%d',// PRC_amount
710
-                '%d',// PRT_ID
711
-                '%s',// PRC_name
712
-                '%d',// PRC_deleted
713
-            )
714
-        );
715
-        $price_id = $wpdb->insert_id;
716
-        // associate the price to the ticket
717
-        $wpdb->insert(
718
-            $this->_new_ticket_price_table,
719
-            array(
720
-                'TKT_ID' => $ticket_id,
721
-                'PRC_ID' => $price_id
722
-            ),
723
-            array(
724
-                '%d',// TKT_ID
725
-                '%d',// PRC_ID
726
-            )
727
-        );
728
-        return $ticket_id;
729
-    }
730
-
731
-    /**
732
-     * Counts all the registrations on this transaction. If $count_only_older is TRUE then returns the number added SO FAR (ie,
733
-     * only considers attendee rows with an ID less than this one's), but if $count_only_older is FALSe returns ALL
734
-     * @global type $wpdb
735
-     * @param array $old_attendee_row
736
-     * @param boolean $count_only_older true if you want the running count (ie, the total up to this row), and false if you want ALL
737
-     * @return int
738
-     */
739
-    private function _sum_old_attendees_on_old_txn($old_attendee_row, $count_only_older = false)
740
-    {
741
-        global $wpdb;
742
-        $count_only_older_sql = $count_only_older ? $wpdb->prepare(" AND id<%d", $old_attendee_row['id']) : '';
743
-        $count = intval($wpdb->get_var($wpdb->prepare("SELECT SUM(quantity) FROM " . $this->_old_table . " WHERE registration_id=%s $count_only_older_sql", $old_attendee_row['registration_id'])));
744
-
745
-        if ($this->_mer_tables_exist()) {
746
-            // if MER exists, then its a little tricky.
747
-            // when users registered by adding items to the cart, and it was a
748
-            // group registration requiring additional attendee INFO, then the attendee rows
749
-            // DO NOT have the same registration_id (although they probably should have)
750
-            // they are related just like MER attendee rows are related, through the MER group table
751
-            // BUT we want to count all the MER attendee rows for the same registration
752
-            $primary_attendee = $this->_find_mer_primary_attendee_using_mer_tables($old_attendee_row['registration_id']);
753
-
754
-            $count_using_mer_table = $wpdb->get_var($wpdb->prepare("SELECT SUM(quantity) FROM {$this->_old_table} att INNER JOIN {$this->_old_mer_table} mer ON att.registration_id = mer.registration_id WHERE att.event_id=%d AND mer.primary_registration_id = %s $count_only_older_sql", $old_attendee_row['event_id'], $primary_attendee['registration_id']));
755
-            $count = max($count_using_mer_table, $count);
756
-        }
757
-        return $count;
758
-    }
759
-
760
-    private function _insert_new_payment($old_attendee, $new_txn_id)
761
-    {
762
-        global $wpdb;
763
-        // Only add a payment for primary attendees. Important note: partial payments were marked as "Pending".
764
-        // Also, an offline payment had blank status (ie, "").
765
-        $old_pay_stati_indicating_no_payment = array('', 'Incomplete', 'Not Completed');
766
-        // if this is for a primary 3.1 attendee which WASN'T free and has a completed, cancelled, or declined payment...
767
-        if (intval($old_attendee['is_primary']) && floatval($old_attendee['total_cost']) && !in_array($old_attendee['payment_status'], $old_pay_stati_indicating_no_payment)) {
768
-            $pay_status_mapping = array(
769
-                'Completed' => 'PAP',
770
-                'Payment Declined' => 'PDC',
771
-                'Cancelled' => 'PCN',
772
-                'Declined' => 'PDC'
773
-            );
774
-            $by_admin = $old_attendee['payment'] == 'Admin';
775
-            $STS_ID = isset($pay_status_mapping[ $old_attendee['payment_status'] ]) ? $pay_status_mapping[ $old_attendee['payment_status'] ] : 'PFL';// IE, if we don't recognize the status, assume payment failed
776
-            $cols_n_values = array(
777
-                'TXN_ID' => $new_txn_id,
778
-                'STS_ID' => $STS_ID,
779
-                'PAY_timestamp' => $this->get_migration_script()->convert_date_string_to_utc($this, $old_attendee, $old_attendee['date']),
780
-                'PAY_method' => 'CART',
781
-                'PAY_amount' => $old_attendee['amount_pd'],
782
-                // 'txn_type' was used both for the gateway's name, and for other payment notes.
783
-                'PAY_gateway' => $old_attendee['txn_type'],
784
-                'PAY_extra_accntng' => $old_attendee['txn_type'],
785
-                'PAY_gateway_response' => '',
786
-                'PAY_txn_id_chq_nmbr' => substr($old_attendee['txn_id'], 0, 32),
787
-                'PAY_via_admin' => $by_admin,
788
-                'PAY_details' => $old_attendee['transaction_details']
789
-            );
790
-            $datatypes = array(
791
-                '%d',// TXN_Id
792
-                '%s',// STS_ID
793
-                '%s',// PAY_timestamp
794
-                '%s',// PAY_method
795
-                '%f',// PAY_amount
796
-                '%s',// PAY_gateway
797
-                '%s',// PAY_gateway_response
798
-                '%s',// PAY_txn_id_chq_nmbr
799
-                '%d',// PAY_via_admin
800
-                '%s',// PAY_details
801
-            );
802
-            $success = $wpdb->insert($this->_new_payment_table, $cols_n_values, $datatypes);
803
-            if (!$success) {
804
-                $this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, $old_attendee, $this->_new_attendee_cpt_table, $cols_n_values, $datatypes));
805
-                return 0;
806
-            }
807
-            $new_id = $wpdb->insert_id;
808
-            return $new_id;
809
-        } else {
810
-            return 0;
811
-        }
812
-    }
813
-
814
-    /**
815
-     * If MER is active, if you want ot fin dthe other registrations on that attendee row
816
-     * @global type $wpdb
817
-     * @param type $old_registration_id
818
-     * @return array
819
-     */
820
-    private function _find_mer_primary_attendee_using_mer_tables($old_registration_id)
821
-    {
822
-        if (!$this->_mer_tables_exist()) {
823
-            return false;
824
-        }
825
-        global $wpdb;
826
-        $old_att_for_primary_reg = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$this->_old_mer_table} AS mer INNER JOIN {$this->_old_table} AS att ON mer.primary_registration_id = att.registration_id WHERE mer.registration_id=%s LIMIT 1", $old_registration_id), ARRAY_A);
827
-        return $old_att_for_primary_reg;
828
-    }
617
+		// start running queries, widening search each time by removing a condition
618
+		do {
619
+			$full_query = $select_and_join_part . " WHERE " . implode(" AND ", $conditions) . " LIMIT 1";
620
+			$ticket_id_found = $wpdb->get_var($full_query);
621
+			array_shift($conditions);
622
+		} while (!$ticket_id_found && $conditions);
623
+		return $ticket_id_found;
624
+	}
625
+
626
+	/**
627
+	 * If we couldn't find a 4.1 ticket for a 3.1 attendee row, this function creates one;
628
+	 * and it also tries to find a datetime that works, and a inserts a price, and associates
629
+	 * the new ticket to that datetime and price.
630
+	 * @return int ticket id
631
+	 */
632
+	private function _insert_new_ticket_because_none_found($old_attendee, $new_event_id)
633
+	{
634
+		global $wpdb;
635
+		$old_att_price_option = $old_attendee['price_option'];
636
+		$old_att_price = floatval($old_attendee['orig_price']);
637
+
638
+		$old_att_start_date = $old_attendee['start_date'];
639
+		$old_att_start_time = $this->get_migration_script()->convertTimeFromAMPM($old_attendee['event_time']);
640
+		$old_att_start_datetime = $this->get_migration_script()->convert_date_string_to_utc($this, $old_attendee, "$old_att_start_date $old_att_start_time:00");
641
+
642
+
643
+		// insert new datetime unless we find one
644
+		$datetime_id = $wpdb->get_var($wpdb->prepare("SELECT DTT_ID FROM " . $this->_new_datetime_table . " WHERE DTT_EVT_start=%s AND EVT_ID=%d LIMIT 1", $old_att_start_datetime, $new_event_id), ARRAY_A);
645
+		if (!$datetime_id) {
646
+			$old_att_end_date = $old_attendee['start_date'];
647
+			$old_att_end_time = $this->get_migration_script()->convertTimeFromAMPM($old_attendee['event_time']);
648
+			$old_att_end_datetime = $this->get_migration_script()->convert_date_string_to_utc($this, $old_attendee, "$old_att_end_date $old_att_end_time:00");
649
+			$wpdb->insert(
650
+				$this->_new_datetime_table,
651
+				array(
652
+					'EVT_ID' => $new_event_id,
653
+					'DTT_EVT_start' => $old_att_start_datetime,
654
+					'DTT_EVT_end' => $old_att_end_datetime,
655
+					'DTT_deleted' => true
656
+				),
657
+				array(
658
+					'%d',// EVT_ID
659
+					'%s',// DTT_EVT_start
660
+					'%s',// DTT_EVT_end
661
+					'%d',// DTT_deleted
662
+				)
663
+			);
664
+			$datetime_id = $wpdb->insert_id;
665
+		}
666
+
667
+		// insert new ticket
668
+		$success = $wpdb->insert(
669
+			$wpdb->prefix . 'esp_ticket',
670
+			array(
671
+				'TKT_name' => $old_att_price_option,
672
+				'TKT_qty' => -1,
673
+				'TKT_price' => $old_att_price,
674
+				'TKT_start_date' => $old_att_start_datetime,// we really have no clue what the time should be, but at least it was available when they attended
675
+				'TKT_end_date' => $old_att_end_datetime,
676
+
677
+			),
678
+			array(
679
+				'%s',// name
680
+				'%d',// qty
681
+				'%d',// price
682
+				'%s',// start_date
683
+				'%s',// end_date
684
+			)
685
+		);
686
+		$ticket_id = $wpdb->insert_id;
687
+		// associate the ticket with the datetime we found earlier
688
+		$wpdb->insert(
689
+			$this->_new_datetime_ticket_table,
690
+			array(
691
+				'DTT_ID' => $datetime_id,
692
+				'TKT_ID' => $ticket_id
693
+			),
694
+			array(
695
+				'%d',// DTT_ID
696
+				'%d',// TKT_ID
697
+			)
698
+		);
699
+		// insert new price
700
+		$wpdb->insert(
701
+			$this->_new_price_table,
702
+			array(
703
+				'PRC_amount' => $old_att_price,
704
+				'PRT_ID' => EE_DMS_4_1_0_prices::price_type_base,
705
+				'PRC_name' => $old_att_price_option,
706
+				'PRC_deleted' => true
707
+			),
708
+			array(
709
+				'%d',// PRC_amount
710
+				'%d',// PRT_ID
711
+				'%s',// PRC_name
712
+				'%d',// PRC_deleted
713
+			)
714
+		);
715
+		$price_id = $wpdb->insert_id;
716
+		// associate the price to the ticket
717
+		$wpdb->insert(
718
+			$this->_new_ticket_price_table,
719
+			array(
720
+				'TKT_ID' => $ticket_id,
721
+				'PRC_ID' => $price_id
722
+			),
723
+			array(
724
+				'%d',// TKT_ID
725
+				'%d',// PRC_ID
726
+			)
727
+		);
728
+		return $ticket_id;
729
+	}
730
+
731
+	/**
732
+	 * Counts all the registrations on this transaction. If $count_only_older is TRUE then returns the number added SO FAR (ie,
733
+	 * only considers attendee rows with an ID less than this one's), but if $count_only_older is FALSe returns ALL
734
+	 * @global type $wpdb
735
+	 * @param array $old_attendee_row
736
+	 * @param boolean $count_only_older true if you want the running count (ie, the total up to this row), and false if you want ALL
737
+	 * @return int
738
+	 */
739
+	private function _sum_old_attendees_on_old_txn($old_attendee_row, $count_only_older = false)
740
+	{
741
+		global $wpdb;
742
+		$count_only_older_sql = $count_only_older ? $wpdb->prepare(" AND id<%d", $old_attendee_row['id']) : '';
743
+		$count = intval($wpdb->get_var($wpdb->prepare("SELECT SUM(quantity) FROM " . $this->_old_table . " WHERE registration_id=%s $count_only_older_sql", $old_attendee_row['registration_id'])));
744
+
745
+		if ($this->_mer_tables_exist()) {
746
+			// if MER exists, then its a little tricky.
747
+			// when users registered by adding items to the cart, and it was a
748
+			// group registration requiring additional attendee INFO, then the attendee rows
749
+			// DO NOT have the same registration_id (although they probably should have)
750
+			// they are related just like MER attendee rows are related, through the MER group table
751
+			// BUT we want to count all the MER attendee rows for the same registration
752
+			$primary_attendee = $this->_find_mer_primary_attendee_using_mer_tables($old_attendee_row['registration_id']);
753
+
754
+			$count_using_mer_table = $wpdb->get_var($wpdb->prepare("SELECT SUM(quantity) FROM {$this->_old_table} att INNER JOIN {$this->_old_mer_table} mer ON att.registration_id = mer.registration_id WHERE att.event_id=%d AND mer.primary_registration_id = %s $count_only_older_sql", $old_attendee_row['event_id'], $primary_attendee['registration_id']));
755
+			$count = max($count_using_mer_table, $count);
756
+		}
757
+		return $count;
758
+	}
759
+
760
+	private function _insert_new_payment($old_attendee, $new_txn_id)
761
+	{
762
+		global $wpdb;
763
+		// Only add a payment for primary attendees. Important note: partial payments were marked as "Pending".
764
+		// Also, an offline payment had blank status (ie, "").
765
+		$old_pay_stati_indicating_no_payment = array('', 'Incomplete', 'Not Completed');
766
+		// if this is for a primary 3.1 attendee which WASN'T free and has a completed, cancelled, or declined payment...
767
+		if (intval($old_attendee['is_primary']) && floatval($old_attendee['total_cost']) && !in_array($old_attendee['payment_status'], $old_pay_stati_indicating_no_payment)) {
768
+			$pay_status_mapping = array(
769
+				'Completed' => 'PAP',
770
+				'Payment Declined' => 'PDC',
771
+				'Cancelled' => 'PCN',
772
+				'Declined' => 'PDC'
773
+			);
774
+			$by_admin = $old_attendee['payment'] == 'Admin';
775
+			$STS_ID = isset($pay_status_mapping[ $old_attendee['payment_status'] ]) ? $pay_status_mapping[ $old_attendee['payment_status'] ] : 'PFL';// IE, if we don't recognize the status, assume payment failed
776
+			$cols_n_values = array(
777
+				'TXN_ID' => $new_txn_id,
778
+				'STS_ID' => $STS_ID,
779
+				'PAY_timestamp' => $this->get_migration_script()->convert_date_string_to_utc($this, $old_attendee, $old_attendee['date']),
780
+				'PAY_method' => 'CART',
781
+				'PAY_amount' => $old_attendee['amount_pd'],
782
+				// 'txn_type' was used both for the gateway's name, and for other payment notes.
783
+				'PAY_gateway' => $old_attendee['txn_type'],
784
+				'PAY_extra_accntng' => $old_attendee['txn_type'],
785
+				'PAY_gateway_response' => '',
786
+				'PAY_txn_id_chq_nmbr' => substr($old_attendee['txn_id'], 0, 32),
787
+				'PAY_via_admin' => $by_admin,
788
+				'PAY_details' => $old_attendee['transaction_details']
789
+			);
790
+			$datatypes = array(
791
+				'%d',// TXN_Id
792
+				'%s',// STS_ID
793
+				'%s',// PAY_timestamp
794
+				'%s',// PAY_method
795
+				'%f',// PAY_amount
796
+				'%s',// PAY_gateway
797
+				'%s',// PAY_gateway_response
798
+				'%s',// PAY_txn_id_chq_nmbr
799
+				'%d',// PAY_via_admin
800
+				'%s',// PAY_details
801
+			);
802
+			$success = $wpdb->insert($this->_new_payment_table, $cols_n_values, $datatypes);
803
+			if (!$success) {
804
+				$this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, $old_attendee, $this->_new_attendee_cpt_table, $cols_n_values, $datatypes));
805
+				return 0;
806
+			}
807
+			$new_id = $wpdb->insert_id;
808
+			return $new_id;
809
+		} else {
810
+			return 0;
811
+		}
812
+	}
813
+
814
+	/**
815
+	 * If MER is active, if you want ot fin dthe other registrations on that attendee row
816
+	 * @global type $wpdb
817
+	 * @param type $old_registration_id
818
+	 * @return array
819
+	 */
820
+	private function _find_mer_primary_attendee_using_mer_tables($old_registration_id)
821
+	{
822
+		if (!$this->_mer_tables_exist()) {
823
+			return false;
824
+		}
825
+		global $wpdb;
826
+		$old_att_for_primary_reg = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$this->_old_mer_table} AS mer INNER JOIN {$this->_old_table} AS att ON mer.primary_registration_id = att.registration_id WHERE mer.registration_id=%s LIMIT 1", $old_registration_id), ARRAY_A);
827
+		return $old_att_for_primary_reg;
828
+	}
829 829
 }
Please login to merge, or discard this patch.
Spacing   +124 added lines, -124 removed lines patch added patch discarded remove patch
@@ -206,26 +206,26 @@  discard block
 block discarded – undo
206 206
     {
207 207
         global $wpdb;
208 208
         $this->_pretty_name = esc_html__("Attendees", "event_espresso");
209
-        $this->_old_table = $wpdb->prefix . "events_attendee";
209
+        $this->_old_table = $wpdb->prefix."events_attendee";
210 210
         // Only select the event status column from the event table.
211 211
         $this->select_expression = 'att.*, e.event_status';
212 212
         // Only select attendees for events that aren't deleted.
213 213
         $this->_extra_where_sql = 'AS att
214
-            INNER JOIN ' . $wpdb->prefix . 'events_detail AS e ON att.event_id=e.id
214
+            INNER JOIN ' . $wpdb->prefix.'events_detail AS e ON att.event_id=e.id
215 215
             WHERE e.event_status!="D"';
216
-        $this->_old_mer_table = $wpdb->prefix . "events_multi_event_registration_id_group";
216
+        $this->_old_mer_table = $wpdb->prefix."events_multi_event_registration_id_group";
217 217
         $this->_new_attendee_cpt_table = $wpdb->posts;
218
-        $this->_new_attendee_meta_table = $wpdb->prefix . "esp_attendee_meta";
219
-        $this->_new_reg_table = $wpdb->prefix . "esp_registration";
220
-        $this->_new_transaction_table = $wpdb->prefix . "esp_transaction";
221
-        $this->_new_payment_table = $wpdb->prefix . "esp_payment";
222
-        $this->_new_line_table = $wpdb->prefix . "esp_line_item";
223
-        $this->_new_ticket_table = $wpdb->prefix . "esp_ticket";
224
-        $this->_new_ticket_datetime_table = $wpdb->prefix . "esp_datetime_ticket";
225
-        $this->_new_datetime_table = $wpdb->prefix . "esp_datetime";
226
-        $this->_new_datetime_ticket_table = $wpdb->prefix . "esp_datetime_ticket";
227
-        $this->_new_price_table = $wpdb->prefix . "esp_price";
228
-        $this->_new_ticket_price_table = $wpdb->prefix . "esp_ticket_price";
218
+        $this->_new_attendee_meta_table = $wpdb->prefix."esp_attendee_meta";
219
+        $this->_new_reg_table = $wpdb->prefix."esp_registration";
220
+        $this->_new_transaction_table = $wpdb->prefix."esp_transaction";
221
+        $this->_new_payment_table = $wpdb->prefix."esp_payment";
222
+        $this->_new_line_table = $wpdb->prefix."esp_line_item";
223
+        $this->_new_ticket_table = $wpdb->prefix."esp_ticket";
224
+        $this->_new_ticket_datetime_table = $wpdb->prefix."esp_datetime_ticket";
225
+        $this->_new_datetime_table = $wpdb->prefix."esp_datetime";
226
+        $this->_new_datetime_ticket_table = $wpdb->prefix."esp_datetime_ticket";
227
+        $this->_new_price_table = $wpdb->prefix."esp_price";
228
+        $this->_new_ticket_price_table = $wpdb->prefix."esp_ticket_price";
229 229
         parent::__construct();
230 230
     }
231 231
 
@@ -233,9 +233,9 @@  discard block
 block discarded – undo
233 233
     {
234 234
         // first check if there's already a new attendee with similar characteristics
235 235
         $new_att_id = $this->_find_attendee_cpt_matching($old_row);
236
-        if (!$new_att_id) {
236
+        if ( ! $new_att_id) {
237 237
             $new_att_id = $this->_insert_new_attendee_cpt($old_row);
238
-            if (!$new_att_id) {
238
+            if ( ! $new_att_id) {
239 239
                 // if we couldnt even make an attendee, abandon all hope
240 240
                 return false;
241 241
             }
@@ -247,7 +247,7 @@  discard block
 block discarded – undo
247 247
         $this->get_migration_script()->set_mapping($this->_old_table, $old_row['id'], $this->_new_attendee_cpt_table, $new_att_id);
248 248
 
249 249
         $txn_id = $this->_insert_new_transaction($old_row);
250
-        if (!$txn_id) {
250
+        if ( ! $txn_id) {
251 251
             // if we couldnt make the transaction, also abandon all hope
252 252
             return false;
253 253
         }
@@ -275,7 +275,7 @@  discard block
 block discarded – undo
275 275
     private function _find_attendee_cpt_matching($old_attendee)
276 276
     {
277 277
         global $wpdb;
278
-        $existing_attendee_id = $wpdb->get_var($wpdb->prepare("SELECT id FROM " . $this->_new_attendee_cpt_table . " AS cpt INNER JOIN " . $this->_new_attendee_meta_table . " AS meta ON cpt.ID = meta.ATT_ID WHERE meta.ATT_fname = %s AND meta.ATT_lname = %s AND meta.ATT_email = %s LIMIT 1", $old_attendee['fname'], $old_attendee['lname'], $old_attendee['email']));
278
+        $existing_attendee_id = $wpdb->get_var($wpdb->prepare("SELECT id FROM ".$this->_new_attendee_cpt_table." AS cpt INNER JOIN ".$this->_new_attendee_meta_table." AS meta ON cpt.ID = meta.ATT_ID WHERE meta.ATT_fname = %s AND meta.ATT_lname = %s AND meta.ATT_email = %s LIMIT 1", $old_attendee['fname'], $old_attendee['lname'], $old_attendee['email']));
279 279
         return intval($existing_attendee_id);
280 280
     }
281 281
 
@@ -283,31 +283,31 @@  discard block
 block discarded – undo
283 283
     {
284 284
         global $wpdb;
285 285
         $cols_n_values = array(
286
-            'post_title' => stripslashes($old_attendee['fname'] . " " . $old_attendee['lname']),// ATT_full_name
287
-            'post_content' => '',// ATT_bio
288
-            'post_name' => sanitize_title($old_attendee['fname'] . "-" . $old_attendee['lname']),// ATT_slug
289
-            'post_date' => $this->get_migration_script()->convert_date_string_to_utc($this, $old_attendee, $old_attendee['date']),// ATT_created
290
-            'post_excerpt' => '',// ATT_short_bio
291
-            'post_modified' => $this->get_migration_script()->convert_date_string_to_utc($this, $old_attendee, $old_attendee['date']),// ATT_modified
292
-            'post_author' => 0,// ATT_author
293
-            'post_parent' => 0,// ATT_parent
294
-            'post_type' => 'espresso_attendees',// post_type
286
+            'post_title' => stripslashes($old_attendee['fname']." ".$old_attendee['lname']), // ATT_full_name
287
+            'post_content' => '', // ATT_bio
288
+            'post_name' => sanitize_title($old_attendee['fname']."-".$old_attendee['lname']), // ATT_slug
289
+            'post_date' => $this->get_migration_script()->convert_date_string_to_utc($this, $old_attendee, $old_attendee['date']), // ATT_created
290
+            'post_excerpt' => '', // ATT_short_bio
291
+            'post_modified' => $this->get_migration_script()->convert_date_string_to_utc($this, $old_attendee, $old_attendee['date']), // ATT_modified
292
+            'post_author' => 0, // ATT_author
293
+            'post_parent' => 0, // ATT_parent
294
+            'post_type' => 'espresso_attendees', // post_type
295 295
             'post_status' => 'publish'// status
296 296
         );
297 297
         $datatypes = array(
298
-            '%s',// ATT_full_name
299
-            '%s',// ATT_bio
300
-            '%s',// ATT_slug
301
-            '%s',// ATT_created
302
-            '%s',// ATT_short_bio
303
-            '%s',// ATT_modified
304
-            '%d',// ATT_author
305
-            '%d',// ATT_parent
306
-            '%s',// post_type
307
-            '%s',// status
298
+            '%s', // ATT_full_name
299
+            '%s', // ATT_bio
300
+            '%s', // ATT_slug
301
+            '%s', // ATT_created
302
+            '%s', // ATT_short_bio
303
+            '%s', // ATT_modified
304
+            '%d', // ATT_author
305
+            '%d', // ATT_parent
306
+            '%s', // post_type
307
+            '%s', // status
308 308
         );
309 309
         $success = $wpdb->insert($this->_new_attendee_cpt_table, $cols_n_values, $datatypes);
310
-        if (!$success) {
310
+        if ( ! $success) {
311 311
             $this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, $old_attendee, $this->_new_attendee_cpt_table, $cols_n_values, $datatypes));
312 312
             return 0;
313 313
         }
@@ -345,20 +345,20 @@  discard block
 block discarded – undo
345 345
             'ATT_phone' => stripslashes($old_attendee['phone']),
346 346
         );
347 347
         $datatypes = array(
348
-            '%d',// ATT_ID
349
-            '%s',// ATT_fname
350
-            '%s',// ATT_lname
351
-            '%s',// ATT_address
352
-            '%s',// ATT_address2
353
-            '%s',// ATT_city
354
-            '%d',// STA_ID
355
-            '%s',// CNT_ISO
356
-            '%s',// ATT_zip
357
-            '%s',// ATT_email
358
-            '%s',// ATT_phone
348
+            '%d', // ATT_ID
349
+            '%s', // ATT_fname
350
+            '%s', // ATT_lname
351
+            '%s', // ATT_address
352
+            '%s', // ATT_address2
353
+            '%s', // ATT_city
354
+            '%d', // STA_ID
355
+            '%s', // CNT_ISO
356
+            '%s', // ATT_zip
357
+            '%s', // ATT_email
358
+            '%s', // ATT_phone
359 359
         );
360 360
         $success = $wpdb->insert($this->_new_attendee_meta_table, $cols_n_values, $datatypes);
361
-        if (!$success) {
361
+        if ( ! $success) {
362 362
             $this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, $old_attendee, $this->_new_attendee_meta_table, $cols_n_values, $datatypes));
363 363
             return 0;
364 364
         }
@@ -382,13 +382,13 @@  discard block
 block discarded – undo
382 382
         if (intval($old_attendee['is_primary'])) {// primary attendee, so create txn
383 383
             $txn_id = $this->get_migration_script()->get_mapping_new_pk($this->_old_table, intval($old_attendee['id']), $this->_new_transaction_table);
384 384
         } else { // non-primary attendee, so find its primary attendee's transaction
385
-            $primary_attendee_old_id = $wpdb->get_var($wpdb->prepare("SELECT id FROM " . $this->_old_table . " WHERE is_primary=1 and registration_id=%s", $old_attendee['registration_id']));
386
-            if (!$primary_attendee_old_id) {
385
+            $primary_attendee_old_id = $wpdb->get_var($wpdb->prepare("SELECT id FROM ".$this->_old_table." WHERE is_primary=1 and registration_id=%s", $old_attendee['registration_id']));
386
+            if ( ! $primary_attendee_old_id) {
387 387
                 $primary_attendee = $this->_find_mer_primary_attendee_using_mer_tables($old_attendee['registration_id']);
388 388
                 $primary_attendee_old_id = is_array($primary_attendee) ? $primary_attendee['id'] : null;
389 389
             }
390 390
             $txn_id = $this->get_migration_script()->get_mapping_new_pk($this->_old_table, intval($primary_attendee_old_id), $this->_new_transaction_table);
391
-            if (!$txn_id) {
391
+            if ( ! $txn_id) {
392 392
                 $this->add_error(sprintf(esc_html__("Could not find primary attendee's new transaction. Current attendee is: %s, we think the 3.1 primary attendee for it has id %d, but there's no 4.1 transaction for that primary attendee id.", "event_espresso"), $this->_json_encode($old_attendee), $primary_attendee_old_id));
393 393
                 $txn_id = 0;
394 394
             }
@@ -396,7 +396,7 @@  discard block
 block discarded – undo
396 396
         // if there isn't yet a transaction row for this, create one
397 397
         // (so even if it was a non-primary attendee with no EE3 primary attendee,
398 398
         // it ought to have SOME transaction, so we'll make one)
399
-        if (!$txn_id) {
399
+        if ( ! $txn_id) {
400 400
             // maps 3.1 payment stati onto 4.1 transaction stati
401 401
             $txn_status_mapping = array(
402 402
                 'Completed' => 'TCM',
@@ -407,7 +407,7 @@  discard block
 block discarded – undo
407 407
                 'Cancelled' => 'TIN',
408 408
                 'Declined' => 'TIN'
409 409
             );
410
-            $STS_ID = isset($txn_status_mapping[ $old_attendee['payment_status'] ]) ? $txn_status_mapping[ $old_attendee['payment_status'] ] : 'TIN';
410
+            $STS_ID = isset($txn_status_mapping[$old_attendee['payment_status']]) ? $txn_status_mapping[$old_attendee['payment_status']] : 'TIN';
411 411
             $cols_n_values = array(
412 412
                 'TXN_timestamp' => $this->get_migration_script()->convert_date_string_to_utc($this, $old_attendee, $old_attendee['date']),
413 413
                 'TXN_total' => floatval($old_attendee['total_cost']),
@@ -416,14 +416,14 @@  discard block
 block discarded – undo
416 416
                 'TXN_hash_salt' => $old_attendee['hashSalt']
417 417
             );
418 418
             $datatypes = array(
419
-                '%s',// TXN_timestamp
420
-                '%f',// TXN_total
421
-                '%f',// TXN_paid
422
-                '%s',// STS_ID
423
-                '%s',// TXN_hash_salt
419
+                '%s', // TXN_timestamp
420
+                '%f', // TXN_total
421
+                '%f', // TXN_paid
422
+                '%s', // STS_ID
423
+                '%s', // TXN_hash_salt
424 424
             );
425 425
             $success = $wpdb->insert($this->_new_transaction_table, $cols_n_values, $datatypes);
426
-            if (!$success) {
426
+            if ( ! $success) {
427 427
                 $this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, $old_attendee, $this->_new_transaction_table, $cols_n_values, $datatypes));
428 428
                 return 0;
429 429
             }
@@ -463,7 +463,7 @@  discard block
 block discarded – undo
463 463
     {
464 464
         // need event default reg status and if pre_approval was required
465 465
         global $wpdb;
466
-        $event_required_pre_approval = $wpdb->get_var($wpdb->prepare("SELECT require_pre_approval FROM " . $wpdb->prefix . "events_detail WHERE id = %d", $old_attendee_row['event_id']));
466
+        $event_required_pre_approval = $wpdb->get_var($wpdb->prepare("SELECT require_pre_approval FROM ".$wpdb->prefix."events_detail WHERE id = %d", $old_attendee_row['event_id']));
467 467
         return $this->get_migration_script()->convert_3_1_payment_status_to_4_1_STS_ID(
468 468
             $old_attendee_row['payment_status'],
469 469
             intval($event_required_pre_approval) && intval($old_attendee_row['pre_approve'])
@@ -484,13 +484,13 @@  discard block
 block discarded – undo
484 484
         global $wpdb;
485 485
 
486 486
         $STS_ID = $this->_get_reg_status_for_old_payment_status($old_attendee);
487
-        $new_event_id = $this->get_migration_script()->get_mapping_new_pk($wpdb->prefix . 'events_detail', $old_attendee['event_id'], $wpdb->posts);
488
-        if (!$new_event_id) {
487
+        $new_event_id = $this->get_migration_script()->get_mapping_new_pk($wpdb->prefix.'events_detail', $old_attendee['event_id'], $wpdb->posts);
488
+        if ( ! $new_event_id) {
489 489
             $this->add_error(sprintf(esc_html__("Could not find NEW event CPT ID for old event '%d' on old attendee %s", "event_espresso"), $old_attendee['event_id'], $this->_json_encode($old_attendee)));
490 490
         }
491 491
 
492 492
         $ticket_id = $this->_try_to_find_new_ticket_id($old_attendee, $new_event_id);
493
-        if (!$ticket_id) {
493
+        if ( ! $ticket_id) {
494 494
             $ticket_id = $this->_insert_new_ticket_because_none_found($old_attendee, $new_event_id);
495 495
             $this->add_error(sprintf(esc_html__('Could not find a ticket for old attendee with id %d for new event %d, so created a new ticket with id %d', 'event_espresso'), $old_attendee['id'], $new_event_id, $ticket_id));
496 496
         }
@@ -514,30 +514,30 @@  discard block
 block discarded – undo
514 514
                 'REG_final_price' => $old_attendee['final_price'],
515 515
                 'REG_session' => substr($old_attendee['attendee_session'], 0, 44),
516 516
                 'REG_code' => sanitize_key($old_attendee['registration_id']),
517
-                'REG_url_link' => sanitize_key($old_attendee['registration_id'] . '-' . $count),
517
+                'REG_url_link' => sanitize_key($old_attendee['registration_id'].'-'.$count),
518 518
                 'REG_count' => $regs_on_this_event_and_txn + $count,
519 519
                 'REG_group_size' => $this->_sum_old_attendees_on_old_txn($old_attendee, false),
520 520
                 'REG_att_is_going' => true,
521 521
                 'REG_deleted' => false
522 522
             );
523 523
             $datatypes = array(
524
-                '%d',// EVT_ID
525
-                '%d',// ATT_ID
526
-                '%d',// TXN_ID
527
-                '%d',// TKT_ID
528
-                '%s',// STS_ID
529
-                '%s',// REG_date
530
-                '%f',// REG_final_price
531
-                '%s',// REG_session
532
-                '%s',// REG_code
533
-                '%s',// REG_url_link
534
-                '%d',// REG_count
535
-                '%d',// REG_group_size
536
-                '%d',// REG_att_is_going
537
-                '%d',// REG_deleted
524
+                '%d', // EVT_ID
525
+                '%d', // ATT_ID
526
+                '%d', // TXN_ID
527
+                '%d', // TKT_ID
528
+                '%s', // STS_ID
529
+                '%s', // REG_date
530
+                '%f', // REG_final_price
531
+                '%s', // REG_session
532
+                '%s', // REG_code
533
+                '%s', // REG_url_link
534
+                '%d', // REG_count
535
+                '%d', // REG_group_size
536
+                '%d', // REG_att_is_going
537
+                '%d', // REG_deleted
538 538
             );
539 539
             $success = $wpdb->insert($this->_new_reg_table, $cols_n_values, $datatypes);
540
-            if (!$success) {
540
+            if ( ! $success) {
541 541
                 $this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, $old_attendee, $this->_new_reg_table, $cols_n_values, $datatypes));
542 542
                 return 0;
543 543
             }
@@ -573,7 +573,7 @@  discard block
 block discarded – undo
573 573
 				INNER JOIN {$this->_new_ticket_datetime_table} as DTK ON TKT.TKT_ID = DTK.TKT_ID
574 574
 				INNER JOIN {$this->_new_datetime_table} as DTT ON DTK.DTT_ID = DTT.DTT_ID
575 575
 				SET DTT.DTT_sold = DTT.DTT_sold + %d WHERE TKT.TKT_ID = %d", $quantity_sold, $new_ticket_id));
576
-            if (!$success_update_dateimtes) {
576
+            if ( ! $success_update_dateimtes) {
577 577
                 $this->add_error(sprintf(esc_html__("Could not update datetimes related to ticket with ID %d's TKT_sold by %d because %s", "event_espresso"), $new_ticket_id, $quantity_sold, $wpdb->last_error));
578 578
             }
579 579
         } else {
@@ -606,20 +606,20 @@  discard block
 block discarded – undo
606 606
         // add all conditions to an array from which we can SHIFT conditions off in order to widen our search
607 607
         // the most important condition should be last, as it will be array_shift'ed off last
608 608
         $conditions = array(
609
-            $wpdb->prepare("$datetime_table.DTT_EVT_start = %s", $old_att_datetime),// times match?
610
-            $wpdb->prepare("$tickets_table.TKT_price = %f", $old_att_price),// prices match?
611
-            $wpdb->prepare("$tickets_table.TKT_name = %s", $old_att_price_option),// names match?
612
-            $wpdb->prepare("$datetime_table.EVT_ID = %d", $new_event_id),// events match?
609
+            $wpdb->prepare("$datetime_table.DTT_EVT_start = %s", $old_att_datetime), // times match?
610
+            $wpdb->prepare("$tickets_table.TKT_price = %f", $old_att_price), // prices match?
611
+            $wpdb->prepare("$tickets_table.TKT_name = %s", $old_att_price_option), // names match?
612
+            $wpdb->prepare("$datetime_table.EVT_ID = %d", $new_event_id), // events match?
613 613
         );
614 614
         $select_and_join_part = "SELECT $tickets_table.TKT_ID FROM $tickets_table INNER JOIN
615 615
 			$datetime_tickets_table ON $tickets_table.TKT_ID = $datetime_tickets_table.TKT_ID INNER JOIN
616 616
 			$datetime_table ON $datetime_tickets_table.DTT_ID = $datetime_table.DTT_ID";
617 617
         // start running queries, widening search each time by removing a condition
618 618
         do {
619
-            $full_query = $select_and_join_part . " WHERE " . implode(" AND ", $conditions) . " LIMIT 1";
619
+            $full_query = $select_and_join_part." WHERE ".implode(" AND ", $conditions)." LIMIT 1";
620 620
             $ticket_id_found = $wpdb->get_var($full_query);
621 621
             array_shift($conditions);
622
-        } while (!$ticket_id_found && $conditions);
622
+        }while ( ! $ticket_id_found && $conditions);
623 623
         return $ticket_id_found;
624 624
     }
625 625
 
@@ -641,8 +641,8 @@  discard block
 block discarded – undo
641 641
 
642 642
 
643 643
         // insert new datetime unless we find one
644
-        $datetime_id = $wpdb->get_var($wpdb->prepare("SELECT DTT_ID FROM " . $this->_new_datetime_table . " WHERE DTT_EVT_start=%s AND EVT_ID=%d LIMIT 1", $old_att_start_datetime, $new_event_id), ARRAY_A);
645
-        if (!$datetime_id) {
644
+        $datetime_id = $wpdb->get_var($wpdb->prepare("SELECT DTT_ID FROM ".$this->_new_datetime_table." WHERE DTT_EVT_start=%s AND EVT_ID=%d LIMIT 1", $old_att_start_datetime, $new_event_id), ARRAY_A);
645
+        if ( ! $datetime_id) {
646 646
             $old_att_end_date = $old_attendee['start_date'];
647 647
             $old_att_end_time = $this->get_migration_script()->convertTimeFromAMPM($old_attendee['event_time']);
648 648
             $old_att_end_datetime = $this->get_migration_script()->convert_date_string_to_utc($this, $old_attendee, "$old_att_end_date $old_att_end_time:00");
@@ -655,10 +655,10 @@  discard block
 block discarded – undo
655 655
                     'DTT_deleted' => true
656 656
                 ),
657 657
                 array(
658
-                    '%d',// EVT_ID
659
-                    '%s',// DTT_EVT_start
660
-                    '%s',// DTT_EVT_end
661
-                    '%d',// DTT_deleted
658
+                    '%d', // EVT_ID
659
+                    '%s', // DTT_EVT_start
660
+                    '%s', // DTT_EVT_end
661
+                    '%d', // DTT_deleted
662 662
                 )
663 663
             );
664 664
             $datetime_id = $wpdb->insert_id;
@@ -666,21 +666,21 @@  discard block
 block discarded – undo
666 666
 
667 667
         // insert new ticket
668 668
         $success = $wpdb->insert(
669
-            $wpdb->prefix . 'esp_ticket',
669
+            $wpdb->prefix.'esp_ticket',
670 670
             array(
671 671
                 'TKT_name' => $old_att_price_option,
672 672
                 'TKT_qty' => -1,
673 673
                 'TKT_price' => $old_att_price,
674
-                'TKT_start_date' => $old_att_start_datetime,// we really have no clue what the time should be, but at least it was available when they attended
674
+                'TKT_start_date' => $old_att_start_datetime, // we really have no clue what the time should be, but at least it was available when they attended
675 675
                 'TKT_end_date' => $old_att_end_datetime,
676 676
 
677 677
             ),
678 678
             array(
679
-                '%s',// name
680
-                '%d',// qty
681
-                '%d',// price
682
-                '%s',// start_date
683
-                '%s',// end_date
679
+                '%s', // name
680
+                '%d', // qty
681
+                '%d', // price
682
+                '%s', // start_date
683
+                '%s', // end_date
684 684
             )
685 685
         );
686 686
         $ticket_id = $wpdb->insert_id;
@@ -692,8 +692,8 @@  discard block
 block discarded – undo
692 692
                 'TKT_ID' => $ticket_id
693 693
             ),
694 694
             array(
695
-                '%d',// DTT_ID
696
-                '%d',// TKT_ID
695
+                '%d', // DTT_ID
696
+                '%d', // TKT_ID
697 697
             )
698 698
         );
699 699
         // insert new price
@@ -706,10 +706,10 @@  discard block
 block discarded – undo
706 706
                 'PRC_deleted' => true
707 707
             ),
708 708
             array(
709
-                '%d',// PRC_amount
710
-                '%d',// PRT_ID
711
-                '%s',// PRC_name
712
-                '%d',// PRC_deleted
709
+                '%d', // PRC_amount
710
+                '%d', // PRT_ID
711
+                '%s', // PRC_name
712
+                '%d', // PRC_deleted
713 713
             )
714 714
         );
715 715
         $price_id = $wpdb->insert_id;
@@ -721,8 +721,8 @@  discard block
 block discarded – undo
721 721
                 'PRC_ID' => $price_id
722 722
             ),
723 723
             array(
724
-                '%d',// TKT_ID
725
-                '%d',// PRC_ID
724
+                '%d', // TKT_ID
725
+                '%d', // PRC_ID
726 726
             )
727 727
         );
728 728
         return $ticket_id;
@@ -740,7 +740,7 @@  discard block
 block discarded – undo
740 740
     {
741 741
         global $wpdb;
742 742
         $count_only_older_sql = $count_only_older ? $wpdb->prepare(" AND id<%d", $old_attendee_row['id']) : '';
743
-        $count = intval($wpdb->get_var($wpdb->prepare("SELECT SUM(quantity) FROM " . $this->_old_table . " WHERE registration_id=%s $count_only_older_sql", $old_attendee_row['registration_id'])));
743
+        $count = intval($wpdb->get_var($wpdb->prepare("SELECT SUM(quantity) FROM ".$this->_old_table." WHERE registration_id=%s $count_only_older_sql", $old_attendee_row['registration_id'])));
744 744
 
745 745
         if ($this->_mer_tables_exist()) {
746 746
             // if MER exists, then its a little tricky.
@@ -764,7 +764,7 @@  discard block
 block discarded – undo
764 764
         // Also, an offline payment had blank status (ie, "").
765 765
         $old_pay_stati_indicating_no_payment = array('', 'Incomplete', 'Not Completed');
766 766
         // if this is for a primary 3.1 attendee which WASN'T free and has a completed, cancelled, or declined payment...
767
-        if (intval($old_attendee['is_primary']) && floatval($old_attendee['total_cost']) && !in_array($old_attendee['payment_status'], $old_pay_stati_indicating_no_payment)) {
767
+        if (intval($old_attendee['is_primary']) && floatval($old_attendee['total_cost']) && ! in_array($old_attendee['payment_status'], $old_pay_stati_indicating_no_payment)) {
768 768
             $pay_status_mapping = array(
769 769
                 'Completed' => 'PAP',
770 770
                 'Payment Declined' => 'PDC',
@@ -772,7 +772,7 @@  discard block
 block discarded – undo
772 772
                 'Declined' => 'PDC'
773 773
             );
774 774
             $by_admin = $old_attendee['payment'] == 'Admin';
775
-            $STS_ID = isset($pay_status_mapping[ $old_attendee['payment_status'] ]) ? $pay_status_mapping[ $old_attendee['payment_status'] ] : 'PFL';// IE, if we don't recognize the status, assume payment failed
775
+            $STS_ID = isset($pay_status_mapping[$old_attendee['payment_status']]) ? $pay_status_mapping[$old_attendee['payment_status']] : 'PFL'; // IE, if we don't recognize the status, assume payment failed
776 776
             $cols_n_values = array(
777 777
                 'TXN_ID' => $new_txn_id,
778 778
                 'STS_ID' => $STS_ID,
@@ -788,19 +788,19 @@  discard block
 block discarded – undo
788 788
                 'PAY_details' => $old_attendee['transaction_details']
789 789
             );
790 790
             $datatypes = array(
791
-                '%d',// TXN_Id
792
-                '%s',// STS_ID
793
-                '%s',// PAY_timestamp
794
-                '%s',// PAY_method
795
-                '%f',// PAY_amount
796
-                '%s',// PAY_gateway
797
-                '%s',// PAY_gateway_response
798
-                '%s',// PAY_txn_id_chq_nmbr
799
-                '%d',// PAY_via_admin
800
-                '%s',// PAY_details
791
+                '%d', // TXN_Id
792
+                '%s', // STS_ID
793
+                '%s', // PAY_timestamp
794
+                '%s', // PAY_method
795
+                '%f', // PAY_amount
796
+                '%s', // PAY_gateway
797
+                '%s', // PAY_gateway_response
798
+                '%s', // PAY_txn_id_chq_nmbr
799
+                '%d', // PAY_via_admin
800
+                '%s', // PAY_details
801 801
             );
802 802
             $success = $wpdb->insert($this->_new_payment_table, $cols_n_values, $datatypes);
803
-            if (!$success) {
803
+            if ( ! $success) {
804 804
                 $this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, $old_attendee, $this->_new_attendee_cpt_table, $cols_n_values, $datatypes));
805 805
                 return 0;
806 806
             }
@@ -819,7 +819,7 @@  discard block
 block discarded – undo
819 819
      */
820 820
     private function _find_mer_primary_attendee_using_mer_tables($old_registration_id)
821 821
     {
822
-        if (!$this->_mer_tables_exist()) {
822
+        if ( ! $this->_mer_tables_exist()) {
823 823
             return false;
824 824
         }
825 825
         global $wpdb;
Please login to merge, or discard this patch.
data_migration_scripts/4_1_0_stages/EE_DMS_4_1_0_org_options.dmsstage.php 2 patches
Spacing   +31 added lines, -31 removed lines patch added patch discarded remove patch
@@ -110,8 +110,8 @@  discard block
 block discarded – undo
110 110
         $old_org_options = get_option('events_organization_settings');
111 111
         foreach ($this->_org_options_we_know_how_to_migrate as $option_name) {
112 112
             // only bother migrating if there's a setting to migrate. Otherwise we'll just use the default
113
-            if (isset($old_org_options[ $option_name ])) {
114
-                $this->_handle_org_option($option_name, $old_org_options[ $option_name ]);
113
+            if (isset($old_org_options[$option_name])) {
114
+                $this->_handle_org_option($option_name, $old_org_options[$option_name]);
115 115
             }
116 116
             if ($option_name == 'surcharge') {
117 117
                 $this->_insert_new_global_surcharge_price($old_org_options);
@@ -120,7 +120,7 @@  discard block
 block discarded – undo
120 120
         }
121 121
 
122 122
         $success = EE_Config::instance()->update_espresso_config(false, true);
123
-        if (! $success) {
123
+        if ( ! $success) {
124 124
             $this->add_error(sprintf(esc_html__('Could not save EE Config during org options stage. Reason: %s', 'event_espresso'), EE_Error::get_notices(false)));
125 125
             EE_Error::overwrite_errors();
126 126
         }
@@ -176,24 +176,24 @@  discard block
 block discarded – undo
176 176
                 $c->organization->email = $value;
177 177
                 break;
178 178
             case 'default_payment_status':
179
-                $c->registration->default_STS_ID =  $this->get_migration_script()->convert_3_1_payment_status_to_4_1_STS_ID($value);
179
+                $c->registration->default_STS_ID = $this->get_migration_script()->convert_3_1_payment_status_to_4_1_STS_ID($value);
180 180
                 break;
181 181
             case 'organization_country':
182 182
                 $iso = $this->get_migration_script()->get_iso_from_3_1_country_id($value);
183 183
                 $c->organization->CNT_ISO = $iso;
184 184
                 $country_row = $this->get_migration_script()->get_or_create_country($iso);
185
-                if (! $country_row) {
185
+                if ( ! $country_row) {
186 186
                     $this->add_error(sprintf(esc_html__("Could not set country's currency config because no country exists for ISO %s", "event_espresso"), $iso));
187 187
                 }
188 188
                 // can't use EE_Currency_Config's handy constructor because the models are off-limits right now (and it uses them)
189
-                $c->currency->code = $country_row['CNT_cur_code'];          // currency code: USD, CAD, EUR
190
-                $c->currency->name = $country_row['CNT_cur_single'];    // Dollar
191
-                $c->currency->plural = $country_row['CNT_cur_plural'];  // Dollars
192
-                $c->currency->sign =  $country_row['CNT_cur_sign'];             // currency sign: $
193
-                $c->currency->sign_b4 = filter_var($country_row['CNT_cur_sign_b4'], FILTER_VALIDATE_BOOLEAN);        // currency sign before or after: $TRUE  or  FALSE$
194
-                $c->currency->dec_plc = (int) $country_row['CNT_cur_dec_plc'];    // decimal places: 2 = 0.00  3 = 0.000
195
-                $c->currency->dec_mrk = $country_row['CNT_cur_dec_mrk'];    // decimal mark: (comma) ',' = 0,01   or (decimal) '.' = 0.01
196
-                $c->currency->thsnds = $country_row['CNT_cur_thsnds'];  // thousands separator: (comma) ',' = 1,000   or (decimal) '.' = 1.000
189
+                $c->currency->code = $country_row['CNT_cur_code']; // currency code: USD, CAD, EUR
190
+                $c->currency->name = $country_row['CNT_cur_single']; // Dollar
191
+                $c->currency->plural = $country_row['CNT_cur_plural']; // Dollars
192
+                $c->currency->sign = $country_row['CNT_cur_sign']; // currency sign: $
193
+                $c->currency->sign_b4 = filter_var($country_row['CNT_cur_sign_b4'], FILTER_VALIDATE_BOOLEAN); // currency sign before or after: $TRUE  or  FALSE$
194
+                $c->currency->dec_plc = (int) $country_row['CNT_cur_dec_plc']; // decimal places: 2 = 0.00  3 = 0.000
195
+                $c->currency->dec_mrk = $country_row['CNT_cur_dec_mrk']; // decimal mark: (comma) ',' = 0,01   or (decimal) '.' = 0.01
196
+                $c->currency->thsnds = $country_row['CNT_cur_thsnds']; // thousands separator: (comma) ',' = 1,000   or (decimal) '.' = 1.000
197 197
   //            $c->currency = new EE_Currency_Config($c->organization->CNT_ISO);break;
198 198
   //        case 'currency_symbol': ignore the currency symbol. we'll just go by their country.
199 199
   //            $c->currency->sign = $value;break;
@@ -298,18 +298,18 @@  discard block
 block discarded – undo
298 298
 
299 299
         );
300 300
         $datatypes = array(
301
-            '%d',// PRT_ID
302
-            '%f',// PRT_amount
303
-            '%s',// PRC_name
304
-            '%d',// PRC_is_default
305
-            '%d',// PRC_overrides
306
-            '%d',// PRC_order
307
-            '%d',// PRC_deleted
308
-            '%d',// PRC_parent
301
+            '%d', // PRT_ID
302
+            '%f', // PRT_amount
303
+            '%s', // PRC_name
304
+            '%d', // PRC_is_default
305
+            '%d', // PRC_overrides
306
+            '%d', // PRC_order
307
+            '%d', // PRC_deleted
308
+            '%d', // PRC_parent
309 309
         );
310
-        $price_table = $wpdb->prefix . "esp_price";
310
+        $price_table = $wpdb->prefix."esp_price";
311 311
         $success = $wpdb->insert($price_table, $cols_n_values, $datatypes);
312
-        if (! $success) {
312
+        if ( ! $success) {
313 313
             $this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion(
314 314
                 'org_options',
315 315
                 array(
@@ -339,8 +339,8 @@  discard block
 block discarded – undo
339 339
       'payment_message',
340 340
       'message',
341 341
       'default_payment_status',
342
-      'surcharge',// unused?
343
-      'country_id',// unused?
342
+      'surcharge', // unused?
343
+      'country_id', // unused?
344 344
       'organization_country',
345 345
 //    'currency_symbol',
346 346
       'expire_on_registration_end',
@@ -352,13 +352,13 @@  discard block
 block discarded – undo
352 352
       'show_pending_payment_options',
353 353
       'show_reg_footer',
354 354
       'skip_confirmation_page',
355
-      'allow_mer_discounts',// no equiv
356
-      'allow_mer_vouchers',// no equiv
355
+      'allow_mer_discounts', // no equiv
356
+      'allow_mer_vouchers', // no equiv
357 357
       'display_short_description_in_event_list',
358 358
       'display_description_on_multi_reg_page',
359 359
       'display_address_in_event_list',
360 360
       'display_address_in_regform',
361
-      'use_custom_post_types',// no equiv
361
+      'use_custom_post_types', // no equiv
362 362
       'display_ical_download',
363 363
       'display_featured_image',
364 364
       'themeroller',
@@ -377,11 +377,11 @@  discard block
 block discarded – undo
377 377
       'espresso_dashboard_widget',
378 378
       'time_reg_limit',
379 379
 //    'use_attendee_pre_approval', removed in 4.1- instead this is factored into the default reg status
380
-      'use_personnel_manager',// no equiv
380
+      'use_personnel_manager', // no equiv
381 381
       'use_event_timezones',
382 382
       'full_logging',
383
-      'surcharge_type',// unused
384
-      'surcharge_text',// unused
383
+      'surcharge_type', // unused
384
+      'surcharge_text', // unused
385 385
       'affiliate_id',
386 386
       'site_license_key',
387 387
     );
Please login to merge, or discard this patch.
Indentation   +270 added lines, -270 removed lines patch added patch discarded remove patch
@@ -102,286 +102,286 @@
 block discarded – undo
102 102
  */
103 103
 class EE_DMS_4_1_0_org_options extends EE_Data_Migration_Script_Stage
104 104
 {
105
-    public function _migration_step($num_items = 50)
106
-    {
105
+	public function _migration_step($num_items = 50)
106
+	{
107 107
 
108
-        $items_actually_migrated = 0;
109
-        $old_org_options = get_option('events_organization_settings');
110
-        foreach ($this->_org_options_we_know_how_to_migrate as $option_name) {
111
-            // only bother migrating if there's a setting to migrate. Otherwise we'll just use the default
112
-            if (isset($old_org_options[ $option_name ])) {
113
-                $this->_handle_org_option($option_name, $old_org_options[ $option_name ]);
114
-            }
115
-            if ($option_name == 'surcharge') {
116
-                $this->_insert_new_global_surcharge_price($old_org_options);
117
-            }
118
-            $items_actually_migrated++;
119
-        }
108
+		$items_actually_migrated = 0;
109
+		$old_org_options = get_option('events_organization_settings');
110
+		foreach ($this->_org_options_we_know_how_to_migrate as $option_name) {
111
+			// only bother migrating if there's a setting to migrate. Otherwise we'll just use the default
112
+			if (isset($old_org_options[ $option_name ])) {
113
+				$this->_handle_org_option($option_name, $old_org_options[ $option_name ]);
114
+			}
115
+			if ($option_name == 'surcharge') {
116
+				$this->_insert_new_global_surcharge_price($old_org_options);
117
+			}
118
+			$items_actually_migrated++;
119
+		}
120 120
 
121
-        $success = EE_Config::instance()->update_espresso_config(false, true);
122
-        if (! $success) {
123
-            $this->add_error(sprintf(esc_html__('Could not save EE Config during org options stage. Reason: %s', 'event_espresso'), EE_Error::get_notices(false)));
124
-            EE_Error::overwrite_errors();
125
-        }
126
-        EE_Network_Config::instance()->update_config(false, false);
127
-        if ($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate()) {
128
-            // we may have added new pages and this might be necessary
129
-            flush_rewrite_rules();
130
-            $this->set_completed();
131
-        }
132
-        return $items_actually_migrated;
133
-    }
134
-    public function _count_records_to_migrate()
135
-    {
136
-        $count_of_options_to_migrate = count($this->_org_options_we_know_how_to_migrate);
137
-        return $count_of_options_to_migrate;
138
-    }
139
-    public function __construct()
140
-    {
141
-        $this->_pretty_name = esc_html__("Organization Options/Config", "event_espresso");
142
-        $this->_org_options_we_know_how_to_migrate = apply_filters('FHEE__EE_DMS_4_1_0_org_options__org_options_we_know_how_to_migrate', $this->_org_options_we_know_how_to_migrate);
143
-        parent::__construct();
144
-    }
121
+		$success = EE_Config::instance()->update_espresso_config(false, true);
122
+		if (! $success) {
123
+			$this->add_error(sprintf(esc_html__('Could not save EE Config during org options stage. Reason: %s', 'event_espresso'), EE_Error::get_notices(false)));
124
+			EE_Error::overwrite_errors();
125
+		}
126
+		EE_Network_Config::instance()->update_config(false, false);
127
+		if ($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate()) {
128
+			// we may have added new pages and this might be necessary
129
+			flush_rewrite_rules();
130
+			$this->set_completed();
131
+		}
132
+		return $items_actually_migrated;
133
+	}
134
+	public function _count_records_to_migrate()
135
+	{
136
+		$count_of_options_to_migrate = count($this->_org_options_we_know_how_to_migrate);
137
+		return $count_of_options_to_migrate;
138
+	}
139
+	public function __construct()
140
+	{
141
+		$this->_pretty_name = esc_html__("Organization Options/Config", "event_espresso");
142
+		$this->_org_options_we_know_how_to_migrate = apply_filters('FHEE__EE_DMS_4_1_0_org_options__org_options_we_know_how_to_migrate', $this->_org_options_we_know_how_to_migrate);
143
+		parent::__construct();
144
+	}
145 145
 
146
-    private function _handle_org_option($option_name, $value)
147
-    {
148
-        $c = EE_Config::instance();
149
-        $cn = EE_Network_Config::instance();
150
-        switch ($option_name) {
151
-            case 'organization':
152
-                $c->organization->name = $value;
153
-                break;
154
-            case 'organization_street1':
155
-                $c->organization->address_1 = $value;
156
-                break;
157
-            case 'organization_street2':
158
-                $c->organization->address_2 = $value;
159
-                break;
160
-            case 'organization_city':
161
-                $c->organization->city = $value;
162
-                break;
163
-            case 'organization_state':
164
-                try {
165
-                    $state = $this->get_migration_script()->get_or_create_state($value);
166
-                    $state_id = $state['STA_ID'];
167
-                    $c->organization->STA_ID = $state_id;
168
-                } catch (EE_Error $e) {
169
-                }
170
-                break;
171
-            case 'organization_zip':
172
-                $c->organization->zip = $value;
173
-                break;
174
-            case 'contact_email':
175
-                $c->organization->email = $value;
176
-                break;
177
-            case 'default_payment_status':
178
-                $c->registration->default_STS_ID =  $this->get_migration_script()->convert_3_1_payment_status_to_4_1_STS_ID($value);
179
-                break;
180
-            case 'organization_country':
181
-                $iso = $this->get_migration_script()->get_iso_from_3_1_country_id($value);
182
-                $c->organization->CNT_ISO = $iso;
183
-                $country_row = $this->get_migration_script()->get_or_create_country($iso);
184
-                if (! $country_row) {
185
-                    $this->add_error(sprintf(esc_html__("Could not set country's currency config because no country exists for ISO %s", "event_espresso"), $iso));
186
-                }
187
-                // can't use EE_Currency_Config's handy constructor because the models are off-limits right now (and it uses them)
188
-                $c->currency->code = $country_row['CNT_cur_code'];          // currency code: USD, CAD, EUR
189
-                $c->currency->name = $country_row['CNT_cur_single'];    // Dollar
190
-                $c->currency->plural = $country_row['CNT_cur_plural'];  // Dollars
191
-                $c->currency->sign =  $country_row['CNT_cur_sign'];             // currency sign: $
192
-                $c->currency->sign_b4 = filter_var($country_row['CNT_cur_sign_b4'], FILTER_VALIDATE_BOOLEAN);        // currency sign before or after: $TRUE  or  FALSE$
193
-                $c->currency->dec_plc = (int) $country_row['CNT_cur_dec_plc'];    // decimal places: 2 = 0.00  3 = 0.000
194
-                $c->currency->dec_mrk = $country_row['CNT_cur_dec_mrk'];    // decimal mark: (comma) ',' = 0,01   or (decimal) '.' = 0.01
195
-                $c->currency->thsnds = $country_row['CNT_cur_thsnds'];  // thousands separator: (comma) ',' = 1,000   or (decimal) '.' = 1.000
146
+	private function _handle_org_option($option_name, $value)
147
+	{
148
+		$c = EE_Config::instance();
149
+		$cn = EE_Network_Config::instance();
150
+		switch ($option_name) {
151
+			case 'organization':
152
+				$c->organization->name = $value;
153
+				break;
154
+			case 'organization_street1':
155
+				$c->organization->address_1 = $value;
156
+				break;
157
+			case 'organization_street2':
158
+				$c->organization->address_2 = $value;
159
+				break;
160
+			case 'organization_city':
161
+				$c->organization->city = $value;
162
+				break;
163
+			case 'organization_state':
164
+				try {
165
+					$state = $this->get_migration_script()->get_or_create_state($value);
166
+					$state_id = $state['STA_ID'];
167
+					$c->organization->STA_ID = $state_id;
168
+				} catch (EE_Error $e) {
169
+				}
170
+				break;
171
+			case 'organization_zip':
172
+				$c->organization->zip = $value;
173
+				break;
174
+			case 'contact_email':
175
+				$c->organization->email = $value;
176
+				break;
177
+			case 'default_payment_status':
178
+				$c->registration->default_STS_ID =  $this->get_migration_script()->convert_3_1_payment_status_to_4_1_STS_ID($value);
179
+				break;
180
+			case 'organization_country':
181
+				$iso = $this->get_migration_script()->get_iso_from_3_1_country_id($value);
182
+				$c->organization->CNT_ISO = $iso;
183
+				$country_row = $this->get_migration_script()->get_or_create_country($iso);
184
+				if (! $country_row) {
185
+					$this->add_error(sprintf(esc_html__("Could not set country's currency config because no country exists for ISO %s", "event_espresso"), $iso));
186
+				}
187
+				// can't use EE_Currency_Config's handy constructor because the models are off-limits right now (and it uses them)
188
+				$c->currency->code = $country_row['CNT_cur_code'];          // currency code: USD, CAD, EUR
189
+				$c->currency->name = $country_row['CNT_cur_single'];    // Dollar
190
+				$c->currency->plural = $country_row['CNT_cur_plural'];  // Dollars
191
+				$c->currency->sign =  $country_row['CNT_cur_sign'];             // currency sign: $
192
+				$c->currency->sign_b4 = filter_var($country_row['CNT_cur_sign_b4'], FILTER_VALIDATE_BOOLEAN);        // currency sign before or after: $TRUE  or  FALSE$
193
+				$c->currency->dec_plc = (int) $country_row['CNT_cur_dec_plc'];    // decimal places: 2 = 0.00  3 = 0.000
194
+				$c->currency->dec_mrk = $country_row['CNT_cur_dec_mrk'];    // decimal mark: (comma) ',' = 0,01   or (decimal) '.' = 0.01
195
+				$c->currency->thsnds = $country_row['CNT_cur_thsnds'];  // thousands separator: (comma) ',' = 1,000   or (decimal) '.' = 1.000
196 196
   //            $c->currency = new EE_Currency_Config($c->organization->CNT_ISO);break;
197 197
   //        case 'currency_symbol': ignore the currency symbol. we'll just go by their country.
198 198
   //            $c->currency->sign = $value;break;
199
-            case 'show_pending_payment_options':
200
-                $c->registration->show_pending_payment_options = ($value == 'Y');
201
-                break;
202
-            case 'display_address_in_regform':
203
-                $c->template_settings->display_address_in_regform = ($value == 'Y');
204
-                break;
205
-            case 'default_logo_url':
206
-                $c->organization->logo_url = $value;
207
-                break;
208
-            case 'event_page_id':
209
-                // also, find that post, and changes the shortcode in it from ESPRESSO_PAYMENTS
210
-                // to ESPRESSO_THANK_YOU
211
-                $reg_page_post = get_post($value);
212
-                $reg_page_post->post_content = str_replace("[ESPRESSO_EVENTS]", "[ESPRESSO_CHECKOUT]", $reg_page_post->post_content);
213
-                wp_update_post($reg_page_post);
214
-                $c->core->reg_page_id = $value;
215
-                break;
216
-            case 'return_url':
217
-                // also, find that post, and changes the shortcode in it from ESPRESSO_PAYMENTS
218
-                // to ESPRESSO_THANK_YOU
219
-                $thank_you_page_post = get_post($value);
220
-                $thank_you_page_post->post_content = str_replace("[ESPRESSO_PAYMENTS]", "[ESPRESSO_THANK_YOU]", $thank_you_page_post->post_content);
221
-                wp_update_post($thank_you_page_post);
222
-                $c->core->thank_you_page_id = $value;
223
-                break;
224
-            case 'cancel_return':
225
-                $c->core->cancel_page_id = $value;
199
+			case 'show_pending_payment_options':
200
+				$c->registration->show_pending_payment_options = ($value == 'Y');
201
+				break;
202
+			case 'display_address_in_regform':
203
+				$c->template_settings->display_address_in_regform = ($value == 'Y');
204
+				break;
205
+			case 'default_logo_url':
206
+				$c->organization->logo_url = $value;
207
+				break;
208
+			case 'event_page_id':
209
+				// also, find that post, and changes the shortcode in it from ESPRESSO_PAYMENTS
210
+				// to ESPRESSO_THANK_YOU
211
+				$reg_page_post = get_post($value);
212
+				$reg_page_post->post_content = str_replace("[ESPRESSO_EVENTS]", "[ESPRESSO_CHECKOUT]", $reg_page_post->post_content);
213
+				wp_update_post($reg_page_post);
214
+				$c->core->reg_page_id = $value;
215
+				break;
216
+			case 'return_url':
217
+				// also, find that post, and changes the shortcode in it from ESPRESSO_PAYMENTS
218
+				// to ESPRESSO_THANK_YOU
219
+				$thank_you_page_post = get_post($value);
220
+				$thank_you_page_post->post_content = str_replace("[ESPRESSO_PAYMENTS]", "[ESPRESSO_THANK_YOU]", $thank_you_page_post->post_content);
221
+				wp_update_post($thank_you_page_post);
222
+				$c->core->thank_you_page_id = $value;
223
+				break;
224
+			case 'cancel_return':
225
+				$c->core->cancel_page_id = $value;
226 226
 
227
-                break;
228
-            case 'notify_url':
229
-                $c->core->txn_page_id = $value;
230
-                break;
231
-            case 'use_captcha':
232
-                $c->registration->use_captcha = ($value == 'Y');
233
-                break;
234
-            case 'recaptcha_publickey':
235
-                $c->registration->recaptcha_publickey = $value;
236
-                break;
237
-            case 'recaptcha_privatekey':
238
-                $c->registration->recaptcha_privatekey = $value;
239
-                break;
240
-            case 'recaptcha_theme':
241
-                $c->registration->recaptcha_theme = $value;
242
-                break;
243
-            case 'recaptcha_width':
244
-                $c->registration->recaptcha_width = $value;
245
-                break;
246
-            case 'recaptcha_language':
247
-                $c->registration->recaptcha_language = $value;
248
-                break;
249
-            case 'espresso_dashboard_widget':
250
-                $c->admin->use_dashboard_widget = ($value == 'Y');
251
-                break;
252
-            case 'use_personnel_manager':
253
-                $c->admin->use_personnel_manager = ($value == 'Y');
254
-                break;
255
-            case 'use_event_timezones':
256
-                $c->admin->use_event_timezones = ($value == 'Y');
257
-                break;
258
-            case 'affiliate_id':
259
-                $c->admin->affiliate_id = $value;
260
-                break;
261
-            case 'site_license_key':
262
-                $cn->core->site_license_key = $value;
263
-                break;
264
-            default:
265
-                do_action('AHEE__EE_DMS_4_1_0__handle_org_option', $option_name, $value);
266
-        }
267
-    }
227
+				break;
228
+			case 'notify_url':
229
+				$c->core->txn_page_id = $value;
230
+				break;
231
+			case 'use_captcha':
232
+				$c->registration->use_captcha = ($value == 'Y');
233
+				break;
234
+			case 'recaptcha_publickey':
235
+				$c->registration->recaptcha_publickey = $value;
236
+				break;
237
+			case 'recaptcha_privatekey':
238
+				$c->registration->recaptcha_privatekey = $value;
239
+				break;
240
+			case 'recaptcha_theme':
241
+				$c->registration->recaptcha_theme = $value;
242
+				break;
243
+			case 'recaptcha_width':
244
+				$c->registration->recaptcha_width = $value;
245
+				break;
246
+			case 'recaptcha_language':
247
+				$c->registration->recaptcha_language = $value;
248
+				break;
249
+			case 'espresso_dashboard_widget':
250
+				$c->admin->use_dashboard_widget = ($value == 'Y');
251
+				break;
252
+			case 'use_personnel_manager':
253
+				$c->admin->use_personnel_manager = ($value == 'Y');
254
+				break;
255
+			case 'use_event_timezones':
256
+				$c->admin->use_event_timezones = ($value == 'Y');
257
+				break;
258
+			case 'affiliate_id':
259
+				$c->admin->affiliate_id = $value;
260
+				break;
261
+			case 'site_license_key':
262
+				$cn->core->site_license_key = $value;
263
+				break;
264
+			default:
265
+				do_action('AHEE__EE_DMS_4_1_0__handle_org_option', $option_name, $value);
266
+		}
267
+	}
268 268
 
269
-    /**
270
-     * Creates a 4.1 member price discount
271
-     * @global type $wpdb
272
-     * @param type $old_price
273
-     * @return int
274
-     */
275
-    private function _insert_new_global_surcharge_price($org_options)
276
-    {
277
-        $amount = floatval($org_options['surcharge']);
278
-        // dont createa a price if the surcharge is 0
279
-        if ($amount <= .01) {
280
-            return 0;
281
-        }
282
-        if ($org_options['surcharge_type'] == 'flat_rate') {
283
-            $price_type = EE_DMS_4_1_0_prices::price_type_flat_surcharge;
284
-        } else {
285
-            $price_type = EE_DMS_4_1_0_prices::price_type_percent_surcharge;
286
-        }
287
-        global $wpdb;
288
-        $cols_n_values = array(
289
-            'PRT_ID' => $price_type,
290
-            'PRC_amount' => $amount,
291
-            'PRC_name' =>  $org_options['surcharge_text'],
292
-            'PRC_is_default' => true,
293
-            'PRC_overrides' => false,
294
-            'PRC_order' => 100,
295
-            'PRC_deleted' => false,
296
-            'PRC_parent' => null
269
+	/**
270
+	 * Creates a 4.1 member price discount
271
+	 * @global type $wpdb
272
+	 * @param type $old_price
273
+	 * @return int
274
+	 */
275
+	private function _insert_new_global_surcharge_price($org_options)
276
+	{
277
+		$amount = floatval($org_options['surcharge']);
278
+		// dont createa a price if the surcharge is 0
279
+		if ($amount <= .01) {
280
+			return 0;
281
+		}
282
+		if ($org_options['surcharge_type'] == 'flat_rate') {
283
+			$price_type = EE_DMS_4_1_0_prices::price_type_flat_surcharge;
284
+		} else {
285
+			$price_type = EE_DMS_4_1_0_prices::price_type_percent_surcharge;
286
+		}
287
+		global $wpdb;
288
+		$cols_n_values = array(
289
+			'PRT_ID' => $price_type,
290
+			'PRC_amount' => $amount,
291
+			'PRC_name' =>  $org_options['surcharge_text'],
292
+			'PRC_is_default' => true,
293
+			'PRC_overrides' => false,
294
+			'PRC_order' => 100,
295
+			'PRC_deleted' => false,
296
+			'PRC_parent' => null
297 297
 
298
-        );
299
-        $datatypes = array(
300
-            '%d',// PRT_ID
301
-            '%f',// PRT_amount
302
-            '%s',// PRC_name
303
-            '%d',// PRC_is_default
304
-            '%d',// PRC_overrides
305
-            '%d',// PRC_order
306
-            '%d',// PRC_deleted
307
-            '%d',// PRC_parent
308
-        );
309
-        $price_table = $wpdb->prefix . "esp_price";
310
-        $success = $wpdb->insert($price_table, $cols_n_values, $datatypes);
311
-        if (! $success) {
312
-            $this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion(
313
-                'org_options',
314
-                array(
315
-                        'surcharge' => $org_options['surcharge'],
316
-                        'surcharge_type' => $org_options['surcharge_type'],
317
-                        'surcharge_text' => $org_options['surcharge_text']),
318
-                $price_table,
319
-                $cols_n_values,
320
-                $datatypes
321
-            ));
322
-            return 0;
323
-        }
324
-        $new_id = $wpdb->insert_id;
325
-        return $new_id;
326
-    }
298
+		);
299
+		$datatypes = array(
300
+			'%d',// PRT_ID
301
+			'%f',// PRT_amount
302
+			'%s',// PRC_name
303
+			'%d',// PRC_is_default
304
+			'%d',// PRC_overrides
305
+			'%d',// PRC_order
306
+			'%d',// PRC_deleted
307
+			'%d',// PRC_parent
308
+		);
309
+		$price_table = $wpdb->prefix . "esp_price";
310
+		$success = $wpdb->insert($price_table, $cols_n_values, $datatypes);
311
+		if (! $success) {
312
+			$this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion(
313
+				'org_options',
314
+				array(
315
+						'surcharge' => $org_options['surcharge'],
316
+						'surcharge_type' => $org_options['surcharge_type'],
317
+						'surcharge_text' => $org_options['surcharge_text']),
318
+				$price_table,
319
+				$cols_n_values,
320
+				$datatypes
321
+			));
322
+			return 0;
323
+		}
324
+		$new_id = $wpdb->insert_id;
325
+		return $new_id;
326
+	}
327 327
 
328
-    protected $_org_options_we_know_how_to_migrate = array(
329
-      'organization',
330
-      'organization_street1',
331
-      'organization_street2',
332
-      'organization_city',
333
-      'organization_state',
334
-      'organization_zip',
335
-      'contact_email',
336
-      'default_mail',
337
-      'payment_subject',
338
-      'payment_message',
339
-      'message',
340
-      'default_payment_status',
341
-      'surcharge',// unused?
342
-      'country_id',// unused?
343
-      'organization_country',
328
+	protected $_org_options_we_know_how_to_migrate = array(
329
+	  'organization',
330
+	  'organization_street1',
331
+	  'organization_street2',
332
+	  'organization_city',
333
+	  'organization_state',
334
+	  'organization_zip',
335
+	  'contact_email',
336
+	  'default_mail',
337
+	  'payment_subject',
338
+	  'payment_message',
339
+	  'message',
340
+	  'default_payment_status',
341
+	  'surcharge',// unused?
342
+	  'country_id',// unused?
343
+	  'organization_country',
344 344
 //    'currency_symbol',
345
-      'expire_on_registration_end',
346
-      'email_before_payment',
347
-      'email_fancy_headers',
348
-      'enable_default_style',
349
-      'event_ssl_active',
350
-      'selected_style',
351
-      'show_pending_payment_options',
352
-      'show_reg_footer',
353
-      'skip_confirmation_page',
354
-      'allow_mer_discounts',// no equiv
355
-      'allow_mer_vouchers',// no equiv
356
-      'display_short_description_in_event_list',
357
-      'display_description_on_multi_reg_page',
358
-      'display_address_in_event_list',
359
-      'display_address_in_regform',
360
-      'use_custom_post_types',// no equiv
361
-      'display_ical_download',
362
-      'display_featured_image',
363
-      'themeroller',
364
-      'default_logo_url',
365
-      'event_page_id',
366
-      'return_url',
367
-      'cancel_return',
368
-      'notify_url',
369
-      'events_in_dashboard',
370
-      'use_captcha',
371
-      'recaptcha_publickey',
372
-      'recaptcha_privatekey',
373
-      'recaptcha_theme',
374
-      'recaptcha_width',
375
-      'recaptcha_language',
376
-      'espresso_dashboard_widget',
377
-      'time_reg_limit',
345
+	  'expire_on_registration_end',
346
+	  'email_before_payment',
347
+	  'email_fancy_headers',
348
+	  'enable_default_style',
349
+	  'event_ssl_active',
350
+	  'selected_style',
351
+	  'show_pending_payment_options',
352
+	  'show_reg_footer',
353
+	  'skip_confirmation_page',
354
+	  'allow_mer_discounts',// no equiv
355
+	  'allow_mer_vouchers',// no equiv
356
+	  'display_short_description_in_event_list',
357
+	  'display_description_on_multi_reg_page',
358
+	  'display_address_in_event_list',
359
+	  'display_address_in_regform',
360
+	  'use_custom_post_types',// no equiv
361
+	  'display_ical_download',
362
+	  'display_featured_image',
363
+	  'themeroller',
364
+	  'default_logo_url',
365
+	  'event_page_id',
366
+	  'return_url',
367
+	  'cancel_return',
368
+	  'notify_url',
369
+	  'events_in_dashboard',
370
+	  'use_captcha',
371
+	  'recaptcha_publickey',
372
+	  'recaptcha_privatekey',
373
+	  'recaptcha_theme',
374
+	  'recaptcha_width',
375
+	  'recaptcha_language',
376
+	  'espresso_dashboard_widget',
377
+	  'time_reg_limit',
378 378
 //    'use_attendee_pre_approval', removed in 4.1- instead this is factored into the default reg status
379
-      'use_personnel_manager',// no equiv
380
-      'use_event_timezones',
381
-      'full_logging',
382
-      'surcharge_type',// unused
383
-      'surcharge_text',// unused
384
-      'affiliate_id',
385
-      'site_license_key',
386
-    );
379
+	  'use_personnel_manager',// no equiv
380
+	  'use_event_timezones',
381
+	  'full_logging',
382
+	  'surcharge_type',// unused
383
+	  'surcharge_text',// unused
384
+	  'affiliate_id',
385
+	  'site_license_key',
386
+	);
387 387
 }
Please login to merge, or discard this patch.
4_1_0_stages/EE_DMS_4_1_0_question_groups.dmsstage.php 2 patches
Indentation   +111 added lines, -111 removed lines patch added patch discarded remove patch
@@ -40,119 +40,119 @@
 block discarded – undo
40 40
  */
41 41
 class EE_DMS_4_1_0_question_groups extends EE_Data_Migration_Script_Stage
42 42
 {
43
-    private $_old_table;
44
-    private $_new_table;
45
-    /**
46
-     * Keeps track of whether or not we've already added a system question group,
47
-     * in order to avoid adding more than 1 (basically, in 3.1 this would happen
48
-     * with the Roles & Permissions addon, because each user had their own set of
49
-     * question groups and questions),
50
-     * @var boolean
51
-     */
52
-    private $_already_got_system_question_group_1 = false;
53
-    public function _migration_step($num_items = 50)
54
-    {
43
+	private $_old_table;
44
+	private $_new_table;
45
+	/**
46
+	 * Keeps track of whether or not we've already added a system question group,
47
+	 * in order to avoid adding more than 1 (basically, in 3.1 this would happen
48
+	 * with the Roles & Permissions addon, because each user had their own set of
49
+	 * question groups and questions),
50
+	 * @var boolean
51
+	 */
52
+	private $_already_got_system_question_group_1 = false;
53
+	public function _migration_step($num_items = 50)
54
+	{
55 55
 
56
-        global $wpdb;
57
-        $start_at_record = $this->count_records_migrated();
58
-        $rows = $wpdb->get_results($wpdb->prepare("SELECT * FROM $this->_old_table LIMIT %d,%d", $start_at_record, $num_items), ARRAY_A);
59
-        $items_actually_migrated = 0;
60
-        foreach ($rows as $question_group) {
61
-            $new_id = $this->_insert_new_question_group($question_group);
56
+		global $wpdb;
57
+		$start_at_record = $this->count_records_migrated();
58
+		$rows = $wpdb->get_results($wpdb->prepare("SELECT * FROM $this->_old_table LIMIT %d,%d", $start_at_record, $num_items), ARRAY_A);
59
+		$items_actually_migrated = 0;
60
+		foreach ($rows as $question_group) {
61
+			$new_id = $this->_insert_new_question_group($question_group);
62 62
 
63
-            $this->get_migration_script()->set_mapping($this->_old_table, $question_group['id'], $this->_new_table, $new_id);
64
-            $items_actually_migrated++;
65
-        }
66
-        if ($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate()) {
67
-            $this->set_completed();
68
-        }
69
-        return $items_actually_migrated;
70
-    }
71
-    public function _count_records_to_migrate()
72
-    {
73
-        global $wpdb;
74
-        $count = $wpdb->get_var("SELECT COUNT(id) FROM " . $this->_old_table);
75
-        return $count;
76
-    }
77
-    public function __construct()
78
-    {
79
-        global $wpdb;
80
-        $this->_old_table = $wpdb->prefix . "events_qst_group";
81
-        $this->_new_table = $wpdb->prefix . "esp_question_group";
82
-        $this->_pretty_name = esc_html__("Question Groups", "event_espresso");
83
-        parent::__construct();
84
-    }
63
+			$this->get_migration_script()->set_mapping($this->_old_table, $question_group['id'], $this->_new_table, $new_id);
64
+			$items_actually_migrated++;
65
+		}
66
+		if ($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate()) {
67
+			$this->set_completed();
68
+		}
69
+		return $items_actually_migrated;
70
+	}
71
+	public function _count_records_to_migrate()
72
+	{
73
+		global $wpdb;
74
+		$count = $wpdb->get_var("SELECT COUNT(id) FROM " . $this->_old_table);
75
+		return $count;
76
+	}
77
+	public function __construct()
78
+	{
79
+		global $wpdb;
80
+		$this->_old_table = $wpdb->prefix . "events_qst_group";
81
+		$this->_new_table = $wpdb->prefix . "esp_question_group";
82
+		$this->_pretty_name = esc_html__("Question Groups", "event_espresso");
83
+		parent::__construct();
84
+	}
85 85
 
86
-    /**
87
-     * Attempts to insert a new question group inthe new format given an old one
88
-     * @global type $wpdb
89
-     * @param array $old_question_group
90
-     * @return int
91
-     */
92
-    private function _insert_new_question_group($old_question_group)
93
-    {
94
-        global $wpdb;
95
-        // try to guess what the QST_system int should be... finding the Personal info system
96
-        // question group is quite easy. But in 3.1 address info WASN'T a system group, it just exitsed by default but
97
-        // could be easily removed.
98
-        if ($old_question_group['system_group'] && ! $this->_already_got_system_question_group_1()) {
99
-            $guess_at_system_number = 1;
100
-        } elseif ($old_question_group['id'] == '2' && strpos($old_question_group['group_name'], 'Address') !== false) {
101
-            $guess_at_system_number = 2;
102
-        } else {
103
-            $guess_at_system_number = 0;
104
-        }
105
-        // if the question group wasn't made by the normal admin,
106
-        // we'd like to keep track of who made it
107
-        if (intval($old_question_group['wp_user']) != 1) {
108
-            $username = $wpdb->get_var($wpdb->prepare("SELECT user_nicename FROM " . $wpdb->users . " WHERE ID = %d", $old_question_group['wp_user']));
109
-            $identifier = $old_question_group['group_identifier'] . "-by-" . $username;
110
-        } else {
111
-            $identifier = $old_question_group['group_identifier'];
112
-        }
113
-        $cols_n_values = array(
114
-            'QSG_name' => stripslashes($old_question_group['group_name']),
115
-            'QSG_identifier' => $identifier,
116
-            'QSG_desc' => stripslashes($old_question_group['group_description']),
117
-            'QSG_order' => $old_question_group['group_order'],
118
-            'QSG_show_group_name' => $old_question_group['show_group_name'],
119
-            'QSG_show_group_desc' => $old_question_group['show_group_description'],
120
-            'QSG_system' => $guess_at_system_number,
121
-            'QSG_deleted' => false
122
-        );
123
-        $datatypes = array(
124
-            '%s',// QSG_name
125
-            '%s',// QSG_identifier
126
-            '%s',// QSG_desc
127
-            '%d',// QSG_order
128
-            '%d',// QSG_show_group_name
129
-            '%d',// QSG_show_group_desc
130
-            '%d',// QSG_system
131
-            '%d',// QSG_deleted
132
-        );
133
-        $success = $wpdb->insert($this->_new_table, $cols_n_values, $datatypes);
134
-        if (! $success) {
135
-            $this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, $old_question_group, $this->_new_table, $cols_n_values, $datatypes));
136
-            return 0;
137
-        }
138
-        return $wpdb->insert_id;
139
-    }
86
+	/**
87
+	 * Attempts to insert a new question group inthe new format given an old one
88
+	 * @global type $wpdb
89
+	 * @param array $old_question_group
90
+	 * @return int
91
+	 */
92
+	private function _insert_new_question_group($old_question_group)
93
+	{
94
+		global $wpdb;
95
+		// try to guess what the QST_system int should be... finding the Personal info system
96
+		// question group is quite easy. But in 3.1 address info WASN'T a system group, it just exitsed by default but
97
+		// could be easily removed.
98
+		if ($old_question_group['system_group'] && ! $this->_already_got_system_question_group_1()) {
99
+			$guess_at_system_number = 1;
100
+		} elseif ($old_question_group['id'] == '2' && strpos($old_question_group['group_name'], 'Address') !== false) {
101
+			$guess_at_system_number = 2;
102
+		} else {
103
+			$guess_at_system_number = 0;
104
+		}
105
+		// if the question group wasn't made by the normal admin,
106
+		// we'd like to keep track of who made it
107
+		if (intval($old_question_group['wp_user']) != 1) {
108
+			$username = $wpdb->get_var($wpdb->prepare("SELECT user_nicename FROM " . $wpdb->users . " WHERE ID = %d", $old_question_group['wp_user']));
109
+			$identifier = $old_question_group['group_identifier'] . "-by-" . $username;
110
+		} else {
111
+			$identifier = $old_question_group['group_identifier'];
112
+		}
113
+		$cols_n_values = array(
114
+			'QSG_name' => stripslashes($old_question_group['group_name']),
115
+			'QSG_identifier' => $identifier,
116
+			'QSG_desc' => stripslashes($old_question_group['group_description']),
117
+			'QSG_order' => $old_question_group['group_order'],
118
+			'QSG_show_group_name' => $old_question_group['show_group_name'],
119
+			'QSG_show_group_desc' => $old_question_group['show_group_description'],
120
+			'QSG_system' => $guess_at_system_number,
121
+			'QSG_deleted' => false
122
+		);
123
+		$datatypes = array(
124
+			'%s',// QSG_name
125
+			'%s',// QSG_identifier
126
+			'%s',// QSG_desc
127
+			'%d',// QSG_order
128
+			'%d',// QSG_show_group_name
129
+			'%d',// QSG_show_group_desc
130
+			'%d',// QSG_system
131
+			'%d',// QSG_deleted
132
+		);
133
+		$success = $wpdb->insert($this->_new_table, $cols_n_values, $datatypes);
134
+		if (! $success) {
135
+			$this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, $old_question_group, $this->_new_table, $cols_n_values, $datatypes));
136
+			return 0;
137
+		}
138
+		return $wpdb->insert_id;
139
+	}
140 140
 
141
-    /**
142
-     * Checks if we've already added a system question 1 to the new question groups table
143
-     * @global type $wpdb
144
-     * @return boolean
145
-     */
146
-    private function _already_got_system_question_group_1()
147
-    {
148
-        if (! $this->_already_got_system_question_group_1) {
149
-            // check the db
150
-            global $wpdb;
151
-            $exists = $wpdb->get_var("SELECT COUNT(*) FROM {$this->_new_table} WHERE QSG_system=1");
152
-            if (intval($exists) > 0) {
153
-                $this->_already_got_system_question_group_1 = true;
154
-            }
155
-        }
156
-        return $this->_already_got_system_question_group_1;
157
-    }
141
+	/**
142
+	 * Checks if we've already added a system question 1 to the new question groups table
143
+	 * @global type $wpdb
144
+	 * @return boolean
145
+	 */
146
+	private function _already_got_system_question_group_1()
147
+	{
148
+		if (! $this->_already_got_system_question_group_1) {
149
+			// check the db
150
+			global $wpdb;
151
+			$exists = $wpdb->get_var("SELECT COUNT(*) FROM {$this->_new_table} WHERE QSG_system=1");
152
+			if (intval($exists) > 0) {
153
+				$this->_already_got_system_question_group_1 = true;
154
+			}
155
+		}
156
+		return $this->_already_got_system_question_group_1;
157
+	}
158 158
 }
Please login to merge, or discard this patch.
Spacing   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -71,14 +71,14 @@  discard block
 block discarded – undo
71 71
     public function _count_records_to_migrate()
72 72
     {
73 73
         global $wpdb;
74
-        $count = $wpdb->get_var("SELECT COUNT(id) FROM " . $this->_old_table);
74
+        $count = $wpdb->get_var("SELECT COUNT(id) FROM ".$this->_old_table);
75 75
         return $count;
76 76
     }
77 77
     public function __construct()
78 78
     {
79 79
         global $wpdb;
80
-        $this->_old_table = $wpdb->prefix . "events_qst_group";
81
-        $this->_new_table = $wpdb->prefix . "esp_question_group";
80
+        $this->_old_table = $wpdb->prefix."events_qst_group";
81
+        $this->_new_table = $wpdb->prefix."esp_question_group";
82 82
         $this->_pretty_name = esc_html__("Question Groups", "event_espresso");
83 83
         parent::__construct();
84 84
     }
@@ -105,8 +105,8 @@  discard block
 block discarded – undo
105 105
         // if the question group wasn't made by the normal admin,
106 106
         // we'd like to keep track of who made it
107 107
         if (intval($old_question_group['wp_user']) != 1) {
108
-            $username = $wpdb->get_var($wpdb->prepare("SELECT user_nicename FROM " . $wpdb->users . " WHERE ID = %d", $old_question_group['wp_user']));
109
-            $identifier = $old_question_group['group_identifier'] . "-by-" . $username;
108
+            $username = $wpdb->get_var($wpdb->prepare("SELECT user_nicename FROM ".$wpdb->users." WHERE ID = %d", $old_question_group['wp_user']));
109
+            $identifier = $old_question_group['group_identifier']."-by-".$username;
110 110
         } else {
111 111
             $identifier = $old_question_group['group_identifier'];
112 112
         }
@@ -121,17 +121,17 @@  discard block
 block discarded – undo
121 121
             'QSG_deleted' => false
122 122
         );
123 123
         $datatypes = array(
124
-            '%s',// QSG_name
125
-            '%s',// QSG_identifier
126
-            '%s',// QSG_desc
127
-            '%d',// QSG_order
128
-            '%d',// QSG_show_group_name
129
-            '%d',// QSG_show_group_desc
130
-            '%d',// QSG_system
131
-            '%d',// QSG_deleted
124
+            '%s', // QSG_name
125
+            '%s', // QSG_identifier
126
+            '%s', // QSG_desc
127
+            '%d', // QSG_order
128
+            '%d', // QSG_show_group_name
129
+            '%d', // QSG_show_group_desc
130
+            '%d', // QSG_system
131
+            '%d', // QSG_deleted
132 132
         );
133 133
         $success = $wpdb->insert($this->_new_table, $cols_n_values, $datatypes);
134
-        if (! $success) {
134
+        if ( ! $success) {
135 135
             $this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, $old_question_group, $this->_new_table, $cols_n_values, $datatypes));
136 136
             return 0;
137 137
         }
@@ -145,7 +145,7 @@  discard block
 block discarded – undo
145 145
      */
146 146
     private function _already_got_system_question_group_1()
147 147
     {
148
-        if (! $this->_already_got_system_question_group_1) {
148
+        if ( ! $this->_already_got_system_question_group_1) {
149 149
             // check the db
150 150
             global $wpdb;
151 151
             $exists = $wpdb->get_var("SELECT COUNT(*) FROM {$this->_new_table} WHERE QSG_system=1");
Please login to merge, or discard this patch.
data_migration_scripts/4_1_0_stages/EE_DMS_4_1_0_questions.dmsstage.php 2 patches
Indentation   +114 added lines, -114 removed lines patch added patch discarded remove patch
@@ -57,119 +57,119 @@
 block discarded – undo
57 57
  */
58 58
 class EE_DMS_4_1_0_questions extends EE_Data_Migration_Script_Stage
59 59
 {
60
-    private $_old_table;
61
-    private $_new_table;
62
-    private $_option_table;
63
-    public function _migration_step($num_items = 50)
64
-    {
65
-        global $wpdb;
66
-        $start_at_record = $this->count_records_migrated();
67
-        $rows = $wpdb->get_results($wpdb->prepare("SELECT * FROM $this->_old_table LIMIT %d,%d", $start_at_record, $num_items), ARRAY_A);
68
-        $items_actually_migrated = 0;
69
-        foreach ($rows as $question_row) {
70
-            $new_id = $this->_insert_new_question($question_row);
71
-            $this->get_migration_script()->set_mapping($this->_old_table, $question_row['id'], $this->_new_table, $new_id);
72
-            $items_actually_migrated++;
73
-        }
74
-        if ($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate()) {
75
-            $this->set_completed();
76
-        }
77
-        return $items_actually_migrated;
78
-    }
79
-    public function _count_records_to_migrate()
80
-    {
81
-        global $wpdb;
82
-        $count = $wpdb->get_var("SELECT COUNT(id) FROM " . $this->_old_table);
83
-        return $count;
84
-    }
85
-    public function __construct()
86
-    {
87
-        global $wpdb;
88
-        $this->_pretty_name = esc_html__("Questions", "event_espresso");
89
-        $this->_old_table = $wpdb->prefix . "events_question";
90
-        $this->_new_table = $wpdb->prefix . "esp_question";
91
-        $this->_option_table = $wpdb->prefix . "esp_question_option";
92
-        parent::__construct();
93
-    }
94
-    private function _insert_new_question($old_question)
95
-    {
96
-        global $wpdb;
97
-        // if this pretends to be a 'system' question, check if we already have a
98
-        // system question for that string. If so, pretend THAT new question
99
-        // is what we just isnerted
100
-        if ($old_question['system_name']) {
101
-            $id_of_new_system_question = intval($wpdb->get_var($wpdb->prepare("SELECT QST_ID FROM {$this->_new_table} WHERE QST_system = %s", $old_question['system_name'])));
102
-            if ($id_of_new_system_question) {
103
-                return $id_of_new_system_question;
104
-            }
105
-            // ok so this must be the first one. Carry on.
106
-        }
60
+	private $_old_table;
61
+	private $_new_table;
62
+	private $_option_table;
63
+	public function _migration_step($num_items = 50)
64
+	{
65
+		global $wpdb;
66
+		$start_at_record = $this->count_records_migrated();
67
+		$rows = $wpdb->get_results($wpdb->prepare("SELECT * FROM $this->_old_table LIMIT %d,%d", $start_at_record, $num_items), ARRAY_A);
68
+		$items_actually_migrated = 0;
69
+		foreach ($rows as $question_row) {
70
+			$new_id = $this->_insert_new_question($question_row);
71
+			$this->get_migration_script()->set_mapping($this->_old_table, $question_row['id'], $this->_new_table, $new_id);
72
+			$items_actually_migrated++;
73
+		}
74
+		if ($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate()) {
75
+			$this->set_completed();
76
+		}
77
+		return $items_actually_migrated;
78
+	}
79
+	public function _count_records_to_migrate()
80
+	{
81
+		global $wpdb;
82
+		$count = $wpdb->get_var("SELECT COUNT(id) FROM " . $this->_old_table);
83
+		return $count;
84
+	}
85
+	public function __construct()
86
+	{
87
+		global $wpdb;
88
+		$this->_pretty_name = esc_html__("Questions", "event_espresso");
89
+		$this->_old_table = $wpdb->prefix . "events_question";
90
+		$this->_new_table = $wpdb->prefix . "esp_question";
91
+		$this->_option_table = $wpdb->prefix . "esp_question_option";
92
+		parent::__construct();
93
+	}
94
+	private function _insert_new_question($old_question)
95
+	{
96
+		global $wpdb;
97
+		// if this pretends to be a 'system' question, check if we already have a
98
+		// system question for that string. If so, pretend THAT new question
99
+		// is what we just isnerted
100
+		if ($old_question['system_name']) {
101
+			$id_of_new_system_question = intval($wpdb->get_var($wpdb->prepare("SELECT QST_ID FROM {$this->_new_table} WHERE QST_system = %s", $old_question['system_name'])));
102
+			if ($id_of_new_system_question) {
103
+				return $id_of_new_system_question;
104
+			}
105
+			// ok so this must be the first one. Carry on.
106
+		}
107 107
 
108
-        $cols_n_values = array(
109
-            'QST_display_text' => stripslashes($old_question['question']),
110
-            'QST_admin_label' => $old_question['system_name'] ? $old_question['system_name'] : sanitize_title($old_question['question']),
111
-            'QST_system' => (string) $old_question['system_name'],
112
-            'QST_type' => $old_question['question_type'],
113
-            'QST_required' => 'Y' == $old_question['required'],
114
-            'QST_required_text' => stripslashes($old_question['required_text']),
115
-            'QST_order' => $old_question['sequence'],
116
-            'QST_admin_only' => 'Y' == $old_question['admin_only'],
117
-            'QST_wp_user' => $old_question['wp_user'],
118
-            'QST_deleted' => false
119
-        );
120
-        $datatypes = array(
121
-            '%s',// QST_display_text
122
-            '%s',// QST-admin_label
123
-            '%s',// QST_system
124
-            '%s',// QST_type
125
-            '%d',// QST_required
126
-            '%s',// QST_required_text
127
-            '%d',// QST_order
128
-            '%d',// QST_admin_only
129
-            '%d',// QST_wp_user
130
-            '%d',// QST_deleted
131
-        );
132
-        $success = $wpdb->insert($this->_new_table, $cols_n_values, $datatypes);
133
-        if (! $success) {
134
-            $this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, $old_question, $this->_new_table, $cols_n_values, $datatypes));
135
-            return 0;
136
-        }
137
-        $new_id = $wpdb->insert_id;
138
-        // now take care of posisbly adding question options
139
-        if (in_array($old_question['question_type'], array('DROPDOWN','SINGLE','MULTIPLE'))) {
140
-            $options = explode(",", $old_question['response']);
141
-            foreach ($options as $option) {
142
-                $this->_insert_question_option($option, $new_id);
143
-            }
144
-        }
145
-        return $new_id;
146
-    }
147
-    /**
148
-     * Adds a question option to the db
149
-     * @global type $wpdb
150
-     * @param string $option
151
-     * @param int $question_id
152
-     * @return int
153
-     */
154
-    private function _insert_question_option($option, $question_id)
155
-    {
156
-        $option = trim($option);
157
-        global $wpdb;
158
-        $cols_n_values = array(
159
-            'QST_ID' => $question_id,
160
-            'QSO_value' => $option,
161
-            'QSO_deleted' => false
162
-        );
163
-        $datatypes = array(
164
-            '%d',// QST_ID
165
-            '%s',// QSO_value
166
-            '%d',// QSO_deleted
167
-        );
168
-        $success = $wpdb->insert($this->_option_table, $cols_n_values, $datatypes);
169
-        if (! $success) {
170
-            $this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, array('option' => $option,'new_question_id' => $question_id), $this->_option_table, $cols_n_values, $datatypes));
171
-            return 0;
172
-        }
173
-        return $wpdb->insert_id;
174
-    }
108
+		$cols_n_values = array(
109
+			'QST_display_text' => stripslashes($old_question['question']),
110
+			'QST_admin_label' => $old_question['system_name'] ? $old_question['system_name'] : sanitize_title($old_question['question']),
111
+			'QST_system' => (string) $old_question['system_name'],
112
+			'QST_type' => $old_question['question_type'],
113
+			'QST_required' => 'Y' == $old_question['required'],
114
+			'QST_required_text' => stripslashes($old_question['required_text']),
115
+			'QST_order' => $old_question['sequence'],
116
+			'QST_admin_only' => 'Y' == $old_question['admin_only'],
117
+			'QST_wp_user' => $old_question['wp_user'],
118
+			'QST_deleted' => false
119
+		);
120
+		$datatypes = array(
121
+			'%s',// QST_display_text
122
+			'%s',// QST-admin_label
123
+			'%s',// QST_system
124
+			'%s',// QST_type
125
+			'%d',// QST_required
126
+			'%s',// QST_required_text
127
+			'%d',// QST_order
128
+			'%d',// QST_admin_only
129
+			'%d',// QST_wp_user
130
+			'%d',// QST_deleted
131
+		);
132
+		$success = $wpdb->insert($this->_new_table, $cols_n_values, $datatypes);
133
+		if (! $success) {
134
+			$this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, $old_question, $this->_new_table, $cols_n_values, $datatypes));
135
+			return 0;
136
+		}
137
+		$new_id = $wpdb->insert_id;
138
+		// now take care of posisbly adding question options
139
+		if (in_array($old_question['question_type'], array('DROPDOWN','SINGLE','MULTIPLE'))) {
140
+			$options = explode(",", $old_question['response']);
141
+			foreach ($options as $option) {
142
+				$this->_insert_question_option($option, $new_id);
143
+			}
144
+		}
145
+		return $new_id;
146
+	}
147
+	/**
148
+	 * Adds a question option to the db
149
+	 * @global type $wpdb
150
+	 * @param string $option
151
+	 * @param int $question_id
152
+	 * @return int
153
+	 */
154
+	private function _insert_question_option($option, $question_id)
155
+	{
156
+		$option = trim($option);
157
+		global $wpdb;
158
+		$cols_n_values = array(
159
+			'QST_ID' => $question_id,
160
+			'QSO_value' => $option,
161
+			'QSO_deleted' => false
162
+		);
163
+		$datatypes = array(
164
+			'%d',// QST_ID
165
+			'%s',// QSO_value
166
+			'%d',// QSO_deleted
167
+		);
168
+		$success = $wpdb->insert($this->_option_table, $cols_n_values, $datatypes);
169
+		if (! $success) {
170
+			$this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, array('option' => $option,'new_question_id' => $question_id), $this->_option_table, $cols_n_values, $datatypes));
171
+			return 0;
172
+		}
173
+		return $wpdb->insert_id;
174
+	}
175 175
 }
Please login to merge, or discard this patch.
Spacing   +21 added lines, -21 removed lines patch added patch discarded remove patch
@@ -79,16 +79,16 @@  discard block
 block discarded – undo
79 79
     public function _count_records_to_migrate()
80 80
     {
81 81
         global $wpdb;
82
-        $count = $wpdb->get_var("SELECT COUNT(id) FROM " . $this->_old_table);
82
+        $count = $wpdb->get_var("SELECT COUNT(id) FROM ".$this->_old_table);
83 83
         return $count;
84 84
     }
85 85
     public function __construct()
86 86
     {
87 87
         global $wpdb;
88 88
         $this->_pretty_name = esc_html__("Questions", "event_espresso");
89
-        $this->_old_table = $wpdb->prefix . "events_question";
90
-        $this->_new_table = $wpdb->prefix . "esp_question";
91
-        $this->_option_table = $wpdb->prefix . "esp_question_option";
89
+        $this->_old_table = $wpdb->prefix."events_question";
90
+        $this->_new_table = $wpdb->prefix."esp_question";
91
+        $this->_option_table = $wpdb->prefix."esp_question_option";
92 92
         parent::__construct();
93 93
     }
94 94
     private function _insert_new_question($old_question)
@@ -118,25 +118,25 @@  discard block
 block discarded – undo
118 118
             'QST_deleted' => false
119 119
         );
120 120
         $datatypes = array(
121
-            '%s',// QST_display_text
122
-            '%s',// QST-admin_label
123
-            '%s',// QST_system
124
-            '%s',// QST_type
125
-            '%d',// QST_required
126
-            '%s',// QST_required_text
127
-            '%d',// QST_order
128
-            '%d',// QST_admin_only
129
-            '%d',// QST_wp_user
130
-            '%d',// QST_deleted
121
+            '%s', // QST_display_text
122
+            '%s', // QST-admin_label
123
+            '%s', // QST_system
124
+            '%s', // QST_type
125
+            '%d', // QST_required
126
+            '%s', // QST_required_text
127
+            '%d', // QST_order
128
+            '%d', // QST_admin_only
129
+            '%d', // QST_wp_user
130
+            '%d', // QST_deleted
131 131
         );
132 132
         $success = $wpdb->insert($this->_new_table, $cols_n_values, $datatypes);
133
-        if (! $success) {
133
+        if ( ! $success) {
134 134
             $this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, $old_question, $this->_new_table, $cols_n_values, $datatypes));
135 135
             return 0;
136 136
         }
137 137
         $new_id = $wpdb->insert_id;
138 138
         // now take care of posisbly adding question options
139
-        if (in_array($old_question['question_type'], array('DROPDOWN','SINGLE','MULTIPLE'))) {
139
+        if (in_array($old_question['question_type'], array('DROPDOWN', 'SINGLE', 'MULTIPLE'))) {
140 140
             $options = explode(",", $old_question['response']);
141 141
             foreach ($options as $option) {
142 142
                 $this->_insert_question_option($option, $new_id);
@@ -161,13 +161,13 @@  discard block
 block discarded – undo
161 161
             'QSO_deleted' => false
162 162
         );
163 163
         $datatypes = array(
164
-            '%d',// QST_ID
165
-            '%s',// QSO_value
166
-            '%d',// QSO_deleted
164
+            '%d', // QST_ID
165
+            '%s', // QSO_value
166
+            '%d', // QSO_deleted
167 167
         );
168 168
         $success = $wpdb->insert($this->_option_table, $cols_n_values, $datatypes);
169
-        if (! $success) {
170
-            $this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, array('option' => $option,'new_question_id' => $question_id), $this->_option_table, $cols_n_values, $datatypes));
169
+        if ( ! $success) {
170
+            $this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, array('option' => $option, 'new_question_id' => $question_id), $this->_option_table, $cols_n_values, $datatypes));
171 171
             return 0;
172 172
         }
173 173
         return $wpdb->insert_id;
Please login to merge, or discard this patch.
4_7_0_stages/EE_DMS_4_7_0_Registration_Payments.dmsstage.php 2 patches
Spacing   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -24,10 +24,10 @@  discard block
 block discarded – undo
24 24
         global $wpdb;
25 25
         $this->_pretty_name = esc_html__('Registration Payment Record Generation', 'event_espresso');
26 26
         // define tables
27
-        $this->_old_table                                   = $wpdb->prefix . 'esp_transaction';
28
-        $this->_payment_table                       = $wpdb->prefix . 'esp_payment';
29
-        $this->_registration_table                  = $wpdb->prefix . 'esp_registration';
30
-        $this->_registration_payment_table  = $wpdb->prefix . 'esp_registration_payment';
27
+        $this->_old_table = $wpdb->prefix.'esp_transaction';
28
+        $this->_payment_table                       = $wpdb->prefix.'esp_payment';
29
+        $this->_registration_table                  = $wpdb->prefix.'esp_registration';
30
+        $this->_registration_payment_table = $wpdb->prefix.'esp_registration_payment';
31 31
         // build SQL WHERE clauses
32 32
         $this->_extra_where_sql = "WHERE STS_ID IN ( 'TIN', 'TCM' ) AND TXN_Total != '0.000'";
33 33
         parent::__construct();
@@ -44,7 +44,7 @@  discard block
 block discarded – undo
44 44
         /** @type WPDB $wpdb */
45 45
         global $wpdb;
46 46
         $TXN_ID = absint($transaction['TXN_ID']);
47
-        if (! $TXN_ID) {
47
+        if ( ! $TXN_ID) {
48 48
             $this->add_error(
49 49
                 sprintf(
50 50
                     esc_html__('Invalid transaction with ID=%1$d. Error: "%2$s"', 'event_espresso'),
@@ -136,12 +136,12 @@  discard block
 block discarded – undo
136 136
         $available_payment_amount = $payment->PAY_amount;
137 137
         foreach ($registrations as $REG_ID => $registration) {
138 138
             // nothing left, then we are done here?
139
-            if (! $available_payment_amount > 0) {
139
+            if ( ! $available_payment_amount > 0) {
140 140
                 break;
141 141
             }
142 142
             // ensure REG_final_price has a valid value, and skip if it turns out to be zero
143 143
             $registration->REG_final_price = ! empty($registration->REG_final_price) ? (float) $registration->REG_final_price : 0.00;
144
-            if (! $registration->REG_final_price > 0) {
144
+            if ( ! $registration->REG_final_price > 0) {
145 145
                 continue;
146 146
             }
147 147
             // because REG_paid may have been updated by a previous payment, we need to retrieve the value from the db
@@ -153,7 +153,7 @@  discard block
 block discarded – undo
153 153
             $registration->REG_paid = ! empty($registration->REG_paid) ? (float) $registration->REG_paid : 0.00;
154 154
             // calculate amount owing, and skip if it turns out to be zero
155 155
             $owing = $registration->REG_final_price - $registration->REG_paid;
156
-            if (! $owing > 0) {
156
+            if ( ! $owing > 0) {
157 157
                 continue;
158 158
             }
159 159
             // don't allow payment amount to exceed the available payment amount, OR the amount owing
@@ -185,8 +185,8 @@  discard block
 block discarded – undo
185 185
         global $wpdb;
186 186
         $success = $wpdb->insert(
187 187
             $this->_registration_payment_table,
188
-            array( 'REG_ID' => $REG_ID, 'PAY_ID' => $PAY_ID, 'RPY_amount' => $PAY_amount, ),  // data
189
-            array( '%f' )   // data format
188
+            array('REG_ID' => $REG_ID, 'PAY_ID' => $PAY_ID, 'RPY_amount' => $PAY_amount,), // data
189
+            array('%f')   // data format
190 190
         );
191 191
         if ($success === false) {
192 192
             $this->add_error(
@@ -216,10 +216,10 @@  discard block
 block discarded – undo
216 216
         global $wpdb;
217 217
         $success = $wpdb->update(
218 218
             $this->_registration_table,
219
-            array( 'REG_paid' => $REG_paid ),  // data
220
-            array( 'REG_ID' => $REG_ID ),  // where
221
-            array( '%f' ),   // data format
222
-            array( '%d' )  // where format
219
+            array('REG_paid' => $REG_paid), // data
220
+            array('REG_ID' => $REG_ID), // where
221
+            array('%f'), // data format
222
+            array('%d')  // where format
223 223
         );
224 224
         if ($success === false) {
225 225
             $this->add_error(
Please login to merge, or discard this patch.
Indentation   +221 added lines, -221 removed lines patch added patch discarded remove patch
@@ -11,225 +11,225 @@
 block discarded – undo
11 11
  */
12 12
 class EE_DMS_4_7_0_Registration_Payments extends EE_Data_Migration_Script_Stage_Table
13 13
 {
14
-    protected $_payment_table;
15
-
16
-    protected $_registration_table;
17
-
18
-    protected $_registration_payment_table;
19
-
20
-    public function __construct()
21
-    {
22
-        /** @type WPDB $wpdb */
23
-        global $wpdb;
24
-        $this->_pretty_name = esc_html__('Registration Payment Record Generation', 'event_espresso');
25
-        // define tables
26
-        $this->_old_table                                   = $wpdb->prefix . 'esp_transaction';
27
-        $this->_payment_table                       = $wpdb->prefix . 'esp_payment';
28
-        $this->_registration_table                  = $wpdb->prefix . 'esp_registration';
29
-        $this->_registration_payment_table  = $wpdb->prefix . 'esp_registration_payment';
30
-        // build SQL WHERE clauses
31
-        $this->_extra_where_sql = "WHERE STS_ID IN ( 'TIN', 'TCM' ) AND TXN_Total != '0.000'";
32
-        parent::__construct();
33
-    }
34
-
35
-
36
-
37
-    /**
38
-     * @param array $transaction
39
-     * @return void
40
-     */
41
-    protected function _migrate_old_row($transaction)
42
-    {
43
-        /** @type WPDB $wpdb */
44
-        global $wpdb;
45
-        $TXN_ID = absint($transaction['TXN_ID']);
46
-        if (! $TXN_ID) {
47
-            $this->add_error(
48
-                sprintf(
49
-                    esc_html__('Invalid transaction with ID=%1$d. Error: "%2$s"', 'event_espresso'),
50
-                    $TXN_ID,
51
-                    $wpdb->last_error
52
-                )
53
-            );
54
-            return;
55
-        }
56
-        // get all payments for the TXN
57
-        $payments = $this->_get_payments($TXN_ID);
58
-        if (empty($payments)) {
59
-            return;
60
-        }
61
-        // then the registrants
62
-        $registrations = $this->_get_registrations($TXN_ID);
63
-        if (empty($registrations)) {
64
-            return;
65
-        }
66
-        // now loop thru each payment and apply it to each of the registrations
67
-        foreach ($payments as $PAY_ID => $payment) {
68
-            if ($payment->STS_ID === 'PAP' && $payment->PAY_amount > 0) {
69
-                $this->_process_registration_payments($payment, $registrations);
70
-            }
71
-        }
72
-    }
73
-
74
-
75
-
76
-    /**
77
-     * _get_registrations
78
-     *
79
-     * @param int $TXN_ID
80
-     * @return array
81
-     */
82
-    protected function _get_registrations($TXN_ID)
83
-    {
84
-        /** @type WPDB $wpdb */
85
-        global $wpdb;
86
-        $SQL = "SELECT * FROM {$this->_registration_table} WHERE TXN_ID = %d AND STS_ID IN ( 'RPP', 'RAP' )";
87
-        return $wpdb->get_results($wpdb->prepare($SQL, $TXN_ID), OBJECT_K);
88
-    }
89
-
90
-
91
-
92
-    /**
93
-     * _get_payments
94
-     *
95
-     * @param int $TXN_ID
96
-     * @return array
97
-     */
98
-    protected function _get_payments($TXN_ID)
99
-    {
100
-        /** @type WPDB $wpdb */
101
-        global $wpdb;
102
-        return $wpdb->get_results($wpdb->prepare("SELECT * FROM {$this->_payment_table} WHERE TXN_ID = %d", $TXN_ID), OBJECT_K);
103
-    }
104
-
105
-
106
-
107
-    /**
108
-     * _get_possibly_updated_REG_paid
109
-     *
110
-     * @param int $REG_ID
111
-     * @return array
112
-     */
113
-    protected function _get_possibly_updated_REG_paid($REG_ID)
114
-    {
115
-        /** @type WPDB $wpdb */
116
-        global $wpdb;
117
-        return $wpdb->get_var($wpdb->prepare("SELECT REG_paid FROM {$this->_registration_table} WHERE REG_ID = %d", $REG_ID));
118
-    }
119
-
120
-
121
-
122
-    /**
123
-     * _process_registration_payments
124
-     *
125
-     * basically a copy of the "Sequential Registration Payment Application Strategy"  logic
126
-     * currently in EE_Payment_Processor::process_registration_payments()
127
-     *
128
-     * @param stdClass $payment
129
-     * @param array $registrations
130
-     * @return bool
131
-     */
132
-    protected function _process_registration_payments($payment, $registrations = array())
133
-    {
134
-        // how much is available to apply to registrations?
135
-        $available_payment_amount = $payment->PAY_amount;
136
-        foreach ($registrations as $REG_ID => $registration) {
137
-            // nothing left, then we are done here?
138
-            if (! $available_payment_amount > 0) {
139
-                break;
140
-            }
141
-            // ensure REG_final_price has a valid value, and skip if it turns out to be zero
142
-            $registration->REG_final_price = ! empty($registration->REG_final_price) ? (float) $registration->REG_final_price : 0.00;
143
-            if (! $registration->REG_final_price > 0) {
144
-                continue;
145
-            }
146
-            // because REG_paid may have been updated by a previous payment, we need to retrieve the value from the db
147
-            $possibly_updated_REG_paid = $this->_get_possibly_updated_REG_paid($REG_ID);
148
-            if (is_float($possibly_updated_REG_paid)) {
149
-                $registration->REG_paid = $possibly_updated_REG_paid;
150
-            }
151
-            // and ensure REG_paid has a valid value
152
-            $registration->REG_paid = ! empty($registration->REG_paid) ? (float) $registration->REG_paid : 0.00;
153
-            // calculate amount owing, and skip if it turns out to be zero
154
-            $owing = $registration->REG_final_price - $registration->REG_paid;
155
-            if (! $owing > 0) {
156
-                continue;
157
-            }
158
-            // don't allow payment amount to exceed the available payment amount, OR the amount owing
159
-            $payment_amount = min($available_payment_amount, $owing);
160
-            // update $available_payment_amount
161
-            $available_payment_amount = $available_payment_amount - $payment_amount;
162
-            // add relation between registration and payment and set amount
163
-            if ($this->_insert_registration_payment($registration->REG_ID, $payment->PAY_ID, $payment_amount)) {
164
-                // calculate and set new REG_paid
165
-                $registration->REG_paid = $registration->REG_paid + $payment_amount;
166
-                $this->_update_registration_paid($registration->REG_ID, $registration->REG_paid);
167
-            }
168
-        }
169
-    }
170
-
171
-
172
-
173
-    /**
174
-     * _insert_registration_payment
175
-     *
176
-     * @param int $REG_ID
177
-     * @param int $PAY_ID
178
-     * @param float $PAY_amount
179
-     * @return bool
180
-     */
181
-    protected function _insert_registration_payment($REG_ID = 0, $PAY_ID = 0, $PAY_amount = 0.00)
182
-    {
183
-        /** @type WPDB $wpdb */
184
-        global $wpdb;
185
-        $success = $wpdb->insert(
186
-            $this->_registration_payment_table,
187
-            array( 'REG_ID' => $REG_ID, 'PAY_ID' => $PAY_ID, 'RPY_amount' => $PAY_amount, ),  // data
188
-            array( '%f' )   // data format
189
-        );
190
-        if ($success === false) {
191
-            $this->add_error(
192
-                sprintf(
193
-                    esc_html__('Could not update registration paid value for registration ID=%1$d because "%2$s"', 'event_espresso'),
194
-                    $REG_ID,
195
-                    $wpdb->last_error
196
-                )
197
-            );
198
-            return false;
199
-        }
200
-        return true;
201
-    }
202
-
203
-
204
-
205
-    /**
206
-     * _update_registration_paid
207
-     *
208
-     * @param int $REG_ID
209
-     * @param float $REG_paid
210
-     * @return bool
211
-     */
212
-    protected function _update_registration_paid($REG_ID = 0, $REG_paid = 0.00)
213
-    {
214
-        /** @type WPDB $wpdb */
215
-        global $wpdb;
216
-        $success = $wpdb->update(
217
-            $this->_registration_table,
218
-            array( 'REG_paid' => $REG_paid ),  // data
219
-            array( 'REG_ID' => $REG_ID ),  // where
220
-            array( '%f' ),   // data format
221
-            array( '%d' )  // where format
222
-        );
223
-        if ($success === false) {
224
-            $this->add_error(
225
-                sprintf(
226
-                    esc_html__('Could not update registration paid value for registration ID=%1$d because "%2$s"', 'event_espresso'),
227
-                    $REG_ID,
228
-                    $wpdb->last_error
229
-                )
230
-            );
231
-            return false;
232
-        }
233
-        return true;
234
-    }
14
+	protected $_payment_table;
15
+
16
+	protected $_registration_table;
17
+
18
+	protected $_registration_payment_table;
19
+
20
+	public function __construct()
21
+	{
22
+		/** @type WPDB $wpdb */
23
+		global $wpdb;
24
+		$this->_pretty_name = esc_html__('Registration Payment Record Generation', 'event_espresso');
25
+		// define tables
26
+		$this->_old_table                                   = $wpdb->prefix . 'esp_transaction';
27
+		$this->_payment_table                       = $wpdb->prefix . 'esp_payment';
28
+		$this->_registration_table                  = $wpdb->prefix . 'esp_registration';
29
+		$this->_registration_payment_table  = $wpdb->prefix . 'esp_registration_payment';
30
+		// build SQL WHERE clauses
31
+		$this->_extra_where_sql = "WHERE STS_ID IN ( 'TIN', 'TCM' ) AND TXN_Total != '0.000'";
32
+		parent::__construct();
33
+	}
34
+
35
+
36
+
37
+	/**
38
+	 * @param array $transaction
39
+	 * @return void
40
+	 */
41
+	protected function _migrate_old_row($transaction)
42
+	{
43
+		/** @type WPDB $wpdb */
44
+		global $wpdb;
45
+		$TXN_ID = absint($transaction['TXN_ID']);
46
+		if (! $TXN_ID) {
47
+			$this->add_error(
48
+				sprintf(
49
+					esc_html__('Invalid transaction with ID=%1$d. Error: "%2$s"', 'event_espresso'),
50
+					$TXN_ID,
51
+					$wpdb->last_error
52
+				)
53
+			);
54
+			return;
55
+		}
56
+		// get all payments for the TXN
57
+		$payments = $this->_get_payments($TXN_ID);
58
+		if (empty($payments)) {
59
+			return;
60
+		}
61
+		// then the registrants
62
+		$registrations = $this->_get_registrations($TXN_ID);
63
+		if (empty($registrations)) {
64
+			return;
65
+		}
66
+		// now loop thru each payment and apply it to each of the registrations
67
+		foreach ($payments as $PAY_ID => $payment) {
68
+			if ($payment->STS_ID === 'PAP' && $payment->PAY_amount > 0) {
69
+				$this->_process_registration_payments($payment, $registrations);
70
+			}
71
+		}
72
+	}
73
+
74
+
75
+
76
+	/**
77
+	 * _get_registrations
78
+	 *
79
+	 * @param int $TXN_ID
80
+	 * @return array
81
+	 */
82
+	protected function _get_registrations($TXN_ID)
83
+	{
84
+		/** @type WPDB $wpdb */
85
+		global $wpdb;
86
+		$SQL = "SELECT * FROM {$this->_registration_table} WHERE TXN_ID = %d AND STS_ID IN ( 'RPP', 'RAP' )";
87
+		return $wpdb->get_results($wpdb->prepare($SQL, $TXN_ID), OBJECT_K);
88
+	}
89
+
90
+
91
+
92
+	/**
93
+	 * _get_payments
94
+	 *
95
+	 * @param int $TXN_ID
96
+	 * @return array
97
+	 */
98
+	protected function _get_payments($TXN_ID)
99
+	{
100
+		/** @type WPDB $wpdb */
101
+		global $wpdb;
102
+		return $wpdb->get_results($wpdb->prepare("SELECT * FROM {$this->_payment_table} WHERE TXN_ID = %d", $TXN_ID), OBJECT_K);
103
+	}
104
+
105
+
106
+
107
+	/**
108
+	 * _get_possibly_updated_REG_paid
109
+	 *
110
+	 * @param int $REG_ID
111
+	 * @return array
112
+	 */
113
+	protected function _get_possibly_updated_REG_paid($REG_ID)
114
+	{
115
+		/** @type WPDB $wpdb */
116
+		global $wpdb;
117
+		return $wpdb->get_var($wpdb->prepare("SELECT REG_paid FROM {$this->_registration_table} WHERE REG_ID = %d", $REG_ID));
118
+	}
119
+
120
+
121
+
122
+	/**
123
+	 * _process_registration_payments
124
+	 *
125
+	 * basically a copy of the "Sequential Registration Payment Application Strategy"  logic
126
+	 * currently in EE_Payment_Processor::process_registration_payments()
127
+	 *
128
+	 * @param stdClass $payment
129
+	 * @param array $registrations
130
+	 * @return bool
131
+	 */
132
+	protected function _process_registration_payments($payment, $registrations = array())
133
+	{
134
+		// how much is available to apply to registrations?
135
+		$available_payment_amount = $payment->PAY_amount;
136
+		foreach ($registrations as $REG_ID => $registration) {
137
+			// nothing left, then we are done here?
138
+			if (! $available_payment_amount > 0) {
139
+				break;
140
+			}
141
+			// ensure REG_final_price has a valid value, and skip if it turns out to be zero
142
+			$registration->REG_final_price = ! empty($registration->REG_final_price) ? (float) $registration->REG_final_price : 0.00;
143
+			if (! $registration->REG_final_price > 0) {
144
+				continue;
145
+			}
146
+			// because REG_paid may have been updated by a previous payment, we need to retrieve the value from the db
147
+			$possibly_updated_REG_paid = $this->_get_possibly_updated_REG_paid($REG_ID);
148
+			if (is_float($possibly_updated_REG_paid)) {
149
+				$registration->REG_paid = $possibly_updated_REG_paid;
150
+			}
151
+			// and ensure REG_paid has a valid value
152
+			$registration->REG_paid = ! empty($registration->REG_paid) ? (float) $registration->REG_paid : 0.00;
153
+			// calculate amount owing, and skip if it turns out to be zero
154
+			$owing = $registration->REG_final_price - $registration->REG_paid;
155
+			if (! $owing > 0) {
156
+				continue;
157
+			}
158
+			// don't allow payment amount to exceed the available payment amount, OR the amount owing
159
+			$payment_amount = min($available_payment_amount, $owing);
160
+			// update $available_payment_amount
161
+			$available_payment_amount = $available_payment_amount - $payment_amount;
162
+			// add relation between registration and payment and set amount
163
+			if ($this->_insert_registration_payment($registration->REG_ID, $payment->PAY_ID, $payment_amount)) {
164
+				// calculate and set new REG_paid
165
+				$registration->REG_paid = $registration->REG_paid + $payment_amount;
166
+				$this->_update_registration_paid($registration->REG_ID, $registration->REG_paid);
167
+			}
168
+		}
169
+	}
170
+
171
+
172
+
173
+	/**
174
+	 * _insert_registration_payment
175
+	 *
176
+	 * @param int $REG_ID
177
+	 * @param int $PAY_ID
178
+	 * @param float $PAY_amount
179
+	 * @return bool
180
+	 */
181
+	protected function _insert_registration_payment($REG_ID = 0, $PAY_ID = 0, $PAY_amount = 0.00)
182
+	{
183
+		/** @type WPDB $wpdb */
184
+		global $wpdb;
185
+		$success = $wpdb->insert(
186
+			$this->_registration_payment_table,
187
+			array( 'REG_ID' => $REG_ID, 'PAY_ID' => $PAY_ID, 'RPY_amount' => $PAY_amount, ),  // data
188
+			array( '%f' )   // data format
189
+		);
190
+		if ($success === false) {
191
+			$this->add_error(
192
+				sprintf(
193
+					esc_html__('Could not update registration paid value for registration ID=%1$d because "%2$s"', 'event_espresso'),
194
+					$REG_ID,
195
+					$wpdb->last_error
196
+				)
197
+			);
198
+			return false;
199
+		}
200
+		return true;
201
+	}
202
+
203
+
204
+
205
+	/**
206
+	 * _update_registration_paid
207
+	 *
208
+	 * @param int $REG_ID
209
+	 * @param float $REG_paid
210
+	 * @return bool
211
+	 */
212
+	protected function _update_registration_paid($REG_ID = 0, $REG_paid = 0.00)
213
+	{
214
+		/** @type WPDB $wpdb */
215
+		global $wpdb;
216
+		$success = $wpdb->update(
217
+			$this->_registration_table,
218
+			array( 'REG_paid' => $REG_paid ),  // data
219
+			array( 'REG_ID' => $REG_ID ),  // where
220
+			array( '%f' ),   // data format
221
+			array( '%d' )  // where format
222
+		);
223
+		if ($success === false) {
224
+			$this->add_error(
225
+				sprintf(
226
+					esc_html__('Could not update registration paid value for registration ID=%1$d because "%2$s"', 'event_espresso'),
227
+					$REG_ID,
228
+					$wpdb->last_error
229
+				)
230
+			);
231
+			return false;
232
+		}
233
+		return true;
234
+	}
235 235
 }
Please login to merge, or discard this patch.
4_7_0_stages/EE_DMS_4_7_0_Add_Taxes_To_REG_Final_Price.dmsstage.php 2 patches
Spacing   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -22,9 +22,9 @@  discard block
 block discarded – undo
22 22
         global $wpdb;
23 23
         $this->_pretty_name = esc_html__('Registration Final Price Tax Calculations', 'event_espresso');
24 24
         // define tables
25
-        $this->_old_table               = $wpdb->prefix . 'esp_registration';
26
-        $this->_ticket_table            = $wpdb->prefix . 'esp_ticket';
27
-        $this->_line_item_table     = $wpdb->prefix . 'esp_line_item';
25
+        $this->_old_table               = $wpdb->prefix.'esp_registration';
26
+        $this->_ticket_table            = $wpdb->prefix.'esp_ticket';
27
+        $this->_line_item_table = $wpdb->prefix.'esp_line_item';
28 28
         parent::__construct();
29 29
     }
30 30
 
@@ -109,7 +109,7 @@  discard block
 block discarded – undo
109 109
         /** @type WPDB $wpdb */
110 110
         global $wpdb;
111 111
         // ensure all required values are present
112
-        if (! isset($row['REG_ID'], $row['REG_final_price'], $row['LIN_ID'])) {
112
+        if ( ! isset($row['REG_ID'], $row['REG_final_price'], $row['LIN_ID'])) {
113 113
             $this->add_error(
114 114
                 sprintf(
115 115
                     esc_html__('Invalid query results returned with the following data:%1$s REG_ID=%2$d, REG_final_price=%3$d, LIN_ID=%4$f. Error: "%5$s"', 'event_espresso'),
@@ -124,7 +124,7 @@  discard block
 block discarded – undo
124 124
         }
125 125
         // get tax subtotal
126 126
         $tax_subtotal_line_item_ID = $this->_get_line_item_ID_for_tax_subtotal($row['LIN_ID']);
127
-        if (! $tax_subtotal_line_item_ID) {
127
+        if ( ! $tax_subtotal_line_item_ID) {
128 128
             $this->add_error(
129 129
                 sprintf(
130 130
                     esc_html__('Invalid line item ID returned. Error: "%1$s"', 'event_espresso'),
@@ -191,7 +191,7 @@  discard block
 block discarded – undo
191 191
         if (is_array($taxes) && ! empty($taxes)) {
192 192
             $total_taxes = 0;
193 193
             foreach ($taxes as $tax) {
194
-                $total_taxes += $final_price * ( $tax->LIN_percent / 100 );
194
+                $total_taxes += $final_price * ($tax->LIN_percent / 100);
195 195
             }
196 196
             $final_price += $total_taxes;
197 197
             $this->_update_registration_final_price($REG_ID, $final_price);
@@ -213,10 +213,10 @@  discard block
 block discarded – undo
213 213
         global $wpdb;
214 214
         $success = $wpdb->update(
215 215
             $this->_old_table,
216
-            array( 'REG_final_price' => $REG_final_price ),  // data
217
-            array( 'REG_ID' => $REG_ID ),  // where
218
-            array( '%f' ),   // data format
219
-            array( '%d' )  // where format
216
+            array('REG_final_price' => $REG_final_price), // data
217
+            array('REG_ID' => $REG_ID), // where
218
+            array('%f'), // data format
219
+            array('%d')  // where format
220 220
         );
221 221
         if ($success === false) {
222 222
             $this->add_error(
Please login to merge, or discard this patch.
Indentation   +200 added lines, -200 removed lines patch added patch discarded remove patch
@@ -11,73 +11,73 @@  discard block
 block discarded – undo
11 11
  */
12 12
 class EE_DMS_4_7_0_Add_Taxes_To_REG_Final_Price extends EE_Data_Migration_Script_Stage_Table
13 13
 {
14
-    protected $_ticket_table;
15
-
16
-    protected $_line_item_table;
17
-
18
-    public function __construct()
19
-    {
20
-        /** @type WPDB $wpdb */
21
-        global $wpdb;
22
-        $this->_pretty_name = esc_html__('Registration Final Price Tax Calculations', 'event_espresso');
23
-        // define tables
24
-        $this->_old_table               = $wpdb->prefix . 'esp_registration';
25
-        $this->_ticket_table            = $wpdb->prefix . 'esp_ticket';
26
-        $this->_line_item_table     = $wpdb->prefix . 'esp_line_item';
27
-        parent::__construct();
28
-    }
29
-
30
-
31
-
32
-    /**
33
-     * @return string
34
-     */
35
-    protected function _get_rest_of_sql_for_query()
36
-    {
37
-        $SQL = "FROM {$this->_old_table} AS reg ";
38
-        $SQL .= "JOIN {$this->_ticket_table} as tkt ON reg.TKT_ID = tkt.TKT_ID ";
39
-        $SQL .= "JOIN {$this->_line_item_table} as line ON reg.TXN_ID = line.TXN_ID ";
40
-        $SQL .= "WHERE tkt.TKT_taxable = 1 ";
41
-        $SQL .= "AND line.LIN_code = 'total' ";
42
-        $SQL .= "AND reg.REG_final_price > 0 ";
43
-        return $SQL;
44
-    }
45
-
46
-
47
-
48
-    /**
49
-     * Counts the records to migrate; the public version may cache it
50
-     * @return int
51
-     */
52
-    public function _count_records_to_migrate()
53
-    {
54
-        /** @type WPDB $wpdb */
55
-        global $wpdb;
56
-        $SQL = "SELECT count( reg.REG_ID ) ";
57
-        $SQL .= $this->_get_rest_of_sql_for_query();
58
-        $count = $wpdb->get_var($SQL);
59
-        return $count;
60
-    }
61
-
62
-
63
-
64
-    /**
65
-     * Gets data for all registrations with taxable tickets in the esp_line_item table
66
-     * @global wpdb $wpdb
67
-     * @param int $limit
68
-     * @return array of arrays like $wpdb->get_results($sql, ARRAY_A)
69
-     */
70
-    protected function _get_rows($limit)
71
-    {
72
-        /** @type WPDB $wpdb */
73
-        global $wpdb;
74
-        $start_at_record = $this->count_records_migrated();
75
-        $SQL = "SELECT reg.REG_ID,  reg.REG_final_price, line.LIN_ID ";
76
-        $SQL .= $this->_get_rest_of_sql_for_query();
77
-        $SQL .= $wpdb->prepare("LIMIT %d, %d", $start_at_record, $limit);
78
-
79
-        // produces something like:
80
-        /*
14
+	protected $_ticket_table;
15
+
16
+	protected $_line_item_table;
17
+
18
+	public function __construct()
19
+	{
20
+		/** @type WPDB $wpdb */
21
+		global $wpdb;
22
+		$this->_pretty_name = esc_html__('Registration Final Price Tax Calculations', 'event_espresso');
23
+		// define tables
24
+		$this->_old_table               = $wpdb->prefix . 'esp_registration';
25
+		$this->_ticket_table            = $wpdb->prefix . 'esp_ticket';
26
+		$this->_line_item_table     = $wpdb->prefix . 'esp_line_item';
27
+		parent::__construct();
28
+	}
29
+
30
+
31
+
32
+	/**
33
+	 * @return string
34
+	 */
35
+	protected function _get_rest_of_sql_for_query()
36
+	{
37
+		$SQL = "FROM {$this->_old_table} AS reg ";
38
+		$SQL .= "JOIN {$this->_ticket_table} as tkt ON reg.TKT_ID = tkt.TKT_ID ";
39
+		$SQL .= "JOIN {$this->_line_item_table} as line ON reg.TXN_ID = line.TXN_ID ";
40
+		$SQL .= "WHERE tkt.TKT_taxable = 1 ";
41
+		$SQL .= "AND line.LIN_code = 'total' ";
42
+		$SQL .= "AND reg.REG_final_price > 0 ";
43
+		return $SQL;
44
+	}
45
+
46
+
47
+
48
+	/**
49
+	 * Counts the records to migrate; the public version may cache it
50
+	 * @return int
51
+	 */
52
+	public function _count_records_to_migrate()
53
+	{
54
+		/** @type WPDB $wpdb */
55
+		global $wpdb;
56
+		$SQL = "SELECT count( reg.REG_ID ) ";
57
+		$SQL .= $this->_get_rest_of_sql_for_query();
58
+		$count = $wpdb->get_var($SQL);
59
+		return $count;
60
+	}
61
+
62
+
63
+
64
+	/**
65
+	 * Gets data for all registrations with taxable tickets in the esp_line_item table
66
+	 * @global wpdb $wpdb
67
+	 * @param int $limit
68
+	 * @return array of arrays like $wpdb->get_results($sql, ARRAY_A)
69
+	 */
70
+	protected function _get_rows($limit)
71
+	{
72
+		/** @type WPDB $wpdb */
73
+		global $wpdb;
74
+		$start_at_record = $this->count_records_migrated();
75
+		$SQL = "SELECT reg.REG_ID,  reg.REG_final_price, line.LIN_ID ";
76
+		$SQL .= $this->_get_rest_of_sql_for_query();
77
+		$SQL .= $wpdb->prepare("LIMIT %d, %d", $start_at_record, $limit);
78
+
79
+		// produces something like:
80
+		/*
81 81
             SELECT
82 82
               reg.REG_ID,
83 83
               reg.REG_final_price,
@@ -94,137 +94,137 @@  discard block
 block discarded – undo
94 94
             LIMIT 1, 50
95 95
          */
96 96
 
97
-        return $wpdb->get_results($SQL, ARRAY_A);
98
-    }
99
-
100
-
101
-
102
-    /**
103
-     * @param array $row
104
-     * @return void
105
-     */
106
-    protected function _migrate_old_row($row)
107
-    {
108
-        /** @type WPDB $wpdb */
109
-        global $wpdb;
110
-        // ensure all required values are present
111
-        if (! isset($row['REG_ID'], $row['REG_final_price'], $row['LIN_ID'])) {
112
-            $this->add_error(
113
-                sprintf(
114
-                    esc_html__('Invalid query results returned with the following data:%1$s REG_ID=%2$d, REG_final_price=%3$d, LIN_ID=%4$f. Error: "%5$s"', 'event_espresso'),
115
-                    '<br />',
116
-                    isset($row['REG_ID']) ? $row['REG_ID'] : '',
117
-                    isset($row['REG_final_price']) ? $row['REG_final_price'] : '',
118
-                    isset($row['LIN_ID']) ? $row['LIN_ID'] : '',
119
-                    $wpdb->last_error
120
-                )
121
-            );
122
-            return;
123
-        }
124
-        // get tax subtotal
125
-        $tax_subtotal_line_item_ID = $this->_get_line_item_ID_for_tax_subtotal($row['LIN_ID']);
126
-        if (! $tax_subtotal_line_item_ID) {
127
-            $this->add_error(
128
-                sprintf(
129
-                    esc_html__('Invalid line item ID returned. Error: "%1$s"', 'event_espresso'),
130
-                    $wpdb->last_error
131
-                )
132
-            );
133
-            return;
134
-        }
135
-        // now get taxes
136
-        $taxes = $this->_get_tax_amounts($tax_subtotal_line_item_ID);
137
-        // apply taxes to registration final price
138
-        $this->_apply_taxes($row['REG_ID'], $row['REG_final_price'], $taxes);
139
-    }
140
-
141
-
142
-
143
-    /**
144
-     * _get_tax_subtotal
145
-     *
146
-     * @param int $LIN_ID
147
-     * @return int
148
-     */
149
-    protected function _get_line_item_ID_for_tax_subtotal($LIN_ID)
150
-    {
151
-        /** @type WPDB $wpdb */
152
-        global $wpdb;
153
-        $SQL = "SELECT LIN_ID ";
154
-        $SQL .= "FROM {$this->_line_item_table} ";
155
-        $SQL .= "WHERE LIN_parent = %d ";
156
-        $SQL .= "AND LIN_code = 'taxes'";
157
-        return $wpdb->get_var($wpdb->prepare($SQL, $LIN_ID));
158
-    }
159
-
160
-
161
-
162
-    /**
163
-     * _get_tax_subtotal
164
-     *
165
-     * @param int $LIN_ID
166
-     * @return array
167
-     */
168
-    protected function _get_tax_amounts($LIN_ID)
169
-    {
170
-        /** @type WPDB $wpdb */
171
-        global $wpdb;
172
-        $SQL = "SELECT LIN_percent ";
173
-        $SQL .= "FROM {$this->_line_item_table} ";
174
-        $SQL .= "WHERE LIN_parent = %d";
175
-        return $wpdb->get_results($wpdb->prepare($SQL, $LIN_ID), OBJECT_K);
176
-    }
177
-
178
-
179
-
180
-    /**
181
-     * _apply_taxes
182
-     *
183
-     * @param int $REG_ID
184
-     * @param float $final_price
185
-     * @param array $taxes
186
-     * @return void
187
-     */
188
-    protected function _apply_taxes($REG_ID = 0, $final_price = 0.00, $taxes = array())
189
-    {
190
-        if (is_array($taxes) && ! empty($taxes)) {
191
-            $total_taxes = 0;
192
-            foreach ($taxes as $tax) {
193
-                $total_taxes += $final_price * ( $tax->LIN_percent / 100 );
194
-            }
195
-            $final_price += $total_taxes;
196
-            $this->_update_registration_final_price($REG_ID, $final_price);
197
-        }
198
-    }
199
-
200
-
201
-
202
-    /**
203
-     * _update_registration_final_price
204
-     *
205
-     * @param int $REG_ID
206
-     * @param float $REG_final_price
207
-     * @return void
208
-     */
209
-    protected function _update_registration_final_price($REG_ID = 0, $REG_final_price = 0.00)
210
-    {
211
-        /** @type WPDB $wpdb */
212
-        global $wpdb;
213
-        $success = $wpdb->update(
214
-            $this->_old_table,
215
-            array( 'REG_final_price' => $REG_final_price ),  // data
216
-            array( 'REG_ID' => $REG_ID ),  // where
217
-            array( '%f' ),   // data format
218
-            array( '%d' )  // where format
219
-        );
220
-        if ($success === false) {
221
-            $this->add_error(
222
-                sprintf(
223
-                    esc_html__('Could not update registration final price value for registration ID=%1$d because "%2$s"', 'event_espresso'),
224
-                    $REG_ID,
225
-                    $wpdb->last_error
226
-                )
227
-            );
228
-        }
229
-    }
97
+		return $wpdb->get_results($SQL, ARRAY_A);
98
+	}
99
+
100
+
101
+
102
+	/**
103
+	 * @param array $row
104
+	 * @return void
105
+	 */
106
+	protected function _migrate_old_row($row)
107
+	{
108
+		/** @type WPDB $wpdb */
109
+		global $wpdb;
110
+		// ensure all required values are present
111
+		if (! isset($row['REG_ID'], $row['REG_final_price'], $row['LIN_ID'])) {
112
+			$this->add_error(
113
+				sprintf(
114
+					esc_html__('Invalid query results returned with the following data:%1$s REG_ID=%2$d, REG_final_price=%3$d, LIN_ID=%4$f. Error: "%5$s"', 'event_espresso'),
115
+					'<br />',
116
+					isset($row['REG_ID']) ? $row['REG_ID'] : '',
117
+					isset($row['REG_final_price']) ? $row['REG_final_price'] : '',
118
+					isset($row['LIN_ID']) ? $row['LIN_ID'] : '',
119
+					$wpdb->last_error
120
+				)
121
+			);
122
+			return;
123
+		}
124
+		// get tax subtotal
125
+		$tax_subtotal_line_item_ID = $this->_get_line_item_ID_for_tax_subtotal($row['LIN_ID']);
126
+		if (! $tax_subtotal_line_item_ID) {
127
+			$this->add_error(
128
+				sprintf(
129
+					esc_html__('Invalid line item ID returned. Error: "%1$s"', 'event_espresso'),
130
+					$wpdb->last_error
131
+				)
132
+			);
133
+			return;
134
+		}
135
+		// now get taxes
136
+		$taxes = $this->_get_tax_amounts($tax_subtotal_line_item_ID);
137
+		// apply taxes to registration final price
138
+		$this->_apply_taxes($row['REG_ID'], $row['REG_final_price'], $taxes);
139
+	}
140
+
141
+
142
+
143
+	/**
144
+	 * _get_tax_subtotal
145
+	 *
146
+	 * @param int $LIN_ID
147
+	 * @return int
148
+	 */
149
+	protected function _get_line_item_ID_for_tax_subtotal($LIN_ID)
150
+	{
151
+		/** @type WPDB $wpdb */
152
+		global $wpdb;
153
+		$SQL = "SELECT LIN_ID ";
154
+		$SQL .= "FROM {$this->_line_item_table} ";
155
+		$SQL .= "WHERE LIN_parent = %d ";
156
+		$SQL .= "AND LIN_code = 'taxes'";
157
+		return $wpdb->get_var($wpdb->prepare($SQL, $LIN_ID));
158
+	}
159
+
160
+
161
+
162
+	/**
163
+	 * _get_tax_subtotal
164
+	 *
165
+	 * @param int $LIN_ID
166
+	 * @return array
167
+	 */
168
+	protected function _get_tax_amounts($LIN_ID)
169
+	{
170
+		/** @type WPDB $wpdb */
171
+		global $wpdb;
172
+		$SQL = "SELECT LIN_percent ";
173
+		$SQL .= "FROM {$this->_line_item_table} ";
174
+		$SQL .= "WHERE LIN_parent = %d";
175
+		return $wpdb->get_results($wpdb->prepare($SQL, $LIN_ID), OBJECT_K);
176
+	}
177
+
178
+
179
+
180
+	/**
181
+	 * _apply_taxes
182
+	 *
183
+	 * @param int $REG_ID
184
+	 * @param float $final_price
185
+	 * @param array $taxes
186
+	 * @return void
187
+	 */
188
+	protected function _apply_taxes($REG_ID = 0, $final_price = 0.00, $taxes = array())
189
+	{
190
+		if (is_array($taxes) && ! empty($taxes)) {
191
+			$total_taxes = 0;
192
+			foreach ($taxes as $tax) {
193
+				$total_taxes += $final_price * ( $tax->LIN_percent / 100 );
194
+			}
195
+			$final_price += $total_taxes;
196
+			$this->_update_registration_final_price($REG_ID, $final_price);
197
+		}
198
+	}
199
+
200
+
201
+
202
+	/**
203
+	 * _update_registration_final_price
204
+	 *
205
+	 * @param int $REG_ID
206
+	 * @param float $REG_final_price
207
+	 * @return void
208
+	 */
209
+	protected function _update_registration_final_price($REG_ID = 0, $REG_final_price = 0.00)
210
+	{
211
+		/** @type WPDB $wpdb */
212
+		global $wpdb;
213
+		$success = $wpdb->update(
214
+			$this->_old_table,
215
+			array( 'REG_final_price' => $REG_final_price ),  // data
216
+			array( 'REG_ID' => $REG_ID ),  // where
217
+			array( '%f' ),   // data format
218
+			array( '%d' )  // where format
219
+		);
220
+		if ($success === false) {
221
+			$this->add_error(
222
+				sprintf(
223
+					esc_html__('Could not update registration final price value for registration ID=%1$d because "%2$s"', 'event_espresso'),
224
+					$REG_ID,
225
+					$wpdb->last_error
226
+				)
227
+			);
228
+		}
229
+	}
230 230
 }
Please login to merge, or discard this patch.
4_2_0_stages/EE_DMS_4_2_0_datetime_fields.dmsstage.php 2 patches
Indentation   +43 added lines, -43 removed lines patch added patch discarded remove patch
@@ -8,49 +8,49 @@
 block discarded – undo
8 8
 
9 9
 class EE_DMS_4_2_0_datetime_fields extends EE_Data_Migration_Script_Stage_Table
10 10
 {
11
-    public function __construct()
12
-    {
13
-        global $wpdb;
14
-        $this->_pretty_name = esc_html__("Datetime Fields", "event_espresso");
15
-        $this->_old_table = $wpdb->prefix . "esp_datetime";
16
-        parent::__construct();
17
-    }
11
+	public function __construct()
12
+	{
13
+		global $wpdb;
14
+		$this->_pretty_name = esc_html__("Datetime Fields", "event_espresso");
15
+		$this->_old_table = $wpdb->prefix . "esp_datetime";
16
+		parent::__construct();
17
+	}
18 18
 
19
-    protected function _migrate_old_row($old_row)
20
-    {
21
-        // foreach question_group_question entry with this QST_ID, we want to set its
22
-        // QSG_order equal to this question's QST_order
23
-        global $wpdb;
19
+	protected function _migrate_old_row($old_row)
20
+	{
21
+		// foreach question_group_question entry with this QST_ID, we want to set its
22
+		// QSG_order equal to this question's QST_order
23
+		global $wpdb;
24 24
 
25
-        $updated = $wpdb->update(
26
-            $this->_old_table,
27
-            array(
28
-                'DTT_name' => '',
29
-                'DTT_description' => '',
30
-            ),
31
-            array(
32
-                'DTT_ID' => $old_row['DTT_ID'],
33
-            ),
34
-            array(
35
-                '%s',// DTT_name,
36
-                '%s',// DTT_description
37
-            ),
38
-            array(
39
-                '%d',// DTT_ID
40
-            )
41
-        );
42
-        if (false === $updated) {
43
-            $this->add_error(
44
-                sprintf(
45
-                    esc_html__(
46
-                        "Error in updating table %s setting DTT_name = '' and DTT_description = '' where DTT_ID = %d",
47
-                        'event_espresso'
48
-                    ),
49
-                    $this->_old_table,
50
-                    $old_row['QST_ID']
51
-                )
52
-            );
53
-        }
54
-        // nothing to map really
55
-    }
25
+		$updated = $wpdb->update(
26
+			$this->_old_table,
27
+			array(
28
+				'DTT_name' => '',
29
+				'DTT_description' => '',
30
+			),
31
+			array(
32
+				'DTT_ID' => $old_row['DTT_ID'],
33
+			),
34
+			array(
35
+				'%s',// DTT_name,
36
+				'%s',// DTT_description
37
+			),
38
+			array(
39
+				'%d',// DTT_ID
40
+			)
41
+		);
42
+		if (false === $updated) {
43
+			$this->add_error(
44
+				sprintf(
45
+					esc_html__(
46
+						"Error in updating table %s setting DTT_name = '' and DTT_description = '' where DTT_ID = %d",
47
+						'event_espresso'
48
+					),
49
+					$this->_old_table,
50
+					$old_row['QST_ID']
51
+				)
52
+			);
53
+		}
54
+		// nothing to map really
55
+	}
56 56
 }
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -12,7 +12,7 @@  discard block
 block discarded – undo
12 12
     {
13 13
         global $wpdb;
14 14
         $this->_pretty_name = esc_html__("Datetime Fields", "event_espresso");
15
-        $this->_old_table = $wpdb->prefix . "esp_datetime";
15
+        $this->_old_table = $wpdb->prefix."esp_datetime";
16 16
         parent::__construct();
17 17
     }
18 18
 
@@ -32,11 +32,11 @@  discard block
 block discarded – undo
32 32
                 'DTT_ID' => $old_row['DTT_ID'],
33 33
             ),
34 34
             array(
35
-                '%s',// DTT_name,
36
-                '%s',// DTT_description
35
+                '%s', // DTT_name,
36
+                '%s', // DTT_description
37 37
             ),
38 38
             array(
39
-                '%d',// DTT_ID
39
+                '%d', // DTT_ID
40 40
             )
41 41
         );
42 42
         if (false === $updated) {
Please login to merge, or discard this patch.
4_2_0_stages/EE_DMS_4_2_0_question_group_questions.dmsstage.php 2 patches
Indentation   +38 added lines, -38 removed lines patch added patch discarded remove patch
@@ -6,42 +6,42 @@
 block discarded – undo
6 6
 
7 7
 class EE_DMS_4_2_0_question_group_questions extends EE_Data_Migration_Script_Stage_Table
8 8
 {
9
-    private $_qgq_table;
10
-    public function __construct()
11
-    {
12
-        global $wpdb;
13
-        $this->_pretty_name = esc_html__("Question Group Questions", "event_espresso");
14
-        $this->_old_table = $wpdb->prefix . "esp_question";
15
-        $this->_qgq_table = $wpdb->prefix . "esp_question_group_question";
16
-        parent::__construct();
17
-    }
18
-    protected function _migrate_old_row($old_row)
19
-    {
20
-        // foreach question_group_question entry with this QST_ID, we want to set its
21
-        // QSG_order equal to this question's QST_order
22
-        global $wpdb;
23
-        $updated = $wpdb->update(
24
-            $this->_qgq_table,
25
-            array('QGQ_order' => $old_row['QST_order']),
26
-            array('QST_ID' => $old_row['QST_ID']),
27
-            array('%d',// QGQ_order
28
-                    ),
29
-            array('%d',// QST_ID
30
-                    )
31
-        );
32
-        if (false === $updated) {
33
-            $this->add_error(
34
-                sprintf(
35
-                    esc_html__(
36
-                        "Error in updating table %s setting QGQ_order = %d where QST_ID = %d",
37
-                        'event_espresso'
38
-                    ),
39
-                    $this->_qgq_table,
40
-                    $old_row['QST_order'],
41
-                    $old_row['QST_ID']
42
-                )
43
-            );
44
-        }
45
-        // nothing to map really
46
-    }
9
+	private $_qgq_table;
10
+	public function __construct()
11
+	{
12
+		global $wpdb;
13
+		$this->_pretty_name = esc_html__("Question Group Questions", "event_espresso");
14
+		$this->_old_table = $wpdb->prefix . "esp_question";
15
+		$this->_qgq_table = $wpdb->prefix . "esp_question_group_question";
16
+		parent::__construct();
17
+	}
18
+	protected function _migrate_old_row($old_row)
19
+	{
20
+		// foreach question_group_question entry with this QST_ID, we want to set its
21
+		// QSG_order equal to this question's QST_order
22
+		global $wpdb;
23
+		$updated = $wpdb->update(
24
+			$this->_qgq_table,
25
+			array('QGQ_order' => $old_row['QST_order']),
26
+			array('QST_ID' => $old_row['QST_ID']),
27
+			array('%d',// QGQ_order
28
+					),
29
+			array('%d',// QST_ID
30
+					)
31
+		);
32
+		if (false === $updated) {
33
+			$this->add_error(
34
+				sprintf(
35
+					esc_html__(
36
+						"Error in updating table %s setting QGQ_order = %d where QST_ID = %d",
37
+						'event_espresso'
38
+					),
39
+					$this->_qgq_table,
40
+					$old_row['QST_order'],
41
+					$old_row['QST_ID']
42
+				)
43
+			);
44
+		}
45
+		// nothing to map really
46
+	}
47 47
 }
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -11,8 +11,8 @@  discard block
 block discarded – undo
11 11
     {
12 12
         global $wpdb;
13 13
         $this->_pretty_name = esc_html__("Question Group Questions", "event_espresso");
14
-        $this->_old_table = $wpdb->prefix . "esp_question";
15
-        $this->_qgq_table = $wpdb->prefix . "esp_question_group_question";
14
+        $this->_old_table = $wpdb->prefix."esp_question";
15
+        $this->_qgq_table = $wpdb->prefix."esp_question_group_question";
16 16
         parent::__construct();
17 17
     }
18 18
     protected function _migrate_old_row($old_row)
@@ -24,9 +24,9 @@  discard block
 block discarded – undo
24 24
             $this->_qgq_table,
25 25
             array('QGQ_order' => $old_row['QST_order']),
26 26
             array('QST_ID' => $old_row['QST_ID']),
27
-            array('%d',// QGQ_order
27
+            array('%d', // QGQ_order
28 28
                     ),
29
-            array('%d',// QST_ID
29
+            array('%d', // QST_ID
30 30
                     )
31 31
         );
32 32
         if (false === $updated) {
Please login to merge, or discard this patch.
4_6_0_stages/EE_DMS_4_6_0_question_types.dmsstage.php 2 patches
Spacing   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -25,7 +25,7 @@  discard block
 block discarded – undo
25 25
     {
26 26
         global $wpdb;
27 27
         $this->_pretty_name = esc_html__('Question Types', 'event_espresso');
28
-        $this->_old_table = $wpdb->prefix . 'esp_question';
28
+        $this->_old_table = $wpdb->prefix.'esp_question';
29 29
         $this->_question_type_conversions = array(
30 30
             'MULTIPLE'          => 'CHECKBOX',
31 31
             'SINGLE'                => 'RADIO_BTN'
@@ -34,7 +34,7 @@  discard block
 block discarded – undo
34 34
         // rows counted before migrating any rows, need to ALSO be counted after a bunch of rows were counted
35 35
         // so we need to include both the migrated rows as well as the non-migrated rows
36 36
         $QST_types_to_count = array_merge(array_keys($this->_question_type_conversions), $this->_question_type_conversions);
37
-        $this->_extra_where_sql = "WHERE QST_type IN ('" . implode("', '", $QST_types_to_count) . "')" ;
37
+        $this->_extra_where_sql = "WHERE QST_type IN ('".implode("', '", $QST_types_to_count)."')";
38 38
         parent::__construct();
39 39
     }
40 40
 
@@ -45,15 +45,15 @@  discard block
 block discarded – undo
45 45
     protected function _migrate_old_row($question)
46 46
     {
47 47
         global $wpdb;
48
-        if ($question['QST_ID'] && isset($this->_question_type_conversions[ $question['QST_type'] ])) {
48
+        if ($question['QST_ID'] && isset($this->_question_type_conversions[$question['QST_type']])) {
49 49
             $success = $wpdb->update(
50 50
                 $this->_old_table,
51
-                array( 'QST_type' => $this->_question_type_conversions[ $question['QST_type'] ] ),  // data
52
-                array( 'QST_ID' => $question['QST_ID'] ),  // where
53
-                array( '%s' ),   // data format
54
-                array( '%d' )  // where format
51
+                array('QST_type' => $this->_question_type_conversions[$question['QST_type']]), // data
52
+                array('QST_ID' => $question['QST_ID']), // where
53
+                array('%s'), // data format
54
+                array('%d')  // where format
55 55
             );
56
-            if (! $success) {
56
+            if ( ! $success) {
57 57
                 $this->add_error(
58 58
                     sprintf(
59 59
                         esc_html__('Could not update question type %1$s for question ID=%2$d because "%3$s"', 'event_espresso'),
Please login to merge, or discard this patch.
Indentation   +49 added lines, -49 removed lines patch added patch discarded remove patch
@@ -11,57 +11,57 @@
 block discarded – undo
11 11
  */
12 12
 class EE_DMS_4_6_0_question_types extends EE_Data_Migration_Script_Stage_Table
13 13
 {
14
-    protected $_question_type_conversions = array();
14
+	protected $_question_type_conversions = array();
15 15
 
16 16
 
17 17
 
18
-    /**
19
-     * Just initializes the status of the migration
20
-     *
21
-     * @return EE_DMS_4_6_0_question_types
22
-     */
23
-    public function __construct()
24
-    {
25
-        global $wpdb;
26
-        $this->_pretty_name = esc_html__('Question Types', 'event_espresso');
27
-        $this->_old_table = $wpdb->prefix . 'esp_question';
28
-        $this->_question_type_conversions = array(
29
-            'MULTIPLE'          => 'CHECKBOX',
30
-            'SINGLE'                => 'RADIO_BTN'
31
-        );
32
-        // when fetching rows, because we automatically use a limit and offset
33
-        // rows counted before migrating any rows, need to ALSO be counted after a bunch of rows were counted
34
-        // so we need to include both the migrated rows as well as the non-migrated rows
35
-        $QST_types_to_count = array_merge(array_keys($this->_question_type_conversions), $this->_question_type_conversions);
36
-        $this->_extra_where_sql = "WHERE QST_type IN ('" . implode("', '", $QST_types_to_count) . "')" ;
37
-        parent::__construct();
38
-    }
18
+	/**
19
+	 * Just initializes the status of the migration
20
+	 *
21
+	 * @return EE_DMS_4_6_0_question_types
22
+	 */
23
+	public function __construct()
24
+	{
25
+		global $wpdb;
26
+		$this->_pretty_name = esc_html__('Question Types', 'event_espresso');
27
+		$this->_old_table = $wpdb->prefix . 'esp_question';
28
+		$this->_question_type_conversions = array(
29
+			'MULTIPLE'          => 'CHECKBOX',
30
+			'SINGLE'                => 'RADIO_BTN'
31
+		);
32
+		// when fetching rows, because we automatically use a limit and offset
33
+		// rows counted before migrating any rows, need to ALSO be counted after a bunch of rows were counted
34
+		// so we need to include both the migrated rows as well as the non-migrated rows
35
+		$QST_types_to_count = array_merge(array_keys($this->_question_type_conversions), $this->_question_type_conversions);
36
+		$this->_extra_where_sql = "WHERE QST_type IN ('" . implode("', '", $QST_types_to_count) . "')" ;
37
+		parent::__construct();
38
+	}
39 39
 
40
-    /**
41
-     * @param array $question an associative array where keys are column names and values are their values.
42
-     * @return null
43
-     */
44
-    protected function _migrate_old_row($question)
45
-    {
46
-        global $wpdb;
47
-        if ($question['QST_ID'] && isset($this->_question_type_conversions[ $question['QST_type'] ])) {
48
-            $success = $wpdb->update(
49
-                $this->_old_table,
50
-                array( 'QST_type' => $this->_question_type_conversions[ $question['QST_type'] ] ),  // data
51
-                array( 'QST_ID' => $question['QST_ID'] ),  // where
52
-                array( '%s' ),   // data format
53
-                array( '%d' )  // where format
54
-            );
55
-            if (! $success) {
56
-                $this->add_error(
57
-                    sprintf(
58
-                        esc_html__('Could not update question type %1$s for question ID=%2$d because "%3$s"', 'event_espresso'),
59
-                        wp_json_encode($question['QST_type']),
60
-                        $question['QST_ID'],
61
-                        $wpdb->last_error
62
-                    )
63
-                );
64
-            }
65
-        }
66
-    }
40
+	/**
41
+	 * @param array $question an associative array where keys are column names and values are their values.
42
+	 * @return null
43
+	 */
44
+	protected function _migrate_old_row($question)
45
+	{
46
+		global $wpdb;
47
+		if ($question['QST_ID'] && isset($this->_question_type_conversions[ $question['QST_type'] ])) {
48
+			$success = $wpdb->update(
49
+				$this->_old_table,
50
+				array( 'QST_type' => $this->_question_type_conversions[ $question['QST_type'] ] ),  // data
51
+				array( 'QST_ID' => $question['QST_ID'] ),  // where
52
+				array( '%s' ),   // data format
53
+				array( '%d' )  // where format
54
+			);
55
+			if (! $success) {
56
+				$this->add_error(
57
+					sprintf(
58
+						esc_html__('Could not update question type %1$s for question ID=%2$d because "%3$s"', 'event_espresso'),
59
+						wp_json_encode($question['QST_type']),
60
+						$question['QST_ID'],
61
+						$wpdb->last_error
62
+					)
63
+				);
64
+			}
65
+		}
66
+	}
67 67
 }
Please login to merge, or discard this patch.