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 = "/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
86
87
88
89
90
# 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::Price.find( item[:price_id] ).price_id
    puts! stripe_price, 'stripe_price'
    invoice_item = Stripe::InvoiceItem.create({
      customer: @invoice.leadset.customer_id,
      price:    stripe_price,
      invoice:  stripe_invoice.id,
      quantity: item[:quantity],
    })
  end
  Stripe::Invoice.finalize_invoice( stripe_invoice[:id] )
  if params[:do_send]
    Stripe::Invoice.send_invoice(stripe_invoice[:id])
    flash_notice "Scheduled to send the invoice via stripe."
  end
  @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



113
114
115
116
117
118
119
120
# File 'app/controllers/ish_manager/invoices_controller.rb', line 113

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



92
93
94
95
96
97
98
99
# File 'app/controllers/ish_manager/invoices_controller.rb', line 92

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



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

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

#new_stripeObject



107
108
109
110
111
# File 'app/controllers/ish_manager/invoices_controller.rb', line 107

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

#send_stripeObject



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

def send_stripe
  @invoice = Ish::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



130
131
132
133
134
# File 'app/controllers/ish_manager/invoices_controller.rb', line 130

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

#updateObject



136
137
138
139
140
141
142
143
144
145
146
# File 'app/controllers/ish_manager/invoices_controller.rb', line 136

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