Class: VendorsController

Inherits:
ApplicationController show all
Includes:
ApplicationHelper
Defined in:
app/controllers/vendors_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



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
125
126
# File 'app/controllers/vendors_controller.rb', line 85

def create
  handler = Vendors::Handlers::CreateOrUpdate.new(
    address_line1: params[:address_line1],
    address_line2: params[:address_line2],
    city: params[:city],
    country_id: Country::CANADA_ID,
    currency_id: params.dig(:currency, :currency_id),
    description: params[:description],
    email: params[:email],
    fax_no: params[:fax_no],
    gst_registration_no: params[:gst_registration_no],
    hst_registration_no: params[:hst_registration_no],
    language: params.dig(:language_option, :value),
    memo: params[:memo],
    other_phone_no: params[:other_phone_no],
    our_code_at_supplier: params[:our_code_at_supplier],
    payment_term_id: params.dig(:payment_terms, :payment_term_id),
    phone_no: params[:phone_no],
    postal_code: params[:postal_code],
    province_id: Province.find_or_create_by_description(
      dig_params(:province, [:description]),
      Country::CANADA_ID
    ).province_id,
    pst_registration_no: params[:pst_registration_no],
    supplier_code: params[:supplier_code],
    supplier_id: params[:supplier_id],
    website: params[:website],
    modified_by: @current_user.email
  )
  result = handler.call
  vendor_id = result[:supplier].supplier_id
  render json: {
    message: 'Vendor created',
    redirect_to: "/app/vendors/#{vendor_id}/"
  }
rescue ActiveRecord::RecordInvalid => e
  render json: {
    errors: e.record.errors,
    message: e.record.errors.full_messages.join(', '),
    redirect_to: '/app/vendors/new/'
  }, status: :bad_request
end

#formObject



171
172
173
174
175
176
177
178
# File 'app/controllers/vendors_controller.rb', line 171

def form
  render json: {
    payment_terms: PaymentTerm.select(:description, :payment_term_id).order(:description),
    currencies: Currency.where(
      company_code: Rails.application.config.company_code
    ).select(:code, :currency_id).distinct.order(:code)
  }
end

#indexObject



8
9
10
11
12
13
14
15
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
42
43
44
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
# File 'app/controllers/vendors_controller.rb', line 8

def index
  page_size = params[:page_size] || 10
  page = params[:page] || 1
  query = params[:query].to_s.strip.downcase
  limit = page_size.to_i
  offset = page_size.to_i * (page.to_i - 1)
  fields = [
    'supplier_id',
    'suppliers.description AS description',
    'address_line1',
    'address_line2',
    'postal_code',
    'city',
    'provinces.description AS province_description',
    'country.description AS country_description',
    'phone_no',
    'website',
    'memo',
    'suppliers.country_id',
    'suppliers.province_id',
    'active'
  ]

  records = Supplier
            .order("#{params['orderBy'] || 'supplier_id'} #{params['orderDirection'] || 'DESC'}")
            .includes(:country, :province)
            .joins(:country, :province)
            .select(*fields)

  search_fields = fields.map do |field|
    next if field === 'active'

    if field.include?('AS')
      field.split(' AS ').first
    else
      field
    end
  end.compact

  if query.present?
    query = query.downcase
    query_string = search_fields
                   .map { |field| "LOWER(#{field}) LIKE :query" }.join(' OR ')
    records = records.where(query_string, query: "%#{query}%")
  end

  total = records.count
  data = records.offset(offset).limit(limit).as_json(
    methods: [:comma_separated_address]
  )
  pages_count = (total.to_f / limit).ceil

  render json: {
    data: data,
    metadata: {
      total: total,
      limit: limit,
      offset: offset,
      pages_count: pages_count
    }
  }
end

#optionsObject



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'app/controllers/vendors_controller.rb', line 180

def options
  query = params[:query].to_s.strip.downcase
  limit = params[:limit] || 10
  offset = params[:offset] || 0
  fields = %w[
    supplier_id
    supplier_code
    description
  ]

  records = Supplier
            .order('supplier_id DESC')
            .select(*fields)

  if query.present?
    query = query.downcase
    query_string = fields
                   .map { |field| "LOWER(#{field}) LIKE :query" }.join(' OR ')
    records = records.where(query_string, query: "%#{query}%")
  end

  data = records.offset(offset).limit(limit).as_json

  render json: {
    data: data
  }
end

#showObject



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/vendors_controller.rb', line 71

def show
  id = params[:id]
  vendor = Supplier.find_by!(supplier_id: id).as_json(
    include: [:province],
    methods: %i[currency language_option payment_terms]
  )
  render json: {
    data: vendor
  }
rescue ActiveRecord::RecordNotFound
  @message = "Could not find vendor with id #{id}"
  render 'show', status: :not_found
end

#updateObject



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

def update
  handler = Vendors::Handlers::CreateOrUpdate.new(
    address_line1: params[:address_line1],
    address_line2: params[:address_line2],
    city: params[:city],
    country_id: Country::CANADA_ID,
    currency_id: params.dig(:currency, :currency_id),
    description: params[:description],
    email: params[:email],
    fax_no: params[:fax_no],
    gst_registration_no: params[:gst_registration_no],
    hst_registration_no: params[:hst_registration_no],
    language: params.dig(:language_option, :value),
    memo: params[:memo],
    other_phone_no: params[:other_phone_no],
    our_code_at_supplier: params[:our_code_at_supplier],
    payment_term_id: params.dig(:payment_terms, :payment_term_id),
    phone_no: params[:phone_no],
    postal_code: params[:postal_code],
    province_id: Province.find_or_create_by_description(
      dig_params(:province, [:description]),
      Country::CANADA_ID
    ).province_id,
    pst_registration_no: params[:pst_registration_no],
    supplier_code: params[:supplier_code],
    supplier_id: params[:supplier_id],
    website: params[:website],
    modified_by: @current_user.email
  )
  handler.call

  render json: {
    message: 'Vendor updated',
    redirect_to: "/app/vendors/#{params[:supplier_id]}/"
  }
rescue ActiveRecord::RecordInvalid => e
  render json: {
    errors: e.record.errors,
    message: e.record.errors.full_messages.join(', '),
    redirect_to: "/app/vendors/#{params[:supplier_id]}/"
  }, status: :bad_request
end