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