Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- try
- {
- //Open database connection
- $con = mysql_connect("localhost","root","itesm");
- mysql_select_db("sgv", $con);
- $ID=$_GET["ID"];
- //Getting records (listAction)
- if(($_GET["ID"] != "") & ($_GET["action"] == "list"))
- {
- //Get record count
- $result = mysql_query("SELECT COUNT(*) AS RecordCount FROM detalle_venta ;");
- $row = mysql_fetch_array($result);
- $recordCount = $row['RecordCount'];
- //Get records from database
- $result = mysql_query("
- SELECT detalle_venta.* ,producto.ID_Producto, producto.Descripcion
- FROM detalle_venta, producto
- WHERE detalle_venta.ID_Venta='" . $_GET["ID"] . "'
- AND producto.ID_Producto=detalle_venta.ID_Producto;");
- //Add all records to an array
- $rows = array();
- while($row = mysql_fetch_array($result))
- {
- $rows[] = $row;
- }
- //Return result to jTable
- $jTableResult1 = array();
- $jTableResult1['Result'] = "OK";
- $jTableResult1['TotalRecordCount'] = $recordCount;
- $jTableResult1['Records'] = $rows;
- print json_encode($jTableResult1);
- }
- else if(($_GET["ID"] != "") & ($_GET["action"] == "create"))
- {
- //Insert record into database
- $result = mysql_query("INSERT INTO detalle_venta(ID_Venta, ID_Producto, Descuento, Impuesto, Precio_Venta, Cantidad, Subtotal) VALUES('" . $_GET["ID"] . "', '" . $_POST["DD_Producto"] . "'," . $_POST["Descuento"] . "," . $_POST["Impuesto"] . ",'" . $_POST["Precio_Venta"] . "','" . $_POST["Cantidad"] . "', '0');");
- $sql1 = mysql_query("UPDATE producto SET Existencia = Existencia - " . $_POST["Cantidad"] . " WHERE ID_Producto = '" . $_POST["DD_Producto"] . "';");
- $sql2 = mysql_query("SELECT @cantidad:= Cantidad FROM detalle_venta WHERE IDT = LAST_INSERT_ID();");
- $sql3 = mysql_query("SELECT @precio_venta:= Precio_Venta FROM detalle_venta WHERE IDT = LAST_INSERT_ID();");
- $sql4 = mysql_query("UPDATE detalle_venta SET Subtotal = (@cantidad*@precio_venta)+((@cantidad*@precio_venta*" . $_POST["Impuesto"] . ")/100) - ((@cantidad*@precio_venta*" . $_POST["Descuento"] . ")/100) WHERE IDT = LAST_INSERT_ID();");
- $sql5 = mysql_query("SELECT @subtotal:= Subtotal FROM detalle_venta WHERE IDT = LAST_INSERT_ID();");
- $sql6 = mysql_query("UPDATE ventas SET Precio_Total = Precio_Total + @subtotal WHERE ID_Venta = '" . $_GET["ID"] . "';");
- //$ultimo_id = mysql_insert_id($con);
- //Get last inserted record (to return to jTable)
- //$result = mysql_query("SELECT * FROM detalle_venta WHERE ID_Venta = LAST_INSERT_ID();");
- //$result = mysql_query("SELECT * FROM detalle_venta WHERE ID_Venta='" . $_POST["ID_Venta"] . "';");
- $result = mysql_query("SELECT *,descripcion FROM detalle_venta,producto WHERE IDT = LAST_INSERT_ID();");
- $row = mysql_fetch_array($result);
- //Return result to jTable
- $jTableResult = array();
- $jTableResult['Result'] = "OK";
- $jTableResult['Record'] = $row;
- print json_encode($jTableResult);
- }
- //Updating a record (updateAction) Not completed ! Añadir ID_DetVenta
- else if(($_GET["ID"] != "") & ($_GET["action"] == "update"))
- {
- //Update record in database
- $result = mysql_query("UPDATE detalle_venta SET Cantidad= '" . $_POST["Cantidad"] . "', Precio_Venta= " . $_POST["Precio_Venta"] . ", Descuento= " . $_POST["Descuento"] . ", Impuesto= '" . $_POST["Impuesto"] . "' WHERE IDT = '" . $_POST["IDT"] . "';");
- //Return result to jTable
- $jTableResult = array();
- $jTableResult['Result'] = "OK";
- print json_encode($jTableResult);
- }
- //Deleting a record (deleteAction) /Incomplete
- else if(($_GET["action"] == "delete"))
- {
- //Delete from database
- $exist = mysql_query("SELECT @ex:= Cantidad FROM detalle_venta WHERE IDT = " . $_POST["IDT"] . ";");
- $product = mysql_query("SELECT @pro:= ID_Producto FROM detalle_venta WHERE IDT = " . $_POST["IDT"] . ";");
- $sqlexistencias = mysql_query("UPDATE producto SET Existencia = Existencia + @ex WHERE ID_Producto = @pro;");
- $sql5 = mysql_query("SELECT @idventa:= ID_Venta FROM detalle_venta WHERE IDT = " . $_POST["IDT"] . ";");
- $sql5 = mysql_query("SELECT @subtotal:= Subtotal FROM detalle_venta WHERE IDT = " . $_POST["IDT"] . ";");
- $sql6 = mysql_query("UPDATE ventas SET Precio_Total = Precio_Total - @subtotal WHERE ID_Venta = @idventa;");
- $result = mysql_query("DELETE FROM detalle_venta WHERE IDT = " . $_POST["IDT"] . ";");
- //Return result to jTable
- $jTableResult = array();
- $jTableResult['Result'] = "OK";
- print json_encode($jTableResult);
- }
- else if($_GET["action"] == "dropdown_producto"){
- $result = mysql_query("SELECT ID_Producto, Descripcion,Precio_Venta FROM producto ORDER BY ID_Producto");
- //Add all records to an array
- $rows = array();
- while ($row = mysql_fetch_array($result)) {
- $rows[] = array(
- 'Value' => $row['ID_Producto'],
- 'DisplayText' => $row['Descripcion']
- );
- }
- $result = array('Result' => 'OK',
- 'Options' => $rows);
- print json_encode($result);
- }
- //Close database connection
- mysql_close($con);
- }
- catch(Exception $ex)
- {
- //Return error message
- $jTableResult = array();
- $jTableResult['Result'] = "ERROR";
- $jTableResult['Message'] = $ex->getMessage();
- print json_encode($jTableResult);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement