dolibarr  9.0.0
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 <http://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', 'alpha');
35 $amounts = array();
36 
37 // Security check
38 $socid=0;
39 if ($user->societe_id > 0)
40 {
41  $socid = $user->societe_id;
42 }
43 
44 
45 /*
46  * Actions
47  */
48 
49 if ($action == 'add_payment' || ($action == 'confirm_paiement' && $confirm=='yes'))
50 {
51  $error=0;
52 
53  if ($_POST["cancel"])
54  {
55  $loc = DOL_URL_ROOT.'/compta/sociales/card.php?id='.$chid;
56  header("Location: ".$loc);
57  exit;
58  }
59 
60  $datepaye = dol_mktime(12, 0, 0, $_POST["remonth"], $_POST["reday"], $_POST["reyear"]);
61 
62  if (! $_POST["paiementtype"] > 0)
63  {
64  setEventMessages($langs->trans("ErrorFieldRequired",$langs->transnoentities("PaymentMode")), null, 'errors');
65  $error++;
66  $action = 'create';
67  }
68  if ($datepaye == '')
69  {
70  setEventMessages($langs->trans("ErrorFieldRequired",$langs->transnoentities("Date")), null, 'errors');
71  $error++;
72  $action = 'create';
73  }
74  if (! empty($conf->banque->enabled) && ! $_POST["accountid"] > 0)
75  {
76  setEventMessages($langs->trans("ErrorFieldRequired",$langs->transnoentities("AccountToCredit")), null, 'errors');
77  $error++;
78  $action = 'create';
79  }
80 
81  if (! $error)
82  {
83  $paymentid = 0;
84 
85  // Read possible payments
86  foreach ($_POST as $key => $value)
87  {
88  if (substr($key,0,7) == 'amount_')
89  {
90  $other_chid = substr($key,7);
91  $amounts[$other_chid] = price2num($_POST[$key]);
92  }
93  }
94 
95  if (count($amounts) <= 0)
96  {
97  $error++;
98  setEventMessages($langs->trans("ErrorNoPaymentDefined"), null, 'errors');
99  $action='create';
100  }
101 
102  if (! $error)
103  {
104  $db->begin();
105 
106  // Create a line of payments
107  $paiement = new PaymentSocialContribution($db);
108  $paiement->chid = $chid;
109  $paiement->datepaye = $datepaye;
110  $paiement->amounts = $amounts; // Tableau de montant
111  $paiement->paiementtype = $_POST["paiementtype"];
112  $paiement->num_paiement = $_POST["num_paiement"];
113  $paiement->note = $_POST["note"];
114 
115  if (! $error)
116  {
117  $paymentid = $paiement->create($user, (GETPOST('closepaidcontrib')=='on'?1:0));
118  if ($paymentid < 0)
119  {
120  $error++;
121  setEventMessages($paiement->error, null, 'errors');
122  $action='create';
123  }
124  }
125 
126  if (! $error)
127  {
128  $result=$paiement->addPaymentToBank($user,'payment_sc','(SocialContributionPayment)', GETPOST('accountid','int'),'','');
129  if (! ($result > 0))
130  {
131  $error++;
132  setEventMessages($paiement->error, null, 'errors');
133  $action='create';
134  }
135  }
136 
137  if (! $error)
138  {
139  $db->commit();
140  $loc = DOL_URL_ROOT.'/compta/sociales/card.php?id='.$chid;
141  header('Location: '.$loc);
142  exit;
143  }
144  else
145  {
146  $db->rollback();
147  }
148  }
149  }
150 }
151 
152 
153 /*
154  * View
155  */
156 
157 llxHeader();
158 
159 $form=new Form($db);
160 
161 
162 // Formulaire de creation d'un paiement de charge
163 if ($action == 'create')
164 {
165 
166  $charge = new ChargeSociales($db);
167  $charge->fetch($chid);
168  $charge->accountid=$charge->fk_account?$charge->fk_account:$charge->accountid;
169  $charge->paiementtype=$charge->mode_reglement_id?$charge->mode_reglement_id:$charge->paiementtype;
170 
171  $total = $charge->amount;
172  if (! empty($conf->use_javascript_ajax))
173  {
174  print "\n".'<script type="text/javascript" language="javascript">';
175 
176  //Add js for AutoFill
177  print ' $(document).ready(function () {';
178  print ' $(".AutoFillAmount").on(\'click touchstart\', function(){
179  var amount = $(this).data("value");
180  document.getElementById($(this).data(\'rowid\')).value = amount ;
181  });';
182  print ' });'."\n";
183 
184  print ' </script>'."\n";
185  }
186 
187  print load_fiche_titre($langs->trans("DoPayment"));
188  print "<br>\n";
189 
190  if ($mesg)
191  {
192  print "<div class=\"error\">$mesg</div>";
193  }
194 
195  print '<form name="add_payment" action="'.$_SERVER['PHP_SELF'].'" method="post">';
196  print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
197  print '<input type="hidden" name="id" value="'.$chid.'">';
198  print '<input type="hidden" name="chid" value="'.$chid.'">';
199  print '<input type="hidden" name="action" value="add_payment">';
200 
201  dol_fiche_head('', '');
202 
203  print '<table class="border" width="100%">';
204 
205  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>';
206  print '<tr><td>'.$langs->trans("Type")."</td><td>".$charge->type_libelle."</td></tr>\n";
207  print '<tr><td>'.$langs->trans("Period")."</td><td>".dol_print_date($charge->periode,'day')."</td></tr>\n";
208  print '<tr><td>'.$langs->trans("Label").'</td><td>'.$charge->lib."</td></tr>\n";
209  /*print '<tr><td>'.$langs->trans("DateDue")."</td><td>".dol_print_date($charge->date_ech,'day')."</td></tr>\n";
210  print '<tr><td>'.$langs->trans("Amount")."</td><td>".price($charge->amount,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
211 
212  $sql = "SELECT sum(p.amount) as total";
213  $sql.= " FROM ".MAIN_DB_PREFIX."paiementcharge as p";
214  $sql.= " WHERE p.fk_charge = ".$chid;
215  $resql = $db->query($sql);
216  if ($resql)
217  {
218  $obj=$db->fetch_object($resql);
219  $sumpaid = $obj->total;
220  $db->free();
221  }
222  /*print '<tr><td>'.$langs->trans("AlreadyPaid").'</td><td>'.price($sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';
223  print '<tr><td class="tdtop">'.$langs->trans("RemainderToPay").'</td><td>'.price($total-$sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
224 
225  print '<tr><td class="fieldrequired">'.$langs->trans("Date").'</td><td>';
226  $datepaye = dol_mktime(12, 0, 0, $_POST["remonth"], $_POST["reday"], $_POST["reyear"]);
227  $datepayment=empty($conf->global->MAIN_AUTOFILL_DATE)?(empty($_POST["remonth"])?-1:$datepaye):0;
228  print $form->selectDate($datepayment, '', '', '', '', "add_payment", 1, 1);
229  print "</td>";
230  print '</tr>';
231 
232  print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td>';
233  $form->select_types_paiements(isset($_POST["paiementtype"])?$_POST["paiementtype"]:$charge->paiementtype, "paiementtype");
234  print "</td>\n";
235  print '</tr>';
236 
237  print '<tr>';
238  print '<td class="fieldrequired">'.$langs->trans('AccountToDebit').'</td>';
239  print '<td>';
240  $form->select_comptes(isset($_POST["accountid"])?$_POST["accountid"]:$charge->accountid, "accountid", 0, '',1); // Show opend bank account list
241  print '</td></tr>';
242 
243  // Number
244  print '<tr><td>'.$langs->trans('Numero');
245  print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
246  print '</td>';
247  print '<td><input name="num_paiement" type="text" value="'.GETPOST('num_paiement').'"></td></tr>'."\n";
248 
249  print '<tr>';
250  print '<td class="tdtop">'.$langs->trans("Comments").'</td>';
251  print '<td class="tdtop"><textarea name="note" wrap="soft" cols="60" rows="'.ROWS_3.'"></textarea></td>';
252  print '</tr>';
253 
254  print '</table>';
255 
256  dol_fiche_end();
257 
258  /*
259  * Autres charges impayees
260  */
261  $num = 1;
262  $i = 0;
263 
264  print '<table class="noborder" width="100%">';
265  print '<tr class="liste_titre">';
266  //print '<td>'.$langs->trans("SocialContribution").'</td>';
267  print '<td align="left">'.$langs->trans("DateDue").'</td>';
268  print '<td align="right">'.$langs->trans("Amount").'</td>';
269  print '<td align="right">'.$langs->trans("AlreadyPaid").'</td>';
270  print '<td align="right">'.$langs->trans("RemainderToPay").'</td>';
271  print '<td align="center">'.$langs->trans("Amount").'</td>';
272  print "</tr>\n";
273 
274  $total=0;
275  $totalrecu=0;
276 
277  while ($i < $num)
278  {
279  $objp = $charge;
280 
281  print '<tr class="oddeven">';
282 
283  if ($objp->date_ech > 0)
284  {
285  print "<td align=\"left\">".dol_print_date($objp->date_ech,'day')."</td>\n";
286  }
287  else
288  {
289  print "<td align=\"center\"><b>!!!</b></td>\n";
290  }
291 
292  print '<td align="right">'.price($objp->amount)."</td>";
293 
294  print '<td align="right">'.price($sumpaid)."</td>";
295 
296  print '<td align="right">'.price($objp->amount - $sumpaid)."</td>";
297 
298  print '<td align="center">';
299  if ($sumpaid < $objp->amount)
300  {
301  $namef = "amount_".$objp->id;
302  $nameRemain = "remain_".$objp->id;
303  if (!empty($conf->use_javascript_ajax))
304  print img_picto("Auto fill",'rightarrow', "class='AutoFillAmount' data-rowid='".$namef."' data-value='".($objp->amount - $sumpaid)."'");
305  $remaintopay=$objp->amount - $sumpaid;
306  print '<input type=hidden class="sum_remain" name="'.$nameRemain.'" value="'.$remaintopay.'">';
307  print '<input type="text" size="8" name="'.$namef.'" id="'.$namef.'">';
308  }
309  else
310  {
311  print '-';
312  }
313  print "</td>";
314 
315  print "</tr>\n";
316  $total+=$objp->total;
317  $total_ttc+=$objp->total_ttc;
318  $totalrecu+=$objp->am;
319  $i++;
320  }
321  if ($i > 1)
322  {
323  // Print total
324  print '<tr class="oddeven">';
325  print '<td colspan="2" align="left">'.$langs->trans("Total").':</td>';
326  print "<td align=\"right\"><b>".price($total_ttc)."</b></td>";
327  print "<td align=\"right\"><b>".price($totalrecu)."</b></td>";
328  print "<td align=\"right\"><b>".price($total_ttc - $totalrecu)."</b></td>";
329  print '<td align="center">&nbsp;</td>';
330  print "</tr>\n";
331  }
332 
333  print "</table>";
334 
335  // Bouton Save payment
336  print '<br><div class="center"><input type="checkbox" checked name="closepaidcontrib"> '.$langs->trans("ClosePaidContributionsAutomatically");
337  print '<br><input type="submit" class="button" name="save" value="'.$langs->trans('ToMakePayment').'">';
338  print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
339  print '<input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'">';
340  print '</div>';
341 
342  print "</form>\n";
343 }
344 
345 llxFooter();
346 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:56
load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
print
Draft customers invoices.
Definition: index.php:91
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->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) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
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...
Class to manage payments of social contributions.
Class to manage generation of HTML components Only common components must be here.
dol_fiche_end($notab=0)
Show tab footer of a card.
llxHeader()
Empty header.
Definition: wrapper.php:44
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it&#39;s its name (generic function)
Classe permettant la gestion des paiements des charges La tva collectee n&#39;est calculee que sur les fa...
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
dol_fiche_head($links=array(), $active='0', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='')
Show tab header of a card.