<?php
/* --------------------------------------------------------------
   $Id: accounting.php,v 1.1.1.1 2008/10/27 07:55:51 cvs Exp $   

   XT-Commerce - community made shopping
   http://www.xt-commerce.com

   Copyright (c) 2003 XT-Commerce
   --------------------------------------------------------------
   based on: 
   (c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
   (c) 2002-2003 osCommercecoding standards www.oscommerce.com 
   (c) 2003	 nextcommerce (accounting.php,v 1.27 2003/08/24); www.nextcommerce.org

   Released under the GNU General Public License 
   --------------------------------------------------------------*/

  require('includes/application_top.php');
  $c_admin_access_query = xtc_db_query("select * from " . TABLE_ADMIN_ACCESS . " where customers_id = '" . (int)$_SESSION['customer_id'] . "'");
  $c_admin_access = xtc_db_fetch_array($c_admin_access_query);

  if ($_GET['action']) {
    switch ($_GET['action']) {
      case 'scheme_select':
	$cID = xtc_db_input($_GET['cID']);
	$aasID = xtc_db_input($_POST['scheme_id']);

	$fields = mysql_list_fields(DB_DATABASE, TABLE_ADMIN_ACCESS);
	$columns = mysql_num_fields($fields);	

	$existing_fields = array();
	for ($i = 0; $i < $columns; $i++) {
		$field=mysql_field_name($fields, $i);
		$existing_fields[$field] = true;
		if ($field!='customers_id') {
			$update = false;
			if($c_admin_access['f_accounting_system_groups'] != 1){
				switch( $group_access[$field] ){
					case 0:
						$update = false;
						break;
					default:
						$update = true;
						break;
				}
			}else{
				$update = true;
			}
			if($update)
				xtc_db_query("UPDATE ".TABLE_ADMIN_ACCESS." SET ".$field."=0 where customers_id='".$cID."'");
		}
	}

	$scheme_query = xtc_db_query("SELECT admin_access_value FROM admin_access_scheme_values WHERE admin_access_scheme_id='".$aasID."'");
	while($scheme = xtc_db_fetch_array($scheme_query)){
		if($existing_fields[$scheme['admin_access_value']])
			xtc_db_query("UPDATE ".TABLE_ADMIN_ACCESS." SET ".$scheme['admin_access_value']."=1 where customers_id='".$cID."'");
	}
	$messageStack->add_session('Berechtigung f&uuml;r User gespeichert. Vorlage aus Berechtigungsgruppe.','success');
	xtc_redirect(xtc_href_link(FILENAME_CUSTOMERS, 'cID=' . $cID, 'NONSSL'));
	break;
      case 'edit_scheme':
	if($c_admin_access['f_accounting_scheme'] != 1){
		die('UNAUTHORISIERTER SCRIPTAUFRUF!');
	}
	include('accounting_scheme.php');
	exit;
	break;
      case 'save':


      // reset values before writing
       $admin_access_query = xtc_db_query("select * from " . TABLE_ADMIN_ACCESS . " where customers_id = '" . (int)$_GET['cID'] . "'");
       $admin_access = xtc_db_fetch_array($admin_access_query);

       $group_query=xtc_db_query("select * from " . TABLE_ADMIN_ACCESS . " where customers_id = 'groups'");
       $group_access = xtc_db_fetch_array($group_query);

       $fields = mysql_list_fields(DB_DATABASE, TABLE_ADMIN_ACCESS);
       $columns = mysql_num_fields($fields);

       for ($i = 0; $i < $columns; $i++) {
       	$field=mysql_field_name($fields, $i);

       	if ($field!='customers_id') {
		if($c_admin_access['f_accounting_system_groups'] != 1){
			switch( $group_access[$field] ){
				case 0:
					$update = false;
					break;
				default:
					$update = true;
					break;
			}
		}else{
			$update = true;
		}
		if($update)
			xtc_db_query("UPDATE ".TABLE_ADMIN_ACCESS." SET ".$field."=0 where customers_id='".(int)$_GET['cID']."'");
	}

       }
       $access_ids='';
       if(isset($_POST['access'])){
	foreach($_POST['access'] as $key){
		xtc_db_query("UPDATE ".TABLE_ADMIN_ACCESS." SET ".$key."=1 where customers_id='".(int)$_GET['cID']."'");
        }
       }
       //xtc_set_admin_access($_GET['id'], $_GET['flag'], $_GET['cID']);
       xtc_redirect(xtc_href_link(FILENAME_CUSTOMERS, 'cID=' . (int)$_GET['cID'], 'NONSSL'));
       break;
      }
    }
    if ($_GET['cID'] != '') {
        $allow_edit_query = xtc_db_query("select customers_status, customers_firstname, customers_lastname from " . TABLE_CUSTOMERS . " where customers_id = '" . (int)$_GET['cID'] . "'");
        $allow_edit = xtc_db_fetch_array($allow_edit_query);
        if ($allow_edit['customers_status'] != 0 || $allow_edit == '') {
          xtc_redirect(xtc_href_link(FILENAME_CUSTOMERS, 'cID=' . (int)$_GET['cID'], 'NONSSL'));
        }
    }
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo $_SESSION['language_charset']; ?>"> 
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
  <tr>
<!-- body_text //-->
    <td class="boxCenter" width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="0">
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading"><?php echo TEXT_ACCOUNTING.' '.$allow_edit['customers_lastname'].' '.$allow_edit['customers_firstname']; ?></td>
            <td class="pageHeading" align="right"><?php echo xtc_draw_separator('pixel_trans.gif', HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT); ?><br><br></td>
          </tr>
        </table></td>
      </tr>
      <tr>
	<td colspan="2" class="main">&nbsp;</td>
      </tr>

      <?php
	if($c_admin_access['f_accounting_scheme'] == 1){
      ?>

      <tr>
	<td colspan="2" class="main"><a href="<?php echo xtc_href_link('accounting.php',xtc_get_all_get_params(array('action')).'action=edit_scheme'); ?>"><u>Berechtigungsgruppen verwalten</u></a></td>
      </tr>

      <?php
	}
      ?>

      <tr>
      <td colspan="2" class="main"> <br><?php echo TXT_GROUPS; ?><br>

      <table width="100%" cellpadding="0" cellspacing="2">
      <tr>
       <td style="border: 1px solid; border-color: #000000;" width="10" bgcolor="#FF6969" ><?php echo xtc_draw_separator('pixel_trans.gif',15, 15); ?></td>
       <td width="100%" class="main"><?php echo TXT_SYSTEM; ?></td>
      </tr>
      <tr>
       <td style="border: 1px solid; border-color: #000000;" width="10" bgcolor="#69CDFF" ><?php echo xtc_draw_separator('pixel_trans.gif',10, 15); ?></td>
       <td width="100%" class="main"><?php echo TXT_CUSTOMERS; ?></td>
      </tr>
      <tr>
       <td style="border: 1px solid; border-color: #000000;" width="10" bgcolor="#6BFF7F" ><?php echo xtc_draw_separator('pixel_trans.gif',15, 15); ?></td>
       <td width="100%" class="main"><?php echo TXT_PRODUCTS; ?></td>
      </tr>
      <tr>
       <td style="border: 1px solid; border-color: #000000;" width="10" bgcolor="#BFA8FF" ><?php echo xtc_draw_separator('pixel_trans.gif',15, 15); ?></td>
       <td width="100%" class="main"><?php echo TXT_STATISTICS; ?></td>
      </tr>
      <tr>
       <td style="border: 1px solid; border-color: #000000;" width="10" bgcolor="#FFE6A8" ><?php echo xtc_draw_separator('pixel_trans.gif',15, 15); ?></td>
       <td width="100%" class="main"><?php echo TXT_TOOLS; ?></td>
      </tr>
      <tr>
       <td style="border: 1px solid; border-color: #000000;" width="10" bgcolor="#800000" ><?php echo xtc_draw_separator('pixel_trans.gif',15, 15); ?></td>
       <td width="100%" class="main">Lieferanten<!-- 6 //--></td>
      </tr>
      <tr>
       <td style="border: 1px solid; border-color: #000000;" width="10" bgcolor="#333333" ><?php echo xtc_draw_separator('pixel_trans.gif',15, 15); ?></td>
       <td width="100%" class="main">Reklamationshandling<!-- 7 //--></td>
      </tr>
      </table>
      <br>
      </td>
      </tr>
      <tr>
        <td>
<?php
if($c_admin_access['f_accounting_scheme'] == 1){
	$scheme_array = array();
	$scheme_query = xtc_db_query("SELECT admin_access_scheme_id,admin_access_scheme_title FROM admin_access_scheme ORDER BY admin_access_scheme_title");
	while($scheme = xtc_db_fetch_array($scheme_query)){
		$scheme_array[] = array('id'=>$scheme['admin_access_scheme_id'],'text'=>$scheme['admin_access_scheme_title']);
	}
	if(sizeOf($scheme_array)>0){
		echo xtc_draw_form('accounting_scheme', FILENAME_ACCOUNTING, 'cID=' . $_GET['cID']  . '&action=scheme_select', 'post');
?>
		<table border="0" cellpadding="0" class="formArea">
		<tr>
			<td class="main">
			<b>Berechtigungsgruppe anwenden:</b>
			<?php echo xtc_draw_pull_down_menu('scheme_id',$scheme_array); ?><input type="submit" value="Anwenden" class="iButton"><br />
			(Achtung, das vorhandene Berechtigungsschema dieses Users wird &uuml;berschrieben!)
			</td>
		</tr>
		</table>
		</form><br />
<?php
	}
}
?>
		<table valign="top" width="100%" border="0" cellpadding="0" cellspacing="0">
          <tr class="dataTableHeadingRow">
            <td class="dataTableHeadingContent"><?php echo TEXT_ACCESS; ?></td>
            <td class="dataTableHeadingContent"><?php echo TEXT_ALLOWED; ?></td>
          </tr>
        </table></td>
      </tr>
      <tr>
	<td>
		<table border="0" cellpadding="0" cellspacing="2">
<?php
if($c_admin_access['f_accounting_change'] == 1){
	echo xtc_draw_form('accounting', FILENAME_ACCOUNTING, 'cID=' . $_GET['cID']  . '&action=save', 'post', 'enctype="multipart/form-data"');
}

   $admin_access='';

    $customers_id = xtc_db_prepare_input($_GET['cID']);
    $admin_access_query = xtc_db_query("select * from " . TABLE_ADMIN_ACCESS . " where customers_id = '" . (int)$_GET['cID'] . "'");
    $admin_access = xtc_db_fetch_array($admin_access_query);

    $group_query=xtc_db_query("select * from " . TABLE_ADMIN_ACCESS . " where customers_id = 'groups'");
    $group_access = xtc_db_fetch_array($group_query);
    if ($admin_access == '') {
      xtc_db_query("INSERT INTO " . TABLE_ADMIN_ACCESS . " (customers_id) VALUES ('" . (int)$_GET['cID'] . "')");
      $admin_access_query = xtc_db_query("select * from " . TABLE_ADMIN_ACCESS . " where customers_id = '" . (int)$_GET['cID'] . "'");
      $group_query=xtc_db_query("select * from " . TABLE_ADMIN_ACCESS . " where customers_id = 'groups'");
      $group_access = xtc_db_fetch_array($admin_access_query);
      $admin_access = xtc_db_fetch_array($admin_access_query);
    }

$fields = mysql_list_fields(DB_DATABASE, TABLE_ADMIN_ACCESS);
$columns = mysql_num_fields($fields);

for ($i = 0; $i < $columns; $i++) {
    $field=mysql_field_name($fields, $i);
    if ($field!='customers_id') {
    $checked='';
    if ($admin_access[$field] == '1') $checked='checked';

    // colors
    switch ($group_access[$field]) {
            case '1':
            $color='#FF6969';
            break;
            case '2':
            $color='#69CDFF';
            break;
            case '3':
            $color='#6BFF7F';
            break;
            case '4':
            $color='#BFA8FF';
            break;
            case '5':
            $color='#FFE6A8';
            case '6':
            $color='#800000';
            case '7':
            $color='#333333';

    }
    $allow_admin = ($_SESSION['customer_id'] != 1 and $_GET['cID'] == 1) ? false : true;
    $input_field = ($c_admin_access['f_accounting_change'] == 1 and $allow_admin) ? '<input type="checkbox" name="access[]" value="'.$field.'"'.$checked.'>' : (($checked == 'checked') ? '<img src="images/check.gif" border="0" alt="Aktiv" title="Aktiv">' : '<img src="images/uncheck.gif" border="0" alt="Inaktiv" title="Inaktiv">');

      // ADDON: SYSTEM GRUPPEN NUR FUER ADMIN
      if($c_admin_access['f_accounting_system_groups'] != 1){
		switch( $group_access[$field] ){
			case 0:
				$show = false;
				break;
			default:
				$show = true;
				break;
		}
      }else{
	$show = true;
      }

      if($show){
	echo '<tr class="dataTable">
    <td style="border: 1px solid; border-color: #000000;" width="10" bgcolor="'.$color.'" >'.xtc_draw_separator('pixel_trans.gif',15, 15).'</td>
        <td width="100%" class="dataTableContentRow">
        '. $input_field.' '.$field.'</td>
        <td></td></tr>';
      }
    }
}
?>
    </table>
<?php 
if($c_admin_access['f_accounting_change'] == 1){
	echo xtc_image_submit('button_save.gif', IMAGE_SAVE,'style="cursor:hand" onClick="return confirm(\''.SAVE_ENTRY.'\')"'); 
}
?>
</td>

<!-- body_text_eof //-->
  </tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
