Dépôt officiel du code source de l'ERP OpenConcerto
Rev 180 | Blame | Compare with Previous | Last modification | View Log | RSS feed
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
*
* The contents of this file are subject to the terms of the GNU General Public License Version 3
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each file.
*/
package org.openconcerto.erp.core.sales.invoice.report;
import org.openconcerto.erp.config.ComptaPropsConfiguration;
import org.openconcerto.erp.core.common.element.BanqueSQLElement;
import org.openconcerto.erp.core.sales.quote.report.PaypalStamper;
import org.openconcerto.erp.core.supplychain.purchase.importer.FacturXExporter;
import org.openconcerto.erp.generationDoc.AbstractSheetXMLWithDate;
import org.openconcerto.erp.generationDoc.PDFAttachment;
import org.openconcerto.erp.generationDoc.SheetUtils;
import org.openconcerto.erp.generationDoc.gestcomm.OptionDocProcessor;
import org.openconcerto.erp.preferences.PayPalPreferencePanel;
import org.openconcerto.erp.preferences.PrinterNXProps;
import org.openconcerto.sql.Configuration;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.preferences.SQLPreferences;
import org.openconcerto.utils.ExceptionHandler;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import org.jopendocument.model.OpenDocument;
public class VenteFactureXmlSheet extends AbstractSheetXMLWithDate {
public static final String TEMPLATE_ID = "VenteFacture";
public static final String TEMPLATE_SITUATION_SUFFIX = "Situation";
public static final String TEMPLATE_PROPERTY_NAME = "LocationFacture";
@Override
public String getReference() {
return this.getSQLRow().getString("NOM");
}
@Override
public String getName() {
final String startName;
if (this.getSQLRow().getBoolean("COMPLEMENT")) {
startName = "FactureComplement_";
} else if (this.getSQLRow().getBoolean("ACOMPTE")) {
startName = "FactureAcompte_";
} else {
startName = "Facture_";
}
return startName + this.getSQLRow().getString("NUMERO");
}
@Override
public SQLRow getRowLanguage() {
SQLRow rowClient = this.getSQLRow().getForeignRow("ID_CLIENT");
if (rowClient.getTable().contains("ID_LANGUE")) {
if (!rowClient.isForeignEmpty("ID_LANGUE")) {
return rowClient.getForeign("ID_LANGUE");
} else {
return null;
}
} else {
return super.getRowLanguage();
}
}
public VenteFactureXmlSheet(SQLRow row) {
super(row);
this.elt = Configuration.getInstance().getDirectory().getElement("SAISIE_VENTE_FACTURE");
this.printer = PrinterNXProps.getInstance().getStringProperty("FacturePrinter");
getDefaultTemplateId();
this.setPostProcess(new OptionDocProcessor());
}
@Override
public String getType() {
String type;
SQLRow row = this.getSQLRow();
if (row.getBoolean("COMPLEMENT")) {
type = "Complement";
} else if (row.getBoolean("ACOMPTE")) {
type = "Acompte";
} else if (row.getBoolean("PARTIAL") || row.getBoolean("SOLDE")) {
type = TEMPLATE_SITUATION_SUFFIX;
} else {
type = null;
}
return type;
}
@Override
public String getDefaultTemplateId() {
return TEMPLATE_ID;
}
@Override
public void createPDF(final File generatedFile, final File pdfFile, final OpenDocument doc, String storagePath) {
if (pdfFile == null) {
throw new IllegalArgumentException("null PDF file");
}
try {
final List<PDFAttachment> attachments = new ArrayList<>(1);
final FacturXExporter ex = new FacturXExporter();
final SQLRow rowSociete = ComptaPropsConfiguration.getInstanceCompta().getRowSociete();
final String xml = ex.createXMLFrom(getSQLRow().getTable().getDBRoot(), getSQLRow().getID(), rowSociete);
attachments.add(new PDFAttachment("factur-x.xml", "factur-x.xml", xml.getBytes(StandardCharsets.UTF_8), "text/xml"));
final SQLPreferences prefs = SQLPreferences.getMemCached(getElement().getTable().getDBRoot());
if (prefs.getBoolean(PayPalPreferencePanel.PAYPAL_INVOICE, false)) {
try {
final File inFile = File.createTempFile("oc_", pdfFile.getName());
SheetUtils.convert2PDF(doc, inFile, attachments);
PaypalStamper s = new PaypalStamper();
int x = prefs.getInt(PayPalPreferencePanel.PAYPAL_INVOICE_X, 0);
int y = prefs.getInt(PayPalPreferencePanel.PAYPAL_INVOICE_Y, 0);
// Reference
String ref = getSQLRow().getString("NUMERO");
// Montant : ex : 10.55
long cents = getSQLRow().getLong("NET_A_PAYER");
String amount = cents / 100 + "." + cents % 100;
// Devise
// TODO : autres devises
String currency = "EUR";
// POST
final URL url = new URL("https://cloud.openconcerto.org/payment");
final URLConnection con = url.openConnection();
final HttpURLConnection http = (HttpURLConnection) con;
http.setRequestMethod("POST");
http.setDoOutput(true);
http.setDefaultUseCaches(false);
String hyperlink = null;
// x-www-form-urlencoded
final Map<String, String> arguments = new HashMap<>();
arguments.put("pI", prefs.get(PayPalPreferencePanel.PAYPAL_CLIENTID, ""));
arguments.put("pS", prefs.get(PayPalPreferencePanel.PAYPAL_SECRET, ""));
arguments.put("ref", ref);
arguments.put("amount", amount);
arguments.put("currency", currency);
arguments.put("type", "paypal");
final StringJoiner sj = new StringJoiner("&");
for (Map.Entry<String, String> entry : arguments.entrySet()) {
sj.add(URLEncoder.encode(entry.getKey(), "UTF-8") + "=" + URLEncoder.encode(entry.getValue(), "UTF-8"));
}
final String postData = sj.toString();
byte[] out = postData.getBytes(StandardCharsets.UTF_8);
int length = out.length;
http.setFixedLengthStreamingMode(length);
http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
http.connect();
try (OutputStream os = http.getOutputStream()) {
os.write(out);
}
if (http.getResponseCode() != 401) {
InputStream is = http.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
int numCharsRead;
char[] charArray = new char[1024];
StringBuilder sb = new StringBuilder();
while ((numCharsRead = isr.read(charArray)) > 0) {
sb.append(charArray, 0, numCharsRead);
}
//
hyperlink = sb.toString();
}
s.addLink(inFile, pdfFile, x, y, hyperlink);
} catch (Exception e) {
e.printStackTrace();
SheetUtils.convert2PDF(doc, pdfFile, attachments);
}
} else {
SheetUtils.convert2PDF(doc, pdfFile, attachments);
}
} catch (Throwable e) {
ExceptionHandler.handle("Impossible de créer le PDF " + pdfFile.getAbsolutePath(), e);
}
if (!pdfFile.canRead()) {
ExceptionHandler.handle("Le fichier PDF " + pdfFile.getAbsolutePath() + " ne peut être lu.");
}
storeWithStorageEngines(generatedFile, pdfFile, storagePath);
}
}