Class: Crm::Reports::InventoryPlanningController

Inherits:
ReportsController
  • Object
show all
Defined in:
app/controllers/crm/reports/inventory_planning_controller.rb

Instance Method Summary collapse

Instance Method Details

#generate_poObject



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
70
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
100
101
102
# File 'app/controllers/crm/reports/inventory_planning_controller.rb', line 39

def generate_po
  errors = []
  selected_items = params[:items].to_h.select { |item_id, details| details['order'].to_b }
  errors << 'Must select one or more items' if selected_items.empty?
  errors << 'Must specify reorder quantity for selected items' if selected_items.any? { |item_id, details| details['quantity'].blank? }
  if params[:commit] == 'Generate Purchase Order'
    supplier_ids = Item.where(id: selected_items.keys).distinct.pluck(:preferred_supplier_id)
    errors << 'Items selected must all be for the same supplier' if supplier_ids.length > 1
    render_errors(errors) and return if errors.any?

    supplier = Supplier.find(supplier_ids[0])
    date = Date.current.to_s
    po_params = { company_id: Company::USA, store_id: Store::WARMLYYOURS_US_ID,
      po_type: 'purchase', supplier_id: supplier.id,
      carrier_id: supplier.id, terms: supplier.terms, currency: supplier.currency,
      order_date: date, request_date: date }
    item_params = {}
    selected_items.each_with_index do |(item_id, details), index|
      si = supplier.supplier_items.where(item_id: item_id).first
      qty = si.uom_quantity.to_f.zero? ? 0 : (details['quantity'].to_i / si.uom_quantity.to_f).ceil
      unit_quantity = si.uom_quantity.to_f.zero? ? 0 : (details['quantity'].to_i / si.uom_quantity.to_f).ceil * si.uom_quantity
      unit_cost = si.current_price.try(:purchasing_cost)
      unit_weight = si.item.base_weight * si.uom_quantity
      item_params[index] = {
        stock_item: '1',
        sku: si.item.sku,
        item_id: item_id,
        description: si.item.name,
        supplier_sku: si.supplier_sku,
        supplier_description: si.supplier_description,
        uom: si.uom,
        quantity: qty,
        unit_quantity: unit_quantity,
        unit_weight: unit_weight,
        total_weight: (unit_weight * qty).round(2),
        unit_cost: unit_cost.to_s,
        total_cost: (unit_cost * qty).round(2),
        auto_receive: si.auto_receive? ? 1 : 0
      }
    end
    po_params[:purchase_order_items_attributes] = item_params

    # Stash the draft server-side and redirect so Turbo Drive performs a
    # proper navigation. Can't pass the full nested payload via query string
    # (risks exceeding URL limits for larger orders) and purchase_orders#new
    # already builds from params[:purchase_order].
    session[:inventory_planning_po_draft] = po_params
    # ApplicationController#redirect_to auto-promotes to 303 on POSTs.
    redirect_to new_purchase_order_path
  elsif params[:commit] == 'Generate Store Transfer'
    store = Store.find(Store::WARMLYYOURS_US_ID)
    new_store = Store.find(Store::WARMLYYOURS_CA_ID)
    # Stash items_list server-side so we don't risk 414 URI Too Long when
    # the user selected many SKUs. item_ledger_entries#store_transfer picks
    # the draft up if no items_list is present on the URL.
    session[:inventory_planning_store_transfer_draft] =
      selected_items.transform_values { |details| details['quantity'].to_i }
    redirect_to store_transfer_item_ledger_entries_path(options: {
                  store_id: store.id,
                  new_store_id: new_store.id,
                  destination_address_id: new_store.consignee_party.addresses.first&.id
                }.compact)
  end
end

#refresh_forecastObject



23
24
25
26
# File 'app/controllers/crm/reports/inventory_planning_controller.rb', line 23

def refresh_forecast
  job_id = RefreshInventoryReportWorker.perform_async
  redirect_to job_path(job_id)
end

#refresh_reportObject



28
29
30
31
# File 'app/controllers/crm/reports/inventory_planning_controller.rb', line 28

def refresh_report
  job_id = RefreshInventoryPlanWorker.perform_async
  redirect_to job_path(job_id)
end

#render_errors(errors) ⇒ Object



104
105
106
107
108
109
110
# File 'app/controllers/crm/reports/inventory_planning_controller.rb', line 104

def render_errors(errors)
  flash.now[:error] = errors.join(', ')
  params[:q] = params[:q].nil? ? nil : YAML.load(params[:q].gsub('=>', ':')).symbolize_keys
  params[:report_command] = params[:report_command].nil? ? nil : YAML.load(params[:report_command].gsub('=>', ':')).symbolize_keys
  show
  render :show, status: :unprocessable_entity
end

#showObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/controllers/crm/reports/inventory_planning_controller.rb', line 4

def show
  if ItemDemandForecast.any?
    params[:q] ||= {}
    params[:report_command] ||= {}
    params[:q][:product_category_ids_overlap_array] = params[:report_command][:product_category_ids]
    params[:q][:product_line_ids_overlap_array] = params[:report_command][:product_line_ids]
    @report_command = ::Report::InventoryPlanningCommand.new(params[:report_command])
    @q = @report_command.execute.ransack(params[:q])
    @q.sorts = 'item_sku asc' if @q.sorts.blank?
    @inventory_plans = @q.result
    @country = @report_command.country
    @country_only = @country.gsub('_st', '')
  else
    @report_refreshing = true
    # report is being refreshed, so find the job, could be working or still queued
    @job_id = BackgroundJobStatus.search(worker_klass: 'RefreshInventoryReportWorker').first&.jid
  end
end

#show_chartObject



33
34
35
36
37
# File 'app/controllers/crm/reports/inventory_planning_controller.rb', line 33

def show_chart
  @item = Item.find(params[:item_id])
  @country = params[:country]
  render layout: false
end