Plugins  LAST
 All Data Structures Files Functions Variables Pages
import_bank.php
Go to the documentation of this file.
1 <?php
2 /*
3  * This file is part of NOALYSS.
4  *
5  * NOALYSS is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * NOALYSS is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with NOALYSS; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 
20 /*
21  * Step 1/4
22  */
23 if ( ! isset ($_REQUEST ['sb']))
24  {
25  echo '<div class="content" style="width:80%;margin-left:10%">';
26  echo '<form method="get">';
27  $iselect=new ISelect('format');
28  $iselect->value=$cn->make_array('select id,format_name from importbank.format_bank order by format_name');
29  $new=array('value'=>0,'label'=>_('--nouveau--'));
30  $iselect->value[]=$new;
31  require_once('template/import_new.php');
32  echo HtmlInput::submit('select_submit','Valider');
33  echo HtmlInput::get_to_hidden(array('gDossier','plugin_code','sa','ac'));
34  echo HtmlInput::hidden('sb','select_form');
35  echo '</form>';
36  echo '</div>';
37  exit();
38  }
39 
40 /*
41  * Initialize all the fields
42  */
43 $format=new IText('format_name');
44 $jrn_def=new ISelect('jrn_def');
45 $jrn_def->value=$cn->make_array('select jrn_def_id,jrn_def_name from jrn_def where '.$g_user->get_ledger_sql('FIN',3).' order by jrn_def_name');
46 $sep_decimal=new ISelect('sep_dec');
47 $sep_decimal->value=$adecimal;
48 
49 $sep_thousand=new ISelect('sep_thous');
51 
52 $sep_field=new ISelect('sep_field');
53 $sep_field->value=$aseparator;
54 
55 $col_valid=new INum('col_valid');
56 
57 $format_date=new ISelect('format_date');
59 
60 $file=new IFile('import_file');
61 $skip=new INum('skip');
62 $skip->value=0;
63 $skip->size=5;
64 
65 $nb_col=new INum('nb_col');
66 
67 /*
68  * Step 2 : show the selected format and upload the file
69  */
70 if ( $_REQUEST ['sb'] == 'select_form')
71  {
72 
73  if ( $_GET['format'] != '0')
74  {
75  $format_bank=new Format_Bank_Sql($cn,$_GET['format']);
76  if ($cn->size() == 1)
77  {
78  $format->value=$format_bank->format_name;
79  $jrn_def->selected=$format_bank->jrn_def_id;
80  $sep_field->selected=$format_bank->sep_field;
81  $sep_thousand->selected=$format_bank->sep_thousand;
82  $sep_decimal->selected=$format_bank->sep_decimal;
83  $format_date->selected=$format_bank->format_date;
84  $nb_col->value=$format_bank->nb_col;
85  $skip->value=$format_bank->skip;
86  }
87  else
88  {
89  throw new Exception(_('Nombre de ligne trouvé incorrect'));
90  }
91  }
92  echo '<div class="content" style="width:80%;margin-left:10%">';
93  $sb='upload_file';
94  require_once ('template/show_field.php');
95  echo '</div>';
96  exit();
97  }
98 
99 /*
100  * Step 3: upload the file, show it and let change the values of the format
101  */
102 if ( $_POST['sb']=='upload_file')
103  {
104  $format_id=HtmlInput::default_value_post("format", -1);
105  if ( $format_id == -1 ||isNumber($format_id) == 0 ) {
106  alert(_('Format inconnu'));
107  return;
108  }
109  /**
110  * if remove format is asked , delete and return
111  */
112  if ( isset($_POST['remove_format'])) {
113  $format_bank=new Format_Bank_Sql($cn,$format_id);
114  $format_bank->delete();
115  return;
116  }
117  /*
118  * First time or the format is not corrected
119  */
120  if ( ! isset($_POST['correct_format']))
121  {
122  $format->value=$_POST['format_name'];
123  $jrn_def->selected=$_POST['jrn_def'];
124  $sep_field->selected=$_POST['sep_field'];
125  $sep_thousand->selected=$_POST['sep_thous'];
126  $sep_decimal->selected=$_POST['sep_dec'];
127  $format_date->selected=$_POST['format_date'];
128  $nb_col->value=$_POST['nb_col'];
129  $skip->value=$_POST['skip'];
130 
131  if ( trim($_FILES['import_file']['name']) == '')
132  {
133  alert(_('Pas de fichier donné'));
134  return -1;
135  }
136  $filename=tempnam($_ENV['TMP'],'upload_');
137  move_uploaded_file($_FILES["import_file"]["tmp_name"],$filename);
138  $fbank=fopen($filename,'r');
139  $pos_date=$pos_amount=$pos_lib=$pos_operation_nb=$pos_third=$pos_extra=-1;
140 
141  // Load the order of the header
142  if ( $_POST['format'] != 0)
143  {
144  $format_bank=new Format_Bank_Sql($cn,$format_id);
145  $pos_date=$format_bank->pos_date;
146  $pos_amount=$format_bank->pos_amount;
147  $pos_lib=$format_bank->pos_lib;
148  $pos_operation_nb=$format_bank->pos_operation_nb;
149  $pos_third=$format_bank->pos_third;
150  $pos_extra=$format_bank->pos_extra;
151 
152  }
153  // Separator for thousand and decimal MUST be
154  // different
155  if( $sep_decimal->selected==$sep_thousand->selected ) {
156  alert(_('Les séparateurs décimals et de millier doivent être différents'));
157 
158  }
159  echo '<div class="content" style="width:80%;margin-left:10%">';
160  $sb='confirm';
161  require_once ('template/confirm_transfer.php');
162  echo '</div>';
163  exit();
164  }
165  else
166  {
167  $format->value=$_POST['format_name'];
168  $jrn_def->selected=$_POST['jrn_def'];
169  $sep_field->selected=$_POST['sep_field'];
170  $sep_thousand->selected=$_POST['sep_thous'];
171  $sep_decimal->selected=$_POST['sep_dec'];
172  $format_date->selected=$_POST['format_date'];
173  $nb_col->value=$_POST['nb_col'];
174  $skip->value=$_POST['skip'];
175 
176 
177  $filename=$_POST['filename'];
178 
179  $fbank=fopen($filename,'r');
180  $pos_date=$pos_amount=$pos_lib=$pos_operation_nb=-1;
181 
182  $pos_date=$pos_amount=$pos_lib=$pos_operation_nb=$pos_third=$pos_extra=-1;
183 
184  // Load the order of the header
185  if ( $_POST['format'] != 0)
186  {
187  $format_bank=new Format_Bank_Sql($cn,$_POST ['format']);
188  $pos_date=$format_bank->pos_date;
189  $pos_amount=$format_bank->pos_amount;
190  $pos_lib=$format_bank->pos_lib;
191  $pos_operation_nb=$format_bank->pos_operation_nb;
192  $pos_third=$format_bank->pos_third;
193  $pos_extra=$format_bank->pos_extra;
194 
195  }
196  // Separator for thousand and decimal MUST be
197  // different
198  if( $sep_decimal->selected==$sep_thousand->selected ) {
199  alert(_('Les séparateurs décimals et de millier doivent être différents'));
200  }
201  echo '<div class="content" style="width:80%;margin-left:10%">';
202  $sb='confirm';
203  require_once ('template/confirm_transfer.php');
204  echo '</div>';
205  exit();
206  }
207  }
208 /*
209  * Step 4
210  * The file is now uploaded, we put in temp and show what has be done, and save the format (or update
211  * if already exist)
212  */
213 if ( $_POST['sb'] == 'confirm')
214  {
215  $id=($_POST['format'] == 0)?-1:$_POST['format'];
216 
217  $format->value=$_POST['format_name'];
218  $jrn_def->selected=$_POST['jrn_def'];
219  $sep_field->selected=$_POST['sep_field'];
220  $sep_thousand->selected=$_POST['sep_thous'];
221  $sep_decimal->selected=$_POST['sep_dec'];
222  $format_date->selected=$_POST['format_date'];
223  $nb_col->value=$_POST['nb_col'];
224  $skip->value=$_POST['skip'];
225 
226  $format_bank=new Format_Bank_Sql($cn,$id);
227  $format_bank->format_name=$_POST['format_name'];
228  $format_bank->jrn_def_id=$_POST['jrn_def'];
229  $format_bank->sep_field=$_POST['sep_field'];
230  $format_bank->sep_thousand=$_POST['sep_thous'];
231  $format_bank->sep_decimal=$_POST['sep_dec'];
232  $format_bank->format_date=$_POST['format_date'];
233  $format_bank->nb_col=$_POST['nb_col'];
234  $format_bank->skip=$_POST['skip'];
235  /**
236  * Check that the destination ledger is well configured and the accounting
237  * is properly set and existing
238  */
239  $jrn_def_id= $jrn_def->selected;
240 
241  if ($jrn_def_id == 0 ) {
242  alert(_('Journal financier mal configuré'));
243  return;
244  }
245 
246  // Check if the accounting is correct and exist
247  $exist = Import_Bank::check_bank_account($jrn_def_id);
248 
249  if ( $exist == 0 ) {
250  alert(_('Poste comptable de la fiche banque est incorrect'));
251  return;
252  }
253  /*
254  * Verify that we have at least date + amount, and not duplicate
255  */
256  $check=Import_Bank::is_valid_header($_POST['header']);
257  if ( $check != '' )
258  {
259  alert($check);
260  /*
261  * Back to step 3 !
262  */
263  $filename=$_POST['filename'];
264 
265  $fbank=fopen($filename,'r');
266  $pos_date=$pos_amount=$pos_lib=$pos_operation_nb=$pos_third=$pos_extra=-1;
267  // Load the order of the header
268  if ( $_POST['format'] != 0)
269  {
270  $format_bank=new Format_Bank_Sql($cn,$_POST ['format']);
271  $pos_date=$format_bank->pos_date;
272  $pos_amount=$format_bank->pos_amount;
273  $pos_lib=$format_bank->pos_lib;
274  $pos_operation_nb=$format_bank->pos_operation_nb;
275  $pos_third=$format_bank->pos_third;
276  $pos_extra=$format_bank->pos_extra;
277  }
278 
279  echo '<div class="content" style="width:80%;margin-left:10%">';
280  $sb='confirm';
281  require_once ('template/confirm_transfer.php');
282  echo '</div>';
283  exit();
284  }
285 
286  /*
287  * save the column position for the date, amount,...
288  */
289  for($i=0;$i<count($_POST['header']);$i++)
290  {
291  switch($_POST['header'][$i])
292  {
293  case 0:
294  $format_bank->pos_date=$i;
295  break;
296  case 1:
297  $format_bank->pos_amount=$i;
298  break;
299  case 2:
300  $format_bank->pos_lib=$i;
301  break;
302  case 3:
303  $format_bank->pos_operation_nb=$i;
304  break;
305  case 4:
306  $format_bank->pos_third=$i;
307  break;
308  case 5:
309  $format_bank->pos_extra=$i;
310  break;
311  }
312  }
313  $format_bank->save();
314 
315  /*
316  *read file and save it into importbank.temp_bank
317  */
318  $fbank=fopen($_REQUEST['filename'],'r');
319  echo '<div class="content" style="width:80%;margin-left:10%">';
320  require_once('template/show_transfer.php');
321  echo '</div>';
322  }
323 ?>
global $aformat_date
$format_id
Definition: index.php:61
$new
Definition: show_import.php:30
$format_date
Definition: import_bank.php:57
$sep_decimal
Definition: import_bank.php:46
$col_valid
Definition: import_bank.php:55
global $aseparator
$sep_thousand
Definition: import_bank.php:49
if(!isset($_REQUEST['sb'])) $format
Definition: import_bank.php:43
$sep_field
Definition: import_bank.php:52
static check_bank_account($p_ledger)
Check that the legder has an card for the bank and this card must also have a valid accounting...
$skip
Definition: import_bank.php:61
$sb
Definition: am_print.php:38
global $adecimal
static is_valid_header($array)
check that there is no duplicate among header and we have at least the date and the amount ...
$_REQUEST['sb']
Definition: am_print.php:39
$check
Definition: show_list.php:118
global $athousand
$file
Definition: import_bank.php:60
$nb_col
Definition: import_bank.php:65
$jrn_def
Definition: import_bank.php:44
global $cn