Related to issue 21250: Pick and Edit definition
authorDavid Miguelez <david.miguelez@openbravo.com>
Fri, 10 Aug 2012 15:02:02 +0200
changeset 17621 9a4192cfb1fb
parent 17620 280e6319d19a
child 17622 e1f966f5e967
Related to issue 21250: Pick and Edit definition
modules/org.openbravo.advpaymentmngt/src-db/database/model/views/APRM_PAYMENT_PROP_PICK_EDIT.xml
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/actionHandler/PaymentProposalPickEditLines.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/org.openbravo.advpaymentmngt/src-db/database/model/views/APRM_PAYMENT_PROP_PICK_EDIT.xml	Fri Aug 10 15:02:02 2012 +0200
@@ -0,0 +1,19 @@
+<?xml version="1.0"?>
+  <database name="VIEW APRM_PAYMENT_PROP_PICK_EDIT">
+    <view name="APRM_PAYMENT_PROP_PICK_EDIT"><![CDATA[SELECT psd.fin_payment_scheduledetail_id AS aprm_payment_prop_pick_edit_id, psd.ad_client_id, psd.ad_org_id, psd.isactive, psd.created, psd.updated, psd.createdby, psd.updatedby, psd.fin_payment_scheduledetail_id, ppd.fin_payment_proposal_id, o.documentno AS order_documentno, i.documentno AS invoice_documentno, COALESCE(i.fin_paymentmethod_id, o.fin_paymentmethod_id) AS fin_paymentmethod_id, COALESCE(i.c_bpartner_id, o.c_bpartner_id) AS c_bpartner_id, COALESCE(psi.duedate, pso.duedate) AS duedate, i.grandtotal AS invoiced, COALESCE(psi.amount, pso.amount) AS expected, psd.amount AS outstanding, COALESCE(ppd.amount, 0) AS payment, COALESCE(psd.amount - ppd.amount, psd.amount) AS difference, 
+CASE
+WHEN ppd.writeoffamt <> 0 THEN 'Y'
+ELSE 'N'
+END AS writeoff, 
+CASE
+WHEN ppd.fin_payment_scheduledetail_id IS NULL THEN 'N'
+ELSE 'Y'
+END AS ob_selected, COALESCE(psi.fin_payment_priority_id, pso.fin_payment_priority_id) AS fin_payment_priority_id, COALESCE(i.c_currency_id, o.c_currency_id) AS c_currency_id, COALESCE(i.issotrx, o.issotrx) AS issotrx
+FROM fin_payment_scheduledetail psd
+LEFT JOIN fin_payment_prop_detail ppd ON (psd.fin_payment_scheduledetail_id=ppd.fin_payment_scheduledetail_id)
+LEFT JOIN fin_payment_schedule psi ON psd.fin_payment_schedule_invoice = psi.fin_payment_schedule_id
+LEFT JOIN fin_payment_schedule pso ON psd.fin_payment_schedule_order = pso.fin_payment_schedule_id
+LEFT JOIN c_invoice i ON psi.c_invoice_id = i.c_invoice_id
+LEFT JOIN c_order o ON pso.c_order_id = o.c_order_id
+WHERE psd.fin_payment_detail_id IS NULL]]></view>
+  </database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/actionHandler/PaymentProposalPickEditLines.java	Fri Aug 10 15:02:02 2012 +0200
@@ -0,0 +1,135 @@
+package org.openbravo.advpaymentmngt.actionHandler;
+
+import java.math.BigDecimal;
+import java.util.Map;
+
+import org.apache.log4j.Logger;
+import org.codehaus.jettison.json.JSONArray;
+import org.codehaus.jettison.json.JSONException;
+import org.codehaus.jettison.json.JSONObject;
+import org.openbravo.base.exception.OBException;
+import org.openbravo.base.provider.OBProvider;
+import org.openbravo.base.secureApp.VariablesSecureApp;
+import org.openbravo.client.application.process.BaseProcessActionHandler;
+import org.openbravo.client.kernel.RequestContext;
+import org.openbravo.dal.core.OBContext;
+import org.openbravo.dal.service.OBDal;
+import org.openbravo.erpCommon.utility.Utility;
+import org.openbravo.model.financialmgmt.payment.FIN_PaymentPropDetail;
+import org.openbravo.model.financialmgmt.payment.FIN_PaymentProposal;
+import org.openbravo.model.financialmgmt.payment.FIN_PaymentScheduleDetail;
+import org.openbravo.service.db.DalConnectionProvider;
+
+public class PaymentProposalPickEditLines extends BaseProcessActionHandler {
+  private static Logger log = Logger.getLogger(PaymentProposalPickEditLines.class);
+
+  @Override
+  protected JSONObject doExecute(Map<String, Object> parameters, String content) {
+    JSONObject jsonRequest = null;
+    OBContext.setAdminMode();
+    try {
+      jsonRequest = new JSONObject(content);
+      log.debug(jsonRequest);
+      // When the focus is NOT in the tab of the button (i.e. any child tab) and the tab does not
+      // contain any record, the inppaymentproposal parameter contains "null" string. Use
+      // Fin_Payment_Proposal_ID
+      // instead because it always contains the id of the selected order.
+      // Issue 20585: https://issues.openbravo.com/view.php?id=20585
+      final String strPaymentProposalId = jsonRequest.getString("Fin_Payment_Proposal_ID");
+      FIN_PaymentProposal paymentProposal = OBDal.getInstance().get(FIN_PaymentProposal.class,
+          strPaymentProposalId);
+
+      if (cleanPaymentProposalDetails(paymentProposal)) {
+        createPaymentProposalDetails(jsonRequest);
+      }
+
+    } catch (Exception e) {
+      OBDal.getInstance().rollbackAndClose();
+      VariablesSecureApp vars = RequestContext.get().getVariablesSecureApp();
+      log.error(e.getMessage(), e);
+
+      try {
+        jsonRequest = new JSONObject();
+
+        JSONObject errorMessage = new JSONObject();
+        errorMessage.put("severity", "error");
+        errorMessage.put("text",
+            Utility.messageBD(new DalConnectionProvider(), e.getMessage(), vars.getLanguage()));
+
+        jsonRequest.put("message", errorMessage);
+      } catch (Exception e2) {
+        log.error(e.getMessage(), e2);
+        // do nothing, give up
+      }
+    } finally {
+      OBContext.restorePreviousMode();
+    }
+    return jsonRequest;
+  }
+
+  private boolean cleanPaymentProposalDetails(FIN_PaymentProposal paymentProposal) {
+    if (paymentProposal == null) {
+      return false;
+    } else if (paymentProposal.getFINPaymentPropDetailList().isEmpty()) {
+      // nothing to delete.
+      return true;
+    }
+    try {
+      paymentProposal.getFINPaymentPropDetailList().clear();
+      paymentProposal.setAmount(BigDecimal.ZERO);
+      paymentProposal.setWriteoffAmount(BigDecimal.ZERO);
+      OBDal.getInstance().save(paymentProposal);
+      OBDal.getInstance().flush();
+    } catch (Exception e) {
+      log.error(e.getMessage(), e);
+      return false;
+    }
+    return true;
+  }
+
+  private void createPaymentProposalDetails(JSONObject jsonRequest) throws JSONException,
+      OBException {
+
+    JSONArray selectedLines = jsonRequest.getJSONArray("_selection");
+    // if no lines selected don't do anything.
+    if (selectedLines.length() == 0) {
+      return;
+    }
+    final String strPaymentProposalId = jsonRequest.getString("Fin_Payment_Proposal_ID");
+    FIN_PaymentProposal paymentProposal = OBDal.getInstance().get(FIN_PaymentProposal.class,
+        strPaymentProposalId);
+    BigDecimal totalAmount = BigDecimal.ZERO, totalWriteOff = BigDecimal.ZERO;
+    for (int i = 0; i < selectedLines.length(); i++) {
+      JSONObject selectedLine = selectedLines.getJSONObject((int) i);
+      log.debug(selectedLine);
+      BigDecimal paidAmount = new BigDecimal(selectedLine.getString("payment"));
+
+      if (paidAmount.compareTo(BigDecimal.ZERO) != 0) {
+        FIN_PaymentPropDetail newPPD = OBProvider.getInstance().get(FIN_PaymentPropDetail.class);
+        newPPD.setOrganization(paymentProposal.getOrganization());
+        newPPD.setClient(paymentProposal.getClient());
+        newPPD.setCreatedBy(paymentProposal.getCreatedBy());
+        newPPD.setUpdatedBy(paymentProposal.getUpdatedBy());
+        newPPD.setFinPaymentProposal(paymentProposal);
+        newPPD.setFINPaymentScheduledetail(OBDal.getInstance().get(FIN_PaymentScheduleDetail.class,
+            selectedLine.getString("paymentScheduleDetail")));
+        BigDecimal difference = new BigDecimal(selectedLine.getString("difference"));
+        boolean writeOff = selectedLine.getString("writeoff").equals("true");
+        newPPD.setAmount(paidAmount);
+        totalAmount = totalAmount.add(paidAmount);
+        if (difference.compareTo(BigDecimal.ZERO) != 0 && writeOff) {
+          newPPD.setWriteoffAmount(difference);
+          totalWriteOff = totalWriteOff.add(difference);
+        }
+
+        OBDal.getInstance().save(newPPD);
+        OBDal.getInstance().save(paymentProposal);
+        OBDal.getInstance().flush();
+      }
+    }
+
+    paymentProposal.setAmount(totalAmount);
+    paymentProposal.setWriteoffAmount(totalWriteOff);
+    OBDal.getInstance().save(paymentProposal);
+  }
+}