<?php
	/**
	* phpGroupWare - property: a Facilities Management System.
	*
	* @author Sigurd Nes <sigurdne@online.no>
	* @copyright Copyright (C) 2003,2004,2005,2006,2007 Free Software Foundation, Inc. http://www.fsf.org/
	* This file is part of phpGroupWare.
	*
	* phpGroupWare is free software; you can redistribute it and/or modify
	* it under the terms of the GNU General Public License as published by
	* the Free Software Foundation; either version 2 of the License, or
	* (at your option) any later version.
	*
	* phpGroupWare is distributed in the hope that it will be useful,
	* but WITHOUT ANY WARRANTY; without even the implied warranty of
	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	* GNU General Public License for more details.
	*
	* You should have received a copy of the GNU General Public License
	* along with phpGroupWare; if not, write to the Free Software
	* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
	*
	* @license http://www.gnu.org/licenses/gpl.html GNU General Public License
	* @internal Development of this application was funded by http://www.bergen.kommune.no/bbb_/ekstern/
	* @package property
	* @subpackage export
 	* @version $Id: GABNr,v 1.10 2007/01/26 14:58:55 sigurdne Exp $
	*/

	/**
	 * Description
	 * @package property
	 */

	class export_conv
	{
		function export_conv()
		{

			$this->currentapp	= $GLOBALS['phpgw_info']['flags']['currentapp'];
			$this->soXport 		= CreateObject($this->currentapp.'.soXport');	
			$this->config 		= CreateObject('phpgwapi.config','property');
			$this->socommon 	= CreateObject($this->currentapp.'.socommon');
			$this->solocation 	= CreateObject($this->currentapp.'.solocation');
			$this->config->read_repository();
			
	//		$this->db = $GLOBALS['phpgw']->db;
			$this->db    = $this->socommon->new_db();

			$this->join			= $this->socommon->join;

		}

		function overfor($download)
		{
			//Generer batch ID
			$batchid = $this->soXport->next_batchid();
		
			$receipt['message'][]= array('msg' => $this->OverforGAB($batchid,$download));

			return $receipt;
		}		
		
		
		function LagFilnavn ($batchid)
		{	
			$fil_katalog = $this->config->config_data['export_path'];
			//Beregn lpenr
			$StartLopeNr = ($batchid % 30) + 70;
			$LopeNr = $StartLopeNr;
		
			do
			{
				//Konstruer filnavn
				$Filnavn = $fil_katalog . '/GABNr'. sprintf("%2d",$LopeNr) . '10.TXT';
		
				//Sjekk om filen eksisterer
				If (!file_exists($Filnavn))
				{
					return $Filnavn;
				}
		
				//Genererer nytt lpenr
				$batchid = $batchid + 1;
				$LopeNr = ($batchid % 30) + 70;
			}
			while  ($LopeNr != $StartLopeNr);
		
			//Ingen lpenr er ledige, gi feilmelding
			return False;
		}
		
		function OverforGAB($batchid,$download)
		{
			//Bestem filnavn
			
			$Filnavn = $this->LagFilnavn($batchid);
			if (!$Filnavn)
			{
				$message='LagFilnavn'.lang('Alle lpenr for filnavn er i bruk!');
				return $message;
			}

			//Test om filen kan opprettes og skrives til
			if (@fopen($Filnavn, "wb"))
			{
				unlink($Filnavn);
			}
			else
			{
				$message='kan ikke lagre til fil: '. $Filnavn;
				return $message;
			}
			
			//Velg ut alle GABNr som skal overfres
		
			$sql = "SELECT loc1_name,gab_id,fm_gab_location.loc1,fm_gab_location.address FROM fm_gab_location $this->join fm_location1 on fm_gab_location.loc1 = fm_location1.loc1 WHERE owner = 'yes' AND ((fm_location1.category <> 99) OR (fm_location1.category IS NULL)) GROUP BY gab_id,fm_gab_location.loc1,loc1_name, fm_gab_location.address ORDER by gab_id asc";

			$this->db->query($sql,__LINE__,__FILE__);

			while ($this->db->next_record())
			{
				$gab_list[] = array
				(
					'gab_id' => $this->db->f('gab_id'),
					'location_code' => $this->db->f('loc1'),
					'address' => $this->db->f('address'),
					'loc1_name' => $this->db->f('loc1_name'),
					);
			}									

			$cols_extra=array(0=>'loc1_name');
			
			$gab_count = count($gab_list);
			
/*			for ($i=0;$i<$gab_count;$i++)
			{
				$location_data=$this->solocation->read_single($gab_list[$i]['location_code']);
				for ($j=0;$j<count($cols_extra);$j++)
				{
					$gab_list[$i][$cols_extra[$j]] = $location_data[$cols_extra[$j]];
				}

			}
*/
			$buffer = 'GABNr'. '	' .'Objekt'.'	'.'Adresse' . "\r\n";

		 	$check_gab_id=array();
		 	
		 	for ($k=0;$k<$gab_count;$k++)
			{				
				if (!$check_gab_id[$gab_list[$k]['gab_id']])
				{
					$check_gab_id[$gab_list[$k]['gab_id']]=True;
				
					$buffer .=substr($gab_list[$k]['gab_id'],-20). '	' .$gab_list[$k]['location_code']. '	' .$gab_list[$k]['address']."\r\n";
				
					$antall = $antall + 1;
				}
			}
		
//			$buffer .= 'antall:'.'	'.$antall;

			if ($download=='on')
			{
				$message = 'Antall GABNr overfrt:'.$antall . ' ,Til fil: ' . $Filnavn;
				$fp = fopen($Filnavn, "wb");
				fwrite($fp,$buffer);
				fclose($fp);
			}
			else
			{
				$message = $buffer;
			}
			return $message;
		}
	}
?>
