public function __construct($admin_page)
 {
     parent::__construct($admin_page);
     require_once EE_MODELS . 'EEM_Price_Type.model.php';
     $this->_PRT = EEM_Price_Type::instance();
     $this->_price_types = $this->_PRT->get_all_deleted_and_undeleted();
 }
 /**
  * This allows setting the $_price_type property to a new price_type object if the incoming args for the
  * new price have a prt_id (or set to default if no prt_id).  This optionally will use any args for price type that is included in the incoming arguments.
  *
  * @since 4.3.0
  * @param int $PRT_ID EE_Price_Type ID
  */
 private function _set_new_price_type($PRT_ID = 0, $args)
 {
     $this->_price_type = empty($PRT_ID) ? EEM_Price_Type::instance()->get_one_by_ID($PRT_ID) : $this->_create_price_type($args);
     //fail safe just in case (so we can be sure to have an price_type).
     if (empty($this->_price_type)) {
         $this->_price_type = $this->_create_price_type($args);
     }
 }
 public function test_add_ticket_to_cart()
 {
     //let's make an interesting ticket, with multiple datetimes, multiple prices etc
     $quantity_purchased = 4;
     /** @type EE_Ticket $ticket */
     $ticket = $this->new_model_obj_with_dependencies('Ticket', array('TKT_price' => '16.5', 'TKT_taxable' => FALSE));
     $base_price_type = EEM_Price_Type::instance()->get_one(array(array('PRT_name' => 'Base Price')));
     $dollar_surcharge_price_type = EEM_Price_Type::instance()->get_one(array(array('PRT_name' => 'Dollar Surcharge')));
     $percent_surcharge_price_type = EEM_Price_Type::instance()->get_one(array(array('PRT_name' => 'Percent Surcharge')));
     $this->assertInstanceOf('EE_Price_Type', $base_price_type);
     $this->assertInstanceOf('EE_Price_Type', $dollar_surcharge_price_type);
     $this->assertInstanceOf('EE_Price_Type', $percent_surcharge_price_type);
     $base_price = $this->new_model_obj_with_dependencies('Price', array('PRC_amount' => 10, 'PRT_ID' => $base_price_type->ID()));
     $dollar_surcharge = $this->new_model_obj_with_dependencies('Price', array('PRC_amount' => 5, 'PRT_ID' => $dollar_surcharge_price_type->ID()));
     $percent_surcharge = $this->new_model_obj_with_dependencies('Price', array('PRC_amount' => 10, 'PRT_ID' => $percent_surcharge_price_type->ID()));
     $ticket->_add_relation_to($base_price, 'Price');
     $ticket->_add_relation_to($dollar_surcharge, 'Price');
     $ticket->_add_relation_to($percent_surcharge, 'Price');
     $this->assertArrayContains($base_price, $ticket->prices());
     $this->assertArrayContains($dollar_surcharge, $ticket->prices());
     $this->assertArrayContains($percent_surcharge, $ticket->prices());
     $event = $this->new_model_obj_with_dependencies('Event');
     $ddt1 = $this->new_model_obj_with_dependencies('Datetime', array('EVT_ID' => $event->ID()));
     $ddt2 = $this->new_model_obj_with_dependencies('Datetime', array('EVT_ID' => $event->ID()));
     $ticket->_add_relation_to($ddt1, 'Datetime');
     $ticket->_add_relation_to($ddt2, 'Datetime');
     $this->assertArrayContains($ddt1, $ticket->datetimes());
     $this->assertArrayContains($ddt2, $ticket->datetimes());
     // reset cart
     $cart = EE_Cart::reset();
     $cart->add_ticket_to_cart($ticket, $quantity_purchased);
     $total_line_item = $cart->get_grand_total();
     $subtotals = $total_line_item->children();
     $this->assertNotEmpty($subtotals);
     $items_purchased = $total_line_item->get_items();
     $this->assertEquals(1, count($items_purchased));
     $item_purchased = array_shift($items_purchased);
     $this->assertEquals($ticket->name(), $item_purchased->name());
     $this->assertEquals($item_purchased->total(), $total_line_item->total());
     $sub_line_items = $item_purchased->children();
     $this->assertEquals(count($ticket->prices()), count($sub_line_items));
     //the first one should be the base price
     $base_price_sli = array_shift($sub_line_items);
     $this->assertEquals($base_price->amount() * 4, $base_price_sli->total());
     $dollar_surcharge_sli = array_shift($sub_line_items);
     $this->assertEquals($dollar_surcharge->amount() * 4, $dollar_surcharge_sli->total());
     $percent_surcharge_sli = array_shift($sub_line_items);
     $this->assertEquals($percent_surcharge->amount(), $percent_surcharge_sli->percent());
     $this->assertEquals(($base_price->amount() + $dollar_surcharge->amount()) * $percent_surcharge->amount() / 100 * $quantity_purchased, $percent_surcharge_sli->total());
     $this->assertEquals($ticket->price() * $quantity_purchased, $cart->get_cart_grand_total());
 }
 /**
  * @group 8964
  * Uses a particular number and quantity that has been shown to cause rounding problems
  * prior to the work on 8964 (specifically, if you had 2 transactions for 1 ticket purchase each
  * the total for both transactions was NOT the same as 1 transaction for 2 ticket purchases)
  */
 function test_recalculate_pre_tax_total__rounding_issues()
 {
     EE_Registry::instance()->load_helper('Line_Item');
     $flat_base_price_type_id = EEM_Price_Type::instance()->get_var(array(array('PRT_name' => 'Base Price')));
     $percent_surcharge_price_type_id = EEM_Price_Type::instance()->get_var(array(array('PRT_name' => 'Percent Surcharge')));
     $base_price = $this->new_model_obj_with_dependencies('Price', array('PRT_ID' => $flat_base_price_type_id, 'PRC_amount' => 21.67));
     $percent_surcharge = $this->new_model_obj_with_dependencies('Price', array('PRT_ID' => $percent_surcharge_price_type_id, 'PRC_amount' => 20));
     $ticket = $this->new_model_obj_with_dependencies('Ticket', array('TKT_price' => $base_price->amount() + $base_price->amount() * $percent_surcharge->amount() / 100, 'TKT_taxable' => false));
     $ticket->_add_relation_to($base_price, 'Price');
     $ticket->_add_relation_to($percent_surcharge, 'Price');
     $event = $this->new_model_obj_with_dependencies('Event');
     $datetime = $this->new_model_obj_with_dependencies('Datetime');
     $ticket->_add_relation_to($datetime, 'Datetime');
     $quantity = 2;
     $total_line_item = EEH_Line_Item::add_ticket_purchase(EEH_Line_Item::create_total_line_item(), $ticket, $quantity);
     $this->assertEquals($ticket->price() * $quantity, $total_line_item->total());
 }
 /**
  * Creates an interesting ticket, with a base price, dollar surcharge, and a percent surcharge,
  * which is for 2 different datetimes.
  * @param array $options {
  *	@type int $dollar_surcharge the dollar surcharge to add to this ticket
  *	@type int $percent_surcharge teh percent surcharge to add to this ticket (value in percent, not in decimal. Eg if it's a 10% surcharge, enter 10.00, not 0.10
  *	@type int $datetimes the number of datetimes for this ticket,
  *	@type int $TKT_price set the TKT_price to this value.
  *	@type int $TKT_taxable set the TKT_taxable to this value.
  * }
  * @return EE_Ticket
  */
 public function new_ticket($options = array())
 {
     // grab ticket price or set to default of 16.50
     $ticket_price = isset($options['ticket_price']) && is_numeric($options['ticket_price']) ? $options['ticket_price'] : 16.5;
     // apply taxes? default = true
     $ticket_taxable = isset($options['ticket_taxable']) ? filter_var($options['ticket_taxable'], FILTER_VALIDATE_BOOLEAN) : true;
     /** @type EE_Ticket $ticket */
     $ticket = $this->new_model_obj_with_dependencies('Ticket', array('TKT_price' => $ticket_price, 'TKT_taxable' => $ticket_taxable));
     $base_price_type = EEM_Price_Type::instance()->get_one(array(array('PRT_name' => 'Base Price')));
     $this->assertInstanceOf('EE_Price_Type', $base_price_type);
     //only associate on the tickets if TKT_price is not included
     if (!isset($options['TKT_price'])) {
         $base_price = $this->new_model_obj_with_dependencies('Price', array('PRC_amount' => 10, 'PRT_ID' => $base_price_type->ID()));
         $ticket->_add_relation_to($base_price, 'Price');
         $this->assertArrayContains($base_price, $ticket->prices());
         if (isset($options['dollar_surcharge'])) {
             $dollar_surcharge_price_type = EEM_Price_Type::instance()->get_one(array(array('PRT_name' => 'Dollar Surcharge')));
             $this->assertInstanceOf('EE_Price_Type', $dollar_surcharge_price_type);
             $dollar_surcharge = $this->new_model_obj_with_dependencies('Price', array('PRC_amount' => $options['dollar_surcharge'], 'PRT_ID' => $dollar_surcharge_price_type->ID()));
             $ticket->_add_relation_to($dollar_surcharge, 'Price');
             $this->assertArrayContains($dollar_surcharge, $ticket->prices());
         }
         if (isset($options['percent_surcharge'])) {
             $percent_surcharge_price_type = EEM_Price_Type::instance()->get_one(array(array('PRT_name' => 'Percent Surcharge')));
             $this->assertInstanceOf('EE_Price_Type', $percent_surcharge_price_type);
             $percent_surcharge = $this->new_model_obj_with_dependencies('Price', array('PRC_amount' => $options['percent_surcharge'], 'PRT_ID' => $percent_surcharge_price_type->ID()));
             $ticket->_add_relation_to($percent_surcharge, 'Price');
             $this->assertArrayContains($percent_surcharge, $ticket->prices());
         }
     }
     if (isset($options['TKT_price'])) {
         $ticket->set('TKT_price', $options['TKT_price']);
         //set the base price
         $base_price = $this->new_model_obj_with_dependencies('Price', array('PRC_amount' => $options['TKT_price'], 'PRT_ID' => $base_price_type->ID()));
         $ticket->_add_relation_to($base_price, 'Price');
         $this->assertArrayContains($base_price, $ticket->prices());
     }
     if (isset($options['TKT_taxable'])) {
         $ticket->set('TKT_taxable', $options['TKT_taxable']);
     }
     // set datetimes, default = 1
     $datetimes = isset($options['datetimes']) ? $options['datetimes'] : 1;
     $event = $this->new_model_obj_with_dependencies('Event');
     for ($i = 0; $i <= $datetimes; $i++) {
         $ddt = $this->new_model_obj_with_dependencies('Datetime', array('EVT_ID' => $event->ID()));
         $ticket->_add_relation_to($ddt, 'Datetime');
         $this->assertArrayContains($ddt, $ticket->datetimes());
     }
     //resave ticket to account for possible field value changes
     $ticket->save();
     return $ticket;
 }
 public function __construct($admin_page)
 {
     parent::__construct($admin_page);
     require_once EE_MODELS . 'EEM_Price_Type.model.php';
     $this->_PRT = EEM_Price_Type::instance();
 }
 /**
  * Tests that if we are joining to a table that has no entries matching the query,
  * but the primary table does that we don't create model objects for the non-existent
  * row, but we do still create model objects for the row that did exist.
  * @group 7634
  */
 function test_get_all_if_related_model_blank_use_nulls()
 {
     $price_sans_price_type = EE_Price::new_instance(array('PRC_name' => 'original', 'PRT_ID' => EEM_Price_Type::instance()->count() + 1));
     $price_sans_price_type->save();
     $fetched_price = EEM_Price::reset()->get_one(array(array('PRC_ID' => $price_sans_price_type->ID()), 'force_join' => array('Price_Type')));
     $this->assertInstanceOf('EE_Price', $fetched_price);
     $this->assertEquals(null, $fetched_price->type_obj());
 }
 /**
  * 		_sort_event_prices_by_type
  *
  * 		@access		private
  * 		@return 		boolean			false on fail
  */
 private function _sort_event_prices_by_type($price_a, $price_b)
 {
     $PRT = EEM_Price_Type::instance();
     if ($price_a->type_obj()->order() == $price_b->type_obj()->order()) {
         return $this->_sort_event_prices_by_order($price_a, $price_b);
     }
     return $price_a->type_obj()->order() < $price_b->type_obj()->order() ? -1 : 1;
 }
 /**
  * 		_delete_price_type
  *		@access protected
  *		@return void
  */
 protected function _delete_price_type()
 {
     //echo '<h3>'. __CLASS__ . '->' . __FUNCTION__ . ' <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span></h3>';
     do_action('AHEE_log', __FILE__, __FUNCTION__, '');
     $PRT = EEM_Price_Type::instance();
     $success = 1;
     //Checkboxes
     if (!empty($this->_req_data['checkbox'])) {
         // if array has more than one element than success message should be plural
         $success = count($this->_req_data['checkbox']) > 1 ? 2 : 1;
         $what = $PRT->item_name($success);
         // cycle thru bulk action checkboxes
         while (list($PRT_ID, $value) = each($this->_req_data['checkbox'])) {
             if (!$PRT->delete_permanently_by_ID($PRT_ID)) {
                 $success = 0;
             }
         }
     }
     $query_args = array('action' => 'price_types');
     $this->_redirect_after_action($success, $what, 'deleted', $query_args);
 }
 /**
  * return the price type object for a given price type ID.
  *
  * @since 4.3.0
  *
  * @param int $PRT_ID the price type id for the price type to attempt to retrieve
  *
  * @return mixed null|EE_Price_Type
  */
 public function get_object_by_id($PRT_ID)
 {
     return EEM_Price_Type::instance()->get_one_by_ID($PRT_ID);
 }
 /**
  * 		This function is a singleton method used to instantiate the EEM_Attendee object
  *
  * 		@access public
  * 		@return EEM_Price_Type instance
  */
 public static function instance()
 {
     // check if instance of EEM_Price_Type already exists
     if (self::$_instance === NULL) {
         // instantiate Price_Type model
         self::$_instance = new self();
     }
     // EEM_Price_Type object
     return self::$_instance;
 }