stockjdialog.java.svn-base

来自「Store Manager」· SVN-BASE 代码 · 共 642 行 · 第 1/2 页

SVN-BASE
642
字号
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 818, Short.MAX_VALUE)                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))                        .addContainerGap())                    .addGroup(layout.createSequentialGroup()                        .addGap(500, 500, 500)                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)                        .addGap(60, 60, 60)                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)                        .addContainerGap())))        );        layout.setVerticalGroup(            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGroup(layout.createSequentialGroup()                .addContainerGap()                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)                .addGap(18, 18, 18)                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)                .addGap(18, 18, 18)                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE)                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)                    .addComponent(jLabel5)                    .addComponent(jNumberField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)                    .addComponent(jLabel6)                    .addComponent(jNumberField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))                .addGap(26, 26, 26)                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE))                .addContainerGap())        );        pack();    }// </editor-fold>//GEN-END:initComponents    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed    new StoreJDialog(this, true).setVisible(true);    storeJComboBox.setModel(getStoreComboBoxModel());}//GEN-LAST:event_jButton8ActionPerformedprivate void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing    exit();}//GEN-LAST:event_formWindowClosingprivate void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed    if (jTable1.getSelectedRow() == -1) {        JOptionPane.showMessageDialog(this, "请选择一个进货单明细!", "错误", JOptionPane.ERROR_MESSAGE);        return;    }    //StockItem si = stockItemList.get(jTable1.getSelectedRow());    stockItemList.remove(jTable1.getSelectedRow());    updateTable();}//GEN-LAST:event_jButton7ActionPerformedprivate void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed    if (jTable1.getSelectedRow() == -1) {        JOptionPane.showMessageDialog(this, "请选择一个进货单明细!", "错误", JOptionPane.ERROR_MESSAGE);        return;    }    StockItem si = stockItemList.get(jTable1.getSelectedRow());    stockItemList.remove(jTable1.getSelectedRow());    si = new StockItemEditer(this, si, true).showDialog();    stockItemList.add(si);    if (si == null) return;        updateTable();}//GEN-LAST:event_jButton6ActionPerformedprivate void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed    //添加已有的商品    StockItem si = new StockItem();    si.setStockID(stock);    si = new AddStockItemJDialog(this, si, true).showDialog();    if (si == null) return;    stockItemList.add(si);    updateTable();}//GEN-LAST:event_jButton3ActionPerformedprivate void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed    if (stock.getStockItemCollection() == null || stock.getStockItemCollection().isEmpty()) {        JOptionPane.showMessageDialog(this, "你还没有添加进货明细!");        return;    }        if (jNumberField2.getText().isEmpty()) {        JOptionPane.showMessageDialog(this, "实付金额不能为空!");        return;    }    stock.setGiveMoney(new BigDecimal(jNumberField2.getText()));    double d = Double.parseDouble(jNumberField1.getText()) - stock.getGiveMoney().doubleValue();    stock.setOweGiveMoney(new BigDecimal(0));    if(d != 0) {        Object[] options = new String[2];        if (d > 0) {            options[0] = "欠供应商 " + String.format("%.2f", d) +  " 元";            options[1] = "进价优惠 " + String.format("%.2f", d) + " 元";        } else {            options[0] = "暂存在供应商 " + String.format("%.2f", -d) + " 元";            options[1] = "还供应商 " + String.format("%.2f", -d) + " 元";        }                int result = JOptionPane.showOptionDialog(this,                "应付款与实付款不一致!",                "警告",                JOptionPane.YES_NO_OPTION,                JOptionPane.QUESTION_MESSAGE,                null,     //don't use a custom Icon                options,  //the titles of buttons                options[1]); //default button title        if (result == JOptionPane.YES_OPTION) {            stock.setOweGiveMoney(new BigDecimal(d));        }    }        em.persist(stock);    for(StockItem si : stock.getStockItemCollection()) {        si.setStockitemState(1);                // 添加到库存商品单StoreGoods        String queryStr = "select sg from StoreGoods sg where sg.storeName = :store and sg.goodsID = :goods";        Query query = em.createQuery(queryStr);        query.setParameter("store", stock.getStoreName());        query.setParameter("goods",si.getGoodsID());        StoreGoods sg;        try {            sg = (StoreGoods) query.getSingleResult();        } catch(NoResultException nre) {            sg = null;        }        if (sg == null) {            sg = new StoreGoods();            sg.setGoodsID(si.getGoodsID());            sg.setStoreName(stock.getStoreName());            sg.setStoreGoodsQuantity(0);            em.persist(sg);        }        // 增加库存商品单的数量        sg.setStoreGoodsQuantity(sg.getStoreGoodsQuantity() + si.getStockQuantity());                em.persist(si);    }    try{        em.getTransaction().begin();        em.getTransaction().commit();    } catch (Exception e) {        e.printStackTrace();        JOptionPane.showMessageDialog(this, "保存失败!");        em.getTransaction().rollback();        //em.getTransaction().begin();    }    JOptionPane.showMessageDialog(this, "进货单已保存!");    this.dispose();}//GEN-LAST:event_jButton1ActionPerformedprivate void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed        // 新建一种商品    Goods goods = new Goods();    goods = new GoodsEditer(this, goods, true).showDialog();    if (goods == null) return;    StockItem si = new StockItem();    si.setStockID(stock);    si.setGoodsID(goods);    si = new StockItemEditer(this, si, true).showDialog();    if (si.getStockQuantity() == 0) return;    stockItemList.add(si);    em.persist(si.getGoodsID());    updateTable();}//GEN-LAST:event_jButton4ActionPerformedprivate void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed    new SupplierJDialog(this, true).setVisible(true);    supplierJComboBox.setModel(getSupplierComboBoxModel());}//GEN-LAST:event_jButton5ActionPerformedprivate void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed    //if (em.isOpen())em.close();}//GEN-LAST:event_formWindowClosedprivate void jDatePickerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jDatePickerActionPerformed    try {        stock.setStockTime(jDatePicker.getSelectedDate());    } catch (ParseException ex) {        stock.setStockTime(new Date());    }}//GEN-LAST:event_jDatePickerActionPerformedprivate void storeJComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_storeJComboBoxActionPerformed    stock.setStoreName(em.find(Store.class, storeJComboBox.getSelectedItem()));}//GEN-LAST:event_storeJComboBoxActionPerformedprivate void supplierJComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_supplierJComboBoxActionPerformed    stock.setSupplierName(em.find(Supplier.class, supplierJComboBox.getSelectedItem()));}//GEN-LAST:event_supplierJComboBoxActionPerformedprivate void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed    exit();}//GEN-LAST:event_jButton2ActionPerformed/** * 退出 */private void exit() {    if (stock.getStockItemCollection().isEmpty()) {        this.dispose();        return;    }    int result = JOptionPane.showConfirmDialog(this, "进货单没有保存,确定退出?", "退出", JOptionPane.OK_CANCEL_OPTION);    if (result != JOptionPane.YES_OPTION) {        return;    }    this.dispose();}/** * 返回供应商combobox空间的模型 * @return 返回供应商combobox空间的模型 */public DefaultComboBoxModel getSupplierComboBoxModel() {    Query query = em.createQuery("select s.supplierName from Supplier s");    Vector<String> supplierNameVector = new Vector<String>(query.getResultList());    return new DefaultComboBoxModel(supplierNameVector);}/** * 返回仓库combobox空间的模型 * @return 返回仓库combobox空间的模型 */public DefaultComboBoxModel getStoreComboBoxModel() {    Query query = em.createQuery("select s.storeName from Store s");    List<Store> storeList = query.getResultList();    Vector<String> storeNameVector = new Vector<String>(query.getResultList());    return new DefaultComboBoxModel(storeNameVector);}/** * 获取用来显示进货明细的表格模型 * @return 用来显示进货明细的表格模型 */public DefaultTableModel getStockItemTableModel() {    Vector columnName=new Vector();    columnName.add("商品名称");    columnName.add("规格");    columnName.add("颜色");    columnName.add("单位");    columnName.add("单价");    columnName.add("数量");    columnName.add("总金额");    Vector content=new Vector();        total = 0;        //    NumberFormat nf = NumberFormat.getInstance();    //    nf.setMaximumFractionDigits(2);    for( StockItem s : stockItemList) {        Vector row = new Vector();        row.add(s.getGoodsID().getGoodsName());        row.add(s.getGoodsID().getSpec());        row.add(s.getGoodsID().getColor());        row.add(s.getGoodsID().getUnit());        row.add(String.format("%,.2f", s.getCostPrice()));        row.add(String.format("%,.2f", s.getStockQuantity()));        //row.add(nf.format(s.getCostPrice().doubleValue() * s.getStockQuantity()));        double d = s.getCostPrice().doubleValue() * s.getStockQuantity();        row.add(String.format("%,.2f", d));        total += d;        content.add(row);    }    DefaultTableModel stockItemTableModel=new DefaultTableModel(content,columnName){        public boolean isCellEditable(int row, int col) {            return false;        }    };    return stockItemTableModel;}/**  * 更新表格 */private void updateTable(){    jTable1.setModel(getStockItemTableModel());    jNumberField1.setText(String.format("%.2f", total));    jNumberField2.setText(String.format("%.2f", total));    jNumberField2.requestFocus();}    // Variables declaration - do not modify//GEN-BEGIN:variables    private javax.swing.JButton jButton1;    private javax.swing.JButton jButton2;    private javax.swing.JButton jButton3;    private javax.swing.JButton jButton4;    private javax.swing.JButton jButton5;    private javax.swing.JButton jButton6;    private javax.swing.JButton jButton7;    private javax.swing.JButton jButton8;    private com.sunking.swing.JDatePicker jDatePicker;    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 com.sunking.swing.JNumberField jNumberField1;    private com.sunking.swing.JNumberField jNumberField2;    private javax.swing.JPanel jPanel1;    private javax.swing.JPanel jPanel2;    private javax.swing.JScrollPane jScrollPane1;    private javax.swing.JTable jTable1;    private com.studio009.store.entity.Stock stock;    private java.util.List<StockItem> stockItemList;    private javax.swing.JComboBox storeJComboBox;    private javax.swing.JComboBox supplierJComboBox;    // End of variables declaration//GEN-END:variables    private EntityManager em;    private Operator operator;    private double total = 0;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?