1 14 package org.compiere.model; 15 16 import java.sql.*; 17 import java.util.*; 18 19 import org.compiere.util.*; 20 21 27 public class MProject extends X_C_Project 28 { 29 public MProject (Properties ctx, int C_Project_ID) 30 { 31 super (ctx, C_Project_ID); 32 if (C_Project_ID == 0) 33 { 34 setCommittedAmt (Env.ZERO); 38 setCommittedQty (Env.ZERO); 39 setInvoicedAmt (Env.ZERO); 40 setInvoicedQty (Env.ZERO); 41 setPlannedAmt (Env.ZERO); 42 setPlannedMarginAmt (Env.ZERO); 43 setPlannedQty (Env.ZERO); 44 setProjectBalanceAmt (Env.ZERO); 45 setIsCommitCeiling (false); 47 setIsCommitment (false); 48 setIsSummary (false); 49 setProcessed (false); 50 } 51 } 53 public MProject (Properties ctx, ResultSet rs) 54 { 55 super (ctx, rs); 56 } 58 59 private int m_M_PriceList_ID = 0; 60 61 65 public int getC_ProjectType_IDasInt() 66 { 67 String pj = super.getC_ProjectType_ID(); 68 if (pj == null) 69 return 0; 70 int C_ProjectType_ID = 0; 71 try 72 { 73 C_ProjectType_ID = Integer.parseInt (pj); 74 } 75 catch (Exception ex) 76 { 77 log.error("getC_ProjectType_IDasInt - " + pj, ex); 78 } 79 return C_ProjectType_ID; 80 } 82 86 public void setC_ProjectType_ID (int C_ProjectType_ID) 87 { 88 if (C_ProjectType_ID == 0) 89 super.setC_ProjectType_ID (null); 90 else 91 super.setC_ProjectType_ID (String.valueOf(C_ProjectType_ID)); 92 } 94 98 public String toString() 99 { 100 StringBuffer sb = new StringBuffer ("MProject[").append(getID()) 101 .append("-").append(getValue()).append(",ProjectCategory=").append(getProjectCategory()) 102 .append("]"); 103 return sb.toString(); 104 } 106 110 public int getM_PriceList_ID() 111 { 112 if (getM_PriceList_Version_ID() == 0) 113 return 0; 114 if (m_M_PriceList_ID > 0) 115 return m_M_PriceList_ID; 116 String sql = "SELECT M_PriceList_ID FROM M_PriceList_Version WHERE M_PriceList_Version_ID=?"; 118 m_M_PriceList_ID = DB.getSQLValue(sql, getM_PriceList_Version_ID()); 119 return m_M_PriceList_ID; 120 } 122 126 public void setM_PriceList_Version_ID (int M_PriceList_Version_ID) 127 { 128 super.setM_PriceList_Version_ID(M_PriceList_Version_ID); 129 m_M_PriceList_ID = 0; } 132 133 134 135 139 public MProjectLine[] getLines() 140 { 141 ArrayList list = new ArrayList(); 142 String sql = "SELECT * FROM C_ProjectLine WHERE C_Project_ID=? ORDER BY Line"; 143 PreparedStatement pstmt = null; 144 try 145 { 146 pstmt = DB.prepareStatement(sql); 147 pstmt.setInt(1, getC_Project_ID()); 148 ResultSet rs = pstmt.executeQuery(); 149 while (rs.next()) 150 list.add(new MProjectLine (getCtx(), rs)); 151 rs.close(); 152 pstmt.close(); 153 pstmt = null; 154 } 155 catch (SQLException ex) 156 { 157 log.error("getLines", ex); 158 } 159 try 160 { 161 if (pstmt != null) 162 pstmt.close(); 163 } 164 catch (SQLException ex1) 165 { 166 } 167 pstmt = null; 168 MProjectLine[] retValue = new MProjectLine[list.size()]; 170 list.toArray(retValue); 171 return retValue; 172 } 174 178 public MProjectIssue[] getIssues() 179 { 180 ArrayList list = new ArrayList(); 181 String sql = "SELECT * FROM C_ProjectIssue WHERE C_Project_ID=? ORDER BY Line"; 182 PreparedStatement pstmt = null; 183 try 184 { 185 pstmt = DB.prepareStatement(sql); 186 pstmt.setInt(1, getC_Project_ID()); 187 ResultSet rs = pstmt.executeQuery(); 188 while (rs.next()) 189 list.add(new MProjectIssue (getCtx(), rs)); 190 rs.close(); 191 pstmt.close(); 192 pstmt = null; 193 } 194 catch (SQLException ex) 195 { 196 log.error("getIssues", ex); 197 } 198 try 199 { 200 if (pstmt != null) 201 pstmt.close(); 202 } 203 catch (SQLException ex1) 204 { 205 } 206 pstmt = null; 207 MProjectIssue[] retValue = new MProjectIssue[list.size()]; 209 list.toArray(retValue); 210 return retValue; 211 } 213 217 public MProjectPhase[] getPhases() 218 { 219 ArrayList list = new ArrayList(); 220 String sql = "SELECT * FROM C_ProjectPhase WHERE C_Project_ID=? ORDER BY SeqNo"; 221 PreparedStatement pstmt = null; 222 try 223 { 224 pstmt = DB.prepareStatement(sql); 225 pstmt.setInt(1, getC_Project_ID()); 226 ResultSet rs = pstmt.executeQuery(); 227 while (rs.next()) 228 list.add(new MProjectPhase (getCtx(), rs)); 229 rs.close(); 230 pstmt.close(); 231 pstmt = null; 232 } 233 catch (SQLException ex) 234 { 235 log.error("getPhases", ex); 236 } 237 try 238 { 239 if (pstmt != null) 240 pstmt.close(); 241 } 242 catch (SQLException ex1) 243 { 244 } 245 pstmt = null; 246 MProjectPhase[] retValue = new MProjectPhase[list.size()]; 248 list.toArray(retValue); 249 return retValue; 250 } 252 253 254 259 public int copyDetailsFrom (MProject project) 260 { 261 if (isProcessed() || project == null) 262 return 0; 263 int count = copyLinesFrom(project) 264 + copyPhasesFrom(project); 265 return count; 266 } 268 273 public int copyLinesFrom (MProject project) 274 { 275 if (isProcessed() || project == null) 276 return 0; 277 int count = 0; 278 MProjectLine[] fromLines = project.getLines(); 279 for (int i = 0; i < fromLines.length; i++) 280 { 281 MProjectLine line = new MProjectLine (getCtx(), 0); 282 PO.copyValues(fromLines[i], line, getAD_Client_ID(), getAD_Org_ID()); 283 line.setC_Project_ID(getC_Project_ID()); 284 line.setInvoicedAmt(Env.ZERO); 285 line.setInvoicedQty(Env.ZERO); 286 line.setC_OrderPO_ID(0); 287 line.setC_Order_ID(0); 288 if (line.save()) 289 count++; 290 } 291 if (fromLines.length != count) 292 log.error("copyLinesFrom - Lines difference - Project=" + fromLines.length + " <> Saved=" + count); 293 294 return count; 295 } 297 302 public int copyPhasesFrom (MProject fromProject) 303 { 304 if (isProcessed() || fromProject == null) 305 return 0; 306 int count = 0; 307 int taskCount = 0; 308 MProjectPhase[] myPhases = getPhases(); 310 MProjectPhase[] fromPhases = fromProject.getPhases(); 311 for (int i = 0; i < fromPhases.length; i++) 313 { 314 int C_Phase_ID = fromPhases[i].getC_Phase_ID(); 316 boolean exists = false; 317 if (C_Phase_ID == 0) 318 exists = false; 319 else 320 { 321 for (int ii = 0; ii < myPhases.length; ii++) 322 { 323 if (myPhases[ii].getC_Phase_ID() == C_Phase_ID) 324 { 325 exists = true; 326 break; 327 } 328 } 329 } 330 if (exists) 332 log.info("copyPhasesFrom - Phase already exists here, ignored - " + fromPhases[i]); 333 else 334 { 335 MProjectPhase toPhase = new MProjectPhase (getCtx (), 0); 336 PO.copyValues (fromPhases[i], toPhase, getAD_Client_ID (), getAD_Org_ID ()); 337 toPhase.setC_Project_ID (getC_Project_ID ()); 338 toPhase.setC_Order_ID (0); 339 toPhase.setIsComplete (false); 340 if (toPhase.save ()) 341 { 342 count++; 343 taskCount += toPhase.copyTasksFrom (fromPhases[i]); 344 } 345 } 346 } 347 if (fromPhases.length != count) 348 log.warn("copyPhasesFrom - Count difference - Project=" + fromPhases.length + " <> Saved=" + count); 349 350 return count + taskCount; 351 } 353 354 359 public void setProjectType (MProjectType type) 360 { 361 if (type == null) 362 return; 363 setC_ProjectType_ID(type.getC_ProjectType_ID()); 364 setProjectCategory(type.getProjectCategory()); 365 if (PROJECTCATEGORY_ServiceChargeProject.equals(getProjectCategory())) 366 copyPhasesFrom(type); 367 } 369 374 public int copyPhasesFrom (MProjectType type) 375 { 376 int count = 0; 378 int taskCount = 0; 379 MProjectTypePhase[] typePhases = type.getPhases(); 380 for (int i = 0; i < typePhases.length; i++) 381 { 382 MProjectPhase toPhase = new MProjectPhase (getCtx(), getC_Project_ID(), typePhases[i]); 383 if (toPhase.save()) 384 { 385 count++; 386 taskCount += toPhase.copyTasksFrom(typePhases[i]); 387 } 388 } 389 if (typePhases.length != count) 390 log.error("copyPhasesFrom - Count difference - Type=" + typePhases.length + " <> Saved=" + count); 391 return count; 392 } 394 395 396 403 public static MProject copyFrom (Properties ctx, int C_Project_ID, Timestamp dateDoc) 404 { 405 MProject from = new MProject (ctx, C_Project_ID); 406 if (from.getC_Project_ID() == 0) 407 throw new IllegalArgumentException ("From Project not found C_Project_ID=" + C_Project_ID); 408 MProject to = new MProject (ctx, 0); 410 PO.copyValues(from, to, from.getAD_Client_ID(), from.getAD_Org_ID()); 411 to.setC_Project_ID(0); 412 String Value = to.getValue() + " "; 414 String Time = dateDoc.toString(); 415 int length = Value.length() + Time.length(); 416 if (length <= 40) 417 Value += Time; 418 else 419 Value += Time.substring (length-40); 420 to.setValue(Value); 421 to.setInvoicedAmt(Env.ZERO); 422 to.setProjectBalanceAmt(Env.ZERO); 423 to.setProcessed(false); 424 if (!to.save()) 426 throw new IllegalStateException ("Could not create Project"); 427 428 if (to.copyDetailsFrom(from) == 0) 429 throw new IllegalStateException ("Could not create Project Details"); 430 431 return to; 432 } 434 } | Popular Tags |