diff --git a/app/controllers/invoices_controller.rb b/app/controllers/invoices_controller.rb index a18f2ac..ae2ed00 100644 --- a/app/controllers/invoices_controller.rb +++ b/app/controllers/invoices_controller.rb @@ -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 @@ -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]) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 41136f7..700c195 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -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 diff --git a/app/helpers/invoices_helper.rb b/app/helpers/invoices_helper.rb new file mode 100644 index 0000000..52c1adc --- /dev/null +++ b/app/helpers/invoices_helper.rb @@ -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 \ No newline at end of file diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 83133ca..4075b0d 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -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 \ No newline at end of file diff --git a/app/models/registration.rb b/app/models/registration.rb index c0513ca..5ac81be 100644 --- a/app/models/registration.rb +++ b/app/models/registration.rb @@ -10,60 +10,45 @@ 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) @@ -71,12 +56,10 @@ def self.find_by_invoice(invoice_id) 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], @@ -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) @@ -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!" @@ -136,4 +115,4 @@ def create_payment_info self.free_ticket = price == 0 return true end -end +end \ No newline at end of file diff --git a/app/views/invoices/_form.html.erb b/app/views/invoices/_form.html.erb index 85f515c..d633b8f 100644 --- a/app/views/invoices/_form.html.erb +++ b/app/views/invoices/_form.html.erb @@ -1,7 +1,6 @@ <%= form_for @invoice do |f| %>
- <% if @invoice.errors.any? %>
+ + + + + + + + + <%= render :partial => "user", :collection => @invoice.users %> +
NavnMailTelefonnummer
+ +

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

+ +
<%= f.submit %>
- -
<% end %> diff --git a/app/views/invoices/_user.html.erb b/app/views/invoices/_user.html.erb new file mode 100644 index 0000000..6fadf77 --- /dev/null +++ b/app/views/invoices/_user.html.erb @@ -0,0 +1,21 @@ + + <%= fields_for_user(user) do |user_form| %> + <%= user_form.text_field :name %> + <%= user_form.text_field :email %> + <%= user_form.text_field :phone_number %> + + <% if user.errors.any? %> +
+ +
+ <% end %> + + + <%= link_to_function "slett", "$(this).closest('tr').remove()" %> + + <% end %> + diff --git a/app/views/invoices/show.html.erb b/app/views/invoices/show.html.erb index 6d41283..c417548 100644 --- a/app/views/invoices/show.html.erb +++ b/app/views/invoices/show.html.erb @@ -25,6 +25,14 @@ <%= @invoice.post_code %>

+

Brukere

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