list.php 33 KB

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