<?php
require_once dirname(__FILE__) . '/DbConnect.php';
$conn=$con;
//Operation Values
// New -
// Edit
// Del
// ReadAll
// Read
$mydata;
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['operation']) && isset($_POST['om_id']))
{
$operation = $_POST['operation'];
$om_id = $_POST['om_id'];
if( isset($_POST['jsonData']) )
{
$jsonData = $_POST['jsonData'];
$mydata = json_decode($jsonData);
}
}
else
{
echo "Required fields are not set";
return;
}
if ($operation == "New" )
{
// Insert Operation
order_insert($mydata, $conn);
}
elseif ($operation == "Edit" )
{
// Update Operation
order_edit($om_id, $mydata, $conn);
}
elseif ($operation == "Del" )
{
// Delete Operation
order_del($om_id, $conn );
}
elseif ($operation == "ReadAll" )
{
order_readall($conn);
}
elseif ($operation == "read " )
{
order_read($om_id,$conn);
}
else
{
echo "Invalid or incomplete data for operation";
}
// Close the database connection
$conn->close();
function order_insert($data, $conn){
// Prepare and execute SQL query for insert (use prepared statements for security)
$stmt = $conn->prepare("INSERT INTO order_master (om_name, om_no, om_grpno) VALUES ( ?, ?, ?)");
$stmt->bind_param("sss", $data->om_name, $data->om_no , $data->om_grpno);
if ($stmt->execute()) {
echo "Data inserted successfully";
} else {
echo "Insert Failed: " . $stmt->error;
}
/* **** **** **** **** ****
$x = getId(); // Read or Get the id of the newly inserted record.
//$x = $conn->lastInsertId();
$orderitems [] =jsondecode( $data->orderitems;)
while ( $orderitems ) // loop thru all the items in the orderitems array
{
$item = $orderitems[]; // get the each and every record/row of the orderitems array
//call Order_Txn.php // call the Order_Txn.php with for insert opration
//"New" , $x , $item ; // and pass the value of newly inserted orderid
//call Insert_Order_Items
Insert_Order_Items($x,$item, $conn) ; // and pass the value of newly inserted orderid
}
**** **** **** **** **** */
// Close the statement
$stmt->close();
}
function order_edit($om_id, $data, $conn)
{
$stmt = $conn->prepare("UPDATE order_master SET om_name=?, om_no=?, om_grpno=? WHERE om_id=?");
$stmt->bind_param("sssi", $data->om_name, $data->om_no , $data->om_grpno, $om_id);
// Check for successful execution
if ($stmt->execute()) {
echo "Update Success";
} else {
echo "Update Failed: " . $stmt->error;
// Add debugging statements
}
/* **** **** **** **** ****
$x = $om_id; // Set the value of OT_Id to the id of the record being edited.
//call Order_Txn.php // call the Order_Txn.php with for delete opration
//"Del" , $x, $item ; // and pass the value of current orderid
//call Del_Order_Items
Del_Order_Items($x, $conn) ; // and pass the value of newly inserted orderid
// now insert all the new items again in order transaction table
$orderitems = $data->orderitems;
while ( $orderitems ) // loop thru all the items in the orderitems array
{
$item = $orderitems[]; // get the each and every record/row of the orderitems array
//call Order_Txn.php // call the Order_Txn.php with for insert opration
//"New" , $x, $item ; // and pass the value of newly inserted orderid
//call Insert_Order_Items
Insert_Order_Items($x,$item, $conn) ; // and pass the value of newly inserted orderid
}
**** **** **** **** **** */
// Close the statement
$stmt->close();
}
function order_del($om_id, $conn )
{
$stmt = $conn->prepare("DELETE FROM order_master WHERE om_id=?");
$stmt->bind_param("i", $om_id);
// Check for successful execution
if ($stmt->execute()) {
echo "Delete Success";
} else {
echo "Delete Failed: " . $stmt->error;
}
// Close the statement
$stmt->close();
}
function order_readall($conn) {
//$query = "SELECT om_id ,om_name,om_no ,om_grpno from order_master;";
$query = "SELECT * from order_master;";
$result = $conn->query($query);
$items = array();
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
$items[] = $row;
}
echo json_encode($items);
}
else
{
echo "No items found";
}
$result->close();
}
function order_read($om_id,$conn)
{ // Read Operation
$result = $conn->query("SELECT * FROM order_master WHERE om_id=?");
$items = array();
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
$items[] = $row;
}
echo json_encode($items);
}
else
{
echo "No items found";
}
// Close the result set
$result->close();
}
function order_items_insert($orderitemid, $data, $conn)
{
// Prepare and execute SQL query for insert (use prepared statements for security)
$stmt = $conn->prepare("INSERT INTO order_transaction (ot_id, ot_srno, ot_itemid, ot_rate, ot_remark, ot_amount, ot_quantity) VALUES (?, ?, ?, ?, ?, ?, ?)");
// Use appropriate data types in bind_param based on your database table
$stmt->bind_param("iiiisss", $orderitemid, $data->ot_srno, $data->ot_itemid, $data->ot_rate, $data->ot_remark, $data->ot_amount, $data->ot_quantity);
if ($stmt->execute()) {
echo "Data inserted successfully";
} else {
echo "Insert Failed: " . $stmt->error;
}
// Close the statement
$stmt->close();
}
function order_items_del($ot_id, $conn )
{
$stmt = $conn->prepare("DELETE FROM order_transaction WHERE ot_id=?");
$stmt->bind_param("i", $ot_id);
// Check for successful execution
if ($stmt->execute()) {
echo "Delete Success";
} else {
echo "Delete Failed: " . $stmt->error;
}
// Close the statement
$stmt->close();
}
?>