OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
18 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
18 ilm 5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.core.sales.pos.element;
15
 
182 ilm 16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
18 ilm 17
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
182 ilm 18
import org.openconcerto.erp.core.finance.payment.element.TypeReglementSQLElement;
19
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
20
import org.openconcerto.erp.core.sales.pos.TicketSheetXML;
21
import org.openconcerto.erp.core.sales.pos.model.Article;
22
import org.openconcerto.erp.core.sales.pos.model.Categorie;
23
import org.openconcerto.erp.core.sales.pos.model.Paiement;
24
import org.openconcerto.erp.core.sales.pos.model.ReceiptCode;
25
import org.openconcerto.erp.core.sales.pos.model.Ticket;
26
import org.openconcerto.sql.Configuration;
18 ilm 27
import org.openconcerto.sql.element.SQLComponent;
182 ilm 28
import org.openconcerto.sql.element.SQLElement;
29
import org.openconcerto.sql.element.SQLElementDirectory;
18 ilm 30
import org.openconcerto.sql.element.UISQLComponent;
144 ilm 31
import org.openconcerto.sql.model.SQLBase;
182 ilm 32
import org.openconcerto.sql.model.SQLRow;
33
import org.openconcerto.sql.model.SQLRowListRSH;
34
import org.openconcerto.sql.model.SQLSelect;
144 ilm 35
import org.openconcerto.sql.utils.PartialUniqueTrigger;
182 ilm 36
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
37
import org.openconcerto.sql.view.list.action.SQLRowValuesAction.PredicateRowAction;
38
import org.openconcerto.utils.DecimalUtils;
39
import org.openconcerto.utils.ExceptionHandler;
18 ilm 40
 
182 ilm 41
import java.math.BigDecimal;
42
import java.text.ParseException;
18 ilm 43
import java.util.ArrayList;
144 ilm 44
import java.util.Arrays;
182 ilm 45
import java.util.Collections;
46
import java.util.HashMap;
18 ilm 47
import java.util.List;
182 ilm 48
import java.util.Map;
18 ilm 49
 
50
import javax.swing.JTextField;
51
 
52
public class TicketCaisseSQLElement extends ComptaSQLConfElement {
53
 
144 ilm 54
    static public final String UNARCHIVED_WHERE = SQLBase.quoteIdentifier("ARCHIVE") + " = " + 0;
55
    static public final String DATE_WHERE = UNARCHIVED_WHERE + " and " + SQLBase.quoteIdentifier("FILE_HASH") + " is not null";
56
 
174 ilm 57
    // needed until H2 databases are upgraded to use JS code
144 ilm 58
    static public final class UniqueNumber_PartialUniqueTrigger extends PartialUniqueTrigger {
59
        public UniqueNumber_PartialUniqueTrigger() {
60
            super(Arrays.asList("NUMERO"), UNARCHIVED_WHERE);
61
        }
62
    }
63
 
174 ilm 64
    // needed until H2 databases are upgraded to use JS code
144 ilm 65
    static public final class UniqueDate_PartialUniqueTrigger extends PartialUniqueTrigger {
66
        public UniqueDate_PartialUniqueTrigger() {
67
            super(Arrays.asList("ID_CAISSE", "DATE"), DATE_WHERE);
68
        }
69
    }
70
 
18 ilm 71
    public TicketCaisseSQLElement() {
72
        super("TICKET_CAISSE", "un ticket de caisse", "tickets de caisses");
182 ilm 73
 
74
        PredicateRowAction action = new PredicateRowAction(true, "ticket.document.generate", (le) -> {
75
            final TicketSheetXML bSheet = new TicketSheetXML(createTicket(le.getSelectedRow().asRow()), ComptaPropsConfiguration.getInstanceCompta());
76
            try {
77
                bSheet.createDocument();
78
                bSheet.showPrintAndExport(true, false, false, Collections.emptyList());
79
            } catch (Exception originalExn) {
80
                ExceptionHandler.handle("Erreur lors de la création de la facture", originalExn);
81
            }
82
        });
83
 
84
        action.setPredicate(IListeEvent.getSingleSelectionPredicate());
85
        getRowValuesActions().add(action);
18 ilm 86
    }
87
 
88
    protected List<String> getListFields() {
89
        final List<String> l = new ArrayList<String>();
90
        l.add("NUMERO");
21 ilm 91
        l.add("DATE");
92
        l.add("ID_CAISSE");
93
        l.add("TOTAL_HT");
18 ilm 94
        l.add("TOTAL_TTC");
95
        return l;
96
    }
97
 
98
    protected List<String> getComboFields() {
99
        final List<String> l = new ArrayList<String>();
100
        l.add("NOM");
101
        return l;
102
    }
103
 
104
    /*
105
     * (non-Javadoc)
106
     *
107
     * @see org.openconcerto.devis.SQLElement#getComponent()
108
     */
109
    public SQLComponent createComponent() {
110
        return new UISQLComponent(this) {
111
            public void addViews() {
112
                this.addRequiredSQLObject(new JTextField(), "NOM", "right");
113
            }
114
        };
115
    }
57 ilm 116
 
182 ilm 117
    public Ticket createTicket(SQLRow row) {
118
        // TODO merger ce code avec CaissepPanel.loadArticles si possible
119
        final Ticket t;
120
        try {
121
            t = new Ticket(new ReceiptCode(row.getString("NUMERO")), row.getDate("DATE"), row.getString("FILE_HASH_PREVIOUS"));
122
        } catch (ParseException e) {
123
            throw new IllegalStateException("Couldn't parse " + row, e);
124
        }
125
 
126
        final SQLElementDirectory directory = Configuration.getInstance().getDirectory();
127
        SQLElement eltEncaisser = directory.getElement("ENCAISSER_MONTANT");
128
        List<SQLRow> l = row.getReferentRows(eltEncaisser.getTable());
129
        for (SQLRow row2 : l) {
130
            long montant = row2.getLong("MONTANT");
131
            SQLRow rowMode = row2.getForeign("ID_MODE_REGLEMENT");
132
            int type = Paiement.CB;
133
            if (rowMode.getInt("ID_TYPE_REGLEMENT") == TypeReglementSQLElement.CB) {
134
                type = Paiement.CB;
135
            } else {
136
                if (rowMode.getInt("ID_TYPE_REGLEMENT") == TypeReglementSQLElement.CHEQUE) {
137
                    type = Paiement.CHEQUE;
138
                } else {
139
                    if (rowMode.getInt("ID_TYPE_REGLEMENT") == TypeReglementSQLElement.ESPECE) {
140
                        type = Paiement.ESPECES;
141
                    }
142
                }
143
            }
144
            Paiement p = new Paiement(type);
145
            p.setMontantInCents((int) montant);
146
            t.addPaiement(p);
147
        }
148
 
149
        SQLElement eltArticle = directory.getElement("SAISIE_VENTE_FACTURE_ELEMENT");
150
 
151
        final SQLSelect selUniteVente = new SQLSelect();
152
        selUniteVente.addSelectStar(directory.getElement("UNITE_VENTE").getTable());
153
        final Map<Integer, String> mapUniteVenteName = new HashMap<>();
154
        for (SQLRow rowUniteVente : SQLRowListRSH.execute(selUniteVente)) {
155
            mapUniteVenteName.put(rowUniteVente.getID(), rowUniteVente.getString("CODE"));
156
        }
157
 
158
        List<SQLRow> l2 = row.getReferentRows(eltArticle.getTable());
159
        Categorie c = new Categorie("");
160
        for (SQLRow row2 : l2) {
161
            Article a = new Article(c, row2.getString("NOM"), row2.getInt("ID_ARTICLE"));
162
            if (row2.getInt("ID_UNITE_VENTE") != 2) {
163
                a.setSalesUnit(mapUniteVenteName.get(row2.getInt("ID_UNITE_VENTE")));
164
            }
165
            BigDecimal ht = (BigDecimal) row2.getObject("PV_HT");
166
            a.setPriceWithoutTax(ht);
167
 
168
            BigDecimal percentRemise = row2.getBigDecimal("POURCENT_REMISE");
169
            if (percentRemise != null) {
170
                a.setDiscountPct(percentRemise.movePointLeft(2));
171
            }
172
 
173
            int idTaxe = row2.getInt("ID_TAXE");
174
            float tva = TaxeCache.getCache().getTauxFromId(idTaxe);
175
            a.setPriceWithTax(ht.multiply(BigDecimal.valueOf(1.0 + (tva / 100.0D)), DecimalUtils.HIGH_PRECISION));
176
            a.setIdTaxe(idTaxe);
177
            t.addArticle(a);
178
            if (a.getSalesUnit() == null) {
179
                t.setArticleCount(a, new BigDecimal(row2.getInt("QTE")));
180
            } else {
181
                t.setArticleCount(a, row2.getBigDecimal("QTE_UNITAIRE"));
182
            }
183
        }
184
 
185
        return t;
186
    }
187
 
57 ilm 188
    @Override
189
    protected String createCode() {
156 ilm 190
        return createCodeOfPackage() + ".ticket";
57 ilm 191
    }
18 ilm 192
}