Class: Comee::Core::ClientPricesController

Inherits:
ApplicationController show all
Includes:
Common
Defined in:
app/controllers/comee/core/client_prices_controller.rb

Instance Method Summary collapse

Methods included from Common

#show

Methods included from Pagination

#default_per_page, #order_by, #order_direction, #page_no, #paginate, #paginate_offset, #per_page

Methods inherited from ApplicationController

#application_code, #authenticate, #current_application, #current_user, #render_content, #render_error, #skip_bullet

Instance Method Details

#approveObject



81
82
83
84
85
86
87
# File 'app/controllers/comee/core/client_prices_controller.rb', line 81

def approve
  price = ClientPrice.find(params[:id])
  price = price.approve
  render_content(price)
rescue StandardError => e
  render json: {success: false, error: e.message}, status: 422
end

#createObject



12
13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/comee/core/client_prices_controller.rb', line 12

def create
  super do
    price = ClientPrice.new(model_params)
    master_price = MasterPrice.find_by(product_id: price.product_id, primary: true)
    price.price = master_price&.selling_price
    if price.price
      price.price = price.margin_increase? ? price.price * (1 + price.margin / 100) : price.price * (1 - price.margin / 100)
    end
    price
  end
end

#extend_validityObject



50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/controllers/comee/core/client_prices_controller.rb', line 50

def extend_validity
  price = ClientPrice.find(params[:id])
  if price.draft?
    render json: {success: false, error: "Price must be an approved price for validity extension."}
  else
    price.valid_to = extend_params[:valid_to]
    price.save!
    render_content(price)
  end
rescue StandardError => e
  render json: {success: false, error: e.message}
end

#fetch_for_clientObject



63
64
65
66
67
68
69
# File 'app/controllers/comee/core/client_prices_controller.rb', line 63

def fetch_for_client
  client = Client.find(params[:id])
  client_id = client.parent_id || client.id
  prices = ClientPrice.includes(:client, :product, :unit, :product_lookup)
                      .where(client_id: client_id)
  render_content(prices)
end

#fetch_oneObject



71
72
73
74
75
76
77
78
79
# File 'app/controllers/comee/core/client_prices_controller.rb', line 71

def fetch_one
  price = ClientPrice.includes(:client, :product, :unit, :product_lookup)
                     .find_by(
                       client_id: params[:id],
                       product_id: fetch_one_params[:product_id],
                       status: Price.statuses[:current]
                     )
  render_content(price)
end

#filterObject



89
90
91
92
93
# File 'app/controllers/comee/core/client_prices_controller.rb', line 89

def filter
  prices = ClientPrice.includes(:client, :product, :unit, :product_lookup)
                      .ransack(params[:q]).result
  render_content(prices)
end

#indexObject



6
7
8
9
10
# File 'app/controllers/comee/core/client_prices_controller.rb', line 6

def index
  super do
    ClientPrice.includes(:client, :product, :unit, :product_lookup).all
  end
end

#updateObject



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
# File 'app/controllers/comee/core/client_prices_controller.rb', line 24

def update
  if @obj.draft?
    product_id = model_params[:product_id] || @obj.product_id
    master_price = MasterPrice.find_by(product_id: product_id, primary: true)
    @obj.price = master_price&.selling_price
    @obj.valid_from = model_params[:valid_from] if model_params[:valid_from]
    @obj.valid_to = model_params[:valid_to] if model_params[:valid_to]
    @obj.margin = model_params[:margin] if model_params[:margin]
    @obj.margin_type = model_params[:margin_type] if model_params[:margin_type]
    if @obj.price
      @obj.price = @obj.margin_increase? ? @obj.price * (1 + @obj.margin / 100) : @obj.price * (1 - @obj.margin / 100)
      @obj.price = @obj.price.round(2)
    end
    if @obj.save
      render_content(@obj)
    else
      render json: {success: false, error: @obj.errors.full_messages[0]}, status: 422
    end
  else
    error = "Client price should be in draft state to edit."
    render json: {success: false, error: error}, status: 422
  end
rescue StandardError => e
  render json: {success: false, error: e.message}
end