Skip to content

Commit

Permalink
#125 - Register a group of participants on an invoice
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivar Nilsen committed Oct 4, 2011
1 parent 932dcc9 commit f6f2245
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 37 deletions.
2 changes: 2 additions & 0 deletions app/controllers/invoices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def show

def new
@invoice = Invoice.new
3.times { @invoice.users.build }

respond_to do |format|
format.html # new.html.erb
Expand All @@ -44,6 +45,7 @@ def create
end

def update
params[:invoice][:existing_user_attributes] ||= {}
@invoice = Invoice.find(params[:id])
respond_to do |format|
@invoice.update_attributes(params[:invoice])
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def new
@user.registration = Registration.new
@user.registration.manual_payment = params[:manual_payment]
@user.registration.free_ticket = !params[:free_ticket].blank?
@user.registration.ticket_type = params[:free_ticket] || params[:ticket_type] || 'early_bird'
@user.registration.ticket_type = params[:free_ticket] || params[:ticket_type] || "early_bird" # TODO: Don't hardcode this
@user.registration.includes_dinner = @user.registration.discounted_ticket?
end

Expand Down
12 changes: 12 additions & 0 deletions app/helpers/invoices_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# -*- encoding : utf-8 -*-
module InvoicesHelper
def fields_for_user(user, &block)
prefix = user.new_record? ? "new" : "existing"
fields_for("invoice[#{prefix}_user_attributes][]", user, &block)
end
def add_user_link(text)
link_to_function(text) do |page|
page.insert_html :bottom, :users, :partial => "user", :object => User.new
end
end
end
38 changes: 36 additions & 2 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,40 @@
# -*- encoding : utf-8 -*-
class Invoice < ActiveRecord::Base

has_many :users
has_many :users

end
def new_user_attributes=(user_attributes)
user_attributes.each do |attributes|
if attributes[:name].present?
user = users.build attributes
user.password = "dnmglksdngldsn"
user.password_confirmation = user.password
user.company = company_name
user.registration = Registration.new
user.registration.ticket_type = "early_bird" # TODO: Don't hardcode this
user.registration.manual_payment = true
# TODO @user.registration_ip = request.remote_ip
end
end
end
def existing_user_attributes=(user_attributes)
users.reject(&:new_record?).each do |user|
attributes = user_attributes[user.id.to_s]
if attributes
user.attributes = attributes
else
users.delete(user)
end
end
end
before_validation do
for user in users
user.company = self.company_name
end
end
after_update do
for user in users
user.save(false)
end
end
end
27 changes: 3 additions & 24 deletions app/models/registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,73 +10,56 @@ class Registration < ActiveRecord::Base
"organizer" => "Arrangør på Smidig 2011",
"speaker" => "Foredragsholder på Smidig 2011"
}

attr_accessible :comments, :includes_dinner, :description,
:ticket_type, :free_ticket,
:manual_payment, :invoice_address, :invoice_description

default_scope :order => 'created_at desc'
belongs_to :user
has_one :payment_notification

validates_presence_of :ticket_type
# validates_presence_of :invoice_address, :if => Proc.new { |reg| reg.manual_payment }

before_create :create_payment_info

def ticket_description
TICKET_TEXTS[self.ticket_type] || ticket_type
TICKET_TEXTS[self.ticket_type] || ticket_type
end

def ticket_price
PAYMENT_CONFIG[:prices][ticket_type].to_i
PAYMENT_CONFIG[:prices][ticket_type].to_i
end

def price_mva
(self.price - price_ex_mva).to_i
end

def price_ex_mva
self.price/1.25
end

def description
ticket_description + (registration_complete ? " (Betalt)" : "")
end

def speaker?
ticket_type == "speaker"
end

def free_ticket
ticket_price == 0
end

def discounted_ticket?
%w(student).include? ticket_type
end

def special_ticket?
%w(sponsor volunteer organizer).include? ticket_type
end

def paid?
paid_amount && paid_amount > 0
end

def self.find_by_invoice(invoice_id)
if invoice_id =~ /^2011t?-(\d+)$/
Registration.find($1.to_i)
else
raise "Invalid invoice_id #{invoice_id}"
end
end

def invoice_id
return "2011-#{id}" if Rails.env == "production"
return "2011t-#{id}"
end

def payment_url(payment_notifications_url, return_url)
values = {
:business => PAYMENT_CONFIG[:paypal_email],
Expand All @@ -91,19 +74,16 @@ def payment_url(payment_notifications_url, return_url)
:item_number_1 => '1',
:quantity_1 => '1'
}

PAYMENT_CONFIG[:paypal_url] +"?"+values.map do
|k,v| "#{k}=#{CGI::escape(v.to_s)}"
end.join("&")
end

def status
paid? ? "Betalt" : (
registration_complete? ? "Godkjent" : (
manual_payment? && !invoiced ? "Skal faktureres" : (
manual_payment? ? "Har blitt fakturert" : "Må følges opp")))
end

def self.find_by_params(params)
if params[:conditions]
find(:all, :conditions => params[:conditions], :include => :user)
Expand All @@ -126,7 +106,6 @@ def self.find_by_params(params)
find(:all, :include => :user)
end
end

def create_payment_info
if paid?
raise "Kan ikke endre en utført betaling!"
Expand All @@ -136,4 +115,4 @@ def create_payment_info
self.free_ticket = price == 0
return true
end
end
end
26 changes: 16 additions & 10 deletions app/views/invoices/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<%= form_for @invoice do |f| %>

<fieldset class="invoice_details">

<% if @invoice.errors.any? %>
<div id="error_explanation">
<ul>
Expand Down Expand Up @@ -32,18 +31,25 @@
<%= f.label :post_code %><br />
<%= f.text_field :post_code %>
</div>


<table id="users">
<tr>
<th>Navn</th>
<th>Mail</th>
<th>Telefonnummer</th>
</tr>
<%= render :partial => "user", :collection => @invoice.users %>
</table>

<p><%= link_to_function "Ny bruker",
"$('#users').append('#{escape_javascript(render(:partial => "user", :object => User.new))}')"
%></p>


<div class="actions">
<%= f.submit %>
</div>

<ul>
<% @invoice.users.each do |user| %>
<li>
<div><%= user.name %></div>
<input type="checkbox" name="deleted_users[]" value="<%= user.id %>"/>
</li>
<% end %>
</ul>

</fieldset>
<% end %>
21 changes: 21 additions & 0 deletions app/views/invoices/_user.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<tr>
<%= fields_for_user(user) do |user_form| %>
<td><%= user_form.text_field :name %></td>
<td><%= user_form.text_field :email %></td>
<td><%= user_form.text_field :phone_number %></td>
<td>
<% if user.errors.any? %>
<div id="error_explanation">
<ul>
<% user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
</td>
<td>
<%= link_to_function "slett", "$(this).closest('tr').remove()" %>
</td>
<% end %>
</tr>
8 changes: 8 additions & 0 deletions app/views/invoices/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
<%= @invoice.post_code %>
</p>

<h3>Brukere</h3>

<ul>
<% for user in @invoice.users %>
<li><%= link_to user.name, user %></li>
<% end %>
</ul>


<%= link_to 'Edit', edit_invoice_path(@invoice) %> |
<%= link_to 'Back', invoices_path %>

0 comments on commit f6f2245

Please sign in to comment.