|
20 | 20 | import io.github.project.openubl.xmlsenderws.webservices.exceptions.InvalidXMLFileException; |
21 | 21 | import io.github.project.openubl.xmlsenderws.webservices.exceptions.UnsupportedDocumentTypeException; |
22 | 22 | import io.github.project.openubl.xmlsenderws.webservices.managers.BillServiceManager; |
| 23 | +import io.github.project.openubl.xmlsenderws.webservices.models.DeliveryURLType; |
23 | 24 | import io.github.project.openubl.xmlsenderws.webservices.providers.BillServiceModel; |
| 25 | +import io.github.project.openubl.xmlsenderws.webservices.utils.UBLUtils; |
24 | 26 | import io.github.project.openubl.xmlsenderws.webservices.wrappers.ServiceConfig; |
25 | 27 | import io.github.project.openubl.xmlsenderws.webservices.xml.DocumentType; |
26 | 28 | import io.github.project.openubl.xmlsenderws.webservices.xml.XmlContentModel; |
|
33 | 35 | import java.io.IOException; |
34 | 36 | import java.nio.file.Files; |
35 | 37 | import java.nio.file.Path; |
36 | | -import java.text.MessageFormat; |
37 | 38 | import java.util.Optional; |
38 | | -import java.util.regex.Pattern; |
39 | 39 |
|
40 | 40 | public class SmartBillServiceManager { |
41 | 41 |
|
42 | | - private final static String FILENAME_FORMAT1 = "{0}-{1}-{2}"; |
43 | | - private final static String FILENAME_FORMAT2 = "{0}-{1}"; |
44 | | - |
45 | 42 | private SmartBillServiceManager() { |
46 | 43 | // Just static methods |
47 | 44 | } |
@@ -70,7 +67,8 @@ public static SmartBillServiceModel send(byte[] file, String username, String pa |
70 | 67 | DocumentType documentType = documentTypeOptional.get(); |
71 | 68 |
|
72 | 69 | String deliveryURL = getDeliveryURL(documentType, xmlContentModel); |
73 | | - String fileNameWithoutExtension = getFileNameWithoutExtension(documentType, xmlContentModel.getRuc(), xmlContentModel.getDocumentID()); |
| 70 | + String fileNameWithoutExtension = UBLUtils.getFileNameWithoutExtension(documentType, xmlContentModel.getRuc(), xmlContentModel.getDocumentID()) |
| 71 | + .orElseThrow(() -> new IllegalStateException("Invalid type of UBL Document, can not extract Serie-Numero to create fileName")); |
74 | 72 |
|
75 | 73 | ServiceConfig config = new ServiceConfig.Builder() |
76 | 74 | .url(deliveryURL) |
@@ -137,64 +135,21 @@ public static BillServiceModel getStatus(String ticket, XmlContentModel xmlConte |
137 | 135 |
|
138 | 136 | private static String getDeliveryURL(DocumentType documentType, XmlContentModel xmlContentModel) { |
139 | 137 | SmartBillServiceConfig config = SmartBillServiceConfig.getInstance(); |
140 | | - switch (documentType) { |
141 | | - case INVOICE: |
142 | | - case CREDIT_NOTE: |
143 | | - case DEBIT_NOTE: |
144 | | - case SUMMARY_DOCUMENT: |
145 | | - return config.getInvoiceAndNoteDeliveryURL(); |
146 | | - case VOIDED_DOCUMENT: |
147 | | - String tipoDocumentoAfectado = xmlContentModel.getVoidedLineDocumentTypeCode(); |
148 | | - Catalogo1 catalog1 = Catalogo1.valueOfCode(tipoDocumentoAfectado).orElseThrow(() -> new IllegalStateException("No se pudo convertir el valor del catálogo")); |
149 | | - if (catalog1.equals(Catalogo1.PERCEPCION) || catalog1.equals(Catalogo1.RETENCION)) { |
150 | | - return config.getPerceptionAndRetentionDeliveryURL(); |
151 | | - } else if (catalog1.equals(Catalogo1.GUIA_REMISION_REMITENTE)) { |
152 | | - return config.getDespatchAdviceDeliveryURL(); |
153 | | - } |
| 138 | + |
| 139 | + DeliveryURLType deliveryURLType = UBLUtils.getDeliveryURLType(documentType, xmlContentModel) |
| 140 | + .orElseThrow(() -> new IllegalStateException("Invalid type of UBL Document, can not create Sunat Server URL")); |
| 141 | + |
| 142 | + switch (deliveryURLType) { |
| 143 | + case BASIC_DOCUMENTS_URL: |
154 | 144 | return config.getInvoiceAndNoteDeliveryURL(); |
155 | | - case PERCEPTION: |
156 | | - case RETENTION: |
157 | | - return config.getPerceptionAndRetentionDeliveryURL(); |
158 | | - case DESPATCH_ADVICE: |
| 145 | + case DESPATCH_DOCUMENT_URL: |
159 | 146 | return config.getDespatchAdviceDeliveryURL(); |
| 147 | + case PERCEPTION_RETENTION_URL: |
| 148 | + return config.getPerceptionAndRetentionDeliveryURL(); |
160 | 149 | default: |
161 | | - throw new IllegalStateException("Invalid type of UBL Document, can not create Sunat Server URL"); |
| 150 | + throw new IllegalStateException("DeliveryURLType not configured to return a value"); |
162 | 151 | } |
163 | 152 | } |
164 | 153 |
|
165 | | - private static String getFileNameWithoutExtension(DocumentType type, String ruc, String documentID) { |
166 | | - String codigoDocumento; |
167 | | - switch (type) { |
168 | | - case INVOICE: |
169 | | - if (Pattern.compile("^[F|f].*$").matcher(documentID).find()) { |
170 | | - codigoDocumento = Catalogo1.FACTURA.getCode(); |
171 | | - } else if (Pattern.compile("^[B|b].*$").matcher(documentID).find()) { |
172 | | - codigoDocumento = Catalogo1.BOLETA.getCode(); |
173 | | - } else { |
174 | | - throw new IllegalStateException("Invalid Serie, can not detect code"); |
175 | | - } |
176 | | - |
177 | | - return MessageFormat.format(FILENAME_FORMAT1, ruc, codigoDocumento, documentID); |
178 | | - case CREDIT_NOTE: |
179 | | - codigoDocumento = Catalogo1.NOTA_CREDITO.getCode(); |
180 | | - return MessageFormat.format(FILENAME_FORMAT1, ruc, codigoDocumento, documentID); |
181 | | - case DEBIT_NOTE: |
182 | | - codigoDocumento = Catalogo1.NOTA_DEBITO.getCode(); |
183 | | - return MessageFormat.format(FILENAME_FORMAT1, ruc, codigoDocumento, documentID); |
184 | | - case VOIDED_DOCUMENT: |
185 | | - case SUMMARY_DOCUMENT: |
186 | | - return MessageFormat.format(FILENAME_FORMAT2, ruc, documentID); |
187 | | - case PERCEPTION: |
188 | | - codigoDocumento = Catalogo1.PERCEPCION.getCode(); |
189 | | - return MessageFormat.format(FILENAME_FORMAT1, ruc, codigoDocumento, documentID); |
190 | | - case RETENTION: |
191 | | - codigoDocumento = Catalogo1.RETENCION.getCode(); |
192 | | - return MessageFormat.format(FILENAME_FORMAT1, ruc, codigoDocumento, documentID); |
193 | | - case DESPATCH_ADVICE: |
194 | | - codigoDocumento = Catalogo1.GUIA_REMISION_REMITENTE.getCode(); |
195 | | - return MessageFormat.format(FILENAME_FORMAT1, ruc, codigoDocumento, documentID); |
196 | | - default: |
197 | | - throw new IllegalStateException("Invalid type of UBL Document, can not extract Serie-Numero to create fileName"); |
198 | | - } |
199 | | - } |
| 154 | + |
200 | 155 | } |
0 commit comments