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