Class: Spree::Billet

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/spree/billet.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.generate_shippingObject



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'app/models/spree/billet.rb', line 200

def self.generate_shipping
  return {reason: :there_are_not_any_billets_to_registry, messages: []} if unregistered.empty?
  config = Spree::BilletConfig
  params = {empresa_mae: config.corporate_name,
            agencia: config.agency,
            conta_corrente: config.,
            sequencial_remessa: config.shipping_number}
  document = case config.bank
               when 'banco_brasil'
                 Brcobranca::Remessa::Cnab240::BancoBrasil.new(params.merge!({convenio: config.agreement,
                                                                              carteira: config.wallet,
                                                                              documento_cedente: config.document,
                                                                              variacao: config.variation_wallet}))
               when 'bradesco'
                 Brcobranca::Remessa::Cnab400::Bradesco.new(params.merge!({digito_conta: config.,
                                                                           carteira: config.wallet,
                                                                           codigo_empresa: config.company_code}))
               when 'caixa'
                 Brcobranca::Remessa::Cnab240::Caixa.new(params.merge!({convenio: config.agreement,
                                                                        digito_conta: config.,
                                                                        documento_cedente: config.document,
                                                                        versao_aplicativo: config.app_version}))
               when 'itau'
                 Brcobranca::Remessa::Cnab400::Itau.new(params.merge!({carteira: config.wallet,
                                                                       documento_cedente: config.document,
                                                                       digito_conta: config.}))
               else
                 return {reason: :billet_bank_not_implemented, messages: []}
             end
  document.pagamentos = []
  unregistered.each do |billet|
    next if billet.user.nil?
    doc_user = billet.user.attributes[config.doc_customer_attr] rescue ''
    user_address = billet.order.bill_address
    payment = Brcobranca::Remessa::Pagamento.new(valor: billet.amount,
                                                 data_vencimento: billet.due_date,
                                                 nosso_numero: billet.document_number,
                                                 documento_sacado: doc_user,
                                                 nome_sacado: billet.customer_name,
                                                 endereco_sacado: user_address.address1,
                                                 bairro_sacado: user_address.address2,
                                                 cep_sacado: user_address.zipcode,
                                                 cidade_sacado: user_address.city,
                                                 uf_sacado: user_address.state.abbr)
    document.pagamentos << payment
  end
  shipping = document.gera_arquivo
  if shipping.is_a? String
    config.shipping_number += 1
    # change the status of billets to pending
    unregistered.each { |billet| billet.to_pending! }
  end
  shipping
rescue Brcobranca::RemessaInvalida => invalid
  {reason: :invalid_billets, messages: invalid.to_s.split(', ')}
end

Instance Method Details

#actionsArray

Displays what actions can be done according to payment method

Returns:

  • (Array)


17
18
19
20
21
22
# File 'app/models/spree/billet.rb', line 17

def actions
  act = []
  act << 'capture' if can_capture? payment
  act << 'void' if can_void? payment
  act
end

#amount=(amount) ⇒ Object

Save the amount of the billet if amount is a string, convert to a BigDecimal

copy of Spree::Payment.amount



29
30
31
32
33
34
35
36
37
# File 'app/models/spree/billet.rb', line 29

def amount=(amount)
  self[:amount] =
      case amount
        when String
          separator = I18n.t('number.currency.format.separator')
          number    = amount.delete("^0-9-#{separator}\.").tr(separator, '.')
          number.to_d if number.present?
      end || amount
end

#can_capture?(payment) ⇒ Boolean

Determines whether can capture the payment (only can capture when the state is checkout or pending)

Returns:

  • (Boolean)


46
47
48
# File 'app/models/spree/billet.rb', line 46

def can_capture?(payment)
  payment.pending? || payment.checkout?
end

#can_generate_document?Boolean

Return if it is possible generate the document (when status is waiting registry or pending)

Returns:

  • (Boolean)


187
188
189
# File 'app/models/spree/billet.rb', line 187

def can_generate_document?
  %w(waiting_registry pending).include?(status)
end

#can_void?(payment) ⇒ Boolean

Determines whether can void the payment (only can void when the state is different of void, failure or invalid)

Returns:

  • (Boolean)


57
58
59
# File 'app/models/spree/billet.rb', line 57

def can_void?(payment)
  !%w(void failure invalid).include?(payment.state)
end

#currencyObject

Defines the currency of the billet based in te currency of the order

copy of Spree::Payment.currency



66
67
68
# File 'app/models/spree/billet.rb', line 66

def currency
  order.currency
end

#customer_nameString

Returns the name of the customer according to bill address of order

Returns:

  • (String)


144
145
146
147
148
# File 'app/models/spree/billet.rb', line 144

def customer_name
  "#{order.bill_address.firstname} #{order.bill_address.lastname}"
rescue
  ''
end

#due_dateDate

Calculates the due date according to created at

Returns:

  • (Date)


77
78
79
# File 'app/models/spree/billet.rb', line 77

def due_date
  created_at + Spree::BilletConfig.due_date.days
end

#generate_documentBrcobranca::Boleto::Base

Generate the object from gem brcobranca

Returns:

  • (Brcobranca::Boleto::Base)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/models/spree/billet.rb', line 87

def generate_document
  config = Spree::BilletConfig
  doc_user = user.attributes[config.doc_customer_attr] rescue ''

  params = {cedente: config.corporate_name,
            documento_cedente: config.document,
            cedente_endereco: config.address,
            sacado: self.customer_name,
            sacado_documento: doc_user,
            sacado_endereco: order.bill_address.full_address,
            numero_documento: document_number,
            dias_vencimento: config.due_date,
            data_documento: Date.parse(created_at.to_s),
            valor: amount,
            aceite: config.acceptance,
            agencia: config.agency,
            conta_corrente: config.,
            convenio: config.agreement,
            carteira: config.wallet,
            variacao: config.variation_wallet
  }
  (1..6).each { |cont| params["instrucao#{cont}".to_sym] = config["instruction_#{cont}"] }
  if config.bank == 'sicredi' then
    params.merge!({posto: config.office_code,
                   byte_idt: config.byte_idt})
  end

  document = case config.bank
               when 'banco_brasil' then Brcobranca::Boleto::BancoBrasil.new params
               when 'bradesco'     then Brcobranca::Boleto::Bradesco.new params
               when 'caixa'        then Brcobranca::Boleto::Caixa.new params
               when 'santander'    then Brcobranca::Boleto::Santander.new params
               when 'itau'         then Brcobranca::Boleto::Itau.new params
               when 'sicredi'      then Brcobranca::Boleto::Sicredi.new params
               else
              raise 'It is necessary set the billet config'
             end
  document
end

#moneyObject Also known as: display_amount

Return the amount converted to Money according to currency

copy of Spree::Payment.money



132
133
134
# File 'app/models/spree/billet.rb', line 132

def money
  Spree::Money.new(amount, { currency: currency })
end

#paid?Boolean

Returns if billet is paid

Returns:

  • (Boolean)


156
157
158
# File 'app/models/spree/billet.rb', line 156

def paid?
  status == 'paid'
end

#pending?Boolean

Returns if billet is pending

Returns:

  • (Boolean)


166
167
168
# File 'app/models/spree/billet.rb', line 166

def pending?
  status == 'pending'
end

#to_pending!Object

Chanege the status to pending



195
196
197
198
# File 'app/models/spree/billet.rb', line 195

def to_pending!
  self.status = 'pending'
  self.save
end

#waiting_registry?Boolean

Returns if billet is waiting registry

Returns:

  • (Boolean)


176
177
178
# File 'app/models/spree/billet.rb', line 176

def waiting_registry?
  status == 'waiting_registry'
end