Advertisement
tko_pb

GenerateInvoiceVendorDetail.java

Jul 18th, 2021
2,615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.21 KB | None | 0 0
  1. package org.aoi.enh.process;
  2.  
  3. import java.math.BigDecimal;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.Timestamp;
  7. import java.util.List;
  8. import java.util.logging.Level;
  9.  
  10. import org.aoi.enh.model.X_rz_invoice_fwd;
  11. import org.aoi.enh.model.X_rz_invoice_fwdline;
  12. import org.compiere.model.MBPartner;
  13. import org.compiere.model.MInOut;
  14. import org.compiere.model.MInvoice;
  15. import org.compiere.model.MInvoiceLine;
  16. import org.compiere.model.MPriceList;
  17. import org.compiere.model.MProject;
  18. import org.compiere.model.Query;
  19. import org.compiere.process.ProcessInfoParameter;
  20. import org.compiere.process.SvrProcess;
  21. import org.compiere.util.DB;
  22. import org.compiere.util.Env;
  23.  
  24.  
  25. public class GenerateInvoiceVendorDetail extends SvrProcess
  26. {
  27.   private int M_InOut_ID;
  28.   private int p_DocTypeAP_Invoice;
  29.   private int p_C_ConversionType_ID;
  30.  
  31.   protected void prepare() {
  32.       ProcessInfoParameter[] para = getParameter();
  33.       for (int i = 0; i < para.length; i++) {
  34.           String name = para[i].getParameterName();
  35.           if (name.equals("C_DocType_ID")) {
  36.               p_DocTypeAP_Invoice = para[i].getParameterAsInt();
  37.           } else if (name.equals("C_ConversionType_ID")) {
  38.               p_C_ConversionType_ID = para[i].getParameterAsInt();
  39.           } else {
  40.               log.log(Level.SEVERE, "Unknown Parameter: " + name);
  41.           }
  42.       }
  43.       M_InOut_ID = getRecord_ID();
  44.   }
  45.  
  46.   protected String doIt() throws Exception {
  47.       String msg = "";
  48.    
  49.       String InvName = "";
  50.       String InvFwd = "";
  51.       MInOut shipment = new MInOut(getCtx(), this.M_InOut_ID, get_TrxName());
  52.       StringBuffer sql = new StringBuffer();
  53.       sql.append("select distinct(invoice_fwd) as invoice_fwd from rz_invoice_fwd where IsInvoiced = 'N' AND processed = 'N' ")
  54.         .append("AND m_inout_ID = ? ");
  55.       PreparedStatement pstmt = null;
  56.       ResultSet rs = null;
  57.       try {
  58.           pstmt = DB.prepareStatement(sql.toString(), get_TrxName());
  59.           pstmt.setInt(1, this.M_InOut_ID);
  60.           rs = pstmt.executeQuery();
  61.           while (rs.next()) {
  62.               int lineNo = 0;
  63.               int PrjID = 0;
  64.               int PartnerID = 0;
  65.               int PartnerLocID = 0;
  66.               InvFwd = rs.getString("invoice_fwd");
  67.               if (InvFwd == null) msg = String.valueOf(msg) + "Invoice_Fwd is Null, ";
  68.               X_rz_invoice_fwd mfwd = (X_rz_invoice_fwd)(new Query(getCtx(), "rz_invoice_fwd", " invoice_fwd = ? AND processed='N' ", get_TrxName()))
  69.                       .setClient_ID()
  70.                       .setOnlyActiveRecords(true)
  71.                       .setParameters(new Object[] { rs.getString("invoice_fwd")})
  72.                       .first();
  73.               PrjID = mfwd.getC_Project_ID();
  74.               if (PrjID == 0) msg = String.valueOf(msg) + "Forwarder is Not Set, ";
  75.               MProject prj = new MProject(getCtx(), PrjID, get_TrxName());
  76.               PartnerID = prj.getC_BPartner_ID();
  77.               PartnerLocID = prj.getC_BPartner_Location_ID();
  78.               MBPartner bpForwarder = new MBPartner(getCtx(), PartnerID, get_TrxName());
  79.               if (PartnerID == 0) {
  80.                   msg = msg.toString() + "Bpartner Forwarder " + InvFwd + " Kosong..... ";
  81.               }
  82.               else if (PartnerLocID == 0) {
  83.                   msg = msg.toString() + "Bpartner Forwarder " + InvFwd + " PO Payment Term Not Set... ";
  84.               }
  85.        
  86.               if (msg == "") {
  87.                   MInvoice invoice = new MInvoice(getCtx(), 0, get_TrxName());
  88.                   invoice.setAD_Org_ID(shipment.getAD_Org_ID());
  89.                   invoice.setC_DocTypeTarget_ID(this.p_DocTypeAP_Invoice);
  90.                   invoice.setC_DocType_ID(this.p_DocTypeAP_Invoice);
  91.                   invoice.setIsSOTrx(false);
  92.                   invoice.setDateInvoiced(new Timestamp(System.currentTimeMillis()));
  93.                   invoice.setDateAcct(new Timestamp(System.currentTimeMillis()));
  94.                   invoice.setC_BPartner_ID(PartnerID);
  95.                   invoice.setC_BPartner_Location_ID(PartnerLocID);
  96.                   invoice.setPaymentRule("P");
  97.                   //invoice.setC_PaymentTerm_ID(bpForwarder.getPO_PaymentTerm_ID());
  98.                   invoice.setC_PaymentTerm_ID(prj.getC_PaymentTerm_ID());
  99.                   invoice.setC_ConversionType_ID(this.p_C_ConversionType_ID);
  100.                   MPriceList pl = (MPriceList)(new Query(getCtx(), "M_PriceList", " C_Currency_ID = ? AND isSOPriceList = 'N' ", get_TrxName()))
  101.                           .setClient_ID()
  102.                           .setOnlyActiveRecords(true)
  103.                           .setParameters(new Object[] { Integer.valueOf(mfwd.getC_Currency_ID())})
  104.                           .first();
  105.                   invoice.setM_PriceList_ID(pl.get_ID());
  106.                   invoice.setC_Currency_ID(mfwd.getC_Currency_ID());
  107.                   invoice.set_ValueOfColumn("kst_InvoiceVendor", InvFwd);
  108.                   invoice.setDescription("Invoice Forwarder");
  109.                   invoice.saveEx(get_TrxName());
  110.                   int fwdID = 0;
  111.          
  112.                   /* Modified by Roynaldo - 20190821 */
  113.                   //String sqlFwd = "invoice_fwd = ? AND processed = 'N' ";
  114.                   String sqlFwd = "invoice_fwd = ? AND processed = 'N' AND M_InOut_ID=?";
  115.                   /* End Modified by Roynaldo - 20190821 */
  116.                   List<X_rz_invoice_fwd> fwd = (new Query(getCtx(), "rz_invoice_fwd", sqlFwd, get_TrxName()))
  117.                           .setClient_ID()
  118.                           .setOnlyActiveRecords(true)
  119.                           /* Modified by Roynaldo - 20190821 */
  120.                           //.setParameters(new Object[] {InvFwd})
  121.                           .setParameters(new Object[] {InvFwd, M_InOut_ID})
  122.                           /* End Modified by Roynaldo - 20190821 */
  123.                           .list();
  124.                   for (X_rz_invoice_fwd m_forwarder : fwd) {
  125.                       fwdID = m_forwarder.get_ID();
  126.                       m_forwarder.setProcessed(true);
  127.                       m_forwarder.setIsInvoiced(true);
  128.                       m_forwarder.saveEx();
  129.                      
  130.                       /* Added by Roynaldo - 20200109 */
  131.                       int C_Activity_ID = m_forwarder.get_ValueAsInt("C_Activity_ID");
  132.                       int C_Campaign_ID = m_forwarder.get_ValueAsInt("C_Campaign_ID");
  133.                      
  134.                       invoice.setC_Activity_ID(C_Activity_ID);
  135.                       invoice.setC_Campaign_ID(C_Campaign_ID);
  136.                       invoice.saveEx(get_TrxName());
  137.                       /* End Added by Roynaldo - 20200109 */
  138.                      
  139.                       BigDecimal actamt = Env.ZERO;
  140.                       String sqlFwdline = " rz_invoice_fwd_ID = ?   ";
  141.            
  142.                       List<X_rz_invoice_fwdline> fwdline = (new Query(getCtx(), "rz_invoice_fwdline", sqlFwdline, get_TrxName()))
  143.                               .setClient_ID()
  144.                               .setOnlyActiveRecords(true)
  145.                               .setParameters(new Object[] { Integer.valueOf(fwdID) })
  146.                               .setOrderBy("Line ASC")
  147.                               .list();
  148.            
  149.                       for (X_rz_invoice_fwdline fwdl : fwdline) {
  150.                           actamt = actamt.add(fwdl.getActualAmt());
  151.                           MInvoiceLine invLine = new MInvoiceLine(invoice);
  152.                           lineNo++;
  153.                           invLine.setLine(lineNo * 10);
  154.                           invLine.setC_Charge_ID(fwdl.getC_Charge_ID());
  155.                           invLine.setPrice(fwdl.getActualAmt());
  156.                           invLine.setQty(Env.ONE);
  157.                           invLine.set_ValueOfColumn("rz_invoice_fwd_ID", Integer.valueOf(fwdID));
  158.                           invLine.set_ValueOfColumn("rz_invoice_fwdline_ID", Integer.valueOf(fwdl.get_ID()));                        
  159.                           /* Added by Roynaldo - 20200109 */
  160.                           invLine.setC_Activity_ID(C_Activity_ID);
  161.                           invLine.setC_Campaign_ID(C_Campaign_ID);
  162.                           /* End Added by Roynaldo - 20200109 */
  163.                           invLine.saveEx(get_TrxName());
  164.  
  165.              
  166.                           fwdl.setProcessed(true);
  167.                           fwdl.saveEx();
  168.                       }
  169.                   }
  170.                   InvName = String.valueOf(InvName) + invoice.getDocumentNo() + " ( " + lineNo + " Lines ), ";
  171.               }
  172.           }
  173.       } catch (Exception e) {
  174.           log.log(Level.WARNING, e.toString());
  175.       } finally {
  176.           DB.close(rs, pstmt);
  177.           pstmt = null;
  178.           rs = null;
  179.       }
  180.      
  181.       return msg.toString() + InvName;
  182.   }
  183. }
  184.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement