lines.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php
  2. /* Copyright (C) 2013-2016 Olivier Geffroy <jeff@jeffinfo.com>
  3. * Copyright (C) 2013-2016 Alexandre Spangaro <aspangaro@zendsi.com>
  4. * Copyright (C) 2014-2015 Ari Elbaz (elarifr) <github@accedinfo.com>
  5. * Copyright (C) 2014-2016 Florian Henry <florian.henry@open-concept.pro>
  6. * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
  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/accountancy/customer/lines.php
  23. * \ingroup Advanced accountancy
  24. * \brief Page of detail of the lines of ventilation of invoices customers
  25. */
  26. require '../../main.inc.php';
  27. require_once DOL_DOCUMENT_ROOT . '/core/class/html.formaccounting.class.php';
  28. require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php';
  29. require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
  30. require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
  31. require_once DOL_DOCUMENT_ROOT . '/core/class/html.formother.class.php';
  32. require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php';
  33. // Load translation files required by the page
  34. $langs->loadLangs(array("bills","compta","accountancy","productbatch"));
  35. $account_parent = GETPOST('account_parent');
  36. $changeaccount = GETPOST('changeaccount');
  37. // Search Getpost
  38. $search_lineid = GETPOST('search_lineid', 'int');
  39. $search_ref = GETPOST('search_ref', 'alpha');
  40. $search_invoice = GETPOST('search_invoice', 'alpha');
  41. $search_label = GETPOST('search_label', 'alpha');
  42. $search_desc = GETPOST('search_desc', 'alpha');
  43. $search_amount = GETPOST('search_amount', 'alpha');
  44. $search_account = GETPOST('search_account', 'alpha');
  45. $search_vat = GETPOST('search_vat', 'alpha');
  46. $search_day=GETPOST("search_day","int");
  47. $search_month=GETPOST("search_month","int");
  48. $search_year=GETPOST("search_year","int");
  49. $search_country = GETPOST('search_country', 'alpha');
  50. $search_tvaintra = GETPOST('search_tvaintra', 'alpha');
  51. // Load variable for pagination
  52. $limit = GETPOST('limit','int')?GETPOST('limit', 'int'):(empty($conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION)?$conf->liste_limit:$conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION);
  53. $sortfield = GETPOST('sortfield', 'alpha');
  54. $sortorder = GETPOST('sortorder', 'alpha');
  55. $page = GETPOST('page', 'int');
  56. if (empty($page) || $page < 0) $page = 0;
  57. $pageprev = $page - 1;
  58. $pagenext = $page + 1;
  59. $offset = $limit * $page;
  60. if (! $sortfield)
  61. $sortfield = "f.datef, f.facnumber, fd.rowid";
  62. if (! $sortorder) {
  63. if ($conf->global->ACCOUNTING_LIST_SORT_VENTILATION_DONE > 0) {
  64. $sortorder = "DESC";
  65. }
  66. }
  67. // Security check
  68. if ($user->societe_id > 0)
  69. accessforbidden();
  70. if (! $user->rights->accounting->bind->write)
  71. accessforbidden();
  72. $formaccounting = new FormAccounting($db);
  73. /*
  74. * Actions
  75. */
  76. // Purge search criteria
  77. if (GETPOST('button_removefilter_x','alpha') || GETPOST('button_removefilter.x','alpha') || GETPOST('button_removefilter','alpha')) // All tests are required to be compatible with all browsers
  78. {
  79. $search_lineid = '';
  80. $search_ref = '';
  81. $search_invoice = '';
  82. $search_label = '';
  83. $search_desc = '';
  84. $search_amount = '';
  85. $search_account = '';
  86. $search_vat = '';
  87. $search_day = '';
  88. $search_month = '';
  89. $search_year = '';
  90. $search_country = '';
  91. $search_tvaintra = '';
  92. }
  93. if (is_array($changeaccount) && count($changeaccount) > 0) {
  94. $error = 0;
  95. if (! (GETPOST('account_parent','int') >= 0))
  96. {
  97. $error++;
  98. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Account")), null, 'errors');
  99. }
  100. if (! $error)
  101. {
  102. $db->begin();
  103. $sql1 = "UPDATE " . MAIN_DB_PREFIX . "facturedet as l";
  104. $sql1 .= " SET l.fk_code_ventilation=" . (GETPOST('account_parent','int') > 0 ? GETPOST('account_parent','int') : '0');
  105. $sql1 .= ' WHERE l.rowid IN (' . implode(',', $changeaccount) . ')';
  106. dol_syslog('accountancy/customer/lines.php::changeaccount sql= ' . $sql1);
  107. $resql1 = $db->query($sql1);
  108. if (! $resql1) {
  109. $error ++;
  110. setEventMessages($db->lasterror(), null, 'errors');
  111. }
  112. if (! $error) {
  113. $db->commit();
  114. setEventMessages($langs->trans('Save'), null, 'mesgs');
  115. } else {
  116. $db->rollback();
  117. setEventMessages($db->lasterror(), null, 'errors');
  118. }
  119. $account_parent = ''; // Protection to avoid to mass apply it a second time
  120. }
  121. }
  122. /*
  123. * View
  124. */
  125. $form = new Form($db);
  126. $formother = new FormOther($db);
  127. llxHeader('', $langs->trans("CustomersVentilation") . ' - ' . $langs->trans("Dispatched"));
  128. print '<script type="text/javascript">
  129. $(function () {
  130. $(\'#select-all\').click(function(event) {
  131. // Iterate each checkbox
  132. $(\':checkbox\').each(function() {
  133. this.checked = true;
  134. });
  135. });
  136. $(\'#unselect-all\').click(function(event) {
  137. // Iterate each checkbox
  138. $(\':checkbox\').each(function() {
  139. this.checked = false;
  140. });
  141. });
  142. });
  143. </script>';
  144. /*
  145. * Customer Invoice lines
  146. */
  147. $sql = "SELECT f.rowid as facid, f.facnumber as ref, f.type, f.datef, f.ref_client,";
  148. $sql.= " fd.rowid, fd.description, fd.product_type as line_type, fd.total_ht, fd.total_tva, fd.tva_tx, fd.vat_src_code, fd.total_ttc,";
  149. $sql.= " s.rowid as socid, s.nom as name, s.code_compta, s.code_client,";
  150. $sql.= " p.rowid as product_id, p.fk_product_type as product_type, p.ref as product_ref, p.label as product_label, p.accountancy_code_sell, aa.rowid as fk_compte, aa.account_number, aa.label as label_compte,";
  151. $sql.= " fd.situation_percent,";
  152. $sql.= " co.code as country_code, co.label as country,";
  153. $sql.= " s.tva_intra";
  154. $parameters=array();
  155. $reshook=$hookmanager->executeHooks('printFieldListSelect',$parameters); // Note that $action and $object may have been modified by hook
  156. $sql.=$hookmanager->resPrint;
  157. $sql.= " FROM " . MAIN_DB_PREFIX . "facturedet as fd";
  158. $sql.= " LEFT JOIN " . MAIN_DB_PREFIX . "product as p ON p.rowid = fd.fk_product";
  159. $sql.= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON aa.rowid = fd.fk_code_ventilation";
  160. $sql.= " INNER JOIN " . MAIN_DB_PREFIX . "facture as f ON f.rowid = fd.fk_facture";
  161. $sql.= " INNER JOIN " . MAIN_DB_PREFIX . "societe as s ON s.rowid = f.fk_soc";
  162. $sql.= " LEFT JOIN " . MAIN_DB_PREFIX . "c_country as co ON co.rowid = s.fk_pays ";
  163. $sql.= " WHERE fd.fk_code_ventilation > 0";
  164. $sql.= " AND f.entity IN (" . getEntity('facture', 0) . ")"; // We don't share object for accountancy
  165. $sql.= " AND f.fk_statut > 0";
  166. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  167. $sql .= " AND f.type IN (" . Facture::TYPE_STANDARD . "," . Facture::TYPE_REPLACEMENT . "," . Facture::TYPE_CREDIT_NOTE . "," . Facture::TYPE_SITUATION . ")";
  168. } else {
  169. $sql .= " AND f.type IN (" . Facture::TYPE_STANDARD . "," . Facture::TYPE_STANDARD . "," . Facture::TYPE_CREDIT_NOTE . "," . Facture::TYPE_DEPOSIT . "," . Facture::TYPE_SITUATION . ")";
  170. }
  171. if ($search_lineid) {
  172. $sql .= natural_search("fd.rowid", $search_lineid, 1);
  173. }
  174. if (strlen(trim($search_invoice))) {
  175. $sql .= natural_search("f.facnumber", $search_invoice);
  176. }
  177. if (strlen(trim($search_ref))) {
  178. $sql .= natural_search("p.ref", $search_ref);
  179. }
  180. if (strlen(trim($search_label))) {
  181. $sql .= natural_search("p.label", $search_label);
  182. }
  183. if (strlen(trim($search_desc))) {
  184. $sql .= natural_search("fd.description", $search_desc);
  185. }
  186. if (strlen(trim($search_amount))) {
  187. $sql .= natural_search("fd.total_ht", $search_amount, 1);
  188. }
  189. if (strlen(trim($search_account))) {
  190. $sql .= natural_search("aa.account_number", $search_account);
  191. }
  192. if (strlen(trim($search_vat))) {
  193. $sql .= natural_search("fd.tva_tx", price2num($search_vat), 1);
  194. }
  195. if ($search_month > 0)
  196. {
  197. if ($search_year > 0 && empty($search_day))
  198. $sql.= " AND f.datef BETWEEN '".$db->idate(dol_get_first_day($search_year,$search_month,false))."' AND '".$db->idate(dol_get_last_day($search_year,$search_month,false))."'";
  199. else if ($search_year > 0 && ! empty($search_day))
  200. $sql.= " AND f.datef BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $search_month, $search_day, $search_year))."' AND '".$db->idate(dol_mktime(23, 59, 59, $search_month, $search_day, $search_year))."'";
  201. else
  202. $sql.= " AND date_format(f.datef, '%m') = '".$db->escape($search_month)."'";
  203. }
  204. else if ($search_year > 0)
  205. {
  206. $sql.= " AND f.datef BETWEEN '".$db->idate(dol_get_first_day($search_year,1,false))."' AND '".$db->idate(dol_get_last_day($search_year,12,false))."'";
  207. }
  208. if (strlen(trim($search_country))) {
  209. $sql .= natural_search("co.code", $search_country);
  210. }
  211. if (strlen(trim($search_tvaintra))) {
  212. $sql .= natural_search("s.tva_intra", $search_tvaintra);
  213. }
  214. $sql .= " AND f.entity IN (" . getEntity('facture', 0) . ")"; // We don't share object for accountancy
  215. $sql .= $db->order($sortfield, $sortorder);
  216. // Count total nb of records
  217. $nbtotalofrecords = '';
  218. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
  219. {
  220. $result = $db->query($sql);
  221. $nbtotalofrecords = $db->num_rows($result);
  222. if (($page * $limit) > $nbtotalofrecords) // if total resultset is smaller then paging size (filtering), goto and load page 0
  223. {
  224. $page = 0;
  225. $offset = 0;
  226. }
  227. }
  228. $sql .= $db->plimit($limit + 1, $offset);
  229. dol_syslog("/accountancy/customer/lines.php", LOG_DEBUG);
  230. $result = $db->query($sql);
  231. if ($result) {
  232. $num_lines = $db->num_rows($result);
  233. $i = 0;
  234. $param='';
  235. if (! empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param.='&contextpage='.urlencode($contextpage);
  236. if ($limit > 0 && $limit != $conf->liste_limit) $param.='&limit='.urlencode($limit);
  237. if ($search_invoice) $param .= "&search_invoice=" . urlencode($search_invoice);
  238. if ($search_ref) $param .= "&search_ref=" . urlencode($search_ref);
  239. if ($search_label) $param .= "&search_label=" . urlencode($search_label);
  240. if ($search_desc) $param .= "&search_desc=" . urlencode($search_desc);
  241. if ($search_account) $param .= "&search_account=" . urlencode($search_account);
  242. if ($search_vat) $param .= "&search_vat=" . urlencode($search_vat);
  243. if ($search_day) $param .= '&search_day='.urlencode($search_day);
  244. if ($search_month) $param .= '&search_month='.urlencode($search_month);
  245. if ($search_year) $param .= '&search_year='.urlencode($search_year);
  246. if ($search_country) $param .= "&search_country=" . urlencode($search_country);
  247. if ($search_tvaintra) $param .= "&search_tvaintra=" . urlencode($search_tvaintra);
  248. print '<form action="' . $_SERVER["PHP_SELF"] . '" method="post">' . "\n";
  249. print '<input type="hidden" name="action" value="ventil">';
  250. if ($optioncss != '') print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  251. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  252. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  253. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  254. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  255. print '<input type="hidden" name="page" value="'.$page.'">';
  256. print_barre_liste($langs->trans("InvoiceLinesDone"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num_lines, $nbtotalofrecords, 'title_accountancy', 0, '', '', $limit);
  257. print $langs->trans("DescVentilDoneCustomer") . '<br>';
  258. print '<br><div class="inline-block divButAction">' . $langs->trans("ChangeAccount") . '<br>';
  259. print $formaccounting->select_account($account_parent, 'account_parent', 2, array(), 0, 0, 'maxwidth300 maxwidthonsmartphone valignmiddle');
  260. print '<input type="submit" class="button valignmiddle" value="' . $langs->trans("ChangeBinding") . '"/></div>';
  261. $moreforfilter = '';
  262. print '<div class="div-table-responsive">';
  263. print '<table class="tagtable liste'.($moreforfilter?" listwithfilterbefore":"").'">'."\n";
  264. print '<tr class="liste_titre_filter">';
  265. print '<td class="liste_titre"><input type="text" class="flat maxwidth25" name="search_lineid" value="' . dol_escape_htmltag($search_lineid) . '""></td>';
  266. print '<td class="liste_titre"><input type="text" class="flat maxwidth50" name="search_invoice" value="' . dol_escape_htmltag($search_invoice) . '"></td>';
  267. print '<td class="liste_titre center">';
  268. if (! empty($conf->global->MAIN_LIST_FILTER_ON_DAY)) print '<input class="flat" type="text" size="1" maxlength="2" name="search_day" value="'.dol_escape_htmltag($search_day).'">';
  269. print '<input class="flat" type="text" size="1" maxlength="2" name="search_month" value="'.dol_escape_htmltag($search_month).'">';
  270. $formother->select_year($search_year,'search_year',1, 20, 5);
  271. print '</td>';
  272. print '<td class="liste_titre"><input type="text" class="flat maxwidth50" name="search_ref" value="' . dol_escape_htmltag($search_ref) . '"></td>';
  273. //print '<td class="liste_titre"><input type="text" class="flat maxwidth50" name="search_label" value="' . dol_escape_htmltag($search_label) . '"></td>';
  274. print '<td class="liste_titre"><input type="text" class="flat maxwidth50" name="search_desc" value="' . dol_escape_htmltag($search_desc) . '"></td>';
  275. print '<td class="liste_titre" align="right"><input type="text" class="right flat maxwidth50" name="search_amount" value="' . dol_escape_htmltag($search_amount) . '"></td>';
  276. print '<td class="liste_titre" align="right"><input type="text" class="right flat maxwidth50" placeholder="%" name="search_vat" size="1" value="' . dol_escape_htmltag($search_vat) . '"></td>';
  277. print '<td class="liste_titre">';
  278. print $form->select_country($search_country, 'search_country', '', 0, 'maxwidth200', 'code2');
  279. //print '<input type="text" class="flat maxwidth50" name="search_country" value="' . dol_escape_htmltag($search_country) . '">';
  280. print '</td>';
  281. print '<td class="liste_titre"><input type="text" class="flat maxwidth50" name="search_tvaintra" value="' . dol_escape_htmltag($search_tvaintra) . '"></td>';
  282. print '<td class="liste_titre" align="center"><input type="text" class="flat maxwidth50" name="search_account" value="' . dol_escape_htmltag($search_account) . '"></td>';
  283. print '<td class="liste_titre" align="center">';
  284. $searchpicto=$form->showFilterButtons();
  285. print $searchpicto;
  286. print "</td></tr>\n";
  287. print '<tr class="liste_titre">';
  288. print_liste_field_titre("LineId", $_SERVER["PHP_SELF"], "fd.rowid", "", $param, '', $sortfield, $sortorder);
  289. print_liste_field_titre("Invoice", $_SERVER["PHP_SELF"], "f.facnumber", "", $param, '', $sortfield, $sortorder);
  290. print_liste_field_titre("Date", $_SERVER["PHP_SELF"], "f.datef, f.facnumber, fd.rowid", "", $param, 'align="center"', $sortfield, $sortorder);
  291. print_liste_field_titre("ProductRef", $_SERVER["PHP_SELF"], "p.ref", "", $param, '', $sortfield, $sortorder);
  292. //print_liste_field_titre("ProductLabel", $_SERVER["PHP_SELF"], "p.label", "", $param, '', $sortfield, $sortorder);
  293. print_liste_field_titre("Description", $_SERVER["PHP_SELF"], "fd.description", "", $param, '', $sortfield, $sortorder);
  294. print_liste_field_titre("Amount", $_SERVER["PHP_SELF"], "fd.total_ht", "", $param, 'align="right"', $sortfield, $sortorder);
  295. print_liste_field_titre("VATRate", $_SERVER["PHP_SELF"], "fd.tva_tx", "", $param, 'align="right"', $sortfield, $sortorder);
  296. print_liste_field_titre("Country", $_SERVER["PHP_SELF"], "co.label", "", $param, '', $sortfield, $sortorder);
  297. print_liste_field_titre("VATIntra", $_SERVER["PHP_SELF"], "s.tva_intra", "", $param, '', $sortfield, $sortorder);
  298. print_liste_field_titre("Account", $_SERVER["PHP_SELF"], "aa.account_number", "", $param, 'align="center"', $sortfield, $sortorder);
  299. $clickpicto=$form->showCheckAddButtons();
  300. print_liste_field_titre($clickpicto, '', '', '', '', 'align="center"');
  301. print "</tr>\n";
  302. $facture_static = new Facture($db);
  303. $product_static = new Product($db);
  304. while ( $objp = $db->fetch_object($result) ) {
  305. $codecompta = length_accountg($objp->account_number) . ' - ' . $objp->label_compte;
  306. $facture_static->ref = $objp->ref;
  307. $facture_static->id = $objp->facid;
  308. $facture_static->type = $objp->ftype;
  309. $product_static->ref = $objp->product_ref;
  310. $product_static->id = $objp->product_id;
  311. $product_static->label = $objp->product_label;
  312. $product_static->type = $objp->line_type;
  313. print '<tr class="oddeven">';
  314. // Line id
  315. print '<td>' . $objp->rowid . '</td>';
  316. // Ref Invoice
  317. print '<td>' . $facture_static->getNomUrl(1) . '</td>';
  318. print '<td align="center">' . dol_print_date($db->jdate($objp->datef), 'day') . '</td>';
  319. // Ref Product
  320. print '<td>';
  321. if ($product_static->id)
  322. print $product_static->getNomUrl(1);
  323. if ($objp->product_label) print '<br>'.$objp->product_label;
  324. print '</td>';
  325. print '<td class="tdoverflowonsmartphone">';
  326. $text = dolGetFirstLineOfText(dol_string_nohtmltag($objp->description));
  327. $trunclength = empty($conf->global->ACCOUNTING_LENGTH_DESCRIPTION) ? 32 : $conf->global->ACCOUNTING_LENGTH_DESCRIPTION;
  328. print $form->textwithtooltip(dol_trunc($text,$trunclength), $objp->description);
  329. print '</td>';
  330. print '<td align="right">' . price($objp->total_ht) . '</td>';
  331. print '<td align="right">' . vatrate($objp->tva_tx.($objp->vat_src_code?' ('.$objp->vat_src_code.')':'')) . '</td>';
  332. print '<td>' . $langs->trans("Country".$objp->country_code) .' ('.$objp->country_code.')</td>';
  333. print '<td>' . $objp->tva_intra . '</td>';
  334. print '<td align="center">';
  335. print $codecompta . ' <a href="./card.php?id=' . $objp->rowid . '&backtopage='.urlencode($_SERVER["PHP_SELF"].($param?'?'.$param:'')).'">';
  336. print img_edit();
  337. print '</a>';
  338. print '</td>';
  339. print '<td class="center"><input type="checkbox" class="checkforaction" name="changeaccount[]" value="' . $objp->rowid . '"/></td>';
  340. print "</tr>";
  341. $i ++;
  342. }
  343. print "</table>";
  344. print "</div>";
  345. if ($nbtotalofrecords > $limit) {
  346. print_barre_liste('', $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num_lines, $nbtotalofrecords, '', 0, '', '', $limit, 1);
  347. }
  348. print '</form>';
  349. } else {
  350. print $db->lasterror();
  351. }
  352. // End of page
  353. llxFooter();
  354. $db->close();