dolibarr  9.0.0
transfer.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2001-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2008 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2005-2015 Regis Houssin <regis.houssin@inodbox.com>
5  * Copyright (C) 2012 Juanjo Menent <jmenent@2byte.es>
6  * Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
7  * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
8  * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
30 require '../../main.inc.php';
31 require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
32 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
33 
34 // Load translation files required by the page
35 $langs->loadLangs(array("banks", "categories", "multicurrency"));
36 
37 if (! $user->rights->banque->transfer)
39 
40 $action = GETPOST('action','alpha');
41 $error = 0;
42 
43 
44 /*
45  * Actions
46  */
47 
48 if ($action == 'add')
49 {
50  $langs->load("errors");
51 
52  $dateo = dol_mktime(12,0,0,GETPOST('remonth','int'),GETPOST('reday','int'),GETPOST('reyear','int'));
53  $label = GETPOST('label','alpha');
54  $amount= GETPOST('amount','alpha');
55  $amountto= GETPOST('amountto','alpha');
56 
57  if (! $label)
58  {
59  $error++;
60  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Description")), null, 'errors');
61  }
62  if (! $amount)
63  {
64  $error++;
65  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount")), null, 'errors');
66  }
67  if (! GETPOST('account_from','int'))
68  {
69  $error++;
70  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("TransferFrom")), null, 'errors');
71  }
72  if (! GETPOST('account_to','int'))
73  {
74  $error++;
75  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("TransferTo")), null, 'errors');
76  }
77  if (! $error)
78  {
79  require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
80 
81  $accountfrom=new Account($db);
82  $accountfrom->fetch(GETPOST('account_from','int'));
83 
84  $accountto=new Account($db);
85  $accountto->fetch(GETPOST('account_to','int'));
86 
87  if ($accountto->currency_code == $accountfrom->currency_code)
88  {
89  $amountto=$amount;
90  }
91  else
92  {
93  if (! $amountto)
94  {
95  $error++;
96  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AmountTo")), null, 'errors');
97  }
98  }
99 
100  if (($accountto->id != $accountfrom->id) && empty($error))
101  {
102  $db->begin();
103 
104  $bank_line_id_from=0;
105  $bank_line_id_to=0;
106  $result=0;
107 
108  // By default, electronic transfert from bank to bank
109  $typefrom='PRE';
110  $typeto='VIR';
111  if ($accountto->courant == Account::TYPE_CASH || $accountfrom->courant == Account::TYPE_CASH)
112  {
113  // This is transfer of change
114  $typefrom='LIQ';
115  $typeto='LIQ';
116  }
117 
118  if (! $error) $bank_line_id_from = $accountfrom->addline($dateo, $typefrom, $label, -1*price2num($amount), '', '', $user);
119  if (! ($bank_line_id_from > 0)) $error++;
120  if (! $error) $bank_line_id_to = $accountto->addline($dateo, $typeto, $label, price2num($amountto), '', '', $user);
121  if (! ($bank_line_id_to > 0)) $error++;
122 
123  if (! $error) $result=$accountfrom->add_url_line($bank_line_id_from, $bank_line_id_to, DOL_URL_ROOT.'/compta/bank/ligne.php?rowid=', '(banktransfert)', 'banktransfert');
124  if (! ($result > 0)) $error++;
125  if (! $error) $result=$accountto->add_url_line($bank_line_id_to, $bank_line_id_from, DOL_URL_ROOT.'/compta/bank/ligne.php?rowid=', '(banktransfert)', 'banktransfert');
126  if (! ($result > 0)) $error++;
127 
128  if (! $error)
129  {
130  $mesgs = $langs->trans("TransferFromToDone", '<a href="bankentries_list.php?id='.$accountfrom->id.'&sortfield=b.datev,b.dateo,b.rowid&sortorder=desc">'.$accountfrom->label."</a>", '<a href="bankentries_list.php?id='.$accountto->id.'">'.$accountto->label."</a>", $amount, $langs->transnoentities("Currency".$conf->currency));
131  setEventMessages($mesgs, null, 'mesgs');
132  $db->commit();
133  }
134  else
135  {
136  setEventMessages($accountfrom->error.' '.$accountto->error, null, 'errors');
137  $db->rollback();
138  }
139  }
140  else
141  {
142  $error++;
143  setEventMessages($langs->trans("ErrorFromToAccountsMustDiffers"), null, 'errors');
144  }
145  }
146 }
147 
148 
149 
150 /*
151  * View
152  */
153 
154 llxHeader();
155 print ' <script type="text/javascript">
156  $(document).ready(function () {
157  $(".selectbankaccount").change(function() {
158  console.log("We change bank account");
159  init_page();
160  });
161 
162  function init_page() {
163  console.log("Set fields according to currency");
164  var account1 = $("#selectaccount_from").val();
165  var account2 = $("#selectaccount_to").val();
166  var currencycode1="";
167  var currencycode2="";
168 
169  $.get("'.DOL_URL_ROOT.'/core/ajax/getaccountcurrency.php", {id: account1})
170  .done(function( data ) {
171  if (data != null)
172  {
173  var item= $.parseJSON(data);
174  if (item.num==-1) {
175  console.error("Error: "+item.error);
176  } else if (item.num!==0) {
177  currencycode1 = item.value;
178  }
179 
180  $.get("'.DOL_URL_ROOT.'/core/ajax/getaccountcurrency.php", {id: account2})
181  .done(function( data ) {
182  if (data != null)
183  {
184  var item=$.parseJSON(data);
185  if (item.num==-1) {
186  console.error("Error: "+item.error);
187  } else if (item.num!==0) {
188  currencycode2 = item.value;
189  }
190 
191  if (currencycode2!==currencycode1 && currencycode2!=="" && currencycode1!=="") {
192  $(".multicurrency").show();
193  } else {
194  $(".multicurrency").hide();
195  }
196  }
197  else {
198  console.error("Error: Ajax url has returned an empty page. Should be an empty json array.");
199  }
200  }).fail(function( data ) {
201  console.error("Error: has returned an empty page. Should be an empty json array.");
202  });
203  }
204  else {
205  console.error("Error: has returned an empty page. Should be an empty json array.");
206  }
207  }).fail(function( data ) {
208  console.error("Error: has returned an empty page. Should be an empty json array.");
209  });
210  }
211 
212  init_page();
213  });
214  </script>';
215 
216 $form=new Form($db);
217 
218 $account_from='';
219 $account_to='';
220 $label='';
221 $amount='';
222 
223 if ($error)
224 {
225  $account_from = GETPOST('account_from','int');
226  $account_to = GETPOST('account_to','int');
227  $label = GETPOST('label','alpha');
228  $amount = GETPOST('amount','alpha');
229 }
230 
231 print load_fiche_titre($langs->trans("MenuBankInternalTransfer"), '', 'title_bank.png');
232 
233 print $langs->trans("TransferDesc");
234 print "<br><br>";
235 
236 print '<form name="add" method="post" action="'.$_SERVER["PHP_SELF"].'">';
237 print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
238 
239 print '<input type="hidden" name="action" value="add">';
240 
241 print '<table class="noborder" width="100%">';
242 print '<tr class="liste_titre">';
243 print '<td>'.$langs->trans("TransferFrom").'</td><td>'.$langs->trans("TransferTo").'</td><td>'.$langs->trans("Date").'</td><td>'.$langs->trans("Description").'</td><td>'.$langs->trans("Amount").'</td>';
244 print '<td style="display:none" class="multicurrency">'.$langs->trans("AmountToOthercurrency").'</td>';
245 print '</tr>';
246 
247 $var=false;
248 print '<tr class="oddeven"><td>';
249 $form->select_comptes($account_from, 'account_from', 0, '', 1, '', empty($conf->multicurrency->enabled)?0:1);
250 print "</td>";
251 
252 print "<td>\n";
253 $form->select_comptes($account_to, 'account_to', 0, '', 1, '', empty($conf->multicurrency->enabled)?0:1);
254 print "</td>\n";
255 
256 print "<td>";
257 print $form->selectDate((! empty($dateo)?$dateo:''), '', '', '', '', 'add');
258 print "</td>\n";
259 print '<td><input name="label" class="flat quatrevingtpercent" type="text" value="'.dol_escape_htmltag($label).'"></td>';
260 print '<td><input name="amount" class="flat" type="text" size="6" value="'.dol_escape_htmltag($amount).'"></td>';
261 print '<td style="display:none" class="multicurrency"><input name="amountto" class="flat" type="text" size="6" value="'.dol_escape_htmltag($amountto).'"></td>';
262 
263 print "</table>";
264 
265 print '<br><div class="center"><input type="submit" class="button" value="'.$langs->trans("Add").'"></div>';
266 
267 print "</form>";
268 
269 // End of page
270 llxFooter();
271 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:56
load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
print
Draft customers invoices.
Definition: index.php:91
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm=false, $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
Class to manage bank accounts.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Class to manage generation of HTML components Only common components must be here.
llxHeader()
Empty header.
Definition: wrapper.php:44
const TYPE_CASH
Cash account.
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is &#39;...