<?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 import
 	* @version $Id: Import_fra_Kemner_xml,v 1.12 2007/01/28 19:13:19 sigurdne Exp $
	*/

	/**
	 * Description
	 * @package property
	 */

	class import_conv
	{
		var $spbudact_code='11954111';
		var $dimb=99;
		var $mvakode=0;
		var $kildeid=1;
		var $splitt=0;
		var $soXport;
		var $invoice;

		var $import = array(
			'Bilagsnr' => 'bilagsnr', 
			'Fakturanr' => 'fakturanr', 
			'Gnr/Bnr-seksjon' => 'gid',
			'Konto' => 'spbudact_code',
			'Objekt' => 'dima', //objectclass: organizationalPerson
			'DimB' => 'dimb',
			'KID' => 'kidnr',
			'MVA' => 'mvakode',
			'Tjeneste'=> 'kostra_id',
			'Belp [kr]' => 'belop',
			'Referanse' => 'referanse',
			'BOEI Gateadresse' => 'boei_gateadresse',
			);

		var $header = array('Bilagsnr','Fakturanr','Gnr/Bnr-seksjon','Konto','Objekt','DimB','KID','MVA','Tjeneste','Belp [kr]','Referanse');

		function import_conv()
		{
			$this->currentapp	= $GLOBALS['phpgw_info']['flags']['currentapp'];
			$this->soXport 		= CreateObject($this->currentapp.'.soXport');	
			$this->invoice 		= CreateObject($this->currentapp.'.boinvoice');
			$this->bocommon		= CreateObject($this->currentapp.'.bocommon');
			$this->dateformat	= $this->bocommon->dateformat;
			$this->datetimeformat	= $this->bocommon->datetimeformat;
//			$this->xmltool		= CreateObject('phpgwapi.xmltool');
		}

		function import($invoice_common,$download)
		{
			$tsvfile	= $invoice_common['tsvfile'];
			$valid_data= False;
			$bilagsnr = $this->invoice->next_bilagsnr();

			$buffer = array();

/*			$xmldata=file_get_contents($tsvfile);
			$this->xmltool->import_xml($xmldata);
			$var_result = $this->xmltool->export_var();
*/
			$xmlparse = CreateObject('property.XmlToArray');
			$xmlparse->setEncoding('ISO-8859-1');
			$var_result = $xmlparse->parseFile($tsvfile);

			set_time_limit(300);

//_debug_array($var_result);

			if(is_array($var_result['Invoice'][0]))
			{
				$data=$var_result['Invoice'];
			}
			else
			{
				$data[0]=$var_result['Invoice'];
			}

			if (isset($data) AND is_array($data))
			{
				$num = count($data);
				$fakturadato = false;
				$forfallsdato = false;
				$periode = false;

				for ($i = 0; $i < $num; ++$i)
				{
					$Gnr 		= $data[$i]['Header'][0]['Text1'];
					$Bnr 		= $data[$i]['Header'][0]['Text2'];
					$sekjonnr 	= $data[$i]['Header'][0]['Text4'];

					$fakturanr 	= $data[$i]['InvoiceNo'];
					if(!$fakturadato)
					{
						$fakturadato	= date($this->dateformat,strtotime($data[$i]['Header'][0]['InvoiceDate']));
					}
					
					if(!$forfallsdato)
					{
						$forfallsdato	= date($this->dateformat,strtotime($data[$i]['Header'][0]['DueDate']));
					}

					if(!$periode)
					{
						$periode = date('n',strtotime($data[$i]['Header'][0]['InvoiceDate']));
					}
					
					$belop 	= $data[$i]['Summary'][0]['TotalInclTax'];
					$kidnr 	= $data[$i]['Header'][0]['PaymentInfo'][0]['BacsId'];

					$gid = $Gnr.'/'.$Bnr.'-'.$sekjonnr;
					$item_type = 1;
					$item_id = '1201' . $data[$i]['Header'][0]['HeaderText'];
					
					$buffer[$i]['fakturanr'] = $fakturanr;
					$buffer[$i]['periode'] = $periode;
					$buffer[$i]['forfallsdato'] = $forfallsdato;
					$buffer[$i]['fakturadato'] = $fakturadato;
					$buffer[$i]['belop'] = $belop;
					$buffer[$i]['godkjentbelop'] = $belop;
					
					$buffer[$i]['kidnr'] = $kidnr;
					$buffer[$i]['gid'] = $gid;
					$buffer[$i]['bilagsnr'] = $bilagsnr;

					if(isset($invoice_common['dim_b']) && $invoice_common['dim_b'])
					{
						$buffer[$i]['dimb'] = $invoice_common['dim_b'];
					}
					else
					{
						$buffer[$i]['dimb'] = $this->dimb;
					}

					$buffer[$i]['item_type'] = $item_type;
					$buffer[$i]['item_id'] = $item_id;

			        //Finn dima fra Boei
					$gabinfo = $this->soXport->gabnr_to_objekt($Gnr,$Bnr,$sekjonnr);
					$buffer[$i]['dima'] = $gabinfo['dima'];
					$buffer[$i]['loc1'] = $gabinfo['loc1'];
					$buffer[$i]['mvakode'] = $this->mvakode;

					if($gabinfo['loc1'] && (isset($invoice_common['auto_tax']) && $invoice_common['auto_tax']))
					{
						$mvakode=$this->soXport->auto_tax($gabinfo['loc1']);
					
						if($mvakode)
						{
							$buffer[$i]['mvakode'] = $mvakode;
						}
					}

					if(isset($invoice_common['auto_tax']) && $invoice_common['auto_tax'])
					{
						$buffer[$i]['mvakode'] = $this->soXport->tax_b_account_override($buffer[$i]['mvakode'] ,$this->spbudact_code);
						$buffer[$i]['mvakode'] = $this->soXport->tax_vendor_override($buffer[$i]['mvakode'] ,$invoice_common['vendor_id']);
					}

					$buffer[$i]['kostra_id'] = $this->soXport->get_kostra_id($gabinfo['loc1']);
					
					//finn boei gateadresse for kontroll
					$boei_gateadresse=$this->soXport->dima_to_address($gabinfo['dima']);
					$buffer[$i]['boei_gateadresse'] = $boei_gateadresse;

					$referanse = $data[$i]['Header'][0]['ShipTo'][0]['AddressInfo'][0]['Address'];
	
					$buffer[$i]['referanse'] = $referanse;                

					$merknad = '';

					$merknad = 'Kemner adr: '.$referanse."\r\n".'BOEI adr: '.$boei_gateadresse."\r\n";

					foreach ($data[$i]['Details'][0]['Detail'] as $detail)
					{
						if (is_array($detail['Products'][0]['SellerProductDescr']))
						{
							$merknad .= implode("",$detail['Products'][0]['SellerProductDescr']) . "\t";
						}
						else
						{
							$merknad .= $detail['Products'][0]['SellerProductDescr'] . "\t";
						}
						

						if (is_array($detail['Products'][0]['UnitCode']))
						{
							$merknad .= implode("",$detail['Products'][0]['UnitCode']) . "\t";
						}
						else
						{
							$merknad .= $detail['Products'][0]['UnitCode'] . "\t";
						}
						
						if (is_array($detail['Products'][0]['Quantity']))
						{
							$merknad .= implode("",$detail['Products'][0]['Quantity']) . "\t";
						}
						else
						{
							$merknad .= $detail['Products'][0]['Quantity'] . "\t";
						}
						
						if (is_array($detail['Products'][0]['Price']))
						{
							$merknad .= implode("",$detail['Products'][0]['Price'])."\r\n";
						}
						else
						{
							$merknad .= $detail['Products'][0]['Price']."\r\n";
						}

					}
					
					$buffer[$i]['merknad'] = $merknad;
					$buffer[$i]['splitt'] = $this->splitt;
					$buffer[$i]['kildeid'] = $this->kildeid;
					$buffer[$i]['spbudact_code'] = $this->spbudact_code;
					$buffer[$i]['typeid'] = $invoice_common['type'];
					$buffer[$i]['regtid'] = date($this->datetimeformat);
					$buffer[$i]['artid'] = $invoice_common['art'];
					$buffer[$i]['spvend_code'] = $invoice_common['vendor_id'];
					$buffer[$i]['oppsynsmannid'] = $invoice_common['janitor'];
					$buffer[$i]['saksbehandlerid'] = $invoice_common['supervisor'];
					$buffer[$i]['budsjettansvarligid'] = $invoice_common['budget_responsible'];
//_debug_array($bilagsnr);
					$bilagsnr++;
				}
			}

			if(!$download)
			{
				$buffer = $this->import_end_file($buffer,$invoice_common['bilagsnr']);
			}

			return $buffer;
		}

		function import_end_file($buffer,$bilagsnr)
		{
			$num	= $this->soXport->add($buffer);
			$receipt['message'][]= array('msg' => lang('Successfully imported %1 records into your invoice register.',$num).' '.lang('ID').': '. $bilagsnr);
			return $receipt;
		}
	}
