18 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
182 |
ilm |
4 |
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
|
18 |
ilm |
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.erp.core.sales.invoice.report;
|
|
|
15 |
|
|
|
16 |
import org.openconcerto.erp.config.ComptaPropsConfiguration;
|
83 |
ilm |
17 |
import org.openconcerto.erp.core.common.element.BanqueSQLElement;
|
182 |
ilm |
18 |
import org.openconcerto.erp.core.sales.quote.report.PaypalStamper;
|
|
|
19 |
import org.openconcerto.erp.core.supplychain.purchase.importer.FacturXExporter;
|
25 |
ilm |
20 |
import org.openconcerto.erp.generationDoc.AbstractSheetXMLWithDate;
|
182 |
ilm |
21 |
import org.openconcerto.erp.generationDoc.PDFAttachment;
|
|
|
22 |
import org.openconcerto.erp.generationDoc.SheetUtils;
|
|
|
23 |
import org.openconcerto.erp.generationDoc.gestcomm.OptionDocProcessor;
|
|
|
24 |
import org.openconcerto.erp.preferences.PayPalPreferencePanel;
|
18 |
ilm |
25 |
import org.openconcerto.erp.preferences.PrinterNXProps;
|
|
|
26 |
import org.openconcerto.sql.Configuration;
|
|
|
27 |
import org.openconcerto.sql.model.SQLRow;
|
182 |
ilm |
28 |
import org.openconcerto.sql.preferences.SQLPreferences;
|
|
|
29 |
import org.openconcerto.utils.ExceptionHandler;
|
18 |
ilm |
30 |
|
182 |
ilm |
31 |
import java.io.File;
|
|
|
32 |
import java.io.InputStream;
|
|
|
33 |
import java.io.InputStreamReader;
|
|
|
34 |
import java.io.OutputStream;
|
|
|
35 |
import java.net.HttpURLConnection;
|
|
|
36 |
import java.net.URL;
|
|
|
37 |
import java.net.URLConnection;
|
|
|
38 |
import java.net.URLEncoder;
|
|
|
39 |
import java.nio.charset.StandardCharsets;
|
|
|
40 |
import java.util.ArrayList;
|
|
|
41 |
import java.util.HashMap;
|
|
|
42 |
import java.util.List;
|
|
|
43 |
import java.util.Map;
|
|
|
44 |
import java.util.StringJoiner;
|
|
|
45 |
|
|
|
46 |
import org.jopendocument.model.OpenDocument;
|
|
|
47 |
|
25 |
ilm |
48 |
public class VenteFactureXmlSheet extends AbstractSheetXMLWithDate {
|
18 |
ilm |
49 |
|
25 |
ilm |
50 |
public static final String TEMPLATE_ID = "VenteFacture";
|
180 |
ilm |
51 |
public static final String TEMPLATE_SITUATION_SUFFIX = "Situation";
|
25 |
ilm |
52 |
public static final String TEMPLATE_PROPERTY_NAME = "LocationFacture";
|
18 |
ilm |
53 |
|
19 |
ilm |
54 |
@Override
|
|
|
55 |
public String getReference() {
|
182 |
ilm |
56 |
return this.getSQLRow().getString("NOM");
|
|
|
57 |
|
19 |
ilm |
58 |
}
|
|
|
59 |
|
|
|
60 |
@Override
|
25 |
ilm |
61 |
public String getName() {
|
|
|
62 |
final String startName;
|
182 |
ilm |
63 |
if (this.getSQLRow().getBoolean("COMPLEMENT")) {
|
25 |
ilm |
64 |
startName = "FactureComplement_";
|
182 |
ilm |
65 |
} else if (this.getSQLRow().getBoolean("ACOMPTE")) {
|
25 |
ilm |
66 |
startName = "FactureAcompte_";
|
|
|
67 |
} else {
|
|
|
68 |
startName = "Facture_";
|
|
|
69 |
}
|
182 |
ilm |
70 |
return startName + this.getSQLRow().getString("NUMERO");
|
25 |
ilm |
71 |
}
|
|
|
72 |
|
|
|
73 |
@Override
|
19 |
ilm |
74 |
public SQLRow getRowLanguage() {
|
182 |
ilm |
75 |
SQLRow rowClient = this.getSQLRow().getForeignRow("ID_CLIENT");
|
180 |
ilm |
76 |
|
19 |
ilm |
77 |
if (rowClient.getTable().contains("ID_LANGUE")) {
|
180 |
ilm |
78 |
if (!rowClient.isForeignEmpty("ID_LANGUE")) {
|
|
|
79 |
return rowClient.getForeign("ID_LANGUE");
|
|
|
80 |
} else {
|
|
|
81 |
return null;
|
|
|
82 |
}
|
19 |
ilm |
83 |
} else {
|
|
|
84 |
return super.getRowLanguage();
|
|
|
85 |
}
|
|
|
86 |
}
|
|
|
87 |
|
18 |
ilm |
88 |
public VenteFactureXmlSheet(SQLRow row) {
|
|
|
89 |
super(row);
|
182 |
ilm |
90 |
this.elt = Configuration.getInstance().getDirectory().getElement("SAISIE_VENTE_FACTURE");
|
18 |
ilm |
91 |
this.printer = PrinterNXProps.getInstance().getStringProperty("FacturePrinter");
|
25 |
ilm |
92 |
getDefaultTemplateId();
|
182 |
ilm |
93 |
this.setPostProcess(new OptionDocProcessor());
|
21 |
ilm |
94 |
}
|
|
|
95 |
|
|
|
96 |
@Override
|
25 |
ilm |
97 |
public String getType() {
|
|
|
98 |
String type;
|
182 |
ilm |
99 |
SQLRow row = this.getSQLRow();
|
18 |
ilm |
100 |
if (row.getBoolean("COMPLEMENT")) {
|
25 |
ilm |
101 |
type = "Complement";
|
|
|
102 |
} else if (row.getBoolean("ACOMPTE")) {
|
|
|
103 |
type = "Acompte";
|
80 |
ilm |
104 |
} else if (row.getBoolean("PARTIAL") || row.getBoolean("SOLDE")) {
|
180 |
ilm |
105 |
type = TEMPLATE_SITUATION_SUFFIX;
|
18 |
ilm |
106 |
} else {
|
28 |
ilm |
107 |
type = null;
|
25 |
ilm |
108 |
|
18 |
ilm |
109 |
}
|
21 |
ilm |
110 |
|
25 |
ilm |
111 |
return type;
|
|
|
112 |
}
|
21 |
ilm |
113 |
|
25 |
ilm |
114 |
@Override
|
|
|
115 |
public String getDefaultTemplateId() {
|
|
|
116 |
return TEMPLATE_ID;
|
18 |
ilm |
117 |
}
|
|
|
118 |
|
182 |
ilm |
119 |
@Override
|
|
|
120 |
public void createPDF(final File generatedFile, final File pdfFile, final OpenDocument doc, String storagePath) {
|
|
|
121 |
if (pdfFile == null) {
|
|
|
122 |
throw new IllegalArgumentException("null PDF file");
|
|
|
123 |
}
|
|
|
124 |
try {
|
|
|
125 |
final List<PDFAttachment> attachments = new ArrayList<>(1);
|
|
|
126 |
final FacturXExporter ex = new FacturXExporter();
|
|
|
127 |
final SQLRow rowSociete = ComptaPropsConfiguration.getInstanceCompta().getRowSociete();
|
|
|
128 |
final String xml = ex.createXMLFrom(getSQLRow().getTable().getDBRoot(), getSQLRow().getID(), rowSociete);
|
|
|
129 |
attachments.add(new PDFAttachment("factur-x.xml", "factur-x.xml", xml.getBytes(StandardCharsets.UTF_8), "text/xml"));
|
|
|
130 |
|
|
|
131 |
final SQLPreferences prefs = SQLPreferences.getMemCached(getElement().getTable().getDBRoot());
|
|
|
132 |
if (prefs.getBoolean(PayPalPreferencePanel.PAYPAL_INVOICE, false)) {
|
|
|
133 |
try {
|
|
|
134 |
final File inFile = File.createTempFile("oc_", pdfFile.getName());
|
|
|
135 |
SheetUtils.convert2PDF(doc, inFile, attachments);
|
|
|
136 |
PaypalStamper s = new PaypalStamper();
|
|
|
137 |
int x = prefs.getInt(PayPalPreferencePanel.PAYPAL_INVOICE_X, 0);
|
|
|
138 |
int y = prefs.getInt(PayPalPreferencePanel.PAYPAL_INVOICE_Y, 0);
|
|
|
139 |
|
|
|
140 |
// Reference
|
|
|
141 |
String ref = getSQLRow().getString("NUMERO");
|
|
|
142 |
// Montant : ex : 10.55
|
|
|
143 |
long cents = getSQLRow().getLong("NET_A_PAYER");
|
|
|
144 |
String amount = cents / 100 + "." + cents % 100;
|
|
|
145 |
// Devise
|
|
|
146 |
// TODO : autres devises
|
|
|
147 |
String currency = "EUR";
|
|
|
148 |
// POST
|
|
|
149 |
final URL url = new URL("https://cloud.openconcerto.org/payment");
|
|
|
150 |
final URLConnection con = url.openConnection();
|
|
|
151 |
final HttpURLConnection http = (HttpURLConnection) con;
|
|
|
152 |
http.setRequestMethod("POST");
|
|
|
153 |
http.setDoOutput(true);
|
|
|
154 |
http.setDefaultUseCaches(false);
|
|
|
155 |
|
|
|
156 |
String hyperlink = null;
|
|
|
157 |
// x-www-form-urlencoded
|
|
|
158 |
final Map<String, String> arguments = new HashMap<>();
|
|
|
159 |
arguments.put("pI", prefs.get(PayPalPreferencePanel.PAYPAL_CLIENTID, ""));
|
|
|
160 |
arguments.put("pS", prefs.get(PayPalPreferencePanel.PAYPAL_SECRET, ""));
|
|
|
161 |
arguments.put("ref", ref);
|
|
|
162 |
arguments.put("amount", amount);
|
|
|
163 |
arguments.put("currency", currency);
|
|
|
164 |
arguments.put("type", "paypal");
|
|
|
165 |
final StringJoiner sj = new StringJoiner("&");
|
|
|
166 |
for (Map.Entry<String, String> entry : arguments.entrySet()) {
|
|
|
167 |
sj.add(URLEncoder.encode(entry.getKey(), "UTF-8") + "=" + URLEncoder.encode(entry.getValue(), "UTF-8"));
|
|
|
168 |
}
|
|
|
169 |
final String postData = sj.toString();
|
|
|
170 |
byte[] out = postData.getBytes(StandardCharsets.UTF_8);
|
|
|
171 |
int length = out.length;
|
|
|
172 |
http.setFixedLengthStreamingMode(length);
|
|
|
173 |
http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
|
|
|
174 |
http.connect();
|
|
|
175 |
try (OutputStream os = http.getOutputStream()) {
|
|
|
176 |
os.write(out);
|
|
|
177 |
}
|
|
|
178 |
if (http.getResponseCode() != 401) {
|
|
|
179 |
|
|
|
180 |
InputStream is = http.getInputStream();
|
|
|
181 |
InputStreamReader isr = new InputStreamReader(is);
|
|
|
182 |
|
|
|
183 |
int numCharsRead;
|
|
|
184 |
char[] charArray = new char[1024];
|
|
|
185 |
StringBuilder sb = new StringBuilder();
|
|
|
186 |
while ((numCharsRead = isr.read(charArray)) > 0) {
|
|
|
187 |
sb.append(charArray, 0, numCharsRead);
|
|
|
188 |
}
|
|
|
189 |
//
|
|
|
190 |
hyperlink = sb.toString();
|
|
|
191 |
}
|
|
|
192 |
s.addLink(inFile, pdfFile, x, y, hyperlink);
|
|
|
193 |
} catch (Exception e) {
|
|
|
194 |
e.printStackTrace();
|
|
|
195 |
SheetUtils.convert2PDF(doc, pdfFile, attachments);
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
} else {
|
|
|
199 |
SheetUtils.convert2PDF(doc, pdfFile, attachments);
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
} catch (Throwable e) {
|
|
|
203 |
ExceptionHandler.handle("Impossible de créer le PDF " + pdfFile.getAbsolutePath(), e);
|
|
|
204 |
}
|
|
|
205 |
if (!pdfFile.canRead()) {
|
|
|
206 |
ExceptionHandler.handle("Le fichier PDF " + pdfFile.getAbsolutePath() + " ne peut être lu.");
|
|
|
207 |
}
|
|
|
208 |
storeWithStorageEngines(generatedFile, pdfFile, storagePath);
|
|
|
209 |
}
|
|
|
210 |
|
18 |
ilm |
211 |
}
|