Class: CustomersController

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

#companies_by_customerObject



270
271
272
273
274
275
276
277
278
279
280
# File 'app/controllers/customers_controller.rb', line 270

def companies_by_customer
  customer_id = Customer.find_by!(customer_id: params[:customer_id], tenant_id: current_tenant_id).customer_id

  result = Customer.get_customer_by_company(customer_id: customer_id, company_id: nil)
  mapped_result = result.map do |r|
    { customer_id: r[:customer_id], company_id: r[:company_id], active: r[:active] }
  end
  render json: {
    result: mapped_result
  }
end

#create_or_updateObject



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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'app/controllers/customers_controller.rb', line 101

def create_or_update
  customer_id = Customer.find_by(customer_id: params[:customer_id], tenant_id: current_tenant_id)&.customer_id

  if customer_id.present? && customer_id.to_i != 0
    existing_customer = Customer.find_by(customer_id: customer_id, tenant_id: current_tenant_id)
    return render json: { error: 'Customer not found for this tenant' }, status: :not_found unless existing_customer

    customer_id = existing_customer.customer_id
  end

  # Create a new customer service instance
  customer_service = CustomerService.new(params[:system_user] || 'Admin')

  # Create or update the customer with params
  result = customer_service.create_or_update(
    customer_id: customer_id,
    tenant_id: current_tenant_id,
    code: params[:code],
    type: params[:type],
    name: params[:name],
    known_as: params[:known_as],
    address_line1: params[:address_line1],
    address_line2: params[:address_line2],
    city: params[:city],
    province_id: params[:province_id],
    postal_code: params[:postal_code],
    phone_1: params[:phone_1],
    phone_2: params[:phone_2],
    fax: params[:fax],
    email: params[:email],
    website: params[:website],
    attention: params[:attention],
    language: params[:language],
    member_of_a_chain: params[:member_of_a_chain],
    hierarchy: params[:hierarchy],
    belongs_to_chain_id: params[:belongs_to_chain_id],
    belongs_to_dc_id: params[:belongs_to_dc_id],
    belongs_to_ho_id: params[:belongs_to_ho_id],
    tax_exemption_code: params[:tax_exemption_code],
    pst_number: params[:pst_number],
    gst_irs_number: params[:gst_irs_number],
    edi_store_code: params[:edi_store_code],
    store_number: params[:store_number],
    active: params[:active],
    reserved_record: params[:reserved_record],
    memo: params[:memo],
    consolidate_invoice_by: params[:consolidate_invoice_by],
    consolidate_asn_by: params[:consolidate_asn_by],
    category_id: params[:category_id],
    stage: params[:stage],
    member: params[:member],
    region: params[:region],
    regional_code: params[:regional_code],
    renewal: params[:renewal]
  )

  # If it's a new customer, create company associations
  if !customer_id && result[:customer_id].present?
    customer_service.create_customer_company_associations(
      result[:customer_id],
      current_tenant.company_ids,
      params[:memo]
    )

    return render json: {
      message: 'Customer created or updated successfully.',
      redirect_to: "/app/settings/customers/#{result[:customer_id]}/company"
    }
  end

  render json: {
    message: 'Customer created or updated successfully.'
  }
end

#create_or_update_customer_by_companyObject



176
177
178
179
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
207
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
# File 'app/controllers/customers_controller.rb', line 176

def create_or_update_customer_by_company
  customer_key = params[:customer_key]
  customer_id = Customer.find_by(customer_id: params[:customer_id], tenant_id: current_tenant_id).customer_id
  company_code = params[:company_id].to_s
  company_id = params[:company_id]
  active = params[:active]
  currency_id = params[:currency_id]
  ship_from_warehouse_id = params[:ship_from_warehouse_id]
  shipping_priority = params[:shipping_priority]
  credit_checking = params[:credit_checking]
  use_store_province_for_taxes = params[:use_store_province_for_taxes]
  freight_terms_id = params[:freight_terms_id]
  ship_via_id = params[:ship_via_id]
  minimum_filling_rate = params[:minimum_filling_rate]
  print_statements = params[:print_statements]
  print_invoices = params[:print_invoices]
  invoice_to_store = params[:invoice_to_store]
  shipping_document = params[:shipping_document]
  accept_back_orders = params[:accept_back_orders]
  payment_terms_id = params[:payment_terms_id]
  discount_on_net = params[:discount_on_net]
  credit_limit = params[:credit_limit]
  minimum_order_limit = params[:minimum_order_limit]
  maximum_order_limit = params[:maximum_order_limit]
  po_required = params[:po_required]
  hold_credit = params[:hold_credit]
  print_mh10_labels = params[:print_mh10_labels]
  mh10_label_format_id = params[:mh10_label_format_id]
  print_packing_slip = params[:print_packing_slip]
  packing_slip_type = params[:packing_slip_type]
  our_code_in_client_system = params[:our_code_in_client_system]
  bank_id = params[:bank_id]
  bank_branch_id = params[:bank_branch_id]
   = params[:bank_account_id]
  box_distribution_type = params[:box_distribution_type]
  memo = params[:memo]
  number_invoice_copies_to_print = params[:number_invoice_copies_to_print]
  number_of_upcs_per_box = params[:number_of_upcs_per_box]
  consolidated_invoice = params[:consolidated_invoice]
  system_user = params[:system_user]

  Customer.create_or_update_customer_by_company_procedure(
    customer_key: customer_key,
    customer_id: customer_id,
    company_code: company_code,
    company_id: company_id,
    active: active,
    currency_id: currency_id,
    ship_from_warehouse_id: ship_from_warehouse_id,
    shipping_priority: shipping_priority,
    credit_checking: credit_checking,
    use_store_province_for_taxes: use_store_province_for_taxes,
    freight_terms_id: freight_terms_id,
    ship_via_id: ship_via_id,
    minimum_filling_rate: minimum_filling_rate,
    print_statements: print_statements,
    print_invoices: print_invoices,
    invoice_to_store: invoice_to_store,
    shipping_document: shipping_document,
    accept_back_orders: accept_back_orders,
    payment_terms_id: payment_terms_id,
    discount_on_net: discount_on_net,
    credit_limit: credit_limit,
    minimum_order_limit: minimum_order_limit,
    maximum_order_limit: maximum_order_limit,
    po_required: po_required,
    hold_credit: hold_credit,
    print_mh10_labels: print_mh10_labels,
    mh10_label_format_id: mh10_label_format_id,
    print_packing_slip: print_packing_slip,
    packing_slip_type: packing_slip_type,
    our_code_in_client_system: our_code_in_client_system,
    bank_id: bank_id,
    bank_branch_id: bank_branch_id,
    bank_account_id: ,
    box_distribution_type: box_distribution_type,
    memo: memo,
    number_invoice_copies_to_print: number_invoice_copies_to_print,
    number_of_upcs_per_box: number_of_upcs_per_box,
    consolidated_invoice: consolidated_invoice,
    system_user: system_user || 'Admin'
  )

  render json: {
    message: 'Customer by company created or updated successfully.'
  }
end

#customer_by_companyObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/controllers/customers_controller.rb', line 86

def customer_by_company
  customer_id = Customer.find_by!(customer_id: params[:customer_id], tenant_id: current_tenant_id).customer_id

  company_id = params[:company_id]
  result = Customer.get_customer_by_company(customer_id: customer_id, company_id: company_id)
  render json: {
    result: result,
    payment_terms: PaymentTerm.all,
    freight_terms: FreightTerm.all,
    warehouses: Warehouse.all,
    currencies: Currency.all,
    ship_via: ShippingCompany.all
  }
end

#distribution_centers_by_customerObject



264
265
266
267
268
# File 'app/controllers/customers_controller.rb', line 264

def distribution_centers_by_customer
  belongs_to_ho_id = params[:belongs_to_ho_id]
  result = Customer.where(hierarchy: 2, belongs_to_ho_id: belongs_to_ho_id)
  render json: result
end

#indexObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/customers_controller.rb', line 38

def index
  display_length = params[:limit] || 10
  search_value = params[:query] || ''
  display_start_from_page_no = params[:offset] || 1
  order_by = params[:order_by] || 'customer_id'
  order_direction = params[:order_direction] || 'DESC'
  grouping = "#{order_by} #{order_direction}"
  tenant_id = current_tenant_id

  result = Customer.customers_by_page(
    display_length: display_length,
    search_value: search_value,
    display_start_from_page_no: display_start_from_page_no,
    grouping: grouping,
    tenant_id: tenant_id
  )
  render json: {
    data: result[:data],
    metadata: result[:metadata]
  }
end

#optionsObject



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

def options
  query = params[:query] || ''
  customers = Customer
              .select(
                :customer_id,
                :name,
                :code,
                :address_line1,
                :address_line2,
                :city,
                :province_id,
                :postal_code
              )
              .includes(province: :country)
              .where('name LIKE :query OR code LIKE :query', query: "%#{query}%")
              .where(reserved_record: [false, nil], tenant_id: current_tenant_id, hierarchy: '1')
              .order(:code)
              .limit(10)

  customer_options = customers.map do |customer|
    customer.attributes.merge('address' => customer.address)
  end

  render json: {
    data: customer_options
  }
end

#showObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/controllers/customers_controller.rb', line 60

def show
  customer_id = params[:id]
  show_active_only = params[:show_active_only] || 0
  text = params[:text] || ''
  customer = nil

  unless customer_id.to_i.zero?
    customer = Customer.get_customer_by_key(
      tenant_id: current_tenant_id,
      customer_id: customer_id,
      show_active_only: show_active_only,
      text: text
    )
  end

  render json: {
    result: customer,
    customer_hierarchy_types: TypesMaster.where(element: 'CUSTOMER HIERARCHY'),
    customer_types: TypesMaster.where(element: 'CUSTOMER TYPE'),
    consolidate_invoice_by_types: TypesMaster.where(element: 'CONSOLIDATE INVOICE BY'),
    consolidate_asn_by_types: TypesMaster.where(element: 'CONSOLIDATE ASN BY'),
    head_offices: Customer.where(tenant_id: current_tenant_id, hierarchy: 1),
    distribution_centers: Customer.where(tenant_id: current_tenant_id, hierarchy: 2)
  }
end