cats_core 1.1.34 → 1.1.35

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 04aa5725df06bf149cf057515dd11ef721dc9511b9aa9555cd2dd311ad9e96ff
4
- data.tar.gz: ec1393337a6276fec08d3897618bb2ed345f66dd027a8a07efc59544bcd326d6
3
+ metadata.gz: 03dde5a3e8c977259c5f5c64e2c72feb8212babba6ed10e7d02ea85db7e5eac2
4
+ data.tar.gz: 970ad6b55fbe1ff4d4c2cc6cecc48585f6d51b5773ba876cce143ad241827f6a
5
5
  SHA512:
6
- metadata.gz: d5b97bdff8990988dd0bebeba15e24fadc28328970e1343f36617c3ee93a149af2470320ad8b7adafca36dcfddcb927acbed34e67b325dbb3828e2941ab5f9cc
7
- data.tar.gz: ec0011655502fd35e6313bd4e5f079557cc58de783d093d640e122fbcd894bbcca459225ebafdc83f7c4c2770a116477dd4f09061ee33c5fadeee0e32790445d
6
+ metadata.gz: a7e4bd908db5af74c49d9306580fbfb743de780e000ba3c8d3406da35f454cfc74fef146255d1bde7e1677207cc9cb0b5bb75b2c357a2c11e0057de631c56ea1
7
+ data.tar.gz: 2d0658c343d3d30b3fc86c11c41fc273c45b00014bda643d0afd21f21a43fcf7803e1b8b6f9de093aee489e76f1b16f3efb8ade979b9c987d923c7c88ea22d24
@@ -15,6 +15,26 @@ module Cats
15
15
  validates :quantity, presence: true, numericality: { greater_than: 0 }
16
16
  validates :commodity_status, inclusion: { in: Cats::Core::Commodity::COMMODITY_STATUSES }
17
17
  validates :allocation_status, inclusion: { in: ALLOCATION_STATUSES }
18
+ validate :validate_quantity_against_items, :validate_quantity_against_commodity
19
+
20
+ def validate_quantity_against_items
21
+ return unless quantity
22
+
23
+ return unless quantity_changed?
24
+
25
+ allocated = allocation_items.sum(:quantity)
26
+ errors.add(:quantity, "is below items quantity. Minimum allowed is #{allocated}") if quantity < allocated
27
+ end
28
+
29
+ def validate_quantity_against_commodity
30
+ return unless quantity && commodity
31
+
32
+ allocated = Allocation.where(commodity: commodity).sum(:quantity)
33
+ remaining = commodity.quantity - allocated
34
+
35
+ remaining += quantity_was if quantity_was
36
+ errors.add(:quantity, "exceeds commodity quantity. Maximum allowed is #{remaining}") if quantity > remaining
37
+ end
18
38
 
19
39
  delegate(:reference_no, to: :rhn_request, prefix: :rhn, allow_nil: true)
20
40
  delegate(:batch_no, to: :commodity, prefix: true)
@@ -6,7 +6,7 @@ module Cats
6
6
 
7
7
  validates :from, :to, presence: true
8
8
  validates :quantity, presence: true, numericality: { greater_than: 0 }
9
- validate :validate_source_and_destination
9
+ validate :validate_source_and_destination, :validate_quantity
10
10
 
11
11
  def validate_source_and_destination
12
12
  return unless allocation.present? && destination.present?
@@ -14,6 +14,16 @@ module Cats
14
14
  errors.add(:base, 'source and destination cannot be the same') if allocation.source == destination
15
15
  end
16
16
 
17
+ def validate_quantity
18
+ return unless quantity && allocation
19
+
20
+ return unless quantity_changed?
21
+
22
+ allocated = AllocationItem.where(allocation: allocation).sum(:quantity)
23
+ remaining = allocation.quantity - allocated
24
+ errors.add(:quantity, "exceeds allocated quantity. Maximum allowed is #{remaining}") if quantity > remaining
25
+ end
26
+
17
27
  delegate(:name, to: :destination, prefix: true)
18
28
  delegate(:reference_no, to: :allocation, prefix: true)
19
29
  delegate(:location_type, to: :destination, prefix: true)
@@ -1,5 +1,5 @@
1
1
  module Cats
2
2
  module Core
3
- VERSION = '1.1.34'.freeze
3
+ VERSION = '1.1.35'.freeze
4
4
  end
5
5
  end
@@ -2,7 +2,7 @@ FactoryBot.define do
2
2
  factory :allocation_item, class: 'Cats::Core::AllocationItem' do
3
3
  allocation
4
4
  destination factory: :location
5
- quantity { 100 }
5
+ quantity { 50 }
6
6
  from { Date.today }
7
7
  to { Date.today + 1.week }
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cats_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.34
4
+ version: 1.1.35
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henock L.