# frozen_string_literal: true class BlankContract def initialize(releasable, copies = 1) @releasable = releasable integer_number_of_copies = copies.to_i @copies = integer_number_of_copies.positive? ? integer_number_of_copies : 1 end def to_pdf kit = PDFKit.new(as_html, margin_right: 1, margin_left: 1, margin_top: 10, margin_bottom: 1) kit.to_file("tmp/#{filename}") end def filename "blank_#{contract_template.release_type}_release.pdf" end def render_attributes attributes = { layout: 'contract_pdf', template: 'blank_contracts/pdf', locals: { releasable: @releasable, contract_template: contract_template, copies: @copies } } add_logo_to_attributes attributes add_codes_to_attributes attributes attributes end private def contract_template @releasable.contract_template end def project @releasable.project end def locale @releasable.locale end def as_html I18n.with_locale(locale) do ApplicationController.render render_attributes end end def add_logo_to_attributes(attributes) logo = @releasable.project.account.logo attributes[:locals][:logo] = logo if logo.attached? end def add_codes_to_attributes(attributes) attributes[:locals][:qr_codes] = [] attributes[:locals][:serial_numbers] = [] @copies.times do random_number = SecureRandom.hex 4 custom_url = "#{contract_template.to_global_id.to_s}/#{random_number}" qr_code = QrCode.new(custom_url) attributes[:locals][:qr_codes] << qr_code.to_base64_png attributes[:locals][:serial_numbers] << random_number.to_s end end end