Advertisement
Guest User

Ventas_Sql_Sub.php

a guest
Nov 5th, 2012
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.17 KB | None | 0 0
  1. <?php
  2.  
  3. try
  4. {
  5.     //Open database connection
  6.     $con = mysql_connect("localhost","root","itesm");
  7.     mysql_select_db("sgv", $con);
  8.     $ID=$_GET["ID"];
  9.     //Getting records (listAction)
  10.     if(($_GET["ID"] != "") & ($_GET["action"] == "list"))
  11.     {
  12.         //Get record count
  13.         $result = mysql_query("SELECT COUNT(*) AS RecordCount FROM detalle_venta ;");
  14.         $row = mysql_fetch_array($result);
  15.         $recordCount = $row['RecordCount'];
  16.  
  17.         //Get records from database
  18.         $result = mysql_query("
  19.             SELECT detalle_venta.* ,producto.ID_Producto, producto.Descripcion
  20.             FROM detalle_venta, producto
  21.             WHERE detalle_venta.ID_Venta='" . $_GET["ID"] . "'
  22.             AND producto.ID_Producto=detalle_venta.ID_Producto;");
  23.  
  24.        
  25.         //Add all records to an array
  26.         $rows = array();
  27.         while($row = mysql_fetch_array($result))
  28.         {
  29.             $rows[] = $row;
  30.         }
  31.  
  32.         //Return result to jTable
  33.         $jTableResult1 = array();
  34.         $jTableResult1['Result'] = "OK";
  35.         $jTableResult1['TotalRecordCount'] = $recordCount;
  36.         $jTableResult1['Records'] = $rows;
  37.         print json_encode($jTableResult1);
  38.     }
  39.    
  40.         else if(($_GET["ID"] != "") & ($_GET["action"] == "create"))
  41.     {
  42.         //Insert record into database
  43.         $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');");
  44.         $sql1 = mysql_query("UPDATE producto SET Existencia = Existencia - " . $_POST["Cantidad"] . " WHERE ID_Producto = '" . $_POST["DD_Producto"] . "';");
  45.         $sql2 = mysql_query("SELECT @cantidad:= Cantidad FROM detalle_venta WHERE IDT = LAST_INSERT_ID();");
  46.         $sql3 = mysql_query("SELECT @precio_venta:= Precio_Venta FROM detalle_venta WHERE IDT = LAST_INSERT_ID();");
  47.         $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();");
  48.         $sql5 = mysql_query("SELECT @subtotal:= Subtotal FROM detalle_venta WHERE IDT = LAST_INSERT_ID();");
  49.         $sql6 = mysql_query("UPDATE ventas SET Precio_Total = Precio_Total + @subtotal WHERE ID_Venta = '" . $_GET["ID"] . "';");
  50.         //$ultimo_id = mysql_insert_id($con);
  51.        
  52.         //Get last inserted record (to return to jTable)
  53.         //$result = mysql_query("SELECT * FROM detalle_venta WHERE ID_Venta = LAST_INSERT_ID();");
  54.        
  55.         //$result = mysql_query("SELECT * FROM detalle_venta WHERE ID_Venta='" . $_POST["ID_Venta"] . "';");
  56.         $result = mysql_query("SELECT *,descripcion FROM detalle_venta,producto WHERE IDT = LAST_INSERT_ID();");
  57.         $row = mysql_fetch_array($result);
  58.  
  59.         //Return result to jTable
  60.         $jTableResult = array();
  61.         $jTableResult['Result'] = "OK";
  62.         $jTableResult['Record'] = $row;
  63.         print json_encode($jTableResult);
  64.     }
  65.    
  66.         //Updating a record (updateAction) Not completed ! Añadir ID_DetVenta
  67.     else if(($_GET["ID"] != "") & ($_GET["action"] == "update"))
  68.     {
  69.         //Update record in database
  70.    
  71.         $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"] . "';");
  72.  
  73.         //Return result to jTable
  74.         $jTableResult = array();
  75.         $jTableResult['Result'] = "OK";
  76.         print json_encode($jTableResult);
  77.     }
  78.    
  79.     //Deleting a record (deleteAction) /Incomplete
  80.     else if(($_GET["action"] == "delete"))
  81.     {
  82.         //Delete from database
  83.         $exist = mysql_query("SELECT @ex:= Cantidad FROM detalle_venta WHERE IDT = " . $_POST["IDT"] . ";");
  84.         $product = mysql_query("SELECT @pro:= ID_Producto FROM detalle_venta WHERE IDT = " . $_POST["IDT"] . ";");
  85.         $sqlexistencias = mysql_query("UPDATE producto SET Existencia = Existencia + @ex WHERE ID_Producto = @pro;");
  86.        
  87.         $sql5 = mysql_query("SELECT @idventa:= ID_Venta FROM detalle_venta WHERE IDT = " . $_POST["IDT"] . ";");
  88.         $sql5 = mysql_query("SELECT @subtotal:= Subtotal FROM detalle_venta WHERE IDT = " . $_POST["IDT"] . ";");
  89.         $sql6 = mysql_query("UPDATE ventas SET Precio_Total = Precio_Total - @subtotal WHERE ID_Venta = @idventa;");
  90.        
  91.         $result = mysql_query("DELETE FROM detalle_venta WHERE IDT = " . $_POST["IDT"] . ";");
  92.         //Return result to jTable
  93.         $jTableResult = array();
  94.         $jTableResult['Result'] = "OK";
  95.         print json_encode($jTableResult);
  96.     }
  97.  
  98.     else if($_GET["action"] == "dropdown_producto"){
  99.    
  100. $result = mysql_query("SELECT ID_Producto, Descripcion,Precio_Venta FROM producto ORDER BY ID_Producto");
  101.    //Add all records to an array
  102.    $rows = array();
  103.    while ($row = mysql_fetch_array($result)) {
  104.          $rows[] = array(
  105.          'Value' => $row['ID_Producto'],
  106.          'DisplayText' => $row['Descripcion']
  107.          );
  108.    }
  109.    
  110.    $result = array('Result' => 'OK',
  111.             'Options' => $rows);
  112.    print json_encode($result);
  113.  
  114. }
  115.  
  116.  
  117.  
  118.  
  119.     //Close database connection
  120.     mysql_close($con);
  121.  
  122. }
  123. catch(Exception $ex)
  124. {
  125.     //Return error message
  126.     $jTableResult = array();
  127.     $jTableResult['Result'] = "ERROR";
  128.     $jTableResult['Message'] = $ex->getMessage();
  129.     print json_encode($jTableResult);
  130. }
  131.    
  132. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement