Vbnet+billing+software+source+code ✦ Top-Rated

Public Class frmBilling
    Dim cartTable As New DataTable()
    Dim currentGrandTotal As Decimal = 0
Private Sub frmBilling_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' Define cart table columns
    cartTable.Columns.Add("ProductID", GetType(Integer))
    cartTable.Columns.Add("ProductName", GetType(String))
    cartTable.Columns.Add("Quantity", GetType(Integer))
    cartTable.Columns.Add("Price", GetType(Decimal))
    cartTable.Columns.Add("GST_Percent", GetType(Integer))
    cartTable.Columns.Add("GST_Amount", GetType(Decimal))
    cartTable.Columns.Add("Total", GetType(Decimal))
    dgvCart.DataSource = cartTable
End Sub
Private Sub txtProductCode_KeyDown(sender As Object, e As KeyEventArgs) Handles txtProductCode.KeyDown
    If e.KeyCode = Keys.Enter Then
        Dim productCode As String = txtProductCode.Text.Trim()
        Dim query As String = $"SELECT ProductID, ProductName, UnitPrice, StockQuantity, GST_Percent FROM tbl_Products WHERE ProductCode='productCode'"
        Dim dt As DataTable = ExecuteQuery(query)
If dt.Rows.Count > 0 Then
            Dim productID As Integer = dt.Rows(0)("ProductID")
            Dim pName As String = dt.Rows(0)("ProductName")
            Dim price As Decimal = Convert.ToDecimal(dt.Rows(0)("UnitPrice"))
            Dim gstPercent As Integer = Convert.ToInt32(dt.Rows(0)("GST_Percent"))
' Check if already in cart
            Dim existingRow() As DataRow = cartTable.Select($"ProductID = productID")
            If existingRow.Length > 0 Then
                existingRow(0)("Quantity") += 1
                Dim qty As Integer = Convert.ToInt32(existingRow(0)("Quantity"))
                Dim totalBeforeGST As Decimal = qty * price
                Dim gstAmt As Decimal = totalBeforeGST * (gstPercent / 100)
                existingRow(0)("GST_Amount") = gstAmt
                existingRow(0)("Total") = totalBeforeGST + gstAmt
            Else
                Dim gstAmt As Decimal = price * (gstPercent / 100)
                Dim totalWithGST As Decimal = price + gstAmt
                cartTable.Rows.Add(productID, pName, 1, price, gstPercent, gstAmt, totalWithGST)
            End If
            CalculateTotals()
            txtProductCode.Clear()
            txtProductCode.Focus()
        Else
            MessageBox.Show("Product not found!")
        End If
    End If
End Sub
Private Sub CalculateTotals()
    Dim subTotal As Decimal = 0
    Dim totalGST As Decimal = 0
    For Each row As DataRow In cartTable.Rows
        subTotal += Convert.ToDecimal(row("Price")) * Convert.ToInt32(row("Quantity"))
        totalGST += Convert.ToDecimal(row("GST_Amount"))
    Next
    Dim grandTotal As Decimal = subTotal + totalGST
    lblSubTotal.Text = subTotal.ToString("N2")
    lblGST.Text = totalGST.ToString("N2")
    lblGrandTotal.Text = grandTotal.ToString("N2")
    currentGrandTotal = grandTotal
End Sub
Private Sub btnGenerateInvoice_Click(sender As Object, e As EventArgs) Handles btnGenerateInvoice.Click
    If cartTable.Rows.Count = 0 Then
        MessageBox.Show("Cart is empty!")
        Return
    End If
Dim invoiceNo As String = "INV-" & DateTime.Now.ToString("yyyyMMddHHmmss")
    Dim customerID As Integer = GetOrCreateCustomer(txtCustomerMobile.Text) ' Function to fetch/add customer
    Dim userId As Integer = CurrentUserID ' Assume global login user ID
' Insert into tbl_Invoices
    Dim insertInvoice As String = $"INSERT INTO tbl_Invoices (InvoiceNo, CustomerID, SubTotal, GST_Amount, GrandTotal, UserID) VALUES ('invoiceNo', customerID, lblSubTotal.Text, lblGST.Text, lblGrandTotal.Text, userId)"
    ExecuteNonQuery(insertInvoice)
' Insert into tbl_InvoiceDetails
    For Each row As DataRow In cartTable.Rows
        Dim productID As Integer = Convert.ToInt32(row("ProductID"))
        Dim qty As Integer = Convert.ToInt32(row("Quantity"))
        Dim price As Decimal = Convert.ToDecimal(row("Price"))
        Dim gstAmt As Decimal = Convert.ToDecimal(row("GST_Amount"))
        Dim total As Decimal = Convert.ToDecimal(row("Total"))
Dim detailQuery As String = $"INSERT INTO tbl_InvoiceDetails (InvoiceNo, ProductID, Quantity, Price, GST_Amount, Total) VALUES ('invoiceNo', productID, qty, price, gstAmt, total)"
        ExecuteNonQuery(detailQuery)
' Reduce stock
        Dim updateStock As String = $"UPDATE tbl_Products SET StockQuantity = StockQuantity - qty WHERE ProductID = productID"
        ExecuteNonQuery(updateStock)
    Next
MessageBox.Show($"Invoice Generated: invoiceNo")
    PrintInvoice(invoiceNo)
    ' Clear cart and reset
    cartTable.Clear()
    CalculateTotals()
End Sub

End Class


The UI is built using Windows Forms.

' Example: Calculating total on DataGridView Cell End Edit
Private Sub dgvCart_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles dgvCart.CellEndEdit
    If e.ColumnIndex = dgvCart.Columns("Quantity").Index Then
        Dim qty As Integer = CInt(dgvCart.Rows(e.RowIndex).Cells("Quantity").Value)
        Dim price As Decimal = CDec(dgvCart.Rows(e.RowIndex).Cells("Price").Value)
' Calculate Line Total
        dgvCart.Rows(e.RowIndex).Cells("LineTotal").Value = qty * price
' Update Grand Total Label
        CalculateGrandTotal()
    End If
End Sub
Private Sub CalculateGrandTotal()
    Dim total As Decimal = 0
    For Each row As DataGridViewRow In dgvCart.Rows
        total += CDec(row.Cells("LineTotal").Value)
    Next
    lblGrandTotal.Text = total.ToString("C2") ' Format as Currency
End Sub
CREATE TABLE tbl_Invoices (
    InvoiceNo NVARCHAR(20) PRIMARY KEY,
    InvoiceDate DATETIME DEFAULT GETDATE(),
    CustomerID INT FOREIGN KEY REFERENCES tbl_Customers(CustomerID),
    SubTotal DECIMAL(18,2),
    GST_Amount DECIMAL(18,2),
    GrandTotal DECIMAL(18,2),
    UserID INT FOREIGN KEY REFERENCES tbl_Users(UserID)
);
CREATE TABLE tbl_Customers (
    CustomerID INT PRIMARY KEY IDENTITY(1,1),
    CustomerName NVARCHAR(100),
    Phone NVARCHAR(15),
    Email NVARCHAR(100)
);