# HG changeset patch
# User Armaignac <collazoandy4@gmail.com>
# Date 1525377652 14400
#      Thu May 03 16:00:52 2018 -0400
# Node ID 407ffc93b2b0f10b2f8ede129d2d6b4217914fa6
# Parent  cbe4b523bc8e3e9c863ecc8641fbfc733345314b
Fixes issue 38340: There is no possibility to put a report template for
Distribution Orders in the Document Type

The Distribution Order document type is ignoring the AD definition for Report
Template.

Now the code have been modified to properly take into account the AD definition
for Report Template.

diff --git a/src/org/openbravo/distributionorder/erpReports/OBDO_DistOrder.jrxml b/src/org/openbravo/distributionorder/erpReports/OBDO_DistOrder.jrxml
--- a/src/org/openbravo/distributionorder/erpReports/OBDO_DistOrder.jrxml
+++ b/src/org/openbravo/distributionorder/erpReports/OBDO_DistOrder.jrxml
@@ -59,13 +59,11 @@
 	</parameter>
 	<parameter name="LOCALE" class="java.util.Locale" isForPrompting="false"/>
 	<parameter name="NUMBERFORMAT" class="java.text.DecimalFormat" isForPrompting="false"/>
-	<parameter name="SUBREP_C_OrderLinesJR_new" class="net.sf.jasperreports.engine.JasperReport" isForPrompting="false"/>
-	<parameter name="SUBREP_C_OrderLinesTaxIncludedJR_new" class="net.sf.jasperreports.engine.JasperReport" isForPrompting="false"/>
+	<parameter name="SUBREP_OBDO_DistOrderLinesJR" class="net.sf.jasperreports.engine.JasperReport" isForPrompting="false"/>
 	<parameter name="SHOW_COMPANYDATA" class="java.lang.String" isForPrompting="false"/>
 	<parameter name="DOCUMENT_ID" class="java.lang.String" isForPrompting="false">
 		<defaultValueExpression><![CDATA["''"]]></defaultValueExpression>
 	</parameter>
-	<parameter name="SR_LINES" class="net.sf.jasperreports.engine.JasperReport" isForPrompting="false"/>
 	<queryString>
 		<![CDATA[SELECT OBDO.DOCUMENTNO, OBDO.DATEORDERED, OBDO.DATEPROMISED, OBDO.ISSOTRX,OBDO.AD_ORG_ID AS organizationid,
 	(CASE WHEN OBDO.ISSOTRX = 'Y' THEN OBDO.ISSUESTATUS ELSE DORE.ISSUESTATUS END) AS ISSUESTATUS,
@@ -398,7 +396,7 @@
 					<subreportParameterExpression><![CDATA[$P{DOCUMENT_ID}]]></subreportParameterExpression>
 				</subreportParameter>
 				<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
-				<subreportExpression><![CDATA[$P{SR_LINES}]]></subreportExpression>
+				<subreportExpression><![CDATA[$P{SUBREP_OBDO_DistOrderLinesJR}]]></subreportExpression>
 			</subreport>
 		</band>
 	</detail>
diff --git a/src/org/openbravo/distributionorder/erpReports/Print_DistOrder.java b/src/org/openbravo/distributionorder/erpReports/Print_DistOrder.java
--- a/src/org/openbravo/distributionorder/erpReports/Print_DistOrder.java
+++ b/src/org/openbravo/distributionorder/erpReports/Print_DistOrder.java
@@ -11,7 +11,7 @@
  * under the License.
  * The Original Code is Openbravo ERP.
  * The Initial Developer of the Original Code is Openbravo SLU
- * All portions are Copyright (C) 2017 Openbravo SLU
+ * All portions are Copyright (C) 2017-2018 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  *************************************************************************
@@ -19,26 +19,29 @@
 
 package org.openbravo.distributionorder.erpReports;
 
+import java.io.File;
 import java.io.IOException;
 import java.util.HashMap;
+import java.util.List;
 
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import net.sf.jasperreports.engine.JRException;
-import net.sf.jasperreports.engine.JasperReport;
-
-import org.apache.log4j.Logger;
 import org.openbravo.base.secureApp.HttpSecureAppServlet;
 import org.openbravo.base.secureApp.VariablesSecureApp;
 import org.openbravo.client.application.report.ReportingUtils;
+import org.openbravo.client.application.report.ReportingUtils.ExportType;
+import org.openbravo.dal.service.OBDal;
+import org.openbravo.distributionorder.DistributionOrder;
+import org.openbravo.model.common.enterprise.DocumentTemplate;
+import org.openbravo.service.db.DalConnectionProvider;
 
 public class Print_DistOrder extends HttpSecureAppServlet {
 
   private static final long serialVersionUID = 1L;
-  private static final Logger log = Logger.getLogger(Print_DistOrder.class);
+  private static final String fileExtension = ".pdf";
 
   public void init(ServletConfig config) {
     super.init(config);
@@ -66,21 +69,38 @@
       log4j.debug("Output: pdf");
     String strBaseDesign = getBaseDesignPath(vars.getLanguage());
     HashMap<String, Object> parameters = new HashMap<String, Object>();
-    JasperReport jasperReportLines;
-    try {
-      jasperReportLines = ReportingUtils.compileReport(strBaseDesign
-          + "/org/openbravo/distributionorder/erpReports/OBDO_DistOrderLinesJR.jrxml");
-    } catch (JRException e) {
-      log.error("Error generating report: " + e.getMessage(), e);
-      throw new ServletException(e.getMessage());
+
+    String reportName = "OBDO_DistOrder";
+    String templateName = "OBDO_DistOrder.jrxml";
+    String templateLocation = "/org/openbravo/distributionorder/erpReports/";
+    DistributionOrder distributionOrder = OBDal.getInstance().get(DistributionOrder.class,
+        strmDistOrderId);
+    List<DocumentTemplate> documentTemplate = distributionOrder.getDocumentType()
+        .getDocumentTemplateList();
+
+    for (DocumentTemplate template : documentTemplate) {
+      if (template.isActive()) {
+        templateLocation = template.getTemplateLocation() + "/";
+        reportName = template.getReportFilename();
+        templateName = template.getTemplateFilename();
+        break;
+      }
     }
 
-    parameters.put("SR_LINES", jasperReportLines);
     parameters.put("DOCUMENT_ID", strmDistOrderId);
 
-    renderJR(vars, response, strBaseDesign
-        + "/org/openbravo/distributionorder/erpReports/OBDO_DistOrder.jrxml", "pdf", parameters,
-        null, null);
+    File outputFile = new File(globalParameters.strFTPDirectory + "/" + reportName + "-"
+        + strmDistOrderId + fileExtension);
+
+    ReportingUtils.exportJR(strBaseDesign + templateLocation + templateName, ExportType.PDF,
+        parameters, outputFile, false, DalConnectionProvider.getReadOnlyConnectionProvider(), null,
+        null, true);
+
+    response.setContentType("text/html;charset=UTF-8");
+    response.setHeader("Content-disposition", "inline" + "; filename=" + reportName + "-"
+        + strmDistOrderId + ".html");
+    printPagePopUpDownload(response.getOutputStream(), reportName + "-" + strmDistOrderId
+        + fileExtension);
   }
 
   public String getServletInfo() {
# HG changeset patch
# User Víctor Martínez Romanos <victor.martinez@openbravo.com>
# Date 1525965387 -7200
#      Thu May 10 17:16:27 2018 +0200
# Node ID a31efe9de6a9b5b4ed772382a3f0561abb61cd95
# Parent  407ffc93b2b0f10b2f8ede129d2d6b4217914fa6
Fixes issue 38340: reverted changes in Print_DistOrder as they are not right

diff --git a/src/org/openbravo/distributionorder/erpReports/Print_DistOrder.java b/src/org/openbravo/distributionorder/erpReports/Print_DistOrder.java
--- a/src/org/openbravo/distributionorder/erpReports/Print_DistOrder.java
+++ b/src/org/openbravo/distributionorder/erpReports/Print_DistOrder.java
@@ -11,7 +11,7 @@
  * under the License.
  * The Original Code is Openbravo ERP.
  * The Initial Developer of the Original Code is Openbravo SLU
- * All portions are Copyright (C) 2017-2018 Openbravo SLU
+ * All portions are Copyright (C) 2017 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  *************************************************************************
@@ -19,29 +19,26 @@
 
 package org.openbravo.distributionorder.erpReports;
 
-import java.io.File;
 import java.io.IOException;
 import java.util.HashMap;
-import java.util.List;
 
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import net.sf.jasperreports.engine.JRException;
+import net.sf.jasperreports.engine.JasperReport;
+
+import org.apache.log4j.Logger;
 import org.openbravo.base.secureApp.HttpSecureAppServlet;
 import org.openbravo.base.secureApp.VariablesSecureApp;
 import org.openbravo.client.application.report.ReportingUtils;
-import org.openbravo.client.application.report.ReportingUtils.ExportType;
-import org.openbravo.dal.service.OBDal;
-import org.openbravo.distributionorder.DistributionOrder;
-import org.openbravo.model.common.enterprise.DocumentTemplate;
-import org.openbravo.service.db.DalConnectionProvider;
 
 public class Print_DistOrder extends HttpSecureAppServlet {
 
   private static final long serialVersionUID = 1L;
-  private static final String fileExtension = ".pdf";
+  private static final Logger log = Logger.getLogger(Print_DistOrder.class);
 
   public void init(ServletConfig config) {
     super.init(config);
@@ -69,38 +66,21 @@
       log4j.debug("Output: pdf");
     String strBaseDesign = getBaseDesignPath(vars.getLanguage());
     HashMap<String, Object> parameters = new HashMap<String, Object>();
-
-    String reportName = "OBDO_DistOrder";
-    String templateName = "OBDO_DistOrder.jrxml";
-    String templateLocation = "/org/openbravo/distributionorder/erpReports/";
-    DistributionOrder distributionOrder = OBDal.getInstance().get(DistributionOrder.class,
-        strmDistOrderId);
-    List<DocumentTemplate> documentTemplate = distributionOrder.getDocumentType()
-        .getDocumentTemplateList();
-
-    for (DocumentTemplate template : documentTemplate) {
-      if (template.isActive()) {
-        templateLocation = template.getTemplateLocation() + "/";
-        reportName = template.getReportFilename();
-        templateName = template.getTemplateFilename();
-        break;
-      }
+    JasperReport jasperReportLines;
+    try {
+      jasperReportLines = ReportingUtils.compileReport(strBaseDesign
+          + "/org/openbravo/distributionorder/erpReports/OBDO_DistOrderLinesJR.jrxml");
+    } catch (JRException e) {
+      log.error("Error generating report: " + e.getMessage(), e);
+      throw new ServletException(e.getMessage());
     }
 
+    parameters.put("SR_LINES", jasperReportLines);
     parameters.put("DOCUMENT_ID", strmDistOrderId);
 
-    File outputFile = new File(globalParameters.strFTPDirectory + "/" + reportName + "-"
-        + strmDistOrderId + fileExtension);
-
-    ReportingUtils.exportJR(strBaseDesign + templateLocation + templateName, ExportType.PDF,
-        parameters, outputFile, false, DalConnectionProvider.getReadOnlyConnectionProvider(), null,
-        null, true);
-
-    response.setContentType("text/html;charset=UTF-8");
-    response.setHeader("Content-disposition", "inline" + "; filename=" + reportName + "-"
-        + strmDistOrderId + ".html");
-    printPagePopUpDownload(response.getOutputStream(), reportName + "-" + strmDistOrderId
-        + fileExtension);
+    renderJR(vars, response, strBaseDesign
+        + "/org/openbravo/distributionorder/erpReports/OBDO_DistOrder.jrxml", "pdf", parameters,
+        null, null);
   }
 
   public String getServletInfo() {
# HG changeset patch
# User Víctor Martínez Romanos <victor.martinez@openbravo.com>
# Date 1525967931 -7200
#      Thu May 10 17:58:51 2018 +0200
# Node ID 259a0ed202429935f2bfb5f9ca187e297d9cd06c
# Parent  a31efe9de6a9b5b4ed772382a3f0561abb61cd95
Fixed issue 38340: improved solution based on #38518
The Template is gotten from the standard TemplateData.getDocumentTemplates() API. If not returns the hardcoded template for DO.
Logic related to download is directly managed by HttpSecureAppServlet

diff --git a/src/org/openbravo/distributionorder/erpReports/Print_DistOrder.java b/src/org/openbravo/distributionorder/erpReports/Print_DistOrder.java
--- a/src/org/openbravo/distributionorder/erpReports/Print_DistOrder.java
+++ b/src/org/openbravo/distributionorder/erpReports/Print_DistOrder.java
@@ -11,7 +11,7 @@
  * under the License.
  * The Original Code is Openbravo ERP.
  * The Initial Developer of the Original Code is Openbravo SLU
- * All portions are Copyright (C) 2017 Openbravo SLU
+ * All portions are Copyright (C) 2017-2018 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  *************************************************************************
@@ -27,60 +27,54 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import net.sf.jasperreports.engine.JRException;
-import net.sf.jasperreports.engine.JasperReport;
-
-import org.apache.log4j.Logger;
 import org.openbravo.base.secureApp.HttpSecureAppServlet;
 import org.openbravo.base.secureApp.VariablesSecureApp;
-import org.openbravo.client.application.report.ReportingUtils;
+import org.openbravo.dal.core.OBContext;
+import org.openbravo.dal.service.OBDal;
+import org.openbravo.distributionorder.DistributionOrder;
+import org.openbravo.erpCommon.utility.reporting.TemplateData;
 
 public class Print_DistOrder extends HttpSecureAppServlet {
-
   private static final long serialVersionUID = 1L;
-  private static final Logger log = Logger.getLogger(Print_DistOrder.class);
+  private static final String DEFAULT_TEMPLATE = "@basedesign@/org/openbravo/distributionorder/erpReports/OBDO_DistOrder.jrxml";
 
   public void init(ServletConfig config) {
     super.init(config);
     boolHist = false;
   }
 
-  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException,
-      ServletException {
-
+  public void doPost(HttpServletRequest request, HttpServletResponse response)
+      throws IOException, ServletException {
     VariablesSecureApp vars = new VariablesSecureApp(request);
-
     if (vars.commandIn("DEFAULT")) {
-
       String strmDistOrderId = vars.getSessionValue("PRINTDISTRIBUTIONORDER.INPOBDODISTORDERID")
           .replace("('", "").replace("')", "");
-
       printPagePartePDF(response, vars, strmDistOrderId);
     } else
       pageError(response);
   }
 
   private void printPagePartePDF(HttpServletResponse response, VariablesSecureApp vars,
-      String strmDistOrderId) throws IOException, ServletException {
-    if (log4j.isDebugEnabled())
-      log4j.debug("Output: pdf");
-    String strBaseDesign = getBaseDesignPath(vars.getLanguage());
-    HashMap<String, Object> parameters = new HashMap<String, Object>();
-    JasperReport jasperReportLines;
+      String strmDistOrderId) throws ServletException {
+    final HashMap<String, Object> parameters = new HashMap<>(1);
+    parameters.put("DOCUMENT_ID", strmDistOrderId);
+    renderJR(vars, response, getDocumentTypeTemplateOrDefaultIfNotFound(strmDistOrderId), "pdf",
+        parameters, null, null);
+  }
+
+  private String getDocumentTypeTemplateOrDefaultIfNotFound(final String strmDistOrderId) {
     try {
-      jasperReportLines = ReportingUtils.compileReport(strBaseDesign
-          + "/org/openbravo/distributionorder/erpReports/OBDO_DistOrderLinesJR.jrxml");
-    } catch (JRException e) {
-      log.error("Error generating report: " + e.getMessage(), e);
-      throw new ServletException(e.getMessage());
+      OBContext.setAdminMode(true);
+      final DistributionOrder distributionOrder = OBDal.getInstance().get(DistributionOrder.class,
+          strmDistOrderId);
+      final TemplateData[] templates = TemplateData.getDocumentTemplates(this,
+          distributionOrder.getDocumentType().getId(), distributionOrder.getOrganization().getId());
+      return templates[0].templateLocation + "/" + templates[0].templateFilename;
+    } catch (Exception noTemplateFound) {
+      return DEFAULT_TEMPLATE;
+    } finally {
+      OBContext.restorePreviousMode();
     }
-
-    parameters.put("SR_LINES", jasperReportLines);
-    parameters.put("DOCUMENT_ID", strmDistOrderId);
-
-    renderJR(vars, response, strBaseDesign
-        + "/org/openbravo/distributionorder/erpReports/OBDO_DistOrder.jrxml", "pdf", parameters,
-        null, null);
   }
 
   public String getServletInfo() {
