Advertisement
tko_pb

ProcessApprovingSalesOrder

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