Module: PrintEngine

Includes:
Exceptions
Defined in:
lib/print_engine.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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
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
# File 'lib/print_engine.rb', line 57

def self.included(base)

  #
  def base.print_list(options={})
    options = set_print_job_defaults options[:resources], options
    options[:print_job][:print_format]        =   options[:print][:collation] || 'list'
    pj = print options[:resources], options
  end

  #
  # def base.default_print_template_path(params,usr)
  #   # TODO 2013-09-02 - select template according to the user/employee
  #   params[:print_job][:view_template_path] || "/test.html.haml"
  # end

  # Creates the PrintJob for any model in need of printing
  # If creating the PrintJob is successful, cycle will be called in print_job.rb
  # and the PrintJob will be enqueued in the delayed_job queue called 'printing'.
  # cycle_error will be called in print_job.rb if it cannot be enqueued sucessfully.
  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..id
      # Delayed::Job.enqueue pj, :queue => 'printing'
      # pj.cycle if Delayed::Job.enqueue pj, :queue => 'printing'
      # If you comment out above line and use below line with pj.perform instead, you thereby surpass delayed_job, and thus don't have to wait for it
      # pj.perform
      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

  # Sets print_job defaults using the provided params
  # and looks up any print_job settings to be tweaked
  # as per klass and user
  def base.set_print_job_defaults(resources, options)
    klass = self.to_s #resources.first.class.to_s rescue "class not found!"
    pb = options[:printed_by]
    printer_name = options[:print].delete(:printer) || "default"

    options[:print_job][:printer_id]          ||= self.default_printer(pb,printer_name,options[:print_job][: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]                       ||= {}
    options[:print_job][:printing_class]      ||= klass
    options[:print_job][:account_id]          ||= pb..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[:context]                         ||= ""
    options[:print_job][:download]            =   options[:print][:medium]=="download" || false
    # options[:print_job][:view_template_path]  =   find_template( options[:print][:template], options[:print][:paper] ) || options[:print_job][:view_template_path]
    #
    # this is what we ultimately return
    options
  end

  #
  # find the best printer for the job
  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 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 found!')
      end
    else
      printer = Printer.active.find_by( 'name like ?',  "%#{printer_name.downcase}%")
    end
    # usr.printers.where{ (printownerables.preferred==true) & (printownerables.preferred==true) }

    # raise PrintJobPrinterNotAvailableError
    printer || Printer.first
  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

Instance Method Details

#find_template(template, paper = "A4") ⇒ Object

# list_title def list_title

self.respond_to?( "name") ? self.name : "please define list_title on model (#{self.class.to_s})!"

end

# implement on relevant models



171
172
173
174
# File 'lib/print_engine.rb', line 171

def find_template template, paper="A4"
  raise 'you have to implement "def find_template(paper)" on your Model'
  # 'label.html.haml'
end

def set_print_defaults options

options[:context]                         ||= ""
options[:print_job][:download]            =   options[:print][:medium]=="download" || false
options[:print_job][:print_driver]        =   spot_the_driver(options)
options[:print_job][:paper]               =   options[:paper] || "A4"
options[:print_job][:view_template_path]  =   find_template( options[:template], options[:paper] ) || params[:print_job][:view_template_path]
options

end



188
189
190
191
192
193
194
195
196
# File 'lib/print_engine.rb', line 188

def print_label(options={})
  options = self.class.set_print_job_defaults [self], options
  options[:print_job][:print_driver]        =   options[:print][:print_driver] || :cab
  options[:print_job][:paper]               =   options[:print][:paper] || "label"
  options[:print_job][:print_format]        =   options[:print][:collation] || 'record'
  options[:print_job][:view_template_path]  =   "stock_items/print/zebra_stock_items_label.html.haml"

  pj = self.class.print [self], options
end


203
204
205
206
207
208
209
210
211
# File 'lib/print_engine.rb', line 203

def print_record(options={})
  options = self.class.set_print_job_defaults [self], 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] || 'record'
  options[:print_job][:view_template_path]  =   "stock_items/print/slip.html.haml"

  pj = self.class.print [self], options
end