/**
  * 	adds related EE_Registration objects for each ticket in the cart to the current EE_Transaction object
  *
  * 	@access private
  * 	@return 	void
  */
 private function _initialize_registrations()
 {
     if ($this->_transaction instanceof EE_Transaction) {
         $att_nmbr = 0;
         $total_items = $this->_cart->all_ticket_quantity_count();
         // now let's add the cart items to the $transaction
         foreach ($this->_cart->get_tickets() as $item) {
             // grab the related ticket object for this line_item
             $ticket = $item->ticket();
             if (!$ticket instanceof EE_Ticket) {
                 EE_Error::add_error(sprintf(__("Line item %s did not contain a valid ticket", "event_espresso"), $item->ID()), __FILE__, __FUNCTION__, __LINE__);
                 break;
             }
             $first_datetime = $ticket->get_first_related('Datetime');
             if (!$first_datetime instanceof EE_Datetime) {
                 EE_Error::add_error(sprintf(__("The ticket (%s) is not associated with any valid datetimes.", "event_espresso"), $ticket->name()), __FILE__, __FUNCTION__, __LINE__);
                 continue;
             }
             $event = $first_datetime->get_first_related('Event');
             if (!$event instanceof EE_Event) {
                 EE_Error::add_error(sprintf(__("The ticket (%s) is not associated with a valid event.", "event_espresso"), $ticket->name()), __FILE__, __FUNCTION__, __LINE__);
                 continue;
             }
             //do the following for each ticket of this type they selected
             for ($x = 1; $x <= $item->quantity(); $x++) {
                 $att_nmbr++;
                 $reg_url_link = $att_nmbr . '-' . $item->code();
                 // grab the default reg status for the event
                 $registration_status = $event->default_registration_status();
                 // if it's set to "Approved", then temporarily downgrade it to "Pending Payment", so that reg limits and/or ticket sales are not skewed in case the reg process is aborted
                 $registration_status = $registration_status == EEM_Registration::status_id_approved ? EEM_Registration::status_id_pending_payment : $registration_status;
                 try {
                     // now create a new registration for the ticket
                     $registration = EE_Registration::new_instance(array('EVT_ID' => $event->ID(), 'TXN_ID' => $this->_transaction->ID(), 'TKT_ID' => $ticket->ID(), 'STS_ID' => $registration_status, 'REG_date' => $this->_transaction->datetime(), 'REG_final_price' => $ticket->price(), 'REG_session' => EE_Registry::instance()->SSN->id(), 'REG_count' => $att_nmbr, 'REG_group_size' => $total_items, 'REG_url_link' => $reg_url_link));
                     // now create relations between various objects
                     $registration->_add_relation_to($event, 'Event', array(), $event->ID());
                     $registration->_add_relation_to($item->ticket(), 'Ticket', array(), $item->ticket()->ID());
                     $this->_transaction->_add_relation_to($registration, 'Registration', array(), $reg_url_link);
                     // if something failed...
                 } catch (Exception $e) {
                     EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__);
                     return;
                 }
             }
         }
         EE_Registry::instance()->SSN->set_session_data(array('transaction' => $this->_transaction));
         EE_Registry::instance()->SSN->update();
         //			echo '<h3>'. __CLASS__ . '->' . __FUNCTION__ . ' <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span></h3>';
     }
     return;
 }
 /**
  * generate_ONE_registration_from_line_item
  *
  * Although a ticket line item may have a quantity greater than 1,
  * this method will ONLY CREATE ONE REGISTRATION !!!
  * Regardless of the ticket line item quantity.
  * This means that any code calling this method is responsible for ensuring
  * that the final registration count matches the ticket line item quantity.
  * This was done to make it easier to match the number of registrations
  * to the number of tickets in the cart, when the cart has been edited
  * after SPCO has already been initialized. So if an additional ticket was added to the cart, you can simply pass
  * the line item to this method to add a second ticket, and in this case, you would not want to add 2 tickets.
  *
  * @param EE_Line_Item $line_item
  * @param \EE_Transaction $transaction
  * @param int $att_nmbr
  * @param int $total_ticket_count
  * @return \EE_Registration | null
  * @throws \EE_Error
  */
 public function generate_ONE_registration_from_line_item(EE_Line_Item $line_item, EE_Transaction $transaction, $att_nmbr = 1, $total_ticket_count = 1)
 {
     // grab the related ticket object for this line_item
     $ticket = $line_item->ticket();
     if (!$ticket instanceof EE_Ticket) {
         EE_Error::add_error(sprintf(__("Line item %s did not contain a valid ticket", "event_espresso"), $line_item->ID()), __FILE__, __FUNCTION__, __LINE__);
         return null;
     }
     $first_datetime = $ticket->get_first_related('Datetime');
     if (!$first_datetime instanceof EE_Datetime) {
         EE_Error::add_error(sprintf(__("The ticket (%s) is not associated with any valid datetimes.", "event_espresso"), $ticket->name()), __FILE__, __FUNCTION__, __LINE__);
         return null;
     }
     $event = $first_datetime->get_first_related('Event');
     if (!$event instanceof EE_Event) {
         EE_Error::add_error(sprintf(__("The ticket (%s) is not associated with a valid event.", "event_espresso"), $ticket->name()), __FILE__, __FUNCTION__, __LINE__);
         return null;
     }
     $reg_url_link = $this->generate_reg_url_link($att_nmbr, $line_item);
     if ($this->_reg_final_price_per_tkt_line_item === null) {
         $this->_reg_final_price_per_tkt_line_item = EEH_Line_Item::calculate_reg_final_prices_per_line_item($transaction->total_line_item());
     }
     //ok now find this new registration's final price
     if (isset($this->_reg_final_price_per_tkt_line_item[$line_item->ID()])) {
         $final_price = $this->_reg_final_price_per_tkt_line_item[$line_item->ID()];
     } else {
         $message = sprintf(__('The ticket line item (ID:%1$d) had no entry in the reg_final_price_per_tkt_line_item array.', 'event_espresso'), $line_item->ID());
         if (WP_DEBUG) {
             throw new EE_Error($message);
         } else {
             EE_Log::instance()->log(__CLASS__, __FUNCTION__, $message);
         }
         $final_price = $ticket->get_ticket_total_with_taxes();
     }
     // now create a new registration for the ticket
     $registration = EE_Registration::new_instance(array('EVT_ID' => $event->ID(), 'TXN_ID' => $transaction->ID(), 'TKT_ID' => $ticket->ID(), 'STS_ID' => EEM_Registration::status_id_incomplete, 'REG_date' => $transaction->datetime(), 'REG_final_price' => $final_price, 'REG_session' => EE_Registry::instance()->SSN->id(), 'REG_count' => $att_nmbr, 'REG_group_size' => $total_ticket_count, 'REG_url_link' => $reg_url_link));
     $registration->set_reg_code($this->generate_reg_code($registration));
     $registration->save();
     $registration->_add_relation_to($event, 'Event', array(), $event->ID());
     $registration->_add_relation_to($line_item->ticket(), 'Ticket', array(), $line_item->ticket()->ID());
     $transaction->_add_relation_to($registration, 'Registration');
     return $registration;
 }
 /**
  *   reset_registration_reg_counts
  *
  * @access protected
  * @param EE_Transaction $transaction
  * @param int $total_ticket_count
  * @return void
  */
 protected static function reset_registration_details(EE_Transaction $transaction, $total_ticket_count = 0)
 {
     $att_nmbr = 0;
     /** @type EE_Registration_Processor $registration_processor */
     $registration_processor = EE_Registry::instance()->load_class('Registration_Processor');
     $registrations = $transaction->registrations();
     uasort($registrations, array('EED_Multi_Event_Registration', 'sort_registrations_by_reg_count_callback'));
     foreach ($registrations as $registration) {
         if ($registration instanceof EE_Registration) {
             $att_nmbr++;
             $registration->set_count($att_nmbr);
             $registration->set_group_size($total_ticket_count);
             $reg_url_bits = explode('-', $registration->reg_url_link());
             $reg_url_link = $att_nmbr . '-' . end($reg_url_bits);
             $registration->set_reg_url_link($reg_url_link);
             $registration->set('REG_code', $registration_processor->generate_reg_code($registration));
             $registration->save();
             $transaction->_add_relation_to($registration, 'Registration');
         }
     }
 }
 /**
  * generate_ONE_registration_from_line_item
  *
  * Although a ticket line item may have a quantity greater than 1,
  * this method will ONLY CREATE ONE REGISTRATION !!!
  * Regardless of the ticket line item quantity.
  * This means that any code calling this method is responsible for ensuring
  * that the final registration count matches the ticket line item quantity.
  * This was done to make it easier to match the number of registrations
  * to the number of tickets in the cart, when the cart has been edited
  * after SPCO has already been initialized. So if an additional ticket was added to the cart, you can simply pass
  * the line item to this method to add a second ticket, and in this case, you would not want to add 2 tickets.
  *
  * @param EE_Line_Item $line_item
  * @param \EE_Transaction $transaction
  * @param int $att_nmbr
  * @param int $total_ticket_count
  * @return \EE_Registration | null
  * @throws \EE_Error
  */
 public function generate_ONE_registration_from_line_item(EE_Line_Item $line_item, EE_Transaction $transaction, $att_nmbr = 1, $total_ticket_count = 1)
 {
     // grab the related ticket object for this line_item
     $ticket = $line_item->ticket();
     if (!$ticket instanceof EE_Ticket) {
         EE_Error::add_error(sprintf(__("Line item %s did not contain a valid ticket", "event_espresso"), $line_item->ID()), __FILE__, __FUNCTION__, __LINE__);
         return null;
     }
     $first_datetime = $ticket->get_first_related('Datetime');
     if (!$first_datetime instanceof EE_Datetime) {
         EE_Error::add_error(sprintf(__("The ticket (%s) is not associated with any valid datetimes.", "event_espresso"), $ticket->name()), __FILE__, __FUNCTION__, __LINE__);
         return null;
     }
     $event = $first_datetime->get_first_related('Event');
     if (!$event instanceof EE_Event) {
         EE_Error::add_error(sprintf(__("The ticket (%s) is not associated with a valid event.", "event_espresso"), $ticket->name()), __FILE__, __FUNCTION__, __LINE__);
         return null;
     }
     $reg_url_link = $this->generate_reg_url_link($att_nmbr, $line_item);
     // now create a new registration for the ticket
     $registration = EE_Registration::new_instance(array('EVT_ID' => $event->ID(), 'TXN_ID' => $transaction->ID(), 'TKT_ID' => $ticket->ID(), 'STS_ID' => EEM_Registration::status_id_incomplete, 'REG_date' => $transaction->datetime(), 'REG_final_price' => $ticket->get_ticket_total_with_taxes(), 'REG_session' => EE_Registry::instance()->SSN->id(), 'REG_count' => $att_nmbr, 'REG_group_size' => $total_ticket_count, 'REG_url_link' => $reg_url_link));
     $registration->set_reg_code($this->generate_reg_code($registration));
     $registration->save();
     $registration->_add_relation_to($event, 'Event', array(), $event->ID());
     $registration->_add_relation_to($line_item->ticket(), 'Ticket', array(), $line_item->ticket()->ID());
     $transaction->_add_relation_to($registration, 'Registration');
     return $registration;
 }
 /**
  * This handles connecting a transaction to related items when the chained flag is true.
  *
  * @since 4.3.0
  *
  * @param EE_Transaction $transaction
  * @param array $args incoming arguments from caller for specifying overrides.
  *
  * @return EE_Transaction
  */
 private function _maybe_chained(EE_Transaction $transaction, $args)
 {
     if ($this->_chained) {
         if (empty($this->_status)) {
             $this->_set_repeated_relation($args, $transaction->ID());
         }
         //YES we DO want to set brand new relation objects because multiple transactions do not share the same related objects (for the purpose of tests at least)
         $this->_set_new_relations($args, $transaction->ID());
         //note relation to registration should already be set via the factory->registration_chained->create() method.
         //add relation to status
         $transaction->_add_relation_to($this->_status, 'Status');
         $transaction->save();
         return $transaction;
     }
     return $transaction;
 }