prelevement.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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-2016 Laurent Destailleur <eldy@users.sourceforge.net>
  5. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  6. * Copyright (C) 2010-2014 Juanjo Menent <jmenent@2byte.es>
  7. * Copyright (C) 2017 Ferran Marcet <fmarcet@2byte.es>
  8. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  22. */
  23. /**
  24. * \file htdocs/compta/facture/prelevement.php
  25. * \ingroup facture
  26. * \brief Management of direct debit order or credit tranfer of invoices
  27. */
  28. require '../../main.inc.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/lib/invoice.lib.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/fourn.lib.php';
  31. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
  33. require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
  34. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  35. require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php';
  36. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php';
  37. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  38. // Load translation files required by the page
  39. $langs->loadLangs(array('bills', 'banks', 'withdrawals', 'companies'));
  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', 'aZ09');
  44. $type = GETPOST('type', 'aZ09');
  45. $fieldid = (!empty($ref) ? 'ref' : 'rowid');
  46. if ($user->socid) {
  47. $socid = $user->socid;
  48. }
  49. $moreparam = '';
  50. if ($type == 'bank-transfer') {
  51. $object = new FactureFournisseur($db);
  52. $moreparam = '&type='.$type;
  53. } else {
  54. $object = new Facture($db);
  55. }
  56. // Load object
  57. if ($id > 0 || !empty($ref)) {
  58. $ret = $object->fetch($id, $ref);
  59. $isdraft = (($object->statut == FactureFournisseur::STATUS_DRAFT) ? 1 : 0);
  60. if ($ret > 0) {
  61. $object->fetch_thirdparty();
  62. }
  63. }
  64. $hookmanager->initHooks(array('directdebitcard', 'globalcard'));
  65. if ($type == 'bank-transfer') {
  66. $result = restrictedArea($user, 'fournisseur', $id, 'facture_fourn', 'facture', 'fk_soc', $fieldid, $isdraft);
  67. if (empty($user->rights->fournisseur->facture->lire)) {
  68. accessforbidden();
  69. }
  70. } else {
  71. $result = restrictedArea($user, 'facture', $id, '', '', 'fk_soc', $fieldid, $isdraft);
  72. if (!$user->rights->facture->lire) {
  73. accessforbidden();
  74. }
  75. }
  76. if ($type == 'bank-transfer') {
  77. $usercancreate = ($user->rights->fournisseur->facture->creer || $user->rights->supplier_invoice->creer);
  78. } else {
  79. $usercancreate = $user->rights->facture->creer;
  80. }
  81. /*
  82. * Actions
  83. */
  84. $parameters = array('socid' => $socid);
  85. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  86. if ($reshook < 0) {
  87. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  88. }
  89. if (empty($reshook)) {
  90. if ($action == "new" && $usercancreate) {
  91. if ($object->id > 0) {
  92. $db->begin();
  93. $newtype = $type;
  94. $sourcetype = 'facture';
  95. if ($type == 'bank-transfer') {
  96. $sourcetype = 'supplier_invoice';
  97. $newtype = 'bank-transfer';
  98. }
  99. $result = $object->demande_prelevement($user, price2num(GETPOST('withdraw_request_amount', 'alpha')), $newtype, $sourcetype);
  100. if ($result > 0) {
  101. $db->commit();
  102. setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
  103. } else {
  104. $db->rollback();
  105. setEventMessages($object->error, $object->errors, 'errors');
  106. }
  107. }
  108. $action = '';
  109. }
  110. if ($action == "delete" && $usercancreate) {
  111. if ($object->id > 0) {
  112. $result = $object->demande_prelevement_delete($user, GETPOST('did', 'int'));
  113. if ($result == 0) {
  114. header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id.'&type='.$type);
  115. exit;
  116. }
  117. }
  118. }
  119. // payments conditions
  120. if ($action == 'setconditions' && $usercancreate) {
  121. $object->fetch($id);
  122. $object->cond_reglement_code = 0; // To clean property
  123. $object->cond_reglement_id = 0; // To clean property
  124. $error = 0;
  125. $db->begin();
  126. if (!$error) {
  127. $result = $object->setPaymentTerms(GETPOST('cond_reglement_id', 'int'));
  128. if ($result < 0) {
  129. $error++;
  130. setEventMessages($object->error, $object->errors, 'errors');
  131. }
  132. }
  133. if (!$error) {
  134. $old_date_echeance = $object->date_echeance;
  135. $new_date_echeance = $object->calculate_date_lim_reglement();
  136. if ($new_date_echeance > $old_date_echeance) {
  137. $object->date_echeance = $new_date_echeance;
  138. }
  139. if ($object->date_echeance < $object->date) {
  140. $object->date_echeance = $object->date;
  141. }
  142. $result = $object->update($user);
  143. if ($result < 0) {
  144. $error++;
  145. setEventMessages($object->error, $object->errors, 'errors');
  146. }
  147. }
  148. if ($error) {
  149. $db->rollback();
  150. } else {
  151. $db->commit();
  152. }
  153. } elseif ($action == 'setmode' && $usercancreate) {
  154. // payment mode
  155. $result = $object->setPaymentMethods(GETPOST('mode_reglement_id', 'int'));
  156. } elseif ($action == 'setdatef' && $usercancreate) {
  157. $newdate = dol_mktime(0, 0, 0, GETPOST('datefmonth', 'int'), GETPOST('datefday', 'int'), GETPOST('datefyear', 'int'), 'tzserver');
  158. if ($newdate > (dol_now('tzuserrel') + (empty($conf->global->INVOICE_MAX_FUTURE_DELAY) ? 0 : $conf->global->INVOICE_MAX_FUTURE_DELAY))) {
  159. if (empty($conf->global->INVOICE_MAX_FUTURE_DELAY)) {
  160. setEventMessages($langs->trans("WarningInvoiceDateInFuture"), null, 'warnings');
  161. } else {
  162. setEventMessages($langs->trans("WarningInvoiceDateTooFarInFuture"), null, 'warnings');
  163. }
  164. }
  165. $object->date = $newdate;
  166. $date_echence_calc = $object->calculate_date_lim_reglement();
  167. if (!empty($object->date_echeance) && $object->date_echeance < $date_echence_calc) {
  168. $object->date_echeance = $date_echence_calc;
  169. }
  170. if ($object->date_echeance && $object->date_echeance < $object->date) {
  171. $object->date_echeance = $object->date;
  172. }
  173. $result = $object->update($user);
  174. if ($result < 0) {
  175. dol_print_error($db, $object->error);
  176. }
  177. } elseif ($action == 'setdate_lim_reglement' && $usercancreate) {
  178. $object->date_echeance = dol_mktime(12, 0, 0, GETPOST('date_lim_reglementmonth', 'int'), GETPOST('date_lim_reglementday', 'int'), GETPOST('date_lim_reglementyear', 'int'));
  179. if (!empty($object->date_echeance) && $object->date_echeance < $object->date) {
  180. $object->date_echeance = $object->date;
  181. setEventMessages($langs->trans("DatePaymentTermCantBeLowerThanObjectDate"), null, 'warnings');
  182. }
  183. $result = $object->update($user);
  184. if ($result < 0) {
  185. dol_print_error($db, $object->error);
  186. }
  187. }
  188. }
  189. /*
  190. * View
  191. */
  192. $form = new Form($db);
  193. $now = dol_now();
  194. if ($type == 'bank-transfer') {
  195. $title = $langs->trans('SupplierInvoice')." - ".$langs->trans('CreditTransfer');
  196. $helpurl = "";
  197. } else {
  198. $title = $langs->trans('InvoiceCustomer')." - ".$langs->trans('StandingOrders');
  199. $helpurl = "EN:Customers_Invoices|FR:Factures_Clients|ES:Facturas_a_clientes";
  200. }
  201. llxHeader('', $title, $helpurl);
  202. if ($object->id > 0) {
  203. $selleruserevenustamp = $mysoc->useRevenueStamp();
  204. $totalpaye = $object->getSommePaiement();
  205. $totalcreditnotes = $object->getSumCreditNotesUsed();
  206. $totaldeposits = $object->getSumDepositsUsed();
  207. //print "totalpaye=".$totalpaye." totalcreditnotes=".$totalcreditnotes." totaldeposts=".$totaldeposits;
  208. // We can also use bcadd to avoid pb with floating points
  209. // For example print 239.2 - 229.3 - 9.9; does not return 0.
  210. //$resteapayer=bcadd($object->total_ttc,$totalpaye,$conf->global->MAIN_MAX_DECIMALS_TOT);
  211. //$resteapayer=bcadd($resteapayer,$totalavoir,$conf->global->MAIN_MAX_DECIMALS_TOT);
  212. $resteapayer = price2num($object->total_ttc - $totalpaye - $totalcreditnotes - $totaldeposits, 'MT');
  213. if ($object->paye) {
  214. $resteapayer = 0;
  215. }
  216. $resteapayeraffiche = $resteapayer;
  217. if ($type == 'bank-transfer') {
  218. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) { // Never use this
  219. $filterabsolutediscount = "fk_invoice_supplier_source IS NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
  220. $filtercreditnote = "fk_invoice_supplier_source IS NOT NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
  221. } else {
  222. $filterabsolutediscount = "fk_invoice_supplier_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS PAID)%')";
  223. $filtercreditnote = "fk_invoice_supplier_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS PAID)%')";
  224. }
  225. $absolute_discount = $object->thirdparty->getAvailableDiscounts('', $filterabsolutediscount, 0, 1);
  226. $absolute_creditnote = $object->thirdparty->getAvailableDiscounts('', $filtercreditnote, 0, 1);
  227. $absolute_discount = price2num($absolute_discount, 'MT');
  228. $absolute_creditnote = price2num($absolute_creditnote, 'MT');
  229. } else {
  230. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  231. $filterabsolutediscount = "fk_facture_source IS NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
  232. $filtercreditnote = "fk_facture_source IS NOT NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
  233. } else {
  234. $filterabsolutediscount = "fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
  235. $filtercreditnote = "fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
  236. }
  237. $absolute_discount = $object->thirdparty->getAvailableDiscounts('', $filterabsolutediscount);
  238. $absolute_creditnote = $object->thirdparty->getAvailableDiscounts('', $filtercreditnote);
  239. $absolute_discount = price2num($absolute_discount, 'MT');
  240. $absolute_creditnote = price2num($absolute_creditnote, 'MT');
  241. }
  242. $author = new User($db);
  243. if ($object->user_author) {
  244. $author->fetch($object->user_author);
  245. }
  246. if ($type == 'bank-transfer') {
  247. $head = facturefourn_prepare_head($object);
  248. } else {
  249. $head = facture_prepare_head($object);
  250. }
  251. $numopen = 0;
  252. $pending = 0;
  253. $numclosed = 0;
  254. // How many Direct debit or Credit transfer open requests ?
  255. $sql = "SELECT pfd.rowid, pfd.traite, pfd.date_demande as date_demande";
  256. $sql .= " , pfd.date_traite as date_traite";
  257. $sql .= " , pfd.amount";
  258. $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_facture_demande as pfd";
  259. if ($type == 'bank-transfer') {
  260. $sql .= " WHERE fk_facture_fourn = ".((int) $object->id);
  261. } else {
  262. $sql .= " WHERE fk_facture = ".((int) $object->id);
  263. }
  264. $sql .= " AND pfd.traite = 0";
  265. $sql .= " AND pfd.ext_payment_id IS NULL";
  266. $sql .= " ORDER BY pfd.date_demande DESC";
  267. $result_sql = $db->query($sql);
  268. if ($result_sql) {
  269. $num = $db->num_rows($result_sql);
  270. $numopen = $num;
  271. } else {
  272. dol_print_error($db);
  273. }
  274. print dol_get_fiche_head($head, 'standingorders', $title, -1, ($type == 'bank-transfer' ? 'supplier_invoice' : 'bill'));
  275. // Invoice content
  276. if ($type == 'bank-transfer') {
  277. $linkback = '<a href="'.DOL_URL_ROOT.'/fourn/facture/list.php?restore_lastsearch_values=1'.(!empty($socid) ? '&socid='.$socid : '').'">'.$langs->trans("BackToList").'</a>';
  278. } else {
  279. $linkback = '<a href="'.DOL_URL_ROOT.'/compta/facture/list.php?restore_lastsearch_values=1'.(!empty($socid) ? '&socid='.$socid : '').'">'.$langs->trans("BackToList").'</a>';
  280. }
  281. $morehtmlref = '<div class="refidno">';
  282. // Ref customer
  283. if ($type == 'bank-transfer') {
  284. $morehtmlref .= $form->editfieldkey("RefSupplier", 'ref_supplier', $object->ref_supplier, $object, 0, 'string', '', 0, 1);
  285. $morehtmlref .= $form->editfieldval("RefSupplier", 'ref_supplier', $object->ref_supplier, $object, 0, 'string', '', null, null, '', 1);
  286. } else {
  287. $morehtmlref .= $form->editfieldkey("RefCustomer", 'ref_client', $object->ref_client, $object, 0, 'string', '', 0, 1);
  288. $morehtmlref .= $form->editfieldval("RefCustomer", 'ref_client', $object->ref_client, $object, 0, 'string', '', null, null, '', 1);
  289. }
  290. // Thirdparty
  291. $morehtmlref .= '<br>'.$langs->trans('ThirdParty').' : '.$object->thirdparty->getNomUrl(1);
  292. if ($type == 'bank-transfer') {
  293. if (empty($conf->global->MAIN_DISABLE_OTHER_LINK) && $object->thirdparty->id > 0) {
  294. $morehtmlref .= ' (<a href="'.DOL_URL_ROOT.'/fourn/facture/list.php?socid='.$object->thirdparty->id.'&search_company='.urlencode($object->thirdparty->name).'">'.$langs->trans("OtherBills").'</a>)';
  295. }
  296. } else {
  297. if (empty($conf->global->MAIN_DISABLE_OTHER_LINK) && $object->thirdparty->id > 0) {
  298. $morehtmlref .= ' (<a href="'.DOL_URL_ROOT.'/compta/facture/list.php?socid='.$object->thirdparty->id.'&search_company='.urlencode($object->thirdparty->name).'">'.$langs->trans("OtherBills").'</a>)';
  299. }
  300. }
  301. // Project
  302. if (!empty($conf->projet->enabled)) {
  303. $langs->load("projects");
  304. $morehtmlref .= '<br>'.$langs->trans('Project').' ';
  305. if ($user->rights->facture->creer) {
  306. if ($action != 'classify') {
  307. //$morehtmlref.='<a class="editfielda" href="' . $_SERVER['PHP_SELF'] . '?action=classify&token='.newToken().'&id=' . $object->id . '">' . img_edit($langs->transnoentitiesnoconv('SetProject')) . '</a> : ';
  308. $morehtmlref .= ' : ';
  309. }
  310. if ($action == 'classify') {
  311. //$morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'projectid', 0, 0, 1, 1);
  312. $morehtmlref .= '<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'">';
  313. $morehtmlref .= '<input type="hidden" name="action" value="classin">';
  314. $morehtmlref .= '<input type="hidden" name="token" value="'.newToken().'">';
  315. $morehtmlref .= '<input type="hidden" name="type" value="'.$type.'">';
  316. $morehtmlref .= $formproject->select_projects($object->socid, $object->fk_project, 'projectid', $maxlength, 0, 1, 0, 1, 0, 0, '', 1);
  317. $morehtmlref .= '<input type="submit" class="button valignmiddle" value="'.$langs->trans("Modify").'">';
  318. $morehtmlref .= '</form>';
  319. } else {
  320. $morehtmlref .= $form->form_project($_SERVER['PHP_SELF'].'?id='.$object->id, $object->socid, $object->fk_project, 'none', 0, 0, 0, 1);
  321. }
  322. } else {
  323. if (!empty($object->fk_project)) {
  324. $proj = new Project($db);
  325. $proj->fetch($object->fk_project);
  326. $morehtmlref .= ' : '.$proj->getNomUrl(1);
  327. if ($proj->title) {
  328. $morehtmlref .= ' - '.$proj->title;
  329. }
  330. } else {
  331. $morehtmlref .= '';
  332. }
  333. }
  334. }
  335. $morehtmlref .= '</div>';
  336. $object->totalpaye = $totalpaye; // To give a chance to dol_banner_tab to use already paid amount to show correct status
  337. dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref, $moreparam, 0, '', '');
  338. print '<div class="fichecenter">';
  339. print '<div class="fichehalfleft">';
  340. print '<div class="underbanner clearboth"></div>';
  341. print '<table class="border centpercent tableforfield">';
  342. // Type
  343. print '<tr><td class="titlefield">'.$langs->trans('Type').'</td><td colspan="3">';
  344. print '<span class="badgeneutral">';
  345. print $object->getLibType();
  346. print '</span>';
  347. if ($object->module_source) {
  348. print ' <span class="opacitymediumbycolor">('.$langs->trans("POS").' '.$object->module_source.' - '.$langs->trans("Terminal").' '.$object->pos_source.')</span>';
  349. }
  350. if ($object->type == $object::TYPE_REPLACEMENT) {
  351. if ($type == 'bank-transfer') {
  352. $facreplaced = new FactureFournisseur($db);
  353. } else {
  354. $facreplaced = new Facture($db);
  355. }
  356. $facreplaced->fetch($object->fk_facture_source);
  357. print ' ('.$langs->transnoentities("ReplaceInvoice", $facreplaced->getNomUrl(1)).')';
  358. }
  359. if ($object->type == $object::TYPE_CREDIT_NOTE) {
  360. if ($type == 'bank-transfer') {
  361. $facusing = new FactureFournisseur($db);
  362. } else {
  363. $facusing = new Facture($db);
  364. }
  365. $facusing->fetch($object->fk_facture_source);
  366. print ' ('.$langs->transnoentities("CorrectInvoice", $facusing->getNomUrl(1)).')';
  367. }
  368. $facidavoir = $object->getListIdAvoirFromInvoice();
  369. if (count($facidavoir) > 0) {
  370. $invoicecredits = array();
  371. foreach ($facidavoir as $facid) {
  372. if ($type == 'bank-transfer') {
  373. $facavoir = new FactureFournisseur($db);
  374. } else {
  375. $facavoir = new Facture($db);
  376. }
  377. $facavoir->fetch($facid);
  378. $invoicecredits[] = $facavoir->getNomUrl(1);
  379. }
  380. print ' ('.$langs->transnoentities("InvoiceHasAvoir") . (count($invoicecredits) ? ' ' : '') . implode(',', $invoicecredits) . ')';
  381. }
  382. /*
  383. if ($facidnext > 0)
  384. {
  385. $facthatreplace=new Facture($db);
  386. $facthatreplace->fetch($facidnext);
  387. print ' ('.$langs->transnoentities("ReplacedByInvoice",$facthatreplace->getNomUrl(1)).')';
  388. }
  389. */
  390. print '</td></tr>';
  391. // Discounts
  392. print '<tr><td>'.$langs->trans('Discounts').'</td><td colspan="3">';
  393. if ($type == 'bank-transfer') {
  394. //$societe = new Fournisseur($db);
  395. //$result = $societe->fetch($object->socid);
  396. $thirdparty = $object->thirdparty;
  397. $discount_type = 1;
  398. } else {
  399. $thirdparty = $object->thirdparty;
  400. $discount_type = 0;
  401. }
  402. $backtopage = urlencode($_SERVER["PHP_SELF"].'?facid='.$object->id);
  403. $cannotApplyDiscount = 1;
  404. include DOL_DOCUMENT_ROOT.'/core/tpl/object_discounts.tpl.php';
  405. print '</td></tr>';
  406. // Label
  407. if ($type == 'bank-transfer') {
  408. print '<tr>';
  409. print '<td>'.$form->editfieldkey("Label", 'label', $object->label, $object, 0).'</td>';
  410. print '<td>'.$form->editfieldval("Label", 'label', $object->label, $object, 0).'</td>';
  411. print '</tr>';
  412. }
  413. // Date invoice
  414. print '<tr><td>';
  415. print '<table class="nobordernopadding centpercent"><tr><td>';
  416. print $langs->trans('DateInvoice');
  417. print '</td>';
  418. if ($object->type != $object::TYPE_CREDIT_NOTE && $action != 'editinvoicedate' && !empty($object->brouillon) && $user->rights->facture->creer) {
  419. print '<td class="right"><a class="editfielda" href="'.$_SERVER["PHP_SELF"].'?action=editinvoicedate&token='.newToken().'&id='.$object->id.'&type='.urlencode($type).'">'.img_edit($langs->trans('SetDate'), 1).'</a></td>';
  420. }
  421. print '</tr></table>';
  422. print '</td><td colspan="3">';
  423. if ($object->type != $object::TYPE_CREDIT_NOTE) {
  424. if ($action == 'editinvoicedate') {
  425. print $form->form_date($_SERVER['PHP_SELF'].'?id='.$object->id, $object->date, 'invoicedate', 0, 0, 1, $type);
  426. } else {
  427. print dol_print_date($object->date, 'day');
  428. }
  429. } else {
  430. print dol_print_date($object->date, 'day');
  431. }
  432. print '</td>';
  433. print '</tr>';
  434. // Payment condition
  435. print '<tr><td>';
  436. print '<table class="nobordernopadding centpercent"><tr><td>';
  437. print $langs->trans('PaymentConditionsShort');
  438. print '</td>';
  439. if ($object->type != $object::TYPE_CREDIT_NOTE && $action != 'editconditions' && !empty($object->brouillon) && $user->rights->facture->creer) {
  440. print '<td class="right"><a class="editfielda" href="'.$_SERVER["PHP_SELF"].'?action=editconditions&token='.newToken().'&id='.$object->id.'&type='.urlencode($type).'">'.img_edit($langs->trans('SetConditions'), 1).'</a></td>';
  441. }
  442. print '</tr></table>';
  443. print '</td><td colspan="3">';
  444. if ($object->type != $object::TYPE_CREDIT_NOTE) {
  445. if ($action == 'editconditions') {
  446. $form->form_conditions_reglement($_SERVER['PHP_SELF'].'?id='.$object->id, $object->cond_reglement_id, 'cond_reglement_id', 0, $type);
  447. } else {
  448. $form->form_conditions_reglement($_SERVER['PHP_SELF'].'?id='.$object->id, $object->cond_reglement_id, 'none');
  449. }
  450. } else {
  451. print '&nbsp;';
  452. }
  453. print '</td></tr>';
  454. // Date payment term
  455. print '<tr><td>';
  456. print '<table class="nobordernopadding centpercent"><tr><td>';
  457. print $langs->trans('DateMaxPayment');
  458. print '</td>';
  459. if ($object->type != $object::TYPE_CREDIT_NOTE && $action != 'editpaymentterm' && !empty($object->brouillon) && $user->rights->facture->creer) {
  460. print '<td class="right"><a class="editfielda" href="'.$_SERVER["PHP_SELF"].'?action=editpaymentterm&token='.newToken().'&id='.$object->id.'&type='.urlencode($type).'">'.img_edit($langs->trans('SetDate'), 1).'</a></td>';
  461. }
  462. print '</tr></table>';
  463. print '</td><td colspan="3">';
  464. if ($object->type != $object::TYPE_CREDIT_NOTE) {
  465. $duedate = $object->date_lim_reglement;
  466. if ($type == 'bank-transfer') {
  467. $duedate = $object->date_echeance;
  468. }
  469. if ($action == 'editpaymentterm') {
  470. print $form->form_date($_SERVER['PHP_SELF'].'?id='.$object->id, $duedate, 'paymentterm', 0, 0, 1, $type);
  471. } else {
  472. print dol_print_date($duedate, 'day');
  473. if ($object->hasDelay()) {
  474. print img_warning($langs->trans('Late'));
  475. }
  476. }
  477. } else {
  478. print '&nbsp;';
  479. }
  480. print '</td></tr>';
  481. // Payment mode
  482. print '<tr><td>';
  483. print '<table class="nobordernopadding centpercent"><tr><td>';
  484. print $langs->trans('PaymentMode');
  485. print '</td>';
  486. if ($action != 'editmode' && !empty($object->brouillon) && $user->rights->facture->creer) {
  487. print '<td class="right"><a class="editfielda" href="'.$_SERVER["PHP_SELF"].'?action=editmode&token='.newToken().'&id='.$object->id.'&type='.urlencode($type).'">'.img_edit($langs->trans('SetMode'), 1).'</a></td>';
  488. }
  489. print '</tr></table>';
  490. print '</td><td colspan="3">';
  491. $filtertype = 'CRDT';
  492. if ($type == 'bank-transfer') {
  493. $filtertype = 'DBIT';
  494. }
  495. if ($action == 'editmode') {
  496. $form->form_modes_reglement($_SERVER['PHP_SELF'].'?id='.$object->id, $object->mode_reglement_id, 'mode_reglement_id', $filtertype, 1, 0, $type);
  497. } else {
  498. $form->form_modes_reglement($_SERVER['PHP_SELF'].'?id='.$object->id, $object->mode_reglement_id, 'none');
  499. }
  500. print '</td></tr>';
  501. // Bank Account
  502. print '<tr><td class="nowrap">';
  503. print '<table width="100%" class="nobordernopadding"><tr><td class="nowrap">';
  504. print $langs->trans('BankAccount');
  505. print '<td>';
  506. if (($action != 'editbankaccount') && $user->rights->commande->creer && !empty($object->brouillon)) {
  507. print '<td class="right"><a class="editfielda" href="'.$_SERVER["PHP_SELF"].'?action=editbankaccount&token='.newToken().'&id='.$object->id.'&type='.urlencode($type).'">'.img_edit($langs->trans('SetBankAccount'), 1).'</a></td>';
  508. }
  509. print '</tr></table>';
  510. print '</td><td colspan="3">';
  511. if ($action == 'editbankaccount') {
  512. $form->formSelectAccount($_SERVER['PHP_SELF'].'?id='.$object->id, $object->fk_account, 'fk_account', 1);
  513. } else {
  514. $form->formSelectAccount($_SERVER['PHP_SELF'].'?id='.$object->id, $object->fk_account, 'none');
  515. }
  516. print "</td>";
  517. print '</tr>';
  518. // IBAN of seller or supplier
  519. $title = 'CustomerIBAN';
  520. if ($type == 'bank-transfer') {
  521. $title = 'SupplierIBAN';
  522. }
  523. print '<tr><td>'.$langs->trans($title).'</td><td colspan="3">';
  524. $bac = new CompanyBankAccount($db);
  525. $bac->fetch(0, $object->thirdparty->id);
  526. print $bac->iban.(($bac->iban && $bac->bic) ? ' / ' : '').$bac->bic;
  527. if (!empty($bac->iban)) {
  528. if ($bac->verif() <= 0) {
  529. print img_warning('Error on default bank number for IBAN : '.$bac->error_message);
  530. }
  531. } else {
  532. if ($numopen || ($type != 'bank-transfer' && $object->mode_reglement_code == 'PRE') || ($type == 'bank-transfer' && $object->mode_reglement_code == 'VIR')) {
  533. print img_warning($langs->trans("NoDefaultIBANFound"));
  534. }
  535. }
  536. print '</td></tr>';
  537. print '</table>';
  538. print '</div>';
  539. print '<div class="fichehalfright">';
  540. print '<div class="underbanner clearboth"></div>';
  541. print '<table class="border centpercent tableforfield">';
  542. if (!empty($conf->multicurrency->enabled) && ($object->multicurrency_code != $conf->currency)) {
  543. // Multicurrency Amount HT
  544. print '<tr><td class="titlefieldmiddle">'.$form->editfieldkey('MulticurrencyAmountHT', 'multicurrency_total_ht', '', $object, 0).'</td>';
  545. print '<td class="nowrap">'.price($object->multicurrency_total_ht, '', $langs, 0, - 1, - 1, (!empty($object->multicurrency_code) ? $object->multicurrency_code : $conf->currency)).'</td>';
  546. print '</tr>';
  547. // Multicurrency Amount VAT
  548. print '<tr><td>'.$form->editfieldkey('MulticurrencyAmountVAT', 'multicurrency_total_tva', '', $object, 0).'</td>';
  549. print '<td class="nowrap">'.price($object->multicurrency_total_tva, '', $langs, 0, - 1, - 1, (!empty($object->multicurrency_code) ? $object->multicurrency_code : $conf->currency)).'</td>';
  550. print '</tr>';
  551. // Multicurrency Amount TTC
  552. print '<tr><td>'.$form->editfieldkey('MulticurrencyAmountTTC', 'multicurrency_total_ttc', '', $object, 0).'</td>';
  553. print '<td class="nowrap">'.price($object->multicurrency_total_ttc, '', $langs, 0, - 1, - 1, (!empty($object->multicurrency_code) ? $object->multicurrency_code : $conf->currency)).'</td>';
  554. print '</tr>';
  555. }
  556. // Amount
  557. print '<tr><td class="titlefield">'.$langs->trans('AmountHT').'</td>';
  558. print '<td class="nowrap">'.price($object->total_ht, 1, '', 1, - 1, - 1, $conf->currency).'</td></tr>';
  559. // Vat
  560. print '<tr><td>'.$langs->trans('AmountVAT').'</td><td colspan="3" class="nowrap">'.price($object->total_tva, 1, '', 1, - 1, - 1, $conf->currency).'</td></tr>';
  561. print '</tr>';
  562. // Amount Local Taxes
  563. if (($mysoc->localtax1_assuj == "1" && $mysoc->useLocalTax(1)) || $object->total_localtax1 != 0) { // Localtax1
  564. print '<tr><td>'.$langs->transcountry("AmountLT1", $mysoc->country_code).'</td>';
  565. print '<td class="nowrap">'.price($object->total_localtax1, 1, '', 1, - 1, - 1, $conf->currency).'</td></tr>';
  566. }
  567. if (($mysoc->localtax2_assuj == "1" && $mysoc->useLocalTax(2)) || $object->total_localtax2 != 0) { // Localtax2
  568. print '<tr><td>'.$langs->transcountry("AmountLT2", $mysoc->country_code).'</td>';
  569. print '<td class=nowrap">'.price($object->total_localtax2, 1, '', 1, - 1, - 1, $conf->currency).'</td></tr>';
  570. }
  571. // Revenue stamp
  572. if ($selleruserevenustamp) { // Test company use revenue stamp
  573. print '<tr><td>';
  574. print '<table class="nobordernopadding" width="100%"><tr><td>';
  575. print $langs->trans('RevenueStamp');
  576. print '</td>';
  577. if ($action != 'editrevenuestamp' && !empty($object->brouillon) && $user->rights->facture->creer) {
  578. print '<td class="right"><a class="editfielda" href="'.$_SERVER["PHP_SELF"].'?action=editrevenuestamp&token='.newToken().'&facid='.$object->id.'">'.img_edit($langs->trans('SetRevenuStamp'), 1).'</a></td>';
  579. }
  580. print '</tr></table>';
  581. print '</td><td>';
  582. print price($object->revenuestamp, 1, '', 1, - 1, - 1, $conf->currency);
  583. print '</td></tr>';
  584. }
  585. // Total with tax
  586. print '<tr><td>'.$langs->trans('AmountTTC').'</td><td class="nowrap">'.price($object->total_ttc, 1, '', 1, - 1, - 1, $conf->currency).'</td></tr>';
  587. $resteapayer = price2num($object->total_ttc - $totalpaye - $totalcreditnotes - $totaldeposits, 'MT');
  588. // TODO Replace this by an include with same code to show already done payment visible in invoice card
  589. print '<tr><td>'.$langs->trans('RemainderToPay').'</td><td class="nowrap">'.price($resteapayer, 1, '', 1, - 1, - 1, $conf->currency).'</td></tr>';
  590. print '</table>';
  591. print '</div>';
  592. print '</div>';
  593. print '<div class="clearboth"></div>';
  594. print dol_get_fiche_end();
  595. // For which amount ?
  596. $sql = "SELECT SUM(pfd.amount) as amount";
  597. $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_facture_demande as pfd";
  598. if ($type == 'bank-transfer') {
  599. $sql .= " WHERE fk_facture_fourn = ".((int) $object->id);
  600. } else {
  601. $sql .= " WHERE fk_facture = ".((int) $object->id);
  602. }
  603. $sql .= " AND pfd.traite = 0";
  604. $sql .= " AND pfd.ext_payment_id IS NULL";
  605. $result_sql = $db->query($sql);
  606. if ($result_sql) {
  607. $obj = $db->fetch_object($result_sql);
  608. if ($obj) {
  609. $pending = $obj->amount;
  610. }
  611. } else {
  612. dol_print_error($db);
  613. }
  614. /*
  615. * Buttons
  616. */
  617. print "\n<div class=\"tabsAction\">\n";
  618. $buttonlabel = $langs->trans("MakeWithdrawRequest");
  619. $user_perms = $user->rights->prelevement->bons->creer;
  620. if ($type == 'bank-transfer') {
  621. $buttonlabel = $langs->trans("MakeBankTransferOrder");
  622. $user_perms = $user->rights->paymentbybanktransfer->create;
  623. }
  624. // Add a transfer request
  625. if ($object->statut > $object::STATUS_DRAFT && $object->paye == 0 && $num == 0) {
  626. if ($resteapayer > 0) {
  627. if ($user_perms) {
  628. $remaintopaylesspendingdebit = $resteapayer - $pending;
  629. print '<form method="POST" action="">';
  630. print '<input type="hidden" name="token" value="'.newToken().'" />';
  631. print '<input type="hidden" name="id" value="'.$object->id.'" />';
  632. print '<input type="hidden" name="type" value="'.$type.'" />';
  633. print '<input type="hidden" name="action" value="new" />';
  634. print '<label for="withdraw_request_amount">'.$langs->trans('BankTransferAmount').' </label>';
  635. print '<input type="text" id="withdraw_request_amount" name="withdraw_request_amount" value="'.$remaintopaylesspendingdebit.'" size="9" />';
  636. print '<input type="submit" class="butAction" value="'.$buttonlabel.'" />';
  637. print '</form>';
  638. } else {
  639. print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->trans("NotEnoughPermissions")).'">'.$buttonlabel.'</a>';
  640. }
  641. } else {
  642. print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->trans("AmountMustBePositive")).'">'.$buttonlabel.'</a>';
  643. }
  644. } else {
  645. if ($num == 0) {
  646. if ($object->statut > $object::STATUS_DRAFT) {
  647. print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->trans("AlreadyPaid")).'">'.$buttonlabel.'</a>';
  648. } else {
  649. print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->trans("Draft")).'">'.$buttonlabel.'</a>';
  650. }
  651. } else {
  652. print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->trans("RequestAlreadyDone")).'">'.$buttonlabel.'</a>';
  653. }
  654. }
  655. print "</div><br>\n";
  656. if ($type == 'bank-transfer') {
  657. print '<div class="opacitymedium">'.$langs->trans("DoCreditTransferBeforePayments").'</div><br>';
  658. } else {
  659. print '<div class="opacitymedium">'.$langs->trans("DoStandingOrdersBeforePayments").'</div><br>';
  660. }
  661. /*
  662. * Withdrawals
  663. */
  664. print '<div class="div-table-responsive-no-min">';
  665. print '<table class="noborder centpercent">';
  666. print '<tr class="liste_titre">';
  667. print '<td class="left">'.$langs->trans("DateRequest").'</td>';
  668. print '<td class="center">'.$langs->trans("User").'</td>';
  669. print '<td class="center">'.$langs->trans("Amount").'</td>';
  670. if ($type == 'bank-transfer') {
  671. print '<td class="center">'.$langs->trans("BankTransferReceipt").'</td>';
  672. } else {
  673. print '<td class="center">'.$langs->trans("WithdrawalReceipt").'</td>';
  674. }
  675. print '<td>&nbsp;</td>';
  676. print '<td class="center">'.$langs->trans("DateProcess").'</td>';
  677. print '<td>&nbsp;</td>';
  678. print '</tr>';
  679. $sql = "SELECT pfd.rowid, pfd.traite, pfd.date_demande as date_demande,";
  680. $sql .= " pfd.date_traite as date_traite, pfd.amount,";
  681. $sql .= " u.rowid as user_id, u.email, u.lastname, u.firstname, u.login, u.statut as user_status";
  682. $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_facture_demande as pfd";
  683. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u on pfd.fk_user_demande = u.rowid";
  684. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."prelevement_bons as pb ON pb.rowid = pfd.fk_prelevement_bons";
  685. if ($type == 'bank-transfer') {
  686. $sql .= " WHERE fk_facture_fourn = ".((int) $object->id);
  687. } else {
  688. $sql .= " WHERE fk_facture = ".((int) $object->id);
  689. }
  690. $sql .= " AND pfd.traite = 0";
  691. $sql .= " AND pfd.ext_payment_id IS NULL";
  692. $sql .= " ORDER BY pfd.date_demande DESC";
  693. $result_sql = $db->query($sql);
  694. $num = 0;
  695. if ($result_sql) {
  696. $i = 0;
  697. $tmpuser = new User($db);
  698. $num = $db->num_rows($result);
  699. while ($i < $num) {
  700. $obj = $db->fetch_object($result_sql);
  701. $tmpuser->id = $obj->user_id;
  702. $tmpuser->login = $obj->login;
  703. $tmpuser->ref = $obj->login;
  704. $tmpuser->email = $obj->email;
  705. $tmpuser->lastname = $obj->lastname;
  706. $tmpuser->firstname = $obj->firstname;
  707. $tmpuser->statut = $obj->user_status;
  708. print '<tr class="oddeven">';
  709. print '<td class="left">'.dol_print_date($db->jdate($obj->date_demande), 'dayhour')."</td>\n";
  710. print '<td align="center">';
  711. print $tmpuser->getNomUrl(1, '', 0, 0, 0, 0, 'login');
  712. print '</td>';
  713. print '<td class="center">'.price($obj->amount).'</td>';
  714. print '<td align="center">-</td>';
  715. print '<td>&nbsp;</td>';
  716. print '<td class="center">'.$langs->trans("OrderWaiting").'</td>';
  717. print '<td class="right">';
  718. print '<a href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&action=delete&token='.newToken().'&did='.$obj->rowid.'&type='.$type.'">';
  719. print img_delete();
  720. print '</a></td>';
  721. print "</tr>\n";
  722. $i++;
  723. }
  724. $db->free($result_sql);
  725. } else {
  726. dol_print_error($db);
  727. }
  728. // Past requests
  729. $sql = "SELECT pfd.rowid, pfd.traite, pfd.date_demande, pfd.date_traite, pfd.fk_prelevement_bons, pfd.amount,";
  730. $sql .= " pb.ref,";
  731. $sql .= " u.rowid as user_id, u.email, u.lastname, u.firstname, u.login, u.statut as user_status";
  732. $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_facture_demande as pfd";
  733. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u on pfd.fk_user_demande = u.rowid";
  734. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."prelevement_bons as pb ON pb.rowid = pfd.fk_prelevement_bons";
  735. if ($type == 'bank-transfer') {
  736. $sql .= " WHERE fk_facture_fourn = ".((int) $object->id);
  737. } else {
  738. $sql .= " WHERE fk_facture = ".((int) $object->id);
  739. }
  740. $sql .= " AND pfd.traite = 1";
  741. $sql .= " AND pfd.ext_payment_id IS NULL";
  742. $sql .= " ORDER BY pfd.date_demande DESC";
  743. $result = $db->query($sql);
  744. if ($result) {
  745. $num = $db->num_rows($result);
  746. $numclosed = $num;
  747. $i = 0;
  748. $tmpuser = new User($db);
  749. while ($i < $num) {
  750. $obj = $db->fetch_object($result);
  751. $tmpuser->id = $obj->user_id;
  752. $tmpuser->login = $obj->login;
  753. $tmpuser->ref = $obj->login;
  754. $tmpuser->email = $obj->email;
  755. $tmpuser->lastname = $obj->lastname;
  756. $tmpuser->firstname = $obj->firstname;
  757. $tmpuser->statut = $obj->user_status;
  758. print '<tr class="oddeven">';
  759. print '<td class="left">'.dol_print_date($db->jdate($obj->date_demande), 'day')."</td>\n";
  760. print '<td align="center">';
  761. print $tmpuser->getNomUrl(1, '', 0, 0, 0, 0, 'login');
  762. print '</td>';
  763. print '<td class="center">'.price($obj->amount).'</td>';
  764. print '<td class="center">';
  765. if ($obj->fk_prelevement_bons > 0) {
  766. $withdrawreceipt = new BonPrelevement($db);
  767. $withdrawreceipt->id = $obj->fk_prelevement_bons;
  768. $withdrawreceipt->ref = $obj->ref;
  769. print $withdrawreceipt->getNomUrl(1);
  770. }
  771. print "</td>\n";
  772. print '<td>&nbsp;</td>';
  773. print '<td class="center">'.dol_print_date($db->jdate($obj->date_traite), 'day')."</td>\n";
  774. print '<td>&nbsp;</td>';
  775. print "</tr>\n";
  776. $i++;
  777. }
  778. if (!$numopen && !$numclosed) {
  779. print '<tr class="oddeven"><td colspan="7"><span class="opacitymedium">'.$langs->trans("None").'</span></td></tr>';
  780. }
  781. $db->free($result);
  782. } else {
  783. dol_print_error($db);
  784. }
  785. print "</table>";
  786. print '</div>';
  787. }
  788. // End of page
  789. llxFooter();
  790. $db->close();