Files
baclight/app/services/id_card_printer_service/cards_generator.rb
T

69 lines
2.9 KiB
Ruby
Raw Normal View History

2026-03-27 08:04:37 -04:00
module IdCardPrinterService
class CardsGenerator
def initialize(employers_member_keys, layout, zip=false)
@employers_member_keys = Array.wrap(employers_member_keys)
@layout = layout
@zip = zip
end
def call
pl_plan_keys = @employers_member_keys.map { |emk| emk[:pl_plan_key] }
IdCard::PrintData.where(pl_plan_key: pl_plan_keys).destroy_all
IdCardPrinterService::CardDataFormatter.new(@employers_member_keys).call
IdCard::PrintData.where(pl_plan_key: pl_plan_keys, primary_mb_member_key: nil).destroy_all
pdf_array = []
batches = break_into_batches(pl_plan_keys)
batches.each do |card_template, batch_pl_plan_keys|
jasper_batch_id = "#{batch_pl_plan_keys.join('_')}-#{Time.current.utc.to_i}"
batch = IdCard::PrintData.where(pl_plan_key: batch_pl_plan_keys)
batch.update!(jasper_batch_id: jasper_batch_id)
batch_pdf = IdCardPrinterService::PdfBatchProcessor.new(card_template, jasper_batch_id, @layout).call
pdf_array << batch_pdf
end
# @employers_member_keys.each do |emk|
# employer_jasper_batches = IdCard::PrintData.where(pl_plan_key: emk[:pl_plan_key]).pluck(:network_logo_id).uniq
# employer_jasper_batches.each do |batch_network_logo|
# jasper_batch_id = "#{emk[:pl_plan_key]}-#{Time.current.utc.to_i}"
# batch = IdCard::PrintData.where(pl_plan_key: emk[:pl_plan_key], network_logo_id: batch_network_logo)
# # batch = employer_card_data.where(network_logo_id: batch_network_logo)
# batch.update!(jasper_batch_id: jasper_batch_id)
# batch_pdf = IdCardPrinterService::PdfBatchProcessor.new(emk[:pl_plan_key], batch_network_logo, jasper_batch_id, @layout).call
# pdf_array << batch_pdf
# end
# end
group_pdfs = combine_pdfs(pdf_array)
group_pdfs
end
private
def break_into_batches(pl_plan_keys)
batches_by_card_template = IdCard::Setup.where(pl_plan_key: pl_plan_keys).group_by(&:card_template)
.transform_values { |setups| setups.map(&:pl_plan_key) }
.compact_blank
end
def combine_pdfs(pdf_array)
if @zip
group_cards_pdf = Zip::OutputStream.write_buffer do |zio|
pdf_array.each do |file|
zio.put_next_entry(file[:name])
zio.write(file[:data])
end
end
else
group_cards_pdf = CombinePDF.new
pdf_array.each { |pdf| group_cards_pdf << pdf }
end
group_cards_pdf
end
end
end