Class: Dorsale::BillingMachine::QuotationsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/dorsale/billing_machine/quotations_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#current_user_scope

Instance Method Details

#copyObject



142
143
144
145
146
147
148
149
# File 'app/controllers/dorsale/billing_machine/quotations_controller.rb', line 142

def copy
  authorize! :copy, @quotation

  new_quotation = @quotation.create_copy!
  flash[:notice] = t("messages.quotations.copy_ok")

  redirect_to dorsale.edit_billing_machine_quotation_path(new_quotation)
end

#createObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/dorsale/billing_machine/quotations_controller.rb', line 67

def create
  # callback in BillingMachine::ApplicationController
  @quotation ||= ::Dorsale::BillingMachine::Quotation.new(quotation_params)

  authorize! :create, @quotation

  if @quotation.save
    flash[:notice] = t("messages.quotations.create_ok")
    redirect_to dorsale.billing_machine_quotations_path
  else
    ap @quotation.errors
    render :edit
  end
end

#create_invoiceObject



151
152
153
154
155
156
157
158
# File 'app/controllers/dorsale/billing_machine/quotations_controller.rb', line 151

def create_invoice
  authorize! :read, @quotation
  authorize! :create, ::Dorsale::BillingMachine::Invoice

  @invoice = @quotation.to_new_invoice

  render "dorsale/billing_machine/invoices/new"
end

#destroyObject



129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/controllers/dorsale/billing_machine/quotations_controller.rb', line 129

def destroy
  # callback in BillingMachine::ApplicationController
  authorize! :delete, @quotation

  if @quotation.destroy
    flash[:notice] = t("messages.quotations.update_ok")
  else
    flash[:alert] = t("messages.quotations.update_error")
  end

  redirect_to dorsale.billing_machine_quotations_path
end

#editObject



107
108
109
110
111
112
113
114
115
# File 'app/controllers/dorsale/billing_machine/quotations_controller.rb', line 107

def edit
  # callback in BillingMachine::ApplicationController
  authorize! :update, @quotation
  if ::Dorsale::BillingMachine.vat_mode == :single
    @quotation.lines.build(vat_rate: @quotation.vat_rate) if @quotation.lines.empty?
  else
    @quotation.lines.build if @quotation.lines.empty?
  end
end

#indexObject



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
54
55
# File 'app/controllers/dorsale/billing_machine/quotations_controller.rb', line 13

def index
  # callback in BillingMachine::ApplicationController
  authorize! :list, ::Dorsale::BillingMachine::Quotation

  @quotations ||= ::Dorsale::BillingMachine::Quotation.all
  @filters    ||= ::Dorsale::BillingMachine::SmallData::FilterForQuotations.new(cookies)
  @order      ||= {unique_index: :desc}

  @quotations = @filters.apply(@quotations)
  @quotations = @quotations.order(@order)
  @quotations_without_pagination = @quotations # All filtered quotations (not paginated)
  @quotations = @quotations.page(params[:page]).per(50)

  @total_excluding_taxes = @quotations_without_pagination.to_a
    .select{ |q| q.state != "canceled" }
    .map(&:total_excluding_taxes)
    .delete_if(&:blank?)
    .sum

  @vat_amount = @quotations_without_pagination.to_a
    .select{ |q| q.state != "canceled" }
    .map(&:vat_amount)
    .delete_if(&:blank?)
    .sum

  @total_including_taxes = @quotations_without_pagination.to_a
    .select{ |q| q.state != "canceled" }
    .map(&:total_including_taxes)
    .delete_if(&:blank?)
    .sum

  respond_to do |format|
    format.csv {
      send_data generate_encoded_csv(@quotations_without_pagination), type: "text/csv"
    }

    format.json {
      respond_with @quotations_without_pagination
    }

    format.html
  end
end

#newObject



57
58
59
60
61
62
63
64
65
# File 'app/controllers/dorsale/billing_machine/quotations_controller.rb', line 57

def new
  # callback in BillingMachine::ApplicationController
  @quotation ||= ::Dorsale::BillingMachine::Quotation.new
  @quotation.lines.build if @quotation.lines.empty?

  @quotation.id_card = @id_cards.first if @id_cards.one?

  authorize! :create, @quotation
end

#showObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/dorsale/billing_machine/quotations_controller.rb', line 82

def show
  # callback in BillingMachine::ApplicationController
  authorize! :read, @quotation

  respond_to do |format|
    format.pdf {
        authorize! :download, @quotation
        pdf_data  = @quotation.pdf.render_with_attachments

        file_name = [
          ::Dorsale::BillingMachine::Quotation.t,
          @quotation.tracking_id,
          @quotation.customer.try(:short_name),
        ].join("_").concat(".pdf")

        send_data pdf_data,
          :type        => "application/pdf",
          :filename    => file_name,
          :disposition => "inline"
    }

    format.html
  end
end

#updateObject



117
118
119
120
121
122
123
124
125
126
127
# File 'app/controllers/dorsale/billing_machine/quotations_controller.rb', line 117

def update
  # callback in BillingMachine::ApplicationController
  authorize! :update, @quotation

  if @quotation.update(quotation_params)
    flash[:notice] = t("messages.quotations.update_ok")
    redirect_to dorsale.billing_machine_quotations_path
  else
    render :edit
  end
end