<?php
//Dostava
ini_set("display_errors",1);
include_once ("_dirinfo.php");
include_once (PATH_TO_ROOT."init.php");
include_once (PATH_TO_ROOT."config.php");
include_once (PATH_TO_ROOT.'CRM/class/class.logger.php');
include_once(PATH_TO_ROOT."CRM/class/class.checkString.php");
$Logger = new LoggerX($db);

if ($_POST['action'] == 'addNote') {
	$sql = "INSERT INTO ".WARPIT_CRM."._statusLog (log_type, log_user, user_type, id_order, status_date, status_state, status_comment) 
			VALUES (1, ".intval($_SESSION['UserInfo']['id']).", '".$_SESSION['UserInfo']['user_type']."', '".StringChecker::Check($_POST['idOrder'])."', NOW(), '".StringChecker::Check($_POST['currentState'])."', '".StringChecker::Check($_POST['opomba'])."')";
	$Logger->Log('statusLog', 'addNote', 'log_type, log_user, user_type, id_order, status_date, status_state, status_comment', 
				  WARPIT_CRM."._statusLog", " ", addslashes($sql));		
	$db->SQLexecute($sql);
	
	$sql = "UPDATE  ".WARPIT_CRM."._visitStatistic SET order_status = ".StringChecker::Check($_POST['currentState']).", documentModify = NOW() WHERE id = ".StringChecker::Check($_POST['idOrder']);
	$Logger->Log('sprememba_statusa_naročila', 'single-field-update', 'order_status', WARPIT_CRM."._visitStatistic", StringChecker::Check($_POST['oldValue']), addslashes($sql));
	$db->SQLexecute($sql);
	
	echo "{success: true}";
}
elseif ($_POST['action'] == 'changeDate') {
	$tmp = strtotime(StringChecker::Check($_POST['datumDostave']));
	$date = date('Y-m-d 00:00:00', $tmp);
	
	$sql = "INSERT INTO ".WARPIT_CRM."._statusLog (log_type, log_user, user_type, id_order, status_date, status_state, status_comment, status_changed_date) 
			VALUES (1, ".intval($_SESSION['UserInfo']['id']).", '".$_SESSION['UserInfo']['user_type']."', '".StringChecker::Check($_POST['idOrder'])."', NOW(), '".StringChecker::Check($_POST['currentState'])."', '".StringChecker::Check($_POST['opomba'])."', '".$date."')";
	$Logger->Log('statusLog', 'changeDate', 'log_type, log_user, user_type, id_order, status_date, status_state, status_comment, status_changed_date', 
		  		  WARPIT_CRM."._statusLog", " ", addslashes($sql));
	$db->SQLexecute($sql);
	
	$sql = "UPDATE  ".WARPIT_CRM."._visitStatistic SET order_status = ".StringChecker::Check($_POST['currentState']).", documentModify = NOW() WHERE id = ".StringChecker::Check($_POST['idOrder']);
	$Logger->Log('sprememba_statusa_naročila', 'single-field-update', 'order_status', WARPIT_CRM."._visitStatistic", StringChecker::Check($_POST['oldValue']), addslashes($sql));
	$db->SQLexecute($sql);
	
	echo "{success: true}";
}

elseif($_POST['action'] == 'delivered'){
	$datumDostave = StringChecker::Check($_POST['datumDostave']);
	$trenutnoStanje = StringChecker::Check($_POST['currentState']);
	$idOrder = StringChecker::Check($_POST['idOrder']);
	
	$sql = "INSERT INTO ".WARPIT_CRM."._statusLog (log_type, log_user, user_type, id_order, status_date, status_state, status_comment) 
			VALUES (1, ".intval($_SESSION['UserInfo']['id']).", '".$_SESSION['UserInfo']['user_type']."', '".$idOrder."', NOW(), '".$trenutnoStanje."', '')";		
	$Logger->Log('statusLog', 'delivered', 'log_type, log_user, user_type, id_order, status_date, status_state, status_comment, status_changed_date', 
		  		  WARPIT_CRM."._statusLog", " ", addslashes($sql));
	$db->SQLexecute($sql);
	
	$sql = "UPDATE  ".WARPIT_CRM."._visitStatistic SET order_status = ".$trenutnoStanje.", documentModify = NOW() WHERE id = ".$idOrder;
	$Logger->Log('sprememba_statusa_naročila', 'single-field-update', 'order_status', WARPIT_CRM."._visitStatistic", StringChecker::Check($_POST['oldValue']), addslashes($sql));
	$db->SQLexecute($sql);
	
	echo "{success: true}";
}

else if($_POST['action'] == 'setReclamation')
{
    foreach($_POST['data'] as $key => $value)
    {
         $tmpSql[] = $key ." = '".StringChecker::Check($value)."'"; 
    }
    
    $tmpSql[] = 'status_date = NOW()';
    $tmpSql[] = 'user_type =  "'.$_SESSION['UserInfo']['user_type'].'"';
    $tmpSql[] = 'log_user =  "'.$_SESSION['UserInfo']['id'].'"';
    
    
    $sql = "INSERT INTO " . WARPIT_CRM."._statusLog SET " . implode(',', $tmpSql) . ", id_order = " . StringChecker::Check($_POST['orderId']) . 
        " ON DUPLICATE KEY UPDATE " . implode(',', $tmpSql);
	$Logger->Log('statusLog', 'reclamation', addslashes(implode(',', $tmpSql)), 
		  		  WARPIT_CRM."._statusLog", " ", addslashes($sql));    
    $db->SQLexecute($sql);
    $results['success'] = true;  
    echo json_encode($results);
}

?>