Class: ItemLedgerEntriesController

Inherits:
CrmController show all
Includes:
Controllers::Showable
Defined in:
app/controllers/item_ledger_entries_controller.rb

Overview

== Schema Information

Table name: item_ledger_entries

id :integer not null, primary key
store_id :integer
item_id :integer
category :string(255)
gl_date :date
quantity :integer
currency :string(255)
unit_cost :decimal(10, 4)
total_cost :decimal(10, 4)
description :string(255)
creator_id :integer
updater_id :integer
created_at :datetime
updated_at :datetime
shipment_receipt_item_id :integer
landed_cost_id :integer
old_unit_cogs :decimal(10, 4)
new_unit_cogs :decimal(10, 4)
location :string(255)
ledger_company_account_id :integer
ledger_transaction_id :integer
ledger_detail_project_id :integer
invoice_id :integer
rma_item_id :integer
old_qty_on_hand :integer
new_qty_on_hand :integer
business_unit_id :integer
quantity_eval :integer
cycle_count_item_id :integer
item_kit_id :integer

Constant Summary

Constants included from Controllers::ReferenceFindable

Controllers::ReferenceFindable::ID_EMBEDDED_PATTERNS

Constants included from Controllers::AnalyticsEvents

Controllers::AnalyticsEvents::MAX_QUEUED_EVENTS, Controllers::AnalyticsEvents::SESSION_KEY

Constants included from Controllers::ErrorRendering

Controllers::ErrorRendering::NON_CONTENT_PATH_PREFIXES

Constants included from Www::SeoHelper

Www::SeoHelper::AWARDS, Www::SeoHelper::CA_ADDRESS, Www::SeoHelper::CA_BUSINESS_HOURS, Www::SeoHelper::CA_CONTACT_POINT, Www::SeoHelper::CA_CURRENCIES, Www::SeoHelper::CA_DESCRIPTION, Www::SeoHelper::CA_FOUNDING_DATE, Www::SeoHelper::CA_GLOBAL_LOCATION_NUMBER, Www::SeoHelper::CA_LEGAL_NAME, Www::SeoHelper::CA_LOCAL_BUSINESS, Www::SeoHelper::CA_ONLINE_STORE, Www::SeoHelper::CA_RETURN_POLICY, Www::SeoHelper::CA_SALES_DEPARTMENT, Www::SeoHelper::CA_SERVICE_AREA, Www::SeoHelper::CA_URL, Www::SeoHelper::CA_VAT_ID, Www::SeoHelper::CA_WAREHOUSE_DEPARTMENT, Www::SeoHelper::CA_WAREHOUSE_HOURS, Www::SeoHelper::COMPANY_EMAIL, Www::SeoHelper::COMPANY_LOGO, Www::SeoHelper::COMPANY_NAME, Www::SeoHelper::COMPANY_SLOGAN, Www::SeoHelper::EXPERTISE, Www::SeoHelper::FAX_NUMBER, Www::SeoHelper::GS1_COMPANY_PREFIX, Www::SeoHelper::ISO6523_CODE, Www::SeoHelper::PAYMENT_METHODS, Www::SeoHelper::PHONE_NUMBER, Www::SeoHelper::PRIMARY_NAICS, Www::SeoHelper::REFUND_TYPE, Www::SeoHelper::RETURN_FEES, Www::SeoHelper::RETURN_METHOD, Www::SeoHelper::RETURN_POLICY_CATEGORY, Www::SeoHelper::SECONDARY_NAICS, Www::SeoHelper::SOCIAL_PROFILES, Www::SeoHelper::US_ADDRESS, Www::SeoHelper::US_BUSINESS_HOURS, Www::SeoHelper::US_CONTACT_POINT, Www::SeoHelper::US_CURRENCIES, Www::SeoHelper::US_DESCRIPTION, Www::SeoHelper::US_FOUNDING_DATE, Www::SeoHelper::US_GLOBAL_LOCATION_NUMBER, Www::SeoHelper::US_IMAGE, Www::SeoHelper::US_LEGAL_NAME, Www::SeoHelper::US_LOCAL_BUSINESS, Www::SeoHelper::US_ONLINE_STORE, Www::SeoHelper::US_RETURN_POLICY, Www::SeoHelper::US_SALES_DEPARTMENT, Www::SeoHelper::US_SERVICE_AREA, Www::SeoHelper::US_TAX_ID, Www::SeoHelper::US_URL, Www::SeoHelper::US_WAREHOUSE_DEPARTMENT, Www::SeoHelper::US_WAREHOUSE_HOURS

Constants included from IconHelper

IconHelper::CUSTOM_ICON_MAP, IconHelper::CUSTOM_SVG_DIR, IconHelper::DEFAULT_FAMILY

Instance Method Summary collapse

Methods included from Controllers::Showable

#perform_show, #show

Methods inherited from CrmController

#access_denied, #context_id, #context_object, #crm_home_path, #current_ability, #default_url_options, #download_temp, #get_tempfile_path_for_download, #initialize_crm_lazy_chunks, #record_not_found, #redirect_to_job_or_fallback, #render_edit_action, #set_context, #set_download_path, #stash_file_for_temp_download

Methods inherited from ApplicationController

#account_impersonated?, #add_to_flash, #append_token, #bypass_forgery_protection?, #chat_enabled?, #cloudflare_cleared?, #default_catalog, #default_url_options, #enable_turbo_frames, #find_publication, #fix_invalid_accept_header, #init_js_utils, #is_globals_call?, #layout_by_resource, #locale_store, #redirect_to, #require_employee_for_crm, #set_base_host, #set_real_ip, #set_report_errors_for, #should_render_layout?, #stamp_impersonation_context, #warmlyyours_canada_ip?, #warmlyyours_ip?, #y

Methods included from Controllers::ReturnPathHandling

#check_for_return_path, #redirect_to_return_path_or_default

Methods included from Controllers::AnalyticsEvents

#consume_queued_analytics_events, #track_event

Methods included from Controllers::DeviceDetection

#device_detector, #is_ie?

Methods included from Controllers::SubdomainDetection

#is_crm_request?, #is_www_request?, #json_request?

Methods included from Controllers::TrackingDetection

#bot_request?, #gdpr_country?, #gdpr_country_data, #prevent_bots, #set_tracking_cookie, #track_visitor?

Methods included from Controllers::AcceleratedFileSending

#send_file_accelerated, #send_upload_accelerated

Methods included from Controllers::ErrorRendering

#excp_string, #mail_to_for_error_reporting, #render_400, #render_404, #render_406, #render_410, #render_500, #render_invalid_authenticity_token, #render_ip_spoof_error, #safe_referer_or_fallback

Methods included from Controllers::TurnstileVerification

#load_turnstile_script_tag, #turnstile_lazy_widget, #turnstile_script_tag, #turnstile_widget, #validate_turnstile!

Methods included from Controllers::CloudflareCaching

edge_cached, #edge_cached_action?, #reset_cloudflare_cache, #set_cloudflare_cache, #skip_session

Methods included from Controllers::Webpackable

#preload_webpack_fonts, #webpack_css_include, #webpack_css_url, #webpack_js_include, #wpd_is_running?

Methods included from Controllers::Localizable

#cloudflare_country_locale, #determine_request_locale, #geocoder_locale, #guest_user_locale_check, #locale_optional_www_auth_path?, #param_locale, #set_locale, #set_request_locale, #skip_localization?, #warmlyyours_ip_locale

Methods included from Controllers::Authenticable

#access_denied, #authenticate_account, #authenticate_account!, #authenticate_account_from_login_token!, #authenticate_account_from_token!, #check_is_a_manager, #check_is_a_sales_manager, #check_is_an_admin, #check_is_an_employee, #check_party, #clear_mismatched_guest_user, #create_guest_user, #credentials?, #current_or_guest_user, #current_or_guest_user_id_read_only, #current_user, #devise_mapping, #fully_logged_in?, #generate_bot_id, #guest_user, #identifiable?, #init_current_user, #initialize_guest, #load_context_user, #logging_in, #resource, #resource_name, #restrict_access_for_non_employees, #scrubbed_request_path, #user_object, #warn_on_session_guest_id_leak

Methods included from ApplicationHelper

#better_number_to_currency, #check_force_logout, #check_or_cross, #check_or_times, #error_messages, #general_disclaimer_on_product_installation_and_local_codes, #gridjs_from_html_table, #gridjs_table, #is_wy_ip, #line_break, #parent_layout, #pass_or_fail, #render_error_messages_list, #render_video_card, #resolved_auth_form_turbo_frame, #return_path_or, #safe_css_color, #set_return_path_if_present, #set_section_if_present, #tab_frame_id, #to_underscore, #track_page?, #turbo_section_wrapper, #turbo_tabs_request?, #url_on_same_domain_as_request, #widget_index_daily_focus_index_path, #working_hours?, #yes_or_no, #yes_or_no_highlighted, #yes_or_no_with_check_or_cross, #youtube_video

Methods included from UppyUploaderHelper

#file_uploader, #image_uploader, #large_file_uploader_s3, #lead_sketch_uploader, #rma_image_uploader, #rma_image_uploader_s3, #uppy_uploader, #video_uploader

Methods included from Www::ImagesHelper

#image_asset_tag, #image_asset_url

Methods included from Www::SeoHelper

#add_page_schema, #canada?, #company_social_links, #ensure_context_json, #json_ld_script_tag, #local_business_schema, #online_store_id, #online_store_schema, #page_main_entity, #page_main_entity_json, #render_auto_collection_page_schema, #render_collection_page_schema, #render_local_business_schema, #render_online_store_schema, #render_page_schemas, #render_page_video_schemas, #render_webpage_schema, #render_webpage_schema_with_collections, #usa?

Methods included from UrlsHelper

#catalog_breadcrumb_links, #catalog_link, #catalog_link_for_product_line, #catalog_link_for_sku, #cms_link, #delocalized_path, #path_to_sales_product_sku, #path_to_sales_product_sku_for_product_line, #path_to_sales_product_sku_for_product_line_slug, #product_line_from_catalog_link, #protocol_neutral_url, #sanitize_external_url, #valid_external_url?

Methods included from IconHelper

#account_nav_icon, #fa_icon, #star_rating_html

Instance Method Details

#check_for_errors(line_item_params) ⇒ Object



478
479
480
481
482
483
484
485
# File 'app/controllers/item_ledger_entries_controller.rb', line 478

def check_for_errors(line_item_params)
  errors = 0
  line_item_params.each do |_index, attrs|
    errors += 1 if attrs[:store_item_id].blank? || attrs[:qty].blank? || (attrs[:qty].to_i <= 0) || (attrs[:qty].to_i > attrs[:qty_on_hand].to_i)
    errors += check_for_serial_number_errors(attrs)
  end
  errors
end

#check_for_item_committed_errors(attrs) ⇒ Object



503
504
505
506
507
508
509
510
# File 'app/controllers/item_ledger_entries_controller.rb', line 503

def check_for_item_committed_errors(attrs)
  errors = 0
  if attrs[:store_item_id].present?
    si = StoreItem.find(attrs[:store_item_id])
    errors += 1 if si.qty_committed > (si.qty_on_hand + attrs[:qty].to_i)
  end
  errors
end

#check_for_serial_number_errors(attrs) ⇒ Object



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# File 'app/controllers/item_ledger_entries_controller.rb', line 487

def check_for_serial_number_errors(attrs)
  errors = 0
  if attrs[:store_item_id].present?
    si = StoreItem.find(attrs[:store_item_id])
    if si.item.require_reservation?
      if attrs[:serial_number_id].blank?
        errors += 1
      else
        serial_number_qty = si.serial_numbers.where(id: attrs[:serial_number_id]).first.qty
        errors += 1 if serial_number_qty != attrs[:qty].to_i.abs
      end
    end
  end
  errors
end

#cogs_adjustmentObject



359
360
361
# File 'app/controllers/item_ledger_entries_controller.rb', line 359

def cogs_adjustment
  @line_items = ActiveSupport::HashWithIndifferentAccess.new('0' => { location: nil })
end

#do_cogs_adjustmentObject



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'app/controllers/item_ledger_entries_controller.rb', line 363

def do_cogs_adjustment
  @line_items = params[:line_items]
  store_id = params.dig(:adjustment, :store)
  if store_id.blank?
    flash.now[:error] = 'You must specify a valid store to continue.'
    render :cogs_adjustment, status: :unprocessable_entity
  else
    store = Store.find(store_id)
    date = begin
      Date.strptime(params.dig(:adjustment, :date), '%Y-%m-%d')
    rescue StandardError
      Date.current
    end
    description = params.dig(:adjustment, :description).presence

    errors = 0
    params[:line_items].each do |_index, attrs|
      errors += 1 if attrs[:store_item_id].blank? || attrs[:new_cogs].blank? || BigDecimal(attrs[:new_cogs]).negative?
    end

    if errors.positive?
      flash.now[:error] = 'You must specify a valid item and new cogs of 0 or more on each line.'
      render :cogs_adjustment, status: :unprocessable_entity
    else
      ItemLedgerEntry.cogs_adjustment(store, params[:line_items], date, description)
      flash[:info] = 'COGS adjustment has been processed successfully.'
      redirect_to item_ledger_entries_path
    end
  end
end

#do_import_from_excelObject



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
127
128
129
130
131
132
# File 'app/controllers/item_ledger_entries_controller.rb', line 100

def do_import_from_excel
  options = {}
  items_list = []
  imported_store_transfer_items_excel = params.dig(:upload, :imported_store_transfer_items_excel)
  if imported_store_transfer_items_excel
    file_path = imported_store_transfer_items_excel.tempfile.path.to_s
    require 'roo'
    st_items_xlsx = Roo::Spreadsheet.open(file_path)
    skipped_skus = []
    st_items_xlsx.parse(headers: true, clean: true).each_with_index do |row, index|
      next unless index >= 1

      next unless row.map { |_k, v| v }.uniq.compact.present?

      options[:store_id] = row['store_id'].to_i if row['store_id'].present? && index == 1
      next unless row['sku'].present? && row['quantity'].present?

      item = Item.find_by(sku: row['sku'])
      unless item
        skipped_skus << row['sku']
        next
      end

      items_list << [item.id, row['quantity'].to_i]
    end
    options[:items_list] = items_list.to_h
    flash[:warning] = "Skipped unknown SKU(s): #{skipped_skus.join(', ')}" if skipped_skus.present?
    redirect_to store_transfer_item_ledger_entries_path(options: options.stringify_keys)
  else
    flash.now[:error] = 'No file was uploaded'
    render :import_from_excel, status: :unprocessable_entity
  end
end

#do_inventory_adjustmentObject



315
316
317
318
319
320
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
# File 'app/controllers/item_ledger_entries_controller.rb', line 315

def do_inventory_adjustment
  @adjustment = OpenStruct.new({ date: Date.current }.merge(params[:adjustment] || {}))
  @line_items = params[:line_items]
  store_id = @adjustment.store
  if store_id.blank?
    flash.now[:error] = 'You must specify a valid store to continue.'
    render :inventory_adjustment, status: :unprocessable_entity
  else
    store = Store.find(store_id)
    date = begin
      Date.strptime(@adjustment.date, '%Y-%m-%d')
    rescue StandardError
      Date.current
    end
    description = @adjustment.description.presence
    override_kit_restriction = @adjustment.override_kit_restriction.to_b
    errors = 0
    qty_committed_errors = 0
    @line_items.each do |_index, attrs|
      errors += 1 if attrs[:store_item_id].blank? || attrs[:qty].blank?
      if attrs[:qty].present? && attrs[:qty].to_i.negative?
        errors += check_for_serial_number_errors(attrs)
        qty_committed_errors += check_for_item_committed_errors(attrs)
      end
    end

    if errors.positive? || qty_committed_errors.positive?
      flash.now[:error] = 'You must specify a valid item and quantity on each line. You must specify a serial number with matching positive qty if item requested requires reservation and has a negative quantity.' if errors.positive?
      flash.now[:error] = "You cannot reduce an item's quantity below its available quantity (on hand - committed)." if qty_committed_errors.positive?
      render :inventory_adjustment, status: :unprocessable_entity
    else
      begin
        ItemLedgerEntry.inventory_adjustment(store, @line_items, date, description, override_kit_restriction)
        flash[:info] = 'Inventory adjustment has been processed successfully. Please note that kit might take up to 30 seconds to have their new stock updated.'
        redirect_to_return_path_or_default item_ledger_entries_path
      rescue ItemLedgerEntry::ItemLedgerEntryError => e
        flash.now[:error] = e
        render :inventory_adjustment, status: :unprocessable_entity
      end

    end
  end
end

#do_issue_to_accountObject



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'app/controllers/item_ledger_entries_controller.rb', line 255

def 
  @line_items = params[:line_items]
  adjustment_params = params.fetch(:adjustment, {})
  store_id = adjustment_params[:store]
   = adjustment_params[:company_account_id]
  if store_id.blank? || .blank?
    flash.now[:error] = 'You must specify a valid store and account.'
    render :issue_to_account, status: :unprocessable_entity
  else
    store = Store.find(store_id)
     = LedgerCompanyAccount.find()
    business_unit = BusinessUnit.where(id: adjustment_params[:business_unit_id]).first
    project = LedgerDetailProject.where(id: adjustment_params[:project_id]).first
    date = begin
      Date.strptime(adjustment_params[:date], '%Y-%m-%d')
    rescue StandardError
      Date.current
    end

    if .requires_business_unit? && business_unit.nil?
      flash.now[:error] = 'You must specify a business unit if issuing to a profit and loss account'
      render(:issue_to_account, status: :unprocessable_entity) and return
    elsif .requires_business_unit? && business_unit.company_id != store.company_id
      flash.now[:error] = 'Business unit must belong to the same company as the store'
      render(:issue_to_account, status: :unprocessable_entity) and return
    end

    errors = check_for_errors(params[:line_items])

    if errors.positive?
      flash.now[:error] = 'You must specify a valid item with a quantity greater than 0 and not more than current quantity. You must specify a serial number with matching qty if item requested requires reservation.'
      render :issue_to_account, status: :unprocessable_entity
    else
      begin
        ItemLedgerEntry.(store, params[:line_items], , business_unit, date, project, adjustment_params[:description])
        flash[:info] = 'Item have been issued to account successfully.'
        redirect_to item_ledger_entries_path
      rescue ActiveRecord::RecordInvalid => e
        flash.now[:error] = e.record.errors.full_messages.join(', ')
        render :issue_to_account, status: :unprocessable_entity
      end
    end
  end
end

#do_item_reclassificationObject



433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'app/controllers/item_ledger_entries_controller.rb', line 433

def do_item_reclassification
  adjustment_params = params.fetch(:adjustment, {})

  @from_line_items = params[:from_line_items]
  @to_line_items = params[:to_line_items]
  date = begin
    Date.strptime(adjustment_params[:date], '%Y-%m-%d')
  rescue StandardError
    Date.current
  end

  if params[:from_line_items].nil? || params[:to_line_items].nil? || adjustment_params[:store].blank?
    flash.now[:error] = 'You must specify a valid store and items to reclassify.'
    render :item_reclassification, status: :unprocessable_entity
  else
    messages = []

    from_errors = check_for_errors(params[:from_line_items])
    messages << 'You must specify valid item(s) with a quantity greater than 0 and not more than current quantity. You must specify a serial number with matching qty if item requested requires reservation.' if from_errors.positive?

    to_errors = 0
    params[:to_line_items].each do |_index, attrs|
      to_errors += 1 if attrs[:item_id].blank? || attrs[:qty].blank? || attrs[:qty].to_i <= 0 || attrs[:location].blank?
    end
    messages << 'You must specify item(s) to reclassify to with a destination location and quantity greater than 0.' if to_errors.positive?

    kit_errors = 0
    params[:from_line_items].merge(params[:to_line_items]).each do |_index, attrs|
      kit_errors += 1 if attrs[:item_id] && Item.find(attrs[:item_id]).is_kit?
    end
    messages << 'Kits cannot be reclassified.' if kit_errors.positive?

    if messages.length.positive?
      flash.now[:error] = messages.join(', ')
      render :item_reclassification, status: :unprocessable_entity
    else
      store = Store.find(adjustment_params[:store])
      ItemLedgerEntry.item_reclassification(store, params[:from_line_items], params[:to_line_items], date, adjustment_params[:description])
      flash[:info] = 'Item transfer has been initiated.'
      redirect_to_return_path_or_default item_ledger_entries_path
    end

  end
end

#do_location_transferObject



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

def do_location_transfer
  adjustment_params = params.fetch(:adjustment, {})
  @line_items = params[:line_items]
  if adjustment_params[:store].blank?
    flash.now[:error] = 'You must specify a valid store to continue.'
    render :location_transfer, status: :unprocessable_entity
  else
    store = Store.find(adjustment_params[:store])
    date = begin
      Date.strptime(adjustment_params[:date], '%Y-%m-%d')
    rescue StandardError
      Date.current
    end

    errors = check_for_errors(params[:line_items])

    if errors.positive?
      flash.now[:error] = 'You must specify a valid item with a quantity greater than 0 and not more than current quantity. You must specify a serial number with matching qty if item requested requires reservation.'
      render :location_transfer, status: :unprocessable_entity
    else
      ItemLedgerEntry.location_transfer(store, params[:line_items], date, adjustment_params[:description])
      flash[:info] = 'Items have been transferred successfully.'
      redirect_to_return_path_or_default item_ledger_entries_path
    end
  end
end

#do_store_transferObject



174
175
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
# File 'app/controllers/item_ledger_entries_controller.rb', line 174

def do_store_transfer
  adjustment_params = params.to_h.fetch(:adjustment, {})
  new_address_params = params.to_h.fetch(:address, {})
  @adjustment = OpenStruct.new(adjustment_params)
  @line_items = params.fetch(:line_items, {}).to_h.with_indifferent_access
  @new_address = Address.new(new_address_params)
  @enter_new_address = adjustment_params[:enter_new_address]
  @addresses = []
  # Needed by the store_transfer template's destination_address_id input so
  # the user's selection survives a re-render after validation failure.
  @preselected_destination_address =
    adjustment_params[:destination_address_id].present? ? Address.find_by(id: adjustment_params[:destination_address_id]) : nil
  if adjustment_params[:store].blank? || adjustment_params[:new_store_id].blank? || adjustment_params[:store] == adjustment_params[:new_store_id]
    flash.now[:error] = 'You must specify valid stores to continue.'
    render :store_transfer, status: :unprocessable_entity
  elsif adjustment_params[:destination_address_id].blank? && new_address_params.dig(:street1).blank?
    flash.now[:error] = 'You must choose an existing destination address or enter a new one to continue.'
    render :store_transfer, status: :unprocessable_entity
  else
    store = Store.find(adjustment_params[:store])
    new_store = Store.find(adjustment_params[:new_store_id])
    date = begin
      Date.strptime(adjustment_params[:date], '%Y-%m-%d')
    rescue StandardError
      Date.current
    end
    new_store_consignee_party_id = StoreTransfer.where(from_store_id: store.id).where(to_store_id: new_store.id).first.consignee_party_id
    @addresses = Address.where(party_id: new_store_consignee_party_id).order('state_code asc').map { |a| [a.to_s, a.id] }
    if @enter_new_address.to_b
      @new_address.party_id = new_store_consignee_party_id
      unless @new_address.save
        flash.now[:error] = 'New address is invalid.'
        render(:store_transfer, status: :unprocessable_entity) and return
      end
      address_id = @new_address.id
    else
      address_id = adjustment_params[:destination_address_id]
    end

    errors = []
    params[:line_items].each do |index, attrs|
      line_prefix = "Line #{index.to_i + 1}: "
      errors << "#{line_prefix} Store Item missing" if attrs[:store_item_id].blank?
      errors << "#{line_prefix} Quantity to transfer missing" if attrs[:qty].blank?
      qty_requested = attrs[:qty].to_i
      errors << "#{line_prefix} Quantity to transfer must be > 0" if qty_requested <= 0
      qty_on_hand = attrs[:qty_on_hand].to_i
      errors << "#{line_prefix} Quantity to transfer exceeds stock on hand" if qty_requested > qty_on_hand
      # Serial number check
      if attrs[:serial_number_id].present? && (sn = SerialNumber.find(attrs[:serial_number_id])) && qty_requested > sn.qty_available
        errors << "#{line_prefix} Quantity to transfer must not be higher than available qty of serial number #{attrs[:serial_number_id]}"
      end
      store_item = StoreItem.find(attrs[:store_item_id])
      item = store_item.item
      cat_item = store.primary_catalog.catalog_items.joins(:store_item).find_by(store_items: { item_id: item.id })
      errors << "Unable to find given item SKU: #{item.sku} / ID: #{item.id} in origin store's primary catalog NAME: #{store.primary_catalog.name} / ID: #{store.primary_catalog_id}." if cat_item.nil?
      dest_cat_item = new_store.primary_catalog.catalog_items.joins(:store_item).find_by(store_items: { item_id: item.id })
      errors << "Unable to find given item SKU: #{item.sku} / ID: #{item.id} in destination store's primary catalog NAME: #{new_store.primary_catalog.name} / ID: #{new_store.primary_catalog_id}." if dest_cat_item.nil?
    end

    if errors.present?
      flash.now[:error] = errors.to_sentence.capitalize
      render :store_transfer, status: :unprocessable_entity
    else
      order = ItemLedgerEntry.store_transfer(
        store: store, line_items: params[:line_items],
        new_store: new_store, shipping_option_id: params[:shipping_option_id],
        gl_date: date, description: adjustment_params[:description], address_id: address_id,
        fulfillment_order_reference: adjustment_params[:fulfillment_order_reference]
      )
      link = view_context.link_to("Order #{order.reference_number}", order)
      flash[:info] = "Item transfer has been initiated as #{link}"
      redirect_to item_ledger_entries_path
    end
  end
end

#do_total_cost_adjustmentObject



398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'app/controllers/item_ledger_entries_controller.rb', line 398

def do_total_cost_adjustment
  adjustment_params = params.fetch(:adjustment, {})
  @line_items = params[:line_items]
  if adjustment_params[:store].blank?
    flash.now[:error] = 'You must specify a valid store to continue.'
    render :total_cost_adjustment, status: :unprocessable_entity
  else
    store = Store.find(adjustment_params[:store])
    date = begin
      Date.strptime(adjustment_params[:date], '%Y-%m-%d')
    rescue StandardError
      Date.current
    end

    errors = 0
    params[:line_items].each do |_index, attrs|
      errors += 1 if attrs[:store_item_id].blank? || attrs[:total_cost].blank?
    end

    if errors.positive?
      flash.now[:error] = 'You must specify a valid item and an amount to adjust by on each line.'
      render :total_cost_adjustment, status: :unprocessable_entity
    else
      ItemLedgerEntry.total_cost_adjustment(store, params[:line_items], date, params[:description], adjustment_params[:override_kit_restriction].to_b)
      flash[:info] = 'Total cost adjustment has been processed successfully.'
      redirect_to item_ledger_entries_path
    end
  end
end

#download_sample_excelObject



134
135
136
137
# File 'app/controllers/item_ledger_entries_controller.rb', line 134

def download_sample_excel
  file_path = Rails.root.join('extras/samples/store_transfers/store_transfer_items_template.xlsx')
  send_file_accelerated(file_path, download: true, preserve_source: true)
end

#indexObject

GET /item_ledger_entries
GET /item_ledger_entries.xml



46
47
48
49
50
51
52
53
54
55
# File 'app/controllers/item_ledger_entries_controller.rb', line 46

def index
  @search = ItemLedgerEntrySearch.new(set_limit: 50, employee: @context_user,
                                      query_params: {}, selected_columns: %w[entry_link category store_id
                                                                             item_link quantity quantity_eval currency unit_cost total_cost
                                                                             old_qty new_qty old_cogs new_cogs location
                                                                             description document_link account business_unit
                                                                             project gl_link created_at cycle_count_id],
                                      sort_columns: ['created_at desc'])
  @links = params[:links] || {}
end

#inventory_adjustmentObject



300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'app/controllers/item_ledger_entries_controller.rb', line 300

def inventory_adjustment
  @adjustment = OpenStruct.new({ date: Date.current }.merge(params[:adjustment] || {}))
  if @adjustment.item_id && @adjustment.store && @adjustment.from
    if store_item = StoreItem.where(store_id: @adjustment.store, item_id: @adjustment.item_id, location: @adjustment.from).first
      @line_items = ActiveSupport::HashWithIndifferentAccess.new('0' => { 'location' => params[:from], 'store_item_id' => store_item.id, 'store_item_name' => store_item.sku, 'qty_on_hand' => store_item.qty_on_hand, 'qty_available' => store_item.qty_available })
    end
  elsif params[:store_item_id]
    store_item = StoreItem.find(params[:store_item_id])
    @adjustment.store = store_item.store_id
    @line_items = ActiveSupport::HashWithIndifferentAccess.new('0' => { 'location' => store_item.location, 'store_item_id' => store_item.id, 'store_item_name' => store_item.sku, 'qty_on_hand' => store_item.qty_on_hand, 'qty_available' => store_item.qty_available })
  else
    @line_items = ActiveSupport::HashWithIndifferentAccess.new('0' => { 'location' => nil })
  end
end

#issue_to_accountObject



251
252
253
# File 'app/controllers/item_ledger_entries_controller.rb', line 251

def 
  @line_items = ActiveSupport::HashWithIndifferentAccess.new('0' => { location: nil })
end

#item_reclassificationObject



428
429
430
431
# File 'app/controllers/item_ledger_entries_controller.rb', line 428

def item_reclassification
  @from_line_items = ActiveSupport::HashWithIndifferentAccess.new('0' => { location: params[:from].presence, store_item_id: params[:from_store_item_id].presence, qty: 1 })
  @to_line_items = ActiveSupport::HashWithIndifferentAccess.new('0' => { location: params[:to].presence, item_id: params[:to_item_id].presence, qty: 1 })
end

#location_transferObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/item_ledger_entries_controller.rb', line 57

def location_transfer
  if params[:item_id] && params[:store_id] && params[:from]
    store_item = StoreItem.where(store_id: params[:store_id], item_id: params[:item_id], location: params[:from]).first
    @line_items = ActiveSupport::HashWithIndifferentAccess.new('0' => {
                      'location' => params[:from],
                      'store_item_id' => store_item.id,
                      'store_item_name' => store_item.sku,
                      'qty_on_hand' => store_item.qty_on_hand,
                      'qty_available' => store_item.qty_available,
                      'new_location' => params[:to]
                    })
  else
    @line_items = ActiveSupport::HashWithIndifferentAccess.new('0' => { 'location' => nil })
  end
end

#store_transferObject



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

def store_transfer
  options = params[:options].present? ? params[:options].to_hash.symbolize_keys : {}
  items_list = options[:items_list].presence
  s = options[:store_id].present? ? Store.find_by(id: options[:store_id]) : nil
  new_store = options[:new_store_id].present? ? Store.find_by(id: options[:new_store_id]) : nil
  @preselected_destination_address =
    options[:destination_address_id].present? ? Address.find_by(id: options[:destination_address_id]) : nil

  adjustment_attrs = { date: Date.current }
  adjustment_attrs[:store] = [s.name, s.id, { data: { company_id: s.company_id } }] if s
  adjustment_attrs[:new_store_id] = new_store.id if new_store
  adjustment_attrs[:destination_address_id] = @preselected_destination_address.id if @preselected_destination_address

  if s && items_list
    @adjustment = OpenStruct.new(adjustment_attrs)
    line_items = {}
    items_list.each_with_index do |(k, v), index|
      i = Item.find_by(id: k)
      next unless i

      si = StoreItem.where(store_id: s.id, item_id: i.id).first
      next unless si

      line_items[index.to_s] = { location: 'AVAILABLE', store_item_id: [i.sku, si.id], qty_on_hand: si.qty_on_hand, qty_available: si.qty_available, qty: v.to_i }
    end
    @line_items = line_items.presence || { '0' => { location: nil } }.with_indifferent_access
  else
    @adjustment = OpenStruct.new(adjustment_attrs)
    @line_items = { '0' => { location: nil } }.with_indifferent_access
  end
  @new_address = Address.new
  @enter_new_address = nil
  @addresses = []
end

#total_cost_adjustmentObject



394
395
396
# File 'app/controllers/item_ledger_entries_controller.rb', line 394

def total_cost_adjustment
  @line_items = ActiveSupport::HashWithIndifferentAccess.new('0' => { location: nil })
end