21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
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
133
134
135
136
137
|
# File 'lib/print_engine.rb', line 21
def self.included(base)
def base.default_printer usr, printer_name, paper=nil
if printer_name=="default"
if paper.nil?
printer = (usr.printers.empty? ? nil : usr.printers.active.preferred_printer.first) or raise NoPreferredPrintersFound.new('No preferred printers were found!')
else
printer = (usr.printers.empty? ? nil : (usr.printers.active.preferred_printer.on_paper(paper).first || usr.printers.on_paper(paper).first ) ) or raise NoPreferredPrintersFound.new('No preferred printers were found!')
end
else
printer = Printer.active.find_by( 'name like ?', "%#{printer_name.downcase}%")
end
printer || Printer.first
end
def base.print( resources, params )
begin
pj = create_print_job( resources, params )
return false unless pj
rescue PrintJobResourceError
base.logit :error, 'PrintJob could not be created - no records!?'
return false
rescue PrintJobPrinterNotAvailableError
logit :error, 'PrintJob could not be created - the user has no printers attached!!'
return false
rescue NoPreferredPrintersFound
logit :error, 'PrintJob could not be created - the user has no preferred printers attached!!'
false
end
begin
if Rails.env=='development' or pj.download
return pj if pj.perform params
else
user = User.find(pj.printed_by_id)
BackgroundPrinterJob.perform_later pj, queue: "printing", account_id: user.account.id
return pj
end
rescue => e
pj.update_columns( state: "could not be queued for printing - #{e}" )
return nil
end
end
def base.create_print_job( resources, params )
printer_name = params.delete(:printer_name) || "default"
params = ActionController::Parameters.new( params) unless params.class == ActionController::Parameters
params = set_print_job_defaults(resources,params, printer_name)
return nil if params[:print_job][:printer_id].nil?
PrintJob.create( params[:print_job].permit(:download, :snap_shot, :account_id, :printer_id, :printed_by_id, :printed_by_type, :view_template_path, :name, :printing_class, :print_driver, :print_format, :paper, :copies, :state, :print_sql) )
end
def base.set_print_job_defaults(resources, params, printer_name)
klass = resources.first.class.to_s rescue "class not found!"
params[:print_job] ||= {}
params[:user] ||= User.first
user = params[:user]
params[:print_job][:account_id] ||= user.account.id
params[:print_job][:paper] ||= nil
params[:print_job][:printer_id] ||= self.default_printer(user,printer_name,params[:print_job][:paper]).id
params[:print_job][:printed_by_id] = user.id
params[:print_job][:printed_by_type] = user.class.to_s
params[:print_job][:name] ||= "#{klass} print at #{I18n.l Time.now, format: :short_date }"
params[:print_job][:printing_class] ||= klass
params[:print_job][:print_driver] ||= :pdf
params[:print_job][:copies] ||= 1
params[:print_job][:print_format] ||= "list"
params[:print_job][:state] = "drafted"
params[:print_job][:snap_shot] ||= false
params[:print_job][:print_sql] = set_resource_sql(resources,params)
params
end
def base.set_resource_sql(resources,params)
return resources if resources.class == String && resources.downcase =~ /select/
raise PrintJobResourceError unless resources.respond_to?(:any?) and resources.any?
params[:print_job][:snap_shot] ? resources.to_yaml : (resources.class==Array ? array_to_arel(resources) : resources.to_sql)
end
def base.array_to_arel resources
arel_instance = Arel::Table.new(resources.first.class.table_name)
ar_rel = resources.first.class
ar_rel.where(arel_instance[:id].in(resources.map(&:id)).to_sql).to_sql
end
end
|