Skip to content

Instantly share code, notes, and snippets.

@airspeed
Last active December 25, 2015 18:39
Show Gist options
  • Save airspeed/7022331 to your computer and use it in GitHub Desktop.
Save airspeed/7022331 to your computer and use it in GitHub Desktop.
Erstellt eine Kopie einer Bestellung, die wie eine neue Bestellung bearbeitet wird.
# Duplicates one order.
# @param order_cart_id int
# @return order_cart_id of the duplicate
# @see To be launched on vps-api-*
# 07.11.2013: Feste Rechnungsadresse für alle Nachdrucke.
def duplicate_order(order_cart_id, invoice_address_id = 38486)
# fotobuch-api-dev: 3, flexiphoto-api: 2388, flexiphoto-dev: 6556, vps-polapix: 1601, vps-calendar: 2948
# 1. Datensammlung
o = Order.find(order_cart_id)
is = o.order_items
nis = []
u = User.where(:email => '[email protected]').first or raise "User [email protected] not found."
ca = nil
# 2. Neue Datensätze erstellen
# 2.1 - Adressen
# 2.2 - Artikel
is.each do |i|
a = i.address
na = {:city => a.city, :zip_code => a.zip_code, :street => a.street, :company => a.company, :phone => a.phone, :email => a.email, :country_code => a.country_code, :name => a.name, :surname => a.surname, :district => a.district}
u.addresses.build(na).save unless na === ca
aid = u.addresses.reload.last.id
ni = {:order_id => nil, :address_id => aid, :quantity => i.quantity, :user_id => u.id, :album_id => i.album_id}
nis << ni
ca = na
end
# 2.3 - Bestellung
no = u.orders.new({:album_id => o.album_id, :invoice_address_id => invoice_address_id, :delivery_at => o.delivery_at, :currency => o.currency, :payment_type_id => 1, :provider_id => o.provider_id})
no.client_id = o.client_id
no.test = o.test
no.save
no.add_items(nis)
no.recalculate
noid = u.orders.reload.last.id
# 3. Indruckgebung
# Order.find(noid).tap{|oc| oc.state = "to_print"}.save # Umsichtiger, da wir nicht genau wissen, in welchem Status sich der Order befindet.
no.proceed
# 4. Benachrichtigung
msg = "#{order_cart_id} reprinted as #{noid}."
puts msg
ExceptionNotifier::Notifier.background_exception_notification(Exception.new(msg))
# 5. Rückgabewert
noid
rescue Exception => e
ExceptionNotifier::Notifier.background_exception_notification(e)
end
# Beispiel:
# duplicate_order(Order.last.id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment