dolibarr  7.0.0-beta
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 Frédéric France <frederic.france@free.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 $langs->load("bills");
31 
32 $chid=GETPOST("id", 'int');
33 $action=GETPOST('action', 'alpha');
34 $amounts = array();
35 
36 // Security check
37 $socid=0;
38 if ($user->societe_id > 0)
39 {
40  $socid = $user->societe_id;
41 }
42 
43 
44 /*
45  * Actions
46  */
47 
48 if ($action == 'add_payment' || ($action == 'confirm_paiement' && $confirm=='yes'))
49 {
50  $error=0;
51 
52  if ($_POST["cancel"])
53  {
54  $loc = DOL_URL_ROOT.'/compta/sociales/card.php?id='.$chid;
55  header("Location: ".$loc);
56  exit;
57  }
58 
59  $datepaye = dol_mktime(12, 0, 0, $_POST["remonth"], $_POST["reday"], $_POST["reyear"]);
60 
61  if (! $_POST["paiementtype"] > 0)
62  {
63  setEventMessages($langs->trans("ErrorFieldRequired",$langs->transnoentities("PaymentMode")), null, 'errors');
64  $error++;
65  $action = 'create';
66  }
67  if ($datepaye == '')
68  {
69  setEventMessages($langs->trans("ErrorFieldRequired",$langs->transnoentities("Date")), null, 'errors');
70  $error++;
71  $action = 'create';
72  }
73  if (! empty($conf->banque->enabled) && ! $_POST["accountid"] > 0)
74  {
75  setEventMessages($langs->trans("ErrorFieldRequired",$langs->transnoentities("AccountToCredit")), null, 'errors');
76  $error++;
77  $action = 'create';
78  }
79 
80  if (! $error)
81  {
82  $paymentid = 0;
83 
84  // Read possible payments
85  foreach ($_POST as $key => $value)
86  {
87  if (substr($key,0,7) == 'amount_')
88  {
89  $other_chid = substr($key,7);
90  $amounts[$other_chid] = price2num($_POST[$key]);
91  }
92  }
93 
94  if (count($amounts) <= 0)
95  {
96  $error++;
97  setEventMessages($langs->trans("ErrorNoPaymentDefined"), null, 'errors');
98  $action='create';
99  }
100 
101  if (! $error)
102  {
103  $db->begin();
104 
105  // Create a line of payments
106  $paiement = new PaymentSocialContribution($db);
107  $paiement->chid = $chid;
108  $paiement->datepaye = $datepaye;
109  $paiement->amounts = $amounts; // Tableau de montant
110  $paiement->paiementtype = $_POST["paiementtype"];
111  $paiement->num_paiement = $_POST["num_paiement"];
112  $paiement->note = $_POST["note"];
113 
114  if (! $error)
115  {
116  $paymentid = $paiement->create($user, (GETPOST('closepaidcontrib')=='on'?1:0));
117  if ($paymentid < 0)
118  {
119  $error++;
120  setEventMessages($paiement->error, null, 'errors');
121  $action='create';
122  }
123  }
124 
125  if (! $error)
126  {
127  $result=$paiement->addPaymentToBank($user,'payment_sc','(SocialContributionPayment)', GETPOST('accountid','int'),'','');
128  if (! ($result > 0))
129  {
130  $error++;
131  setEventMessages($paiement->error, null, 'errors');
132  $action='create';
133  }
134  }
135 
136  if (! $error)
137  {
138  $db->commit();
139  $loc = DOL_URL_ROOT.'/compta/sociales/card.php?id='.$chid;
140  header('Location: '.$loc);
141  exit;
142  }
143  else
144  {
145  $db->rollback();
146  }
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  $form->select_date($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  $var=true;
275  $total=0;
276  $totalrecu=0;
277 
278  while ($i < $num)
279  {
280  $objp = $charge;
281 
282 
283 
284  print '<tr class="oddeven">';
285 
286  if ($objp->date_ech > 0)
287  {
288  print "<td align=\"left\">".dol_print_date($objp->date_ech,'day')."</td>\n";
289  }
290  else
291  {
292  print "<td align=\"center\"><b>!!!</b></td>\n";
293  }
294 
295  print '<td align="right">'.price($objp->amount)."</td>";
296 
297  print '<td align="right">'.price($sumpaid)."</td>";
298 
299  print '<td align="right">'.price($objp->amount - $sumpaid)."</td>";
300 
301  print '<td align="center">';
302  if ($sumpaid < $objp->amount)
303  {
304  $namef = "amount_".$objp->id;
305  $nameRemain = "remain_".$objp->id;
306  if (!empty($conf->use_javascript_ajax))
307  print img_picto("Auto fill",'rightarrow', "class='AutoFillAmount' data-rowid='".$namef."' data-value='".($objp->amount - $sumpaid)."'");
308  $remaintopay=$objp->amount - $sumpaid;
309  print '<input type=hidden class="sum_remain" name="'.$nameRemain.'" value="'.$remaintopay.'">';
310  print '<input type="text" size="8" name="'.$namef.'" id="'.$namef.'">';
311  }
312  else
313  {
314  print '-';
315  }
316  print "</td>";
317 
318  print "</tr>\n";
319  $total+=$objp->total;
320  $total_ttc+=$objp->total_ttc;
321  $totalrecu+=$objp->am;
322  $i++;
323  }
324  if ($i > 1)
325  {
326  // Print total
327  print "<tr ".$bc[!$var].">";
328  print '<td colspan="2" align="left">'.$langs->trans("Total").':</td>';
329  print "<td align=\"right\"><b>".price($total_ttc)."</b></td>";
330  print "<td align=\"right\"><b>".price($totalrecu)."</b></td>";
331  print "<td align=\"right\"><b>".price($total_ttc - $totalrecu)."</b></td>";
332  print '<td align="center">&nbsp;</td>';
333  print "</tr>\n";
334  }
335 
336  print "</table>";
337 
338  // Bouton Save payment
339  print '<br><div class="center"><input type="checkbox" checked name="closepaidcontrib"> '.$langs->trans("ClosePaidContributionsAutomatically");
340  print '<br><input type="submit" class="button" name="save" value="'.$langs->trans('ToMakePayment').'">';
341  print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
342  print '<input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'">';
343  print '</div>';
344 
345  print "</form>\n";
346 }
347 
348 llxFooter();
349 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:58
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it's its name (generic function)
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
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.
Class to manage payments of social contributions.
if(empty($reshook)) $form
View.
Definition: perms.php:103
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.
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
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
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:1013
Classe permettant la gestion des paiements des charges La tva collectee n'est calculee que sur les fa...
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is '...