Class: Spree::Billet
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Spree::Billet
- Defined in:
- app/models/spree/billet.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#actions ⇒ Array
Displays what actions can be done according to payment method.
-
#amount=(amount) ⇒ Object
Save the amount of the billet if amount is a string, convert to a BigDecimal.
-
#can_capture?(payment) ⇒ Boolean
Determines whether can capture the payment (only can capture when the state is checkout or pending).
-
#can_generate_document? ⇒ Boolean
Return if it is possible generate the document (when status is waiting registry or pending).
-
#can_void?(payment) ⇒ Boolean
Determines whether can void the payment (only can void when the state is different of void, failure or invalid).
-
#currency ⇒ Object
Defines the currency of the billet based in te currency of the order.
-
#customer_name ⇒ String
Returns the name of the customer according to bill address of order.
-
#due_date ⇒ Date
Calculates the due date according to created at.
-
#generate_document ⇒ Brcobranca::Boleto::Base
Generate the object from gem brcobranca.
-
#money ⇒ Object
(also: #display_amount)
Return the amount converted to Money according to currency.
-
#paid? ⇒ Boolean
Returns if billet is paid.
-
#pending? ⇒ Boolean
Returns if billet is pending.
-
#to_pending! ⇒ Object
Chanege the status to pending.
-
#waiting_registry? ⇒ Boolean
Returns if billet is waiting registry.
Class Method Details
.generate_shipping ⇒ Object
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 256 |
# File 'app/models/spree/billet.rb', line 201 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.account, 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.account_digit, carteira: config.wallet, codigo_empresa: config.company_code})) when 'caixa' Brcobranca::Remessa::Cnab240::Caixa.new(params.merge!({convenio: config.agreement, digito_conta: config.account_digit, 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.account_digit})) 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
#actions ⇒ Array
Displays what actions can be done according to payment method
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)
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)
188 189 190 |
# File 'app/models/spree/billet.rb', line 188 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)
57 58 59 |
# File 'app/models/spree/billet.rb', line 57 def can_void?(payment) !%w(void failure invalid).include?(payment.state) end |
#currency ⇒ Object
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_name ⇒ String
Returns the name of the customer according to bill address of order
145 146 147 148 149 |
# File 'app/models/spree/billet.rb', line 145 def customer_name "#{order.bill_address.firstname} #{order.bill_address.lastname}" rescue '' end |
#due_date ⇒ Date
Calculates the due date according to created at
77 78 79 |
# File 'app/models/spree/billet.rb', line 77 def due_date created_at + Spree::BilletConfig.due_date.days end |
#generate_document ⇒ Brcobranca::Boleto::Base
Generate the object from gem brcobranca
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 126 |
# File 'app/models/spree/billet.rb', line 87 def generate_document config = Spree::BilletConfig doc_user = user.attributes[config.doc_customer_attr] rescue '' due_date = Date.today + config.due_date.days 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, data_vencimento: due_date, data_documento: Date.parse(created_at.to_s), valor: amount, aceite: config.acceptance, agencia: config.agency, conta_corrente: config.account, 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 |
#money ⇒ Object Also known as: display_amount
Return the amount converted to Money according to currency
copy of Spree::Payment.money
133 134 135 |
# File 'app/models/spree/billet.rb', line 133 def money Spree::Money.new(amount, { currency: currency }) end |
#paid? ⇒ Boolean
Returns if billet is paid
157 158 159 |
# File 'app/models/spree/billet.rb', line 157 def paid? status == 'paid' end |
#pending? ⇒ Boolean
Returns if billet is pending
167 168 169 |
# File 'app/models/spree/billet.rb', line 167 def pending? status == 'pending' end |
#to_pending! ⇒ Object
Chanege the status to pending
196 197 198 199 |
# File 'app/models/spree/billet.rb', line 196 def to_pending! self.status = 'pending' self.save end |
#waiting_registry? ⇒ Boolean
Returns if billet is waiting registry
177 178 179 |
# File 'app/models/spree/billet.rb', line 177 def waiting_registry? status == 'waiting_registry' end |