<br>
<div id="container">
<div id="upper">&nbsp;Активность пользователя</div>
<?php
$sql = "SELECT * FROM users WHERE id='".$s_user_id."'";
$bonus_prod=0;
$bonus_uslugi=0;
foreach ($db->query($sql) as $row) {
	$bonus_prod=$row['bonus_prod'];
	$bonus_uslugi=$row['bonus_uslugi'];
	break;
	}
if ($action==''){
echo '<form method="post">';
echo '<select type="combobox" name="s_user_id">';
if ($autorized_user_group!=3){ 
	if ($s_user_id==0){echo '<option value=0 selected="selected">Все</option>';} else {echo '<option value=0>Все</option>';}}
$sql = "SELECT * FROM users;";
foreach ($db->query($sql) as $row) {
	if ($s_user_id==$row['id']){echo '<option value='.$row['id'].' selected="selected">'.$row['name'].'</option>';}
	elseif ($autorized_user_group!=3) {echo '<option value='.$row['id'].'>'.$row['name'].'</option>';}
	}
echo '</select>';
echo '<input type="text" style="width:30" name="day" value="'.$mday.'">';
echo '<select type="combobox" name="mesuac">';
for ($i=1;$i<=12;$i++){
	if ($mesuac==$i){echo '<option value='.$i.' selected="selected">'.NameMes($i).'</option>';}
	else {echo '<option value='.$i.'>'.NameMes($i).'</option>';}
	}
echo '</select>
<input type="text" style="width:50" name="year" value="'.$myear.'">
<input type="hidden" name="go" value="userpanel">
<input type="submit" value="Применить">
</form>';

echo '<table class="nice_table" cellspacing=1 cellpadding=2>
	<caption>Продажи</caption>
	<TR>
        <Th bgcolor="#C4D9F7">#</Th>
        <Th bgcolor="#C4D9F7">id</Th>
        <Th bgcolor="#C4D9F7">Наименование</Th>
        <Th bgcolor="#C4D9F7">Кол-во</Th>
        <Th bgcolor="#C4D9F7">Дата продажи</Th>
        <Th bgcolor="#C4D9F7">Тип продажи</Th>
        <Th bgcolor="#C4D9F7">Стоимость</Th>
	<Th bgcolor="#C4D9F7">Бонус</Th>
     </TR>';
$zapicey=0;
$bonus=0;
$obSUMMbonus=0;
$dohod=0;
$pribil=0;
$spisal=0;
$oborot=0;

if (strlen($mesuac)<2){$mesuac='0'.$mesuac;}
$zadaty=$myear.'-'.$mesuac.'-'.$mday;
//$zadaty=strtotime($zadaty);
if ($mday==0){$sql = "SELECT prodal.id AS id, prodal.id_ttn AS id_ttn, prodal.id_korzsp AS id_korzsp, sklad.naim AS naim, sklad.bonus AS bonus, prodal.kol AS kolvo,DATE_FORMAT(prodal.dateprod,'%d.%m.%Y') AS dateprod, ROUND(ttn.Cena*prodal.kol,2) AS stoimost, typeprod FROM prodal,sklad,ttn WHERE ttn.id=sklad.id_ttn AND ttn.id=prodal.id_ttn AND ((prodal.id_users=".$s_user_id.") OR(0=".$s_user_id.")) AND prodal.typeprod<>5 AND MONTH(prodal.dateprod)=".$mesuac." AND YEAR(prodal.dateprod)=".$myear." ORDER BY prodal.dateprod DESC;";}
else{$sql = "SELECT prodal.id AS id, prodal.id_ttn AS id_ttn, prodal.id_korzsp AS id_korzsp, sklad.naim AS naim, sklad.bonus AS bonus, prodal.kol AS kolvo,DATE_FORMAT(prodal.dateprod,'%d.%m.%Y') AS dateprod, ROUND(ttn.Cena*prodal.kol,2) AS stoimost, typeprod FROM prodal,sklad,ttn WHERE ttn.id=sklad.id_ttn AND ttn.id=prodal.id_ttn AND ((prodal.id_users=".$s_user_id.") OR(0=".$s_user_id.")) AND prodal.typeprod<>5 AND prodal.dateprod='".$zadaty."' ORDER BY prodal.dateprod DESC;";}
	foreach ($db->query($sql) as $row) {
		$mcolor='FBFCE8';
		$type_pr=TypeProd($row['typeprod']);
		if ($row['typeprod']==1) {$type_pr.='. №'.$row['id_korzsp'];}
		echo '<tr bgcolor="#'.$mcolor.'">'.
		'<td><a href=# onclick="DellProd('.$row['id'].')"><img src="./img/del.png"></a></td>'.
		'<td>'.$row['id_ttn'].'</td>'.
		'<td>'.$row['naim'].'</td>'.
		'<td>'.$row['kolvo'].'</td>'.
		'<td>'.$row['dateprod'].'</td>'.
		'<td>'.$type_pr.'</td>'.
		'<td>'.CeniProbel($row['stoimost']).'</td>';
		if ($row['bonus']==0){echo '<td>'.CeniProbel(round($row['stoimost']*$bonus_prod/100,$okrugl)).'</td>';} else {echo '<td>'.CeniProbel($row['bonus']).'</td>';}
		echo '</tr>';
		$zapicey++;
		$dohod+=$row['stoimost'];
		if ($row['bonus']==0){$bonus+=$row['stoimost']*$bonus_prod/100;}else{$bonus+=$row['bonus'];}
		}

echo '</TABLE>';
if ($mday==0){$query=$db->query("SELECT SUM(skidka) AS skid FROM client_shops WHERE MONTH(date)=".$mesuac." AND YEAR(date)=".$myear);}
	else{$query=$db->query("SELECT SUM(skidka) AS skid FROM client_shops WHERE date='".$zadaty."'");}
$query->setFetchMode(PDO::FETCH_ASSOC);
$row=$query->fetch();
$summa_skidki=$row['skid'];
$oborot+=$dohod;

echo '<div id="container" align="left">&nbsp;<font color="gray">Записей: '.$zapicey.' | Продано на сумму: '.CeniProbel($dohod).' | Бонус: '.CeniProbel(round($bonus,$okrugl)).'</font></div>';
$obSUMMbonus+=$bonus;

echo '<table class="nice_table" cellspacing=1 cellpadding=2>
	<caption>Списано</caption>
	<TR>
        <Th bgcolor="#C4D9F7">#</Th>
        <Th bgcolor="#C4D9F7">id</Th>
        <Th bgcolor="#C4D9F7">Наименование</Th>
        <Th bgcolor="#C4D9F7">Кол-во</Th>
        <Th bgcolor="#C4D9F7">Дата списания</Th>
        <Th bgcolor="#C4D9F7">Стоимость</Th>
     </TR>';
if ($mday==0){$sql = "SELECT prodal.id AS id, prodal.id_ttn AS id_ttn,sklad.naim AS naim, sklad.bonus AS bonus, prodal.kol AS kolvo,DATE_FORMAT(prodal.dateprod,'%d.%m.%Y') AS dateprod, ROUND(ttn.Cena*prodal.kol,2) AS stoimost FROM prodal,sklad,ttn WHERE ttn.id=sklad.id_ttn AND ttn.id=prodal.id_ttn AND ((prodal.id_users=".$s_user_id.") OR(0=".$s_user_id.")) AND prodal.typeprod=5 AND MONTH(prodal.dateprod)=".$mesuac." AND YEAR(prodal.dateprod)=".$myear." ORDER BY prodal.dateprod DESC;";}
else{$sql = "SELECT prodal.id AS id, prodal.id_ttn AS id_ttn,sklad.naim AS naim, sklad.bonus AS bonus, prodal.kol AS kolvo,DATE_FORMAT(prodal.dateprod,'%d.%m.%Y') AS dateprod, ROUND(ttn.Cena*prodal.kol,2) AS stoimost FROM prodal,sklad,ttn WHERE ttn.id=sklad.id_ttn AND ttn.id=prodal.id_ttn AND ((prodal.id_users=".$s_user_id.") OR(0=".$s_user_id.")) AND prodal.typeprod=5 AND prodal.dateprod='".$zadaty."' ORDER BY prodal.dateprod DESC;";}
	foreach ($db->query($sql) as $row) {
		$mcolor='FBFCE8';
		echo '<tr bgcolor="#'.$mcolor.'">'.
		'<td><a href=# onclick="DellProd('.$row['id'].')"><img src="./img/del.png"></a></td>'.
		'<td>'.$row['id_ttn'].'</td>'.
		'<td>'.$row['naim'].'</td>'.
		'<td>'.$row['kolvo'].'</td>'.
		'<td>'.$row['dateprod'].'</td>'.
		'<td>'.CeniProbel($row['stoimost']).'</td>';
		echo '</tr>';
		$zapicey++;
		$spisal+=$row['stoimost'];
		}
echo '</TABLE>';
echo '<div id="container" align="left">&nbsp;<font color="gray">Записей: '.$zapicey.' | Списано на сумму: '.CeniProbel($spisal).'</font></div>';
echo '<table class="nice_table" cellspacing=1 cellpadding=2>
	<caption>Работы физ.лица</caption>
	<TR>
        <Th bgcolor="#C4D9F7">№ Заказа</Th>
        <Th bgcolor="#C4D9F7">Описание</Th>
        <Th bgcolor="#C4D9F7">Дата</Th>
        <Th bgcolor="#C4D9F7">Дата оплаты</Th>
        <Th bgcolor="#C4D9F7">Клиент</Th>
        <Th bgcolor="#C4D9F7">Стоимость</Th>
	<Th bgcolor="#C4D9F7">Бонус</Th>
     </TR>';
$zapicey=0;
$bonus=0;
$dohod=0;
if ($mday==0){$sql = "SELECT zakazi.id, DATE_FORMAT(zakazi.datezak,'%d.%m.%Y') AS date_zak, DATE_FORMAT(zakazi.dateoplati,'%d.%m.%Y') AS date_oplati, zakazi.dopinf, (SELECT concat(organization,' --> ',fio) FROM klients WHERE id=zakazi.id_klients) AS klient, zakazi.stoim AS stoimost, (SELECT bonus_uslugi FROM users WHERE zakazi.id_users=id) AS bonus FROM zakazi WHERE ((zakazi.id_users=".$s_user_id.") OR(0=".$s_user_id.")) AND zakazi.status>=3 AND (SELECT type_client FROM klients WHERE id=zakazi.id_klients)=1 AND MONTH(zakazi.datezak)=".$mesuac." AND YEAR(zakazi.datezak)=".$myear." ORDER BY zakazi.datezak DESC;";}
else{$sql = "SELECT zakazi.id, DATE_FORMAT(zakazi.datezak,'%d.%m.%Y') AS date_zak, DATE_FORMAT(zakazi.dateoplati,'%d.%m.%Y') AS date_oplati, zakazi.dopinf, (SELECT concat(organization,' --> ',fio) FROM klients WHERE id=zakazi.id_klients) AS klient, zakazi.stoim AS stoimost, (SELECT bonus_uslugi FROM users WHERE zakazi.id_users=id) AS bonus FROM zakazi WHERE ((zakazi.id_users=".$s_user_id.") OR(0=".$s_user_id.")) AND zakazi.status>=3 AND (SELECT type_client FROM klients WHERE id=zakazi.id_klients)=1 AND zakazi.datezak='".$zadaty."' ORDER BY zakazi.datezak DESC;";}
	foreach ($db->query($sql) as $row) {
		$mcolor='FBFCE8';
		echo '<tr bgcolor="#'.$mcolor.'">'.
		'<td>'.$row['id'].'</td>'.
		'<td>'.$row['dopinf'].'</td>'.
		'<td>'.$row['date_zak'].'</td>'.
		'<td>'.$row['date_oplati'].'</td>'.
		'<td>'.$row['klient'].'</td>'.
		'<td>'.CeniProbel($row['stoimost']).'</td>'.
//		if ($row['bonus']==0){echo '<td>'.round(($row['stoimost']*$bonus_prod/100),0).'</td>';} else {echo '<td>'.$row['bonus'].'</td>';}
		'<td>'.CeniProbel(round($row['stoimost']*$bonus_uslugi/100,$okrugl)).'</td>';
		echo '</tr>';
		$zapicey++;
		$bonus+=$row['stoimost']*$bonus_uslugi/100;
		$dohod+=$row['stoimost'];
		}

echo '</TABLE>';
echo '<div id="container" align="left">&nbsp;<font color="gray">Записей: '.$zapicey.' | Доход с работ: '.CeniProbel($dohod).' | Бонус: '.CeniProbel(round($bonus,$okrugl)).'</font></div>';
$obSUMMbonus+=$bonus;
$oborot+=$dohod;

echo '<table class="nice_table" cellspacing=1 cellpadding=2>
	<caption>Работы юр.лица за месяц </caption>
	<TR>
        <Th bgcolor="#C4D9F7">№ Заказа</Th>
        <Th bgcolor="#C4D9F7">Дата</Th>
        <Th bgcolor="#C4D9F7">Дата оплаты</Th>
        <Th bgcolor="#C4D9F7">Клиент</Th>
        <Th bgcolor="#C4D9F7">Стоимость</Th>
	<Th bgcolor="#C4D9F7">Бонус</Th>
     </TR>';
$zapicey=0;
$bonus=0;
$dohod=0;
$sql = "SELECT zakazi.id, DATE_FORMAT(zakazi.datezak,'%d.%m.%Y') AS date_zak, DATE_FORMAT(zakazi.dateoplati,'%d.%m.%Y') AS date_oplati, zakazi.dopinf, 
(SELECT organization FROM klients WHERE id=zakazi.id_klients) AS klient, 
(SELECT SUM(stoim*kolvo) FROM rabota WHERE id_zak=zakazi.id AND rabota.id_users=".$s_user_id.") AS stoimost, 
(SELECT bonus_uslugi FROM users WHERE zakazi.id_users=id) AS bonus 
FROM zakazi WHERE zakazi.status>=3 AND (SELECT type_client FROM klients WHERE id=zakazi.id_klients)=2 AND MONTH(zakazi.dateoplati)=".$mesuac." AND YEAR(zakazi.dateoplati)=".$myear." ORDER BY zakazi.datezak DESC;";
	foreach ($db->query($sql) as $row) {
		if ($row['stoimost']>0){
		$mcolor='FBFCE8';
		echo '<tr bgcolor="#'.$mcolor.'">'.
		'<td>'.$row['id'].'</td>'.
		'<td>'.$row['date_zak'].'</td>'.
		'<td>'.$row['date_oplati'].'</td>'.
		'<td>'.$row['klient'].'</td>'.
		'<td>'.CeniProbel($row['stoimost']).'</td>'.
		'<td>'.CeniProbel(round($row['stoimost']*$bonus_uslugi/100,$okrugl)).'</td>';
		echo '</tr>';
		$zapicey++;
		$bonus+=$row['stoimost']*$bonus_uslugi/100;
		$dohod+=$row['stoimost'];
		}}

echo '</TABLE>';
echo '<div id="container" align="left">&nbsp;<font color="gray">Записей: '.$zapicey.' | Доход с работ: '.CeniProbel($dohod).' | Бонус: '.CeniProbel(round($bonus,$okrugl)).'</font></div></div>';
if ($s_user_id!=2){
$obSUMMbonus+=$bonus;
$oborot+=$dohod;
}

$obSUMMbonus=round($obSUMMbonus,$okrugl);
//$obSUMMbonus=round($obSUMMbonus,-3);
$pribil=$dohod;
if ($mday==0) {$sql = "SELECT SUM(prodal.kol*ROUND((ttn.stoim*ttn.RoznNadb/100),2)) FROM prodal,ttn WHERE ttn.id=prodal.id_ttn AND ((prodal.id_users=".$s_user_id.") OR(0=".$s_user_id.")) AND prodal.typeprod<>5 AND MONTH(prodal.dateprod)=".$mesuac." AND YEAR(prodal.dateprod)=".$myear;}
else {$sql = "SELECT SUM(prodal.kol*ROUND((ttn.stoim*ttn.RoznNadb/100),2)) FROM prodal,ttn WHERE ttn.id=prodal.id_ttn AND ((prodal.id_users=".$s_user_id.") OR(0=".$s_user_id.")) AND prodal.typeprod<>5 AND prodal.dateprod='".$zadaty."';";}
foreach ($db->query($sql) as $row) {
	$pribil+=$row[0];
	break;}

echo '<div id="container" align="left">&nbsp;<b>Общая сумма бонуса:</b> '.CeniProbel($obSUMMbonus).' <font color="gray">| Общая сумма прибыли: '.CeniProbel($pribil);
if ($s_user_id==0) {echo ' | Оборот: '.CeniProbel($oborot).' | За вычетом скидки: '.CeniProbel($oborot-$summa_skidki);}
echo '</font>';
echo '</div></div>';
echo '<div id="results"></div>';
}
?>

<script language="JavaScript">
<!--
function DellOK() {
location.href = 'index.php?go=dvijsklad';
MessageOK('Удалено.');
}

function DellProd(idtov) {
pass = prompt('Подтвердите операцию паролем','');
$.ajax({ type: "POST", url: "function/delete_prod.php",
	data: "id_prod="+idtov+"&pass="+pass,  
	success: function(data) {$('#results').html(data);}
});
}
// -->
</script>
