0% found this document useful (0 votes)
63 views9 pages

Bills PHP

Uploaded by

saran252060
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views9 pages

Bills PHP

Uploaded by

saran252060
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

bills.

php

<!doctype html>
<html lang="en">
<div class="container-fluid">
<!-- Row 1 -->
<div class="row">
<div class="col-lg-13 d-flex align-items-strech">
<title>bills</title>

<head>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-
Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"
integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
crossorigin="anonymous"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"

integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
crossorigin="anonymous"></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"

integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
crossorigin="anonymous"></script>
</head>

<body>
<div class="modal" tabindex="-1" role="dialog" id='modal_frm'>
<div class=".modal-{xl}" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">bills</h5>
<button type="button" class="close" data-dismiss="modal" aria-
label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>

<div class="modal-body">
<form id='frm'>
<input type='hidden' name='action' id='action' value='Insert'>
<input type='hidden' name='bill_id' id='uid' value='0'>
<form>
<div class='row'>
<div class='col-md-4'>
<h5 class='text-success'>BILL</h5>
<div class='form-group'>
<label>bill_date</label>
<input type='date' name='bill_date' id='bill_date'
required class='form-control'>
</div>
<div class='form-group'>
<label>bill_number</label>
<input type='text' name='bill_number' id='bill_number'
required class='form-control'>
</div>
</div>
<div class='col-md-8'>
<label for="customer_id">Choose customer_id:</label>
<select name="customer_id" id="customer_id" required
class='form-control'>
<option value="1">sam</option>
<option value="2">rahul</option>
<option value="3">shalu</option>
<option value="4">janu</option>
<option value="5">asha</option>
</select>
<div class='form-group'>
<label>bill_total_amount</label>
<input type='text' name='bill_total_amount'
id='bill_total_amount' required
class='form-control'>
</div>
</div>
</div>
<div class='row'>
<div class='col-md-12'>
<h5 class='text-success'>Bill Items</h5>
<table class='table table-bordered'>
<thead>
<tr>
<th>product_id</th>
<th>quantity</th>
<th>price</th>
<th>tax_percentage</th>
<th>tax_price</th>
<th>total_amount</th>
<th>Action</th>
</thead>
<tbody id='tbody'>
<tr>
<td><select name="product_id[]" id="product_id"
required class='form-control product_id'>
<option value="3">pen</option>
<option value="4">dairy milk</option>
<option value="54">chocolate</option>
</select></td>
<td><input type='text' required
name='bill_item_quantity[]'
class='form-control bill_item_quantity'></td>
<td><input type='text' required
name='bill_item_price[]'
class='form-control bill_item_price'></td>
<td><input type='text' required
name='bill_item_tax_percentage[]'
class='form-control
bill_item_tax_percentage[]'></td>
<td><input type='text' required
name='bill_item_tax_price[]'
class='form-control bill_item_tax_price'>
</td>
<td><input type='text' required
name='bill_item_total_amount[]'
class='form-control bill_item_total_amount'>
</td>
<td><input type='button' value='x' class='btn btn-
danger btn-sm btn-row-remove'>
</td>
</tr>
</tbody>
<tr>
<td><input type='button' value='+ Add Row' class='btn
btn-primary btn-sm' id='btn-add-row'>
</td>
<td colspan='2' class='text-right'>Total</td>
<td><input type='text' name='grand_total'
id='grand_total' class='form-control' required>
</td>
</tr>
</tfoot>
</table>
<input type='submit' name='submit' value='Submit'
class='btn btn-success float-right'>
</div>
</div>

</div>
</div>
</div>
</div>
</div>

<div class='container mt-5'>


<p class='text-right'><a href='#' class='btn btn-success' id='add_record'>Add
Record</a></p>
<h3>Bills</h3>
<table class='table table-bordered'>
<thead>
<th>bill_date</th>
<th>bill_no</th>
<th>cus_id</th>
<th>billTM</th>
<th>pro_id</th>
<th>quantity</th>
<th>price</th>
<th>tax_per</th>
<th>tax_price</th>
<th>total_amount</th>
<th>action</th>
<th>print</th>
</thead>
<tbody id='tbody'>
<?php
include "../config/dbconnect.php";

$sql = "SELECT b.*, bi.*


FROM bills AS b
JOIN bill_items AS bi ON b.bill_total_amount =
bi.bill_item_total_amount";

$res = $conn->query($sql);
if ($res) {
while ($row = $res->fetch_assoc()) {
echo "
<tr uid='{$row["bill_id"]}'>
<td>{$row["bill_date"]}</td>
<td>{$row["bill_number"]}</td>
<td>{$row["customer_id"]}</td>
<td>{$row["bill_total_amount"]}</td>
<td>{$row["product_id"]}</td>
<td>{$row["bill_item_quantity"]}</td>
<td>{$row["bill_item_price"]}</td>
<td>{$row["bill_item_tax_percentage"]}</td>
<td>{$row["bill_item_tax_price"]}</td>
<td>{$row["bill_item_total_amount"]}</td>
<td>
<a href='#' class='btn btn-primary edit'>Edit</a>
<a href='#' class='btn btn-danger delete'>Delete</a>
</td>
<td>
<a href='./index/print.php' class='btn btn-success
print'>Print</a>
</td>
</tr>
";
}
}
?>
</tbody>
</table>
</div>
</div>
</div>
<script>
$(document).ready(function () {
var current_row = null;

$("#add_record").click(function () {
$("#modal_frm").modal();
});

$("#frm").submit(function (event) {
event.preventDefault();
$.ajax({
url: "./bills/bills_action.php",
type: "post",
data: $("#frm").serialize(),
beforeSend: function () {
$("#frm").find("input[type='submit']").val('Loading...');
},
success: function (res) {
if (res) {
if ($("#uid").val() == "0") {
$("#tbody").append(res);
} else {
$(current_row).html(res);
}
} else {
alert("Failed Try Again");
}
$("#frm").find("input[type='submit']").val('Submit');
clear_input();
$("#modal_frm").modal('hide');
}
});
});

$("body").on("click", ".edit", function (event) {


event.preventDefault();
current_row = $(this).closest("tr");
$("#modal_frm").modal();
var bill_id = $(this).closest("tr").attr("uid");
var bill_date = $(this).closest("tr").find("td:eq(0)").text();
var bill_number = $(this).closest("tr").find("td:eq(1)").text();
var customer_id = $(this).closest("tr").find("td:eq(2)").text();
var bill_total_amount = $(this).closest("tr").find("td:eq(3)").text();
jQuery.ajax({
type: "GET",
url: "http://localhost/lessons/admin_panel/ajax/bills.php",
data: "bill_id=" + bill_id,
dataType: "json",
success: function (data) {
console.log('Data', data);
$("#action").val("Update");
$("#uid").val(data['bill_id']); // Error occurs here
$("#bill_date").val(data['bill_date']);
$("#bill_number").val(data['bill_number']);
$("#customer_id").val(data['customer_id']);
$("#bill_total_amount").val(data['bill_total_amount']);
},

error: function (request, status, error) {


alertError('An error occurred while processing the request');
},
complete: function(request, status) {
},
});
});

$("body").on("click", ".delete", function (event) {


event.preventDefault();
var bill_id = $(this).closest("tr").attr("uid");
var cls = $(this);
if (confirm("Are You Sure")) {
$.ajax({
url: "./bills/bills_action.php",
type: "post",
data: { bill_id: bill_id, action: 'Delete' },
beforeSend: function () {
$(cls).text("Loading...");
},
success: function (res) {
if (res) {
$(cls).closest("tr").remove();
} else {
alert("Failed TryAgain");
$(cls).text("Try Again");
}
}
});
}
});

function clear_input() {
$("#frm").find(".form-control").val("");
$("#action").val("Insert");
$("#uid").val("0");
}

// Function to calculate the row total


function calculateRowTotal(row) {
var price = parseFloat(row.find('.bill_item_price').val()) || 0;
var quantity = parseFloat(row.find('.bill_item_quantity').val()) || 0;
var total = price * quantity;
row.find('.bill_item_total_amount').val(total.toFixed(2)); // Display total
with two decimal places
return total;
}

// Function to update the grand total


function updateGrandTotal() {
var grandTotal = 0;
$('.bill_item_total_amount').each(function () {
grandTotal += parseFloat($(this).val()) || 0;
});
$('#grand_total').val(grandTotal.toFixed(2)); // Display grand total with two
decimal places
}

// Initialize event handling for existing rows


$('.bill_item_price, .bill_item_quantity').on('keyup', function () {
var row = $(this).closest('tr');
calculateRowTotal(row);
updateGrandTotal();
});

// Handle adding a new row


$('#btn-add-row').click(function () {
var newRow = `
<tr>
<td>
<select name='product_id[]' required class='form-control
product_id'>
<option value='52'>pen</option>
<option value='53'>dairy milk</option>
<option value='54'>chocolate</option>
</select>
</td>
<td><input type='text' required name='bill_item_quantity[]'
class='form-control bill_item_quantity'></td>
<td><input type='text' required name='bill_item_price[]' class='form-
control bill_item_price'></td>
<td><input type='text' required name='bill_item_tax_percentage[]'
class='form-control bill_item_tax_percentage'></td>
<td><input type='text' required name='bill_item_tax_price[]'
class='form-control bill_item_tax_price'></td>
<td><input type='text' required name='bill_item_total_amount[]'
class='form-control bill_item_total_amount'></td>
<td><input type='button' value='x' class='btn btn-danger btn-sm btn-
row-remove'></td>
</tr>
`;
$('#tbody').append(newRow);
});

// Handle removing a row


$('body').on('click', '.btn-row-remove', function () {
if (confirm('Are You Sure?')) {
$(this).closest('tr').remove();
updateGrandTotal();
}
});

// Initialize grand total


updateGrandTotal();
});
</script>

</body>

</html>

bills_action.php
<?php
include "../config/dbconnect.php";
if (isset($_POST['action'])) {
$bill_date = $_POST["bill_date"];
$bill_number =$_POST["bill_number"];
$customer_id = $_POST["customer_id"];
$bill_total_amount = $_POST["bill_total_amount"];
$sql_bill_item = "INSERT INTO bill_items
(product_id,bill_item_quantity,bill_item_price,bill_item_tax_percentage,bill_item_t
ax_price,bill_item_total_amount,bill_id) VALUES (?,?,?,?,?,?,?)";
$rows = [];
for ($i = 0; $i < count($_POST["product_id"]); $i++) {
$product_id = $_POST["product_id"][$i];
$bill_item_quantity = $_POST["bill_item_quantity"][$i];
$bill_item_price = $_POST["bill_item_price"][$i];
$bill_item_tax_percentage = $_POST["bill_item_tax_percentage"][$i];
$bill_item_tax_price = $_POST["bill_item_tax_price"][$i];
$bill_item_total_amount = $_POST["bill_item_total_amount"][$i];
$bill_id = $_POST["bill_id"][$i];
$rows[] = "('$product_id', '$bill_item_quantity', '$bill_item_price',
'$bill_item_tax_percentage',
'$bill_item_tax_price','$bill_item_total_amount','$bill_id')";
// Insert data into Table 1
$sql_bills = "INSERT INTO bills (bill_date, bill_number, customer_id,
bill_total_amount) VALUES (?,?,?,?)";
$stmt_bills = $conn->prepare($sql_bills);

if ($stmt_bills === false) {


die("Error in preparing the statement for Table 1: " . $conn->error);
}

$stmt_bills->bind_param("siii", $bill_date, $bill_number, $customer_id,


$bill_total_amount);
if ($stmt_bills->execute() === false) {
echo "Error in executing the statement for Table 1: " . $stmt_bills->error;
}

// Insert data into Table 2


$sql_bill_item = "INSERT INTO bill_items
(product_id,bill_item_quantity,bill_item_price,bill_item_tax_percentage,bill_item_t
ax_price,bill_item_total_amount,bill_id) VALUES (?,?,?,?,?,?,?)";
$stmt_bill_item = $conn->prepare($sql_bill_item);

if ($stmt_bill_item === false) {


die("Error in preparing the statement for Table 2: " . $conn->error);
}

$stmt_bill_item->bind_param("iiiiiii",$product_id,$bill_item_quantity,
$bill_item_price,$bill_item_tax_percentage,$bill_item_tax_price,
$bill_item_total_amount,$bill_id);
if ($stmt_bill_item->execute() === false) {
echo "Error in executing the statement for Table 2: " . $stmt_bill_item-
>error;
}
}
// Add more code to insert data into other tables as needed

$stmt_bills->close();
$stmt_bill_item->close();
}

else if ($action == "Update") {

$bill_id = mysqli_real_escape_string($con, $_POST["bill_id"]);


$bill_date = mysqli_real_escape_string($con, $_POST["bill_date"]);
$bill_number = mysqli_real_escape_string($con, $_POST["bill_number"]);
$customer_id = mysqli_real_escape_string($con, $_POST["customer_id"]);
$bill_total_amount = mysqli_real_escape_string($con,
$_POST["bill_total_amount"]);
$sql = "update bills SET
bill_id='{$bill_id}',bill_date='{$bill_date}',bill_number='{$bill_number}',customer
_id='{$customer_id}',bill_total_amount='{$bill_total_amount}' where
bill_id='{$bill_id}'";

$bill_item_id = mysqli_real_escape_string($con, $_POST["bill_item_id"]);


$product_id = mysqli_real_escape_string($con, $_POST["product_id"]);
$bill_item_quantity = mysqli_real_escape_string($con,
$_POST["bill_item_quantity"]);
$bill_item_price = mysqli_real_escape_string($con, $_POST["bill_item_price"]);
$bill_item_tax_percentage = mysqli_real_escape_string($con,
$_POST["bill_item_tax_percentage"]);
$bill_item_tax_price = mysqli_real_escape_string($con,
$_POST["bill_item_tax_price"]);
$bill_item_total_amount = mysqli_real_escape_string($con,
$_POST["bill_item_total_amount"]);
$bill_id = mysqli_real_escape_string($con, $_POST["bill_id"]);
$sql_bill_item = "UPDATE bill_items SET product_id=?, bill_item_quantity=?,
bill_item_price=?, bill_item_tax_percentage=?, bill_item_tax_price=?,
bill_item_total_amount=?, bill_id=? WHERE bill_item_id=?";
// Fix the SQL query syntax issue and use prepared statements
$sql_bill_item = "UPDATE bill_items SET product_id=?, bill_item_quantity=?,
bill_item_price=?, bill_item_tax_percentage=?, bill_item_tax_price=?,
bill_item_total_amount=?, bill_id=? WHERE bill_item_id=?";

$stmt = $con->prepare($sql_bill_item);

if ($stmt) {
// Bind parameters
$stmt->bind_param("iiiiiiii", $product_id, $bill_item_quantity,
$bill_item_price, $bill_item_tax_percentage, $bill_item_tax_price,
$bill_item_total_amount, $bill_id, $bill_item_id);

// Execute the update query


if (!$stmt->execute()) {
echo json_encode(["success" => false, "message" => "Error: " . $stmt-
>error]);
} else {
echo json_encode(["success" => true, "message" => "Bill item updated
successfully"]);
}

// Close the prepared statement


$stmt->close();
} else {
// Error in preparing the statement
echo json_encode(["success" => false, "message" => "Error in preparing the
statement"]);
}
}

ajax
<?php
if (!empty($_GET['bill_id'])) {
$con = mysqli_connect("localhost", "root", "", "adminpanel");
$sql = "SELECT * FROM bills WHERE bill_id = " . $_GET['bill_id'];
//echo $sql;
$res = $con->query($sql);

if (!$res) {
die("Query error: " . mysqli_error($con));
}

$bill = $res->fetch_assoc();
header('Content-Type: application/json; charset=utf-8');
echo json_encode($bill, JSON_PRETTY_PRINT);
}
if(!empty($_GET['bill_item_id'])) {
$con = mysqli_connect("localhost", "root", "", "adminpanel");
$sql = "select * from bill_items WHERE bill_item_id = ".$_GET['bill_item_id'];
//echo $sql;
$res = $con->query($sql);
$bill_items = $res->fetch_assoc();
header('Content-Type: application/json; charset=utf-8');
echo json_encode($bill_items, JSON_PRETTY_PRINT);
}
?>

You might also like