Skip to content

Instantly share code, notes, and snippets.

@mabsboza
Created February 27, 2019 21:08
Show Gist options
  • Save mabsboza/8f8396ebd3663dcb94fc2c86f49ab305 to your computer and use it in GitHub Desktop.
Save mabsboza/8f8396ebd3663dcb94fc2c86f49ab305 to your computer and use it in GitHub Desktop.
update stock
old_stock = Inventory::StockByWarehouse.where(master_data_item: master_data_item)
.where(' warehouse_id in (?)', warehouses_ids)
.select('sum(physical_quantity) as physical_quantity,
sum(reserved_quantity) as reserved_quantity')
old_total = old_stock[0].physical_quantity.nil? ? 0 : (old_stock[0].physical_quantity * master_data_item.unit_cost)
buy = (transaction_cost * quantity)
new_cost = 0
if old_stock[0].physical_quantity != 0
new_cost = (buy + old_total) / (
(old_stock[0].physical_quantity.nil? ? 0 : old_stock[0].physical_quantity) + quantity)
else
new_cost = transaction_cost
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment