PageRenderTime 75ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/db/objects/Product.java

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