list.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <?php
  2. /* Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
  3. * Copyright (C) 2013-2016 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>
  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/customer/list.php
  24. * \ingroup Advanced accountancy
  25. * \brief Ventilation page from customers invoices
  26. */
  27. require '../../main.inc.php';
  28. // Class
  29. require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php';
  30. require_once DOL_DOCUMENT_ROOT . '/product/class/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.facnumber, 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. $aarowid_s = $accounting->fetch('', $conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT, 1);
  82. $aarowid_p = $accounting->fetch('', $conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT, 1);
  83. /*
  84. * Action
  85. */
  86. if (GETPOST('cancel')) { $action='list'; $massaction=''; }
  87. if (! GETPOST('confirmmassaction') && $massaction != 'presend' && $massaction != 'confirm_presend') { $massaction=''; }
  88. // Purge search criteria
  89. if (GETPOST("button_removefilter_x") || GETPOST("button_removefilter.x") || GETPOST("button_removefilter")) // All test are required to be compatible with all browsers
  90. {
  91. $search_lineid = '';
  92. $search_ref = '';
  93. $search_invoice = '';
  94. $search_label = '';
  95. $search_desc = '';
  96. $search_amount = '';
  97. $search_account = '';
  98. $search_vat = '';
  99. }
  100. // Mass actions
  101. $objectclass='Skeleton';
  102. $objectlabel='Skeleton';
  103. $permtoread = $user->rights->accounting->read;
  104. $permtodelete = $user->rights->accounting->delete;
  105. $uploaddir = $conf->accounting->dir_output;
  106. include DOL_DOCUMENT_ROOT.'/core/actions_massactions.inc.php';
  107. if ($massaction == 'ventil') {
  108. $msg='';
  109. //print '<div><font color="red">' . $langs->trans("Processing") . '...</font></div>';
  110. if (! empty($mesCasesCochees)) {
  111. $msg = '<div>' . $langs->trans("SelectedLines") . ': '.count($mesCasesCochees).'</div>';
  112. $msg.='<div class="detail">';
  113. $cpt = 0;
  114. $ok=0;
  115. $ko=0;
  116. foreach ($mesCasesCochees as $maLigneCochee) {
  117. $maLigneCourante = explode("_", $maLigneCochee);
  118. $monId = $maLigneCourante[0];
  119. $monCompte = GETPOST('codeventil'.$monId);
  120. if ($monCompte <= 0)
  121. {
  122. $msg.= '<div><font color="red">' . $langs->trans("Lineofinvoice") . ' ' . $monId . ' - ' . $langs->trans("NoAccountSelected") . '</font></div>';
  123. $ko++;
  124. }
  125. else
  126. {
  127. $sql = " UPDATE " . MAIN_DB_PREFIX . "facturedet";
  128. $sql .= " SET fk_code_ventilation = " . $monCompte;
  129. $sql .= " WHERE rowid = " . $monId;
  130. $accountventilated = new AccountingAccount($db);
  131. $accountventilated->fetch($monCompte, '');
  132. dol_syslog("/accountancy/customer/list.php sql=" . $sql, LOG_DEBUG);
  133. if ($db->query($sql)) {
  134. $msg.= '<div><font color="green">' . $langs->trans("Lineofinvoice") . ' ' . $monId . ' - ' . $langs->trans("VentilatedinAccount") . ' : ' . length_accountg($accountventilated->account_number) . '</font></div>';
  135. $ok++;
  136. } else {
  137. $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>';
  138. $ko++;
  139. }
  140. }
  141. $cpt++;
  142. }
  143. $msg.='</div>';
  144. $msg.= '<div>' . $langs->trans("EndProcessing") . '</div>';
  145. //} else {
  146. // setEventMessages($langs->trans("NoRecordSelected"), null, 'warnings');
  147. }
  148. }
  149. /*
  150. * View
  151. */
  152. $form = new Form($db);
  153. llxHeader('', $langs->trans("Ventilation"));
  154. // Customer Invoice lines
  155. $sql = "SELECT f.facnumber, f.rowid as facid, f.datef, f.type as ftype,";
  156. $sql .= " l.rowid, l.fk_product, l.description, l.total_ht, l.fk_code_ventilation, l.product_type as type_l, l.tva_tx as tva_tx_line,";
  157. $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_sell as code_sell, p.tva_tx as tva_tx_prod,";
  158. $sql .= " aa.rowid as aarowid";
  159. $sql .= " FROM " . MAIN_DB_PREFIX . "facture as f";
  160. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "facturedet as l ON f.rowid = l.fk_facture";
  161. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product as p ON p.rowid = l.fk_product";
  162. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON p.accountancy_code_sell = aa.account_number";
  163. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_system as accsys ON accsys.pcg_version = aa.fk_pcg_version";
  164. $sql .= " WHERE f.fk_statut > 0 AND l.fk_code_ventilation <= 0";
  165. $sql .= " AND product_type <= 2";
  166. $sql .= " AND (accsys.rowid='" . $conf->global->CHARTOFACCOUNTS . "' OR p.accountancy_code_sell IS NULL OR p.accountancy_code_sell ='')";
  167. // Add search filter like
  168. if ($search_lineid) {
  169. $sql .= natural_search("l.rowid", $search_lineid, 1);
  170. }
  171. if (strlen(trim($search_invoice))) {
  172. $sql .= natural_search("f.facnumber",$search_invoice);
  173. }
  174. if (strlen(trim($search_ref))) {
  175. $sql .= natural_search("p.ref",$search_ref);
  176. }
  177. if (strlen(trim($search_label))) {
  178. $sql .= natural_search("p.label",$search_label);
  179. }
  180. if (strlen(trim($search_desc))) {
  181. $sql .= natural_search("l.description",$search_desc);
  182. }
  183. if (strlen(trim($search_amount))) {
  184. $sql .= natural_search("l.total_ht",$search_amount,1);
  185. }
  186. if (strlen(trim($search_account))) {
  187. $sql .= natural_search("aa.account_number",$search_account);
  188. }
  189. if (strlen(trim($search_vat))) {
  190. $sql .= natural_search("l.tva_tx",$search_vat,1);
  191. }
  192. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  193. $sql .= " AND f.type IN (" . Facture::TYPE_STANDARD . "," . Facture::TYPE_REPLACEMENT . "," . Facture::TYPE_CREDIT_NOTE . "," . Facture::TYPE_SITUATION . ")";
  194. } else {
  195. $sql .= " AND f.type IN (" . Facture::TYPE_STANDARD . "," . Facture::TYPE_STANDARD . "," . Facture::TYPE_CREDIT_NOTE . "," . Facture::TYPE_DEPOSIT . "," . Facture::TYPE_SITUATION . ")";
  196. }
  197. $sql .= " AND f.entity IN (" . getEntity("facture", 0) . ")"; // We don't share object for accountancy
  198. $sql .= $db->order($sortfield, $sortorder);
  199. // Count total nb of records
  200. $nbtotalofrecords = '';
  201. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
  202. {
  203. $result = $db->query($sql);
  204. $nbtotalofrecords = $db->num_rows($result);
  205. }
  206. $sql .= $db->plimit($limit + 1, $offset);
  207. dol_syslog("/accountancy/customer/list.php", LOG_DEBUG);
  208. $result = $db->query($sql);
  209. if ($result) {
  210. $num_lines = $db->num_rows($result);
  211. $i = 0;
  212. $arrayofselected=is_array($toselect)?$toselect:array();
  213. $param='';
  214. if (! empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param.='&contextpage='.$contextpage;
  215. if ($limit > 0 && $limit != $conf->liste_limit) $param.='&limit='.$limit;
  216. $arrayofmassactions = array(
  217. 'ventil'=>$langs->trans("Ventilate")
  218. //'presend'=>$langs->trans("SendByMail"),
  219. //'builddoc'=>$langs->trans("PDFMerge"),
  220. );
  221. //if ($user->rights->mymodule->supprimer) $arrayofmassactions['delete']=$langs->trans("Delete");
  222. //if ($massaction == 'presend') $arrayofmassactions=array();
  223. $massactionbutton=$form->selectMassAction('ventil', $arrayofmassactions, 1);
  224. print '<form action="' . $_SERVER["PHP_SELF"] . '" method="post">' . "\n";
  225. print '<input type="hidden" name="action" value="ventil">';
  226. if ($optioncss != '') print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  227. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  228. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  229. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  230. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  231. print '<input type="hidden" name="page" value="'.$page.'">';
  232. print_barre_liste($langs->trans("InvoiceLines"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num_lines, $nbtotalofrecords, 'title_accountancy', 0, '', '', $limit);
  233. print $langs->trans("DescVentilTodoCustomer") . '</br><br>';
  234. if ($msg) print $msg.'<br>';
  235. $moreforfilter = '';
  236. print '<div class="div-table-responsive">';
  237. print '<table class="tagtable liste'.($moreforfilter?" listwithfilterbefore":"").'">'."\n";
  238. // We add search filter
  239. print '<tr class="liste_titre_filter">';
  240. print '<td class="liste_titre"><input type="text" class="flat maxwidth50" name="search_lineid" value="' . dol_escape_htmltag($search_lineid) . '""></td>';
  241. print '<td class="liste_titre"><input type="text" class="flat maxwidth50" name="search_invoice" value="' . dol_escape_htmltag($search_invoice) . '"></td>';
  242. print '<td class="liste_titre"></td>';
  243. print '<td class="liste_titre"><input type="text" class="flat maxwidth50" name="search_ref" value="' . dol_escape_htmltag($search_ref) . '"></td>';
  244. //print '<td class="liste_titre"><input type="text" class="flat maxwidth50" name="search_label" value="' . dol_escape_htmltag($search_label) . '"></td>';
  245. print '<td class="liste_titre"><input type="text" class="flat maxwidthonsmartphone" name="search_desc" value="' . dol_escape_htmltag($search_desc) . '"></td>';
  246. print '<td class="liste_titre" align="right"><input type="text" class="flat maxwidth50 right" name="search_amount" value="' . dol_escape_htmltag($search_amount) . '"></td>';
  247. print '<td class="liste_titre" align="right"><input type="text" class="flat maxwidth50 right" name="search_vat" placeholder="%" size="1" value="' . dol_escape_htmltag($search_vat) . '"></td>';
  248. print '<td class="liste_titre"></td>';
  249. print '<td class="liste_titre"></td>';
  250. print '<td align="center" class="liste_titre">';
  251. $searchpicto=$form->showFilterButtons();
  252. print $searchpicto;
  253. print '</td>';
  254. print '</tr>';
  255. print '<tr class="liste_titre">';
  256. print_liste_field_titre($langs->trans("LineId"), $_SERVER["PHP_SELF"], "l.rowid", "", $param, '', $sortfield, $sortorder);
  257. print_liste_field_titre($langs->trans("Invoice"), $_SERVER["PHP_SELF"], "f.facnumber", "", $param, '', $sortfield, $sortorder);
  258. print_liste_field_titre($langs->trans("Date"), $_SERVER["PHP_SELF"], "f.datef, f.facnumber, l.rowid", "", $param, 'align="center"', $sortfield, $sortorder);
  259. print_liste_field_titre($langs->trans("ProductRef"), $_SERVER["PHP_SELF"], "p.ref", "", $param, '', $sortfield, $sortorder);
  260. //print_liste_field_titre($langs->trans("ProductLabel"), $_SERVER["PHP_SELF"], "p.label", "", $param, '', $sortfield, $sortorder);
  261. print_liste_field_titre($langs->trans("Description"), $_SERVER["PHP_SELF"], "l.description", "", $param, '', $sortfield, $sortorder);
  262. print_liste_field_titre($langs->trans("Amount"), $_SERVER["PHP_SELF"], "l.total_ht", "", $param, 'align="right"', $sortfield, $sortorder);
  263. print_liste_field_titre($langs->trans("VATRate"), $_SERVER["PHP_SELF"], "l.tva_tx", "", $param, 'align="right"', $sortfield, $sortorder);
  264. print_liste_field_titre($langs->trans("AccountAccountingSuggest"), '', '', '', '', 'align="center"');
  265. print_liste_field_titre($langs->trans("IntoAccount"), '', '', '', '', 'align="center"');
  266. $checkpicto='';
  267. if ($massactionbutton) $checkpicto=$form->showCheckAddButtons('checkforselect', 1);
  268. print_liste_field_titre($checkpicto, '', '', '', '', 'align="center"');
  269. print "</tr>\n";
  270. $facture_static = new Facture($db);
  271. $product_static = new Product($db);
  272. $form = new Form($db);
  273. $var = true;
  274. while ( $i < min($num_lines, $limit) ) {
  275. $objp = $db->fetch_object($result);
  276. $objp->code_sell_l = '';
  277. $objp->code_sell_p = '';
  278. $objp->aarowid_suggest = '';
  279. $product_static->ref = $objp->product_ref;
  280. $product_static->id = $objp->product_id;
  281. $product_static->type = $objp->type;
  282. $product_static->label = $objp->product_label;
  283. $facture_static->ref = $objp->facnumber;
  284. $facture_static->id = $objp->facid;
  285. $facture_static->type = $objp->ftype;
  286. $code_sell_p_notset = '';
  287. $objp->aarowid_suggest = $objp->aarowid;
  288. if ($objp->type_l == 1) {
  289. $objp->code_sell_l = (! empty($conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT : '');
  290. if ($objp->aarowid == '') {
  291. $objp->aarowid_suggest = $aarowid_s;
  292. }
  293. } elseif ($objp->type_l == 0) {
  294. $objp->code_sell_l = (! empty($conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT : '');
  295. if ($objp->aarowid == '') {
  296. $objp->aarowid_suggest = $aarowid_p;
  297. }
  298. }
  299. if ($objp->code_sell_l == -1) $objp->code_sell_l='';
  300. if (! empty($objp->code_sell)) {
  301. $objp->code_sell_p = $objp->code_sell; // Code on product
  302. } else {
  303. $code_sell_p_notset = 'color:orange';
  304. }
  305. if (empty($objp->code_sell_l) && empty($objp->code_sell_p)) $code_sell_p_notset = 'color:red';
  306. // $objp->code_sell_p is now code of product/service
  307. // $objp->code_sell_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>' . $facture_static->getNomUrl(1) . '</td>';
  313. print '<td align="center">' . dol_print_date($db->jdate($objp->datef), 'day') . '</td>';
  314. // Ref Product
  315. print '<td>';
  316. if ($product_static->id)
  317. print $product_static->getNomUrl(1);
  318. if ($objp->product_label) print '<br>'.$objp->product_label;
  319. print '</td>';
  320. print '<td class="tdoverflowonsmartphone">';
  321. $text = dolGetFirstLineOfText(dol_string_nohtmltag($objp->description));
  322. $trunclength = defined('ACCOUNTING_LENGTH_DESCRIPTION') ? ACCOUNTING_LENGTH_DESCRIPTION : 32;
  323. print $form->textwithtooltip(dol_trunc($text,$trunclength), $objp->description);
  324. print '</td>';
  325. print '<td align="right">';
  326. print price($objp->total_ht);
  327. print '</td>';
  328. // Vat rate
  329. if ($objp->vat_tx_l != $objp->vat_tx_p)
  330. $code_vat_differ = 'font-weight:bold; text-decoration:blink; color:red';
  331. print '<td style="' . $code_vat_differ . '" align="right">';
  332. print price($objp->tva_tx_line);
  333. print '</td>';
  334. // Current account
  335. print '<td align="center" style="' . $code_sell_p_notset . '">';
  336. print (($objp->type_l == 1)?$langs->trans("DefaultForService"):$langs->trans("DefaultForProduct")) . ' = ' . ($objp->code_sell_l > 0 ? length_accountg($objp->code_sell_l) : $langs->trans("Unknown"));
  337. if ($objp->product_id > 0)
  338. {
  339. print '<br>';
  340. print (($objp->type_l == 1)?$langs->trans("ThisService"):$langs->trans("ThisProduct")) . ' = ' . (empty($objp->code_sell_p) ? $langs->trans("Unknown") : length_accountg($objp->code_sell_p));
  341. }
  342. print '</td>';
  343. // Suggested accounting account
  344. print '<td align="center">';
  345. print $formaccounting->select_account($objp->aarowid_suggest, 'codeventil'.$objp->rowid, 1, array(), 0, 0, 'maxwidth300 maxwidthonsmartphone', 'cachewithshowemptyone');
  346. print '</td>';
  347. print '<td align="center">';
  348. print '<input type="checkbox" class="flat checkforselect" name="toselect[]" value="' . $objp->rowid . "_" . $i . '"' . ($objp->aarowid ? "checked" : "") . '/>';
  349. print '</td>';
  350. print '</tr>';
  351. $i ++;
  352. }
  353. print '</table>';
  354. print "</div>";
  355. print '</form>';
  356. } else {
  357. print $db->error();
  358. }
  359. llxFooter();
  360. $db->close();