Last active
August 29, 2015 14:11
-
-
Save airspeed/87d02cef8dc297f0b9a1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # Replays one order. | |
| # @param order_cart_id int | |
| # @return order_cart_id of the duplicate | |
| # @see To be launched on vps-api-* | |
| def duplicate_order(order_cart_id) | |
| # 1. Datensammlung | |
| o = Order.find(order_cart_id) | |
| is = o.order_items | |
| nis = [] | |
| u = o.user or raise "User for order #{o.id} 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 => o.invoice_address_id, :delivery_at => o.delivery_at, :currency => o.currency, :payment_type_id => o.payment_type_id, :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