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.Point;
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.MouseAdapter;
import java.awt.event.MouseEvent;
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.LinkedList;
import java.util.List;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButton;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.hibernate.Query;
import org.hibernate.Session;
import pojos.Kullanici;
import pojos.YapilanSatis;
import service.JTableToPDF;
import service.PSAUtil;
import com.toedter.calendar.JDateChooser;
import com.toedter.calendar.JTextFieldDateEditor;
public class PSAKasaEkrani extends JFrame {
private static final long serialVersionUID = 1L;
private static PSAKasaEkrani instance;
private JPanel panel;
private JRadioButton cRadioButtonTumu;
private JRadioButton cRadioButtonNakit;
private JRadioButton cRadioButtonKrediKart;
private ButtonGroup buttonGroup;
private JPanel cPanelArama;
private JDateChooser jDateChooserIlkTarih;
private JDateChooser jDateChooserSonTarih;
private JLabel cLabelIle;
private JLabel cLabelTarih;
private JLabel cLabelSatisTuru;
private TablePanel cTablePanelSatisListe;
private JButton JButtonRaporAl;
private JLabel cLabelBilgi;
private JLabel cLabelTL;
private JButton JButtonIptal;
private JLabel cLabelTutar;
private JPanel cPanelBilgi;
private double toplamTutar;
private double iskontoTutar;
private double kdvTutar;
private JPanel cPanelIskontoBilgi;
private JLabel cLabelIskontoBilgi;
private JLabel cLabelIskontoTL;
private JLabel cLabelIskontoMiktar;
private String selectedButton;
private List<YapilanSatis> satisListesi;
private JButton jButtonDetay;
private JPanel cPanelKDVBilgi;
private JLabel cLabelKDVBilgi;
private JLabel cLabelKDVTL;
private JLabel cLabelKDVMiktar;
private YapilanSatis satis;
private JPopupMenu pMenu;
private JMenuItem menuItem;
private JMenuItem menuItem2;
private JPanel jPanelSube;
private JLabel jLabelSubeBilgi;
private static CComboBox<String> cComboBoxSube;
private List<?> list = new LinkedList<>();
private final static String KREDI_KART = "KREDİ KART";
private final static String NAKIT = "NAKİT";
private final static String TUMU = "TÜMÜ";
private static final String PARCALI_ODEME = "PARÇALI ÖDEME";
private static final String DEFAULT_COMBOBOX_VALUE = "TÜMÜ";
public static PSAKasaEkrani getInstance(int state) {
if (state == 0) {
instance = new PSAKasaEkrani();
}
return instance;
}
public PSAKasaEkrani() {
initialize();
}
private void initialize() {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setBounds(0, 0, 774, 678);
getContentPane().setLayout(null);
setTitle("PSA MERKEZ SATIŞ BİLGİLERİ EKRANI");
setResizable(false);
setIconImage(PSAUtil.getImageIconViaRelativePath("picture/psaIcon.png")
.getImage());
panel = new JPanel();
panel.setBounds(10, 11, 738, 628);
panel.setLayout(null);
cPanelArama = new JPanel();
cPanelArama.setBackground(UIManager.getColor("Button.shadow"));
cPanelArama.setBounds(15, 63, 718, 66);
panel.add(cPanelArama);
cPanelArama.setLayout(null);
buttonGroup = new ButtonGroup();
cRadioButtonTumu = new JRadioButton();
cRadioButtonNakit = new JRadioButton();
cRadioButtonKrediKart = new JRadioButton();
buttonGroup.add(cRadioButtonTumu);
buttonGroup.add(cRadioButtonNakit);
buttonGroup.add(cRadioButtonKrediKart);
cRadioButtonTumu.setBackground(UIManager.getColor("Button.shadow"));
cRadioButtonTumu.setFont(new Font("Tahoma", Font.BOLD, 12));
cRadioButtonTumu.setBounds(6, 34, 59, 21);
cRadioButtonTumu.setSelected(true);
cPanelArama.add(cRadioButtonTumu);
cRadioButtonTumu.setText("T\u00DCM\u00DC");
cRadioButtonNakit.setBackground(UIManager.getColor("Button.shadow"));
cRadioButtonNakit.setFont(new Font("Tahoma", Font.BOLD, 12));
cRadioButtonNakit.setBounds(67, 34, 63, 21);
cPanelArama.add(cRadioButtonNakit);
cRadioButtonNakit.setText("NAK\u0130T");
cRadioButtonKrediKart
.setBackground(UIManager.getColor("Button.shadow"));
cRadioButtonKrediKart.setFont(new Font("Tahoma", Font.BOLD, 12));
cRadioButtonKrediKart.setBounds(132, 34, 110, 21);
cPanelArama.add(cRadioButtonKrediKart);
cRadioButtonKrediKart.setText("KRED\u0130 KARTI");
jDateChooserIlkTarih = new JDateChooser();
jDateChooserIlkTarih.setDate(new Date());
jDateChooserIlkTarih.setBounds(427, 35, 110, 20);
cPanelArama.add(jDateChooserIlkTarih);
jDateChooserSonTarih = new JDateChooser();
jDateChooserSonTarih.setDate(new Date());
jDateChooserSonTarih.setBounds(574, 35, 110, 20);
cPanelArama.add(jDateChooserSonTarih);
cLabelIle = new JLabel();
cLabelIle.setFont(new Font("Tahoma", Font.BOLD, 15));
cLabelIle.setText("ile");
cLabelIle.setBounds(547, 35, 24, 20);
cPanelArama.add(cLabelIle);
cLabelTarih = new JLabel();
cLabelTarih.setFont(new Font("Tahoma", Font.BOLD, 14));
cLabelTarih.setText("Tarih aral\u0131\u011F\u0131 se\u00E7iniz :");
cLabelTarih.setBounds(427, 7, 257, 20);
cPanelArama.add(cLabelTarih);
cLabelSatisTuru = new JLabel();
cLabelSatisTuru.setFont(new Font("Tahoma", Font.BOLD, 14));
cLabelSatisTuru
.setText("Listelemek istedi\u011Finiz sat\u0131\u015F t\u00FCr\u00FCn\u00FC se\u00E7iniz :");
cLabelSatisTuru.setBounds(6, 7, 301, 20);
cPanelArama.add(cLabelSatisTuru);
cTablePanelSatisListe = new TablePanel(null, YapilanSatis.class, true,
true);
cTablePanelSatisListe.getTable().setAutoResizeMode(
JTable.AUTO_RESIZE_ALL_COLUMNS);
cTablePanelSatisListe.setBorder(new LineBorder(new Color(0, 0, 0), 2));
cTablePanelSatisListe.setBounds(15, 140, 718, 287);
panel.add(cTablePanelSatisListe);
getContentPane().add(panel);
cPanelBilgi = new JPanel();
cPanelBilgi.setBounds(133, 535, 600, 30);
panel.add(cPanelBilgi);
cPanelBilgi.setBackground(UIManager.getColor("Button.shadow"));
cPanelBilgi.setLayout(null);
cLabelBilgi = new JLabel();
cLabelBilgi.setBounds(0, 5, 233, 20);
cPanelBilgi.add(cLabelBilgi);
cLabelBilgi.setHorizontalAlignment(SwingConstants.CENTER);
cLabelBilgi.setFont(new Font("Tahoma", Font.BOLD, 15));
cLabelBilgi.setText("TOPLAM TUTAR");
cLabelTL = new JLabel();
cLabelTL.setBounds(570, 6, 20, 20);
cPanelBilgi.add(cLabelTL);
cLabelTL.setHorizontalAlignment(SwingConstants.CENTER);
cLabelTL.setFont(new Font("Tahoma", Font.BOLD, 13));
cLabelTL.setText("TL");
cLabelTutar = new JLabel();
cLabelTutar.setBounds(381, 5, 160, 20);
cPanelBilgi.add(cLabelTutar);
cLabelTutar.setHorizontalAlignment(SwingConstants.CENTER);
cLabelTutar.setFont(new Font("Tahoma", Font.BOLD, 15));
JButtonRaporAl = new JButton();
JButtonRaporAl.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/rapor.png"));
JButtonRaporAl.setBounds(500, 576, 233, 41);
JButtonRaporAl.setFont(new Font("Tahoma", Font.BOLD, 15));
JButtonRaporAl.setText("RAPOR AL");
panel.add(JButtonRaporAl);
JButtonIptal = new JButton();
JButtonIptal.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/geriButonu.png"));
JButtonIptal.setBounds(255, 576, 233, 41);
JButtonIptal.setText("\u0130PTAL");
JButtonIptal.setFont(new Font("Tahoma", Font.BOLD, 15));
panel.add(JButtonIptal);
cPanelIskontoBilgi = new JPanel();
cPanelIskontoBilgi.setLayout(null);
cPanelIskontoBilgi.setBackground(SystemColor.controlShadow);
cPanelIskontoBilgi.setBounds(133, 494, 600, 30);
panel.add(cPanelIskontoBilgi);
cLabelIskontoBilgi = new JLabel();
cLabelIskontoBilgi.setText("TOPLAM \u0130SKONTO TUTARI");
cLabelIskontoBilgi.setHorizontalAlignment(SwingConstants.CENTER);
cLabelIskontoBilgi.setFont(new Font("Tahoma", Font.BOLD, 12));
cLabelIskontoBilgi.setBounds(0, 5, 233, 20);
cPanelIskontoBilgi.add(cLabelIskontoBilgi);
cLabelIskontoTL = new JLabel();
cLabelIskontoTL.setText("TL");
cLabelIskontoTL.setHorizontalAlignment(SwingConstants.CENTER);
cLabelIskontoTL.setFont(new Font("Tahoma", Font.BOLD, 11));
cLabelIskontoTL.setBounds(570, 6, 20, 20);
cPanelIskontoBilgi.add(cLabelIskontoTL);
cLabelIskontoMiktar = new JLabel();
cLabelIskontoMiktar.setHorizontalAlignment(SwingConstants.CENTER);
cLabelIskontoMiktar.setFont(new Font("Tahoma", Font.BOLD, 12));
cLabelIskontoMiktar.setBounds(381, 5, 160, 20);
cPanelIskontoBilgi.add(cLabelIskontoMiktar);
((JTextFieldDateEditor) jDateChooserIlkTarih.getDateEditor())
.setEditable(false);
((JTextFieldDateEditor) jDateChooserIlkTarih.getDateEditor())
.setFocusable(false);
((JTextFieldDateEditor) jDateChooserSonTarih.getDateEditor())
.setEditable(false);
((JTextFieldDateEditor) jDateChooserSonTarih.getDateEditor())
.setFocusable(false);
cRadioButtonKrediKart.setActionCommand(KREDI_KART);
cRadioButtonNakit.setActionCommand(NAKIT);
cRadioButtonTumu.setActionCommand(TUMU);
jButtonDetay = new JButton();
jButtonDetay.setEnabled(false);
jButtonDetay.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/detay.png"));
jButtonDetay.setText("SATI\u015E DETAYI");
jButtonDetay.setFont(new Font("Tahoma", Font.BOLD, 15));
jButtonDetay.setBounds(10, 576, 233, 41);
panel.add(jButtonDetay);
cPanelKDVBilgi = new JPanel();
cPanelKDVBilgi.setLayout(null);
cPanelKDVBilgi.setBackground(SystemColor.controlShadow);
cPanelKDVBilgi.setBounds(133, 453, 600, 30);
panel.add(cPanelKDVBilgi);
cLabelKDVBilgi = new JLabel();
cLabelKDVBilgi.setText("TOPLAM KDV TUTARI");
cLabelKDVBilgi.setHorizontalAlignment(SwingConstants.CENTER);
cLabelKDVBilgi.setFont(new Font("Tahoma", Font.BOLD, 12));
cLabelKDVBilgi.setBounds(0, 5, 233, 20);
cPanelKDVBilgi.add(cLabelKDVBilgi);
cLabelKDVTL = new JLabel();
cLabelKDVTL.setText("TL");
cLabelKDVTL.setHorizontalAlignment(SwingConstants.CENTER);
cLabelKDVTL.setFont(new Font("Tahoma", Font.BOLD, 11));
cLabelKDVTL.setBounds(570, 6, 20, 20);
cPanelKDVBilgi.add(cLabelKDVTL);
cLabelKDVMiktar = new JLabel();
cLabelKDVMiktar.setHorizontalAlignment(SwingConstants.CENTER);
cLabelKDVMiktar.setFont(new Font("Tahoma", Font.BOLD, 12));
cLabelKDVMiktar.setBounds(381, 5, 160, 20);
cPanelKDVBilgi.add(cLabelKDVMiktar);
jPanelSube = new JPanel();
jPanelSube.setBackground(UIManager.getColor("Button.shadow"));
jPanelSube.setBounds(439, 11, 294, 41);
panel.add(jPanelSube);
jLabelSubeBilgi = new JLabel();
jLabelSubeBilgi.setText("Şube :");
jLabelSubeBilgi.setFont(new Font("Tahoma", Font.BOLD, 14));
cComboBoxSube = new CComboBox<String>(null);
GroupLayout gl_jPanelSube = new GroupLayout(jPanelSube);
gl_jPanelSube.setHorizontalGroup(gl_jPanelSube.createParallelGroup(
Alignment.LEADING).addGroup(
gl_jPanelSube
.createSequentialGroup()
.addGap(10)
.addComponent(jLabelSubeBilgi,
GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)
.addGap(10)
.addComponent(cComboBoxSube, GroupLayout.DEFAULT_SIZE,
193, Short.MAX_VALUE).addGap(10)));
gl_jPanelSube
.setVerticalGroup(gl_jPanelSube
.createParallelGroup(Alignment.LEADING)
.addGroup(
gl_jPanelSube
.createSequentialGroup()
.addGap(11)
.addGroup(
gl_jPanelSube
.createParallelGroup(
Alignment.LEADING)
.addGroup(
gl_jPanelSube
.createSequentialGroup()
.addComponent(
jLabelSubeBilgi,
GroupLayout.DEFAULT_SIZE,
GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addGap(3))
.addComponent(
cComboBoxSube,
GroupLayout.DEFAULT_SIZE,
GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
.addGap(10)));
jPanelSube.setLayout(gl_jPanelSube);
pMenu = new JPopupMenu();
menuItem = new JMenuItem("Satış Detay");
pMenu.add(menuItem);
menuItem2 = new JMenuItem("Rapor Al");
pMenu.add(menuItem2);
initListener();
fillComboBox();
dateControl();
}
private static void fillComboBox() {
List<?> subeListesi = HibernateManager.getInstance()
.distinctSelectField(Kullanici.class, "systemName");
cComboBoxSube.setInitialObject(DEFAULT_COMBOBOX_VALUE);
cComboBoxSube.setObjects(subeListesi);
if (subeListesi.size() > 0) {
for (int i = 0; i < subeListesi.size(); i++) {
if (HibernateManager.getInstance().getSystemName()
.equals(subeListesi.get(i))) {
cComboBoxSube.setSelectedIndex(i);
break;
}
}
}
}
private static List<?> kasaHqlQuery(String odemeTuru, 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 ");
if (!odemeTuru.equals(TUMU)) {
sBuilder.append("( odemeTur = '").append(odemeTuru).append("' or ");
sBuilder.append("odemeTur = '").append(PARCALI_ODEME)
.append("' ) and ");
}
if (!cComboBoxSube.getSelectedItem().toString()
.equals(DEFAULT_COMBOBOX_VALUE)) {
sBuilder.append("systemName = '").append(
cComboBoxSube.getSelectedItem().toString() + "' 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;
}
private void tutarLabelDuzenle() {
toplamTutar = 0;
iskontoTutar = 0;
kdvTutar = 0;
for (int i = 0; i < satisListesi.size(); i++) {
if (satisListesi.get(i).getOdemeTur().equals(NAKIT)
|| satisListesi.get(i).getOdemeTur().equals(KREDI_KART)) {
toplamTutar += satisListesi.get(i).getTutar()
+ satisListesi.get(i).getIadeTutar();
}
if (satisListesi.get(i).getOdemeTur().equals(PARCALI_ODEME)) {
if (selectedButton.equals(NAKIT)) {
toplamTutar += satisListesi.get(i).getOdemeBilgisi()
.getNakitTutar()
+ satisListesi.get(i).getIadeTutar();
} else if (selectedButton.equals(KREDI_KART)) {
toplamTutar += satisListesi.get(i).getOdemeBilgisi()
.getKrediKartTutar();
} else if (selectedButton.equals(TUMU)) {
toplamTutar += satisListesi.get(i).getTutar()
+ satisListesi.get(i).getIadeTutar();
}
}
iskontoTutar += satisListesi.get(i).getIskontoMiktari();
kdvTutar += satisListesi.get(i).getKdvMiktari();
}
BigDecimal toplamTutarBG = new BigDecimal(toplamTutar);
String dividedPriceTutar = Utils.DECIMAL_FORMAT.format(toplamTutarBG);
cLabelTutar.setText(dividedPriceTutar);
BigDecimal iskontoTutarBGBG = new BigDecimal(iskontoTutar);
String dividedPriceIskonto = Utils.DECIMAL_FORMAT
.format(iskontoTutarBGBG);
cLabelIskontoMiktar.setText(dividedPriceIskonto);
BigDecimal kdvTutarBGBG = new BigDecimal(kdvTutar);
String dividedPriceKDV = Utils.DECIMAL_FORMAT.format(kdvTutarBGBG);
cLabelKDVMiktar.setText(dividedPriceKDV);
}
@SuppressWarnings("unchecked")
private void dateControl() {
cTablePanelSatisListe.getTable().clearSelection();
selectedButton = buttonGroup.getSelection().getActionCommand();
satisListesi = (List<YapilanSatis>) kasaHqlQuery(selectedButton,
jDateChooserIlkTarih.getDate(), jDateChooserSonTarih.getDate());
if (satisListesi.size() == 0) {
cTablePanelSatisListe.setObjects(list);
} else {
cTablePanelSatisListe.setObjects(satisListesi);
}
tutarLabelDuzenle();
}
private void initListener() {
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
anaEkranSetEnable();
}
});
JButtonIptal.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent 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();
}
}
});
JButtonRaporAl.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
raporAl();
}
});
cRadioButtonKrediKart.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
dateControl();
}
});
cRadioButtonNakit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
dateControl();
}
});
cRadioButtonTumu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
dateControl();
}
});
jButtonDetay.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
satisDetayClicked();
}
});
cTablePanelSatisListe.getTable().getSelectionModel()
.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent arg0) {
if (arg0.getValueIsAdjusting()) {
return;
}
satis = (YapilanSatis) cTablePanelSatisListe
.getSelectedObject();
if (satis == null) {
jButtonDetay.setEnabled(false);
} else {
jButtonDetay.setEnabled(true);
}
}
});
cTablePanelSatisListe.getTable().addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
satisBilgileriTablePopupMenu(e);
}
@Override
public void mouseReleased(MouseEvent e) {
satisBilgileriTablePopupMenu(e);
}
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
satisDetayClicked();
}
}
});
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
satisDetayClicked();
}
});
menuItem2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
raporAl();
}
});
cComboBoxSube.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent event) {
if (event.getStateChange() == ItemEvent.DESELECTED) {
return;
}
dateControl();
}
});
}
protected void anaEkranSetEnable() {
PSAAnaEkran.getInstance(null).setEnabled(true);
instance.dispose();
}
protected void satisBilgileriTablePopupMenu(MouseEvent e) {
if (e.isPopupTrigger()) {
Point p = e.getPoint();
int rowNumber = cTablePanelSatisListe.getTable().rowAtPoint(p);
ListSelectionModel model = cTablePanelSatisListe.getTable()
.getSelectionModel();
model.setSelectionInterval(rowNumber, rowNumber);
pMenu.show(e.getComponent(), e.getX(), e.getY());
}
}
protected void raporAl() {
@SuppressWarnings("unused")
JTableToPDF pdf = new JTableToPDF(cTablePanelSatisListe.getTable(),
jDateChooserIlkTarih, jDateChooserSonTarih, cLabelTutar,
cLabelIskontoMiktar, cLabelKDVMiktar);
}
protected void satisDetayClicked() {
if (satis != null) {
PSASatisDetayEkrani.getSatisDetayEkrani(satis).setVisible(true);
}
setEnabled(false);
}
}