package gui;
import hibernateSwingApi.component.CComboBox;
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.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.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.Personel;
import pojos.YapilanSatis;
import service.PSAUtil;
import com.toedter.calendar.JDateChooser;
import com.toedter.calendar.JTextFieldDateEditor;
public class PSAPersonelPrim extends JFrame {
private static final long serialVersionUID = 1L;
private static PSAPersonelPrim instance;
private JPanel contentPane;
private JPanel jPanelFiltre;
private JDateChooser jDateChooserIlkTarih;
private JDateChooser jDateChooserSonTarih;
private JLabel jLabelIle;
private JLabel jLabelTarih;
private JLabel jLabelPersonelBilgi;
private CComboBox<Personel> cComboBoxPersonel;
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 Personel per;
public static PSAPersonelPrim getInstance(Personel personel) {
instance = new PSAPersonelPrim(personel);
return instance;
}
public PSAPersonelPrim(Personel personel) {
per = personel;
initialize();
}
private void initialize() {
setTitle("PSA MERKEZ PERSONEL PRİM BİLGİSİ EKRANI");
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);
jLabelPersonelBilgi = new JLabel();
jLabelPersonelBilgi
.setText("Prim miktarını görmek istediğiniz personeli seçiniz :");
jLabelPersonelBilgi.setFont(new Font("Tahoma", Font.BOLD, 14));
jLabelPersonelBilgi.setBounds(6, 7, 362, 20);
jPanelFiltre.add(jLabelPersonelBilgi);
cComboBoxPersonel = new CComboBox<Personel>(Personel.class);
cComboBoxPersonel.setBounds(6, 35, 351, 20);
jPanelFiltre.add(cComboBoxPersonel);
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 PRİM 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);
initListener();
personelDoldur();
}
private void personelDoldur() {
cComboBoxPersonel.refresh();
for (int i = 0; i < cComboBoxPersonel.getItemCount(); i++) {
String personelBilgi = per.getAd() + " " + per.getSoyad();
if (personelBilgi.equals(cComboBoxPersonel.getItemAt(i).toString())) {
cComboBoxPersonel.setSelectedIndex(i);
break;
}
}
}
private void initListener() {
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
anaEkranSetEnable();
}
});
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();
}
}
});
cComboBoxPersonel.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent evt) {
if (evt.getStateChange() == ItemEvent.DESELECTED) {
return;
}
dateControl();
}
});
jButtonGeri.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
anaEkranSetEnable();
}
});
}
protected void anaEkranSetEnable() {
PSAPersonelEkrani.getInstance(1).setEnabled(true);
instance.dispose();
}
@SuppressWarnings("unchecked")
protected void dateControl() {
tablePanelSatis.getTable().clearSelection();
String personelName = cComboBoxPersonel.getSelectedItem().toString();
satisListesi = (List<YapilanSatis>) primSatisHqlQuery(personelName,
jDateChooserIlkTarih.getDate(), jDateChooserSonTarih.getDate());
tablePanelSatis.setObjects(satisListesi);
tutarLabelDuzenle();
}
private void tutarLabelDuzenle() {
toplamTutar = 0;
toplamPrimTutar = 0;
for (int i = 0; i < satisListesi.size(); i++) {
toplamTutar += satisListesi.get(i).getTutar();
toplamPrimTutar += (satisListesi.get(i).getTutar() * (satisListesi
.get(i).getPersonelPrimOrani() / 100.0));
}
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<?> primSatisHqlQuery(String personelName, 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("personel ='").append(personelName).append("' and ");
sBuilder.append("systemName = '").append(
HibernateManager.getInstance().getSystemName() + "' 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;
}
}