# HG changeset patch
# User Prakash M <prakash@qualiantech.com>
# Date 1595328241 -19800
#      Tue Jul 21 16:14:01 2020 +0530
# Node ID 330de0439fef6552eedddee41a7280c5922603fb
# Parent  8a7424cc03927eac58f63cb80619fc0798fa6ec6
Fixes following issue

* Unable to remove added redeem loyalty points when all module sample data is not
installed (org.openbravo.retail.testsampledata)

* This is because of foreign key voilation of Earn Rule
* In class CancelGiftCertificateAmount, MANUAL_EARN_RULE_ID is hardcoded
* This id will not exists if testsampledata module is not installed

* Instead of hardcoded earn rule id, fetched earn rule using subscription's category
and manual rule id

diff -r 8a7424cc0392 -r 330de0439fef src/org/openbravo/retail/loyalty/burn/giftcertificate/process/CancelGiftCertificateAmount.java
--- a/src/org/openbravo/retail/loyalty/burn/giftcertificate/process/CancelGiftCertificateAmount.java	Tue Dec 31 13:13:25 2019 +0100
+++ b/src/org/openbravo/retail/loyalty/burn/giftcertificate/process/CancelGiftCertificateAmount.java	Tue Jul 21 16:14:01 2020 +0530
@@ -19,6 +19,7 @@
 import org.codehaus.jettison.json.JSONObject;
 import org.openbravo.base.provider.OBProvider;
 import org.openbravo.dal.service.OBDal;
+import org.openbravo.dal.service.OBQuery;
 import org.openbravo.erpCommon.utility.OBMessageUtils;
 import org.openbravo.loyalty.programs.data.OBLPEarnRule;
 import org.openbravo.loyalty.programs.data.OBLPEarnedPoints;
@@ -32,6 +33,7 @@
 public class CancelGiftCertificateAmount extends JSONProcessSimple {
 
   private static final String MANUAL_EARN_RULE_ID = "E6F0FABF3F8843C7977008EFFD1C8859";
+  private static final String MANUAL_RULE_ID = "B369B6ED59AD4057BCCAEBBA25741EF8";
   private static final String RESTORED_CONSUMED_POINTS_BY_GIFT_CERTIFICATE = "OBRLPGC_RestoredConsumedPointsByGiftCert";
 
   @Override
@@ -81,8 +83,7 @@
     restoredPoints.setOrganization(subscription.getOrganization());
     restoredPoints.setManual(true);
     restoredPoints.setDate(new Date());
-    restoredPoints
-        .setOblpEarnRule(OBDal.getInstance().getProxy(OBLPEarnRule.class, MANUAL_EARN_RULE_ID));
+    restoredPoints.setOblpEarnRule(getEarnRule(subscription));
     restoredPoints.setOblpSubscription(subscription);
     restoredPoints.setEvent(OBMessageUtils.messageBD(RESTORED_CONSUMED_POINTS_BY_GIFT_CERTIFICATE));
     restoredPoints.setTotalpoints(points);
@@ -94,6 +95,16 @@
     return restoredPoints;
   }
 
+  private OBLPEarnRule getEarnRule(OBLPSubscription subscription) {
+    OBQuery<OBLPEarnRule> earnRuleQuery = OBDal.getInstance()
+        .createQuery(OBLPEarnRule.class,
+            " as er where er.oblpRule.id = :ruleId and er.oblpCategory.id = :categoryId");
+    earnRuleQuery.setNamedParameter("ruleId", MANUAL_RULE_ID);
+    earnRuleQuery.setNamedParameter("categoryId", subscription.getOblpCategory().getId());
+    earnRuleQuery.setMaxResult(1);
+    return earnRuleQuery.uniqueResult();
+  }
+
   @Override
   protected boolean bypassPreferenceCheck() {
     return true;
