Class: Centralpos::Batch

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/centralpos/batch.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#ensure_array, #in_time_zone, #inspect

Constructor Details

#initialize(account: nil, **params) ⇒ Batch

Returns a new instance of Batch.



6
7
8
9
10
# File 'lib/centralpos/batch.rb', line 6

def initialize(account: nil, **params)
  @account =  if  && .is_a?(Centralpos::)
  @id = params.delete(:id) || params.delete(:id_presentacion)
  process_data(params)
end

Instance Attribute Details

#cardObject (readonly)

Returns the value of attribute card.



4
5
6
# File 'lib/centralpos/batch.rb', line 4

def card
  @card
end

#card_idObject (readonly)

Returns the value of attribute card_id.



4
5
6
# File 'lib/centralpos/batch.rb', line 4

def card_id
  @card_id
end

#commerce_numberObject (readonly)

Returns the value of attribute commerce_number.



4
5
6
# File 'lib/centralpos/batch.rb', line 4

def commerce_number
  @commerce_number
end

#date_sinceObject (readonly)

Returns the value of attribute date_since.



4
5
6
# File 'lib/centralpos/batch.rb', line 4

def date_since
  @date_since
end

#date_untilObject (readonly)

Returns the value of attribute date_until.



4
5
6
# File 'lib/centralpos/batch.rb', line 4

def date_until
  @date_until
end

#idObject (readonly)

Returns the value of attribute id.



4
5
6
# File 'lib/centralpos/batch.rb', line 4

def id
  @id
end

#periodObject (readonly)

Returns the value of attribute period.



4
5
6
# File 'lib/centralpos/batch.rb', line 4

def period
  @period
end

#processed_dateObject (readonly)

Returns the value of attribute processed_date.



4
5
6
# File 'lib/centralpos/batch.rb', line 4

def processed_date
  @processed_date
end

#statusObject (readonly)

Returns the value of attribute status.



4
5
6
# File 'lib/centralpos/batch.rb', line 4

def status
  @status
end

#status_idObject (readonly)

Returns the value of attribute status_id.



4
5
6
# File 'lib/centralpos/batch.rb', line 4

def status_id
  @status_id
end

Instance Method Details

#add_transaction(transaction) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/centralpos/batch.rb', line 29

def add_transaction(transaction)
   && ensure_transaction_valid!(transaction)

  response = @account.gateway.call(:add_registro, batch_params.merge(transaction.to_add_params))

  if response[:success] && response[:error].nil?
    { transaction: transaction.to_hash }
  else
    response
  end
end

#can_process?(date_when = nil) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
# File 'lib/centralpos/batch.rb', line 69

def can_process?(date_when = nil)
  date_when = Time.now if date_when.nil? || !date_when.is_a?(DateTime)
  date_when_utc = date_when.utc

  (@date_since.utc <= date_when_utc) && (date_when_utc <= @date_until.utc)
end

#get_transaction(transaction) ⇒ Object



63
64
65
66
67
# File 'lib/centralpos/batch.rb', line 63

def get_transaction(transaction)
  return unless has_transaction?(transaction)

  { transaction: transactions_by_id.fetch(transaction.owner_id) }
end

#has_transaction?(transaction) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/centralpos/batch.rb', line 59

def has_transaction?(transaction)
  transactions_by_id.key?(transaction.owner_id)
end

#processObject



76
77
78
79
80
# File 'lib/centralpos/batch.rb', line 76

def process
  

  @account.gateway.call(:put_procesa_presentacion, batch_params)
end

#remove_transaction(transaction) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/centralpos/batch.rb', line 41

def remove_transaction(transaction)
   && ensure_transaction_valid!(transaction)

  response = @account.gateway.call(:del_registro, batch_params.merge(transaction.to_remove_params))

  if response[:success] && response[:error].nil?
    { transaction: transaction.to_hash }
  else
    response
  end
end

#transactionsObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/centralpos/batch.rb', line 12

def transactions
  

  response = @account.gateway.call(:list_registros, batch_params)

  if response[:success] && response[:error].nil?
    return [] if response[:result][:lista_registros].nil?

    entries = ensure_array(response[:result][:lista_registros][:registro])
    entries.map do |entries_data|
      Centralpos::Transaction.load_it(entries_data.merge(batch: self))
    end
  else
    response
  end
end

#update_transaction(transaction) ⇒ Object



53
54
55
56
57
# File 'lib/centralpos/batch.rb', line 53

def update_transaction(transaction)
   && ensure_transaction_valid!(transaction)
  remove_transaction(transaction)
  add_transaction(transaction)
end