Class: PurchasePricesController

Inherits:
ApplicationController show all
Includes:
ApplicationHelper
Defined in:
app/controllers/purchase_prices_controller.rb

Instance Method Summary collapse

Methods included from AuthentificationHelper

#accessible_company_ids_from_tenant_and_user, #application_mode_from_hostname, #current_company_id_from_accessible_company_ids, #current_tenant_from_user, #current_user_and_locale_from_token

Instance Method Details

#createObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/controllers/purchase_prices_controller.rb', line 45

def create
  purchase_price_params.require(:sku_code)

  if Time.parse(purchase_price_params[:effective_from_date]).to_date < Date.today - 2
    return render json: { error: 'Effective from date cannot be in the past.' }, status: :bad_request
  end

  uom_id = if (uom_description = purchase_price_params[:uom_description]).present?
             UnitOfMeasurement.find_by(description: uom_description).id
           else
             ''
           end

  sku_id = SkuMaster.where(product_id: params[:product_id], sku_code: purchase_price_params[:sku_code]).first.id

  currency_id = Currency.find_by(code: purchase_price_params[:currency_code]).id

  discount = purchase_price_params[:discount].presence || 0
  purchase_price = PurchasePrice.create_or_update(
    sku_id: sku_id,
    supplier_key: purchase_price_params[:vendor_id],
    currency_id: currency_id,
    uom_id: uom_id,
    effective_from_date: Time.parse(purchase_price_params[:effective_from_date]),
    price: purchase_price_params[:price],
    discount: discount,
    product_id: params[:product_id],
    minimum_quantity: purchase_price_params[:minimum_quantity],
    system_date: Time.now,
    system_user: current_user.email[0..19]
  )

  render json: purchase_price
end

#formObject



115
116
117
118
119
120
121
122
123
124
# File 'app/controllers/purchase_prices_controller.rb', line 115

def form
  render json: {
    data: {
      currencies: Currency.select(:code, :currency_id).all,
      vendors: Supplier.select(:supplier_id, :description).limit(50),
      units_of_measurement: UnitOfMeasurement.select(:description, :unit_of_measurement_id).all,
      skus: SkuMaster.where(product_id: params[:product_id]).select(:sku_code, :sku_id)
    }
  }
end

#indexObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/purchase_prices_controller.rb', line 12

def index
  product_id = params[:product_id]

  query = params[:query] || ''
  @limit = params[:limit].to_i || 10
  @offset = params[:offset].to_i || 0

  @headers = PurchasePrice::HEADERS
  @data, @metadata = PurchasePrice.where(
    product_id: product_id,
    limit: @limit,
    offset: @offset,
    query: query,
    column_name: 'effective_from_date',
    column_dir: 'desc'
  ).values_at(:data, :metadata)
end

#showObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/purchase_prices_controller.rb', line 30

def show
  purchase_price = PurchasePrice.find_by(id: params[:id])[0]
  purchase_price[:uom_description] =
    UnitOfMeasurement.find_by(unit_of_measurement_id: purchase_price[:uom_id])&.description
  purchase_price[:sku_code] = SkuMaster.find_by(sku_id: purchase_price[:sku_id]).sku_code

  vendor = Vendor.find(supplier_id: purchase_price[:supplier_key])
  purchase_price[:vendor] = {
    supplier_id: vendor.supplier_id,
    supplier_code: vendor.supplier_code,
    description: vendor.description
  }
  @data = [purchase_price]
end

#updateObject



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
# File 'app/controllers/purchase_prices_controller.rb', line 80

def update
  purchase_price_params.require(:sku_code)

  if Time.parse(purchase_price_params[:effective_from_date]).to_date < Date.today - 2
    return render json: { error: 'Effective from date cannot be in the past.' }, status: :bad_request
  end

  uom_id = if (uom_description = purchase_price_params[:uom_description]).present?
             UnitOfMeasurement.find_by(description: uom_description).id
           else
             ''
           end

  sku_id = SkuMaster.where(product_id: params[:product_id], sku_code: purchase_price_params[:sku_code]).first.id

  currency_id = Currency.find_by(code: purchase_price_params[:currency_code]).id

  purchase_price = PurchasePrice.create_or_update(
    id: params[:id],
    sku_id: sku_id,
    supplier_key: purchase_price_params[:vendor_id],
    currency_id: currency_id,
    uom_id: uom_id,
    effective_from_date: Time.parse(purchase_price_params[:effective_from_date]),
    price: purchase_price_params[:price],
    discount: purchase_price_params[:discount],
    product_id: params[:product_id],
    minimum_quantity: purchase_price_params[:minimum_quantity],
    system_date: Time.now,
    system_user: current_user.email[0..19]
  )

  render json: purchase_price
end