OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 177 | 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
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
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.generationEcritures;
15
 
83 ilm 16
import org.openconcerto.erp.core.common.element.BanqueSQLElement;
18 ilm 17
import org.openconcerto.erp.core.finance.accounting.element.ComptePCESQLElement;
18
import org.openconcerto.erp.core.finance.accounting.element.JournalSQLElement;
19
import org.openconcerto.erp.core.finance.payment.element.ModeDeReglementSQLElement;
180 ilm 20
import org.openconcerto.erp.generationEcritures.provider.AccountingRecordsProvider;
21
import org.openconcerto.erp.generationEcritures.provider.AccountingRecordsProviderManager;
18 ilm 22
import org.openconcerto.erp.model.PrixTTC;
23
import org.openconcerto.sql.model.SQLRow;
24
import org.openconcerto.sql.model.SQLRowValues;
25
import org.openconcerto.sql.model.SQLTable;
177 ilm 26
import org.openconcerto.sql.model.Where;
27
import org.openconcerto.sql.request.UpdateBuilder;
18 ilm 28
 
29
import java.sql.SQLException;
30
import java.text.DateFormat;
31
import java.text.SimpleDateFormat;
177 ilm 32
import java.util.ArrayList;
18 ilm 33
import java.util.Date;
34
import java.util.HashMap;
80 ilm 35
import java.util.List;
18 ilm 36
import java.util.Map;
37
 
38
public class GenerationReglementAchat extends GenerationEcritures {
39
 
180 ilm 40
 
41
    public static final String ID = "accounting.records.order.supplychain.payment";
42
 
43
 
18 ilm 44
    // Journal Caisse
45
    private static final Integer journalCaisse = new Integer(JournalSQLElement.CAISSES);
46
    private static final SQLTable tablePrefCompte = base.getTable("PREFS_COMPTE");
47
    private static final SQLTable tableMouvement = base.getTable("MOUVEMENT");
48
    private static final SQLRow rowPrefsCompte = tablePrefCompte.getRow(2);
49
 
73 ilm 50
    public GenerationReglementAchat(int idRegMontant) throws Exception {
18 ilm 51
 
52
        SQLRow regMontantRow = base.getTable("REGLER_MONTANT").getRow(idRegMontant);
53
 
80 ilm 54
        SQLRow rowFournisseur = regMontantRow.getForeign("ID_FOURNISSEUR");
18 ilm 55
 
56
        System.err.println("Génération des ecritures du reglement du mouvement " + this.idMvt);
57
 
58
        SQLRow modeRegRow = base.getTable("MODE_REGLEMENT").getRow(regMontantRow.getInt("ID_MODE_REGLEMENT"));
59
        SQLRow typeRegRow = base.getTable("TYPE_REGLEMENT").getRow(modeRegRow.getInt("ID_TYPE_REGLEMENT"));
60
 
61
        System.err.println("Mode de reglement " + regMontantRow.getInt("ID_MODE_REGLEMENT"));
62
 
63
        PrixTTC prixTTC = new PrixTTC(((Long) regMontantRow.getObject("MONTANT")).longValue());
64
 
65
        // iniatilisation des valeurs de la map
66
        this.date = (Date) regMontantRow.getObject("DATE");
67
 
68
        // "Règlement achat" + SOURCE.getNom() ??
69
        this.nom = "Règlement achat " + rowFournisseur.getString("NOM") + " (" + typeRegRow.getString("NOM") + ")";
180 ilm 70
        AccountingRecordsProvider provider = AccountingRecordsProviderManager.get(ID);
71
        if (provider != null) {
72
            provider.putLabel(regMontantRow, this.mEcritures);
73
        }
18 ilm 74
 
80 ilm 75
        List<SQLRow> l = regMontantRow.getReferentRows(regMontantRow.getTable().getTable("REGLER_MONTANT_ELEMENT"));
76
        int mvtSource = -1;
177 ilm 77
        List<Integer> factureFids = new ArrayList<Integer>();
78
 
80 ilm 79
        for (SQLRow sqlRow : l) {
80
            SQLRow mvtEch = sqlRow.getForeignRow("ID_MOUVEMENT_ECHEANCE");
81
            if (mvtEch.getID() != mvtSource) {
82
                getNewMouvement("REGLER_MONTANT", idRegMontant, mvtEch.getID(), mvtEch.getInt("ID_PIECE"));
83
                if (mvtSource == -1) {
84
                    mvtSource = mvtEch.getID();
85
                }
86
            }
177 ilm 87
            if (!sqlRow.isForeignEmpty("ID_ECHEANCE_FOURNISSEUR")) {
88
                SQLRow ech = sqlRow.getForeign("ID_ECHEANCE_FOURNISSEUR");
89
                if (!ech.isForeignEmpty("ID_FACTURE_FOURNISSEUR")) {
90
                    factureFids.add(ech.getForeignID("ID_FACTURE_FOURNISSEUR"));
91
                }
92
            }
80 ilm 93
        }
94
 
95
        SQLRow rowMvtSource = tableMouvement.getRow(mvtSource);
156 ilm 96
        if (rowMvtSource == null) {
97
            throw new IllegalStateException("Aucun mouvement source associé aux échéances.\n(Mouvement source : " + mvtSource + ", REGLER_MONTANT " + regMontantRow.getID() + ")");
98
        }
80 ilm 99
 
18 ilm 100
        // si paiement comptant
101
        if ((modeRegRow.getInt("AJOURS") == 0) && (modeRegRow.getInt("LENJOUR") == 0)) {
102
 
103
            System.err.println("Règlement Comptant");
104
            // test Cheque
105
            if (typeRegRow.getID() == 2) {
106
 
107
                // Ajout dans cheque fournisseur
80 ilm 108
                paiementCheque(this.date, rowMvtSource, rowFournisseur.getID(), idRegMontant);
18 ilm 109
            } else {
110
 
111
                if (typeRegRow.getID() == 4) {
149 ilm 112
                    this.putValue("ID_JOURNAL", GenerationReglementAchat.journalCaisse);
18 ilm 113
                } else {
83 ilm 114
                    fillJournalBanqueFromRow(modeRegRow);
18 ilm 115
                }
116
 
117
                // SQLRow echeanceRow = base.getTable("ECHEANCE_FOURNISSEUR").getRow(idEchFourn);
118
 
80 ilm 119
                this.idMvt = getNewMouvement("REGLER_MONTANT", idRegMontant, rowMvtSource.getID(), rowMvtSource.getInt("ID_PIECE"));
120
 
149 ilm 121
                this.putValue("DATE", this.date);
122
                this.putValue("NOM", this.nom);
123
                this.putValue("ID_MOUVEMENT", new Integer(this.idMvt));
18 ilm 124
 
125
                // compte Fournisseurs
126
                int idCompteFourn = rowFournisseur.getInt("ID_COMPTE_PCE");
127
 
128
                if (idCompteFourn <= 1) {
129
                    idCompteFourn = rowPrefsCompte.getInt("ID_COMPTE_PCE_FOURNISSEUR");
130
                    if (idCompteFourn <= 1) {
73 ilm 131
                        idCompteFourn = ComptePCESQLElement.getIdComptePceDefault("Fournisseurs");
132
 
18 ilm 133
                    }
134
                }
135
 
149 ilm 136
                this.putValue("ID_COMPTE_PCE", new Integer(idCompteFourn));
137
                this.putValue("DEBIT", new Long(prixTTC.getLongValue()));
138
                this.putValue("CREDIT", new Long(0));
73 ilm 139
                ajoutEcriture();
140
 
141
                // compte de reglement, caisse, CB, ...
83 ilm 142
                fillCompteBanqueFromRow(modeRegRow, "AchatCB", true);
149 ilm 143
                this.putValue("DEBIT", new Long(0));
144
                this.putValue("CREDIT", new Long(prixTTC.getLongValue()));
73 ilm 145
                ajoutEcriture();
146
 
18 ilm 147
            }
177 ilm 148
 
149
            setDateReglement(regMontantRow.getTable().getTable("FACTURE_FOURNISSEUR"), factureFids, this.date);
150
 
18 ilm 151
        } else {
152
 
153
            Date dateEch = ModeDeReglementSQLElement.calculDate(modeRegRow.getInt("AJOURS"), modeRegRow.getInt("LENJOUR"), this.date);
154
            DateFormat dateFormat = new SimpleDateFormat();
155
            System.out.println("Date d'échéance " + dateFormat.format(dateEch));
156
            // System.out.println("Echeance" + dateEch);
157
 
158
            // Cheque
159
            if (typeRegRow.getID() == 2) {
160
 
161
                // Ajout dans cheque fournisseur
80 ilm 162
                paiementCheque(dateEch, rowMvtSource, rowFournisseur.getID(), idRegMontant);
18 ilm 163
            } else {
164
 
165
                // Ajout dans echeance
73 ilm 166
                Map<String, Object> mEcheance = new HashMap<String, Object>();
80 ilm 167
 
177 ilm 168
                this.idMvt = getNewMouvement(rowMvtSource.getString("SOURCE"), rowMvtSource.getInt("IDSOURCE"), rowMvtSource.getID(), rowMvtSource.getInt("ID_PIECE"));
18 ilm 169
                mEcheance.put("ID_MOUVEMENT", new Integer(this.idMvt));
170
 
171
                mEcheance.put("DATE", dateEch);
172
                mEcheance.put("MONTANT", new Long(prixTTC.getLongValue()));
173
 
80 ilm 174
                mEcheance.put("ID_FOURNISSEUR", rowFournisseur.getID());
18 ilm 175
 
176
                SQLRowValues valEcheance = new SQLRowValues(base.getTable("ECHEANCE_FOURNISSEUR"), mEcheance);
73 ilm 177
                if (valEcheance.getInvalid() == null) {
178
                    // ajout de l'ecriture
179
                    SQLRow row = valEcheance.insert();
180
                    SQLRowValues rowVals = new SQLRowValues(tableMouvement);
181
                    rowVals.put("IDSOURCE", row.getID());
182
                    rowVals.update(this.idMvt);
183
                }
18 ilm 184
 
185
            }
186
        }
187
    }
188
 
177 ilm 189
    private void setDateReglement(SQLTable table, List<Integer> ids, Date d) throws SQLException {
190
        if (!ids.isEmpty()) {
191
            UpdateBuilder b = new UpdateBuilder(table);
192
            b.setObject("DATE_REGLEMENT", d);
193
            b.setWhere(new Where(table.getKey(), ids));
194
            table.getDBSystemRoot().getDataSource().execute(b.asString());
195
        }
196
    }
197
 
80 ilm 198
    private void paiementCheque(Date dateEch, SQLRow rowMvtSource, int idFourn, int idRegMontant) throws SQLException {
18 ilm 199
 
200
        SQLRow regMontantRow = base.getTable("REGLER_MONTANT").getRow(idRegMontant);
201
        PrixTTC prixTTC = new PrixTTC(((Long) regMontantRow.getObject("MONTANT")).longValue());
202
 
203
        SQLRowValues valCheque = new SQLRowValues(base.getTable("CHEQUE_FOURNISSEUR"));
204
 
80 ilm 205
        valCheque.put("ID_FOURNISSEUR", idFourn);
18 ilm 206
        valCheque.put("DATE_ACHAT", this.date);
207
        valCheque.put("DATE_MIN_DECAISSE", dateEch);
83 ilm 208
        if (!regMontantRow.isForeignEmpty("ID_MODE_REGLEMENT")) {
209
            SQLRow rowModeRegl = regMontantRow.getForeignRow("ID_MODE_REGLEMENT");
210
            valCheque.put("ID_" + BanqueSQLElement.TABLENAME, rowModeRegl.getInt("ID_" + BanqueSQLElement.TABLENAME));
211
        }
80 ilm 212
 
213
        this.idMvt = getNewMouvement("CHEQUE_FOURNISSEUR", 1, rowMvtSource.getID(), rowMvtSource.getInt("ID_PIECE"));
18 ilm 214
        valCheque.put("ID_MOUVEMENT", new Integer(this.idMvt));
215
        valCheque.put("MONTANT", new Long(prixTTC.getLongValue()));
216
 
73 ilm 217
        if (valCheque.getInvalid() == null) {
218
            // ajout de l'ecriture
219
            SQLRow row = valCheque.insert();
220
            SQLRowValues rowVals = new SQLRowValues(tableMouvement);
221
            rowVals.put("IDSOURCE", row.getID());
222
            rowVals.update(this.idMvt);
223
        }
18 ilm 224
 
225
    }
226
}