list.php 36 KB

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