Class: Comee::Core::QuotationRequestService

Inherits:
Object
  • Object
show all
Defined in:
app/services/comee/core/quotation_request_service.rb

Instance Method Summary collapse

Instance Method Details

#confirm(id) ⇒ Object



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
# File 'app/services/comee/core/quotation_request_service.rb', line 65

def confirm(id)
  rfq = QuotationRequest.find(id)
  if QuotationRequest.statuses[rfq.status] != QuotationRequest.statuses[:awaiting_confirmation]
    raise(StandardError, "RFQ should be in awaiting_confirmation state.")
  end

  data = rfq.quotation_request_items.each_with_object([]) do |item, res|
    res << {
      client_id: rfq.client_id,
      product_id: item.product_id,
      unit_id: item.unit_id,
      price: item.price,
      margin: 0,
      margin_type: QuotationRequestItem.margin_types[:increase],
      valid_from: item.valid_from,
      valid_to: item.valid_to,
      status: ClientPrice.statuses[:current]
    }
  end

  ClientPrice.transaction do
    ClientPrice.insert_all!(data)
    rfq.status = QuotationRequest.statuses[:confirmed]
    rfq.save!
  end
  rfq
end

#convert_to_order(id) ⇒ Object



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
# File 'app/services/comee/core/quotation_request_service.rb', line 93

def convert_to_order(id)
  rfq = QuotationRequest.find(id)
  if QuotationRequest.statuses[rfq.status] != QuotationRequest.statuses[:confirmed]
    raise(StandardError, "RFQ should be in confirmed state for conversion to order.")
  end

  if rfq.quotation_request_items.any? { |item| item.customer_item_no.nil? }
    raise(StandardError, "All RFQ items should have customer item number set.")
  end

  order = nil
  CustomerOrder.transaction do
    order = CustomerOrder.create!(
      order_number: rfq.order_number,
      order_date: Date.current,
      client_id: rfq.client_id,
      order_terms: "",
      delivery_address: rfq.delivery_address,
      invoice_address: rfq.invoice_address,
      status: CustomerOrder.statuses[:draft]
    )

    items = rfq.quotation_request_items.each_with_object([]) do |item, res|
      res << {
        customer_order_id: order.id,
        product_id: item.product_id,
        unit_id: item.unit_id,
        customer_item_no: item.customer_item_no,
        customer_item_description: item.customer_item_description,
        quantity: item.quantity,
        price: item.price,
        delivery_date: item.expected_delivery_date,
        canceled: false
      }
    end

    CustomerOrderItem.insert_all!(items)
    rfq.status = QuotationRequest.statuses[:converted]
    rfq.save!
  end

  order
end

#create_request_with_items(params) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/services/comee/core/quotation_request_service.rb', line 4

def create_request_with_items(params)
  request = nil
  QuotationRequest.transaction do
    request = QuotationRequest.create!(
      client_id: params[:client_id],
      status: QuotationRequest.statuses[:draft],
      order_number: params[:order_number],
      order_date: params[:order_date],
      delivery_address_id: params[:delivery_address_id],
      invoice_address_id: params[:invoice_address_id]
    )
    items = params[:items]
    items.each do |item|
      item[:price] = 0
      item[:quotation_request_id] = request.id
      item[:margin] = 0
      item[:margin_type] = QuotationRequestItem.margin_types[:increase]
    end
    QuotationRequestItem.insert_all!(items)
  end
  request
rescue StandardError
  raise
end

#submit(id) ⇒ Object

Raises:

  • (StandardError)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/services/comee/core/quotation_request_service.rb', line 29

def submit(id)
  rfq = QuotationRequest.find(id)

  raise(StandardError, "RFQ should be in draft state.") if QuotationRequest.statuses[rfq.status] != QuotationRequest.statuses[:draft]

  raise(StandardError, "RFQ does not have any items.") if rfq.quotation_request_items.count.zero?

  raise(StandardError, "RFQ contains only canceled items.") if rfq.quotation_request_items.all?(&:canceled?)

  QuotationRequest.transaction do
    product_ids = rfq.quotation_request_items.map(&:product_id)
    prices = MasterPrice.where(product_id: product_ids).each_with_object({}) do |price, res|
      res[price.product_id] = [price.selling_price, price.valid_from, price.valid_to]
    end
    items = rfq.quotation_request_items.each_with_object({}) do |item, res|
      price = prices[item.product_id]
      res[item.id] = {price: price[0], valid_from: price[1], valid_to: price[2]} if price
    end
    QuotationRequestItem.update!(items.keys, items.values)
    rfq.status = QuotationRequest.statuses[:submitted]
    rfq.save!
  end
  rfq
end

#submit_for_confirmation(id) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'app/services/comee/core/quotation_request_service.rb', line 54

def submit_for_confirmation(id)
  rfq = QuotationRequest.find(id)
  if QuotationRequest.statuses[rfq.status] != QuotationRequest.statuses[:submitted]
    raise(StandardError, "RFQ should be in submitted state.")
  end

  rfq.status = QuotationRequest.statuses[:awaiting_confirmation]
  rfq.save!
  rfq
end