Class: Admin::InvoicesController

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

Direct Known Subclasses

DraftInvoicesController

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

Methods included from ApplicationHelper

#controller_id, #define_application_layout_variables, #h_money, #money_for_input

Class Method Details

.active_scaffold_controller_for(klass) ⇒ Object

override_form_field_partial in the helper gets buggered out b/c our controller name doesn’t match the model name. This fixes that:



95
96
97
# File 'app/controllers/admin/invoices_controller.rb', line 95

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

.invoices_scaffold_config(&block) ⇒ Object

TODO: Put in a plugin-helper



10
11
12
13
# File 'app/controllers/admin/invoices_controller.rb', line 10

def self.invoices_scaffold_config(&block)
  @invoices_scaffold_config ||= []
  @invoices_scaffold_config << block.to_proc  
end

.invoices_scaffold_initObject



15
16
17
18
19
20
# File 'app/controllers/admin/invoices_controller.rb', line 15

def self.invoices_scaffold_init
  # I dont love this mechanism of scaffold_init - but I think its going to work quite well...
  configs = @invoices_scaffold_config

  active_scaffold(:invoice){|c| configs.each{|ac| ac.call c if ac.respond_to? :call} }
end

Instance Method Details

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



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/controllers/admin/invoices_controller.rb', line 107

def before_update_save(invoice)
  super
  
  # Here we handle activity assignments:
  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))
    )
  )
end

#downloadObject



123
124
125
126
127
128
# File 'app/controllers/admin/invoices_controller.rb', line 123

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

#toggle_publishedObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'app/controllers/admin/invoices_controller.rb', line 130

def toggle_published
  if params[:is_confirmed] == '1'
    invoice = find_if_allowed(params[:id], :toggle_published)
    
    invoice.is_published = !invoice.is_published
    invoice.payment_assignments.clear unless invoice.is_published
    invoice.save!

    if invoice.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
  
      # Send the notifier
      if params[:email_invoice].to_i == 1
        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

    render(:update) do |page| 
      # Close the "Nested" Div, if it's open:
      page << 'if($(%s)){Element.remove(%s);};' % ([element_row_id(:action => :nested, :id => invoice.id).to_json]*2)

      page.replace(
        element_row_id(:action => :list, :id => invoice.id), 
        :partial => 'list_record', 
        :locals => {:record => invoice}
      )
      
      # Update the flash as well, if needed:
      page.replace_html active_scaffold_messages_id, :partial => 'messages'
    end
  else
    @record = Invoice.find params[:id].to_i, :include => [:payment_assignments]

    render :action => :confirm_publish_modal, :layout => false
  end
end

#update_record_from_params(*args) ⇒ Object

We overide 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.



102
103
104
105
# File 'app/controllers/admin/invoices_controller.rb', line 102

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