altocompta/include/postgres.php
Dany De Bontridder 8834d14ee5 Merged revisions 2258,2260-2262,2264-2268,2270-2283,2285-2297,2299-2322,2324-2340 via svnmerge from
svn+ssh://danydb@svn/svn/phpcompta/branches/rel430

........
  r2258 | danydb | 2008-12-18 23:21:00 +0100 (Thu, 18 Dec 2008) | 1 line
  
  Add debug
........
  r2262 | danydb | 2008-12-19 22:38:25 +0100 (Fri, 19 Dec 2008) | 1 line
  
  Add a local admin 
........
  r2270 | danydb | 2008-12-21 20:11:26 +0100 (Sun, 21 Dec 2008) | 1 line
  
  Doc. remove the useless todo list
........
  r2271 | danydb | 2008-12-22 21:18:46 +0100 (Mon, 22 Dec 2008) | 3 lines
  
  Remove obsolete functions insertRapt, deleteRapt and getconcerned
  Remove obsolete file user_update
........
  r2272 | danydb | 2008-12-22 21:21:46 +0100 (Mon, 22 Dec 2008) | 2 lines
  
  Remove obsolete function isFicheOfJrn
........
  r2273 | danydb | 2008-12-22 21:44:28 +0100 (Mon, 22 Dec 2008) | 1 line
  
  Improve Invoice : add the username 
........
  r2287 | danydb | 2009-01-13 21:37:07 +0100 (Tue, 13 Jan 2009) | 1 line
  
  Code cleaning replace GetConcerned by Acc_Reconciliation
........
  r2288 | danydb | 2009-01-13 21:38:40 +0100 (Tue, 13 Jan 2009) | 1 line
  
  Add a comment on jrn operation
........
  r2289 | danydb | 2009-01-13 21:40:13 +0100 (Tue, 13 Jan 2009) | 1 line
  
  Code cleaning : add comments to the code
........
  r2290 | danydb | 2009-01-13 21:40:46 +0100 (Tue, 13 Jan 2009) | 1 line
  
  Code cleaning replace GetConcerned by Acc_Reconciliation
........
  r2291 | danydb | 2009-01-13 21:41:35 +0100 (Tue, 13 Jan 2009) | 1 line
  
  Code cleaning replace GetConcerned by Acc_Reconciliation
........
  r2292 | danydb | 2009-01-13 21:42:15 +0100 (Tue, 13 Jan 2009) | 1 line
  
  Add documentation about new tags
........
  r2293 | danydb | 2009-01-13 21:43:22 +0100 (Tue, 13 Jan 2009) | 1 line
  
  Code cleaning start rewriting of the security part 
........
  r2294 | danydb | 2009-01-14 20:09:18 +0100 (Wed, 14 Jan 2009) | 1 line
  
  Improve : template of security + security code
........
  r2295 | danydb | 2009-01-14 20:21:51 +0100 (Wed, 14 Jan 2009) | 1 line
  
  Improve security : add new variable
........
  r2296 | danydb | 2009-01-14 20:39:58 +0100 (Wed, 14 Jan 2009) | 1 line
  
  Improve security : modify layout
........
  r2297 | danydb | 2009-01-15 19:21:47 +0100 (Thu, 15 Jan 2009) | 1 line
  
  Improve security : user setting
........
  r2299 | danydb | 2009-01-15 19:28:56 +0100 (Thu, 15 Jan 2009) | 2 lines
  
  Clean code : can_request doens't have a connx parameter
........
  r2300 | danydb | 2009-01-16 19:12:47 +0100 (Fri, 16 Jan 2009) | 1 line
  
  Improve Ecriture directe libelle : new widget js_search_card_control
........
  r2301 | danydb | 2009-01-16 22:29:39 +0100 (Fri, 16 Jan 2009) | 1 line
  
  Improve Quick Writing : remove span, and update the comment field
........
  r2302 | danydb | 2009-01-16 23:01:24 +0100 (Fri, 16 Jan 2009) | 1 line
  
  Improve cosmetic : GL + remove the select topmenu
........
  r2303 | danydb | 2009-01-19 08:41:09 +0100 (Mon, 19 Jan 2009) | 1 line
  
  Cosmetic : a inline
........
  r2304 | danydb | 2009-01-20 23:03:59 +0100 (Tue, 20 Jan 2009) | 6 lines
  
  Code Cleaning
  Security : remove obsolete files & function
  Class_user : add new functions for the security
  Only the menu in text mode with a break line
........
  r2305 | danydb | 2009-01-20 23:05:01 +0100 (Tue, 20 Jan 2009) | 1 line
  
  code cleaning rewriting of Security 
........
  r2306 | danydb | 2009-01-21 21:13:15 +0100 (Wed, 21 Jan 2009) | 1 line
  
  Security printing unicode and fix some bugs
........
  r2309 | danydb | 2009-01-26 19:56:28 +0100 (Mon, 26 Jan 2009) | 1 line
  
  ADD : company without VAT
........
  r2310 | danydb | 2009-01-26 23:33:14 +0100 (Mon, 26 Jan 2009) | 1 line
  
  Add : no vat for Purchase ledger 
........
  r2312 | danydb | 2009-01-28 19:57:30 +0100 (Wed, 28 Jan 2009) | 1 line
  
  Integration 
........
  r2313 | danydb | 2009-01-28 22:46:27 +0100 (Wed, 28 Jan 2009) | 1 line
  
  Work with or without VAT
........
  r2314 | danydb | 2009-01-29 22:18:57 +0100 (Thu, 29 Jan 2009) | 2 lines
  
  Remove debug info
........
  r2315 | danydb | 2009-01-29 23:23:14 +0100 (Thu, 29 Jan 2009) | 1 line
  
  ADD the possibility to enter a piece number
........
  r2316 | danydb | 2009-01-31 23:20:00 +0100 (Sat, 31 Jan 2009) | 1 line
  
  Documentation
........
  r2317 | danydb | 2009-02-04 20:14:54 +0100 (Wed, 04 Feb 2009) | 1 line
  
  Template for ledger
........
  r2318 | danydb | 2009-02-05 15:52:58 +0100 (Thu, 05 Feb 2009) | 1 line
  
  Add PJ setting + default in Ledger, fix bug in listJrn
........
  r2319 | danydb | 2009-02-05 17:09:56 +0100 (Thu, 05 Feb 2009) | 2 lines
  
  Search by PJ
........
  r2320 | danydb | 2009-02-05 17:23:24 +0100 (Thu, 05 Feb 2009) | 1 line
  
  Finished with pj
........
  r2321 | danydb | 2009-02-05 18:11:35 +0100 (Thu, 05 Feb 2009) | 1 line
  
  PJ is readonly
........
  r2322 | danydb | 2009-02-05 20:10:34 +0100 (Thu, 05 Feb 2009) | 1 line
  
  Saldo : write the first saldo (ajax)
........
  r2324 | danydb | 2009-02-06 00:07:50 +0100 (Fri, 06 Feb 2009) | 5 lines
  
  Add database script
  Cosmetic (menu)
  Fix Bug 
........
  r2325 | danydb | 2009-02-06 14:07:07 +0100 (Fri, 06 Feb 2009) | 1 line
  
  Improve Pj seq 
........
  r2326 | danydb | 2009-02-09 22:47:23 +0100 (Mon, 09 Feb 2009) | 6 lines
  
  Fix Bug privilege (compta_ven)
  Fix if the pj is empty do not increment the sequence (class_acc_operation)
  Fix remove PJ if pj is empty (modify_op.php)
  Cosmetic do not print PJ if pj is null or empty (listing)
........
  r2327 | danydb | 2009-02-10 21:04:48 +0100 (Tue, 10 Feb 2009) | 5 lines
  
  Fix bug in PJ seq
  Fix bug in export jrn csv
  Improve avoid the removal of vat rate
  Add security for card
........
  r2328 | danydb | 2009-02-12 20:26:36 +0100 (Thu, 12 Feb 2009) | 4 lines
  
  Cosmetic menu_tool
  Impression add tag PJ
  Cosmetic menu administration
........
  r2329 | danydb | 2009-02-12 21:43:19 +0100 (Thu, 12 Feb 2009) | 5 lines
  
  filter the folder the local admin can access
  add a function check_dossier which must be implemented everywhere
  Printing with PJ
  NoAccess default = javascript
........
  r2330 | danydb | 2009-02-14 18:11:49 +0100 (Sat, 14 Feb 2009) | 2 lines
  
  improve add libelle for balance csv
........
  r2331 | danydb | 2009-02-14 19:10:56 +0100 (Sat, 14 Feb 2009) | 7 lines
  
  Balance csv add label
  Bug Financial ledger list
  BUG period access 
  BUG stock access
  IMPROVE list ledger with PJ
........
  r2332 | danydb | 2009-02-15 18:21:59 +0100 (Sun, 15 Feb 2009) | 1 line
  
  Bug order was not saved for report
........
  r2333 | danydb | 2009-02-15 18:22:17 +0100 (Sun, 15 Feb 2009) | 1 line
  
  Add security for normal user
........
  r2334 | danydb | 2009-02-15 18:23:24 +0100 (Sun, 15 Feb 2009) | 3 lines
  
  Bug forget to import class
........
  r2335 | danydb | 2009-02-17 21:33:17 +0100 (Tue, 17 Feb 2009) | 5 lines
  
  Fix quick writing let write
  Fix card priv
  Remove button if no priv to add card or cancel an op
........
  r2336 | danydb | 2009-02-19 21:01:36 +0100 (Thu, 19 Feb 2009) | 4 lines
  
  Check for CA
  common Bug with the repository
........
  r2337 | danydb | 2009-02-19 21:50:35 +0100 (Thu, 19 Feb 2009) | 2 lines
  
  Test the parameter security part
........
2009-02-19 21:11:08 +00:00

620 lines
17 KiB
PHP

<?php
/*
* This file is part of PhpCompta.
*
* PhpCompta 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.
*
* PhpCompta 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 PhpCompta; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
//$Revision$
// Copyright Author Dany De Bontridder ddebontridder@yahoo.fr
/*! \file
* \brief Contains the procedure for connecting to postgresql
*/
/*!
* \param p_type string : all for all dossiers lim for only the
* dossier where we've got rights
*
* Show the folder where user have access. Return : nothing
++*/
function ShowDossier($p_type,$p_first=0,$p_max=10,$p_Num=0) {
$l_user=$_SESSION['g_user'];
if ( $p_max == 0 ) {
$l_step="";
} else {
$l_step="LIMIT $p_max OFFSET $p_first";
}
$cn=DbConnect();
if ( $p_type == "all") {
$l_sql="select *, 'W' as priv_priv from ac_dossier ORDER BY dos_name ";
$p_Num=CountSql($cn,$l_sql);
} else {
$l_sql="select * from jnt_use_dos
natural join ac_dossier
natural join ac_users
inner join priv_user on priv_jnt=jnt_id where
use_login='".$l_user."' and priv_priv !='NO'
order by dos_name ";
$p_Num=CountSql($cn,$l_sql);
}
$l_sql=$l_sql.$l_step;
$p_res=ExecSql($cn,$l_sql);
echo_debug('postgres.php',__LINE__,"ShowDossier:".$p_res." Line = $p_Num");
$Max=pg_NumRows($p_res);
if ( $Max == 0 ) return null;
for ( $i=0;$i<$Max; $i++) {
// echo_debug ("i = $i");
$row[]=pg_fetch_array($p_res,$i);
//echo $row[dossier];
}
return $row;
}
/*!
* \brief connect to the database
* <br>example
* <ul>
* <li> DbConnect () connects to account_repository</li>
* <li> DbConnect(1) connects to dossier1</li>
* <li> DbConnect(1,'mod') connects to mod1</li>
* <li> DbConnect(-2,'postgres') connects to the db postgres</li>
*</ul>
*
* \param p_db : db_name id (or -1 for account_repository or -2 if p_type contains the full name of a database
*
* \param p_type = dossier, mod or the name of a datase
* \return the connection
*/
function DbConnect($p_db=-1,$p_type='dossier') {
if ( IsNumber($p_db) == false || strlen($p_db) > 5 )
die ("Dossier invalide");
if ( $p_db==-1)
$l_dossier=sprintf("%saccount_repository",domaine);
else {
if ( $p_db == -2 )
$l_dossier=$p_type;
else
switch ($p_type) {
case 'dossier':
$l_dossier=sprintf("%sdossier%d",domaine,$p_db);
break;
case 'mod':
$l_dossier=sprintf("%smod%d",domaine,$p_db);
break;
}
}
$phpcompta_user=phpcompta_user;
$password=phpcompta_password;
$port=phpcompta_psql_port;
ob_start();
$a=pg_connect("dbname=$l_dossier host=127.0.0.1 user='$phpcompta_user'
password='$password' port=$port");
if ( $a == false )
{
ob_end_clean();
echo '<h2 class="error">Impossible de se connecter &agrave; postgreSql !</h2>';
echo "Vos param&egrave;tres sont incorrectes : <br>";
echo "<br>";
echo "base de donn&eacute;e : $l_dossier<br>";
echo "Domaine : ".domaine."<br>";
echo "Port $port <br>";
echo "Utilisateur : $phpcompta_user <br>";
exit ("Connection impossible : v&eacute;rifiez vos param&egrave;tres de base
de donn&eacute;es");
}
ob_end_clean();
echo_debug ('postgres.php',__LINE__,"connect to $p_db dbname $l_dossier");
return $a;
}
/*!
* \brief send a sql string to the database
* \param $p_connection db connection
* \param $p_string sql string
* \return false if error otherwise true
*/
function ExecSql($p_connection, $p_string,$p_encoding='utf8') {
echo_debug('postgres.php',__LINE__,"SQL = $p_string");
// probl. with Ubuntu & UTF8
//----
try {
pg_set_client_encoding($p_connection,$p_encoding);
$ret=pg_query($p_connection,$p_string);
if ( ! $ret ) throw new Exception (" SQL ERROR $p_string ",1);
} catch (Exception $a) {
echo $p_string;
$a->getMessage();
$a->getTrace();
return $ret;
}
return $ret;
}
/*!
* \brief send a sql string to the database using the function
* pg_query_params. Useful for avoiding the problem with the quotes
* and to execute several times the same command
* \param $p_connection db connection
* \param $p_string sql string
* \param $p_array array containing the value
* \return false if error otherwise true
*/
function ExecSqlParam($p_connection, $p_string,$p_array) {
echo_debug('postgres.php',__LINE__,"SQL = $p_string");
echo_debug('postgres.php',__LINE__,$p_array);
// probl. with Ubuntu & UTF8
//----
pg_set_client_encoding($p_connection,'utf8');
//ob_start();
$ret=pg_query_params($p_connection,$p_string,$p_array);
if ( ! $ret ) {
//ob_end_clean();
$r=$p_string."array ".var_export($p_array,TRUE);
throw new Exception (" SQL ERROR $r",1);
}
//ob_end_clean();
return $ret;
}
/*!
* \brief Return all the users
* as an array
*/
function GetAllUser() {
echo_debug('postgres.php',__LINE__,"GetUser");
$cn=DbConnect();
$sql="select * from ac_users where use_login!='phpcompta'";
echo_debug('postgres.php',__LINE__,"ExecSql");
$Res=ExecSql($cn,$sql);
$Num=pg_NumRows($Res);
if ( $Num == 0 ) return null;
for ($i=0;$i < $Num; $i++) {
$User[]=pg_fetch_array($Res,$i);
}
return $User;
}
/*!
* \brief Get the number of rows
* from table jnt_use_dos where $p_dossier = dos_id and
* use_id=$p_user.
*/
function ExisteJnt($p_dossier,$p_user)
{
$cn=DbConnect();
$Res=ExecSql($cn,"select * from jnt_use_dos where dos_id=".$p_dossier." and use_id=".$p_user);
return pg_NumRows($Res);
}
/* ExistePriv
*
*
*
*/
function ExistePriv($p_jntid)
{
$cn=DbConnect();
$Res=ExecSql($cn,"select * from priv_user where priv_jnt=".$p_jntid);
return pg_NumRows($Res);
}
/* GetJnt
* Get the jnt
*
*
*/
function GetJnt($p_dossier,$p_user)
{
$cn=DbConnect();
$Res=ExecSql($cn,"select jnt_id from jnt_use_dos where dos_id=".$p_dossier." and use_id=".$p_user);
$R=pg_fetch_array($Res,0);
return $R['jnt_id'];
}
/*! \brief Count the number of row
*
* \param $p_conn connection handler
* \param $p_sql sql string
* \param $array if not null we use the safer ExecSqlParam
*/
function CountSql($p_conn,$p_sql,$array=null)
{
if ( $array == null )
$r_sql=ExecSql($p_conn,$p_sql);
else
$r_sql=ExecSqlParam($p_conn,$p_sql,$array);
return pg_NumRows($r_sql);
}
/*!
* \param id of a dossier
* \return Name of the dossier
*/
function GetDossierName($p_dossier)
{
$cn=DbConnect();
$Ret=ExecSql($cn,"select dos_name from ac_dossier where dos_id=".$p_dossier);
$r= pg_fetch_array($Ret,0);
return $r['dos_name'];
}
/*!\brief get the current sequence
*/
function GetSequence($p_cn,$p_seq)
{
$Res=ExecSql($p_cn,"select currval('$p_seq') as seq");
$seq=pg_fetch_array($Res,0);
return $seq['seq'];
}
/*!\brief get the current sequence
*/
function NextSequence($p_cn,$p_seq)
{
$Res=ExecSql($p_cn,"select nextval('$p_seq') as seq");
$seq=pg_fetch_array($Res,0);
return $seq['seq'];
}
function StartSql($p_cn) {
$Res=ExecSql($p_cn,"start transaction");
}
function EndSql($p_cn) {
$Res=ExecSql($p_cn,"end transaction");
}
function Commit($p_cn) {
$Res=ExecSql($p_cn,"commit");
}
function Rollback($p_cn) {
$Res=ExecSql($p_cn,"rollback");
}
function AlterSequence($p_cn,$p_name,$p_value) {
$Res=ExecSql($p_cn,"alter sequence $p_name restart $p_value");
}
/*!
* \brief Get the properties of an user
* it means theme, profile, admin...
*
* \param $p_user user login
* \param $p_cn connection
*
* \return an array containing
* - use_admin
* - use_usertype
* - g_theme
* - use_name
* - use_login
*/
function GetUserProperty($p_cn,$p_user)
{
$sql="select use_login,use_first_name,use_name,use_admin,use_usertype,g_theme
from ac_users where use_login='$p_user'";
$Ret=ExecSql($p_cn,$sql);
if ( pg_NumRows($Ret) == 0)
return array('use_first_name'=>'?',
'use_name'=>'Unknown',
'use_admin'=>0,
'use_usertype'=>'user',
'g_theme'=>'classic',
'use_login'=>$p_user);
$a=pg_fetch_array($Ret,0);
return $a;
}
/*!
* \brief Give the mod_id from modeledef
*
*
* \param $p_cn database connection (repository)
* \param $p_modname template name
* \return template id or 0 if not found
*
*/
function GetModeleId($p_cn,$p_modname) {
$Res=ExecSql($p_cn,"select mod_id from modeledef where mod_name='$p_modname'");
if (pg_NumRows($Res) == 0) return 0;
$name=pg_fetch_array($Res,0);
return $name['mod_id'];
}
/*!\brief purpose return the result of a sql statment
* in a array
* \param $p_cn database connection
* \param $p_sql sql query
* \param $p_array if not null we use ExecSqlParam
*/
function get_array($p_cn,$p_sql,$p_array=null) {
echo_debug('postgres.php',__LINE__,"get_array");
if ( $p_array == null ) {
$r=ExecSql($p_cn,$p_sql);
} else {
$r=ExecSqlParam($p_cn,$p_sql,$p_array);
}
if ( ($Max= pg_NumRows($r)) == 0 ) return null;
$array=pg_fetch_all($r);
echo_debug('postgres.php',__LINE__,var_export($array,true));
return $array;
}
/*! save_upload_document
* \brief Save a "piece justificative"
*
* \param $cn database connection
* \param $seq jr_grpt_id
* \return $oid of the lob file if success
* null if a error occurs
*
*/
function save_upload_document ($cn,$seq) {
$new_name=tempnam($_ENV['TMP'],'pj');
echo_debug('postgres.php',__LINE__,"new name=".$new_name);
echo_debug(__FILE__.":".__LINE__.$_FILES);
if ($_FILES["pj"]["error"] > 0)
{
echo_error(__FILE__.":".__LINE__."Error: " . $_FILES["pj"]["error"] );
}
if ( strlen ($_FILES['pj']['tmp_name']) != 0 ) {
echo_debug(__FILE__.":".__LINE__.'_FILE is'.$_FILES['pj']['tmp_name']);
if (move_uploaded_file($_FILES['pj']['tmp_name'],
$new_name)) {
// echo "Image saved";
echo_debug(__FILE__.":".__LINE__."Doc saved");
$oid= pg_lo_import($cn,$new_name);
if ( $oid == false ) {
echo_error('postgres.php',__LINE__,"cannot upload document");
Rollback($cn);
return;
}
echo_debug('postgres.php',__LINE__,"Loading document");
// Remove old document
$ret=ExecSql($cn,"select jr_pj from jrn where jr_grpt_id=$seq");
if (pg_num_rows($ret) != 0) {
$r=pg_fetch_array($ret,0);
$old_oid=$r['jr_pj'];
if (strlen($old_oid) != 0)
pg_lo_unlink($cn,$old_oid);
}
// Load new document
ExecSql($cn,"update jrn set jr_pj=".$oid.", jr_pj_name='".$_FILES['pj']['name']."', ".
"jr_pj_type='".$_FILES['pj']['type']."' where jr_grpt_id=$seq");
return $oid;
} else {
echo "<H1>Error</H1>";
Rollback($cn);
}
}
return 0;
}
/*!\brief return the value of the sql, the sql will return only one value
* with the value
* \param $p_cn database connection
* \param $p_sql the sql stmt example :select s_value from
document_state where s_id=2
*\param $p_array if array is not null we use the ExecSqlParm (safer)
*\see ExecSqlParm
* \return only the first value or an empty string if nothing is found
*/
function getDbValue($p_cn,$p_sql,$p_array=null)
{
if ( $p_array == null) {
$ret=ExecSql($p_cn,$p_sql);
} else {
$ret=ExecSqlParam($p_cn,$p_sql,$p_array);
}
if ( pg_NumRows($ret) == 0 ) return "";
$r=pg_fetch_row($ret,0);
return $r[0];
}
/*!\brief test if a sequence exist */
/* \return true if the seq. exist otherwise false
*/
function exist_sequence($p_cn,$p_name) {
$r=CountSql($p_cn,"select relname from pg_class where relname=lower('".$p_name."')");
if ( $r==0)
return false;
return true;
}
function create_sequence($p_cn,$p_name) {
$sql="create sequence ".$p_name;
ExecSql($p_cn,$sql);
}
/*!
**************************************************
* \brief Get version of a database, the content of the
* table version
*
* \param $p_cn database connection
*
* \return version number
*
*/
function get_version($p_cn) {
$Res=ExecSql($p_cn,"select val from version");
$a=pg_fetch_array($Res,0);
return $a['val'];
}
/*! execute_script
**************************************************
* \brief Execute a sql script
*
* \param $p_cn database
* \param $script script name
*/
function execute_script($p_cn,$script) {
if ( DEBUG=='false' ) ob_start();
$hf=fopen($script,'r');
if ( $hf == false ) {
echo 'Ne peut ouvrir '.$script;
exit();
}
$sql="";
$flag_function=false;
while (!feof($hf)) {
$buffer=fgets($hf);
$buffer=str_replace ("$","\$",$buffer);
print $buffer."<br>";
// comment are not execute
if ( substr($buffer,0,2) == "--" ) {
//echo "comment $buffer";
continue;
}
// Blank Lines Are Skipped
If ( Strlen($buffer)==0) {
//echo "Blank $buffer";
Continue;
}
if ( strpos(strtolower($buffer),"create function")===0 ) {
echo "found a function";
$flag_function=true;
$sql=$buffer;
continue;
}
if ( strpos(strtolower($buffer),"create or replace function")===0 ) {
echo "found a function";
$flag_function=true;
$sql=$buffer;
continue;
}
// No semi colon -> multiline command
if ( $flag_function== false && strpos($buffer,';') == false ) {
$sql.=$buffer;
continue;
}
if ( $flag_function ) {
if ( strpos(strtolower($buffer), "language plpgsql") === false &&
strpos(strtolower($buffer), "language 'plpgsql'") === false ) {
$sql.=$buffer;
continue;
}
} else {
// cut the semi colon
$buffer=str_replace (';','',$buffer);
}
$sql.=$buffer;
echo_debug('setup.php',__LINE__,"Execute sql $sql");
if ( ExecSql($p_cn,$sql) == false ) {
Rollback($p_cn);
if ( DEBUG=='false' ) ob_end_clean();
print "ERROR : $sql";
exit();
}
$sql="";
$flag_function=false;
print "<hr>";
} // while (feof)
fclose($hf);
if ( DEBUG=='false' ) ob_end_clean();
}
/*!\brief loop to apply all the path to a folder or
* a template
* \param $p_cn database connexion
* \param $p_name database name
*
*/
function apply_patch($p_cn,$p_name,$from_setup=1)
{
$MaxVersion=DBVERSION-1;
echo '<ul>';
$add=($from_setup==0)?'admin/':'';
for ( $i = 4;$i <= $MaxVersion;$i++)
{
$to=$i+1;
if ( get_version($p_cn) <= $i ) {
echo "<li>Patching ".$p_name.
" from the version ".get_version($p_cn)." to $to</h3> </li>";
execute_script($p_cn,$add.'sql/patch/upgrade'.$i.'.sql');
if ( DEBUG=='false' ) ob_start();
// specific for version 4
if ( $i == 4 )
{
$sql="select jrn_def_id from jrn_def ";
$Res=ExecSql($p_cn,$sql);
$Max=pg_NumRows($Res);
for ($seq=0;$seq<$Max;$seq++) {
$row=pg_fetch_array($Res,$seq);
$sql=sprintf ("create sequence s_jrn_%d",$row['jrn_def_id']);
ExecSql($p_cn,$sql);
}
}
// specific to version 7
if ( $i == 7 )
{
// now we use sequence instead of computing a max
//
$Res2=ExecSql($p_cn,'select coalesce(max(jr_grpt_id),1) as l from jrn');
$Max2= pg_NumRows($Res2) ;
if ( $Max2 == 1) {
$Row=pg_fetch_array($Res2,0);
var_dump($Row);
$M=$Row['l'];
ExecSql($p_cn,"select setval('s_grpt',$M,true)");
}
}
// specific to version 17
if ( $i == 17 )
{
execute_script($p_cn,$add.'sql/patch/upgrade17.sql');
$max=getDbValue($p_cn,'select last_value from s_jnt_fic_att_value');
AlterSequence($p_cn,'s_jnt_fic_att_value',$max+1);
} // version
// reset sequence in the modele
//--
if ( $i == 30 && $p_name=="mod" )
{
$a_seq=array('s_jrn','s_jrn_op','s_centralized',
's_stock_goods','c_order','s_central');
foreach ($a_seq as $seq ) {
$sql=sprintf("select setval('%s',1,false)",$seq);
$Res=ExecSql($p_cn,$sql);
}
$sql="select jrn_def_id from jrn_def ";
$Res=ExecSql($p_cn,$sql);
$Max=pg_NumRows($Res);
for ($seq=0;$seq<$Max;$seq++) {
$row=pg_fetch_array($Res,$seq);
$sql=sprintf ("select setval('s_jrn_%d',1,false)",$row['jrn_def_id']);
ExecSql($p_cn,$sql);
}
}
if ( $i == 36 ) {
/* check the country and apply the path */
$res=ExecSql($p_cn,"select pr_value from parameter where pr_id='MY_COUNTRY'");
$country=pg_fetch_result($res,0,0);
execute_script($p_cn,$add."sql/patch/upgrade36.".$country.".sql");
ExecSql($p_cn,'update tmp_pcmn set pcm_type=find_pcm_type(pcm_val)');
}
if ( DEBUG == 'false') ob_end_clean();
}
}
echo '</ul>';
}
?>