|
Server : Apache/2.4.62 System : FreeBSD fbsdweb2.web.rcn.net 14.1-RELEASE FreeBSD 14.1-RELEASE releng/14.1-n267679-10e31f0946d8 GENERIC amd64 User : www ( 80) PHP Version : 8.3.8 Disable Function : NONE Directory : /domains/logicswapweb/aplicaciones/admon/carrito/fnc/ |
Upload File : |
<?
//Carrito de compras
class Carrito extends Componente{
var $items; /* array de accesorios */
var $index; /* indice para el array items*/
function Carrito() {
$this->items = array();
$this->index = 0;
}
function existeEnCarrito($id_pro){
$existe = false;
$cantidad = 1;
if($this->items && $this->index > 0)
{
for($i = 0; $i < $this->index; $i++)
{
if($this->items[$i]['id_pro'] == $id_pro)
{
$this->items[$i]['cantidad'] += $cantidad;
$existe=true;
break;
}
}
}
return($existe);
}
function agregar($id_pro, $cantidad, $tipo, $parent) {
//Verifica que el producto ya este agregado e incrementa la cantidad
$existe = false;
if($this->items && $this->index > 0){
for($i = 0; $i < $this->index; $i++){
if($this->items[$i]['id_pro'] == $id_pro){
$this->items[$i]['cantidad'] += $cantidad;
$existe=true;
break;
}
}
}
if($existe==false)
{
if($tipo=='p'){
//Agrego el Producto al carro de compras
$this->items[$this->index]['id_pro'] = $id_pro;
$this->items[$this->index]['cantidad'] = $cantidad;
$this->items[$this->index]['tipo'] = $tipo;
$this->items[$this->index]['parent'] = $parent;
$this->index++;
//Busco y agrego el Sub-Producto respectivo
// verifico que el subProducto exista.
$subProducto = $this->getSubProducto($id_pro);
if ($subProducto['id_subproducto'] > 0){
$esta = $this->existeEnCarrito($subProducto['id_subproducto']);
if(!$esta){
$this->items[$this->index]['id_pro'] = $subProducto['id_subproducto'];
$this->items[$this->index]['cantidad'] = $cantidad;
$this->items[$this->index]['tipo'] = 's';
$this->items[$this->index]['parent'] = $subProducto['parent'];
$this->index++;
}
}
}else{
// Si es un subproducto, solo lo agrego. Si solo deseo el mantenimiento.
$this->items[$this->index]['id_pro'] = $id_pro;
$this->items[$this->index]['cantidad'] = $cantidad;
$this->items[$this->index]['tipo'] = $tipo;
$this->items[$this->index]['parent'] = $parent;
$this->index++;
}
}else{
//Si existe
if($this->items[$i]['tipo']=='s'){
//$this->mostrarMensaje($this->items[$i]['id_pro']);
//$this->mostrarMensaje($cantidad);
$this->items[$i]['cantidad'] = $this->items[$i-1]['cantidad'];
}
}
return true;
}
function actualiza($id_pro, $cantidad, $tipo, $parent) {
//Verifica que el producto ya este agregado e incrementa la cantidad
$existe = false;
if($this->items && $this->index > 0){
for($i = 0; $i < $this->index; $i++){
if($this->items[$i]['id_pro'] == $id_pro){
$this->items[$i]['cantidad'] += $cantidad;
$existe=true;
break;
}
}
}
if($existe==false)
{
//Agrego el Producto al carro de compras
$this->items[$this->index]['id_pro'] = $id_pro;
$this->items[$this->index]['cantidad'] = $cantidad;
$this->items[$this->index]['tipo'] = $tipo;
$this->items[$this->index]['parent'] = $parent;
$this->index++;
}
return true;
}
function del_productos()
{
$this->items = '';
$this->index = 0;
}
function agregarProductos($prods, $cant, $tipo, $parent, $quitar)
{
$p = count($prods);
$c = count($cant);
if($quitar)
$q = count($quitar);
else
$q=0;
if($p != $c)
return false;
for($i = 0; $i < $p; $i++)
{
$quit = false;
if($q > 0)
{
for($j = 0; $j < $q; $j++)
{
if($quitar[$j] == $prods[$i])
{
$quit = true;
}
}
}
if($quit == false)
{
$this->actualiza($prods[$i], $cant[$i], $tipo[$i], $parent[$i]);
}
}
}
function agregarProductosUpdate($prods, $cant, $tipo, $parent, $quitar)
{
$p = count($prods);
$c = count($cant);
if($quitar)
$q = count($quitar);
else
$q=0;
if($p != $c)
return false;
for($i = 0; $i < $p; $i++)
{
$quit = false;
if($q > 0)
{
for($j = 0; $j < $q; $j++)
{
if($quitar[$j] == $prods[$i])
{
$quit = true;
}
}
}
if($quit == false)
{
$this->agregar($prods[$i], $cant[$i], $tipo[$i], $parent[$i]);
}
}
}
function getSubProducto($id_producto){
$db = new DBManager('MySql');
$db->connect();
$sql_sub = "SELECT id,parent from catalogo_producto WHERE parent='$id_producto'";
$db->execute($sql_sub);
list($id_s,$parent)=$db->fetchRow();
$data['id_subproducto'] =$id_s;
$data['parent'] =$parent;
return $data;
}
}
?>