Class: InvoiceBar::InvoicesController

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

Instance Method Summary collapse

Instance Method Details

#createObject

POST /invoices/1 POST /invoices/1.json



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/controllers/invoice_bar/invoices_controller.rb', line 102

def create
  flash[:notice], flash[:alert] = nil, nil

  @invoice = Invoice.new(invoice_params)

  apply_templates if params[:fill_in]
  fill_in_contact if params[:fill_in_contact]

  if params[:tax_id]
    if (@invoice.load_contact_from_tax_id(@invoice.contact_tax_id))
      flash[:notice] = I18n.t('messages.tax_id_loaded')
    else
      flash[:alert] = I18n.t('messages.cannot_load_tax_id')
    end
  end

  if params[:fill_in] || params[:fill_in_contact] || params[:tax_id]
    respond_on_new @invoice
  else
    @invoice.user_name = current_user.name
    @invoice.user_tax_id = current_user.tax_id
    @invoice.user_tax_id2 = current_user.tax_id2
    @invoice.user_address = current_user.address.copy(
      addressable_type: "InvoiceBar::Invoice#user_address"
    )
    current_user.invoices << @invoice
    respond_on_create @invoice
  end
end

#create_receipt_for_invoiceObject



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

def create_receipt_for_invoice
  @invoice = current_user.invoices.where(number: params[:number]).first

  unless @invoice.receipt_id.blank?
    @receipt = current_user.receipts.find(@invoice.receipt_id)

    flash[:notice] = "Doklad #{@receipt.number} již vytvořen."
  else
    @receipt = Receipt.for_invoice(@invoice)

    if @invoice.issuer
      @receipt.issuer = true
      next_income_in_line = current_user.receipts.income.size + 1
      @next_number = ::InvoiceBar::Generators.income_receipt_number(next_income_in_line)
    else
      @receipt.issuer = false
      next_expense_in_line = current_user.receipts.expense.size + 1
      @next_number = ::InvoiceBar::Generators.income_receipt_number(next_income_in_line)
    end

    @receipt.number = @next_number
    @receipt.invoice = @invoice
    current_user.receipts << @receipt

    if @receipt.save
      @invoice.mark_as_paid
      @invoice.save!

      flash[:notice] = "Doklad #{@receipt.number} vytvořen."
    else
      flash[:alert] = 'Nepodařilo se vytvořit korespondující doklad.'
    end
  end

  redirect_to action: :show
end

#destroyObject

DELETE /invoices/1 DELETE /invoices/1.json



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

def destroy
  @invoice.destroy
  respond_on_destroy @invoice, invoices_url
end

#editObject

GET /invoices/1/edit



133
134
135
# File 'app/controllers/invoice_bar/invoices_controller.rb', line 133

def edit
  respond_on_edit @invoice
end

#filterObject



203
204
205
206
207
208
# File 'app/controllers/invoice_bar/invoices_controller.rb', line 203

def filter
  @invoices = current_user.invoices.limit(nil)
  @invoices = filter_params(@invoices)

  render action: 'index'
end

#from_templateObject



88
89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/invoice_bar/invoices_controller.rb', line 88

def from_template
  @template = current_user.invoice_templates.find(params[:id])
  @invoice = Invoice.from_template(@template)
  @invoice.user_name = current_user.name
  @invoice.user_tax_id = current_user.tax_id
  @invoice.user_tax_id2 = current_user.tax_id2
  @invoice.user_address = current_user.address.copy(
    addressable_type: "InvoiceBar::Invoice#user_address"
  )
  respond_on_new @invoice
end

#indexObject

GET /invoices GET /invoices.json



11
12
13
14
# File 'app/controllers/invoice_bar/invoices_controller.rb', line 11

def index
  @invoices = current_user.invoices.page(params[:page])
  respond_on_index @invoices
end

#issuedObject



195
196
197
198
199
200
201
# File 'app/controllers/invoice_bar/invoices_controller.rb', line 195

def issued
  @section = :issued
  @invoices = current_user.invoices.issued
  @invoices = filter_params(@invoices)

  render action: 'index'
end

#mark_as_paidObject



167
168
169
170
171
172
173
174
175
# File 'app/controllers/invoice_bar/invoices_controller.rb', line 167

def mark_as_paid
  @invoice = current_user.invoices.where(number: params[:number]).first
  @invoice.mark_as_paid
  @invoice.save!

  flash[:notice] = 'Označeno jako zaplaceno.'

  redirect_to action: :show
end

#mark_as_sentObject



177
178
179
180
181
182
183
184
185
# File 'app/controllers/invoice_bar/invoices_controller.rb', line 177

def mark_as_sent
  @invoice = current_user.invoices.where(number: params[:number]).first
  @invoice.mark_as_sent
  @invoice.save!

  flash[:notice] = 'Označena za odeslanou.'

  redirect_to action: :show
end

#newObject

GET /invoices/new



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/invoice_bar/invoices_controller.rb', line 34

def new
  # Set the number of the document
  next_issued_in_line = current_user.invoices.issued.size + 1
  next_received_in_line = current_user.invoices.received.size + 1
  @next_issued = ::InvoiceBar::Generators.issued_invoice_number(next_issued_in_line)
  @next_received = ::InvoiceBar::Generators.received_invoice_number(next_received_in_line)

  @invoice = Invoice.new
  @invoice.number = @next_issued
  @invoice.items.build
  @invoice.build_address
  @invoice.issue_date = Date.today
  @invoice.due_date = @invoice.issue_date + 14.days

  respond_on_new @invoice
end

#receivedObject



187
188
189
190
191
192
193
# File 'app/controllers/invoice_bar/invoices_controller.rb', line 187

def received
  @section = :received
  @invoices = current_user.invoices.received
  @invoices = filter_params(@invoices)

  render action: 'index'
end

#showObject

GET /invoices/1 GET /invoices/1.json GET /invoices/1.pdf



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/invoice_bar/invoices_controller.rb', line 19

def show
  @address = @invoice.address
  @account = current_user.accounts.find(@invoice.)

  respond_to do |format|
    format.html { render :show }
    format.json { render json: @invoice }
    format.pdf {
      @pdf = InvoicePDF.new(@invoice).render
      send_data @pdf, type: 'application/pdf', disposition: 'inline'
    }
  end
end

#updateObject

PATCH/PUT /invoices/1 PATCH/PUT /invoices/1.json



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'app/controllers/invoice_bar/invoices_controller.rb', line 139

def update
  flash[:notice], flash[:alert] = nil, nil

  apply_templates if params[:fill_in]
  fill_in_contact if params[:fill_in_contact]

  if params[:tax_id]
    if (@invoice.load_contact_from_tax_id(@invoice.contact_tax_id))
      flash[:notice] = I18n.t('messages.tax_id_loaded')
    else
      flash[:alert] = I18n.t('messages.cannot_load_tax_id')
    end
  end

  if params[:fill_in] || params[:fill_in_contact] || params[:tax_id]
    respond_on_edit @invoice
  else
    respond_on_update @invoice, invoice_params
  end
end