PageRenderTime 115ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/db/objects/ProductlistSubItem.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 642 lines | 424 code | 61 blank | 157 comment | 45 complexity | 001f5e4477dd1e534cc611d0945f895f 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 java.util.ArrayList;
  19. import java.util.Date;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Vector;
  23. import javax.swing.JComponent;
  24. import javax.swing.JTable;
  25. import mpv5.db.common.Context;
  26. import mpv5.db.common.DatabaseObject;
  27. import mpv5.db.common.NodataFoundException;
  28. import mpv5.db.common.QueryCriteria;
  29. import mpv5.globals.Headers;
  30. import mpv5.logging.Log;
  31. import mpv5.ui.frames.MPView;
  32. import mpv5.ui.panels.ItemPanel;
  33. import mpv5.ui.panels.ProductListsPanel;
  34. import mpv5.utils.models.MPTableModel;
  35. import mpv5.utils.numberformat.FormatNumber;
  36. /**
  37. *
  38. *
  39. */
  40. public class ProductlistSubItem extends DatabaseObject {
  41. /**
  42. * Save the model of SubItems
  43. * @param dataOwner
  44. * @param model
  45. */
  46. public static void saveModel(MPTableModel model, int listid) {
  47. List<Object[]> rowsl = model.getValidRows(new int[]{4});
  48. Log.Debug(ProductlistSubItem.class, "Rows found: " + rowsl.size());
  49. for (int i = 0; i < rowsl.size(); i++) {
  50. Object[] row = rowsl.get(i);
  51. for (int j = 0; j < row.length; j++) {
  52. if (row[j] == null) {
  53. row[j] = "";
  54. }
  55. }
  56. ProductlistSubItem it = new ProductlistSubItem();
  57. try {
  58. if (row[0] != null && Integer.valueOf(row[0].toString()).intValue() > 0) {
  59. it.setIDS(Integer.valueOf(row[0].toString()).intValue());
  60. } else {
  61. it.setIDS(-1);
  62. }
  63. } catch (Exception e) {
  64. Log.Debug(ProductlistSubItem.class, e.getMessage());
  65. }
  66. it.setCname(row[14].toString());
  67. it.setProductlistsids(listid);
  68. it.setCountvalue(Double.valueOf(row[1].toString()));
  69. it.setDescription(row[4].toString());
  70. it.setExternalvalue(Double.valueOf(row[5].toString()));
  71. it.setInternalvalue(Double.valueOf(row[5].toString()));//not supported yet
  72. it.setMeasure(row[3].toString());
  73. it.setOriginalproductsids(Integer.valueOf(row[10].toString()));
  74. it.setQuantityvalue(Double.valueOf(row[2].toString()));
  75. it.setTaxpercentvalue(Double.valueOf(row[6].toString()));
  76. it.setLinkurl((row[12 + 1].toString()));
  77. calculate(it);
  78. if (!it.isExisting()) {
  79. it.setDateadded(new Date());
  80. it.setGroupsids(mpv5.db.objects.User.getCurrentUser().__getGroupsids());
  81. }
  82. it.save(true);
  83. }
  84. }
  85. /**
  86. * Convert the model to a list of String arrays
  87. * @param dataOwner
  88. * @param model
  89. * @param t
  90. * @return
  91. */
  92. public static Vector<String[]> convertModel(MPTableModel model, Template t, int listid) {
  93. List<Object[]> rowsl = model.getValidRows(new int[]{4});
  94. Vector<String[]> rowsk = new Vector<String[]>();
  95. final List<ProductlistSubItem> its = new Vector<ProductlistSubItem>();
  96. Log.Debug(ProductlistSubItem.class, "Rows found: " + rowsl.size());
  97. for (int i = 0; i < rowsl.size(); i++) {
  98. Object[] row = rowsl.get(i);
  99. for (int j = 0; j < row.length; j++) {
  100. if (row[j] == null) {
  101. row[j] = "";
  102. }
  103. }
  104. final ProductlistSubItem it = new ProductlistSubItem();
  105. try {
  106. if (row[0] != null && Integer.valueOf(row[0].toString()).intValue() > 0) {
  107. it.setIDS(Integer.valueOf(row[0].toString()).intValue());
  108. } else {
  109. it.setIDS(-1);
  110. }
  111. } catch (Exception e) {
  112. Log.Debug(ProductlistSubItem.class, e.getMessage());
  113. }
  114. it.setCname(row[14].toString());
  115. it.setProductlistsids(listid);
  116. it.setCountvalue(Double.valueOf(row[1].toString()));
  117. it.setDescription(row[4].toString());
  118. it.setExternalvalue(Double.valueOf(row[5].toString()));
  119. it.setInternalvalue(Double.valueOf(row[5].toString()));//not supported yet
  120. it.setMeasure(row[3].toString());
  121. it.setOriginalproductsids(Integer.valueOf(row[10].toString()));
  122. it.setQuantityvalue(Double.valueOf(row[2].toString()));
  123. it.setTaxpercentvalue(Double.valueOf(row[6].toString()));
  124. it.setLinkurl((row[12 + 1].toString()));
  125. calculate(it);
  126. if (!it.isExisting()) {
  127. it.setDateadded(new Date());
  128. it.setGroupsids(mpv5.db.objects.User.getCurrentUser().__getGroupsids());
  129. }
  130. rowsk.add(it.toStringArray(t));
  131. }
  132. return rowsk;
  133. }
  134. /**
  135. * Add some value
  136. * @param table
  137. * @param percentValue
  138. * @param panel dirty dirty
  139. */
  140. public static void changeValueFields(JTable table, Integer percentValue, ProductListsPanel panel) {
  141. List<Object[]> rowsl = ((MPTableModel) table.getModel()).getValidRows(new int[]{4});
  142. Log.Debug(ProductlistSubItem.class, "Rows found: " + rowsl.size());
  143. ProductlistSubItem[] items = new ProductlistSubItem[rowsl.size()];
  144. for (int i = 0; i < rowsl.size(); i++) {
  145. Object[] row = rowsl.get(i);
  146. for (int j = 0; j < row.length; j++) {
  147. if (row[j] == null) {
  148. row[j] = "";
  149. }
  150. }
  151. ProductlistSubItem it = new ProductlistSubItem();
  152. try {
  153. if (row[0] != null && Integer.valueOf(row[0].toString()).intValue() > 0) {
  154. it.setIDS(Integer.valueOf(row[0].toString()).intValue());
  155. } else {
  156. it.setIDS(-1);
  157. }
  158. } catch (Exception e) {
  159. Log.Debug(ProductlistSubItem.class, e.getMessage());
  160. }
  161. it.setCname(row[14].toString());
  162. it.setCountvalue(Double.valueOf(row[1].toString()));
  163. it.setDescription(row[4].toString());
  164. it.setExternalvalue(Double.valueOf(row[5].toString()) * (((Double.valueOf(percentValue) / 100) + 1)));
  165. it.setInternalvalue(Double.valueOf(row[5].toString()) * (((Double.valueOf(percentValue) / 100) + 1)));//not supported yet
  166. it.setMeasure(row[3].toString());
  167. it.setOriginalproductsids(Integer.valueOf(row[10].toString()));
  168. it.setQuantityvalue(Double.valueOf(row[2].toString()));
  169. it.setTaxpercentvalue(Double.valueOf(row[6].toString()));
  170. it.setLinkurl((row[12 + 1].toString()));
  171. calculate(it);
  172. items[i] = it;
  173. }
  174. table.setModel(toModel(items));
  175. if (panel != null) {
  176. panel.formatTable();
  177. }
  178. }
  179. /**
  180. * Convert a Product into a Row
  181. * @param product
  182. * @return
  183. */
  184. public static ProductlistSubItem toRow(Product product) {
  185. return new ProductlistSubItem(product);
  186. }
  187. private int productlistsids;
  188. private int originalproductsids;
  189. private double countvalue;
  190. private double quantityvalue;
  191. private String measure = "";
  192. private String description = "";
  193. private double internalvalue;
  194. private double externalvalue;
  195. private double taxpercentvalue;
  196. private double totalnetvalue;
  197. private double totalbrutvalue;
  198. private double totaltaxvalue;
  199. private String linkurl = "";
  200. public ProductlistSubItem() {
  201. setContext(Context.getProductListItems());
  202. }
  203. /**
  204. *
  205. * @return
  206. */
  207. public static ProductlistSubItem getDefaultItem() {
  208. ProductlistSubItem i = new ProductlistSubItem();
  209. if (mpv5.db.objects.User.getCurrentUser().getProperties().hasProperty("defunit")) {
  210. String defunit = mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("defunit");
  211. i.setMeasure(defunit);
  212. }
  213. Double deftax = 0d;
  214. if (mpv5.db.objects.User.getCurrentUser().getProperties().hasProperty("deftax")) {
  215. int taxid = mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("deftax", 0);
  216. deftax = Tax.getTaxValue(taxid).doubleValue();
  217. i.setTaxpercentvalue(deftax);
  218. }
  219. Double defcount = 1d;
  220. if (mpv5.db.objects.User.getCurrentUser().getProperties().hasProperty("defcount")) {
  221. defcount = mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("defcount", 0d);
  222. i.setQuantityvalue(defcount);
  223. }
  224. return i;
  225. }
  226. /**
  227. *
  228. * @param o
  229. */
  230. public ProductlistSubItem(Product o) {
  231. this();
  232. setCname(o.__getCname());
  233. setDateadded(new Date());
  234. setDescription(o.__getDescription());
  235. setExternalvalue(o.__getExternalnetvalue().doubleValue());
  236. setGroupsids(mpv5.db.objects.User.getCurrentUser().__getGroupsids());
  237. setIntaddedby(mpv5.db.objects.User.getCurrentUser().__getIDS());
  238. setInternalvalue(o.__getInternalnetvalue().doubleValue());
  239. setMeasure(o.__getMeasure());
  240. setOriginalproductsids(o.__getIDS());
  241. setQuantityvalue(1);
  242. setTaxpercentvalue(Tax.getTaxValue(o.__getTaxids()).doubleValue());
  243. setLinkurl(o.__getUrl());
  244. calculate(this);
  245. }
  246. /**
  247. * Generates a SubItem with useless sample data
  248. * @return
  249. */
  250. public static ProductlistSubItem getRandomItem() {
  251. ProductlistSubItem i = new ProductlistSubItem();
  252. i.fillSampleData();
  253. if (mpv5.db.objects.User.getCurrentUser().getProperties().hasProperty("defunit")) {
  254. String defunit = mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("defunit");
  255. i.setMeasure(defunit);
  256. }
  257. Double deftax = 0d;
  258. if (mpv5.db.objects.User.getCurrentUser().getProperties().hasProperty("deftax")) {
  259. int taxid = mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("deftax", 0);
  260. deftax = Tax.getTaxValue(taxid).doubleValue();
  261. i.setTaxpercentvalue(deftax);
  262. }
  263. Double defcount = 1d;
  264. if (mpv5.db.objects.User.getCurrentUser().getProperties().hasProperty("defcount")) {
  265. defcount = mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("defcount", 0d);
  266. i.setQuantityvalue(defcount);
  267. }
  268. return i;
  269. }
  270. /**
  271. * Generates a String array out of this SubItem
  272. * @param template
  273. * @return
  274. */
  275. public String[] toStringArray(Template template) {
  276. String[] possibleCols = new String[]{
  277. ////////////////// The exported columns///////////////////////////////////////
  278. String.valueOf(FormatNumber.formatInteger(this.__getCountvalue())),
  279. String.valueOf(FormatNumber.formatDezimal(this.__getQuantityvalue())),
  280. __getMeasure(),
  281. __getDescription(),
  282. String.valueOf(FormatNumber.formatLokalCurrency(this.__getExternalvalue())),
  283. String.valueOf(FormatNumber.formatLokalCurrency(this.__getTotalnetvalue())),
  284. String.valueOf(FormatNumber.formatPercent(this.__getTaxpercentvalue())),
  285. String.valueOf(FormatNumber.formatLokalCurrency(this.getTotalTaxValue())),
  286. String.valueOf(FormatNumber.formatLokalCurrency(this.__getTotalbrutvalue())),
  287. __getLinkurl(),
  288. __getCname()
  289. ///////////////////////////////////////////////////////////////////////////////
  290. };
  291. String format = template.__getFormat();
  292. int[] intcols;
  293. try {
  294. String[] cols = format.split(",");
  295. intcols = new int[cols.length];
  296. for (int i = 0; i < intcols.length; i++) {
  297. String string = cols[i];
  298. intcols[i] = Integer.valueOf(string).intValue();
  299. }
  300. } catch (Exception ex) {
  301. Log.Debug(this, "An error occured, using default format now. " + ex.getMessage());
  302. intcols = new int[possibleCols.length];
  303. for (int i = 0; i < intcols.length; i++) {
  304. intcols[i] = i + 1;
  305. }
  306. }
  307. String[] form = new String[intcols.length];
  308. for (int i = 0; i < intcols.length; i++) {
  309. form[i] = possibleCols[intcols[i] - 1];
  310. }
  311. return form;
  312. }
  313. /**
  314. * @return the originalproductsids
  315. */
  316. public int __getOriginalproductsids() {
  317. return originalproductsids;
  318. }
  319. /**
  320. * @param originalproductsids the originalproductsids to set
  321. */
  322. public void setOriginalproductsids(int originalproductsids) {
  323. this.originalproductsids = originalproductsids;
  324. }
  325. /**
  326. * @return the count
  327. */
  328. public double __getCountvalue() {
  329. return countvalue;
  330. }
  331. /**
  332. * @param count the count to set
  333. */
  334. public void setCountvalue(double count) {
  335. this.countvalue = count;
  336. }
  337. /**
  338. * @return the quantity
  339. */
  340. public double __getQuantityvalue() {
  341. return quantityvalue;
  342. }
  343. /**
  344. * @param quantity the quantity to set
  345. */
  346. public void setQuantityvalue(double quantity) {
  347. this.quantityvalue = quantity;
  348. }
  349. /**
  350. * @return the measure
  351. */
  352. public String __getMeasure() {
  353. return measure;
  354. }
  355. /**
  356. * @param measure the measure to set
  357. */
  358. public void setMeasure(String measure) {
  359. this.measure = measure;
  360. }
  361. /**
  362. * @return the description
  363. */
  364. public String __getDescription() {
  365. return description;
  366. }
  367. /**
  368. * @param description the description to set
  369. */
  370. public void setDescription(String description) {
  371. this.description = description;
  372. }
  373. /**
  374. * @return the taxpercent
  375. */
  376. public double __getTaxpercentvalue() {
  377. return taxpercentvalue;
  378. }
  379. /**
  380. * @param taxpercent the taxpercent to set
  381. */
  382. public void setTaxpercentvalue(double taxpercent) {
  383. this.taxpercentvalue = taxpercent;
  384. }
  385. @Override
  386. public JComponent getView() {
  387. return null;
  388. }
  389. @Override
  390. public mpv5.utils.images.MPIcon getIcon() {
  391. return null;
  392. }
  393. /**
  394. * Generates a table model out of the given SubItems
  395. * @param items
  396. * @return
  397. */
  398. public static MPTableModel toModel(ProductlistSubItem[] items) {
  399. //"Internal ID", "ID", "Count", "Measure", "Description", "Netto Price", "Tax Value", "Total Price"
  400. Object[][] data = new Object[items.length][11];
  401. for (int i = 0; i < data.length; i++) {
  402. data[i] = items[i].getRowData(i + 1);
  403. }
  404. MPTableModel model = new MPTableModel(
  405. new Class[]{Integer.class, Integer.class, Double.class, String.class, String.class, Double.class, Double.class, Double.class, Double.class, Double.class, Integer.class, Object.class, Object.class, Object.class, String.class, String.class, String.class},
  406. new boolean[]{false, false, true, true, true, true, true, false, false, false, false, true, true, false, false, true, true},
  407. data,
  408. Headers.SUBITEMS.getValue());
  409. model.setContext(Context.getSubItem());
  410. String defunit = null;
  411. if (mpv5.db.objects.User.getCurrentUser().getProperties().hasProperty("defunit")) {
  412. defunit = mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("defunit");
  413. }
  414. Double deftax = 0d;
  415. if (mpv5.db.objects.User.getCurrentUser().getProperties().hasProperty("deftax")) {
  416. int taxid = mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("deftax", 0);
  417. deftax = Tax.getTaxValue(taxid).doubleValue();
  418. }
  419. Double defcount = 1d;
  420. if (mpv5.db.objects.User.getCurrentUser().getProperties().hasProperty("defcount")) {
  421. defcount = mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("defcount", 0d);
  422. }
  423. model.defineRow(new Object[]{0, 0, defcount, defunit, null, 0.0, deftax, 0.0, 0.0, 0.0, 0, "A", "C", "x", "y"});
  424. model.setAutoCountColumn(1);
  425. return model;
  426. }
  427. /**
  428. * Turn this SubItem into table row data
  429. * @param row
  430. * @return
  431. */
  432. public synchronized Object[] getRowData(int row) {
  433. Object[] data = new Object[15];
  434. for (int i = 0; i < data.length; i++) {
  435. data[0] = __getIDS();
  436. data[1] = Integer.valueOf(row);
  437. data[2] = __getQuantityvalue();
  438. data[3] = __getMeasure();
  439. data[4] = __getDescription();
  440. data[5] = __getExternalvalue();
  441. data[6] = __getTaxpercentvalue();
  442. data[7] = __getQuantityvalue() * __getExternalvalue() * ((__getTaxpercentvalue() / 100) + 1);
  443. data[8] = 0.0;
  444. data[9] = 0.0;
  445. data[10] = Integer.valueOf(__getOriginalproductsids());
  446. data[11] = "A";
  447. data[12] = "C";
  448. data[12 + 1] = __getLinkurl();
  449. data[14] = __getCname();
  450. }
  451. return data;
  452. }
  453. /**
  454. * @return the internalvalue
  455. */
  456. public double __getInternalvalue() {
  457. return internalvalue;
  458. }
  459. /**
  460. * @param internalvalue the internalvalue to set
  461. */
  462. public void setInternalvalue(double internalvalue) {
  463. this.internalvalue = internalvalue;
  464. }
  465. /**
  466. * @return the externalvalue
  467. */
  468. public double __getExternalvalue() {
  469. return externalvalue;
  470. }
  471. /**
  472. * @param externalvalue the externalvalue to set
  473. */
  474. public void setExternalvalue(double externalvalue) {
  475. this.externalvalue = externalvalue;
  476. }
  477. @Override
  478. public HashMap<String, Object> resolveReferences(HashMap<String, Object> map) {
  479. super.resolveReferences(map);
  480. if (map.containsKey("originalproductsids")) {
  481. try {
  482. try {
  483. map.put("originalproduct", DatabaseObject.getObject(Context.getProduct(), Integer.valueOf(map.get("originalproductsids").toString())));
  484. map.remove("originalproductsids");
  485. } catch (NodataFoundException ex) {
  486. map.put("originalproduct", null);
  487. }
  488. } catch (NumberFormatException numberFormatException) {
  489. //already resolved?
  490. }
  491. }
  492. return map;
  493. }
  494. /**
  495. * @return the totalnetvalue
  496. */
  497. public double __getTotalnetvalue() {
  498. return totalnetvalue;
  499. }
  500. /**
  501. * @param totalnetvalue the totalnetvalue to set
  502. */
  503. public void setTotalnetvalue(double totalnetvalue) {
  504. this.totalnetvalue = totalnetvalue;
  505. }
  506. /**
  507. * @return the totalbrutvalue
  508. */
  509. public double __getTotalbrutvalue() {
  510. return totalbrutvalue;
  511. }
  512. /**
  513. * @param totalbrutvalue the totalbrutvalue to set
  514. */
  515. public void setTotalbrutvalue(double totalbrutvalue) {
  516. this.totalbrutvalue = totalbrutvalue;
  517. }
  518. private static void calculate(ProductlistSubItem s) {
  519. s.setTotalbrutvalue(s.quantityvalue * s.externalvalue * ((s.taxpercentvalue / 100) + 1));
  520. s.defTotaltaxvalue(s.quantityvalue * s.externalvalue * (s.taxpercentvalue / 100));
  521. s.setTotalnetvalue(s.quantityvalue * s.externalvalue);
  522. }
  523. @Override
  524. public boolean save() {
  525. calculate(this);
  526. return super.save();
  527. }
  528. @Override
  529. public boolean save(boolean b) {
  530. if (getCname().length() == 0) {
  531. setCname(" ");
  532. }
  533. return super.save(b);
  534. }
  535. private void defTotaltaxvalue(double value) {
  536. totaltaxvalue = value;
  537. }
  538. /**
  539. *
  540. * @return
  541. */
  542. public double getTotalTaxValue() {
  543. return totaltaxvalue;
  544. }
  545. /**
  546. * Get the items of this list
  547. * @param listid
  548. * @param listname
  549. * @return
  550. * @throws NodataFoundException
  551. */
  552. public static List<ProductlistSubItem> getList(int listid) throws NodataFoundException {
  553. QueryCriteria c = new QueryCriteria("productlistsids", listid);
  554. ArrayList<ProductlistSubItem> data = getObjects(new ProductlistSubItem(), c);
  555. return data;
  556. }
  557. /**
  558. * @return the productlistsids
  559. */
  560. public int __getProductlistsids() {
  561. return productlistsids;
  562. }
  563. /**
  564. * @param productlistsids the productlistsids to set
  565. */
  566. public void setProductlistsids(int productlistsids) {
  567. this.productlistsids = productlistsids;
  568. }
  569. /**
  570. * @return the linkurl
  571. */
  572. public String __getLinkurl() {
  573. return linkurl;
  574. }
  575. /**
  576. * @param linkurl the linkurl to set
  577. */
  578. public void setLinkurl(String linkurl) {
  579. this.linkurl = linkurl;
  580. }
  581. }