dolibarr  16.0.5
paiement_charge.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2004-2014 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2016-2018 Frédéric France <frederic.france@netlogic.fr>
4  * Copyright (C) 2022 Alexandre Spangaro <aspangaro@open-dsi.fr>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
26 require '../main.inc.php';
27 require_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/chargesociales.class.php';
28 require_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/paymentsocialcontribution.class.php';
29 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
30 
31 // Load translation files required by the page
32 $langs->load("bills");
33 
34 $chid = GETPOST("id", 'int');
35 $action = GETPOST('action', 'aZ09');
36 $cancel = GETPOST('cancel');
37 
38 $amounts = array();
39 
40 // Security check
41 $socid = 0;
42 if ($user->socid > 0) {
43  $socid = $user->socid;
44 }
45 
46 $charge = new ChargeSociales($db);
47 
48 
49 /*
50  * Actions
51  */
52 
53 if ($action == 'add_payment' || ($action == 'confirm_paiement' && $confirm == 'yes')) {
54  $error = 0;
55 
56  if ($cancel) {
57  $loc = DOL_URL_ROOT.'/compta/sociales/card.php?id='.$chid;
58  header("Location: ".$loc);
59  exit;
60  }
61 
62  $datepaye = dol_mktime(12, 0, 0, GETPOST("remonth", "int"), GETPOST("reday", "int"), GETPOST("reyear", "int"));
63 
64  if (!(GETPOST("paiementtype") > 0)) {
65  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode")), null, 'errors');
66  $error++;
67  $action = 'create';
68  }
69  if ($datepaye == '') {
70  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Date")), null, 'errors');
71  $error++;
72  $action = 'create';
73  }
74  if (isModEnabled('banque') && !(GETPOST("accountid") > 0)) {
75  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountToCredit")), null, 'errors');
76  $error++;
77  $action = 'create';
78  }
79 
80  if (!$error) {
81  $paymentid = 0;
82 
83  // Read possible payments
84  foreach ($_POST as $key => $value) {
85  if (substr($key, 0, 7) == 'amount_') {
86  $other_chid = substr($key, 7);
87  $amounts[$other_chid] = price2num(GETPOST($key));
88  }
89  }
90 
91  if (count($amounts) <= 0) {
92  $error++;
93  setEventMessages($langs->trans("ErrorNoPaymentDefined"), null, 'errors');
94  $action = 'create';
95  }
96 
97  if (!$error) {
98  $db->begin();
99 
100  // Create a line of payments
101  $paiement = new PaymentSocialContribution($db);
102  $paiement->chid = $chid;
103  $paiement->datepaye = $datepaye;
104  $paiement->amounts = $amounts; // Amount list
105  $paiement->paiementtype = GETPOST("paiementtype", 'alphanohtml');
106  $paiement->num_payment = GETPOST("num_payment", 'alphanohtml');
107  $paiement->note = GETPOST("note", 'restricthtml');
108  $paiement->note_private = GETPOST("note", 'restricthtml');
109 
110  if (!$error) {
111  $paymentid = $paiement->create($user, (GETPOST('closepaidcontrib') == 'on' ? 1 : 0));
112  if ($paymentid < 0) {
113  $error++;
114  setEventMessages($paiement->error, null, 'errors');
115  $action = 'create';
116  }
117  }
118 
119  if (!$error) {
120  $result = $paiement->addPaymentToBank($user, 'payment_sc', '(SocialContributionPayment)', GETPOST('accountid', 'int'), '', '');
121  if (!($result > 0)) {
122  $error++;
123  setEventMessages($paiement->error, null, 'errors');
124  $action = 'create';
125  }
126  }
127 
128  if (!$error) {
129  $db->commit();
130  $loc = DOL_URL_ROOT.'/compta/sociales/card.php?id='.$chid;
131  header('Location: '.$loc);
132  exit;
133  } else {
134  $db->rollback();
135  }
136  }
137  }
138 }
139 
140 
141 /*
142  * View
143  */
144 
145 llxHeader();
146 
147 $form = new Form($db);
148 
149 
150 // Form of charge payment creation
151 if ($action == 'create') {
152  $charge->fetch($chid);
153  $charge->accountid = $charge->fk_account ? $charge->fk_account : $charge->accountid;
154  $charge->paiementtype = $charge->mode_reglement_id ? $charge->mode_reglement_id : $charge->paiementtype;
155 
156  $total = $charge->amount;
157  if (!empty($conf->use_javascript_ajax)) {
158  print "\n".'<script type="text/javascript">';
159 
160  //Add js for AutoFill
161  print ' $(document).ready(function () {';
162  print ' $(".AutoFillAmount").on(\'click touchstart\', function(){
163  var amount = $(this).data("value");
164  document.getElementById($(this).data(\'rowid\')).value = amount ;
165  });';
166  print ' });'."\n";
167 
168  print ' </script>'."\n";
169  }
170 
171  print load_fiche_titre($langs->trans("DoPayment"));
172  print "<br>\n";
173 
174  if ($mesg) {
175  print "<div class=\"error\">$mesg</div>";
176  }
177 
178  print '<form name="add_payment" action="'.$_SERVER['PHP_SELF'].'" method="post">';
179  print '<input type="hidden" name="token" value="'.newToken().'">';
180  print '<input type="hidden" name="id" value="'.$chid.'">';
181  print '<input type="hidden" name="chid" value="'.$chid.'">';
182  print '<input type="hidden" name="action" value="add_payment">';
183 
184  print dol_get_fiche_head('', '');
185 
186  print '<table class="border centpercent">';
187 
188  print '<tr><td class="titlefieldcreate">'.$langs->trans("Ref").'</td><td><a href="'.DOL_URL_ROOT.'/compta/sociales/card.php?id='.$chid.'">'.$chid.'</a></td></tr>';
189  print '<tr><td>'.$langs->trans("Label").'</td><td>'.$charge->label."</td></tr>\n";
190  print '<tr><td>'.$langs->trans("Type")."</td><td>".$charge->type_label."</td></tr>\n";
191  print '<tr><td>'.$langs->trans("Period")."</td><td>".dol_print_date($charge->periode, 'day')."</td></tr>\n";
192  /*print '<tr><td>'.$langs->trans("DateDue")."</td><td>".dol_print_date($charge->date_ech,'day')."</td></tr>\n";
193  print '<tr><td>'.$langs->trans("Amount")."</td><td>".price($charge->amount,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
194 
195  $sql = "SELECT sum(p.amount) as total";
196  $sql .= " FROM ".MAIN_DB_PREFIX."paiementcharge as p";
197  $sql .= " WHERE p.fk_charge = ".((int) $chid);
198  $resql = $db->query($sql);
199  if ($resql) {
200  $obj = $db->fetch_object($resql);
201  $sumpaid = $obj->total;
202  $db->free($resql);
203  }
204  /*print '<tr><td>'.$langs->trans("AlreadyPaid").'</td><td>'.price($sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';
205  print '<tr><td class="tdtop">'.$langs->trans("RemainderToPay").'</td><td>'.price($total-$sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
206 
207  print '<tr><td class="fieldrequired">'.$langs->trans("Date").'</td><td>';
208  $datepaye = dol_mktime(12, 0, 0, GETPOST("remonth", 'int'), GETPOST("reday", 'int'), GETPOST("reyear", 'int'));
209  $datepayment = empty($conf->global->MAIN_AUTOFILL_DATE) ? (GETPOSTISSET("remonth") ? $datepaye : -1) : '';
210  print $form->selectDate($datepayment, '', '', '', 0, "add_payment", 1, 1, 0, '', '', $charge->date_ech, '', 1, $langs->trans("DateOfSocialContribution"));
211  print "</td>";
212  print '</tr>';
213 
214  print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td>';
215  $form->select_types_paiements(GETPOSTISSET("paiementtype") ? GETPOST("paiementtype") : $charge->paiementtype, "paiementtype");
216  print "</td>\n";
217  print '</tr>';
218 
219  print '<tr>';
220  print '<td class="fieldrequired">'.$langs->trans('AccountToDebit').'</td>';
221  print '<td>';
222  print img_picto('', 'bank_account', 'class="pictofixedwidth"');
223  $form->select_comptes(GETPOSTISSET("accountid") ? GETPOST("accountid", 'int') : $charge->accountid, "accountid", 0, '', 2); // Show opend bank account list
224  print '</td></tr>';
225 
226  // Number
227  print '<tr><td>'.$langs->trans('Numero');
228  print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
229  print '</td>';
230  print '<td><input name="num_payment" type="text" value="'.GETPOST('num_payment', 'alphanohtml').'"></td></tr>'."\n";
231 
232  print '<tr>';
233  print '<td class="tdtop">'.$langs->trans("Comments").'</td>';
234  print '<td class="tdtop"><textarea name="note" wrap="soft" cols="60" rows="'.ROWS_3.'">'.GETPOST('note', 'alphanohtml').'</textarea></td>';
235  print '</tr>';
236 
237  print '</table>';
238 
239  print dol_get_fiche_end();
240 
241  /*
242  * Other unpaid charges
243  */
244  $num = 1;
245  $i = 0;
246 
247  print '<table class="noborder centpercent">';
248  print '<tr class="liste_titre">';
249  //print '<td>'.$langs->trans("SocialContribution").'</td>';
250  print '<td class="left">'.$langs->trans("DateDue").'</td>';
251  print '<td class="right">'.$langs->trans("Amount").'</td>';
252  print '<td class="right">'.$langs->trans("AlreadyPaid").'</td>';
253  print '<td class="right">'.$langs->trans("RemainderToPay").'</td>';
254  print '<td class="center">'.$langs->trans("Amount").'</td>';
255  print "</tr>\n";
256 
257  $total = 0;
258  $total_ttc = 0;
259  $totalrecu = 0;
260 
261  while ($i < $num) {
262  $objp = $charge;
263 
264  print '<tr class="oddeven">';
265 
266  if ($objp->date_ech > 0) {
267  print '<td class="left">'.dol_print_date($objp->date_ech, 'day').'</td>'."\n";
268  } else {
269  print "<td align=\"center\"><b>!!!</b></td>\n";
270  }
271 
272  print '<td class="right"><span class="amount">'.price($objp->amount)."</span></td>";
273 
274  print '<td class="right"><span class="amount">'.price($sumpaid)."</span></td>";
275 
276  print '<td class="right"><span class="amount">'.price($objp->amount - $sumpaid)."</span></td>";
277 
278  print '<td class="center">';
279  if ($sumpaid < $objp->amount) {
280  $namef = "amount_".$objp->id;
281  $nameRemain = "remain_".$objp->id;
282  if (!empty($conf->use_javascript_ajax)) {
283  print img_picto("Auto fill", 'rightarrow', "class='AutoFillAmount' data-rowid='".$namef."' data-value='".($objp->amount - $sumpaid)."'");
284  }
285  $remaintopay = $objp->amount - $sumpaid;
286  print '<input type=hidden class="sum_remain" name="'.$nameRemain.'" value="'.$remaintopay.'">';
287  print '<input type="text" size="8" name="'.$namef.'" id="'.$namef.'" value="'.GETPOST('amount_'.$objp->id, 'alpha').'">';
288  } else {
289  print '-';
290  }
291  print "</td>";
292 
293  print "</tr>\n";
294  $total += $objp->total;
295  $total_ttc += $objp->total_ttc;
296  $totalrecu += $objp->amount;
297  $i++;
298  }
299  if ($i > 1) {
300  // Print total
301  print '<tr class="oddeven">';
302  print '<td colspan="2" class="left">'.$langs->trans("Total").':</td>';
303  print '<td class="right"><b>'.price($total_ttc).'</b></td>';
304  print '<td class="right"><b>'.price($totalrecu).'</b></td>';
305  print '<td class="right"><b>'.price($total_ttc - $totalrecu).'</b></td>';
306  print '<td align="center">&nbsp;</td>';
307  print "</tr>\n";
308  }
309 
310  print "</table>";
311 
312  // Save payment button
313  print '<br><div class="center"><input type="checkbox" checked name="closepaidcontrib"> '.$langs->trans("ClosePaidContributionsAutomatically");
314  print '<br><input type="submit" class="button" name="save" value="'.$langs->trans('ToMakePayment').'">';
315  print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
316  print '<input type="submit" class="button button-cancel" name="cancel" value="'.$langs->trans("Cancel").'">';
317  print '</div>';
318 
319  print "</form>\n";
320 }
321 
322 llxFooter();
323 $db->close();
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:73
ChargeSociales
Classe permettant la gestion des paiements des charges La tva collectee n'est calculee que sur les fa...
Definition: chargesociales.class.php:34
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
PaymentSocialContribution
Class to manage payments of social contributions.
Definition: paymentsocialcontribution.class.php:33
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5661
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2514
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
dol_get_fiche_head
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='')
Show tabs of a record.
Definition: functions.lib.php:1822
dol_get_fiche_end
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition: functions.lib.php:2018
isModEnabled
isModEnabled($module)
Is Dolibarr module enabled.
Definition: functions.lib.php:105
GETPOSTISSET
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
Definition: functions.lib.php:386
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
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