Sha256: 97e4162851ddcf0f995233ca84ba4026dd4d53dda96757b1b36f7b53fd70c86c
Contents?: true
Size: 1.17 KB
Versions: 60
Compression:
Stored size: 1.17 KB
Contents
module Comee module Core class WarehouseShipmentItem < ApplicationRecord before_save :update_inventory belongs_to :warehouse_shipment belongs_to :sales_order_item validates :quantity, presence: true validate :validate_inventory_quantity def validate_inventory_quantity return unless quantity && sales_order_item inventory = Inventory.find_by(product_id: sales_order_item.customer_order_item.product_id) unless inventory errors.add(:base, "Inventory entry not found for product `#{sales_order_item.customer_order_item.product.name}`.") return end old_quantity = quantity_was || 0 diff = inventory.quantity - (quantity - old_quantity) errors.add(:quantity, "exceeds available quantity in inventory by #{diff.abs}.") if diff.negative? end def update_inventory inventory = Inventory.find_by(product_id: sales_order_item.customer_order_item.product_id) return unless quantity_changed? old_quantity = quantity_was || 0 diff = quantity - old_quantity inventory.quantity -= diff inventory.save! end end end end
Version data entries
60 entries across 60 versions & 1 rubygems