| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace EventEspresso\core\domain\services\registration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use EE_Error; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use EE_Event; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use EE_Line_Item; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use EE_Registration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use EE_Registry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use EE_Ticket; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use EE_Transaction; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use EEH_Line_Item; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use EEM_Registration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use EventEspresso\core\domain\entities\RegCode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use EventEspresso\core\domain\entities\RegUrlLink; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use EventEspresso\core\domain\services\DomainService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use EventEspresso\core\exceptions\UnexpectedEntityException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use OutOfRangeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * Class CreateRegistrationService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * Description | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @package       Event Espresso | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @author        Brent Christensen | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @since         4.9.1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | class CreateRegistrationService extends DomainService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param EE_Event        $event | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param EE_Transaction  $transaction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param EE_Ticket       $ticket | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param EE_Line_Item    $ticket_line_item | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param                 $reg_count | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param                 $reg_group_size | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param string          $reg_status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @return EE_Registration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @throws OutOfRangeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @throws EE_Error | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @throws UnexpectedEntityException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     public function create( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         EE_Event $event, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         EE_Transaction $transaction, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         EE_Ticket $ticket, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         EE_Line_Item $ticket_line_item, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $reg_count, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $reg_group_size, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $reg_status = EEM_Registration::status_id_incomplete | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $registrations = $transaction->registrations(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $reg_count = $reg_count ? $reg_count : count($registrations) + 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $reg_url_link = new RegUrlLink($reg_count, $ticket_line_item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $reg_code = new RegCode($reg_url_link, $transaction, $ticket); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         // generate new EE_Registration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $registration = EE_Registration::new_instance( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 'EVT_ID'          => $event->ID(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 'TXN_ID'          => $transaction->ID(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 'TKT_ID'          => $ticket->ID(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 'STS_ID'          => $reg_status, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 'REG_final_price' => $this->resolveFinalPrice($transaction, $ticket, $ticket_line_item), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 'REG_session'     => EE_Registry::instance()->SSN->id(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 'REG_count'       => $reg_count, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 'REG_group_size'  => $reg_group_size ? $reg_group_size : $this->incrementRegGroupSize($registrations), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 'REG_url_link'    => $reg_url_link, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 'REG_code'        => $reg_code, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         if (! $registration instanceof EE_Registration) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             throw new UnexpectedEntityException($registration, 'EE_Registration'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         // save registration so that we have an ID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $registration->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         // track reservation on reg but don't adjust ticket and datetime reserved counts | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         // because that is done as soon as the tickets are added/removed from the cart | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $registration->reserve_ticket(false, 'CreateRegistrationService:' . __LINE__); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $registration->_add_relation_to($event, 'Event', array(), $event->ID()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $registration->_add_relation_to($ticket, 'Ticket', array(), $ticket->ID()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $transaction->_add_relation_to($registration, 'Registration', array(), $registration->ID()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $registration->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         return $registration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @param EE_Transaction $transaction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @param EE_Ticket      $ticket | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param EE_Line_Item   $ticket_line_item | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @return float | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @throws EE_Error | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @throws OutOfRangeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     protected function resolveFinalPrice( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         EE_Transaction $transaction, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         EE_Ticket $ticket, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         EE_Line_Item $ticket_line_item | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $final_price = EEH_Line_Item::calculate_final_price_for_ticket_line_item( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $transaction->total_line_item(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $ticket_line_item | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $final_price = $final_price !== null ? $final_price : $ticket->get_ticket_total_with_taxes(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         return (float) $final_price; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @param  EE_Registration[] $registrations | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @param  boolean           $update_existing_registrations | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @throws EE_Error | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 116 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 117 |  |  |     protected function incrementRegGroupSize(array $registrations, $update_existing_registrations = true) | 
            
                                                        
            
                                    
            
            
                | 118 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |         $new_reg_group_size = count($registrations) + 1; | 
            
                                                        
            
                                    
            
            
                | 120 |  |  |         if ($update_existing_registrations) { | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |             foreach ($registrations as $registration) { | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |                 if ($registration instanceof EE_Registration) { | 
            
                                                        
            
                                    
            
            
                | 123 |  |  |                     $registration->set_group_size($new_reg_group_size); | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |                     $registration->save(); | 
            
                                                        
            
                                    
            
            
                | 125 |  |  |                 } | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 127 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 128 |  |  |         return $new_reg_group_size; | 
            
                                                        
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 130 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 131 |  |  |  |