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

#create_monthly_pdfObject



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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/ish_manager/invoices_controller.rb', line 8

def create_monthly_pdf
  month_on = DateTime.strptime(params[:month_on], '%Y-%m-%d').strftime('%Y-%m-01')
  @leadset = Leadset.find params[:leadset_id]
  authorize! :create_monthly_invoice_pdf, @leadset

  @invoice = Ish::Invoice.where({ leadset_id: @leadset.id, month_on: month_on }).first
  if @invoice && !params[:replace]
    flash_alert "Already created this invoice."
    redirect_to controller: :leadsets, action: :show, id: @leadset.id
    return
  end

  if !@invoice
    @invoice = Ish::Invoice.create({
      leadset_id: @leadset.id,
      month_on:   month_on,
      number:     @leadset.next_invoice_number,
    })
    @leadset.update_attributes({ next_invoice_number: @leadset.next_invoice_number + 1 })
  end

  @pdf = @invoice.generate_monthly_invoice
  path = Rails.root.join 'tmp', @invoice.filename
  @pdf.render_file path
  data = File.read path

  asset = ::Gameui::Asset3d.new invoice: @invoice
  f = File.new( path )
  asset.object = f
  f.close
  @invoice.asset = asset
  if asset.save
    flash_notice "Saved the asset."
  end
  if @invoice.save
    flash_notice "Saved the invoice."
  end

  File.delete(path) if File.exist?(path)
  redirect_to controller: :leadsets, action: :show, id: @invoice.leadset_id

  # send_data( data, { :filename => @invoice.filename,
  #   :disposition => params[:disposition] ? params[:disposition] : :attachment,
  #   :type => 'application/pdf'
  # })
end

#create_stripeObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/controllers/ish_manager/invoices_controller.rb', line 55

def create_stripe
  @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[:product_id] ).price_id
    invoice_item = Stripe::InvoiceItem.create({
      customer: @invoice.leadset.customer_id,
      price:    stripe_price,
      invoice:  stripe_invoice.id,
      quantity: item[:quantity],
    })
  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

#email_sendObject



108
109
110
111
112
113
114
115
# File 'app/controllers/ish_manager/invoices_controller.rb', line 108

def email_send
  @invoice = Ish::Invoice.find params[:id]
  authorize! :send_email, @invoice
  out = ::IshManager::LeadsetMailer.monthly_invoice( @invoice.id.to_s )
  Rails.env.production? ? out.deliver_later : out.deliver_now
  flash_notice "Scheduled to send an email."
  redirect_to controller: :leadsets, action: :show, id: @invoice.leadset_id
end

#indexObject



87
88
89
90
91
92
93
94
# File 'app/controllers/ish_manager/invoices_controller.rb', line 87

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

#new_pdfObject



96
97
98
99
100
# File 'app/controllers/ish_manager/invoices_controller.rb', line 96

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

#new_stripeObject



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

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

#showObject



117
118
119
120
121
# File 'app/controllers/ish_manager/invoices_controller.rb', line 117

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

#updateObject



123
124
125
126
127
128
129
130
131
132
133
# File 'app/controllers/ish_manager/invoices_controller.rb', line 123

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