Class: Facturama::Services::CfdiService

Inherits:
CrudService show all
Defined in:
lib/facturama/services/cfdi_service.rb

Instance Method Summary collapse

Methods inherited from CrudService

#update

Methods inherited from HttpService

#delete, #get, #post, #put

Constructor Details

#initialize(connection_info) ⇒ CfdiService

Returns a new instance of CfdiService.



9
10
11
# File 'lib/facturama/services/cfdi_service.rb', line 9

def initialize(connection_info)
  super(connection_info, '')
end

Instance Method Details

#create(model) ⇒ Object

CFDI 3.3



16
17
18
# File 'lib/facturama/services/cfdi_service.rb', line 16

def create(model)
  post(model, '2/cfdis')
end

#create3(model) ⇒ Object

CFDI 4.0



21
22
23
# File 'lib/facturama/services/cfdi_service.rb', line 21

def create3(model)
  post(model, '3/cfdis')
end

#get_file(id, format, type) ⇒ Object

Obtiene un archivo referente a un CFDI del tipo “Issued”

Parameters:

  • id

    Identificador del CFDI

  • format

    Formato deseado ( pdf | html | xml )

  • type

    Tipo de comprobante ( payroll | received | issued )

Returns:

  • Archivo en cuestion



51
52
53
54
# File 'lib/facturama/services/cfdi_service.rb', line 51

def get_file(id, format, type)
  resource = "api/cfdi/#{format}/#{type}/#{id}"
  get(resource)
end

#list(folio_start = -1,, folio_end = -1,, rfc = nil, tax_entity_name = nil, date_start = nil, date_end = nil, id_branch = nil, serie = nil, status = Facturama::CfdiStatus::ACTIVE, type = Facturama::InvoiceType::ISSUED) ⇒ Object

Listado con todas las opciones posibles



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 'lib/facturama/services/cfdi_service.rb', line 100

def list(folio_start = -1, folio_end = -1,
         rfc = nil,  tax_entity_name = nil,
         date_start = nil, date_end = nil,
         id_branch = nil,  serie = nil,
         status = Facturama::CfdiStatus::ACTIVE,
         type = Facturama::InvoiceType::ISSUED)

  resource = "cfdi?type=#{type}&status=#{status}"

  resource += "&folioStart=#{folio_start}" if folio_start > -1

  resource += "&folioEnd=#{folio_end}" if folio_end > -1

  resource += "&rfc=#{rfc}" unless rfc.nil?

  resource += "&taxEntityName=#{tax_entity_name}" unless tax_entity_name.nil?

  resource += "&dateStart=#{date_start}" unless date_start.nil?

  resource += "&dateEnd=#{date_end}" unless date_end.nil?

  resource += "&idBranch=#{id_branch}" unless id_branch.nil?

  resource += "&serie=#{serie}" unless serie.nil?

  get(resource)
end

#list_by_keyword(keyword, status = Facturama::CfdiStatus::ACTIVE, type = Facturama::InvoiceType::ISSUED) ⇒ Object

Listado de Cfdi filtrando por palabra clave



82
83
84
85
86
87
88
# File 'lib/facturama/services/cfdi_service.rb', line 82

def list_by_keyword(keyword,
                    status = Facturama::CfdiStatus::ACTIVE,
                    type = Facturama::InvoiceType::ISSUED)

  resource = "cfdi?type=#{type}&status=#{status}&keyword=#{keyword}"
  get(resource)
end

#list_by_rfc(rfc, status = Facturama::CfdiStatus::ACTIVE, type = Facturama::InvoiceType::ISSUED) ⇒ Object

Listado de Cfdi filtrando por palabra RFC



91
92
93
94
95
96
97
# File 'lib/facturama/services/cfdi_service.rb', line 91

def list_by_rfc(rfc,
                status = Facturama::CfdiStatus::ACTIVE,
                type = Facturama::InvoiceType::ISSUED)

  resource = "cfdi?type=#{type}&status=#{status}&rfc=#{rfc}"
  get(resource)
end

#remove(id, type, motive, uuidReplacement) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/facturama/services/cfdi_service.rb', line 25

def remove(id, type, motive, uuidReplacement)
  motive = '02' if motive.nil? && motive != ''
  uuidReplacement = '' if uuidReplacement.nil? && uuidReplacement != ''

  raise(FacturamaException('El Id del cfdi a eliminar es obligatorio')) unless !id.nil? && id != ''

  delete("cfdi/#{id}?type=#{type}&motive=#{motive}&uuidReplacement=#{uuidReplacement}")
end

#retrieve(id, type = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/facturama/services/cfdi_service.rb', line 34

def retrieve(id, type = nil)
  str_type = if type.nil?
               'Issued'
             else
               type
             end

  get("cfdi/#{id}?type=#{str_type}")
end

#save_file(file_path, file_content_base64) ⇒ Object

Decodifica y guarda un archivo base64 en una ruta



57
58
59
60
61
62
# File 'lib/facturama/services/cfdi_service.rb', line 57

def save_file(file_path, file_content_base64)
  file_content_decoded = Base64.decode64(file_content_base64)
  File.open(file_path, 'wb') do |f|
    f.write(file_content_decoded)
  end
end

#save_html(file_path, id, type = Facturama::InvoiceType::ISSUED) ⇒ Object



74
75
76
77
# File 'lib/facturama/services/cfdi_service.rb', line 74

def save_html(file_path, id, type = Facturama::InvoiceType::ISSUED)
  file_content = get_file(id, Facturama::FileFormat::HTML, type)
  save_file(file_path, file_content['Content'])
end

#save_pdf(file_path, id, type = Facturama::InvoiceType::ISSUED) ⇒ Object



64
65
66
67
# File 'lib/facturama/services/cfdi_service.rb', line 64

def save_pdf(file_path, id, type = Facturama::InvoiceType::ISSUED)
  file_content = get_file(id, Facturama::FileFormat::PDF, type)
  save_file(file_path, file_content['Content'])
end

#save_xml(file_path, id, type = Facturama::InvoiceType::ISSUED) ⇒ Object



69
70
71
72
# File 'lib/facturama/services/cfdi_service.rb', line 69

def save_xml(file_path, id, type = Facturama::InvoiceType::ISSUED)
  file_content = get_file(id, Facturama::FileFormat::XML, type)
  save_file(file_path, file_content['Content'])
end

#send_by_mail(id, email, subject, type = Facturama::InvoiceType::ISSUED) ⇒ Object

Envía el CFDI por correo, con el asunto especificado



131
132
133
134
# File 'lib/facturama/services/cfdi_service.rb', line 131

def send_by_mail(id, email, subject, type = Facturama::InvoiceType::ISSUED)
  response = post(nil, "cfdi?cfdiType=#{type}&cfdiId=#{id}&email=#{email}&subject=#{subject}")
  !!response['success']
end