/**
  *	get_cart_from_reg_url_link
  *	@access public
  *	@return class instance
  */
 public static function get_cart_from_txn(EE_Transaction $transaction)
 {
     $grand_total = $transaction->total_line_item();
     $grand_total->get_items();
     $grand_total->tax_descendants();
     return EE_Cart::instance($grand_total);
 }
 /**
  * Returns what a simple summing of items and taxes for this transaction. This
  * can be used to determine if some more complex line items, like promotions,
  * surcharges, or cancellations occurred (in which case we might want to forget
  * about creating an itemized list of purchases and instead only send the total due)
  * @param EE_Transaction  $transaction
  * @return float
  */
 protected function _sum_items_and_taxes(EE_Transaction $transaction)
 {
     $total_line_item = $transaction->total_line_item();
     $total = 0;
     foreach ($total_line_item->get_items() as $item_line_item) {
         $total += max($item_line_item->total(), 0);
     }
     foreach ($total_line_item->tax_descendants() as $tax_line_item) {
         $total += max($tax_line_item->total(), 0);
     }
     return $total;
 }
 /**
  * This helper method can be used by any incoming data handlers to setup the data correctly.  All that is required is that $this->reg_objs be set.
  * @throws \EE_Error
  */
 protected function _assemble_data()
 {
     //verify that reg_objs is set
     if (!is_array($this->reg_objs) && !reset($this->reg_objs) instanceof EE_Registration) {
         throw new EE_Error(__('In order to assemble the data correctly, the "reg_objs" property must be an array of EE_Registration objects', 'event_espresso'));
     }
     //get all attendee and events associated with the registrations in this transaction
     $events = $event_setup = $evtcache = $tickets = $datetimes = array();
     $answers = $questions = $attendees = $line_items = $registrations = array();
     $total_ticket_count = 0;
     if (!empty($this->reg_objs)) {
         $event_attendee_count = array();
         foreach ($this->reg_objs as $reg) {
             //account for filtered registrations by status.
             if (!empty($this->filtered_reg_status) && $this->filtered_reg_status !== $reg->status_ID()) {
                 continue;
             }
             $evt_id = $reg->event_ID();
             /** @type EE_Ticket $ticket */
             $ticket = $reg->get_first_related('Ticket');
             $relateddatetime = $ticket->datetimes();
             $total_ticket_count++;
             $tickets[$ticket->ID()]['ticket'] = $ticket;
             $tickets[$ticket->ID()]['count'] = is_array($tickets[$ticket->ID()]) && isset($tickets[$ticket->ID()]['count']) ? $tickets[$ticket->ID()]['count'] + 1 : 1;
             $tickets[$ticket->ID()]['att_objs'][$reg->attendee_ID()] = $reg->attendee();
             $tickets[$ticket->ID()]['dtt_objs'] = $relateddatetime;
             $tickets[$ticket->ID()]['reg_objs'][$reg->ID()] = $reg;
             $event = $reg->event();
             $tickets[$ticket->ID()]['EE_Event'] = $event;
             $evtcache[$evt_id] = $event;
             $eventsetup[$evt_id]['reg_objs'][$reg->ID()] = $reg;
             $eventsetup[$evt_id]['tkt_objs'][$ticket->ID()] = $ticket;
             $eventsetup[$evt_id]['att_objs'][$reg->attendee_ID()] = $reg->attendee();
             $event_attendee_count[$evt_id] = isset($event_attendee_count[$evt_id]) ? $event_attendee_count[$evt_id] + 1 : 0;
             $attendees[$reg->attendee_ID()]['line_ref'][] = $evt_id;
             $attendees[$reg->attendee_ID()]['att_obj'] = $reg->attendee();
             $attendees[$reg->attendee_ID()]['reg_objs'][$reg->ID()] = $reg;
             //$attendees[ $reg->attendee_ID() ]['registration_id'] = $reg->ID();
             $attendees[$reg->attendee_ID()]['attendee_email'] = $reg->attendee() instanceof EE_Attendee ? $reg->attendee()->email() : '';
             $attendees[$reg->attendee_ID()]['tkt_objs'][$ticket->ID()] = $ticket;
             $attendees[$reg->attendee_ID()]['evt_objs'][$evt_id] = $event;
             //registrations
             $registrations[$reg->ID()]['tkt_obj'] = $ticket;
             $registrations[$reg->ID()]['evt_obj'] = $event;
             $registrations[$reg->ID()]['reg_obj'] = $reg;
             $registrations[$reg->ID()]['att_obj'] = $reg->attendee();
             //set up answer objects
             $rel_ans = $reg->get_many_related('Answer');
             foreach ($rel_ans as $ansid => $answer) {
                 if (!isset($questions[$ansid])) {
                     $questions[$ansid] = $answer->get_first_related('Question');
                 }
                 $answers[$ansid] = $answer;
                 $registrations[$reg->ID()]['ans_objs'][$ansid] = $answer;
             }
             foreach ($relateddatetime as $dtt_id => $datetime) {
                 $eventsetup[$evt_id]['dtt_objs'][$dtt_id] = $datetime;
                 $registrations[$reg->ID()]['dtt_objs'][$dtt_id] = $datetime;
                 if (isset($datetimes[$dtt_id])) {
                     continue;
                     //already have this info in the datetimes array.
                 }
                 $datetimes[$dtt_id]['tkt_objs'][] = $ticket;
                 $datetimes[$dtt_id]['datetime'] = $datetime;
                 $datetimes[$dtt_id]['evt_objs'][$evt_id] = $event;
                 $datetimes[$dtt_id]['reg_objs'][$reg->ID()] = $reg;
             }
         }
         //let's loop through the unique event=>reg items and setup data on them
         if (!empty($eventsetup)) {
             foreach ($eventsetup as $evt_id => $items) {
                 if ($this->txn instanceof EE_Transaction) {
                     $ticket_line_items_for_event = EEM_Line_Item::instance()->get_all(array(array('Ticket.Datetime.EVT_ID' => $evt_id, 'TXN_ID' => $this->txn->ID()), 'default_where_conditions' => 'none'));
                 } else {
                     $ticket_line_items_for_event = array();
                 }
                 $events[$evt_id] = array('ID' => $evt_id, 'event' => $evtcache[$evt_id], 'name' => $evtcache[$evt_id] instanceof EE_Event ? $evtcache[$evt_id]->name() : '', 'total_attendees' => $event_attendee_count[$evt_id], 'reg_objs' => $items['reg_objs'], 'tkt_objs' => $items['tkt_objs'], 'att_objs' => $items['att_objs'], 'dtt_objs' => isset($items['dtt_objs']) ? $items['dtt_objs'] : array(), 'line_items' => $ticket_line_items_for_event);
                 //make sure the tickets have the line items setup for them.
                 foreach ($ticket_line_items_for_event as $line_id => $line_item) {
                     if ($line_item instanceof EE_Line_Item) {
                         $tickets[$line_item->ticket()->ID()]['line_item'] = $line_item;
                         $tickets[$line_item->ticket()->ID()]['sub_line_items'] = $line_item->children();
                         $line_items[$line_item->ID()]['children'] = $line_item->children();
                         $line_items[$line_item->ID()]['EE_Ticket'] = $line_item->ticket();
                     }
                 }
             }
         }
         $this->grand_total_line_item = $this->txn instanceof EE_Transaction ? $this->txn->total_line_item() : null;
     }
     //lets set the attendees and events properties
     $this->attendees = $attendees;
     $this->events = $events;
     $this->tickets = $tickets;
     $this->line_items_with_children = $line_items;
     $this->datetimes = $datetimes;
     $this->questions = $questions;
     $this->answers = $answers;
     $this->total_ticket_count = $total_ticket_count;
     $this->registrations = $registrations;
     if ($this->txn instanceof EE_Transaction) {
         $this->tax_line_items = $this->txn->tax_items();
         $this->additional_line_items = $this->txn->non_ticket_line_items();
         $this->payments = $this->txn->payments();
         //setup primary registration if we have a single transaction object to work with
         //let's get just the primary_attendee_data!  First we get the primary registration object.
         $primary_reg = $this->txn->primary_registration();
         // verify
         if ($primary_reg instanceof EE_Registration) {
             // get attendee object
             if ($primary_reg->attendee() instanceof EE_Attendee) {
                 //now we can setup the primary_attendee_data array
                 $this->primary_attendee_data = array('registration_id' => $primary_reg->ID(), 'att_obj' => $primary_reg->attendee(), 'reg_obj' => $primary_reg, 'primary_att_obj' => $primary_reg->attendee(), 'primary_reg_obj' => $primary_reg);
             } else {
                 EE_Error::add_error(__('Incoming data does not have a valid Attendee object for the primary registrant.', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__);
             }
         } else {
             EE_Error::add_error(__('Incoming data does not have a valid Registration object for the primary registrant.', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__);
         }
     }
 }
 /**
  * Updates the registration' final prices based on the current line item tree (taking into account
  * discounts, taxes, and other line items unrelated to tickets.)
  * @param EE_Transaction $transaction
  * @param boolean $save_regs whether to immediately save registrations in this function or not
  * @return void
  */
 public function update_registration_final_prices($transaction, $save_regs = true)
 {
     $reg_final_price_per_ticket_line_item = EEH_Line_Item::calculate_reg_final_prices_per_line_item($transaction->total_line_item());
     foreach ($transaction->registrations() as $registration) {
         $line_item = EEM_Line_Item::instance()->get_line_item_for_registration($registration);
         if (isset($reg_final_price_per_ticket_line_item[$line_item->ID()])) {
             $registration->set_final_price($reg_final_price_per_ticket_line_item[$line_item->ID()]);
             if ($save_regs) {
                 $registration->save();
             }
         }
     }
     //and make sure there's no rounding problem
     $this->fix_reg_final_price_rounding_issue($transaction);
 }
 public function send_invoice($download = FALSE)
 {
     $template_args = array();
     $EE = EE_Registry::instance();
     //allow the request to override the default theme defined in the invoice settings
     $theme_requested = isset($_REQUEST['theme']) && $_REQUEST['theme'] > 0 && $_REQUEST['theme'] < 8 ? absint($_REQUEST['theme']) : null;
     $themes = array(1 => "simple.css", 2 => "bauhaus.css", 3 => "ejs.css", 4 => "horizon.css", 5 => "lola.css", 6 => "tranquility.css", 7 => "union.css");
     //Get the CSS file
     if (isset($themes[$theme_requested])) {
         $template_args['invoice_css'] = $themes[$theme_requested];
     } else {
         $template_args['invoice_css'] = $this->invoice_payment_method->get_extra_meta('legacy_invoice_css', TRUE, 'simple.css');
     }
     if (is_dir(EVENT_ESPRESSO_GATEWAY_DIR . '/invoice')) {
         $template_args['base_url'] = EVENT_ESPRESSO_GATEWAY_URL . 'Invoice/lib/templates/';
     } else {
         $template_args['base_url'] = EE_GATEWAYS . '/Invoice/lib/templates/';
     }
     $primary_attendee = $this->transaction->primary_registration()->attendee();
     $template_args['organization'] = $EE->CFG->organization->get_pretty('name');
     $template_args['street'] = empty($EE->CFG->organization->address_2) ? $EE->CFG->organization->get_pretty('address_1') : $EE->CFG->organization->get_pretty('address_1') . '<br>' . $EE->CFG->organization->get_pretty('address_2');
     $template_args['city'] = $EE->CFG->organization->get_pretty('city');
     $template_args['state'] = EE_Registry::instance()->load_model('State')->get_one_by_ID($EE->CFG->organization->STA_ID);
     $template_args['country'] = EE_Registry::instance()->load_model('Country')->get_one_by_ID($EE->CFG->organization->CNT_ISO);
     $template_args['zip'] = $EE->CFG->organization->get_pretty('zip');
     $template_args['email'] = $EE->CFG->organization->get_pretty('email');
     $template_args['registration_code'] = $this->registration->reg_code();
     $template_args['registration_date'] = $this->registration->date();
     $template_args['name'] = $primary_attendee->full_name();
     $template_args['attendee_address'] = $primary_attendee->address();
     $template_args['attendee_address2'] = $primary_attendee->address2();
     $template_args['attendee_city'] = $primary_attendee->city();
     $attendee_state = $primary_attendee->state_obj();
     if ($attendee_state) {
         $attendee_state_name = $attendee_state->name();
     } else {
         $attendee_state_name = '';
     }
     $template_args['attendee_state'] = $attendee_state_name;
     $template_args['attendee_zip'] = $primary_attendee->zip();
     $template_args['ship_name'] = $template_args['name'];
     $template_args['ship_address'] = $template_args['attendee_address'];
     $template_args['ship_city'] = $template_args['attendee_city'];
     $template_args['ship_state'] = $template_args['attendee_state'];
     $template_args['ship_zip'] = $template_args['attendee_zip'];
     $template_args['total_cost'] = number_format($this->transaction->total(), 2, '.', '');
     $template_args['transaction'] = $this->transaction;
     $template_args['amount_pd'] = $this->transaction->paid();
     $template_args['amount_owed'] = $this->transaction->total() - $this->transaction->paid();
     $template_args['payments'] = $this->transaction->approved_payments();
     $template_args['net_total'] = '';
     $template_args['edit_reg_info_url'] = $this->registration->edit_attendee_information_url();
     $template_args['retry_payment_url'] = $this->registration->payment_overview_url();
     $template_args['show_line_item_description'] = $this->check_if_any_line_items_have_a_description($this->transaction->total_line_item());
     if ($template_args['amount_pd'] != $template_args['total_cost']) {
         //$template_args['net_total'] = $this->espressoInvoiceTotals( __('SubTotal', 'event_espresso'), $this->transaction->total());//$this->session_data['cart']['REG']['sub_total']);
         $tax_items = $this->transaction->tax_items();
         if (!empty($tax_items)) {
             foreach ($tax_items as $tax) {
                 $template_args['net_total'] .= $this->espressoInvoiceTotals($tax->name(), $tax->total());
             }
         }
         $difference = $template_args['amount_pd'] - $template_args['total_cost'];
         if ($difference < 0) {
             $text = __('Discount', 'event_espresso');
         } else {
             $text = __('Extra', 'event_espresso');
         }
         $template_args['discount'] = $this->espressoInvoiceTotals($text, $difference);
     }
     $template_args['currency_symbol'] = $EE->CFG->currency->sign;
     $template_args['template_payment_instructions'] = wpautop(stripslashes_deep(html_entity_decode($this->invoice_payment_method->get_extra_meta('pdf_instructions', TRUE), ENT_QUOTES)));
     $template_args['shameless_plug'] = apply_filters('FHEE_Invoice__send_invoice__shameless_plug', true);
     if (isset($_GET['receipt'])) {
         //receipt-specific stuff
         $events_for_txn = EEM_Event::instance()->get_all(array(array('Registration.TXN_ID' => $this->transaction->ID())));
         $ticket_line_items_per_event = array();
         $registrations_per_line_item = array();
         $venues_for_events = array();
         foreach ($events_for_txn as $event_id => $event) {
             $line_items_for_this_event = EEM_Line_Item::instance()->get_all(array(array('Ticket.Datetime.EVT_ID' => $event_id, 'TXN_ID' => $this->transaction->ID())));
             $ticket_line_items_per_event[$event_id] = $line_items_for_this_event;
             foreach ($line_items_for_this_event as $line_item_id => $line_item) {
                 $ticket = $line_item->ticket();
                 $registrations_for_this_ticket = EEM_Registration::instance()->get_all(array(array('TKT_ID' => $ticket->ID(), 'TXN_ID' => $this->transaction->ID())));
                 $registrations_per_line_item[$line_item_id] = $registrations_for_this_ticket;
             }
             $venues_for_events = array_merge($venues_for_events, $event->venues());
         }
         $tax_total_line_item = EEM_Line_Item::instance()->get_one(array(array('TXN_ID' => $this->transaction->ID(), 'LIN_type' => EEM_Line_Item::type_tax_sub_total)));
         $questions_to_skip = array(EEM_Attendee::system_question_fname, EEM_Attendee::system_question_lname, EEM_Attendee::system_question_email);
         $template_args['events_for_txn'] = $events_for_txn;
         $template_args['ticket_line_items_per_event'] = $ticket_line_items_per_event;
         $template_args['registrations_per_line_item'] = $registrations_per_line_item;
         $template_args['venues_for_events'] = $venues_for_events;
         $template_args['tax_total_line_item'] = $tax_total_line_item;
         $template_args['questions_to_skip'] = $questions_to_skip;
         //			d($template_args);
         $template_args['download_link'] = $this->registration->receipt_url('download');
     } else {
         //it's just an invoice we're accessing
         $template_args['download_link'] = $this->registration->invoice_url('download');
     }
     //Get the HTML as an object
     $templates_relative_path = 'modules/gateways/Invoice/lib/templates/';
     $template_header = EEH_Template::locate_template($templates_relative_path . 'invoice_header.template.php', $template_args, TRUE, TRUE);
     if (isset($_GET['receipt'])) {
         $template_body = EEH_Template::locate_template($templates_relative_path . 'receipt_body.template.php', $template_args, TRUE, TRUE);
     } else {
         $template_body = EEH_Template::locate_template($templates_relative_path . 'invoice_body.template.php', $template_args, TRUE, TRUE);
     }
     $template_footer = EEH_Template::locate_template($templates_relative_path . 'invoice_footer.template.php', $template_args, TRUE, TRUE);
     $copies = !empty($_REQUEST['copies']) ? $_REQUEST['copies'] : 1;
     $content = $this->espresso_replace_invoice_shortcodes($template_header);
     for ($x = 1; $x <= $copies; $x++) {
         $content .= $this->espresso_replace_invoice_shortcodes($template_body);
     }
     $content .= $this->espresso_replace_invoice_shortcodes($template_footer);
     //Check if debugging or mobile is set
     if (!empty($_REQUEST['html'])) {
         echo $content;
         exit(0);
     }
     $invoice_name = $template_args['organization'] . ' ' . __('Invoice #', 'event_espresso') . $template_args['registration_code'] . __(' for ', 'event_espresso') . $template_args['name'];
     $invoice_name = str_replace(' ', '_', $invoice_name);
     //Create the PDF
     if (array_key_exists('html', $_GET)) {
         echo $content;
     } else {
         //only load dompdf if nobody else has yet...
         if (!defined('DOMPDF_DIR')) {
             define('DOMPDF_ENABLE_REMOTE', TRUE);
             define('DOMPDF_ENABLE_JAVASCRIPT', FALSE);
             define('DOMPDF_ENABLE_CSS_FLOAT', TRUE);
             require_once EE_THIRD_PARTY . 'dompdf/dompdf_config.inc.php';
         }
         $dompdf = new DOMPDF();
         $dompdf->load_html($content);
         $dompdf->render();
         $dompdf->stream($invoice_name . ".pdf", array('Attachment' => $download));
     }
     exit(0);
 }
 /**
  * txn_details_meta_box
  * generates HTML for the Transaction main meta box
  *
  * @access public
  *	@return void
  */
 public function txn_details_meta_box()
 {
     $this->_set_transaction_object();
     $this->_template_args['TXN_ID'] = $this->_transaction->ID();
     $this->_template_args['attendee'] = $this->_transaction->primary_registration() instanceof EE_Registration ? $this->_transaction->primary_registration()->attendee() : null;
     //get line table
     EEH_Autoloader::register_line_item_display_autoloaders();
     $Line_Item_Display = new EE_Line_Item_Display('admin_table', 'EE_Admin_Table_Line_Item_Display_Strategy');
     $this->_template_args['line_item_table'] = $Line_Item_Display->display_line_item($this->_transaction->total_line_item());
     $this->_template_args['REG_code'] = $this->_transaction->get_first_related('Registration')->get('REG_code');
     // process taxes
     $taxes = $this->_transaction->get_many_related('Line_Item', array(array('LIN_type' => EEM_Line_Item::type_tax)));
     $this->_template_args['taxes'] = !empty($taxes) ? $taxes : FALSE;
     $this->_template_args['grand_total'] = EEH_Template::format_currency($this->_transaction->get('TXN_total'), FALSE, FALSE);
     $this->_template_args['grand_raw_total'] = $this->_transaction->get('TXN_total');
     $this->_template_args['TXN_status'] = $this->_transaction->get('STS_ID');
     //		$txn_status_class = 'status-' . $this->_transaction->get('STS_ID');
     // process payment details
     $payments = $this->_transaction->get_many_related('Payment');
     if (!empty($payments)) {
         $this->_template_args['payments'] = $payments;
         $this->_template_args['existing_reg_payments'] = $this->_get_registration_payment_IDs($payments);
     } else {
         $this->_template_args['payments'] = false;
         $this->_template_args['existing_reg_payments'] = array();
     }
     $this->_template_args['edit_payment_url'] = add_query_arg(array('action' => 'edit_payment'), TXN_ADMIN_URL);
     $this->_template_args['delete_payment_url'] = add_query_arg(array('action' => 'espresso_delete_payment'), TXN_ADMIN_URL);
     if (isset($txn_details['invoice_number'])) {
         $this->_template_args['txn_details']['invoice_number']['value'] = $this->_template_args['REG_code'];
         $this->_template_args['txn_details']['invoice_number']['label'] = __('Invoice Number', 'event_espresso');
     }
     $this->_template_args['txn_details']['registration_session']['value'] = $this->_transaction->get_first_related('Registration')->get('REG_session');
     $this->_template_args['txn_details']['registration_session']['label'] = __('Registration Session', 'event_espresso');
     $this->_template_args['txn_details']['ip_address']['value'] = isset($this->_session['ip_address']) ? $this->_session['ip_address'] : '';
     $this->_template_args['txn_details']['ip_address']['label'] = __('Transaction placed from IP', 'event_espresso');
     $this->_template_args['txn_details']['user_agent']['value'] = isset($this->_session['user_agent']) ? $this->_session['user_agent'] : '';
     $this->_template_args['txn_details']['user_agent']['label'] = __('Registrant User Agent', 'event_espresso');
     $reg_steps = '<ul>';
     foreach ($this->_transaction->reg_steps() as $reg_step => $reg_step_status) {
         if ($reg_step_status === true) {
             $reg_steps .= '<li style="color:#70cc50">' . sprintf(__('%1$s : Completed', 'event_espresso'), ucwords(str_replace('_', ' ', $reg_step))) . '</li>';
         } else {
             if (is_numeric($reg_step_status) && $reg_step_status !== false) {
                 $reg_steps .= '<li style="color:#2EA2CC">' . sprintf(__('%1$s : Initiated %2$s', 'event_espresso'), ucwords(str_replace('_', ' ', $reg_step)), gmdate(get_option('date_format') . ' ' . get_option('time_format'), $reg_step_status + get_option('gmt_offset') * HOUR_IN_SECONDS)) . '</li>';
             } else {
                 $reg_steps .= '<li style="color:#E76700">' . sprintf(__('%1$s : Never Initiated', 'event_espresso'), ucwords(str_replace('_', ' ', $reg_step))) . '</li>';
             }
         }
     }
     $reg_steps .= '</ul>';
     $this->_template_args['txn_details']['reg_steps']['value'] = $reg_steps;
     $this->_template_args['txn_details']['reg_steps']['label'] = __('Registration Step Progress', 'event_espresso');
     $this->_get_registrations_to_apply_payment_to();
     $this->_get_payment_methods($payments);
     $this->_get_payment_status_array();
     $this->_get_reg_status_selection();
     //sets up the template args for the reg status array for the transaction.
     $this->_template_args['transaction_form_url'] = add_query_arg(array('action' => 'edit_transaction', 'process' => 'transaction'), TXN_ADMIN_URL);
     $this->_template_args['apply_payment_form_url'] = add_query_arg(array('page' => 'espresso_transactions', 'action' => 'espresso_apply_payment'), WP_AJAX_URL);
     $this->_template_args['delete_payment_form_url'] = add_query_arg(array('page' => 'espresso_transactions', 'action' => 'espresso_delete_payment'), WP_AJAX_URL);
     // 'espresso_delete_payment_nonce'
     $template_path = TXN_TEMPLATE_PATH . 'txn_admin_details_main_meta_box_txn_details.template.php';
     echo EEH_Template::display_template($template_path, $this->_template_args, TRUE);
 }