Bills PHP
Bills PHP
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">×</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>
$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');
}
});
});
function clear_input() {
$("#frm").find(".form-control").val("");
$("#action").val("Insert");
$("#uid").val("0");
}
</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);
$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();
}
$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);
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);
}
?>