2023-09-07 19:12:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'csv'
|
|
|
|
|
|
|
|
module RepositoryStockLedgerZipExport
|
|
|
|
COLUMNS = %w(
|
|
|
|
consumption_type
|
|
|
|
item_name
|
|
|
|
item_id
|
|
|
|
consumed_amount
|
|
|
|
consumed_amount_unit
|
|
|
|
added_amount
|
|
|
|
added_amount_unit
|
|
|
|
consumed_by
|
|
|
|
consumed_on
|
|
|
|
team
|
|
|
|
project
|
|
|
|
experiment
|
|
|
|
task
|
|
|
|
task_id
|
|
|
|
stock_amount_balance
|
|
|
|
stock_balance_unit
|
|
|
|
).freeze
|
|
|
|
|
2023-09-08 17:35:16 +08:00
|
|
|
def self.to_csv(repository_row_ids)
|
2023-09-07 19:12:27 +08:00
|
|
|
csv_header = COLUMNS.map { |col| I18n.t("repository_stock_values.stock_export.headers.#{col}") }
|
2023-09-08 17:35:16 +08:00
|
|
|
repository_ledger_records = load_records(repository_row_ids)
|
2023-09-07 19:12:27 +08:00
|
|
|
|
|
|
|
CSV.generate do |csv|
|
|
|
|
csv << csv_header
|
2023-09-08 17:35:16 +08:00
|
|
|
repository_ledger_records.each do |record|
|
|
|
|
csv << generate_record_data(record)
|
2023-09-07 19:12:27 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-09-08 17:35:16 +08:00
|
|
|
class << self
|
|
|
|
private
|
|
|
|
|
|
|
|
def load_records(repository_row_ids)
|
|
|
|
RepositoryLedgerRecord
|
|
|
|
.joins(:repository_row)
|
|
|
|
.preload(:user, repository_row: { repository: :team })
|
|
|
|
.preload(my_module_repository_row: { my_module: { experiment: { project: :team } } })
|
|
|
|
.where(repository_row: { id: repository_row_ids })
|
|
|
|
.order(:created_at)
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_record_data(record)
|
2023-09-07 19:12:27 +08:00
|
|
|
consumption_type = record.reference_type == 'MyModuleRepositoryRow' ? 'Task' : 'Inventory'
|
|
|
|
|
2023-09-26 19:20:15 +08:00
|
|
|
if (consumption_type == 'Task' && record.amount.positive?) ||
|
|
|
|
(consumption_type == 'Inventory' && record.amount.negative?)
|
2023-09-07 19:12:27 +08:00
|
|
|
consumed_amount = record.amount.abs.to_d
|
|
|
|
consumed_amount_unit = record.unit
|
2023-09-26 19:20:15 +08:00
|
|
|
else
|
2023-10-06 18:04:33 +08:00
|
|
|
added_amount = record.amount.abs.to_d
|
2023-09-26 19:20:15 +08:00
|
|
|
added_amount_unit = record.unit
|
2023-09-07 19:12:27 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
breadcrumbs_data = Array.new(4, '')
|
|
|
|
|
|
|
|
row_data = [
|
|
|
|
consumption_type,
|
2023-09-08 17:35:16 +08:00
|
|
|
record.repository_row.name,
|
|
|
|
record.repository_row.code,
|
2023-09-07 19:12:27 +08:00
|
|
|
consumed_amount,
|
|
|
|
consumed_amount_unit,
|
|
|
|
added_amount,
|
|
|
|
added_amount_unit,
|
|
|
|
record.user.full_name,
|
2023-10-03 17:17:05 +08:00
|
|
|
I18n.l(record.created_at, format: :full),
|
2023-09-26 19:20:15 +08:00
|
|
|
record.balance.to_d,
|
|
|
|
record.unit
|
2023-09-07 19:12:27 +08:00
|
|
|
]
|
2023-10-06 18:04:33 +08:00
|
|
|
breadcrumbs_data = Array.new(5)
|
2023-09-07 19:12:27 +08:00
|
|
|
if consumption_type == 'Task'
|
2023-09-08 17:35:16 +08:00
|
|
|
my_module = record.my_module_repository_row.my_module
|
2023-09-07 19:12:27 +08:00
|
|
|
breadcrumbs_data = [
|
2023-10-06 18:04:33 +08:00
|
|
|
my_module.experiment.project.team.name,
|
2023-09-08 17:35:16 +08:00
|
|
|
my_module.experiment.project.name,
|
|
|
|
my_module.experiment.name,
|
|
|
|
my_module.name,
|
|
|
|
my_module.code
|
2023-09-07 19:12:27 +08:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2023-10-06 18:04:33 +08:00
|
|
|
row_data.insert(9, *breadcrumbs_data)
|
2023-09-08 17:35:16 +08:00
|
|
|
row_data
|
2023-09-07 19:12:27 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|