/src/mpv5/db/objects/ActivityListSubItem.java
Java | 554 lines | 395 code | 54 blank | 105 comment | 33 complexity | 82b7b2841e486ff9144b806fcac89014 MD5 | raw file
1/* 2 * This file is part of YaBS. 3 * 4 * YaBS is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * YaBS is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with YaBS. If not, see <http://www.gnu.org/licenses/>. 16 */ 17package mpv5.db.objects; 18 19import java.math.BigDecimal; 20import java.util.ArrayList; 21import java.util.Arrays; 22import java.util.Collections; 23import java.util.Comparator; 24import java.util.Date; 25import java.util.HashMap; 26import java.util.LinkedList; 27import java.util.List; 28import javax.swing.JComponent; 29import mpv5.db.common.Context; 30import mpv5.db.common.DatabaseObject; 31import mpv5.db.common.NodataFoundException; 32import mpv5.db.common.QueryCriteria; 33import mpv5.db.common.QueryHandler; 34import mpv5.globals.GlobalSettings; 35import mpv5.globals.Headers; 36import mpv5.logging.Log; 37import mpv5.utils.date.DateConverter; 38import mpv5.utils.models.MPTableModel; 39import mpv5.utils.numberformat.FormatNumber; 40import mpv5.utils.text.RandomText; 41 42/** 43 * 44 * 45 */ 46public final class ActivityListSubItem extends DatabaseObject { 47 48 private static final long serialVersionUID = 1L; 49 50 /** 51 * Save the model of SubItems 52 * @param dataOwner 53 * @param model 54 */ 55 public static void saveModel(MPTableModel model, int listid) { 56 //"Internal ID", "ID", "Date", "Count", "Description", "Netto Price", "Tax Value", "Total Price", "Product", "cname" 57 List<Object[]> rowsl = model.getValidRows(new int[]{5}); 58 Log.Debug(ActivityListSubItem.class, "Rows found: " + rowsl.size()); 59 for (int i = 0; i < rowsl.size(); i++) { 60 Object[] row = rowsl.get(i); 61 for (int j = 0; j < row.length; j++) { 62 if (row[j] == null) { 63 row[j] = ""; 64 } 65 } 66 ActivityListSubItem it = null; 67 if (row[10] instanceof ActivityListSubItem) { 68 it = (ActivityListSubItem) row[10]; 69 } else { 70 it = new ActivityListSubItem(); 71 } 72 try { 73 if (row[0] != null && Integer.valueOf(row[0].toString()).intValue() > 0) { 74 it.setIDS(Integer.valueOf(row[0].toString()).intValue()); 75 } else { 76 it.setIDS(-1); 77 } 78 } catch (Exception e) { 79 Log.Debug(ActivityListSubItem.class, e.getMessage()); 80 } 81 82 it.setActivitylistsids(listid); 83 it.setDatedoing((Date) row[2]); 84 it.setQuantityvalue(new BigDecimal(row[3].toString())); 85 it.setMeasure(row[4].toString()); 86 it.setDescription(row[5].toString()); 87 it.setInternalvalue(new BigDecimal(row[6].toString())); 88 it.setTaxpercentvalue(new BigDecimal(row[7].toString())); 89 it.setTotalbrutvalue(new BigDecimal(row[8].toString())); 90 it.setProductsids(Integer.valueOf(row[9].toString())); 91 it.setCname(new RandomText(20).getString()); 92 calculate(it); 93 94 95 if (!it.isExisting()) { 96 it.setDateadded(new Date()); 97 it.setGroupsids(mpv5.db.objects.User.getCurrentUser().__getGroupsids()); 98 } 99 it.save(true); 100 } 101 102 for (int i = 0; i < deletionQueue.size(); i++) { 103 try { 104 QueryHandler.delete(ActivityListSubItem.getObject(Context.getActivityListItems(), deletionQueue.get(i))); 105 } catch (NodataFoundException ex) { 106 Log.Debug(ex); 107 } 108 } 109 110 deletionQueue.clear(); 111 } 112 113 /** 114 * Convert a Product into a Row 115 * @param product 116 * @return 117 */ 118 public static ActivityListSubItem toRow(Product product) { 119 return new ActivityListSubItem(product); 120 } 121 private static List<Integer> deletionQueue = new ArrayList<Integer>(); 122 123 /** 124 * Mark a subitem for deletion 125 * @param valueAt INT or Entity 126 */ 127 public static void addToDeletionQueue(Object valueAt) { 128 Log.Debug(ActivityListSubItem.class, "Adding to deletionqueue: " + valueAt); 129 if (valueAt != null) { 130 try { 131 Integer isd = Integer.valueOf(valueAt.toString()); 132 deletionQueue.add(isd); 133 } catch (NumberFormatException numberFormatException) { 134 Integer isd = ((Entity) valueAt).getValue(); 135 deletionQueue.add(isd); 136 } 137 } 138 } 139 140 /** 141 * UN-Mark a subitem for deletion 142 * @param valueAt INT or Entity 143 */ 144 public static void removeFromDeletionQueue(Object valueAt) { 145 Log.Debug(ActivityListSubItem.class, "Removing from deletionqueue: " + valueAt); 146 if (valueAt != null) { 147 try { 148 Integer isd = Integer.valueOf(valueAt.toString()); 149 deletionQueue.remove(isd); 150 } catch (NumberFormatException numberFormatException) { 151 Integer isd = ((Entity) valueAt).getValue(); 152 deletionQueue.add(isd); 153 } 154 } 155 } 156 private int activitylistsids; 157 private int id; 158 private Date datedoing; 159 private BigDecimal countvalue = BigDecimal.ZERO; 160 private BigDecimal quantityvalue = BigDecimal.ZERO; 161 private String measure = ""; 162 private String description = ""; 163 private int productsids; 164 private BigDecimal internalvalue = BigDecimal.ZERO; 165 private BigDecimal taxpercentvalue = BigDecimal.ZERO; 166 private BigDecimal totalbrutvalue = BigDecimal.ZERO; 167 168 public ActivityListSubItem() { 169 setContext(Context.getActivityListItems()); 170 } 171 172 /** 173 * 174 * @return 175 */ 176 public static ActivityListSubItem getDefaultItem() { 177 ActivityListSubItem as = new ActivityListSubItem(); 178 String defunit = ""; 179 if (mpv5.db.objects.User.getCurrentUser().getProperties().hasProperty("defunit")) { 180 defunit = mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("defunit"); 181 } 182 Double deftax = 0d; 183 if (mpv5.db.objects.User.getCurrentUser().getProperties().hasProperty("deftax")) { 184 int taxid = mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("deftax", 0); 185 deftax = Tax.getTaxValue(taxid).doubleValue(); 186 } 187 Double defcount = 1d; 188 if (mpv5.db.objects.User.getCurrentUser().getProperties().hasProperty("defcount")) { 189 defcount = mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("defcount", 0d); 190 } 191 as.setMeasure(defunit); 192 as.setQuantityvalue(new BigDecimal(defcount)); 193 as.setTaxpercentvalue(BigDecimal.valueOf(deftax)); 194 return as; 195 } 196 197 /** 198 * 199 * @param o 200 */ 201 public ActivityListSubItem(Product o) { 202 this(); 203 setCname(o.__getCname()); 204 setDateadded(new Date()); 205 setDatedoing(new Date()); 206 setMeasure(o.__getMeasure()); 207 setDescription(o.__getDescription()); 208 setGroupsids(mpv5.db.objects.User.getCurrentUser().__getGroupsids()); 209 setIntaddedby(mpv5.db.objects.User.getCurrentUser().__getIDS()); 210 setProductsids(o.__getIDS()); 211 setInternalvalue(o.__getExternalnetvalue()); 212 setTaxpercentvalue(Tax.getTaxValue(o.__getTaxids())); 213 calculate(this); 214 } 215 216 public Date __getDatedoing() { 217 return datedoing; 218 } 219 220 public void setDatedoing(Date datedoing) { 221 this.datedoing = datedoing; 222 } 223 224 /** 225 * @return the originalproductsids 226 */ 227 public int __getProductsids() { 228 return productsids; 229 } 230 231 /** 232 * @param originalproductsids the originalproductsids to set 233 */ 234 public void setProductsids(int productsids) { 235 this.productsids = productsids; 236 } 237 238 /** 239 * @return the quantity 240 */ 241 public BigDecimal __getQuantityvalue() { 242 return quantityvalue; 243 } 244 245 /** 246 * @param quantity the quantity to set 247 */ 248 public void setQuantityvalue(BigDecimal quantity) { 249 this.quantityvalue = quantity; 250 } 251 252 /** 253 * @return the description 254 */ 255 public String __getDescription() { 256 return description; 257 } 258 259 /** 260 * @param description the description to set 261 */ 262 public void setDescription(String description) { 263 this.description = description; 264 } 265 266 public BigDecimal __getInternalvalue() { 267 return internalvalue; 268 } 269 270 public void setInternalvalue(BigDecimal internalvalue) { 271 this.internalvalue = internalvalue; 272 } 273 274 public BigDecimal __getTaxpercentvalue() { 275 return taxpercentvalue; 276 } 277 278 public void setTaxpercentvalue(BigDecimal taxpercentvalue) { 279 this.taxpercentvalue = taxpercentvalue; 280 } 281 282 public BigDecimal __getTotalbrutvalue() { 283 return totalbrutvalue; 284 } 285 286 public void setTotalbrutvalue(BigDecimal totalbrutvalue) { 287 this.totalbrutvalue = totalbrutvalue; 288 } 289 290 /** 291 * @return the productlistsids 292 */ 293 public int __getActivitylistsids() { 294 return activitylistsids; 295 } 296 297 /** 298 * @param productlistsids the productlistsids to set 299 */ 300 public void setActivitylistsids(int activitylistsids) { 301 this.activitylistsids = activitylistsids; 302 } 303 304 public int getId() { 305 return id; 306 } 307 308 public void setId(int id) { 309 this.id = id; 310 } 311 312 public String __getMeasure() { 313 return measure; 314 } 315 316 public void setMeasure(String measure) { 317 this.measure = measure; 318 } 319 320 @Override 321 public JComponent getView() { 322 return null; 323 } 324 325 @Override 326 public mpv5.utils.images.MPIcon getIcon() { 327 return null; 328 } 329 330 /** 331 * Generates a table model out of the given SubItems 332 * @param items 333 * @return 334 */ 335 public static MPTableModel toModel(ActivityListSubItem[] items) { 336 Object[][] data = new Object[items.length][11]; 337 for (int i = 0; i < data.length; i++) { 338 data[i] = items[i].getRowData(i + 1); 339 } 340 MPTableModel model = new MPTableModel( 341 new Class[]{ 342 Integer.class, // "Internal ID", 343 Integer.class, // "ID", 344 Date.class, // "Date", 345 Double.class, // "Count", 346 String.class, // measure 347 String.class, // "Description", 348 BigDecimal.class, // "Netto Price", 349 BigDecimal.class, // "Tax Value", 350 BigDecimal.class, // "Total Price", 351 Integer.class, // "Product", 352 String.class, // "cname" 353 ActivityListSubItem.class // "Object" 354 }, 355 new boolean[]{ 356 false, // "Internal ID", 357 false, // "ID", 358 true, // "Date", 359 true, // "Count", 360 true, // measure 361 true, // "Description", 362 true, // "Netto Price", 363 true, // "Tax Value", 364 true, // "Total Price", 365 false, // "Product", 366 false, // "cname" 367 false}, // Object 368 data, 369 Headers.ACTIVITY.getValue()); 370 model.setContext(Context.getActivityListItems()); 371 String defunit = ""; 372 if (mpv5.db.objects.User.getCurrentUser().getProperties().hasProperty("defunit")) { 373 defunit = mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("defunit"); 374 } 375 Double deftax = 0d; 376 if (mpv5.db.objects.User.getCurrentUser().getProperties().hasProperty("deftax")) { 377 int taxid = mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("deftax", 0); 378 deftax = Tax.getTaxValue(taxid).doubleValue(); 379 } 380 Double defcount = 1d; 381 if (mpv5.db.objects.User.getCurrentUser().getProperties().hasProperty("defcount")) { 382 defcount = mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("defcount", 0d); 383 } 384 model.defineRow(new Object[]{0, 0, null, defcount, defunit, null, 0.0, deftax, 0.0, null, null}); 385 model.setAutoCountColumn(1); 386 387 return model; 388 } 389 390 /** 391 * Turn this SubItem into table row data 392 * @param row 393 * @return 394 */ 395 public synchronized Object[] getRowData(int row) { 396 //"Internal ID", "ID", "Date", "Count", "Measure", "Description", "Netto Price", "Tax Value", "Total Price", "Product", "cname" 397 Object[] data = new Object[12]; 398 data[0] = __getIDS(); 399 data[1] = Integer.valueOf(row); 400 data[2] = __getDatedoing(); 401 data[3] = __getQuantityvalue(); 402 data[4] = __getMeasure(); 403 data[5] = __getDescription(); 404 data[6] = __getInternalvalue(); 405 data[7] = __getTaxpercentvalue(); 406 data[8] = __getTotalbrutvalue(); 407 data[9] = Integer.valueOf(__getProductsids()); 408 data[10] = __getCname(); 409 if (!__getDescription().equals("")) { 410 data[11] = this; 411 } 412 413 return data; 414 } 415 416 @Override 417 public HashMap<String, Object> resolveReferences(HashMap<String, Object> map) { 418 if (map.containsKey("originalproductsids")) { 419 try { 420 try { 421 map.put("product", DatabaseObject.getObject(Context.getProduct(), Integer.valueOf(map.get("productsids").toString()))); 422 map.remove("productsids"); 423 } catch (NodataFoundException ex) { 424 map.put("product", null); 425 } 426 } catch (NumberFormatException numberFormatException) { 427 //already resolved? 428 } 429 } 430 431 return super.resolveReferences(map); 432 } 433 434 private static void calculate(ActivityListSubItem s) { 435 s.setTotalbrutvalue(s.quantityvalue.multiply(s.__getInternalvalue()).multiply(s.taxpercentvalue.divide(new BigDecimal(100), 9, BigDecimal.ROUND_HALF_UP).add(new BigDecimal(1)))); 436 } 437 438 @Override 439 public boolean save() { 440 calculate(this); 441 return super.save(); 442 } 443 444 @Override 445 public boolean save(boolean b) { 446 if (getCname().length() == 0) { 447 setCname(" "); 448 } 449 calculate(this); 450 return super.save(b); 451 } 452 453 /** 454 * Get the items of this list 455 * @param listid 456 * @param listname 457 * @return 458 * @throws NodataFoundException 459 */ 460 public static List<ActivityListSubItem> getList(int listid) throws NodataFoundException { 461 QueryCriteria c = new QueryCriteria("activitylistsids", listid); 462 ArrayList<ActivityListSubItem> data = getObjects(new ActivityListSubItem(), c); 463 return data; 464 } 465 466 public static BigDecimal getModelSum(MPTableModel model, int listid) { 467 List<Object[]> rowsl = model.getValidRows(new int[]{5}); 468 Log.Debug(ActivityListSubItem.class, "Rows found: " + rowsl.size()); 469 BigDecimal sum = BigDecimal.ZERO; 470 for (int i = 0; i < rowsl.size(); i++) { 471 Object[] row = rowsl.get(i); 472 sum.add(FormatNumber.parseDezimal(row[8].toString())); 473 } 474 return sum; 475 } 476 477 /** 478 * Save the model of SubItems 479 * @param dataOwner 480 * @param model 481 */ 482 public void updateRowFromModel(MPTableModel model, int row) { 483 //"Internal ID", "ID", "Date", "Count", "Description", "Netto Price", "Tax Value", "Total Price", "Product", "cname" 484 Log.Debug(ActivityListSubItem.class, "Row Searching: " + row); 485 this.setDatedoing((Date) model.getValueAt(row, 2)); 486 this.setQuantityvalue(new BigDecimal(model.getValueAt(row, 3).toString())); 487 this.setMeasure(model.getValueAt(row, 4).toString()); 488 this.setDescription(model.getValueAt(row, 5).toString()); 489 this.setInternalvalue(new BigDecimal(model.getValueAt(row, 6).toString())); 490 this.setTaxpercentvalue(new BigDecimal(model.getValueAt(row, 7).toString())); 491 this.setTotalbrutvalue(new BigDecimal(model.getValueAt(row, 8).toString())); 492 this.setProductsids(Integer.valueOf(model.getValueAt(row, 9).toString())); 493 this.setCname(model.getValueAt(row, 10).toString()); 494 } 495 496 /** 497 * Generates a String array out of this ActivityListSubItem 498 * @return 499 */ 500 public synchronized String[] toStringArray(Template template) { 501 calculate(this); 502 String[] possibleCols = new String[]{ 503 ////////////////// The exported columns/////////////////////////////////////// 504 String.valueOf(FormatNumber.formatInteger(1)), 505 String.valueOf(FormatNumber.formatDezimal(this.__getQuantityvalue())), 506 __getMeasure(), 507 __getDescription(), 508 String.valueOf(FormatNumber.formatLokalCurrency(this.__getInternalvalue())), 509 String.valueOf(FormatNumber.formatLokalCurrency(BigDecimal.ZERO)), 510 String.valueOf(FormatNumber.formatPercent(this.__getTaxpercentvalue())), 511 String.valueOf(FormatNumber.formatLokalCurrency(BigDecimal.ZERO)), 512 String.valueOf(FormatNumber.formatLokalCurrency(this.__getTotalbrutvalue())), 513 "", 514 DateConverter.getDefDateString(__getDatedoing()) 515 /////////////////////////////////////////////////////////////////////////////// 516 }; 517 List<String> all = new LinkedList<String>(); 518 List<String> l = Arrays.asList(possibleCols); 519 all.addAll(l); 520 521 if (GlobalSettings.getBooleanProperty("org.openyabs.exportproperty.productsresolved", false) && __getProductsids() > 0) { 522 try { 523 Product p = (Product) DatabaseObject.getObject(Context.getProduct(), __getProductsids()); 524 List<String[]> vals = p.getValues3(); 525 Collections.sort(vals, new Comparator<String[]>() { 526 527 public int compare(String[] o1, String[] o2) { 528 return o1[0].compareTo(o2[0]); 529 } 530 }); 531 for (int i = 0; i < vals.size(); i++) { 532 String[] v = vals.get(i); 533 all.add(v[1]); 534 if (Log.getLoglevel() == Log.LOGLEVEL_DEBUG) { 535 Log.Debug(this, (12 + i) + ": " + v[0] + " "); 536 } 537 } 538 } catch (Exception ex) { 539 Log.Debug(ex); 540 } 541 } 542 543 return all.toArray(new String[0]); 544 } 545 /** 546 * Compares by date 547 */ 548 public static Comparator<ActivityListSubItem> ORDER_COMPARATOR = new Comparator<ActivityListSubItem>() { 549 550 public int compare(ActivityListSubItem o1, ActivityListSubItem o2) { 551 return (o1.__getDatedoing().after(o2.__getDatedoing()) ? 1 : -1); 552 } 553 }; 554}