Class: IshManager::InvoicesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/ish_manager/invoices_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#basic_auth, #home, #tinymce

Instance Method Details

#createObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/ish_manager/invoices_controller.rb', line 6

def create
  @invoice = Ish::Invoice.new params[:invoice].permit!
  authorize! :create, @invoice

  stripe_invoice = Stripe::Invoice.create({
    customer:          @invoice.leadset.customer_id,
    collection_method: 'send_invoice',
    days_until_due:    0,
    # collection_method: 'charge_automatically',
    pending_invoice_items_behavior: 'exclude',
  })
  params[:invoice][:items].each do |item|
    stripe_price = Wco::Product.find( item ).price_id
    invoice_item = Stripe::InvoiceItem.create({
      customer: @invoice.leadset.customer_id,
      price:    stripe_price,
      invoice:  stripe_invoice.id,
    })
  end
  Stripe::Invoice.send_invoice(stripe_invoice[:id])
  @invoice.update_attributes({ invoice_id: stripe_invoice[:id] })

  if @invoice.save
    flash[:notice] = "Created the invoice."
    redirect_to action: :show, id: @invoice.id
  else
    flash[:alert] = "Cannot create invoice: #{@invoice.errors.messages}"
    render :new
  end
end

#indexObject



37
38
39
40
41
42
43
44
# File 'app/controllers/ish_manager/invoices_controller.rb', line 37

def index
  authorize! :index, Ish::Invoice
  @invoices = Ish::Invoice.all
  if params[:leadset_id]
    @invoices = @invoices.where( leadset_id: params[:leadset_id] )
  end
  @invoices = @invoices.includes( :payments )
end

#newObject



46
47
48
49
50
# File 'app/controllers/ish_manager/invoices_controller.rb', line 46

def new
  authorize! :new, @invoice
  @leadset = Leadset.find params[:leadset_id]
  @products_list = Wco::Product.list
end

#showObject



52
53
54
55
56
# File 'app/controllers/ish_manager/invoices_controller.rb', line 52

def show
  @invoice = Ish::Invoice.find params[:id]
  authorize! :show, @invoice
  @stripe_invoice = Stripe::Invoice.retrieve @invoice.invoice_id
end

#updateObject



58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/ish_manager/invoices_controller.rb', line 58

def update
  @invoice = Ish::Invoice.find params[:id]
  authorize! :update, @invoice
  if @invoice.update_attributes params[:invoice].permit!
    flash[:notice] = 'Success'
    redirect_to :action => 'index'
  else
    flash[:alert] = "Cannot update invoice: #{@invoice.errors.messages}"
  end
  redirect_to :action => 'index'
end