dolibarr  7.0.0-beta
card.php
1 <?php
2 /* Copyright (C) 2017 Alexandre Spangaro <aspangaro@zendsi.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
24 require '../../../main.inc.php';
25 require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
26 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
27 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/paymentvarious.class.php';
28 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formaccounting.class.php';
30 require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingaccount.class.php';
31 require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingjournal.class.php';
32 if (! empty($conf->projet->enabled))
33 {
34  require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
35  require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php';
36 }
37 
38 $langs->loadLangs(array("compta", "banks", "bills", "users", "accountancy"));
39 
40 // Get parameters
41 $id = GETPOST('id', 'int');
42 $action = GETPOST('action', 'alpha');
43 $cancel = GETPOST('cancel', 'aZ09');
44 $backtopage = GETPOST('backtopage', 'alpha');
45 
46 $accountid=GETPOST("accountid") > 0 ? GETPOST("accountid","int") : 0;
47 $label=GETPOST("label","alpha");
48 $sens=GETPOST("sens","int");
49 $amount=GETPOST("amount");
50 $paymenttype=GETPOST("paymenttype");
51 $accountancy_code=GETPOST("accountancy_code","int");
52 $projectid = (GETPOST('projectid') ? GETPOST('projectid', 'int') : 0);
53 
54 // Security check
55 $socid = GETPOST("socid","int");
56 if ($user->societe_id) $socid=$user->societe_id;
57 $result = restrictedArea($user, 'banque', '', '', '');
58 
59 $object = new PaymentVarious($db);
60 
61 // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
62 $hookmanager->initHooks(array('variouscard','globalcard'));
63 
64 
65 
70 $parameters=array();
71 $reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
72 if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
73 
74 if (empty($reshook))
75 {
76  // Link to a project
77  if ($action == 'classin' && $user->rights->banque->modifier)
78  {
79  $object->fetch($id);
80  $object->setProject(GETPOST('projectid'));
81  }
82 
83  if ($cancel)
84  {
85  if ($action != 'addlink')
86  {
87  $urltogo=$backtopage?$backtopage:dol_buildpath('/compta/bank/various_payment/index.php',1);
88  header("Location: ".$urltogo);
89  exit;
90  }
91  if ($id > 0 || ! empty($ref)) $ret = $object->fetch($id,$ref);
92  $action='';
93  }
94 
95  if ($action == 'add')
96  {
97  $error=0;
98 
99  $datep=dol_mktime(12,0,0, GETPOST("datepmonth"), GETPOST("datepday"), GETPOST("datepyear"));
100  $datev=dol_mktime(12,0,0, GETPOST("datevmonth"), GETPOST("datevday"), GETPOST("datevyear"));
101  if (empty($datev)) $datev=$datep;
102 
103  $object->accountid=GETPOST("accountid") > 0 ? GETPOST("accountid","int") : 0;
104  $object->datev=$datev;
105  $object->datep=$datep;
106  $object->amount=price2num(GETPOST("amount"));
107  $object->label=GETPOST("label");
108  $object->note=GETPOST("note");
109  $object->type_payment=GETPOST("paymenttype") > 0 ? GETPOST("paymenttype", "int") : 0;
110  $object->num_payment=GETPOST("num_payment");
111  $object->fk_user_author=$user->id;
112  $object->accountancy_code=GETPOST("accountancy_code") > 0 ? GETPOST("accountancy_code","int") : "";
113  $object->sens=GETPOST('sens');
114  $object->fk_project= GETPOST('fk_project');
115 
116  if (empty($datep) || empty($datev))
117  {
118  $langs->load('errors');
119  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Date")), null, 'errors');
120  $error++;
121  }
122  if (empty($object->type_payment) || $object->type_payment < 0)
123  {
124  $langs->load('errors');
125  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("PaymentMode")), null, 'errors');
126  $error++;
127  }
128  if (empty($object->amount))
129  {
130  $langs->load('errors');
131  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Amount")), null, 'errors');
132  $error++;
133  }
134  if (! empty($conf->banque->enabled) && ! $object->accountid > 0)
135  {
136  $langs->load('errors');
137  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("BankAccount")), null, 'errors');
138  $error++;
139  }
140  if (! empty($conf->accounting->enabled) && ! $object->accountancy_code)
141  {
142  $langs->load('errors');
143  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("AccountAccounting")), null, 'errors');
144  $error++;
145  }
146 
147  if (! $error)
148  {
149  $db->begin();
150 
151  $ret=$object->create($user);
152  if ($ret > 0)
153  {
154  $db->commit();
155  header("Location: index.php");
156  exit;
157  }
158  else
159  {
160  $db->rollback();
161  setEventMessages($object->error, $object->errors, 'errors');
162  $action="create";
163  }
164  }
165 
166  $action='create';
167  }
168 
169  if ($action == 'delete')
170  {
171  $result=$object->fetch($id);
172 
173  if ($object->rappro == 0)
174  {
175  $db->begin();
176 
177  $ret=$object->delete($user);
178  if ($ret > 0)
179  {
180  if ($object->fk_bank)
181  {
182  $accountline=new AccountLine($db);
183  $result=$accountline->fetch($object->fk_bank);
184  if ($result > 0) $result=$accountline->delete($user); // $result may be 0 if not found (when bank entry was deleted manually and fk_bank point to nothing)
185  }
186 
187  if ($result >= 0)
188  {
189  $db->commit();
190  header("Location: ".DOL_URL_ROOT.'/compta/bank/various_payment/index.php');
191  exit;
192  }
193  else
194  {
195  $object->error=$accountline->error;
196  $db->rollback();
197  setEventMessages($object->error, $object->errors, 'errors');
198  }
199  }
200  else
201  {
202  $db->rollback();
203  setEventMessages($object->error, $object->errors, 'errors');
204  }
205  }
206  else
207  {
208  setEventMessages('Error try do delete a line linked to a conciliated bank transaction', null, 'errors');
209  }
210  }
211 }
212 
213 
214 /*
215  * View
216  */
217 
218 llxHeader("",$langs->trans("VariousPayment"));
219 
220 $form = new Form($db);
221 if (! empty($conf->accounting->enabled)) $formaccounting = New FormAccounting($db);
222 if (! empty($conf->projet->enabled)) $formproject = new FormProjets($db);
223 
224 if ($id)
225 {
226  $object = new PaymentVarious($db);
227  $result = $object->fetch($id);
228  if ($result <= 0)
229  {
230  dol_print_error($db);
231  exit;
232  }
233 }
234 
235 /* ************************************************************************** */
236 /* */
237 /* Create mode */
238 /* */
239 /* ************************************************************************** */
240 if ($action == 'create')
241 {
242  print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
243  print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
244  print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
245  print '<input type="hidden" name="action" value="add">';
246 
247  print load_fiche_titre($langs->trans("NewVariousPayment"),'', 'title_accountancy.png');
248 
249  dol_fiche_head('', '');
250 
251  print '<table class="border" width="100%">';
252 
253  // Date payment
254  print '<tr><td>';
255  print fieldLabel('DatePayment','datep',1).'</td><td>';
256  print $form->select_date((empty($datep)?-1:$datep),"datep",'','','','add',1,1);
257  print '</td></tr>';
258 
259  // Date value for bank
260  print '<tr><td>';
261  print fieldLabel('DateValue','datev',0).'</td><td>';
262  print $form->select_date((empty($datev)?-1:$datev),"datev",'','','','add',1,1);
263  print '</td></tr>';
264 
265  // Label
266  print '<tr><td>';
267  print fieldLabel('Label','label',1).'</td><td>';
268  print '<input name="label" id="label" class="minwidth300" value="'.($label?$label:$langs->trans("VariousPayment")).'">';
269  print '</td></tr>';
270 
271  // Sens
272  print '<tr><td>';
273  print fieldLabel('Sens','sens',1).'</td><td>';
274  $sensarray=array( '0' => $langs->trans("Debit"), '1' => $langs->trans("Credit"));
275  print $form->selectarray('sens',$sensarray,$sens);
276  print '</td></tr>';
277 
278  // Amount
279  print '<tr><td>';
280  print fieldLabel('Amount','amount',1).'</td><td>';
281  print '<input name="amount" id="amount" class="minwidth100" value="'.$amount.'">';
282  print '</td></tr>';
283 
284  // Bank
285  if (! empty($conf->banque->enabled))
286  {
287  print '<tr><td>';
288  print fieldLabel('BankAccount','selectaccountid',1).'</td><td>';
289  $form->select_comptes($accountid,"accountid",0,'',1); // Affiche liste des comptes courant
290  print '</td></tr>';
291  }
292 
293  // Type payment
294  print '<tr><td>';
295  print fieldLabel('PaymentMode','selectpaymenttype',1).'</td><td>';
296  $form->select_types_paiements($paymenttype, "paymenttype");
297  print '</td></tr>';
298 
299  // Number
300  if (! empty($conf->banque->enabled))
301  {
302  // Number
303  print '<tr><td><label for="num_payment">'.$langs->trans('Numero');
304  print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
305  print '</label></td>';
306  print '<td><input name="num_payment" id="num_payment" type="text" value="'.GETPOST("num_payment").'"></td></tr>'."\n";
307  }
308 
309  // Accountancy account
310  if (! empty($conf->accounting->enabled))
311  {
312  print '<tr><td class="fieldrequired">'.$langs->trans("AccountAccounting").'</td>';
313  print '<td>';
314  print $formaccounting->select_account($accountancy_code, 'accountancy_code', 1, null, 1, 1, '');
315  print '</td></tr>';
316  }
317  else // For external software
318  {
319  print '<tr><td>'.$langs->trans("AccountAccounting").'</td>';
320  print '<td class="maxwidthonsmartphone"><input class="minwidth100" name="accountancy_code" value="'.$accountancy_code.'">';
321  print '</td></tr>';
322  }
323 
324  // Project
325  if (! empty($conf->projet->enabled))
326  {
327  $formproject=new FormProjets($db);
328 
329  // Associated project
330  $langs->load("projects");
331 
332  print '<tr><td>'.$langs->trans("Project").'</td><td>';
333 
334  $numproject=$formproject->select_projects(-1, $projectid,'fk_project',0,0,1,1);
335 
336  print '</td></tr>';
337  }
338 
339  // Other attributes
340  $parameters=array();
341  $reshook=$hookmanager->executeHooks('formObjectOptions',$parameters,$object,$action); // Note that $action and $object may have been modified by hook
342  print $hookmanager->resPrint;
343 
344  print '</table>';
345 
346  dol_fiche_end();
347 
348  print '<div class="center">';
349  print '<input type="submit" class="button" value="'.$langs->trans("Save").'">';
350  print ' &nbsp; ';
351  print '<input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'">';
352  print '</div>';
353 
354  print '</form>';
355 }
356 
357 
358 /* ************************************************************************** */
359 /* */
360 /* View mode */
361 /* */
362 /* ************************************************************************** */
363 
364 if ($id)
365 {
366  $head=various_payment_prepare_head($object);
367 
368  dol_fiche_head($head, 'card', $langs->trans("VariousPayment"), -1, 'payment');
369 
370  $morehtmlref='<div class="refidno">';
371  // Project
372  if (! empty($conf->projet->enabled))
373  {
374  $langs->load("projects");
375  $morehtmlref.=$langs->trans('Project') . ' ';
376  if ($user->rights->tax->charges->creer)
377  {
378  if ($action != 'classify')
379  $morehtmlref.='<a href="' . $_SERVER['PHP_SELF'] . '?action=classify&amp;id=' . $object->id . '">' . img_edit($langs->transnoentitiesnoconv('SetProject')) . '</a> : ';
380  if ($action == 'classify') {
381  //$morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'projectid', 0, 0, 1, 1);
382  $morehtmlref.='<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'">';
383  $morehtmlref.='<input type="hidden" name="action" value="classin">';
384  $morehtmlref.='<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
385  $morehtmlref.=$formproject->select_projects(0, $object->fk_project, 'projectid', $maxlength, 0, 1, 0, 1, 0, 0, '', 1);
386  $morehtmlref.='<input type="submit" class="button valignmiddle" value="'.$langs->trans("Modify").'">';
387  $morehtmlref.='</form>';
388  } else {
389  $morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'none', 0, 0, 0, 1);
390  }
391  } else {
392  if (! empty($object->fk_project)) {
393  $proj = new Project($db);
394  $proj->fetch($object->fk_project);
395  $morehtmlref.='<a href="'.DOL_URL_ROOT.'/projet/card.php?id=' . $object->fk_project . '" title="' . $langs->trans('ShowProject') . '">';
396  $morehtmlref.=$proj->ref;
397  $morehtmlref.='</a>';
398  } else {
399  $morehtmlref.='';
400  }
401  }
402  }
403  $morehtmlref.='</div>';
404  $linkback = '<a href="'.DOL_URL_ROOT.'/compta/bank/various_payment/index.php'.(! empty($socid)?'?socid='.$socid:'').'">'.$langs->trans("BackToList").'</a>';
405 
406  dol_banner_tab($object, 'id', $linkback, 1, 'rowid', 'ref', $morehtmlref, '', 0, '', $morehtmlright);
407 
408  print '<div class="fichecenter">';
409  print '<div class="underbanner clearboth"></div>';
410 
411  print '<table class="border" width="100%">';
412 
413  // Label
414  print '<tr><td>'.$langs->trans("Label").'</td><td>'.$object->label.'</td></tr>';
415 
416  // Payment date
417  print "<tr>";
418  print '<td>'.$langs->trans("DatePayment").'</td><td>';
419  print dol_print_date($object->datep,'day');
420  print '</td></tr>';
421 
422  // Value date
423  print '<tr><td>'.$langs->trans("DateValue").'</td><td>';
424  print dol_print_date($object->datev,'day');
425  print '</td></tr>';
426 
427  // Debit / Credit
428  if ($object->sens == '1') $sens = $langs->trans("Credit"); else $sens = $langs->trans("Debit");
429  print '<tr><td>'.$langs->trans("Sens").'</td><td>'.$sens.'</td></tr>';
430 
431  print '<tr><td>'.$langs->trans("Amount").'</td><td>'.price($object->amount,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';
432 
433  // Accountancy code
434  print '<tr><td class="nowrap">';
435  print $langs->trans("AccountAccounting");
436  print '</td><td>';
437  if (! empty($conf->accounting->enabled))
438  {
439  $accountingaccount = new AccountingAccount($db);
440  $accountingaccount->fetch('',$object->accountancy_code, 1);
441 
442  print $accountingaccount->getNomUrl(0,1,1,'',1);
443  } else {
444  print $object->accountancy_code;
445  }
446  print '</td></tr>';
447 
448  if (! empty($conf->banque->enabled))
449  {
450  if ($object->fk_account > 0)
451  {
452  $bankline=new AccountLine($db);
453  $bankline->fetch($object->fk_bank);
454 
455  print '<tr>';
456  print '<td>'.$langs->trans('BankTransactionLine').'</td>';
457  print '<td colspan="3">';
458  print $bankline->getNomUrl(1,0,'showall');
459  print '</td>';
460  print '</tr>';
461  }
462  }
463 
464  // Other attributes
465  $parameters=array('socid'=>$object->id);
466  include DOL_DOCUMENT_ROOT . '/core/tpl/extrafields_view.tpl.php';
467 
468  print '</table>';
469 
470  print '</div>';
471  print '</div>';
472 
473  print '<div class="clearboth"></div>';
474 
475  dol_fiche_end();
476 
477 
478  /*
479  * Action buttons
480  */
481  print '<div class="tabsAction">'."\n";
482  if ($object->rappro == 0)
483  {
484  if (! empty($user->rights->banque->modifier))
485  {
486  print '<a class="butActionDelete" href="card.php?id='.$object->id.'&action=delete">'.$langs->trans("Delete").'</a>';
487  }
488  else
489  {
490  print '<a class="butActionRefused" href="#" title="'.(dol_escape_htmltag($langs->trans("NotAllowed"))).'">'.$langs->trans("Delete").'</a>';
491  }
492  }
493  else
494  {
495  print '<a class="butActionRefused" href="#" title="'.$langs->trans("LinkedToAConciliatedTransaction").'">'.$langs->trans("Delete").'</a>';
496  }
497  print "</div>";
498 }
499 
500 
501 
502 llxFooter();
503 
504 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:58
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
Class to manage various payments.
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...
dol_fiche_head($links=array(), $active='0', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='')
Show tab header of a card.
dol_banner_tab($object, $paramid, $morehtml='', $shownav=1, $fieldid='rowid', $fieldref='ref', $morehtmlref='', $moreparam='', $nodbprefix=0, $morehtmlleft='', $morehtmlstatus='', $onlybanner=0, $morehtmlright='')
Show tab footer of a card.
various_payment_prepare_head($object)
Prepare array with list of tabs.
Definition: bank.lib.php:164
Class to manage bank transaction lines.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
if(empty($reshook)) $form
View.
Definition: perms.php:103
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
GETPOST($paramname, $check='none', $method=0, $filter=NULL, $options=NULL, $noreplace=0)
Return value of a param into GET or POST supervariable.
Class to manage generation of HTML components Only common components must be here.
Class to manage projects.
Class to manage building of HTML components.
dol_fiche_end($notab=0)
Show tab footer of a card.
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
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages...
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
print
Draft customers invoices.
Definition: index.php:91
Class to manage generation of HTML components for accounting management.
Class to manage accounting accounts.
fieldLabel($langkey, $fieldkey, $fieldrequired=0)
Show a string with the label tag dedicated to the HTML edit field.
img_edit($titlealt= 'default', $float=0, $other= 'class="pictoedit"')
Show logo editer/modifier fiche.
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is '...
restrictedArea($user, $features, $objectid=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $objcanvas=null)
Check permissions of a user to show a page and an object.
$parameters
Actions.
Definition: card.php:112