Class: BBMB::Util::Server

Inherits:
SBSM::DRbServer
  • Object
show all
Defined in:
lib/bbmb/util/server.rb

Constant Summary collapse

ENABLE_ADMIN =
true
SESSION =
Html::Util::Session
VALIDATOR =
Html::Util::Validator

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#updater ⇒ Object (readonly)

Returns the value of attribute updater.



22
23
24
# File 'lib/bbmb/util/server.rb', line 22

def updater
  @updater
end

Instance Method Details

#inject_order(customer_id, products, infos, opts = {}) ⇒ Object



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
# File 'lib/bbmb/util/server.rb', line 23

def inject_order(customer_id, products, infos, opts={})
  customer = Model::Customer.find_by_customer_id(customer_id) \
    || Model::Customer.find_by_ean13(customer_id)
  needed_create = false
  unless customer
    if idtype = opts[:create_missing_customer] && !customer_id.empty?
      customer = Model::Customer.new(customer_id)
      if idtype.to_s == 'ean13'
        customer.ean13 = customer_id
      end
      BBMB.persistence.save(customer)
      needed_create = true
    else
      raise "Unknown Customer #{customer_id}"
    end
  end
  order = Model::Order.new(customer)
  products.each { |info|
    if(product = Model::Product.find_by_pcode(info[:pcode]) \
       || Model::Product.find_by_ean13(info[:ean13]) \
       || Model::Product.find_by_article_number(info[:article_number]))
      order.add(info[:quantity], product)
      [:article_number, :backorder].each do |key|
        info.store key, product.send(key)
      end
      info.store :description, product.description.de
      info[:deliverable] = info[:quantity]
    else
      info[:deliverable] = 0
    end
  }
  infos.each { |key, value|
    order.send("#{key}=", value)
  }
  customer.inject_order(order)
  if opts[:deliver]
    async do
      send_order order, customer
    end
  end
  if needed_create
    BBMB::Util::Mail.notify_inject_error(order, opts)
  end
  { :order_id => order.order_id, :products => products }
end

#invoice(range) ⇒ Object



68
69
70
71
72
# File 'lib/bbmb/util/server.rb', line 68

def invoice(range)
  Invoicer.run(range)
rescue Exception => e
  Mail.notify_error(e)
end

#login(email, pass) ⇒ Object



73
74
75
76
# File 'lib/bbmb/util/server.rb', line 73

def (email, pass)
  session = BBMB.auth.(email, pass, BBMB.config.auth_domain)
  Html::Util::KnownUser.new(session)
end

#logout(session) ⇒ Object



77
78
79
80
# File 'lib/bbmb/util/server.rb', line 77

def logout(session)
  BBMB.auth.logout(session)
rescue DRb::DRbError, RangeError, NameError
end

#rename_user(old_name, new_name) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/bbmb/util/server.rb', line 81

def rename_user(old_name, new_name)
  return if old_name.eql?(new_name)
  BBMB.auth.autosession(BBMB.config.auth_domain) do |session|
    if(old_name.nil?)
      session.create_entity(new_name)
    else
      session.rename(old_name, new_name)
    end
  end
end

#run_invoicer ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/bbmb/util/server.rb', line 91

def run_invoicer
  BBMB.logger.debug("run_invoicer starting")
  @invoicer ||= Thread.new {
    Thread.current.abort_on_exception = true
    loop {
      today = Date.today
      day = today >> 1
      start = Time.local(today.year, today.month)
      now = Time.now
      at = Time.local(day.year, day.month)
      secs = at - now
      BBMB.logger.debug("invoicer") {
        "sleeping %.2f seconds" % secs
      }
      sleep(secs)
      BBMB.logger.debug("invoice starting")
      invoice(start...at)
      BBMB.logger.debug("invoice finished")
    }
  }
end

#run_updater ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/bbmb/util/server.rb', line 112

def run_updater
  run_only_once_at_startup = false
  BBMB.logger.debug("updater") { "run_updater run_only_once_at_startup? #{run_only_once_at_startup} " }
  @updater ||= Thread.new {
    loop {
      day = Date.today
      now = Time.now
      if(now.hour >= BBMB.config.update_hour)
        day += 1
      end
      at = Time.local(day.year, day.month, day.day, BBMB.config.update_hour)
      secs = at - now
      BBMB.logger.debug("updater") { "sleeping %.2f seconds. run_only_once_at_startup #{run_only_once_at_startup}" % secs  }
      if run_only_once_at_startup then puts "Skipped sleeping #{secs}" else sleep(secs) end

      BBMB.logger.debug("update starting")
      update
      BBMB.logger.debug("update finished")
      Thread.abort if run_only_once_at_startup
    }
  }
end

#send_order(order, customer) ⇒ Object



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
# File 'lib/bbmb/util/server.rb', line 134

def send_order order, customer
  begin
    Timeout.timeout(300) {
      BBMB::Util::TargetDir.send_order(order)
    }
  rescue StandardError => err
    err.message << " (Email: #{customer.email} - Customer-Id: #{customer.customer_id})"
    BBMB::Util::Mail.notify_error(err)
  end
  begin
    Timeout.timeout(300) {
      BBMB::Util::Mail.send_order(order)
    }
  rescue StandardError => err
    err.message << " (Email: #{customer.email} - Customer-Id: #{customer.customer_id})"
    BBMB::Util::Mail.notify_error(err)
  end
  begin
    Timeout.timeout(300) {
      BBMB::Util::Mail.send_confirmation(order)
    }
  rescue StandardError => err
    err.message << " (Email: #{customer.email} - Customer-Id: #{customer.customer_id})"
    BBMB::Util::Mail.notify_error(err)
  end
end

#update ⇒ Object



160
161
162
163
164
# File 'lib/bbmb/util/server.rb', line 160

def update
  Updater.run
rescue Exception => e
  Mail.notify_error(e)
end