<?php
/* **
 * @application PhpMyShopping
 * @part        Boutique
 * @file        save_customer_data.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 donnees client
 */

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

if ((isset($_POST['Type'])) and ($_POST['Type'])) {
    switch($_POST['Type']) {
    case 'C':
        $data['ID_Gender'] = $_POST['ID_Gender'];
        $data['FirstName'] = strtoupper($_POST['FirstName']);
        $data['LastName']  = ucwords(strtolower($_POST['LastName']));
        $data['Telephone'] = $_POST['Telephone'];
        $data['Gsm']       = $_POST['Gsm'];
        $data['Email']     = strtolower($_POST['Email']);

        //$_SESSION['CUST_GN'] = $data['GenderName'];
        $_SESSION['CUST_FN'] = $data['FirstName'];
        $_SESSION['CUST_LN'] = $data['LastName'];

        if ((($_POST['Password']) and ($_POST['Password_confirm'])) and ($_POST['Password'] == $_POST['Password_confirm'])) {
            $data['Password'] = $_POST['Password'];
        }

        $shop->update_sql('Customer', $data, $_SESSION['IDC'], 'SHA1');
        header('Location: compte.php?msg=ok');
        break;

    case 'D':
    case 'B':
        if ($_POST['Type'] == 'B') {
            $ID_City = $_POST['B_ID_City'];
        } else {
            $ID_City = $_POST['D_ID_City'];
        }

        if ($ID_City == 'X') {
            header('Location: compte.php?msg=er');
        } else {
            $sql = 'UPDATE CustomerAddress
                    SET ID_City   = '.$ID_City.',
                        ID_Gender = '.$_POST['ID_Gender'].',
                        FirstName = "'.strtoupper($_POST['FirstName']).'",
                        LastName  = "'.ucwords(strtolower($_POST['LastName'])).'",
                        Street    = "'.$_POST['Street'].'"
                    WHERE ID_Customer = '.$_SESSION['IDC'].'
                    AND AddressType = "'.$_POST['Type'].'"';

            $shop->query($sql);

            if ($_POST['Step'] == 'ca') {
                header('Location: adresse.php');
            } else {
                header('Location: compte.php?msg=ok');
            }
        }

        break;

    default:
        header('Location: compte.php?msg=er');
    }
} else {
    header('Location: compte.php?msg=er');
}
?>
