package gui;
import hibernateSwingApi.component.TablePanel;
import hibernateSwingApi.hibernate.HibernateManager;
import hibernateSwingApi.utils.Utils;
import java.awt.Color;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import org.hibernate.Query;
import org.hibernate.Session;
import pojos.BireyselMusteri;
import pojos.KurumsalMusteri;
import pojos.Musteri;
import pojos.YapilanSatis;
import service.PSAUtil;
import com.toedter.calendar.JDateChooser;
import com.toedter.calendar.JTextFieldDateEditor;
public class PSAMusteriHareketEkrani extends JFrame {
private static final long serialVersionUID = 1L;
private static PSAMusteriHareketEkrani instance;
private JPanel contentPane;
private JPanel jPanelFiltre;
private JDateChooser jDateChooserIlkTarih;
private JDateChooser jDateChooserSonTarih;
private JLabel jLabelIle;
private JLabel jLabelTarih;
private JLabel jLabelMusteriBilgi;
private TablePanel tablePanelSatis;
private JPanel jPanelTutarBilgi;
private JLabel jLabelToplamTutarBilgi;
private JLabel jLabelTutarTL;
private JLabel jLabelToplamTutar;
private JPanel panel;
private JLabel jLabelToplamPrimTutarBilgi;
private JLabel jLabelPrimTL;
private JLabel jLabelToplamPrimTutar;
private double toplamTutar;
private double toplamPrimTutar;
private List<YapilanSatis> satisListesi;
private JButton jButtonGeri;
private Musteri musteriBilgi;
private String musteriTuruBilgi;
private String musteriAd = null;
private JLabel jLabelMusteriAd;
private final String B_MUSTERI = "bireysel";
private final String K_MUSTERI = "kurumsal";
public static PSAMusteriHareketEkrani getInstance(Musteri musteri,
String musteriTuru) {
instance = new PSAMusteriHareketEkrani(musteri, musteriTuru);
return instance;
}
public PSAMusteriHareketEkrani(Musteri musteri, String musteriTuru) {
musteriBilgi = musteri;
musteriTuruBilgi = musteriTuru;
initialize();
}
private void initialize() {
setTitle("PSA MERKEZ MÜŞTERİ HAREKET BİLGİSİ SAYFASI");
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setBounds(0, 0, 754, 633);
setResizable(false);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
setIconImage(PSAUtil.getImageIconViaRelativePath("picture/psaIcon.png")
.getImage());
contentPane.setLayout(null);
jPanelFiltre = new JPanel();
jPanelFiltre.setLayout(null);
jPanelFiltre.setBackground(SystemColor.controlShadow);
jPanelFiltre.setBounds(10, 11, 718, 66);
contentPane.add(jPanelFiltre);
jDateChooserIlkTarih = new JDateChooser(new Date());
jDateChooserIlkTarih.setBounds(427, 35, 110, 20);
jPanelFiltre.add(jDateChooserIlkTarih);
jDateChooserSonTarih = new JDateChooser(new Date());
jDateChooserSonTarih.setBounds(574, 35, 110, 20);
jPanelFiltre.add(jDateChooserSonTarih);
((JTextFieldDateEditor) jDateChooserIlkTarih.getDateEditor())
.setEditable(false);
((JTextFieldDateEditor) jDateChooserIlkTarih.getDateEditor())
.setFocusable(false);
((JTextFieldDateEditor) jDateChooserSonTarih.getDateEditor())
.setEditable(false);
((JTextFieldDateEditor) jDateChooserSonTarih.getDateEditor())
.setFocusable(false);
jLabelIle = new JLabel();
jLabelIle.setText("ile");
jLabelIle.setFont(new Font("Tahoma", Font.BOLD, 15));
jLabelIle.setBounds(547, 35, 24, 20);
jPanelFiltre.add(jLabelIle);
jLabelTarih = new JLabel();
jLabelTarih.setText("Tarih aralığı seçiniz :");
jLabelTarih.setFont(new Font("Tahoma", Font.BOLD, 14));
jLabelTarih.setBounds(427, 7, 257, 20);
jPanelFiltre.add(jLabelTarih);
jLabelMusteriBilgi = new JLabel();
jLabelMusteriBilgi.setText("Müşteri Bilgisi :");
jLabelMusteriBilgi.setFont(new Font("Tahoma", Font.BOLD, 14));
jLabelMusteriBilgi.setBounds(6, 7, 411, 20);
jPanelFiltre.add(jLabelMusteriBilgi);
jLabelMusteriAd = new JLabel("");
jLabelMusteriAd.setFont(new Font("Tahoma", Font.PLAIN, 13));
jLabelMusteriAd.setBounds(120, 8, 242, 20);
jPanelFiltre.add(jLabelMusteriAd);
tablePanelSatis = new TablePanel((String) null, YapilanSatis.class,
true, true);
tablePanelSatis.getTable().setAutoResizeMode(
JTable.AUTO_RESIZE_ALL_COLUMNS);
tablePanelSatis.setBorder(new LineBorder(new Color(0, 0, 0), 2));
tablePanelSatis.setBounds(10, 88, 718, 352);
contentPane.add(tablePanelSatis);
jPanelTutarBilgi = new JPanel();
jPanelTutarBilgi.setLayout(null);
jPanelTutarBilgi.setBackground(SystemColor.controlShadow);
jPanelTutarBilgi.setBounds(128, 464, 600, 30);
contentPane.add(jPanelTutarBilgi);
jLabelToplamTutarBilgi = new JLabel();
jLabelToplamTutarBilgi.setText("TOPLAM TUTAR");
jLabelToplamTutarBilgi.setHorizontalAlignment(SwingConstants.CENTER);
jLabelToplamTutarBilgi.setFont(new Font("Tahoma", Font.BOLD, 15));
jLabelToplamTutarBilgi.setBounds(0, 5, 233, 20);
jPanelTutarBilgi.add(jLabelToplamTutarBilgi);
jLabelTutarTL = new JLabel();
jLabelTutarTL.setText("TL");
jLabelTutarTL.setHorizontalAlignment(SwingConstants.CENTER);
jLabelTutarTL.setFont(new Font("Tahoma", Font.BOLD, 13));
jLabelTutarTL.setBounds(570, 6, 20, 20);
jPanelTutarBilgi.add(jLabelTutarTL);
jLabelToplamTutar = new JLabel();
jLabelToplamTutar.setText("0");
jLabelToplamTutar.setHorizontalAlignment(SwingConstants.RIGHT);
jLabelToplamTutar.setFont(new Font("Tahoma", Font.BOLD, 15));
jLabelToplamTutar.setBounds(381, 5, 160, 20);
jPanelTutarBilgi.add(jLabelToplamTutar);
panel = new JPanel();
panel.setLayout(null);
panel.setBackground(SystemColor.controlShadow);
panel.setBounds(128, 499, 600, 30);
contentPane.add(panel);
jLabelToplamPrimTutarBilgi = new JLabel();
jLabelToplamPrimTutarBilgi.setText("TOPLAM İSKONTO TUTAR");
jLabelToplamPrimTutarBilgi
.setHorizontalAlignment(SwingConstants.CENTER);
jLabelToplamPrimTutarBilgi.setFont(new Font("Tahoma", Font.BOLD, 15));
jLabelToplamPrimTutarBilgi.setBounds(0, 5, 233, 20);
panel.add(jLabelToplamPrimTutarBilgi);
jLabelPrimTL = new JLabel();
jLabelPrimTL.setText("TL");
jLabelPrimTL.setHorizontalAlignment(SwingConstants.CENTER);
jLabelPrimTL.setFont(new Font("Tahoma", Font.BOLD, 13));
jLabelPrimTL.setBounds(570, 6, 20, 20);
panel.add(jLabelPrimTL);
jLabelToplamPrimTutar = new JLabel();
jLabelToplamPrimTutar.setText("0");
jLabelToplamPrimTutar.setHorizontalAlignment(SwingConstants.RIGHT);
jLabelToplamPrimTutar.setFont(new Font("Tahoma", Font.BOLD, 15));
jLabelToplamPrimTutar.setBounds(381, 5, 160, 20);
panel.add(jLabelToplamPrimTutar);
jButtonGeri = new JButton();
jButtonGeri.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/geriButonu.png"));
jButtonGeri.setText("GERİ DÖN");
jButtonGeri.setFont(new Font("Tahoma", Font.BOLD, 15));
jButtonGeri.setBounds(243, 540, 233, 41);
contentPane.add(jButtonGeri);
fillMusteriName();
initListener();
}
private void fillMusteriName() {
if (musteriTuruBilgi.equals(B_MUSTERI)) {
BireyselMusteri bm = (BireyselMusteri) musteriBilgi;
musteriAd = bm.getAd() + " " + bm.getSoyad();
} else if (musteriTuruBilgi.equals(K_MUSTERI)) {
KurumsalMusteri km = (KurumsalMusteri) musteriBilgi;
musteriAd = km.getFirmaUnvani();
}
jLabelMusteriBilgi.setText(musteriAd);
dateControl();
}
private void initListener() {
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
musteriEkraniSetEnable();
}
});
jDateChooserIlkTarih.getDateEditor().addPropertyChangeListener(
new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("date")) {
dateControl();
}
}
});
jDateChooserSonTarih.getDateEditor().addPropertyChangeListener(
new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("date")) {
dateControl();
}
}
});
jButtonGeri.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
musteriEkraniSetEnable();
}
});
}
protected void musteriEkraniSetEnable() {
PSAMusteriEkrani.getInstance(1).setEnabled(true);
instance.dispose();
}
@SuppressWarnings("unchecked")
protected void dateControl() {
tablePanelSatis.getTable().clearSelection();
satisListesi = (List<YapilanSatis>) musteriSatisHqlQuery(musteriAd,
jDateChooserIlkTarih.getDate(), jDateChooserSonTarih.getDate());
tablePanelSatis.setObjects(satisListesi);
tutarLabelDuzenle();
}
private void tutarLabelDuzenle() {
toplamTutar = 0;
toplamPrimTutar = 0;
for (int i = 0; i < satisListesi.size(); i++) {
double iade = satisListesi.get(i).getIadeTutar();
toplamTutar += satisListesi.get(i).getTutar() + iade;
toplamPrimTutar += (satisListesi.get(i).getIskontoMiktari());
}
BigDecimal toplamTutarBG = new BigDecimal(toplamTutar);
String toplamTutar;
String[] fiyat = decimalControl(String.valueOf(toplamTutarBG));
if (fiyat.length != 1) {
if (fiyat[1].length() > 1) {
if (fiyat[1].length() > 2) {
if (Integer.parseInt(fiyat[1].substring(2, 3)) > 5) {
toplamTutar = fiyat[0]
+ "."
+ String.valueOf(Integer.parseInt(fiyat[1]
.substring(0, 2)) + 1);
} else {
toplamTutar = fiyat[0] + "." + fiyat[1].substring(0, 2);
}
} else {
toplamTutar = fiyat[0] + "." + fiyat[1].substring(0, 2);
}
} else {
toplamTutar = fiyat[0] + "." + fiyat[1].substring(0, 1) + "0";
}
} else {
toplamTutar = String.valueOf(toplamTutarBG);
}
toplamTutarBG = BigDecimal.valueOf(Double.parseDouble(toplamTutar));
String dividedPriceTutar = Utils.DECIMAL_FORMAT.format(toplamTutarBG);
jLabelToplamTutar.setText(dividedPriceTutar);
BigDecimal toplamPrimTutarBG = new BigDecimal(toplamPrimTutar);
String toplamPrimTutar;
String[] fiyatPrim = decimalControl(String.valueOf(toplamPrimTutarBG));
if (fiyatPrim.length != 1) {
if (fiyatPrim[1].length() > 1) {
if (fiyatPrim[1].length() > 2) {
if (Integer.parseInt(fiyatPrim[1].substring(2, 3)) > 5) {
toplamPrimTutar = fiyatPrim[0]
+ "."
+ String.valueOf(Integer.parseInt(fiyatPrim[1]
.substring(0, 2)) + 1);
} else {
toplamPrimTutar = fiyatPrim[0] + "."
+ fiyatPrim[1].substring(0, 2);
}
} else {
toplamPrimTutar = fiyatPrim[0] + "."
+ fiyatPrim[1].substring(0, 2);
}
} else {
toplamPrimTutar = fiyatPrim[0] + "."
+ fiyatPrim[1].substring(0, 1) + "0";
}
} else {
toplamPrimTutar = String.valueOf(toplamPrimTutarBG);
}
toplamPrimTutarBG = BigDecimal.valueOf(Double
.parseDouble(toplamPrimTutar));
String dividedPricePrim = Utils.DECIMAL_FORMAT
.format(toplamPrimTutarBG);
jLabelToplamPrimTutar.setText(dividedPricePrim);
}
private static String[] decimalControl(String valueOf) {
String[] parts = valueOf.split("\\.");
return parts;
}
private static List<?> musteriSatisHqlQuery(String musteri, 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 YapilanSatis where ");
sBuilder.append("musteri ='").append(musteri).append("' and ");
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;
}
}