package urun.gui;
import giris.gui.PSAAnaEkran;
import hibernateSwingApi.component.CTable;
import hibernateSwingApi.component.TablePanel;
import hibernateSwingApi.hibernate.HibernateManager;
import hibernateSwingApi.utils.Utils;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
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.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.JPanel;
import javax.swing.JScrollPane;
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 service.AutoClosingMessageDialog;
import service.PSAUtil;
import urun.pojos.UrunSevk;
import urun.pojos.UrunSevkDetay;
import urun.pojos.UrunStok;
import com.toedter.calendar.JDateChooser;
import com.toedter.calendar.JTextFieldDateEditor;
import database.DatabaseConnection;
public class PSAUrunSevkBilgisiEkrani extends JFrame {
private static final long serialVersionUID = 1L;
private static PSAUrunSevkBilgisiEkrani instance;
private JButton jButtonConfirm;
private JButton jButtonReject;
private JButton jButtonBack;
private JScrollPane scrollPaneDetayTable;
private CTable tableDetay;
private UrunSevk urunSevk;
private JLabel jLabelSevkBilgileri;
private JLabel jLabelAralikSeciniz;
private JDateChooser jDateChooserIlkTarih;
private JDateChooser jDateChooserSonTarih;
private JLabel jLabelDetayBilgileri;
private UrunSevkDetay urunSevkDetay;
private JButton jButtonSevkIptal;
private TablePanel tableTransfer;
private JPanel jPanelTarihSecim;
private UrunStok urunStok;
private List<UrunSevk> urunSevkList = new LinkedList<>();
private List<UrunSevkDetay> stok = new LinkedList<>();
private JButton jButtonRefreshTable;
private static final String SEVK_AGREE_STATE = "ONAYLANDI";
private static final String SEVK_DEFAULT_STATE = "ONAY BEKLENİYOR";
private static final String SEVK_IGNORE_STATE = "REDDEDİLDİ";
private static final String SEVK_CANCEL_STATE = "İPTAL EDİLDİ";
public static PSAUrunSevkBilgisiEkrani getInstance() {
instance = new PSAUrunSevkBilgisiEkrani();
return instance;
}
public PSAUrunSevkBilgisiEkrani() {
initialize();
}
private void initialize() {
setTitle("PSA MERKEZ SEVK BİLGİSİ EKRANI");
setBounds(0, 0, 1026, 441);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setIconImage(PSAUtil.getImageIconViaRelativePath("picture/psaIcon.png")
.getImage());
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[] { 34, 145, 156, 437, 196, 0, 0 };
gridBagLayout.rowHeights = new int[] { 0, 60, 65, 60, 60, 17, 57, 0, 0 };
gridBagLayout.columnWeights = new double[] { 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, Double.MIN_VALUE };
gridBagLayout.rowWeights = new double[] { 1.0, 1.0, 1.0, 1.0, 1.0, 0.0,
1.0, 1.0, Double.MIN_VALUE };
getContentPane().setLayout(gridBagLayout);
jLabelSevkBilgileri = new JLabel("SEVK BİLGİLERİ");
jLabelSevkBilgileri.setFont(new Font("Tahoma", Font.BOLD, 14));
GridBagConstraints gbc_jLabelSevkBilgileri = new GridBagConstraints();
gbc_jLabelSevkBilgileri.fill = GridBagConstraints.BOTH;
gbc_jLabelSevkBilgileri.insets = new Insets(0, 0, 5, 5);
gbc_jLabelSevkBilgileri.gridx = 1;
gbc_jLabelSevkBilgileri.gridy = 1;
getContentPane().add(jLabelSevkBilgileri, gbc_jLabelSevkBilgileri);
jPanelTarihSecim = new JPanel();
jPanelTarihSecim.setBackground(UIManager.getColor("Button.shadow"));
GridBagConstraints gbc_jPanelTarihSecim = new GridBagConstraints();
gbc_jPanelTarihSecim.fill = GridBagConstraints.BOTH;
gbc_jPanelTarihSecim.insets = new Insets(0, 0, 5, 5);
gbc_jPanelTarihSecim.gridx = 3;
gbc_jPanelTarihSecim.gridy = 1;
getContentPane().add(jPanelTarihSecim, gbc_jPanelTarihSecim);
GridBagLayout gbl_jPanelTarihSecim = new GridBagLayout();
gbl_jPanelTarihSecim.columnWidths = new int[] { 0, 121, 138, 138, 0, 0 };
gbl_jPanelTarihSecim.rowHeights = new int[] { 20, 0 };
gbl_jPanelTarihSecim.columnWeights = new double[] { 0.0, 1.0, 1.0, 1.0,
0.0, Double.MIN_VALUE };
gbl_jPanelTarihSecim.rowWeights = new double[] { 1.0, Double.MIN_VALUE };
jPanelTarihSecim.setLayout(gbl_jPanelTarihSecim);
jLabelAralikSeciniz = new JLabel("ARALIK SEÇİNİZ");
GridBagConstraints gbc_jLabelAralikSeciniz = new GridBagConstraints();
gbc_jLabelAralikSeciniz.anchor = GridBagConstraints.WEST;
gbc_jLabelAralikSeciniz.insets = new Insets(0, 0, 0, 5);
gbc_jLabelAralikSeciniz.gridx = 1;
gbc_jLabelAralikSeciniz.gridy = 0;
jPanelTarihSecim.add(jLabelAralikSeciniz, gbc_jLabelAralikSeciniz);
jLabelAralikSeciniz.setFont(new Font("Tahoma", Font.BOLD, 14));
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;
jPanelTarihSecim.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.insets = new Insets(0, 0, 0, 5);
gbc_jDateChooserSonTarih.fill = GridBagConstraints.HORIZONTAL;
gbc_jDateChooserSonTarih.gridx = 3;
gbc_jDateChooserSonTarih.gridy = 0;
jPanelTarihSecim.add(jDateChooserSonTarih, gbc_jDateChooserSonTarih);
jDateChooserSonTarih.setDate(new Date());
((JTextFieldDateEditor) jDateChooserSonTarih.getDateEditor())
.setEditable(false);
((JTextFieldDateEditor) jDateChooserSonTarih.getDateEditor())
.setFocusable(false);
jButtonRefreshTable = new JButton("YENİLE");
jButtonRefreshTable.setFont(new Font("Tahoma", Font.BOLD, 14));
jButtonRefreshTable.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/refresh.png"));
GridBagConstraints gbc_jButtonRefreshTable = new GridBagConstraints();
gbc_jButtonRefreshTable.fill = GridBagConstraints.BOTH;
gbc_jButtonRefreshTable.insets = new Insets(0, 0, 5, 5);
gbc_jButtonRefreshTable.gridx = 4;
gbc_jButtonRefreshTable.gridy = 1;
getContentPane().add(jButtonRefreshTable, gbc_jButtonRefreshTable);
tableTransfer = new TablePanel((String) null, UrunSevk.class);
tableTransfer.setBorder(new LineBorder(new Color(0, 0, 0), 2));
tableTransfer.getRaporButton().setVisible(false);
GridBagConstraints gbc_tableTransfer = new GridBagConstraints();
gbc_tableTransfer.fill = GridBagConstraints.BOTH;
gbc_tableTransfer.insets = new Insets(0, 0, 5, 5);
gbc_tableTransfer.gridheight = 3;
gbc_tableTransfer.gridwidth = 3;
gbc_tableTransfer.gridx = 1;
gbc_tableTransfer.gridy = 2;
getContentPane().add(tableTransfer, gbc_tableTransfer);
jButtonConfirm = new JButton("SEVK ONAYLA");
jButtonConfirm.setFont(new Font("Tahoma", Font.BOLD, 14));
jButtonConfirm.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/sevkOnayla.png"));
GridBagConstraints gbc_jButtonConfirm = new GridBagConstraints();
gbc_jButtonConfirm.fill = GridBagConstraints.BOTH;
gbc_jButtonConfirm.insets = new Insets(0, 0, 5, 5);
gbc_jButtonConfirm.gridx = 4;
gbc_jButtonConfirm.gridy = 2;
getContentPane().add(jButtonConfirm, gbc_jButtonConfirm);
jButtonReject = new JButton("SEVK REDDET");
jButtonReject.setFont(new Font("Tahoma", Font.BOLD, 14));
jButtonReject.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/sevkReddet.png"));
GridBagConstraints gbc_jButtonReject = new GridBagConstraints();
gbc_jButtonReject.fill = GridBagConstraints.BOTH;
gbc_jButtonReject.insets = new Insets(0, 0, 5, 5);
gbc_jButtonReject.gridx = 4;
gbc_jButtonReject.gridy = 3;
getContentPane().add(jButtonReject, gbc_jButtonReject);
jButtonSevkIptal = new JButton("SEVK İPTAL");
jButtonSevkIptal.setFont(new Font("Tahoma", Font.BOLD, 14));
jButtonSevkIptal.setEnabled(false);
jButtonSevkIptal.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/sevkIptal.png"));
GridBagConstraints gbc_jButtonSevkIptal = new GridBagConstraints();
gbc_jButtonSevkIptal.fill = GridBagConstraints.BOTH;
gbc_jButtonSevkIptal.insets = new Insets(0, 0, 5, 5);
gbc_jButtonSevkIptal.gridx = 4;
gbc_jButtonSevkIptal.gridy = 4;
getContentPane().add(jButtonSevkIptal, gbc_jButtonSevkIptal);
jLabelDetayBilgileri = new JLabel("ÜRÜN DETAY");
jLabelDetayBilgileri.setFont(new Font("Tahoma", Font.BOLD, 14));
GridBagConstraints gbc_jLabelDetayBilgileri = new GridBagConstraints();
gbc_jLabelDetayBilgileri.anchor = GridBagConstraints.NORTH;
gbc_jLabelDetayBilgileri.fill = GridBagConstraints.HORIZONTAL;
gbc_jLabelDetayBilgileri.insets = new Insets(0, 0, 5, 5);
gbc_jLabelDetayBilgileri.gridx = 1;
gbc_jLabelDetayBilgileri.gridy = 5;
getContentPane().add(jLabelDetayBilgileri, gbc_jLabelDetayBilgileri);
jButtonBack = new JButton("GERİ DÖN");
jButtonBack.setFont(new Font("Tahoma", Font.BOLD, 14));
jButtonBack.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/geriButonu.png"));
GridBagConstraints gbc_jButtonBack = new GridBagConstraints();
gbc_jButtonBack.insets = new Insets(0, 0, 5, 5);
gbc_jButtonBack.fill = GridBagConstraints.BOTH;
gbc_jButtonBack.gridheight = 2;
gbc_jButtonBack.gridx = 4;
gbc_jButtonBack.gridy = 5;
getContentPane().add(jButtonBack, gbc_jButtonBack);
scrollPaneDetayTable = new JScrollPane();
GridBagConstraints gbc_scrollPaneDetayTable = new GridBagConstraints();
gbc_scrollPaneDetayTable.fill = GridBagConstraints.BOTH;
gbc_scrollPaneDetayTable.insets = new Insets(0, 0, 5, 5);
gbc_scrollPaneDetayTable.gridwidth = 3;
gbc_scrollPaneDetayTable.gridx = 1;
gbc_scrollPaneDetayTable.gridy = 6;
getContentPane().add(scrollPaneDetayTable, gbc_scrollPaneDetayTable);
tableDetay = new CTable((String) null, UrunSevkDetay.class);
scrollPaneDetayTable.setViewportView(tableDetay);
disableButtons();
dateControl();
initListeners();
}
private void disableButtons() {
jButtonConfirm.setEnabled(false);
jButtonReject.setEnabled(false);
jButtonSevkIptal.setEnabled(false);
}
private void initListeners() {
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
anaEkranSetEnable();
}
});
jButtonConfirm.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
buttonConfirmClicked();
}
});
jButtonReject.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
buttonRejectClicked();
}
});
jButtonBack.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
anaEkranSetEnable();
}
});
jButtonSevkIptal.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
sevkIptalButtonClicked();
}
});
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();
}
}
});
tableTransfer.getTable().getSelectionModel()
.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting())
return;
urunSevk = (UrunSevk) tableTransfer.getSelectedObject();
if (urunSevk == null) {
disableButtons();
emptyDetayTable();
} else {
changeButtonsState();
fillDetayTable();
}
}
});
jButtonRefreshTable.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
DatabaseConnection.databaseConnect();
dateControl();
}
});
}
protected void sevkIptalButtonClicked() {
if (!controlIfUrunStateChanged()) {
return;
}
boolean check;
urunStok = (UrunStok) HibernateManager.getInstance()
.getObjectWithUniqueColumn(UrunStok.class, "barkod",
urunSevk.getBarkod());
if (urunStok == null) {
urunStok = new UrunStok(urunSevk);
urunStok.setSystemName(urunSevk.getUrunSevkFrom());
check = HibernateManager.getInstance().save(urunStok);
} else {
urunStok.setAdet(urunStok.getAdet() + urunSevk.getAdet());
check = HibernateManager.getInstance().update(urunStok);
}
if (check) {
urunSevk.setUrunSevkState(SEVK_CANCEL_STATE);
urunSevk.setUrunSevkToDate(new Date());
boolean check1 = HibernateManager.getInstance().update(urunSevk);
if (check1) {
AutoClosingMessageDialog.showAutoClosingMessageDialog(
"Sevk işlemi iptal edilmiştir !", 2);
dateControl();
}
}
}
protected void emptyDetayTable() {
tableDetay.refresh();
}
@SuppressWarnings("unchecked")
protected void dateControl() {
urunSevkList = (List<UrunSevk>) sevkHqlQuery(
jDateChooserIlkTarih.getDate(), jDateChooserSonTarih.getDate());
tableTransfer.setObjects(urunSevkList);
}
protected void buttonRejectClicked() {
if (!controlIfUrunStateChanged()) {
return;
}
urunSevk.setUrunSevkState(SEVK_IGNORE_STATE);
urunSevk.setUrunSevkToDate(new Date());
boolean check = HibernateManager.getInstance().update(urunSevk);
if (check) {
AutoClosingMessageDialog.showAutoClosingMessageDialog(
"Sevk işlemi reddedildi !", 2);
dateControl();
}
}
protected void anaEkranSetEnable() {
PSAAnaEkran.getInstance(null).setEnabled(true);
instance.dispose();
}
protected void fillDetayTable() {
stok.clear();
urunSevkDetay = new UrunSevkDetay(urunSevk);
stok.add(urunSevkDetay);
tableDetay.setObjects(stok);
}
protected void buttonConfirmClicked() {
if (!controlIfUrunStateChanged()) {
return;
}
long urunBarkod = urunSevk.getBarkod();
boolean check1 = false;
boolean check = false;
boolean check2 = false;
UrunStok urunStokControl = (UrunStok) HibernateManager
.getInstance()
.getObjectWithUniqueColumn(UrunStok.class, "barkod", urunBarkod);
if (urunStokControl == null) {
UrunStok yeniStok = new UrunStok(urunSevk);
yeniStok.setSystemName(urunSevk.getUrunSevkTo());
check = HibernateManager.getInstance().save(yeniStok);
} else {
urunStokControl.setAdet(urunStokControl.getAdet()
+ urunSevk.getAdet());
check2 = HibernateManager.getInstance().update(urunStokControl);
}
if (check || check2) {
urunSevk.setUrunSevkState(SEVK_AGREE_STATE);
urunSevk.setUrunSevkToDate(new Date());
check1 = HibernateManager.getInstance().update(urunSevk);
}
if (check1) {
AutoClosingMessageDialog.showAutoClosingMessageDialog(
"Sevk onaylanmıştır!", 2);
dateControl();
}
}
private static List<?> sevkHqlQuery(Date from, Date to) {
String userSystemName = HibernateManager.getInstance().getSystemName();
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 UrunSevk where ");
sBuilder.append("tarih >= ").append(":from");
sBuilder.append(" and tarih <= ").append(":to");
sBuilder.append(" and ( urunSevkFrom =").append(
"'" + userSystemName + "'");
sBuilder.append(" or urunSevkTo =")
.append("'" + userSystemName + "' )");
sBuilder.append(" and ( urunSevkState =").append(
"'" + SEVK_DEFAULT_STATE + "'");
sBuilder.append(" or urunSevkState =").append(
"'" + SEVK_IGNORE_STATE + "' )");
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 stateWaitingConfirm() {
if (urunSevk.getUrunSevkTo().equals(
HibernateManager.getInstance().getSystemName())) {
jButtonConfirm.setEnabled(true);
jButtonReject.setEnabled(true);
jButtonSevkIptal.setEnabled(false);
return;
}
jButtonConfirm.setEnabled(false);
jButtonReject.setEnabled(false);
jButtonSevkIptal.setEnabled(true);
}
protected void stateReject() {
if (urunSevk.getUrunSevkTo().equals(
HibernateManager.getInstance().getSystemName())) {
jButtonSevkIptal.setEnabled(false);
jButtonConfirm.setEnabled(false);
jButtonReject.setEnabled(false);
return;
}
jButtonSevkIptal.setEnabled(true);
jButtonConfirm.setEnabled(false);
jButtonReject.setEnabled(false);
}
protected boolean controlIfUrunStateChanged() {
String firstState = urunSevk.getUrunSevkState();
DatabaseConnection.databaseConnect();
UrunSevk databaseControl = (UrunSevk) HibernateManager.getInstance()
.getObjectWithUniqueColumn(UrunSevk.class, "id",
urunSevk.getId(), false);
if (databaseControl == null) {
return false;
}
String afterControl = databaseControl.getUrunSevkState();
if (!firstState.equals(afterControl)) {
Utils.showMessage("Ürün durum bilgisi değişmiştir, sayfayı yenilemeden işlem yapmayınız !");
return false;
}
return true;
}
protected void changeButtonsState() {
String state = urunSevk.getUrunSevkState();
if (state.equals(SEVK_DEFAULT_STATE)) {
stateWaitingConfirm();
} else {
stateReject();
}
}
}