Last active
March 19, 2020 02:13
-
-
Save agungsetiawan/78f3c8842923a40d65edeb946eaf8d06 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 AssetType < ApplicationRecord | |
has_many :asset_inventories | |
after_save :set_accuracy | |
def set_accuracy | |
update_column(:accuracy, asset_accuracy) | |
end | |
def asset_accuracy | |
if quantity_on_hand.to_f > 0 | |
((asset_inventories.count.to_f / quantity_on_hand.to_f) * 100).round(2) | |
elsif asset_inventories.count > 0 | |
asset_inventories.count * 100 | |
elsif quantity_on_hand.to_f == 0 && asset_inventories.count == 0 | |
100 | |
else | |
0 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment