package satis.gui;
import giris.gui.PSAAnaEkran;
import hibernateSwingApi.component.CTable;
import hibernateSwingApi.component.CTextField;
import hibernateSwingApi.hibernate.HibernateManager;
import java.awt.AWTException;
import java.awt.Checkbox;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
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.JTable;
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 personel.pojos.Personel;
import satis.manager.SatisManager;
import satisBilgi.pojos.OdemeBilgileri;
import satisBilgi.pojos.UrunSatis;
import service.AutoClosingMessageDialog;
import service.PSAUtil;
import banka.pojos.POSBilgileri;
import banka.pojos.POSBilgileriSatis;
import cari.pojos.Musteri;
import control.DecimalValue;
import control.PriceControl;
public class PSAKrediKartOdemeEkrani extends JFrame {
private static final long serialVersionUID = 1L;
private static PSAKrediKartOdemeEkrani instance;
private JPanel jPanelToplamTutar;
private JLabel jLabelToplamTutar;
private JLabel jLabelToplamTutarBilgisi;
private JPanel jPanelTutar;
private JLabel jLabelKrediKartTutar;
private CTextField jTextFieldKrediKartTutar;
private JLabel jLabelTL1;
private JButton jButtonOdemeKaydet;
private JButton jButtonİptal;
private JLabel jLabelBankaSeciniz;
private JLabel jLabelTL2;
private Robot robot;
private JPanel jPanelPosBilgisi;
private OdemeBilgileri odemeBilgisi;
private JLabel jLabelNakitTutar;
private CTextField jTextFieldNakitTutar;
private JLabel jLabelTL3;
private Checkbox cCheckBoxParcaliOdeme;
private JScrollPane scrollPaneBanka;
private CTable cTableBanka;
private POSBilgileriSatis posBilgiSatis;
private List<UrunSatis> urunListesi = new LinkedList<>();
private static final String KREDI_KART = "KREDİ KART";
private static final String PARCALI_ODEME = "PARÇALI ÖDEME";
private double iskontoMiktariBilgi;
private boolean vadeFarkBilgi;
private double kdvToplamBilgi;
private String odemeTurBilgi;
private double pesinToplamBilgi;
private Personel personelBilgi;
private Musteri musteriBilgi;
private double satisTutar;
private double kampanyaTutarBilgi;
public static PSAKrediKartOdemeEkrani getInstance(double iskontoMiktari,
boolean vadeFark, double kdvToplam, String odemeTur,
double pesinToplam, double kampanyaTutar, Personel personel,
Musteri musteri, List<UrunSatis> urunSatisListesi) {
instance = new PSAKrediKartOdemeEkrani(iskontoMiktari, vadeFark,
kdvToplam, odemeTur, pesinToplam, kampanyaTutar, personel,
musteri, urunSatisListesi);
return instance;
}
public PSAKrediKartOdemeEkrani(double iskontoMiktari, boolean vadeFark,
double kdvToplam, String odemeTur, double pesinToplam,
double kampanyaTutar, Personel personel, Musteri musteri,
List<UrunSatis> urunSatisListesi) {
setValue(iskontoMiktari, vadeFark, kdvToplam, odemeTur, pesinToplam,
kampanyaTutar, personel, musteri, urunSatisListesi);
initialize();
}
private void setValue(double iskontoMiktari, boolean vadeFark,
double kdvToplam, String odemeTur, double pesinToplam,
double kampanyaTutar, Personel personel, Musteri musteri,
List<UrunSatis> urunSatisListesi) {
iskontoMiktariBilgi = iskontoMiktari;
vadeFarkBilgi = vadeFark;
kdvToplamBilgi = kdvToplam;
odemeTurBilgi = odemeTur;
pesinToplamBilgi = pesinToplam;
kampanyaTutarBilgi = kampanyaTutar;
personelBilgi = personel;
musteriBilgi = musteri;
urunListesi = urunSatisListesi;
}
private void initialize() {
setTitle("PSA KREDİ KARTI ÖDEME EKRANI");
setBounds(0, 0, 730, 400);
setResizable(false);
setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
setIconImage(PSAUtil.getImageIconViaRelativePath("picture/psaIcon.png")
.getImage());
setSize(751, 445);
locateFrameToMiddle();
getContentPane().setLayout(null);
jPanelToplamTutar = new JPanel();
jPanelToplamTutar.setBorder(new LineBorder(new Color(0, 0, 0), 2));
jPanelToplamTutar.setBackground(UIManager.getColor("Button.shadow"));
jPanelToplamTutar.setBounds(12, 11, 723, 58);
getContentPane().add(jPanelToplamTutar);
jPanelToplamTutar.setLayout(null);
jLabelToplamTutar = new JLabel("TOPLAM TUTAR");
jLabelToplamTutar.setBounds(179, 11, 237, 36);
jPanelToplamTutar.add(jLabelToplamTutar);
jLabelToplamTutar.setFont(new Font("Tahoma", Font.BOLD, 16));
jLabelToplamTutar.setHorizontalAlignment(SwingConstants.CENTER);
jLabelToplamTutarBilgisi = new JLabel("0.00");
jLabelToplamTutarBilgisi.setBounds(422, 11, 179, 36);
jPanelToplamTutar.add(jLabelToplamTutarBilgisi);
jLabelToplamTutarBilgisi.setHorizontalAlignment(SwingConstants.CENTER);
jLabelToplamTutarBilgisi.setFont(new Font("Tahoma", Font.BOLD, 16));
jLabelTL2 = new JLabel("TL");
jLabelTL2.setFont(new Font("Tahoma", Font.BOLD, 14));
jLabelTL2.setBounds(610, 11, 46, 36);
jPanelToplamTutar.add(jLabelTL2);
jPanelTutar = new JPanel();
jPanelTutar.setBackground(UIManager.getColor("Button.shadow"));
jPanelTutar.setBorder(new LineBorder(new Color(0, 0, 0), 2));
jPanelTutar.setBounds(336, 95, 399, 137);
getContentPane().add(jPanelTutar);
jPanelTutar.setLayout(null);
jLabelKrediKartTutar = new JLabel("KREDİ KARTI TUTAR");
jLabelKrediKartTutar.setEnabled(false);
jLabelKrediKartTutar.setHorizontalAlignment(SwingConstants.LEFT);
jLabelKrediKartTutar.setFont(new Font("Tahoma", Font.BOLD, 18));
jLabelKrediKartTutar.setBounds(10, 27, 202, 35);
jPanelTutar.add(jLabelKrediKartTutar);
jTextFieldKrediKartTutar = new CTextField();
jTextFieldKrediKartTutar.setHorizontalAlignment(SwingConstants.RIGHT);
jTextFieldKrediKartTutar.setText("0.00");
jTextFieldKrediKartTutar.setEditable(false);
jTextFieldKrediKartTutar.setFont(new Font("Tahoma", Font.PLAIN, 18));
jTextFieldKrediKartTutar.setBounds(214, 27, 110, 35);
jPanelTutar.add(jTextFieldKrediKartTutar);
jLabelTL1 = new JLabel("TL");
jLabelTL1.setEnabled(false);
jLabelTL1.setHorizontalAlignment(SwingConstants.CENTER);
jLabelTL1.setFont(new Font("Tahoma", Font.BOLD, 14));
jLabelTL1.setBounds(334, 27, 46, 35);
jPanelTutar.add(jLabelTL1);
jLabelNakitTutar = new JLabel("NAKİT TUTAR");
jLabelNakitTutar.setEnabled(false);
jLabelNakitTutar.setHorizontalAlignment(SwingConstants.LEFT);
jLabelNakitTutar.setFont(new Font("Tahoma", Font.BOLD, 18));
jLabelNakitTutar.setBounds(10, 76, 202, 35);
jPanelTutar.add(jLabelNakitTutar);
jTextFieldNakitTutar = new CTextField();
jTextFieldNakitTutar.setHorizontalAlignment(SwingConstants.RIGHT);
jTextFieldNakitTutar.setText("0.00");
jTextFieldNakitTutar.setFont(new Font("Tahoma", Font.PLAIN, 18));
jTextFieldNakitTutar.setEditable(false);
jTextFieldNakitTutar.setBounds(214, 76, 110, 35);
jPanelTutar.add(jTextFieldNakitTutar);
jLabelTL3 = new JLabel("TL");
jLabelTL3.setEnabled(false);
jLabelTL3.setHorizontalAlignment(SwingConstants.CENTER);
jLabelTL3.setFont(new Font("Tahoma", Font.BOLD, 14));
jLabelTL3.setBounds(334, 73, 46, 32);
jPanelTutar.add(jLabelTL3);
jPanelPosBilgisi = new JPanel();
jPanelPosBilgisi.setBackground(UIManager.getColor("Button.shadow"));
jPanelPosBilgisi.setBorder(new LineBorder(new Color(0, 0, 0), 2));
jPanelPosBilgisi.setBounds(10, 95, 316, 311);
getContentPane().add(jPanelPosBilgisi);
jPanelPosBilgisi.setLayout(null);
jLabelBankaSeciniz = new JLabel("BANKA SEÇİNİZ");
jLabelBankaSeciniz.setBounds(10, 0, 296, 37);
jPanelPosBilgisi.add(jLabelBankaSeciniz);
jLabelBankaSeciniz.setFont(new Font("Tahoma", Font.BOLD, 14));
jLabelBankaSeciniz.setHorizontalAlignment(SwingConstants.CENTER);
scrollPaneBanka = new JScrollPane();
scrollPaneBanka.setBounds(10, 36, 296, 264);
jPanelPosBilgisi.add(scrollPaneBanka);
cTableBanka = new CTable((String) null, POSBilgileriSatis.class);
cTableBanka.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
cTableBanka.setRowHeight(30);
scrollPaneBanka.setViewportView(cTableBanka);
cCheckBoxParcaliOdeme = new Checkbox("PARÇALI ÖDEME");
cCheckBoxParcaliOdeme.setFont(new Font("Dialog", Font.BOLD, 12));
cCheckBoxParcaliOdeme.setBounds(336, 72, 120, 23);
getContentPane().add(cCheckBoxParcaliOdeme);
jButtonOdemeKaydet = new JButton("İŞLEM TAMAMLA");
jButtonOdemeKaydet.setEnabled(false);
jButtonOdemeKaydet.setBounds(336, 255, 399, 70);
getContentPane().add(jButtonOdemeKaydet);
jButtonOdemeKaydet.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/satisTamamla.png"));
jButtonOdemeKaydet.setFont(new Font("Tahoma", Font.BOLD, 16));
jButtonİptal = new JButton("İPTAL");
jButtonİptal.setBounds(336, 336, 399, 70);
getContentPane().add(jButtonİptal);
jButtonİptal.setFont(new Font("Tahoma", Font.BOLD, 16));
jButtonİptal.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/geriButonu.png"));
refreshTable();
fillInfo();
initListeners();
}
@SuppressWarnings("unchecked")
private void refreshTable() {
List<POSBilgileri> bankaListesi = (List<POSBilgileri>) HibernateManager
.getInstance().get(POSBilgileri.class, false);
List<POSBilgileriSatis> convertToPOSBilgileri = convertToPOSBilgileriSatis(bankaListesi);
cTableBanka.setObjects(convertToPOSBilgileri);
}
private void fillInfo() {
satisTutar = pesinToplamBilgi - iskontoMiktariBilgi;
jLabelToplamTutarBilgisi.setText(DecimalValue
.getDecimalValue(satisTutar));
jTextFieldKrediKartTutar.setText(DecimalValue
.getDecimalValue(satisTutar));
}
private void initListeners() {
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
anaEkranSetEnable();
}
});
jButtonİptal.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
anaEkranSetEnable();
}
});
jTextFieldKrediKartTutar.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent arg0) {
try {
if (jTextFieldKrediKartTutar.getText().trim().equals("")) {
jTextFieldNakitTutar.setText("0.00");
return;
}
robot = new Robot();
if (PriceControl.validate(jTextFieldKrediKartTutar
.getText())) {
if (Math.abs(satisTutar) >= Double
.parseDouble(jTextFieldKrediKartTutar.getText())) {
double nakitTutar = satisTutar
- Double.parseDouble(jTextFieldKrediKartTutar
.getText());
String[] nakitTutartoString = DecimalValue
.decimalControl(DecimalValue
.getDecimalValue(nakitTutar));
String nakit;
if (nakitTutartoString[1].length() > 1) {
nakit = nakitTutartoString[0] + "."
+ nakitTutartoString[1].substring(0, 2);
} else {
nakit = nakitTutartoString[0] + "."
+ nakitTutartoString[1].substring(0, 1)
+ "0";
;
}
jTextFieldNakitTutar.setText(nakit);
} else {
robot.keyPress(KeyEvent.VK_BACK_SPACE);
robot.keyRelease(KeyEvent.VK_BACK_SPACE);
}
} else {
robot.keyPress(KeyEvent.VK_BACK_SPACE);
robot.keyRelease(KeyEvent.VK_BACK_SPACE);
}
} catch (AWTException e) {
e.printStackTrace();
}
}
});
jTextFieldKrediKartTutar.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent arg0) {
if (jTextFieldKrediKartTutar.getText().equals(""))
jTextFieldKrediKartTutar.setText("0.00");
}
@Override
public void focusGained(FocusEvent arg0) {
if (jTextFieldKrediKartTutar.getText().equals("0.00"))
jTextFieldKrediKartTutar.setText("");
}
});
jTextFieldNakitTutar.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent arg0) {
try {
if (jTextFieldNakitTutar.getText().trim().equals("")) {
jTextFieldKrediKartTutar.setText("0.00");
return;
}
robot = new Robot();
if (PriceControl.validate(jTextFieldNakitTutar.getText())) {
if (Math.abs(satisTutar) >= Double
.parseDouble(jTextFieldNakitTutar.getText())) {
double krediTutar = satisTutar
- Double.parseDouble(jTextFieldNakitTutar
.getText());
String[] krediTutartoString = DecimalValue
.decimalControl(DecimalValue
.getDecimalValue(krediTutar));
String kredi;
if (krediTutartoString[1].length() > 1) {
kredi = krediTutartoString[0] + "."
+ krediTutartoString[1].substring(0, 2);
} else {
kredi = krediTutartoString[0] + "."
+ krediTutartoString[1].substring(0, 1)
+ "0";
}
jTextFieldKrediKartTutar.setText(kredi);
} else {
robot.keyPress(KeyEvent.VK_BACK_SPACE);
robot.keyRelease(KeyEvent.VK_BACK_SPACE);
}
} else {
robot.keyPress(KeyEvent.VK_BACK_SPACE);
robot.keyRelease(KeyEvent.VK_BACK_SPACE);
}
} catch (AWTException e) {
e.printStackTrace();
}
}
});
jTextFieldNakitTutar.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent arg0) {
if (jTextFieldNakitTutar.getText().equals(""))
jTextFieldNakitTutar.setText("0.00");
}
@Override
public void focusGained(FocusEvent arg0) {
if (jTextFieldNakitTutar.getText().equals("0.00"))
jTextFieldNakitTutar.setText("");
}
});
cTableBanka.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting())
return;
posBilgiSatis = (POSBilgileriSatis) cTableBanka
.getSelectedObject();
if (posBilgiSatis == null) {
jButtonOdemeKaydet.setEnabled(false);
return;
}
jButtonOdemeKaydet.setEnabled(true);
}
});
cCheckBoxParcaliOdeme.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent evt) {
cCheckBoxStateControl();
}
});
jButtonOdemeKaydet.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
islemTamamlaClicked();
}
});
}
protected void cCheckBoxStateControl() {
jTextFieldKrediKartTutar.setText("0.00");
jTextFieldNakitTutar.setText("0.00");
if (cCheckBoxParcaliOdeme.getState()) {
enableControl(true);
odemeTurBilgi = PARCALI_ODEME;
return;
}
enableControl(false);
odemeTurBilgi = KREDI_KART;
}
private void enableControl(boolean control) {
jTextFieldKrediKartTutar.setEditable(control);
jTextFieldNakitTutar.setEditable(control);
jLabelKrediKartTutar.setEnabled(control);
jLabelNakitTutar.setEnabled(control);
jLabelTL1.setEnabled(control);
jLabelTL3.setEnabled(control);
}
protected void anaEkranSetEnable() {
PSAAnaEkran.getInstance(null).setEnabled(true);
dispose();
}
protected void islemTamamlaClicked() {
if (cCheckBoxParcaliOdeme.getState()) {
if (jTextFieldKrediKartTutar.getText().trim().equals("")
|| Double.parseDouble(jTextFieldKrediKartTutar.getText()
.trim()) == 0) {
AutoClosingMessageDialog.showAutoClosingMessageDialog(
"Kredi kart tutarı giriniz !", 2);
return;
}
if (jTextFieldNakitTutar.getText().trim().equals("")
|| Double
.parseDouble(jTextFieldNakitTutar.getText().trim()) == 0) {
AutoClosingMessageDialog.showAutoClosingMessageDialog(
"Nakit tutarı giriniz !", 2);
return;
}
}
if (posBilgiSatis == null) {
AutoClosingMessageDialog.showAutoClosingMessageDialog(
"Banka bilgisi seçiniz !", 2);
return;
}
double krediTutar = Double.parseDouble(jTextFieldKrediKartTutar
.getText());
double nakitTutar = Double.parseDouble(jTextFieldNakitTutar.getText());
POSBilgileri posBilgi = (POSBilgileri) HibernateManager.getInstance()
.getObjectWithUniqueColumn(POSBilgileri.class, "id",
posBilgiSatis.getId(), false);
boolean control;
if (odemeTurBilgi.equals(PARCALI_ODEME)) {
odemeBilgisi = SatisManager.getOdemeBilgisi(nakitTutar, krediTutar,
0, PARCALI_ODEME, posBilgi);
control = SatisManager.satisYap(iskontoMiktariBilgi, vadeFarkBilgi,
kdvToplamBilgi, odemeTurBilgi, pesinToplamBilgi,
kampanyaTutarBilgi, personelBilgi, musteriBilgi,
urunListesi, null, odemeBilgisi);
} else {
odemeBilgisi = SatisManager.getOdemeBilgisi(0, pesinToplamBilgi, 0,
KREDI_KART, posBilgi);
control = SatisManager.satisYap(iskontoMiktariBilgi, vadeFarkBilgi,
kdvToplamBilgi, odemeTurBilgi, pesinToplamBilgi,
kampanyaTutarBilgi, personelBilgi, musteriBilgi,
urunListesi, null, odemeBilgisi);
}
if (control) {
if (odemeTurBilgi.equals(PARCALI_ODEME)) {
AutoClosingMessageDialog
.showAutoClosingMessageDialog(
"Parçalı ödeme satış işlemi başarı ile tamamlanmıştır.",
2);
} else {
AutoClosingMessageDialog
.showAutoClosingMessageDialog(
"Kredi kartı ile satış işlemi başarı ile tamamlanmıştır.",
2);
}
PSAAnaEkran.getDefaultValue();
anaEkranSetEnable();
}
}
private void locateFrameToMiddle() {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int w = this.getSize().width;
int h = this.getSize().height;
int x = (dim.width - w) / 2;
int y = (dim.height - h) / 2;
setLocation(x, y);
}
private static List<POSBilgileriSatis> convertToPOSBilgileriSatis(
List<POSBilgileri> bankaListesi) {
List<POSBilgileriSatis> posList = new LinkedList<>();
for (POSBilgileri pos : bankaListesi) {
POSBilgileriSatis posBilgileriSatis = new POSBilgileriSatis(pos);
posList.add(posBilgileriSatis);
}
return posList;
}
}