dolibarr  20.0.0-alpha
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-2024 Frédéric France <frederic.france@free.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 // Load Dolibarr environment
27 require '../main.inc.php';
28 require_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/chargesociales.class.php';
29 require_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/paymentsocialcontribution.class.php';
30 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
31 
32 // Load translation files required by the page
33 $langs->loadLangs(array("banks", "bills", "compta"));
34 
35 $chid = GETPOSTINT("id");
36 $action = GETPOST('action', 'aZ09');
37 $cancel = GETPOST('cancel');
38 
39 $amounts = array();
40 
41 // Security check
42 $socid = 0;
43 if ($user->socid > 0) {
44  $socid = $user->socid;
45 }
46 
47 $charge = new ChargeSociales($db);
48 
49 
50 /*
51  * Actions
52  */
53 
54 if ($action == 'add_payment' || ($action == 'confirm_paiement' && $confirm == 'yes')) {
55  $error = 0;
56 
57  if ($cancel) {
58  $loc = DOL_URL_ROOT.'/compta/sociales/card.php?id='.$chid;
59  header("Location: ".$loc);
60  exit;
61  }
62 
63  $datepaye = dol_mktime(12, 0, 0, GETPOSTINT("remonth"), GETPOSTINT("reday"), GETPOSTINT("reyear"));
64 
65  if (!(GETPOST("paiementtype") > 0)) {
66  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode")), null, 'errors');
67  $error++;
68  $action = 'create';
69  }
70  if ($datepaye == '') {
71  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Date")), null, 'errors');
72  $error++;
73  $action = 'create';
74  }
75  if (isModEnabled("bank") && !(GETPOST("accountid") > 0)) {
76  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountToCredit")), null, 'errors');
77  $error++;
78  $action = 'create';
79  }
80 
81  if (!$error) {
82  $paymentid = 0;
83 
84  // Read possible payments
85  foreach ($_POST as $key => $value) {
86  if (substr($key, 0, 7) == 'amount_') {
87  $other_chid = substr($key, 7);
88  $amounts[$other_chid] = (float) price2num(GETPOST($key));
89  }
90  }
91 
92  if (count($amounts) <= 0) {
93  $error++;
94  setEventMessages($langs->trans("ErrorNoPaymentDefined"), null, 'errors');
95  $action = 'create';
96  }
97 
98  if (!$error) {
99  $db->begin();
100 
101  // Create a line of payments
102  $paiement = new PaymentSocialContribution($db);
103  $paiement->chid = $chid;
104  $paiement->datepaye = $datepaye;
105  $paiement->amounts = $amounts; // Amount list
106  $paiement->paiementtype = GETPOST("paiementtype", 'alphanohtml');
107  $paiement->num_payment = GETPOST("num_payment", 'alphanohtml');
108  $paiement->note = GETPOST("note", 'restricthtml');
109  $paiement->note_private = GETPOST("note", 'restricthtml');
110 
111  if (!$error) {
112  $paymentid = $paiement->create($user, (GETPOST('closepaidcontrib') == 'on' ? 1 : 0));
113  if ($paymentid < 0) {
114  $error++;
115  setEventMessages($paiement->error, null, 'errors');
116  $action = 'create';
117  }
118  }
119 
120  if (!$error) {
121  $result = $paiement->addPaymentToBank($user, 'payment_sc', '(SocialContributionPayment)', GETPOSTINT('accountid'), '', '');
122  if (!($result > 0)) {
123  $error++;
124  setEventMessages($paiement->error, null, 'errors');
125  $action = 'create';
126  }
127  }
128 
129  if (!$error) {
130  $db->commit();
131  $loc = DOL_URL_ROOT.'/compta/sociales/card.php?id='.$chid;
132  header('Location: '.$loc);
133  exit;
134  } else {
135  $db->rollback();
136  }
137  }
138  }
139 }
140 
141 
142 /*
143  * View
144  */
145 
146 llxHeader();
147 
148 $form = new Form($db);
149 
150 
151 // Form of charge payment creation
152 if ($action == 'create') {
153  $charge->fetch($chid);
154  $charge->accountid = $charge->fk_account ? $charge->fk_account : $charge->accountid;
155  $charge->paiementtype = $charge->mode_reglement_id ? $charge->mode_reglement_id : $charge->paiementtype;
156 
157  $total = $charge->amount;
158  if (!empty($conf->use_javascript_ajax)) {
159  print "\n".'<script type="text/javascript">';
160 
161  //Add js for AutoFill
162  print ' $(document).ready(function () {';
163  print ' $(".AutoFillAmount").on(\'click touchstart\', function() {
164  console.log("Click on .AutoFillAmount");
165  var amount = $(this).data("value");
166  document.getElementById($(this).data(\'rowid\')).value = amount ;
167  });';
168  print ' });'."\n";
169 
170  print ' </script>'."\n";
171  }
172 
173  print load_fiche_titre($langs->trans("DoPayment"));
174 
175  print '<form name="add_payment" action="'.$_SERVER['PHP_SELF'].'" method="POST">';
176  print '<input type="hidden" name="token" value="'.newToken().'">';
177  print '<input type="hidden" name="id" value="'.$chid.'">';
178  print '<input type="hidden" name="chid" value="'.$chid.'">';
179  print '<input type="hidden" name="action" value="add_payment">';
180 
181  print dol_get_fiche_head('', '');
182 
183  print '<table class="border centpercent">';
184 
185  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>';
186  print '<tr><td>'.$langs->trans("Label").'</td><td>'.$charge->label."</td></tr>\n";
187  print '<tr><td>'.$langs->trans("Type")."</td><td>".$charge->type_label."</td></tr>\n";
188  print '<tr><td>'.$langs->trans("Period")."</td><td>".dol_print_date($charge->periode, 'day')."</td></tr>\n";
189  /*print '<tr><td>'.$langs->trans("DateDue")."</td><td>".dol_print_date($charge->date_ech,'day')."</td></tr>\n";
190  print '<tr><td>'.$langs->trans("Amount")."</td><td>".price($charge->amount,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
191 
192  $sql = "SELECT sum(p.amount) as total";
193  $sql .= " FROM ".MAIN_DB_PREFIX."paiementcharge as p";
194  $sql .= " WHERE p.fk_charge = ".((int) $chid);
195  $resql = $db->query($sql);
196  if ($resql) {
197  $obj = $db->fetch_object($resql);
198  $sumpaid = $obj->total;
199  $db->free($resql);
200  }
201  /*print '<tr><td>'.$langs->trans("AlreadyPaid").'</td><td>'.price($sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';
202  print '<tr><td class="tdtop">'.$langs->trans("RemainderToPay").'</td><td>'.price($total-$sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
203 
204  print '<tr><td class="fieldrequired">'.$langs->trans("Date").'</td><td>';
205  $datepaye = dol_mktime(12, 0, 0, GETPOSTINT("remonth"), GETPOSTINT("reday"), GETPOSTINT("reyear"));
206  $datepayment = !getDolGlobalString('MAIN_AUTOFILL_DATE') ? (GETPOSTISSET("remonth") ? $datepaye : -1) : '';
207  print $form->selectDate($datepayment, '', 0, 0, 0, "add_payment", 1, 1, 0, '', '', $charge->date_ech, '', 1, $langs->trans("DateOfSocialContribution"));
208  print "</td>";
209  print '</tr>';
210 
211  print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td>';
212  print img_picto('', 'bank', 'class="pictofixedwidth"');
213  print $form->select_types_paiements(GETPOSTISSET("paiementtype") ? GETPOST("paiementtype") : $charge->paiementtype, "paiementtype", '', 0, 1, 0, 0, 1, 'maxwidth500 widthcentpercentminusxx', 1);
214  print "</td>\n";
215  print '</tr>';
216 
217  print '<tr>';
218  print '<td class="fieldrequired">'.$langs->trans('AccountToDebit').'</td>';
219  print '<td>';
220  print img_picto('', 'bank_account', 'class="pictofixedwidth"');
221  print $form->select_comptes(GETPOSTISSET("accountid") ? GETPOSTINT("accountid") : $charge->accountid, "accountid", 0, '', 2, '', 0, 'maxwidth500 widthcentpercentminusx', 1); // Show opend bank account list
222  print '</td></tr>';
223 
224  // Number
225  print '<tr><td>'.$langs->trans('Numero');
226  if (empty($conf->dol_optimize_smallscreen)) {
227  print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
228  }
229  print '</td>';
230  print '<td><input name="num_payment" class="width100" 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 class="quatrevingtpercent" name="note" wrap="soft" 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 '<div class="div-table-responsive-no-min">'; // You can use div-table-responsive-no-min if you don't need reserved height for your table
248  print '<table class="noborder centpercent">';
249  print '<tr class="liste_titre">';
250  //print '<td>'.$langs->trans("SocialContribution").'</td>';
251  print '<td class="left">'.$langs->trans("DateDue").'</td>';
252  print '<td class="right">'.$langs->trans("Amount").'</td>';
253  print '<td class="right">'.$langs->trans("AlreadyPaid").'</td>';
254  print '<td class="right">'.$langs->trans("RemainderToPay").'</td>';
255  print '<td class="center">'.$langs->trans("Amount").'</td>';
256  print "</tr>\n";
257 
258  $total = 0;
259  $total_ttc = 0;
260  $totalrecu = 0;
261 
262  while ($i < $num) {
263  $objp = $charge;
264 
265  print '<tr class="oddeven">';
266 
267  if ($objp->date_ech > 0) {
268  print '<td class="left">'.dol_print_date($objp->date_ech, 'day').'</td>'."\n";
269  } else {
270  print "<td align=\"center\"><b>!!!</b></td>\n";
271  }
272 
273  print '<td class="right nowraponall"><span class="amount">'.price($objp->amount)."</span></td>";
274 
275  print '<td class="right nowraponall"><span class="amount">'.price($sumpaid)."</span></td>";
276 
277  print '<td class="right nowraponall"><span class="amount">'.price($objp->amount - $sumpaid)."</span></td>";
278 
279  print '<td class="center nowraponall">';
280  if ($sumpaid < $objp->amount) {
281  $namef = "amount_".$objp->id;
282  $nameRemain = "remain_".$objp->id;
283  if (!empty($conf->use_javascript_ajax)) {
284  print img_picto("Auto fill", 'rightarrow', "class='AutoFillAmount' data-rowid='".$namef."' data-value='".($objp->amount - $sumpaid)."'");
285  }
286  $remaintopay = $objp->amount - $sumpaid;
287  print '<input type=hidden class="sum_remain" name="'.$nameRemain.'" value="'.$remaintopay.'">';
288  print '<input type="text" size="8" name="'.$namef.'" id="'.$namef.'" value="'.GETPOST('amount_'.$objp->id, 'alpha').'">';
289  } else {
290  print '-';
291  }
292  print "</td>";
293 
294  print "</tr>\n";
295  $total += $objp->total;
296  $total_ttc += $objp->total_ttc;
297  $totalrecu += $objp->amount;
298  $i++;
299  }
300  if ($i > 1) {
301  // Print total
302  print '<tr class="oddeven">';
303  print '<td colspan="2" class="left">'.$langs->trans("Total").':</td>';
304  print '<td class="right"><b>'.price($total_ttc).'</b></td>';
305  print '<td class="right"><b>'.price($totalrecu).'</b></td>';
306  print '<td class="right"><b>'.price($total_ttc - $totalrecu).'</b></td>';
307  print '<td align="center">&nbsp;</td>';
308  print "</tr>\n";
309  }
310 
311  print "</table>";
312  print '</div>';
313 
314  // Save payment button
315  print '<br><div class="center"><input type="checkbox" checked name="closepaidcontrib"> '.$langs->trans("ClosePaidContributionsAutomatically");
316  print '<br><input type="submit" class="button" name="save" value="'.$langs->trans('ToMakePayment').'">';
317  print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
318  print '<input type="submit" class="button button-cancel" name="cancel" value="'.$langs->trans("Cancel").'">';
319  print '</div>';
320 
321  print "</form>\n";
322 }
323 
324 llxFooter();
325 $db->close();
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 for managing the social charges.
Class to manage generation of HTML components Only common components must be here.
Class to manage payments of social contributions.
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:744
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 information (by default a local PHP server timestamp) Rep...
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0)
Show tabs of a record.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
td amount
Definition: global.inc.php:554
div float
Buy price without taxes.
Definition: style.css.php:959