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

#updaterObject (readonly)

Returns the value of attribute updater.



20
21
22
# File 'lib/bbmb/util/server.rb', line 20

def updater
  @updater
end

Instance Method Details

#inject_order(customer_id, products, infos, opts = {}) ⇒ 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
# File 'lib/bbmb/util/server.rb', line 21

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



66
67
68
69
70
# File 'lib/bbmb/util/server.rb', line 66

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

#login(email, pass) ⇒ Object



71
72
73
74
# File 'lib/bbmb/util/server.rb', line 71

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

#logout(session) ⇒ Object



75
76
77
78
# File 'lib/bbmb/util/server.rb', line 75

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

#rename_user(old, new) ⇒ Object



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

def rename_user(old, new)
  return if(old == new)
  BBMB.auth.autosession(BBMB.config.auth_domain) { |session|
    if(old.nil?)
      session.create_entity(new)
    else
      session.rename(old, new)
    end
  }
end

#run_invoicerObject



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

def run_invoicer
  @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)
      invoice(start...at)
    }
  }
end

#run_updaterObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/bbmb/util/server.rb', line 107

def run_updater
  @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" % secs
      }
      sleep(secs)
      update
    }
  }
end

#send_order(order, customer) ⇒ Object



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

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

#updateObject



152
153
154
155
156
# File 'lib/bbmb/util/server.rb', line 152

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