list.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. /* Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
  3. * Copyright (C) 2013-2015 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. *
  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/list.php
  23. * \ingroup Accounting Expert
  24. * \brief Ventilation page from customers invoices
  25. */
  26. require '../../main.inc.php';
  27. // Class
  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.'/accountancy/class/html.formventilation.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingaccount.class.php';
  32. // Langs
  33. $langs->load("compta");
  34. $langs->load("bills");
  35. $langs->load("other");
  36. $langs->load("main");
  37. $langs->load("accountancy");
  38. $action = GETPOST('action');
  39. $codeventil = GETPOST('codeventil', 'array');
  40. $mesCasesCochees = GETPOST('mesCasesCochees', 'array');
  41. $search_ref = GETPOST('search_ref','alpha');
  42. $search_label = GETPOST('search_label','alpha');
  43. $search_desc = GETPOST('search_desc','alpha');
  44. $sortfield = GETPOST('sortfield','alpha');
  45. $sortorder = GETPOST('sortorder','alpha');
  46. //Should move to top with all GETPOST
  47. $page = GETPOST('page');
  48. if ($page < 0) $page = 0;
  49. if (! empty($conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION)) {
  50. $limit = $conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION;
  51. } else if ($conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION <= 0) {
  52. $limit = $conf->liste_limit;
  53. } else {
  54. $limit = $conf->liste_limit;
  55. }
  56. $offset = $limit * $page;
  57. //End Should move to top with all GETPOST
  58. // TODO : remove comment
  59. //elarifr we can not use only
  60. //$sql .= " ORDER BY l.rowid";
  61. // f.datef will order like FA08 FA09 FA10 FA05 FA06 FA07 FA04...
  62. // f.facnumber will not order properly invoice / avoir / accompte you can have All AC then All AV and all FA
  63. // l.rowid when an invoice is edited rowid are added at end of table & facturedet.rowid are not ordered
  64. //if (! $sortfield) $sortfield="l.rowid";
  65. if (! $sortfield) $sortfield="f.datef, f.facnumber, l.rowid";
  66. //if (! $sortorder) $sortorder="DESC";
  67. if (! $sortorder) {
  68. if ($conf->global->ACCOUNTING_LIST_SORT_VENTILATION_TODO > 0) {
  69. $sortorder = " DESC ";
  70. }
  71. }
  72. // Security check
  73. if ($user->societe_id > 0)
  74. accessforbidden();
  75. if (! $user->rights->accounting->ventilation->dispatch)
  76. accessforbidden();
  77. $formventilation = new FormVentilation($db);
  78. //Defaut AccountingAccount RowId Product / Service
  79. //at this time ACCOUNTING_SERVICE_SOLD_ACCOUNT & ACCOUNTING_PRODUCT_SOLD_ACCOUNT are account number not accountingacount rowid
  80. //so we need to get those default value rowid first
  81. $accounting = new AccountingAccount($db);
  82. //TODO: we should need to check if result is a really exist accountaccount rowid.....
  83. $aarowid_s = $accounting->fetch('', ACCOUNTING_SERVICE_SOLD_ACCOUNT);
  84. $aarowid_p = $accounting->fetch('', ACCOUNTING_PRODUCT_SOLD_ACCOUNT);
  85. // Purge search criteria
  86. if (GETPOST("button_removefilter_x") || GETPOST("button_removefilter")) // Both test are required to be compatible with all browsers
  87. {
  88. $search_ref='';
  89. $search_label='';
  90. $search_desc='';
  91. }
  92. /*
  93. * View
  94. */
  95. llxHeader('', $langs->trans("Ventilation"));
  96. //debug
  97. //print_r($aarowid_s);
  98. //print_r($aarowid_p);
  99. print '<script type="text/javascript">
  100. $(function () {
  101. $(\'#select-all\').click(function(event) {
  102. // Iterate each checkbox
  103. $(\':checkbox\').each(function() {
  104. this.checked = true;
  105. });
  106. });
  107. $(\'#unselect-all\').click(function(event) {
  108. // Iterate each checkbox
  109. $(\':checkbox\').each(function() {
  110. this.checked = false;
  111. });
  112. });
  113. });
  114. </script>';
  115. /*
  116. * Action
  117. */
  118. if ($action == 'ventil') {
  119. print '<div><font color="red">' . $langs->trans("Processing") . '...</font></div>';
  120. if (! empty($codeventil) && ! empty($mesCasesCochees)) {
  121. print '<div><font color="red">' . count($mesCasesCochees) . ' ' . $langs->trans("SelectedLines") . '</font></div>';
  122. $mesCodesVentilChoisis = $codeventil;
  123. $cpt = 0;
  124. foreach ( $mesCasesCochees as $maLigneCochee ) {
  125. // print '<div><font color="red">id selectionnee : '.$monChoix."</font></div>";
  126. $maLigneCourante = explode("_", $maLigneCochee);
  127. $monId = $maLigneCourante[0];
  128. $monNumLigne = $maLigneCourante[1];
  129. $monCompte = $mesCodesVentilChoisis[$monNumLigne];
  130. $sql = " UPDATE " . MAIN_DB_PREFIX . "facturedet";
  131. $sql .= " SET fk_code_ventilation = " . $monCompte;
  132. $sql .= " WHERE rowid = " . $monId;
  133. dol_syslog("/accountancy/customer/list.php sql=" . $sql, LOG_DEBUG);
  134. if ($db->query($sql)) {
  135. print '<div><font color="green">' . $langs->trans("Lineofinvoice") . ' ' . $monId . ' ' . $langs->trans("VentilatedinAccount") . ' : ' . $monCompte . '</font></div>';
  136. } else {
  137. print '<div><font color="red">' . $langs->trans("ErrorDB") . ' : ' . $langs->trans("Lineofinvoice") . ' ' . $monId . ' ' . $langs->trans("NotVentilatedinAccount") . ' : ' . $monCompte . '<br/> <pre>' . $sql . '</pre></font></div>';
  138. }
  139. $cpt ++;
  140. }
  141. } else {
  142. print '<div><font color="red">' . $langs->trans("AnyLineVentilate") . '</font></div>';
  143. }
  144. print '<div><font color="red">' . $langs->trans("EndProcessing") . '</font></div>';
  145. }
  146. /*
  147. * Customer Invoice lines
  148. */
  149. $page = GETPOST('page');
  150. if ($page < 0)
  151. $page = 0;
  152. if (! empty($conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION)) {
  153. $limit = $conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION;
  154. } else if ($conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION <= 0) {
  155. $limit = $conf->liste_limit;
  156. } else {
  157. $limit = $conf->liste_limit;
  158. }
  159. $offset = $limit * $page;
  160. $sql = "SELECT f.facnumber, f.rowid as facid, l.fk_product, l.description, l.total_ht, l.rowid, l.fk_code_ventilation,";
  161. $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";
  162. // A REVOIR elarifr si vraiment necessaire de rajouter , p.fk_product_type as type. le type produit / service est de facto defini pour chaque ligne de facturedet.product_type
  163. // il est donc plus logique de se servir de l.product_type au lieu de p.fk_product_type
  164. $sql .= " , aa.rowid as aarowid";
  165. // we need f.datef to reorder lines
  166. $sql .= " , f.datef";
  167. // we need to use llx_facturedet l.product_type as used at the time on invoice. if llx_product fk_product_type is changed later it could not change the sell already made !
  168. $sql .= " , l.product_type as type_l";
  169. $sql .= " FROM " . MAIN_DB_PREFIX . "facture as f";
  170. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "facturedet as l ON f.rowid = l.fk_facture";
  171. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product as p ON p.rowid = l.fk_product";
  172. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accountingaccount as aa ON p.accountancy_code_sell = aa.account_number";
  173. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_system as accsys ON accsys.pcg_version = aa.fk_pcg_version";
  174. $sql .= " WHERE f.fk_statut > 0 AND fk_code_ventilation <= 0";
  175. $sql .= " AND (accsys.rowid='" . $conf->global->CHARTOFACCOUNTS . "' OR p.accountancy_code_sell IS NULL OR p.accountancy_code_sell ='')";
  176. // Add search filter like
  177. if (strlen(trim($search_ref))) {
  178. $sql .= " AND (p.ref like '%" . $search_ref . "%')";
  179. }
  180. if (strlen(trim($search_label))) {
  181. $sql .= " AND (p.label like '%" . $search_label . "%')";
  182. }
  183. if (strlen(trim($search_desc))) {
  184. $sql .= " AND (l.description like '%" . $search_desc . "%')";
  185. }
  186. if (! empty($conf->multicompany->enabled)) {
  187. $sql .= " AND f.entity IN (" . getEntity("facture", 1) . ")";
  188. }
  189. //TODO: Remove comment
  190. //replaced by default value $sortfield,$sortorder
  191. //$sql .= " ORDER BY l.rowid";
  192. //if ($conf->global->ACCOUNTING_LIST_SORT_VENTILATION_TODO > 0) {
  193. // $sql .= " DESC ";
  194. //}
  195. $sql.= $db->order($sortfield,$sortorder);
  196. $sql .= $db->plimit($limit + 1, $offset);
  197. dol_syslog("/accountancy/customer/list.php sql=" . $sql, LOG_DEBUG);
  198. $result = $db->query($sql);
  199. if ($result) {
  200. $num_lines = $db->num_rows($result);
  201. $i = 0;
  202. // TODO : print_barre_liste always use $conf->liste_limit and do not care about custom limit in list...
  203. print_barre_liste($langs->trans("InvoiceLines"), $page, $_SERVER["PHP_SELF"], "", $sortfield, $sortorder, '', $num_lines);
  204. print '<br><b>' . $langs->trans("DescVentilTodoCustomer") . '</b></br>';
  205. print '<form action="' . $_SERVER["PHP_SELF"] . '" method="post">' . "\n";
  206. print '<input type="hidden" name="action" value="ventil">';
  207. print '<table class="noborder" width="100%">';
  208. print '<tr class="liste_titre">';
  209. print_liste_field_titre($langs->trans("Invoice"), $_SERVER["PHP_SELF"],"f.facnumber","",$param,'',$sortfield,$sortorder);
  210. print_liste_field_titre($langs->trans("Ref"), $_SERVER["PHP_SELF"],"p.ref","",$param,'',$sortfield,$sortorder);
  211. print_liste_field_titre($langs->trans("Label"), $_SERVER["PHP_SELF"],"p.label","",$param,'',$sortfield,$sortorder);
  212. print_liste_field_titre($langs->trans("Description"), $_SERVER["PHP_SELF"],"l.description","",$param,'',$sortfield,$sortorder);
  213. print_liste_field_titre($langs->trans("Amount"),'','','','','align="right"');
  214. print_liste_field_titre($langs->trans("AccountAccounting"),'','','','','align="center"');
  215. print_liste_field_titre($langs->trans("IntoAccount"),'','','','','align="center"');
  216. print_liste_field_titre('');
  217. print_liste_field_titre($langs->trans("Ventilate") . '<br><label id="select-all">'.$langs->trans('All').'</label>/<label id="unselect-all">'.$langs->trans('None').'</label>','','','','','align="center"');
  218. print '</tr>';
  219. // We add search filter
  220. /* But Hit Enter will validate ventilation....
  221. print '<tr class="liste_titre">';
  222. print '<td class="liste_titre" >&nbsp;</td>';
  223. print '<td class="liste_titre"><input type="text" class="flat" size="10" name="search_ref" value="' . $search_ref . '"></td>';
  224. print '<td class="liste_titre"><input type="text" class="flat" size="20" name="search_label" value="' . $search_label . '"></td>';
  225. print '<td class="liste_titre"><input type="text" class="flat" size="30" name="search_desc" value="' . $search_desc . '"></td>';
  226. print '<td class="liste_titre" colspan="3">&nbsp;</td>';
  227. print '<td align="right" colspan="2" class="liste_titre">';
  228. print '<input type="image" class="liste_titre" src="'.img_picto($langs->trans("Search"),'search.png','','',1).'" name="button_search" value="'.dol_escape_htmltag($langs->trans("Search")).'" title="'.dol_escape_htmltag($langs->trans("Search")).'">';
  229. print '&nbsp;';
  230. print '<input type="image" class="liste_titre" src="'.img_picto($langs->trans("Search"),'searchclear.png','','',1).'" name="button_removefilter" value="'.dol_escape_htmltag($langs->trans("RemoveFilter")).'" title="'.dol_escape_htmltag($langs->trans("RemoveFilter")).'">';
  231. print '</td>';
  232. print '</tr>';
  233. */
  234. $facture_static = new Facture($db);
  235. $product_static = new Product($db);
  236. $form = new Form($db);
  237. $var = true;
  238. while ( $i < min($num_lines, $limit) ) {
  239. $objp = $db->fetch_object($result);
  240. $var = ! $var;
  241. // product_type: 0 = service ? 1 = product
  242. // C'est le contraire dans les base !!!!!! IT IS INVERTED IN LLX_PRODUCT & LLX_FACTUREDET
  243. // elarifr define account numbercode comptable si pas defini dans la fiche produit a partir des lignes de facturation
  244. // product_type: 1 = service ? 0 = product
  245. // because some modules like subtotal module l.product_type can be other than 0 or 1 ! and we don't put in account lines with product_type=9
  246. // first we check product.fk_product_type as type and l.product_type as type_l
  247. // if product does not exist we use the value of l.product_type provided in facturedet to define if this is a product or service
  248. // issue : if we change product_type value in product DB it should differ from the value stored in facturedet DB ! so we report both and user make choice of accounting account.
  249. $objp->code_sell_l = '';
  250. $objp->code_sell_p = '';
  251. $objp->aarowid_suggest = '';
  252. $code_sell_p_l_differ = '';
  253. //check if code_sell defined in product or set default value according p.fk_product_type do not care lines when product_type value not 0 || 1
  254. //and we set suggested accounting account rowid as $objp->aarowid_s
  255. $code_sell_p_notset = '';
  256. $objp->aarowid_suggest = $objp->aarowid;
  257. if ( ! empty($objp->code_sell)) {
  258. $objp->code_sell_p = $objp->code_sell;
  259. } else {
  260. $code_sell_p_notset = 'color:red';
  261. if ($objp->type == 1) {
  262. $objp->code_sell_p = (! empty($conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT : $langs->trans("CodeNotDef"));
  263. }
  264. elseif ($objp->type == 0) {
  265. $objp->code_sell_p = (! empty($conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT : $langs->trans("CodeNotDef"));
  266. }
  267. }
  268. // check facturedet.product_type & set default value according l.type_l, do not care lines when product_type value not 0 || 1
  269. // if ( ! empty($objp->type_l)) {
  270. // $objp->code_sell_l = $objp->type_l;
  271. // } else {
  272. if ($objp->type_l == 1) {
  273. $objp->code_sell_l = (! empty($conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT : $langs->trans("CodeNotDef"));
  274. if ($objp->aarowid == '') $objp->aarowid_suggest = $aarowid_s;
  275. }
  276. elseif ($objp->type_l == 0) {
  277. $objp->code_sell_l = (! empty($conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT : $langs->trans("CodeNotDef"));
  278. if ($objp->aarowid == '') $objp->aarowid_suggest = $aarowid_p;
  279. }
  280. // }
  281. //if not same code for product fk_prouct_type and facturedet.product_type, product has been change after sale and must report
  282. if ($objp->code_sell_l <> $objp->code_sell_p) $code_sell_p_l_differ = 'color:red';
  283. print "<tr $bc[$var]>";
  284. // Ref Invoice
  285. $facture_static->ref = $objp->facnumber;
  286. $facture_static->id = $objp->facid;
  287. print '<td>' . $facture_static->getNomUrl(1) . '</td>';
  288. // Ref Customer Invoice
  289. $product_static->ref = $objp->product_ref;
  290. $product_static->id = $objp->product_id;
  291. $product_static->type = $objp->type;
  292. print '<td>';
  293. if ($product_static->id)
  294. print $product_static->getNomUrl(1);
  295. else
  296. print '&nbsp;';
  297. print '</td>';
  298. print '<td style="' . $code_sell_p_l_differ . '">' . dol_trunc($objp->product_label, 24) . '</td>';
  299. //TODO: we should set a user defined value to adjust user square / wide screen size
  300. $trunclength = defined('ACCOUNTING_LENGTH_DESCRIPTION') ? ACCOUNTING_LENGTH_DESCRIPTION : 32;
  301. print '<td style="' . $code_sell_p_l_differ . '">' . nl2br(dol_trunc($objp->description, $trunclength)) . '</td>';
  302. print '<td align="right">';
  303. print price($objp->total_ht);
  304. print '</td>';
  305. print '<td align="center" style="' . $code_sell_p_notset . '">';
  306. // if not same kind of product_type stored in product & facturedet we display both account and let user choose
  307. if ($objp->code_sell_l == $objp->code_sell_p) {
  308. print $objp->code_sell_l;
  309. } else {
  310. print 'lines='.$objp->code_sell_l . '<br />product=' . $objp->code_sell_p;
  311. }
  312. print '</td>';
  313. // Colonne choix du compte
  314. print '<td align="center">';
  315. // TODO: we should set a user defined value to adjust user square / wide screen size
  316. // $trunclengthform = defined('ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT') ? ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT : 50;
  317. print $formventilation->select_account($objp->aarowid_suggest, 'codeventil[]', 1);
  318. print '</td>';
  319. print '<td align="center">' . $objp->rowid . '</td>';
  320. // Colonne choix ligne a ventiler
  321. print '<td align="center">';
  322. //TODO checked only if account exist in product, if only suggested do not check, user must validate
  323. print '<input type="checkbox" name="mesCasesCochees[]" value="' . $objp->rowid . "_" . $i . '"' . ($objp->aarowid_suggest ? "checked" : "") . '/>';
  324. print '</td>';
  325. //debug
  326. //print '</tr><tr><td colspan=6>Product: p.type='. $objp->type .' - p.code_sell='. $objp->code_sell .' --- Check code_sell_product=' . $objp->code_sell_p .' ---Check facturedet l.type_l='. $objp->type_l .' - code_sell_lines=' . $objp->code_sell_l . ' -- aarowid_suggest=' . $objp->aarowid_suggest.'</td>';
  327. print '</tr>';
  328. $i ++;
  329. }
  330. print '</table>';
  331. print '<br><div align="center"><input type="submit" class="butAction" value="' . $langs->trans("Ventilate") . '"></div>';
  332. print '</form>';
  333. } else {
  334. print $db->error();
  335. }
  336. llxFooter();
  337. $db->close();