Class: Wco::InvoicesController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#home, #tinymce

Methods included from ApplicationHelper

#my_truthy?, #obfuscate, #pp_amount, #pp_currency, #pp_date, #pp_datetime, #pp_money, #pp_percent, #pp_time, #pretty_date

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/wco/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 = Wco::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 = Wco::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 = "/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
# File 'app/controllers/wco/invoices_controller.rb', line 55

def create_stripe
  params[:invoice][:is_stripe] = true
  @invoice = Wco::Invoice.new params[:invoice].permit!
  authorize! :create, @invoice

  if @invoice.save
    flash_notice "Created the invoice."

    if params[:do_send]
      Stripe::Invoice.send_invoice(@invoice[:invoice_id])
    end

    redirect_to action: :show, id: @invoice.id
  else
    flash_alert "Cannot create invoice: #{@invoice.errors.messages}"
    render :new
  end
end

#editObject



74
75
76
77
# File 'app/controllers/wco/invoices_controller.rb', line 74

def edit
  @invoice = Wco::Invoice.find params[:id]
  authorize! :edit, @invoice
end

#email_sendObject



79
80
81
82
83
84
85
86
# File 'app/controllers/wco/invoices_controller.rb', line 79

def email_send
  @invoice = Wco::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



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

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

#new_pdfObject



96
97
98
99
100
# File 'app/controllers/wco/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/wco/invoices_controller.rb', line 102

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

#send_stripeObject



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

def send_stripe
  @invoice = Wco::Invoice.find params[:id]
  authorize! :send_stripe, @invoice
  Stripe::Invoice.send_invoice( @invoice.invoice_id )
  flash_notice "Scheduled to send the invoice."
  redirect_to action: :show, id: @invoice.id
end

#showObject



116
117
118
119
120
# File 'app/controllers/wco/invoices_controller.rb', line 116

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

#updateObject



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

def update
  @invoice = Wco::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