cats_core 1.1.2 → 1.1.3

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: ae62c0b230969901c7405d3bdf56b72dc099e77e4e6d9a21ba89243f91a1bd07
4
- data.tar.gz: cc0d85257ffb25fc70a890011b1ae670dd18a0ea6938f79e73ec87a757787cd1
3
+ metadata.gz: 20f6349c8a17610b338d05e2fa64a35d76bf0f3737f898ed9ab4d58d76053085
4
+ data.tar.gz: 74f16a28eaaf4608b0b2d1935331d8837f49cc805511412df57f902c6d421349
5
5
  SHA512:
6
- metadata.gz: 582d0f4d63492a2ce3b3e12a7d6e84088c5c5badab3135cb108fc8876348f3e593f70d0498d6c23e79de143efb04436fd858325c6bac8a5bf29785d3a1d4d899
7
- data.tar.gz: 581e221e3dff760e51095cc7094736b0a2c7f7c261d4693c8f811f3b9e2aac1ee5093ba6823895bb910488947f8616cee32b799178b5d1da64905ca40cc7b20c
6
+ metadata.gz: 1b3dbbd8c4da02cb692bf9eae19d6e653ced98de74f54a6a9cae9c6c0dddbe2dfd7fb7b5ec4d1881445e0cf159a2e093111b2acc25dd72309b2c183ff6f94c5b
7
+ data.tar.gz: 05ad4779c0d90d975fc172c4c98c6729e3ff45092953408c31f4cdade4b01dae94eaf655dc857d793e6064770a3a44b93127e62b31ee890633871a509998768a
@@ -23,19 +23,26 @@ module Cats
23
23
  def validate_dispatch_status
24
24
  return unless dispatch
25
25
 
26
- return if dispatch.dispatch_status == Cats::Core::Dispatch::STARTED
26
+ statuses = [Cats::Core::Dispatch::STARTED, Cats::Core::Dispatch::RECEIVED]
27
+ return if statuses.include?(dispatch.dispatch_status)
27
28
 
28
- errors.add(:dispatch, 'Dispatch should be in "Started" state.')
29
+ errors.add(:dispatch, 'should be in "Started" state.')
29
30
  end
30
31
 
31
32
  def validate_total_quantity
32
33
  return unless dispatch && quantity
33
34
 
34
- received = Cats::Core::Receipt.where(dispatch: dispatch).sum(:quantity)
35
- remaining = dispatch.quantity - received
36
- return if quantity <= remaining
35
+ received = dispatch.receipts.sum(:quantity)
36
+ diff = dispatch.quantity - received
37
+ if new_record?
38
+ return if quantity <= diff
37
39
 
38
- errors.add(:quantity, "Total receipt quantity is higher than dispatch quantity (Max = #{remaining}).")
40
+ errors.add(:quantity, "total is higher than dispatch quantity (Max = #{diff}).")
41
+ else
42
+ return unless diff.negative?
43
+
44
+ errors.add(:quantity, "total is higher than dispatch quantity (Max = #{diff.abs}).")
45
+ end
39
46
  end
40
47
  end
41
48
  end
@@ -3,6 +3,8 @@ module Cats
3
3
  class ReceiptTransaction < Transaction
4
4
  belongs_to :source, class_name: 'Cats::Core::Receipt'
5
5
  belongs_to :destination, class_name: 'Cats::Core::Stack'
6
+
7
+ delegate(:code, to: :destination, prefix: true)
6
8
  end
7
9
  end
8
10
  end
@@ -12,12 +12,14 @@ module Cats
12
12
  validates :transaction_date, :quantity, :status, presence: true
13
13
  validates :quantity, numericality: { greater_than: 0 }
14
14
  validates :status, inclusion: { in: STATUSES }
15
- validate :validate_quantity
15
+ validate :validate_quantity, unless: :skip_quantity_validation
16
16
 
17
17
  def validate_quantity
18
18
  return unless quantity.present? && source.present?
19
19
 
20
- errors.add(:quantity, 'cannot be more than source quantity') if quantity > source.quantity
20
+ total = self.class.where(source: source).sum(:quantity)
21
+ diff = source.quantity - total
22
+ errors.add(:quantity, "total is higher than source quantity (Max = #{diff}).") if quantity > diff
21
23
  end
22
24
 
23
25
  def commit
@@ -27,10 +29,23 @@ module Cats
27
29
  source.save
28
30
  destination.save
29
31
  self.status = COMMITTED
30
- save
32
+ save!
31
33
  end
32
34
  end
33
35
 
36
+ def skip_quantity_validation
37
+ # Quantity validation should be skipped if we are commiting transactions.
38
+ (
39
+ instance_of?(Cats::Core::DispatchTransaction) &&
40
+ destination &&
41
+ destination.dispatch_status == Cats::Core::Dispatch::STARTED
42
+ ) || (
43
+ instance_of?(Cats::Core::ReceiptTransaction) &&
44
+ source &&
45
+ source.status == Cats::Core::Receipt::STACKING
46
+ )
47
+ end
48
+
34
49
  def set_status
35
50
  return unless new_record?
36
51
 
@@ -126,7 +126,7 @@ module Cats
126
126
  dispatch.dispatch_status = Cats::Core::Dispatch::RECEIVED
127
127
  dispatch.receipts.each { |r| r.status = Cats::Core::Receipt::CONFIRMED }
128
128
  dispatch.save
129
- dispatch.receipts.each(&:save)
129
+ dispatch.receipts.each(&:save!)
130
130
  end
131
131
 
132
132
  dispatch
@@ -1,5 +1,5 @@
1
1
  module Cats
2
2
  module Core
3
- VERSION = '1.1.2'.freeze
3
+ VERSION = '1.1.3'.freeze
4
4
  end
5
5
  end
@@ -1,5 +1,5 @@
1
1
  FactoryBot.define do
2
- factory :receipt_transaction do
2
+ factory :receipt_transaction, class: 'Cats::Core::ReceiptTransaction' do
3
3
  source factory: :receipt
4
4
  destination factory: :stack
5
5
  transaction_date { Date.today }
@@ -1,7 +1,7 @@
1
1
  FactoryBot.define do
2
2
  factory :receipt, class: 'Cats::Core::Receipt' do
3
3
  dispatch factory: :dispatch, dispatch_status: Cats::Core::Dispatch::STARTED
4
- quantity { 1.5 }
4
+ quantity { 50 }
5
5
  commodity_status { Cats::Core::Commodity::GOOD }
6
6
  status { Cats::Core::Receipt::DRAFT }
7
7
  remark { FFaker::Name.name }
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.2
4
+ version: 1.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henock L.