1 14 package org.compiere.process; 15 16 import java.sql.*; 17 import java.math.*; 18 import java.net.*; 19 20 import org.compiere.model.*; 21 import org.compiere.util.DB; 22 import org.compiere.util.Env; 23 24 30 public class ExpenseSOrder extends SvrProcess 31 { 32 35 public ExpenseSOrder() 36 { 37 super(); 38 log.info("ExpenseSOrder - Start"); 39 } 41 private int m_C_BPartner_ID = 0; 42 private Timestamp m_DateFrom = null; 43 private Timestamp m_DateTo = null; 44 private int m_noOrders = 0; 45 46 49 protected void prepare() 50 { 51 ProcessInfoParameter[] para = getParameter(); 52 for (int i = 0; i < para.length; i++) 53 { 54 String name = para[i].getParameterName(); 55 if (para[i].getParameter() == null) 56 ; 57 else if (name.equals("C_BPartner_ID")) 58 m_C_BPartner_ID = ((BigDecimal)para[i].getParameter()).intValue(); 59 else if (name.equals("DateReport")) 60 { 61 m_DateFrom = (Timestamp)para[i].getParameter(); 62 m_DateTo = (Timestamp)para[i].getParameter_To(); 63 } 64 else 65 log.error("prepare - Unknown Parameter: " + name); 66 } 67 } 69 70 75 protected String doIt() throws java.lang.Exception 76 { 77 StringBuffer sql = new StringBuffer ("SELECT e.AD_Client_ID, e.AD_Org_ID," + "el.C_BPartner_ID, e.M_Warehouse_ID," + "el.C_Project_ID, el.C_Activity_ID, el.C_Campaign_ID," + "el.M_Product_ID, el.S_ResourceAssignment_ID, el.C_UOM_ID," + "el.Qty, el.Description, el.Note, el.InvoicePrice," + "COALESCE(bp.M_PriceList_ID, e.M_PriceList_ID), " + "e.DocumentNo, el.S_TimeExpenseLine_ID " + "FROM S_TimeExpense e" 85 + " INNER JOIN S_TimeExpenseLine el ON (e.S_TimeExpense_ID=el.S_TimeExpense_ID)" 86 + " INNER JOIN C_BPartner bp ON (el.C_BPartner_ID=bp.C_BPartner_ID)" 87 + " LEFT OUTER JOIN S_ResourceAssignment ra ON (el.S_ResourceAssignment_ID=ra.S_ResourceAssignment_ID) " 88 + "WHERE el.C_OrderLine_ID IS NULL AND el.IsInvoiced='Y' AND e.Processed='Y'" 89 + " AND e.AD_Client_ID=?"); if (m_C_BPartner_ID != 0) 91 sql.append(" AND el.C_BPartner_ID=?"); if (m_DateFrom != null) 93 sql.append(" AND e.DateReport >= ?"); if (m_DateTo != null) 95 sql.append(" AND e.DateReport <= ?"); sql.append(" ORDER BY el.C_BPartner_ID, el.C_Project_ID, e.S_TimeExpense_ID, el.Line"); 97 int old_BPartner_ID = -1; 99 int old_Project_ID = -1; 100 MOrder order = null; 101 PreparedStatement pstmt = null; 103 try 104 { 105 pstmt = DB.prepareStatement(sql.toString()); 106 int par = 1; 107 pstmt.setInt(par++, getAD_Client_ID()); 108 if (m_C_BPartner_ID != 0) 109 pstmt.setInt(par++, m_C_BPartner_ID); 110 if (m_DateFrom != null) 111 pstmt.setTimestamp(par++, m_DateFrom); 112 if (m_DateTo != null) 113 pstmt.setTimestamp(par++, m_DateTo); 114 ResultSet rs = pstmt.executeQuery(); 115 while (rs.next()) { 117 int AD_Client_ID = rs.getInt(1); 119 int AS_org_ID = rs.getInt(2); 120 int C_BPartner_ID = rs.getInt(3); 122 int M_Warehouse_ID = rs.getInt(4); 123 int C_Project_ID = rs.getInt(5); 125 int C_Activity_ID = rs.getInt(6); 126 int C_Campaign_ID = rs.getInt(7); 127 int M_Product_ID = rs.getInt(8); 129 int S_ResourceAssignment_ID = rs.getInt(9); 130 int C_UOM_ID = rs.getInt(10); 131 BigDecimal Qty = rs.getBigDecimal(11); 133 String Description = rs.getString(12); 134 String Note = rs.getString(13); 135 BigDecimal InvoicePrice = rs.getBigDecimal(14); 136 int M_PriceList_ID = rs.getInt(15); 138 String DocumentNo = rs.getString(16); 140 int S_TimeExpenseLine_ID = rs.getInt(17); 141 142 143 if (C_BPartner_ID != old_BPartner_ID || C_Project_ID != old_Project_ID) 145 { 146 completeOrder (order); 147 MBPartner bp = new MBPartner (getCtx(), C_BPartner_ID); 148 log.info("doIt - New Order for " + bp + ", Project=" + C_Project_ID); 150 order = new MOrder (getCtx(), 0); 151 order.setC_DocTypeTarget_ID(MOrder.DocSubTypeSO_OnCredit); 152 order.setBPartner(bp); 154 order.setM_Warehouse_ID(M_Warehouse_ID); 155 if (C_Activity_ID != 0) 156 order.setC_Activity_ID(C_Activity_ID); 157 if (C_Campaign_ID != 0) 158 order.setC_Campaign_ID(C_Campaign_ID); 159 if (C_Project_ID != 0) 160 order.setC_Project_ID(C_Project_ID); 161 order.setM_PriceList_ID (M_PriceList_ID); 163 if (!order.save()) 165 { 166 log.error("doIt - cannot save Order"); 167 order = null; 168 break; 169 } 170 old_BPartner_ID = C_BPartner_ID; 171 old_Project_ID = C_Project_ID; 172 } 173 MOrderLine ol = new MOrderLine (order); 175 if (M_Product_ID != 0) 177 ol.setM_Product_ID(M_Product_ID); 178 if (S_ResourceAssignment_ID != 0) 179 ol.setS_ResourceAssignment_ID(S_ResourceAssignment_ID); 180 ol.setQtyOrdered(Qty); 181 ol.setDescription(Description); 182 if (InvoicePrice != null && InvoicePrice.compareTo(Env.ZERO) != 0) 184 ol.setPriceActual(InvoicePrice); 185 else 186 ol.setPrice(); 187 if (C_UOM_ID != 0 && ol.getC_UOM_ID() == 0) 188 ol.setC_UOM_ID(C_UOM_ID); 189 ol.setTax(); 190 if (!ol.save()) 191 { 192 log.error ("doIt - cannot save OrderLine"); 193 order = null; 194 break; 195 } 196 sql = new StringBuffer ("UPDATE S_TimeExpenseLine SET C_OrderLine_ID=") 198 .append(ol.getID()) 199 .append(" WHERE S_TimeExpenseLine_ID=").append(S_TimeExpenseLine_ID); 200 int no = DB.executeUpdate(sql.toString()); 201 if (no == 1) 202 log.debug("doIt Updated - S_TimeExpenseLine_ID=" + S_TimeExpenseLine_ID 203 + " with C_OrderLine_ID=" + ol.getID()); 204 else 205 log.error("doIt Not Updated #" + no 206 + " - S_TimeExpenseLine_ID=" + S_TimeExpenseLine_ID 207 + " with C_OrderLine_ID=" + ol.getID()); 208 } rs.close(); 210 pstmt.close(); 211 pstmt = null; 212 } 213 catch (Exception e) 214 { 215 log.error("doIt", e); 216 } 217 finally 218 { 219 try 220 { 221 if (pstmt != null) 222 pstmt.close (); 223 } 224 catch (Exception e) 225 {} 226 pstmt = null; 227 } 228 completeOrder (order); 229 230 return "@Created@=" + m_noOrders; 231 } 233 237 private void completeOrder (MOrder order) 238 { 239 if (order == null) 240 return; 241 m_noOrders++; 242 addLog (order.getID(), order.getDateOrdered(), order.getGrandTotal(), order.getDocumentNo()); 243 } 245 } | Popular Tags |