PageRenderTime 56ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/fourn/facture/paiement.php

https://bitbucket.org/speedealing/speedealing
PHP | 505 lines | 387 code | 60 blank | 58 comment | 72 complexity | d7109b03a455329009800fc49bc44647 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  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-2012 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. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/fourn/facture/paiement.php
  24. * \ingroup fournisseur,facture
  25. * \brief Payment page for suppliers invoices
  26. */
  27. require '../../main.inc.php';
  28. require DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php';
  29. require DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  30. require DOL_DOCUMENT_ROOT.'/fourn/class/paiementfourn.class.php';
  31. $langs->load('companies');
  32. $langs->load('bills');
  33. $langs->load('banks');
  34. $facid=GETPOST('facid','int');
  35. $action=GETPOST('action');
  36. $socid=GETPOST('socid','int');
  37. $sortfield = GETPOST("sortfield",'alpha');
  38. $sortorder = GETPOST("sortorder",'alpha');
  39. $page = GETPOST("page",'int');
  40. if ($page == -1) { $page = 0; }
  41. $offset = $conf->liste_limit * $page;
  42. $pageprev = $page - 1;
  43. $pagenext = $page + 1;
  44. $limit = $conf->liste_limit;
  45. if (! $sortorder) $sortorder="DESC";
  46. if (! $sortfield) $sortfield="p.rowid";
  47. $amounts = array();
  48. // Security check
  49. if ($user->societe_id > 0)
  50. {
  51. $socid = $user->societe_id;
  52. }
  53. /*
  54. * Actions
  55. */
  56. if ($action == 'add_paiement')
  57. {
  58. $error = 0;
  59. $datepaye = dol_mktime(12, 0, 0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']);
  60. $paiement_id = 0;
  61. $total = 0;
  62. // Genere tableau des montants amounts
  63. foreach ($_POST as $key => $value)
  64. {
  65. if (substr($key,0,7) == 'amount_')
  66. {
  67. $other_facid = substr($key,7);
  68. $amounts[$other_facid] = price2num(GETPOST($key));
  69. $total = $total + $amounts[$other_facid];
  70. }
  71. }
  72. // Effectue les verifications des parametres
  73. if ($_POST['paiementid'] <= 0)
  74. {
  75. $mesg = '<div class="error">'.$langs->trans('ErrorFieldRequired',$langs->transnoentities('PaymentMode')).'</div>';
  76. $error++;
  77. }
  78. if (! empty($conf->banque->enabled))
  79. {
  80. // Si module bank actif, un compte est obligatoire lors de la saisie
  81. // d'un paiement
  82. if (! $_POST['accountid'])
  83. {
  84. $mesg = '<div class="error">'.$langs->trans('ErrorFieldRequired',$langs->transnoentities('AccountToCredit')).'</div>';
  85. $error++;
  86. }
  87. }
  88. if ($total == 0)
  89. {
  90. $mesg = '<div class="error">'.$langs->transnoentities('ErrorFieldRequired',$langs->trans('PaymentAmount')).'</div>';
  91. $error++;
  92. }
  93. if (empty($datepaye))
  94. {
  95. $mesg = '<div class="error">'.$langs->trans('ErrorFieldRequired',$langs->transnoentities('Date')).'</div>';
  96. $error++;
  97. }
  98. if (! $error)
  99. {
  100. $db->begin();
  101. // Creation de la ligne paiement
  102. $paiement = new PaiementFourn($db);
  103. $paiement->datepaye = $datepaye;
  104. $paiement->amounts = $amounts; // Array of amounts
  105. $paiement->paiementid = $_POST['paiementid'];
  106. $paiement->num_paiement = $_POST['num_paiement'];
  107. $paiement->note = $_POST['comment'];
  108. if (! $error)
  109. {
  110. $paiement_id = $paiement->create($user,(GETPOST('closepaidinvoices')=='on'?1:0));
  111. if ($paiement_id < 0)
  112. {
  113. $errmsg='<div class="error">'.$paiement->error.'</div>';
  114. $error++;
  115. }
  116. }
  117. if (! $error)
  118. {
  119. $result=$paiement->addPaymentToBank($user,'payment_supplier','(SupplierInvoicePayment)',$_POST['accountid'],'','');
  120. if ($result < 0)
  121. {
  122. $errmsg='<div class="error">'.$paiement->error.'</div>';
  123. $error++;
  124. }
  125. }
  126. if (! $error)
  127. {
  128. $db->commit();
  129. // If payment dispatching on more than one invoice, we keep on summary page, otherwise go on invoice card
  130. $invoiceid=0;
  131. foreach ($paiement->amounts as $key => $amount)
  132. {
  133. $facid = $key;
  134. if (is_numeric($amount) && $amount <> 0)
  135. {
  136. if ($invoiceid != 0) $invoiceid=-1; // There is more than one invoice payed by this payment
  137. else $invoiceid=$facid;
  138. }
  139. }
  140. if ($invoiceid > 0) $loc = DOL_URL_ROOT.'/fourn/facture/fiche.php?facid='.$invoiceid;
  141. else $loc = DOL_URL_ROOT.'/fourn/paiement/fiche.php?id='.$paiement_id;
  142. header('Location: '.$loc);
  143. exit;
  144. }
  145. else
  146. {
  147. $db->rollback();
  148. }
  149. }
  150. }
  151. /*
  152. * View
  153. */
  154. $supplierstatic=new Societe($db);
  155. $invoicesupplierstatic = new FactureFournisseur($db);
  156. llxHeader();
  157. $form=new Form($db);
  158. if ($action == 'create' || $action == 'add_paiement')
  159. {
  160. $facture = new FactureFournisseur($db);
  161. $facture->fetch($facid);
  162. $datefacture=dol_mktime(12, 0, 0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']);
  163. $dateinvoice=($datefacture==''?(empty($conf->global->MAIN_AUTOFILL_DATE)?-1:0):$datefacture);
  164. $sql = 'SELECT s.nom, s.rowid as socid,';
  165. $sql.= ' f.rowid as ref, f.facnumber, f.amount, f.total_ttc as total';
  166. if (!$user->rights->societe->client->voir && !$socid) $sql .= ", sc.fk_soc, sc.fk_user ";
  167. $sql.= ' FROM '.MAIN_DB_PREFIX.'societe as s, '.MAIN_DB_PREFIX.'facture_fourn as f';
  168. if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  169. $sql.= ' WHERE f.fk_soc = s.rowid';
  170. $sql.= ' AND f.rowid = '.$facid;
  171. if (!$user->rights->societe->client->voir && !$socid) $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id;
  172. $resql = $db->query($sql);
  173. if ($resql)
  174. {
  175. $num = $db->num_rows($resql);
  176. if ($num)
  177. {
  178. $obj = $db->fetch_object($resql);
  179. $total = $obj->total;
  180. print_fiche_titre($langs->trans('DoPayment'));
  181. if ($mesg) dol_htmloutput_mesg($mesg);
  182. if ($errmsg) dol_htmloutput_errors($errmsg);
  183. print '<form name="addpaiement" action="paiement.php" method="post">';
  184. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  185. print '<input type="hidden" name="action" value="add_paiement">';
  186. print '<input type="hidden" name="facid" value="'.$facid.'">';
  187. print '<input type="hidden" name="facnumber" value="'.$obj->facnumber.'">';
  188. print '<input type="hidden" name="socid" value="'.$obj->socid.'">';
  189. print '<input type="hidden" name="societe" value="'.$obj->nom.'">';
  190. print '<table class="border" width="100%">';
  191. print '<tr class="liste_titre"><td colspan="3">'.$langs->trans('Payment').'</td>';
  192. print '<tr><td>'.$langs->trans('Company').'</td><td colspan="2">';
  193. $supplierstatic->id=$obj->socid;
  194. $supplierstatic->name=$obj->nom;
  195. print $supplierstatic->getNomUrl(1,'supplier');
  196. print '</td></tr>';
  197. print '<tr><td class="fieldrequired">'.$langs->trans('Date').'</td><td>';
  198. $form->select_date($dateinvoice,'','','','',"addpaiement",1,1);
  199. print '</td>';
  200. print '<td>'.$langs->trans('Comments').'</td></tr>';
  201. print '<tr><td class="fieldrequired">'.$langs->trans('PaymentMode').'</td><td>';
  202. $form->select_types_paiements(empty($_POST['paiementid'])?'':$_POST['paiementid'],'paiementid');
  203. print '</td>';
  204. print '<td rowspan="3" valign="top">';
  205. print '<textarea name="comment" wrap="soft" cols="60" rows="'._ROWS_3.'">'.(empty($_POST['comment'])?'':$_POST['comment']).'</textarea></td></tr>';
  206. print '<tr><td>'.$langs->trans('Numero').'</td><td><input name="num_paiement" type="text" value="'.(empty($_POST['num_paiement'])?'':$_POST['num_paiement']).'"></td></tr>';
  207. if (! empty($conf->banque->enabled))
  208. {
  209. print '<tr><td class="fieldrequired">'.$langs->trans('Account').'</td><td>';
  210. $form->select_comptes(empty($_POST['accountid'])?'':$_POST['accountid'],'accountid',0,'',2);
  211. print '</td></tr>';
  212. }
  213. else
  214. {
  215. print '<tr><td colspan="2">&nbsp;</td></tr>';
  216. }
  217. print '</table>';
  218. /*
  219. * Autres factures impayees
  220. */
  221. $sql = 'SELECT f.rowid as facid,f.rowid as ref,f.facnumber,f.total_ttc, f.datef as df';
  222. $sql.= ', sum(pf.amount) as am';
  223. $sql.= ' FROM '.MAIN_DB_PREFIX.'facture_fourn as f';
  224. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'paiementfourn_facturefourn as pf ON pf.fk_facturefourn = f.rowid';
  225. $sql.= " WHERE f.entity = ".$conf->entity;
  226. $sql.= ' AND f.fk_soc = '.$facture->socid;
  227. $sql.= ' AND f.paye = 0';
  228. $sql.= ' AND f.fk_statut = 1'; // Statut=0 => non validee, Statut=2 => annulee
  229. $sql.= ' GROUP BY f.rowid,f.facnumber,f.total_ttc,f.datef';
  230. $resql = $db->query($sql);
  231. if ($resql)
  232. {
  233. $num = $db->num_rows($resql);
  234. if ($num > 0)
  235. {
  236. $i = 0;
  237. print '<br>';
  238. print $langs->trans('Invoices').'<br>';
  239. print '<table class="noborder" width="100%">';
  240. print '<tr class="liste_titre">';
  241. print '<td>'.$langs->trans('Ref').'</td>';
  242. print '<td>'.$langs->trans('RefSupplier').'</td>';
  243. print '<td align="center">'.$langs->trans('Date').'</td>';
  244. print '<td align="right">'.$langs->trans('AmountTTC').'</td>';
  245. print '<td align="right">'.$langs->trans('AlreadyPaid').'</td>';
  246. print '<td align="right">'.$langs->trans('RemainderToPay').'</td>';
  247. print '<td align="center">'.$langs->trans('Amount').'</td>';
  248. print '</tr>';
  249. $var=True;
  250. $total=0;
  251. $totalrecu=0;
  252. while ($i < $num)
  253. {
  254. $objp = $db->fetch_object($resql);
  255. $var=!$var;
  256. print '<tr '.$bc[$var].'>';
  257. print '<td><a href="fiche.php?facid='.$objp->facid.'">'.img_object($langs->trans('ShowBill'),'bill').' '.$objp->ref;
  258. print '</a></td>';
  259. print '<td>'.$objp->facnumber.'</td>';
  260. if ($objp->df > 0 )
  261. {
  262. print '<td align="center">';
  263. print dol_print_date($db->jdate($objp->df)).'</td>';
  264. }
  265. else
  266. {
  267. print '<td align="center"><b>!!!</b></td>';
  268. }
  269. print '<td align="right">'.price($objp->total_ttc).'</td>';
  270. print '<td align="right">'.price($objp->am).'</td>';
  271. print '<td align="right">'.price($objp->total_ttc - $objp->am).'</td>';
  272. print '<td align="center">';
  273. $namef = 'amount_'.$objp->facid;
  274. print '<input type="text" size="8" name="'.$namef.'" value="'.GETPOST($namef).'">';
  275. print "</td></tr>\n";
  276. $total+=$objp->total;
  277. $total_ttc+=$objp->total_ttc;
  278. $totalrecu+=$objp->am;
  279. $i++;
  280. }
  281. if ($i > 1)
  282. {
  283. // Print total
  284. print '<tr class="liste_total">';
  285. print '<td colspan="3" align="left">'.$langs->trans('TotalTTC').':</td>';
  286. print '<td align="right"><b>'.price($total_ttc).'</b></td>';
  287. print '<td align="right"><b>'.price($totalrecu).'</b></td>';
  288. print '<td align="right"><b>'.price($total_ttc - $totalrecu).'</b></td>';
  289. print '<td align="center">&nbsp;</td>';
  290. print "</tr>\n";
  291. }
  292. print "</table>\n";
  293. }
  294. $db->free($resql);
  295. }
  296. else
  297. {
  298. dol_print_error($db);
  299. }
  300. // print '<tr><td colspan="3" align="center">';
  301. print '<center><br><input type="checkbox" checked="checked" name="closepaidinvoices"> '.$langs->trans("ClosePaidInvoicesAutomatically");
  302. print '<br><input type="submit" class="button" value="'.$langs->trans('Save').'"></center>';
  303. // print '</td></tr>';
  304. print '</form>';
  305. }
  306. }
  307. }
  308. /*
  309. * Show list
  310. */
  311. if (! $_GET['action'] && ! $_POST['action'])
  312. {
  313. if ($page == -1) $page = 0 ;
  314. $limit = $conf->liste_limit;
  315. $offset = $limit * $page ;
  316. if (! $sortorder) $sortorder='DESC';
  317. if (! $sortfield) $sortfield='p.datep';
  318. $sql = 'SELECT p.rowid as pid, p.datep as dp, p.amount as pamount, p.num_paiement,';
  319. $sql.= ' s.rowid as socid, s.nom,';
  320. $sql.= ' c.libelle as paiement_type,';
  321. $sql.= ' ba.rowid as bid, ba.label,';
  322. if (!$user->rights->societe->client->voir) $sql .= ' sc.fk_soc, sc.fk_user,';
  323. $sql.= ' SUM(f.amount)';
  324. $sql.= ' FROM '.MAIN_DB_PREFIX.'paiementfourn AS p';
  325. if (!$user->rights->societe->client->voir) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  326. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'paiementfourn_facturefourn AS pf ON p.rowid=pf.fk_paiementfourn';
  327. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'facture_fourn AS f ON f.rowid=pf.fk_facturefourn';
  328. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement AS c ON p.fk_paiement = c.id';
  329. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe AS s ON s.rowid = f.fk_soc';
  330. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON p.fk_bank = b.rowid';
  331. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank_account as ba ON b.fk_account = ba.rowid';
  332. $sql.= " WHERE f.entity = ".$conf->entity;
  333. if (!$user->rights->societe->client->voir) $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id;
  334. if ($socid)
  335. {
  336. $sql .= ' AND f.fk_soc = '.$socid;
  337. }
  338. // Search criteria
  339. if ($_REQUEST["search_ref"])
  340. {
  341. $sql .= ' AND p.rowid='.$db->escape($_REQUEST["search_ref"]);
  342. }
  343. if ($_REQUEST["search_account"])
  344. {
  345. $sql .= ' AND b.fk_account='.$db->escape($_REQUEST["search_account"]);
  346. }
  347. if ($_REQUEST["search_paymenttype"])
  348. {
  349. $sql .= " AND c.code='".$db->escape($_REQUEST["search_paymenttype"])."'";
  350. }
  351. if ($_REQUEST["search_amount"])
  352. {
  353. $sql .= " AND p.amount=".price2num($_REQUEST["search_amount"]);
  354. }
  355. if ($_REQUEST["search_company"])
  356. {
  357. $sql .= " AND s.nom LIKE '%".$db->escape($_REQUEST["search_company"])."%'";
  358. }
  359. $sql.= " GROUP BY p.rowid, p.datep, p.amount, p.num_paiement, s.rowid, s.nom, c.libelle, ba.rowid, ba.label";
  360. if (!$user->rights->societe->client->voir) $sql .= ", sc.fk_soc, sc.fk_user";
  361. $sql.= $db->order($sortfield,$sortorder);
  362. $sql.= $db->plimit($limit+1, $offset);
  363. $resql = $db->query($sql);
  364. if ($resql)
  365. {
  366. $num = $db->num_rows($resql);
  367. $i = 0;
  368. $var=True;
  369. $paramlist='';
  370. $paramlist.=($_REQUEST["search_ref"]?"&search_ref=".$_REQUEST["search_ref"]:"");
  371. $paramlist.=($_REQUEST["search_company"]?"&search_company=".$_REQUEST["search_company"]:"");
  372. $paramlist.=($_REQUEST["search_amount"]?"&search_amount=".$_REQUEST["search_amount"]:"");
  373. print_barre_liste($langs->trans('SupplierPayments'), $page, 'paiement.php',$paramlist,$sortfield,$sortorder,'',$num);
  374. if ($mesg) dol_htmloutput_mesg($mesg);
  375. if ($errmsg) dol_htmloutput_errors($errmsg);
  376. print '<form method="GET" action="'.$_SERVER["PHP_SELF"].'">';
  377. print '<table class="noborder" width="100%">';
  378. print '<tr class="liste_titre">';
  379. print_liste_field_titre($langs->trans('RefPayment'),'paiement.php','p.rowid','',$paramlist,'',$sortfield,$sortorder);
  380. print_liste_field_titre($langs->trans('Date'),'paiement.php','dp','',$paramlist,'align="center"',$sortfield,$sortorder);
  381. print_liste_field_titre($langs->trans('ThirdParty'),'paiement.php','s.nom','',$paramlist,'',$sortfield,$sortorder);
  382. print_liste_field_titre($langs->trans('Type'),'paiement.php','c.libelle','',$paramlist,'',$sortfield,$sortorder);
  383. print_liste_field_titre($langs->trans('Account'),'paiement.php','ba.label','',$paramlist,'',$sortfield,$sortorder);
  384. print_liste_field_titre($langs->trans('Amount'),'paiement.php','f.amount','',$paramlist,'align="right"',$sortfield,$sortorder);
  385. //print_liste_field_titre($langs->trans('Invoice'),'paiement.php','facnumber','',$paramlist,'',$sortfield,$sortorder);
  386. print "</tr>\n";
  387. // Lines for filters fields
  388. print '<tr class="liste_titre">';
  389. print '<td align="left">';
  390. print '<input class="fat" type="text" size="4" name="search_ref" value="'.$_REQUEST["search_ref"].'">';
  391. print '</td>';
  392. print '<td>&nbsp;</td>';
  393. print '<td align="left">';
  394. print '<input class="fat" type="text" size="6" name="search_company" value="'.$_REQUEST["search_company"].'">';
  395. print '</td>';
  396. print '<td>';
  397. $form->select_types_paiements($_REQUEST["search_paymenttype"],'search_paymenttype','',2,1,1);
  398. print '</td>';
  399. print '<td>';
  400. $form->select_comptes($_REQUEST["search_account"],'search_account',0,'',1);
  401. print '</td>';
  402. print '<td align="right">';
  403. print '<input class="fat" type="text" size="4" name="search_amount" value="'.$_REQUEST["search_amount"].'">';
  404. print '<input type="image" class="liste_titre" name="button_search" src="'.DOL_URL_ROOT.'/theme/'.$conf->theme.'/img/search.png" alt="'.$langs->trans("Search").'">';
  405. print '</td>';
  406. print "</tr>\n";
  407. while ($i < min($num,$limit))
  408. {
  409. $objp = $db->fetch_object($resql);
  410. $var=!$var;
  411. print '<tr '.$bc[$var].'>';
  412. // Ref payment
  413. print '<td nowrap="nowrap"><a href="'.DOL_URL_ROOT.'/fourn/paiement/fiche.php?id='.$objp->pid.'">'.img_object($langs->trans('ShowPayment'),'payment').' '.$objp->pid.'</a></td>';
  414. // Date
  415. print '<td nowrap="nowrap" align="center">'.dol_print_date($db->jdate($objp->dp),'day')."</td>\n";
  416. print '<td>';
  417. if ($objp->socid) print '<a href="'.DOL_URL_ROOT.'/societe/soc.php?socid='.$objp->socid.'">'.img_object($langs->trans('ShowCompany'),'company').' '.dol_trunc($objp->nom,32).'</a>';
  418. else print '&nbsp;';
  419. print '</td>';
  420. print '<td>'.dol_trunc($objp->paiement_type.' '.$objp->num_paiement,32)."</td>\n";
  421. print '<td>';
  422. if ($objp->bid) print '<a href="'.DOL_URL_ROOT.'/compta/bank/account.php?account='.$objp->bid.'">'.img_object($langs->trans("ShowAccount"),'account').' '.dol_trunc($objp->label,24).'</a>';
  423. else print '&nbsp;';
  424. print '</td>';
  425. print '<td align="right">'.price($objp->pamount).'</td>';
  426. // Ref invoice
  427. /*$invoicesupplierstatic->ref=$objp->facnumber;
  428. $invoicesupplierstatic->id=$objp->facid;
  429. print '<td nowrap="nowrap">';
  430. print $invoicesupplierstatic->getNomUrl(1);
  431. print '</td>';*/
  432. print '</tr>';
  433. $i++;
  434. }
  435. print "</table>";
  436. print "</form>\n";
  437. }
  438. else
  439. {
  440. dol_print_error($db);
  441. }
  442. }
  443. $db->close();
  444. llxFooter();
  445. ?>