Class: Cats::Core::DispatchTransaction
Constant Summary
Constants inherited
from Transaction
Transaction::COMMITTED, Transaction::DRAFT, Transaction::STATUSES
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Transaction
#set_status
Class Method Details
.ransackable_associations(_auth_object = nil) ⇒ Object
82
83
84
85
86
87
|
# File 'app/models/cats/core/dispatch_transaction.rb', line 82
def self.ransackable_associations(_auth_object = nil)
%w[
source
dispatch_authorization
]
end
|
.ransackable_attributes(_auth_object = nil) ⇒ Object
71
72
73
74
75
76
77
78
79
80
|
# File 'app/models/cats/core/dispatch_transaction.rb', line 71
def self.ransackable_attributes(_auth_object = nil)
%w[
dispatch_authorization_id
quantity
source_id
status
transaction_date
unit_id
]
end
|
Instance Method Details
#commit ⇒ Object
24
25
26
27
28
29
30
31
32
|
# File 'app/models/cats/core/dispatch_transaction.rb', line 24
def commit
Transaction.transaction do
source.quantity -= UnitConversion.convert(unit, source.unit, quantity)
source.save!
self.status = COMMITTED
save!
end
end
|
#set_unit ⇒ Object
16
17
18
19
20
21
22
|
# File 'app/models/cats/core/dispatch_transaction.rb', line 16
def set_unit
return if unit
return unless dispatch_authorization
self.unit = dispatch_authorization.unit
end
|
#skip_quantity_validation ⇒ Object
62
63
64
65
66
67
68
69
|
# File 'app/models/cats/core/dispatch_transaction.rb', line 62
def skip_quantity_validation
return true unless dispatch_authorization
return true if status == COMMITTED
![Dispatch::DRAFT, Dispatch::APPROVED].include?(dispatch_authorization.dispatch.dispatch_status)
end
|
#validate_authorized_quantity ⇒ Object
55
56
57
58
59
60
|
# File 'app/models/cats/core/dispatch_transaction.rb', line 55
def validate_authorized_quantity
return unless quantity && unit && dispatch_authorization
transactions = DispatchTransaction.where(dispatch_authorization: dispatch_authorization)
ValidationUtil.validate_quantity(self, "dispatch_authorization", transactions, "authorized")
end
|
#validate_dispatch ⇒ Object
34
35
36
37
38
39
|
# File 'app/models/cats/core/dispatch_transaction.rb', line 34
def validate_dispatch
return unless dispatch_authorization
status = dispatch_authorization.dispatch.dispatch_status
errors.add(:base, "Dispatch must be approved first.") if status == Dispatch::DRAFT
end
|
#validate_source_quantity ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'app/models/cats/core/dispatch_transaction.rb', line 41
def validate_source_quantity
return unless quantity && source
total = UnitConversion.convert(source.unit, unit, source.quantity)
transactions = DispatchTransaction.where(status: DRAFT, source: source)
used = UnitConversion.harmonized_total(transactions, unit)
used -= quantity_was if quantity_was
remaining = total - used
return unless quantity > remaining
errors.add(:quantity, "exceeds source quantity. Maximum allowed is #{remaining} #{unit.abbreviation}")
end
|