Advertisement
tko_pb

ProcessApprovingSalesOrder

Oct 30th, 2018
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.05 KB | None | 0 0
  1. package org.infinite.retail.process;
  2.  
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7.  
  8. import org.apache.log4j.Logger;
  9. import org.openbravo.base.exception.OBException;
  10. import org.openbravo.base.secureApp.VariablesSecureApp;
  11. import org.openbravo.dal.core.OBContext;
  12. import org.openbravo.dal.service.OBDal;
  13. import org.openbravo.erpCommon.utility.OBError;
  14. import org.openbravo.erpCommon.utility.Utility;
  15. import org.openbravo.model.ad.process.ProcessInstance;
  16. import org.openbravo.model.ad.ui.Process;
  17. import org.openbravo.model.common.enterprise.Organization;
  18. import org.openbravo.model.common.order.Order;
  19. import org.openbravo.scheduling.ProcessBundle;
  20. import org.openbravo.service.db.CallProcess;
  21. import org.openbravo.service.db.DalBaseProcess;
  22.  
  23. public class ProcessApprovingSalesOrder extends DalBaseProcess {
  24.     private final String doc_status_to = "CO";
  25.     private final String draftStatus="DR";
  26.     private final String orderProcessID="104";
  27.     private final String closedStatus="CL";
  28.     private final String voidStatus="VO";
  29.  
  30.     protected Logger log4j = Logger.getLogger(this.getClass());
  31.     @Override
  32.     protected void doExecute(ProcessBundle bundle) throws Exception {
  33.         // TODO Auto-generated method stub
  34.  
  35.         //load unprocessed list Order
  36.         VariablesSecureApp varsAux = bundle.getContext().toVars();
  37.         String org = bundle.getContext().getOrganization();
  38.         Organization orgId = OBDal.getInstance().get(Organization.class, org);
  39.         String clientId = orgId.getClient().getId();
  40.  
  41.         String posOrderWhereClause = Utility.getPreference(varsAux, "POS_order_pending_complete_whereclause", null);
  42.         List<String> orderList = getOrderList(posOrderWhereClause, clientId);
  43.  
  44.         for (String order : orderList) {
  45.             Order orderId  = OBDal.getInstance().get(Order.class, order);      
  46.             //cek apakah dari CL atau VO, jika ya, maka exception
  47.             if (orderId.getDocumentStatus().equalsIgnoreCase(closedStatus)||
  48.                     orderId.getDocumentStatus().equalsIgnoreCase(voidStatus))
  49.                 continue;
  50.             orderId.setDocumentStatus(draftStatus);
  51.             orderId.setDocumentAction(doc_status_to);
  52.             OBDal.getInstance().save(orderId);
  53.             OBDal.getInstance().flush();
  54.             // call store procedure
  55.             doExecuteProcedureCall(orderId.getId(), orderProcessID);
  56.         }
  57.     }
  58.  
  59.     private List<String> getOrderList(String posOrderWhereClause, String clientId) {
  60.         if(posOrderWhereClause.isEmpty()) {
  61.             posOrderWhereClause = "(1=1)";
  62.         }
  63.         String sql = " select e.c_order_id as orderId" +
  64.                 " from c_order e" +
  65.                 " inner join c_doctype as b on b.c_doctype_id = e.c_doctypetarget_id" +
  66.                 " where e.issotrx ='Y'" +
  67.                 " and e.processed ='N'" +
  68.                 " and b.docbasetype ='SOO'" +
  69.                 " and b.docsubtypeso ='WR' " +
  70.                 " and e.ad_client_id = ?" +
  71.                 " and ?";  
  72.  
  73.         java.sql.Connection conn = OBDal.getInstance().getConnection();
  74.         ArrayList<String> output = new ArrayList<>();
  75.         try {
  76.             PreparedStatement ps = conn.prepareStatement(sql);
  77.             ps.setString(1, clientId );
  78.             ps.setString(2, posOrderWhereClause);
  79.             ResultSet rs = ps.executeQuery();
  80.             while(rs.next())
  81.             {
  82.                 String orderId = rs.getString("orderID");
  83.                 System.out.println(orderId);
  84.                 output.add(orderId);
  85.             }
  86.             return output;
  87.         }
  88.         catch (Exception e) {
  89.             e.printStackTrace();
  90.             throw new OBException(e);
  91.         }
  92.  
  93.     }
  94.  
  95.     private OBError doExecuteProcedureCall(String recordID, String processID){
  96.         OBError oberror = new OBError();
  97.         oberror.setType("Success");
  98.         oberror.setTitle("Success");
  99.         OBContext.setAdminMode();
  100.         final Process process = OBDal.getInstance().get(Process.class, processID);
  101.         log4j.debug("execute procedure call "+process.getName());
  102.         final ProcessInstance pInstance = CallProcess.getInstance().call(process, recordID, null);
  103.         long result = pInstance.getResult();
  104.         if (result==0){
  105.             String errormessage = pInstance.getErrorMsg();
  106.             log4j.debug("error message "+errormessage);
  107.             oberror.setType("Error");
  108.             oberror.setTitle("Error");
  109.             oberror.setMessage(errormessage);
  110.         }
  111.         OBContext.restorePreviousMode();
  112.         return oberror;
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement