PageRenderTime 103ms CodeModel.GetById 40ms app.highlight 13ms RepoModel.GetById 47ms app.codeStats 0ms

/src/mpv5/utils/export/DTAFile.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 146 lines | 108 code | 17 blank | 21 comment | 6 complexity | b5dafd496f50b6ee211df4d5aa45aede 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.utils.export;
 18
 19import java.io.File;
 20import java.io.IOException;
 21import java.math.BigDecimal;
 22import java.math.RoundingMode;
 23import java.text.DecimalFormat;
 24import java.text.DecimalFormatSymbols;
 25import java.util.ArrayList;
 26import java.util.HashMap;
 27import java.util.Iterator;
 28import java.util.List;
 29import java.util.Vector;
 30import mpv5.db.common.Context;
 31import mpv5.db.objects.Contact;
 32import mpv5.db.objects.Item;
 33import mpv5.db.objects.Tax;
 34import mpv5.db.objects.User;
 35import mpv5.globals.Messages;
 36import mpv5.handler.VariablesHandler;
 37import mpv5.logging.Log;
 38import mpv5.ui.dialogs.Popup;
 39import mpv5.ui.frames.MPView;
 40import mpv5.utils.date.DateConverter;
 41import dtaus.DTAus;
 42import dtaus.Konto;
 43import mpv5.utils.files.FileDirectoryHandler;
 44import mpv5.utils.files.FileReaderWriter;
 45import mpv5.utils.jobs.Waitable;
 46
 47/**
 48 *
 49 *  
 50 */
 51public class DTAFile extends Exportable implements Waitable {
 52
 53    private DTAFile(String pathToFile) {
 54        super(pathToFile);
 55        if (!exists()) {
 56            try {
 57                createNewFile();
 58            } catch (IOException ex) {
 59                Log.Debug(ex);
 60            }
 61        }
 62    }
 63
 64    public DTAFile(HashMap<String, Object> map) {
 65        this(FileDirectoryHandler.getTempFile("export-" + DateConverter.getTodayDBDate(), "dtaus").getAbsolutePath());
 66        setData(map);
 67    }
 68
 69    @Override
 70    public void run() {
 71
 72        try {
 73
 74            Log.Debug(this, "run: ");
 75            mpv5.YabsViewProxy.instance().setWaiting(true);
 76            Log.Debug(this, "All fields:");
 77            HashMap<String, Object> datas = getData();
 78
 79            DTAus dta = null;
 80            try {
 81                dta = new DTAus(User.getCurrentUser().getDTAAccount(), "lk");
 82            } catch (Exception e) {
 83                Popup.notice(Messages.DTAUS_NOT_SET);
 84                return;
 85            }
 86            List<String> control = new ArrayList<String>();
 87
 88            for (Iterator<String> it = datas.keySet().iterator(); it.hasNext();) {
 89                Item dbo = (Item) datas.get(it.next());
 90                Contact c = (Contact) Item.getObject(Context.getContact(), dbo.__getContactsids());
 91
 92                Konto k = new Konto(c.__getBankid(), c.__getBankaccount(), c.__getBankname());
 93//
 94                DTAus.Transaction t = dta.new Transaction(k);
 95                List<String> usages = User.getCurrentUser().getDTAUsages();
 96                for (int i = 0; i < usages.size(); i++) {
 97                    String string = usages.get(i);
 98                    t.addUsage(VariablesHandler.parse(string, dbo));
 99                }
100
101                String cid = "";
102                if (c.__getCNumber().length() > 10) {
103                    cid = c.__getCNumber().substring(c.__getCNumber().length() - 10, c.__getCNumber().length());
104                } else {
105                    cid = c.__getCNumber();
106                }
107                t.internalCustomerId = cid;
108
109                BigDecimal value = null;
110
111                value = //netvalue
112                            dbo.__getTaxvalue().add(dbo.__getNetvalue());
113
114                if (value.doubleValue() > 0.15) {
115                    value.setScale(2, RoundingMode.HALF_UP);
116                    DecimalFormat f = new DecimalFormat("##,###0.00");
117                    DecimalFormatSymbols sym = new DecimalFormatSymbols();
118                    sym.setDecimalSeparator(',');
119                    sym.setGroupingSeparator('.');
120                    f.setDecimalFormatSymbols(sym);
121                    t.setValue(f.format(value.doubleValue()));
122                    dta.addEntry(t);
123                    control.add(c.__getCname() + "\t" + c.__getBankaccount() + "\t" + c.__getBankname() + "\t" + dbo.__getCnumber() + "\t" + value.toPlainString());
124                }
125            }
126
127            FileReaderWriter w = new FileReaderWriter(this);
128            w.writeOnce(dta.toDTAstring());
129            Popup.notice(control, Messages.DTAUS_CREATED);
130
131        } catch (Exception ex) {
132            Log.Debug(ex);
133        } finally {
134            mpv5.YabsViewProxy.instance().setWaiting(false);
135        }
136    }
137
138    public Exception waitFor() {
139        try {
140            new Thread(this).start();
141        } catch (Exception e) {
142            return e;
143        }
144        return null;
145    }
146}