122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
# File 'lib/print_engine.rb', line 122
def self.included(base)
def base.print_list(options={})
raise NoContextFound.new('ViewContext missing!') if options[:context].blank?
options[:print] ||= {}
options[:print][:collation] = 'list'
options[:print][:paper] ||= 'A4'
options[:print][:output_type] ||= 'pdf'
options = set_print_job_defaults options[:resources], options
options[:print_job][:print_driver] = options[:print][:print_driver] || :pdf
options[:print_job][:paper] = options[:print][:paper] || "A4"
options[:print_job][:print_format] = options[:print][:collation] || 'list'
options[:print_job][:view_template_path] = "stock_items/print/pdf_stock_items_list.html.haml"
pj = print options[:resources], options
end
def base.print( resources, params )
pj = create_print_job( resources, params )
if Rails.env=='development' or pj.download
return pj.perform params
else
user = User.find(pj.printed_by_id)
BackgroundPrinterJob.perform_later pj, queue: "printing", account_id: user.account.id, print: params[:print]
return pj
end
end
def base.create_print_job( resources, params )
params = ActionController::Parameters.new( params) unless params.class == ActionController::Parameters
pj=PrintJob.create( params_permit(params) )
raise PrintJobNotCreatedError.new('arguments not permitted!') unless pj
pj
end
def base.params_permit(params)
params[:print_job].permit(:download, :snap_shot, :account_id, :printer_id, :printed_by_id,
:printed_by_type, :view_template_path, :printing_class, :name, :print_driver, :print_format, :paper, :copies, :state, :print_sql)
end
def base.set_print_job_defaults(resources, options)
klass = self.to_s
pb = options[:printed_by]
printer_name = options[:print].delete(:printer) || "default"
options[:print_job] ||= {}
options[:print_job][:printer_id] ||= self.default_printer(pb,printer_name,options[:print][:paper]).id
options[:print_job][:state] = "drafted"
options[:print_job][:snap_shot] ||= false
options[:print_job][:print_sql] = set_resource_sql(resources,options)
options[:print_job][:printing_class] ||= klass
options[:print_job][:account_id] ||= pb.account.id
options[:print_job][:printed_by_id] = pb.id
options[:print_job][:printed_by_type] = pb.class.to_s
options[:print_job][:name] ||= "#{klass} print at #{I18n.l Time.now, format: :short_date }"
options[:print_job][:copies] ||= 1
options[:print_job][:download] = options[:print][:medium]=="download" || false
options
end
def base.default_printer usr, printer_name, paper=nil
if printer_name!="default"
return Printer.find(printer_name) if printer_name.to_i.to_s == printer_name
Printer.active.find_by( 'name like ?', "%#{printer_name.downcase}%") or raise NoPreferredPrintersFound.new('Printer with name not found!')
else
raise NoPreferredPrintersFound.new('No printers found!') if usr.printers.empty?
if paper.nil?
usr.printers.active.preferred_printer.first or raise NoPreferredPrintersFound.new('No preferred printers found!')
else
usr.printers.active.preferred_printer.on_paper(paper).first || usr.printers.on_paper(paper).first or raise NoPreferredPrintersFound.new('No preferred printers found!')
end
end
end
def base.set_resource_sql(resources,params)
return resources if resources.class == String && resources.downcase =~ /select/
raise PrintJobResourceError.new('No items found to print?!') 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
|