package urun.gui;
import giris.gui.PSAAnaEkran;
import hibernateSwingApi.component.CComboBox;
import hibernateSwingApi.hibernate.HibernateManager;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
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.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
import kullanici.pojos.Kullanici;
import service.AutoClosingMessageDialog;
import service.PSAUtil;
import urun.pojos.Urun;
import urun.pojos.UrunSevk;
public class PSAUrunSevk extends JFrame {
private static final long serialVersionUID = 1L;
private static PSAUrunSevk instance;
private JPanel contentPane;
private JPanel jPanelUrun;
private JLabel jLabelUrunBilgi;
private JLabel jLabelUrunKodu;
private JLabel jLabelUrunKoduBilgi;
private JLabel jLabelUrunBarkod;
private JLabel jLabelUrunBarkodBilgi;
private JLabel jLabelUrunAdetBilgi;
private JLabel jLabelUrunAdet;
private JPanel jPanelSevk;
private JLabel jLabelSevkBilgi;
private JLabel jLabelSevkSubeBilgi;
private CComboBox<String> cComboBoxSube;
private JLabel jLabelUrunSevkAdet;
private JSpinner jSpinnerUrunAdet;
private JButton jButtonUrunSevk;
private Urun sevkUrun;
private JButton jBUttonGeriButton;
private static final String DEFAULT_COMBOBOX_VALUE = "Seçiniz";
private static final String SEVK_DEFAULT_STATE = "ONAY BEKLENİYOR";
public static PSAUrunSevk getInstance(Urun urun) {
instance = new PSAUrunSevk(urun);
return instance;
}
public PSAUrunSevk(Urun urun) {
sevkUrun = urun;
initialize();
}
private void initialize() {
setTitle("PSA MERKEZ ÜRÜN SEVK EKRANI");
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setIconImage(PSAUtil.getImageIconViaRelativePath("picture/psaIcon.png")
.getImage());
setBounds(100, 100, 450, 427);
setResizable(false);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
jPanelUrun = new JPanel();
jPanelUrun.setBackground(UIManager.getColor("Button.shadow"));
jPanelUrun.setBounds(10, 11, 414, 125);
contentPane.add(jPanelUrun);
jPanelUrun.setLayout(null);
jLabelUrunBilgi = new JLabel();
jLabelUrunBilgi.setText("ÜRÜN BİLGİSİ");
jLabelUrunBilgi.setHorizontalAlignment(SwingConstants.CENTER);
jLabelUrunBilgi.setFont(new Font("Tahoma", Font.BOLD, 16));
jLabelUrunBilgi.setBounds(10, 10, 394, 20);
jPanelUrun.add(jLabelUrunBilgi);
jLabelUrunKodu = new JLabel();
jLabelUrunKodu.setText("Ürün Kodu");
jLabelUrunKodu.setFont(new Font("Tahoma", Font.BOLD, 14));
jLabelUrunKodu.setBounds(53, 41, 127, 20);
jPanelUrun.add(jLabelUrunKodu);
jLabelUrunKoduBilgi = new JLabel();
jLabelUrunKoduBilgi.setText((String) null);
jLabelUrunKoduBilgi.setBounds(190, 41, 186, 20);
jPanelUrun.add(jLabelUrunKoduBilgi);
jLabelUrunBarkod = new JLabel();
jLabelUrunBarkod.setText("Ürün Barkod");
jLabelUrunBarkod.setFont(new Font("Tahoma", Font.BOLD, 14));
jLabelUrunBarkod.setBounds(53, 69, 127, 20);
jPanelUrun.add(jLabelUrunBarkod);
jLabelUrunBarkodBilgi = new JLabel();
jLabelUrunBarkodBilgi.setText((String) null);
jLabelUrunBarkodBilgi.setBounds(190, 69, 186, 20);
jPanelUrun.add(jLabelUrunBarkodBilgi);
jLabelUrunAdetBilgi = new JLabel();
jLabelUrunAdetBilgi.setText((String) null);
jLabelUrunAdetBilgi.setBounds(190, 94, 186, 20);
jPanelUrun.add(jLabelUrunAdetBilgi);
jLabelUrunAdet = new JLabel();
jLabelUrunAdet.setText("Ürün Adet");
jLabelUrunAdet.setFont(new Font("Tahoma", Font.BOLD, 14));
jLabelUrunAdet.setBounds(53, 94, 127, 20);
jPanelUrun.add(jLabelUrunAdet);
jPanelSevk = new JPanel();
jPanelSevk.setLayout(null);
jPanelSevk.setBackground(UIManager.getColor("Button.shadow"));
jPanelSevk.setBounds(10, 147, 414, 137);
contentPane.add(jPanelSevk);
jLabelSevkBilgi = new JLabel();
jLabelSevkBilgi.setText("SEVK BİLGİSİ");
jLabelSevkBilgi.setHorizontalAlignment(SwingConstants.CENTER);
jLabelSevkBilgi.setFont(new Font("Tahoma", Font.BOLD, 16));
jLabelSevkBilgi.setBounds(10, 10, 394, 20);
jPanelSevk.add(jLabelSevkBilgi);
jLabelSevkSubeBilgi = new JLabel();
jLabelSevkSubeBilgi.setText("Sevk edeceğiniz şubeyi seçiniz :");
jLabelSevkSubeBilgi.setFont(new Font("Tahoma", Font.BOLD, 14));
jLabelSevkSubeBilgi.setBounds(10, 41, 394, 20);
jPanelSevk.add(jLabelSevkSubeBilgi);
cComboBoxSube = new CComboBox<String>(null);
cComboBoxSube.setBounds(10, 72, 394, 20);
jPanelSevk.add(cComboBoxSube);
jLabelUrunSevkAdet = new JLabel();
jLabelUrunSevkAdet.setText("Sevk edilecek ürün adeti");
jLabelUrunSevkAdet.setFont(new Font("Tahoma", Font.BOLD, 14));
jLabelUrunSevkAdet.setBounds(72, 103, 175, 20);
jPanelSevk.add(jLabelUrunSevkAdet);
SpinnerModel spinnerModel = new SpinnerNumberModel(0, 0, 1000000, 1);
jSpinnerUrunAdet = new JSpinner();
jSpinnerUrunAdet.setBounds(257, 103, 58, 20);
jSpinnerUrunAdet.setModel(spinnerModel);
jPanelSevk.add(jSpinnerUrunAdet);
jButtonUrunSevk = new JButton();
jButtonUrunSevk.setText("ÜRÜNÜ SEVK ET");
jButtonUrunSevk.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/sevkEt.png"));
jButtonUrunSevk.setFont(new Font("Tahoma", Font.BOLD, 15));
jButtonUrunSevk.setBounds(60, 296, 323, 40);
contentPane.add(jButtonUrunSevk);
jBUttonGeriButton = new JButton();
jBUttonGeriButton.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/geriButonu.png"));
jBUttonGeriButton.setText("İPTAL");
jBUttonGeriButton.setFont(new Font("Tahoma", Font.BOLD, 15));
jBUttonGeriButton.setBounds(60, 347, 323, 40);
contentPane.add(jBUttonGeriButton);
initComboBox();
fillUrunBilgi();
initListener();
}
private void fillUrunBilgi() {
jLabelUrunKoduBilgi.setText(sevkUrun.getKod());
jLabelUrunBarkodBilgi.setText(String.valueOf(sevkUrun.getBarkod()));
jLabelUrunAdetBilgi.setText(String.valueOf(sevkUrun.getAdet()));
}
private void initListener() {
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
anaEkraniSetEnable();
}
});
jBUttonGeriButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
anaEkraniSetEnable();
}
});
jButtonUrunSevk.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
urunSevkClicked();
}
});
}
protected void urunSevkClicked() {
boolean check;
boolean check1;
String sevkUrunTo = cComboBoxSube.getSelectedItem().toString();
String sevkUrunFrom = HibernateManager.getInstance().getSystemName();
int sevkUrunAdet = (int) jSpinnerUrunAdet.getValue();
if (sevkUrunAdet > sevkUrun.getAdet()) {
AutoClosingMessageDialog.showAutoClosingMessageDialog(
"Ürünün adedinden fazla sevk yapılmak istenmektedir.", 2);
return;
}
if (sevkUrunAdet == 0) {
AutoClosingMessageDialog.showAutoClosingMessageDialog(
"Sevk edilecek ürün adetini giriniz.", 2);
return;
}
if (cComboBoxSube.getSelectedItem().toString()
.equals(DEFAULT_COMBOBOX_VALUE)) {
AutoClosingMessageDialog.showAutoClosingMessageDialog(
"Sevk edilecek şubeyi seçiniz.", 2);
return;
}
sevkUrun.setAdet(sevkUrun.getAdet() - sevkUrunAdet);
check = HibernateManager.getInstance().update(sevkUrun);
if (check) {
UrunSevk us = new UrunSevk(sevkUrun);
us.setAdet(sevkUrunAdet);
us.setUrunSevkFrom(sevkUrunFrom);
us.setUrunSevkTo(sevkUrunTo);
us.setUrunSevkFromDate(new Date());
us.setUrunSevkState(SEVK_DEFAULT_STATE);
check1 = HibernateManager.getInstance().save(us);
if (check1) {
AutoClosingMessageDialog.showAutoClosingMessageDialog(
"Üründen " + sevkUrunAdet + " adet " + sevkUrunFrom
+ " şubesinden " + sevkUrunTo
+ " şubesine sevk edilmiştir." + ".", 2);
jBUttonGeriButton.doClick();
}
}
}
protected void anaEkraniSetEnable() {
PSAAnaEkran.getInstance(null).setEnabled(true);
instance.dispose();
}
private void initComboBox() {
List<?> subeListesi = HibernateManager.getInstance()
.distinctSelectField(Kullanici.class, "systemName");
for (int i = 0; i < subeListesi.size(); i++) {
if (HibernateManager.getInstance().getSystemName()
.equals(subeListesi.get(i))) {
subeListesi.remove(i);
break;
}
}
cComboBoxSube.setInitialObject(DEFAULT_COMBOBOX_VALUE);
cComboBoxSube.setObjects(subeListesi);
}
}