carrito de compras con php

14
CARRITO DE COMPRAS Pagina Catalogo.aspx

Upload: gerson-purisaca-martinez

Post on 18-Jan-2016

41 views

Category:

Documents


5 download

DESCRIPTION

creando un carrito de compras con php

TRANSCRIPT

Page 1: Carrito de Compras con PHP

CARRITO DE COMPRAS

Pagina Catalogo.aspx

Page 2: Carrito de Compras con PHP

Pagina listacomprados.aspx

Estructura del proyecto

Page 3: Carrito de Compras con PHP

COMPONENTEDATOS

CLASE Conexion

Public Class Conexion

Private Shared ReadOnly _instancia As Conexion = New Conexion

Private Sub New()

End Sub

Public Shared ReadOnly Property Instancia() As Conexion

Get

Return _instancia

End Get

End Property

Public Function cadenaconexion() As String

Return "Data Source=SERVER;Initial Catalog=BDClases;Integrated

Security=True"

End Function

End Class

CLASE ProductosDA

Imports System.Data

Imports System.Data.SqlClient

Imports ComponenteEntidad

Public Class ProductosDA

Private Shared ReadOnly _instancia As New ProductosDA

Public Shared ReadOnly Property Instancia() As ProductosDA

Get

Return _instancia

End Get

End Property

Public Function ListarTodos() As List(Of Productos)

Dim cnn As New SqlConnection(Conexion.Instancia.cadenaconexion)

cnn.Open()

Dim sqlcmd As New SqlCommand("pa_productos_Listar", cnn)

sqlcmd.CommandType = CommandType.StoredProcedure

Dim PaTable As SqlDataReader = sqlcmd.ExecuteReader

Dim Coleccion As New List(Of Productos)

While PaTable.Read

Coleccion.Add(New Productos(PaTable.Item(0), PaTable.Item(1),

PaTable.Item(2), PaTable.Item(3), PaTable.Item(4)))

End While

cnn.Close()

cnn.Dispose()

Return Coleccion

End Function

Page 4: Carrito de Compras con PHP

Public Function ListarporCodigo(ByVal cod As String) As List(Of

Productos)

Dim dsDatos As New DataSet

Dim cnn As New SqlConnection(Conexion.Instancia.cadenaconexion)

cnn.Open()

Dim sqlcmd As New SqlCommand("pa_productos_buscarcodigo", cnn)

sqlcmd.CommandType = CommandType.StoredProcedure

sqlcmd.Parameters.Add("@codproducto", SqlDbType.Char, 4).Value =

cod

Dim PaTable As SqlDataReader = sqlcmd.ExecuteReader

Dim Coleccion As New List(Of Productos)

While PaTable.Read

Coleccion.Add(New Productos(PaTable.Item(0), PaTable.Item(1),

PaTable.Item(2), PaTable.Item(3), PaTable.Item(4)))

End While

cnn.Close()

cnn.Dispose()

Return Coleccion

End Function

Public Function Insertar(ByVal Productos As Productos) As Boolean

Dim cnn As New SqlConnection(Conexion.Instancia.cadenaconexion)

cnn.Open()

Dim Sqlcmd As New SqlCommand("pa_productos_insertar", cnn)

Sqlcmd.CommandType = CommandType.StoredProcedure

Sqlcmd.Parameters.Add("@codproducto", SqlDbType.Char, 4).Value =

Productos.codproducto

Sqlcmd.Parameters.Add("@desproducto", SqlDbType.VarChar,

40).Value = Productos.descripcion

Sqlcmd.Parameters.Add("@preproducto", SqlDbType.Decimal).Value =

Productos.precio

Sqlcmd.Parameters.Add("@canproducto", SqlDbType.Int).Value =

Productos.cantidad

Sqlcmd.Parameters.Add("@foto", SqlDbType.VarChar, 20).Value =

Productos.foto

Sqlcmd.ExecuteNonQuery()

cnn.Close()

cnn.Dispose()

Return True

End Function

Public Function Editar(ByVal Productos As Productos) As Boolean

Dim cnn As New SqlConnection(Conexion.Instancia.cadenaconexion)

cnn.Open()

Page 5: Carrito de Compras con PHP

Dim Sqlcmd As New SqlCommand("pa_productos_modificar", cnn)

Sqlcmd.CommandType = CommandType.StoredProcedure

Sqlcmd.Parameters.Add("@codlibro", SqlDbType.Char, 4).Value =

Productos.codproducto

Sqlcmd.Parameters.Add("@titulo", SqlDbType.VarChar, 40).Value =

Productos.descripcion

Sqlcmd.Parameters.Add("@precio", SqlDbType.Decimal).Value =

Productos.precio

Sqlcmd.Parameters.Add("@cantidad", SqlDbType.Int).Value =

Productos.cantidad

Sqlcmd.Parameters.Add("@foto", SqlDbType.VarChar, 20).Value =

Productos.foto

Sqlcmd.ExecuteNonQuery()

cnn.Close()

cnn.Dispose()

Return True

End Function

Public Function Eliminar(ByVal codproducto As String) As Boolean

Dim cnn As New SqlConnection(Conexion.Instancia.cadenaconexion)

cnn.Open()

Dim Sqlcmd As New SqlCommand("pa_productos_eliminar", cnn)

Sqlcmd.CommandType = CommandType.StoredProcedure

Sqlcmd.Parameters.Add("@codproducto", SqlDbType.Char, 4).Value =

codproducto

Sqlcmd.ExecuteNonQuery()

cnn.Close()

cnn.Dispose()

Return True

End Function

End Class

COMPONENTEENTIDAD

Public Class Productos

Private _codproducto As String

Private _descripcion As String

Private _precio As Double

Private _cantidad As Integer

Private _foto As String

Public Property codproducto() As String

Get

Return _codproducto

End Get

Set(ByVal value As String)

_codproducto = value

End Set

End Property

Page 6: Carrito de Compras con PHP

Public Property descripcion() As String

Get

Return _descripcion

End Get

Set(ByVal value As String)

_descripcion = value

End Set

End Property

Public Property precio() As Double

Get

Return _precio

End Get

Set(ByVal value As Double)

_precio = value

End Set

End Property

Public Property cantidad() As Integer

Get

Return _cantidad

End Get

Set(ByVal value As Integer)

_cantidad = value

End Set

End Property

Public Property foto() As String

Get

Return _foto

End Get

Set(ByVal value As String)

_foto = value

End Set

End Property

Public Sub New(ByVal ccodproducto As String, ByVal cdesproducto As

String, ByVal cprecio As Double, ByVal ccantidad As Integer, ByVal cfoto

As String)

_codproducto = ccodproducto

_descripcion = cdesproducto

_precio = cprecio

_cantidad = ccantidad

_foto = cfoto

End Sub

End Class

COMPONENTENEGOCIO

Imports ComponenteEntidad

Imports ComponenteDatos

Public Class ProductosCN

Public Sub New()

End Sub

Private Shared ReadOnly _instancia As New ProductosCN

Public Shared ReadOnly Property Instancia() As ProductosCN

Get

Return _instancia

End Get

Page 7: Carrito de Compras con PHP

End Property

Public Function ListarTodos() As List(Of Productos)

Return ProductosDA.Instancia.ListarTodos

End Function

Public Function ListarporCodigo(ByVal cod As String) As List(Of

Productos)

Return ProductosDA.Instancia.ListarporCodigo(cod)

End Function

Public Function Insertar(ByVal Productos As Productos) As Boolean

ProductosDA.Instancia.Insertar(Productos)

End Function

Public Function Editar(ByVal Productos As Productos) As Boolean

ProductosDA.Instancia.Editar(Productos)

End Function

Public Function Eliminar(ByVal codproducto As String) As Boolean

ProductosDA.Instancia.Eliminar(codproducto)

End Function

End Class

CREAR CANASTADS

EN LA PAGINA CATALOGOS

Page 8: Carrito de Compras con PHP

EDIT TEMPLATES

Page 9: Carrito de Compras con PHP

PROPIEDADES DE PRECIOLABEL

CODIGO PAGINA CATALOGOS.ASPX

Imports ComponenteEntidad

Imports ComponenteDatos

Imports ComponenteNegocio

Partial Public Class Catalogo

Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As

System.EventArgs) Handles Me.Load

End Sub

Private Sub DataList1_ItemCommand(ByVal source As Object, ByVal e As

System.Web.UI.WebControls.DataListCommandEventArgs) Handles

DataList1.ItemCommand

Dim cod, des As String

Dim pre As Double

If e.CommandName = "seleccionar" Then

DataList1.SelectedIndex = e.Item.ItemIndex

cod =

CType(DataList1.SelectedItem.FindControl("codproductoLabel"), Label).Text

des =

CType(DataList1.SelectedItem.FindControl("descripcionLabel"), Label).Text

pre =

CType(DataList1.SelectedItem.FindControl("precioLabel"), Label).Text

Page 10: Carrito de Compras con PHP

AgregarIdprod(cod, des, pre)

End If

End Sub

Public Function Producto() As CanastaDS

Dim obj As CanastaDS = CType(Session("Canasta"), CanastaDS)

If obj Is Nothing Then

obj = New CanastaDS()

Session("Canasta") = obj

End If

Return obj

End Function

Public Sub AgregarIdprod(ByVal cod As String, ByVal des As String,

ByVal pre As Double)

Dim obj As CanastaDS = Me.Producto

Dim fila As CanastaDS.CanastaRow = obj.Canasta.NewCanastaRow()

fila.codproducto = cod

fila.desproducto = des

fila.preproducto = pre

fila.canproducto = 1

fila.subtotal = pre * 1

obj.Canasta.Rows.Add(fila)

End Sub

Protected Sub DataList1_SelectedIndexChanged(ByVal sender As Object,

ByVal e As EventArgs) Handles DataList1.SelectedIndexChanged

End Sub

Protected Sub Btnseleccionar_Click(ByVal sender As Object, ByVal e As

EventArgs)

End Sub

Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As

System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click

Response.Redirect("listacomprados.aspx")

End Sub

End Class

EN LA PAGINA LISTACOMPRADOS

Page 11: Carrito de Compras con PHP

<table class="style1">

<tr>

<td bgcolor="#006699">

<asp:Label ID="Label2" runat="server" Font-Bold="True"

ForeColor="White"

Text="Mi Carrito de Compras"></asp:Label>

</td>

</tr>

<tr>

<td class="style2">

<asp:GridView ID="GvwCarrito" runat="server"

AutoGenerateColumns="False"

BackColor="White" BorderColor="#CCCCCC" BorderStyle="None"

BorderWidth="1px"

CellPadding="3" Width="444px">

<RowStyle ForeColor="#000066" />

<Columns>

<asp:TemplateField HeaderText="Quitar">

<ItemTemplate>

<asp:ImageButton ID="ImageButton1" runat="server"

CommandArgument='<%#

Eval("codproducto") %>' CommandName='Borrar'

ImageUrl="~/Imagenes/Eliminar.png" HeaderText="Quitar"

onclick="ImageButton1_Click"/>

</ItemTemplate>

</asp:TemplateField>

Page 12: Carrito de Compras con PHP

<asp:BoundField DataField="codproducto"

HeaderText="Codigo" />

<asp:BoundField DataField="desproducto"

HeaderText="Producto" />

<asp:BoundField DataField="preproducto"

HeaderText="Precio" />

<asp:TemplateField HeaderText="Cantidad" >

<ItemTemplate>

<asp:TextBox ID="TextBox1" runat="server" Text='<%#

Bind("canproducto") %>'

ontextchanged="TextBox1_TextChanged"

Width="80px"></asp:TextBox>

</ItemTemplate>

</asp:TemplateField>

<asp:BoundField DataField="subtotal"

HeaderText="Subtotal" />

</Columns>

<FooterStyle BackColor="White" ForeColor="#000066" />

<PagerStyle BackColor="White" ForeColor="#000066"

HorizontalAlign="Left" />

<SelectedRowStyle BackColor="#669999" Font-Bold="True"

ForeColor="White" />

<HeaderStyle BackColor="#006699" Font-Bold="True"

ForeColor="White" />

</asp:GridView>

</td>

</tr>

<tr>

<td align="right" class="style2">

<asp:Label ID="Label1" runat="server" Text="Subtotal S/." Font-

Bold="True"></asp:Label>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&

nbsp;&nbsp;&nbsp;&nbsp;

<asp:Label ID="Lblsubtotal" runat="server"

Text="Label"></asp:Label>

</td>

</tr>

<tr>

<td align="center" class="style2">

<asp:Button ID="Button1" runat="server" Text="Actualizar Datos"

style="height: 26px" />

<asp:Button ID="Button2" runat="server" Text="Continuar Comprando" />

</td>

</tr>

</table>

Page 13: Carrito de Compras con PHP

CODIGO EN LA PAGINA LISTACOMPRADOS.ASPX

Imports ComponenteEntidad

Imports ComponenteDatos

Imports ComponenteNegocio

Partial Public Class ListaComprados

Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As

System.EventArgs) Handles Me.Load

If Page.IsPostBack = False Then

cargarcarrito()

End If

End Sub

Sub cargarcarrito()

Dim GV As New GridView

GvwCarrito.DataSource = Session("Canasta")

GvwCarrito.DataBind()

Call Button1_Click(Button1, Nothing)

End Sub

Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As

EventArgs)

End Sub

Private Sub GvwCarrito_RowCommand(ByVal sender As Object, ByVal e As

System.Web.UI.WebControls.GridViewCommandEventArgs) Handles

GvwCarrito.RowCommand

Dim DS As New DataSet

DS = CType(Session("Canasta"), DataSet)

Dim DT As New DataTable

DT = DS.Tables("Canasta")

Dim i As Integer

Dim cod, codb As String

If e.CommandName = "Borrar" Then

cod = e.CommandArgument.ToString

For i = 0 To DT.Rows.Count - 1

codb = DT.Rows(i).Item(0).ToString

If codb = cod Then

DT.Rows(i).Delete()

DT.AcceptChanges()

Exit For

End If

Next

End If

cargarcarrito()

End Sub

Protected Sub GvwCarrito_SelectedIndexChanged(ByVal sender As Object,

ByVal e As EventArgs) Handles GvwCarrito.SelectedIndexChanged

End Sub

Page 14: Carrito de Compras con PHP

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As

EventArgs) Handles Button1.Click

Dim i As Integer

Dim total, prec, subtotal As Double

Dim cod, des As String

Dim cant As Integer

Dim obj As CanastaDS = CType(Session("Canasta"), CanastaDS)

For i = 0 To GvwCarrito.Rows.Count - 1

cod = (GvwCarrito.Rows(i).Cells(1).Text)

des = (GvwCarrito.Rows(i).Cells(2).Text)

prec = Double.Parse(GvwCarrito.Rows(i).Cells(3).Text)

cant =

CType(GvwCarrito.Rows(i).Cells(0).FindControl("TextBox1"), TextBox).Text

prec = Double.Parse(GvwCarrito.Rows(i).Cells(3).Text)

subtotal = cant * prec

'Actualiza la canasta

GvwCarrito.Rows(i).Cells(5).Text = subtotal

For Each objDR In obj.Canasta.Rows

If objDR("codproducto") = cod Then

objDR("canproducto") = cant

objDR("subtotal") = subtotal

Exit For

End If

Next

total = total + subtotal

Next

Lblsubtotal.Text = total.ToString("0.00")

End Sub

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As

EventArgs) Handles Button2.Click

Response.Redirect("Catalogo.aspx")

End Sub

Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As

System.Web.UI.ImageClickEventArgs)

End Sub

End Class