list.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. <?php
  2. /* Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
  3. * Copyright (C) 2013-2017 Alexandre Spangaro <aspangaro.dolibarr@gmail.com>
  4. * Copyright (C) 2014-2015 Ari Elbaz (elarifr) <github@accedinfo.com>
  5. * Copyright (C) 2013-2014 Florian Henry <florian.henry@open-concept.pro>
  6. * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>s
  7. * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
  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/accountancy/supplier/list.php
  24. * \ingroup Advanced accountancy
  25. * \brief Ventilation page from suppliers invoices
  26. */
  27. require '../../main.inc.php';
  28. // Class
  29. require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.facture.class.php';
  30. require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.product.class.php';
  31. require_once DOL_DOCUMENT_ROOT . '/core/class/html.formaccounting.class.php';
  32. require_once DOL_DOCUMENT_ROOT . '/accountancy/class/accountingaccount.class.php';
  33. require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
  34. // Langs
  35. $langs->load("compta");
  36. $langs->load("bills");
  37. $langs->load("other");
  38. $langs->load("main");
  39. $langs->load("accountancy");
  40. $langs->load("productbatch");
  41. $action=GETPOST('action','alpha');
  42. $massaction=GETPOST('massaction','alpha');
  43. $show_files=GETPOST('show_files','int');
  44. $confirm=GETPOST('confirm','alpha');
  45. $toselect = GETPOST('toselect', 'array');
  46. // Select Box
  47. $mesCasesCochees = GETPOST('toselect', 'array');
  48. // Search Getpost
  49. $search_lineid = GETPOST('search_lineid', 'int');
  50. $search_invoice = GETPOST('search_invoice', 'alpha');
  51. $search_ref = GETPOST('search_ref', 'alpha');
  52. $search_label = GETPOST('search_label', 'alpha');
  53. $search_desc = GETPOST('search_desc', 'alpha');
  54. $search_amount = GETPOST('search_amount', 'alpha');
  55. $search_account = GETPOST('search_account', 'alpha');
  56. $search_vat = GETPOST('search_vat', 'alpha');
  57. $btn_ventil = GETPOST('ventil', 'alpha');
  58. // Load variable for pagination
  59. $limit = GETPOST('limit') ? GETPOST('limit', 'int') : (empty($conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION)?$conf->liste_limit:$conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION);
  60. $sortfield = GETPOST('sortfield', 'alpha');
  61. $sortorder = GETPOST('sortorder', 'alpha');
  62. $page = GETPOST('page','int');
  63. if ($page < 0) { $page = 0; }
  64. $offset = $limit * $page;
  65. $pageprev = $page - 1;
  66. $pagenext = $page + 1;
  67. if (! $sortfield)
  68. $sortfield = "f.datef, f.ref, l.rowid";
  69. if (! $sortorder) {
  70. if ($conf->global->ACCOUNTING_LIST_SORT_VENTILATION_TODO > 0) {
  71. $sortorder = "DESC";
  72. }
  73. }
  74. // Security check
  75. if ($user->societe_id > 0)
  76. accessforbidden();
  77. if (! $user->rights->accounting->bind->write)
  78. accessforbidden();
  79. $formaccounting = new FormAccounting($db);
  80. $accounting = new AccountingAccount($db);
  81. // TODO: we should need to check if result is a really exist accountaccount rowid.....
  82. $aarowid_s = $accounting->fetch('', $conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT, 1);
  83. $aarowid_p = $accounting->fetch('', $conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT, 1);
  84. /*
  85. * Action
  86. */
  87. if (GETPOST('cancel')) { $action='list'; $massaction=''; }
  88. if (! GETPOST('confirmmassaction') && $massaction != 'presend' && $massaction != 'confirm_presend') { $massaction=''; }
  89. // Purge search criteria
  90. if (GETPOST("button_removefilter_x") || GETPOST("button_removefilter.x") || GETPOST("button_removefilter")) // All test are required to be compatible with all browsers
  91. {
  92. $search_lineid = '';
  93. $search_ref = '';
  94. $search_invoice = '';
  95. $search_label = '';
  96. $search_desc = '';
  97. $search_amount = '';
  98. $search_account = '';
  99. $search_vat = '';
  100. }
  101. // Mass actions
  102. $objectclass='Skeleton';
  103. $objectlabel='Skeleton';
  104. $permtoread = $user->rights->accounting->read;
  105. $permtodelete = $user->rights->accounting->delete;
  106. $uploaddir = $conf->accounting->dir_output;
  107. include DOL_DOCUMENT_ROOT.'/core/actions_massactions.inc.php';
  108. if ($massaction == 'ventil') {
  109. $msg='';
  110. //print '<div><font color="red">' . $langs->trans("Processing") . '...</font></div>';
  111. if (! empty($mesCasesCochees)) {
  112. $msg = '<div>' . $langs->trans("SelectedLines") . ': '.count($mesCasesCochees).'</div>';
  113. $msg.='<div class="detail">';
  114. $mesCodesVentilChoisis = $codeventil;
  115. $cpt = 0;
  116. $ok=0;
  117. $ko=0;
  118. foreach ( $mesCasesCochees as $maLigneCochee ) {
  119. $maLigneCourante = explode("_", $maLigneCochee);
  120. $monId = $maLigneCourante[0];
  121. $monCompte = GETPOST('codeventil'.$monId);
  122. if ($monCompte <= 0)
  123. {
  124. $msg.= '<div><font color="red">' . $langs->trans("Lineofinvoice") . ' ' . $monId . ' - ' . $langs->trans("NoAccountSelected") . '</font></div>';
  125. $ko++;
  126. }
  127. else
  128. {
  129. $sql = " UPDATE " . MAIN_DB_PREFIX . "facture_fourn_det";
  130. $sql .= " SET fk_code_ventilation = " . $monCompte;
  131. $sql .= " WHERE rowid = " . $monId;
  132. $accountventilated = new AccountingAccount($db);
  133. $accountventilated->fetch($monCompte, '');
  134. dol_syslog('accountancy/supplier/list.php:: sql=' . $sql, LOG_DEBUG);
  135. if ($db->query($sql)) {
  136. $msg.= '<div><font color="green">' . $langs->trans("Lineofinvoice") . ' ' . $monId . ' - ' . $langs->trans("VentilatedinAccount") . ' : ' . length_accountg($accountventilated->account_number) . '</font></div>';
  137. $ok++;
  138. } else {
  139. $msg.= '<div><font color="red">' . $langs->trans("ErrorDB") . ' : ' . $langs->trans("Lineofinvoice") . ' ' . $monId . ' - ' . $langs->trans("NotVentilatedinAccount") . ' : ' . length_accountg($accountventilated->account_number) . '<br/> <pre>' . $sql . '</pre></font></div>';
  140. $ko++;
  141. }
  142. }
  143. $cpt++;
  144. }
  145. $msg.='</div>';
  146. $msg.= '<div>' . $langs->trans("EndProcessing") . '</div>';
  147. //} else {
  148. // setEventMessages($langs->trans("NoRecordSelected"), null, 'warnings');
  149. }
  150. }
  151. /*
  152. * View
  153. */
  154. $form = new Form($db);
  155. llxHeader('', $langs->trans("SuppliersVentilation"));
  156. // Supplier Invoice Lines
  157. $sql = "SELECT f.rowid as facid, f.ref, f.ref_supplier, f.libelle as invoice_label, f.datef,";
  158. $sql.= " l.rowid, l.fk_product, l.description, l.total_ht as price, l.fk_code_ventilation, l.product_type as type_l, l.tva_tx as tva_tx_line, ";
  159. $sql.= " p.rowid as product_id, p.ref as product_ref, p.label as product_label, p.fk_product_type as type, p.accountancy_code_buy as code_buy, p.tva_tx as tva_tx_prod,";
  160. $sql.= " aa.rowid as aarowid";
  161. $sql.= " FROM " . MAIN_DB_PREFIX . "facture_fourn as f";
  162. $sql.= " INNER JOIN " . MAIN_DB_PREFIX . "facture_fourn_det as l ON f.rowid = l.fk_facture_fourn";
  163. $sql.= " LEFT JOIN " . MAIN_DB_PREFIX . "product as p ON p.rowid = l.fk_product";
  164. $sql.= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON p.accountancy_code_buy = aa.account_number";
  165. $sql.= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_system as accsys ON accsys.pcg_version = aa.fk_pcg_version";
  166. $sql.= " WHERE f.fk_statut > 0 AND l.fk_code_ventilation <= 0";
  167. $sql.= " AND product_type <= 2";
  168. $sql.= " AND (accsys.rowid='" . $conf->global->CHARTOFACCOUNTS . "' OR p.accountancy_code_buy IS NULL OR p.accountancy_code_buy ='')";
  169. // Add search filter like
  170. if ($search_lineid) {
  171. $sql .= natural_search("l.rowid", $search_lineid, 1);
  172. }
  173. if (strlen(trim($search_invoice))) {
  174. $sql .= natural_search("f.ref",$search_invoice);
  175. }
  176. if (strlen(trim($search_ref))) {
  177. $sql .= natural_search("p.ref",$search_ref);
  178. }
  179. if (strlen(trim($search_label))) {
  180. $sql .= natural_search("p.label",$search_label);
  181. }
  182. if (strlen(trim($search_desc))) {
  183. $sql .= natural_search("l.description",$search_desc);
  184. }
  185. if (strlen(trim($search_amount))) {
  186. $sql .= natural_search("l.total_ht",$search_amount,1);
  187. }
  188. if (strlen(trim($search_account))) {
  189. $sql .= natural_search("aa.account_number",$search_account);
  190. }
  191. if (strlen(trim($search_vat))) {
  192. $sql .= natural_search("l.tva_tx",$search_vat,1);
  193. }
  194. $sql .= " AND f.entity IN (" . getEntity("facture_fourn", 0) . ")"; // We don't share object for accountancy
  195. $sql .= $db->order($sortfield, $sortorder);
  196. // Count total nb of records
  197. $nbtotalofrecords = '';
  198. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
  199. {
  200. $result = $db->query($sql);
  201. $nbtotalofrecords = $db->num_rows($result);
  202. }
  203. $sql .= $db->plimit($limit + 1, $offset);
  204. dol_syslog('accountancy/supplier/list.php');
  205. $result = $db->query($sql);
  206. if ($result) {
  207. $num_lines = $db->num_rows($result);
  208. $i = 0;
  209. $arrayofselected=is_array($toselect)?$toselect:array();
  210. $param='';
  211. if (! empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param.='&contextpage='.$contextpage;
  212. if ($limit > 0 && $limit != $conf->liste_limit) $param.='&limit='.$limit;
  213. $arrayofmassactions = array(
  214. 'ventil'=>$langs->trans("Ventilate")
  215. //'presend'=>$langs->trans("SendByMail"),
  216. //'builddoc'=>$langs->trans("PDFMerge"),
  217. );
  218. //if ($user->rights->mymodule->supprimer) $arrayofmassactions['delete']=$langs->trans("Delete");
  219. //if ($massaction == 'presend') $arrayofmassactions=array();
  220. $massactionbutton=$form->selectMassAction('ventil', $arrayofmassactions, 1);
  221. print '<form action="' . $_SERVER["PHP_SELF"] . '" method="post">' . "\n";
  222. print '<input type="hidden" name="action" value="ventil">';
  223. if ($optioncss != '') print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  224. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  225. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  226. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  227. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  228. print '<input type="hidden" name="page" value="'.$page.'">';
  229. print_barre_liste($langs->trans("InvoiceLines"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num_lines, $nbtotalofrecords, 'title_accountancy', 0, '', '', $limit);
  230. print $langs->trans("DescVentilTodoCustomer") . '</br><br>';
  231. if ($msg) print $msg.'<br>';
  232. $moreforfilter = '';
  233. print '<div class="div-table-responsive">';
  234. print '<table class="tagtable liste'.($moreforfilter?" listwithfilterbefore":"").'">'."\n";
  235. // We add search filter
  236. print '<tr class="liste_titre_filter">';
  237. print '<td class="liste_titre"><input type="text" class="flat maxwidth50" name="search_lineid" value="' . dol_escape_htmltag($search_lineid) . '""></td>';
  238. print '<td class="liste_titre"><input type="text" class="flat maxwidth50" name="search_invoice" value="' . dol_escape_htmltag($search_invoice) . '"></td>';
  239. print '<td class="liste_titre"></td>';
  240. print '<td class="liste_titre"></td>';
  241. print '<td class="liste_titre"><input type="text" class="flat maxwidth50" name="search_ref" value="' . dol_escape_htmltag($search_ref) . '"></td>';
  242. //print '<td class="liste_titre"><input type="text" class="flat maxwidth50" name="search_label" value="' . dol_escape_htmltag($search_label) . '"></td>';
  243. print '<td class="liste_titre"><input type="text" class="flat maxwidthonsmartphone" name="search_desc" value="' . dol_escape_htmltag($search_desc) . '"></td>';
  244. print '<td class="liste_titre" align="right"><input type="text" class="right flat maxwidth50" name="search_amount" value="' . dol_escape_htmltag($search_amount) . '"></td>';
  245. print '<td class="liste_titre" align="right"><input type="text" class="right flat maxwidth50" name="search_vat" placeholder="%" size="1" value="' . dol_escape_htmltag($search_vat) . '"></td>';
  246. print '<td class="liste_titre"></td>';
  247. print '<td class="liste_titre"></td>';
  248. print '<td align="center" class="liste_titre">';
  249. $searchpicto=$form->showFilterButtons();
  250. print $searchpicto;
  251. print '</td>';
  252. print '</tr>';
  253. print '<tr class="liste_titre">';
  254. print_liste_field_titre($langs->trans("LineId"), $_SERVER["PHP_SELF"], "l.rowid", "", $param, '', $sortfield, $sortorder);
  255. print_liste_field_titre($langs->trans("Invoice"), $_SERVER["PHP_SELF"], "f.ref", "", $param, '', $sortfield, $sortorder);
  256. print_liste_field_titre($langs->trans("InvoiceLabel"), $_SERVER["PHP_SELF"], "f.libelle", "", $param, '', $sortfield, $sortorder);
  257. print_liste_field_titre($langs->trans("Date"), $_SERVER["PHP_SELF"], "f.datef, f.ref, l.rowid", "", $param, 'align="center"', $sortfield, $sortorder);
  258. print_liste_field_titre($langs->trans("ProductRef"), $_SERVER["PHP_SELF"], "p.ref", "", $param, '', $sortfield, $sortorder);
  259. //print_liste_field_titre($langs->trans("ProductLabel"), $_SERVER["PHP_SELF"], "p.label", "", $param, '', $sortfield, $sortorder);
  260. print_liste_field_titre($langs->trans("Description"), $_SERVER["PHP_SELF"], "l.description", "", $param, '', $sortfield, $sortorder);
  261. print_liste_field_titre($langs->trans("Amount"), $_SERVER["PHP_SELF"], "l.total_ht", "", $param, 'align="right"', $sortfield, $sortorder);
  262. print_liste_field_titre($langs->trans("VATRate"), $_SERVER["PHP_SELF"], "l.tva_tx", "", $param, 'align="right"', $sortfield, $sortorder);
  263. print_liste_field_titre($langs->trans("AccountAccountingSuggest"), '', '', '', '', 'align="center"');
  264. print_liste_field_titre($langs->trans("IntoAccount"), '', '', '', '', 'align="center"');
  265. $checkpicto='';
  266. if ($massactionbutton) $checkpicto=$form->showCheckAddButtons('checkforselect', 1);
  267. print_liste_field_titre($checkpicto, '', '', '', '', 'align="center"');
  268. print "</tr>\n";
  269. $facturefourn_static = new FactureFournisseur($db);
  270. $productfourn_static = new ProductFournisseur($db);
  271. $form = new Form($db);
  272. $var = true;
  273. while ( $i < min($num_lines, $limit) ) {
  274. $objp = $db->fetch_object($result);
  275. // product_type: 0 = service ? 1 = product
  276. // if product does not exist we use the value of product_type provided in facturedet to define if this is a product or service
  277. // issue : if we change product_type value in product DB it should differ from the value stored in facturedet DB !
  278. $objp->code_buy_l = '';
  279. $objp->code_buy_p = '';
  280. $objp->aarowid_suggest = '';
  281. $productfourn_static->ref = $objp->product_ref;
  282. $productfourn_static->id = $objp->product_id;
  283. $productfourn_static->type = $objp->type;
  284. $productfourn_static->label = $objp->product_label;
  285. $facturefourn_static->ref = $objp->ref;
  286. $facturefourn_static->id = $objp->facid;
  287. $facturefourn_static->type = $objp->type;
  288. $code_buy_p_notset = '';
  289. $objp->aarowid_suggest = $objp->aarowid;
  290. if ($objp->type_l == 1) {
  291. $objp->code_buy_l = (! empty($conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT : '');
  292. if ($objp->aarowid == '')
  293. $objp->aarowid_suggest = $aarowid_s;
  294. } elseif ($objp->type_l == 0) {
  295. $objp->code_buy_l = (! empty($conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT : '');
  296. if ($objp->aarowid == '')
  297. $objp->aarowid_suggest = $aarowid_p;
  298. }
  299. if ($objp->code_buy_l == -1) $objp->code_buy_l='';
  300. if (! empty($objp->code_buy)) {
  301. $objp->code_buy_p = $objp->code_buy; // Code on product
  302. } else {
  303. $code_buy_p_notset = 'color:orange';
  304. }
  305. if (empty($objp->code_buy_l) && empty($objp->code_buy_p)) $code_buy_p_notset = 'color:red';
  306. // $objp->code_buy_p is now code of product/service
  307. // $objp->code_buy_l is now default code of product/service
  308. print '<tr class="oddeven">';
  309. // Line id
  310. print '<td>' . $objp->rowid . '</td>';
  311. // Ref Invoice
  312. print '<td>' . $facturefourn_static->getNomUrl(1) . '</td>';
  313. print '<td class="tdoverflowonsmartphone">';
  314. print $objp->invoice_label;
  315. print '</td>';
  316. print '<td align="center">' . dol_print_date($db->jdate($objp->datef), 'day') . '</td>';
  317. // Ref product
  318. print '<td>';
  319. if ($productfourn_static->id)
  320. print $productfourn_static->getNomUrl(1);
  321. if ($objp->product_label) print '<br>'.$objp->product_label;
  322. print '</td>';
  323. // Description
  324. print '<td>';
  325. $text = dolGetFirstLineOfText(dol_string_nohtmltag($objp->description));
  326. $trunclength = defined('ACCOUNTING_LENGTH_DESCRIPTION') ? ACCOUNTING_LENGTH_DESCRIPTION : 32;
  327. print $form->textwithtooltip(dol_trunc($text,$trunclength), $objp->description);
  328. print '</td>';
  329. print '<td align="right">';
  330. print price($objp->price);
  331. print '</td>';
  332. // Vat rate
  333. if ($objp->vat_tx_l != $objp->vat_tx_p)
  334. $code_vat_differ = 'font-weight:bold; text-decoration:blink; color:red';
  335. print '<td style="' . $code_vat_differ . '" align="right">';
  336. print price($objp->tva_tx_line);
  337. print '</td>';
  338. // Current account
  339. print '<td align="center" style="' . $code_buy_p_notset . '">';
  340. print (($objp->type_l == 1)?$langs->trans("DefaultForService"):$langs->trans("DefaultForProduct")) . ' = ' . ($objp->code_buy_l > 0 ? length_accountg($objp->code_buy_l) : $langs->trans("Unknown"));
  341. if ($objp->product_id > 0)
  342. {
  343. print '<br>';
  344. print (($objp->type_l == 1)?$langs->trans("ThisService"):$langs->trans("ThisProduct")) . ' = ' . (empty($objp->code_buy_p) ? $langs->trans("Unknown") : length_accountg($objp->code_buy_p));
  345. }
  346. print '</td>';
  347. // Suggested accounting account
  348. print '<td align="center">';
  349. print $formaccounting->select_account($objp->aarowid_suggest, 'codeventil'.$objp->rowid, 1, array(), 0, 0, 'maxwidth300 maxwidthonsmartphone', 'cachewithshowemptyone');
  350. print '</td>';
  351. // Colonne choix ligne a ventiler
  352. print '<td align="center">';
  353. print '<input type="checkbox" class="flat checkforselect" name="toselect[]" value="' . $objp->rowid . "_" . $i . '"' . ($objp->aarowid ? "checked" : "") . '/>';
  354. print '</td>';
  355. print "</tr>";
  356. $i ++;
  357. }
  358. print '</table>';
  359. print "</div>";
  360. print '</form>';
  361. } else {
  362. print $db->error();
  363. }
  364. llxFooter();
  365. $db->close();