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