package iade.gui;
import giris.gui.PSAAnaEkran;
import hibernateSwingApi.component.TablePanel;
import hibernateSwingApi.hibernate.HibernateManager;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
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.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.hibernate.Query;
import org.hibernate.Session;
import satis.pojos.YapilanSatis;
import satisBilgi.pojos.UrunSatis;
import satisBilgi.pojos.UrunSatisAraPojo;
import satisBilgi.pojos.UrunSatisKasa;
import service.PSAUtil;
import com.toedter.calendar.JDateChooser;
import com.toedter.calendar.JTextFieldDateEditor;
public class PSAIadeEkrani extends JFrame {
private static final long serialVersionUID = 1L;
private static PSAIadeEkrani instance;
private static TablePanel cTablePanelIade;
private JLabel jLabelSatisListe;
private JButton jButtonUrunParaIade;
private UrunSatis urunSatis;
private UrunSatisKasa usk;
private JButton jButtonGeri;
private JPopupMenu pMenu;
private JMenuItem menuItem2;
private JPanel jPanelTarih;
private JLabel jLabelTarih;
private static JDateChooser jDateChooserIlkTarih;
private static JDateChooser jDateChooserSonTarih;
private YapilanSatis st;
public static PSAIadeEkrani getInstance(int state) {
if (state == 0)
instance = new PSAIadeEkrani();
return instance;
}
private PSAIadeEkrani() {
initialize();
}
private void initialize() {
setTitle("PSA IADE EKRANI");
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setBounds(0, 0, 1000, 554);
setIconImage(PSAUtil.getImageIconViaRelativePath("picture/psaIcon.png")
.getImage());
pMenu = new JPopupMenu();
menuItem2 = new JMenuItem("Ürün İade");
pMenu.add(menuItem2);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[] { 412, 72, 460, 0 };
gridBagLayout.rowHeights = new int[] { 58, 362, 50, 0 };
gridBagLayout.columnWeights = new double[] { 1.0, 1.0, 1.0,
Double.MIN_VALUE };
gridBagLayout.rowWeights = new double[] { 0.0, 1.0, 1.0,
Double.MIN_VALUE };
getContentPane().setLayout(gridBagLayout);
jLabelSatisListe = new JLabel("SATIŞ LİSTESİ");
GridBagConstraints gbc_jLabelSatisListe = new GridBagConstraints();
gbc_jLabelSatisListe.anchor = GridBagConstraints.SOUTHWEST;
gbc_jLabelSatisListe.insets = new Insets(0, 0, 5, 5);
gbc_jLabelSatisListe.gridx = 0;
gbc_jLabelSatisListe.gridy = 0;
getContentPane().add(jLabelSatisListe, gbc_jLabelSatisListe);
jLabelSatisListe.setFont(new Font("Tahoma", Font.BOLD, 15));
jPanelTarih = new JPanel();
jPanelTarih.setBackground(SystemColor.controlShadow);
GridBagConstraints gbc_jPanelTarih = new GridBagConstraints();
gbc_jPanelTarih.fill = GridBagConstraints.BOTH;
gbc_jPanelTarih.insets = new Insets(0, 0, 5, 0);
gbc_jPanelTarih.gridx = 2;
gbc_jPanelTarih.gridy = 0;
getContentPane().add(jPanelTarih, gbc_jPanelTarih);
GridBagLayout gbl_jPanelTarih = new GridBagLayout();
gbl_jPanelTarih.columnWidths = new int[] { 0, 121, 138, 138, 0, 0 };
gbl_jPanelTarih.rowHeights = new int[] { 20, 0 };
gbl_jPanelTarih.columnWeights = new double[] { 0.0, 1.0, 1.0, 1.0, 0.0,
Double.MIN_VALUE };
gbl_jPanelTarih.rowWeights = new double[] { 1.0, Double.MIN_VALUE };
jPanelTarih.setLayout(gbl_jPanelTarih);
jLabelTarih = new JLabel("ARALIK SEÇİNİZ");
jLabelTarih.setFont(new Font("Tahoma", Font.BOLD, 14));
GridBagConstraints gbc_jLabelTarih = new GridBagConstraints();
gbc_jLabelTarih.anchor = GridBagConstraints.WEST;
gbc_jLabelTarih.insets = new Insets(0, 0, 0, 5);
gbc_jLabelTarih.gridx = 1;
gbc_jLabelTarih.gridy = 0;
jPanelTarih.add(jLabelTarih, gbc_jLabelTarih);
jDateChooserIlkTarih = new JDateChooser();
GridBagConstraints gbc_jDateChooserIlkTarih = new GridBagConstraints();
gbc_jDateChooserIlkTarih.fill = GridBagConstraints.HORIZONTAL;
gbc_jDateChooserIlkTarih.insets = new Insets(0, 0, 0, 5);
gbc_jDateChooserIlkTarih.gridx = 2;
gbc_jDateChooserIlkTarih.gridy = 0;
jPanelTarih.add(jDateChooserIlkTarih, gbc_jDateChooserIlkTarih);
jDateChooserIlkTarih.setDate(new Date());
((JTextFieldDateEditor) jDateChooserIlkTarih.getDateEditor())
.setEditable(false);
((JTextFieldDateEditor) jDateChooserIlkTarih.getDateEditor())
.setFocusable(false);
jDateChooserSonTarih = new JDateChooser();
GridBagConstraints gbc_jDateChooserSonTarih = new GridBagConstraints();
gbc_jDateChooserSonTarih.fill = GridBagConstraints.HORIZONTAL;
gbc_jDateChooserSonTarih.insets = new Insets(0, 0, 0, 5);
gbc_jDateChooserSonTarih.gridx = 3;
gbc_jDateChooserSonTarih.gridy = 0;
jPanelTarih.add(jDateChooserSonTarih, gbc_jDateChooserSonTarih);
jDateChooserSonTarih.setDate(new Date());
((JTextFieldDateEditor) jDateChooserSonTarih.getDateEditor())
.setEditable(false);
((JTextFieldDateEditor) jDateChooserSonTarih.getDateEditor())
.setFocusable(false);
cTablePanelIade = new TablePanel((String) null, UrunSatisAraPojo.class,
true, true);
cTablePanelIade.getRaporButton().setVisible(false);
GridBagConstraints gbc_cTablePanelIade = new GridBagConstraints();
gbc_cTablePanelIade.fill = GridBagConstraints.BOTH;
gbc_cTablePanelIade.insets = new Insets(0, 0, 5, 0);
gbc_cTablePanelIade.gridwidth = 3;
gbc_cTablePanelIade.gridx = 0;
gbc_cTablePanelIade.gridy = 1;
getContentPane().add(cTablePanelIade, gbc_cTablePanelIade);
cTablePanelIade.getTable().setAutoResizeMode(
JTable.AUTO_RESIZE_ALL_COLUMNS);
cTablePanelIade.setBorder(new LineBorder(new Color(0, 0, 0), 2));
cTablePanelIade.getTable().setBorder(
new TitledBorder(null, "", TitledBorder.LEADING,
TitledBorder.TOP, null, null));
jButtonGeri = new JButton();
GridBagConstraints gbc_jButtonGeri = new GridBagConstraints();
gbc_jButtonGeri.fill = GridBagConstraints.BOTH;
gbc_jButtonGeri.insets = new Insets(0, 0, 0, 5);
gbc_jButtonGeri.gridx = 0;
gbc_jButtonGeri.gridy = 2;
getContentPane().add(jButtonGeri, gbc_jButtonGeri);
jButtonGeri.setText("İPTAL");
jButtonGeri.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/geriButonu.png"));
jButtonGeri.setFont(new Font("Tahoma", Font.BOLD, 15));
jButtonUrunParaIade = new JButton();
GridBagConstraints gbc_jButtonUrunParaIade = new GridBagConstraints();
gbc_jButtonUrunParaIade.fill = GridBagConstraints.BOTH;
gbc_jButtonUrunParaIade.gridx = 2;
gbc_jButtonUrunParaIade.gridy = 2;
getContentPane().add(jButtonUrunParaIade, gbc_jButtonUrunParaIade);
jButtonUrunParaIade.setText("ÜRÜN İADE");
jButtonUrunParaIade.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/pesin.png"));
jButtonUrunParaIade.setFont(new Font("Tahoma", Font.BOLD, 15));
jButtonUrunParaIade.setEnabled(false);
tabloDoldur();
initListener();
}
private void initListener() {
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
anaEkranSetEnable();
}
});
jButtonUrunParaIade.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
urunIadeMethod();
}
});
jButtonGeri.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
anaEkranSetEnable();
}
});
cTablePanelIade.getTable().getSelectionModel()
.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent evt) {
if (evt.getValueIsAdjusting()) {
return;
}
usk = (UrunSatisKasa) cTablePanelIade
.getSelectedObject();
if (usk != null) {
jButtonUrunParaIade.setEnabled(true);
return;
}
jButtonUrunParaIade.setEnabled(false);
}
});
cTablePanelIade.getTable().addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
iadeTablePopupMenu(e);
}
}
@Override
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
iadeTablePopupMenu(e);
}
}
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
urunIadeMethod();
}
}
});
menuItem2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
urunIadeMethod();
}
});
jDateChooserIlkTarih.getDateEditor().addPropertyChangeListener(
new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("date")) {
tabloDoldur();
}
}
});
jDateChooserSonTarih.getDateEditor().addPropertyChangeListener(
new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("date")) {
tabloDoldur();
}
}
});
}
protected void iadeTablePopupMenu(MouseEvent e) {
Point p = e.getPoint();
int rowNumber = cTablePanelIade.getTable().rowAtPoint(p);
ListSelectionModel model = cTablePanelIade.getTable()
.getSelectionModel();
model.setSelectionInterval(rowNumber, rowNumber);
pMenu.show(e.getComponent(), e.getX(), e.getY());
}
private static List<?> urunIadeHqlQuery(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("yapilanSatis = ").append("1");
sBuilder.append(" and satisDurum >= '").append("NS'");
sBuilder.append(" and 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;
}
protected void urunIadeMethod() {
urunSatis = (UrunSatis) HibernateManager.getInstance()
.getObjectWithUniqueColumn(UrunSatis.class, "id", usk.getId());
st = (YapilanSatis) HibernateManager.getInstance()
.getObjectWithUniqueColumn(YapilanSatis.class, "id",
urunSatis.getSatisId());
PSAUrunParaIadeEkrani.getInstance(st, urunSatis).setVisible(true);
setEnabled(false);
}
protected void anaEkranSetEnable() {
PSAAnaEkran.getInstance(null).setEnabled(true);
instance.dispose();
}
@SuppressWarnings("unchecked")
public static void tabloDoldur() {
List<UrunSatis> satisListesi = (List<UrunSatis>) urunIadeHqlQuery(
jDateChooserIlkTarih.getDate(), jDateChooserSonTarih.getDate());
List<UrunSatisKasa> convertToUrunSatisKasa = convertToUrunSatisKasa(satisListesi);
cTablePanelIade.setObjects(convertToUrunSatisKasa);
}
private static List<UrunSatisKasa> convertToUrunSatisKasa(
List<UrunSatis> list) {
List<UrunSatisKasa> kasaList = new LinkedList<>();
for (UrunSatis urunSatis : list) {
UrunSatisKasa kasaUrun = new UrunSatisKasa(urunSatis);
kasaList.add(kasaUrun);
}
return kasaList;
}
}