diff --git a/src/DataBaseInterface/Handler.java b/src/DataBaseInterface/Handler.java index a6d3f56..ebd905c 100644 --- a/src/DataBaseInterface/Handler.java +++ b/src/DataBaseInterface/Handler.java @@ -4,13 +4,18 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import javax.persistence.EntityManager; + import org.hibernate.HibernateException; +import org.hibernate.Query; +import org.hibernate.SQLQuery; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; +import org.hibernate.Query; import org.hibernate.SQLQuery; import Util.*; @@ -465,7 +470,6 @@ public List get_sms_queue() { Session session = factory.openSession(); List data = null; try{ - data = session.createQuery("FROM SMSQueue WHERE sent = 'f'").list(); }catch (HibernateException e) { e.printStackTrace(); @@ -476,6 +480,44 @@ public List get_sms_queue() { return data; } + public List get_bill_details(){ + + Session session = factory.openSession(); + + List result = null; + + try{ + // Query have to be optimize (instead of get all columns, should fetch required columns) + //fetching particular columns throws error. + + SQLQuery query = session.createSQLQuery( + "SELECT *" + + " FROM service_detail as sd" + + " JOIN service_bill as sb ON sd.service_bill_id = sb.id" + + " JOIN service_particulars as sp ON sd.service_particular_id = sp.id" + + " JOIN customer_vehicle as veh ON sb.vehicle_id = veh.id" + + " JOIN customer as cus ON veh.customer_id = cus.id order by sb.id" + ); + + query.addEntity(ServiceDetail.class); + query.addEntity(ServiceBill.class); + query.addEntity(ServiceParticulars.class); + query.addEntity(CustomerVehicle.class); + query.addEntity(Customer.class); + + result = query.list(); + + }catch (HibernateException e) { + + e.printStackTrace(); + Logger.log.severe(e.toString()); + }finally { + session.close(); + } + return result; + } + + public List get_single_bill_detail(int bill_id){ Session session = factory.openSession(); @@ -497,10 +539,11 @@ public List get_single_bill_detail(int bill_id){ query.addEntity(ServiceBill.class); query.addEntity(CustomerVehicle.class); query.addEntity(Customer.class); - result = query.list(); + }catch (HibernateException e) { + e.printStackTrace(); Logger.log.severe(e.toString()); }finally { @@ -508,4 +551,21 @@ public List get_single_bill_detail(int bill_id){ } return result; } + + public ServiceBill get_service_bill(int vehicle_id, String date_str ) { + System.out.println(vehicle_id + " " + date_str); + Session session = factory.openSession(); + ServiceBill obj_service_bill = null; + int epoch = Time.get_epoch(date_str, "dd/MM/yyyy HH:mm:ss"); + try{ + obj_service_bill = (ServiceBill)session.createQuery("FROM ServiceBill WHERE vehicle_id = '" + vehicle_id + "' and created_epoch = " + epoch).uniqueResult(); + }catch (HibernateException e) { + e.printStackTrace(); + Logger.log.severe(e.toString()); + }finally { + session.close(); + } + + return obj_service_bill; + } } diff --git a/src/DataBaseInterface/ServiceDetail.java b/src/DataBaseInterface/ServiceDetail.java index 2dddf78..451bd10 100644 --- a/src/DataBaseInterface/ServiceDetail.java +++ b/src/DataBaseInterface/ServiceDetail.java @@ -9,8 +9,8 @@ public class ServiceDetail { public ServiceDetail(){} - public ServiceDetail(Integer service_particulars_id, Integer service_bill_id,Integer quantity, double amount){ - this.service_particular_id = service_particulars_id; + public ServiceDetail(Integer service_particular_id, Integer service_bill_id,Integer quantity, double amount){ + this.service_particular_id = service_particular_id; this.service_bill_id = service_bill_id; this.quantity = quantity; this.amount = amount; diff --git a/src/UserInterface/MainFrame.java b/src/UserInterface/MainFrame.java index bf539f3..306f31e 100644 --- a/src/UserInterface/MainFrame.java +++ b/src/UserInterface/MainFrame.java @@ -31,15 +31,12 @@ public class MainFrame extends JFrame { static public JPanel pnl_home; static MainFrame frame; - /** - * Launch the application. - */ public static void main(String[] args) { - ProgressBar.showProgress("Loading Components....", 10); - Handler.getInstance(); - ProgressBar.showProgress("Launching Application..", ProgressBar.MAXIMUM_VALUE); - + ProgressBar.showProgress("Loading Components....", 10); + Handler.getInstance(); + ProgressBar.showProgress("Launching Application..", ProgressBar.MAXIMUM_VALUE); + EventQueue.invokeLater(new Runnable() { public void run() { try { @@ -144,7 +141,7 @@ public void actionPerformed(ActionEvent e) { // Title Lable // TODO have to add icon - JLabel lbl_title = new JLabel("ABHI TRAVELS AND WHEELS"); + JLabel lbl_title = new JLabel("ABHI WHEELS"); lbl_title.setForeground(new Color(210, 105, 30)); lbl_title.setFont(new Font("URW Bookman L", Font.ITALIC, 32)); lbl_title.setHorizontalAlignment(SwingConstants.CENTER); @@ -247,6 +244,12 @@ public void actionPerformed(ActionEvent e) { pnl_sub_wheel.add(btn_view_wheel); + btn_view_wheel.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + pnl_wheels_view obj_wheels_view = new pnl_wheels_view(); + setContentPane(obj_wheels_view); + } + }); // update wheel window navigation button JButton btn_update_wheel = new JButton("Edit Wheel Bill"); btn_update_wheel.setFont(btn_font); diff --git a/src/UserInterface/pnl_wheels_add.java b/src/UserInterface/pnl_wheels_add.java index 91c5460..df17aeb 100644 --- a/src/UserInterface/pnl_wheels_add.java +++ b/src/UserInterface/pnl_wheels_add.java @@ -19,29 +19,29 @@ import javax.swing.JTextField; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; - import javax.swing.JScrollPane; import javax.swing.JButton; import DataBaseInterface.*; + import javax.swing.ListSelectionModel; + import java.awt.Font; import Util.*; + import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; - import javax.swing.JFormattedTextField; import javax.swing.JTextArea; public class pnl_wheels_add extends JPanel implements TableModelListener { - /** - * - */ + private static final long serialVersionUID = 1L; + private JTextField txt_mobile_no, txt_customer_name, txt_vehicle_make, txt_date, txt_time; private JTextArea txt_comment; private JTable tbl_particulars; @@ -66,6 +66,8 @@ public pnl_wheels_add() { add(pnl_customer_details); pnl_customer_details.setLayout(null); + + int now_epoch = Time.now(); JLabel lbl_mobile_no = new JLabel("Mobile No"); @@ -201,9 +203,7 @@ public void actionPerformed(ActionEvent e) { o[1] = sp.getService_name(); boolean is_multi = sp.isIs_multiple(); - if(is_multi){ - o[2] = new Integer(1); - } else { + if(!is_multi){ o[2] = "NA"; } @@ -253,7 +253,7 @@ public void actionPerformed(ActionEvent e) { txt_comment.setBounds(247, 476, 393, 49); add(txt_comment); - JButton btnTestBill = new JButton("Test Bill"); + /*JButton btnTestBill = new JButton("Test Bill"); btnTestBill.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // Wheel bill print preview and print @@ -262,7 +262,7 @@ public void actionPerformed(ActionEvent e) { } }); btnTestBill.setBounds(733, 570, 117, 25); - add(btnTestBill); + add(btnTestBill);*/ txt_vehicle_no.addFocusListener(new FocusAdapter() { @Override @@ -467,7 +467,7 @@ private boolean validate_details() { c_mobile = txt_mobile_no.getText(); date = txt_date.getText(); time = txt_time.getText(); - + if(!v_num.matches(Formatter.VEHICLE_PATTERN)){ alert("Vehicle number is invalid, Please correct it"); txt_vehicle_no.requestFocus(); @@ -526,7 +526,7 @@ public void tableChanged(TableModelEvent e) { int column = e.getColumn(); Pattern pattern = Pattern.compile("(^\\d+\\.?\\d*$)"); - // validation for accept only numbers + String current_cell_str = tbl_particulars.getValueAt(row, column).toString(); if((! current_cell_str.trim().isEmpty()) && (!pattern.matcher(current_cell_str).find())){ if(column == 3 ) @@ -542,6 +542,7 @@ else if(column == 2) // parsing null value will throw NumberFormatException // To avoid that exception amount stored in string String str_amount = tbl_particulars.getValueAt(row, 3).toString(); + if(! str_amount.trim().isEmpty()){ @@ -551,8 +552,14 @@ else if(column == 2) if(is_multiple[row].toString() == "false") { total = amount; } else { - int quantity = Integer.parseInt(tbl_particulars.getValueAt(row, 2).toString()); - total = amount * quantity; + + int quan = 1; + if( tbl_particulars.getValueAt(row, 2) != null) + quan = Integer.parseInt(tbl_particulars.getValueAt(row, 2).toString()); + else + tbl_particulars.setValueAt(new Integer(1), row, 2); + + total = amount * quan; } tbl_particulars.setValueAt(new Double(total), row, 4); diff --git a/src/UserInterface/pnl_wheels_view.java b/src/UserInterface/pnl_wheels_view.java new file mode 100644 index 0000000..d057d14 --- /dev/null +++ b/src/UserInterface/pnl_wheels_view.java @@ -0,0 +1,475 @@ +package UserInterface; + +import java.awt.Color; +import java.awt.Component; +import java.awt.Dimension; +import java.awt.Font; +import java.awt.Toolkit; +import java.awt.event.MouseEvent; +import java.util.ArrayList; +import java.util.List; + +import javax.swing.BoxLayout; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTable; +import javax.swing.JTextArea; +import javax.swing.JTextField; +import javax.swing.RowFilter; +import javax.swing.border.MatteBorder; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; +import javax.swing.table.AbstractTableModel; +import javax.swing.table.DefaultTableModel; +import javax.swing.table.JTableHeader; +import javax.swing.table.TableCellRenderer; +import javax.swing.table.TableModel; +import javax.swing.table.TableRowSorter; + +import DataBaseInterface.Customer; +import DataBaseInterface.CustomerVehicle; +import DataBaseInterface.Handler; +import DataBaseInterface.ServiceBill; +import DataBaseInterface.ServiceDetail; +import DataBaseInterface.ServiceParticulars; + +import javax.swing.JComboBox; +import javax.swing.border.LineBorder; + +import Util.Time; + +public class pnl_wheels_view extends JPanel { + + private static final long serialVersionUID = 1L; + + private JTable tbl_service_details; + + private TableRowSorter rowSorter ; + + private JTextField txt_search_word,txt_search_text; + private DefaultTableModel mdl_service_details; + JComboBox cmb_search_catagory; + private Handler dbh = Handler.getInstance(); + int window_width,window_height; + + private ArrayList vehicle_id = new ArrayList(); + + public pnl_wheels_view() { + setBackground(Color.WHITE); + Toolkit tool=Toolkit.getDefaultToolkit(); + Dimension dim=tool.getScreenSize(); + setSize(dim.width,dim.height - 50); + + window_width = this.getWidth(); + window_height = this.getHeight(); + + //setSize(1190,713); + setLayout(null); + + tbl_service_details = new JTable(){ + + private static final long serialVersionUID = 1L; + + protected JTableHeader createDefaultTableHeader() { + return new JTableHeader(columnModel) { + + private static final long serialVersionUID = 1L; + + public String getToolTipText(MouseEvent e) { + String tip = null; + java.awt.Point p = e.getPoint(); + int index = columnModel.getColumnIndexAtX(p.x); + if(index == 8){ + tip = "Enable checkbox if Free checkup is Completed"; + } + return tip; + + } + }; + } + }; + tbl_service_details.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); + tbl_service_details.setRowSelectionAllowed(false); + mdl_service_details = new DefaultTableModel(){ + + private static final long serialVersionUID = 1L; + + public boolean isCellEditable(int row, int col) { + if (col == 8) + return true; + else + return false; + } + + public Class getColumnClass(int c) { + return getValueAt(0, c).getClass(); + } + }; + + String[] columns = { + "S.NO", + "Customer Name", + "Mobile No", + "Vehicle No", + "Bill Date", + "Remarks", + "Particulars", + "Total", + "FCD" + }; + + mdl_service_details.setColumnIdentifiers(columns); + + load_service_bill_details(); + + tbl_service_details.setModel(mdl_service_details); + tbl_service_details.setFont(new Font("Arial", Font.PLAIN, 14)); + tbl_service_details.setForeground(Color.DARK_GRAY); + tbl_service_details.setGridColor(Color.BLUE); + tbl_service_details.setBackground(Color.WHITE); + tbl_service_details.setRowMargin(5); + tbl_service_details.setRowHeight(50); + + tbl_service_details.getColumn("Particulars").setCellRenderer(new SubTableRenderer()); + tbl_service_details.getColumn("Customer Name").setCellRenderer(new WrapCellRenderer()); + tbl_service_details.getColumn("Mobile No").setCellRenderer(new WrapCellRenderer()); + tbl_service_details.getColumn("Vehicle No").setCellRenderer(new WrapCellRenderer()); + tbl_service_details.getColumn("Bill Date").setCellRenderer(new WrapCellRenderer()); + tbl_service_details.getColumn("Remarks").setCellRenderer(new WrapCellRenderer()); + + + + tbl_service_details.addMouseListener(new java.awt.event.MouseAdapter() { + @Override + public void mouseClicked(java.awt.event.MouseEvent evt) { + int col = tbl_service_details.columnAtPoint(evt.getPoint()); + ServiceBill obj_service_bill = null; + int row = 0; + if(col == 0 || col == 8){ + row = tbl_service_details.rowAtPoint(evt.getPoint()); + String date_str = tbl_service_details.getValueAt(row, 4).toString(); + + obj_service_bill = dbh.get_service_bill(vehicle_id.get(row), date_str); + } + + if(col == 0){ + + wheel_bill_print obj_wbp = new wheel_bill_print(obj_service_bill.getId()); + + obj_wbp.setVisible(true); + + } else if(col == 8) { + + obj_service_bill.setFree_checkup_completed((boolean) tbl_service_details.getValueAt(row, col)); + dbh.update(obj_service_bill); + + } + } + }); + + JTableHeader obj_jtable_header = tbl_service_details.getTableHeader(); + obj_jtable_header.setBackground(Color.BLUE); + obj_jtable_header.setForeground(Color.WHITE); + obj_jtable_header.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 12)); + + JScrollPane jsp_service_details =new JScrollPane(tbl_service_details); + jsp_service_details.setForeground(new Color(102, 153, 255)); + jsp_service_details.setBorder(new MatteBorder(3, 3, 3, 3, (Color) new Color(204, 51, 102))); + jsp_service_details.setBackground(new Color(204, 102, 153)); + jsp_service_details.setBounds(20, 120, window_width - 40 , window_height - 140); + add(jsp_service_details); + + int tbl_width = jsp_service_details.getWidth(); + + + tbl_service_details.getColumn("S.NO").setMaxWidth((tbl_width/100)*4); + tbl_service_details.getColumn("Customer Name").setPreferredWidth((tbl_width/100)*10); + tbl_service_details.getColumn("Mobile No").setWidth((tbl_width/100)*10); + tbl_service_details.getColumn("Vehicle No").setWidth((tbl_width/100)*10); + tbl_service_details.getColumn("Bill Date").setWidth((tbl_width/100)*10); + tbl_service_details.getColumn("Remarks").setPreferredWidth((tbl_width/100)*16); + tbl_service_details.getColumn("Total").setMaxWidth((tbl_width/100)*10); + tbl_service_details.getColumn("FCD").setMaxWidth((tbl_width/100)*4); + tbl_service_details.getColumn("Particulars").setPreferredWidth((tbl_width/100)*30); + + rowSorter = new TableRowSorter<>(tbl_service_details.getModel()); + tbl_service_details.setRowSorter(rowSorter); + + JPanel pnl_search = new JPanel(); + pnl_search.setBackground(new Color(204, 51, 153)); + pnl_search.setBorder(new MatteBorder(3, 3, 3, 3, (Color) new Color(204, 51, 153))); + pnl_search.setBounds(20, 95, tbl_width, 30); + add(pnl_search); + pnl_search.setLayout(new BoxLayout(pnl_search, BoxLayout.X_AXIS)); + + JLabel lbl_search_word = new JLabel("Search Text "); + lbl_search_word.setBackground(new Color(204, 153, 0)); + lbl_search_word.setAlignmentX(Component.CENTER_ALIGNMENT); + pnl_search.add(lbl_search_word); + lbl_search_word.setForeground(new Color(255, 255, 153)); + lbl_search_word.setFont(new Font("Bitstream Charter", Font.BOLD | Font.ITALIC, 18)); + + txt_search_word = new JTextField(); + txt_search_word.setBackground(new Color(255, 255, 255)); + pnl_search.add(txt_search_word); + txt_search_word.setColumns(10); + txt_search_word.getDocument().addDocumentListener(new DocumentListener(){ + @Override + public void insertUpdate(DocumentEvent e) { + + String text = txt_search_word.getText(); + + if (text.trim().length() == 0) { + rowSorter.setRowFilter(null); + } else { + rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text)); + } + } + + @Override + public void removeUpdate(DocumentEvent e) { + + String text = txt_search_word.getText(); + + if (text.trim().length() == 0) { + rowSorter.setRowFilter(null); + } else { + rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text)); + } + } + + @Override + public void changedUpdate(DocumentEvent e) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + }); + + cmb_search_catagory = new JComboBox(); + cmb_search_catagory.setBounds((window_width /2) - 245, 45, 225, 24); + cmb_search_catagory.addItem("Vehicle Number"); + cmb_search_catagory.addItem("Customer Name"); + add(cmb_search_catagory); + + txt_search_text = new JTextField(); + txt_search_text.setBorder(new LineBorder(new Color(0, 0, 0))); + txt_search_text.setBounds((window_width/2), 45, 225, 24); + add(txt_search_text); + txt_search_text.getDocument().addDocumentListener(new DocumentListener() { + @Override + public void insertUpdate(DocumentEvent e) { + updateFilter(); + } + + @Override + public void removeUpdate(DocumentEvent e) { + updateFilter(); + } + + @Override + public void changedUpdate(DocumentEvent e) { + updateFilter(); + } + }); + + } + + public void load_service_bill_details(){ + + int serial_number = 0, quantity; + + String customer_name = "", service_name ; + + Object[] row = new Object[9]; + + ArrayList service_particulars = new ArrayList(); + + List bill_details = dbh.get_bill_details(); + + for (Object[] object : bill_details){ + + ServiceDetail obj_sd = (ServiceDetail) object[0]; + ServiceBill obj_sb = (ServiceBill) object[1]; + ServiceParticulars obj_sp = (ServiceParticulars) object[2]; + CustomerVehicle obj_cveh = (CustomerVehicle) object[3]; + Customer obj_cus = (Customer) object[4]; + + + if(customer_name.compareTo(obj_cus.getName()) == 0) { + + service_name = obj_sp.getService_name(); + quantity = obj_sd.getQuantity(); + double amount = obj_sd.getAmount(); + + service_particulars.add(service_name); + service_particulars.add(quantity + " * " + amount + " = " +(quantity * amount)); + + } else { + + //Don't execute for first row + if (customer_name != "") { + + row[6] = get_string_array(service_particulars); + service_particulars.clear(); + mdl_service_details.addRow(row); + row = new Object[9]; + } + + customer_name = obj_cus.getName(); + + row[0] = ++serial_number; + row[1] = customer_name; + row[2] = obj_cus.getMobile(); + row[3] = obj_cveh.getVehicle_number() + "\n" + obj_cveh.getVehicle_make(); + vehicle_id.add(obj_sb.getVehicle_id()); + row[4] = Time.get_date(obj_sb.getCreated_epoch(), "dd/MM/yyyy HH:mm:ss a"); + row[5] = obj_sb.getComments(); + + service_name = obj_sp.getService_name(); + quantity = obj_sd.getQuantity(); + double amount = obj_sd.getAmount(); + + service_particulars.add(service_name); + service_particulars.add(quantity + " * " + amount + " = " +(quantity * amount)); + + row[7] = obj_sb.getTotal_amount(); + row[8] = new Boolean(obj_sb.isFree_checkup_completed()); + + } + + } + + //adding last record + row[6] = get_string_array(service_particulars); + mdl_service_details.addRow(row); + } + + //This method used to convert the ArrayList to String[][] + public String[][] get_string_array(ArrayList< String> al ) { + + String[][] srt_array = new String[(al.size()/2)][2]; + + int ind = 0; + + for (int i=0;i < al.size(); i++) + { + + srt_array[ind][0]= al.get(i); + + i++; + srt_array[ind][1]= al.get(i); + + if (i != 0 && i % 2 != 0) + ind++; + } + + return srt_array; + + } + + //This method is used to filter table by column + protected void updateFilter() { + Object selected = cmb_search_catagory.getSelectedItem(); + TableRowSorter sorter = (TableRowSorter) tbl_service_details.getRowSorter(); + String text = "(?i)" + txt_search_text.getText(); + if ("Nothing".equals(selected)) { + sorter.setRowFilter(null); + } else { + int col = -1; + if ("Customer Name".equals(selected)) { + col = 1; + } else if ("Vehicle Number".equals(selected)) { + col = 3; + } + sorter.setRowFilter(RowFilter.regexFilter(text, col)); + } + } +}; + +class SubTableRenderer extends JTable implements TableCellRenderer { + + + private static final long serialVersionUID = 1L; + //TableCellRenderer jTableCellRenderer = new TableCellRenderer() { + /* These are necessary variables to store the row's height */ + + private int tbl_row_height = -1; + private int curr_height = -1; + /* Magic Happens */ + public Component getTableCellRendererComponent(JTable table, + Object value, boolean isSelected, boolean hasFocus, + int row, int column) { + /* If what we're displaying isn't an array of values we +return the normal renderer*/ + + if(value == null || (!value.getClass().isArray())){ + return table.getDefaultRenderer( + value.getClass()).getTableCellRendererComponent( + table, value, isSelected, hasFocus,row, column); + } + else{ + final Object[][] passed = (Object[][])value; + /* We calculate the row's height to display data + * THis is not complete and has some bugs that + * will be analyzed in further articles */ + if(tbl_row_height == -1){ + tbl_row_height = table.getRowHeight(); + } + if(curr_height != tbl_row_height){ + curr_height = (passed.length * passed.length + 20 ) + tbl_row_height; + + table.setRowHeight(row,curr_height); + } + + /* We create the table that will hold the multivalue + *fields and that will be embedded in the main table */ + + JTable tbl = new JTable(new AbstractTableModel() { + + private static final long serialVersionUID = 1L; + + public int getColumnCount() { + return 2; + } + public int getRowCount() { + return passed.length; + } + public Object getValueAt(int rowIndex, int columnIndex) { + + return passed[rowIndex][columnIndex]; + } + public boolean isCellEditable(int row, int col){ return true; } + }); + + tbl.setShowGrid(false); + + return tbl; + + } + } +} + + +class WrapCellRenderer extends JTextArea implements TableCellRenderer { + + private static final long serialVersionUID = 1L; + + public WrapCellRenderer() { + setLineWrap(true); + setWrapStyleWord(true); + } + + public Component getTableCellRendererComponent(JTable table, Object + value, boolean isSelected, boolean hasFocus, int row, int column) { + setText(value.toString());//or something in value, like value.getNote()... + setSize(table.getColumnModel().getColumn(column).getWidth(), + getPreferredSize().height); + if (table.getRowHeight(row) != getPreferredSize().height) { + table.setRowHeight(row, getPreferredSize().height); + } + return this; + } +} + +