<?php



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



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



// | PHP version 4/5                                                      |



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



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



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



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



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



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



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



class UserAddressesController extends AppController {



    var $name = "UserAddress";



    var $helpers = array('Html', 'Form','Pagination');



    var $components = array ('Pagination','Catmenu','History','Email');



    var $uses = array('UserAddress', 'Country','Category','Cart','Product','User','Statecode','Giftcard','Cmspage');



    function beforeRender() {



        //$lst	=	$this->Catmenu->getCatList(1);



        //$this->set('catmenu', $lst);



        //Code Added By shyam for Category Menu Listing starts here..



       // $lst	=	$this->Category->findAll();

        $lst	=	$this->Category->findAll(null, NULL, 'Category.cname ASC', null, null);

        foreach($lst as $det)



            $menu_array[$det["Category"]["id"]] = array('name' => $det["Category"]["cname"],'parent' => $det["Category"]["parent_id"]);



        $this->set('menu_array', $menu_array);


        //Code Added By shyam for Category Menu Listing ends here..
        $footer_links = $this->Cmspage->find('list',array('conditions' => array('Cmspage.type' => 'content','Cmspage.status' => 'Y')));
        $this->set('footerlinks', $footer_links);
        //Code Added By shyam for Category Menu Listing ends here..







        if (isset($this->Setting) && !empty($this->Setting->table)) {



            $this->Setting->load();



        }



        global $sitefolder;



        global $meta_description;



        global $meta_keywords;



        global $page_title;



        $cnfg 				=	Configure::read('Neutrino');



        $sitefolder			=	$cnfg["sitebasefolder"];



        $meta_description	=	$cnfg["meta_description"];



        $meta_keywords		=	$cnfg["meta_keywords"];



        $page_title			=	$cnfg["page_title"];



        global $style;



        $style				=	$cnfg["style"];



        $this->set('style', $style);



        $this->set('meta_description', $meta_description);



        $this->set('meta_keywords', $meta_keywords);



        $this->set('siteurl', $sitefolder);



        $this->pageTitle = $page_title	;



        $this->set('cnfg',$cnfg);

        $this->set('sitefolder', $sitefolder);

        $httpHost	=	$_SERVER['HTTP_HOST'];

        $this->set('sitebase','http://'.$httpHost);
                //get cart count
                 $sus = $this->Session->read();
                if ($this->Session->check('Userlog') === true) {
                   $userid = $sus["Userlog"]["id"];
                    $cartCount = $this->Cart->getCartCount($userid,'user');
                }else {
                   $sessid = $this->Session->id();
                    $cartCount = $this->Cart->getCartCount($sessid,'G');
                }
                $this->set('cartcount',$cartCount);



    }



    function __validateLoginStatus($typ='A') {



        if($typ == 'A') {



            if($this->Session->check('Admin') == false) {



                $this->redirect(array('controller' => 'admins', 'action' => 'login'));



                $this->Session->setFlash('The URL you\'ve followed requires you login.');



            }



        }else {



            if($this->Session->check('Userlog') == false) {



                $this->redirect(array('controller' => 'users', 'action' => 'login'));



                $this->Session->setFlash('The URL you\'ve followed requires you login.');



            }



        }



    }



    function show_bill_ship() {

        $this->__validateLoginStatus('U');

        $sus			=	$this->Session->read('Userlog');
        
        $this->set('sus', $sus);

        $this->set('utype', $sus["usertype"]);

        $user_id		=	$sus["id"];

        $cnfg 				=	Configure::read('Neutrino');

        $i	=0;

        //print_r($cnfg);exit;

        $shipmeth=array();



        if($cnfg["shipping_require_australia"]	==	"Y") {



            $shipmeth[$i]	=	"Australiapost";



            $i++;
            


        }



        if($cnfg["shipping_require_canada"]	==	"Y") {



            $shipmeth[$i]	=	"Canadapost";



            $i++;



        }



        if($cnfg["shipping_require_fedex"]	==	"Y") {



            $shipmeth[$i]	=	"FEDEX";



            $i++;



        }



        if($cnfg["shipping_require"]	==	"Y") {



            $shipmeth[$i]	=	"UPS";



            $i++;



        }



        if($cnfg["shipping_require_usps"]	==	"Y") {



            $shipmeth[$i]	=	"USPS";



            $i++;



        }



        if($cnfg["fflat_shipping_require"]	==	"Y") {



            $shipmeth[$i]	=	$cnfg["flat_ship_name"];



            $i++;



        }



        if($cnfg["sflat_shipping_require"]	==	"Y") {



            $shipmeth[$i]	=	$cnfg["secflat_ship_name"];



            $i++;



        }



        if($cnfg["tflat_shipping_require"]	==	"Y") {



            $shipmeth[$i]	=	$cnfg["thirdflat_ship_name"];



            $i++;



        }



        if($cnfg["foflat_shipping_require"]	==	"Y") {



            $shipmeth[$i]	=	$cnfg["forthflat_ship_name"];



            $i++;



        }



        /*



		if($_SERVER['REQUEST_METHOD']=='POST'){



			$ship["fname"]		=	$_POST["sfname"];



			$ship["lname"]		=	$_POST["slname"];



			$ship["address"]	=	$_POST["saddress"];



			//$ship["state"]		=	$_POST["sstate"];

			//Setting states for US & other Countries



			$ship["country"]	=	$_POST["data"]["scountry"];



			if($_POST["data"]["scountry"] == 'US')

			{

			  $ship["state"] = $_POST["us_sstate"];

			}

			else

			{

			  $ship["state"]		=	$_POST["sstate"];

			  unset($_POST["us_sstate"]);

			}



            $ship["city"]		=	$_POST["scity"];



			$ship["zip"]		=	$_POST["szip"];



			$ship["phone"]		=	$_POST["sphone"];



			$ship["fax"]		=	$_POST["sfax"];



			$ship["mobile"]		=	$_POST["smobile"];



			$ship["user_id"]	=	$user_id;



			$ship["addr_type"]	=	"Shipping";



			$this->UserAddress->deleteAll("UserAddress.user_id = ".$user_id." AND UserAddress.addr_type = 'Shipping'");



			$this->UserAddress->create($data = array());



			$data				=	$ship;



			$this->UserAddress->save($data);



			$bill["email"]		=	$_POST["bemail"];



			$bill["fname"]		=	$_POST["bfname"];



			$bill["lname"]		=	$_POST["blname"];



			$bill["address"]	=	$_POST["baddress"];



			$bill["state"]		=	$_POST["bstate"];



			$bill["country"]	=	$_POST["data"]["bcountry"];



			if($_POST["data"]["bcountry"] == 'US')

			{

			  $bill["state"] = $_POST["us_bstate"];

			}

			else

			{

			  $bill["state"]		=	$_POST["bstate"];

			  unset($_POST["us_bstate"]);

			}



            $bill["city"]		=	$_POST["bcity"];



			$bill["zip"]		=	$_POST["bzip"];



			$bill["phone"]		=	$_POST["bphone"];



			$bill["fax"]		=	$_POST["bfax"];



			$bill["mobile"]		=	$_POST["bmobile"];



			$bill["user_id"]	=	$user_id;



			$bill["addr_type"]	=	"Billing";



			$this->UserAddress->deleteAll("UserAddress.user_id = ".$user_id." AND UserAddress.addr_type = 'Billing'");



			$this->UserAddress->create($data = array());



			$data				=	$bill;



			$this->UserAddress->save($data);







			/* updating Email Address for Guest in users table...



			if($sus["reg_status"]=='G')



			{



			  $this->Cart->updateGuestEmail($user_id,trim($_POST["bemail"]));



			  $sus["email"]=$_POST["bemail"];



			  $this->Session->write('Userlog', $sus);



			  //Sending login details mail to the Guest User starts here..



			        $email_dups = $this->User->find('count', array('conditions' => array('User.email' => trim($_POST["bemail"]))));



                    if($email_dups  < 1)



					{



						$cnfg 			=	Configure::read('Neutrino');



						$subject 		= "New login details at ".$cnfg["site_name"];



						$headers 		= "From: ".$cnfg["site_name"]."<".$cnfg["admin_email"].">\r\n";



						$headers 		.= "MIME-Version: 1.0\r\n";



						$headers 		.= "Content-type: text/html; charset=iso-8859-1\r\n";



						$mailcontent='<table width="27%" border="0" align="center">



								<tr><td colspan="3" align="center"><font color="#CC6600"><strong>Vista cart Login Details</strong></font></td></td></tr>



								<tr><td width="27%"><strong>Username</strong></td><td><strong>:</strong></td><td width="73%">'.$this->Session->read('guestuser').'</td></tr>



								<tr><td><strong>Password</strong></td><td><strong>:</strong></td><td width="73%">'.$this->Session->read('guestpswd').'</td></tr>



								</table>';



						$mailsent 		= mail($_POST["bemail"],$subject,$mailcontent,$headers);



						$this->Session->delete('guestuser');$this->Session->delete('guestpswd');



				  }



				//Sending login details mail to the Guest User ends here..



			}



			$this->redirect(array('controller'=>'carts','action'=>'checkout/'.$_POST["shipmeth"]));

		}

        */

        //updating the cart table starts here...

        $sessid	=	$this->Session->id();

        $this->Cart->updateCartuserid($sessid,$user_id);



        $citem			=	$this->Cart->findAllByUserId($user_id);



        foreach($citem as $cval) {

            $cart_id=$cval["Cart"]["id"];

            $prod_id=$cval["Cart"]["product_id"];

            $prodet=$this->Product->findById($prod_id);

            if($prodet["Product"]["wholesale_price"] == 0) {

                $wholesaleprice=$prodet["Product"]["price"];
            }

            else {

                $wholesaleprice=$prodet["Product"]["wholesale_price"];
            }

            $retailprice=$prodet["Product"]["price"];



            if($sus["usertype"]=='wholesale') {

                if($cval["Cart"]["type"]=='P') {

                    $this->Cart->id = $cart_id;

                    $this->Cart->saveField('product_price',$wholesaleprice);

                }

                else  if($cval["Cart"]["type"]=='G') {

                    $gdet=$this->Giftcard->findById($prod_id);

                    $wholesaleprice=$gdet["Giftcard"]["gift_amnt"];

                }

            }

            else if($sus["usertype"]=='retail') {

                if($cval["Cart"]["type"]=='P') {

                    $this->Cart->id = $cart_id;

                    $this->Cart->saveField('product_price',$retailprice);

                }

                else   if($cval["Cart"]["type"]=='G') {

                    $gdet=$this->Giftcard->findById($prod_id);

                    $retailprice=$gdet["Giftcard"]["gift_amnt"];

                }

            }

        }

        //updating the cart table ends here..

        //Setting the product type
        $citem1			=	$this->Cart->findAllByUserId($user_id);
        $this->set('prodtype', "Non-digital");
        if(!empty($citem1)) {
            foreach($citem1 as $it) {
                if(trim($it["Cart"]["prodtype"]) == "Digital") {
                    $this->set('prodtype', "Digital");
                }else if(trim($it["Cart"]["type"]) == "G") {
                    $this->set('prodtype', "Digital");
                }
            }
        }
       
        $data	=	$this->UserAddress->findAllByUserId($user_id);
                        
        $row   =           $this->User->findById($user_id);
      
       
                        
        $bill	=	array();
        $ship	=	array();

        $email = $row['User']['email'];;

        foreach($data  as $add) {



            if($add["UserAddress"]["addr_type"] 	== 	"Billing") {



                $bill["email"]		=	$add["UserAddress"]["email"];



                $bill["fname"]		=	$add["UserAddress"]["fname"];



                $bill["lname"]		=	$add["UserAddress"]["lname"];



                $bill["address"]	=	$add["UserAddress"]["address"];



                $bill["city"]		=	$add["UserAddress"]["city"];



                $bill["state"]		=	$add["UserAddress"]["state"];



                $bill["country"]	=	$add["UserAddress"]["country"];



                $bill["zip"]		=	$add["UserAddress"]["zip"];



                $bill["phone"]		=	$add["UserAddress"]["phone"];



                $bill["fax"]		=	$add["UserAddress"]["fax"];



                $bill["mobile"]		=	$add["UserAddress"]["mobile"];



            }



            if($add["UserAddress"]["addr_type"] 	== 	"Shipping") {



                $ship["fname"]		=	$add["UserAddress"]["fname"];



                $ship["lname"]		=	$add["UserAddress"]["lname"];



                $ship["address"]	=	$add["UserAddress"]["address"];



                $ship["city"]		=	$add["UserAddress"]["city"];



                $ship["state"]		=	$add["UserAddress"]["state"];



                $ship["country"]	=	$add["UserAddress"]["country"];



                $ship["zip"]		=	$add["UserAddress"]["zip"];



                $ship["phone"]		=	$add["UserAddress"]["phone"];



                $ship["fax"]		=	$add["UserAddress"]["fax"];



                $ship["mobile"]		=	$add["UserAddress"]["mobile"];



            }



        }

       // print '<pre />';print_r($bill);exit;

        $countrylist	=	$this->Country->find('list',array('fields'=>array('Country.country_iso_code_2','Country.country_name')));



        $this->set('countrylist', $countrylist);



        $this->set('ship', $ship);



        $this->set('shipmethods', $shipmeth);



        $this->set('bill', $bill);
        
        $this->set('email', $email);



        //Setting the US States dtails

        $us_cntrydet = $this->Country->find("Country.country_iso_code_2 ='US'");

        if(isset($us_cntrydet["Country"]["country_id"])) {

            $us_states	=	$this->Statecode->findAll("Statecode.country_id ='".$us_cntrydet["Country"]["country_id"]."'");

            $this->set('us_states', $us_states);

        }

        //Setting the US States dtails



    }



    function myshippadd($uid) {



        $this->__validateLoginStatus('U');



        $sus		=	$this->Session->read('Userlog');



        $this->set('sus', $sus);



        $user_id	=	$sus["id"];



        $shipadd	=	$this->UserAddress->find("UserAddress.user_id =".$user_id." AND UserAddress.addr_type ='Shipping'");



        $countrylist	=	$this->Country->find('list',array('fields'=>array('Country.country_iso_code_2','Country.country_name')));



        $this->set('countrylist', $countrylist);



        $this->set('ship', $shipadd["UserAddress"]);



        $this->set('user_id', $user_id);



        //Setting the US States dtails

        $us_cntrydet = $this->Country->find("Country.country_iso_code_2 ='US'");

        if(isset($us_cntrydet["Country"]["country_id"])) {

            $us_states	=	$this->Statecode->findAll("Statecode.country_id ='".$us_cntrydet["Country"]["country_id"]."'");

            $this->set('us_states', $us_states);

        }

        //Setting the US States dtails



        if($_SERVER['REQUEST_METHOD'] == "POST") {



            $this->UserAddress->deleteAll("UserAddress.user_id = ".$user_id." AND UserAddress.addr_type = 'Shipping'");



            $_POST["country"]	=	$_POST["data"]["country"];



            //Setting states for US & other Countries

            if($_POST["country"] == 'US') {

                $_POST["state"] = $_POST["us_state"];

            }

            else {

                unset($_POST["us_state"]);

            }



            unset($_POST["data"]);



            $data				=	$_POST;



            if($this->UserAddress->save($data)) {



                $this->Session->setFlash('Your Shipping Address has been updated Successfully!');



                $this->redirect(array('controller' => 'users', 'action' => 'myaccount'));



                exit;



            }



        }



    }

    function mybilladd($uid) {

        $this->__validateLoginStatus('U');



        $sus		=	$this->Session->read('Userlog');



        $this->set('sus', $sus);



        $user_id	=	$sus["id"];



        $billadd	=	$this->UserAddress->find("UserAddress.user_id =".$user_id." AND UserAddress.addr_type ='Billing'");



        $countrylist	=	$this->Country->find('list',array('fields'=>array('Country.country_iso_code_2','Country.country_name')));



        $this->set('countrylist', $countrylist);



        $this->set('bill', $billadd["UserAddress"]);



        $this->set('user_id', $user_id);



        //Setting the US States dtails

        $us_cntrydet = $this->Country->find("Country.country_iso_code_2 ='US'");

        if(isset($us_cntrydet["Country"]["country_id"])) {

            $us_states	=	$this->Statecode->findAll("Statecode.country_id ='".$us_cntrydet["Country"]["country_id"]."'");

            $this->set('us_states', $us_states);

        }

        //Setting the US States dtails



        if($_SERVER['REQUEST_METHOD'] == "POST") {



            $this->UserAddress->deleteAll("UserAddress.user_id = ".$user_id." AND UserAddress.addr_type = 'Billing'");

            $_POST["country"]	=	$_POST["data"]["country"];



            //Setting states for US & other Countries

            if($_POST["country"] == 'US') {

                $_POST["state"] = $_POST["us_state"];

            }

            else {

                unset($_POST["us_state"]);

            }

            unset($_POST["data"]);



            $data				=	$_POST;



            if($this->UserAddress->save($data)) {



                $this->Session->setFlash('Your Billing Address has been updated Successfully!');



                $this->redirect(array('controller' => 'users', 'action' => 'myaccount'));



                exit;



            }



        }



    }



}



