<?php
if (isset($_REQUEST['id_tov'])) {$tov_id=$_REQUEST['id_tov'];} else {$tov_id=0;}
include('../config.php');
include('../units.php');
if ($tov_id==0) exit;
$zapicey=0;
echo '> Товар, артикул: <b>'.$tov_id.'</b><br>';
$query=$db->query("SELECT naim, (SELECT klients.organization FROM ttnsp,ttn,klients WHERE ttnsp.id=ttn.idsp AND ttnsp.id_klients=klients.id AND ttn.id=sklad.id_ttn) AS org, (SELECT ttnsp.ttn FROM ttnsp,ttn WHERE ttnsp.id=ttn.idsp AND ttn.id=sklad.id_ttn) AS ttn, (SELECT DATE_FORMAT(ttnsp.date,'%d.%m.%Y') FROM ttnsp,ttn WHERE ttnsp.id=ttn.idsp AND ttn.id=sklad.id_ttn) AS date FROM sklad WHERE id_ttn=".$tov_id);
$query->setFetchMode(PDO::FETCH_ASSOC);
$row=$query->fetch();
echo '> Прибыл от: '.$row['org'].'<br>';
echo '> ТТН/ТН: '.$row['ttn'].' от '.$row['date'].'<br>';
echo '> Наименование: '.$row['naim'].'<br>';
$query=$db->query("SELECT ttn.kolvo, ttn.kolvo-IFNULL((SELECT SUM(prodal.kol) FROM prodal WHERE prodal.id_ttn=ttn.id),0) AS ostatok, ROUND(ttn.stoim+(ttn.stoim*ttn.nds/100),2) AS zakup, cena, RoznNadb FROM ttn WHERE id=".$tov_id);
$query->setFetchMode(PDO::FETCH_ASSOC);
$row=$query->fetch();
echo '> Количество: '.$row['kolvo'].'<br>';
echo '> Остаток: '.$row['ostatok'].'<br>';
echo '> Закупочная цена: '.CeniProbel($row['zakup']).'<br>';
echo '> Розничная цена: '.CeniProbel($row['cena']).'<br>';
echo '> Надбавка: '.CeniProbel($row['cena']-$row['zakup']).' ( '.$row['RoznNadb'].'% )<br>';
$sql = "SELECT id, DATE_FORMAT(prodal.dateprod,'%d.%m.%Y') AS date, kol, (SELECT name FROM users WHERE users.id=prodal.id_users) AS uname, typeprod, id_korzsp  FROM prodal WHERE id_ttn=".$tov_id;
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>';
if ($admin_mode) echo '<Th bgcolor="#C4D9F7">tools</Th>';
echo '</TR>';
foreach ($db->query($sql) as $row) {
	$zapicey++;
	$type_pr=TypeProd($row['typeprod']);
	if ($row['typeprod']==1) {$type_pr.='. №'.$row['id_korzsp'];}
	if ($zapicey%2==0){ $mcolor='bgcolor="FBFCE8"';}else{ $mcolor='bgcolor="#FFFFF3"';}
		echo '<tr><td bgcolor="#ffffff">'.$zapicey.'</td>
		<td '.$mcolor.'>'.$row['date'].'</td>
		<td '.$mcolor.'>'.$type_pr.'</td>
		<td '.$mcolor.'>'.$row['kol'].'</td>
		<td '.$mcolor.'>'.$row['uname'].'</td>';
		if ($admin_mode) {echo '<td '.$mcolor.'><a href="#" onclick="DellProd('.$row['id'].')"><img src="./img/del.png"></a></td>';}
		echo '</tr>'; 
	}
echo '</TABLE>';
echo '<div id="container" align="left">&nbsp;Записей: '.$zapicey.'</div>';
$sql = "SELECT korzsp.id AS nom, DATE_FORMAT(korzsp.date,'%d.%m.%Y') AS date, korzina.kolvo AS kol, (SELECT klients.organization FROM klients WHERE klients.id=korzsp.id_klients) AS org, (SELECT name FROM users WHERE users.id=korzsp.id_users) AS uname FROM korzina, korzsp WHERE korzina.id_korzsp=korzsp.id AND ((korzsp.status=1)OR(korzsp.status=2)) AND id_ttn=".$tov_id;
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>';
if ($admin_mode) echo '<Th bgcolor="#C4D9F7">tools</Th>';
echo '</TR>';

foreach ($db->query($sql) as $row) {
	$zapicey++;
	if ($zapicey%2==0){ $mcolor='bgcolor="FBFCE8"';}else{ $mcolor='bgcolor="#FFFFF3"';}
		echo '<tr><td bgcolor="#ffffff">'.$row['nom'].' от '.$row['date'].'</td>
		<td '.$mcolor.'>'.$row['kol'].'</td>
		<td '.$mcolor.'>'.$row['org'].'</td>
		<td '.$mcolor.'>'.$row['uname'].'</td>';
		if ($admin_mode) {echo '<td '.$mcolor.'><a href="#" onclick="DellProd('.$row['id'].')"><img src="./img/del.png"></a></td>';}
		echo '</tr>'; 
	}
echo '</TABLE>';

echo '<div id="container" align="left">&nbsp;Записей: '.$zapicey.'</div>';

?>
