def init_cart            
      # Check if the cart ID is defined and that it exists in the database
      create_new_invoice = false
      if session[:cart_id]
        @invoice = Caboose::Invoice.where(:id => session[:cart_id]).first
        create_new_invoice = true if @invoice.nil? || @invoice.status != 'cart'                    
      else                        
        create_new_invoice = true                         
      end

      if create_new_invoice # Create an invoice to associate with the session        
        @invoice = Caboose::Invoice.new        
        @invoice.site_id          = @site ? @site.id : nil
        @invoice.status           = Caboose::Invoice::STATUS_CART
        @invoice.financial_status = Caboose::Invoice::STATUS_PENDING
        @invoice.date_created     = DateTime.now
        @invoice.referring_site   = request.env['HTTP_REFERER']
        @invoice.landing_page     = request.fullpath
        @invoice.landing_page_ref = params[:ref] || nil
        @invoice.payment_terms    = @site.store_config.default_payment_terms
        @invoice.save
        
        InvoiceLog.create(
          :invoice_id     => @invoice.id,          
          :user_id        => logged_in_user.id,
          :date_logged    => DateTime.now.utc,
          :invoice_action => InvoiceLog::ACTION_INVOICE_CREATED                
        )
        
        # Save the cart ID in the session
        session[:cart_id] = @invoice.id
      end                  
    end
def init_cart            
      # Check if the cart ID is defined and that it exists in the database
      create_new_invoice = false
      if session[:cart_id]
        @invoice = Caboose::Invoice.where(:id => session[:cart_id]).first
        create_new_invoice = true if @invoice.nil? || @invoice.status != 'cart'                    
      else                        
        create_new_invoice = true                         
      end

      if create_new_invoice # Create an invoice to associate with the session        
        @invoice = Caboose::Invoice.new        
        @invoice.site_id          = @site ? @site.id : nil
        @invoice.status           = Caboose::Invoice::STATUS_CART
        @invoice.financial_status = Caboose::Invoice::STATUS_PENDING
        @invoice.date_created     = DateTime.now
        @invoice.referring_site   = request.env['HTTP_REFERER']
        @invoice.landing_page     = request.fullpath
        @invoice.landing_page_ref = params[:ref] || nil
        @invoice.payment_terms    = @site.store_config.default_payment_terms
        @invoice.save
        
        InvoiceLog.create(
          :invoice_id     => @invoice.id,          
          :user_id        => logged_in_user.id,
          :date_logged    => DateTime.now.utc,
          :invoice_action => InvoiceLog::ACTION_INVOICE_CREATED                
        )
        
        # Save the cart ID in the session
        session[:cart_id] = @invoice.id
      end                  
    end
Initialize the cart in the session