|
1 | 1 | from .pdf import Pdf |
2 | 2 | from .service import Service |
3 | | -from pathlib import Path |
| 3 | +from .file import File |
| 4 | +from .addenda import Addenda |
4 | 5 |
|
5 | | -class Cfdi: |
| 6 | +class Cfdi(File): |
6 | 7 | def __init__(self): |
| 8 | + super().__init__() |
7 | 9 | self.pdf = None |
8 | 10 | self.addenda = None |
9 | | - self.file_path = None |
10 | | - self.file_content = None |
| 11 | + self.addenda_replace_values = None |
11 | 12 | self.service = Service.get_instance() |
12 | 13 |
|
13 | | - def from_file(self, file_path): |
14 | | - self.file_path = file_path |
15 | | - return self |
| 14 | + def set_addenda(self, addenda, replace_values=None): |
| 15 | + if addenda and not isinstance(addenda, Addenda): |
| 16 | + raise TypeError('addenda must be an instance of Addenda.') |
| 17 | + if replace_values and not isinstance(replace_values, dict): |
| 18 | + raise TypeError('Addenda replace values must be a valid key-value object.') |
16 | 19 |
|
17 | | - def from_string(self, file_content): |
18 | | - self.file_content = file_content |
19 | | - return self |
| 20 | + self.addenda = addenda |
| 21 | + self.addenda_replace_values = replace_values |
20 | 22 |
|
21 | 23 | def to_pdf(self, payload={}): |
22 | 24 | if self.pdf: |
23 | 25 | return self.pdf |
| 26 | + |
| 27 | + if not isinstance(payload, dict): |
| 28 | + raise TypeError('toPdf function only accepts an object as a parameter.') |
24 | 29 |
|
25 | | - file_content = self._get_xml_content() |
26 | | - payload['format'] = 'pdf' |
| 30 | + file = self.get_file() |
27 | 31 |
|
28 | 32 | if self.addenda: |
29 | | - payload['addenda'] = self.addenda |
| 33 | + addenda_content = self.addenda.get_file_content(self.addenda_replace_values) |
| 34 | + payload['addenda'] = addenda_content |
30 | 35 |
|
31 | | - result = self.service.cfdis_convert(file_content, payload) |
| 36 | + payload['format'] = 'pdf' |
| 37 | + |
| 38 | + result = self.service.cfdis_convert(file['content'], payload) |
32 | 39 | self.pdf = Pdf(result) |
33 | 40 |
|
34 | 41 | return self.pdf |
35 | 42 |
|
36 | | - def _get_xml_content(self): |
37 | | - if self.file_content: |
38 | | - return self.file_content |
39 | | - elif self.file_path: |
40 | | - path = Path(self.file_path) |
41 | | - if not path.exists() or not path.is_file(): |
42 | | - raise FileNotFoundError(f"Failed to read XML content from file: {self.file_path}. The file does not exist.") |
43 | | - return path.read_text() |
44 | | - else: |
45 | | - raise ValueError("XML content for the CFDI must be provided.") |
46 | | - |
47 | | - def set_addenda(self, addenda): |
48 | | - if not isinstance(addenda, str): |
49 | | - raise TypeError("setAddenda function only accepts a string parameter.") |
50 | | - self.addenda = addenda |
0 commit comments