Author: Hugo Melo <hugo@riseup.net>
Fix purchases qtt moving from orders to purchases
plugins/orders_cycle/models/orders_cycle_plugin/item.rb | 8 +++++--- plugins/orders_cycle/models/orders_cycle_plugin/sale.rb | 5 ++++-
diff --git a/plugins/orders_cycle/models/orders_cycle_plugin/item.rb b/plugins/orders_cycle/models/orders_cycle_plugin/item.rb index 3b261bdcd7e3330c8e3653fb56247ee32e15c778..0473abe37149230cabc7d11b0896e1c2aeeba7d1 100644 --- a/plugins/orders_cycle/models/orders_cycle_plugin/item.rb +++ b/plugins/orders_cycle/models/orders_cycle_plugin/item.rb @@ -24,7 +24,7 @@ has_many :suppliers, through: :offered_product has_one :supplier, through: :offered_product after_save :update_order - after_save :change_purchases, if: :cycle + after_save :change_purchase, if: :cycle before_destroy :remove_purchase_item, if: :cycle def cycle @@ -54,8 +54,10 @@ end protected - def change_purchases - return unless ["orders", 'purchases'].include? self.cycle.status + def change_purchase + # if we've already passed through purchases, don't change purchases + return unless self.cycle.status == 'orders' + # when it's draft, it handled by sale#change_purchases return if self.order.status == 'draft' if id_changed? diff --git a/plugins/orders_cycle/models/orders_cycle_plugin/sale.rb b/plugins/orders_cycle/models/orders_cycle_plugin/sale.rb index cbb03b1e0ee9dc77a6f39557ee9e6cc1f3519bc5..63be8f09f2d4a70c61bede8f9bc7bf9778b3e463 100644 --- a/plugins/orders_cycle/models/orders_cycle_plugin/sale.rb +++ b/plugins/orders_cycle/models/orders_cycle_plugin/sale.rb @@ -25,8 +25,10 @@ end def change_purchases return unless self.status_was.present? + # only on sale confirmation if self.ordered_at_was.nil? and self.ordered_at.present? self.add_purchases_items + # only on sale cancellation/unconfirmation elsif self.ordered_at_was.present? and self.ordered_at.nil? self.remove_purchases_items end @@ -60,7 +62,8 @@ def update_purchase_item item return unless supplier_product = item.product.supplier_product return unless supplier = supplier_product.profile - if item.quantity_supplier_accepted_was != item.quantity_supplier_accepted + previous_value = item.quantity_supplier_accepted_was || item.quantity_consumer_ordered + if previous_value != item.quantity_supplier_accepted qtt_diff = (item.quantity_supplier_accepted || 0) - (item.quantity_supplier_accepted_was || 0) elsif item.quantity_consumer_ordered_was != item.quantity_consumer_ordered qtt_diff = (item.quantity_consumer_ordered || 0) - (item.quantity_consumer_ordered_was || 0)