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