Skip to content

Commit 8ae5b04

Browse files
Add custom bill service (#22)
1 parent 84fa570 commit 8ae5b04

File tree

3 files changed

+207
-97
lines changed

3 files changed

+207
-97
lines changed

src/main/java/io/github/project/openubl/xmlsenderws/webservices/managers/smart/SmartBillServiceManager.java

Lines changed: 29 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,17 @@
1616
*/
1717
package io.github.project.openubl.xmlsenderws.webservices.managers.smart;
1818

19-
import io.github.project.openubl.xmlsenderws.webservices.catalogs.Catalogo1;
2019
import io.github.project.openubl.xmlsenderws.webservices.exceptions.InvalidXMLFileException;
2120
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.models.DeliveryURLType;
21+
import io.github.project.openubl.xmlsenderws.webservices.managers.smart.custom.CustomBillServiceConfig;
22+
import io.github.project.openubl.xmlsenderws.webservices.managers.smart.custom.CustomSmartBillServiceManager;
2423
import io.github.project.openubl.xmlsenderws.webservices.providers.BillServiceModel;
25-
import io.github.project.openubl.xmlsenderws.webservices.utils.UBLUtils;
26-
import io.github.project.openubl.xmlsenderws.webservices.wrappers.ServiceConfig;
27-
import io.github.project.openubl.xmlsenderws.webservices.xml.DocumentType;
2824
import io.github.project.openubl.xmlsenderws.webservices.xml.XmlContentModel;
29-
import io.github.project.openubl.xmlsenderws.webservices.xml.XmlContentProvider;
30-
import org.xml.sax.SAXException;
3125

32-
import javax.xml.parsers.ParserConfigurationException;
33-
import java.io.ByteArrayInputStream;
3426
import java.io.File;
3527
import java.io.IOException;
3628
import java.nio.file.Files;
3729
import java.nio.file.Path;
38-
import java.util.Optional;
3930

4031
public class SmartBillServiceManager {
4132

@@ -52,104 +43,45 @@ public static SmartBillServiceModel send(Path path, String username, String pass
5243
}
5344

5445
public static SmartBillServiceModel send(byte[] file, String username, String password) throws InvalidXMLFileException, UnsupportedDocumentTypeException {
55-
XmlContentModel xmlContentModel;
56-
try {
57-
xmlContentModel = XmlContentProvider.getSunatDocument(new ByteArrayInputStream(file));
58-
} catch (ParserConfigurationException | SAXException | IOException e) {
59-
throw new InvalidXMLFileException(e);
60-
}
61-
62-
Optional<DocumentType> documentTypeOptional = DocumentType.valueFromDocumentType(xmlContentModel.getDocumentType());
63-
if (!documentTypeOptional.isPresent()) {
64-
throw new UnsupportedDocumentTypeException(xmlContentModel.getDocumentType() + " is not supported yet");
65-
}
66-
67-
DocumentType documentType = documentTypeOptional.get();
68-
69-
String deliveryURL = getDeliveryURL(documentType, xmlContentModel);
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"));
46+
SmartBillServiceConfig config = SmartBillServiceConfig.getInstance();
7247

73-
ServiceConfig config = new ServiceConfig.Builder()
74-
.url(deliveryURL)
75-
.username(username)
76-
.password(password)
77-
.build();
48+
return CustomSmartBillServiceManager.send(file, username, password, new CustomBillServiceConfig() {
49+
@Override
50+
public String getInvoiceAndNoteDeliveryURL() {
51+
return config.getInvoiceAndNoteDeliveryURL();
52+
}
7853

79-
BillServiceModel billServiceModel;
80-
switch (documentType) {
81-
case INVOICE:
82-
case CREDIT_NOTE:
83-
case DEBIT_NOTE:
84-
case PERCEPTION:
85-
case RETENTION:
86-
case DESPATCH_ADVICE:
87-
billServiceModel = BillServiceManager.sendBill(fileNameWithoutExtension + ".xml", file, config);
88-
break;
89-
case VOIDED_DOCUMENT:
90-
case SUMMARY_DOCUMENT:
91-
billServiceModel = BillServiceManager.sendSummary(fileNameWithoutExtension + ".xml", file, config);
92-
break;
93-
default:
94-
throw new IllegalStateException("Could not determine the correct service for the document");
95-
}
54+
@Override
55+
public String getPerceptionAndRetentionDeliveryURL() {
56+
return config.getPerceptionAndRetentionDeliveryURL();
57+
}
9658

97-
return new SmartBillServiceModel(xmlContentModel, billServiceModel);
59+
@Override
60+
public String getDespatchAdviceDeliveryURL() {
61+
return config.getDespatchAdviceDeliveryURL();
62+
}
63+
});
9864
}
9965

10066
public static BillServiceModel getStatus(String ticket, XmlContentModel xmlContentModel, String username, String password) {
10167
SmartBillServiceConfig config = SmartBillServiceConfig.getInstance();
10268

103-
String deliveryURL = null;
104-
105-
// Only for voided-document and not for summary-document
106-
Optional<Catalogo1> catalogo1 = Catalogo1.valueOfCode(xmlContentModel.getVoidedLineDocumentTypeCode());
107-
if (catalogo1.isPresent()) {
108-
switch (catalogo1.get()) {
109-
case PERCEPCION:
110-
case RETENCION:
111-
deliveryURL = config.getPerceptionAndRetentionDeliveryURL();
112-
break;
113-
// No se pueden dar bajas de guias de remision
114-
// case GUIA_REMISION_REMITENTE:
115-
// deliveryURL = config.getDespatchAdviceDeliveryURL();
116-
// break;
117-
default:
118-
deliveryURL = config.getInvoiceAndNoteDeliveryURL();
69+
return CustomSmartBillServiceManager.getStatus(ticket, xmlContentModel, username, password, new CustomBillServiceConfig() {
70+
@Override
71+
public String getInvoiceAndNoteDeliveryURL() {
72+
return config.getInvoiceAndNoteDeliveryURL();
11973
}
120-
}
12174

122-
// If summary-document
123-
if (deliveryURL == null) {
124-
deliveryURL = config.getInvoiceAndNoteDeliveryURL();
125-
}
126-
127-
ServiceConfig serviceConfig = new ServiceConfig.Builder()
128-
.url(deliveryURL)
129-
.username(username)
130-
.password(password)
131-
.build();
132-
133-
return BillServiceManager.getStatus(ticket, serviceConfig);
134-
}
135-
136-
private static String getDeliveryURL(DocumentType documentType, XmlContentModel xmlContentModel) {
137-
SmartBillServiceConfig config = SmartBillServiceConfig.getInstance();
138-
139-
DeliveryURLType deliveryURLType = UBLUtils.getDeliveryURLType(documentType, xmlContentModel)
140-
.orElseThrow(() -> new IllegalStateException("Invalid type of UBL Document, can not create Sunat Server URL"));
75+
@Override
76+
public String getPerceptionAndRetentionDeliveryURL() {
77+
return config.getPerceptionAndRetentionDeliveryURL();
78+
}
14179

142-
switch (deliveryURLType) {
143-
case BASIC_DOCUMENTS_URL:
144-
return config.getInvoiceAndNoteDeliveryURL();
145-
case DESPATCH_DOCUMENT_URL:
80+
@Override
81+
public String getDespatchAdviceDeliveryURL() {
14682
return config.getDespatchAdviceDeliveryURL();
147-
case PERCEPTION_RETENTION_URL:
148-
return config.getPerceptionAndRetentionDeliveryURL();
149-
default:
150-
throw new IllegalStateException("DeliveryURLType not configured to return a value");
151-
}
83+
}
84+
});
15285
}
15386

154-
15587
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
public interface CustomBillServiceConfig {
20+
21+
String getInvoiceAndNoteDeliveryURL();
22+
23+
String getPerceptionAndRetentionDeliveryURL();
24+
25+
String getDespatchAdviceDeliveryURL();
26+
27+
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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

Comments
 (0)