list.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. <?php
  2. /* Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
  3. * Copyright (C) 2013-2021 Alexandre Spangaro <aspangaro@open-dsi.fr>
  4. * Copyright (C) 2014-2015 Ari Elbaz (elarifr) <github@accedinfo.com>
  5. * Copyright (C) 2013-2021 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. * Copyright (C) 2021 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  22. */
  23. /**
  24. * \file htdocs/accountancy/customer/list.php
  25. * \ingroup Accountancy (Double entries)
  26. * \brief Ventilation page from customers invoices
  27. */
  28. require '../../main.inc.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formaccounting.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  33. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  34. require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingaccount.class.php';
  35. require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
  36. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  37. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  38. // Load translation files required by the page
  39. $langs->loadLangs(array("bills", "companies", "compta", "accountancy", "other", "productbatch", "products"));
  40. $action = GETPOST('action', 'aZ09');
  41. $massaction = GETPOST('massaction', 'alpha');
  42. $show_files = GETPOST('show_files', 'int');
  43. $confirm = GETPOST('confirm', 'alpha');
  44. $toselect = GETPOST('toselect', 'array');
  45. $optioncss = GETPOST('optioncss', 'alpha');
  46. $default_account = GETPOST('default_account', 'int');
  47. // Select Box
  48. $mesCasesCochees = GETPOST('toselect', 'array');
  49. // Search Getpost
  50. $search_societe = GETPOST('search_societe', 'alpha');
  51. $search_lineid = GETPOST('search_lineid', 'int');
  52. $search_ref = GETPOST('search_ref', 'alpha');
  53. $search_invoice = GETPOST('search_invoice', 'alpha');
  54. $search_label = GETPOST('search_label', 'alpha');
  55. $search_desc = GETPOST('search_desc', 'alpha');
  56. $search_amount = GETPOST('search_amount', 'alpha');
  57. $search_account = GETPOST('search_account', 'alpha');
  58. $search_vat = GETPOST('search_vat', 'alpha');
  59. $search_date_startday = GETPOST('search_date_startday', 'int');
  60. $search_date_startmonth = GETPOST('search_date_startmonth', 'int');
  61. $search_date_startyear = GETPOST('search_date_startyear', 'int');
  62. $search_date_endday = GETPOST('search_date_endday', 'int');
  63. $search_date_endmonth = GETPOST('search_date_endmonth', 'int');
  64. $search_date_endyear = GETPOST('search_date_endyear', 'int');
  65. $search_date_start = dol_mktime(0, 0, 0, $search_date_startmonth, $search_date_startday, $search_date_startyear); // Use tzserver
  66. $search_date_end = dol_mktime(23, 59, 59, $search_date_endmonth, $search_date_endday, $search_date_endyear);
  67. $search_country = GETPOST('search_country', 'alpha');
  68. $search_tvaintra = GETPOST('search_tvaintra', 'alpha');
  69. $btn_ventil = GETPOST('ventil', 'alpha');
  70. // Load variable for pagination
  71. $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : (empty($conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION) ? $conf->liste_limit : $conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION);
  72. $sortfield = GETPOST('sortfield', 'aZ09comma');
  73. $sortorder = GETPOST('sortorder', 'aZ09comma');
  74. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  75. if (empty($page) || $page < 0) {
  76. $page = 0;
  77. }
  78. $offset = $limit * $page;
  79. $pageprev = $page - 1;
  80. $pagenext = $page + 1;
  81. if (!$sortfield) {
  82. $sortfield = "f.datef, f.ref, l.rowid";
  83. }
  84. if (!$sortorder) {
  85. if ($conf->global->ACCOUNTING_LIST_SORT_VENTILATION_TODO > 0) {
  86. $sortorder = "DESC";
  87. }
  88. }
  89. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  90. $hookmanager->initHooks(array('accountancycustomerlist'));
  91. $formaccounting = new FormAccounting($db);
  92. $accountingAccount = new AccountingAccount($db);
  93. $chartaccountcode = dol_getIdFromCode($db, $conf->global->CHARTOFACCOUNTS, 'accounting_system', 'rowid', 'pcg_version');
  94. // Security check
  95. if (empty($conf->accounting->enabled)) {
  96. accessforbidden();
  97. }
  98. if ($user->socid > 0) {
  99. accessforbidden();
  100. }
  101. if (empty($user->rights->accounting->mouvements->lire)) {
  102. accessforbidden();
  103. }
  104. /*
  105. * Actions
  106. */
  107. if (GETPOST('cancel', 'alpha')) {
  108. $action = 'list'; $massaction = '';
  109. }
  110. if (!GETPOST('confirmmassaction', 'alpha') && $massaction != 'presend' && $massaction != 'confirm_presend') {
  111. $massaction = '';
  112. }
  113. $parameters = array();
  114. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  115. if ($reshook < 0) {
  116. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  117. }
  118. if (empty($reshook)) {
  119. // Purge search criteria
  120. if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // All test are required to be compatible with all browsers
  121. $search_societe = '';
  122. $search_lineid = '';
  123. $search_ref = '';
  124. $search_invoice = '';
  125. $search_label = '';
  126. $search_desc = '';
  127. $search_amount = '';
  128. $search_account = '';
  129. $search_vat = '';
  130. $search_date_startday = '';
  131. $search_date_startmonth = '';
  132. $search_date_startyear = '';
  133. $search_date_endday = '';
  134. $search_date_endmonth = '';
  135. $search_date_endyear = '';
  136. $search_date_start = '';
  137. $search_date_end = '';
  138. $search_country = '';
  139. $search_tvaintra = '';
  140. }
  141. // Mass actions
  142. $objectclass = 'AccountingAccount';
  143. $permissiontoread = $user->rights->accounting->read;
  144. $permissiontodelete = $user->rights->accounting->delete;
  145. $uploaddir = $conf->accounting->dir_output;
  146. include DOL_DOCUMENT_ROOT.'/core/actions_massactions.inc.php';
  147. }
  148. if ($massaction == 'ventil' && $user->rights->accounting->bind->write) {
  149. $msg = '';
  150. //print '<div><span style="color:red">' . $langs->trans("Processing") . '...</span></div>';
  151. if (!empty($mesCasesCochees)) {
  152. $msg = '<div>'.$langs->trans("SelectedLines").': '.count($mesCasesCochees).'</div>';
  153. $msg .= '<div class="detail">';
  154. $cpt = 0;
  155. $ok = 0;
  156. $ko = 0;
  157. foreach ($mesCasesCochees as $maLigneCochee) {
  158. $maLigneCourante = explode("_", $maLigneCochee);
  159. $monId = $maLigneCourante[0];
  160. $monCompte = GETPOST('codeventil'.$monId);
  161. if ($monCompte <= 0) {
  162. $msg .= '<div><span style="color:red">'.$langs->trans("Lineofinvoice").' '.$monId.' - '.$langs->trans("NoAccountSelected").'</span></div>';
  163. $ko++;
  164. } else {
  165. $sql = " UPDATE ".MAIN_DB_PREFIX."facturedet";
  166. $sql .= " SET fk_code_ventilation = ".((int) $monCompte);
  167. $sql .= " WHERE rowid = ".((int) $monId);
  168. $accountventilated = new AccountingAccount($db);
  169. $accountventilated->fetch($monCompte, '', 1);
  170. dol_syslog("accountancy/customer/list.php", LOG_DEBUG);
  171. if ($db->query($sql)) {
  172. $msg .= '<div><span style="color:green">'.$langs->trans("Lineofinvoice").' '.$monId.' - '.$langs->trans("VentilatedinAccount").' : '.length_accountg($accountventilated->account_number).'</span></div>';
  173. $ok++;
  174. } else {
  175. $msg .= '<div><span style="color:red">'.$langs->trans("ErrorDB").' : '.$langs->trans("Lineofinvoice").' '.$monId.' - '.$langs->trans("NotVentilatedinAccount").' : '.length_accountg($accountventilated->account_number).'<br> <pre>'.$sql.'</pre></span></div>';
  176. $ko++;
  177. }
  178. }
  179. $cpt++;
  180. }
  181. $msg .= '</div>';
  182. $msg .= '<div>'.$langs->trans("EndProcessing").'</div>';
  183. }
  184. }
  185. /*
  186. * View
  187. */
  188. $form = new Form($db);
  189. $formother = new FormOther($db);
  190. llxHeader('', $langs->trans("Ventilation"));
  191. if (empty($chartaccountcode)) {
  192. print $langs->trans("ErrorChartOfAccountSystemNotSelected");
  193. // End of page
  194. llxFooter();
  195. $db->close();
  196. exit;
  197. }
  198. // Customer Invoice lines
  199. $sql = "SELECT f.rowid as facid, f.ref, f.datef, f.type as ftype,";
  200. $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, l.vat_src_code,";
  201. $sql .= " p.rowid as product_id, p.ref as product_ref, p.label as product_label, p.fk_product_type as type, p.tva_tx as tva_tx_prod,";
  202. if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
  203. $sql .= " ppe.accountancy_code_sell as code_sell, ppe.accountancy_code_sell_intra as code_sell_intra, ppe.accountancy_code_sell_export as code_sell_export,";
  204. $sql .= " ppe.accountancy_code_buy as code_buy, ppe.accountancy_code_buy_intra as code_buy_intra, ppe.accountancy_code_buy_export as code_buy_export,";
  205. } else {
  206. $sql .= " p.accountancy_code_sell as code_sell, p.accountancy_code_sell_intra as code_sell_intra, p.accountancy_code_sell_export as code_sell_export,";
  207. $sql .= " p.accountancy_code_buy as code_buy, p.accountancy_code_buy_intra as code_buy_intra, p.accountancy_code_buy_export as code_buy_export,";
  208. }
  209. $sql .= " p.tosell as status, p.tobuy as status_buy,";
  210. $sql .= " aa.rowid as aarowid, aa2.rowid as aarowid_intra, aa3.rowid as aarowid_export, aa4.rowid as aarowid_thirdparty,";
  211. $sql .= " co.code as country_code, co.label as country_label,";
  212. $sql .= " s.rowid as socid, s.nom as name, s.tva_intra, s.email, s.town, s.zip, s.fk_pays, s.client, s.fournisseur, s.code_client, s.code_fournisseur,";
  213. if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
  214. $sql .= " spe.accountancy_code_customer as code_compta_client,";
  215. $sql .= " spe.accountancy_code_supplier as code_compta_fournisseur,";
  216. $sql .= " spe.accountancy_code_sell as company_code_sell";
  217. } else {
  218. $sql .= " s.code_compta as code_compta_client,";
  219. $sql .= " s.code_compta_fournisseur,";
  220. $sql .= " s.accountancy_code_sell as company_code_sell";
  221. }
  222. $parameters = array();
  223. $reshook = $hookmanager->executeHooks('printFieldListSelect', $parameters); // Note that $action and $object may have been modified by hook
  224. $sql .= $hookmanager->resPrint;
  225. $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
  226. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc";
  227. if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
  228. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe_perentity as spe ON spe.fk_soc = s.rowid AND spe.entity = " . ((int) $conf->entity);
  229. }
  230. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co ON co.rowid = s.fk_pays ";
  231. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."facturedet as l ON f.rowid = l.fk_facture";
  232. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = l.fk_product";
  233. if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
  234. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product_perentity as ppe ON ppe.fk_product = p.rowid AND ppe.entity = " . ((int) $conf->entity);
  235. }
  236. $alias_societe_perentity = empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED) ? "s" : "spe";
  237. $alias_product_perentity = empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED) ? "p" : "ppe";
  238. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa ON " . $alias_product_perentity . ".accountancy_code_sell = aa.account_number AND aa.active = 1 AND aa.fk_pcg_version = '".$db->escape($chartaccountcode)."' AND aa.entity = ".$conf->entity;
  239. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa2 ON " . $alias_product_perentity . ".accountancy_code_sell_intra = aa2.account_number AND aa2.active = 1 AND aa2.fk_pcg_version = '".$db->escape($chartaccountcode)."' AND aa2.entity = ".$conf->entity;
  240. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa3 ON " . $alias_product_perentity . ".accountancy_code_sell_export = aa3.account_number AND aa3.active = 1 AND aa3.fk_pcg_version = '".$db->escape($chartaccountcode)."' AND aa3.entity = ".$conf->entity;
  241. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa4 ON " . $alias_societe_perentity . ".accountancy_code_sell = aa4.account_number AND aa4.active = 1 AND aa4.fk_pcg_version = '".$db->escape($chartaccountcode)."' AND aa4.entity = ".$conf->entity;
  242. $sql .= " WHERE f.fk_statut > 0 AND l.fk_code_ventilation <= 0";
  243. $sql .= " AND l.product_type <= 2";
  244. // Define begin binding date
  245. if (!empty($conf->global->ACCOUNTING_DATE_START_BINDING)) {
  246. $sql .= " AND f.datef >= '".$db->idate($conf->global->ACCOUNTING_DATE_START_BINDING)."'";
  247. }
  248. // Add search filter like
  249. if ($search_societe) {
  250. $sql .= natural_search('s.nom', $search_societe);
  251. }
  252. if ($search_lineid) {
  253. $sql .= natural_search("l.rowid", $search_lineid, 1);
  254. }
  255. if (strlen(trim($search_invoice))) {
  256. $sql .= natural_search("f.ref", $search_invoice);
  257. }
  258. if (strlen(trim($search_ref))) {
  259. $sql .= natural_search("p.ref", $search_ref);
  260. }
  261. if (strlen(trim($search_label))) {
  262. $sql .= natural_search("p.label", $search_label);
  263. }
  264. if (strlen(trim($search_desc))) {
  265. $sql .= natural_search("l.description", $search_desc);
  266. }
  267. if (strlen(trim($search_amount))) {
  268. $sql .= natural_search("l.total_ht", $search_amount, 1);
  269. }
  270. if (strlen(trim($search_account))) {
  271. $sql .= natural_search("aa.account_number", $search_account);
  272. }
  273. if (strlen(trim($search_vat))) {
  274. $sql .= natural_search("l.tva_tx", price2num($search_vat), 1);
  275. }
  276. if ($search_date_start) {
  277. $sql .= " AND f.datef >= '".$db->idate($search_date_start)."'";
  278. }
  279. if ($search_date_end) {
  280. $sql .= " AND f.datef <= '".$db->idate($search_date_end)."'";
  281. }
  282. if (strlen(trim($search_country))) {
  283. $arrayofcode = getCountriesInEEC();
  284. $country_code_in_EEC = $country_code_in_EEC_without_me = '';
  285. foreach ($arrayofcode as $key => $value) {
  286. $country_code_in_EEC .= ($country_code_in_EEC ? "," : "")."'".$value."'";
  287. if ($value != $mysoc->country_code) {
  288. $country_code_in_EEC_without_me .= ($country_code_in_EEC_without_me ? "," : "")."'".$value."'";
  289. }
  290. }
  291. if ($search_country == 'special_allnotme') {
  292. $sql .= " AND co.code <> '".$db->escape($mysoc->country_code)."'";
  293. } elseif ($search_country == 'special_eec') {
  294. $sql .= " AND co.code IN (".$db->sanitize($country_code_in_EEC, 1).")";
  295. } elseif ($search_country == 'special_eecnotme') {
  296. $sql .= " AND co.code IN (".$db->sanitize($country_code_in_EEC_without_me, 1).")";
  297. } elseif ($search_country == 'special_noteec') {
  298. $sql .= " AND co.code NOT IN (".$db->sanitize($country_code_in_EEC, 1).")";
  299. } else {
  300. $sql .= natural_search("co.code", $search_country);
  301. }
  302. }
  303. if (strlen(trim($search_tvaintra))) {
  304. $sql .= natural_search("s.tva_intra", $search_tvaintra);
  305. }
  306. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  307. $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.",".Facture::TYPE_REPLACEMENT.",".Facture::TYPE_CREDIT_NOTE.",".Facture::TYPE_SITUATION.")";
  308. } else {
  309. $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.",".Facture::TYPE_REPLACEMENT.",".Facture::TYPE_CREDIT_NOTE.",".Facture::TYPE_DEPOSIT.",".Facture::TYPE_SITUATION.")";
  310. }
  311. $sql .= " AND f.entity IN (".getEntity('invoice', 0).")"; // We don't share object for accountancy
  312. // Add where from hooks
  313. $parameters = array();
  314. $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters); // Note that $action and $object may have been modified by hook
  315. $sql .= $hookmanager->resPrint;
  316. $sql .= $db->order($sortfield, $sortorder);
  317. // Count total nb of records
  318. $nbtotalofrecords = '';
  319. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
  320. $result = $db->query($sql);
  321. $nbtotalofrecords = $db->num_rows($result);
  322. if (($page * $limit) > $nbtotalofrecords) { // if total resultset is smaller then paging size (filtering), goto and load page 0
  323. $page = 0;
  324. $offset = 0;
  325. }
  326. }
  327. $sql .= $db->plimit($limit + 1, $offset);
  328. dol_syslog("accountancy/customer/list.php", LOG_DEBUG);
  329. // MAX_JOIN_SIZE can be very low (ex: 300000) on some limited configurations (ex: https://www.online.net/fr/hosting/online-perso)
  330. // This big SELECT command may exceed the MAX_JOIN_SIZE limit => Therefore we use SQL_BIG_SELECTS=1 to disable the MAX_JOIN_SIZE security
  331. if ($db->type == 'mysqli') {
  332. $db->query("SET SQL_BIG_SELECTS=1");
  333. }
  334. $result = $db->query($sql);
  335. if ($result) {
  336. $num_lines = $db->num_rows($result);
  337. $i = 0;
  338. $arrayofselected = is_array($toselect) ? $toselect : array();
  339. $param = '';
  340. if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) {
  341. $param .= '&contextpage='.urlencode($contextpage);
  342. }
  343. if ($limit > 0 && $limit != $conf->liste_limit) {
  344. $param .= '&limit='.urlencode($limit);
  345. }
  346. if ($search_societe) {
  347. $param .= '&search_societe='.urlencode($search_societe);
  348. }
  349. if ($search_lineid) {
  350. $param .= '&search_lineid='.urlencode($search_lineid);
  351. }
  352. if ($search_date_startday) {
  353. $param .= '&search_date_startday='.urlencode($search_date_startday);
  354. }
  355. if ($search_date_startmonth) {
  356. $param .= '&search_date_startmonth='.urlencode($search_date_startmonth);
  357. }
  358. if ($search_date_startyear) {
  359. $param .= '&search_date_startyear='.urlencode($search_date_startyear);
  360. }
  361. if ($search_date_endday) {
  362. $param .= '&search_date_endday='.urlencode($search_date_endday);
  363. }
  364. if ($search_date_endmonth) {
  365. $param .= '&search_date_endmonth='.urlencode($search_date_endmonth);
  366. }
  367. if ($search_date_endyear) {
  368. $param .= '&search_date_endyear='.urlencode($search_date_endyear);
  369. }
  370. if ($search_invoice) {
  371. $param .= '&search_invoice='.urlencode($search_invoice);
  372. }
  373. if ($search_ref) {
  374. $param .= '&search_ref='.urlencode($search_ref);
  375. }
  376. if ($search_label) {
  377. $param .= '&search_label='.urlencode($search_label);
  378. }
  379. if ($search_desc) {
  380. $param .= '&search_desc='.urlencode($search_desc);
  381. }
  382. if ($search_amount) {
  383. $param .= '&search_amount='.urlencode($search_amount);
  384. }
  385. if ($search_vat) {
  386. $param .= '&search_vat='.urlencode($search_vat);
  387. }
  388. if ($search_country) {
  389. $param .= "&search_country=".urlencode($search_country);
  390. }
  391. if ($search_tvaintra) {
  392. $param .= "&search_tvaintra=".urlencode($search_tvaintra);
  393. }
  394. $arrayofmassactions = array(
  395. 'ventil'=>img_picto('', 'check', 'class="pictofixedwidth"').$langs->trans("Ventilate")
  396. ,'set_default_account'=>img_picto('', 'check', 'class="pictofixedwidth"').$langs->trans("ConfirmPreselectAccount")
  397. //'presend'=>img_picto('', 'email', 'class="pictofixedwidth"').$langs->trans("SendByMail"),
  398. //'builddoc'=>img_picto('', 'pdf', 'class="pictofixedwidth"').$langs->trans("PDFMerge"),
  399. );
  400. //if ($user->rights->mymodule->supprimer) $arrayofmassactions['predelete'] = img_picto('', 'delete', 'class="pictofixedwidth"').$langs->trans("Delete");
  401. //if (in_array($massaction, array('presend','predelete'))) $arrayofmassactions=array();
  402. if ($massaction !== 'set_default_account') {
  403. $massactionbutton = $form->selectMassAction('ventil', $arrayofmassactions, 1);
  404. }
  405. print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">'."\n";
  406. print '<input type="hidden" name="action" value="ventil">';
  407. if ($optioncss != '') {
  408. print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  409. }
  410. print '<input type="hidden" name="token" value="'.newToken().'">';
  411. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  412. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  413. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  414. print '<input type="hidden" name="page" value="'.$page.'">';
  415. print_barre_liste($langs->trans("InvoiceLines"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num_lines, $nbtotalofrecords, 'title_accountancy', 0, '', '', $limit);
  416. if ($massaction == 'set_default_account') {
  417. $formquestion[]=array('type' => 'other',
  418. 'name' => 'set_default_account',
  419. 'label' => $langs->trans("AccountancyCode"),
  420. 'value' => $formaccounting->select_account('', 'default_account', 1, array(), 0, 0, 'maxwidth200 maxwidthonsmartphone', 'cachewithshowemptyone'));
  421. print $form->formconfirm($_SERVER["PHP_SELF"], $langs->trans("ConfirmPreselectAccount"), $langs->trans("ConfirmPreselectAccountQuestion", count($toselect)), "confirm_set_default_account", $formquestion, 1, 0, 200, 500, 1);
  422. }
  423. print '<span class="opacitymedium">'.$langs->trans("DescVentilTodoCustomer").'</span></br><br>';
  424. if ($msg) {
  425. print $msg.'<br>';
  426. }
  427. $moreforfilter = '';
  428. print '<div class="div-table-responsive">';
  429. print '<table class="tagtable liste'.($moreforfilter ? " listwithfilterbefore" : "").'">'."\n";
  430. // We add search filter
  431. print '<tr class="liste_titre_filter">';
  432. print '<td class="liste_titre"><input type="text" class="flat maxwidth25" name="search_lineid" value="'.dol_escape_htmltag($search_lineid).'"></td>';
  433. print '<td class="liste_titre"><input type="text" class="flat maxwidth50" name="search_invoice" value="'.dol_escape_htmltag($search_invoice).'"></td>';
  434. print '<td class="liste_titre center">';
  435. print '<div class="nowrap">';
  436. print $form->selectDate($search_date_start ? $search_date_start : -1, 'search_date_start', 0, 0, 1, '', 1, 0, 0, '', '', '', '', 1, '', $langs->trans('From'));
  437. print '</div>';
  438. print '<div class="nowrap">';
  439. print $form->selectDate($search_date_end ? $search_date_end : -1, 'search_date_end', 0, 0, 1, '', 1, 0, 0, '', '', '', '', 1, '', $langs->trans('to'));
  440. print '</div>';
  441. print '</td>';
  442. print '<td class="liste_titre"><input type="text" class="flat maxwidth50" name="search_ref" value="'.dol_escape_htmltag($search_ref).'"></td>';
  443. print '<td class="liste_titre"><input type="text" class="flat maxwidth100" name="search_desc" value="'.dol_escape_htmltag($search_desc).'"></td>';
  444. print '<td class="liste_titre right"><input type="text" class="flat maxwidth50 right" name="search_amount" value="'.dol_escape_htmltag($search_amount).'"></td>';
  445. print '<td class="liste_titre right"><input type="text" class="flat maxwidth50 right" name="search_vat" placeholder="%" size="1" value="'.dol_escape_htmltag($search_vat).'"></td>';
  446. print '<td class="liste_titre"><input type="text" class="flat maxwidth75imp" name="search_societe" value="'.dol_escape_htmltag($search_societe).'"></td>';
  447. print '<td class="liste_titre">';
  448. print $form->select_country($search_country, 'search_country', '', 0, 'maxwidth125', 'code2', 1, 0, 1, null, 1);
  449. //print '<input type="text" class="flat maxwidth50" name="search_country" value="' . dol_escape_htmltag($search_country) . '">';
  450. print '</td>';
  451. print '<td class="liste_titre"><input type="text" class="flat maxwidth50" name="search_tvaintra" value="'.dol_escape_htmltag($search_tvaintra).'"></td>';
  452. print '<td class="liste_titre"></td>';
  453. print '<td class="liste_titre"></td>';
  454. print '<td class="center liste_titre">';
  455. $searchpicto = $form->showFilterButtons();
  456. print $searchpicto;
  457. print '</td>';
  458. print "</tr>\n";
  459. print '<tr class="liste_titre">';
  460. print_liste_field_titre("LineId", $_SERVER["PHP_SELF"], "l.rowid", "", $param, '', $sortfield, $sortorder);
  461. print_liste_field_titre("Invoice", $_SERVER["PHP_SELF"], "f.ref", "", $param, '', $sortfield, $sortorder);
  462. print_liste_field_titre("Date", $_SERVER["PHP_SELF"], "f.datef, f.ref, l.rowid", "", $param, '', $sortfield, $sortorder, 'center ');
  463. print_liste_field_titre("ProductRef", $_SERVER["PHP_SELF"], "p.ref", "", $param, '', $sortfield, $sortorder);
  464. //print_liste_field_titre("ProductLabel", $_SERVER["PHP_SELF"], "p.label", "", $param, '', $sortfield, $sortorder);
  465. print_liste_field_titre("ProductDescription", $_SERVER["PHP_SELF"], "l.description", "", $param, '', $sortfield, $sortorder);
  466. print_liste_field_titre("Amount", $_SERVER["PHP_SELF"], "l.total_ht", "", $param, '', $sortfield, $sortorder, 'right maxwidth50 ');
  467. print_liste_field_titre("VATRate", $_SERVER["PHP_SELF"], "l.tva_tx", "", $param, '', $sortfield, $sortorder, 'right ', '', 1);
  468. print_liste_field_titre("ThirdParty", $_SERVER["PHP_SELF"], "s.nom", "", $param, '', $sortfield, $sortorder);
  469. print_liste_field_titre("Country", $_SERVER["PHP_SELF"], "co.label", "", $param, '', $sortfield, $sortorder);
  470. print_liste_field_titre("VATIntraShort", $_SERVER["PHP_SELF"], "s.tva_intra", "", $param, '', $sortfield, $sortorder);
  471. print_liste_field_titre("AccountAccountingSuggest", '', '', '', '', '', '', '', 'nowraponall ');
  472. print_liste_field_titre("IntoAccount", '', '', '', '', '', '', '', 'center ');
  473. $checkpicto = '';
  474. if ($massactionbutton) {
  475. $checkpicto = $form->showCheckAddButtons('checkforselect', 1);
  476. }
  477. print_liste_field_titre($checkpicto, '', '', '', '', '', '', '', 'center ');
  478. print "</tr>\n";
  479. $thirdpartystatic = new Societe($db);
  480. $facture_static = new Facture($db);
  481. $facture_static_det = new FactureLigne($db);
  482. $product_static = new Product($db);
  483. $accountingaccount_codetotid_cache = array();
  484. while ($i < min($num_lines, $limit)) {
  485. $objp = $db->fetch_object($result);
  486. // product_type: 0 = service, 1 = product
  487. // if product does not exist we use the value of product_type provided in facturedet to define if this is a product or service
  488. // issue : if we change product_type value in product DB it should differ from the value stored in facturedet DB !
  489. $code_sell_l = '';
  490. $code_sell_p = '';
  491. $code_sell_t = '';
  492. $thirdpartystatic->id = $objp->socid;
  493. $thirdpartystatic->name = $objp->name;
  494. $thirdpartystatic->client = $objp->client;
  495. $thirdpartystatic->fournisseur = $objp->fournisseur;
  496. $thirdpartystatic->code_client = $objp->code_client;
  497. $thirdpartystatic->code_compta = $objp->code_compta_client; // For backward compatibility
  498. $thirdpartystatic->code_compta_client = $objp->code_compta_client;
  499. $thirdpartystatic->code_fournisseur = $objp->code_fournisseur;
  500. $thirdpartystatic->code_compta_fournisseur = $objp->code_compta_fournisseur;
  501. $thirdpartystatic->email = $objp->email;
  502. $thirdpartystatic->country_code = $objp->country_code;
  503. $thirdpartystatic->tva_intra = $objp->tva_intra;
  504. $thirdpartystatic->code_compta_product = $objp->company_code_sell; // The accounting account for product stored on thirdparty object (for level3 suggestion)
  505. $product_static->ref = $objp->product_ref;
  506. $product_static->id = $objp->product_id;
  507. $product_static->type = $objp->type;
  508. $product_static->label = $objp->product_label;
  509. $product_static->status = $objp->status;
  510. $product_static->status_buy = $objp->status_buy;
  511. $product_static->accountancy_code_sell = $objp->code_sell;
  512. $product_static->accountancy_code_sell_intra = $objp->code_sell_intra;
  513. $product_static->accountancy_code_sell_export = $objp->code_sell_export;
  514. $product_static->accountancy_code_buy = $objp->code_buy;
  515. $product_static->accountancy_code_buy_intra = $objp->code_buy_intra;
  516. $product_static->accountancy_code_buy_export = $objp->code_buy_export;
  517. $product_static->tva_tx = $objp->tva_tx_prod;
  518. $facture_static->ref = $objp->ref;
  519. $facture_static->id = $objp->facid;
  520. $facture_static->type = $objp->ftype;
  521. $facture_static->date = $db->jdate($objp->datef);
  522. $facture_static_det->id = $objp->rowid;
  523. $facture_static_det->total_ht = $objp->total_ht;
  524. $facture_static_det->tva_tx = $objp->tva_tx_line;
  525. $facture_static_det->vat_src_code = $objp->vat_src_code;
  526. $facture_static_det->product_type = $objp->type_l;
  527. $facture_static_det->desc = $objp->description;
  528. $accountingAccountArray = array(
  529. 'dom'=>$objp->aarowid,
  530. 'intra'=>$objp->aarowid_intra,
  531. 'export'=>$objp->aarowid_export,
  532. 'thirdparty' =>$objp->aarowid_thirdparty);
  533. $code_sell_p_notset = '';
  534. $code_sell_t_notset = '';
  535. $suggestedid = 0;
  536. $return=$accountingAccount->getAccountingCodeToBind($thirdpartystatic, $mysoc, $product_static, $facture_static, $facture_static_det, $accountingAccountArray, 'customer');
  537. if (!is_array($return) && $return<0) {
  538. setEventMessage($accountingAccount->error, 'errors');
  539. } else {
  540. $suggestedid=$return['suggestedid'];
  541. $suggestedaccountingaccountfor=$return['suggestedaccountingaccountfor'];
  542. $suggestedaccountingaccountbydefaultfor=$return['suggestedaccountingaccountbydefaultfor'];
  543. $code_sell_l=$return['code_l'];
  544. $code_sell_p=$return['code_p'];
  545. $code_sell_t=$return['code_t'];
  546. }
  547. //var_dump($return);
  548. if (!empty($code_sell_p)) {
  549. // Value was defined previously
  550. } else {
  551. $code_sell_p_notset = 'color:orange';
  552. }
  553. if (empty($code_sell_l) && empty($code_sell_p)) {
  554. $code_sell_p_notset = 'color:red';
  555. }
  556. if ($suggestedaccountingaccountfor == 'eecwithoutvatnumber' && empty($code_sell_p_notset)) {
  557. $code_sell_p_notset = 'color:orange';
  558. }
  559. // $code_sell_l is now default code of product/service
  560. // $code_sell_p is now code of product/service
  561. // $code_sell_t is now code of thirdparty
  562. //var_dump($code_sell_l.' - '.$code_sell_p.' - '.$code_sell_t.' -> '.$suggestedid.' ('.$suggestedaccountingaccountbydefaultfor.' '.$suggestedaccountingaccountfor.')');
  563. print '<tr class="oddeven">';
  564. // Line id
  565. print '<td>'.$facture_static_det->id.'</td>';
  566. // Ref Invoice
  567. print '<td class="nowraponall">'.$facture_static->getNomUrl(1).'</td>';
  568. print '<td class="center">'.dol_print_date($facture_static->date, 'day').'</td>';
  569. // Ref Product
  570. print '<td class="tdoverflowmax150">';
  571. if ($product_static->id > 0) {
  572. print $product_static->getNomUrl(1);
  573. }
  574. if ($product_static->label) {
  575. print '<br><span class="opacitymedium small">'.dol_escape_htmltag($product_static->label).'</span>';
  576. }
  577. print '</td>';
  578. // Description
  579. print '<td class="tdoverflowonsmartphone small">';
  580. $text = dolGetFirstLineOfText(dol_string_nohtmltag($facture_static_det->desc, 1));
  581. $trunclength = empty($conf->global->ACCOUNTING_LENGTH_DESCRIPTION) ? 32 : $conf->global->ACCOUNTING_LENGTH_DESCRIPTION;
  582. print $form->textwithtooltip(dol_trunc($text, $trunclength), $facture_static_det->desc);
  583. print '</td>';
  584. print '<td class="right nowraponall amount">';
  585. print price($objp->total_ht);
  586. print '</td>';
  587. // Vat rate
  588. $code_vat_differ = '';
  589. if ($product_static->tva_tx !== $facture_static_det->tva_tx && price2num($product_static->tva_tx) && price2num($facture_static_det->tva_tx)) { // Note: having a vat rate of 0 is often the normal case when sells is intra b2b or to export
  590. $code_vat_differ = 'warning bold';
  591. }
  592. print '<td class="right'.($code_vat_differ?' '.$code_vat_differ:'').'">';
  593. print vatrate($facture_static_det->tva_tx.($facture_static_det->vat_src_code ? ' ('.$facture_static_det->vat_src_code.')' : ''));
  594. print '</td>';
  595. // Thirdparty
  596. print '<td class="tdoverflowmax100">'.$thirdpartystatic->getNomUrl(1, 'customer').'</td>';
  597. // Country
  598. $labelcountry = ($objp->country_code && ($langs->trans("Country".$objp->country_code) != "Country".$objp->country_code)) ? $langs->trans("Country".$objp->country_code) : $objp->country_label;
  599. print '<td class="tdoverflowmax100" title="'.dol_escape_htmltag($labelcountry).'">';
  600. print dol_escape_htmltag($labelcountry);
  601. print '</td>';
  602. // VAT Num
  603. print '<td class="tdoverflowmax80" title="'.dol_escape_htmltag($objp->tva_intra).'">'.dol_escape_htmltag($objp->tva_intra).'</td>';
  604. // Found accounts
  605. print '<td class="small">';
  606. $s = '1. '.(($facture_static_det->product_type == 1) ? $langs->trans("DefaultForService") : $langs->trans("DefaultForProduct")).': ';
  607. $shelp = '';
  608. if ($suggestedaccountingaccountbydefaultfor == 'eec') {
  609. $shelp .= $langs->trans("SaleEEC");
  610. } elseif ($suggestedaccountingaccountbydefaultfor == 'export') {
  611. $shelp .= $langs->trans("SaleExport");
  612. }
  613. $s .= ($code_sell_l > 0 ? length_accountg($code_sell_l) : '<span style="'.$code_sell_p_notset.'">'.$langs->trans("NotDefined").'</span>');
  614. print $form->textwithpicto($s, $shelp, 1, 'help', '', 0, 2, '', 1);
  615. if ($product_static->id > 0) {
  616. print '<br>';
  617. $s = '2. '.(($facture_static_det->product_type == 1) ? $langs->trans("ThisService") : $langs->trans("ThisProduct")).': ';
  618. $shelp = ''; $ttype = 'help';
  619. if ($suggestedaccountingaccountfor == 'eec') {
  620. $shelp = $langs->trans("SaleEEC");
  621. } elseif ($suggestedaccountingaccountfor == 'eecwithvat') {
  622. $shelp = $langs->trans("SaleEECWithVAT");
  623. } elseif ($suggestedaccountingaccountfor == 'eecwithoutvatnumber') {
  624. $shelp = $langs->trans("SaleEECWithoutVATNumber");
  625. $ttype = 'warning';
  626. } elseif ($suggestedaccountingaccountfor == 'export') {
  627. $shelp = $langs->trans("SaleExport");
  628. }
  629. $s .= (empty($code_sell_p) ? '<span style="'.$code_sell_p_notset.'">'.$langs->trans("NotDefined").'</span>' : length_accountg($code_sell_p));
  630. print $form->textwithpicto($s, $shelp, 1, $ttype, '', 0, 2, '', 1);
  631. } else {
  632. print '<br>';
  633. $s = '2. '.(($objp->type_l == 1) ? $langs->trans("ThisService") : $langs->trans("ThisProduct")).': ';
  634. $shelp = '';
  635. $s .= $langs->trans("NotDefined");
  636. print $form->textwithpicto($s, $shelp, 1, 'help', '', 0, 2, '', 1);
  637. }
  638. if (!empty($conf->global->ACCOUNTANCY_USE_PRODUCT_ACCOUNT_ON_THIRDPARTY)) {
  639. print '<br>';
  640. $s = '3. '.(($facture_static_det->product_type == 1) ? $langs->trans("ServiceForThisThirdparty") : $langs->trans("ProductForThisThirdparty")).': ';
  641. $shelp = '';
  642. $s .= ($code_sell_t > 0 ? length_accountg($code_sell_t) : '<span style="'.$code_sell_t_notset.'">'.$langs->trans("NotDefined").'</span>');
  643. print $form->textwithpicto($s, $shelp, 1, 'help', '', 0, 2, '', 1);
  644. }
  645. print '</td>';
  646. // Suggested accounting account
  647. print '<td>';
  648. print $formaccounting->select_account(($default_account > 0 && $confirm === 'yes' && in_array($objp->rowid."_".$i, $toselect)) ? $default_account : $suggestedid, 'codeventil'.$facture_static_det->id, 1, array(), 0, 0, 'codeventil maxwidth200 maxwidthonsmartphone', 'cachewithshowemptyone');
  649. print '</td>';
  650. // Column with checkbox
  651. print '<td class="center">';
  652. $ischecked = 0;
  653. if (!empty($suggestedid) && $suggestedaccountingaccountfor != '' && $suggestedaccountingaccountfor != 'eecwithoutvatnumber') {
  654. $ischecked = 1;
  655. }
  656. if (!empty($toselect)) {
  657. $ischecked = 0;
  658. if (in_array($objp->rowid."_".$i, $toselect)) {
  659. $ischecked=1;
  660. }
  661. }
  662. print '<input type="checkbox" class="flat checkforselect checkforselect'.$facture_static_det->id.'" name="toselect[]" value="'.$facture_static_det->id."_".$i.'"'.($ischecked ? " checked" : "").'/>';
  663. print '</td>';
  664. print '</tr>';
  665. $i++;
  666. }
  667. print '</table>';
  668. print "</div>";
  669. print '</form>';
  670. } else {
  671. print $db->error();
  672. }
  673. if ($db->type == 'mysqli') {
  674. $db->query("SET SQL_BIG_SELECTS=0"); // Enable MAX_JOIN_SIZE limitation
  675. }
  676. // Add code to auto check the box when we select an account
  677. print '<script type="text/javascript">
  678. jQuery(document).ready(function() {
  679. jQuery(".codeventil").change(function() {
  680. var s=$(this).attr("id").replace("codeventil", "")
  681. console.log(s+" "+$(this).val());
  682. if ($(this).val() == -1) jQuery(".checkforselect"+s).prop("checked", false);
  683. else jQuery(".checkforselect"+s).prop("checked", true);
  684. });
  685. });
  686. </script>';
  687. // End of page
  688. llxFooter();
  689. $db->close();