package gui;
import hibernateSwingApi.component.CComboBox;
import hibernateSwingApi.hibernate.HibernateManager;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;
import org.hibernate.Query;
import org.hibernate.Session;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.title.TextTitle;
import pojos.Kullanici;
import pojos.UrunSatis;
import service.PSAUtil;
import service.XYGrafikPaneliDonenSinif;
import com.toedter.calendar.JDateChooser;
public class PSAIstatistikEkrani extends JFrame {
private static final long serialVersionUID = 1L;
private static DefaultTableModel dtm;
private static PSAIstatistikEkrani instance;
private List<UrunSatis> enCokSatilanUrunlerListesi;
private UrunSatis us;
private List<UrunSatis> tabloyaKonulacakListe;
private List<UrunSatis> tabloyaAdetSiraliKonulanListe;
Calendar date;
private static JTable table;
private CComboBox<String> comboBox;
private JLabel jLabelSatilanGunlerOrtalama;
private static String[] haftaninGunleri = new String[] { "Pazar",
"Pazartesi", "Salı", "Çarşamba", "Perşembe", "Cuma", "Cumartesi" };
private List<UrunSatis> enUzunSuredirSatilmayanlarListesi;
private JDateChooser jDateChooserBaslangic;
private JDateChooser jDateChooserBitis;
private JLabel jLabelTarihlerArasi;
private SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
private JPanel panel_2;
private XYGrafikPaneliDonenSinif grafikCiz = new XYGrafikPaneliDonenSinif();
private ChartPanel cp;
private String[] kategoriAdis;
private Double[] tutar;
private CComboBox<String> cComboBoxGrafikTuru;
private JSpinner spinner;
private JSpinner spinnerAltTaban;
private JSpinner spinnerUstTaban;
private JPanel jPanelTarih;
private JLabel jLabelIle;
private JLabel jLabelTarih;
private JPanel panel;
private JScrollPane scrollPane_1;
private JPanel panel_3;
private JLabel jLabelGrafikOge;
private JLabel lblNewLabel_1;
private JLabel jLabelGrafikteGösterilecekSıra;
private JLabel lblNewLabel_2;
private JLabel lblNewLabel_3;
private JPanel panel_4;
private JLabel lblNewLabel;
private JPanel jPanelstatistik;
private JTabbedPane tabbedPane;
private JPanel analiz;
private JPanel panel_6;
private static CComboBox<String> cComboboxSube;
private static final String ALL_COMBOBOX_VALUE = "TÜMÜ";
private static final String COMBOBOX_VALUE = "Şube seçiniz";
public static PSAIstatistikEkrani getInstance(int state) {
if (state == 0)
instance = new PSAIstatistikEkrani();
return instance;
}
public static CComboBox<String> getSube() {
return cComboboxSube;
}
public PSAIstatistikEkrani() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
private void initialize() {
setBounds(0, 0, 900, 700);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setTitle(("PSA MERKEZ İSTATİSTİK EKRANI"));
setIconImage(PSAUtil.getImageIconViaRelativePath("picture/psaIcon.png")
.getImage());
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[] { 0, 861, 0 };
gridBagLayout.rowHeights = new int[] { 5, 44, 606, 0 };
gridBagLayout.columnWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 1.0,
Double.MIN_VALUE };
getContentPane().setLayout(gridBagLayout);
panel_6 = new JPanel();
GridBagConstraints gbc_panel_6 = new GridBagConstraints();
gbc_panel_6.insets = new Insets(0, 0, 5, 0);
gbc_panel_6.fill = GridBagConstraints.BOTH;
gbc_panel_6.gridx = 1;
gbc_panel_6.gridy = 1;
getContentPane().add(panel_6, gbc_panel_6);
GridBagLayout gbl_panel_6 = new GridBagLayout();
gbl_panel_6.columnWidths = new int[] { 277, 547, 0 };
gbl_panel_6.rowHeights = new int[] { 36, 0 };
gbl_panel_6.columnWeights = new double[] { 1.0, 1.0, Double.MIN_VALUE };
gbl_panel_6.rowWeights = new double[] { 0.0, Double.MIN_VALUE };
panel_6.setLayout(gbl_panel_6);
cComboboxSube = new CComboBox(String.class);
GridBagConstraints gbc_cComboboxSube = new GridBagConstraints();
gbc_cComboboxSube.fill = GridBagConstraints.BOTH;
gbc_cComboboxSube.insets = new Insets(0, 0, 0, 5);
gbc_cComboboxSube.gridx = 0;
gbc_cComboboxSube.gridy = 0;
panel_6.add(cComboboxSube, gbc_cComboboxSube);
jPanelTarih = new JPanel();
GridBagConstraints gbc_jPanelTarih = new GridBagConstraints();
gbc_jPanelTarih.fill = GridBagConstraints.BOTH;
gbc_jPanelTarih.gridx = 1;
gbc_jPanelTarih.gridy = 0;
panel_6.add(jPanelTarih, gbc_jPanelTarih);
jPanelTarih.setBorder(new LineBorder(new Color(0, 0, 0), 2));
jPanelTarih.setBackground(UIManager.getColor("Button.shadow"));
GridBagLayout gbl_jPanelTarih = new GridBagLayout();
gbl_jPanelTarih.columnWidths = new int[] { 151, 110, 24, 110, 0 };
gbl_jPanelTarih.rowHeights = new int[] { 20, 0 };
gbl_jPanelTarih.columnWeights = new double[] { 1.0, 1.0, 0.0, 1.0,
Double.MIN_VALUE };
gbl_jPanelTarih.rowWeights = new double[] { 1.0, Double.MIN_VALUE };
jPanelTarih.setLayout(gbl_jPanelTarih);
jLabelTarih = new JLabel();
jLabelTarih.setText("Tarih aralığı seçiniz :");
jLabelTarih.setFont(new Font("Tahoma", Font.BOLD, 14));
GridBagConstraints gbc_jLabelTarih = new GridBagConstraints();
gbc_jLabelTarih.anchor = GridBagConstraints.EAST;
gbc_jLabelTarih.insets = new Insets(0, 0, 0, 5);
gbc_jLabelTarih.gridx = 0;
gbc_jLabelTarih.gridy = 0;
jPanelTarih.add(jLabelTarih, gbc_jLabelTarih);
jDateChooserBaslangic = new JDateChooser();
jDateChooserBaslangic.setDate(new Date());
GridBagConstraints gbc_jDateChooserBaslangic = new GridBagConstraints();
gbc_jDateChooserBaslangic.fill = GridBagConstraints.HORIZONTAL;
gbc_jDateChooserBaslangic.insets = new Insets(0, 0, 0, 5);
gbc_jDateChooserBaslangic.gridx = 1;
gbc_jDateChooserBaslangic.gridy = 0;
jPanelTarih.add(jDateChooserBaslangic, gbc_jDateChooserBaslangic);
jLabelIle = new JLabel();
jLabelIle.setText("ile");
jLabelIle.setFont(new Font("Tahoma", Font.BOLD, 15));
GridBagConstraints gbc_jLabelIle = new GridBagConstraints();
gbc_jLabelIle.fill = GridBagConstraints.HORIZONTAL;
gbc_jLabelIle.insets = new Insets(0, 0, 0, 5);
gbc_jLabelIle.gridx = 2;
gbc_jLabelIle.gridy = 0;
jPanelTarih.add(jLabelIle, gbc_jLabelIle);
jDateChooserBitis = new JDateChooser();
jDateChooserBitis.setDate(new Date());
GridBagConstraints gbc_jDateChooserBitis = new GridBagConstraints();
gbc_jDateChooserBitis.fill = GridBagConstraints.HORIZONTAL;
gbc_jDateChooserBitis.gridx = 3;
gbc_jDateChooserBitis.gridy = 0;
jPanelTarih.add(jDateChooserBitis, gbc_jDateChooserBitis);
analiz = PSAAnalizPanel.getInstance(jDateChooserBaslangic,
jDateChooserBitis).getAnalizPanel();
tabbedPane = new JTabbedPane(SwingConstants.TOP);
GridBagConstraints gbc_tabbedPane = new GridBagConstraints();
gbc_tabbedPane.fill = GridBagConstraints.BOTH;
gbc_tabbedPane.gridx = 1;
gbc_tabbedPane.gridy = 2;
getContentPane().add(tabbedPane, gbc_tabbedPane);
jPanelstatistik = new JPanel();
tabbedPane.addTab("İSTATİSTİK", null, jPanelstatistik, null);
GridBagLayout gbl_jPanelstatistik = new GridBagLayout();
gbl_jPanelstatistik.columnWidths = new int[] { 418, 432, 0 };
gbl_jPanelstatistik.rowHeights = new int[] { 30, 257, 37, 235, 0 };
gbl_jPanelstatistik.columnWeights = new double[] { 1.0, 1.0,
Double.MIN_VALUE };
gbl_jPanelstatistik.rowWeights = new double[] { 0.0, 1.0, 0.0, 1.0,
Double.MIN_VALUE };
jPanelstatistik.setLayout(gbl_jPanelstatistik);
comboBox = new CComboBox<>(String.class);
GridBagConstraints gbc_comboBox = new GridBagConstraints();
gbc_comboBox.fill = GridBagConstraints.BOTH;
gbc_comboBox.insets = new Insets(0, 0, 5, 0);
gbc_comboBox.gridx = 0;
gbc_comboBox.gridy = 0;
jPanelstatistik.add(comboBox, gbc_comboBox);
comboBox.setToolTipText("");
comboBox.insertItemAt(
"İncelemek İstediğiniz İstatistik Türünü Seçiniz", 0);
comboBox.setSelectedIndex(0);
panel = new JPanel();
GridBagConstraints gbc_panel = new GridBagConstraints();
gbc_panel.fill = GridBagConstraints.BOTH;
gbc_panel.insets = new Insets(0, 0, 5, 0);
gbc_panel.gridwidth = 2;
gbc_panel.gridx = 0;
gbc_panel.gridy = 1;
jPanelstatistik.add(panel, gbc_panel);
panel.setBorder(new LineBorder(new Color(0, 0, 0), 2));
panel.setBackground(UIManager.getColor("Button.shadow"));
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[] { 874, 0 };
gbl_panel.rowHeights = new int[] { 21, 201, 21, 0 };
gbl_panel.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
gbl_panel.rowWeights = new double[] { 0.0, 1.0, 0.0, Double.MIN_VALUE };
panel.setLayout(gbl_panel);
jLabelSatilanGunlerOrtalama = new JLabel("*Küsuratlar yuvarlanmıştır");
jLabelSatilanGunlerOrtalama
.setHorizontalAlignment(SwingConstants.CENTER);
jLabelSatilanGunlerOrtalama.setFont(new Font("Tahoma", Font.PLAIN, 11));
jLabelSatilanGunlerOrtalama.setVisible(false);
GridBagConstraints gbc_jLabelSatilanGunlerOrtalama = new GridBagConstraints();
gbc_jLabelSatilanGunlerOrtalama.fill = GridBagConstraints.BOTH;
gbc_jLabelSatilanGunlerOrtalama.insets = new Insets(0, 0, 5, 0);
gbc_jLabelSatilanGunlerOrtalama.gridx = 0;
gbc_jLabelSatilanGunlerOrtalama.gridy = 0;
panel.add(jLabelSatilanGunlerOrtalama, gbc_jLabelSatilanGunlerOrtalama);
scrollPane_1 = new JScrollPane();
GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints();
gbc_scrollPane_1.fill = GridBagConstraints.BOTH;
gbc_scrollPane_1.insets = new Insets(0, 0, 5, 0);
gbc_scrollPane_1.gridx = 0;
gbc_scrollPane_1.gridy = 1;
panel.add(scrollPane_1, gbc_scrollPane_1);
table = new JTable();
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
table.setEnabled(false);
table.setEnabled(false);
scrollPane_1.setViewportView(table);
jLabelTarihlerArasi = new JLabel("");
jLabelTarihlerArasi.setForeground(Color.BLACK);
GridBagConstraints gbc_jLabelTarihlerArasi = new GridBagConstraints();
gbc_jLabelTarihlerArasi.fill = GridBagConstraints.BOTH;
gbc_jLabelTarihlerArasi.gridx = 0;
gbc_jLabelTarihlerArasi.gridy = 2;
panel.add(jLabelTarihlerArasi, gbc_jLabelTarihlerArasi);
panel_4 = new JPanel();
GridBagConstraints gbc_panel_4 = new GridBagConstraints();
gbc_panel_4.fill = GridBagConstraints.HORIZONTAL;
gbc_panel_4.insets = new Insets(0, 0, 5, 0);
gbc_panel_4.gridwidth = 2;
gbc_panel_4.gridx = 0;
gbc_panel_4.gridy = 2;
jPanelstatistik.add(panel_4, gbc_panel_4);
GridBagLayout gbl_panel_4 = new GridBagLayout();
gbl_panel_4.columnWidths = new int[] { 125, -66, 110, 0 };
gbl_panel_4.rowHeights = new int[] { 31, 0 };
gbl_panel_4.columnWeights = new double[] { 1.0, 0.0, 1.0,
Double.MIN_VALUE };
gbl_panel_4.rowWeights = new double[] { 0.0, Double.MIN_VALUE };
panel_4.setLayout(gbl_panel_4);
cComboBoxGrafikTuru = new CComboBox(String.class);
cComboBoxGrafikTuru.insertItemAt("Grafik Türü Seçiniz", 0);
cComboBoxGrafikTuru.setSelectedIndex(0);
GridBagConstraints gbc_cComboBoxGrafikTuru = new GridBagConstraints();
gbc_cComboBoxGrafikTuru.fill = GridBagConstraints.BOTH;
gbc_cComboBoxGrafikTuru.insets = new Insets(0, 0, 0, 5);
gbc_cComboBoxGrafikTuru.gridx = 0;
gbc_cComboBoxGrafikTuru.gridy = 0;
panel_4.add(cComboBoxGrafikTuru, gbc_cComboBoxGrafikTuru);
lblNewLabel = new JLabel("DETAYLAR");
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.fill = GridBagConstraints.VERTICAL;
gbc_lblNewLabel.anchor = GridBagConstraints.WEST;
gbc_lblNewLabel.gridx = 2;
gbc_lblNewLabel.gridy = 0;
panel_4.add(lblNewLabel, gbc_lblNewLabel);
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 16));
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
panel_2 = new JPanel();
GridBagConstraints gbc_panel_2 = new GridBagConstraints();
gbc_panel_2.fill = GridBagConstraints.BOTH;
gbc_panel_2.insets = new Insets(0, 0, 0, 5);
gbc_panel_2.gridx = 0;
gbc_panel_2.gridy = 3;
jPanelstatistik.add(panel_2, gbc_panel_2);
panel_2.setBorder(new LineBorder(new Color(0, 0, 0), 2));
panel_2.setBackground(UIManager.getColor("Button.shadow"));
panel_2.setLayout(new BorderLayout(0, 0));
panel_3 = new JPanel();
GridBagConstraints gbc_panel_3 = new GridBagConstraints();
gbc_panel_3.fill = GridBagConstraints.BOTH;
gbc_panel_3.gridx = 1;
gbc_panel_3.gridy = 3;
jPanelstatistik.add(panel_3, gbc_panel_3);
panel_3.setBorder(new LineBorder(new Color(0, 0, 0), 2));
panel_3.setBackground(UIManager.getColor("Button.shadow"));
panel_3.setLayout(null);
jLabelGrafikOge = new JLabel("Grafikte Gösterilecek Öğe Sayısı:");
jLabelGrafikOge.setBounds(10, 11, 201, 21);
panel_3.add(jLabelGrafikOge);
spinner = new JSpinner();
spinner.setModel(new SpinnerNumberModel(10, 1, 25, 1));
spinner.setValue(10);
spinner.setBounds(221, 11, 43, 21);
panel_3.add(spinner);
spinnerAltTaban = new JSpinner();
spinnerAltTaban.setModel(new SpinnerNumberModel(new Integer(0),
new Integer(0), null, new Integer(1)));
spinnerAltTaban.setBounds(221, 43, 43, 21);
panel_3.add(spinnerAltTaban);
spinnerUstTaban = new JSpinner();
spinnerUstTaban.setModel(new SpinnerNumberModel(new Integer(10),
new Integer(1), null, new Integer(1)));
spinnerUstTaban.setBounds(288, 43, 43, 21);
panel_3.add(spinnerUstTaban);
lblNewLabel_1 = new JLabel("(Maksimum)");
lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 9));
lblNewLabel_1.setBounds(264, 11, 75, 21);
panel_3.add(lblNewLabel_1);
jLabelGrafikteGösterilecekSıra = new JLabel(
"Grafikte Gösterilecek Aralık:");
jLabelGrafikteGösterilecekSıra.setBounds(10, 43, 199, 21);
panel_3.add(jLabelGrafikteGösterilecekSıra);
lblNewLabel_2 = new JLabel("ile");
lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 9));
lblNewLabel_2.setBounds(274, 43, 20, 21);
panel_3.add(lblNewLabel_2);
lblNewLabel_3 = new JLabel("arasında");
lblNewLabel_3.setFont(new Font("Tahoma", Font.PLAIN, 9));
lblNewLabel_3.setBounds(337, 43, 46, 21);
panel_3.add(lblNewLabel_3);
tabbedPane.addTab("ANALİZ", null, analiz, null);
initListener();
comboBoxDoldur();
fillComboBox();
}
private static void fillComboBox() {
List<?> subeListesi = HibernateManager.getInstance()
.distinctSelectField(Kullanici.class, "systemName");
cComboboxSube.setInitialObject(COMBOBOX_VALUE);
cComboboxSube.setObjects(subeListesi);
cComboboxSube.insertItemAt(ALL_COMBOBOX_VALUE, 1);
}
@SuppressWarnings("unchecked")
private void firmalaraGoreSatisPerformansi() {
List<UrunSatis> firmalaraGoreSatisList = (List<UrunSatis>) tarihAraliginaGoreListeCek(
jDateChooserBaslangic.getDate(), jDateChooserBitis.getDate());
Object[] o = new Object[] { "Firmanın Adı", "Satılan Ürün Adedi",
"Ürünlerin Ortalama Fiyatı ", "Satılan ürünün Tutarı" };
tabloyuDizaynEt(o);
Map<String, Double> firmaUrunTutarlari = new HashMap<>();
Map<String, Integer> firmaUrunAdetleri = new HashMap<>();
for (UrunSatis us : firmalaraGoreSatisList) {
mapeFirmaAdetEkle(firmaUrunAdetleri, us);
mapeFirmaTutarEkle(firmaUrunTutarlari, us);
}
hashMapiTabloyaDoldur(firmaUrunTutarlari, firmaUrunAdetleri);
chartDoldur(0, 3, "Firmalara Göre Satış Grafiği", "Tutar");
}
private void mapeFirmaTutarEkle(Map<String, Double> map, UrunSatis us) {
if (!map.containsKey(us.getFirmaAd())) {
if (!us.getFirmaAd().equals("-")) {
map.put(us.getFirmaAd(), us.getTutar());
}
} else {
double x = map.get(us.getFirmaAd());
x += us.getTutar();
map.put(us.getFirmaAd(), x);
}
}
private void mapeFirmaAdetEkle(Map<String, Integer> map, UrunSatis us) {
if (!map.containsKey(us.getFirmaAd())) {
if (!us.getFirmaAd().equals("-")) {
map.put(us.getFirmaAd(), us.getAdet());
}
return;
} else {
int x = map.get(us.getFirmaAd());
x += us.getAdet();
map.put(us.getFirmaAd(), x);
}
}
@SuppressWarnings("unchecked")
private void gunlereGoreSatisDagilimi() {
List<UrunSatis> list = (List<UrunSatis>) tarihAraliginaGoreListeCek(
jDateChooserBaslangic.getDate(), jDateChooserBitis.getDate());
Object[] o = new Object[] { "Satıldığı gün", "Satılan Ürün Adedi",
"Ürünlerin Ortalama Fiyatı", "Satılan Ürün Tutarı" };
tabloyuDizaynEt(o);
Map<String, Double> urunTutarlari = new HashMap<>();
Map<String, Integer> urunAdetleri = new HashMap<>();
for (UrunSatis urunSatis : list) {
addAdetToMap(urunAdetleri, urunSatis);
addTutarToMap(urunTutarlari, urunSatis);
}
hashMapiTabloyaDoldur(urunTutarlari, urunAdetleri);
chartDoldur(0, 3, "Günlere Göre Satış Dağılımı", "Tutar");
}
private void chartDoldur(int columnString, int columnDeger, String title,
String yAxisAdi) {
int j = 0;
int x = 0;
spinnerEnabledOrNot(true);
int spinAlt = (int) spinnerAltTaban.getValue();
if ((table.getModel().getRowCount() - spinAlt) > (int) spinner
.getValue()) {
x = (int) spinner.getValue();
} else {
if (table.getModel().getRowCount() > 0) {
x = table.getModel().getRowCount();
}
}
kategoriAdis = new String[x];
tutar = new Double[x];
for (int i = spinAlt; i < x; i++) {
kategoriAdis[j] = String.valueOf(table.getModel().getValueAt(i,
columnString));
tutar[j] = Double.valueOf((String.valueOf(table.getModel()
.getValueAt(i, columnDeger))));
j++;
}
paneleGrafikEkle(tutar, "Satış Yoğunluğu", kategoriAdis, panel_2,
title, yAxisAdi);
}
private void paneleGrafikEkle(Double[] deger, String baremAdi,
String[] degerIsimleri, JPanel chartEklenecekPanel,
String chartTitle, String yAxisAdi) {
cp = grafikCiz.pastaDilimi(deger, baremAdi, degerIsimleri);
String grafikSecStringi = (String) cComboBoxGrafikTuru
.getSelectedItem();
switch (grafikSecStringi) {
case "Sütun Grafiği":
cp = grafikCiz.xyGrafigi(deger, baremAdi, degerIsimleri, yAxisAdi);
break;
case "Pasta Dilimi":
cp = grafikCiz.pastaDilimi(deger, baremAdi, degerIsimleri);
break;
}
TextTitle txtTitle = new TextTitle(chartTitle, new Font("Tahoma",
Font.BOLD, 18));
cp.getChart().setTitle(txtTitle);
chartEklenecekPanel.add(cp);
chartEklenecekPanel.updateUI();
cp.setVisible(true);
}
@SuppressWarnings("unchecked")
private void enUzunSuredirSatilmayanUrunler() {
enUzunSuredirSatilmayanlarListesi = (List<UrunSatis>) tarihAraliginaGoreListeCek(
jDateChooserBaslangic.getDate(), jDateChooserBitis.getDate());
Object[] o = new Object[] {"ÜRÜN BARKODU", "ÜRÜN KODU", "SON SATILDIĞI TARİH",
"ÜRÜNÜN GRUBU", "ÜRÜNÜN MODELİ", "ÜRÜNÜN BEDENİ" };
tabloyuDizaynEt(o);
tabloyuSatilmayanlaraGoreDoldur();
enUzunSuredirSatilmayanGrafik();
}
private void enUzunSuredirSatilmayanGrafik() {
List<UrunSatis> usList = verilenListeyiHashMapeAt(enUzunSuredirSatilmayanlarListesi);
int size = usList.size();
spinnerEnabledOrNot(false);
Double[] tarihArasiAdet = new Double[6];
String[] kategoriIsmi = new String[6];
double tarihAdeti = 0;
int index = 0;
int kacinciParcacik = 1;
int eklenecekTarih = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
Calendar cal1 = Calendar.getInstance();
if (size == 0) {
paneleGrafikEkle(tarihArasiAdet, "Satılmadığı Gün", kategoriIsmi,
panel_2, "Ürünlerin Son Satıldığı Tarihler",
"Gündür Satılmayan");
size = 1;
return;
}
int gunlerArasiFark = daysBetween(usList.get(size - 1).getTarih(),
cal.getTime());
double tarihParcasi = (gunlerArasiFark / 6.0);
try {
cal.setTime(sdf.parse(usList.get(size - 1).getTarih().toString()));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cal.add(Calendar.DATE, (int) tarihParcasi);
for (int i = (size - 1); i >= 0; i--) {
try {
cal1.setTime(sdf.parse(usList.get(i).getTarih().toString()));
} catch (ParseException e1) {
e1.printStackTrace();
}
if (cal1.compareTo(cal) <= 0) {
tarihAdeti++;
if (i == 0) {
tarihArasiAdet[index] = tarihAdeti;
kategoriIsmi[index] = enUzunSuredirSatilmayanKategoriIsmı(
gunlerArasiFark, tarihParcasi, kacinciParcacik);
}
} else {
tarihArasiAdet[index] = tarihAdeti;
kategoriIsmi[index] = enUzunSuredirSatilmayanKategoriIsmı(
gunlerArasiFark, tarihParcasi, kacinciParcacik);
index++;
tarihAdeti = 0;
i++;
while (cal1.after(cal)) {
kacinciParcacik++;
if (eklenecekTarih == 0) {
eklenecekTarih = (int) ((tarihParcasi * kacinciParcacik));
} else {
eklenecekTarih = (int) ((tarihParcasi * kacinciParcacik) - (int) (tarihParcasi * (kacinciParcacik - 1)));
}
cal.add(Calendar.DATE, eklenecekTarih);
}
}
}
paneleGrafikEkle(tarihArasiAdet, "Satılmadığı Gün", kategoriIsmi,
panel_2, "Ürünlerin Son Satıldığı Tarihler",
"Gündür Satılmayan");
}
private void spinnerEnabledOrNot(boolean b) {
if (b == true && !spinner.isEnabled()) {
spinner.setEnabled(b);
spinnerAltTaban.setEnabled(b);
spinnerUstTaban.setEnabled(b);
}
if (b == false && spinner.isEnabled()) {
spinner.setEnabled(b);
spinnerAltTaban.setEnabled(b);
spinnerUstTaban.setEnabled(b);
}
}
private static String enUzunSuredirSatilmayanKategoriIsmı(
int gunlerArasiFark, double tarihParcasi, int kacinciParcacik) {
String kategoriIsmi = String.valueOf(gunlerArasiFark
- (int) Math.round(((tarihParcasi * (kacinciParcacik)))))
+ "-"
+ String.valueOf(gunlerArasiFark
- (int) Math
.round(((tarihParcasi * (kacinciParcacik - 1)))))
+ " gündür";
return kategoriIsmi;
}
private static int daysBetween(Date d1, Date d2) {
int x = (int) Math.round((double) (d2.getTime() - d1.getTime())
/ (double) (1000 * 60 * 60 * 24));
return x;
// TODO Auto-generated method stub
}
@SuppressWarnings("unchecked")
private void enCokSatilanUrunler() {
enCokSatilanUrunlerListesi = (List<UrunSatis>) tarihAraliginaGoreListeCek(
jDateChooserBaslangic.getDate(), jDateChooserBitis.getDate());
tabloyaKonulacakListe = new ArrayList<UrunSatis>();
Object[] o = new Object[] { "BARKOD","KOD", "SATIŞ ADEDİ", "ALIŞ FİYATI",
"PEŞİN SATIŞ", "TAKSİTLİ SATIŞ FİYATI", "TUTAR" };
tabloyuDizaynEt(o);
us = null;
listeyiDuzenle(enCokSatilanUrunlerListesi);
adeteGoreListeyiSırala();
tabloyuDoldur(tabloyaAdetSiraliKonulanListe);
chartDoldur(0, 2, "En Çok Satılan Ürünler", "Satilan Adet");
}
private static void tabloyuDizaynEt(Object[] o) {
dtm = new DefaultTableModel();
dtm.setColumnIdentifiers(o);
table.setModel(dtm);
}
private void adeteGoreListeyiSırala() {
int index = Integer.MIN_VALUE;
int iterator = -1;
tabloyaAdetSiraliKonulanListe = new ArrayList<UrunSatis>();
while (!tabloyaKonulacakListe.isEmpty()) {
index = Integer.MIN_VALUE;
for (int i = 0; i < tabloyaKonulacakListe.size(); i++) {
if (tabloyaKonulacakListe.get(i).getAdet() > index) {
iterator = i;
index = tabloyaKonulacakListe.get(i).getAdet();
}
}
tabloyaAdetSiraliKonulanListe.add(tabloyaKonulacakListe
.get(iterator));
tabloyaKonulacakListe.remove(iterator);
}
}
private void hashMapiTabloyaDoldur(Map<String, Double> mapTutar,
Map<String, Integer> mapAdet) {
String gun = "";
int iterator = mapTutar.size();
Entry<String, Double> maxEntry = null;
for (int i = 0; i < iterator; i++) {
for (Entry<String, Double> stEntry : mapTutar.entrySet()) {
if ((maxEntry == null || stEntry.getValue() > maxEntry
.getValue())) {
maxEntry = stEntry;
gun = stEntry.getKey();
}
}
int x = (int) Math.round(mapTutar.get(gun) / mapAdet.get(gun));
dtm.addRow(new Object[] { gun, mapAdet.get(gun), x,
mapTutar.get(gun) });
mapTutar.remove(gun);
maxEntry = null;
}
jLabelSatilanGunlerOrtalama.setVisible(true);
}
private void comboBoxDoldur() {
LinkedList<String> comboBoxIndexLL = new LinkedList<>();
LinkedList<String> comboBoxGrafikIndexLL = new LinkedList<>();
comboBoxIndexLL.add("En çok satılan ürünler");
comboBoxIndexLL.add("En çok satış yapılan günler");
comboBoxIndexLL.add("Uzun süredir satılmayan ürünler");
comboBoxIndexLL.add("Firmalara Göre Satış Performansı");
comboBox.setObjects(comboBoxIndexLL);
comboBoxGrafikIndexLL.add("Sütun Grafiği");
comboBoxGrafikIndexLL.add("Pasta Dilimi");
cComboBoxGrafikTuru.setObjects(comboBoxGrafikIndexLL);
}
private void eventKontrolEt(ChangeEvent e) {
Object source = e.getSource();
int x = (int) spinnerAltTaban.getValue();
int y = (int) spinner.getValue();
int z = (int) spinnerUstTaban.getValue();
if (source == spinner) {
spinnerUstTaban.setValue(x + y);
}
if (source == spinnerAltTaban) {
spinnerUstTaban.setValue(x + y);
spinnerAltTaban.setValue(x);
}
if (source == spinnerUstTaban) {
if (x == 0 && y < 25) {
spinner.setValue(z);
}
if (x == 0 && y == 25) {
if (z > y) {
spinnerAltTaban.setValue(z - y);
} else {
spinner.setValue(z);
}
}
if (x > 0 && y < 25) {
spinnerAltTaban.setValue(z - y);
}
if (x > 0 && y == 25) {
spinnerAltTaban.setValue(z - y);
}
}
comboBoxDegisti();
}
private String getDayOfTheWeek(UrunSatis us) {
Date tarih = us.getTarih();
Calendar instance = Calendar.getInstance();
instance.setTime(tarih);
int i = instance.get(Calendar.DAY_OF_WEEK);
return haftaninGunleri[i - 1];
}
private void addTutarToMap(Map<String, Double> map, UrunSatis us) {
String dayOfTheWeek = getDayOfTheWeek(us);
if (!map.containsKey(dayOfTheWeek)) {
map.put(dayOfTheWeek, us.getTutar());
return;
}
Double double1 = map.get(dayOfTheWeek);
double1 += us.getTutar();
map.put(dayOfTheWeek, double1);
}
private void addAdetToMap(Map<String, Integer> map, UrunSatis us) {
String dayOfTheWeek = getDayOfTheWeek(us);
if (!map.containsKey(dayOfTheWeek)) {
map.put(dayOfTheWeek, us.getAdet());
return;
}
Integer integer = map.get(dayOfTheWeek);
integer += us.getAdet();
map.put(dayOfTheWeek, integer);
}
private List<UrunSatis> verilenListeyiHashMapeAt(List<UrunSatis> us) {
List<UrunSatis> tariheGoreTabloyaAtilacakList = new ArrayList<UrunSatis>();
HashMap<Long, UrunSatis> satilmayanUrunlerHashMapi = new HashMap<Long, UrunSatis>();
for (int i = (us.size() - 1); i >= 0; i--) {
if (!satilmayanUrunlerHashMapi.containsKey(us.get(i).getBarkod())) {
satilmayanUrunlerHashMapi.put(us.get(i).getBarkod(), us.get(i));
tariheGoreTabloyaAtilacakList.add(us.get(i));
}
}
return tariheGoreTabloyaAtilacakList;
}
// verilen listede ki urunlerin belirtilen özelliklerini tabloya doldurur
private static void tabloyuDoldur(List<UrunSatis> liste) {
for (int i = 0; i < liste.size(); i++) {
dtm.addRow(new Object[] { liste.get(i).getBarkod(),liste.get(i).getKod(),
liste.get(i).getAdet(), liste.get(i).getAlisFiyati(),
liste.get(i).getPesinSatisFiyati(),
liste.get(i).getTaksitliSatisFiyati(),
liste.get(i).getTutar() });
}
}
private void tabloyuSatilmayanlaraGoreDoldur() {
List<UrunSatis> usList = verilenListeyiHashMapeAt(enUzunSuredirSatilmayanlarListesi);
int size = usList.size();
for (int i = (size - 1); i >= 0; i--) {
dtm.addRow(new Object[] { usList.get(i).getBarkod(),usList.get(i).getKod(),
usList.get(i).getTarih(), usList.get(i).getGrup(),
usList.get(i).getModel(), usList.get(i).getMarka() });
}
}
// verilen listede ki aynı barkodlu ürünleri bir araya toplar
private void listeyiDuzenle(List<UrunSatis> liste) {
while (!liste.isEmpty()) {
us = liste.get(0);
for (int i = 1; i < liste.size(); i++) {
if (us.getBarkod() == liste.get(i).getBarkod()) {
us.setAdet(us.getAdet() + liste.get(i).getAdet());
liste.remove(i);
i--;
}
}
tabloyaKonulacakListe.add(us);
liste.remove(0);
}
}
private void dateDegisti() {
if (comboBox.getItemCount() == 0) {
comboBoxDoldur();
return;
}
comboBoxDegisti();
}
private void comboBoxDegisti() {
jLabelSatilanGunlerOrtalama.setVisible(false);
if (cp != null) {
cp.setVisible(false);
}
// panel_2.removeAll();
String istatistikStringi = (String) comboBox.getSelectedItem();
if (istatistikStringi == null) {
return;
}
switch (istatistikStringi) {
case "En çok satılan ürünler":
enCokSatilanUrunler();
break;
case "En çok satış yapılan günler":
gunlereGoreSatisDagilimi();
break;
case "Uzun süredir satılmayan ürünler":
enUzunSuredirSatilmayanUrunler();
break;
case "Firmalara Göre Satış Performansı":
firmalaraGoreSatisPerformansi();
break;
}
jLabelTarihlerArasi
.setText((sdf.format(jDateChooserBaslangic.getDate()) + " ile " + sdf
.format(jDateChooserBitis.getDate()))
+ " tarihleri arasında "
+ table.getRowCount()
+ " sonuc gösteriliyor");
}
private static List<?> tarihAraliginaGoreListeCek(Date from, Date to) {
Calendar cal = Calendar.getInstance();
cal.setTime(from);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
from = cal.getTime();
cal.setTime(to);
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
cal.set(Calendar.MILLISECOND, 99);
to = cal.getTime();
Session openSession = HibernateManager.getInstance().openSession();
openSession.beginTransaction();
StringBuilder sBuilder = new StringBuilder();
sBuilder.append("from UrunSatis where ");
sBuilder.append("tarih >= ").append(":from");
sBuilder.append(" and tarih <= ").append(":to");
String hql = sBuilder.toString();
Query createQuery = openSession.createQuery(hql);
createQuery.setParameter("from", from);
createQuery.setParameter("to", to);
List<?> list = createQuery.list();
openSession.close();
return list;
}
private void initListener() {
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
anaEkranSetEnable();
}
});
jDateChooserBitis
.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if ("date".equals(evt.getPropertyName())) {
dateDegisti();
}
}
});
comboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent arg0) {
if (arg0.getStateChange() == ItemEvent.SELECTED) {
comboBoxDegisti();
}
}
});
jDateChooserBaslangic
.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if ("date".equals(evt.getPropertyName())) {
dateDegisti();
}
}
});
cComboBoxGrafikTuru.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent arg0) {
comboBoxDegisti();
}
});
spinner.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
eventKontrolEt(e);
}
});
spinnerAltTaban.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
eventKontrolEt(e);
}
});
spinnerUstTaban.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
eventKontrolEt(e);
}
});
}
protected void anaEkranSetEnable() {
PSAAnaEkran.getInstance(null).setEnabled(true);
instance.dispose();
}
}