|
| 1 | +/** |
| 2 | + * Copyright 2019 Project OpenUBL, Inc. and/or its affiliates |
| 3 | + * and other contributors as indicated by the @author tags. |
| 4 | + * |
| 5 | + * Licensed under the Eclipse Public License - v 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package io.github.project.openubl.xmlsenderws.webservices.managers.smart.custom; |
| 18 | + |
| 19 | +import io.github.project.openubl.xmlsenderws.webservices.catalogs.Catalogo1; |
| 20 | +import io.github.project.openubl.xmlsenderws.webservices.exceptions.InvalidXMLFileException; |
| 21 | +import io.github.project.openubl.xmlsenderws.webservices.exceptions.UnsupportedDocumentTypeException; |
| 22 | +import io.github.project.openubl.xmlsenderws.webservices.managers.BillServiceManager; |
| 23 | +import io.github.project.openubl.xmlsenderws.webservices.managers.smart.SmartBillServiceModel; |
| 24 | +import io.github.project.openubl.xmlsenderws.webservices.models.DeliveryURLType; |
| 25 | +import io.github.project.openubl.xmlsenderws.webservices.providers.BillServiceModel; |
| 26 | +import io.github.project.openubl.xmlsenderws.webservices.utils.UBLUtils; |
| 27 | +import io.github.project.openubl.xmlsenderws.webservices.wrappers.ServiceConfig; |
| 28 | +import io.github.project.openubl.xmlsenderws.webservices.xml.DocumentType; |
| 29 | +import io.github.project.openubl.xmlsenderws.webservices.xml.XmlContentModel; |
| 30 | +import io.github.project.openubl.xmlsenderws.webservices.xml.XmlContentProvider; |
| 31 | +import org.xml.sax.SAXException; |
| 32 | + |
| 33 | +import javax.xml.parsers.ParserConfigurationException; |
| 34 | +import java.io.ByteArrayInputStream; |
| 35 | +import java.io.File; |
| 36 | +import java.io.IOException; |
| 37 | +import java.nio.file.Files; |
| 38 | +import java.nio.file.Path; |
| 39 | +import java.util.Optional; |
| 40 | + |
| 41 | +public class CustomSmartBillServiceManager { |
| 42 | + |
| 43 | + private CustomSmartBillServiceManager() { |
| 44 | + // Just static methods |
| 45 | + } |
| 46 | + |
| 47 | + public static SmartBillServiceModel send(File file, String username, String password, CustomBillServiceConfig config) throws InvalidXMLFileException, UnsupportedDocumentTypeException, IOException { |
| 48 | + return send(file.toPath(), username, password, config); |
| 49 | + } |
| 50 | + |
| 51 | + public static SmartBillServiceModel send(Path path, String username, String password, CustomBillServiceConfig config) throws InvalidXMLFileException, UnsupportedDocumentTypeException, IOException { |
| 52 | + return send(Files.readAllBytes(path), username, password, config); |
| 53 | + } |
| 54 | + |
| 55 | + public static SmartBillServiceModel send(byte[] file, String username, String password, CustomBillServiceConfig config) throws InvalidXMLFileException, UnsupportedDocumentTypeException { |
| 56 | + XmlContentModel xmlContentModel; |
| 57 | + try { |
| 58 | + xmlContentModel = XmlContentProvider.getSunatDocument(new ByteArrayInputStream(file)); |
| 59 | + } catch (ParserConfigurationException | SAXException | IOException e) { |
| 60 | + throw new InvalidXMLFileException(e); |
| 61 | + } |
| 62 | + |
| 63 | + Optional<DocumentType> documentTypeOptional = DocumentType.valueFromDocumentType(xmlContentModel.getDocumentType()); |
| 64 | + if (!documentTypeOptional.isPresent()) { |
| 65 | + throw new UnsupportedDocumentTypeException(xmlContentModel.getDocumentType() + " is not supported yet"); |
| 66 | + } |
| 67 | + |
| 68 | + DocumentType documentType = documentTypeOptional.get(); |
| 69 | + |
| 70 | + String deliveryURL = getDeliveryURL(config, documentType, xmlContentModel); |
| 71 | + String fileNameWithoutExtension = UBLUtils.getFileNameWithoutExtension(documentType, xmlContentModel.getRuc(), xmlContentModel.getDocumentID()) |
| 72 | + .orElseThrow(() -> new IllegalStateException("Invalid type of UBL Document, can not extract Serie-Numero to create fileName")); |
| 73 | + |
| 74 | + ServiceConfig serviceConfig = new ServiceConfig.Builder() |
| 75 | + .url(deliveryURL) |
| 76 | + .username(username) |
| 77 | + .password(password) |
| 78 | + .build(); |
| 79 | + |
| 80 | + BillServiceModel billServiceModel; |
| 81 | + switch (documentType) { |
| 82 | + case INVOICE: |
| 83 | + case CREDIT_NOTE: |
| 84 | + case DEBIT_NOTE: |
| 85 | + case PERCEPTION: |
| 86 | + case RETENTION: |
| 87 | + case DESPATCH_ADVICE: |
| 88 | + billServiceModel = BillServiceManager.sendBill(fileNameWithoutExtension + ".xml", file, serviceConfig); |
| 89 | + break; |
| 90 | + case VOIDED_DOCUMENT: |
| 91 | + case SUMMARY_DOCUMENT: |
| 92 | + billServiceModel = BillServiceManager.sendSummary(fileNameWithoutExtension + ".xml", file, serviceConfig); |
| 93 | + break; |
| 94 | + default: |
| 95 | + throw new IllegalStateException("Could not determine the correct service for the document"); |
| 96 | + } |
| 97 | + |
| 98 | + return new SmartBillServiceModel(xmlContentModel, billServiceModel); |
| 99 | + } |
| 100 | + |
| 101 | + public static BillServiceModel getStatus(String ticket, XmlContentModel xmlContentModel, String username, String password, CustomBillServiceConfig config) { |
| 102 | + String deliveryURL = null; |
| 103 | + |
| 104 | + // Only for voided-document and not for summary-document |
| 105 | + Optional<Catalogo1> catalogo1 = Catalogo1.valueOfCode(xmlContentModel.getVoidedLineDocumentTypeCode()); |
| 106 | + if (catalogo1.isPresent()) { |
| 107 | + switch (catalogo1.get()) { |
| 108 | + case PERCEPCION: |
| 109 | + case RETENCION: |
| 110 | + deliveryURL = config.getPerceptionAndRetentionDeliveryURL(); |
| 111 | + break; |
| 112 | + // No se pueden dar bajas de guias de remision |
| 113 | +// case GUIA_REMISION_REMITENTE: |
| 114 | +// deliveryURL = config.getDespatchAdviceDeliveryURL(); |
| 115 | +// break; |
| 116 | + default: |
| 117 | + deliveryURL = config.getInvoiceAndNoteDeliveryURL(); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // If summary-document |
| 122 | + if (deliveryURL == null) { |
| 123 | + deliveryURL = config.getInvoiceAndNoteDeliveryURL(); |
| 124 | + } |
| 125 | + |
| 126 | + ServiceConfig serviceConfig = new ServiceConfig.Builder() |
| 127 | + .url(deliveryURL) |
| 128 | + .username(username) |
| 129 | + .password(password) |
| 130 | + .build(); |
| 131 | + |
| 132 | + return BillServiceManager.getStatus(ticket, serviceConfig); |
| 133 | + } |
| 134 | + |
| 135 | + public static String getDeliveryURL(CustomBillServiceConfig config, DocumentType documentType, XmlContentModel xmlContentModel) { |
| 136 | + DeliveryURLType deliveryURLType = UBLUtils.getDeliveryURLType(documentType, xmlContentModel) |
| 137 | + .orElseThrow(() -> new IllegalStateException("Invalid type of UBL Document, can not create Sunat Server URL")); |
| 138 | + |
| 139 | + switch (deliveryURLType) { |
| 140 | + case BASIC_DOCUMENTS_URL: |
| 141 | + return config.getInvoiceAndNoteDeliveryURL(); |
| 142 | + case DESPATCH_DOCUMENT_URL: |
| 143 | + return config.getDespatchAdviceDeliveryURL(); |
| 144 | + case PERCEPTION_RETENTION_URL: |
| 145 | + return config.getPerceptionAndRetentionDeliveryURL(); |
| 146 | + default: |
| 147 | + throw new IllegalStateException("DeliveryURLType not configured to return a value"); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments