In this section, we will build the Sale window.
Create a window called JDialogSale, and add two Buttons, four Labels, four TextFields and a Table. Also change the Window property of the window to Dialog.
Design the window like this:
Change the Text of Labels for "Description", "Price" , "Customer ID" and "Product ID". Change the Name of the TextFields to jTextFieldDescription, jTextFieldPrice, jTextFieldCustomerID and jTextFieldProductID, and the Table for jTableItems.
Change Text Buttons for the "New" and "Save". Change the Name of the Buttons for jButtonNew and jButtonSave.
Also changes the Editable property of jTextFieldDescription and jTextFieldPrice for false and the Column property to description and price.
In the Titles and Columns properties of the JTable Items, enter:
sale_detail.product_id
product.description
sale_detail.amount
sale_detail.price
Add two Procedures and name: fillTable and addRow.
Add a variable and name: Sales. Change Column property to sale_id.
In jButtonNew, add the following functions OnClick event:
Function: selectRecord
Parameters:
sale_header
ifnull(max(id), 0)+1 as sale_id
order by id
Function: insertRecord
Parameters:
sale_header
id, sale_date, sale_time, customer_id
'"+getsaleID()+"', current_date(), current_time(), '"+jTextFieldCustomerID.getText()+"'
Function: runProcedure
Parameters:
fillTable
In OnExecute Event Procedure fillTable, add a function to fillTable with the following parameters:
jTableItems
sale_detail, product //Selecting two tables
sale_detail.product_id, product.description, sale_detail.amount, sale_detail.price //Columns of diferent tables
where product.id=sale_detail.product_id and sale_detail.sale_header_id='"+getsaleID()+"' //(Do not break line)
In OnExecute Event Procedure addRow, add the functions:
Function: javaCommand
Parameters:
if(getsaleID().equals("")){
javax.swing.JOptionPane.showMessageDialog(getJDialogSale(), "Start a sale!");
return;
}
Function: verifyRecord
Parameters:
product
id
where id='"+jTextFieldProductID.getText()+"'
Select a valid product!
==
Function: selectRecord
Parameters:
product
id, description, price
where id='"+jTextFieldProductID.getText()+"'
Function: insertRecord
Parameters:
sale_detail
sale_header_id, product_id, price, amount
'"+getsaleID()+"', '"+jTextFieldProductID.getText()+"', '"+jTextFieldPrice.getText()+"', 1
Function: runProcedure
Parameters:
fillTable
Also add the function to alterRecord with the following parameters:
product
amount=amount-1
where id='"+jTextFieldProductID.getText()+"'
This function will decrease the amount that leaves the stock of the product.
In OnKeyReleased event jTextFieldProductID complete the key with the value Enter and add a runProcedure function with parameter:
addRow
In the OnClick event of the jButtonSave, add the functions:
Function: mudarValor
Parameters:
getJDialogSale()
saleID
""
Function: runProcedure
Parameters:
fillTable
Go back to the main window and in the onclick event of the Sale menu, add the openWindow function with parameter:
JDialogSale
Save and Run your application, clicking in the buttons on Toolbar or pressing CTRL + S and F9.