create.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2010-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@capnetworks.com>
  5. * Copyright (C) 2010-2012 Juanjo Menent <jmenent@2byte.es>
  6. * Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file htdocs/compta/prelevement/create.php
  23. * \ingroup prelevement
  24. * \brief Prelevement creation page
  25. */
  26. require '../../main.inc.php';
  27. require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/lib/prelevement.lib.php';
  33. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  34. // Load translation files required by the page
  35. $langs->loadLangs(array('banks', 'categories', 'widthdrawals', 'companies', 'bills'));
  36. // Security check
  37. if ($user->societe_id) $socid=$user->societe_id;
  38. $result = restrictedArea($user, 'prelevement', '', '', 'bons');
  39. // Get supervariables
  40. $action = GETPOST('action','alpha');
  41. $mode = GETPOST('mode','alpha')?GETPOST('mode','alpha'):'real';
  42. $format = GETPOST('format','aZ09');
  43. /*
  44. * Actions
  45. */
  46. // Change customer bank information to withdraw
  47. if ($action == 'modify')
  48. {
  49. for ($i = 1 ; $i < 9 ; $i++)
  50. {
  51. dolibarr_set_const($db, GETPOST("nom$i"), GETPOST("value$i"),'chaine',0,'',$conf->entity);
  52. }
  53. }
  54. if ($action == 'create')
  55. {
  56. // $conf->global->PRELEVEMENT_CODE_BANQUE and $conf->global->PRELEVEMENT_CODE_GUICHET should be empty
  57. $bprev = new BonPrelevement($db);
  58. $executiondate = dol_mktime(0, 0, 0, GETPOST('remonth'), GETPOST('reday'), GETPOST('reyear'));
  59. $result = $bprev->create($conf->global->PRELEVEMENT_CODE_BANQUE, $conf->global->PRELEVEMENT_CODE_GUICHET, $mode, $format,$executiondate);
  60. if ($result < 0)
  61. {
  62. setEventMessages($bprev->error, $bprev->errors, 'errors');
  63. }
  64. elseif ($result == 0)
  65. {
  66. $mesg=$langs->trans("NoInvoiceCouldBeWithdrawed", $format);
  67. setEventMessages($mesg, null, 'errors');
  68. $mesg.='<br>'."\n";
  69. foreach($bprev->invoice_in_error as $key => $val)
  70. {
  71. $mesg.='<span class="warning">'.$val."</span><br>\n";
  72. }
  73. }
  74. else
  75. {
  76. setEventMessages($langs->trans("DirectDebitOrderCreated", $bprev->getNomUrl(1)), null);
  77. }
  78. }
  79. /*
  80. * View
  81. */
  82. $form = new Form($db);
  83. $thirdpartystatic=new Societe($db);
  84. $invoicestatic=new Facture($db);
  85. $bprev = new BonPrelevement($db);
  86. llxHeader('', $langs->trans("NewStandingOrder"));
  87. if (prelevement_check_config() < 0)
  88. {
  89. $langs->load("errors");
  90. setEventMessages($langs->trans("ErrorModuleSetupNotComplete"), null, 'errors');
  91. }
  92. /*$h=0;
  93. $head[$h][0] = DOL_URL_ROOT.'/compta/prelevement/create.php';
  94. $head[$h][1] = $langs->trans("NewStandingOrder");
  95. $head[$h][2] = 'payment';
  96. $hselected = 'payment';
  97. $h++;
  98. dol_fiche_head($head, $hselected, $langs->trans("StandingOrders"), 0, 'payment');
  99. */
  100. print load_fiche_titre($langs->trans("NewStandingOrder"));
  101. dol_fiche_head();
  102. $nb=$bprev->NbFactureAPrelever();
  103. $nb1=$bprev->NbFactureAPrelever(1);
  104. $nb11=$bprev->NbFactureAPrelever(1,1);
  105. $pricetowithdraw=$bprev->SommeAPrelever();
  106. if ($nb < 0 || $nb1 < 0 || $nb11 < 0)
  107. {
  108. dol_print_error($bprev->error);
  109. }
  110. print '<table class="border" width="100%">';
  111. print '<tr><td class="titlefield">'.$langs->trans("NbOfInvoiceToWithdraw").'</td>';
  112. print '<td>';
  113. print $nb;
  114. print '</td></tr>';
  115. print '<tr><td>'.$langs->trans("AmountToWithdraw").'</td>';
  116. print '<td>';
  117. print price($pricetowithdraw);
  118. print '</td>';
  119. print '</tr>';
  120. print '</table>';
  121. print '</div>';
  122. if ($mesg) print $mesg;
  123. print "<div class=\"tabsAction\">\n";
  124. print '<form action="' . $_SERVER['PHP_SELF'] . '?action=create" method="POST">';
  125. print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
  126. if ($nb) {
  127. if ($pricetowithdraw) {
  128. print $langs->trans('ExecutionDate').' ';
  129. print $form->select_date();
  130. if ($mysoc->isInEEC()) {
  131. print '<select name="format"><option value="FRST">'.$langs->trans('SEPAFRST').'</option><option value="RCUR">'.$langs->trans('SEPARCUR').'</option></select>';
  132. print '<input class="butAction" type="submit" value="' . $langs->trans("CreateForSepa") . '"/>';
  133. } else {
  134. print '<a class="butAction" type="submit" href="create.php?action=create&format=ALL">' . $langs->trans("CreateAll") . "</a>\n";
  135. }
  136. }
  137. else
  138. {
  139. if ($mysoc->isInEEC())
  140. {
  141. print '<a class="butActionRefused" href="#">'.$langs->trans("CreateForSepaFRST")."</a>\n";
  142. print '<a class="butActionRefused" href="#">'.$langs->trans("CreateForSepaRCUR")."</a>\n";
  143. }
  144. else
  145. {
  146. print '<a class="butActionRefused" href="#">'.$langs->trans("CreateAll")."</a>\n";
  147. }
  148. }
  149. }
  150. else
  151. {
  152. print '<a class="butActionRefused" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("NoInvoiceToWithdraw", $langs->transnoentitiesnoconv("StandingOrders"))).'">'.$langs->trans("CreateAll")."</a>\n";
  153. }
  154. print "</div>\n";
  155. print '<br>';
  156. /*
  157. * Invoices waiting for withdraw
  158. */
  159. $sql = "SELECT f.facnumber, f.rowid, f.total_ttc, s.nom as name, s.rowid as socid,";
  160. $sql.= " pfd.date_demande, pfd.amount";
  161. $sql.= " FROM ".MAIN_DB_PREFIX."facture as f,";
  162. $sql.= " ".MAIN_DB_PREFIX."societe as s,";
  163. $sql.= " ".MAIN_DB_PREFIX."prelevement_facture_demande as pfd";
  164. $sql.= " WHERE s.rowid = f.fk_soc";
  165. $sql.= " AND f.entity = ".$conf->entity;
  166. $sql.= " AND pfd.traite = 0";
  167. $sql.= " AND pfd.fk_facture = f.rowid";
  168. if ($socid) $sql.= " AND f.fk_soc = ".$socid;
  169. $resql=$db->query($sql);
  170. if ($resql)
  171. {
  172. $num = $db->num_rows($resql);
  173. $i = 0;
  174. print load_fiche_titre($langs->trans("InvoiceWaitingWithdraw").($num > 0?' ('.$num.')':''),'','');
  175. print '<table class="noborder" width="100%">';
  176. print '<tr class="liste_titre">';
  177. print '<td>'.$langs->trans("Invoice").'</td>';
  178. print '<td>'.$langs->trans("ThirdParty").'</td>';
  179. print '<td>'.$langs->trans("RIB").'</td>';
  180. print '<td>'.$langs->trans("RUM").'</td>';
  181. print '<td align="right">'.$langs->trans("AmountTTC").'</td>';
  182. print '<td align="right">'.$langs->trans("DateRequest").'</td>';
  183. print '</tr>';
  184. if ($num)
  185. {
  186. require_once DOL_DOCUMENT_ROOT . '/societe/class/companybankaccount.class.php';
  187. $bac = new CompanyBankAccount($db);
  188. while ($i < $num && $i < 20)
  189. {
  190. $obj = $db->fetch_object($resql);
  191. print '<tr class="oddeven">';
  192. print '<td>';
  193. $invoicestatic->id=$obj->rowid;
  194. $invoicestatic->ref=$obj->facnumber;
  195. print $invoicestatic->getNomUrl(1,'withdraw');
  196. print '</td>';
  197. // Thirdparty
  198. print '<td>';
  199. $thirdpartystatic->fetch($obj->socid);
  200. print $thirdpartystatic->getNomUrl(1,'ban');
  201. print '</td>';
  202. // RIB
  203. print '<td>';
  204. print $thirdpartystatic->display_rib();
  205. $bac->fetch(0, $obj->socid);
  206. if ($bac->verif() <= 0) print img_warning('Error on default bank number for IBAN : '.$bac->error_message);
  207. print '</td>';
  208. // RUM
  209. print '<td>';
  210. print $thirdpartystatic->display_rib('rum');
  211. $format = $thirdpartystatic->display_rib('format');
  212. if ($format) print ' ('.$format.')';
  213. print '</td>';
  214. // Amount
  215. print '<td align="right">';
  216. print price($obj->amount,0,$langs,0,0,-1,$conf->currency);
  217. print '</td>';
  218. // Date
  219. print '<td align="right">';
  220. print dol_print_date($db->jdate($obj->date_demande),'day');
  221. print '</td>';
  222. print '</tr>';
  223. $i++;
  224. }
  225. }
  226. else print '<tr '.$bc[0].'><td colspan="5" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
  227. print "</table>";
  228. print "<br>\n";
  229. }
  230. else
  231. {
  232. dol_print_error($db);
  233. }
  234. /*
  235. * List of latest withdraws
  236. */
  237. /*
  238. $limit=5;
  239. print load_fiche_titre($langs->trans("LastWithdrawalReceipts",$limit),'','');
  240. $sql = "SELECT p.rowid, p.ref, p.amount, p.statut";
  241. $sql.= ", p.datec";
  242. $sql.= " FROM ".MAIN_DB_PREFIX."prelevement_bons as p";
  243. $sql.= " WHERE p.entity IN (".getEntity('facture').")";
  244. $sql.= " ORDER BY datec DESC";
  245. $sql.=$db->plimit($limit);
  246. $result = $db->query($sql);
  247. if ($result)
  248. {
  249. $num = $db->num_rows($result);
  250. $i = 0;
  251. print"\n<!-- debut table -->\n";
  252. print '<table class="noborder" width="100%">';
  253. print '<tr class="liste_titre"><td>'.$langs->trans("Ref").'</td>';
  254. print '<td align="center">'.$langs->trans("Date").'</td><td align="right">'.$langs->trans("Amount").'</td>';
  255. print '</tr>';
  256. while ($i < min($num,$limit))
  257. {
  258. $obj = $db->fetch_object($result);
  259. print '<tr class="oddeven">';
  260. print "<td>";
  261. $bprev->id=$obj->rowid;
  262. $bprev->ref=$obj->ref;
  263. print $bprev->getNomUrl(1);
  264. print "</td>\n";
  265. print '<td align="center">'.dol_print_date($db->jdate($obj->datec),'day')."</td>\n";
  266. print '<td align="right">'.price($obj->amount,0,$langs,0,0,-1,$conf->currency)."</td>\n";
  267. print "</tr>\n";
  268. $i++;
  269. }
  270. print "</table><br>";
  271. $db->free($result);
  272. }
  273. else
  274. {
  275. dol_print_error($db);
  276. }
  277. */
  278. llxFooter();
  279. $db->close();