/src/mpv5/db/objects/SubItem.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/ · Java · 962 lines · 666 code · 98 blank · 198 comment · 108 complexity · 5cc44dcfceb0c86ec3fce7ddc66b203a 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. */
  17. package mpv5.db.objects;
  18. import java.util.Arrays;
  19. import mpv5.db.common.Triggerable;
  20. import java.math.BigDecimal;
  21. import java.util.ArrayList;
  22. import java.util.Collections;
  23. import java.util.Comparator;
  24. import java.util.Date;
  25. import java.util.HashMap;
  26. import java.util.LinkedList;
  27. import java.util.List;
  28. import javax.swing.JButton;
  29. import javax.swing.JComponent;
  30. import mpv5.db.common.Context;
  31. import mpv5.db.common.DatabaseObject;
  32. import mpv5.db.common.NodataFoundException;
  33. import mpv5.db.common.QueryCriteria;
  34. import mpv5.db.common.QueryHandler;
  35. import mpv5.globals.Constants;
  36. import mpv5.globals.GlobalSettings;
  37. import mpv5.globals.Headers;
  38. import mpv5.handler.VariablesHandler;
  39. import mpv5.logging.Log;
  40. import mpv5.ui.panels.ItemPanel;
  41. import mpv5.utils.models.MPComboBoxModelItem;
  42. import mpv5.utils.models.MPTableModel;
  43. import mpv5.utils.numberformat.FormatNumber;
  44. /**
  45. *
  46. *
  47. */
  48. public final class SubItem extends DatabaseObject implements Triggerable {
  49. public static final int TYPE_TEXT = 1;
  50. public static final int TYPE_PRODUCT = 2;
  51. public static final int TYPE_NORMAL = 0;
  52. private static final long serialVersionUID = 1L;
  53. public static LinkedList<SubItem> saveModel(Item dataOwner, MPTableModel model, boolean deleteRemovedSubitems, boolean cloneSubitems) {
  54. List<Object[]> rowsl = model.getValidRows(new int[]{4});
  55. LinkedList<SubItem> items = new LinkedList<SubItem>();
  56. Log.Debug(SubItem.class, "Rows found: " + rowsl.size());
  57. for (int i = 0; i < rowsl.size(); i++) {
  58. Object[] row = rowsl.get(i);
  59. for (int j = 0; j < row.length; j++) {
  60. if (row[j] == null) {
  61. row[j] = "";
  62. }
  63. }
  64. SubItem it = new SubItem();
  65. it.setOrdernr(i);
  66. Log.Print(Arrays.asList(row));
  67. try {
  68. if (!cloneSubitems && row[0] != null && Integer.valueOf(row[0].toString()).intValue() > 0) {
  69. it.setIDS(Integer.valueOf(row[0].toString()).intValue());
  70. } else {
  71. it.setIDS(-1);
  72. }
  73. } catch (Exception e) {
  74. Log.Debug(SubItem.class, e.getMessage());
  75. }
  76. it.setCname(row[14].toString());
  77. it.setItemsids(dataOwner.__getIDS());
  78. it.setCountvalue(new BigDecimal(row[1].toString()));
  79. it.setDatedelivery(dataOwner.__getDatetodo());
  80. it.setDescription(row[4].toString());
  81. it.setExternalvalue(new BigDecimal(row[5].toString()));
  82. it.setInternalvalue(new BigDecimal(row[5].toString()));//not supported yet
  83. it.setMeasure(row[3].toString());
  84. if (row[10] instanceof Product) {
  85. try {
  86. it.setOriginalproductsids(((Product) row[10]).__getIDS());
  87. } catch (Exception e) {
  88. Log.Debug(e);
  89. }
  90. } else {
  91. }
  92. it.setLinkurl((row[12 + 1].toString()));
  93. it.setQuantityvalue(new BigDecimal(row[2].toString()));
  94. it.setTaxpercentvalue(new BigDecimal(row[6].toString()));
  95. it.setDiscount(new BigDecimal(row[15].toString()));
  96. calculate(it);
  97. if (!it.isExisting()) {
  98. it.setDateadded(new Date());
  99. it.setGroupsids(dataOwner.__getGroupsids());
  100. }
  101. it.save(true);
  102. items.add(it.getOrdernr(), it);
  103. }
  104. if (deleteRemovedSubitems) {
  105. for (int i = 0; i < deletionQueue.size(); i++) {
  106. try {
  107. QueryHandler.delete(SubItem.getObject(Context.getSubItem(), deletionQueue.get(i)));
  108. } catch (NodataFoundException ex) {
  109. Log.Debug(ex);
  110. }
  111. }
  112. }
  113. deletionQueue.clear();
  114. return items;
  115. }
  116. /**
  117. * Save the model of SubItems
  118. * @param dataOwner
  119. * @param model
  120. * @param deleteRemovedSubitems
  121. * @return
  122. */
  123. public static List<SubItem> saveModel(Item dataOwner, MPTableModel model, boolean deleteRemovedSubitems) {
  124. return saveModel(dataOwner, model, deleteRemovedSubitems, false);
  125. }
  126. /**
  127. * Convert the model to a list of String arrays
  128. * @param dataOwner
  129. * @param model
  130. * @param t
  131. * @return
  132. */
  133. public static LinkedList<String[]> convertModel(Item dataOwner, MPTableModel model, Template t) {
  134. List<Object[]> rowsl = model.getValidRows(new int[]{4});
  135. LinkedList<String[]> rowsk = new LinkedList<String[]>();
  136. Log.Debug(SubItem.class, "Rows found: " + rowsl.size());
  137. for (int i = 0; i < rowsl.size(); i++) {
  138. Object[] row = rowsl.get(i);
  139. for (int j = 0; j < row.length; j++) {
  140. if (row[j] == null) {
  141. row[j] = "";
  142. }
  143. }
  144. final SubItem it = new SubItem();
  145. it.setOrdernr(i);
  146. try {
  147. if (row[0] != null && Integer.valueOf(row[0].toString()).intValue() > 0) {
  148. it.setIDS(Integer.valueOf(row[0].toString()).intValue());
  149. } else {
  150. it.setIDS(-1);
  151. }
  152. } catch (Exception e) {
  153. Log.Debug(SubItem.class, e.getMessage());
  154. }
  155. it.setCname(row[14].toString());
  156. it.setItemsids(dataOwner.__getIDS());
  157. it.setCountvalue(new BigDecimal(row[1].toString()));
  158. it.setDatedelivery(dataOwner.__getDatetodo());
  159. it.setDescription(row[4].toString());
  160. it.setExternalvalue(new BigDecimal(row[5].toString()));
  161. it.setInternalvalue(new BigDecimal(row[5].toString()));//not supported yet
  162. it.setMeasure(row[3].toString());
  163. it.setOriginalproductsids(Integer.valueOf(row[10].toString()));
  164. if (row[10] instanceof Product) {
  165. try {
  166. it.setOriginalproductsids(((Product) row[10]).__getIDS());
  167. } catch (Exception e) {
  168. Log.Debug(e);
  169. }
  170. }
  171. it.setQuantityvalue(new BigDecimal(row[2].toString()));
  172. it.setTaxpercentvalue(new BigDecimal(row[6].toString()));
  173. it.setLinkurl((row[12 + 1].toString()));
  174. it.setDiscount(new BigDecimal(row[15].toString()));
  175. calculate(it);
  176. if (!it.isExisting()) {
  177. it.setDateadded(new Date());
  178. it.setGroupsids(dataOwner.__getGroupsids());
  179. }
  180. // it.save();
  181. rowsk.add(it.getOrdernr(), it.toStringArray());
  182. }
  183. return rowsk;
  184. }
  185. private static List<Integer> deletionQueue = new ArrayList<Integer>();
  186. /**
  187. * Mark a subitem for deletion
  188. * @param valueAt INT or Entity
  189. */
  190. public static void addToDeletionQueue(Object valueAt) {
  191. Log.Debug(SubItem.class, "Adding to deletionqueue: " + valueAt);
  192. if (valueAt != null) {
  193. try {
  194. Integer isd = Integer.valueOf(valueAt.toString());
  195. deletionQueue.add(isd);
  196. } catch (NumberFormatException numberFormatException) {
  197. Integer isd = ((Entity) valueAt).getValue();
  198. deletionQueue.add(isd);
  199. }
  200. }
  201. }
  202. /**
  203. * UN-Mark a subitem for deletion
  204. * @param valueAt INT or Entity
  205. */
  206. public static void removeFromDeletionQueue(Object valueAt) {
  207. Log.Debug(SubItem.class, "Removing from deletionqueue: " + valueAt);
  208. if (valueAt != null) {
  209. try {
  210. Integer isd = Integer.valueOf(valueAt.toString());
  211. deletionQueue.remove(isd);
  212. } catch (NumberFormatException numberFormatException) {
  213. Integer isd = ((Entity) valueAt).getValue();
  214. deletionQueue.add(isd);
  215. }
  216. }
  217. }
  218. private int itemsids;
  219. private int ordernr;
  220. private int inttype;
  221. private int originalproductsids;
  222. private BigDecimal countvalue = BigDecimal.ZERO;
  223. private BigDecimal quantityvalue = BigDecimal.ZERO;
  224. private String measure = "";
  225. private String description = "";
  226. private String linkurl = "";
  227. private BigDecimal internalvalue = BigDecimal.ZERO;
  228. private BigDecimal externalvalue = BigDecimal.ZERO;
  229. private BigDecimal taxpercentvalue = BigDecimal.ZERO;
  230. private BigDecimal totalnetvalue = BigDecimal.ZERO;
  231. private BigDecimal totalbrutvalue = BigDecimal.ZERO;
  232. private Date datedelivery;
  233. private BigDecimal totaltaxvalue = BigDecimal.ZERO;
  234. private BigDecimal discount = BigDecimal.ZERO;
  235. private BigDecimal discvalue = BigDecimal.ZERO;
  236. public SubItem() {
  237. setContext(Context.getSubItem());
  238. }
  239. /**
  240. *
  241. * @return
  242. */
  243. public static SubItem getDefaultItem() {
  244. SubItem i = new SubItem();
  245. if (mpv5.db.objects.User.getCurrentUser().getProperties().hasProperty("defunit")) {
  246. String defunit = mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("defunit");
  247. i.setMeasure(defunit);
  248. }
  249. BigDecimal deftax = BigDecimal.ZERO;
  250. if (mpv5.db.objects.User.getCurrentUser().getProperties().hasProperty("deftax")) {
  251. int taxid = mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("deftax", new Integer(0));
  252. deftax = Tax.getTaxValue(taxid);
  253. }
  254. i.setTaxpercentvalue(deftax);
  255. BigDecimal defdiscount = BigDecimal.ZERO;
  256. if (mpv5.db.objects.User.getCurrentUser().getProperties().hasProperty("defdiscount")) {
  257. defdiscount = new BigDecimal(mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("defdiscount"));
  258. }
  259. i.setDiscount(defdiscount);
  260. if (mpv5.db.objects.User.getCurrentUser().getProperties().hasProperty("defcount")) {
  261. Double defcount = mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("defcount", 0d);
  262. i.setQuantityvalue(new BigDecimal(defcount.toString()));
  263. } else {
  264. i.setQuantityvalue(BigDecimal.ONE);
  265. }
  266. return i;
  267. }
  268. /**
  269. *
  270. * @param product
  271. */
  272. public SubItem(Product product) {
  273. this();
  274. setCname(product.__getCname());
  275. setDateadded(new Date());
  276. setDatedelivery(new Date());
  277. ////////////////format///////////////////////////////////////////////////////////
  278. Context contextl = product.getContext();
  279. String params = "cname";
  280. String vars = null;
  281. if (mpv5.db.objects.User.getCurrentUser().getProperties().hasProperty(contextl + mpv5.ui.beans.LightMPComboBox.VALUE_SEARCHFIELDS)
  282. && mpv5.db.objects.User.getCurrentUser().getProperties().getProperty(contextl + mpv5.ui.beans.LightMPComboBox.VALUE_SEARCHFIELDS).contains("_$")) {
  283. try {
  284. params = "ids";
  285. vars = mpv5.db.objects.User.getCurrentUser().getProperties().getProperty(contextl + mpv5.ui.beans.LightMPComboBox.VALUE_SEARCHFIELDS);
  286. String[] vaars = vars.split("_\\$");
  287. for (int i = 0; i < vaars.length; i++) {
  288. try {
  289. if (vaars[i] != null && vaars[i].contains("$_")) {
  290. params += "," + vaars[i].split("\\$_")[0].replace(",", "").replace("'", "`");
  291. }
  292. } catch (Exception e) {
  293. Log.Debug(e);
  294. }
  295. }
  296. QueryCriteria qc = new QueryCriteria("ids", product.__getIDS());
  297. Object[][] result = QueryHandler.instanceOf().clone(Context.getProduct()).select(params, qc);
  298. String formatString = vars;
  299. if (formatString != null) {
  300. vaars = formatString.split("_\\$");
  301. Log.Debug(MPComboBoxModelItem.class, "Length of var string: " + vaars.length);
  302. }
  303. String x = "";
  304. String format = formatString;
  305. for (int j = 1; j < result[0].length; j++) {
  306. String k = String.valueOf(result[0][j]);
  307. if (format == null) {
  308. x += k;
  309. } else {
  310. try {
  311. format = format.replaceFirst("_\\$(.*?)\\$_", k);
  312. } catch (Exception e) {
  313. Log.Debug(e);
  314. }
  315. x = format;
  316. }
  317. }
  318. setDescription(VariablesHandler.parse(x, product));
  319. } catch (NodataFoundException nodataFoundException) {
  320. setDescription(VariablesHandler.parse(product.__getCname(), product));
  321. }
  322. } else {
  323. Log.Debug(SubItem.class, "No format defined..");
  324. setDescription(VariablesHandler.parse(product.__getCname(), product));
  325. }
  326. ///////////////end format////////////////////////////////////////////////////////
  327. setExternalvalue(product.findPriceFor(1d));
  328. setGroupsids(mpv5.db.objects.User.getCurrentUser().__getGroupsids());
  329. setIntaddedby(mpv5.db.objects.User.getCurrentUser().__getIDS());
  330. setInternalvalue(product.__getInternalnetvalue());
  331. setMeasure(product.__getMeasure());
  332. setOriginalproductsids(product.__getIDS());
  333. setQuantityvalue(BigDecimal.ONE);
  334. setTaxpercentvalue(Tax.getTaxValue(product.__getTaxids()));
  335. setLinkurl(product.__getUrl());
  336. calculate(this);
  337. }
  338. /**
  339. * Generates a SubItem with useless sample data
  340. * @return
  341. */
  342. public static SubItem getRandomItem() {
  343. SubItem i = new SubItem();
  344. i.fillSampleData();
  345. i.setGroupsids(1);
  346. if (mpv5.db.objects.User.getCurrentUser().getProperties().hasProperty("defunit")) {
  347. String defunit = mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("defunit");
  348. i.setMeasure(defunit);
  349. }
  350. BigDecimal deftax = BigDecimal.ZERO;
  351. if (mpv5.db.objects.User.getCurrentUser().getProperties().hasProperty("deftax")) {
  352. int taxid = mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("deftax", 0);
  353. deftax = Tax.getTaxValue(taxid);
  354. i.setTaxpercentvalue(deftax);
  355. }
  356. Double defcount = 1d;
  357. if (mpv5.db.objects.User.getCurrentUser().getProperties().hasProperty("defcount")) {
  358. defcount = mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("defcount", 0d);
  359. i.setQuantityvalue(new BigDecimal(defcount.toString()));
  360. }
  361. return i;
  362. }
  363. /**
  364. * Generates a String array out of this SubItem
  365. * @return
  366. */
  367. public synchronized String[] toStringArray() {
  368. calculate(this);
  369. List<String> all = new LinkedList<String>();
  370. String[] possibleCols = new String[]{
  371. ////////////////// The exported columns///////////////////////////////////////
  372. getInttype() != TYPE_TEXT ? String.valueOf(FormatNumber.formatInteger(this.__getCountvalue())) : "",
  373. getInttype() != TYPE_TEXT ? String.valueOf(FormatNumber.formatDezimal(this.__getQuantityvalue())) : "",
  374. getInttype() != TYPE_TEXT ? __getMeasure() : "",
  375. __getDescription(),
  376. getInttype() != TYPE_TEXT ? String.valueOf(FormatNumber.formatLokalCurrency(this.__getExternalvalue())) : "",
  377. getInttype() != TYPE_TEXT ? String.valueOf(FormatNumber.formatLokalCurrency(this.__getTotalnetvalue())) : "",
  378. getInttype() != TYPE_TEXT ? String.valueOf(FormatNumber.formatPercent(this.__getTaxpercentvalue())) : "",
  379. getInttype() != TYPE_TEXT ? String.valueOf(FormatNumber.formatLokalCurrency(this.getTotalTaxValue())) : "",
  380. getInttype() != TYPE_TEXT ? String.valueOf(FormatNumber.formatLokalCurrency(this.__getTotalbrutvalue())) : "",
  381. __getLinkurl(),
  382. __getCname()
  383. ///////////////////////////////////////////////////////////////////////////////
  384. };
  385. List<String> l = Arrays.asList(possibleCols);
  386. all.addAll(l);
  387. if (GlobalSettings.getBooleanProperty("org.openyabs.exportproperty.productsresolved", false) && __getOriginalproductsids() > 0) {
  388. try {
  389. Product p = (Product) DatabaseObject.getObject(Context.getProduct(), __getOriginalproductsids());
  390. List<String[]> vals = p.getValues3();
  391. Collections.sort(vals, new Comparator<String[]>() {
  392. public int compare(String[] o1, String[] o2) {
  393. return o1[0].compareTo(o2[0]);
  394. }
  395. });
  396. for (int i = 0; i < vals.size(); i++) {
  397. String[] v = vals.get(i);
  398. all.add(v[1]);
  399. if (Log.getLoglevel() == Log.LOGLEVEL_DEBUG) {
  400. Log.Debug(this, (12 + i) + ": " + v[0] + " ");
  401. }
  402. }
  403. } catch (Exception ex) {
  404. Log.Debug(ex);
  405. }
  406. }
  407. return all.toArray(new String[0]);
  408. }
  409. /**
  410. * @return the itemsids
  411. */
  412. public int __getItemsids() {
  413. return itemsids;
  414. }
  415. /**
  416. * @param itemsids the itemsids to set
  417. */
  418. public void setItemsids(int itemsids) {
  419. this.itemsids = itemsids;
  420. }
  421. /**
  422. * @return the originalproductsids
  423. */
  424. public int __getOriginalproductsids() {
  425. return originalproductsids;
  426. }
  427. /**
  428. * @param originalproductsids the originalproductsids to set
  429. */
  430. public void setOriginalproductsids(int originalproductsids) {
  431. this.originalproductsids = originalproductsids;
  432. if (originalproductsids > 0) {
  433. setInttype(TYPE_PRODUCT);
  434. } else {
  435. setInttype(TYPE_NORMAL);
  436. }
  437. }
  438. /**
  439. * @return the count
  440. */
  441. public BigDecimal __getCountvalue() {
  442. return countvalue;
  443. }
  444. /**
  445. * @param count the count to set
  446. */
  447. public void setCountvalue(BigDecimal count) {
  448. this.countvalue = count;
  449. }
  450. /**
  451. * @return the quantity
  452. */
  453. public BigDecimal __getQuantityvalue() {
  454. return quantityvalue;
  455. }
  456. /**
  457. * @param quantity the quantity to set
  458. */
  459. public void setQuantityvalue(BigDecimal quantity) {
  460. this.quantityvalue = quantity;
  461. if (quantity.compareTo(BigDecimal.ZERO) == 0) {
  462. setInttype(TYPE_TEXT);
  463. }
  464. }
  465. /**
  466. * @return the measure
  467. */
  468. public String __getMeasure() {
  469. return measure;
  470. }
  471. /**
  472. * @param measure the measure to set
  473. */
  474. public void setMeasure(String measure) {
  475. this.measure = measure;
  476. }
  477. /**
  478. * @return the description
  479. */
  480. public String __getDescription() {
  481. return description;
  482. }
  483. /**
  484. * @param description the description to set
  485. */
  486. public void setDescription(String description) {
  487. this.description = description;
  488. }
  489. /**
  490. * @return the taxpercent
  491. */
  492. public BigDecimal __getTaxpercentvalue() {
  493. return taxpercentvalue;
  494. }
  495. /**
  496. * @param taxpercent the taxpercent to set
  497. */
  498. public void setTaxpercentvalue(BigDecimal taxpercent) {
  499. this.taxpercentvalue = taxpercent;
  500. }
  501. /**
  502. * @return the datedelivery
  503. */
  504. public Date __getDatedelivery() {
  505. return datedelivery;
  506. }
  507. /**
  508. * @param datedelivery the datedelivery to set
  509. */
  510. public void setDatedelivery(Date datedelivery) {
  511. this.datedelivery = datedelivery;
  512. }
  513. /**
  514. *
  515. * @return
  516. */
  517. public BigDecimal __getDiscount() {
  518. return discount;
  519. }
  520. /**
  521. *
  522. * @param discount
  523. */
  524. public void setDiscount(BigDecimal discount) {
  525. this.discount = discount;
  526. }
  527. public BigDecimal getDiscvalue() {
  528. return discvalue;
  529. }
  530. public void defDiscvalue(BigDecimal discvalue) {
  531. this.discvalue = discvalue;
  532. }
  533. @Override
  534. public JComponent getView() {
  535. try {
  536. Item dos = (Item) DatabaseObject.getObject(Context.getItem(), __getItemsids());
  537. ItemPanel ip = new ItemPanel(Context.getItem(), dos.__getInttype());
  538. return ip;
  539. } catch (NodataFoundException ex) {
  540. throw new RuntimeException(ex);
  541. }
  542. }
  543. @Override
  544. public mpv5.utils.images.MPIcon getIcon() {
  545. return null;
  546. }
  547. /**
  548. * Generates a table model out of the given SubItems
  549. * @param items
  550. * @return
  551. */
  552. public static MPTableModel toModel(SubItem[] items) {
  553. return toModel(items, false);
  554. }
  555. /**
  556. * The ADD button index
  557. */
  558. public static int COLUMNINDEX_ADD = 11;
  559. /**
  560. * The CLEAR button index
  561. */
  562. public static int COLUMNINDEX_REMOVE = 12;
  563. /**
  564. * Compares by ordernr
  565. */
  566. public static Comparator<SubItem> ORDER_COMPARATOR = new Comparator<SubItem>() {
  567. @Override
  568. public int compare(SubItem o1, SubItem o2) {
  569. return (o1.getOrdernr() > o2.getOrdernr()) ? 1 : -1;
  570. }
  571. };
  572. /**
  573. * Generates a table model out of the given SubItems
  574. * @param items
  575. * @param removeSubitemIds
  576. * @return
  577. */
  578. public static MPTableModel toModel(SubItem[] items, boolean removeSubitemIds) {
  579. //"Internal ID", "ID", "Count", "Measure", "Description", "Netto Price", "Tax Value", "Total Price"
  580. Object[][] data = new Object[items.length][];
  581. List<SubItem> tlist = new LinkedList<SubItem>();
  582. tlist.addAll(Arrays.asList(items));
  583. Collections.sort(tlist, SubItem.ORDER_COMPARATOR);
  584. for (int i = 0; i < tlist.size(); i++) {
  585. SubItem subItem = tlist.get(i);
  586. if (subItem != null) {
  587. data[i] = subItem.getRowData(i + 1);
  588. } else {
  589. data[i] = getDefaultItem().getRowData(i + 1);
  590. }
  591. if (removeSubitemIds) {
  592. data[i][0] = -1;
  593. }
  594. }
  595. MPTableModel model = new MPTableModel(
  596. new Class[]{
  597. Integer.class, Integer.class, BigDecimal.class, String.class, Object.class, BigDecimal.class, BigDecimal.class, BigDecimal.class,
  598. BigDecimal.class, BigDecimal.class, Product.class, JButton.class, JButton.class, String.class, String.class, BigDecimal.class, BigDecimal.class},
  599. new boolean[]{false, false, true, true, true, true, true, false, false, false, false, true, true, true, true, true, true, true},
  600. data,
  601. Headers.SUBITEMS.getValue());
  602. model.setContext(Context.getSubItem());
  603. String defunit = null;
  604. if (mpv5.db.objects.User.getCurrentUser().getProperties().hasProperty("defunit")) {
  605. defunit = mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("defunit");
  606. }
  607. BigDecimal deftax = BigDecimal.ZERO;
  608. if (mpv5.db.objects.User.getCurrentUser().getProperties().hasProperty("deftax")) {
  609. int taxid = mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("deftax", 0);
  610. deftax = Tax.getTaxValue(taxid);
  611. }
  612. Double defcount = 1d;
  613. if (mpv5.db.objects.User.getCurrentUser().getProperties().hasProperty("defcount")) {
  614. defcount = mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("defcount", 0d);
  615. }
  616. model.defineRow(new Object[]{0, 0, defcount, defunit, null, 0.0, deftax, 0.0, 0.0, 0.0, null, "A", "C", "", "", 0.0, 0.0});
  617. model.setAutoCountColumn(1);
  618. return model;
  619. }
  620. /**
  621. * Turn this SubItem into table row data
  622. * @param row
  623. * @return
  624. */
  625. public synchronized Object[] getRowData(int row) {
  626. Object[] data = new Object[17];
  627. for (int i = 0; i < data.length; i++) {
  628. data[0] = __getIDS();
  629. data[1] = Integer.valueOf(row);
  630. data[2] = __getQuantityvalue();
  631. data[3] = __getMeasure();
  632. data[4] = __getDescription();
  633. data[5] = __getExternalvalue();
  634. data[6] = __getTaxpercentvalue();
  635. data[7] = __getQuantityvalue().multiply(__getExternalvalue().multiply(((__getTaxpercentvalue().divide(Constants.BD100, 9, BigDecimal.ROUND_HALF_UP).add(BigDecimal.ONE)))));
  636. data[8] = 0.0;
  637. data[9] = 0.0;
  638. if (__getOriginalproductsids() > 0) {
  639. try {
  640. data[10] = Product.getObject(Context.getProduct(), __getOriginalproductsids());
  641. } catch (NodataFoundException ex) {
  642. Log.Debug(ex);
  643. }
  644. }
  645. data[11] = "A";
  646. data[12] = "C";
  647. data[12 + 1] = __getLinkurl();
  648. data[14] = __getCname();
  649. data[15] = __getDiscount();
  650. data[16] = 0.0;
  651. }
  652. return data;
  653. }
  654. /**
  655. * Turn this SubItem into table row data
  656. * @param row
  657. * @return
  658. */
  659. public static synchronized SubItem getFromModel(MPTableModel m, int row) {
  660. SubItem t = new SubItem();
  661. if (m.getValueAt(row, 0) != null) {
  662. t.setIDS(Integer.valueOf(m.getValueAt(row, 0).toString()));
  663. }
  664. if (m.getValueAt(row, 1) != null) {
  665. t.setCountvalue(new BigDecimal(m.getValueAt(row, 1).toString()));
  666. }
  667. if (m.getValueAt(row, 2) != null) {
  668. t.setQuantityvalue(new BigDecimal(m.getValueAt(row, 2).toString()));
  669. }
  670. if (m.getValueAt(row, 3) != null) {
  671. t.setMeasure(m.getValueAt(row, 3).toString());
  672. }
  673. if (m.getValueAt(row, 4) != null) {
  674. t.setDescription(m.getValueAt(row, 4).toString());
  675. }
  676. if (m.getValueAt(row, 5) != null) {
  677. t.setExternalvalue(new BigDecimal(m.getValueAt(row, 5).toString()));
  678. }
  679. if (m.getValueAt(row, 6) != null) {
  680. t.setTaxpercentvalue(new BigDecimal(m.getValueAt(row, 6).toString()));
  681. }
  682. if (m.getValueAt(row, 10) != null) {
  683. try {
  684. t.setOriginalproductsids(((Product) m.getValueAt(row, 10)).__getIDS());
  685. } catch (Exception e) {
  686. Log.Debug(e);
  687. }
  688. }
  689. if (m.getValueAt(row, 13) != null) {
  690. t.setLinkurl(m.getValueAt(row, 13).toString());
  691. }
  692. if (m.getValueAt(row, 14) != null) {
  693. t.setCname(m.getValueAt(row, 14).toString());
  694. }
  695. if (m.getValueAt(row, 15) != null) {
  696. t.setDiscount(new BigDecimal(m.getValueAt(row, 15).toString()));
  697. }
  698. return t;
  699. }
  700. /**
  701. * @return the internalvalue
  702. */
  703. public BigDecimal __getInternalvalue() {
  704. return internalvalue;
  705. }
  706. /**
  707. * @param internalvalue the internalvalue to set
  708. */
  709. public void setInternalvalue(BigDecimal internalvalue) {
  710. this.internalvalue = internalvalue;
  711. }
  712. /**
  713. * @return the externalvalue
  714. */
  715. public BigDecimal __getExternalvalue() {
  716. return externalvalue;
  717. }
  718. /**
  719. * @param externalvalue the externalvalue to set
  720. */
  721. public void setExternalvalue(BigDecimal externalvalue) {
  722. this.externalvalue = externalvalue;
  723. }
  724. @Override
  725. public HashMap<String, Object> resolveReferences(HashMap<String, Object> map) {
  726. if (map.containsKey("originalproductsids")) {
  727. try {
  728. try {
  729. map.put("originalproduct", DatabaseObject.getObject(Context.getProduct(), Integer.valueOf(map.get("originalproductsids").toString())));
  730. map.remove("originalproductsids");
  731. } catch (NodataFoundException ex) {
  732. map.put("originalproduct", null);
  733. }
  734. } catch (NumberFormatException numberFormatException) {
  735. //already resolved?
  736. }
  737. }
  738. return super.resolveReferences(map);
  739. }
  740. /**
  741. * @return the totalnetvalue
  742. */
  743. public BigDecimal __getTotalnetvalue() {
  744. return totalnetvalue;
  745. }
  746. /**
  747. * @param totalnetvalue the totalnetvalue to set
  748. */
  749. public void setTotalnetvalue(BigDecimal totalnetvalue) {
  750. this.totalnetvalue = totalnetvalue;
  751. }
  752. /**
  753. * @return the totalbrutvalue
  754. */
  755. public BigDecimal __getTotalbrutvalue() {
  756. return totalbrutvalue;
  757. }
  758. /**
  759. * @param totalbrutvalue the totalbrutvalue to set
  760. */
  761. public void setTotalbrutvalue(BigDecimal totalbrutvalue) {
  762. this.totalbrutvalue = totalbrutvalue;
  763. }
  764. static void calculate(SubItem s) {
  765. BigDecimal disc = s.__getDiscount().divide(BD100, 9, BigDecimal.ROUND_HALF_UP);
  766. s.setTotalbrutvalue(s.quantityvalue.multiply(s.externalvalue.multiply(((s.taxpercentvalue.divide(Constants.BD100, 9, BigDecimal.ROUND_HALF_UP)).add(BigDecimal.ONE)))));
  767. s.setTotalbrutvalue(s.totalbrutvalue.subtract(s.totalbrutvalue.multiply(disc)));
  768. s.defTotaltaxvalue(s.quantityvalue.multiply(s.externalvalue.multiply((s.taxpercentvalue.divide(Constants.BD100, 9, BigDecimal.ROUND_HALF_UP)))));
  769. s.defTotaltaxvalue(s.totaltaxvalue.subtract(s.totaltaxvalue.multiply(disc)));
  770. s.setTotalnetvalue(s.quantityvalue.multiply(s.externalvalue));
  771. s.defDiscvalue(s.totalnetvalue.multiply(disc));
  772. }
  773. @Override
  774. public boolean save() {
  775. calculate(this);
  776. return super.save();
  777. }
  778. private void defTotaltaxvalue(BigDecimal value) {
  779. totaltaxvalue = value;
  780. }
  781. /**
  782. *
  783. * @return
  784. */
  785. public BigDecimal getTotalTaxValue() {
  786. return totaltaxvalue;
  787. }
  788. /**
  789. * @return the linkurl
  790. */
  791. public String __getLinkurl() {
  792. return linkurl;
  793. }
  794. /**
  795. * @param linkurl the linkurl to set
  796. */
  797. public void setLinkurl(String linkurl) {
  798. this.linkurl = linkurl;
  799. }
  800. @Override
  801. public boolean save(boolean b) {
  802. if (getCname().length() == 0) {
  803. setCname(" ");
  804. }
  805. return super.save(b);
  806. }
  807. @Override
  808. public void triggerOnCreate() {
  809. if (__getOriginalproductsids() > 0) {
  810. try {
  811. Product p = (Product) DatabaseObject.getObject(Context.getProduct(), originalproductsids);
  812. if (p.__getIntinventorytype() == 1) {
  813. p.setStockvalue(p.__getStockvalue().subtract(__getQuantityvalue()));
  814. p.save(true);
  815. }
  816. } catch (NodataFoundException ex) {
  817. Log.Debug(this, ex.getMessage());//jemand hats gel??scht:-/
  818. originalproductsids = -1;
  819. save(true);
  820. }
  821. }
  822. }
  823. @Override
  824. public void triggerOnUpdate() {
  825. }
  826. @Override
  827. public void triggerOnDelete() {
  828. // Log.Debug(this, "Deleting... " + IDENTITY);
  829. }
  830. @Override
  831. public String toString() {
  832. if (description != null && description.length() > 15) {
  833. return description.substring(0, 14);
  834. } else {
  835. return description;
  836. }
  837. }
  838. /**
  839. * @return the order
  840. */
  841. @Persistable(true)
  842. public int getOrdernr() {
  843. return ordernr;
  844. }
  845. /**
  846. * @param ordernr the order to set
  847. */
  848. public void setOrdernr(int ordernr) {
  849. this.ordernr = ordernr;
  850. }
  851. /**
  852. * @return the inttype
  853. */
  854. @Persistable(true)
  855. public int getInttype() {
  856. return inttype;
  857. }
  858. /**
  859. * @param inttype the inttype to set
  860. */
  861. public void setInttype(int inttype) {
  862. this.inttype = inttype;
  863. }
  864. }