Module: PrintEngine

Includes:
Exceptions
Defined in:
lib/print_engine.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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)

  #
  # 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 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
    # usr.printers.where{ (printownerables.preferred==true) & (printownerables.preferred==true) }

    # raise PrintJobPrinterNotAvailableError
    printer || Printer.first
  # rescue
  #   Printer.new
  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 )
    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..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
    rescue => e
      # self.logit :error, 'PrintJob could not be enqueued'
      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

  # 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, 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..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)

    # this is what we ultimately return
    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