Advertisement
Guest User

Ventas_sql.php

a guest
Nov 5th, 2012
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.16 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.     //Getting records (listAction)
  9.     if($_GET["action"] == "list")
  10.     {
  11.         //Get record count
  12.         $result = mysql_query("SELECT COUNT(*) AS RecordCount FROM ventas;");
  13.         $row = mysql_fetch_array($result);
  14.         $recordCount = $row['RecordCount'];
  15.  
  16.         //Get records from database
  17.         $result = mysql_query("
  18.             SELECT distinct ventas.ID_Venta, ventas.Fecha_Venta, ventas.Precio_Total, clientes.ID_Cliente, clientes.Nombre, monedas.*
  19.             FROM ventas,clientes, monedas
  20.             WHERE clientes.ID_Cliente=ventas.ID_Cliente
  21.             AND monedas.ID_Moneda=ventas.ID_Moneda ORDER BY " . $_GET["jtSorting"] . " LIMIT " . $_GET["jtStartIndex"] . "," . $_GET["jtPageSize"] . ";");
  22.  
  23.         //Add all records to an array
  24.         $rows = array();
  25.        
  26.         while($row = mysql_fetch_array($result))
  27.         {
  28.             $rows[] = $row;
  29.         }
  30.  
  31.         //Return result to jTable
  32.         $jTableResult = array();
  33.         $jTableResult['Result'] = "OK";
  34.         $jTableResult['TotalRecordCount'] = $recordCount;
  35.         $jTableResult['Records'] = $rows;
  36.         print json_encode($jTableResult);
  37.     }
  38.     //Creating a new record (createAction)
  39.     else if($_GET["action"] == "create")
  40.     {
  41.         //Insert record into database
  42.         $result = mysql_query("INSERT INTO ventas(ID_Cliente, Fecha_Venta, ID_Moneda,Precio_Total) VALUES('" . $_POST["DD_Cliente"] . "', '" . $_POST["Fecha_Venta"] . "','" . $_POST["DD_Moneda"] . "', '0');");
  43.        
  44.         //Get last inserted record (to return to jTable)
  45.         $result = mysql_query("SELECT * FROM ventas WHERE ID_Venta = LAST_INSERT_ID();");
  46.         $row = mysql_fetch_array($result);
  47.  
  48.         //Return result to jTable
  49.         $jTableResult = array();
  50.         $jTableResult['Result'] = "OK";
  51.         $jTableResult['Record'] = $row;
  52.         print json_encode($jTableResult);
  53.     }
  54.     //Updating a record (updateAction)
  55.     else if($_GET["action"] == "update")
  56.     {
  57.         //Update record in database
  58.    
  59.         $result = mysql_query("UPDATE ventas SET ID_Cliente = '" . $_POST["DD_Cliente"] . "', Fecha_Venta= '" . $_POST["Fecha_Venta"] . "', ID_Moneda= '" . $_POST["DD_Moneda"] . "' WHERE ID_Venta = " . $_POST["ID_Venta"] . ";");
  60.  
  61.         //Return result to jTable
  62.         $jTableResult = array();
  63.         $jTableResult['Result'] = "OK";
  64.         print json_encode($jTableResult);
  65.     }
  66.     //Deleting a record (deleteAction)
  67.     else if($_GET["action"] == "delete")
  68.     {
  69.         //Delete from database
  70.         $result = mysql_query("DELETE FROM ventas WHERE ID_Venta = " . $_POST["ID_Venta"] . ";");
  71.  
  72.         //Return result to jTable
  73.         $jTableResult = array();
  74.         $jTableResult['Result'] = "OK";
  75.         print json_encode($jTableResult);
  76.     }
  77.    
  78.     else if($_GET["action"] == "dropdown_cliente"){
  79.    
  80.         $result = mysql_query("SELECT ID_Cliente, Nombre FROM clientes ORDER BY ID_Cliente");
  81.         //Add all records to an array
  82.         $rows = array();
  83.         while ($row = mysql_fetch_array($result)) {
  84.             $rows[] = array('Value' => $row['ID_Cliente'],
  85.             'DisplayText' => $row['Nombre']);  
  86.         }
  87.    
  88.     $result = array('Result' => 'OK',
  89.             'Options' => $rows);
  90.     print json_encode($result);
  91.  
  92.     }
  93.    
  94.     else if($_GET["action"] == "dropdown_moneda"){
  95.    
  96.         $result = mysql_query("SELECT ID_Moneda, Nombre_Moneda FROM monedas ORDER BY ID_Moneda");
  97.         //Add all records to an array
  98.         $rows = array();
  99.         while ($row = mysql_fetch_array($result)) {
  100.             $rows[] = array('Value' => $row['ID_Moneda'],
  101.             'DisplayText' => $row['Nombre_Moneda']);   
  102.         }
  103.    
  104.         $result = array('Result' => 'OK',
  105.             'Options' => $rows);
  106.         print json_encode($result);
  107.  
  108.     }
  109.    
  110.     else if($_GET["action"] == "calculartotal"){
  111.    
  112.     $result = mysql_query("SELECT ID_Cliente, Nombre FROM clientes ORDER BY ID_Cliente");
  113.     //Add all records to an array
  114.     $rows = array();
  115.     while ($row = mysql_fetch_array($result)) {
  116.         $rows[] = array('Value' => $row['ID_Cliente'],
  117.         'DisplayText' => $row['Nombre']);  
  118.     }
  119.    
  120.     $result = array('Result' => 'OK',
  121.             'Options' => $rows);
  122.     print json_encode($result);
  123.  
  124.     }
  125.  
  126.     //Close database connection
  127.     mysql_close($con);
  128.  
  129. }
  130. catch(Exception $ex)
  131. {
  132.     //Return error message
  133.     $jTableResult = array();
  134.     $jTableResult['Result'] = "ERROR";
  135.     $jTableResult['Message'] = $ex->getMessage();
  136.     print json_encode($jTableResult);
  137. }
  138.    
  139. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement