noalyss Version-9
NOALYSS : serveur de comptabilité et ERP (2002)
Loading...
Searching...
No Matches
database.class.php
Go to the documentation of this file.
1<?php
2
3/*
4 * This file is part of NOALYSS.
5 *
6 * NOALYSS is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * NOALYSS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with NOALYSS; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21// Copyright Author Dany De Bontridder danydb@aevalys.eu
22
23/**
24 * \file
25 * \brief contains the class for connecting to Noalyss
26 */
27
28/**
29 * \class Database
30 * \brief contains the class for connecting to Noalyss
31 */
33{
34 /**\brief constructor
35 * \param $p_database_id is the id of the dossier, or the modele following the
36 * p_type if = 0 then connect to the repository
37 * \param $p_type is 'DOS' (defaut) for dossier or 'MOD'
38 */
39
40 function __construct($p_database_id = 0, $p_type = 'dos')
41 {
42 if (IsNumber($p_database_id) == false || strlen($p_database_id??"") > 10)
43 die("-->Dossier invalide [$p_database_id]");
44 $noalyss_user = (defined("noalyss_user")) ? noalyss_user : phpcompta_user;
45 $password = (defined("noalyss_password")) ? noalyss_password : phpcompta_password;
46 $port = (defined("noalyss_psql_port")) ? noalyss_psql_port : phpcompta_psql_port;
47 $host = (!defined("noalyss_psql_host")) ? '127.0.0.1' : noalyss_psql_host;
48 if (defined("MULTI") && MULTI == "0") {
49 $l_dossier = dbname;
50 } else {
51
52 if ($p_database_id == 0) { /* connect to the repository */
53 $l_dossier = sprintf("%saccount_repository", strtolower(domaine));
54 } else if ($p_type == 'dos') { /* connect to a folder (dossier) */
55 $l_dossier = sprintf("%sdossier%d", strtolower(domaine), $p_database_id);
56 } else if ($p_type == 'mod') { /* connect to a template (modele) */
57 $l_dossier = sprintf("%smod%d", strtolower(domaine), $p_database_id);
58 } else if ($p_type == 'template') {
59 $l_dossier = 'template1';
60 } else {
61 throw new Exception('Connection invalide');
62 }
63 }
64
65 parent::__construct($noalyss_user, $password, $l_dossier, $host, $port);
66
67 $this->exec_sql('set search_path to public,pg_catalog;');
68 if ($this->exist_schema('comptaproc')) {
69 $this->exec_sql('set search_path to public,comptaproc,pg_catalog;');
70 }
71 $this->exec_sql('set DateStyle to ISO, MDY;');
72
73 }
74
75 /***
76 * \brief Save a "piece justificative" , the name must be pj
77 *
78 * \param $seq jr_grpt_id
79 * \return $oid of the lob file if success
80 * null if a error occurs
81 *
82 */
84 {
85 $oid = $this->upload('pj');
86 if ($oid == false) {
87 return false;
88 }
89 // Remove old document
90 $ret = $this->exec_sql("select jr_pj from jrn where jr_grpt_id=$seq");
91 if (pg_num_rows($ret) != 0) {
92 $r = pg_fetch_array($ret, 0);
93 $old_oid = $r['jr_pj'];
94 if (strlen($old_oid??"") != 0)
95 $this->lo_unlink( $old_oid);
96 }
97 // Load new document
98 $this->exec_sql("update jrn set jr_pj=$1 , jr_pj_name=$2,
99 jr_pj_type=$3 where jr_grpt_id=$4",
100 array($oid, $_FILES['pj']['name'], $_FILES['pj']['type'], $seq));
101 return $oid;
102 }
103
104 /**
105 * \brief Get version of a database, the content of the
106 * table version
107 *
108 * \return version number
109 *
110 */
111
112 function get_version()
113 {
114 $Res = $this->get_value("select max(val) from version");
115 return $Res;
116 }
117
118 /**
119 * \brief loop to apply all the path to a folder or a template
120 * Upgrade check if the folder $p_name needs to be upgrade thanks the variable DBVERSION
121 * and run all the SQL script named upgradeX.sql from the folder noalyss/include/sql/patch
122 * until X equal DBVERSION-1
123 *
124 * \param $p_name database name
125 *
126 */
127
128 function apply_patch($p_name)
129 {
130 if (!$this->exist_table('version')) {
131 echo _('Base de donnée vide');
132 return;
133 }
134 $MaxVersion = DBVERSION - 1;
135 $succeed = "<span style=\"font-size:18px;color:green\">&#x2713;</span>";
136 echo '<ul style="list-type-style:square">';
137 for ($i = 4; $i <= $MaxVersion; $i++) {
138 $to = $i + 1;
139 if ($this->get_version() <= $i) {
140 if ($this->get_version() == 97) {
141 if ($this->exist_schema("amortissement")) {
142 $this->exec_sql('ALTER TABLE amortissement.amortissement_histo
143 ADD CONSTRAINT internal_fk FOREIGN KEY (jr_internal) REFERENCES jrn (jr_internal)
144 ON UPDATE CASCADE ON DELETE SET NULL');
145 }
146 }
147 echo "<li>Patching " . $p_name .
148 " from the version " . $this->get_version() . " to $to ";
149
150 $this->execute_script(NOALYSS_INCLUDE . '/sql/patch/upgrade' . $i . '.sql');
151 echo $succeed;
152
153 if ( DEBUGNOALYSS == 0)
154 ob_start();
155 // specific for version 4
156 if ($i == 4) {
157 $sql = "select jrn_def_id from jrn_def ";
158 $Res = $this->exec_sql($sql);
159 $Max = $this->size();
160 for ($seq = 0; $seq < $Max; $seq++) {
161 $row = pg_fetch_array($Res, $seq);
162 $sql = sprintf("create sequence s_jrn_%d", $row['jrn_def_id']);
163 $this->exec_sql($sql);
164 }
165 }
166 // specific to version 7
167 if ($i == 7) {
168 // now we use sequence instead of computing a max
169 //
170 $Res2 = $this->exec_sql('select coalesce(max(jr_grpt_id),1) as l from jrn');
171 $Max2 = pg_num_rows($Res2);
172 if ($Max2 == 1) {
173 $Row = pg_fetch_array($Res2, 0);
174 var_dump($Row);
175 $M = $Row['l'];
176 $this->exec_sql("select setval('s_grpt',$M,true)");
177 }
178 }
179 // specific to version 17
180 if ($i == 17) {
181 $this->execute_script(NOALYSS_INCLUDE . '/sql/patch/upgrade17.sql');
182 $max = $this->get_value('select last_value from s_jnt_fic_att_value');
183 $this->alter_seq('s_jnt_fic_att_value', $max + 1);
184 } // version
185 // reset sequence in the modele
186 //--
187 if ($i == 30 && $p_name == "mod") {
188 $a_seq = array('s_jrn', 's_jrn_op', 's_centralized',
189 's_stock_goods', 'c_order', 's_central');
190 foreach ($a_seq as $seq) {
191 $sql = sprintf("select setval('%s',1,false)", $seq);
192 $Res = $this->exec_sql($sql);
193 }
194 $sql = "select jrn_def_id from jrn_def ";
195 $Res = $this->exec_sql($sql);
196 $Max = pg_num_rows($Res);
197 for ($seq = 0; $seq < $Max; $seq++) {
198 $row = pg_fetch_array($Res, $seq);
199 $sql = sprintf("select setval('s_jrn_%d',1,false)", $row['jrn_def_id']);
200 $this->exec_sql($sql);
201 }
202 }
203 if ($i == 36) {
204 /* check the country and apply the path */
205 $res = $this->exec_sql("select pr_value from parameter where pr_id='MY_COUNTRY'");
206 $country = pg_fetch_result($res, 0, 0);
207 $this->execute_script(NOALYSS_INCLUDE . "/sql/patch/upgrade36." . $country . ".sql");
208 $this->exec_sql('update tmp_pcmn set pcm_type=find_pcm_type(pcm_val)');
209 }
210 if ($i == 59) {
211 $res = $this->exec_sql("select pr_value from parameter where pr_id='MY_COUNTRY'");
212 $country = pg_fetch_result($res, 0, 0);
213 if ($country == 'BE')
214 $this->exec_sql("insert into parm_code values ('SUPPLIER',440,'Poste par défaut pour les fournisseurs')");
215 if ($country == 'FR')
216 $this->exec_sql("insert into parm_code values ('SUPPLIER',400,'Poste par défaut pour les fournisseurs')");
217 }
218 if ($i == 61) {
219 $country = $this->get_value("select pr_value from parameter where pr_id='MY_COUNTRY'");
220 $this->execute_script(NOALYSS_INCLUDE . "/sql/patch/upgrade61." . $country . ".sql");
221 }
222 /**
223 * Bug in parm_code for FRANCE
224 */
225 if ($i == 141 ) {
226 $country = $this->get_value("select pr_value from parameter where pr_id='MY_COUNTRY'");
227 $this->execute_script(NOALYSS_INCLUDE . "/sql/patch/upgrade141." . $country . ".sql");
228 }
229
230 if (DEBUGNOALYSS == 0 )
231 ob_end_clean();
232
233 }
234 }
235 echo '</ul>';
236 }
237
238 /**
239 * return the name of the database with the domain name
240 * @param $p_id of the folder WITHOUT the domain name
241 * @param $p_type dos for folder mod for template
242 * @return formatted name
243 */
245 {
246 switch ($p_type) {
247 case 'dos':
248 $sys_name = sprintf("%sdossier%d", strtolower(domaine), $p_id);
249 break;
250 case 'mod':
251 $sys_name = sprintf("%smod%d", strtolower(domaine), $p_id);
252 break;
253 default:
254 echo_error(__FILE__ . " format_name invalid type " . $p_type, __LINE__);
255 throw new Exception(__FILE__ . " format_name invalid type " . $p_type . __LINE__);
256 }
257 return $sys_name;
258 }
259}
echo_error($p_log, $p_line="", $p_message="")
log error into the /tmp/noalyss_error.log it doesn't work on windows
catch(Exception $exc) if(! $g_user->can_write_action($ag_id)) $r
$name size
$to
_("actif, passif,charge,...")
This class allow you to connect to the postgresql database, execute sql, retrieve data.
alter_seq($p_name, $min)
alter the sequence value
$sql
last SQL stmt executed
get_value($p_sql, $p_array=null)
return the value of the sql, the sql will return only one value with the value
exec_sql($p_string, $p_array=null)
send a sql string to the database
lo_unlink($p_oid)
wrapper for the function pg_lo_unlink
execute_script($script)
Execute a sql script.
exist_table($p_name, $p_schema='public')
test if a table exist
contains the class for connecting to Noalyss
__construct($p_database_id=0, $p_type='dos')
constructor
format_name($p_id, $p_type)
return the name of the database with the domain name
save_receipt($seq)
get_version()
Get version of a database, the content of the table version.
apply_patch($p_name)
loop to apply all the path to a folder or a template Upgrade check if the folder $p_name needs to be ...
const DBVERSION
Definition constant.php:31
$succeed
Definition install.php:183
domaine
Definition install.php:370