Advertisement
tko_pb

ProcessApprovingSalesOrder 5 november

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