OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 180 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 180 Rev 182
Line 1... Line 1...
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
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
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
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.
9
 * language governing permissions and limitations under the License.
Line 13... Line 13...
13
 
13
 
14
 package org.openconcerto.erp.core.sales.pos.ui;
14
 package org.openconcerto.erp.core.sales.pos.ui;
15
 
15
 
16
import org.openconcerto.erp.core.sales.pos.POSConfiguration;
16
import org.openconcerto.erp.core.sales.pos.POSConfiguration;
17
import org.openconcerto.erp.core.sales.pos.io.BarcodeReader;
17
import org.openconcerto.erp.core.sales.pos.io.BarcodeReader;
18
import org.openconcerto.erp.core.sales.pos.io.ConcertProtocol;
-
 
19
import org.openconcerto.erp.core.sales.pos.io.ESCSerialDisplay;
18
import org.openconcerto.erp.core.sales.pos.io.ESCSerialDisplay;
20
import org.openconcerto.erp.core.sales.pos.io.TicketPrinter;
19
import org.openconcerto.erp.core.sales.pos.io.TicketPrinter;
21
import org.openconcerto.erp.core.sales.pos.model.Article;
20
import org.openconcerto.erp.core.sales.pos.model.Article;
22
import org.openconcerto.erp.core.sales.pos.model.Client;
21
import org.openconcerto.erp.core.sales.pos.model.Client;
23
import org.openconcerto.erp.core.sales.pos.model.Paiement;
22
import org.openconcerto.erp.core.sales.pos.model.Paiement;
Line 27... Line 26...
27
import org.openconcerto.erp.core.sales.pos.model.RegisterLogEntry.ReceiptEntry;
26
import org.openconcerto.erp.core.sales.pos.model.RegisterLogEntry.ReceiptEntry;
28
import org.openconcerto.erp.core.sales.pos.model.RegisterState.Status;
27
import org.openconcerto.erp.core.sales.pos.model.RegisterState.Status;
29
import org.openconcerto.erp.core.sales.pos.model.Ticket;
28
import org.openconcerto.erp.core.sales.pos.model.Ticket;
30
import org.openconcerto.erp.core.sales.pos.model.TicketItem;
29
import org.openconcerto.erp.core.sales.pos.model.TicketItem;
31
import org.openconcerto.erp.preferences.TemplateNXProps;
30
import org.openconcerto.erp.preferences.TemplateNXProps;
-
 
31
import org.openconcerto.erp.utils.SoundGenerator;
32
import org.openconcerto.sql.element.SQLElementDirectory;
32
import org.openconcerto.sql.element.SQLElementDirectory;
33
import org.openconcerto.utils.FileUtils;
33
import org.openconcerto.utils.FileUtils;
34
import org.openconcerto.utils.StringUtils;
34
import org.openconcerto.utils.StringUtils;
35
 
35
 
36
import java.awt.event.KeyEvent;
36
import java.awt.event.KeyEvent;
Line 43... Line 43...
43
import java.text.ParseException;
43
import java.text.ParseException;
44
import java.util.ArrayList;
44
import java.util.ArrayList;
45
import java.util.Calendar;
45
import java.util.Calendar;
46
import java.util.HashSet;
46
import java.util.HashSet;
47
import java.util.List;
47
import java.util.List;
-
 
48
import java.util.Map;
48
import java.util.Set;
49
import java.util.Set;
49
import java.util.Timer;
50
import java.util.Timer;
50
import java.util.TimerTask;
51
import java.util.TimerTask;
51
 
52
 
52
import javax.swing.JOptionPane;
-
 
53
 
-
 
54
import org.jdom2.JDOMException;
53
import org.jdom2.JDOMException;
55
 
54
 
56
public class CaisseControler implements BarcodeListener {
55
public class CaisseControler implements BarcodeListener {
57
 
56
 
58
    private Article articleSelected;
57
    private Article articleSelected;
Line 170... Line 169...
170
 
169
 
171
    // Articles
170
    // Articles
172
    public void addArticle(Article a) {
171
    public void addArticle(Article a) {
173
        this.t.addArticle(a);
172
        this.t.addArticle(a);
174
        fire();
173
        fire();
-
 
174
 
175
        String price = TicketCellRenderer.centsToString(a.getPriceWithTax().movePointRight(2).setScale(0, RoundingMode.HALF_UP).intValue());
175
        String price = TicketCellRenderer.centsToString(a.getPriceWithTax(this.t.getItemCount(a), false).movePointRight(2).setScale(0, RoundingMode.HALF_UP).intValue());
176
        this.setLCD(a.getName(), price, 0);
176
        this.setLCD(a.getName(), price, 0);
177
        this.setLCDDefaultDisplay(2);
177
        this.setLCDDefaultDisplay(2);
178
    }
178
    }
179
 
179
 
180
    void incrementArticle(Article a) {
180
    void incrementArticle(Article a) {
181
        this.t.incrementArticle(a);
181
        this.t.incrementArticle(a);
182
        fire();
182
        fire();
-
 
183
 
183
        String price = TicketCellRenderer.centsToString(a.getPriceWithTax().movePointRight(2).setScale(0, RoundingMode.HALF_UP).intValue());
184
        String price = TicketCellRenderer.centsToString(a.getPriceWithTax(this.t.getItemCount(a), false).movePointRight(2).setScale(0, RoundingMode.HALF_UP).intValue());
184
        this.setLCD(a.getName(), price, 0);
185
        this.setLCD(a.getName(), price, 0);
185
        this.setLCDDefaultDisplay(2);
186
        this.setLCDDefaultDisplay(2);
186
    }
187
    }
187
 
188
 
188
    void removeArticle(Article a) {
189
    void removeArticle(Article a) {
Line 273... Line 274...
273
        } else if (code.equalsIgnoreCase("facture")) {
274
        } else if (code.equalsIgnoreCase("facture")) {
274
 
275
 
275
        } else if (code.equalsIgnoreCase("ticket")) {
276
        } else if (code.equalsIgnoreCase("ticket")) {
276
 
277
 
277
        } else {
278
        } else {
278
            Article a = Article.getArticleFromBarcode(code);
279
            List<Article> list = Article.getArticleFromBarcode(code);
-
 
280
            if (list != null && !list.isEmpty()) {
279
            if (a != null) {
281
                if (list.size() == 1) {
-
 
282
                    final Article a = list.get(0);
280
                System.err.println("CaisseControler.barcodeRead() barcode : " + code + " : product found : " + a.getName());
283
                    System.err.println("CaisseControler.barcodeRead() barcode : " + code + " : product found : " + a.getName());
281
                this.incrementArticle(a);
284
                    this.incrementArticle(a);
282
                this.setArticleSelected(a);
285
                    this.setArticleSelected(a);
283
            } else {
286
                } else {
-
 
287
                    // choix de l'article
-
 
288
                    caisseFrame.showArticleSelector(list, new ArticleSelectionListener() {
-
 
289
                        public void articleSelected(Article a) {
-
 
290
                            System.err.println("CaisseControler.barcodeRead() barcode : " + code + " : product selected : " + a.getName());
-
 
291
                            incrementArticle(a);
-
 
292
                            setArticleSelected(a);
-
 
293
                            caisseFrame.showCaisse();
-
 
294
                        }
-
 
295
 
-
 
296
                    });
-
 
297
                }
-
 
298
            } else {
284
                System.err.println("CaisseControler.barcodeRead() barcode : " + code + " : no product found");
299
                System.err.println("CaisseControler.barcodeRead() barcode : " + code + " : no product found");
285
                Ticket t = Ticket.getTicketFromCode(code, this.caisseFrame.getFiles());
300
                Ticket t = Ticket.getTicketFromCode(code, this.caisseFrame.getFiles());
286
                if (t != null) {
301
                if (t != null) {
287
                    System.err.println("CaisseControler.barcodeRead() barcode : " + code + " : receipt found :" + t.getNumber());
302
                    System.err.println("CaisseControler.barcodeRead() barcode : " + code + " : receipt found :" + t.getNumber());
288
                    this.caisseFrame.showTickets(t);
303
                    this.caisseFrame.showTickets(t);
-
 
304
 
-
 
305
                } else {
-
 
306
                    // Joue un son si rien n'est trouvé
-
 
307
                    new SoundGenerator().playSound(300.0, 0.15, 0.75, SoundGenerator.FADE_NONE, SoundGenerator.WAVE_SQUARE);
-
 
308
 
289
                }
309
                }
290
            }
310
            }
291
        }
311
        }
292
 
312
 
293
    }
313
    }
Line 420... Line 440...
420
            setLCD(getPOSConf().getLCDLine1(), getPOSConf().getLCDLine2(), delay);
440
            setLCD(getPOSConf().getLCDLine1(), getPOSConf().getLCDLine2(), delay);
421
        }
441
        }
422
    }
442
    }
423
 
443
 
424
    public void sendCBRequest(final Paiement p) {
444
    public void sendCBRequest(final Paiement p) {
425
 
-
 
426
        final String creditCardPort = getPOSConf().getCreditCardPort();
-
 
427
        if (creditCardPort != null && creditCardPort.trim().length() > 2) {
-
 
428
            final Thread thread = new Thread(new Runnable() {
-
 
429
 
-
 
430
                @Override
-
 
431
                public void run() {
-
 
432
                    try {
-
 
433
                        ConcertProtocol cp = new ConcertProtocol(creditCardPort);
-
 
434
                        boolean ok = cp.sendCardPayment(p.getMontantInCents(), ConcertProtocol.CURRENCY_EUR);
-
 
435
                        if (ok) {
-
 
436
                            JOptionPane.showMessageDialog(null, "Paiement CB OK");
-
 
437
                        } else {
-
 
438
                            JOptionPane.showMessageDialog(null, "Erreur paiement CB");
-
 
439
                        }
-
 
440
                    } catch (Throwable ex) {
-
 
441
                        JOptionPane.showMessageDialog(null, "Erreur terminal CB");
-
 
442
                    }
-
 
443
                }
-
 
444
            });
-
 
445
            thread.setDaemon(true);
445
        caisseFrame.showCBPanel(p);
446
            thread.start();
-
 
447
 
-
 
448
        }
-
 
449
 
-
 
450
    }
446
    }
451
 
447
 
452
    public void setArticleHT(TicketItem item, BigDecimal ht) {
448
    public void setArticleHT(TicketItem item, BigDecimal ht) {
453
        final Article newArticle = new Article(item.getArticle());
449
        final Article newArticle = new Article(item.getArticle());
454
        newArticle.updatePriceWithoutTax(ht);
450
        newArticle.updatePriceWithoutTax(ht);
Line 470... Line 466...
470
 
466
 
471
    public boolean isTicketValid() {
467
    public boolean isTicketValid() {
472
        return (!this.t.getItems().isEmpty()) && ((this.getTotal() >= 0 && this.getPaidTotal() >= this.getTotal()) || (this.getTotal() < 0 && this.getPaidTotal() == this.getTotal()));
468
        return (!this.t.getItems().isEmpty()) && ((this.getTotal() >= 0 && this.getPaidTotal() >= this.getTotal()) || (this.getTotal() < 0 && this.getPaidTotal() == this.getTotal()));
473
    }
469
    }
474
 
470
 
475
    public Set<Integer> loadFavoriteProductsIds() {
-
 
476
        final TemplateNXProps nxprops = (TemplateNXProps) TemplateNXProps.getInstance();
-
 
477
        final File f = new File(nxprops.getDefaultStringValue(), "favorites.txt");
-
 
478
        System.out.println("CaisseControler.saveFavoriteProductsIds() loading favorites from " + f.getAbsolutePath());
-
 
479
        final Set<Integer> result = new HashSet<>();
-
 
480
        if (f.exists()) {
-
 
481
            try {
-
 
482
                String s = FileUtils.read(f);
-
 
483
                List<String> sIds = StringUtils.fastSplit(s, ',');
-
 
484
                for (String string : sIds) {
-
 
485
                    if (!string.isEmpty()) {
-
 
486
                        result.add(Integer.parseInt(string));
-
 
487
                    }
-
 
488
                }
-
 
489
            } catch (IOException e) {
-
 
490
                e.printStackTrace();
-
 
491
            }
-
 
492
        }
-
 
493
        return result;
-
 
494
    }
-
 
495
 
471
  
496
    public void saveFavoriteProductsIds(List<Article> products) {
-
 
497
        final TemplateNXProps nxprops = (TemplateNXProps) TemplateNXProps.getInstance();
-
 
498
        final File f = new File(nxprops.getDefaultStringValue(), "favorites.txt");
-
 
499
        System.out.println("CaisseControler.saveFavoriteProductsIds() saving favorites to " + f.getAbsolutePath());
-
 
500
        try (FileOutputStream fOut = new FileOutputStream(f);) {
-
 
501
            for (Article product : products) {
-
 
502
                fOut.write(String.valueOf(product.getId()).getBytes());
-
 
503
                fOut.write(',');
-
 
504
            }
-
 
505
            fOut.flush();
-
 
506
        } catch (IOException e) {
-
 
507
            e.printStackTrace();
-
 
508
        }
-
 
509
    }
-
 
510
 
472
 
511
    public void setTicketItemSelected(TicketItem item) {
473
    public void setTicketItemSelected(TicketItem item) {
512
        this.ticketItemSelected = item;
474
        this.ticketItemSelected = item;
513
        if (item == null) {
475
        if (item == null) {
514
            this.articleSelected = null;
476
            this.articleSelected = null;
Line 564... Line 526...
564
 
526
 
565
        this.caisseFrame.showCaisse();
527
        this.caisseFrame.showCaisse();
566
        fire();
528
        fire();
567
    }
529
    }
568
 
530
 
-
 
531
    /**
-
 
532
     * Ouvre la fenetre d'alerte de stock
-
 
533
     * 
-
 
534
     * @param runnable a executer en cas de transfert de stock ou de bypass
-
 
535
     * 
-
 
536
     * @param map TicketItem <-> qté manquante
-
 
537
     */
-
 
538
    public void openStockErrorPanel(Map<TicketItem, Integer> missingQty, Runnable runnable) {
-
 
539
        this.caisseFrame.showStockErrorPanel(missingQty, runnable);
-
 
540
 
-
 
541
    }
-
 
542
 
569
}
543
}