PageRenderTime 13ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mpv5/db/objects/ActivityList.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 216 lines | 161 code | 28 blank | 27 comment | 13 complexity | e4e8c156ca942b870a61504ffabdd40b 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 enoa.handler.TableHandler;
 20import enoa.handler.TemplateHandler;
 21import java.math.BigDecimal;
 22import java.text.DateFormat;
 23import java.util.ArrayList;
 24import java.util.Collections;
 25import java.util.HashMap;
 26import java.util.List;
 27import java.util.Locale;
 28import java.util.Vector;
 29import javax.swing.JComponent;
 30
 31import mpv5.db.common.Context;
 32import mpv5.db.common.DatabaseObject;
 33import mpv5.db.common.Formattable;
 34import mpv5.db.common.NodataFoundException;
 35import mpv5.db.common.Templateable;
 36import mpv5.globals.Constants;
 37import mpv5.globals.GlobalSettings;
 38import mpv5.globals.Messages;
 39import mpv5.handler.FormatHandler;
 40import mpv5.logging.Log;
 41import mpv5.ui.panels.ActivityConfirmationPanel;
 42import mpv5.utils.images.MPIcon;
 43import mpv5.utils.numberformat.FormatNumber;
 44import mpv5.utils.text.TypeConversion;
 45
 46/**
 47 *
 48 * 
 49 */
 50public class ActivityList extends DatabaseObject implements Formattable, Templateable {
 51
 52    private static final long serialVersionUID = 1L;
 53    private String cnumber;
 54    private int contactsids;
 55    private int orderids;
 56    private BigDecimal totalamount = BigDecimal.ZERO;
 57    private FormatHandler formatHandler;
 58
 59    public ActivityList() {
 60        setContext(Context.getActivityList());
 61    }
 62
 63    @Override
 64    public JComponent getView() {
 65        return ActivityConfirmationPanel.instanceOf();
 66    }
 67
 68    @Override
 69    public mpv5.utils.images.MPIcon getIcon() {
 70        return new MPIcon("/mpv5/resources/images/22/folder_documents.png");
 71    }
 72
 73    @Override
 74    public boolean save(boolean b) {
 75        if (getCname().length() == 0) {
 76            setCname("<unnamed>");
 77        }
 78        return super.save(b);
 79    }
 80
 81    public int __getContactsids() {
 82        return contactsids;
 83    }
 84
 85    public void setContactsids(int contactsids) {
 86        this.contactsids = contactsids;
 87    }
 88
 89    public int __getOrderids() {
 90        return orderids;
 91    }
 92
 93    public void setOrderids(int orderids) {
 94        this.orderids = orderids;
 95    }
 96
 97    public BigDecimal __getTotalamount() {
 98        return totalamount;
 99    }
100
101    public void setTotalamount(BigDecimal totalamount) {
102        this.totalamount = totalamount;
103    }
104
105    public String __getCNumber() {
106        return cnumber;
107    }
108
109    public void setCNumber(String cnumber) {
110        this.cnumber = cnumber;
111    }
112
113    /**
114     * @return the formatHandler
115     */
116    public synchronized FormatHandler getFormatHandler() {
117        if (formatHandler == null) {
118            formatHandler = new FormatHandler(this);
119        }
120        return formatHandler;
121    }
122
123    public int templateType() {
124        return Constants.TYPE_ACTIVITY;
125    }
126
127    public int templateGroupIds() {
128        return __getGroupsids();
129    }
130
131    public void defineFormatHandler(FormatHandler handler) {
132        formatHandler = handler;
133    }
134
135    @Override
136    public void ensureUniqueness() {
137        setCNumber(getFormatHandler().next());
138    }
139
140    @Override
141    public HashMap<String, Object> resolveReferences(HashMap<String, Object> map) {
142        Contact dbo;
143        if (map.containsKey("contactsids")) {
144            try {
145                try {
146                    dbo = (Contact) DatabaseObject.getObject(Context.getContact(), Integer.valueOf(map.get("contactsids").toString()));
147                    map.put("contact", dbo);
148                    //map.putAll(dbo.getValues4());
149                    map.remove("contactsids");
150                } catch (NodataFoundException ex) {
151                    map.put("contact", null);
152                    Log.Debug(this, ex.getMessage());
153                }
154            } catch (NumberFormatException numberFormatException) {
155                //already resolved?
156            }
157        }
158
159        List<ActivityListSubItem> data;
160        List<String[]> data2;
161        ArrayList<String[]> list = new ArrayList<String[]>();
162
163        try {
164            data = DatabaseObject.getReferencedObjects(this, Context.getActivityListItems(), new ActivityListSubItem());
165            Collections.sort(data, ActivityListSubItem.ORDER_COMPARATOR);
166
167            for (int i = 0; i < data.size(); i++) {
168                ActivityListSubItem t = data.get(i);
169                list.add(t.toStringArray(TemplateHandler.loadTemplate(this.templateGroupIds(), this.templateType())));
170                if (GlobalSettings.getBooleanProperty("org.openyabs.exportproperty.pdftable", false)) {
171                    data2 = t.getValues3();
172                    for (int j = 0; j < data2.size(); j++) {
173                        String[] strings = data2.get(j);
174                        map.put("subactivity" + i + "." + strings[0].toLowerCase(), strings[1]);
175                    }
176                }
177            }
178        } catch (NodataFoundException ex) {
179            Log.Debug(this, ex.getMessage());
180        }
181
182        map.put(TableHandler.KEY_TABLE + "1", list);
183
184        //date format localization
185        if (mpv5.db.objects.User.getCurrentUser().getProperties().hasProperty("item.date.locale")) {
186            Locale l = null;
187            try {
188                l = TypeConversion.stringToLocale(mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("item.date.locale"));
189            } catch (Exception e) {
190            }
191            if (l != null) {
192                map.put("dateadded", DateFormat.getDateInstance(DateFormat.MEDIUM, l).format(__getDateadded()));
193            } else {
194                Log.Debug(this, "Error while using item.date.locale");
195            }
196        }
197        map.put("grosvaluef", FormatNumber.formatLokalCurrency(__getTotalamount()));
198        return super.resolveReferences(map);
199    }
200
201    public Object[] getDataForInvoice() {
202        //"Internal ID", "ID", "Count", "Measure", "Description", "Netto Price", "Tax Value", "Total Price
203        Object[] data = new Object[8];
204        String defunit = "";
205        if (mpv5.db.objects.User.getCurrentUser().getProperties().hasProperty("defunit")) {
206            defunit = mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("defunit");
207        }
208        data[2] = new BigDecimal("1.0");
209        data[3] = defunit;
210        data[4] = Messages.ActivityList_toSubItem.toString() + this.__getCname();
211        data[5] = this.__getTotalamount();
212        data[6] = new BigDecimal("0.0");
213        data[7] = this.__getTotalamount();
214        return data;
215    }
216}