
# HG changeset patch
# User Guillermo Alvarez de Eulate <guillermo.alvarez@openbravo.com>
# Date 1448042725 -3600
# Node ID 35fb47c9edda371422a04532bf28476cbd6f837c
# Parent  7d8b9586731bdd33d8ebe1bece25bc71a9ed6deb
Fixed issue 31471: Check if any invoice is present

diff -r 7d8b9586731b -r 35fb47c9edda src/org/openbravo/retail/posterminal/OrderLoader.java
--- a/src/org/openbravo/retail/posterminal/OrderLoader.java	Fri Nov 20 19:05:01 2015 +0100
+++ b/src/org/openbravo/retail/posterminal/OrderLoader.java	Fri Nov 20 19:05:25 2015 +0100
@@ -29,6 +29,7 @@
 import org.codehaus.jettison.json.JSONArray;
 import org.codehaus.jettison.json.JSONException;
 import org.codehaus.jettison.json.JSONObject;
+import org.hibernate.Query;
 import org.hibernate.ScrollMode;
 import org.hibernate.ScrollableResults;
 import org.hibernate.criterion.Restrictions;
@@ -224,6 +225,17 @@
           createShipment &= jsonorder.getBoolean("generateShipment");
           createInvoice &= jsonorder.getBoolean("generateShipment");
         }
+
+        // We have to check if there is any line in the order which have been already invoiced. If
+        // it is the case we will not create the invoice.
+        List<Invoice> lstInvoice = getInvoicesRelatedToOrder(jsonorder.getString("id"));
+        if (lstInvoice != null) {
+          // We have found and invoice, so it will be used to assign payments
+          // TODO several invoices involved
+          invoice = lstInvoice.get(0);
+          createInvoice = false;
+        }
+
         // Order header
         if (log.isDebugEnabled()) {
           t111 = System.currentTimeMillis();
@@ -741,6 +753,33 @@
     }
   }
 
+  protected List<Invoice> getInvoicesRelatedToOrder(String orderId) {
+    List<String> lstInvoicesIds = new ArrayList<String>();
+    List<Invoice> lstInvoices = new ArrayList<Invoice>();
+    StringBuffer involvedInvoicedHqlQueryWhereStr = new StringBuffer();
+    involvedInvoicedHqlQueryWhereStr
+        .append("SELECT il.invoice.id FROM InvoiceLine il WHERE il.salesOrderLine.salesOrder.id = :orderid ORDER BY il.invoice.creationDate ASC");
+    Query qryRelatedInvoices = OBDal.getInstance().getSession()
+        .createQuery(involvedInvoicedHqlQueryWhereStr.toString());
+    qryRelatedInvoices.setParameter("orderid", orderId);
+
+    ScrollableResults relatedInvoices = qryRelatedInvoices.scroll(ScrollMode.FORWARD_ONLY);
+
+    while (relatedInvoices.next()) {
+      lstInvoicesIds.add((String) relatedInvoices.get(0));
+    }
+
+    if (lstInvoicesIds.size() > 0 && lstInvoicesIds.size() <= 1) {
+      lstInvoices.add(OBDal.getInstance().get(Invoice.class, lstInvoicesIds.get(0)));
+      return lstInvoices;
+    } else if (lstInvoices.size() > 1) {
+      // TODO several invoices
+      return null;
+    } else {
+      return null;
+    }
+  }
+
   protected void createInvoice(Invoice invoice, Order order, JSONObject jsonorder)
       throws JSONException {
     Entity invoiceEntity = ModelProvider.getInstance().getEntity(Invoice.class);

