PageRenderTime 4ms CodeModel.GetById 6ms app.highlight 43ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/facture/prelevement.php

https://bitbucket.org/speedealing/speedealing
PHP | 577 lines | 422 code | 77 blank | 78 comment | 111 complexity | 96000cc86ccb0e364f7e28f98f92dc7b MD5 | raw file
  1<?php
  2/* Copyright (C) 2002-2005	Rodolphe Quiedeville	<rodolphe@quiedeville.org>
  3 * Copyright (C) 2004		Eric Seigne				<eric.seigne@ryxeo.com>
  4 * Copyright (C) 2004-2010	Laurent Destailleur		<eldy@users.sourceforge.net>
  5 * Copyright (C) 2005-2012	Regis Houssin			<regis.houssin@capnetworks.com>
  6 * Copyright (C) 2010		Juanjo Menent			<jmenent@2byte.es>
  7 *
  8 * This program is free software; you can redistribute it and/or modify
  9 * it under the terms of the GNU General Public License as published by
 10 * the Free Software Foundation; either version 3 of the License, or
 11 * (at your option) any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 20 */
 21
 22/**
 23 *	\file       htdocs/compta/facture/prelevement.php
 24 *	\ingroup    facture
 25 *	\brief      Gestion des prelevement d'une facture
 26 */
 27
 28require '../../main.inc.php';
 29require_once DOL_DOCUMENT_ROOT.'/core/lib/invoice.lib.php';
 30require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
 31require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
 32require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
 33
 34if (!$user->rights->facture->lire) accessforbidden();
 35
 36$langs->load("bills");
 37$langs->load("banks");
 38$langs->load("withdrawals");
 39
 40$id=(GETPOST('id','int')?GETPOST('id','int'):GETPOST('facid','int'));  // For backward compatibility
 41$ref=GETPOST('ref','alpha');
 42$socid=GETPOST('socid','int');
 43$action=GETPOST('action','alpha');
 44
 45$fieldid = (! empty($ref)?'facnumber':'rowid');
 46if ($user->societe_id) $socid=$user->societe_id;
 47$result = restrictedArea($user, 'facture', $id, '', '', 'fk_soc', $fieldid);
 48
 49$object = new Facture($db);
 50
 51// Load object
 52if ($id > 0 || ! empty($ref))
 53{
 54	$ret=$object->fetch($id, $ref);
 55	if ($ret > 0)
 56	{
 57		$object->fetch_thirdparty();
 58	}
 59}
 60
 61/*
 62 * Actions
 63 */
 64
 65if ($action == "new")
 66{
 67    if ($object->id > 0)
 68    {
 69        $result = $object->demande_prelevement($user);
 70        if ($result > 0)
 71        {
 72            header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
 73            exit;
 74        }
 75        else
 76        {
 77        	setEventMessage($object->error, 'errors');
 78        }
 79    }
 80}
 81
 82if ($action == "delete")
 83{
 84    if ($object->id > 0)
 85    {
 86        $result = $object->demande_prelevement_delete($user, GETPOST('did'));
 87        if ($result == 0)
 88        {
 89            header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
 90            exit;
 91        }
 92    }
 93}
 94
 95
 96/*
 97 * View
 98 */
 99
100$now=dol_now();
101
102llxHeader('', $langs->trans("Bill"));
103
104$form = new Form($db);
105
106/* *************************************************************************** */
107/*                                                                             */
108/* Mode fiche                                                                  */
109/*                                                                             */
110/* *************************************************************************** */
111
112if ($object->id > 0)
113{
114	$totalpaye  = $object->getSommePaiement();
115	$totalcreditnotes = $object->getSumCreditNotesUsed();
116	$totaldeposits = $object->getSumDepositsUsed();
117	//print "totalpaye=".$totalpaye." totalcreditnotes=".$totalcreditnotes." totaldeposts=".$totaldeposits;
118
119	// We can also use bcadd to avoid pb with floating points
120	// For example print 239.2 - 229.3 - 9.9; does not return 0.
121	//$resteapayer=bcadd($object->total_ttc,$totalpaye,$conf->global->MAIN_MAX_DECIMALS_TOT);
122	//$resteapayer=bcadd($resteapayer,$totalavoir,$conf->global->MAIN_MAX_DECIMALS_TOT);
123	$resteapayer = price2num($object->total_ttc - $totalpaye - $totalcreditnotes - $totaldeposits,'MT');
124
125	if ($object->paye) $resteapayer=0;
126	$resteapayeraffiche=$resteapayer;
127
128	$absolute_discount=$object->thirdparty->getAvailableDiscounts('','fk_facture_source IS NULL');
129	$absolute_creditnote=$object->thirdparty->getAvailableDiscounts('','fk_facture_source IS NOT NULL');
130	$absolute_discount=price2num($absolute_discount,'MT');
131	$absolute_creditnote=price2num($absolute_creditnote,'MT');
132
133	$author = new User($db);
134	if ($object->user_author)
135	{
136		$author->fetch($object->user_author);
137	}
138
139	$head = facture_prepare_head($object);
140
141	dol_fiche_head($head, 'standingorders', $langs->trans('InvoiceCustomer'),0,'bill');
142
143	/*
144	 *   Facture
145	*/
146	print '<table class="border" width="100%">';
147
148	$linkback = '<a href="'.DOL_URL_ROOT.'/compta/facture/list.php'.(! empty($socid)?'?socid='.$socid:'').'">'.$langs->trans("BackToList").'</a>';
149
150	// Ref
151	print '<tr><td width="20%">'.$langs->trans("Ref").'</td><td colspan="5">';
152	$morehtmlref='';
153	$discount=new DiscountAbsolute($db);
154	$result=$discount->fetch(0,$object->id);
155	if ($result > 0)
156	{
157		$morehtmlref=' ('.$langs->trans("CreditNoteConvertedIntoDiscount",$discount->getNomUrl(1,'discount')).')';
158	}
159	if ($result < 0)
160	{
161		dol_print_error('',$discount->error);
162	}
163	print $form->showrefnav($object, 'ref', $linkback, 1, 'facnumber', 'ref', $morehtmlref);
164	print "</td></tr>";
165
166	// Ref customer
167	print '<tr><td width="20%">';
168	print '<table class="nobordernopadding" width="100%"><tr><td>';
169	print $langs->trans('RefCustomer');
170	print '</td>';
171	print '</tr></table>';
172	print '</td>';
173	print '<td colspan="5">';
174	print $object->ref_client;
175	print '</td></tr>';
176
177	// Third party
178	print '<tr><td>'.$langs->trans('Company').'</td>';
179	print '<td colspan="5">'.$object->thirdparty->getNomUrl(1,'compta');
180	print ' &nbsp; (<a href="'.DOL_URL_ROOT.'/compta/facture/list.php?socid='.$object->socid.'">'.$langs->trans('OtherBills').'</a>)</td>';
181	print '</tr>';
182
183	// Type
184	print '<tr><td>'.$langs->trans('Type').'</td><td colspan="5">';
185	print $object->getLibType();
186	if ($object->type == 1)
187	{
188		$facreplaced=new Facture($db);
189		$facreplaced->fetch($object->fk_facture_source);
190		print ' ('.$langs->transnoentities("ReplaceInvoice",$facreplaced->getNomUrl(1)).')';
191	}
192	if ($object->type == 2)
193	{
194		$facusing=new Facture($db);
195		$facusing->fetch($object->fk_facture_source);
196		print ' ('.$langs->transnoentities("CorrectInvoice",$facusing->getNomUrl(1)).')';
197	}
198
199	$facidavoir=$object->getListIdAvoirFromInvoice();
200	if (count($facidavoir) > 0)
201	{
202		print ' ('.$langs->transnoentities("InvoiceHasAvoir");
203		$i=0;
204		foreach($facidavoir as $id)
205		{
206			if ($i==0) print ' ';
207			else print ',';
208			$facavoir=new Facture($db);
209			$facavoir->fetch($id);
210			print $facavoir->getNomUrl(1);
211		}
212		print ')';
213	}
214	// FIXME $facidnext not defined
215	/*
216	if ($facidnext > 0)
217	{
218		$facthatreplace=new Facture($db);
219		$facthatreplace->fetch($facidnext);
220		print ' ('.$langs->transnoentities("ReplacedByInvoice",$facthatreplace->getNomUrl(1)).')';
221	}
222	*/
223	print '</td></tr>';
224
225	// Discounts
226	print '<tr><td>'.$langs->trans('Discounts').'</td><td colspan="5">';
227	if ($object->thirdparty->remise_client) print $langs->trans("CompanyHasRelativeDiscount",$object->thirdparty->remise_client);
228	else print $langs->trans("CompanyHasNoRelativeDiscount");
229	print '. ';
230	if ($absolute_discount > 0)
231	{
232		if ($object->statut > 0 || $object->type == 2 || $object->type == 3)
233		{
234			if ($object->statut == 0)
235			{
236				print $langs->trans("CompanyHasAbsoluteDiscount",price($absolute_discount),$langs->transnoentities("Currency".$conf->currency)).'. ';
237			}
238			else
239			{
240				if ($object->statut < 1 || $object->type == 2 || $object->type == 3)
241				{
242					$text=$langs->trans("CompanyHasAbsoluteDiscount",price($absolute_discount),$langs->transnoentities("Currency".$conf->currency));
243					print '<br>'.$text.'.<br>';
244				}
245				else
246				{
247					$text=$langs->trans("CompanyHasAbsoluteDiscount",price($absolute_discount),$langs->transnoentities("Currency".$conf->currency));
248					$text2=$langs->trans("AbsoluteDiscountUse");
249					print $form->textwithpicto($text,$text2);
250				}
251			}
252		}
253		else
254		{
255			// Remise dispo de type non avoir
256			$filter='fk_facture_source IS NULL';
257			print '<br>';
258			$form->form_remise_dispo($_SERVER["PHP_SELF"].'?id='.$object->id,0,'remise_id',$object->thirdparty->id,$absolute_discount,$filter,$resteapayer);
259		}
260	}
261	if ($absolute_creditnote > 0)
262	{
263		// If validated, we show link "add credit note to payment"
264		if ($object->statut != 1 || $object->type == 2 || $object->type == 3)
265		{
266			if ($object->statut == 0 && $object->type != 3)
267			{
268				$text=$langs->trans("CompanyHasCreditNote",price($absolute_creditnote),$langs->transnoentities("Currency".$conf->currency));
269				print $form->textwithpicto($text,$langs->trans("CreditNoteDepositUse"));
270			}
271			else print $langs->trans("CompanyHasCreditNote",price($absolute_creditnote),$langs->transnoentities("Currency".$conf->currency)).'.';
272		}
273		else
274		{
275			// Remise dispo de type avoir
276			$filter='fk_facture_source IS NOT NULL';
277			if (! $absolute_discount) print '<br>';
278			$form->form_remise_dispo($_SERVER["PHP_SELF"].'?id='.$object->id,0,'remise_id_for_payment',$object->thirdparty->id,$absolute_creditnote,$filter,$resteapayer);
279		}
280	}
281	if (! $absolute_discount && ! $absolute_creditnote) print $langs->trans("CompanyHasNoAbsoluteDiscount").'.';
282	print '</td></tr>';
283
284	// Date invoice
285	print '<tr><td>';
286	print '<table class="nobordernopadding" width="100%"><tr><td>';
287	print $langs->trans('Date');
288	print '</td>';
289	if ($object->type != 2 && $action != 'editinvoicedate' && ! empty($object->brouillon) && $user->rights->facture->creer) print '<td align="right"><a href="'.$_SERVER["PHP_SELF"].'?action=editinvoicedate&amp;id='.$object->id.'">'.img_edit($langs->trans('SetDate'),1).'</a></td>';
290	print '</tr></table>';
291	print '</td><td colspan="3">';
292
293	if ($object->type != 2)
294	{
295		if ($action == 'editinvoicedate')
296		{
297			$form->form_date($_SERVER['PHP_SELF'].'?id='.$object->id,$object->date,'invoicedate');
298		}
299		else
300		{
301			print dol_print_date($object->date,'daytext');
302		}
303	}
304	else
305	{
306		print dol_print_date($object->date,'daytext');
307	}
308	print '</td>';
309	print '</tr>';
310
311	// Date payment term
312	print '<tr><td>';
313	print '<table class="nobordernopadding" width="100%"><tr><td>';
314	print $langs->trans('DateMaxPayment');
315	print '</td>';
316	if ($object->type != 2 && $action != 'editpaymentterm' && ! empty($object->brouillon) && $user->rights->facture->creer) print '<td align="right"><a href="'.$_SERVER["PHP_SELF"].'?action=editpaymentterm&amp;id='.$object->id.'">'.img_edit($langs->trans('SetDate'),1).'</a></td>';
317	print '</tr></table>';
318	print '</td><td colspan="3">';
319	if ($object->type != 2)
320	{
321		if ($action == 'editpaymentterm')
322		{
323			$form->form_date($_SERVER['PHP_SELF'].'?id='.$object->id,$object->date_lim_reglement,'paymentterm');
324		}
325		else
326		{
327			print dol_print_date($object->date_lim_reglement,'daytext');
328			if ($object->date_lim_reglement < ($now - $conf->facture->client->warning_delay) && ! $object->paye && $object->statut == 1 && ! isset($object->am)) print img_warning($langs->trans('Late'));
329		}
330	}
331	else
332	{
333		print '&nbsp;';
334	}
335	print '</td></tr>';
336
337	// Conditions de reglement
338	print '<tr><td>';
339	print '<table class="nobordernopadding" width="100%"><tr><td>';
340	print $langs->trans('PaymentConditionsShort');
341	print '</td>';
342	if ($object->type != 2 && $action != 'editconditions' && ! empty($object->brouillon) && $user->rights->facture->creer) print '<td align="right"><a href="'.$_SERVER["PHP_SELF"].'?action=editconditions&amp;id='.$object->id.'">'.img_edit($langs->trans('SetConditions'),1).'</a></td>';
343	print '</tr></table>';
344	print '</td><td colspan="3">';
345	if ($object->type != 2)
346	{
347		if ($action == 'editconditions')
348		{
349			$form->form_conditions_reglement($_SERVER['PHP_SELF'].'?id='.$object->id,$object->cond_reglement_id,'cond_reglement_id');
350		}
351		else
352		{
353			$form->form_conditions_reglement($_SERVER['PHP_SELF'].'?id='.$object->id,$object->cond_reglement_id,'none');
354		}
355	}
356	else
357	{
358		print '&nbsp;';
359	}
360	print '</td></tr>';
361
362	// Mode de reglement
363	print '<tr><td>';
364	print '<table class="nobordernopadding" width="100%"><tr><td>';
365	print $langs->trans('PaymentMode');
366	print '</td>';
367	if ($action != 'editmode' && ! empty($object->brouillon) && $user->rights->facture->creer) print '<td align="right"><a href="'.$_SERVER["PHP_SELF"].'?action=editmode&amp;id='.$object->id.'">'.img_edit($langs->trans('SetMode'),1).'</a></td>';
368	print '</tr></table>';
369	print '</td><td colspan="3">';
370	if ($action == 'editmode')
371	{
372		$form->form_modes_reglement($_SERVER['PHP_SELF'].'?id='.$object->id,$object->mode_reglement_id,'mode_reglement_id');
373	}
374	else
375	{
376		$form->form_modes_reglement($_SERVER['PHP_SELF'].'?id='.$object->id,$object->mode_reglement_id,'none');
377	}
378	print '</td></tr>';
379
380	// Montants
381	print '<tr><td>'.$langs->trans('AmountHT').'</td>';
382	print '<td align="right" colspan="2" nowrap>'.price($object->total_ht).'</td>';
383	print '<td>'.$langs->trans('Currency'.$conf->currency).'</td></tr>';
384	print '<tr><td>'.$langs->trans('AmountVAT').'</td><td align="right" colspan="2" nowrap>'.price($object->total_tva).'</td>';
385	print '<td>'.$langs->trans('Currency'.$conf->currency).'</td></tr>';
386
387	// Amount Local Taxes
388	if ($mysoc->country_code=='ES')
389	{
390		if ($mysoc->localtax1_assuj=="1") //Localtax1 RE
391		{
392			print '<tr><td>'.$langs->transcountry("AmountLT1",$mysoc->country_code).'</td>';
393			print '<td align="right" colspan="2" nowrap>'.price($object->total_localtax1).'</td>';
394			print '<td>'.$langs->trans("Currency".$conf->currency).'</td></tr>';
395		}
396		if ($mysoc->localtax2_assuj=="1") //Localtax2 IRPF
397		{
398			print '<tr><td>'.$langs->transcountry("AmountLT2",$mysoc->country_code).'</td>';
399			print '<td align="right" colspan="2" nowrap>'.price($object->total_localtax2).'</td>';
400			print '<td>'.$langs->trans("Currency".$conf->currency).'</td></tr>';
401		}
402	}
403
404	print '<tr><td>'.$langs->trans('AmountTTC').'</td><td align="right" colspan="2" nowrap>'.price($object->total_ttc).'</td>';
405	print '<td>'.$langs->trans('Currency'.$conf->currency).'</td></tr>';
406
407	// Statut
408	print '<tr><td>'.$langs->trans('Status').'</td>';
409	print '<td align="left" colspan="3">'.($object->getLibStatut(4,$totalpaye)).'</td></tr>';
410
411	print '<tr><td>'.$langs->trans("RIB").'</td><td colspan="5">';
412	print $object->thirdparty->display_rib();
413	print '</td></tr>';
414
415	print '</table>';
416
417	dol_fiche_end();
418
419
420
421	/*
422	 * Withdrawal request
423	*/
424
425	$sql = "SELECT pfd.rowid, pfd.traite, pfd.date_demande as date_demande";
426	$sql .= " , pfd.date_traite as date_traite";
427	$sql .= " , pfd.amount";
428	$sql .= " , u.rowid as user_id, u.name, u.firstname, u.login";
429	$sql .= " FROM ".MAIN_DB_PREFIX."prelevement_facture_demande as pfd";
430	$sql .= " , ".MAIN_DB_PREFIX."user as u";
431	$sql .= " WHERE fk_facture = ".$object->id;
432	$sql .= " AND pfd.fk_user_demande = u.rowid";
433	$sql .= " AND pfd.traite = 0";
434	$sql .= " ORDER BY pfd.date_demande DESC";
435
436	$result_sql = $db->query($sql);
437	if ($result_sql)
438	{
439		$num = $db->num_rows($result_sql);
440	}
441
442
443	/*
444	 * Buttons
445	*/
446	print "\n<div class=\"tabsAction\">\n";
447
448	// Add a withdraw request
449	if ($object->statut > 0 && $object->paye == 0 && $num == 0)
450	{
451		if ($user->rights->prelevement->bons->creer)
452		{
453			print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&amp;action=new">'.$langs->trans("MakeWithdrawRequest").'</a>';
454		}
455		else
456		{
457			print '<a class="butActionRefused" href="#">'.$langs->trans("MakeWithdrawRequest").'</a>';
458		}
459	}
460	else
461	{
462		print '<a class="butActionRefused" href="#">'.$langs->trans("MakeWithdrawRequest").'</a>';
463	}
464
465	print "</div><br>\n";
466
467
468	print $langs->trans("DoStandingOrdersBeforePayments").'<br><br>';
469
470
471	/*
472	 * Withdrawals
473	*/
474	print '<table class="noborder" width="100%">';
475
476	print '<tr class="liste_titre">';
477	print '<td align="left">'.$langs->trans("DateRequest").'</td>';
478	print '<td align="center">'.$langs->trans("DateProcess").'</td>';
479	print '<td align="center">'.$langs->trans("Amount").'</td>';
480	print '<td align="center">'.$langs->trans("WithdrawalReceipt").'</td>';
481	print '<td align="center">'.$langs->trans("User").'</td><td>&nbsp;</td><td>&nbsp;</td>';
482	print '</tr>';
483	$var=True;
484
485	if ($result_sql)
486	{
487		$i = 0;
488
489		while ($i < $num)
490		{
491			$obj = $db->fetch_object($result_sql);
492			$var=!$var;
493
494			print "<tr ".$bc[$var].">";
495			print '<td align="left">'.dol_print_date($db->jdate($obj->date_demande),'day')."</td>\n";
496			print '<td align="center">'.$langs->trans("OrderWaiting").'</td>';
497			print '<td align="center">'.price($obj->amount).'</td>';
498			print '<td align="center">-</td>';
499			print '<td align="center"><a href="'.DOL_URL_ROOT.'/user/fiche.php?id='.$obj->user_id.'">'.img_object($langs->trans("ShowUser"),'user').' '.$obj->login.'</a></td>';
500			print '<td>&nbsp;</td>';
501			print '<td>';
502			print '<a href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&amp;action=delete&amp;did='.$obj->rowid.'">';
503			print img_delete();
504			print '</a></td>';
505			print "</tr>\n";
506			$i++;
507		}
508
509		$db->free($result_sql);
510	}
511	else
512	{
513		dol_print_error($db);
514	}
515
516	$sql = "SELECT pfd.rowid, pfd.traite, pfd.date_demande,";
517	$sql.= " pfd.date_traite, pfd.fk_prelevement_bons, pfd.amount,";
518	$sql.= " pb.ref,";
519	$sql.= " u.rowid as user_id, u.name, u.firstname, u.login";
520	$sql.= " FROM ".MAIN_DB_PREFIX."prelevement_facture_demande as pfd,";
521	$sql.= " ".MAIN_DB_PREFIX."prelevement_bons as pb,";
522	$sql.= " ".MAIN_DB_PREFIX."user as u";
523	$sql.= " WHERE fk_facture = ".$object->id;
524	$sql.= " AND pfd.fk_user_demande = u.rowid";
525	$sql.= " AND pb.rowid = pfd.fk_prelevement_bons";
526	$sql.= " AND pfd.traite = 1";
527	$sql.= " ORDER BY pfd.date_demande DESC";
528
529	$result = $db->query($sql);
530	if ($result)
531	{
532		$num = $db->num_rows($result);
533		$i = 0;
534
535		while ($i < $num)
536		{
537			$obj = $db->fetch_object($result);
538			$var=!$var;
539
540			print "<tr $bc[$var]>";
541
542			print '<td align="left">'.dol_print_date($db->jdate($obj->date_demande),'day')."</td>\n";
543
544			print '<td align="center">'.dol_print_date($db->jdate($obj->date_traite),'day')."</td>\n";
545
546			print '<td align="center">'.price($obj->amount).'</td>';
547
548			print '<td align="center">';
549			$withdrawreceipt=new BonPrelevement($db);
550			$withdrawreceipt->id=$obj->fk_prelevement_bons;
551			$withdrawreceipt->ref=$obj->ref;
552			print $withdrawreceipt->getNomUrl(1);
553			print "</td>\n";
554
555			print '<td align="center"><a href="'.DOL_URL_ROOT.'/user/fiche.php?id='.$obj->user_id.'">'.img_object($langs->trans("ShowUser"),'user').' '.$obj->login.'</a></td>';
556
557			print '<td>&nbsp;</td>';
558			print '<td>&nbsp;</td>';
559
560			print "</tr>\n";
561			$i++;
562		}
563
564		$db->free($result);
565	}
566	else
567	{
568		dol_print_error($db);
569	}
570
571	print "</table>";
572}
573
574
575llxFooter();
576$db->close();
577?>