dolibarr  x.y.z
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("AmountTo")), 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 $help_url = 'EN:Module_Banks_and_Cash|FR:Module_Banques_et_Caisses|ES:M&oacute;dulo_Bancos_y_Cajas';
176 $title = $langs->trans('MenuBankInternalTransfer');
177 
178 llxHeader('', $title, $help_url);
179 
180 print ' <script type="text/javascript">
181  $(document).ready(function () {
182  $(".selectbankaccount").change(function() {
183  console.log("We change bank account");
184  init_page();
185  });
186 
187  function init_page() {
188  console.log("Set fields according to currency");
189  var account1 = $("#selectaccount_from").val();
190  var account2 = $("#selectaccount_to").val();
191  var currencycode1="";
192  var currencycode2="";
193 
194  $.get("'.DOL_URL_ROOT.'/core/ajax/getaccountcurrency.php", {id: account1})
195  .done(function( data ) {
196  if (data != null)
197  {
198  var item= $.parseJSON(data);
199  if (item.num==-1) {
200  console.error("Error: "+item.error);
201  } else if (item.num!==0) {
202  currencycode1 = item.value;
203  }
204 
205  $.get("'.DOL_URL_ROOT.'/core/ajax/getaccountcurrency.php", {id: account2})
206  .done(function( data ) {
207  if (data != null)
208  {
209  var item=$.parseJSON(data);
210  if (item.num==-1) {
211  console.error("Error: "+item.error);
212  } else if (item.num!==0) {
213  currencycode2 = item.value;
214  }
215 
216  if (currencycode2!==currencycode1 && currencycode2!=="" && currencycode1!=="") {
217  $(".multicurrency").show();
218  } else {
219  $(".multicurrency").hide();
220  }
221  }
222  else {
223  console.error("Error: Ajax url has returned an empty page. Should be an empty json array.");
224  }
225  }).fail(function( data ) {
226  console.error("Error: has returned an empty page. Should be an empty json array.");
227  });
228  }
229  else {
230  console.error("Error: has returned an empty page. Should be an empty json array.");
231  }
232  }).fail(function( data ) {
233  console.error("Error: has returned an empty page. Should be an empty json array.");
234  });
235  }
236 
237  init_page();
238  });
239  </script>';
240 
241 $form = new Form($db);
242 
243 $account_from = '';
244 $account_to = '';
245 $label = '';
246 $amount = '';
247 $amountto = '';
248 
249 if ($error) {
250  $account_from = GETPOST('account_from', 'int');
251  $account_to = GETPOST('account_to', 'int');
252  $label = GETPOST('label', 'alpha');
253  $amount = GETPOST('amount', 'alpha');
254 }
255 
256 print load_fiche_titre($langs->trans("MenuBankInternalTransfer"), '', 'bank_account');
257 
258 print '<span class="opacitymedium">'.$langs->trans("TransferDesc").'</span>';
259 print "<br><br>";
260 
261 print '<form name="add" method="post" action="'.$_SERVER["PHP_SELF"].'">';
262 print '<input type="hidden" name="token" value="'.newToken().'">';
263 
264 print '<input type="hidden" name="action" value="add">';
265 
266 print '<div class="div-table-responsive-no-min">';
267 print '<table class="noborder centpercent">';
268 print '<tr class="liste_titre">';
269 print '<td>'.$langs->trans("TransferFrom").'</td><td>'.$langs->trans("TransferTo").'</td><td>'.$langs->trans("Date").'</td><td>'.$langs->trans("Description").'</td>';
270 print '<td class="right">'.$langs->trans("Amount").'</td>';
271 print '<td style="display:none" class="multicurrency">'.$langs->trans("AmountToOthercurrency").'</td>';
272 print '</tr>';
273 
274 print '<tr class="oddeven"><td>';
275 print img_picto('', 'bank_account', 'class="paddingright"');
276 $form->select_comptes($account_from, 'account_from', 0, '', 1, '', !isModEnabled('multicurrency') ? 0 : 1);
277 print "</td>";
278 
279 print "<td>\n";
280 print img_picto('', 'bank_account', 'class="paddingright"');
281 $form->select_comptes($account_to, 'account_to', 0, '', 1, '', !isModEnabled('multicurrency') ? 0 : 1);
282 print "</td>\n";
283 
284 print "<td>";
285 print $form->selectDate((!empty($dateo) ? $dateo : ''), '', '', '', '', 'add');
286 print "</td>\n";
287 print '<td><input name="label" class="flat quatrevingtpercent" type="text" value="'.dol_escape_htmltag($label).'"></td>';
288 print '<td class="right"><input name="amount" class="flat right" type="text" size="6" value="'.dol_escape_htmltag($amount).'"></td>';
289 print '<td style="display:none" class="multicurrency"><input name="amountto" class="flat" type="text" size="6" value="'.dol_escape_htmltag($amountto).'"></td>';
290 
291 print "</table>";
292 print '</div>';
293 
294 print '<br><div class="center"><input type="submit" class="button" value="'.$langs->trans("Create").'"></div>';
295 
296 print "</form>";
297 
298 // End of page
299 llxFooter();
300 $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.