Class: Spree::Admin::BilletsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/spree/admin/billets_controller.rb

Instance Method Summary collapse

Instance Method Details

#registerObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/spree/admin/billets_controller.rb', line 7

def register
  shipping = Spree::Billet.generate_shipping
  if shipping.is_a? String
    # formats the name of the file
    name = ''
    name << 'C' + Spree::BilletConfig.bank[0].upcase
    name << Date.today.day.to_s.rjust(2, '0')
    name << Date.today.month.to_s.rjust(2, '0')
    name << SecureRandom.hex(1).upcase
    name

    send_data shipping, :content_type => 'text/plain', :filename => "#{name}.rem"
  else
    flash[:error] = Spree.t("errors.#{shipping[:reason]}")
    redirect_to :admin_billets_shipping
  end
end

#returnObject



25
26
# File 'app/controllers/spree/admin/billets_controller.rb', line 25

def return
end

#return_infoObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/spree/admin/billets_controller.rb', line 28

def return_info
  data = case params[:file_type].to_sym
           when :cnab240 then Brcobranca::Retorno::RetornoCnab240.load_lines params[:file].path
           when :cnab400 then Brcobranca::Retorno::RetornoCnab400.load_lines params[:file].path
           when :cbr643  then Brcobranca::Retorno::RetornoCbr643.load_lines params[:file].path
         end

  @lines = []
  data.each do |line|
    # se for o registro trailer passa para a proxima iteracao
    next if line == data.last
    payment = Spree::Payment.find_by id: line.nosso_numero

    if payment and line.valor_recebido.to_i > 0 and !(payment.completed? or payment.void?)
      amount_paid = line.valor_recebido.to_f + line.valor_tarifa.to_f
      payment.capture! amount_paid

      # formats the date
      year = line.data_credito.size == 6 ? line.data_credito : "20#{line.data_credito}"
      paid_at = "#{year}#{line.data_credito[2..3]}#{line.data_credito[0..1]}".to_date
      # formats the value
      value = (line.valor_titulo.to_f / 100) + (line.valor_tarifa.to_f / 100)

      @lines.push({document_number: line.nosso_numero,
                   value: Spree::Money.new(value, { currency: payment.currency }),
                   paid_at: paid_at,
                   order_id: payment.order_id,
                   order_number: payment.order.number})
    end
  end
rescue
  flash[:error] = Spree.t('errors.error_in_billets_return')
end

#shippingObject



3
4
5
# File 'app/controllers/spree/admin/billets_controller.rb', line 3

def shipping
  @billets = Spree::Billet.unregistered
end