dolibarr  17.0.4
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-2019 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-2021 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 <https://www.gnu.org/licenses/>.
22  */
23 
30 // Load Dolibarr environment
31 require '../../main.inc.php';
32 require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
33 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
34 
35 // Load translation files required by the page
36 $langs->loadLangs(array('banks', 'categories', 'multicurrency'));
37 
38 
39 $socid = 0;
40 if ($user->socid > 0) {
41  $socid = $user->socid;
42 }
43 if (!$user->rights->banque->transfer) {
45 }
46 
47 $action = GETPOST('action', 'aZ09');
48 $error = 0;
49 
50 $hookmanager->initHooks(array('banktransfer'));
51 
52 
53 /*
54  * Actions
55  */
56 
57 $parameters = array('socid' => $socid);
58 $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
59 if ($reshook < 0) {
60  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
61 }
62 if ($action == 'add') {
63  $langs->load("errors");
64 
65  $dateo = dol_mktime(12, 0, 0, GETPOST('remonth', 'int'), GETPOST('reday', 'int'), GETPOST('reyear', 'int'));
66  $label = GETPOST('label', 'alpha');
67  $amount = price2num(GETPOST('amount', 'alpha'), 'MT', 2);
68  $amountto = price2num(GETPOST('amountto', 'alpha'), 'MT', 2);
69 
70  if (!$label) {
71  $error++;
72  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Description")), null, 'errors');
73  }
74  if (!$amount) {
75  $error++;
76  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount")), null, 'errors');
77  }
78  if (!GETPOST('account_from', 'int')) {
79  $error++;
80  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("TransferFrom")), null, 'errors');
81  }
82  if (!GETPOST('account_to', 'int')) {
83  $error++;
84  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("TransferTo")), null, 'errors');
85  }
86  if (!$error) {
87  require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
88 
89  $accountfrom = new Account($db);
90  $accountfrom->fetch(GETPOST('account_from', 'int'));
91 
92  $accountto = new Account($db);
93  $accountto->fetch(GETPOST('account_to', 'int'));
94 
95  if ($accountto->currency_code == $accountfrom->currency_code) {
96  $amountto = $amount;
97  } else {
98  if (!$amountto) {
99  $error++;
100  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AmountToOthercurrency")), null, 'errors');
101  }
102  }
103  if ($amountto < 0) {
104  $error++;
105  setEventMessages($langs->trans("AmountMustBePositive"), null, 'errors');
106  }
107 
108  if ($accountto->id == $accountfrom->id) {
109  $error++;
110  setEventMessages($langs->trans("ErrorFromToAccountsMustDiffers"), null, 'errors');
111  }
112 
113  if (empty($error)) {
114  $db->begin();
115 
116  $bank_line_id_from = 0;
117  $bank_line_id_to = 0;
118  $result = 0;
119 
120  // By default, electronic transfert from bank to bank
121  $typefrom = 'PRE';
122  $typeto = 'VIR';
123  if ($accountto->courant == Account::TYPE_CASH || $accountfrom->courant == Account::TYPE_CASH) {
124  // This is transfer of change
125  $typefrom = 'LIQ';
126  $typeto = 'LIQ';
127  }
128 
129  if (!$error) {
130  $bank_line_id_from = $accountfrom->addline($dateo, $typefrom, $label, price2num(-1 * $amount), '', '', $user);
131  }
132  if (!($bank_line_id_from > 0)) {
133  $error++;
134  }
135  if (!$error) {
136  $bank_line_id_to = $accountto->addline($dateo, $typeto, $label, $amountto, '', '', $user);
137  }
138  if (!($bank_line_id_to > 0)) {
139  $error++;
140  }
141 
142  if (!$error) {
143  $result = $accountfrom->add_url_line($bank_line_id_from, $bank_line_id_to, DOL_URL_ROOT.'/compta/bank/line.php?rowid=', '(banktransfert)', 'banktransfert');
144  }
145  if (!($result > 0)) {
146  $error++;
147  }
148  if (!$error) {
149  $result = $accountto->add_url_line($bank_line_id_to, $bank_line_id_from, DOL_URL_ROOT.'/compta/bank/line.php?rowid=', '(banktransfert)', 'banktransfert');
150  }
151  if (!($result > 0)) {
152  $error++;
153  }
154 
155  if (!$error) {
156  $mesgs = $langs->trans("TransferFromToDone", '{s1}', '{s2}', $amount, $langs->transnoentitiesnoconv("Currency".$conf->currency));
157  $mesgs = str_replace('{s1}', '<a href="bankentries_list.php?id='.$accountfrom->id.'&sortfield=b.datev,b.dateo,b.rowid&sortorder=desc">'.$accountfrom->label.'</a>', $mesgs);
158  $mesgs = str_replace('{s2}', '<a href="bankentries_list.php?id='.$accountto->id.'">'.$accountto->label.'</a>', $mesgs);
159  setEventMessages($mesgs, null, 'mesgs');
160  $db->commit();
161  } else {
162  setEventMessages($accountfrom->error.' '.$accountto->error, null, 'errors');
163  $db->rollback();
164  }
165  }
166  }
167 }
168 
169 
170 
171 /*
172  * View
173  */
174 
175 $form = new Form($db);
176 
177 $help_url = 'EN:Module_Banks_and_Cash|FR:Module_Banques_et_Caisses|ES:M&oacute;dulo_Bancos_y_Cajas';
178 $title = $langs->trans('MenuBankInternalTransfer');
179 
180 llxHeader('', $title, $help_url);
181 
182 
183 print ' <script type="text/javascript">
184  $(document).ready(function () {
185  $(".selectbankaccount").change(function() {
186  console.log("We change bank account. We check if currency differs. If yes, we show multicurrency field");
187  init_page();
188  });
189 
190  function init_page() {
191  console.log("Set fields according to currency");
192  var account1 = $("#selectaccount_from").val();
193  var account2 = $("#selectaccount_to").val();
194  var currencycode1="";
195  var currencycode2="";
196 
197  $.get("'.DOL_URL_ROOT.'/core/ajax/getaccountcurrency.php", {id: account1})
198  .done(function( data ) {
199  if (data != null)
200  {
201  var item= $.parseJSON(data);
202  if (item.num==-1) {
203  console.error("Error: "+item.error);
204  } else if (item.num!==0) {
205  currencycode1 = item.value;
206  }
207  console.log(currencycode1);
208 
209  $.get("'.DOL_URL_ROOT.'/core/ajax/getaccountcurrency.php", {id: account2})
210  .done(function( data ) {
211  if (data != null)
212  {
213  var item=$.parseJSON(data);
214  if (item.num==-1) {
215  console.error("Error: "+item.error);
216  } else if (item.num!==0) {
217  currencycode2 = item.value;
218  }
219  console.log(currencycode2);
220 
221  if (currencycode2!==currencycode1 && currencycode2!=="" && currencycode1!=="") {
222  console.log("We show multicurrency fields");
223  $(".multicurrency").show();
224  } else {
225  $(".multicurrency").hide();
226  }
227  }
228  else {
229  console.error("Error: Ajax url has returned an empty page. Should be an empty json array.");
230  }
231  }).fail(function( data ) {
232  console.error("Error: has returned an empty page. Should be an empty json array.");
233  });
234  }
235  else {
236  console.error("Error: has returned an empty page. Should be an empty json array.");
237  }
238  }).fail(function( data ) {
239  console.error("Error: has returned an empty page. Should be an empty json array.");
240  });
241  }
242 
243  init_page();
244  });
245  </script>';
246 
247 
248 $account_from = '';
249 $account_to = '';
250 $label = '';
251 $amount = '';
252 $amountto = '';
253 
254 if ($error) {
255  $account_from = GETPOST('account_from', 'int');
256  $account_to = GETPOST('account_to', 'int');
257  $label = GETPOST('label', 'alpha');
258  $amount = GETPOST('amount', 'alpha');
259 }
260 
261 print load_fiche_titre($langs->trans("MenuBankInternalTransfer"), '', 'bank_account');
262 
263 print '<span class="opacitymedium">'.$langs->trans("TransferDesc").'</span>';
264 print "<br><br>";
265 
266 print '<form name="add" method="post" action="'.$_SERVER["PHP_SELF"].'">';
267 print '<input type="hidden" name="token" value="'.newToken().'">';
268 print '<input type="hidden" name="action" value="add">';
269 
270 print '<div class="div-table-responsive-no-min">';
271 print '<table class="noborder centpercent">';
272 print '<tr class="liste_titre">';
273 print '<th>'.$langs->trans("TransferFrom").'</th>';
274 print '<th>'.$langs->trans("TransferTo").'</th>';
275 print '<th>'.$langs->trans("Date").'</th>';
276 print '<th>'.$langs->trans("Description").'</th>';
277 print '<th class="right">'.$langs->trans("Amount").'</th>';
278 print '<td class="hideobject multicurrency right">'.$langs->trans("AmountToOthercurrency").'</td>';
279 print '</tr>';
280 
281 print '<tr class="oddeven">';
282 
283 print '<td class="nowraponall">';
284 print img_picto('', 'bank_account', 'class="paddingright"');
285 $form->select_comptes($account_from, 'account_from', 0, '', 1, '', !isModEnabled('multicurrency') ? 0 : 1, 'minwidth100');
286 print "</td>";
287 
288 print '<td class="nowraponall">';
289 print img_picto('', 'bank_account', 'class="paddingright"');
290 $form->select_comptes($account_to, 'account_to', 0, '', 1, '', !isModEnabled('multicurrency') ? 0 : 1, 'minwidth100');
291 print "</td>\n";
292 
293 print '<td class="nowraponall">';
294 print $form->selectDate((!empty($dateo) ? $dateo : ''), '', '', '', '', 'add');
295 print "</td>";
296 
297 print '<td><input name="label" class="flat quatrevingtpercent" type="text" value="'.dol_escape_htmltag($label).'"></td>';
298 
299 print '<td class="right"><input name="amount" class="flat right" type="text" size="6" value="'.dol_escape_htmltag($amount).'"></td>';
300 
301 print '<td class="hideobject multicurrency right"><input name="amountto" class="flat" type="text" size="6" value="'.dol_escape_htmltag($amountto).'"></td>';
302 
303 print "</table>";
304 print '</div>';
305 
306 print '<br><div class="center"><input type="submit" class="button" value="'.$langs->trans("Create").'"></div>';
307 
308 print "</form>";
309 
310 // End of page
311 llxFooter();
312 $db->close();
if(GETPOST('button_removefilter_x', 'alpha')||GETPOST('button_removefilter.x', 'alpha')||GETPOST('button_removefilter', 'alpha')) if(GETPOST('button_search_x', 'alpha')||GETPOST('button_search.x', 'alpha')||GETPOST('button_search', 'alpha')) if($action=="save" &&empty($cancel)) $help_url
View.
Definition: agenda.php:118
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:56
llxFooter()
Empty footer.
Definition: wrapper.php:70
Class to manage bank accounts.
const TYPE_CASH
Cash account.
Class to manage generation of HTML components Only common components must be here.
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
isModEnabled($module)
Is Dolibarr module enabled.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.