[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Fmsystem-commits] [14942] property: export order to accouting system
From: |
Sigurd Nes |
Subject: |
[Fmsystem-commits] [14942] property: export order to accouting system |
Date: |
Tue, 26 Apr 2016 13:45:28 +0000 |
Revision: 14942
http://svn.sv.gnu.org/viewvc/?view=rev&root=fmsystem&revision=14942
Author: sigurdne
Date: 2016-04-26 13:45:26 +0000 (Tue, 26 Apr 2016)
Log Message:
-----------
property: export order to accouting system
Added Paths:
-----------
trunk/property/inc/custom/default/BkBygg_exporter_ordre_til_Agresso.php
trunk/property/inc/custom/default/BkBygg_lag_Agresso_ordre_fra_melding.php
Added: trunk/property/inc/custom/default/BkBygg_exporter_ordre_til_Agresso.php
===================================================================
--- trunk/property/inc/custom/default/BkBygg_exporter_ordre_til_Agresso.php
(rev 0)
+++ trunk/property/inc/custom/default/BkBygg_exporter_ordre_til_Agresso.php
2016-04-26 13:45:26 UTC (rev 14942)
@@ -0,0 +1,435 @@
+<?php
+ /**
+ * phpGroupWare - property: a Facilities Management System.
+ *
+ * @author Sigurd Nes <address@hidden>
+ * @copyright Copyright (C) 2016 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 helpdesk
+ * @version $Id: class.uitts.inc.php 14940 2016-04-26 11:02:40Z
sigurdne $
+ */
+ /**
+ * Description
+ * @package property
+ */
+
+
+ /**
+ * Description of BkBygg_exporter_ordre_til_Agresso
+ *
+ * @author Sigurd Nes
+ */
+ class BkBygg_exporter_ordre_til_Agresso
+ {
+
+ var $dim0; // Art
+ var $dim1; // Ansvar
+ var $dim2; // Tjeneste
+ var $dim3; // Objekt
+ var $dim4; // Kontrakt - frivillig
+ var $dim5; // Prosjekt
+ var $dim6; // Aktivitet - frivillig
+ var $transfer_xml;
+ var $connection;
+ var $order_id;
+
+ public function __construct( $param )
+ {
+ $this->db = & $GLOBALS['phpgw']->db;
+ $this->soXport = CreateObject('property.soXport');
+ $this->config = CreateObject('admin.soconfig',
$GLOBALS['phpgw']->locations->get_id('property', '.invoice'));
+ $this->order_id = $param['order_id'];
+ }
+
+ public function create_transfer_xml( $param )
+ {
+ $Orders = array();
+ /*
+ UN-kodene i Agresso ligger med prefiks UN- foran
koden.
+ Eks: UN-70111601: Plantetjenester
+ */
+
+ $Seller = array(
+ 'Name' => $param['vendor_name'],
+ 'AddressInfo' => array(
+ array(
+ 'Address' =>
$param['vendor_address']
+ )
+ ),
+ 'SellerNo' => $param['vendor_id'],
+// 'SellerReferences' => array(
+// array(
+// 'SalesMan' => 12573,
+// )
+// )
+ );
+
+ $Header = array(
+ 'AcceptFlag' => 1,
+ 'OrderType]' => 'WB',
+ 'Status' => 'N',
+ 'OrderDate' => date('Y-m-d'),
+ 'Currency' => 'NOK',
+ 'Seller' => array($Seller),
+ 'Buyer' => array($param['buyer']),
+ );
+
+ $DetailInfo = array();
+ $DetailInfo[] = array(
+ 'ReferenceCode' => array(
+ 'Code' => 'A0',
+ 'Value' => $param['dim0'] // Art
+ )
+ );
+ $DetailInfo[] = array(
+ 'ReferenceCode' => array(
+ 'Code' => 'C1',
+ 'Value' => $param['dim1'] // Ansvar
+ )
+ );
+ $DetailInfo[] = array(
+ 'ReferenceCode' => array(
+ 'Code' => 'Q0',
+ 'Value' => $param['dim2'] // Tjeneste
+ )
+ );
+ $DetailInfo[] = array(
+ 'ReferenceCode' => array(
+ 'Code' => 'F0',
+ 'Value' => $param['dim3'] // Objekt
+ )
+ );
+ $DetailInfo[] = array(
+ 'ReferenceCode' => array(
+ 'Code' => 'A7',
+ 'Value' => $param['dim4'] // Kontrakt
+ )
+ );
+ $DetailInfo[] = array(
+ 'ReferenceCode' => array(
+ 'Code' => 'B0',
+ 'Value' => $param['dim5'] // Prosjekt
+ )
+ );
+ $DetailInfo[] = array(
+ 'ReferenceCode' => array(
+ 'Code' => 'B1',
+ 'Value' => $param['dim6'] // Aktivitet
+ )
+ );
+ $DetailInfo[] = array(
+ 'ReferenceCode' => array(
+ 'Code' => 'A1',
+ 'Value' => $param['tax_code'] // Moms
kode
+ )
+ );
+
+ $Detail = array();
+ $i = 1;
+ foreach ($param['lines'] as $line)
+ {
+
+ $Detail[] = array(
+ 'LineNo' => $i,
+ 'Status' => 'N',
+ 'BuyerProductCode' =>
$line['unspsc_code'], //74000176, //UN-kode
+ 'BuyerProductDescr' => $line['descr'],
//'Kopipapir',
+ 'UnitCode' => 'STK',
+ 'Quantity' => 100,
+ 'DetailInfo' => $DetailInfo
+ );
+
+ $i++;
+ }
+
+
+ $Orders['Order'][] = array(
+ 'OrderNo' => $param['order_id'],
+ 'VoucherType' => 'IV',
+ 'TransType' => 41,
+ 'Header' => array($Header),
+ 'Details' => array('Detail' => $Detail)
+ );
+
+// _debug_array($Orders);
+// die();
+
+ $root_attributes = array(
+ 'Version' => "542",
+ 'xmlns:xsi' =>
"http://www.w3.org/2001/XMLSchema-instance",
+ 'xsi:noNamespaceSchemaLocation' =>
"http://services.agresso.com/schema/ABWOrder/2004/07/02/ABWOrder.xsd"
+ );
+ $xml_creator = new xml_creator('ABWOrder',
$root_attributes);
+ $xml_creator->fromArray($Orders);
+ $this->transfer_xml = $xml_creator->getDocument();
+ return $this->transfer_xml;
+ // $xml_creator->output();
+ // die();
+ }
+
+ /**
+ * Output the content of a current xml document.
+ * @access public
+ * @param null
+ */
+ public function output()
+ {
+ header('Content-type: text/xml');
+ echo $this->transfer_xml;
+ }
+
+
+ protected function create_file_name( $ref = '' )
+ {
+ if (!$ref)
+ {
+ throw new
Exception('BkBygg_exporter_ordre_til_Agresso::create_file_name() Mangler
referanse');
+ }
+ $fil_katalog =
$this->config->config_data['export']['path'];
+
+ $filename = "{$fil_katalog}/FDV_{$ref}.xml";
+
+ //Sjekk om filen eksisterer
+ if (file_exists($filename))
+ {
+ unlink($filename);
+ }
+
+ return $filename;
+ }
+
+ public function transfer( )
+ {
+ $this->db->transaction_begin();
+
+ $filename = $this->create_file_name($this->order_id);
+ $batchid = $this->soXport->increment_batchid();
+ $content = $this->transfer_xml;
+
+ if(false) // keep a copy?
+ {
+ $file_written = false;
+ $fp = fopen($filename, "wb");
+ fwrite($fp, $buffer);
+
+ if (fclose($fp))
+ {
+ $file_written = true;
+ }
+ }
+
+ $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($filename);
+ }
+ else
+ {
+ $remote_file = basename($filename);
+ }
+
+ switch
($this->config->config_data['common']['method'])
+ {
+ case 'ftp';
+ $tmp = tmpfile();
+ fwrite($tmp, $content);
+ rewind($tmp);
+ $transfer_ok =
ftp_fput($connection, $remote_file, $tmp, FTP_BINARY);
+ fclose($tmp);
+ // $transfer_ok =
ftp_put($connection, $remote_file, $filename, FTP_BINARY);
+ break;
+ case 'ssh';
+ $sftp = ssh2_sftp($connection);
+ $stream =
@fopen("ssh2.sftp://$sftp$remote_file", 'w');
+ fwrite($stream, $content);
+ $transfer_ok = @fclose($stream);
+ break;
+ default:
+ $transfer_ok = false;
+ }
+ if ($transfer_ok)
+ {
+
$this->soXport->log_transaction($batchid, $this->order_id, lang('transferred
Order %1 to Agresso', basename($filename)));
+ $this->db->transaction_commit(); //
Reverse the batch_id - increment
+ }
+ else
+ {
+ $this->db->transaction_abort();
+
$this->soXport->log_transaction($batchid, $this->order_id, lang('Failed to
transfere Order %1 to Agresso', basename($filename)));
+ // @unlink($filename);
+ }
+ }
+ return $transfer_ok;
+ }
+
+ 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;
+ }
+ }
+
+ class xml_creator extends XMLWriter
+ {
+
+ /**
+ * Constructor.
+ * @param string $prm_rootElementName A root element's name of
a current xml document
+ * @param ARRAY $root_attributtes array of root attributes.
+ * @param string $prm_xsltFilePath Path of a XSLT file.
+ * @access public
+ * @param null
+ */
+ public function __construct( $prm_rootElementName,
$root_attributes = array(), $prm_xsltFilePath = '' )
+ {
+ $this->openMemory();
+ $this->setIndent(true);
+ $this->setIndentString(' ');
+ $this->startDocument('1.0', 'UTF-8');
+
+ if ($prm_xsltFilePath)
+ {
+ $this->writePi('xml-stylesheet',
'type="text/xsl" href="' . $prm_xsltFilePath . '"');
+ }
+
+ $this->startElement($prm_rootElementName);
+
+ foreach ($root_attributes as $key => $value)
+ {
+ $this->writeAttribute($key, $value);
+ }
+ }
+
+ /**
+ * Set an element with a text to a current xml document.
+ * @access public
+ * @param string $prm_elementName An element's name
+ * @param string $prm_ElementText An element's text
+ * @return null
+ */
+ public function setElement( $prm_elementName, $prm_ElementText )
+ {
+ $this->startElement($prm_elementName);
+ $this->text($prm_ElementText);
+ $this->endElement();
+ }
+
+ /**
+ * Construct elements and texts from an array.
+ * The array should contain an attribute's name in index part
+ * and a attribute's text in value part.
+ * @access public
+ * @param array $prm_array Contains attributes and texts
+ * @return null
+ */
+ public function fromArray( array $array )
+ {
+ foreach ($array as $key => $val)
+ {
+ if (is_array($val))
+ {
+ if (is_numeric($key))
+ {
+ // numeric keys aren't allowed
so we'll skip the key
+ $this->fromArray($val);
+ }
+ else
+ {
+ $this->startElement($key);
+ $this->fromArray($val);
+ $this->endElement();
+ }
+ }
+ else
+ {
+ $this->writeElement($key, $val);
+ }
+ }
+ }
+
+ /**
+ * Return the content of a current xml document.
+ * @access public
+ * @param null
+ * @return string Xml document
+ */
+ public function getDocument()
+ {
+ $this->endElement();
+ $this->endDocument();
+ return $this->outputMemory();
+ }
+
+ /**
+ * Output the content of a current xml document.
+ * @access public
+ * @param null
+ */
+ public function output()
+ {
+ header('Content-type: text/xml');
+ echo $this->getDocument();
+ }
+ }
\ No newline at end of file
Added:
trunk/property/inc/custom/default/BkBygg_lag_Agresso_ordre_fra_melding.php
===================================================================
--- trunk/property/inc/custom/default/BkBygg_lag_Agresso_ordre_fra_melding.php
(rev 0)
+++ trunk/property/inc/custom/default/BkBygg_lag_Agresso_ordre_fra_melding.php
2016-04-26 13:45:26 UTC (rev 14942)
@@ -0,0 +1,122 @@
+<?php
+ /**
+ * phpGroupWare - property: a Facilities Management System.
+ *
+ * @author Sigurd Nes <address@hidden>
+ * @copyright Copyright (C) 2016 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 helpdesk
+ * @version $Id: class.uitts.inc.php 14940 2016-04-26 11:02:40Z
sigurdne $
+ */
+ /**
+ * Description
+ * @package property
+ */
+
+
+ //if (false)
+ if(isset($data['order_id']) && $data['order_id'] &&
isset($data['save']) && $data['save'] && isset($data['vendor_email'][0]) &&
$data['vendor_email'][0])
+ {
+ $_ticket = $this->read_single($id);
+// _debug_array($_ticket);die();
+
+ $contacts = CreateObject('property.sogeneric');
+ $contacts->get_location_info('vendor', false);
+
+ $custom = createObject('property.custom_fields');
+ $vendor_data['attributes'] = $custom->find('property',
'.vendor', 0, '', 'ASC', 'attrib_sort', true, true);
+
+ $vendor_data = $contacts->read_single(array('id' =>
$_ticket['vendor_id']), $vendor_data);
+ if (is_array($vendor_data))
+ {
+ foreach ($vendor_data['attributes'] as $attribute)
+ {
+ if ($attribute['name'] == 'adresse')
+ {
+ $vendor['address'] =
$attribute['value'];
+ }
+ if ($attribute['name'] == 'org_name')
+ {
+ $vendor['name'] = $attribute['value'];
+ }
+ }
+ }
+ unset($contacts);
+
+
+ if (phpgw::get_var('on_behalf_of_assigned', 'bool') &&
isset($_ticket['assignedto_name']))
+ {
+ $user_name = $_ticket['assignedto_name'];
+
$GLOBALS['phpgw']->preferences->set_account_id($_ticket['assignedto'], true);
+ $GLOBALS['phpgw_info']['user']['preferences'] =
$GLOBALS['phpgw']->preferences->data;
+ $account_lid =
$GLOBALS['phpgw']->accounts->id2lid($_ticket['assignedto']);
+ }
+ else
+ {
+ $user_name = $GLOBALS['phpgw_info']['user']['fullname'];
+ $account_lid =
$GLOBALS['phpgw_info']['user']['account_lid'];
+ }
+ // $ressursnr =
$GLOBALS['phpgw_info']['user']['preferences']['property']['ressursnr'];
+
+ $buyer = array(
+ 'Name' => $user_name,
+ 'AddressInfo' => array(
+ array(
+ 'Address' => $_ticket['address']
+ )
+ ),
+ 'BuyerReferences' => array(
+ array(
+ 'Responsible' => $account_lid,
+ 'RequestedBy' => $account_lid,
+ 'Accountable' => $account_lid,
+ )
+ )
+ );
+
+ $param = array(
+ 'dim0' => $_ticket['b_account_id'],
// Art
+ 'dim1' => $_ticket['ecodimb'],
// Ansvar
+ 'dim2' => $_ticket['service_id'] ?
$_ticket['service_id'] : 9, // Tjeneste liste 30 stk, default 9
+ 'dim3' => $_ticket['location_data']['loc1'] .
($_ticket['location_data']['loc2'] ? $_ticket['location_data']['loc2'] : '01')
, // Objekt: eiendom + bygg: 6 siffer
+ 'dim4' => $_ticket['contract_id'],
// Kontrakt - frivillig / 9, 7 tegn -
alfanumerisk
+ 'dim5' => $_ticket['external_project_id'],
// Prosjekt
+ 'dim6' => $_ticket['order_dim1'],
// Aktivitet - frivillig: bygningsdel,
3 siffer + bokstavkode
+ 'vendor_id' => $_ticket['vendor_id'],
+ 'vendor_name' => $vendor['name'],
+ 'vendor_address' => $vendor['address'],
+ 'order_id' => $_ticket['order_id'],
+ 'tax_code' => $_ticket['tax_code'],
+ 'buyer' => $buyer,
+ 'lines' => array(
+ array(
+ 'unspsc_code' =>
$_ticket['unspsc_code'],
+ 'descr' =>
strip_tags($_ticket['order_descr'])
+ )
+ )
+ );
+
+ $exporter_ordre = new BkBygg_exporter_ordre_til_Agresso();
+ $exporter_ordre->create_transfer_xml($param);
+ $exporter_ordre->output();
+ die();
+ // $exporter_ordre->transfer();
+ }
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Fmsystem-commits] [14942] property: export order to accouting system,
Sigurd Nes <=