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
# File 'app/controllers/ish_manager/invoices_controller.rb', line 8

def create_monthly_pdf
  @leadset = Leadset.find params[:leadset_id]
  authorize! :create_monthly_invoice_pdf, @leadset

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

  @invoice = Ish::Invoice.create({ leadset_id: @leadset.id, month_on: params[:month_on] })

  @pdf = @invoice.generate_monthly_invoice params[:month_on]
  filename = "invoice-#{@invoice.number}.pdf"
  path = Rails.root.join 'tmp', filename
  @pdf.render_file path
  data = File.read path
  File.delete(path) if File.exist?(path)

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

#create_pdfObject

@TODO: obsolete, remove



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/controllers/ish_manager/invoices_controller.rb', line 37

def create_pdf
  @invoice = Ish::Invoice.new
  authorize! :new, @invoice

  tree_img_url = "#{Rails.root.join('public')}/tree-1.jpg"
  wasya_co_logo_url = "#{Rails.root.join('public')}/259x66-WasyaCo-logo.png"

  pdf = Prawn::Document.new

  pdf.canvas do
    pdf.image tree_img_url, at: [ 0, 792 ], width: 612

    pdf.fill_color 'ffffff'
    pdf.transparent( 0.75 ) do
      pdf.fill_rectangle [0, 792], 612, 792
    end
    pdf.fill_color '000000'

    pdf.image wasya_co_logo_url, at: [252, 720], width: 108 ## 1.5"

    pdf.bounding_box( [0.75*72, 9.25*72], width: 3.25*72, height: 0.75*72 ) do
      pdf.text "From:"
      pdf.text "Wasya Co"
      pdf.text "(415) 948-0368"
    end

    pdf.bounding_box( [4.5*72, 9.25*72], width: 3.25*72, height: 0.75*72 ) do
      pdf.text "Stats:"
      pdf.text "Date: #{ '2023-09-07' }"
      pdf.text "Invoice # #{ '111' }"
    end

    pdf.bounding_box( [0.75*72, 8.25*72], width: 3.25*72, height: 0.75*72 ) do
      pdf.text "To:"
      pdf.text "Creen Enterprise"
    end

    pdf.bounding_box( [4.5*72, 8.25*72], width: 3.25*72, height: 0.75*72 ) do
      pdf.text "Notes:"
      pdf.text "Support & various, for the month of August '23."
    end

    pdf.move_down 20

    pdf.table([
      [ 'Description', 'Type', 'Hours', 'Subtotal' ],
      [ 'Part 2/2', 'indep. proj.', '-', '$3,501' ],
    ], {
      position: :center,
      width: 7.5*72,
      cell_style: {
        inline_format: true,
        borders: [ :bottom ]
      },
    } )

    pdf.table([
      [ 'Total' ],
      [ '$3,501' ],
    ], {
      position: 7*72,
      width: 1*72,
      cell_style: {
        inline_format: true,
        borders: [ :bottom ]
      },
    } )

    pdf.text_box "Thank you!", at: [ 3.25*72, 1.25*72 ], width: 2*72, height: 1*72, align: :center
  end

  filename = "a-summary.pdf"
  path = Rails.root.join 'tmp', filename
  pdf.render_file path
  data = File.read path
  File.delete(path) if File.exist?(path)

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

#create_stripeObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/controllers/ish_manager/invoices_controller.rb', line 120

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

#indexObject



152
153
154
155
156
157
158
159
# File 'app/controllers/ish_manager/invoices_controller.rb', line 152

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



161
162
163
164
165
# File 'app/controllers/ish_manager/invoices_controller.rb', line 161

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

#new_stripeObject



167
168
169
170
171
# File 'app/controllers/ish_manager/invoices_controller.rb', line 167

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

#showObject



173
174
175
176
177
# File 'app/controllers/ish_manager/invoices_controller.rb', line 173

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

#updateObject



179
180
181
182
183
184
185
186
187
188
189
# File 'app/controllers/ish_manager/invoices_controller.rb', line 179

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