php_billing/119/create_bill.php

184 lines
7.1 KiB
PHP

<?php
// echo $_POST['phone'];
if(isset($_POST['remove_item_from_bill'])){
//echo $_POST['remove_item_from_bill'];
$conn = new mysqli(MYSQL_HOST,MYSQL_USER,MYSQL_PASS,MYSQL_DB);
//$link = new mysqli(MYSQL_HOST,MYSQL_USER,MYSQL_PASS,MYSQL_DB);
$conn->set_charset("utf8");
$query="UPDATE `".SHOP_ID."_product` SET `stock` = `stock` +1 WHERE `id`='".$_POST['product_id']."'";
if($conn->query($query) === TRUE){
$sql = "DELETE FROM `".SHOP_ID."_bill_product` WHERE `id` = '".$_POST['remove_item_from_bill']."'";
if ($conn->query($sql) === TRUE) {
echo "Removed from Bill no: ".$_GET['bill'].",(this bill) and added to stock.";
} else {
echo "Error deleting record: " . $conn->error;
}
}
$conn->close();
}
if(isset($_POST['phone'])){
// echo strlen($_POST['phone']);exit();
if(strlen($_POST['phone']) >4) $name=$_POST['name']; else $name='0';
$link = new mysqli(MYSQL_HOST,MYSQL_USER,MYSQL_PASS,MYSQL_DB);
$link->set_charset("utf8");
$started=1;
$query="INSERT INTO `".SHOP_ID."_bill` ( `status`,`cust_phone`,`cust_name`)VALUES(?,?,?)";
$stmt = $link->prepare($query);
if($stmt){
$stmt->bind_param("sss", $started,$_POST['phone'],$name );
//if(!$stmt->execute()) echo '<div class="alert alert-danger">' . mysqli_error($link) . '</div>';
if(!$stmt->execute()) echo '<div class="alert alert-danger">' . mysqli_error($link) . '</div>';
else {
$last_id = $link->insert_id;
echo '<div class="alert alert-success"> '.$last_id .'Bill Started</div> <br>
<meta http-equiv="refresh" content="0; url=create_bill?bill='.$last_id .'">
';
}
}
else echo mysqli_error($link);
$link->close();
}
elseif(isset($_GET['bill'])){
// echo $_GET['bill'];
echo ' <br /> <form method="POST"> <input type="hidden" name="bill" value="'.$_GET['bill'].'" > <input type="text" name="code" autofocus> </form>';
if(isset($_POST['code'])){
$code = explode('-', $_POST['code']); // get the product id #$code[0]
$link = new mysqli(MYSQL_HOST,MYSQL_USER,MYSQL_PASS,MYSQL_DB);
$query="SELECT * FROM `".SHOP_ID."_product` WHERE `id`='".$code[0]."'";
$result= mysqli_query($link, $query);
if(mysqli_num_rows($result)<1){
echo ' <br /> <h2> Err: no Product Found </h2> ';
}
else {
$row = $result -> fetch_array(MYSQLI_ASSOC);
if($row['stock']<1) echo ' <br /> <h2> Err: out of stock </h2> ';
else {
$link = new mysqli(MYSQL_HOST,MYSQL_USER,MYSQL_PASS,MYSQL_DB);
$link->set_charset("utf8");
$query="UPDATE `".SHOP_ID."_product` SET `stock` = `stock` -1 WHERE `id`='".$code[0]."'";
if($link->query($query) === TRUE){
$query="INSERT INTO `".SHOP_ID."_bill_product` ( `bill_id`,`product_id`,`mrp`,`price`,`cost`,`product_name`, `hsn`)VALUES(?,?,?,?,?,?,?)";
$stmt = $link->prepare($query);
if($stmt){
$stmt->bind_param("sssssss", $_GET['bill'],$code[0],$row['mrp'],$row['price'],$row['cost'],$row['name'],$row['hsn'] ); echo $row['mrp'],$row['price'];
//if(!$stmt->execute()) echo '<div class="alert alert-danger">' . mysqli_error($link) . '</div>';
if(!$stmt->execute()) echo '<div class="alert alert-danger"> ERR: 8776' . mysqli_error($link) . '</div>';
// else {
// $last_id = $link->insert_id;
// echo '<div class="alert alert-success"> '.$last_id .'Bill Started</div> <br>
// ';
// }
}
else echo mysqli_error($link);
}
$link->close();
}
}
}
if(isset($_POST['payment_mode'])){
//echo $_POST['payment_mode'];
$conn = new mysqli(MYSQL_HOST,MYSQL_USER,MYSQL_PASS,MYSQL_DB);
$sql = "UPDATE `".SHOP_ID."_bill` SET `payment_mode` = '".$_POST['payment_mode']."' , `total` = '".$_POST['bill_total']."' , `cost` = '".$_POST['bill_total_cost']."' WHERE `id` = '".$_POST['bill_no']."'";
if ($conn->query($sql) === TRUE) {
echo "Bill Saved!";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
}
}
?>
<div id="section-to-print">
Bill No: <?php
$tz = 'Asia/Kolkata';
$timestamp = time();
$dt = new DateTime("now", new DateTimeZone($tz)); //first argument "must" be a string
$dt->setTimestamp($timestamp); //adjust the object to correct timestamp
echo $_GET['bill'], ", ",$dt->format('Y/m/d h:i:sa');
//echo $_GET['bill'], ", ",date("Y/m/d h:i:sa");
?> </span>
<br />
<table>
<tr>
<td>SL</td>
<td>Product</td>
<td>Price</td>
<td>Action</td>
</tr>
<?php
$sl=0;
$total=0;$total_cost=0;
$link = new mysqli(MYSQL_HOST,MYSQL_USER,MYSQL_PASS,MYSQL_DB);
$query="SELECT * FROM `".SHOP_ID."_bill_product` WHERE `bill_id`='".$_GET['bill']."' ORDER BY `id` DESC";
$result= mysqli_query($link, $query);
while ($row= mysqli_fetch_assoc($result)) {
$sl++;$total=$total+$row['price']; $total_cost=$total_cost+$row['cost'];
?>
<tr>
<td><?php echo $sl; ?></td>
<td><?php echo $row['product_name']; ?><br><span style="font-size: 10px;"><?php echo $row['hsn']; ?></span></td>
<td><?php echo $row['price']; ?></td>
<td> <form method="POST"> <input type="hidden" name="product_id" value="<?php echo $row['product_id']; ?>" /> <input type="hidden" name="remove_item_from_bill" value="<?php echo $row['id'] ?>" /> <input type="submit" value="Remove" /> </form> </td>
</tr>
<?php } ?>
</table>
<?php
$gst=18*$total/100;
//echo 'GST: ',$gst,'<br />' ;
echo 'Total:',$total;
// echo '<a href="print_bill.php?'.$_GET['bill'].'"> Print </a>';
?>
<br /> <br />
<span> Payment Mode </span>
<form method="POST">
<input type="radio" id="Cash" name="payment_mode" value="1">
<label for="Cash">Cash</label><br>
<input type="radio" id="Card" name="payment_mode" value="2">
<label for="Card">Card</label><br>
<input type="radio" id="UPI" name="payment_mode" value="3">
<label for="UPI">UPI</label><br>
<input type="radio" id="Other" name="payment_mode" value="9">
<label for="Other">Other</label> <br /><br />
<input type="hidden" name="bill_no" value="<?php echo $_GET['bill']; ?>" />
<input type="hidden" name="bill_total" value="<?php echo $total; ?>" /> <input type="hidden" name="bill_total_cost" value="<?php echo $total_cost; ?>" />
<input type="submit" value="Save" />
</form>
<?php
$conn = new mysqli(MYSQL_HOST,MYSQL_USER,MYSQL_PASS,MYSQL_DB);
$sql = "SELECT * FROM `".SHOP_ID."_bill` WHERE `id` = '".$_GET['bill']."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if($row["payment_mode"]!='0') echo '<a href="print-bill?bill='.$_GET['bill'].'" > Print </a>';
}
} else {
echo "ERR: 4499";
}
$conn->close();
?>
<style>
@media print {
body * {
visibility: hidden;
}
#section-to-print, #section-to-print * {
visibility: visible;
}
#section-to-print {
position: absolute;
left: 0;
top: 0;
}
}
</style>