Class: Admin::InvoicesController

Inherits:
ApplicationController
  • Object
show all
Includes:
AdminLayoutHelper, ExtensibleObjectHelper, InvoicePdfHelper
Defined in:
app/controllers/admin/invoices_controller.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ExtensibleObjectHelper

append_features

Methods included from AdminLayoutHelper

append_features, #controller_url, #define_layout_variables, #index_with_admin_helper

Methods included from ApplicationHelper

#controller_id, #define_application_layout_variables, #h_money, #money_for_input

Class Method Details

.active_scaffold_controller_for(klass) ⇒ Object



64
65
66
# File 'app/controllers/admin/invoices_controller.rb', line 64

def self.active_scaffold_controller_for(klass)
  (klass == Activity) ? Admin::ActivitiesWithPricesController : super
end

Instance Method Details

#after_update_save(invoice) ⇒ Object Also known as: after_create_save



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/controllers/admin/invoices_controller.rb', line 97

def after_update_save(invoice)
  super

  if successful? and invoice.is_published and (@invoice_new_record || @invoice_changes.include?(:is_published))
    # Unfortunately we have to assign payments using the quick_create and not by concat'ing on the
    # invoice AssociationProxy. THis is due to a bug in my InvoicesWithTotal wrapper that I think is related
    # to the association proxy not resettting its owenr id on an id change (as is the case of a create)
    invoice.client.recommend_payment_assignments_for(invoice.amount).each do |ip|
       InvoicePayment.quick_create! invoice.id, ip.payment_id, ip.amount
    end

    define_invoice invoice
    
    attachments = [
      {
      :content_type => "application/pdf", 
      :body         => render_to_string(:action => 'download', :layout => false),
      :disposition  => "attachment",
      :filename     => @rails_pdf_name
      }
    ]
    
    mail = Notifier.deliver_invoice_available @invoice, @client, @footer_text, attachments
    
    dest_addresses = []
    dest_addresses += mail.to_addrs if mail.to_addrs
    dest_addresses += mail.cc_addrs if mail.cc_addrs
    
    dest_addresses.collect!{|t_a| '%s <%s>' % [t_a.name, t_a.address] }
    
    flash[:warning]  = "Invoice e-mailed to : %s" % dest_addresses.join(',')
  end
end

#before_update_save(invoice) ⇒ Object Also known as: before_create_save



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/admin/invoices_controller.rb', line 76

def before_update_save(invoice)
  super
     
  invoice.activities = invoice.recommended_activities if (
    invoice.new_record? or (
      !invoice.is_published and
      # Unfortunately, there's no easy way to accomplish a invoice.activity_types.changed?, so - this will 
      # effectively ascertain that answer by comparing the params against the activity_type_ids 
      (invoice.issued_on_changed? || (@activity_type_ids_before != invoice.activity_type_ids))
    )
  )

  invoice.payment_assignments.clear if !invoice.is_published and invoice.is_published_changed?

  # We're going to need this in the after_update_save... Note it now.
  @invoice_changes = invoice.changes.keys.collect(&:to_sym)
  @invoice_new_record = invoice.new_record?
end

#downloadObject



133
134
135
136
137
138
# File 'app/controllers/admin/invoices_controller.rb', line 133

def download
  define_invoice Invoice.find(params[:id].to_i, :include => [:client])
  
  rescue 
    render :file => RAILS_ROOT+'/public/500.html', :status => 500
end

#update_record_from_params(*args) ⇒ Object

We ovverride the defaut behavior here only so that we can use this method as a hook to detyermine the invoice.activity_type_ids Before they’ve been updated by the form. We reference @activity_type_ids_before in before_update_save to determine whether we need to update the activity associations for this invoice.



71
72
73
74
# File 'app/controllers/admin/invoices_controller.rb', line 71

def update_record_from_params(*args)
  @activity_type_ids_before = @record.activity_type_ids.dup if @record
  super
end