/src/mpv5/db/objects/Product.java
Java | 620 lines | 389 code | 77 blank | 154 comment | 39 complexity | c2ba70779f2490edfffa8fa86ede5de0 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
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 19 20import enoa.handler.TemplateHandler; 21import java.io.File; 22import java.math.BigDecimal; 23import java.util.Collections; 24import java.util.HashMap; 25import java.util.List; 26import java.util.Vector; 27import java.util.logging.Level; 28import java.util.logging.Logger; 29import javax.swing.JComponent; 30import mpv5.db.common.Context; 31import mpv5.db.common.DatabaseObject; 32import mpv5.db.common.Formattable; 33import mpv5.db.common.NodataFoundException; 34import mpv5.db.common.QueryHandler; 35import mpv5.db.common.Templateable; 36import mpv5.globals.Constants; 37import mpv5.globals.Messages; 38import mpv5.handler.FormatHandler; 39import mpv5.handler.MPEnum; 40import mpv5.logging.Log; 41import mpv5.ui.dialogs.Notificator; 42import mpv5.ui.panels.ProductPanel; 43import mpv5.utils.images.MPIcon; 44import mpv5.utils.numberformat.FormatNumber; 45 46/** 47 * 48 * 49 */ 50public class Product extends DatabaseObject implements Formattable, Templateable { 51 52 /** 53 * Returns a localized string represenation of the given product type 54 * @param type 55 * @return 56 */ 57 public static String getTypeString(int type) { 58 switch (type) { 59 case (Constants.TYPE_PRODUCT): 60 return Messages.TYPE_PRODUCT.toString(); 61 case (Constants.TYPE_SERVICE): 62 return Messages.TYPE_SERVICE.toString(); 63 } 64 return null; 65 } 66 67 /** 68 * Returns all possible Types 69 * @return 70 */ 71 public static MPEnum[] getTypes() { 72 MPEnum[] en = new MPEnum[2]; 73 en[0] = new MPEnum() { 74 75 @Override 76 public Integer getId() { 77 return new Integer(TYPE_PRODUCT); 78 } 79 80 @Override 81 public String getName() { 82 return getTypeString(TYPE_PRODUCT); 83 } 84 }; 85 en[1] = new MPEnum() { 86 87 @Override 88 public Integer getId() { 89 return new Integer(TYPE_SERVICE); 90 } 91 92 @Override 93 public String getName() { 94 return getTypeString(TYPE_SERVICE); 95 } 96 }; 97 98 return en; 99 100 } 101 private int taxids = 1; 102 private int inttype = 0; 103 private int manufacturersids = 0; 104 private int suppliersids = 0; 105 private int productgroupsids = 1; 106 private int productlistsids = 0; 107 private BigDecimal externalnetvalue = BigDecimal.ZERO; 108 private BigDecimal internalnetvalue = BigDecimal.ZERO; 109 private String description = ""; 110 private String cnumber = ""; 111 private String measure = ""; 112 private String url = ""; 113 private String ean = ""; 114 private String reference = "";//herstellernummer 115 private String defaultimage = ""; 116 private BigDecimal stockvalue = BigDecimal.ZERO; 117 private BigDecimal thresholdvalue = BigDecimal.ZERO; 118 private int intinventorytype = 0; 119 public static int TYPE_PRODUCT ;//TemplateHandler.TYPE_PRODUCT; 120 public static int TYPE_SERVICE ; 121 private FormatHandler formatHandler; 122 123 static{ 124 TYPE_PRODUCT = Constants.TYPE_PRODUCT; 125 TYPE_SERVICE = Constants.TYPE_SERVICE; 126 } 127 128 public Product() { 129 setContext(Context.getProduct()); 130 } 131 132 @Override 133 public mpv5.utils.images.MPIcon getIcon() { 134 MPIcon mpi = null; 135 if (defaultimage != null && defaultimage.length() > 0) { 136 File file = QueryHandler.instanceOf().clone(Context.getFiles()).retrieveFile(defaultimage); 137 if (file != null) { 138 try { 139 mpi = new MPIcon(file.toURI().toURL()); 140 return mpi.getScaledIcon(100, 100); 141 } catch (Exception ex) { 142 Log.Debug(ex); 143 } 144 } 145 } 146 return mpi; 147 } 148 149 @Override 150 public JComponent getView() { 151 ProductPanel p = new ProductPanel(getContext()); 152 return p; 153 } 154 155 /** 156 * @return the inttaxids 157 */ 158 public int __getTaxids() { 159 return taxids; 160 } 161 162 /** 163 * @param inttaxids the inttaxids to set 164 */ 165 public void setTaxids(int inttaxids) { 166 this.taxids = inttaxids; 167 } 168 169 /** 170 * @return the inttype 171 */ 172 public int __getInttype() { 173 return inttype; 174 } 175 176 /** 177 * @param inttype the inttype to set 178 */ 179 public void setInttype(int inttype) { 180 this.inttype = inttype; 181 } 182 183 /** 184 * @return the externalnetvalue 185 */ 186 public BigDecimal __getExternalnetvalue() { 187 return externalnetvalue; 188 } 189 190 /** 191 * @param externalnetvalue the externalnetvalue to set 192 */ 193 public void setExternalnetvalue(BigDecimal externalnetvalue) { 194 this.externalnetvalue = externalnetvalue; 195 } 196 197 /** 198 * @return the internalnetvalue 199 */ 200 public BigDecimal __getInternalnetvalue() { 201 return internalnetvalue; 202 } 203 204 /** 205 * @param internalnetvalue the internalnetvalue to set 206 */ 207 public void setInternalnetvalue(BigDecimal internalnetvalue) { 208 this.internalnetvalue = internalnetvalue; 209 } 210 211 /** 212 * @return the description 213 */ 214 public String __getDescription() { 215 return description; 216 } 217 218 /** 219 * @param description the description to set 220 */ 221 public void setDescription(String description) { 222 this.description = description; 223 } 224 225 /** 226 * @return the cnumber 227 */ 228 public String __getCnumber() { 229 return cnumber; 230 } 231 232 /** 233 * @param cnumber the cnumber to set 234 */ 235 public void setCnumber(String cnumber) { 236 this.cnumber = cnumber; 237 } 238 239 /** 240 * @return the measure 241 */ 242 public String __getMeasure() { 243 return measure; 244 } 245 246 /** 247 * @param measure the measure to set 248 */ 249 public void setMeasure(String measure) { 250 this.measure = measure; 251 } 252 253 /** 254 * @return the url 255 */ 256 public String __getUrl() { 257 return url; 258 } 259 260 /** 261 * @param url the url to set 262 */ 263 public void setUrl(String url) { 264 this.url = url; 265 } 266 267 /** 268 * @return the ean 269 */ 270 public String __getEan() { 271 return ean; 272 } 273 274 /** 275 * @param ean the ean to set 276 */ 277 public void setEan(String ean) { 278 this.ean = ean; 279 } 280 281 /** 282 * @return the reference 283 */ 284 public String __getReference() { 285 return reference; 286 } 287 288 /** 289 * @param reference the reference to set 290 */ 291 public void setReference(String reference) { 292 this.reference = reference; 293 } 294 295 /** 296 * @return the formatHandler 297 */ 298 @Override 299 public FormatHandler getFormatHandler() { 300 if (formatHandler == null) { 301 formatHandler = new FormatHandler(this); 302 } 303 return formatHandler; 304 } 305 306 @Override 307 public void ensureUniqueness() { 308 if ((cnumber == null || cnumber.length() == 0 ) || !QueryHandler.instanceOf().clone(Context.getProduct()).checkUniqueness("cnumber", cnumber)) { 309 setCnumber(getFormatHandler().next()); 310 } 311 } 312 313 /** 314 * @return the manufacturersids 315 */ 316 public int __getManufacturersids() { 317 return manufacturersids; 318 } 319 320 /** 321 * @param manufacturersids the manufacturersids to set 322 */ 323 public void setManufacturersids(int manufacturersids) { 324 this.manufacturersids = manufacturersids; 325 } 326 327 /** 328 * @return the suppliersids 329 */ 330 public int __getSuppliersids() { 331 return suppliersids; 332 } 333 334 /** 335 * @param suppliersids the suppliersids to set 336 */ 337 public void setSuppliersids(int suppliersids) { 338 this.suppliersids = suppliersids; 339 } 340 341 /** 342 * @return the defaultimage 343 */ 344 public String __getDefaultimage() { 345 return defaultimage; 346 } 347 348 /** 349 * @param defaultimage the defaultimage to set 350 */ 351 public void setDefaultimage(String defaultimage) { 352 this.defaultimage = defaultimage; 353 } 354 355 /** 356 * @return the productgroupsids 357 */ 358 public int __getProductgroupsids() { 359 return productgroupsids; 360 } 361 362 /** 363 * @param productgroupsids the productgroupsids to set 364 */ 365 public void setProductgroupsids(int productgroupsids) { 366 this.productgroupsids = productgroupsids; 367 } 368 369 @Override 370 public HashMap<String, Object> resolveReferences(HashMap<String, Object> map) { 371 372 if (map.containsKey("productgroupsids")) { 373 try { 374 try { 375 map.put("productgroup", DatabaseObject.getObject(Context.getProductGroup(), Integer.valueOf(map.get("productgroupsids").toString()))); 376 map.remove("productgroupsids"); 377 } catch (NodataFoundException ex) { 378 map.put("productgroup", null); 379 Log.Debug(this, ex.getMessage()); 380 } 381 } catch (NumberFormatException numberFormatException) { 382 //already resolved? 383 } 384 } 385 386 if (map.containsKey("suppliersids")) { 387 try { 388 try { 389 map.put("supplier", DatabaseObject.getObject(Context.getContact(), Integer.valueOf(map.get("suppliersids").toString()))); 390 map.remove("suppliersids"); 391 } catch (NodataFoundException ex) { 392 map.put("supplier", null); 393 Log.Debug(this, ex.getMessage()); 394 } 395 } catch (NumberFormatException numberFormatException) { 396 //already resolved? 397 } 398 } 399 400 if (map.containsKey("manufacturersids")) { 401 try { 402 403 //if Integer.valueOf(map.get("manufacturersids")>0 404 try { 405 map.put("manufacturer", DatabaseObject.getObject(Context.getContact(), Integer.valueOf(map.get("manufacturersids").toString()))); 406 map.remove("manufacturersids"); 407 } catch (NodataFoundException ex) { 408 map.put("manufacturer", null); 409 Log.Debug(this, ex.getMessage()); 410 } 411 } catch (NumberFormatException numberFormatException) { 412 //already resolved? 413 } 414 } 415 416 if (map.containsKey("taxids")) { 417 try { 418 map.put("tax", FormatNumber.formatPercent(Tax.getTaxValue(Integer.valueOf(map.get("taxids").toString())))); 419 map.remove("taxids"); 420 } catch (NumberFormatException numberFormatException) { 421 Log.Debug(numberFormatException); 422 } 423 } 424 425 try { 426 if (map.containsKey("inttype")) { 427 map.put("type", getTypeString(Integer.valueOf(map.get("inttype").toString()))); 428 map.remove("inttype"); 429 } 430 } catch (NumberFormatException numberFormatException) { 431 //already resolved? 432 Log.Debug(numberFormatException); 433 } 434 435 return super.resolveReferences(map); 436 } 437 438 public void defineFormatHandler(FormatHandler handler) { 439 formatHandler = handler; 440 } 441 442 /** 443 * Creates products from subitems which contain the trackid identifier - "tid#" 444 * @param saveModel 445 * @param dataowner 446 */ 447 public static void createProducts(List<SubItem> saveModel, Item dataowner) { 448 List<String> refs = new Vector<String>(); 449 for (SubItem i : saveModel) { 450 if (i.__getDescription().contains(TRACKID_END_IDENTIFIER) && i.__getDescription().contains(TRACKID_START_IDENTIFIER)) { 451 try { 452 Product p = new Product(); 453 p.setReference(i.__getDescription().substring(i.__getDescription().indexOf(TRACKID_START_IDENTIFIER) + TRACKID_START_IDENTIFIER.length(), i.__getDescription().indexOf(TRACKID_END_IDENTIFIER))); 454 p.setCname(i.__getDescription().substring(i.__getDescription().indexOf(TRACKID_END_IDENTIFIER) + TRACKID_START_IDENTIFIER.length())); 455 p.setExternalnetvalue(i.__getExternalvalue()); 456 p.setDescription(Messages.AUTO_GENERATED_VALUE.getValue()); 457 p.setInttype(Product.TYPE_SERVICE); 458 p.setProductgroupsids(1); 459 p.setGroupsids(i.__getGroupsids()); 460 p.setTaxids(Tax.getTaxId(i.__getTaxpercentvalue())); 461 p.setMeasure(i.__getMeasure()); 462 p.setUrl(i.__getLinkurl()); 463 p.save(true); 464 refs.add(p.__getReference()); 465 i.setDescription(i.__getDescription().replace(TRACKID_END_IDENTIFIER, " ")); 466 i.setDescription(i.__getDescription().replace(TRACKID_START_IDENTIFIER, "")); 467 i.setCname(i.__getDescription().replace(TRACKID_END_IDENTIFIER, " ")); 468 i.setCname(i.__getDescription().replace(TRACKID_START_IDENTIFIER, "")); 469 i.save(true); 470 } catch (Exception e) { 471 Log.Debug(e); 472 } 473 } 474 } 475 476 String r = ""; 477 for (int i = 0; i < refs.size(); i++) { 478 String string = refs.get(i); 479 if (!dataowner.__getDescription().contains(string)) { 480 r += string + ", "; 481 } 482 } 483 484 if (!r.equals("")) { 485 dataowner.setDescription(dataowner.__getDescription() + "\n" + r.substring(0, r.length() - 2)); 486 dataowner.save(true); 487 } 488 } 489 public static String TRACKID_START_IDENTIFIER = "tid#"; 490 public static String TRACKID_END_IDENTIFIER = "#tid"; 491 492 @Override 493 public boolean saveImport() { 494 495 if (__getProductgroupsids() <= 0 || !DatabaseObject.exists(Context.getProductGroup(), __getProductgroupsids())) { 496 setProductgroupsids(1); 497 } 498 499 if (__getManufacturersids() <= 0 || !DatabaseObject.exists(Context.getManufacturer(), __getManufacturersids())) { 500 setManufacturersids(0); 501 } 502 503 if (__getSuppliersids() <= 0 || !DatabaseObject.exists(Context.getSupplier(), __getSuppliersids())) { 504 setSuppliersids(0); 505 } 506 507 return super.saveImport(); 508 } 509 510 /** 511 * @return the stockvalue 512 */ 513 public BigDecimal __getStockvalue() { 514 return stockvalue; 515 } 516 517 /** 518 * @param stockvalue the stockvalue to set 519 */ 520 public void setStockvalue(BigDecimal stockvalue) { 521 this.stockvalue = stockvalue; 522 } 523 524 /** 525 * @return the thresholdvalue 526 */ 527 public BigDecimal __getThresholdvalue() { 528 return thresholdvalue; 529 } 530 531 /** 532 * @param thresholdvalue the thresholdvalue to set 533 */ 534 public void setThresholdvalue(BigDecimal thresholdvalue) { 535 this.thresholdvalue = thresholdvalue; 536 } 537 538 /** 539 * @return the inventorytype 540 */ 541 public int __getIntinventorytype() { 542 return intinventorytype; 543 } 544 545 /** 546 * @param inventorytype the inventorytype to set 547 */ 548 public void setIntinventorytype(int inventorytype) { 549 this.intinventorytype = inventorytype; 550 } 551 552 /** 553 * @return the productlistsids 554 */ 555 public int __getProductlistsids() { 556 return productlistsids; 557 } 558 559 /** 560 * @param productlistsids the productlistsids to set 561 */ 562 public void setProductlistsids(int productlistsids) { 563 this.productlistsids = productlistsids; 564 } 565 566 @Override 567 public boolean save(boolean silent) { 568 569 if (__getInttype() == TYPE_PRODUCT && __getIntinventorytype() == 1 570 && __getStockvalue().intValue() <= __getThresholdvalue().intValue()) { 571 Notificator.raiseNotification(Messages.INVENTORY_STOCK_TRESHOLD + toString(), true, true, this); 572 } 573 return super.save(silent); 574 } 575 576 @Override 577 public int templateType() { 578 return __getInttype(); 579 } 580 581 /** 582 * 583 * @return 584 */ 585 @Override 586 public int templateGroupIds() { 587 return __getGroupsids(); 588 } 589 590 @Override 591 public boolean delete() { 592 List<ProductPrice> pp = findProductPrices(); 593 for (int i = 0; i < pp.size(); i++) { 594 pp.get(i).delete(); 595 } 596 return super.delete(); 597 } 598 599 public List<ProductPrice> findProductPrices() { 600 try { 601 return getReferencedObjects(this, Context.getProductPrice(), new ProductPrice(), false); 602 } catch (NodataFoundException ex) { 603 Log.Debug(this, ex.getMessage()); 604 } 605 return Collections.emptyList(); 606 } 607 608 public BigDecimal findPriceFor(double amount) { 609 BigDecimal res = BigDecimal.ZERO; 610 BigDecimal am = BigDecimal.valueOf(amount); 611 List<ProductPrice> pp = findProductPrices(); 612 Collections.sort(pp); 613 for (ProductPrice p : pp) { 614 if (am.compareTo(p.getMincountvalue()) > 0) { 615 res = p.getExternalnetvalue(); 616 } 617 } 618 return res.compareTo(BigDecimal.ZERO) == 0 ? __getExternalnetvalue() : res; 619 } 620}