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 +4 -4
- data/app/models/cats/core/receipt.rb +13 -6
- data/app/models/cats/core/receipt_transaction.rb +2 -0
- data/app/models/cats/core/transaction.rb +18 -3
- data/app/services/cats/core/dispatch_service.rb +1 -1
- data/lib/cats/core/version.rb +1 -1
- data/spec/factories/cats/core/receipt_transactions.rb +1 -1
- data/spec/factories/cats/core/receipts.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 20f6349c8a17610b338d05e2fa64a35d76bf0f3737f898ed9ab4d58d76053085
|
4
|
+
data.tar.gz: 74f16a28eaaf4608b0b2d1935331d8837f49cc805511412df57f902c6d421349
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
26
|
+
statuses = [Cats::Core::Dispatch::STARTED, Cats::Core::Dispatch::RECEIVED]
|
27
|
+
return if statuses.include?(dispatch.dispatch_status)
|
27
28
|
|
28
|
-
errors.add(:dispatch, '
|
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 =
|
35
|
-
|
36
|
-
|
35
|
+
received = dispatch.receipts.sum(:quantity)
|
36
|
+
diff = dispatch.quantity - received
|
37
|
+
if new_record?
|
38
|
+
return if quantity <= diff
|
37
39
|
|
38
|
-
|
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
|
@@ -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
|
-
|
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
|
data/lib/cats/core/version.rb
CHANGED
@@ -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 {
|
4
|
+
quantity { 50 }
|
5
5
|
commodity_status { Cats::Core::Commodity::GOOD }
|
6
6
|
status { Cats::Core::Receipt::DRAFT }
|
7
7
|
remark { FFaker::Name.name }
|