<?php
include('../style.php');
include('include.php');
RecalculateTariffs(1);

ShowHeader('Seznam členů sítě','Seznam členů');

if(array_key_exists('id',$_GET)) $UserID = $_GET['id'];
if(!array_key_exists('show',$_GET)) $_GET['show'] = '';
switch($_GET['show'])
{
  case 'user':
    $DbResult = $Database->query("SELECT *,CONCAT(second_name, ' ', first_name) as fullname FROM users WHERE id=".$UserID);
    $Row2 = $DbResult->fetch_array();
    $TarifName = $Tarify[$Row2['inet_tarif_next']]['name'];
    echo('<table width="100%" border="0" cellspacing="0" cellpadding="3"><tr><td valign="top">');
  
    // Tabulka operaci
    echo('Výpis operací pro člena '.$Row2['fullname'].':<br>');
    echo('<table style="font-size: smaller;" border="1" cellspacing="0" cellpadding="3"><tr><th>Datum</th><th>Změna</th><th>Komentář</th></tr>');
    $DbResult = $Database->query("SELECT * FROM finance_operations WHERE user=".$_GET['id'].' ORDER BY date DESC');
    while($Row = $DbResult->fetch_array())
    {
      if($Row['money']>0) $Row['money'] = '+'.$Row['money'];
      echo('<tr><td align="right">'.HumanDate($Row['date']).'</td><td>'.$Row['money'].'</td><td>'.$Row['comment'].'&nbsp;</td></tr>');
    }
    echo('<tr><td>&nbsp;</td><td>-'.$Row2['network_device'].'</td><td>Proměnná cena za zařízení</td></tr></table>');
    echo('<h3><strong>Stav účtu: </strong>'.$Row2['cash'].' Kč</h3><br>');
    echo('</td><td valign="top">');

    echo('Variabilní symbol pro platby: <strong>'.$Row2['id'].'</strong><br><br>');

    // Měsíční bilance
    if($Row2['inet'] == 1) $InternetPrice = round($Row2['monthly_inet']); else $InternetPrice = 0;
    if($Row2['overheads'] == 0) 
    {
      $PoplatekSprava = 0;
      $PoplatekSpotreba = 0;
      $PrijemSpotreba = $Row2['plus'];
    } else {
      $PoplatekSprava = $Sprava;
      $PoplatekSpotreba = $Row2['consumption'];
      $PrijemSpotreba = $Row2['plus'];
    }
    echo('Rozpis měsíčního poplatku:<br><table border="1" cellspacing="0" cellpadding="3">'.
    '<tr><th>Část</th><th>Cena [Kč]</th></tr>'.
    '<tr><td>Internet - tarif '.$TarifName.'</td><td>'.$InternetPrice.'</td></tr>'.
    '<tr><td>Správa</td><td>'.$PoplatekSprava.'</td></tr>'.
    '<tr><td>Poplatek za spotřebu</td><td>'.$PoplatekSpotreba.'</td></tr>'.
    '<tr><td>Příjem za spotřebu</td><td>'.(-1*$PrijemSpotreba)  .'</td></tr>'.
    '<tr><td><strong>Celkem</strong></td><td><strong>'.($InternetPrice + $PoplatekSprava + $PoplatekSpotreba - $PrijemSpotreba).'</strong></td></tr></table>');
    //echo('Tarif pro příští měsíc: '.$Tarify[$Row2['inet_tarif_next']]['name'].'<br><br>');
    echo('<br>');
    
    // Rozpis ceny za zařízení
    echo('Výpis podílu na zařízení segmentů:');
    echo('<table border="1" cellspacing="0" cellpadding="3"><tr><th>Název segmentu</th><th>Cena [Kč]</th><th>Uživatelů</th><th>Podíl [Kč]</th></tr>');
    $ID = $Row2['network_segment'];
    $Money = 0;
    while($ID > 0)
    {
      $DbResult = $Database->query("SELECT * FROM network_segments WHERE id=".$ID);
      $Row = $DbResult->fetch_array();
      if(!(($Row2['inet_hw'] == 0) and ($ID == $InternetSegmentId)))
      {     
        $Money += $Row['price']/$Row['users'];
        echo('<tr><td>'.$Row['name'].'</td><td>'.$Row['price'].'</td><td>'.$Row['users'].'&nbsp;</td><td>'.round($Row['price']/$Row['users']).'</td></tr>');
      }
      $ID = $Row['parent'];
    }
    echo('<tr><td><strong>Celkem</strong></td><td>&nbsp;</td><td>&nbsp;</td><td><strong>'.round($Money).'</strong></td></tr>');
    echo('</table><br>');

    // Rozpis platba spotřeby segmentů
    echo('Výpis podílu na spotřebě segmentů:');
    echo('<table border="1" cellspacing="0" cellpadding="3"><tr><th>Název segmentu</th><th>Cena [Kč]</th><th>Uživatelů</th><th>Podíl [Kč]</th></tr>');
    $ID = $Row2['network_segment'];
    $Money = 0;
    while($ID>0)
    {
      $DbResult = $Database->query("SELECT * FROM network_segments WHERE id=".$ID);
      $Row = $DbResult->fetch_array();
      $Money += round(W2Kc($Row['consumption'])/$Row['users_overheads']);
      echo('<tr><td>'.$Row['name'].'</td><td>'.W2Kc($Row['consumption']).'</td><td>'.$Row['users_overheads'].'&nbsp;</td><td>'.round(W2Kc($Row['consumption'])/$Row['users_overheads']).'</td></tr>');
      $ID = $Row['parent'];
    }
    echo('<tr><td><strong>Celkem</strong></td><td>&nbsp;</td><td>&nbsp;</td><td><strong>'.round($Money).'</strong></td></tr>');
    echo('</table><br>');
    
    echo('Měsíční příjem za spotřebu zařízení umístěných u uživatele:<br>');
    echo('<table border="1" cellspacing="0" cellpadding="3"><tr><th>Název zařízení</th><th>Spotřeba [W]</th><th>Příjem [Kč]</th></tr>');
    $DbResult = $Database->query("SELECT * FROM network_devices WHERE user=".$UserID." AND used=1");
    $TotalW = 0;
    $TotalPrice = 0;
    while($Row = $DbResult->fetch_array())
    {
      echo('<tr><td>'.$Row['name'].'</td><td>'.$Row['consumption'].'</td><td>'.W2Kc($Row['consumption']).'</td></tr>');
      $TotalW += $Row['consumption'];
      $TotalPrice += W2Kc($Row['consumption']);
    }
    echo('<tr><td><strong>Celkem</strong></td><td><strong>'.$TotalW.'</strong></td><td><strong>'.$TotalPrice.'</strong></td></tr>');
    echo('</table>');

    echo('</td></tr></table>');
    break;
  default:
    // Seznam členů
    echo('Seznam členů:<br>'.
    '<table style="font-size: smaller;" border="1" cellspacing="0" cellpadding="3">'.
    '<tr><th>Jméno</th><th>Cena za zařízení [Kč]</th><th>Měsíční poplatek [Kč]</th><th>Stav účtu [Kč]</th><th>Běžící tarif</th><th>Příští tarif</th><th>Poznámky</th></tr>');
    $TotalDevice = 0;
    $TotalMonth = 0;
    $TotalCash = 0;
    $DbResult = $Database->query("SELECT *,CONCAT(second_name, ' ', first_name) as fullname FROM users WHERE role=0 ORDER BY fullname");
    while($Row = $DbResult->fetch_array())
    {
      $Monthly = $Row['monthly'] - $Row['plus'];
      if($Row['hw'] == 0) $Monthly = '(trvale nepřipojen) '.(-$Row['plus']);
      else {     
        if($Row['overheads'] == 0) $Monthly = '(dočasně nepřipojen) '.$Monthly;
        else {
          if($Row['inet'] == 0) $Monthly = '(bez internetu) '.$Monthly;
          else $Monthly = $Monthly;
        }
      } 
      $TotalCash += $Row['cash'];
      if($Row['cash'] < 0) $Row['cash'] = '<span style="color: red;">'.$Row['cash'].'</span>';
      $Tarif = $Tarify[$Row['inet_tarif_now']]['name'];
      $PristiTarif = $Tarify[$Row['inet_tarif_next']]['name'];
      echo('<tr><td><a href="?show=user&amp;id='.$Row['id'].'">'.$Row['fullname'].'</a></td><td align="right">'.$Row['network_device'].'</td><td align="right">'.$Monthly.'</td><td align="right">'.$Row['cash'].'</td><td align="center">'.$Tarif.'</td><td align="center">'.$PristiTarif.'</td><td>'.$Row['notice'].'&nbsp;</td></tr>');
      $TotalDevice += $Row['network_device'];
      $TotalMonth += ($Row['monthly']-$Row['plus']);
    }
    echo('<tr><td><strong>Celkem</strong></td><td align="right"><strong>'.$TotalDevice.'</strong></td><td align="right"><strong>'.$TotalMonth.'</strong></td><td align="right"><strong>'.$TotalCash.'</strong></td><td align="right">&nbsp;</td><td align="right">&nbsp;</td><td align="right">&nbsp;</td></tr>');
    echo('</table>');
  }
ShowFooter();

?>
