fmsystem-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Fmsystem-commits] [9834] proprty: exportfilter


From: Sigurd Nes
Subject: [Fmsystem-commits] [9834] proprty: exportfilter
Date: Mon, 06 Aug 2012 13:59:03 +0000

Revision: 9834
          http://svn.sv.gnu.org/viewvc/?view=rev&root=fmsystem&revision=9834
Author:   sigurdne
Date:     2012-08-06 13:59:03 +0000 (Mon, 06 Aug 2012)
Log Message:
-----------
proprty: exportfilter

Added Paths:
-----------
    trunk/property/inc/export/default/El_anlegg_LRS

Added: trunk/property/inc/export/default/El_anlegg_LRS
===================================================================
--- trunk/property/inc/export/default/El_anlegg_LRS                             
(rev 0)
+++ trunk/property/inc/export/default/El_anlegg_LRS     2012-08-06 13:59:03 UTC 
(rev 9834)
@@ -0,0 +1,563 @@
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @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: Basware_X114 9585 2012-06-13 18:22:44Z sigurdne $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       phpgw::import_class('phpgwapi.datetime');
+       phpgw::import_class('phpgwapi.xmlhelper');
+
+       class export_conv
+       {
+               //var $fil_katalog='c:/temp'; //On windows use 
"//computername/share/filename" or "\\\\computername\share\filename" to check 
files on network shares.
+
+               var $debug;
+               var $client_code = 14;
+               protected $connection = false;
+               protected $global_lock = false;
+               protected $orders_affected = array();
+
+               function  __construct()
+               {
+                       $GLOBALS['phpgw_info']['flags']['currentapp']   =       
'property';
+                       $this->currentapp                       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->db                                       = & 
$GLOBALS['phpgw']->db;
+                       $this->join                                     = & 
$this->db->join;
+
+                       $this->soXport                          = 
CreateObject('property.soXport');
+                       $this->config                           = 
CreateObject('admin.soconfig',$GLOBALS['phpgw']->locations->get_id('property', 
'.invoice'));
+                       $this->cats                                     = 
CreateObject('phpgwapi.categories', -1, 'property', '.project');
+                       $this->cats->supress_info       = true;
+
+               }
+
+
+               protected function get_anlegg()
+               {
+                       $sql= "SELECT * FROM fm_entity_1_11 WHERE 
auto_kontering = 1";
+
+                       $this->db->limit_query($sql,__LINE__,__FILE__);
+                       $anlegg = array();
+
+                       $PeriodFrom = date('Ym');
+                       $PeriodTo = 202012;
+                       while ($this->db->next_record())
+                       {
+                               $anlegg[]       = array
+                               (
+                                       'Attribute' => array
+                                       (
+                                               'ID'                    => 'TJ',
+                                               'Name'                  => 
'MALEPID',
+                                               'Value'                 => 
$this->db->f('ext_system_id2'),
+                                       //      'id'                    => 
$this->db->f('id'),
+                                               'Description'   => 
$this->db->f('address'),
+                                               'PeriodFrom'    => $PeriodFrom,
+                                               'PeriodTo'              => 
$PeriodTo,
+                                               'Status'                => 'N',
+                                               'RelationList'  => array
+                                               (
+                                                       'RelationValue' => array
+                                                       (
+                                                               array
+                                                               (
+                                                                       'ID'    
=> '',
+                                                                       'Name'  
=> 'FIRMA',
+                                                                       'Value' 
=> 'BBB',
+                                                                       
'Description'   => '',
+                                                                       
'PeriodFrom'    => '',
+                                                                       
'PeriodTo'      => '',
+                                                                       
'Status'        => '',
+                                                                       
'Percentage'    => '100'
+                                                               ),
+                                                               array
+                                                               (
+                                                                       'ID'    
=> '',
+                                                                       'Name'  
=> 'Art',
+                                                                       'Value' 
=> '12304121',
+                                                                       
'Description'   => '',
+                                                                       
'PeriodFrom'    => '',
+                                                                       
'PeriodTo'      => '',
+                                                                       
'Status'        => '',
+                                                                       
'Percentage'    => '100'
+                                                               ),
+                                                               array
+                                                               (
+                                                                       'ID'    
=> '',
+                                                                       'Name'  
=> 'Ansvar',
+                                                                       'Value' 
=> '', // fylles ut
+                                                                       
'Description'   => '',
+                                                                       
'PeriodFrom'    => '',
+                                                                       
'PeriodTo'      => '',
+                                                                       
'Status'        => '',
+                                                                       
'Percentage'    => '100'
+                                                               ),
+                                                               array
+                                                               (
+                                                                       'ID'    
=> '',
+                                                                       'Name'  
=> 'Objekt',
+                                                                       'Value' 
=> $this->db->f('loc1'),
+                                                                       
'Description'   => '',
+                                                                       
'PeriodFrom'    => '',
+                                                                       
'PeriodTo'      => '',
+                                                                       
'Status'        => '',
+                                                                       
'Percentage'    => '100'
+                                                               ),
+                                                               array
+                                                               (
+                                                                       'ID'    
=> '',
+                                                                       'Name'  
=> 'Prosjekt',
+                                                                       'Value' 
=> '',
+                                                                       
'Description'   => '',
+                                                                       
'PeriodFrom'    => '',
+                                                                       
'PeriodTo'      => '',
+                                                                       
'Status'        => '',
+                                                                       
'Percentage'    => '100'
+                                                               ),
+                                                               array
+                                                               (
+                                                                       'ID'    
=> '',
+                                                                       'Name'  
=> 'Fagkode',
+                                                                       'Value' 
=> '999',
+                                                                       
'Description'   => '',
+                                                                       
'PeriodFrom'    => '',
+                                                                       
'PeriodTo'      => '',
+                                                                       
'Status'        => '',
+                                                                       
'Percentage'    => '100'
+                                                               ),
+                                                               array
+                                                               (
+                                                                       'ID'    
=> '',
+                                                                       'Name'  
=> 'Prosent',
+                                                                       'Value' 
=> '100',
+                                                                       
'Description'   => '',
+                                                                       
'PeriodFrom'    => '',
+                                                                       
'PeriodTo'      => '',
+                                                                       
'Status'        => '',
+                                                                       
'Percentage'    => '100'
+                                                               ),
+                                                       )
+                                               )
+                                       )
+                               );
+                       }
+                       $AttributeList = array('AttributeList' => $anlegg);
+//_debug_array($AttributeList);die();
+                       
+                       $xmldata = phpgwapi_xmlhelper::toXML($AttributeList, 
'AttributeList');
+                       $doc = new DOMDocument('1.0', 'utf-8');
+                       $doc->preserveWhiteSpace = true;
+                       $doc->loadXML( $xmldata );
+
+                       $doc->formatOutput = true;
+
+                       $xml = trim($doc->saveXML());
+                       echo $xml; die();
+
+/*
+MålepunktID    =       Nøkkel
+Firma          =       BB
+Art            =       Skal fylles ut
+Ansvar         =       Skal fylles ut
+Objekt         =       Skal fylles ut
+Prosjekt       =       kan leveres blank, valgfri i konteringsregel
+Fagkode        =       kan leveres blank, forslag 999 i konteringsregel
+Prosent                =       100% hvis hele beløpet skal fordeles på samme 
kontering.
+
+*/
+
+//_debug_array($anlegg);die();
+                       return $xml;
+               }
+
+               protected function log_end($batchid)
+               {
+                       $tid=date($this->soXport->datetimeformat);
+                       $sql= "insert into fm_ecologg (batchid,melding,tid) 
values ('$batchid','End transfer','$tid')";
+                       $this->db->query($sql,__LINE__,__FILE__);
+               }
+
+               protected function log_error($batchid,$error_desr)
+               {
+                       $tid = date($this->soXport->datetimeformat);
+                       $sql = "INSERT INTO fm_ecologg 
(batchid,ecobilagid,status,melding,tid) VALUES 
('$batchid',NULL,0,'$error_desr','$tid')";
+                       $this->db->query($sql,__LINE__,__FILE__);
+               }
+
+               protected function increment_batchid()
+               {
+                       $this->db->query("UPDATE fm_idgenerator SET value = 
value + 1 WHERE name = 'Ecobatchid'",__LINE__,__FILE__);
+                       $this->db->query("SELECT value from fm_idgenerator 
WHERE name = 'Ecobatchid'",__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $batchid = $this->db->f('value');
+                       return $batchid;
+               }
+
+               protected function next_batchid()
+               {
+                       $this->db->query("SELECT value from fm_idgenerator 
WHERE name = 'Ecobatchid'",__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $batchid = $this->db->f('value')+1;
+                       return $batchid;
+               }
+
+               //Lagre start melding
+               protected function log_start($batchid)
+               {
+                       $tid = date($this->db->datetime_format());
+                       $sql= "INSERT INTO fm_ecologg (batchid,melding,tid) 
VALUES ('$batchid','Start transfer','$tid')";
+                       $this->db->query($sql,__LINE__,__FILE__);
+               }
+
+               protected function get_vendor_info($vendor_id='')
+               {
+                       $sql = "SELECT org_nr, konto_nr FROM fm_vendor WHERE 
id='$vendor_id'";
+                       $this->db->query($sql,__LINE__,__FILE__);
+                       $this->db->next_record();
+
+                       $vendor_info= array
+                       (
+                               'org_nr' => $this->db->f('org_nr'),
+                               'konto_nr' => $this->db->f('konto_nr')
+                       );
+
+                       return $vendor_info;
+               }
+
+               protected function get_order_info($order_id='')
+               {
+                       $order_info = array();
+                       $sql = "SELECT type FROM fm_orders WHERE 
id='$order_id'";
+                       $this->db->query($sql,__LINE__,__FILE__);
+                       $this->db->next_record();
+
+                       switch($this->db->f('type'))
+                       {
+                               case 'workorder':
+                                       $sql2 = "SELECT title, category FROM 
fm_workorder WHERE id='$order_id'";
+                                       
$this->db->query($sql2,__LINE__,__FILE__);
+                                       $this->db->next_record();
+                                       $order_info['title'] = 
$this->db->f('title');
+                                       $cat_id = (int)$this->db->f('category');
+                                       $category = 
$this->cats->return_single($cat_id);
+                                       $category_arr = 
explode('-',$category[0]['name']);
+                                       $order_info['category'] = 
(int)trim($category_arr[0]);
+                                       break;
+                               case 's_agreement':
+                                       $sql2 = "SELECT descr as title FROM 
fm_s_agreement WHERE id='$order_id'";
+                                       
$this->db->query($sql2,__LINE__,__FILE__);
+                                       $this->db->next_record();
+                                       $order_info['title'] = 
$this->db->f('title');
+                                       break;
+                       }
+
+                       return $order_info;
+               }
+
+
+
+               public function overfor($download)
+               {
+//                     $download = 'on';
+//                     $download = False;
+//                     $this->debug=True;
+
+                       //Velg ut alle anlegg som skal overføres
+                       $anlegg = $this->get_anlegg();
+
+
+                       //Bestem filnavn
+                       $external_ref = '';
+                       $Filnavn = $this->LagFilnavn($external_ref);
+
+                       if (!$Filnavn)
+                       {
+                               $message='LagFilnavn: Filnavn er i bruk';
+                               $this->errorhandler($batchid,$message);
+                               return $message;
+                       }
+
+                       //Test om filen kan opprettes og skrives til
+                       if (@fopen($Filnavn, "wb"))
+                       {
+                               unlink($Filnavn);
+                       }
+                       else
+                       {
+                               $message='kan ikke lagre til fil: '. $Filnavn 
.'<br>';
+                               if($this->debug)
+                               {
+                                       echo $message;
+                               }
+                               else
+                               {
+                                       return $message;
+                               }
+                       }
+
+
+                       if ($anlegg)
+                       {
+                               $receipt['message'][]= array('msg' => 
$this->transfer_anlegg($anlegg, $Filnavn, $download));
+                       }
+
+
+                       if($this->connection)
+                       {
+                               switch 
($this->config->config_data['common']['method'])
+                               {
+                                       case 'ftp';
+                                               ftp_quit($this->connection);
+                                               break;
+                                       case 'ssh';
+                                               ssh2_exec($this->connection, 
'exit');
+                                               break;
+                               }
+                       }
+                       //Lagre melding
+                       if ($download=='on')
+                       {
+                               $this->log_end($batchid); //Lagre melding
+                       }
+
+                       return $receipt;
+               }
+
+               protected function errorhandler($batchid,$error_desr)
+               {
+                       if($this->db->get_transaction())
+                       {
+                               $this->db->transaction_abort();
+                       }
+
+                       $meld = $error_desr;
+
+                       //Vis feilmelding
+                       echo $meld;
+
+                       //Lagre feilmelding
+                       $this->log_error($batchid,$error_desr);
+               }
+
+
+               protected function LagFilnavn ($external_ref = '')
+               {
+                       if(!$external_ref)
+                       {
+                               $external_ref = mt_rand();
+                       }
+                       $fil_katalog = 
$this->config->config_data['export']['path'];
+                       $continue = True;
+                       $Filnavn = $fil_katalog . 
"/x114_14_{$external_ref}.xml";
+
+                       //Sjekk om filen eksisterer
+                       if (file_exists($Filnavn))
+                       {
+                               unlink($Filnavn);
+                       }
+
+                       return $Filnavn;
+               }
+
+               protected function transfer_anlegg($anlegg, $Filnavn, $download)
+               {
+
+                       $antall = count($anlegg);
+
+                       if ( $this->db->get_transaction() )
+                       {
+                               $this->global_lock = true;
+                       }
+                       else
+                       {
+                               $this->db->transaction_begin();
+                       }
+
+                       $tranfser_bilag = $bilagsnr_ut ? array($bilagsnr_ut) : 
array($voucher_id);
+
+                       $localtime = phpgwapi_datetime::user_localtime();
+
+                       $buffer = $xmldata;
+
+
+
+                       //Fullfør transaksjon
+                       if ($download=='on' && !$this->debug)
+                       {
+                       //      $file_written = true;
+
+                       // -- Start
+                        
+                               $file_written = false;
+                               $fp = fopen($Filnavn, "wb");
+                               fwrite($fp,$buffer);
+
+                               if(fclose($fp))
+                               {
+                                       $file_written=true;
+                               }
+
+                       // -- END
+                               if( $file_written && 
($this->config->config_data['common']['method'] != 'ftp' && 
$this->config->config_data['common']['method'] != 'ssh'))
+                               {
+                                       $transfer_ok = true;
+                               }
+                               else if($file_written)
+                               {
+                                       $transfer_ok = 
$this->transfer($buffer,$Filnavn,$batchid,$tranfser_bilag);
+                               }
+
+                               if($transfer_ok)
+                               {
+                                       if ( !$this->global_lock )
+                                       {
+                                               $this->db->transaction_commit();
+                                       }
+
+                                       $message = "Antall bilag/underbilag 
overfort: {$antall}, fil: {$Filnavn}";
+                               }
+                               else
+                               {
+                                       $this->db->transaction_abort();
+                                       $message = 'Noe gikk galt med 
overforing av godkjendte fakturaer!';
+                               }
+                       }
+                       else
+                       {
+                               $message = $buffer;
+                               $this->db->transaction_abort();
+
+                       }
+
+                       return $message;
+               }
+
+
+
+               protected function 
transfer($buffer,$Filnavn,$batchid,$tranfser_bilag)
+               {
+                       $transfer_ok = false;
+                       
if($this->config->config_data['common']['method']=='ftp' || 
$this->config->config_data['common']['method']=='ssh')
+                       {
+                               if(!$connection = $this->connection)
+                               {
+                                       $connection     = 
$this->phpftp_connect();
+                               }
+
+                               $basedir = 
$this->config->config_data['export']['remote_basedir'];
+                               if($basedir)
+                               {
+                                       $remote_file = $basedir . '/' . 
basename($Filnavn);
+                               }
+                               else
+                               {
+                                       $remote_file = basename($Filnavn);
+                               }
+
+                               switch 
($this->config->config_data['common']['method'])
+                               {
+                                       case 'ftp';
+                                               $transfer_ok = 
ftp_put($connection,$remote_file, $Filnavn, FTP_BINARY);
+                                               break;
+                                       case 'ssh';
+                                               $sftp = ssh2_sftp($connection);
+                                               $stream = 
@fopen("ssh2.sftp://$sftp$remote_file";, 'w');
+                                               $data_to_send = 
@file_get_contents($Filnavn);
+                                               fwrite($stream, $data_to_send);
+                                               $transfer_ok = @fclose($stream);
+                                               break;
+                                       default:
+                                               $transfer_ok = false;
+                               }
+                               if ($transfer_ok)
+                               {
+                                       for 
($i=0;$i<count($tranfser_bilag);$i++)
+                                       {
+               //                              
$this->soXport->log_transaction($batchid,$tranfser_bilag[$i],lang('Invoice 
transferred %1 to Basware',basename($Filnavn)));
+                                       }
+                               }
+                               else
+                               {
+                                       for 
($i=0;$i<count($tranfser_bilag);$i++)
+                                       {
+               //                              
$this->soXport->log_transaction($batchid,$tranfser_bilag[$i],lang('Failed to 
transfere %1 to Basware',basename($Filnavn)));
+                                       }
+                               }
+                               if(!$transfer_ok)
+                               {
+                                       unlink($Filnavn);
+                               }
+                       }
+                       return  $transfer_ok;
+               }
+
+               protected function phpftp_connect() 
+               {
+                       $server                         = 
$this->config->config_data['common']['host'];
+                       $user                           = 
$this->config->config_data['common']['user'];
+                       $password                       = 
$this->config->config_data['common']['password'];
+                       $port                           = 22;
+
+                       switch ($this->config->config_data['common']['method'])
+                       {
+                               case 'ftp';
+                                       if($connection = ftp_connect($server))
+                                       {
+                                               
ftp_login($connection,$user,$password);
+                                       }
+                                       break;
+                               case 'ssh';
+                                       if (!function_exists("ssh2_connect"))
+                                       {
+                                               die("function ssh2_connect 
doesn't exist");
+                                       }
+                                       if(!($connection = 
ssh2_connect("$server", $port)))
+                                       {
+                                               $message = "fail: unable to 
establish connection";
+                                               _debug_array($message);
+                                               //$receipt['error'][]= 
array('msg' => $message);
+                                       }
+                                       else
+                                       {
+                                               // try to authenticate with 
username root, password secretpassword
+                                               
if(!ssh2_auth_password($connection, $user, $password))
+                                               {
+                                                       $message = "fail: 
unable to authenticate";
+                                                       _debug_array($message);
+                                                       //$receipt['error'][]= 
array('msg' => $message);
+                                               }
+                                       }
+                                       break;
+                       }
+                       $this->connection = $connection;
+                       return $connection;
+               }
+       }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]