tko_pb

ProcessApprovingSalesOrder 7 nov

Nov 6th, 2018
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.70 KB | None | 0 0
  1. package org.infinite.retail.process;
  2.  
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8.  
  9. import org.apache.log4j.Logger;
  10. import org.hibernate.criterion.Restrictions;
  11. import org.openbravo.base.exception.OBException;
  12. import org.openbravo.base.secureApp.VariablesSecureApp;
  13. import org.openbravo.dal.core.OBContext;
  14. import org.openbravo.dal.service.OBCriteria;
  15. import org.openbravo.dal.service.OBDal;
  16. import org.openbravo.database.ConnectionProvider;
  17. import org.openbravo.erpCommon.utility.OBError;
  18. import org.openbravo.erpCommon.utility.OBMessageUtils;
  19. import org.openbravo.erpCommon.utility.Utility;
  20. import org.openbravo.model.ad.domain.Preference;
  21. import org.openbravo.model.ad.process.ProcessInstance;
  22. import org.openbravo.model.ad.system.Client;
  23. import org.openbravo.model.ad.ui.Process;
  24. import org.openbravo.model.common.enterprise.Organization;
  25. import org.openbravo.model.common.order.Order;
  26. import org.openbravo.scheduling.ProcessBundle;
  27. import org.openbravo.scheduling.ProcessLogger;
  28. import org.openbravo.service.db.CallProcess;
  29. import org.openbravo.service.db.DalBaseProcess;
  30. import org.openbravo.service.db.DalConnectionProvider;
  31.  
  32. public class ProcessApprovingSalesOrder extends DalBaseProcess {
  33.     private final String doc_status_to = "CO";
  34.     private final String draftStatus="DR";
  35.     private final String orderProcessID="104";
  36.     private final String closedStatus="CL";
  37.     private final String voidStatus="VO";
  38.  
  39.     final ConnectionProvider conn = new DalConnectionProvider();
  40.     protected Logger log4j = Logger.getLogger(this.getClass());
  41.     private ProcessLogger logger;
  42.  
  43.     @Override
  44.     protected void doExecute(ProcessBundle bundle) throws Exception {
  45.         // get logger
  46.         logger = bundle.getLogger();
  47.         logger.log("Starting background Process Approving Sales Order \n");
  48.  
  49.         //load unprocessed list Order
  50.         VariablesSecureApp varsAux = bundle.getContext().toVars();
  51.         String org = bundle.getContext().getOrganization();
  52.         Organization orgId = OBDal.getInstance().get(Organization.class, org);
  53.         String clientId = orgId.getClient().getId();
  54.  
  55.         String WhereClause = "POS_order_pending_complete_whereclause";
  56.         Client client = orgId.getClient();
  57.  
  58.         OBCriteria<Preference> preferenceCriteria = OBDal.getInstance().createCriteria(Preference.class);
  59.         preferenceCriteria.add(Restrictions.eq(Preference.PROPERTY_ATTRIBUTE, WhereClause));
  60.         preferenceCriteria.add(Restrictions.eq(Preference.PROPERTY_CLIENT, client));
  61.         Preference posOrderWhereClausea = preferenceCriteria.list().get(0);
  62.         String posOrderWhereClause = posOrderWhereClausea.getSearchKey();
  63.  
  64.         List<String> orderList = getOrderList(posOrderWhereClause, clientId);
  65.  
  66.         for (String order : orderList) {
  67.             Order orderId  = OBDal.getInstance().get(Order.class, order);  
  68.  
  69.             String docstatus=orderId.getDocumentStatus();
  70.             String docaction=orderId.getDocumentAction();
  71.  
  72.             //cek apakah dari CL atau VO, jika ya, maka exception
  73.             if (orderId.getDocumentStatus().equalsIgnoreCase(closedStatus)||
  74.                     orderId.getDocumentStatus().equalsIgnoreCase(voidStatus))
  75.                 continue;
  76.             orderId.setDocumentStatus(draftStatus);
  77.             orderId.setDocumentAction(doc_status_to);
  78.             OBDal.getInstance().save(orderId);
  79.             OBDal.getInstance().flush();
  80.             // call store procedure
  81.             OBError oberror = doExecuteProcedureCall(orderId.getId(), orderProcessID);
  82.             String pesan = oberror.getMessage();
  83.             if (pesan == null || pesan.isEmpty()) {
  84.                 pesan = "aproved to completed";
  85.             }
  86.             logger.log("Sales Order Document No:" + orderId.getDocumentNo() + " - " + pesan + "\n");
  87.  
  88.             if (oberror.getType().equalsIgnoreCase("Error")){  
  89.                 OBDal.getInstance().refresh(orderId);
  90.                 orderId.setDocumentStatus(docstatus);
  91.                 orderId.setDocumentAction(docaction);
  92.                 OBDal.getInstance().save(orderId);
  93.                 OBDal.getInstance().flush();
  94.                 oberror.setType("Error");
  95.                 oberror.setTitle("Error");
  96.                 oberror.setMessage(pesan);
  97.                 bundle.setResult(oberror);
  98.             }
  99.         }
  100.     }
  101.  
  102.     private List<String> getOrderList(String posOrderWhereClause, String clientId) {
  103.         List<String> output = new ArrayList<>();
  104.         if(posOrderWhereClause == null) {
  105.             posOrderWhereClause = "(1=1)";
  106.         }
  107.  
  108.         String sql = " select e.c_order_id as orderId" +
  109.                 " from c_order e" +
  110.                 " inner join c_doctype as b on b.c_doctype_id = e.c_doctypetarget_id" +
  111.                 " where e.issotrx ='Y'" +
  112.                 " and e.processed ='N'" +
  113.                 " and b.docbasetype ='SOO'" +
  114.                 " and b.docsubtypeso ='WR' " +
  115.                 " and e.ad_client_id = ?" +
  116.                 " and "+posOrderWhereClause;  
  117.  
  118.         java.sql.Connection conn = OBDal.getInstance().getConnection();
  119.         try {
  120.             PreparedStatement ps = conn.prepareStatement(sql);
  121.             ps.setString(1, clientId );
  122.             ResultSet rs = ps.executeQuery();
  123.             while(rs.next())
  124.             {
  125.                 String orderId = rs.getString("orderID");
  126.                 output.add(orderId);
  127.             }
  128.             return output;
  129.         }
  130.         catch (Exception e) {
  131.             e.printStackTrace();
  132.             throw new OBException(e);
  133.         }
  134.     }
  135.  
  136.     private OBError doExecuteProcedureCall(String recordID, String processID){
  137.         OBError oberror = new OBError();
  138.         oberror.setType("Success");
  139.         oberror.setTitle("Success");
  140.         OBContext.setAdminMode();
  141.         final Process process = OBDal.getInstance().get(Process.class, processID);
  142.         log4j.debug("execute procedure call "+process.getName());
  143.         final ProcessInstance pInstance = CallProcess.getInstance().call(process, recordID, null);
  144.         long result = pInstance.getResult();
  145.         if (result==0){
  146.             String errormessage = pInstance.getErrorMsg();
  147.             log4j.debug("error message "+errormessage);
  148.             oberror.setType("Error");
  149.             oberror.setTitle("Error");
  150.             oberror.setMessage(errormessage);
  151.         }
  152.         OBContext.restorePreviousMode();
  153.         return oberror;
  154.     }
  155.  
  156. }
Add Comment
Please, Sign In to add comment