Skip to content

Commit

Permalink
Agregando clase para obtención de totales
Browse files Browse the repository at this point in the history
  • Loading branch information
hermes-logicalbricks committed Jan 18, 2014
1 parent 5dc8e20 commit 34b977d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/fm_timbrado_cfdi.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "fm_timbrado_cfdi/version"
require "fm_timbrado_cfdi/fm_cliente"
require "fm_timbrado_cfdi/fm_informacion_cfdi"
require 'nokogiri'
require 'base64'

Expand Down
51 changes: 51 additions & 0 deletions lib/fm_timbrado_cfdi/fm_informacion_cfdi.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# -*- encoding : utf-8 -*-
require 'nokogiri'

module FmTimbradoCfdi
class FmInformacionCfdi
attr_reader :total, :subtotal, :descuento

def initialize ( nodo_timbre )
parse( nodo_timbre )
end

private
def parse ( nodo_timbre )
xml = Nokogiri::XML(nodo_timbre)
ns = generar_namespaces(xml)
atributos.each do |variable|
instance_variable_set("@#{variable}", send("obtener_#{variable}", xml, ns))
end
end

def atributos
[ 'total',
'subtotal',
'descuento',
]
end

def obtener_total(xml,ns)
xml.xpath("//cfdi:Comprobante",ns).attribute('total').value rescue nil
end

def obtener_subtotal(xml,ns)
xml.xpath("//cfdi:Comprobante",ns).attribute('subTotal').value rescue nil
end

def obtener_descuento(xml,ns)
xml.xpath("//cfdi:Comprobante",ns).attribute('descuento').value rescue nil
end

def generar_namespaces(xml)
namespaces = xml.collect_namespaces
ns = {}
namespaces.each_pair do |key, value|
ns[key.sub(/^xmlns:/, '')] = value
end
ns
end

end
end

2 changes: 1 addition & 1 deletion lib/fm_timbrado_cfdi/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module FmTimbradoCfdi
VERSION = "0.0.4"
VERSION = "0.0.5"
end
17 changes: 17 additions & 0 deletions spec/fm_timbrado_cfdi/fm_informacion_cfdi_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# encoding: utf-8
require 'spec_helper'

describe FmTimbradoCfdi::FmInformacionCfdi do
context "debe crear un objeto válido" do
let(:plantilla){File.open('spec/fixtures/layout_example.txt').read}
let(:layout){ plantilla.gsub('--fecha-comprobante--', 'asignarFecha' )}
let(:respuesta){ FmTimbradoCfdi.timbra_cfdi_layout 'ESI920427886', layout }
let(:informacion) { FmTimbradoCfdi::FmInformacionCfdi.new(respuesta.xml)}
it { informacion.total.should_not be_nil }
it { informacion.total.should == "116.00"}
it { informacion.subtotal.should_not be_nil }
it { informacion.subtotal.should == "100.00"}
it { informacion.descuento.should_not be_nil }
it { informacion.descuento.should == "0.00" }
end
end

0 comments on commit 34b977d

Please sign in to comment.