Plugins  LAST
 All Data Structures Files Functions Variables Pages
class_impacc_file.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 /* $Revision$ */
21 
22 // Copyright (c) 2002 Author Dany De Bontridder dany@alchimerys.be
23 
24 /**
25  * @file
26  * @brief
27  *
28  */
29 require_once DIR_IMPORT_ACCOUNT."/include/class_impacc_csv.php";
30 require_once DIR_IMPORT_ACCOUNT."/include/class_impacc_operation.php";
31 require_once DIR_IMPORT_ACCOUNT."/database/class_impacc_import_file_sql.php";
32 require_once DIR_IMPORT_ACCOUNT."/database/class_impacc_import_csv_sql.php";
33 
35 {
36 
37 //--------------------------------------------------
38 // Will be replaced by Impacc_CSV as soon as the XML
39 //
40 // Import is proposed
41 //-----------------------------------------------------
42 
43  static $aformat=array(1=>"CSV");
44  var $format;
45  var $filename;
46  var $import_file; //!< Impacc_Import_file_SQL $impid
47 
48  /// Save the uploaded file and CSV setting if it is CSV import
49 
50  function save_file()
51  {
52  if (trim($_FILES['file_operation']['name'])=='')
53  {
54  alert(_('Pas de fichier donnĂ©'));
55  return -1;
56  }
57  $format=HtmlInput::default_value_post("format", -1);
58 
59  if (!isset(self::$aformat[$format]))
60  {
61  alert(_("Format inconnu"));
62  return -1;
63  }
64  $this->format=self::$aformat[$format];
65  $this->filename=tempnam($_ENV['TMP'], 'upload_');
66  if (!move_uploaded_file($_FILES["file_operation"]["tmp_name"],
67  $this->filename))
68  {
69  throw new Exception(_("Fichier non sauvĂ©"), 1);
70  }
71  $cn=Dossier::connect();
72  $imp=new Impacc_Import_file_SQL($cn);
73  $imp->setp('i_tmpname', $this->filename);
74  $imp->setp('i_filename', $_FILES['file_operation']['name']);
75  $imp->setp("i_type", self::$aformat[$format]);
76  $imp->insert();
77  $cn->exec_sql("update impacc.import_file set i_date_transfer=now() where id=$1",
78  array($imp->id));
79  $this->import_file=$imp;
80  $this->impid=$imp->getp("id");
81 
82  // For CSV only
83  if ($imp->i_type=="CSV")
84  {
85  try
86  {
87  $csv=new Impacc_CSV();
88  $csv->set_import($this->impid);
89  $csv->set_setting();
90  $csv->check_setting();
91  $csv->save_setting();
92  }
93  catch (Exception $ex)
94  {
95  error_log($ex->getTraceAsString());
96  echo _("Format invalide")," : ",$ex->getMessage();
97  }
98  }
99  }
100  function load($p_import_id)
101  {
102  $cn=Dossier::connect();
103  $this->import_file=new Impacc_Import_file_SQL($cn,$p_import_id);
104 
105  }
106  /// Load the file in a temporary table
107  function record()
108  {
109  $operation=new Impacc_Operation();
110  $operation->record_file($this);
111  $cn=Dossier::connect();
112  $cn->exec_sql("update impacc.import_file set i_date_transfer=now() where id=$1",
113  array($this->import_file->id));
114  }
115 
116  /// Display the parameters and the file
117  /// to upload
118  function input_file()
119  {
120 
121  $file=new IFile("file_operation");
122  $format=new ISelect("format");
123  $format->id="format_sel";
124  $format->value=array(
125  array("value"=>0, "label"=>"-"),
126  array("value"=>1, "label"=>"CSV")
127  );
128  $format->javascript="onchange=\"ctl_display()\"";
129  require_once DIR_IMPORT_ACCOUNT."/template/input_file.php";
130  }
131 
132  /// Check the rows of the imported file
133  function check()
134  {
135  if ($this->import_file->i_type=='CSV')
136  {
137  $operation=new Impacc_Operation();
138  $operation->check($this);
139  }
140  }
141 
142  /// Display the rows of the imported file and a status for each row
143  function result()
144  {
145  if ($this->import_file->i_type=='CSV')
146  {
147  $operation=new Impacc_CSV();
148  $operation->result($this);
149  }
150  }
151  /// Transfer to accountancy
152  function transfer()
153  {
154  if ($this->import_file->i_type=='CSV')
155  {
156  $operation=new Impacc_CSV();
157  $operation->load_import($this->impid);
158  $operation->transfer();
159  }
160  }
161 
162  /// Show the result of the file import
163  function result_transfer()
164  {
165  if ($this->import_file->i_type == 'CSV')
166  {
167  $cn=Dossier::connect();
168  // Show first the successfully transfered record
169  // Show the failed
170  $operation=new Impacc_CSV();
171  $operation->load_import($this->import_file->id);
172  // Show the target ledger
173  $ledger=new Acc_Ledger($cn,$operation->detail->jrn_def_id);
174  echo "<h1>";
175  printf(_("Importation dans le journal %s"),$ledger->get_name());
176  echo "</h1>";
177  $operation->result($this);
178  }
179  }
180  function display_list()
181  {
182  $cn=Dossier::connect();
183  $array=$cn->get_array(
184  "
185  select
186  id,
187  i_filename,
188  i_type,
189  to_char(i_date_transfer,'DD.MM.YY HH:MI') as stransfer,
190  to_char(i_date_import,'DD.MM.YY HH:MI') as simport,
191  to_char(i_date_transfer,'YYYYMMDDHHMI') as sorder_transfer
192  from
193  impacc.import_file
194  order by i_date_transfer desc
195  "
196  );
197  require_once DIR_IMPORT_ACCOUNT."/template/history_file.php";
198  }
199  /// Delete a row in impacc.import_file
200  function delete($id)
201  {
202  $cn=Dossier::connect();
203  $cn->exec_sql("delete from impacc.import_file where id=$1",array($id));
204 
205  }
206 }
207 
208 ?>
result()
Display the rows of the imported file and a status for each row.
check()
Check the rows of the imported file.
$import_file
Impacc_Import_file_SQL $impid.
record()
Load the file in a temporary table.
Used by all Import CSV Operation , contains the setting (delimiter,thousand ...)
if(isset($_POST['remove'])) $array
transfer()
Transfer to accountancy.
input_file()
Display the parameters and the file to upload.
save_file()
Save the uploaded file and CSV setting if it is CSV import.
load($p_import_id)
$file
Definition: import_bank.php:60
result_transfer()
Show the result of the file import.
global $cn
Redirect to CSV or other format.