Class: Cats::Core::ReceiptTransaction

Inherits:
Transaction show all
Defined in:
app/models/cats/core/receipt_transaction.rb

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



62
63
64
65
66
67
# File 'app/models/cats/core/receipt_transaction.rb', line 62

def self.ransackable_associations(_auth_object = nil)
  %w[
    receipt_authorization
    destination
  ]
end

.ransackable_attributes(_auth_object = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/cats/core/receipt_transaction.rb', line 50

def self.ransackable_attributes(_auth_object = nil)
  %w[
    destination_id
    quantity
    receipt_authorization_id
    receipt_number
    status
    transaction_date
    unit_id
  ]
end

Instance Method Details

#commitObject



40
41
42
43
44
45
46
47
48
# File 'app/models/cats/core/receipt_transaction.rb', line 40

def commit
  ReceiptTransaction.transaction do
    destination.quantity += UnitConversion.convert(unit, destination.unit, quantity)
    destination.stack_status = Cats::Core::Stack::ALLOCATED
    destination.save!
    self.status = COMMITTED
    save!
  end
end

#set_unitObject



18
19
20
21
22
23
24
# File 'app/models/cats/core/receipt_transaction.rb', line 18

def set_unit
  return if unit

  return unless receipt_authorization

  self.unit = receipt_authorization.unit
end

#validate_quantityObject



33
34
35
36
37
38
# File 'app/models/cats/core/receipt_transaction.rb', line 33

def validate_quantity
  return unless quantity && destination && receipt_authorization

  transactions = ReceiptTransaction.where(receipt_authorization: receipt_authorization)
  ValidationUtil.validate_quantity(self, "receipt_authorization", transactions, "authorized")
end

#validate_receiptObject



26
27
28
29
30
31
# File 'app/models/cats/core/receipt_transaction.rb', line 26

def validate_receipt
  return unless receipt_authorization

  status = receipt_authorization.status
  errors.add(:receipt_authorization, "must be confirmed.") if status == ReceiptAuthorization::AUTHORIZED
end