* Copyright (C) 2004 Eric Seigne * Copyright (C) 2004-2016 Laurent Destailleur * Copyright (C) 2005-2012 Regis Houssin * Copyright (C) 2010-2014 Juanjo Menent * Copyright (C) 2017 Ferran Marcet * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /** * \file htdocs/compta/facture/prelevement.php * \ingroup facture * \brief Gestion des prelevement d'une facture */ require '../../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/invoice.lib.php'; require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php'; require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php'; if (! empty($conf->projet->enabled)) { require_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php'; } if (!$user->rights->facture->lire) accessforbidden(); // Load translation files required by the page $langs->loadLangs(array('bills', 'banks', 'withdrawals', 'companies')); $id=(GETPOST('id','int')?GETPOST('id','int'):GETPOST('facid','int')); // For backward compatibility $ref=GETPOST('ref','alpha'); $socid=GETPOST('socid','int'); $action=GETPOST('action','alpha'); $fieldid = (! empty($ref)?'facnumber':'rowid'); if ($user->societe_id) $socid=$user->societe_id; $result = restrictedArea($user, 'facture', $id, '', '', 'fk_soc', $fieldid); $object = new Facture($db); // Load object if ($id > 0 || ! empty($ref)) { $ret=$object->fetch($id, $ref); if ($ret > 0) { $object->fetch_thirdparty(); } } /* * Actions */ if ($action == "new") { if ($object->id > 0) { $db->begin(); $result = $object->demande_prelevement($user, GETPOST('withdraw_request_amount')); if ($result > 0) { $db->commit(); setEventMessages($langs->trans("RecordSaved"), null, 'mesgs'); } else { $db->rollback(); setEventMessages($object->error, $object->errors, 'errors'); } } $action=''; } if ($action == "delete") { if ($object->id > 0) { $result = $object->demande_prelevement_delete($user, GETPOST('did')); if ($result == 0) { header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id); exit; } } } /* * View */ $now=dol_now(); $title = $langs->trans('InvoiceCustomer') . " - " . $langs->trans('StandingOrders'); $helpurl = "EN:Customers_Invoices|FR:Factures_Clients|ES:Facturas_a_clientes"; llxHeader('', $title, $helpurl); $form = new Form($db); /* *************************************************************************** */ /* */ /* Mode fiche */ /* */ /* *************************************************************************** */ if ($object->id > 0) { $selleruserevenustamp = $mysoc->useRevenueStamp(); $totalpaye = $object->getSommePaiement(); $totalcreditnotes = $object->getSumCreditNotesUsed(); $totaldeposits = $object->getSumDepositsUsed(); //print "totalpaye=".$totalpaye." totalcreditnotes=".$totalcreditnotes." totaldeposts=".$totaldeposits; // We can also use bcadd to avoid pb with floating points // For example print 239.2 - 229.3 - 9.9; does not return 0. //$resteapayer=bcadd($object->total_ttc,$totalpaye,$conf->global->MAIN_MAX_DECIMALS_TOT); //$resteapayer=bcadd($resteapayer,$totalavoir,$conf->global->MAIN_MAX_DECIMALS_TOT); $resteapayer = price2num($object->total_ttc - $totalpaye - $totalcreditnotes - $totaldeposits,'MT'); if ($object->paye) $resteapayer=0; $resteapayeraffiche=$resteapayer; if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) { $filterabsolutediscount = "fk_facture_source IS NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice $filtercreditnote = "fk_facture_source IS NOT NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice } else { $filterabsolutediscount = "fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')"; $filtercreditnote = "fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')"; } $absolute_discount=$object->thirdparty->getAvailableDiscounts('',$filterabsolutediscount); $absolute_creditnote=$object->thirdparty->getAvailableDiscounts('',$filtercreditnote); $absolute_discount=price2num($absolute_discount,'MT'); $absolute_creditnote=price2num($absolute_creditnote,'MT'); $author = new User($db); if ($object->user_author) { $author->fetch($object->user_author); } $head = facture_prepare_head($object); dol_fiche_head($head, 'standingorders', $langs->trans('InvoiceCustomer'), -1, 'bill'); // Invoice content $linkback = '' . $langs->trans("BackToList") . ''; $morehtmlref='
'; // Ref customer $morehtmlref.=$form->editfieldkey("RefCustomer", 'ref_client', $object->ref_client, $object, 0, 'string', '', 0, 1); $morehtmlref.=$form->editfieldval("RefCustomer", 'ref_client', $object->ref_client, $object, 0, 'string', '', null, null, '', 1); // Thirdparty $morehtmlref.='
'.$langs->trans('ThirdParty') . ' : ' . $object->thirdparty->getNomUrl(1); // Project if (! empty($conf->projet->enabled)) { $langs->load("projects"); $morehtmlref.='
'.$langs->trans('Project') . ' '; if ($user->rights->facture->creer) { if ($action != 'classify') //$morehtmlref.='' . img_edit($langs->transnoentitiesnoconv('SetProject')) . ' : '; $morehtmlref.=' : '; if ($action == 'classify') { //$morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'projectid', 0, 0, 1, 1); $morehtmlref.='
'; $morehtmlref.=''; $morehtmlref.=''; $morehtmlref.=$formproject->select_projects($object->socid, $object->fk_project, 'projectid', $maxlength, 0, 1, 0, 1, 0, 0, '', 1); $morehtmlref.=''; $morehtmlref.='
'; } else { $morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'none', 0, 0, 0, 1); } } else { if (! empty($object->fk_project)) { $proj = new Project($db); $proj->fetch($object->fk_project); $morehtmlref.=''; $morehtmlref.=$proj->ref; $morehtmlref.=''; } else { $morehtmlref.=''; } } } $morehtmlref.='
'; $object->totalpaye = $totalpaye; // To give a chance to dol_banner_tab to use already paid amount to show correct status dol_banner_tab($object, 'ref', $linkback, 1, 'facnumber', 'ref', $morehtmlref, '', 0, '', ''); print '
'; print '
'; print '
'; print ''; // Type print ''; // Discounts print ''; // Date invoice print ''; print ''; // Payment condition print ''; // Date payment term print ''; // Payment mode print ''; // Bank Account print '"; print ''; print ''; print '
'.$langs->trans('Type').''; print $object->getLibType(); if ($object->type == Facture::TYPE_REPLACEMENT) { $facreplaced=new Facture($db); $facreplaced->fetch($object->fk_facture_source); print ' ('.$langs->transnoentities("ReplaceInvoice",$facreplaced->getNomUrl(1)).')'; } if ($object->type == Facture::TYPE_CREDIT_NOTE) { $facusing=new Facture($db); $facusing->fetch($object->fk_facture_source); print ' ('.$langs->transnoentities("CorrectInvoice",$facusing->getNomUrl(1)).')'; } $facidavoir=$object->getListIdAvoirFromInvoice(); if (count($facidavoir) > 0) { print ' ('.$langs->transnoentities("InvoiceHasAvoir"); $i=0; foreach($facidavoir as $id) { if ($i==0) print ' '; else print ','; $facavoir=new Facture($db); $facavoir->fetch($id); print $facavoir->getNomUrl(1); } print ')'; } /* if ($facidnext > 0) { $facthatreplace=new Facture($db); $facthatreplace->fetch($facidnext); print ' ('.$langs->transnoentities("ReplacedByInvoice",$facthatreplace->getNomUrl(1)).')'; } */ print '
'.$langs->trans('Discounts').''; $thirdparty = $object->thirdparty; $discount_type = 0; $backtopage = urlencode($_SERVER["PHP_SELF"] . '?facid=' . $object->id); $cannotApplyDiscount = 1; include DOL_DOCUMENT_ROOT.'/core/tpl/object_discounts.tpl.php'; print '
'; print ''; if ($object->type != Facture::TYPE_CREDIT_NOTE && $action != 'editinvoicedate' && ! empty($object->brouillon) && $user->rights->facture->creer) print ''; print '
'; print $langs->trans('DateInvoice'); print 'id.'">'.img_edit($langs->trans('SetDate'),1).'
'; print '
'; if ($object->type != Facture::TYPE_CREDIT_NOTE) { if ($action == 'editinvoicedate') { $form->form_date($_SERVER['PHP_SELF'].'?id='.$object->id,$object->date,'invoicedate'); } else { print dol_print_date($object->date,'daytext'); } } else { print dol_print_date($object->date,'daytext'); } print '
'; print ''; if ($object->type != Facture::TYPE_CREDIT_NOTE && $action != 'editconditions' && ! empty($object->brouillon) && $user->rights->facture->creer) print ''; print '
'; print $langs->trans('PaymentConditionsShort'); print 'id.'">'.img_edit($langs->trans('SetConditions'),1).'
'; print '
'; if ($object->type != Facture::TYPE_CREDIT_NOTE) { if ($action == 'editconditions') { $form->form_conditions_reglement($_SERVER['PHP_SELF'].'?id='.$object->id,$object->cond_reglement_id,'cond_reglement_id'); } else { $form->form_conditions_reglement($_SERVER['PHP_SELF'].'?id='.$object->id,$object->cond_reglement_id,'none'); } } else { print ' '; } print '
'; print ''; if ($object->type != Facture::TYPE_CREDIT_NOTE && $action != 'editpaymentterm' && ! empty($object->brouillon) && $user->rights->facture->creer) print ''; print '
'; print $langs->trans('DateMaxPayment'); print 'id.'">'.img_edit($langs->trans('SetDate'),1).'
'; print '
'; if ($object->type != Facture::TYPE_CREDIT_NOTE) { if ($action == 'editpaymentterm') { $form->form_date($_SERVER['PHP_SELF'].'?id='.$object->id,$object->date_lim_reglement,'paymentterm'); } else { print dol_print_date($object->date_lim_reglement,'daytext'); if ($object->hasDelay()) { print img_warning($langs->trans('Late')); } } } else { print ' '; } print '
'; print ''; if ($action != 'editmode' && ! empty($object->brouillon) && $user->rights->facture->creer) print ''; print '
'; print $langs->trans('PaymentMode'); print 'id.'">'.img_edit($langs->trans('SetMode'),1).'
'; print '
'; if ($action == 'editmode') { $form->form_modes_reglement($_SERVER['PHP_SELF'].'?id='.$object->id, $object->mode_reglement_id, 'mode_reglement_id'); } else { $form->form_modes_reglement($_SERVER['PHP_SELF'].'?id='.$object->id, $object->mode_reglement_id, 'none'); } print '
'; print ''; print '
'; print $langs->trans('BankAccount'); print ''; if (($action != 'editbankaccount') && $user->rights->commande->creer && ! empty($object->brouillon)) print 'id.'">'.img_edit($langs->trans('SetBankAccount'),1).'
'; print '
'; if ($action == 'editbankaccount') { $form->formSelectAccount($_SERVER['PHP_SELF'].'?id='.$object->id, $object->fk_account, 'fk_account', 1); } else { $form->formSelectAccount($_SERVER['PHP_SELF'].'?id='.$object->id, $object->fk_account, 'none'); } print "
'.$langs->trans("RIB").''; print $object->thirdparty->display_rib(); print '
'; print '
'; print '
'; print '
'; print '
'; print ''; if (!empty($conf->multicurrency->enabled) && ($object->multicurrency_code != $conf->currency)) { // Multicurrency Amount HT print ''; print ''; print ''; // Multicurrency Amount VAT print ''; print ''; print ''; // Multicurrency Amount TTC print ''; print ''; print ''; } // Amount print ''; print ''; // Vat print ''; print ''; // Amount Local Taxes if (($mysoc->localtax1_assuj == "1" && $mysoc->useLocalTax(1)) || $object->total_localtax1 != 0) // Localtax1 { print ''; print ''; } if (($mysoc->localtax2_assuj == "1" && $mysoc->useLocalTax(2)) || $object->total_localtax2 != 0) // Localtax2 { print ''; print ''; } // Revenue stamp if ($selleruserevenustamp) // Test company use revenue stamp { print ''; } // Total with tax print ''; $resteapayer = price2num($object->total_ttc - $totalpaye - $totalcreditnotes - $totaldeposits,'MT'); // TODO Replace this by an include with same code to show already done payment visible in invoice card print ''; print '
' . fieldLabel('MulticurrencyAmountHT','multicurrency_total_ht') . '' . price($object->multicurrency_total_ht, '', $langs, 0, - 1, - 1, (!empty($object->multicurrency_code) ? $object->multicurrency_code : $conf->currency)) . '
' . fieldLabel('MulticurrencyAmountVAT','multicurrency_total_tva') . '' . price($object->multicurrency_total_tva, '', $langs, 0, - 1, - 1, (!empty($object->multicurrency_code) ? $object->multicurrency_code : $conf->currency)) . '
' . fieldLabel('MulticurrencyAmountTTC','multicurrency_total_ttc') . '' . price($object->multicurrency_total_ttc, '', $langs, 0, - 1, - 1, (!empty($object->multicurrency_code) ? $object->multicurrency_code : $conf->currency)) . '
' . $langs->trans('AmountHT') . '' . price($object->total_ht, 1, '', 1, - 1, - 1, $conf->currency) . '
' . $langs->trans('AmountVAT') . '' . price($object->total_tva, 1, '', 1, - 1, - 1, $conf->currency) . '
' . $langs->transcountry("AmountLT1", $mysoc->country_code) . '' . price($object->total_localtax1, 1, '', 1, - 1, - 1, $conf->currency) . '
' . $langs->transcountry("AmountLT2", $mysoc->country_code) . '' . price($object->total_localtax2, 1, '', 1, - 1, - 1, $conf->currency) . '
'; print ''; if ($action != 'editrevenuestamp' && ! empty($object->brouillon) && $user->rights->facture->creer) { print ''; } print '
'; print $langs->trans('RevenueStamp'); print 'id . '">' . img_edit($langs->trans('SetRevenuStamp'), 1) . '
'; print '
'; print price($object->revenuestamp, 1, '', 1, - 1, - 1, $conf->currency); print '
' . $langs->trans('AmountTTC') . '' . price($object->total_ttc, 1, '', 1, - 1, - 1, $conf->currency) . '
'.$langs->trans('RemainderToPay').''.price($resteapayer, 1, '', 1, - 1, - 1, $conf->currency).'
'; print '
'; print '
'; print '
'; print '
'; dol_fiche_end(); $numopen = 0; $pending = 0; $numclosed = 0; // How many Direct debit opened requests ? $sql = "SELECT pfd.rowid, pfd.traite, pfd.date_demande as date_demande"; $sql .= " , pfd.date_traite as date_traite"; $sql .= " , pfd.amount"; $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_facture_demande as pfd"; $sql .= " WHERE fk_facture = ".$object->id; $sql .= " AND pfd.traite = 0"; $sql .= " ORDER BY pfd.date_demande DESC"; $result_sql = $db->query($sql); if ($result_sql) { $num = $db->num_rows($result_sql); $numopen = $num; } else { dol_print_error($db); } // For wich amount ? $sql = "SELECT SUM(pfd.amount) as amount"; $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_facture_demande as pfd"; $sql .= " WHERE fk_facture = ".$object->id; $sql .= " AND pfd.traite = 0"; $result_sql = $db->query($sql); if ($result_sql) { $obj = $db->fetch_object($result_sql); if ($obj) $pending = $obj->amount; } else { dol_print_error($db); } /* * Buttons */ print "\n
\n"; // Add a transfer request if ($object->statut > Facture::STATUS_DRAFT && $object->paye == 0 && $num == 0) { if ($resteapayer > 0) { if ($user->rights->prelevement->bons->creer) { $remaintopaylesspendingdebit = $resteapayer - $pending; print '
'; print ''; print ''; print ''; print ''; print ''; print '
'; } else { print ''.$langs->trans("MakeWithdrawRequest").''; } } else { print ''.$langs->trans("MakeWithdrawRequest").''; } } else { if ($num == 0) { if ($object->statut > Facture::STATUS_DRAFT) print ''.$langs->trans("MakeWithdrawRequest").''; else print ''.$langs->trans("MakeWithdrawRequest").''; } else print ''.$langs->trans("MakeWithdrawRequest").''; } print "

\n"; print '
'.$langs->trans("DoStandingOrdersBeforePayments").'

'; /* * Withdrawals */ print '
'; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; $sql = "SELECT pfd.rowid, pfd.traite, pfd.date_demande as date_demande"; $sql.= " , pfd.date_traite as date_traite, pfd.amount,"; $sql.= " u.rowid as user_id, u.lastname, u.firstname, u.login"; $sql.= " FROM ".MAIN_DB_PREFIX."prelevement_facture_demande as pfd"; $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."user as u on pfd.fk_user_demande = u.rowid"; $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."prelevement_bons as pb ON pb.rowid = pfd.fk_prelevement_bons"; $sql.= " WHERE fk_facture = ".$object->id; $sql.= " AND pfd.traite = 0"; $sql.= " ORDER BY pfd.date_demande DESC"; $result_sql = $db->query($sql); $num = 0; if ($result_sql) { $i = 0; $num = $db->num_rows($result); while ($i < $num) { $obj = $db->fetch_object($result_sql); print ''; print '\n"; print ''; print ''; print ''; print ''; print ''; print ''; print "\n"; $i++; } $db->free($result_sql); } else { dol_print_error($db); } // Past requests $sql = "SELECT pfd.rowid, pfd.traite, pfd.date_demande, pfd.date_traite, pfd.fk_prelevement_bons, pfd.amount,"; $sql.= " pb.ref,"; $sql.= " u.rowid as user_id, u.lastname, u.firstname, u.login"; $sql.= " FROM ".MAIN_DB_PREFIX."prelevement_facture_demande as pfd"; $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."user as u on pfd.fk_user_demande = u.rowid"; $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."prelevement_bons as pb ON pb.rowid = pfd.fk_prelevement_bons"; $sql.= " WHERE fk_facture = ".$object->id; $sql.= " AND pfd.traite = 1"; $sql.= " ORDER BY pfd.date_demande DESC"; $result = $db->query($sql); if ($result) { $num = $db->num_rows($result); $numclosed = $num; $i = 0; while ($i < $num) { $obj = $db->fetch_object($result); print ''; print '\n"; print ''; print ''; print '\n"; print ''; print '\n"; print ''; print "\n"; $i++; } if (! $numopen && ! $numclosed) print ''; $db->free($result); } else { dol_print_error($db); } print "
'.$langs->trans("DateRequest").''.$langs->trans("User").''.$langs->trans("Amount").''.$langs->trans("WithdrawalReceipt").' '.$langs->trans("DateProcess").' 
'.dol_print_date($db->jdate($obj->date_demande),'day')."'.img_object($langs->trans("ShowUser"),'user').' '.$obj->login.''.price($obj->amount).'- '.$langs->trans("OrderWaiting").''; print ''; print img_delete(); print '
'.dol_print_date($db->jdate($obj->date_demande),'day')."'.img_object($langs->trans("ShowUser"),'user').' '.$obj->login.''.price($obj->amount).''; if ($obj->fk_prelevement_bons > 0) { $withdrawreceipt=new BonPrelevement($db); $withdrawreceipt->id=$obj->fk_prelevement_bons; $withdrawreceipt->ref=$obj->ref; print $withdrawreceipt->getNomUrl(1); } print " '.dol_print_date($db->jdate($obj->date_traite),'day')." 
'.$langs->trans("None").'
"; print '
'; } llxFooter(); $db->close();