paiement.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. <?php
  2. /* Copyright (C) 2003-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) 2004 Christophe Combelles <ccomb@free.fr>
  6. * Copyright (C) 2005 Marc Barilley / Ocebo <marc@ocebo.com>
  7. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
  8. * Copyright (C) 2014 Teddy Andreotti <125155@supinfo.com>
  9. * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
  10. * Copyright (C) 2015 Juanjo Menent <jmenent@2byte.es>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. /**
  26. * \file htdocs/fourn/facture/paiement.php
  27. * \ingroup fournisseur,facture
  28. * \brief Payment page for suppliers invoices
  29. */
  30. require '../../main.inc.php';
  31. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  33. require_once DOL_DOCUMENT_ROOT.'/fourn/class/paiementfourn.class.php';
  34. $langs->load('companies');
  35. $langs->load('bills');
  36. $langs->load('banks');
  37. $langs->load('compta');
  38. $action = GETPOST('action','alpha');
  39. $confirm = GETPOST('confirm');
  40. $facid=GETPOST('facid','int');
  41. $socid=GETPOST('socid','int');
  42. $accountid = GETPOST('accountid');
  43. $search_ref=GETPOST("search_ref","int");
  44. $search_account=GETPOST("search_account","int");
  45. $search_paymenttype=GETPOST("search_paymenttype");
  46. $search_amount=GETPOST("search_amount",'alpha'); // alpha because we must be able to search on "< x"
  47. $search_company=GETPOST("search_company",'alpha');
  48. $search_payment_num=GETPOST('search_payment_num','alpha');
  49. $sortfield = GETPOST("sortfield",'alpha');
  50. $sortorder = GETPOST("sortorder",'alpha');
  51. $page = GETPOST("page",'int');
  52. if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
  53. $offset = $conf->liste_limit * $page;
  54. $pageprev = $page - 1;
  55. $pagenext = $page + 1;
  56. $limit = GETPOST('limit')?GETPOST('limit','int'):$conf->liste_limit;
  57. if (! $sortorder) $sortorder="DESC";
  58. if (! $sortfield) $sortfield="p.rowid";
  59. $optioncss = GETPOST('optioncss','alpha');
  60. $amounts = array();array();
  61. $amountsresttopay=array();
  62. $addwarning=0;
  63. $multicurrency_amounts=array();
  64. $multicurrency_amountsresttopay=array();
  65. // Security check
  66. if ($user->societe_id > 0)
  67. {
  68. $socid = $user->societe_id;
  69. }
  70. // Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array
  71. $hookmanager->initHooks(array('paymentsupplier'));
  72. $extrafields = new ExtraFields($db);
  73. // fetch optionals attributes and labels
  74. $extralabels = $extrafields->fetch_name_optionals_label('paymentsupplier');
  75. $search_array_options=$extrafields->getOptionalsFromPost($extralabels,'','search_');
  76. $arrayfields=array();
  77. /*
  78. * Actions
  79. */
  80. if (GETPOST("button_removefilter_x") || GETPOST("button_removefilter.x") || GETPOST("button_removefilter")) // All tests are required to be compatible with all browsers
  81. {
  82. $search_ref="";
  83. $search_account="";
  84. $search_amount="";
  85. $search_paymenttype="";
  86. $search_payment_num="";
  87. $search_company="";
  88. $day='';
  89. $year='';
  90. $month='';
  91. $search_array_options=array();
  92. }
  93. $parameters=array('socid'=>$socid);
  94. $reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
  95. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  96. if (empty($reshook))
  97. {
  98. if ($action == 'add_paiement' || ($action == 'confirm_paiement' && $confirm=='yes'))
  99. {
  100. $error = 0;
  101. $datepaye = dol_mktime(12, 0, 0, GETPOST('remonth'), GETPOST('reday'), GETPOST('reyear'));
  102. $paiement_id = 0;
  103. $totalpayment = 0;
  104. $atleastonepaymentnotnull = 0;
  105. // Generate payment array and check if there is payment higher than invoice and payment date before invoice date
  106. $tmpinvoice=new FactureFournisseur($db);
  107. foreach ($_POST as $key => $value)
  108. {
  109. if (substr($key,0,7) == 'amount_')
  110. {
  111. $cursorfacid = substr($key,7);
  112. $amounts[$cursorfacid] = price2num(trim(GETPOST($key)));
  113. $totalpayment = $totalpayment + $amounts[$cursorfacid];
  114. if (! empty($amounts[$cursorfacid])) $atleastonepaymentnotnull++;
  115. $result=$tmpinvoice->fetch($cursorfacid);
  116. if ($result <= 0) dol_print_error($db);
  117. $amountsresttopay[$cursorfacid]=price2num($tmpinvoice->total_ttc - $tmpinvoice->getSommePaiement());
  118. if ($amounts[$cursorfacid])
  119. {
  120. // Check amount
  121. if ($amounts[$cursorfacid] && (abs($amounts[$cursorfacid]) > abs($amountsresttopay[$cursorfacid])))
  122. {
  123. $addwarning=1;
  124. $formquestion['text'] = img_warning($langs->trans("PaymentHigherThanReminderToPaySupplier")).' '.$langs->trans("HelpPaymentHigherThanReminderToPaySupplier");
  125. }
  126. // Check date
  127. if ($datepaye && ($datepaye < $tmpinvoice->date))
  128. {
  129. $langs->load("errors");
  130. //$error++;
  131. setEventMessages($langs->transnoentities("WarningPaymentDateLowerThanInvoiceDate", dol_print_date($datepaye,'day'), dol_print_date($tmpinvoice->date, 'day'), $tmpinvoice->ref), null, 'warnings');
  132. }
  133. }
  134. $formquestion[$i++]=array('type' => 'hidden','name' => $key, 'value' => $_POST[$key]);
  135. }
  136. elseif (substr($key,0,21) == 'multicurrency_amount_')
  137. {
  138. $cursorfacid = substr($key,21);
  139. $multicurrency_amounts[$cursorfacid] = price2num(trim(GETPOST($key)));
  140. $multicurrency_totalpayment += $multicurrency_amounts[$cursorfacid];
  141. if (! empty($multicurrency_amounts[$cursorfacid])) $atleastonepaymentnotnull++;
  142. $result=$tmpinvoice->fetch($cursorfacid);
  143. if ($result <= 0) dol_print_error($db);
  144. $multicurrency_amountsresttopay[$cursorfacid]=price2num($tmpinvoice->total_ttc - $tmpinvoice->getSommePaiement(1));
  145. if ($multicurrency_amounts[$cursorfacid])
  146. {
  147. // Check amount
  148. if ($multicurrency_amounts[$cursorfacid] && (abs($multicurrency_amounts[$cursorfacid]) > abs($multicurrency_amountsresttopay[$cursorfacid])))
  149. {
  150. $addwarning=1;
  151. $formquestion['text'] = img_warning($langs->trans("PaymentHigherThanReminderToPaySupplier")).' '.$langs->trans("HelpPaymentHigherThanReminderToPaySupplier");
  152. }
  153. // Check date
  154. if ($datepaye && ($datepaye < $tmpinvoice->date))
  155. {
  156. $langs->load("errors");
  157. //$error++;
  158. setEventMessages($langs->transnoentities("WarningPaymentDateLowerThanInvoiceDate", dol_print_date($datepaye,'day'), dol_print_date($tmpinvoice->date, 'day'), $tmpinvoice->ref), null, 'warnings');
  159. }
  160. }
  161. $formquestion[$i++]=array('type' => 'hidden','name' => $key, 'value' => GETPOST($key, 'int'));
  162. }
  163. }
  164. // Check parameters
  165. if ($_POST['paiementid'] <= 0)
  166. {
  167. setEventMessages($langs->transnoentities('ErrorFieldRequired',$langs->transnoentities('PaymentMode')), null, 'errors');
  168. $error++;
  169. }
  170. if (! empty($conf->banque->enabled))
  171. {
  172. // If bank module is on, account is required to enter a payment
  173. if (GETPOST('accountid') <= 0)
  174. {
  175. setEventMessages($langs->transnoentities('ErrorFieldRequired',$langs->transnoentities('AccountToCredit')), null, 'errors');
  176. $error++;
  177. }
  178. }
  179. if (empty($totalpayment) && empty($multicurrency_totalpayment) && empty($atleastonepaymentnotnull))
  180. {
  181. setEventMessages($langs->transnoentities('ErrorFieldRequired',$langs->trans('PaymentAmount')), null, 'errors');
  182. $error++;
  183. }
  184. if (empty($datepaye))
  185. {
  186. setEventMessages($langs->transnoentities('ErrorFieldRequired',$langs->transnoentities('Date')), null, 'errors');
  187. $error++;
  188. }
  189. // Check if payments in both currency
  190. if ($totalpayment > 0 && $multicurrency_totalpayment > 0)
  191. {
  192. setEventMessages($langs->transnoentities('ErrorPaymentInBothCurrency'), null, 'errors');
  193. $error++;
  194. }
  195. }
  196. /*
  197. * Action add_paiement
  198. */
  199. if ($action == 'add_paiement')
  200. {
  201. if ($error)
  202. {
  203. $action = 'create';
  204. }
  205. // Le reste propre a cette action s'affiche en bas de page.
  206. }
  207. /*
  208. * Action confirm_paiement
  209. */
  210. if ($action == 'confirm_paiement' && $confirm == 'yes')
  211. {
  212. $error=0;
  213. $datepaye = dol_mktime(12, 0, 0, GETPOST('remonth'), GETPOST('reday'), GETPOST('reyear'));
  214. if (! $error)
  215. {
  216. $db->begin();
  217. // Creation de la ligne paiement
  218. $paiement = new PaiementFourn($db);
  219. $paiement->datepaye = $datepaye;
  220. $paiement->amounts = $amounts; // Array of amounts
  221. $paiement->multicurrency_amounts = $multicurrency_amounts;
  222. $paiement->paiementid = $_POST['paiementid'];
  223. $paiement->num_paiement = $_POST['num_paiement'];
  224. $paiement->note = $_POST['comment'];
  225. if (! $error)
  226. {
  227. $paiement_id = $paiement->create($user,(GETPOST('closepaidinvoices')=='on'?1:0));
  228. if ($paiement_id < 0)
  229. {
  230. setEventMessages($paiement->error, $paiement->errors, 'errors');
  231. $error++;
  232. }
  233. }
  234. if (! $error)
  235. {
  236. $result=$paiement->addPaymentToBank($user,'payment_supplier','(SupplierInvoicePayment)',$accountid,'','');
  237. if ($result < 0)
  238. {
  239. setEventMessages($paiement->error, $paiement->errors, 'errors');
  240. $error++;
  241. }
  242. }
  243. if (! $error)
  244. {
  245. $db->commit();
  246. // If payment dispatching on more than one invoice, we keep on summary page, otherwise go on invoice card
  247. $invoiceid=0;
  248. foreach ($paiement->amounts as $key => $amount)
  249. {
  250. $facid = $key;
  251. if (is_numeric($amount) && $amount <> 0)
  252. {
  253. if ($invoiceid != 0) $invoiceid=-1; // There is more than one invoice payed by this payment
  254. else $invoiceid=$facid;
  255. }
  256. }
  257. if ($invoiceid > 0) $loc = DOL_URL_ROOT.'/fourn/facture/card.php?facid='.$invoiceid;
  258. else $loc = DOL_URL_ROOT.'/fourn/paiement/card.php?id='.$paiement_id;
  259. header('Location: '.$loc);
  260. exit;
  261. }
  262. else
  263. {
  264. $db->rollback();
  265. }
  266. }
  267. }
  268. }
  269. /*
  270. * View
  271. */
  272. $supplierstatic=new Societe($db);
  273. $invoicesupplierstatic = new FactureFournisseur($db);
  274. llxHeader('',$langs->trans('ListPayment'));
  275. $form=new Form($db);
  276. if ($action == 'create' || $action == 'confirm_paiement' || $action == 'add_paiement')
  277. {
  278. $object = new FactureFournisseur($db);
  279. $object->fetch($facid);
  280. $datefacture=dol_mktime(12, 0, 0, GETPOST('remonth'), GETPOST('reday'), GETPOST('reyear'));
  281. $dateinvoice=($datefacture==''?(empty($conf->global->MAIN_AUTOFILL_DATE)?-1:''):$datefacture);
  282. $sql = 'SELECT s.nom as name, s.rowid as socid,';
  283. $sql.= ' f.rowid, f.ref, f.ref_supplier, f.amount, f.total_ttc as total, f.fk_mode_reglement, f.fk_account';
  284. if (!$user->rights->societe->client->voir && !$socid) $sql .= ", sc.fk_soc, sc.fk_user ";
  285. $sql.= ' FROM '.MAIN_DB_PREFIX.'societe as s, '.MAIN_DB_PREFIX.'facture_fourn as f';
  286. if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  287. $sql.= ' WHERE f.fk_soc = s.rowid';
  288. $sql.= ' AND f.rowid = '.$facid;
  289. if (!$user->rights->societe->client->voir && !$socid) $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id;
  290. $resql = $db->query($sql);
  291. if ($resql)
  292. {
  293. $num = $db->num_rows($resql);
  294. if ($num)
  295. {
  296. $obj = $db->fetch_object($resql);
  297. $total = $obj->total;
  298. print load_fiche_titre($langs->trans('DoPayment'));
  299. print '<form id="payment_form" name="addpaiement" action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  300. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  301. print '<input type="hidden" name="action" value="add_paiement">';
  302. print '<input type="hidden" name="facid" value="'.$facid.'">';
  303. print '<input type="hidden" name="ref_supplier" value="'.$obj->ref_supplier.'">';
  304. print '<input type="hidden" name="socid" value="'.$obj->socid.'">';
  305. print '<input type="hidden" name="societe" value="'.$obj->name.'">';
  306. dol_fiche_head(null);
  307. print '<table class="border" width="100%">';
  308. print '<tr><td class="fieldrequired titlefieldcreate">'.$langs->trans('Company').'</td><td>';
  309. $supplierstatic->id=$obj->socid;
  310. $supplierstatic->name=$obj->name;
  311. print $supplierstatic->getNomUrl(1,'supplier');
  312. print '</td></tr>';
  313. print '<tr><td class="fieldrequired">'.$langs->trans('Date').'</td><td>';
  314. $form->select_date($dateinvoice,'','','','',"addpaiement",1,1,0,0,'','',$object->date);
  315. print '</td></tr>';
  316. print '<tr><td class="fieldrequired">'.$langs->trans('PaymentMode').'</td><td>';
  317. $form->select_types_paiements(empty($_POST['paiementid'])?$obj->fk_mode_reglement:$_POST['paiementid'],'paiementid');
  318. print '</td>';
  319. if (! empty($conf->banque->enabled))
  320. {
  321. print '<tr><td class="fieldrequired">'.$langs->trans('Account').'</td><td>';
  322. $form->select_comptes(empty($accountid)?$obj->fk_account:$accountid,'accountid',0,'',2);
  323. print '</td></tr>';
  324. }
  325. else
  326. {
  327. print '<tr><td>&nbsp;</td></tr>';
  328. }
  329. print '<tr><td>'.$langs->trans('Numero').'</td><td><input name="num_paiement" type="text" value="'.(empty($_POST['num_paiement'])?'':$_POST['num_paiement']).'"></td></tr>';
  330. print '<tr><td>'.$langs->trans('Comments').'</td>';
  331. print '<td class="tdtop">';
  332. print '<textarea name="comment" wrap="soft" class="quatrevingtpercent" rows="'.ROWS_3.'">'.(empty($_POST['comment'])?'':$_POST['comment']).'</textarea></td></tr>';
  333. print '</table>';
  334. dol_fiche_end();
  335. $parameters=array('facid'=>$facid, 'ref'=>$ref, 'objcanvas'=>$objcanvas);
  336. $reshook=$hookmanager->executeHooks('paymentsupplierinvoices',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
  337. $error=$hookmanager->error; $errors=$hookmanager->errors;
  338. if (empty($reshook))
  339. {
  340. /*
  341. * Autres factures impayees
  342. */
  343. $sql = 'SELECT f.rowid as facid, f.ref, f.ref_supplier, f.total_ht, f.total_ttc, f.multicurrency_total_ttc, f.datef as df,';
  344. $sql.= ' SUM(pf.amount) as am, SUM(pf.multicurrency_amount) as multicurrency_am';
  345. $sql.= ' FROM '.MAIN_DB_PREFIX.'facture_fourn as f';
  346. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'paiementfourn_facturefourn as pf ON pf.fk_facturefourn = f.rowid';
  347. $sql.= " WHERE f.entity = ".$conf->entity;
  348. $sql.= ' AND f.fk_soc = '.$object->socid;
  349. $sql.= ' AND f.paye = 0';
  350. $sql.= ' AND f.fk_statut = 1'; // Statut=0 => non validee, Statut=2 => annulee
  351. $sql.= ' GROUP BY f.rowid, f.ref, f.ref_supplier, f.total_ht, f.total_ttc, f.multicurrency_total_ttc, f.datef';
  352. $resql = $db->query($sql);
  353. if ($resql)
  354. {
  355. $num = $db->num_rows($resql);
  356. if ($num > 0)
  357. {
  358. $i = 0;
  359. print '<br>';
  360. if (!empty($conf->use_javascript_ajax)){
  361. //Add js for AutoFill
  362. print "\n".'<script type="text/javascript" language="javascript">';
  363. print ' $(document).ready(function () {';
  364. print ' $(".AutoFillAmout").on(\'click touchstart\', function(){
  365. $("input[name="+$(this).data(\'rowname\')+"]").val($(this).data("value"));
  366. });';
  367. print ' });'."\n";
  368. print ' </script>'."\n";
  369. }
  370. print '<div class="div-table-responsive-no-min">';
  371. print '<table class="tagtable liste'.($moreforfilter?" listwithfilterbefore":"").'">'."\n";
  372. print '<tr class="liste_titre">';
  373. print '<td>'.$langs->trans('Invoice').'</td>';
  374. print '<td>'.$langs->trans('RefSupplier').'</td>';
  375. print '<td align="center">'.$langs->trans('Date').'</td>';
  376. if (!empty($conf->multicurrency->enabled)) print '<td>'.$langs->trans('Currency').'</td>';
  377. if (!empty($conf->multicurrency->enabled)) print '<td align="right">'.$langs->trans('MulticurrencyAmountTTC').'</td>';
  378. if (!empty($conf->multicurrency->enabled)) print '<td align="right">'.$langs->trans('MulticurrencyAlreadyPaid').'</td>';
  379. if (!empty($conf->multicurrency->enabled)) print '<td align="right">'.$langs->trans('MulticurrencyRemainderToPay').'</td>';
  380. print '<td align="right">'.$langs->trans('AmountTTC').'</td>';
  381. print '<td align="right">'.$langs->trans('AlreadyPaid').'</td>';
  382. print '<td align="right">'.$langs->trans('RemainderToPay').'</td>';
  383. print '<td align="center">'.$langs->trans('PaymentAmount').'</td>';
  384. if (!empty($conf->multicurrency->enabled)) print '<td align="center">'.$langs->trans('MulticurrencyPaymentAmount').'</td>';
  385. print '</tr>';
  386. $var=True;
  387. $total=0;
  388. $total_ttc=0;
  389. $totalrecu=0;
  390. while ($i < $num)
  391. {
  392. $objp = $db->fetch_object($resql);
  393. print '<tr class="oddeven">';
  394. // Ref
  395. print '<td>';
  396. $invoicesupplierstatic->ref=$objp->ref;
  397. $invoicesupplierstatic->id=$objp->facid;
  398. print $invoicesupplierstatic->getNomUrl(1);
  399. print '</td>';
  400. // Ref supplier
  401. print '<td>'.$objp->ref_supplier.'</td>';
  402. // Date
  403. if ($objp->df > 0 )
  404. {
  405. print '<td align="center">';
  406. print dol_print_date($db->jdate($objp->df), 'day').'</td>';
  407. }
  408. else
  409. {
  410. print '<td align="center"><b>!!!</b></td>';
  411. }
  412. // Multicurrency
  413. if (!empty($conf->multicurrency->enabled))
  414. {
  415. // Currency
  416. print '<td align="center">'.$objp->multicurrency_code."</td>\n";
  417. print '<td align="right">';
  418. if ($objp->multicurrency_code && $objp->multicurrency_code != $conf->currency)
  419. {
  420. print price($objp->multicurrency_total_ttc);
  421. }
  422. print '</td>';
  423. print '<td align="right">';
  424. if ($objp->multicurrency_code && $objp->multicurrency_code != $conf->currency)
  425. {
  426. print price($objp->multicurrency_am);
  427. }
  428. print '</td>';
  429. print '<td align="right">';
  430. if ($objp->multicurrency_code && $objp->multicurrency_code != $conf->currency)
  431. {
  432. print price($objp->multicurrency_total_ttc - $objp->multicurrency_am);
  433. }
  434. print '</td>';
  435. }
  436. print '<td align="right">'.price($objp->total_ttc).'</td>';
  437. print '<td align="right">'.price($objp->am).'</td>';
  438. print '<td align="right">'.price($objp->total_ttc - $objp->am).'</td>';
  439. print '<td align="center">';
  440. $namef = 'amount_'.$objp->facid;
  441. if (!empty($conf->use_javascript_ajax))
  442. print img_picto("Auto fill",'rightarrow', "class='AutoFillAmout' data-rowname='".$namef."' data-value='".($objp->total_ttc - $objp->am)."'");
  443. print '<input type="text" size="8" name="'.$namef.'" value="'.GETPOST($namef).'">';
  444. print "</td>";
  445. // Multicurrency
  446. if (!empty($conf->multicurrency->enabled))
  447. {
  448. print '<td align="center">';
  449. if ($objp->multicurrency_code && $objp->multicurrency_code != $conf->currency)
  450. {
  451. $namef = 'multicurrency_amount_'.$objp->facid;
  452. if (!empty($conf->use_javascript_ajax))
  453. print img_picto("Auto fill",'rightarrow', "class='AutoFillAmout' data-rowname='".$namef."' data-value='".($objp->multicurrency_total_ttc - $objp->multicurrency_am)."'");
  454. print '<input type="text" size="8" class="multicurrency_amount" name="'.$namef.'" value="'.GETPOST($namef).'">';
  455. }
  456. print "</td>";
  457. }
  458. print "</tr>\n";
  459. $total+=$objp->total_ht;
  460. $total_ttc+=$objp->total_ttc;
  461. $totalrecu+=$objp->am;
  462. $i++;
  463. }
  464. if ($i > 1)
  465. {
  466. // Print total
  467. print '<tr class="liste_total">';
  468. print '<td colspan="3" align="left">'.$langs->trans('TotalTTC').':</td>';
  469. if (!empty($conf->multicurrency->enabled)) print '<td>&nbsp;</td>';
  470. if (!empty($conf->multicurrency->enabled)) print '<td>&nbsp;</td>';
  471. if (!empty($conf->multicurrency->enabled)) print '<td>&nbsp;</td>';
  472. if (!empty($conf->multicurrency->enabled)) print '<td>&nbsp;</td>';
  473. print '<td align="right"><b>'.price($total_ttc).'</b></td>';
  474. print '<td align="right"><b>'.price($totalrecu).'</b></td>';
  475. print '<td align="right"><b>'.price($total_ttc - $totalrecu).'</b></td>';
  476. print '<td align="center">&nbsp;</td>';
  477. if (!empty($conf->multicurrency->enabled)) print '<td>&nbsp;</td>';
  478. print "</tr>\n";
  479. }
  480. print "</table>\n";
  481. print "</div>";
  482. }
  483. $db->free($resql);
  484. }
  485. else
  486. {
  487. dol_print_error($db);
  488. }
  489. }
  490. // Bouton Enregistrer
  491. if ($action != 'add_paiement')
  492. {
  493. print '<br><div class="center"><input type="checkbox" checked name="closepaidinvoices"> '.$langs->trans("ClosePaidInvoicesAutomatically");
  494. print '<br><input type="submit" class="button" value="'.$langs->trans('ToMakePayment').'"></div>';
  495. }
  496. // Form to confirm payment
  497. if ($action == 'add_paiement')
  498. {
  499. $preselectedchoice=$addwarning?'no':'yes';
  500. print '<br>';
  501. if (!empty($totalpayment)) $text=$langs->trans('ConfirmSupplierPayment',price($totalpayment),$langs->trans("Currency".$conf->currency));
  502. if (!empty($multicurrency_totalpayment))
  503. {
  504. $text.='<br>'.$langs->trans('ConfirmSupplierPayment',price($multicurrency_totalpayment),$langs->trans("paymentInInvoiceCurrency"));
  505. }
  506. if (GETPOST('closepaidinvoices'))
  507. {
  508. $text.='<br>'.$langs->trans("AllCompletelyPayedInvoiceWillBeClosed");
  509. print '<input type="hidden" name="closepaidinvoices" value="'.GETPOST('closepaidinvoices').'">';
  510. }
  511. print $form->formconfirm($_SERVER['PHP_SELF'].'?facid='.$facture->id.'&socid='.$facture->socid.'&type='.$facture->type,$langs->trans('PayedSuppliersPayments'),$text,'confirm_paiement',$formquestion,$preselectedchoice);
  512. }
  513. print '</form>';
  514. }
  515. }
  516. else dol_print_error($db);
  517. }
  518. /*
  519. * Show list
  520. */
  521. if (empty($action))
  522. {
  523. $limit = GETPOST('limit')?GETPOST('limit','int'):$conf->liste_limit;
  524. $sortfield = GETPOST("sortfield",'alpha');
  525. $sortorder = GETPOST("sortorder",'alpha');
  526. $page=GETPOST("page",'int');
  527. if ($page == -1) { $page = 0 ; }
  528. $offset = $limit * $page ;
  529. $pageprev = $page - 1;
  530. $pagenext = $page + 1;
  531. if (! $sortorder) $sortorder='DESC';
  532. if (! $sortfield) $sortfield='p.datep';
  533. $sql = 'SELECT p.rowid as pid, p.datep as dp, p.amount as pamount, p.num_paiement,';
  534. $sql.= ' s.rowid as socid, s.nom as name,';
  535. $sql.= ' c.code as paiement_type, c.libelle as paiement_libelle,';
  536. $sql.= ' ba.rowid as bid, ba.label,';
  537. if (!$user->rights->societe->client->voir) $sql .= ' sc.fk_soc, sc.fk_user,';
  538. $sql.= ' SUM(f.amount)';
  539. $sql.= ' FROM '.MAIN_DB_PREFIX.'paiementfourn AS p';
  540. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'paiementfourn_facturefourn AS pf ON p.rowid=pf.fk_paiementfourn';
  541. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'facture_fourn AS f ON f.rowid=pf.fk_facturefourn';
  542. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement AS c ON p.fk_paiement = c.id';
  543. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe AS s ON s.rowid = f.fk_soc';
  544. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON p.fk_bank = b.rowid';
  545. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank_account as ba ON b.fk_account = ba.rowid';
  546. if (!$user->rights->societe->client->voir) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  547. $sql.= " WHERE f.entity = ".$conf->entity;
  548. if (!$user->rights->societe->client->voir) $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id;
  549. if ($socid > 0) $sql .= ' AND f.fk_soc = '.$socid;
  550. // Search criteria
  551. if ($search_ref) $sql .= natural_search('p.rowid', $search_ref);
  552. if ($search_account > 0) $sql .=" AND b.fk_account=".$search_account;
  553. if ($search_paymenttype != "") $sql .=" AND c.code='".$db->escape($search_paymenttype)."'";
  554. if ($search_payment_num != '') $sql .= natural_search('p.num_paiement', $search_payment_num);
  555. if ($search_amount) $sql .= natural_search('p.amount', $search_amount, 1);
  556. if ($search_company) $sql .= natural_search('s.nom', $search_company);
  557. $sql.= " GROUP BY p.rowid, p.datep, p.amount, p.num_paiement, s.rowid, s.nom, c.code, c.libelle, ba.rowid, ba.label";
  558. if (!$user->rights->societe->client->voir) $sql .= ", sc.fk_soc, sc.fk_user";
  559. $sql.= $db->order($sortfield,$sortorder);
  560. $nbtotalofrecords = '';
  561. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
  562. {
  563. $result = $db->query($sql);
  564. $nbtotalofrecords = $db->num_rows($result);
  565. }
  566. $sql.= $db->plimit($limit+1, $offset);
  567. $resql = $db->query($sql);
  568. if ($resql)
  569. {
  570. $num = $db->num_rows($resql);
  571. $i = 0;
  572. $var=True;
  573. $param='';
  574. if (! empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param.='&contextpage='.$contextpage;
  575. if ($limit > 0 && $limit != $conf->liste_limit) $param.='&limit='.$limit;
  576. if ($search_ref) $param.=($search_ref?"&search_ref=".urlencode($search_ref):"");
  577. if ($search_company) $param.=($search_company?"&search_company=".urlencode($search_company):"");
  578. if ($search_amount != '') $param.=($search_amount?"&search_amount=".urlencode($search_amount):"");
  579. if ($search_payment_num) $param.=($search_payment_num?"&search_payment_num=".urlencode($search_payment_num):"");
  580. if ($optioncss != '') $param.='&optioncss='.$optioncss;
  581. // Add $param from extra fields
  582. foreach ($search_array_options as $key => $val)
  583. {
  584. $crit=$val;
  585. $tmpkey=preg_replace('/search_options_/','',$key);
  586. if ($val != '') $param.='&search_options_'.$tmpkey.'='.urlencode($val);
  587. }
  588. $massactionbutton=$form->selectMassAction('', $massaction == 'presend' ? array() : array('presend'=>$langs->trans("SendByMail"), 'builddoc'=>$langs->trans("PDFMerge")));
  589. print_barre_liste($langs->trans('SupplierPayments'), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, $nbtotalofrecords, 'title_accountancy.png', 0, '', '', $limit);
  590. print '<form method="GET" action="'.$_SERVER["PHP_SELF"].'">';
  591. if ($optioncss != '') print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  592. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  593. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  594. print '<input type="hidden" name="action" value="list">';
  595. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  596. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  597. print '<input type="hidden" name="page" value="'.$page.'">';
  598. $moreforfilter='';
  599. $parameters=array();
  600. $reshook=$hookmanager->executeHooks('printFieldPreListTitle',$parameters); // Note that $action and $object may have been modified by hook
  601. if (empty($reshook)) $moreforfilter .= $hookmanager->resPrint;
  602. else $moreforfilter = $hookmanager->resPrint;
  603. if ($moreforfilter)
  604. {
  605. print '<div class="liste_titre liste_titre_bydiv centpercent">';
  606. print $moreforfilter;
  607. print '</div>';
  608. }
  609. $varpage=empty($contextpage)?$_SERVER["PHP_SELF"]:$contextpage;
  610. $selectedfields=$form->multiSelectArrayWithCheckbox('selectedfields', $arrayfields, $varpage); // This also change content of $arrayfields
  611. print '<div class="div-table-responsive">';
  612. print '<table class="tagtable liste'.($moreforfilter?" listwithfilterbefore":"").'">'."\n";
  613. // Lines for filters fields
  614. print '<tr class="liste_titre_filter">';
  615. print '<td class="liste_titre" align="left">';
  616. print '<input class="flat" type="text" size="4" name="search_ref" value="'.dol_escape_htmltag($search_ref).'">';
  617. print '</td>';
  618. print '<td class="liste_titre">&nbsp;</td>';
  619. print '<td class="liste_titre" align="left">';
  620. print '<input class="flat" type="text" size="6" name="search_company" value="'.dol_escape_htmltag($search_company).'">';
  621. print '</td>';
  622. print '<td class="liste_titre">';
  623. $form->select_types_paiements($search_paymenttype,'search_paymenttype','',2,1,1);
  624. print '</td>';
  625. print '<td class="liste_titre" align="left">';
  626. print '<input class="flat" type="text" size="4" name="search_payment_num" value="'.dol_escape_htmltag($search_payment_num).'">';
  627. print '</td>';
  628. print '<td class="liste_titre">';
  629. $form->select_comptes($search_account,'search_account',0,'',1);
  630. print '</td>';
  631. print '<td class="liste_titre" align="right">';
  632. print '<input class="flat" type="text" size="4" name="search_amount" value="'.dol_escape_htmltag($search_amount).'">';
  633. print '</td>';
  634. print '<td class="liste_titre" align="right">';
  635. $searchpicto=$form->showFilterAndCheckAddButtons(0);
  636. print $searchpicto;
  637. print '</td>';
  638. print "</tr>\n";
  639. print '<tr class="liste_titre">';
  640. print_liste_field_titre($langs->trans('RefPayment'),$_SERVER["PHP_SELF"],'p.rowid','',$param,'',$sortfield,$sortorder);
  641. print_liste_field_titre($langs->trans('Date'),$_SERVER["PHP_SELF"],'dp','',$param,'align="center"',$sortfield,$sortorder);
  642. print_liste_field_titre($langs->trans('ThirdParty'),$_SERVER["PHP_SELF"],'s.nom','',$param,'',$sortfield,$sortorder);
  643. print_liste_field_titre($langs->trans('Type'),$_SERVER["PHP_SELF"],'c.libelle','',$param,'',$sortfield,$sortorder);
  644. print_liste_field_titre($langs->trans("Numero"),$_SERVER["PHP_SELF"],"p.num_paiement","",$param,"",$sortfield,$sortorder);
  645. print_liste_field_titre($langs->trans('Account'),$_SERVER["PHP_SELF"],'ba.label','',$param,'',$sortfield,$sortorder);
  646. print_liste_field_titre($langs->trans('Amount'),$_SERVER["PHP_SELF"],'p.amount','',$param,'align="right"',$sortfield,$sortorder);
  647. //print_liste_field_titre($langs->trans('Invoice'),$_SERVER["PHP_SELF"],'ref_supplier','',$param,'',$sortfield,$sortorder);
  648. print_liste_field_titre('');
  649. print "</tr>\n";
  650. while ($i < min($num,$limit))
  651. {
  652. $objp = $db->fetch_object($resql);
  653. print '<tr class="oddeven">';
  654. // Ref payment
  655. print '<td class="nowrap"><a href="'.DOL_URL_ROOT.'/fourn/paiement/card.php?id='.$objp->pid.'">'.img_object($langs->trans('ShowPayment'),'payment').' '.$objp->pid.'</a></td>';
  656. // Date
  657. print '<td class="nowrap" align="center">'.dol_print_date($db->jdate($objp->dp),'day')."</td>\n";
  658. // Thirdparty
  659. print '<td>';
  660. if ($objp->socid) print '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.$objp->socid.'">'.img_object($langs->trans('ShowCompany'),'company').' '.dol_trunc($objp->name,32).'</a>';
  661. else print '&nbsp;';
  662. print '</td>';
  663. // Type
  664. $payment_type = $langs->trans("PaymentType".$objp->paiement_type)!=("PaymentType".$objp->paiement_type)?$langs->trans("PaymentType".$objp->paiement_type):$objp->paiement_libelle;
  665. print '<td>'.$payment_type.' '.dol_trunc($objp->num_paiement,32)."</td>\n";
  666. // Payment number
  667. print '<td>'.$objp->num_paiement.'</td>';
  668. print '<td>';
  669. if ($objp->bid) print '<a href="'.DOL_URL_ROOT.'/compta/bank/bankentries.php?account='.$objp->bid.'">'.img_object($langs->trans("ShowAccount"),'account').' '.dol_trunc($objp->label,24).'</a>';
  670. else print '&nbsp;';
  671. print '</td>';
  672. print '<td align="right">'.price($objp->pamount).'</td>';
  673. // Ref invoice
  674. /*$invoicesupplierstatic->ref=$objp->ref_supplier;
  675. $invoicesupplierstatic->id=$objp->facid;
  676. print '<td class="nowrap">';
  677. print $invoicesupplierstatic->getNomUrl(1);
  678. print '</td>';*/
  679. print '<td>&nbsp;</td>';
  680. print '</tr>';
  681. $i++;
  682. }
  683. print "</table>";
  684. print "</div>";
  685. print "</form>\n";
  686. }
  687. else
  688. {
  689. dol_print_error($db);
  690. }
  691. }
  692. llxFooter();
  693. $db->close();