Class: Facturama::Services::CfdiMultiService

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

Instance Method Summary collapse

Methods inherited from CrudService

#update

Methods inherited from HttpService

#delete, #get, #post, #put

Constructor Details

#initialize(connection_info) ⇒ CfdiMultiService

Returns a new instance of CfdiMultiService.



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

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

Instance Method Details

#create(model) ⇒ Object



17
18
19
# File 'lib/facturama/services/cfdi_multi_service.rb', line 17

def create(model)
    post(model, "api-lite/2/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



46
47
48
49
# File 'lib/facturama/services/cfdi_multi_service.rb', line 46

def get_file(id, format, type)
    resource = "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



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/facturama/services/cfdi_multi_service.rb', line 104

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 = "api-lite/cfdis?type=" +  type + "&status=" + status

    if folio_start > -1
        resource += "&folioStart=" + folio_start
    end

    if folio_end > -1
        resource += "&folioEnd=" + folio_end
    end

    if rfc != nil
        resource += "&rfc=" + rfc
    end

    if tax_entity_name != nil
        resource += "&taxEntityName=" + tax_entity_name
    end

    if date_start != nil
        resource += "&dateStart=" + date_start
    end

    if date_end != nil
        resource += "&dateEnd=" + date_end
    end

    if id_branch != nil
        resource += "&idBranch=" + id_branch
    end

    if serie != nil
        resource += "&serie=" + serie
    end


    get(resource)
end

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

Listado de Cfdi filtrando por palabra clave



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

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

    resource = "api-lite/cfdis?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



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

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

    resource = "api-lite/cfdis?status=" + status + "&rfc=" + rfc
    get(resource)
end

#remove(id) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/facturama/services/cfdi_multi_service.rb', line 22

def remove(id)

    if !id.nil? && id != ""
        delete("api-lite/cfdis/" + id )
    else
        raise( FacturamaException("El Id del cfdi a eliminar es obligatorio") )
    end
end

#retrieve(id) ⇒ Object



32
33
34
# File 'lib/facturama/services/cfdi_multi_service.rb', line 32

def retrieve(id)
    get("api-lite/cfdis/" + id )
end

#save_file(file_path, file_content_base64) ⇒ Object

Decodifica y guarda un archivo base64 en una ruta



53
54
55
56
57
58
# File 'lib/facturama/services/cfdi_multi_service.rb', line 53

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_LITE) ⇒ Object



71
72
73
74
# File 'lib/facturama/services/cfdi_multi_service.rb', line 71

def save_html(file_path, id, type = Facturama::InvoiceType::ISSUED_LITE)
    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_LITE) ⇒ Object



61
62
63
64
# File 'lib/facturama/services/cfdi_multi_service.rb', line 61

def save_pdf(file_path, id, type = Facturama::InvoiceType::ISSUED_LITE)
    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_LITE) ⇒ Object



66
67
68
69
# File 'lib/facturama/services/cfdi_multi_service.rb', line 66

def save_xml(file_path, id, type = Facturama::InvoiceType::ISSUED_LITE)
    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_LITE) ⇒ Object

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



154
155
156
157
# File 'lib/facturama/services/cfdi_multi_service.rb', line 154

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