1 14 package org.compiere.process; 15 16 import java.math.*; 17 import java.util.*; 18 import java.sql.*; 19 20 import org.compiere.model.*; 21 import org.compiere.util.*; 22 23 29 public class ProjectGenPO extends SvrProcess 30 { 31 32 private int m_C_Project_ID = 0; 33 34 private int m_C_ProjectLine_ID = 0; 35 36 private boolean m_ConsolidateDocument = true; 37 38 private ArrayList m_pos = new ArrayList(); 39 40 43 protected void prepare() 44 { 45 ProcessInfoParameter[] para = getParameter(); 46 for (int i = 0; i < para.length; i++) 47 { 48 String name = para[i].getParameterName(); 49 if (para[i].getParameter() == null) 50 ; 51 else if (name.equals("C_Project_ID")) 52 m_C_Project_ID = ((BigDecimal)para[i].getParameter()).intValue(); 53 else if (name.equals("C_ProjectLine_ID")) 54 m_C_ProjectLine_ID = ((BigDecimal)para[i].getParameter()).intValue(); 55 else if (name.equals("ConsolidateDocument")) 56 m_ConsolidateDocument = "Y".equals(para[i].getParameter()); 57 else 58 log.error("prepare - Unknown Parameter: " + name); 59 } 60 } 62 67 protected String doIt() throws Exception 68 { 69 log.info("doIt - C_Project_ID=" + m_C_Project_ID + " - C_ProjectLine_ID=" + m_C_ProjectLine_ID + " - Consolidate=" + m_ConsolidateDocument); 70 if (m_C_ProjectLine_ID != 0) 71 createPO (new MProjectLine(getCtx(), m_C_ProjectLine_ID)); 72 else 73 { 74 MProject project = new MProject (getCtx(), m_C_Project_ID); 75 MProjectLine[] lines = project.getLines(); 76 for (int i = 0; i < lines.length; i++) 77 createPO (lines[i]); 78 } 79 return ""; 80 } 82 86 private void createPO (MProjectLine projectLine) 87 { 88 if (projectLine.getM_Product_ID() == 0) 89 { 90 addLog (projectLine.getLine() ,null,null, "Line has no Product"); 91 return; 92 } 93 if (projectLine.getC_OrderPO_ID() != 0) 94 { 95 addLog (projectLine.getLine() ,null,null, "Line was ordered previously"); 96 return; 97 } 98 99 X_M_Product_PO po = getMProduct_PO(projectLine.getM_Product_ID()); 101 if (po == null) 102 { 103 addLog (projectLine.getLine() ,null,null, "Product has no PO record"); 104 return; 105 } 106 107 BigDecimal poPrice = projectLine.getPlannedPrice(); 109 if (poPrice == null || poPrice.compareTo(Env.ZERO) == 0) 110 poPrice = po.getPricePO(); 111 if (poPrice == null || poPrice.compareTo(Env.ZERO) == 0) 112 poPrice = po.getPriceLastPO(); 113 if (poPrice == null || poPrice.compareTo(Env.ZERO) == 0) 114 poPrice = po.getPriceList(); 115 116 MOrder order = null; 118 for (int i = 0; i < m_pos.size(); i++) 120 { 121 MOrder test = (MOrder)m_pos.get(i); 122 if (test.getC_BPartner_ID() == po.getC_BPartner_ID()) 123 { 124 order = test; 125 break; 126 } 127 } 128 if (order == null) { 130 MBPartner bp = new MBPartner (getCtx(), po.getC_BPartner_ID()); 132 order = new MOrder (getCtx (), 0); 134 order.setClientOrg (projectLine.getAD_Client_ID (), projectLine.getAD_Org_ID ()); 135 order.setIsSOTrx (false); 136 order.setBPartner (bp); 137 order.setC_DocTypeTarget_ID (); 138 order.save (); 139 if (m_ConsolidateDocument) 141 m_pos.add(order); 142 } 143 144 MOrderLine orderLine = new MOrderLine (order); 146 orderLine.setM_Product_ID(projectLine.getM_Product_ID()); 147 orderLine.setQtyOrdered(projectLine.getPlannedQty()); 148 orderLine.setPrice(); 149 if (poPrice != null && poPrice.compareTo(Env.ZERO) != 0) 150 orderLine.setPriceActual(poPrice); 151 orderLine.setDescription(projectLine.getDescription()); 152 orderLine.setTax(); 153 orderLine.save(); 154 155 projectLine.setC_OrderPO_ID(order.getC_Order_ID()); 157 projectLine.save(); 158 addLog (projectLine.getLine(), null, projectLine.getPlannedQty(), order.getDocumentNo()); 159 } 161 162 167 private X_M_Product_PO getMProduct_PO (int M_Product_ID) 168 { 169 X_M_Product_PO retValue = null; 170 String sql = "SELECT * FROM M_Product_PO WHERE M_Product_ID=? ORDER BY IsCurrentVendor DESC"; 171 PreparedStatement pstmt = null; 172 try 173 { 174 pstmt = DB.prepareStatement (sql); 175 pstmt.setInt (1, M_Product_ID); 176 ResultSet rs = pstmt.executeQuery (); 177 if (rs.next ()) 178 retValue = new X_M_Product_PO (getCtx(), rs); 179 rs.close (); 180 pstmt.close (); 181 pstmt = null; 182 } 183 catch (SQLException ex) 184 { 185 log.error ("getMProduct_PO", ex); 186 } 187 try 188 { 189 if (pstmt != null) 190 pstmt.close (); 191 } 192 catch (SQLException ex1) 193 { 194 } 195 pstmt = null; 196 return retValue; 197 } 199 } | Popular Tags |