Advertisement
tko_pb

documentRoutingHandler.java

Jan 13th, 2019
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.99 KB | None | 0 0
  1. package org.wirabumi.gen.oez.event;
  2.  
  3. import java.math.BigDecimal;
  4. import java.util.Date;
  5. import java.util.List;
  6. import java.util.Map;
  7.  
  8. import javax.servlet.http.HttpServletRequest;
  9.  
  10. import org.apache.log4j.Logger;
  11. import org.codehaus.jettison.json.JSONArray;
  12. import org.codehaus.jettison.json.JSONException;
  13. import org.codehaus.jettison.json.JSONObject;
  14. import org.hibernate.criterion.Restrictions;
  15. import org.openbravo.base.exception.OBException;
  16. import org.openbravo.base.model.Entity;
  17. import org.openbravo.base.model.ModelProvider;
  18. import org.openbravo.base.model.Property;
  19. import org.openbravo.base.secureApp.VariablesSecureApp;
  20. import org.openbravo.base.structure.BaseOBObject;
  21. import org.openbravo.client.kernel.BaseActionHandler;
  22. import org.openbravo.client.kernel.RequestContext;
  23. import org.openbravo.dal.core.OBContext;
  24. import org.openbravo.dal.service.OBCriteria;
  25. import org.openbravo.dal.service.OBDal;
  26. import org.openbravo.database.ConnectionProvider;
  27. import org.openbravo.erpCommon.utility.OBMessageUtils;
  28. import org.openbravo.erpCommon.utility.Utility;
  29. import org.openbravo.model.ad.datamodel.Table;
  30. import org.openbravo.model.ad.system.Client;
  31. import org.openbravo.model.ad.ui.Tab;
  32. import org.openbravo.model.common.currency.ConversionRate;
  33. import org.openbravo.model.common.currency.Currency;
  34. import org.openbravo.model.common.enterprise.Organization;
  35. import org.openbravo.service.db.DalConnectionProvider;
  36. import org.openbravo.service.db.DbUtility;
  37. import org.wirabumi.gen.oez.porting.GoodsMovementActionHandler;
  38.  
  39. public class DocumentRoutingHandler extends BaseActionHandler {
  40.     final static Logger log4jDocRouting = Logger.getLogger(DocumentRoutingHandler.class);
  41.     final String referenceId = "135"; //id untuk ad_reference All_Document Action
  42.  
  43.     ConnectionProvider connectionProvider = new DalConnectionProvider();
  44.  
  45.     @Override
  46.     protected JSONObject execute(Map<String, Object> parameters, String content) {
  47.         JSONObject response = new JSONObject();
  48.         HttpServletRequest request = RequestContext.get().getRequest();
  49.         VariablesSecureApp vars = new VariablesSecureApp(request);
  50.         try {
  51.             final JSONObject jsonData = new JSONObject(content);
  52.             final String action = jsonData.getString("action");
  53.             final String adTabId = jsonData.getString("adTabId");
  54.             final String windowId = jsonData.getString("windowId");
  55.             String doc_status_from = jsonData.getString("doc_status_from");
  56.             final JSONArray RecordList = jsonData.getJSONArray("recordIdList");
  57.             List<String> recordIdList=DocumentRoutingHandlerServer.parseJSON(RecordList);
  58.  
  59.             // cek apakah dalam record yang dicentang ada perbedaan docSatus, jika Ya maka exception
  60.             OBContext.setAdminMode();
  61.             doc_status_from=getCurrentStatus(windowId, adTabId, referenceId, recordIdList);
  62.             if(doc_status_from==null || doc_status_from.equals("")){
  63.                 String message = Utility.parseTranslation(connectionProvider, vars, vars.getLanguage(),
  64.                         "@OEZ_MultipleDocStatus@");
  65.                 throw new Exception(message);
  66.             }
  67.  
  68.             //set docAction, jika tidak terdefinisi di role | document routing, maka exception karena dianggap tidak memiliki otorisasi
  69.             String doc_status_to=null;
  70.             if(!action.equals("OpenPopupParamater")){
  71.                 doc_status_to=action;
  72.             }
  73.             if (action.equals("OpenPopupParamater")) {
  74.                 JSONObject comboItem=getActionComboBox(windowId, adTabId,referenceId,  recordIdList, vars,doc_status_from);
  75.                 if (comboItem!=null) {
  76.                     response.put("actionComboBox", comboItem);
  77.                 } else {
  78.                     String message = Utility.parseTranslation(connectionProvider, vars, vars.getLanguage(),
  79.                             "@oez_documentapprovaldenied@ ");
  80.                     throw new Exception(message);
  81.                 }
  82.             } else {
  83.                 DocumentRoutingHandlerServer.doRouting(windowId, adTabId, recordIdList, doc_status_to, doc_status_from, vars);
  84.                 String actionName = DocumentRoutingHandlerServer.getDocumentStatusName(referenceId, action);
  85.                 String message = Utility.parseTranslation(connectionProvider, vars, vars.getLanguage(),RecordList.length() + " @RowsUpdated@ " + actionName);
  86.                 JSONObject Message = new JSONObject();
  87.                 Message.put("severity", "success");
  88.                 Message.put("text", message);
  89.                 response.put("message", Message);
  90.                 return response;
  91.             }
  92.             OBContext.restorePreviousMode();
  93.         } catch (Exception e) {
  94.             OBDal.getInstance().rollbackAndClose();
  95.             log4jDocRouting.error("Document Action Failed: " + e.getMessage(), e);
  96.             Throwable ex = DbUtility.getUnderlyingSQLException(e);
  97.             String message = OBMessageUtils.translateError(ex.getMessage()).getMessage();
  98.             try {
  99.                 JSONObject errorMessage = new JSONObject();
  100.                 errorMessage.put("severity", "error");
  101.                 errorMessage.put("text", message);
  102.                 response.put("message", errorMessage);
  103.             } catch (JSONException ignore) {
  104.                 e.printStackTrace();
  105.             }
  106.         }
  107.         return response;
  108.     }
  109.  
  110.     private String getCurrentStatus(String windowID, String tabID, String referenceID, List<String> recordId) throws Exception{
  111.         log4jDocRouting.debug("start getCurrentStatus");
  112.         Tab tab=OBDal.getInstance().get(Tab.class, tabID);
  113.         Entity entityActive = ModelProvider.getInstance().getEntityByTableId(tab.getTable().getId());
  114.         Property docStatusProp=null;
  115.         String column="documentStatus";
  116.         log4jDocRouting.debug("get value from documentStatus column");
  117.         docStatusProp = entityActive.getProperty(column, false);
  118.         if(docStatusProp==null)
  119.         {
  120.             log4jDocRouting.debug("get value from oezDocStatus column");
  121.             column="oezDocstatus";
  122.             docStatusProp=entityActive.getProperty(column, false); 
  123.         }          
  124.         if(docStatusProp==null){
  125.             log4jDocRouting.debug("get value from user defined document status column");
  126.             Table table = tab.getTable();
  127.             String columnStatus = table.getOezDocstatuscolumn();
  128.             docStatusProp = entityActive.getProperty(columnStatus, false);
  129.             if(docStatusProp!=null){
  130.                 column=columnStatus;
  131.             }else{
  132.                 throw new Exception("Column documentStatus or oezDocstatus not found");
  133.             }
  134.  
  135.         }
  136.         BaseOBObject obObject = OBDal.getInstance().get(entityActive.toString(), recordId.get(0));           
  137.         String currentStatus= (String)obObject.get(column);
  138.         if(recordId.size()>1){
  139.             for(int i=1;i<recordId.size();i++){
  140.                 BaseOBObject ob= OBDal.getInstance().get(entityActive.toString(), recordId.get(i));
  141.                 String tm= (String)ob.get(column);
  142.                 if(!tm.equalsIgnoreCase(currentStatus)){
  143.                     return null;
  144.                 }
  145.  
  146.             }  
  147.         }
  148.  
  149.         return currentStatus;
  150.     }
  151.     private JSONObject getActionComboBox(String windowID, String tabID, String referenceID, List<String> recordId,
  152.             VariablesSecureApp vars,String currentStatus) throws Exception {
  153.         OBContext.setAdminMode();
  154.         BigDecimal maxTrxAmount = getMaxTrxAmount(tabID, recordId);
  155.         String defaultValue = null;
  156.         JSONObject response = null;
  157.         String roleID = vars.getRole();        
  158.         DocumentActionList[] docHandlerAcct = DocumentRoutingHandlerServer
  159.                 .actionData(referenceID, roleID,windowID, tabID, currentStatus, maxTrxAmount);         
  160.         if(docHandlerAcct.length>0){
  161.             JSONObject valueMap = new JSONObject();
  162.             response=new JSONObject();
  163.             for (DocumentActionList DocRouteAction : docHandlerAcct) {
  164.                 String key = DocRouteAction.getField("ID");
  165.                 String Name = DocRouteAction.getField("NAME");
  166.                 valueMap.put(key, Name);
  167.             }
  168.             response.put("valueMap", valueMap);
  169.             response.put("defaultValue", defaultValue);
  170.         }          
  171.         OBContext.restorePreviousMode();
  172.         return response;
  173.     }
  174.  
  175.     private BigDecimal getMaxTrxAmount(String tabID, List<String> recordId) {
  176.         OBContext.setAdminMode();
  177.         Tab tab=OBDal.getInstance().get(Tab.class, tabID);
  178.         Entity entityActive = ModelProvider.getInstance().getEntityByTableId(tab.getTable().getId());
  179.  
  180.         Table table = tab.getTable();
  181.         if (table.getOezTrxamtcolumn()==null)
  182.             return BigDecimal.ZERO;
  183.         String columnAmount = table.getOezTrxamtcolumn();
  184.         Property trxAmountProp = entityActive.getProperty(columnAmount, false);
  185.  
  186.         if (trxAmountProp==null)
  187.             return BigDecimal.ZERO; //kolom transaction amount di ad_table tidak didefinisikan artinya dokumen ini tidak ber-amount, return zero sebab dengan trx amount zero maka akan di-approve
  188.  
  189.         if(table.getOezTrxdatecolumn()==null) // jika null maka tidak menggunakan validasi currency
  190.             return BigDecimal.ZERO;
  191.            
  192.         BaseOBObject obObject = OBDal.getInstance().get(entityActive.toString(), recordId.get(0));           
  193.         BigDecimal trxAmount= (BigDecimal)obObject.get(columnAmount);
  194.         String trxDate = table.getOezTrxdatecolumn();
  195.  
  196.         Currency trxCurrency = (Currency)obObject.get("currency");
  197.         Date trxOrderDate = (Date)obObject.get(trxDate);
  198.         Organization currencyOrganization = (Organization)obObject.get("organization");
  199.         Currency organizationCurrency = currencyOrganization.getCurrency();
  200.         Client clientCurrency = (Client)obObject.get("client");
  201.  
  202.         if(!(trxCurrency.equals(organizationCurrency))) {
  203.             trxAmount = GetCurrencyConverter(clientCurrency, trxCurrency, trxOrderDate, trxAmount);
  204.         }
  205.  
  206.         if(recordId.size()>1){
  207.             for(int i=1;i<recordId.size();i++){
  208.                 BaseOBObject ob= OBDal.getInstance().get(entityActive.toString(), recordId.get(i));
  209.                 BigDecimal newAmount= (BigDecimal)ob.get(columnAmount);
  210.                
  211.                 Currency trxCurrency2 = (Currency)ob.get("currency");
  212.                 Date trxOrderDate2 = (Date)obObject.get(trxDate);
  213.                 Organization currencyOrganization2 = (Organization)obObject.get("organization");
  214.                 Currency organizationCurrency2 = currencyOrganization2.getCurrency();
  215.                 Client clientCurrency2 = (Client)obObject.get("client");
  216.  
  217.                 if(!(trxCurrency.equals(organizationCurrency2))) {
  218.                     newAmount = GetCurrencyConverter(clientCurrency2, trxCurrency2, trxOrderDate2, trxAmount);
  219.                 }
  220.                
  221.                 if (newAmount.compareTo(trxAmount)>0)
  222.                     trxAmount = newAmount;
  223.             }  
  224.         }
  225.  
  226.         OBContext.restorePreviousMode();
  227.         return trxAmount;
  228.     }
  229.    
  230.     private BigDecimal GetCurrencyConverter (Client clientCurrency, Currency trxCurrency,Date trxOrderDate, BigDecimal trxAmount) {
  231.        
  232.         OBCriteria<ConversionRate> conversionRate = OBDal.getInstance().createCriteria(ConversionRate.class);
  233.         conversionRate.add(Restrictions.eq(ConversionRate.PROPERTY_CLIENT, clientCurrency));
  234.         conversionRate.add(Restrictions.eq(ConversionRate.PROPERTY_TOCURRENCY, trxCurrency ));
  235.         List<ConversionRate> conversionRateList = conversionRate.list();
  236.        
  237.         String trxCurrecyIsoCode = trxCurrency.getISOCode();
  238.        
  239.         if(conversionRateList == null || conversionRateList.isEmpty()) {               
  240.             String string = String.format("conversion rate untuk %s tidak tersedia", trxCurrecyIsoCode);
  241.             throw new OBException(string);
  242.         }
  243.         int jumlahList = conversionRateList.size();
  244.         for(ConversionRate conversionRateId : conversionRateList) {
  245.             if( trxOrderDate.compareTo(conversionRateId.getValidFromDate()) >= 0 && trxOrderDate.compareTo(conversionRateId.getValidToDate()) <= 0 ) {
  246.                 trxAmount = trxAmount.multiply(conversionRateId.getMultipleRateBy());  
  247.             }
  248.             else {
  249.                 jumlahList --;
  250.             }
  251.         }
  252.         if (jumlahList == 0) {
  253.             String string = String.format("conversion rate untuk %s tidak valid Date", trxCurrecyIsoCode);
  254.             throw new OBException(string);
  255.         }
  256.        
  257.         return trxAmount;
  258.        
  259.     }
  260.  
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement