Index: openacs.org-dev/packages/ecommerce/www/shopping-cart-quantities-change.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs.org-dev/packages/ecommerce/www/shopping-cart-quantities-change.tcl,v diff -u -r1.1.1.1 -r1.1.1.2 --- openacs.org-dev/packages/ecommerce/www/shopping-cart-quantities-change.tcl 9 Jul 2002 17:35:05 -0000 1.1.1.1 +++ openacs.org-dev/packages/ecommerce/www/shopping-cart-quantities-change.tcl 8 Oct 2002 15:47:06 -0000 1.1.1.2 @@ -12,7 +12,7 @@ @author heavily modified by jgoler@arsdigita.com @author hbrock@arsdigita.com @creation-date - @cvs-id shopping-cart-quantities-change.tcl,v 3.3.2.8 2000/09/22 01:37:31 kevin Exp + @cvs-id $Id$ @author ported by Jerry Asher (jerry@theashergroup.com) } { return_url:optional @@ -41,7 +41,7 @@ [ec_continue_shopping_options] " - return + ad_script_abort } set order_id [db_string get_order_id "select order_id from ec_orders where order_state='in_basket' and user_session_id=:user_session_id" -default ""] @@ -50,20 +50,10 @@ # so just return them to their empty cart if { [empty_string_p $order_id] } { - ad_returnredirect "shopping-cart.tcl" - return + ad_returnredirect "shopping-cart" + ad_script_abort } -# set product_id_list [db_list get_product_ids "select unique product_id from ec_items where order_id=:order_id"] - -# # if product_id_list is empty, this probably means that they got to this page by pushing back -# # so just return them to their empty cart - -# if { [llength $product_id_list] == 0 } { -# ad_returnredirect "shopping-cart.tcl" -# return -# } - db_foreach get_products_w_attribs " select i.product_id, i.color_choice, @@ -83,27 +73,11 @@ set pid_bak $product_id - set real_quantity([list $product_id $color_choice $size_choice $style_choice]) $r_quantity -} + set array_value [list $product_id $color_choice $size_choice $style_choice] + regsub -all "{" $array_value {} array_value + regsub -all "}" $array_value {} array_value -# You could call a transaction here, but why bother the database -# when you can just check with a foreach whether there are any passed-in -# item combinations that don't match combinations already in the database. -# If we find any such combinations, it means the user's passed in data is -# stale and we should redirect back to the shopping cart. - -foreach product_color_size_style [array names quantity] { - - if { ![info exists real_quantity($product_color_size_style)] } { - - if { [info exists return_url] } { - ad_returnredirect $return_url - } else { - ad_returnredirect shopping-cart.tcl - } - return - - } + set real_quantity($array_value) $r_quantity } # quantity_to_add might be negative @@ -125,60 +99,65 @@ foreach product_color_size_style [array names quantity] { - set quantity_to_add "[expr $quantity($product_color_size_style) - $real_quantity($product_color_size_style)]" + set product_lookup $product_color_size_style - set product_id [lindex $product_color_size_style 0] - set color_choice [lindex $product_color_size_style 1] - set size_choice [lindex $product_color_size_style 2] - set style_choice [lindex $product_color_size_style 3] - - if { $quantity_to_add > 0 } { - set remaining_quantity $quantity_to_add - while { $remaining_quantity > 0 } { - - db_dml insert_new_quantity_to_add "insert into ec_items - (item_id, product_id, color_choice, size_choice, style_choice, order_id, in_cart_date) - values - (ec_item_id_sequence.nextval, :product_id, :color_choice, :size_choice, :style_choice, :order_id, sysdate) - " - set remaining_quantity [expr $remaining_quantity - 1] - } - } elseif { $quantity_to_add < 0 } { - set remaining_quantity [expr abs($quantity_to_add)] + regsub -all "{" $product_lookup {} product_lookup + regsub -all "}" $product_lookup {} product_lookup - set rows_to_delete [list] - while { $remaining_quantity > 0 } { - # determine the rows to delete in ec_items (the last instance of this product within this order) - if { [llength $rows_to_delete] > 0 } { - set extra_condition "and item_id not in ([join $rows_to_delete ", "])" - } else { - set extra_condition "" + if { [info exists real_quantity($product_lookup)] } { + + set quantity_to_add "[expr $quantity($product_color_size_style) - $real_quantity($product_lookup)]" + + set product_id [lindex $product_color_size_style 0] + set color_choice [lindex $product_color_size_style 1] + set size_choice [lindex $product_color_size_style 2] + set style_choice [lindex $product_color_size_style 3] + + if { $quantity_to_add > 0 } { + set remaining_quantity $quantity_to_add + while { $remaining_quantity > 0 } { + + db_dml insert_new_quantity_to_add "insert into ec_items + (item_id, product_id, color_choice, size_choice, style_choice, order_id, in_cart_date) + values + (ec_item_id_sequence.nextval, :product_id, :color_choice, :size_choice, :style_choice, :order_id, sysdate)" + set remaining_quantity [expr $remaining_quantity - 1] } - lappend rows_to_delete [db_string get_rows_to_delete " - select max(item_id) - from ec_items - where product_id=:product_id - and color_choice [ec_decode $color_choice "" "is null" "= :color_choice"] - and size_choice [ec_decode $size_choice "" "is null" "= :size_choice"] - and style_choice [ec_decode $style_choice "" "is null" "= :style_choice"] - and order_id=:order_id $extra_condition - "] + } elseif { $quantity_to_add < 0 } { + set remaining_quantity [expr abs($quantity_to_add)] + + set rows_to_delete [list] + while { $remaining_quantity > 0 } { + # determine the rows to delete in ec_items (the last instance of this product within this order) + if { [llength $rows_to_delete] > 0 } { + set extra_condition "and item_id not in ([join $rows_to_delete ", "])" + } else { + set extra_condition "" + } + lappend rows_to_delete [db_string get_rows_to_delete " + select max(item_id) + from ec_items + where product_id=:product_id + and color_choice [ec_decode $color_choice "" "is null" "= :color_choice"] + and size_choice [ec_decode $size_choice "" "is null" "= :size_choice"] + and style_choice [ec_decode $style_choice "" "is null" "= :style_choice"] + and order_id=:order_id $extra_condition"] - set remaining_quantity [expr $remaining_quantity - 1] + set remaining_quantity [expr $remaining_quantity - 1] + } + db_dml delete_from_ec_items "delete from ec_items where item_id in ([join $rows_to_delete ", "])" } - db_dml delete_from_ec_items "delete from ec_items where item_id in ([join $rows_to_delete ", "])" + # otherwise, do nothing } - # otherwise, do nothing - } - + } } db_release_unused_handles if { [info exists return_url] } { ad_returnredirect $return_url } else { - ad_returnredirect shopping-cart.tcl + ad_returnredirect shopping-cart }