/**
  * Given the grand total line item and a ticket, finds the event sub-total
  * line item the ticket's purchase should be added onto
  *
  * @access public
  * @param EE_Line_Item $grand_total the grand total line item
  * @param EE_Ticket $ticket
  * @throws \EE_Error
  * @return EE_Line_Item
  */
 public static function get_event_line_item_for_ticket(EE_Line_Item $grand_total, EE_Ticket $ticket)
 {
     $first_datetime = $ticket->first_datetime();
     if (!$first_datetime instanceof EE_Datetime) {
         throw new EE_Error(sprintf(__('The supplied ticket (ID %d) has no datetimes', 'event_espresso'), $ticket->ID()));
     }
     $event = $first_datetime->event();
     if (!$event instanceof EE_Event) {
         throw new EE_Error(sprintf(__('The supplied ticket (ID %d) has no event data associated with it.', 'event_espresso'), $ticket->ID()));
     }
     $event_line_item = NULL;
     $found = false;
     foreach (EEH_Line_Item::get_event_subtotals($grand_total) as $event_line_item) {
         // default event subtotal, we should only ever find this the first time this method is called
         if (!$event_line_item->OBJ_ID()) {
             // let's use this! but first... set the event details
             EEH_Line_Item::set_event_subtotal_details($event_line_item, $event);
             $found = true;
             break;
         } else {
             if ($event_line_item->OBJ_ID() === $event->ID()) {
                 // found existing line item for this event in the cart, so break out of loop and use this one
                 $found = true;
                 break;
             }
         }
     }
     if (!$found) {
         //there is no event sub-total yet, so add it
         $pre_tax_subtotal = EEH_Line_Item::get_pre_tax_subtotal($grand_total);
         // create a new "event" subtotal below that
         $event_line_item = EEH_Line_Item::create_event_subtotal($pre_tax_subtotal, null, $event);
         // and set the event details
         EEH_Line_Item::set_event_subtotal_details($event_line_item, $event);
     }
     return $event_line_item;
 }
 /**
  * Gets the event line item
  * @param EE_Line_Item $grand_total
  * @param EE_Event $event
  * @return EE_Line_Item for the event subtotal which is a child of $grand_total
  */
 public static function get_event_line_item(EE_Line_Item $grand_total, $event)
 {
     /** @type EE_Event $event */
     $event = EEM_Event::instance()->ensure_is_obj($event, true);
     $event_line_item = NULL;
     $found = false;
     foreach (EEH_Line_Item::get_event_subtotals($grand_total) as $event_line_item) {
         // default event subtotal, we should only ever find this the first time this method is called
         if (!$event_line_item->OBJ_ID()) {
             // let's use this! but first... set the event details
             EEH_Line_Item::set_event_subtotal_details($event_line_item, $event);
             $found = true;
             break;
         } else {
             if ($event_line_item->OBJ_ID() === $event->ID()) {
                 // found existing line item for this event in the cart, so break out of loop and use this one
                 $found = true;
                 break;
             }
         }
     }
     if (!$found) {
         //there is no event sub-total yet, so add it
         $pre_tax_subtotal = EEH_Line_Item::get_pre_tax_subtotal($grand_total);
         // create a new "event" subtotal below that
         $event_line_item = EEH_Line_Item::create_event_subtotal($pre_tax_subtotal, null, $event);
         // and set the event details
         EEH_Line_Item::set_event_subtotal_details($event_line_item, $event);
     }
     return $event_line_item;
 }