/**
  * This attaches a list of given prices to a ticket.
  * Note we dont' have to worry about ever removing relationships (or archiving prices) because if there is a change in price information on a ticket, a new ticket is created anyways so the archived ticket will retain the old price info and prices are automatically "archived" via the ticket.
  *
  * @access  private
  * @param array  	$prices  	Array of prices from the form.
  * @param EE_Ticket $ticket  	EE_Ticket object that prices are being attached to.
  * @param bool 		$new_prices Whether attach existing incoming prices or create new ones.
  * @return  void
  */
 private function _add_prices_to_ticket($prices, EE_Ticket $ticket, $new_prices = FALSE)
 {
     foreach ($prices as $row => $prc) {
         $PRC_values = array('PRC_ID' => !empty($prc['PRC_ID']) ? $prc['PRC_ID'] : NULL, 'PRT_ID' => !empty($prc['PRT_ID']) ? $prc['PRT_ID'] : NULL, 'PRC_amount' => !empty($prc['PRC_amount']) ? $prc['PRC_amount'] : 0, 'PRC_name' => !empty($prc['PRC_name']) ? $prc['PRC_name'] : '', 'PRC_desc' => !empty($prc['PRC_desc']) ? $prc['PRC_desc'] : '', 'PRC_is_default' => 0, 'PRC_order' => $row);
         if ($new_prices || empty($PRC_values['PRC_ID'])) {
             $PRC_values['PRC_ID'] = 0;
             $PRC = EE_Registry::instance()->load_class('Price', array($PRC_values), FALSE, FALSE);
         } else {
             $PRC = EE_Registry::instance()->load_model('Price')->get_one_by_ID($prc['PRC_ID']);
             //update this price with new values
             foreach ($PRC_values as $field => $newprc) {
                 $PRC->set($field, $newprc);
             }
             $PRC->save();
         }
         $ticket->_add_relation_to($PRC, 'Price');
     }
 }
 /**
  * This attaches a list of given prices to a ticket.
  * Note we dont' have to worry about ever removing relationships (or archiving prices) because if there is a change in price information on a ticket, a new ticket is created anyways so the archived ticket will retain the old price info and prices are automatically "archived" via the ticket.
  *
  * @access  private
  * @param array  	$prices  	Array of prices from the form.
  * @param EE_Ticket $ticket  	EE_Ticket object that prices are being attached to.
  * @param bool 		$new_prices Whether attach existing incoming prices or create new ones.
  * @param int|bool 		$base_price if FALSE then NOT doing a base price add.
  * @param int|bool 		$base_price_id  if present then this is the base_price_id being updated.
  * @return EE_Ticket
  */
 protected function _add_prices_to_ticket($prices = array(), EE_Ticket $ticket, $new_prices = FALSE, $base_price = FALSE, $base_price_id = FALSE)
 {
     //let's just get any current prices that may exist on the given ticket so we can remove any prices that got trashed in this session.
     $current_prices_on_ticket = $base_price !== FALSE ? $ticket->base_price(TRUE) : $ticket->price_modifiers();
     $updated_prices = array();
     // if $base_price ! FALSE then updating a base price.
     if ($base_price !== FALSE) {
         $prices[1] = array('PRC_ID' => $new_prices || $base_price_id === 1 ? NULL : $base_price_id, 'PRT_ID' => 1, 'PRC_amount' => $base_price, 'PRC_name' => $ticket->get('TKT_name'), 'PRC_desc' => $ticket->get('TKT_description'));
     }
     //possibly need to save tkt
     if (!$ticket->ID()) {
         $ticket->save();
     }
     foreach ($prices as $row => $prc) {
         $prt_id = !empty($prc['PRT_ID']) ? $prc['PRT_ID'] : NULL;
         if (empty($prt_id)) {
             continue;
         }
         //prices MUST have a price type id.
         $PRC_values = array('PRC_ID' => !empty($prc['PRC_ID']) ? $prc['PRC_ID'] : NULL, 'PRT_ID' => $prt_id, 'PRC_amount' => !empty($prc['PRC_amount']) ? $prc['PRC_amount'] : 0, 'PRC_name' => !empty($prc['PRC_name']) ? $prc['PRC_name'] : '', 'PRC_desc' => !empty($prc['PRC_desc']) ? $prc['PRC_desc'] : '', 'PRC_is_default' => false, 'PRC_order' => $row);
         if ($new_prices || empty($PRC_values['PRC_ID'])) {
             $PRC_values['PRC_ID'] = 0;
             $PRC = EE_Registry::instance()->load_class('Price', array($PRC_values), FALSE, FALSE);
         } else {
             $PRC = EE_Registry::instance()->load_model('Price')->get_one_by_ID($prc['PRC_ID']);
             //update this price with new values
             foreach ($PRC_values as $field => $newprc) {
                 $PRC->set($field, $newprc);
             }
         }
         $PRC->save();
         $prcid = $PRC->ID();
         $updated_prices[$prcid] = $PRC;
         $ticket->_add_relation_to($PRC, 'Price');
     }
     //now let's remove any prices that got removed from the ticket
     if (!empty($current_prices_on_ticket)) {
         $current = array_keys($current_prices_on_ticket);
         $updated = array_keys($updated_prices);
         $prices_to_remove = array_diff($current, $updated);
         if (!empty($prices_to_remove)) {
             foreach ($prices_to_remove as $prc_id) {
                 $p = $current_prices_on_ticket[$prc_id];
                 $ticket->_remove_relation_to($p, 'Price');
                 //delete permanently the price
                 $p->delete_permanently();
             }
         }
     }
     return $ticket;
 }
 /**
  * This handles connecting a ticket to the datetime and price object that's been generated.
  *
  * @since 4.3.0
  *
  * @param EE_Ticket $tkt
  * @param array $args incoming arguments from caller for specifying overrides.
  *
  * @return EE_Ticket
  */
 private function _maybe_chained(EE_Ticket $tkt, $args)
 {
     if ($this->_chained) {
         if (empty($this->_datetime)) {
             $DTT_ID = isset($this->_special_args['DTT_ID']) ? $this->_special_args['DTT_ID'] : 0;
             $this->_set_new_datetime($DTT_ID);
         }
         if (empty($this->_price)) {
             $PRC_ID = isset($this->_special_args['PRC_ID']) ? $this->_special_args['PRC_ID'] : 0;
             $this->_set_new_price($PRC_ID);
         }
         //add relation to datetime
         $tkt->_add_relation_to($this->_datetime, 'Datetime');
         //add relation to price
         $tkt->_add_relation_to($this->_price, 'Price');
         $tkt->save();
         return $tkt;
     }
     return $tkt;
 }