// Openbravo POS is a point of sales application designed for touch screens.
// Copyright (C) 2007-2009 Openbravo, S.L.
// http://www.openbravo.com/product/pos
//
// This file is part of Openbravo POS.
//
// Openbravo 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.
//
// Openbravo 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 Openbravo POS. If not, see <http://www.gnu.org/licenses/>.
package com.openbravo.pos.inventory;
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.ListModel;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.user.BrowsableEditableData;
import com.openbravo.pos.forms.AppLocal;
import com.nordpos.device.plu.DeviceInputOutput;
import com.nordpos.device.plu.DeviceInputOutputException;
import com.nordpos.device.plu.ProductIO;
import com.openbravo.basic.BasicException;
import com.openbravo.pos.forms.AppView;
import com.openbravo.pos.forms.DataLogicSales;
import com.openbravo.pos.sales.TaxesLogic;
import com.openbravo.pos.util.RoundUtils;
import java.util.Date;
import java.util.Properties;
/**
*
* @author adrianromero
* @author Andrey Svininykh
* @version NORD POS 3
*/
public class JDlgUploadProducts extends javax.swing.JDialog {
private DeviceInputOutput m_deviceplu;
private BrowsableEditableData m_bd;
private DataLogicSales dataLogicSales;
private TaxesLogic taxesLogic;
private String locationId;
private JDlgUploadProducts(java.awt.Frame parent, boolean modal) {
super(parent, modal);
}
private JDlgUploadProducts(java.awt.Dialog parent, boolean modal) {
super(parent, modal);
}
private void init(DeviceInputOutput deviceplu, BrowsableEditableData bd, DataLogicSales dataLogicSales, String locationId) {
initComponents();
getRootPane().setDefaultButton(jcmdOK);
m_deviceplu = deviceplu;
m_bd = bd;
this.dataLogicSales = dataLogicSales;
this.locationId = locationId;
setVisible(true);
}
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());
}
}
public static void showMessage(Component parent, AppView appView, BrowsableEditableData bd, DataLogicSales dataLogicSales) {
Window window = getWindow(parent);
JDlgUploadProducts myMsg;
if (window instanceof Frame) {
myMsg = new JDlgUploadProducts((Frame) window, true);
} else {
myMsg = new JDlgUploadProducts((Dialog) window, true);
}
myMsg.init(appView.getDevicePLUs(), bd, dataLogicSales, appView.getAppUserView().getUser().getProperties().getProperty("user.location.id", appView.getDefaultInventoryLocation()));
}
/**
* 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() {
jPanel2 = new javax.swing.JPanel();
jcmdOK = new javax.swing.JButton();
jcmdCancel = new javax.swing.JButton();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle(AppLocal.getIntString("caption.upload")); // NOI18N
setResizable(false);
jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));
jcmdOK.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/button_ok.png"))); // NOI18N
jcmdOK.setText(AppLocal.getIntString("Button.OK")); // NOI18N
jcmdOK.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jcmdOKActionPerformed(evt);
}
});
jPanel2.add(jcmdOK);
jcmdCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/button_cancel.png"))); // NOI18N
jcmdCancel.setText(AppLocal.getIntString("Button.Cancel")); // NOI18N
jcmdCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jcmdCancelActionPerformed(evt);
}
});
jPanel2.add(jcmdCancel);
getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);
jPanel1.setLayout(null);
jLabel1.setText(AppLocal.getIntString("message.preparepludevice")); // NOI18N
jPanel1.add(jLabel1);
jLabel1.setBounds(20, 30, 440, 50);
getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-474)/2, (screenSize.height-161)/2, 474, 161);
}// </editor-fold>//GEN-END:initComponents
private void jcmdCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcmdCancelActionPerformed
dispose();
}//GEN-LAST:event_jcmdCancelActionPerformed
private void jcmdOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcmdOKActionPerformed
// Ponemos el estado de leyendo productos
String stext = jLabel1.getText();
jLabel1.setText(AppLocal.getIntString("label.uploadingproducts"));
jcmdOK.setEnabled(false);
jcmdCancel.setEnabled(false);
// Ejecutamos la descarga...
try {
taxesLogic = new TaxesLogic(dataLogicSales.getTaxList().list());
m_deviceplu.connectDevice();
m_deviceplu.startUploadProduct();
ListModel l = m_bd.getListModel();
int size = l.getSize();
for (int i = 0; i < size; i++) {
Object[] myprod = (Object[]) l.getElementAt(i);
ProductIO product = new ProductIO();
product.setName((String) myprod[3]);
product.setCode((String) myprod[2]);
product.setPriceBuy((Double) myprod[6]);
double price = RoundUtils.round(((Double) myprod[7]) * (1 + taxesLogic.getTaxRate((String) myprod[9], new Date())));
product.setPriceSell(price);
Properties properties = new Properties();
properties.setProperty("plu.currrent", Integer.toString(i + 1));
properties.setProperty("plu.size", Integer.toString(size));
product.setProperties(properties);
double qty = dataLogicSales.findProductStock(locationId, (String) myprod[0], null);
product.setQuantity(qty);
m_deviceplu.sendProduct(product);
}
m_deviceplu.stopUploadProduct();
MessageInf msg = new MessageInf(MessageInf.SGN_SUCCESS, AppLocal.getIntString("message.scannerok"));
msg.show(this);
} catch (DeviceInputOutputException | BasicException e) {
MessageInf msg = new MessageInf(MessageInf.SGN_WARNING, AppLocal.getIntString("message.scannerfail"), e);
msg.show(this);
} finally {
m_deviceplu.disconnectDevice();
}
// Deshacemos el estado de leyendo productos
jLabel1.setText(stext);
jcmdOK.setEnabled(true);
jcmdCancel.setEnabled(true);
dispose();
}//GEN-LAST:event_jcmdOKActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JButton jcmdCancel;
private javax.swing.JButton jcmdOK;
// End of variables declaration//GEN-END:variables
}