<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: 			*/

// +----------------------------------------------------------------------+

// | PHP version 4/5                                                      |

// +----------------------------------------------------------------------+

// | Copyright (c) 2004-2010 ARMIA INC                                    |

// +----------------------------------------------------------------------+

// | This source file is a part of iScripts VistaCart                     |

// +----------------------------------------------------------------------+

// | Authors: Jipson<jipson.thomas@armiasystems.com>             		  |

// +----------------------------------------------------------------------+

class Cart extends AppModel

{

   var $name = 'Cart';

   var $belongsTo = array('Product','User'); 

   ###### Function to update quantity in cart........

   function update($id,$qty){

   		$Db = ConnectionManager::getDataSource($this->useDbConfig);

		$crtbl = $Db->fullTableName('carts');

		$this->query("UPDATE $crtbl SET quantity = ".$qty." WHERE id = ".$id);

		return true;

   }

   function updateCartuserid($sessid,$userid){

   		$Db = ConnectionManager::getDataSource($this->useDbConfig);

		$crtbl = $Db->fullTableName('carts');

		$this->query("UPDATE $crtbl SET user_id = '".$userid."' WHERE sess_id = '".$sessid."'");

		return true;

   }

    function updateGuestEmail($userid,$email){

   		$Db = ConnectionManager::getDataSource($this->useDbConfig);

		$usrtbl = $Db->fullTableName('users');

		$this->query("UPDATE $usrtbl SET email = '".$email."' WHERE id = '".$userid."'");

		return true;

   }

   function updateGuestUserName($userid,$name){

      $Db = ConnectionManager::getDataSource($this->useDbConfig);

    $usrtbl = $Db->fullTableName('users');

    $this->query("UPDATE $usrtbl SET uname = '".$name."' WHERE id = '".$userid."'");

    return true;

   }



   function updatesessuser($sessid,$userid)

    {

     	$Db = ConnectionManager::getDataSource($this->useDbConfig);

		$crtbl = $Db->fullTableName('carts');

		$this->query("UPDATE $crtbl SET user_id = '".$userid."' WHERE sess_id = '".$sessid."' AND user_id = 0");

		return true;

    }

	

	function getTableName($tablename)

		 {

			$Db = ConnectionManager::getDataSource($this->useDbConfig);

			$tblnme = $Db->fullTableName($tablename);			

			$tblnme=trim(str_replace('`','',$tblnme));		

			return $tblnme;

		}

	

	function getCartCount($userid = 0, $userType = 'user')
        {
         if ($userType == 'user') {

             $citem = $this->find('all', array('conditions' => array('user_id' => $userid)));
            
            }else {
                $citem = $this->find('all', array('conditions' => array('sess_id' => $userid)));

            }
        return  count($citem) ;//+ count($gitem);


        }

}

?>