com.endro.belajar.controller.MainController.java Source code

Java tutorial

Introduction

Here is the source code for com.endro.belajar.controller.MainController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.endro.belajar.controller;

import com.endro.belajar.config.Database;
import com.endro.belajar.dao.InvoiceDao;
import com.endro.belajar.entity.InvoiceOrder;
import com.endro.belajar.gui.DialogExportPenjualan;
import com.endro.belajar.gui.DialogReportPenjualan;
import com.endro.belajar.gui.MainFrame;
import com.endro.belajar.gui.MasterPelanggan;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JasperViewer;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 *
 * @author it6
 */
public class MainController {

    private MainFrame mainFrame;
    private DialogExportPenjualan exportPenjualan;
    private DialogReportPenjualan reportPenjualan;

    private InvoiceDao invoiceDao;

    public MainController() throws SQLException {
        invoiceDao = new InvoiceDao();
    }

    private void initReportPenjualan() {
        if (reportPenjualan == null) {
            reportPenjualan = new DialogReportPenjualan(mainFrame, true);
            reportPenjualan.setLocationRelativeTo(null);
        }
    }

    private void showReportPenjualan() {
        initReportPenjualan();

        fiilComboBox();
        clickedShowReport();
        clickedClose();
    }

    private void initMainFrame() {
        if (mainFrame == null) {
            mainFrame = new MainFrame();
        }

        clickedSalesOrder();
        clickedPenjualan();
        clickedReportPenjualan();
        clickedExportPenjualan();
    }

    private void initExportPenjualan() {
        if (exportPenjualan == null) {
            exportPenjualan = new DialogExportPenjualan();
            exportPenjualan.setLocationRelativeTo(null);
        }
        clickedExport();
    }

    public void showMainFrame() {
        initMainFrame();
        mainFrame.setVisible(true);
    }

    private void clickedExportPenjualan() {
        mainFrame.getButtonExportPenjualan().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                initExportPenjualan();
                exportPenjualan.setVisible(true);
            }
        });
    }

    private void clickedSalesOrder() {
        mainFrame.getButtonSalesOrder().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new SalesOrderController().showFrameSalesOrder();
                } catch (SQLException ex) {
                    Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    private void clickedReportPenjualan() {
        mainFrame.getButtonReportPenjualan().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showReportPenjualan();
                reportPenjualan.setVisible(true);
            }
        });
    }

    private void clickedPenjualan() {
        mainFrame.getButtonPenjualan().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new InvoiceProdukController().showFramePenjualan();
                } catch (SQLException ex) {
                    Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    private void clickedExport() {
        exportPenjualan.getButtonExport().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    LocalDate tanggalAwal = exportPenjualan.getTanggalAwalChooser().getDate().toInstant()
                            .atZone(ZoneId.systemDefault()).toLocalDate();
                    LocalDate tanggalAkhir = exportPenjualan.getTanggalAkhirChooser().getDate().toInstant()
                            .atZone(ZoneId.systemDefault()).toLocalDate();

                    List<InvoiceOrder> daftar = invoiceDao.findAllByTanggal(tanggalAwal, tanggalAkhir);
                    processConvertExcel(daftar);
                } catch (SQLException ex) {
                    Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
                } catch (NullPointerException ex) {
                    JOptionPane.showMessageDialog(exportPenjualan, "Form tanggal diisi dengan lengkap!");
                } catch (IOException ex) {
                    Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    exportPenjualan.dispose();
                    exportPenjualan = null;
                }
            }

            private void processConvertExcel(List<InvoiceOrder> daftarInvoice)
                    throws FileNotFoundException, IOException {
                Integer returnVal = exportPenjualan.getChooserSaveFile().showOpenDialog(exportPenjualan);

                if (returnVal == exportPenjualan.getChooserSaveFile().APPROVE_OPTION) {
                    XSSFWorkbook workbook = new XSSFWorkbook();
                    XSSFSheet sheet = workbook.createSheet("Just Example");

                    List<InvoiceOrder> list = daftarInvoice;

                    Integer rowTable = 0;
                    Integer cellTable = 0;
                    CellStyle cellStyleTanggal = workbook.createCellStyle();
                    CellStyle cellStyleHeader = workbook.createCellStyle();
                    CellStyle cellStyleDouble = workbook.createCellStyle();

                    CreationHelper createHelper = workbook.getCreationHelper();
                    XSSFFont font = workbook.createFont();

                    cellStyleTanggal.setDataFormat(createHelper.createDataFormat().getFormat("dd/mm/yyyy"));
                    cellStyleDouble.setDataFormat(
                            createHelper.createDataFormat().getFormat("[$Rp-421]#,##0.0000;-[$Rp-421]#,##0.0000"));
                    font.setBold(true);
                    cellStyleHeader.setFont(font);
                    cellStyleHeader.setWrapText(true);
                    //cellStyleHeader.setFillBackgroundColor(IndexedColors.YELLOW.getIndex());
                    cellStyleHeader.setFillPattern(FillPatternType.DIAMONDS);
                    for (InvoiceOrder order : list) {
                        Row row = sheet.createRow(rowTable);

                        if (rowTable == 0) {
                            sheet.setColumnWidth(0, 2000);
                            Cell cellHeader = row.createCell(0);
                            cellHeader.setCellValue("ID");
                            cellHeader.setCellStyle(cellStyleHeader);

                            sheet.setColumnWidth(1, 5000);
                            cellHeader = row.createCell(1);
                            cellHeader.setCellValue("Nomor Transaksi");
                            cellHeader.setCellStyle(cellStyleHeader);

                            sheet.setColumnWidth(2, 4000);
                            cellHeader = row.createCell(2);
                            cellHeader.setCellValue("Tanggal");
                            cellHeader.setCellStyle(cellStyleHeader);

                            sheet.setColumnWidth(3, 6000 * 3);
                            cellHeader = row.createCell(3);
                            cellHeader.setCellValue("Informasi Posting");
                            cellHeader.setCellStyle(cellStyleHeader);

                            sheet.setColumnWidth(4, 4850);
                            cellHeader = row.createCell(4);
                            cellHeader.setCellValue("Total Sebelum Diskon");
                            cellHeader.setCellStyle(cellStyleHeader);

                            sheet.setColumnWidth(5, 5000);
                            cellHeader = row.createCell(5);
                            cellHeader.setCellValue("Diskon");
                            cellHeader.setCellStyle(cellStyleHeader);

                            sheet.setColumnWidth(6, 4500);
                            cellHeader = row.createCell(6);
                            cellHeader.setCellValue("Total Setelah Diskon");
                            cellHeader.setCellStyle(cellStyleHeader);

                            sheet.setColumnWidth(7, 5000 * 2);
                            cellHeader = row.createCell(7);
                            cellHeader.setCellValue("Alamat Pengiriman");
                            cellHeader.setCellStyle(cellStyleHeader);
                        } else {
                            row.createCell(0).setCellValue((Integer) order.getPk());
                            row.createCell(1).setCellValue((String) order.getNomortransaksi());

                            Cell cellTanggal = row.createCell(2);
                            cellTanggal.setCellValue((Date) order.getTanggal());
                            cellTanggal.setCellStyle(cellStyleTanggal);

                            row.createCell(3).setCellValue((String) order.getInformasiposting());

                            Cell cellDouble = row.createCell(4);
                            cellDouble.setCellValue(order.getTotalbelumdiskon());
                            cellDouble.setCellStyle(cellStyleDouble);

                            cellDouble = row.createCell(5);
                            cellDouble.setCellValue(order.getDiskonfaktur());
                            cellDouble.setCellStyle(cellStyleDouble);

                            cellDouble = row.createCell(6);
                            cellDouble.setCellValue(order.getTotalsetelahdiskon());
                            cellDouble.setCellStyle(cellStyleDouble);

                            row.createCell(7).setCellValue((String) order.getAlamatpengiriman() == null ? "Null"
                                    : order.getAlamatpengiriman());
                        }
                        rowTable++;
                    }

                    File file = exportPenjualan.getChooserSaveFile().getSelectedFile();

                    FileOutputStream outputStream = new FileOutputStream(file + File.separator + "Penjualan.xlsx");
                    workbook.write(outputStream);

                    int pesan = JOptionPane.showConfirmDialog(exportPenjualan,
                            "Telah tersimpan di " + file + File.separator
                                    + "Penjualan.xlsx \n Apakah anda ingin membuka file tersebut?",
                            "Notification", JOptionPane.OK_CANCEL_OPTION);
                    if (pesan == JOptionPane.YES_OPTION) {
                        if ("Linux".equals(System.getProperty("os.name"))) {
                            String runPenjualan = "xdg-open " + file + File.separator + "Penjualan.xlsx";
                            Runtime.getRuntime().exec(runPenjualan);
                        } else if ("Windows".equals(System.getProperty("os.name"))) {
                            String runPenjualan = "excel.exe /r" + file + File.separator + "Penjualan.xlsx";
                            Runtime.getRuntime().exec(runPenjualan);
                        }
                    }
                } else {
                    exportPenjualan.getChooserSaveFile().cancelSelection();
                }
            }
        });
    }

    private void clickedClose() {
        reportPenjualan.getButtonClose().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                reportPenjualan.dispose();
                reportPenjualan.setVisible(false);
                reportPenjualan = null;

            }
        });
    }

    private void clickedShowReport() {
        reportPenjualan.getButtonShowReport().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String noTransaksi = (String) reportPenjualan.getComboNoInvoice().getSelectedItem();
                    Map<String, Object> map = new HashMap<>();
                    map.put("noinvoice", noTransaksi);

                    Connection koneksi = Database.getKonekPenjualan();
                    JasperPrint jasPrint = JasperFillManager.fillReport(
                            getClass().getResourceAsStream("/com/endro/belajar/report/NotaTransaksi.jasper"), map,
                            koneksi);
                    JasperViewer.viewReport(jasPrint, false);
                } catch (JRException ex) {
                    Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
                } catch (Exception ex) {
                    Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    reportPenjualan.dispose();
                    reportPenjualan.setVisible(false);
                    reportPenjualan = null;
                }
            }
        });
    }

    private void fiilComboBox() {
        try {
            List<InvoiceOrder> daftarInvoice = invoiceDao.findAll();

            for (InvoiceOrder order : daftarInvoice) {
                reportPenjualan.getComboNoInvoice().addItem(order.getNomortransaksi());
            }
        } catch (SQLException ex) {
            Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}