dolibarr  13.0.2
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 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 
37 if (!$user->rights->banque->transfer)
39 
40 $action = GETPOST('action', 'aZ09');
41 $error = 0;
42 
43 $hookmanager->initHooks(array('banktransfer'));
44 
45 /*
46  * Actions
47  */
48 $parameters = array('socid' => $socid);
49 $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
50 if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
51 if ($action == 'add')
52 {
53  $langs->load("errors");
54 
55  $dateo = dol_mktime(12, 0, 0, GETPOST('remonth', 'int'), GETPOST('reday', 'int'), GETPOST('reyear', 'int'));
56  $label = GETPOST('label', 'alpha');
57  $amount = price2num(GETPOST('amount', 'alpha'), 'MT');
58  $amountto = price2num(GETPOST('amountto', 'alpha'), 'MT');
59 
60  if (!$label)
61  {
62  $error++;
63  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Description")), null, 'errors');
64  }
65  if (!$amount)
66  {
67  $error++;
68  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount")), null, 'errors');
69  }
70  if (!GETPOST('account_from', 'int'))
71  {
72  $error++;
73  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("TransferFrom")), null, 'errors');
74  }
75  if (!GETPOST('account_to', 'int'))
76  {
77  $error++;
78  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("TransferTo")), null, 'errors');
79  }
80  if (!$error)
81  {
82  require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
83 
84  $accountfrom = new Account($db);
85  $accountfrom->fetch(GETPOST('account_from', 'int'));
86 
87  $accountto = new Account($db);
88  $accountto->fetch(GETPOST('account_to', 'int'));
89 
90  if ($accountto->currency_code == $accountfrom->currency_code)
91  {
92  $amountto = $amount;
93  } else {
94  if (!$amountto)
95  {
96  $error++;
97  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AmountTo")), null, 'errors');
98  }
99  }
100 
101  if (($accountto->id != $accountfrom->id) && empty($error))
102  {
103  $db->begin();
104 
105  $bank_line_id_from = 0;
106  $bank_line_id_to = 0;
107  $result = 0;
108 
109  // By default, electronic transfert from bank to bank
110  $typefrom = 'PRE';
111  $typeto = 'VIR';
112  if ($accountto->courant == Account::TYPE_CASH || $accountfrom->courant == Account::TYPE_CASH)
113  {
114  // This is transfer of change
115  $typefrom = 'LIQ';
116  $typeto = 'LIQ';
117  }
118 
119  if (!$error) $bank_line_id_from = $accountfrom->addline($dateo, $typefrom, $label, price2num(-1 * $amount), '', '', $user);
120  if (!($bank_line_id_from > 0)) $error++;
121  if (!$error) $bank_line_id_to = $accountto->addline($dateo, $typeto, $label, $amountto, '', '', $user);
122  if (!($bank_line_id_to > 0)) $error++;
123 
124  if (!$error) $result = $accountfrom->add_url_line($bank_line_id_from, $bank_line_id_to, DOL_URL_ROOT.'/compta/bank/line.php?rowid=', '(banktransfert)', 'banktransfert');
125  if (!($result > 0)) $error++;
126  if (!$error) $result = $accountto->add_url_line($bank_line_id_to, $bank_line_id_from, DOL_URL_ROOT.'/compta/bank/line.php?rowid=', '(banktransfert)', 'banktransfert');
127  if (!($result > 0)) $error++;
128 
129  if (!$error)
130  {
131  $mesgs = $langs->trans("TransferFromToDone", '{s1}', '{s2}', $amount, $langs->transnoentitiesnoconv("Currency".$conf->currency));
132  $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);
133  $mesgs = str_replace('{s2}', '<a href="bankentries_list.php?id='.$accountto->id.'">'.$accountto->label.'</a>', $mesgs);
134  setEventMessages($mesgs, null, 'mesgs');
135  $db->commit();
136  } else {
137  setEventMessages($accountfrom->error.' '.$accountto->error, null, 'errors');
138  $db->rollback();
139  }
140  } else {
141  $error++;
142  setEventMessages($langs->trans("ErrorFromToAccountsMustDiffers"), null, 'errors');
143  }
144  }
145 }
146 
147 
148 
149 /*
150  * View
151  */
152 
153 llxHeader();
154 
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"), '', 'bank_account');
232 
233 print '<span class="opacitymedium">'.$langs->trans("TransferDesc").'</span>';
234 print "<br><br>";
235 
236 print '<form name="add" method="post" action="'.$_SERVER["PHP_SELF"].'">';
237 print '<input type="hidden" name="token" value="'.newToken().'">';
238 
239 print '<input type="hidden" name="action" value="add">';
240 
241 print '<div class="div-table-responsive-no-min">';
242 print '<table class="noborder centpercent">';
243 print '<tr class="liste_titre">';
244 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>';
245 print '<td style="display:none" class="multicurrency">'.$langs->trans("AmountToOthercurrency").'</td>';
246 print '</tr>';
247 
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 print '</div>';
265 
266 print '<br><div class="center"><input type="submit" class="button" value="'.$langs->trans("Add").'"></div>';
267 
268 print "</form>";
269 
270 // End of page
271 llxFooter();
272 $db->close();
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
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...
Class to manage bank accounts.
llxHeader()
Empty header.
Definition: wrapper.php:45
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
Class to manage generation of HTML components Only common components must be here.
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 &#39;...
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 ...
const TYPE_CASH
Cash account.
print
Draft customers invoices.
Definition: index.php:89
llxFooter()
Empty footer.
Definition: wrapper.php:59