create.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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@inodbox.com>
  5. * Copyright (C) 2010-2012 Juanjo Menent <jmenent@2byte.es>
  6. * Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
  7. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  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/compta/prelevement/create.php
  24. * \ingroup prelevement
  25. * \brief Prelevement creation page
  26. */
  27. require '../../main.inc.php';
  28. require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  33. require_once DOL_DOCUMENT_ROOT.'/core/lib/prelevement.lib.php';
  34. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  35. // Load translation files required by the page
  36. $langs->loadLangs(array('banks', 'categories', 'widthdrawals', 'companies', 'bills'));
  37. // Security check
  38. if ($user->societe_id) $socid=$user->societe_id;
  39. $result = restrictedArea($user, 'prelevement', '', '', 'bons');
  40. // Get supervariables
  41. $action = GETPOST('action','alpha');
  42. $mode = GETPOST('mode','alpha')?GETPOST('mode','alpha'):'real';
  43. $format = GETPOST('format','aZ09');
  44. $limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;
  45. $page = GETPOST("page",'int');
  46. if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
  47. $offset = $limit * $page;
  48. /*
  49. * Actions
  50. */
  51. // Change customer bank information to withdraw
  52. if ($action == 'modify')
  53. {
  54. for ($i = 1 ; $i < 9 ; $i++)
  55. {
  56. dolibarr_set_const($db, GETPOST("nom$i"), GETPOST("value$i"),'chaine',0,'',$conf->entity);
  57. }
  58. }
  59. if ($action == 'create')
  60. {
  61. // $conf->global->PRELEVEMENT_CODE_BANQUE and $conf->global->PRELEVEMENT_CODE_GUICHET should be empty
  62. $bprev = new BonPrelevement($db);
  63. $executiondate = dol_mktime(0, 0, 0, GETPOST('remonth'), GETPOST('reday'), GETPOST('reyear'));
  64. $result = $bprev->create($conf->global->PRELEVEMENT_CODE_BANQUE, $conf->global->PRELEVEMENT_CODE_GUICHET, $mode, $format,$executiondate);
  65. if ($result < 0)
  66. {
  67. setEventMessages($bprev->error, $bprev->errors, 'errors');
  68. }
  69. elseif ($result == 0)
  70. {
  71. $mesg=$langs->trans("NoInvoiceCouldBeWithdrawed", $format);
  72. setEventMessages($mesg, null, 'errors');
  73. $mesg.='<br>'."\n";
  74. foreach($bprev->invoice_in_error as $key => $val)
  75. {
  76. $mesg.='<span class="warning">'.$val."</span><br>\n";
  77. }
  78. }
  79. else
  80. {
  81. setEventMessages($langs->trans("DirectDebitOrderCreated", $bprev->getNomUrl(1)), null);
  82. }
  83. }
  84. /*
  85. * View
  86. */
  87. $form = new Form($db);
  88. $thirdpartystatic=new Societe($db);
  89. $invoicestatic=new Facture($db);
  90. $bprev = new BonPrelevement($db);
  91. llxHeader('', $langs->trans("NewStandingOrder"));
  92. if (prelevement_check_config() < 0)
  93. {
  94. $langs->load("errors");
  95. setEventMessages($langs->trans("ErrorModuleSetupNotComplete"), null, 'errors');
  96. }
  97. /*$h=0;
  98. $head[$h][0] = DOL_URL_ROOT.'/compta/prelevement/create.php';
  99. $head[$h][1] = $langs->trans("NewStandingOrder");
  100. $head[$h][2] = 'payment';
  101. $hselected = 'payment';
  102. $h++;
  103. dol_fiche_head($head, $hselected, $langs->trans("StandingOrders"), 0, 'payment');
  104. */
  105. print load_fiche_titre($langs->trans("NewStandingOrder"));
  106. dol_fiche_head();
  107. $nb=$bprev->NbFactureAPrelever();
  108. $nb1=$bprev->NbFactureAPrelever(1);
  109. $nb11=$bprev->NbFactureAPrelever(1,1);
  110. $pricetowithdraw=$bprev->SommeAPrelever();
  111. if ($nb < 0 || $nb1 < 0 || $nb11 < 0)
  112. {
  113. dol_print_error($bprev->error);
  114. }
  115. print '<table class="border" width="100%">';
  116. print '<tr><td class="titlefield">'.$langs->trans("NbOfInvoiceToWithdraw").'</td>';
  117. print '<td>';
  118. print $nb;
  119. print '</td></tr>';
  120. print '<tr><td>'.$langs->trans("AmountToWithdraw").'</td>';
  121. print '<td>';
  122. print price($pricetowithdraw);
  123. print '</td>';
  124. print '</tr>';
  125. print '</table>';
  126. print '</div>';
  127. if ($mesg) print $mesg;
  128. print "<div class=\"tabsAction\">\n";
  129. print '<form action="' . $_SERVER['PHP_SELF'] . '?action=create" method="POST">';
  130. print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
  131. if ($nb) {
  132. if ($pricetowithdraw) {
  133. print $langs->trans('ExecutionDate').' ';
  134. print $form->selectDate();
  135. if ($mysoc->isInEEC()) {
  136. print '<select name="format"><option value="FRST">'.$langs->trans('SEPAFRST').'</option><option value="RCUR">'.$langs->trans('SEPARCUR').'</option></select>';
  137. print '<input class="butAction" type="submit" value="' . $langs->trans("CreateForSepa") . '"/>';
  138. } else {
  139. print '<a class="butAction" type="submit" href="create.php?action=create&format=ALL">' . $langs->trans("CreateAll") . "</a>\n";
  140. }
  141. }
  142. else
  143. {
  144. if ($mysoc->isInEEC())
  145. {
  146. print '<a class="butActionRefused" href="#">'.$langs->trans("CreateForSepaFRST")."</a>\n";
  147. print '<a class="butActionRefused" href="#">'.$langs->trans("CreateForSepaRCUR")."</a>\n";
  148. }
  149. else
  150. {
  151. print '<a class="butActionRefused" href="#">'.$langs->trans("CreateAll")."</a>\n";
  152. }
  153. }
  154. }
  155. else
  156. {
  157. print '<a class="butActionRefused" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("NoInvoiceToWithdraw", $langs->transnoentitiesnoconv("StandingOrders"))).'">'.$langs->trans("CreateAll")."</a>\n";
  158. }
  159. print "</div>\n";
  160. print '<br>';
  161. /*
  162. * Invoices waiting for withdraw
  163. */
  164. $sql = "SELECT f.ref, f.rowid, f.total_ttc, s.nom as name, s.rowid as socid,";
  165. $sql.= " pfd.date_demande, pfd.amount";
  166. $sql.= " FROM ".MAIN_DB_PREFIX."facture as f,";
  167. $sql.= " ".MAIN_DB_PREFIX."societe as s,";
  168. $sql.= " ".MAIN_DB_PREFIX."prelevement_facture_demande as pfd";
  169. $sql.= " WHERE s.rowid = f.fk_soc";
  170. $sql.= " AND f.entity = ".$conf->entity;
  171. $sql.= " AND pfd.traite = 0";
  172. $sql.= " AND pfd.fk_facture = f.rowid";
  173. if ($socid) $sql.= " AND f.fk_soc = ".$socid;
  174. $nbtotalofrecords = '';
  175. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
  176. {
  177. $result = $db->query($sql);
  178. $nbtotalofrecords = $db->num_rows($result);
  179. }
  180. $sql.= $db->plimit($limit+1,$offset);
  181. $resql=$db->query($sql);
  182. if ($resql)
  183. {
  184. $num = $db->num_rows($resql);
  185. $i = 0;
  186. $param='';
  187. if ($limit > 0 && $limit != $conf->liste_limit) $param.='&limit='.urlencode($limit);
  188. if($socid) $param .= '&socid='.urlencode($socid);
  189. if($option) $param .= "&option=".urlencode($option);
  190. if(! empty($page) && $num <= $nbtotalofrecords) $page = 0;
  191. print '<form method="POST" id="searchFormList" action="'.$_SERVER["PHP_SELF"].'">';
  192. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  193. print '<input type="hidden" name="page" value="'.$page.'">';
  194. print_barre_liste($langs->trans("InvoiceWaitingWithdraw"),$page,$_SERVER['PHP_SELF'],$param,'','','',$num,$nbtotalofrecords,'title_accountancy.png',0,'','', $limit);
  195. print '<table class="noborder" width="100%">';
  196. print '<tr class="liste_titre">';
  197. print '<td>'.$langs->trans("Invoice").'</td>';
  198. print '<td>'.$langs->trans("ThirdParty").'</td>';
  199. print '<td>'.$langs->trans("RIB").'</td>';
  200. print '<td>'.$langs->trans("RUM").'</td>';
  201. print '<td align="right">'.$langs->trans("AmountTTC").'</td>';
  202. print '<td align="right">'.$langs->trans("DateRequest").'</td>';
  203. print '</tr>';
  204. if ($num)
  205. {
  206. require_once DOL_DOCUMENT_ROOT . '/societe/class/companybankaccount.class.php';
  207. $bac = new CompanyBankAccount($db);
  208. while ($i < $num && $i < $limit)
  209. {
  210. $obj = $db->fetch_object($resql);
  211. print '<tr class="oddeven">';
  212. print '<td>';
  213. $invoicestatic->id=$obj->rowid;
  214. $invoicestatic->ref=$obj->ref;
  215. print $invoicestatic->getNomUrl(1,'withdraw');
  216. print '</td>';
  217. // Thirdparty
  218. print '<td>';
  219. $thirdpartystatic->fetch($obj->socid);
  220. print $thirdpartystatic->getNomUrl(1,'ban');
  221. print '</td>';
  222. // RIB
  223. print '<td>';
  224. print $thirdpartystatic->display_rib();
  225. $bac->fetch(0, $obj->socid);
  226. if ($bac->verif() <= 0) print img_warning('Error on default bank number for IBAN : '.$bac->error_message);
  227. print '</td>';
  228. // RUM
  229. print '<td>';
  230. print $thirdpartystatic->display_rib('rum');
  231. $format = $thirdpartystatic->display_rib('format');
  232. if ($format) print ' ('.$format.')';
  233. print '</td>';
  234. // Amount
  235. print '<td align="right">';
  236. print price($obj->amount,0,$langs,0,0,-1,$conf->currency);
  237. print '</td>';
  238. // Date
  239. print '<td align="right">';
  240. print dol_print_date($db->jdate($obj->date_demande),'day');
  241. print '</td>';
  242. print '</tr>';
  243. $i++;
  244. }
  245. }
  246. else print '<tr '.$bc[0].'><td colspan="5" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
  247. print "</table>";
  248. print "</form>";
  249. print "<br>\n";
  250. }
  251. else
  252. {
  253. dol_print_error($db);
  254. }
  255. /*
  256. * List of latest withdraws
  257. */
  258. /*
  259. $limit=5;
  260. print load_fiche_titre($langs->trans("LastWithdrawalReceipts",$limit),'','');
  261. $sql = "SELECT p.rowid, p.ref, p.amount, p.statut";
  262. $sql.= ", p.datec";
  263. $sql.= " FROM ".MAIN_DB_PREFIX."prelevement_bons as p";
  264. $sql.= " WHERE p.entity IN (".getEntity('facture').")";
  265. $sql.= " ORDER BY datec DESC";
  266. $sql.=$db->plimit($limit);
  267. $result = $db->query($sql);
  268. if ($result)
  269. {
  270. $num = $db->num_rows($result);
  271. $i = 0;
  272. print"\n<!-- debut table -->\n";
  273. print '<table class="noborder" width="100%">';
  274. print '<tr class="liste_titre"><td>'.$langs->trans("Ref").'</td>';
  275. print '<td align="center">'.$langs->trans("Date").'</td><td align="right">'.$langs->trans("Amount").'</td>';
  276. print '</tr>';
  277. while ($i < min($num,$limit))
  278. {
  279. $obj = $db->fetch_object($result);
  280. print '<tr class="oddeven">';
  281. print "<td>";
  282. $bprev->id=$obj->rowid;
  283. $bprev->ref=$obj->ref;
  284. print $bprev->getNomUrl(1);
  285. print "</td>\n";
  286. print '<td align="center">'.dol_print_date($db->jdate($obj->datec),'day')."</td>\n";
  287. print '<td align="right">'.price($obj->amount,0,$langs,0,0,-1,$conf->currency)."</td>\n";
  288. print "</tr>\n";
  289. $i++;
  290. }
  291. print "</table><br>";
  292. $db->free($result);
  293. }
  294. else
  295. {
  296. dol_print_error($db);
  297. }
  298. */
  299. // End of page
  300. llxFooter();
  301. $db->close();