Last active
August 29, 2015 14:18
-
-
Save airspeed/c8572f313ac09348352e to your computer and use it in GitHub Desktop.
Nachbestellung mit Lieferung an femory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Duplicates one order and delivers it to femory. | |
# @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 | |
# 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 = Address.find( invoice_address_id ) | |
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 | |
# Eine Nachbestellung darf nicht über PayPal verlaufen. | |
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.save | |
no.add_items( nis ) | |
no.recalculate | |
noid = u.orders.reload.last.id | |
# 3. Indruckgebung | |
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