<?php
/*
###################################################################################
  Bigware Shop 2.3
  Release Datum: 23.08.2015
  
  Bigware Shop
  http://www.bigware.de

  Copyright (c) 2015 Bigware LTD
  (c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
  (c) 2015	Bigware LTD

  Copyrightvermerke dürfen nicht entfernt werden.
  ------------------------------------------------------------------------
  Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
  der GNU General Public License, wie von der Free Software Foundation
  veroeffentlicht, weitergeben und/oder modifizieren, entweder gemaess Version 2 
  der Lizenz oder (nach Ihrer Option) jeder spaeteren Version.
  Die Veroeffentlichung dieses Programms erfolgt in der Hoffnung, dass es Ihnen
  von Nutzen sein wird, aber OHNE IRGENDEINE GARANTIE, sogar ohne die
  implizite Garantie der MARKTREIFE oder der VERWENDBARKEIT FUER EINEN
  BESTIMMTEN ZWECK. Details finden Sie in der GNU General Public License.
  
  Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
  Programm erhalten haben. Falls nicht, schreiben Sie an die Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

  Infos:
  ------------------------------------------------------------------------
  Der Bigware Shop wurde vor vielen Jahren bereits aus dem bekannten Shopsystem osCommerce
  weiter- und neuentwickelt.
  Der Bigware Shop legt im hohen Masse Wert auf Bedienerfreundlichkeit, beinhaltet eine leichte
  Installation, viele neue professionelle Werkzeuge und zeichnet sich aus durch eine große 
  Community, die bei Problemen weiterhelfen kann.
  
  Der Bigware Shop ist auf jedem System lauffaehig, welches eine PHP Umgebung
  (ab PHP 4.1.3) und mySQL zur Verfuegung stellt und auf Linux basiert.
 
  Hilfe erhalten Sie im Forum auf www.bigware.de 
  
  -----------------------------------------------------------------------
  
 ##################################################################################




*/

require('load_this/load_bigware_11.php');	

				$name_der_csv = "../picture/artikel.csv";	
			  @chmod ("$name_der_csv", 0777);	  		
	
				if ( (file_exists($name_der_csv)) && (is_writeable($name_der_csv)) && $htmlseite == "1"){  
					  $fh=fopen("$name_der_csv","w");
				
				
				
				
							      $status = go_db_producing_input($_POST['status']);
							      if ($status == "0") {
								  		$orders_query_raw = "select o.orders_id, o.attendees_id, o.attendees_name, o.attendees_group_id, o.attendees_company, o.payment_method, o.shipping_method, o.date_purchased, o.last_modified, o.currency, o.currency_value, s.orders_status_name, ot.text as order_total from ((" . DB_TBL_ORDERS . " o) left join " . DB_TBL_ORDERS_TOTAL . " ot on (o.orders_id = ot.orders_id), " . DB_TBL_ORDERS_STATUS . " s) where o.orders_status = s.orders_status_id and s.language_id = '" . (int)$languages_id . "' and ot.class = 'ot_total' order by o.orders_id DESC";
								  	} else {
							      	$orders_query_raw = "select o.orders_id, o.attendees_id, o.attendees_name, o.attendees_group_id, o.attendees_company, o.payment_method, o.shipping_method, o.date_purchased, o.last_modified, o.currency, o.currency_value, s.orders_status_name, ot.text as order_total from ((" . DB_TBL_ORDERS . " o) left join " . DB_TBL_ORDERS_TOTAL . " ot on (o.orders_id = ot.orders_id), " . DB_TBL_ORDERS_STATUS . " s) where o.orders_status = s.orders_status_id and s.language_id = '" . (int)$languages_id . "' and s.orders_status_id = '" . (int)$status . "' and ot.class = 'ot_total' order by o.orders_id DESC";
							      }
				
				
					  
					 	$str = "orders id;customer id;customer name;order date;total value;total weight;shipping methode;order status name;\n";	
					  fwrite($fh,$str); 
					  $orders_query = go_db_query($orders_query_raw);
						while ($orders = go_db_fetch_array($orders_query)) {
				
									      $weight_query = go_db_query("select pd.items_quantity, p.items_weight from " . DB_TBL_ITEMS . " p, " . DB_TBL_ORDERS_ITEMS . " pd, " . DB_TBL_ORDERS . " po where pd.orders_id = po.orders_id and p.items_id = pd.items_id and pd.orders_id = '" . $orders['orders_id'] . "' and po.orders_id = '" . $orders['orders_id'] . "'"); 
									      $count_weight = 0;
									      while ($weight_result = go_db_fetch_array($weight_query)) {  
									        $count_weight += $weight_result['items_quantity']*$weight_result['items_weight'];
									      }
									      if ( $count_weight == 0 ){
									      	$geamtgewicht = '0,00';
									      }
									      else{
									      	$geamtgewicht = preg_replace('/\./', ',', round ($count_weight,2));  
									      }  
				
								$orders_id = $orders['orders_id'];				
								$attendees_id = $orders['attendees_id'];
								$attendees_name = preg_replace('/\r\n|\r|\n|\n\r|\/\/|;/', '', $orders['attendees_name']);
								$bestelldatum = go_datetime_short($orders['date_purchased']);
								$gesamtwert = preg_replace('/\./', ',', strip_tags($orders['order_total']));
								$gesamtwert = preg_replace('/€|\$/', '', $gesamtwert);
								$shipping_methode = preg_replace('/\r\n|\r|\n|\n\r|\/\/|;/', '', $orders['shipping_method']);
								$order_status_name = preg_replace('/\r\n|\r|\n|\n\r|\/\/|;/', '', $orders['orders_status_name']);
				
				
							
							$str = "$orders_id;$attendees_id;$attendees_name;$bestelldatum;$gesamtwert;$geamtgewicht;$shipping_methode;$order_status_name;\n";	
								
							fwrite($fh,$str);				
						}
					  fclose($fh);
					  
						header("Content-type: application/x-octet-stream"); 
						header("Content-Disposition: attachment; filename=artikel.csv"); 
						readfile('../picture/artikel.csv');
				}
	

else {
        $languages = go_get_languages();
        for ($i=0, $n=sizeof($languages); $i<$n; $i++) {
        	if ($languages[$i]['code'] == $language){
	          $language_id = $languages[$i]['id'];
        	}
        }	
		
	  $action = (isset($_GET['action']) ? $_GET['action'] : '');

if ($htmlseite == "1"){	
	if ( (file_exists($name_der_csv)) && (!is_writeable($name_der_csv)) ) {
	
	        $file_writeable = false;
	        $messageStack->reset();
	        $messageStack->add(sprintf('Die Datei ../picture/artikel.csv ist nicht schreibbar. Bitte setzen Sie diese auf chmod 777.', $name_der_csv), 'error');
	        //echo $messageStack->output();
	
	}	
}


	$orders_statuses = array();
  $orders_statuses[] = array('id' => '0',
                             'text' => ''. TEXT_STATUSE . '' );
  $orders_status_query = go_db_query("select orders_status_id, orders_status_name from " . DB_TBL_ORDERS_STATUS . " where language_id = '" . $language_id . "'");
  while ($orders_status = go_db_fetch_array($orders_status_query)) {
    $orders_statuses[] = array('id' => $orders_status['orders_status_id'],
                               'text' => $orders_status['orders_status_name']);
  }
	?>
	<!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 CHARSET; ?>">
	<title><?php echo TITLE; ?></title>
	<link rel="stylesheet" type="text/css" href="load_this/style.css">
	<script language="javascript" src="load_this/menu.js"></script>
	<script language="JavaScript" type="text/JavaScript">
	<!--
	function ow(seite)
	  { 	
		w1 = window.open(seite, 'LEAF', 'toolbar=no,status=no,width=500,height=600,directories=no,scrollbars=yes,location=no,resize=yes,menubar=no');
	  	w1.focus();
	}
	//-->
	</script>
	</head>
	<body  marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
	<!-- header //-->
	<?php require(FOLDER_RELATIV_INCLUDES . 'load_bigware_9.php'); ?>
	<!-- header_eof //-->
	
	<!-- body //-->
	
	<table border="0" width="100%" cellspacing="2" cellpadding="2">
	  <tr>
	    <td width="<?php echo FRAME_WIDTH; ?>" valign="top"></td>
	<!-- body_text //-->
	    <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="0">
	      <tr>
	        <td width="100%"><table border="0" width="100%" cellspacing="0" cellpadding="0">
	          <tr>
	            <td class="pageGeneralsign"><?php echo ABOVE_TITLE; ?></td>
	            <td class="pageGeneralsign" align="right"><?php echo go_fetch_dividing_up('tranparentes.gif', ABOVE_PICTURE_WIDTH, ABOVE_PICTURE_HEIGHT); ?></td>
	          </tr>
	        </table></td>
	      </tr>
	      <tr>
	        <td><table border="0" width="100%" cellspacing="0" cellpadding="2">
	
	          <tr>
	            <td>
	            
	
	            
		<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" name="einlesen">

		  <table border="0" cellpadding="5" cellspacing="1" style="border-collapse: collapse" width="100%" id="AutoNumber1">
	        <tr class=infoFrameInside>
	          <td width="33%" valign="top" class="TbInsideData">
	
	

	
	              <?php echo go_picture(FOLDER_RELATIV_PICTURES . 'icon_arrow_right.gif'); ?> 
	              <?php echo TEXT_EXPLAIN; ?><br><br>
	              <?php echo TEXT_EXPLAIN_STATUS; ?><br><br>
	              <?php echo go_fetch_pull_down_menu('status', $orders_statuses); ?>
		          </td>
	            </tr>
	          </table>
	
				
	<br>
	<?php echo go_picture_submit('button_update.gif', PICTURE_UPDATE); ?>
					
	
	
	 <br><br>
		      </td>
	        </tr>
	      </table>
		 
		  <p>
		  <br>
	<input type="hidden" name="htmlseite" value="1">
		  
	</p>
	</form>     
	         
	            </td>
	              </tr>
	            </table></td>
	          </tr>
	
	          <tr>
	            <td><table border="0" cellpadding="0" cellspacing="2">
	              <tr>
	                <td colspan="2"><?php echo go_fetch_dividing_up('tranparentes.gif', '1', '10'); ?></td>
	              </tr>
	
	<!-- body_text_eof //-->
	        </table></td>
	  </tr>
	</table>
	
	<!-- body_eof //-->
	
	<!-- footer //-->
	<?php require(FOLDER_RELATIV_INCLUDES . 'load_bigware_7.php'); ?>
	<!-- footer_eof //-->
	<br>
	</body>
	</html>
	<?php 
	
	require(FOLDER_RELATIV_INCLUDES . 'load_bigware_6.php'); 
	
	
}
?>