Class: Smerp::Quotation::Engine::QuotationsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/smerp/quotation/engine/quotations_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /quotations



53
54
55
56
57
58
59
60
61
# File 'app/controllers/smerp/quotation/engine/quotations_controller.rb', line 53

def create
  @quotation = Quotation.new(quotation_params)

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

#create_calculatorObject



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
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
151
152
153
154
155
156
157
158
159
160
# File 'app/controllers/smerp/quotation/engine/quotations_controller.rb', line 92

def create_calculator
  root = params[:calculator]
  calType = root[:cal_type]

  val = root[:value]
  app = root[:applicability]

  indx = (val =~ /p/)
  if not indx.nil?
    valType = :percent
    vval = val[0...indx]
    vval = vval.to_f.percent
  else
    valType = :raw
    vval = val.to_f.value
  end

  case calType
  when "tax"
    cal = Smerp::Quotation::ExtendedCalculator.instance(:tax,vval)
    case app
    when "quotation"
      cal.input_field = :total_after_discount
      cal.output_field = [{ total_tax: Smerp::Quotation::TaxCalculator::TaxAmount }, { total_with_tax: Smerp::Quotation::TaxCalculator::ValueWithTax }]
    when "items"
      cal.input_field = :line_total_after_discount
      cal.output_field = [{ tax: Smerp::Quotation::TaxCalculator::TaxAmount }, { line_total_with_tax: Smerp::Quotation::TaxCalculator::ValueWithTax }]
    end
  when "discount"
    cal = Smerp::Quotation::ExtendedCalculator.instance(:discount,vval)

    case app
    when "quotation"
      cal.input_field = :total
      cal.output_field = [{ total_discount: Smerp::Quotation::DiscountCalculator::DiscountedAmount }, { total_after_discount: Smerp::Quotation::DiscountCalculator::ValueAfterDiscount }]
    when "items"
      cal.input_field = :line_total
      cal.output_field = [{ discount: Smerp::Quotation::DiscountCalculator::DiscountedAmount }, { line_total_after_discount: Smerp::Quotation::DiscountCalculator::ValueAfterDiscount }]
    end

  #when "roundup"
  #  cal = Smerp::Quotation::ExtendedCalculator.instance(:round_up,vval)

  #  case app
  #  when "quotation"
  #    cal.input_field = :total
  #    cal.output_field = [{ total_discount: Smerp::Quotation::DiscountCalculator::DiscountedAmount }, { total_after_discount: Smerp::Quotation::DiscountCalculator::ValueAfterDiscount }]
  #  when "items"
  #    cal.input_field = :line_total
  #    cal.output_field = [{ discount: Smerp::Quotation::DiscountCalculator::DiscountedAmount }, { line_total_after_discount: Smerp::Quotation::DiscountCalculator::ValueAfterDiscount }]
  #  end


  end

  if not cal.nil?

    case app
    when "quotation"
      @quotation.add_priv_ext_cal(cal)
    when "items"
      @quotation.add_ext_cal(cal)
    end

    @quotation.save
  end

  redirect_to extended_calculators_quotation_path(@quotation)
end

#destroyObject

DELETE /quotations/1



73
74
75
76
# File 'app/controllers/smerp/quotation/engine/quotations_controller.rb', line 73

def destroy
  @quotation.destroy
  redirect_to quotations_url, notice: "Quotation was successfully destroyed."
end

#download_excelObject



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'app/controllers/smerp/quotation/engine/quotations_controller.rb', line 208

def download_excel
 
  p @quotation
  exp = Smerp::Exporter::Excel::Exporter.instance 
  exp.start_cell(8,0)

  currencyStyle = exp.workbook.styles.add_style(format_code: "#,##0.00;[Red]-#,##0.#0")
  exp.worksheet("quotation") do |s|
    s.insert_row ["Ref. No.", @quotation.qid]
    s.insert_row ["Date", @quotation.qdate]
    s.insert_row
    s.insert_row ["To", @quotation.name]
    s.insert_row ["Address", @quotation.address]

    s.insert_row
    s.insert_row
    s.insert_row

    s.start_col = 1
    s.insert_row ["Description", "Quantity", "", "Unit Price (RM)", "Total (RM)"]

    seq = 1
    level = 0
    @quotation.quotation_items.where(["parent_id is null"]).order(:position).each do |i|

      next if not i.quotation_product.nil? and i.quotation_product.quotation_product_category_id == 1

      patch = "   " * level
      name = [""]
      name << patch
      name << "#{helpers.level_seq_conv(level, seq)}. "
      name << i.name

      if i.children.length > 0 and not i.children[0].quotation_product.nil? and i.children[0].quotation_product.quotation_product_category_id == 1
        s.insert_row [name.join, i.quantity, i.unit, i.unit_price, i.line_total]
      else
        s.insert_row [name.join, "","","",""]
      end
      s.col_style(4, currencyStyle)
      s.col_style(5, currencyStyle)

      recurse_items(i, s, level+1)
      seq += 1
    end

    s.insert_row

    s.insert_row ["","","","Total", @quotation.total]
    s.col_style(5, currencyStyle)

    s.insert_row ["","","","Tax", @quotation.total_tax]
    s.col_style(5, currencyStyle)

    s.insert_row ["","","","Grand Total", @quotation.total_with_tax]
    s.col_style(5, currencyStyle)

  end

  output = "#{@quotation.id}.xlsx"
  exp.save_to(output)

  send_data(File.read(File.join(Rails.root, output)), type: 'application/octet-stream', disposition: "attachment")

end

#editObject

GET /quotations/1/edit



49
50
# File 'app/controllers/smerp/quotation/engine/quotations_controller.rb', line 49

def edit
end

#edit_extended_calculatorObject



162
163
164
# File 'app/controllers/smerp/quotation/engine/quotations_controller.rb', line 162

def edit_extended_calculator
  redirect_to extended_calculators_quotation_path(@quotation)
end

#extended_calculatorsObject

extended calculators operations



83
84
85
86
87
# File 'app/controllers/smerp/quotation/engine/quotations_controller.rb', line 83

def extended_calculators
  @extCals = @quotation.extended_calculators.nil? ? [] : YAML.load(@quotation.extended_calculators)
  @privExtCals = @quotation.private_extended_calculators.nil? ? [] : YAML.load(@quotation.private_extended_calculators)
  render "extended_calculators"
end

#filterObject



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'app/controllers/smerp/quotation/engine/quotations_controller.rb', line 189

def filter

  val = params[:val]

  case val
  when "main_only"
    session[:hide_children_parent_id] = [] if session[:hide_children_parent_id].nil?
    @quotation.quotation_items.where(["parent_id is null"]).each do |qi|
      session[:hide_children_parent_id] << qi.id
    end
  when "show_all"
    if not session[:hide_children_parent_id].nil?
      session[:hide_children_parent_id].clear
    end
  end

  redirect_to quotation_url(@quotation)
end

#indexObject

GET /quotations



11
12
13
# File 'app/controllers/smerp/quotation/engine/quotations_controller.rb', line 11

def index
  @quotations = Quotation.all
end

#newObject

GET /quotations/new



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

def new
  @quotation = Quotation.new
end

#new_extended_calculatorObject



89
90
# File 'app/controllers/smerp/quotation/engine/quotations_controller.rb', line 89

def new_extended_calculator
end

#new_product_itemObject

def delete_extended_calculator

end



174
175
176
# File 'app/controllers/smerp/quotation/engine/quotations_controller.rb', line 174

def new_product_item
  @quotation_item = QuotationItem.new
end

#recurse_items(item, sheet, level) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'app/controllers/smerp/quotation/engine/quotations_controller.rb', line 273

def recurse_items(item, sheet, level)
  seq = 1
  item.children.each do |c|

    next if not c.quotation_product.nil? and c.quotation_product.quotation_product_category_id == 1

    patch = "   " * level
    name = [""]
    name << patch
    name << "#{helpers.level_seq_conv(level, seq)}. "
    name << c.name
    
    if c.children.length > 0 and not c.children[0].quotation_product.nil? and c.children[0].quotation_product.quotation_product_category_id == 1
      sheet.insert_row [name.join, c.quantity, c.unit, c.unit_price, c.line_total]
    else
      sheet.insert_row [name.join,"","","",""]
    end

    #sheet.insert_row [name.join, c.quantity, c.unit, c.unit_price, c.line_total]
    recurse_items(c, sheet, level+1) if c.children.length > 0
    seq += 1
  end
end

#resources_allocation_summaryObject



178
179
180
181
182
183
184
185
186
187
# File 'app/controllers/smerp/quotation/engine/quotations_controller.rb', line 178

def resources_allocation_summary
  @resTable = {}
  @quotation.quotation_items_allocated_resources.each do |qi|
    prodId = qi.quotation_product.name
    @resTable[prodId] = { total: 0, rate: qi.quotation_product.unit_price.to_f } if @resTable[prodId].nil?
    @resTable[prodId][:total] += qi.quantity.to_i

    @resTable[prodId][:total_rate] = @resTable[prodId][:total] * @resTable[prodId][:rate]
  end
end

#showObject

GET /quotations/1



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
# File 'app/controllers/smerp/quotation/engine/quotations_controller.rb', line 16

def show
  @qi = QuotationItem.new
  @qig = QuotationItemGroup.new

  @parent = [["(Group) Others", 0]]
  @parent += @quotation.quotation_item_groups.collect { |g| ["(Group) #{g.name}", "group-#{g.id}"] }
  @parent += @quotation.quotation_items.collect { |i| 
    nm = []

    if not i.quotation_item_group.nil?
      nm << "(Group) #{i.quotation_item_group.name}"  
    else
      nm << "(Group) Others"  
    end

    nm << "(Parent) #{i.parent.name}" if not i.parent.nil?
    fnm = "#{nm.join("/")} - #{i.name}"
    [fnm, "item-#{i.id}"] 
  }

  @extCals = @quotation.extended_calculators.nil? ? [] : YAML.load(@quotation.extended_calculators)
  @privExtCals = @quotation.private_extended_calculators.nil? ? [] : YAML.load(@quotation.private_extended_calculators)
 

  render layout: "full_width_application"
end

#updateObject

PATCH/PUT /quotations/1



64
65
66
67
68
69
70
# File 'app/controllers/smerp/quotation/engine/quotations_controller.rb', line 64

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