My favorites
▼
|
Sign in
substruct
Open-source Ruby on Rails E-Commerce
Project Home
Downloads
Wiki
Issues
Source
READ-ONLY: This project has been
archived
. For more information see
this post
.
Search
Search within:
All issues
Open issues
New issues
Issues to verify
for
Advanced search
Search tips
Subscriptions
Issue
112
attachment: concurrent_stock_update.patch
(7.8 KB)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
Index: /home/edmundo/workspace_aptana/trunk/vendor/plugins/substruct/app/models/order.rb
===================================================================
--- /home/edmundo/workspace_aptana/trunk/vendor/plugins/substruct/app/models/order.rb (revision 106)
+++ /home/edmundo/workspace_aptana/trunk/vendor/plugins/substruct/app/models/order.rb (working copy)
@@ -576,16 +580,8 @@
# Cleans up a successful order
def cleanup_successful
- # Decrement inventory for items...
- # Also driven by the inventory control preference from the
- # admin UI
- if Preference.find_by_name('store_use_inventory_control').is_true?
- for oli in self.order_line_items do
- oli.item.update_attribute('quantity', oli.item.quantity-oli.quantity)if oli.quantity rescue false && oli.item.quantity rescue false
- end
- end
-
- self.order_status_code_id = 5
+ new_order_code = OrderStatusCode.find_by_name("ORDERED - PAID - TO SHIP")
+ self.order_status_code = new_order_code if new_order_code
self.new_notes="Order completed."
if Preference.find_by_name('cc_clear_after_order').is_true?
self.account.clear_personal_information
Index: /home/edmundo/workspace_aptana/trunk/vendor/plugins/substruct/app/controllers/store_controller.rb
===================================================================
--- /home/edmundo/workspace_aptana/trunk/vendor/plugins/substruct/app/controllers/store_controller.rb (revision 106)
+++ /home/edmundo/workspace_aptana/trunk/vendor/plugins/substruct/app/controllers/store_controller.rb (working copy)
@@ -313,38 +313,123 @@
#
# Submits order info to Authorize.net
def finish_order
- @title = "Thanks for your order!"
- cc_processor = Order.get_cc_processor
- order_success = @order.run_transaction
- if order_success == true
- @payment_message = "Card processed successfully"
- clear_cart_and_order(false)
- elsif cc_processor == Preference::CC_PROCESSORS[1]
- case order_success
- when 4
- @payment_message = %q\
- Your order has been processed at PayPal but we
- have not heard back from them yet. Your order
- will be ready to ship as soon as we receive
- confirmation of your payment.
- \
- clear_cart_and_order(false)
- when 5
- @payment_message = "Transaction processed successfully"
- clear_cart_and_order(false)
- else
- error_message = "Something went wrong and your transaction failed.<br/>"
- error_message << "Please try again or contact us."
- redirect_to_checkout(error_message)
+ # If the inventory control preference is set, we will update the inventory
+ if Preference.find_by_name('store_use_inventory_control').is_true?
+ begin
+ # Start a transaction for the stock updating, if something raise an exception, like a database
+ # trigger, it will be rolled back.
+ Item.transaction do
+ for oli in @order.order_line_items do
+ if oli.item.quantity
+ oli.item.update_attribute('quantity', oli.item.quantity-oli.quantity)
+ end
+ end # for
+ end # item transaction
+ rescue => e
+ # Something bad happened when getting the items, it will:
+ # - rollback the inventory update.
+ # - delete the problem item from the cart.
+ # - redirect to checkout to the order items be cleaned and filled again and warn the user.
+
+ # Create a regular expression with the message send by the trigger.
+ # See if it matches the exception, and create a MatchData object.
+ re = /Negative item quantity not allowed./
+ md = re.match(e.message)
+ if md
+ logger.error("AN ITEM ISN'T IN THE STOCK ANYMORE. The error was #{e}")
+ end # if md
+ end # begin/rescue
+
+ removed_items = @cart.check_inventory()
+ logger.info "REMOVED ITEMS: #{removed_items.inspect}"
+ # If any items were removed, flash and alert.
+ if removed_items.size > 0
+ flash_msg = "The following item(s) have gone out of stock before you could purchase them:"
+ for item_name in removed_items do
+ flash_msg << "\n* #{item_name}"
end
- else
- # Redirect to checkout and allow them to enter info again.
- error_message = "Sorry, but your transaction didn't go through.<br/>"
- error_message << "#{order_success}<br/>"
- error_message << "Please try again or contact us."
- redirect_to_checkout(error_message)
- end
- end
+ flash_msg << "\n\n...The item(s) have been removed from your cart."
+
+ if @cart.empty?
+ redirect_to_index(flash_msg) and return
+ else
+ redirect_to_checkout(flash_msg) and return
+ end
+ end
+
+ end # if store_use_inventory_control
+
+
+ begin
+ # Payment processing, if something raise an exception, the stock will
+ # be recovered back and the order will be left in an failed status.
+
+ @title = "Thanks for your order!"
+ cc_processor = Order.get_cc_processor
+ order_success = @order.run_transaction
+ if !(order_success == true) && !(order_success == 5)
+ # Authorize transaction didn't returned true nor paypal transaction returned 5,
+ # So, the payment failed, raise an exception and handle it below.
+ raise order_success
+ else
+ # Payment succeded.
+ if order_success == true
+ @payment_message = "Card processed successfully"
+ clear_cart_and_order(false)
+ elsif cc_processor == Preference::CC_PROCESSORS[1] && (order_success == 5)
+ @payment_message = "Transaction processed successfully"
+ clear_cart_and_order(false)
+ end
+ end
+ rescue => e
+ # Something bad happened.
+
+ # If the inventory control preference is set, we will put it back in place.
+ if Preference.find_by_name('store_use_inventory_control').is_true?
+ begin
+ # Start a transaction for the stock updating, the quantities need to be put back.
+ Item.transaction do
+ for oli in @order.order_line_items do
+ if oli.item.quantity
+ oli.item.update_attribute('quantity', oli.item.quantity+oli.quantity)
+ end
+ end # for
+ end # item transaction
+ rescue => f
+ logger.error("FAILED TO RECOVER THE QUANTITY FOR THE ITEMS OF THE ORDER #{@order.order_number} AFTER FAILED PAYMENT: #{f}")
+ end
+ end # if store_use_inventory_control
+
+ # Now handle the payment error.
+
+ # If Authorize was used.
+ if cc_processor == Preference::CC_PROCESSORS[0]
+ # Redirect to checkout and allow them to enter info again.
+ error_message = "Sorry, but your transaction didn't go through.<br/>"
+ error_message << "#{e}<br/>"
+ error_message << "Please try again or contact us."
+ redirect_to_checkout(error_message)
+ # If Paypal was used.
+ elsif cc_processor == Preference::CC_PROCESSORS[1]
+ case order_success
+ when 4
+ @payment_message = %q\
+ Your order has been processed at PayPal but we
+ have not heard back from them yet. Your order
+ will be ready to ship as soon as we receive
+ confirmation of your payment.
+ \
+ clear_cart_and_order(false)
+ else
+ error_message = "Something went wrong and your transaction failed.<br/>"
+ error_message << "Please try again or contact us."
+ redirect_to_checkout(error_message)
+ end
+ end # elsif
+
+ end # begin/rescue
+
+ end # finish_order
#############################################################################
# PRIVATE METHODS
Powered by
Google Project Hosting