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