/**
  *    _set_initial_ticket_datetime_availability
  *
  * @access 	private
  * @param 	EE_Ticket $ticket
  * @return 	int
  */
 private static function _set_initial_ticket_datetime_availability(EE_Ticket $ticket)
 {
     // first, get all of the datetimes that are available to this ticket
     $datetimes = $ticket->get_many_related('Datetime', array(array('DTT_EVT_end' => array('>=', EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end'))), 'order_by' => array('DTT_EVT_start' => 'ASC')));
     if (!empty($datetimes)) {
         // now loop thru all of the datetimes
         foreach ($datetimes as $datetime) {
             if ($datetime instanceof EE_Datetime) {
                 // the number of spaces available for the datetime without considering individual ticket quantities
                 $spaces_remaining = $datetime->spaces_remaining();
                 // save the total available spaces ( the lesser of the ticket qty minus the number of tickets sold or the datetime spaces remaining) to this ticket using the datetime ID as the key
                 self::$_available_spaces['tickets'][$ticket->ID()][$datetime->ID()] = min($ticket->qty() - $ticket->sold(), $spaces_remaining);
                 // if the remaining spaces for this datetime is already set, then compare that against the datetime spaces remaining, and take the lowest number,
                 // else just take the datetime spaces remaining, and assign to the datetimes array
                 self::$_available_spaces['datetimes'][$datetime->ID()] = isset(self::$_available_spaces['datetimes'][$datetime->ID()]) ? min(self::$_available_spaces['datetimes'][$datetime->ID()], $spaces_remaining) : $spaces_remaining;
             }
         }
     }
 }
 /**
  *
  * @access  protected
  * @param \EE_Ticket $ticket
  * @param array $price_rows
  * @param int $ticket_price
  * @param int $base_price
  * @param int $base_price_id
  * @return \EE_Ticket
  * @throws \EE_Error
  */
 protected function _duplicate_ticket(EE_Ticket $ticket, $price_rows = array(), $ticket_price = 0, $base_price = 0, $base_price_id = 0)
 {
     // create new ticket that's a copy of the existing
     // except a new id of course (and not archived)
     // AND has the new TKT_price associated with it.
     $new_ticket = clone $ticket;
     $new_ticket->set('TKT_ID', 0);
     $new_ticket->set('TKT_deleted', 0);
     $new_ticket->set('TKT_price', $ticket_price);
     $new_ticket->set('TKT_sold', 0);
     // let's get a new ID for this ticket
     $new_ticket->save();
     // we also need to make sure this new ticket gets the same datetime attachments as the archived ticket
     $datetimes_on_existing = $ticket->get_many_related('Datetime');
     $new_ticket = $this->_update_ticket_datetimes($new_ticket, $datetimes_on_existing, array_keys($datetimes_on_existing));
     // $ticket will get archived later b/c we are NOT adding it to the saved_tickets array.
     // if existing $ticket has sold amount, then we need to adjust the qty for the new TKT to = the remaining
     // available.
     if ($ticket->sold() > 0) {
         $new_qty = $ticket->qty() - $ticket->sold();
         $new_ticket->set_qty($new_qty);
     }
     //now we update the prices just for this ticket
     $new_ticket = $this->_add_prices_to_ticket($price_rows, $new_ticket, true);
     //and we update the base price
     $new_ticket = $this->_add_prices_to_ticket(array(), $new_ticket, true, $base_price, $base_price_id);
     return $new_ticket;
 }