Created
June 9, 2009 04:26
-
-
Save jimmybaker/126265 to your computer and use it in GitHub Desktop.
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
class InventoryTransaction < ActiveRecord::Base | |
belongs_to :inventory_adjustable, :polymorphic => true | |
belongs_to :inventory | |
after_create :adjust_inventory | |
def adjust_inventory | |
puts "current inventory: #{inventory.quantity_on_hand}" | |
inventory.quantity_on_hand += adjustment | |
inventory.save | |
end | |
def readjust_inventory(new_adjustment, new_description) | |
inventory.quantity_on_hand += (adjustment * -1) | |
self.adjustment = new_adjustment | |
self.description = new_description | |
self.save | |
adjust_inventory | |
end | |
end |
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
class ShipmentItem < ActiveRecord::Base | |
belongs_to :shipment | |
belongs_to :sales_order_item | |
has_one :inventory_transaction, :as => :inventory_adjustable | |
named_scope :shipped, :include => :shipment, :conditions => { 'shipments.state' => :shipped } | |
# callback | |
after_save :adjust_inventory | |
def product | |
@product ||= sales_order_item.product | |
end | |
def adjust_inventory | |
if inventory_transaction | |
inventory_transaction.readjust_inventory(self.quantity * -1, "Updated: #{quantity} for Shipment ##{self.shipment.id}") | |
inventory_transaction.adjust_inventory | |
else | |
inventory_transaction = InventoryTransaction.new({ | |
:adjustment => (self.quantity * -1), | |
:inventory_id => product.inventory_for_warehouse(shipment.warehouse).id, | |
:description => "#{quantity} for Shipment ##{self.shipment.id}"}) | |
inventory_transaction.save | |
end | |
end | |
end |
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
require 'test_helper' | |
class ShipmentItemTest < ActiveSupport::TestCase | |
should_belong_to :shipment | |
should_belong_to :sales_order_item | |
should_have_one :inventory_transaction | |
context "A ShipmentItem" do | |
should "reduce the quantity on hand for it's product" do | |
@shipment_item = Factory.build(:shipment_item) | |
@inventory = Factory(:inventory, :product_id => @shipment_item.product.id, :warehouse_id => @shipment_item.shipment.warehouse_id) | |
assert_difference '@inventory.quantity_on_hand', (@shipment_item.quantity * -1) do | |
@shipment_item.save | |
@inventory.reload | |
end | |
end | |
should "when updated, re-adjust it's inventory adjustment to reflect the new quantity" do | |
@shipment_item = Factory.build(:shipment_item) | |
@inventory = Factory(:inventory, :product_id => @shipment_item.product.id, :warehouse_id => @shipment_item.shipment.warehouse_id) | |
assert_difference '@inventory.quantity_on_hand', (@shipment_item.quantity * -1) do | |
@shipment_item.save | |
@inventory.reload | |
end | |
old_quantity = @shipment_item.quantity | |
@shipment_item.quantity = 30 | |
assert_difference '@inventory.quantity_on_hand', (@shipment_item.quantity * -1) - (old_quantity * -1) do | |
@shipment_item.save | |
@inventory.reload | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment