Class: EdiPartnersController

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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/controllers/edi_partners_controller.rb', line 71

def create
  description = params[:description]
  default_isa_id = params[:default_isa_id]
  repetition_separator = params[:repetition_separator]
  partner_is_active = params[:partner_is_active]
  duns_number = params[:duns_number]
  network_code = params[:network_code]
  entity_type = params[:entity_type][:type]
  head_office_id = entity_type == 'S' ? params[:supplier][:supplier_id] : params[:customer][:customer_id]

  edi_partner = EdiPartner.create_update_procedure(
    company_id: @app_mode.supports_partner_by_company? ? current_company.company_id : -1,
    default_isa_id: default_isa_id,
    description: description,
    duns_number: duns_number,
    head_office_id: head_office_id,
    active: partner_is_active,
    repetition_separator: repetition_separator,
    entity_type: entity_type,
    network_code: network_code,
    system_user: current_user.email.first(20),
    system_date: Time.now
  )

  render json: {
    redirect_to: "/app/edi/partners/#{edi_partner.edi_partner_id}/",
    message: 'Edi partner created successfully'
  }
end

#create_or_update_transactionObject



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

def create_or_update_transaction
  if params[:edi_partner_transaction_id].blank? && PartnerTransaction.exists?(
    transaction_set: params[:transaction_set],
    document_direction: params.dig(:document_direction_option, :value)
  )
    return render json: {
      message: 'A transaction with the same transaction set and document direction already exists'
    }, status: :bad_request
  end
  PartnerTransaction.create_update_procedure(
    edi_partner_transaction_id: params[:edi_partner_transaction_id],
    edi_partner_id: params[:edi_partner_id],
    sender_isa_qualifier: params[:sender_isa_qualifier],
    sender_isa_id: params[:sender_isa_id],
    receiver_isa_qualifier: params[:receiver_isa_qualifier],
    receiver_isa_id: params[:receiver_isa_id],
    sender_gs_id: params[:sender_gs_id],
    receiver_gs_id: params[:receiver_gs_id],
    element_separator: params[:element_separator],
    last_isa_control_no: params[:last_isa_control_no],
    last_gs_control_no: params[:last_gs_control_no],
    our_vendor_id: params[:our_vendor_id],
    transaction_set: params.dig(:transaction_set_option, :transaction_set),
    transaction_standard: params[:transaction_standard],
    transaction_version: params[:transaction_version],
    status: params[:status_option][:value],
    export_allowances_charges: params[:export_allowances_charges],
    memo: params[:memo],
    gs_version_release_code: params[:gs_version_release_code],
    document_direction: params[:document_direction_option][:value],
    acknowledgment_requested: params[:acknowledgment_requested],
    functional_identifier_code: params[:functional_identifier_code],
    use_receiver_isa_id_from_order: params[:use_receiver_isa_id_from_order]
  )
  render json: {
    message: params[:edi_partner_transaction_id].present? ? 'Transaction updated' : 'Transaction created'
  }
end

#destroyObject



135
136
137
138
139
# File 'app/controllers/edi_partners_controller.rb', line 135

def destroy
  edi_partner = EdiPartner.find(params[:id])
  edi_partner.destroy
  render json: { message: 'Edi partner deleted' }
end

#destroy_transactionObject



220
221
222
223
224
# File 'app/controllers/edi_partners_controller.rb', line 220

def destroy_transaction
  transaction = PartnerTransaction.find(params[:edi_transaction_id])
  transaction.destroy
  render json: { message: 'Transaction deleted' }
end

#indexObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/edi_partners_controller.rb', line 7

def index
  query = params[:query] || ''
  limit = params[:limit] || 10
  offset = params[:offset] || 0
  entity_type = params.dig(:entity_type, :type)
  order_by = params[:order_by].presence || 'edi_partner_id'
  order_direction = params[:order_direction].presence || 'DESC'

  data,  = EdiPartner.from_procedure(
    company_id: @app_mode.supports_partner_by_company? ? current_company.company_id : -1,
    entity_type: entity_type,
    query: query,
    limit: limit,
    offset: offset,
    grouping: "#{order_by} #{order_direction}"
  ).values_at(:data, :metadata)

  render json: {
    data: data,
    metadata: 
  }
end

#partners_by_companyObject



398
399
400
401
402
# File 'app/controllers/edi_partners_controller.rb', line 398

def partners_by_company
  company_id = current_company_id
  results = EdiPartner.where(company_id: company_id)
  render json: results
end

#payment_terms_translationsObject



265
266
267
268
269
270
271
272
273
274
275
# File 'app/controllers/edi_partners_controller.rb', line 265

def payment_terms_translations
  edi_partner = EdiPartner.find(params[:edi_partner_id])
  limit = params[:limit].to_i || 10
  offset = params[:offset].to_i || 0
  @records = EdiPaymentTermsTranslation.table_data(
    edi_partner_id: params[:edi_partner_id],
    limit: limit,
    offset: offset
  )
  @total = PaymentTerm.count
end

#qualifier_code_optionsObject



383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'app/controllers/edi_partners_controller.rb', line 383

def qualifier_code_options
  records = EdiTransactionQualifier.select(
    :transaction_qualifier_id,
    :description,
    :code
  ).where(
    transaction_set: params[:transaction_set],
    extracted_from_segment: params[:extracted_from_segment]
  )

  render json: {
    data: records
  }
end

#segment_fieldsObject



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'app/controllers/edi_partners_controller.rb', line 246

def segment_fields
  result = SegmentField.where_procedure(
    transaction_id: params[:transaction_id],
    segment_id: params[:segment_id],
    limit: params[:limit].to_i,
    offset: params[:offset].to_i
  )

  data = result[:data]
  total = result[:total]

  render json: {
    data: data,
    metadata: {
      total: total
    }
  }
end

#segmentsObject



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'app/controllers/edi_partners_controller.rb', line 226

def segments
  result = TransactionSegment.where_procedure(
    transaction_id: params[:transaction_id],
    document_transaction_set: params[:document_transaction_set],
    document_direction: params[:document_direction],
    limit: params[:limit].to_i,
    offset: params[:offset].to_i
  )

  data = result[:data]
  total = result[:total]

  render json: {
    data: data,
    metadata: {
      total: total
    }
  }
end

#showObject



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/edi_partners_controller.rb', line 30

def show
  partner = EdiPartner::View.find(params[:id])
  attributes = partner.attributes
  data = if partner.entity_type == 'S'
           attributes.merge(
             entity_type: {
               type: partner.entity_type,
               label: 'Supplier'
             },
             supplier: {
               supplier_id: partner.head_office_id,
               description: partner.head_office_description,
               supplier_code: partner.head_office_code
             }
           )
         else
           attributes.merge(
             entity_type: {
               type: partner.entity_type,
               label: 'Customer'
             },
             customer: {
               customer_id: partner.head_office_id,
               code: partner.head_office_code,
               name: partner.head_office_description
             }
           )
         end

  # data.merge!(
  #   company: {
  #     company_code: '',
  #     company_name_e: current_company.company_name_e,
  #   },
  # )

  render json: {
    data: data
  }
end

#transactionsObject



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

def transactions
  edi_partner = EdiPartner.find(params[:edi_partner_id])
  query = params[:query] || ''
  limit = params[:limit] || 10
  offset = params[:offset] || 0

  @data, @metadata = fetch_records(
    records: edi_partner.transactions,
    order_by: 'transaction_set',
    order: 'DESC',
    query: query,
    limit: limit,
    offset: offset
  ).values_at(:data, :metadata)

  @data = @data.map do |transaction|
    transaction.attributes.merge({
                                   status_option: {
                                     value: transaction.status,
                                     label: status_label(transaction.status)
                                   },
                                   document_direction_option: {
                                     value: transaction.document_direction,
                                     label: document_direction_label(transaction.document_direction)
                                   },
                                   transaction_set_option: {
                                     transaction_set: transaction.transaction_set,
                                     document_name: EdiDocument.find_by(
                                       transaction_set: transaction.transaction_set
                                     )&.document_name
                                   }
                                 })
  end

  render json: {
    data: @data,
    metadata: @metadata
  }
end

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

def update
  edi_partner_id = @app_mode.supports_partner_by_company? ? current_company.edi_partners.find(params[:id]) : params[:id]
  partner = EdiPartner.find(edi_partner_id)

  description = params[:description]
  default_isa_id = params[:default_isa_id]
  repetition_separator = params[:repetition_separator]
  partner_is_active = params[:partner_is_active]
  duns_number = params[:duns_number]
  entity_type = partner.entity_type
  network_code = params[:network_code]
  head_office_id = entity_type == 'S' ? params[:supplier][:supplier_id] : params[:customer][:customer_id]

  EdiPartner.create_update_procedure(
    company_id: @app_mode.supports_partner_by_company? ? current_company.company_id : -1,
    edi_partner_id: edi_partner_id,
    default_isa_id: default_isa_id,
    description: description,
    duns_number: duns_number,
    entity_type: entity_type,
    head_office_id: head_office_id,
    active: partner_is_active,
    repetition_separator: repetition_separator,
    system_user: current_user.email.first(20),
    system_date: Time.zone.now,
    network_code: network_code
  )

  render json: {
    redirect_to: "/app/edi/partners/#{edi_partner_id}/",
    message: 'Edi partner created successfully'
  }
end

#update_fields_from_table_editsObject



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'app/controllers/edi_partners_controller.rb', line 321

def update_fields_from_table_edits
  uneditable_fields_for_system_records = {
    'type' => 'type',
    'attribute' => 'attribute',
    'length_or_mask' => 'format',
    'value' => 'value',
    'qualifier_code' => 'qualifier code',
    'active' => 'active status'
  }

  params[:edits].each do |row_edits|
    row_hash = row_edits[:rowHash]
    edits = row_edits[:edits]

    edi_field = SegmentField.find(row_hash[:edi_field_id])
    forbid_system_record_edits = false

    if forbid_system_record_edits &&
       edi_field.system_record &&
       (edit_keys = %w[type attribute length_or_mask value qualifier_code active] & edits.keys).any?

      message = 'You cannot edit the ' + edit_keys.map { |key|
        uneditable_fields_for_system_records[key]
      }.to_sentence + ' of system records'
      return render json: { message: message }, status: :bad_request
    end

    if !edi_field.type == 'Data' && edits.keys.include?('qualifier_code')
      return render json: {
        message: 'You can only edit the qualifier code of non system record data type fields'
      }, status: :bad_request
    end

    SegmentField.update_procedure(
      edi_field_id: edi_field.edi_field_id,
      edi_segment_id: edi_field.edi_segment_id,
      edi_field_code: edi_field.edi_field_code,
      description: edits[:description] || edi_field.description,
      type: edits.dig(:type, :value) || edi_field.type,
      sequence_no: edi_field.sequence_no,
      field_attribute: edits.dig(:attribute, :value) || edi_field[:attribute],
      min_length: edi_field.min_length,
      max_length: edi_field.max_length,
      length_or_mask: edits[:length_or_mask] || edi_field.length_or_mask,
      value: edits[:value] || edi_field.value,
      qualifier_code: edits.dig(:qualifier_code, :value) || edi_field.qualifier_code,
      active: edits[:active].nil? ? edi_field.active : edits[:active],
      system_record: edi_field.system_record,
      mandatory: edi_field.mandatory,
      xml_reference: edi_field.xml_reference,
      xml_path: edi_field.xml_path,
      occurence_no: edi_field.occurence_no,
      edi_field_template_id: edi_field.edi_field_template_id
    )
  end

  message = params[:edits].length > 1 ? 'Fields updated' : 'Field updated'
  render json: {
    message: message
  }
end

#update_segments_from_table_editsObject



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'app/controllers/edi_partners_controller.rb', line 277

def update_segments_from_table_edits
  table_edits = params[:edits]
  table_edits.each do |row_edits|
    row_hash = row_edits[:rowHash]
    edits = row_edits[:edits]

    edi_segment = TransactionSegment.find(row_hash[:edi_segment_id])

    if edi_segment.partner_transaction.document_direction != 'I' && edits.keys.include?('qualifier_code')
      return render json: {
        message: 'You can only edit the qualifier code of incoming transactions'
      }, status: :bad_request
    end

    if edi_segment.mandatory && edits.keys.include?('active')
      return render json: {
        message: 'You cannot change the active status of mandatory segments'
      }, status: :bad_request
    end

    TransactionSegment.create_update_procedure(
      edi_segment_id: edi_segment.edi_segment_id,
      edi_partner_transaction_id: edi_segment.edi_partner_transaction_id,
      edi_segment_level: edi_segment.edi_segment_level,
      edi_segment_code: edi_segment.edi_segment_code,
      name: edits[:name] || edi_segment.name,
      max_occurences: edi_segment.max_occurences,
      sequence_no: edi_segment.sequence_no,
      qualifier_code: edits.dig(:qualifier_code, :value) || edi_segment.qualifier_code,
      mandatory: edi_segment.mandatory,
      active: edits[:active].nil? ? edi_segment.active : edits[:active],
      system_record: edi_segment.system_record,
      tax_segment: edi_segment.tax_segment,
      parent_xml_path: edi_segment.parent_xml_path,
      edi_segment_template_id: edi_segment.edi_segment_template_id
    )
  end

  message = table_edits.length > 1 ? 'Segments updated' : 'Segment updated'
  render json: {
    message: message
  }
end