<?php
/* **
 * @application PhpMyShopping
 * @part        Boutique
 * @file        sauver_adresse.php
 * @path        ./
 * @author      Stephane DINE <stephane@phpmyshopping.org>
 * @site        http://www.phpmyshopping.org
 * @license     http://www.php.net/license/3_0.txt  PHP License 3.0
  *
 * PHP version 5
 *
 * Enregistrement des modifications apportees a l'adresse du client
 */

session_start();
require_once './admin/include/class.customer.php';
$cust = new Customer();
$cust->log(__FILE__, array('action' => 'Ouverture fichier'));

/**
 * Modification des donnees de l'adresse
 */
if (($_POST['C_FirstName'])
	and ($_POST['C_LastName'])
	and ($_POST['C_Email'])
	and ($_POST['C_Password'])
	and ($_POST['B_Street'])) {

	/**
	 * Preparation des donnees
	 */
	$data['ID_Gender']   = $_POST['C_ID_Gender'];
	$data['FirstName']   = $_POST['C_FirstName'];
	$data['LastName']    = $_POST['C_LastName'];
	$data['Telephone']   = $_POST['C_Telephone'];
	$data['Gsm']         = $_POST['C_Gsm'];
	$data['Email']       = $_POST['C_Email'];
	$data['Password']    = $_POST['C_Password'];
	$data['Status']      = 'A';
	$data['B_ID_Gender'] = $_POST['C_ID_Gender'];
	$data['B_FirstName'] = $_POST['C_FirstName'];
	$data['B_LastName']  = $_POST['C_LastName'];
	$data['B_Street']    = $_POST['B_Street'];

	/**
	 * Verification si une nouvelle ville a ete renseignee pour l'adresse de facturation
	 */
	if (($_POST['B_NewCityName']) and ($_POST['B_NewPostCode'])) {
		/**
		 * Insertion de la nouvelle ville
		 */
		$cityB['ID_State'] = $_POST['B_ID_State'];
		$cityB['CityName'] = ucwords($_POST['B_NewCityName']);
		$cityB['PostCode'] = $_POST['B_NewPostCode'];
		$data['B_ID_City'] = $cust->insert_sql('City', $cityB);
        $cust->log(__FILE__, array('action' => 'Creation ville', 'variable' => array('$cityB[\'CityName\']' => $cityB['CityName'])));
	} else {
		$data['B_ID_City'] = $_POST['B_ID_City'];
	}

	if ((($_POST['D_ID_City'] == 'X') or (!isset($_POST['D_ID_City'])))
		and (!$_POST['D_FirstName'])
		and (!$_POST['D_LastName'])
		and (!$_POST['D_Street'])
	   ) {
		/**
		 * Adresse de livraison non renseignee, on prend l'adresse de facturation pour definir celle-ci
		 */
		$data['D_ID_Gender'] = $_POST['C_ID_Gender'];
		$data['D_FirstName'] = $_POST['C_FirstName'];
		$data['D_LastName']  = $_POST['C_LastName'];
		$data['D_Street']    = $_POST['B_Street'];
		$data['D_ID_City']   = $data['B_ID_City'];
	} else {
		/**
		 * Adresse de livraison renseignee
		 *
		 * Verification si une nouvelle ville a ete renseignee pour l'adresse de livraison
		 */
		if (($_POST['D_NewCityName']) and ($_POST['D_NewPostCode'])) {
			/**
			 * Insertion de la nouvelle ville
			 */
			$cityD['ID_State'] = $_POST['D_ID_State'];
			$cityD['CityName'] = ucwords($_POST['D_NewCityName']);
			$cityD['PostCode'] = $_POST['D_NewPostCode'];
			$data['D_ID_City'] = $cust->insert_sql('City', $cityD);
            $cust->log(__FILE__, array('action' => 'Creation ville', 'variable' => array('$cityD[\'CityName\']' => $cityD['CityName'])));
		} else {
			$data['D_ID_City'] = $_POST['D_ID_City'];
		}

		$data['D_ID_Gender'] = $_POST['D_ID_Gender'];
		$data['D_FirstName'] = $_POST['D_FirstName'];
		$data['D_LastName']  = $_POST['D_LastName'];
		$data['D_Street']    = $_POST['D_Street'];
	}

	/**
	 * Enregistrement du client
	 */
    $data['IDB'] = $_SESSION['IDB'];
	$result = $cust->addCustomer($data);

	if ($result == 'error') {
        $cust->log(__FILE__, array('message' => 'Erreur > Redirection vers saisie_adresse.php'));
        header('Location: ./saisie_adresse.php');
	} else {
		/**
		 * Auto-login du client apres enregistrement de celui-ci
		 */
		$ident['IDC']      = $_SESSION['IDC'];
		$ident['IDB']      = $_SESSION['IDB'];
		$ident['Email']    = trim(strtolower($data['Email']));
		$ident['Password'] = trim($data['Password']);

		$ID_Customer = $cust->verrifIdentCustomer($ident);

        $sql = 'SELECT GenderName, FirstName, LastName FROM Customer LEFT JOIN Gender ON Gender.ID_Gender = Customer.ID_Gender WHERE ID_Customer = '.$ID_Customer;
        $query = $cust->query($sql);
        $result = mysql_fetch_array($query);

        $_SESSION['IDI'] = 1;
        $_SESSION['IDC'] = $ID_Customer;
        $_SESSION['CUST_GN'] = $result['GenderName'];
        $_SESSION['CUST_FN'] = $result['FirstName'];
        $_SESSION['CUST_LN'] = $result['LastName'];

		header('Location: ./redirect.php?r=cfcmd');
	}
} else {
	header('Location: ./saisie_adresse.php');
}
?>
