Class: Smerp::Quotation::Engine::QuotationItemsController

Inherits:
ApplicationController show all
Includes:
TR::CondUtils
Defined in:
app/controllers/smerp/quotation/engine/quotation_items_controller.rb

Instance Method Summary collapse

Instance Method Details

#add_sub_itemObject



191
192
193
194
195
196
197
198
199
200
# File 'app/controllers/smerp/quotation/engine/quotation_items_controller.rb', line 191

def add_sub_item
  @parent = @quotation_item
  @quotation = @parent.quotation

  @quotation_item = QuotationItem.new
  @quotation_item.parent = @parent
  @quotation_item.quotation = @quotation

  render "new", quotation_item: @quotation_item, quotation: @quotation
end

#apply_discountObject



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
119
120
121
122
123
124
# File 'app/controllers/smerp/quotation/engine/quotation_items_controller.rb', line 75

def apply_discount
  val = params[:quotation_item][:discount]
  target = params[:quotation_item][:discount_on]
  ecid = params[:quotation_item][:ecid]

  if not @quotation_item.attributes.keys.include?(target)
    raise "Discount target '#{target}' not a valid field"
  end

  indx = val =~ /p/
  if not indx.nil?
    vv = val[0...indx]
    vf = vv.to_f

    if not_empty?(ecid)
      @quotation_item.update_ext_cal(ecid, params: vf.percent)
    else
      cal = Smerp::Quotation::ExtendedCalculator.instance(:discount, vf.percent)
      cal.input_field = target
      cal.output_field = [{ discount: Smerp::Quotation::DiscountCalculator::DiscountedAmount }, { line_total_after_discount: Smerp::Quotation::DiscountCalculator::ValueAfterDiscount }]
      @quotation_item.add_ext_cal(cal)
    end

    if @quotation_item.save
      redirect_to quotation_url(@quotation_item.quotation), notice: "Discount applied"
    else
      redirect_to quotation_url(@quotation_item.quotation), error: "Quotation item update field. Error was : #{@quotation_item.errors}"
    end

  else
    vf = val.to_f

    if not_empty?(ecid)
      @quotation_item.update_ext_cal(ecid, params: vf)
    else
      cal = Smerp::Quotation::ExtendedCalculator.instance(:discount, vf)
      cal.input_field = target
      cal.output_field = [{ discount: Smerp::Quotation::DiscountCalculator::DiscountedAmount }, { line_total_after_discount: Smerp::Quotation::DiscountCalculator::ValueAfterDiscount }]
      @quotation_item.add_ext_cal(cal)
    end

    if @quotation_item.save
      redirect_to quotation_url(@quotation_item.quotation), notice: "Discount applied"
    else
      redirect_to quotation_url(@quotation_item.quotation), error: "Quotation item update field. Error was : #{@quotation_item.errors}"
    end

  end

end

#apply_taxObject



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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'app/controllers/smerp/quotation/engine/quotation_items_controller.rb', line 126

def apply_tax
  val = params[:quotation_item][:tax]
  ecid = params[:quotation_item][:ecid]

  indx = val =~ /p/
  if not indx.nil?
    vv = val[0...indx]
    vf = vv.to_f

    if not_empty?(ecid)
      @quotation_item.update_ext_cal(ecid, params: vf.percent)
    else
      cal = Smerp::Quotation::ExtendedCalculator.instance(:tax, vf.percent)
      cal.input_field = :line_total_after_discount
      cal.output_field = [{ tax: Smerp::Quotation::TaxCalculator::TaxAmount }, { line_total_with_tax: Smerp::Quotation::TaxCalculator::ValueWithTax }]
      @quotation_item.add_ext_cal(cal)
    end

    if @quotation_item.save
      redirect_to quotation_url(@quotation_item.quotation), notice: "Tax applied"
    else
      redirect_to quotation_url(@quotation_item.quotation), error: "Quotation item update field. Error was : #{@quotation_item.errors}"
    end

  else
    vf = val.to_f

    if not_empty?(ecid)
      @quotation_item.update_ext_cal(ecid, params: vf)
    else
      cal = Smerp::Quotation::ExtendedCalculator.instance(:tax, vf)
      cal.input_field = target
      cal.output_field = [{ tax: Smerp::Quotation::TaxCalculator::TaxAmount }, { line_total_with_tax: Smerp::Quotation::TaxCalculator::ValueWithTax }]
      @quotation_item.add_ext_cal(cal)
    end

    if @quotation_item.save
      redirect_to quotation_url(@quotation_item.quotation), notice: "Tax applied"
    else
      redirect_to quotation_url(@quotation_item.quotation), error: "Quotation item update field. Error was : #{@quotation_item.errors}"
    end

  end
  
end

#createObject

POST /quotation_items



48
49
50
51
52
53
54
55
56
# File 'app/controllers/smerp/quotation/engine/quotation_items_controller.rb', line 48

def create
  @quotation_item = QuotationItem.new(quotation_item_params)

  if @quotation_item.save
    redirect_to @quotation, notice: "Quotation item was successfully created."
  else
    render :new, status: :unprocessable_entity
  end
end

#destroyObject

DELETE /quotation_items/1



68
69
70
71
72
73
# File 'app/controllers/smerp/quotation/engine/quotation_items_controller.rb', line 68

def destroy
  q = @quotation_item.quotation
  @quotation_item.destroy
  
  redirect_to quotation_url(q), notice: "Quotation item was successfully destroyed."
end

#distributeObject



225
226
227
228
# File 'app/controllers/smerp/quotation/engine/quotation_items_controller.rb', line 225

def distribute
  @quotation = @quotation_item.quotation
  #redirect_to quotation_url(@quotation_item.quotation)
end

#editObject

GET /quotation_items/1/edit



44
45
# File 'app/controllers/smerp/quotation/engine/quotation_items_controller.rb', line 44

def edit
end

#find_parentObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/smerp/quotation/engine/quotation_items_controller.rb', line 9

def find_parent
  if params[:quotation_item].nil?
    qid = params[:quotation_id]
    gid = params[:quotation_item_group_id]
  else
    qid = params[:quotation_item][:quotation_id]
    gid = params[:quotation_item][:quotation_item_group_id]
  end

  if not_empty?(qid)
    @quotation = Quotation.find(qid)
  elsif not_empty?(gid)
    @group = QuotationItemGroup.find(gid)
    @quotation = @group.quotation
  else
    # use sprangly as the route allow direct access without parent
  end

end

#hide_childrenObject



202
203
204
205
206
207
208
# File 'app/controllers/smerp/quotation/engine/quotation_items_controller.rb', line 202

def hide_children
  session[:hide_children_parent_id] = [] if session[:hide_children_parent_id].nil?
  session[:hide_children_parent_id] << @quotation_item.id
  session[:hide_children_parent_id].uniq!

  redirect_to quotation_url(@quotation_item.quotation)
end

#indexObject

GET /quotation_items



30
31
32
# File 'app/controllers/smerp/quotation/engine/quotation_items_controller.rb', line 30

def index
  @quotation_items = QuotationItem.all
end

#newObject

GET /quotation_items/new



39
40
41
# File 'app/controllers/smerp/quotation/engine/quotation_items_controller.rb', line 39

def new
  @quotation_item = QuotationItem.new
end

#new_product_itemObject



218
219
220
221
222
223
# File 'app/controllers/smerp/quotation/engine/quotation_items_controller.rb', line 218

def new_product_item
  @parent = @quotation_item
  @quotation = @quotation_item.quotation

  @quotation_item = QuotationItem.new
end

#remove_taxObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'app/controllers/smerp/quotation/engine/quotation_items_controller.rb', line 172

def remove_tax
  if not_empty?(@quotation_item.extended_calculators)
    cal = YAML.load(@quotation_item.extended_calculators)
    # cannot directly remove as there is current tax value calculated before
    # If remove means the previous value will not get updated.
    # Provbably later device a mechanism to remove after update
    cal[:local].each do |c|
      c.params = 0.percent if c.is_a?(Smerp::Quotation::TaxCalculator)
    end
    cal[:quotation].each do |c|
      c.params = 0.percent if c.is_a?(Smerp::Quotation::TaxCalculator)
    end
    @quotation_item.extended_calculators = YAML.dump(cal)
    @quotation_item.save
  end

  redirect_to quotation_url(@quotation_item.quotation), notice: "Tax removed for '#{@quotation_item.name}'"
end

#showObject

GET /quotation_items/1



35
36
# File 'app/controllers/smerp/quotation/engine/quotation_items_controller.rb', line 35

def show
end

#show_childrenObject



210
211
212
213
214
215
216
# File 'app/controllers/smerp/quotation/engine/quotation_items_controller.rb', line 210

def show_children
  if not session[:hide_children_parent_id].nil?
    session[:hide_children_parent_id].delete_if { |i| i == @quotation_item.id }
  end

  redirect_to quotation_url(@quotation_item.quotation)
end

#updateObject

PATCH/PUT /quotation_items/1



59
60
61
62
63
64
65
# File 'app/controllers/smerp/quotation/engine/quotation_items_controller.rb', line 59

def update
  if @quotation_item.update(quotation_item_params)
    redirect_to @quotation, notice: "Quotation item was successfully updated."
  else
    render :edit, status: :unprocessable_entity
  end
end