<?
  if (basename(__FILE__) == basename($PHP_SELF))
    die("Darf nur als include benutzt werden\n");
?>
<?
/*----------------------------------------------------------------------------*/
/*-- Program   :     Mediartec Webshop 1.0                                    */
/*-- Author    :     Markus@Kaatze.de, Mediartec GmbH                         */
/*-- Created on:     9 September                                              */
/*-- Purpose   :     Alle Datenbankfunktionen                                 */
/*----------------------------------------------------------------------------*/
/*-- Change History                                                           */
/*-- DATE         Who               What                                      */
/*----------------------------------------------------------------------------*/
/*-- 9-10-01      mKaatze           Creation.                                 */
/*----------------------------------------------------------------------------*/

/*-----SQL-Befehl ausführen: Rückgabewert $res--------------------------------*/
function send_sql($sql)
{
  global $db,$MySQL_Host,$MySQL_User,$MySQL_Passw;
  $connect=mysql_connect($MySQL_Host,$MySQL_User,$MySQL_Passw);
  mysql_select_db($db);
  if (! $res=mysql_query($sql))
  {
    echo mysql_error();
    exit;
  }
  return $res;
  mysql_close($connect);
}

/*-----SQL-Befehl ausführen: Rückgabewert $lastID-----------------------------*/
function send_sql_IDback($sql)
{
  global $db,$MySQL_Host,$MySQL_User,$MySQL_Passw;
  $connect=mysql_connect($MySQL_Host,$MySQL_User,$MySQL_Passw);
  if (! $res=mysql_db_query($db, $sql))
  {
    echo mysql_error();
    exit;
  }
  $lastID=mysql_insert_id($connect);
  return $lastID;
  mysql_close($connect);
}

/*-----Aus DB löschen---------------------------------------------------------*/
function db_delete($sql)
{
  $res=send_sql($sql);
  $num=mysql_affected_rows();
  echo "<div align=center>$num Datensatz wurde gelöscht.</div><br>";
}

/*-----Tabelle mit DB-Daten erzeugen--------------------------------------------*/
function tab_normal($sql)
{
  $res=send_sql($sql);
  $anz=mysql_num_fields($res);
  echo "<table border=0 cellpadding='2' cellspacing='2' align='center'  bgcolor=white>";
    echo "<tr>";
    for ($i=0;$i<$anz;$i++)
    {
      echo "<th width='150'>";
      echo mysql_field_name($res,$i);
      echo "</th>";
    }
    echo "</tr>";
    echo "<tr>";
    while ($row  =  mysql_fetch_array($res,MYSQL_NUM))
    {
      foreach ($row as $elem)
      {
        echo "<td width='150' align='center'>$elem &nbsp;</td>";
      }
    echo "</tr>";
    }
  echo "</table>";
}

/*-----Tabelle mit Addition erzeugen--------------------------------------------*/
function tab_add($sql)
{
  global $status;
  $sum=0;
  $res=send_sql($sql);
  $anz=mysql_num_fields($res);
  echo "<table border=0 cellpadding='2' cellspacing='2' align='center'  bgcolor=white>";
    echo "<tr>";
      for ($i=0;$i<$anz;$i++)
      {
        echo "<th> &nbsp; &nbsp;";
        echo mysql_field_name($res,$i);
        echo " &nbsp; &nbsp;</th>";
      }
      echo "<th> &nbsp; &nbsp; Summe &nbsp; &nbsp;</th>";
    echo "</tr>";
    echo "<tr>";
    while ($row  =  mysql_fetch_array($res,MYSQL_NUM))
    {
      $i=0;
      foreach ($row as $elem)
      {
        echo "<td width='150' align='center'>$elem &nbsp;</td>";
        if (mysql_field_name($res,$i)=="umsatz") $sum=$sum+$elem;
        $i++;
      }
      echo "<td width='150' align='center'>$sum &euro; &nbsp;</td>";
    echo "</tr>";
    }
  echo "</table>";
}


/*-----Tabelle mit DB-Daten und Button  erzeugen-------------------------*/
function tab_but($sql,$men,$idfeld)
{
  global $session_id;
  $res=send_sql($sql);
  $anz=mysql_num_fields($res);
  echo "<table border=1 cellpadding='1' cellspacing='0' align='center'  bgcolor=white>";
    echo "<tr>";
    for ($i=0;$i<$anz;$i++)
    {
      echo "<th><font face='Arial' size='-1'>";
      echo mysql_field_name($res,$i);
      echo "</th></font>";
    }
    echo "<th>&nbsp;</th>";
    echo "<th>&nbsp;</th>";
    echo "</tr>";
    echo "<tr>";
    while ($row  =  mysql_fetch_array($res,MYSQL_NUM))
    {
      $i=0;
      foreach ($row as $elem)
      {
        echo "<td><div align=center><font face='Arial' size='-2'>$elem &nbsp;</font></div></td>";
      }
      echo "<td><a href='$PHP_SELF?menue=$men&session_id=$session_id&todo=edit&$idfeld=$row[0]'><img src='bilder/inhalt/edit.gif' width='16' height='16' border='0'></a></td>";
      echo "<td><a href='javascript:kontrollabfrage(\"$session_id\",\"$row[0]\")'><img src='bilder/inhalt/delete.gif' width='16' height='16' border='0'></a></td>";
    echo "</tr>";
    }
  echo "</table>";
}


/*-----Auswahlliste DB-Daten erzeugen-------------------------------------------*/
function select_list($db,$selekt,$name,$sql,$size)
{
  $res=send_sql($db,$sql);
  echo "<select name=$name size=$size>";
    while ($row  =  mysql_fetch_array($res,MYSQL_NUM))
    {
      $id=array_shift($row);
      $str=implode(", ",$row);
      if ($id==$selekt)
        echo "<option selected value=$id>$str</option>";
      else
        echo "<option value=$id>$str</option>";
    }
    echo "</select>";
}

/*-----Tabelle mit Addition erzeugen--------------------------------------------*/
function tab_mwst($kasse_id)
{
  global $mwst1,$mwst2;
  $sum_mwst1=0;
  $sum_mwst2=0;
  $sum=0;
  $res=send_sql("select k.datum, k.kasse, u.vorname, u.name from kasse as k, user as u where k.user_id= u.user_id and k.kasse_id=$kasse_id");
  while ($row  =  mysql_fetch_array($res,MYSQL_NUM))
  {
    echo "<h3 align=center>Umsatz vom $row[0] auf Kasse $row[1] von $row[2] $row[3]</h3>";
  }
  $sql="SELECT a.Name as Artikelname, Sum(u.anz) AS Anzahl, a.mwst as Mehrwertsteuersatz, Sum(u.akt_preis) FROM umsatz as u, artikel as a where u.artnr=a.artnr and u.kasse_id=$kasse_id GROUP BY u.artnr";
  $res=send_sql($sql);
  $anz=mysql_num_fields($res);

  echo "<table border=0 cellpadding='0' cellspacing='0' align=center bgcolor=white>";
    echo "<tr>";
       echo "<th>Artikelname</th>";
       echo "<th>Anzahl</th>";
       echo "<th>Summe</th>";
       echo "<th><font size=-1>Davon Mwst $mwst1%</font></th>";
       echo "<th><font size=-1>Davon Mwst $mwst2%</font></th>";
    echo "</tr>";

    while ($row  =  mysql_fetch_array($res,MYSQL_NUM))
    {
      $sum=$sum+$row[3];
      echo "<tr>";
        echo "<td width='130'><font size=-1>$row[0]</font></td>";
        echo "<td width='80' align='center'><font size=-1>$row[1]</font></td>";
        echo "<td width='80' align='center'><font size=-1>$row[3] &euro;</font></td>";
        if($row[2]==1)
        {
          $sum_mwst1=$sum_mwst1+($row[3]/100*$mwst1);
          echo "<td width='110' align='right'><font size=-1>".($row[3]/100*$mwst1)." &euro;</font></td>";
        }
        else
          echo "<td width='110' align='right'><font size=-1>-</font></td>";
        if($row[2]==2)
        {
          $sum_mwst2=$sum_mwst2+($row[3]/100*$mwst2);
          echo "<td width='110' align='right'><font size=-1>".($row[3]/100*$mwst2)." &euro;</font></td>";
        }
        else
          echo "<td width='110' align='right'><font size=-1>-</font></td>";
      echo "</tr>";
    }
    echo "<tr colspan=5>";
      echo "<td>&nbsp;</td>";
    echo "</tr>";
    echo "<tr>";
      echo "<td>&nbsp;</td>";
      echo "<td align='center'>Gesamtsumme:</td>";
      echo "<td align='center'>$sum &euro;</td>";
      echo "<td align='right'>$sum_mwst1 &euro;</td>";
      echo "<td align='right'>$sum_mwst2 &euro;</td>";
    echo "</tr>";
  echo "</table>";
}


?>