PageRenderTime 97ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mpv5/db/objects/ActivityListSubItem.java

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