// Chromis POS - The New Face of Open Source POS
// Copyright (c) (c) 2015-2016
// http://www.chromis.co.uk
//
// This file is part of Chromis POS
//
// Chromis POS is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Chromis POS is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Chromis POS. If not, see <http://www.gnu.org/licenses/>.
package uk.chromis.pos.sales;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Window;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JFrame;
import uk.chromis.basic.BasicException;
import uk.chromis.data.loader.Session;
import uk.chromis.pos.forms.AppConfig;
import uk.chromis.pos.forms.AppLocal;
import uk.chromis.pos.forms.AppView;
import uk.chromis.pos.forms.AppViewConnection;
import uk.chromis.pos.ticket.TicketLineInfo;
import uk.chromis.pos.util.AltEncrypter;
/**
*
* @author adrianromero
*/
public class JProductLineEdit extends javax.swing.JDialog {
private TicketLineInfo returnLine;
private TicketLineInfo m_oLine;
private boolean m_bunitsok;
private boolean m_bpriceok;
private String productID;
private Session session;
private Connection connection;
private PreparedStatement pstmt;
/**
* Creates new form JProductLineEdit
*/
private JProductLineEdit(java.awt.Frame parent, boolean modal) {
super(parent, modal);
}
/**
* Creates new form JProductLineEdit
*/
private JProductLineEdit(java.awt.Dialog parent, boolean modal) {
super(parent, modal);
}
private TicketLineInfo init(AppView app, TicketLineInfo oLine) throws BasicException {
// Inicializo los componentes
initComponents();
productID = oLine.getProductID();
if (oLine.getTaxInfo() == null) {
throw new BasicException(AppLocal.getIntString("message.cannotcalculatetaxes"));
}
if (!productID.equals("xxx999_999xxx_x9x9x9")) {
m_jButtonUpdate.setVisible(AppConfig.getInstance().getBoolean("db.productupdate"));
}else{
m_jButtonUpdate.setVisible(false);
}
m_jButtonUpdate.setEnabled(false);
m_oLine = new TicketLineInfo(oLine);
m_bunitsok = true;
m_bpriceok = true;
m_jName.setEnabled(app.getAppUserView().getUser().hasPermission("uk.chromis.pos.sales.JPanelTicketEdits"));
m_jPrice.setEnabled(app.getAppUserView().getUser().hasPermission("uk.chromis.pos.sales.JPanelTicketEdits"));
m_jPriceTax.setEnabled(app.getAppUserView().getUser().hasPermission("uk.chromis.pos.sales.JPanelTicketEdits"));
m_jName.setText(oLine.getProductName());
m_jUnits.setDoubleValue(oLine.getMultiply());
m_jPrice.setDoubleValue(oLine.getPrice());
m_jPriceTax.setDoubleValue(oLine.getPriceTax());
m_jTaxrate.setText(oLine.getTaxInfo().getName());
m_jName.addPropertyChangeListener("Edition", new RecalculateName());
m_jUnits.addPropertyChangeListener("Edition", new RecalculateUnits());
m_jPrice.addPropertyChangeListener("Edition", new RecalculatePrice());
m_jPriceTax.addPropertyChangeListener("Edition", new RecalculatePriceTax());
m_jName.addEditorKeys(m_jKeys);
m_jUnits.addEditorKeys(m_jKeys);
m_jPrice.addEditorKeys(m_jKeys);
m_jPriceTax.addEditorKeys(m_jKeys);
if (m_jName.isEnabled()) {
m_jName.activate();
} else {
m_jUnits.activate();
}
printTotals();
getRootPane().setDefaultButton(m_jButtonOK);
returnLine = null;
setVisible(true);
return returnLine;
}
private void printTotals() {
if (m_bunitsok && m_bpriceok) {
m_jSubtotal.setText(m_oLine.printSubValue());
m_jTotal.setText(m_oLine.printValue());
m_jButtonOK.setEnabled(true);
} else {
m_jSubtotal.setText(null);
m_jTotal.setText(null);
m_jButtonOK.setEnabled(false);
}
}
private class RecalculateUnits implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent evt) {
Double value = m_jUnits.getDoubleValue();
if (value == null || value == 0.0) {
m_bunitsok = false;
} else {
m_oLine.setMultiply(value);
m_bunitsok = true;
}
printTotals();
}
}
private class RecalculatePrice implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent evt) {
Double value = m_jPrice.getDoubleValue();
if (value == null || value == 0.0) {
m_bpriceok = false;
} else {
m_oLine.setPrice(value);
m_jPriceTax.setDoubleValue(m_oLine.getPriceTax());
m_bpriceok = true;
m_jButtonUpdate.setEnabled(AppConfig.getInstance().getBoolean("db.productupdate"));
}
printTotals();
}
}
private class RecalculatePriceTax implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent evt) {
Double value = m_jPriceTax.getDoubleValue();
if (value == null || value == 0.0) {
// m_jPriceTax.setValue(m_oLine.getPriceTax());
m_bpriceok = false;
} else {
m_oLine.setPriceTax(value);
m_jPrice.setDoubleValue(m_oLine.getPrice());
m_bpriceok = true;
m_jButtonUpdate.setEnabled(AppConfig.getInstance().getBoolean("db.productupdate"));
}
printTotals();
}
}
private class RecalculateName implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent evt) {
m_oLine.setProperty("product.name", m_jName.getText());
}
}
private static Window getWindow(Component parent) {
if (parent == null) {
return new JFrame();
} else if (parent instanceof Frame || parent instanceof Dialog) {
return (Window) parent;
} else {
return getWindow(parent.getParent());
}
}
/**
*
* @param parent
* @param app
* @param oLine
* @return
* @throws BasicException
*/
public static TicketLineInfo showMessage(Component parent, AppView app, TicketLineInfo oLine) throws BasicException {
Window window = getWindow(parent);
JProductLineEdit myMsg;
if (window instanceof Frame) {
myMsg = new JProductLineEdit((Frame) window, true);
} else {
myMsg = new JProductLineEdit((Dialog) window, true);
}
return myMsg.init(app, oLine);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel5 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
m_jName = new uk.chromis.editor.JEditorString();
m_jUnits = new uk.chromis.editor.JEditorDouble();
m_jPrice = new uk.chromis.editor.JEditorCurrency();
m_jPriceTax = new uk.chromis.editor.JEditorCurrency();
m_jTaxrate = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
m_jTotal = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
m_jSubtotal = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
m_jButtonUpdate = new javax.swing.JButton();
m_jButtonCancel = new javax.swing.JButton();
m_jButtonOK = new javax.swing.JButton();
jPanel3 = new javax.swing.JPanel();
jPanel4 = new javax.swing.JPanel();
m_jKeys = new uk.chromis.editor.JEditorKeys();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle(AppLocal.getIntString("label.editline")); // NOI18N
jPanel5.setLayout(new java.awt.BorderLayout());
jPanel2.setLayout(null);
jLabel1.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
jLabel1.setText(AppLocal.getIntString("label.price")); // NOI18N
jPanel2.add(jLabel1);
jLabel1.setBounds(10, 80, 90, 25);
jLabel2.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
jLabel2.setText(AppLocal.getIntString("label.units")); // NOI18N
jPanel2.add(jLabel2);
jLabel2.setBounds(10, 50, 90, 25);
jLabel3.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
jLabel3.setText(AppLocal.getIntString("label.pricetax")); // NOI18N
jPanel2.add(jLabel3);
jLabel3.setBounds(10, 110, 90, 25);
jLabel4.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
jLabel4.setText(AppLocal.getIntString("label.item")); // NOI18N
jPanel2.add(jLabel4);
jLabel4.setBounds(10, 20, 90, 25);
m_jName.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
jPanel2.add(m_jName);
m_jName.setBounds(100, 20, 270, 25);
m_jUnits.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
jPanel2.add(m_jUnits);
m_jUnits.setBounds(100, 50, 240, 25);
m_jPrice.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
jPanel2.add(m_jPrice);
m_jPrice.setBounds(100, 80, 240, 25);
m_jPriceTax.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
jPanel2.add(m_jPriceTax);
m_jPriceTax.setBounds(100, 110, 240, 25);
m_jTaxrate.setBackground(javax.swing.UIManager.getDefaults().getColor("TextField.disabledBackground"));
m_jTaxrate.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
m_jTaxrate.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
m_jTaxrate.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createLineBorder(javax.swing.UIManager.getDefaults().getColor("Button.darkShadow")), javax.swing.BorderFactory.createEmptyBorder(1, 4, 1, 4)));
m_jTaxrate.setOpaque(true);
m_jTaxrate.setPreferredSize(new java.awt.Dimension(150, 25));
m_jTaxrate.setRequestFocusEnabled(false);
jPanel2.add(m_jTaxrate);
m_jTaxrate.setBounds(100, 140, 210, 25);
jLabel5.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
jLabel5.setText(AppLocal.getIntString("label.tax")); // NOI18N
jPanel2.add(jLabel5);
jLabel5.setBounds(10, 140, 90, 25);
jLabel6.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
jLabel6.setText(AppLocal.getIntString("label.totalcash")); // NOI18N
jPanel2.add(jLabel6);
jLabel6.setBounds(10, 200, 90, 25);
m_jTotal.setBackground(javax.swing.UIManager.getDefaults().getColor("TextField.disabledBackground"));
m_jTotal.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
m_jTotal.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
m_jTotal.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createLineBorder(javax.swing.UIManager.getDefaults().getColor("Button.darkShadow")), javax.swing.BorderFactory.createEmptyBorder(1, 4, 1, 4)));
m_jTotal.setOpaque(true);
m_jTotal.setPreferredSize(new java.awt.Dimension(150, 25));
m_jTotal.setRequestFocusEnabled(false);
jPanel2.add(m_jTotal);
m_jTotal.setBounds(100, 200, 210, 25);
jLabel7.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
jLabel7.setText(AppLocal.getIntString("label.subtotalcash")); // NOI18N
jPanel2.add(jLabel7);
jLabel7.setBounds(10, 170, 90, 25);
m_jSubtotal.setBackground(javax.swing.UIManager.getDefaults().getColor("TextField.disabledBackground"));
m_jSubtotal.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
m_jSubtotal.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
m_jSubtotal.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createLineBorder(javax.swing.UIManager.getDefaults().getColor("Button.darkShadow")), javax.swing.BorderFactory.createEmptyBorder(1, 4, 1, 4)));
m_jSubtotal.setOpaque(true);
m_jSubtotal.setPreferredSize(new java.awt.Dimension(150, 25));
m_jSubtotal.setRequestFocusEnabled(false);
jPanel2.add(m_jSubtotal);
m_jSubtotal.setBounds(100, 170, 210, 25);
jPanel5.add(jPanel2, java.awt.BorderLayout.CENTER);
jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));
m_jButtonUpdate.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
m_jButtonUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/uk/chromis/images/filesave.png"))); // NOI18N
java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("pos_messages"); // NOI18N
m_jButtonUpdate.setText(bundle.getString("Button.UpdateProduct")); // NOI18N
m_jButtonUpdate.setFocusPainted(false);
m_jButtonUpdate.setFocusable(false);
m_jButtonUpdate.setMargin(new java.awt.Insets(8, 16, 8, 16));
m_jButtonUpdate.setRequestFocusEnabled(false);
m_jButtonUpdate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
m_jButtonUpdateActionPerformed(evt);
}
});
jPanel1.add(m_jButtonUpdate);
m_jButtonCancel.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
m_jButtonCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/uk/chromis/images/cancel.png"))); // NOI18N
m_jButtonCancel.setText(AppLocal.getIntString("Button.Cancel")); // NOI18N
m_jButtonCancel.setFocusPainted(false);
m_jButtonCancel.setFocusable(false);
m_jButtonCancel.setMargin(new java.awt.Insets(8, 16, 8, 16));
m_jButtonCancel.setRequestFocusEnabled(false);
m_jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
m_jButtonCancelActionPerformed(evt);
}
});
jPanel1.add(m_jButtonCancel);
m_jButtonOK.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
m_jButtonOK.setIcon(new javax.swing.ImageIcon(getClass().getResource("/uk/chromis/images/ok.png"))); // NOI18N
m_jButtonOK.setText(AppLocal.getIntString("Button.OK")); // NOI18N
m_jButtonOK.setFocusPainted(false);
m_jButtonOK.setFocusable(false);
m_jButtonOK.setMargin(new java.awt.Insets(8, 16, 8, 16));
m_jButtonOK.setRequestFocusEnabled(false);
m_jButtonOK.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
m_jButtonOKActionPerformed(evt);
}
});
jPanel1.add(m_jButtonOK);
jPanel5.add(jPanel1, java.awt.BorderLayout.SOUTH);
getContentPane().add(jPanel5, java.awt.BorderLayout.CENTER);
jPanel3.setLayout(new java.awt.BorderLayout());
jPanel4.setLayout(new javax.swing.BoxLayout(jPanel4, javax.swing.BoxLayout.Y_AXIS));
jPanel4.add(m_jKeys);
jPanel3.add(jPanel4, java.awt.BorderLayout.NORTH);
getContentPane().add(jPanel3, java.awt.BorderLayout.EAST);
setSize(new java.awt.Dimension(580, 362));
setLocationRelativeTo(null);
}// </editor-fold>//GEN-END:initComponents
private void m_jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_jButtonCancelActionPerformed
dispose();
}//GEN-LAST:event_m_jButtonCancelActionPerformed
private void m_jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_jButtonOKActionPerformed
returnLine = m_oLine;
dispose();
}//GEN-LAST:event_m_jButtonOKActionPerformed
private void m_jButtonUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_jButtonUpdateActionPerformed
// Update the database with the new price passed
String db_password = (AppConfig.getInstance().getProperty("db.password"));
if (AppConfig.getInstance().getProperty("db.user") != null && db_password != null && db_password.startsWith("crypt:")) {
// the password is encrypted
AltEncrypter cypher = new AltEncrypter("cypherkey" + AppConfig.getInstance().getProperty("db.user"));
db_password = cypher.decrypt(db_password.substring(6));
}
try {
session = AppViewConnection.createSession();
connection = DriverManager.getConnection(AppConfig.getInstance().getProperty("db.URL"), AppConfig.getInstance().getProperty("db.user"), db_password);
pstmt = connection.prepareStatement("UPDATE PRODUCTS SET PRICESELL = ? WHERE ID = ?");
pstmt.setDouble(1, m_jPrice.getDoubleValue());
pstmt.setString(2, productID);
pstmt.executeUpdate();
m_jButtonUpdate.setEnabled(false);
} catch (BasicException | SQLException e) {
//put error messsage here
return;
}
m_oLine.setUpdated(true);
}//GEN-LAST:event_m_jButtonUpdateActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JPanel jPanel5;
private javax.swing.JButton m_jButtonCancel;
private javax.swing.JButton m_jButtonOK;
private javax.swing.JButton m_jButtonUpdate;
private uk.chromis.editor.JEditorKeys m_jKeys;
private uk.chromis.editor.JEditorString m_jName;
private uk.chromis.editor.JEditorCurrency m_jPrice;
private uk.chromis.editor.JEditorCurrency m_jPriceTax;
private javax.swing.JLabel m_jSubtotal;
private javax.swing.JLabel m_jTaxrate;
private javax.swing.JLabel m_jTotal;
private uk.chromis.editor.JEditorDouble m_jUnits;
// End of variables declaration//GEN-END:variables
}