<?php


function addclient(){

   $buys =  db_get("SELECT `buy_id`, `buy_email`, `date_start` FROM `anbuy`");

   foreach($buys as $buy){

        if ($buy['date_start'] == '0000-00-00 00:00:00')
            $clientdate = date('Y-m-d G:i:s');
        else
            $clientdate = $buy['date_start'];
    

        $clientmail = strtolower(trim($buy['buy_email']));    
        $clienthash = md5($clientmail);

        $client = db_get("SELECT `client_id`, `clientcard`, `clientdate` FROM `clients` WHERE `clientmail` = '$clientmail' LIMIT 1");
    

        if (isset($client['client_id'])){
        
            if ($client['clientdate'] == '0000-00-00 00:00:00'){
                $updsql = "UPDATE `clients` SET `clientdate` = '$clientdate'  WHERE `client_id` = '{$client['client_id']}'";
                db_get($updsql, 'writer');
                echo "$updsql \n";
            }

            if ($client['clienthash'] == '' ){
                $updsql = "UPDATE `clients` SET `clienthash` = '$clienthash'  WHERE `client_id` = '{$client['client_id']}'";
                db_get($updsql, 'writer');
                echo "$updsql \n";
            }

            if ($buy['client_id'] == 0){
                $client_id = $buy['client_id'];
                $updsql = "UPDATE `clients` SET `client_id` = '$clienthash'  WHERE `client_id` = '{$client['client_id']}'";
            }


        }
        else {
        
            $inssql = "INSERT INTO `clients` (clientmail, clientdate, clienthash) VALUES ('$clientmail', '$clientdate', '$clienthash')";
            db_get($inssql, 'writer');
            echo "$inssql \n";
        }
   }    

}


function addclientid(){

    $buys =  db_get("SELECT `buy_id`, `buy_email` FROM `anbuy` WHERE `client_id` = 0");

    foreach ($buys as $buy){
        $clientmail = strtolower(trim($buy['buy_email'])); 
        $client = db_get("SELECT `client_id` FROM `clients` WHERE `clientmail` = '$clientmail' LIMIT 1");
        if (isset($client['client_id'])){
            $updsql = "UPDATE `anbuy` SET `client_id` = '{$client['client_id']}'  WHERE `buy_id` = '{$buy['buy_id']}'";
            echo "$updsql\n";
            db_get($updsql);
        }    

    }    
}


// номер клубной карты
function addcard($id, $date) {

    $year = substr($date, 0, 4);
    $number = str_pad($id, 9, '0', STR_PAD_LEFT);

    return "$year$number";


}


function mybooks_kuri($category = 'gazeta', $filter = 'all', $page = 1){

    $login = check_login_kuri();


    if (!isset($login['client_id'])){
       header('Location: '.SITE.'login');
       exit;
    }
    $type='mybooks';
    if ($category == 'gazeta')
        $category_id = 1;
    elseif ($category == 'books')
        $category_id = 3;   
    
    $limit = 25;
    $client = api_client_kuri($login['clienthash']);
    $baseurl = SITE.'mybooks/'.$category.'/'.$filter.'/';

    if (!$client) {
        echo 'Пользователь не найден';
        return;
    }

    $items = clientbuys_kuri($client['client_id'], $page, $category_id, $client['clientgroup_id'], $filter);
    $count = $items['count'];
    
    if ($count > $limit){
        $pages = ceil($count / $limit);
    }

    if ($page > 1) {
        $title = "Моя библиотека - страница $page";
        $prevpage = $page - 1 ;
        $prevurl = $baseurl.$prevpage;
    }    
    else {
        $title = "Моя библиотека";  
    }  

  

    if ($pages > $page){
        $nextpage = $page + 1;
        $nexturl = $baseurl.$nextpage;
        $endurl = $baseurl.$pages;
    }    
        
    $tempcontent = VIEWPATH.'allbooks.phtml';
    include VIEWPATH.'layout.phtml';


}



//все покупки
function payments_kuri($filter = 1 ){

    $login = check_login_kuri();

    if (!isset($login['client_id'])){
        header('Location: '.SITE.'login');
        exit;
    }

    $paysql = "
        SELECT 
            buy_id, date, anbuy.price AS price, buy_status, pricename, category_id, pricehash
        FROM 
            anbuy 
        LEFT JOIN price ON
            anbuy.number_id = price.price_id    
        WHERE 
                client_id = {$login['client_id']}
            AND
                buy_status = $filter
        ORDER BY  
            buy_id DESC  
    ";

    $payments = db_get($paysql);

    $title = 'Мои покупки';

    $tempcontent = VIEWPATH.'payments.phtml';
    include VIEWPATH.'layout.phtml';





}



