list.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. <?php
  2. /* Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
  3. * Copyright (C) 2013-2019 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.'/compta/facture/class/facture.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formaccounting.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingaccount.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
  33. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
  34. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  35. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  36. // Load translation files required by the page
  37. $langs->loadLangs(array("bills", "companies", "compta", "accountancy", "other", "productbatch"));
  38. $action = GETPOST('action', 'alpha');
  39. $massaction = GETPOST('massaction', 'alpha');
  40. $show_files = GETPOST('show_files', 'int');
  41. $confirm = GETPOST('confirm', 'alpha');
  42. $toselect = GETPOST('toselect', 'array');
  43. // Select Box
  44. $mesCasesCochees = GETPOST('toselect', 'array');
  45. // Search Getpost
  46. $search_lineid = GETPOST('search_lineid', 'int');
  47. $search_ref = GETPOST('search_ref', 'alpha');
  48. $search_invoice = GETPOST('search_invoice', 'alpha');
  49. $search_label = GETPOST('search_label', 'alpha');
  50. $search_desc = GETPOST('search_desc', 'alpha');
  51. $search_amount = GETPOST('search_amount', 'alpha');
  52. $search_account = GETPOST('search_account', 'alpha');
  53. $search_vat = GETPOST('search_vat', 'alpha');
  54. $search_day = GETPOST("search_day", "int");
  55. $search_month = GETPOST("search_month", "int");
  56. $search_year = GETPOST("search_year", "int");
  57. $search_country = GETPOST('search_country', 'alpha');
  58. $search_tvaintra = GETPOST('search_tvaintra', 'alpha');
  59. $btn_ventil = GETPOST('ventil', 'alpha');
  60. // Load variable for pagination
  61. $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : (empty($conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION) ? $conf->liste_limit : $conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION);
  62. $sortfield = GETPOST('sortfield', 'alpha');
  63. $sortorder = GETPOST('sortorder', 'alpha');
  64. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  65. if (empty($page) || $page < 0) { $page = 0; }
  66. $offset = $limit * $page;
  67. $pageprev = $page - 1;
  68. $pagenext = $page + 1;
  69. if (!$sortfield)
  70. $sortfield = "f.datef, f.ref, l.rowid";
  71. if (!$sortorder) {
  72. if ($conf->global->ACCOUNTING_LIST_SORT_VENTILATION_TODO > 0) {
  73. $sortorder = "DESC";
  74. }
  75. }
  76. // Security check
  77. if ($user->socid > 0)
  78. accessforbidden();
  79. if (!$user->rights->accounting->bind->write)
  80. accessforbidden();
  81. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  82. $hookmanager->initHooks(array('accountancycustomerlist'));
  83. $formaccounting = new FormAccounting($db);
  84. $chartaccountcode = dol_getIdFromCode($db, $conf->global->CHARTOFACCOUNTS, 'accounting_system', 'rowid', 'pcg_version');
  85. /*
  86. * Actions
  87. */
  88. if (GETPOST('cancel', 'alpha')) { $action = 'list'; $massaction = ''; }
  89. if (!GETPOST('confirmmassaction', 'alpha') && $massaction != 'presend' && $massaction != 'confirm_presend') { $massaction = ''; }
  90. $parameters = array();
  91. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  92. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  93. if (empty($reshook))
  94. {
  95. // Purge search criteria
  96. 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
  97. {
  98. $search_lineid = '';
  99. $search_ref = '';
  100. $search_invoice = '';
  101. $search_label = '';
  102. $search_desc = '';
  103. $search_amount = '';
  104. $search_account = '';
  105. $search_vat = '';
  106. $search_day = '';
  107. $search_month = '';
  108. $search_year = '';
  109. $search_country = '';
  110. $search_tvaintra = '';
  111. }
  112. // Mass actions
  113. $objectclass = 'AccountingAccount';
  114. $permissiontoread = $user->rights->accounting->read;
  115. $permissiontodelete = $user->rights->accounting->delete;
  116. $uploaddir = $conf->accounting->dir_output;
  117. include DOL_DOCUMENT_ROOT.'/core/actions_massactions.inc.php';
  118. }
  119. if ($massaction == 'ventil') {
  120. $msg = '';
  121. //print '<div><font color="red">' . $langs->trans("Processing") . '...</font></div>';
  122. if (!empty($mesCasesCochees)) {
  123. $msg = '<div>'.$langs->trans("SelectedLines").': '.count($mesCasesCochees).'</div>';
  124. $msg .= '<div class="detail">';
  125. $cpt = 0;
  126. $ok = 0;
  127. $ko = 0;
  128. foreach ($mesCasesCochees as $maLigneCochee) {
  129. $maLigneCourante = explode("_", $maLigneCochee);
  130. $monId = $maLigneCourante[0];
  131. $monCompte = GETPOST('codeventil'.$monId);
  132. if ($monCompte <= 0)
  133. {
  134. $msg .= '<div><font color="red">'.$langs->trans("Lineofinvoice").' '.$monId.' - '.$langs->trans("NoAccountSelected").'</font></div>';
  135. $ko++;
  136. }
  137. else
  138. {
  139. $sql = " UPDATE ".MAIN_DB_PREFIX."facturedet";
  140. $sql .= " SET fk_code_ventilation = ".$monCompte;
  141. $sql .= " WHERE rowid = ".$monId;
  142. $accountventilated = new AccountingAccount($db);
  143. $accountventilated->fetch($monCompte, '');
  144. dol_syslog("accountancy/customer/list.php sql=".$sql, LOG_DEBUG);
  145. if ($db->query($sql)) {
  146. $msg .= '<div><font color="green">'.$langs->trans("Lineofinvoice").' '.$monId.' - '.$langs->trans("VentilatedinAccount").' : '.length_accountg($accountventilated->account_number).'</font></div>';
  147. $ok++;
  148. } else {
  149. $msg .= '<div><font color="red">'.$langs->trans("ErrorDB").' : '.$langs->trans("Lineofinvoice").' '.$monId.' - '.$langs->trans("NotVentilatedinAccount").' : '.length_accountg($accountventilated->account_number).'<br> <pre>'.$sql.'</pre></font></div>';
  150. $ko++;
  151. }
  152. }
  153. $cpt++;
  154. }
  155. $msg .= '</div>';
  156. $msg .= '<div>'.$langs->trans("EndProcessing").'</div>';
  157. }
  158. }
  159. /*
  160. * View
  161. */
  162. $form = new Form($db);
  163. $formother = new FormOther($db);
  164. llxHeader('', $langs->trans("Ventilation"));
  165. if (empty($chartaccountcode))
  166. {
  167. print $langs->trans("ErrorChartOfAccountSystemNotSelected");
  168. // End of page
  169. llxFooter();
  170. $db->close();
  171. exit;
  172. }
  173. // Customer Invoice lines
  174. $sql = "SELECT f.rowid as facid, f.ref as ref, f.datef, f.type as ftype,";
  175. $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,";
  176. $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,";
  177. $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,";
  178. $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,";
  179. $sql .= " p.tosell as status, p.tobuy as status_buy,";
  180. $sql .= " aa.rowid as aarowid, aa2.rowid as aarowid_intra, aa3.rowid as aarowid_export,";
  181. $sql .= " co.code as country_code, co.label as country_label,";
  182. $sql .= " s.tva_intra";
  183. $parameters = array();
  184. $reshook = $hookmanager->executeHooks('printFieldListSelect', $parameters); // Note that $action and $object may have been modified by hook
  185. $sql .= $hookmanager->resPrint;
  186. $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
  187. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc";
  188. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co ON co.rowid = s.fk_pays ";
  189. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."facturedet as l ON f.rowid = l.fk_facture";
  190. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = l.fk_product";
  191. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa ON p.accountancy_code_sell = aa.account_number AND aa.active = 1 AND aa.fk_pcg_version = '".$chartaccountcode."' AND aa.entity = ".$conf->entity;
  192. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa2 ON p.accountancy_code_sell_intra = aa2.account_number AND aa2.active = 1 AND aa2.fk_pcg_version = '".$chartaccountcode."' AND aa2.entity = ".$conf->entity;
  193. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa3 ON p.accountancy_code_sell_export = aa3.account_number AND aa3.active = 1 AND aa3.fk_pcg_version = '".$chartaccountcode."' AND aa3.entity = ".$conf->entity;
  194. $sql .= " WHERE f.fk_statut > 0 AND l.fk_code_ventilation <= 0";
  195. $sql .= " AND l.product_type <= 2";
  196. // Add search filter like
  197. if ($search_lineid) {
  198. $sql .= natural_search("l.rowid", $search_lineid, 1);
  199. }
  200. if (strlen(trim($search_invoice))) {
  201. $sql .= natural_search("f.ref", $search_invoice);
  202. }
  203. if (strlen(trim($search_ref))) {
  204. $sql .= natural_search("p.ref", $search_ref);
  205. }
  206. if (strlen(trim($search_label))) {
  207. $sql .= natural_search("p.label", $search_label);
  208. }
  209. if (strlen(trim($search_desc))) {
  210. $sql .= natural_search("l.description", $search_desc);
  211. }
  212. if (strlen(trim($search_amount))) {
  213. $sql .= natural_search("l.total_ht", $search_amount, 1);
  214. }
  215. if (strlen(trim($search_account))) {
  216. $sql .= natural_search("aa.account_number", $search_account);
  217. }
  218. if (strlen(trim($search_vat))) {
  219. $sql .= natural_search("l.tva_tx", price2num($search_vat), 1);
  220. }
  221. $sql .= dolSqlDateFilter('f.datef', $search_day, $search_month, $search_year);
  222. if (strlen(trim($search_country))) {
  223. $arrayofcode = getCountriesInEEC();
  224. $country_code_in_EEC = $country_code_in_EEC_without_me = '';
  225. foreach ($arrayofcode as $key => $value)
  226. {
  227. $country_code_in_EEC .= ($country_code_in_EEC ? "," : "")."'".$value."'";
  228. if ($value != $mysoc->country_code) $country_code_in_EEC_without_me .= ($country_code_in_EEC_without_me ? "," : "")."'".$value."'";
  229. }
  230. if ($search_country == 'special_allnotme') $sql .= " AND co.code <> '".$db->escape($mysoc->country_code)."'";
  231. elseif ($search_country == 'special_eec') $sql .= " AND co.code IN (".$country_code_in_EEC.")";
  232. elseif ($search_country == 'special_eecnotme') $sql .= " AND co.code IN (".$country_code_in_EEC_without_me.")";
  233. elseif ($search_country == 'special_noteec') $sql .= " AND co.code NOT IN (".$country_code_in_EEC.")";
  234. else $sql .= natural_search("co.code", $search_country);
  235. }
  236. if (strlen(trim($search_tvaintra))) {
  237. $sql .= natural_search("s.tva_intra", $search_tvaintra);
  238. }
  239. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  240. $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.",".Facture::TYPE_REPLACEMENT.",".Facture::TYPE_CREDIT_NOTE.",".Facture::TYPE_SITUATION.")";
  241. } else {
  242. $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.",".Facture::TYPE_REPLACEMENT.",".Facture::TYPE_CREDIT_NOTE.",".Facture::TYPE_DEPOSIT.",".Facture::TYPE_SITUATION.")";
  243. }
  244. $sql .= " AND f.entity IN (".getEntity('invoice', 0).")"; // We don't share object for accountancy
  245. // Add where from hooks
  246. $parameters = array();
  247. $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters); // Note that $action and $object may have been modified by hook
  248. $sql .= $hookmanager->resPrint;
  249. $sql .= $db->order($sortfield, $sortorder);
  250. // Count total nb of records
  251. $nbtotalofrecords = '';
  252. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
  253. {
  254. $result = $db->query($sql);
  255. $nbtotalofrecords = $db->num_rows($result);
  256. if (($page * $limit) > $nbtotalofrecords) // if total resultset is smaller then paging size (filtering), goto and load page 0
  257. {
  258. $page = 0;
  259. $offset = 0;
  260. }
  261. }
  262. $sql .= $db->plimit($limit + 1, $offset);
  263. dol_syslog("accountancy/customer/list.php", LOG_DEBUG);
  264. // MAX_JOIN_SIZE can be very low (ex: 300000) on some limited configurations (ex: https://www.online.net/fr/hosting/online-perso)
  265. // 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
  266. if ($db->type == 'mysqli') {
  267. $db->query("SET SQL_BIG_SELECTS=1");
  268. }
  269. $result = $db->query($sql);
  270. if ($result) {
  271. $num_lines = $db->num_rows($result);
  272. $i = 0;
  273. $arrayofselected = is_array($toselect) ? $toselect : array();
  274. $param = '';
  275. if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param .= '&contextpage='.urlencode($contextpage);
  276. if ($limit > 0 && $limit != $conf->liste_limit) $param .= '&limit='.urlencode($limit);
  277. if ($search_lineid) $param .= '&search_lineid='.urlencode($search_lineid);
  278. if ($search_day) $param .= '&search_day='.urlencode($search_day);
  279. if ($search_month) $param .= '&search_month='.urlencode($search_month);
  280. if ($search_year) $param .= '&search_year='.urlencode($search_year);
  281. if ($search_invoice) $param .= '&search_invoice='.urlencode($search_invoice);
  282. if ($search_ref) $param .= '&search_ref='.urlencode($search_ref);
  283. if ($search_desc) $param .= '&search_desc='.urlencode($search_desc);
  284. if ($search_amount) $param .= '&search_amount='.urlencode($search_amount);
  285. if ($search_vat) $param .= '&search_vat='.urlencode($search_vat);
  286. if ($search_country) $param .= "&search_country=".urlencode($search_country);
  287. if ($search_tvaintra) $param .= "&search_tvaintra=".urlencode($search_tvaintra);
  288. $arrayofmassactions = array(
  289. 'ventil'=>$langs->trans("Ventilate")
  290. //'presend'=>$langs->trans("SendByMail"),
  291. //'builddoc'=>$langs->trans("PDFMerge"),
  292. );
  293. //if ($user->rights->mymodule->supprimer) $arrayofmassactions['predelete']='<span class="fa fa-trash paddingrightonly"></span>'.$langs->trans("Delete");
  294. //if (in_array($massaction, array('presend','predelete'))) $arrayofmassactions=array();
  295. $massactionbutton = $form->selectMassAction('ventil', $arrayofmassactions, 1);
  296. print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">'."\n";
  297. print '<input type="hidden" name="action" value="ventil">';
  298. if ($optioncss != '') print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  299. print '<input type="hidden" name="token" value="'.newToken().'">';
  300. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  301. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  302. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  303. print '<input type="hidden" name="page" value="'.$page.'">';
  304. print_barre_liste($langs->trans("InvoiceLines"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num_lines, $nbtotalofrecords, 'title_accountancy', 0, '', '', $limit);
  305. print '<span class="opacitymedium">'.$langs->trans("DescVentilTodoCustomer").'</span></br><br>';
  306. /*$topicmail="Information";
  307. $modelmail="project";
  308. $objecttmp=new Project($db);
  309. $trackid='prj'.$object->id;
  310. include DOL_DOCUMENT_ROOT.'/core/tpl/massactions_pre.tpl.php';*/
  311. if ($msg) print $msg.'<br>';
  312. $moreforfilter = '';
  313. print '<div class="div-table-responsive">';
  314. print '<table class="tagtable liste'.($moreforfilter ? " listwithfilterbefore" : "").'">'."\n";
  315. // We add search filter
  316. print '<tr class="liste_titre_filter">';
  317. print '<td class="liste_titre"><input type="text" class="flat maxwidth25" name="search_lineid" value="'.dol_escape_htmltag($search_lineid).'""></td>';
  318. print '<td class="liste_titre"><input type="text" class="flat maxwidth50" name="search_invoice" value="'.dol_escape_htmltag($search_invoice).'"></td>';
  319. print '<td class="liste_titre center nowraponall minwidth100imp">';
  320. if (!empty($conf->global->MAIN_LIST_FILTER_ON_DAY)) {
  321. print '<input class="flat valignmiddle maxwidth25" type="text" maxlength="2" name="search_day" value="'.$search_day.'">';
  322. }
  323. print '<input class="flat valignmiddle maxwidth25" type="text" maxlength="2" name="search_month" value="'.$search_month.'">';
  324. $formother->select_year($search_year, 'search_year', 1, 20, 5);
  325. print '</td>';
  326. print '<td class="liste_titre"><input type="text" class="flat maxwidth50" name="search_ref" value="'.dol_escape_htmltag($search_ref).'"></td>';
  327. //print '<td class="liste_titre"><input type="text" class="flat maxwidth50" name="search_label" value="' . dol_escape_htmltag($search_label) . '"></td>';
  328. print '<td class="liste_titre"><input type="text" class="flat maxwidth100" name="search_desc" value="'.dol_escape_htmltag($search_desc).'"></td>';
  329. print '<td class="liste_titre right"><input type="text" class="flat maxwidth50 right" name="search_amount" value="'.dol_escape_htmltag($search_amount).'"></td>';
  330. 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>';
  331. print '<td class="liste_titre">';
  332. print $form->select_country($search_country, 'search_country', '', 0, 'maxwidth150', 'code2', 1, 0, 1);
  333. //print '<input type="text" class="flat maxwidth50" name="search_country" value="' . dol_escape_htmltag($search_country) . '">';
  334. print '</td>';
  335. print '<td class="liste_titre"><input type="text" class="flat maxwidth50" name="search_tvaintra" value="'.dol_escape_htmltag($search_tvaintra).'"></td>';
  336. print '<td class="liste_titre"></td>';
  337. print '<td class="liste_titre"></td>';
  338. print '<td class="center liste_titre">';
  339. $searchpicto = $form->showFilterButtons();
  340. print $searchpicto;
  341. print '</td>';
  342. print '</tr>';
  343. print '<tr class="liste_titre">';
  344. print_liste_field_titre("LineId", $_SERVER["PHP_SELF"], "l.rowid", "", $param, '', $sortfield, $sortorder);
  345. print_liste_field_titre("Invoice", $_SERVER["PHP_SELF"], "f.ref", "", $param, '', $sortfield, $sortorder);
  346. print_liste_field_titre("Date", $_SERVER["PHP_SELF"], "f.datef, f.ref, l.rowid", "", $param, '', $sortfield, $sortorder, 'center ');
  347. print_liste_field_titre("ProductRef", $_SERVER["PHP_SELF"], "p.ref", "", $param, '', $sortfield, $sortorder);
  348. //print_liste_field_titre("ProductLabel", $_SERVER["PHP_SELF"], "p.label", "", $param, '', $sortfield, $sortorder);
  349. print_liste_field_titre("ProductDescription", $_SERVER["PHP_SELF"], "l.description", "", $param, '', $sortfield, $sortorder);
  350. print_liste_field_titre("Amount", $_SERVER["PHP_SELF"], "l.total_ht", "", $param, '', $sortfield, $sortorder, 'right maxwidth50 ');
  351. print_liste_field_titre("VATRate", $_SERVER["PHP_SELF"], "l.tva_tx", "", $param, '', $sortfield, $sortorder, 'right ');
  352. print_liste_field_titre("Country", $_SERVER["PHP_SELF"], "co.label", "", $param, '', $sortfield, $sortorder);
  353. print_liste_field_titre("VATIntra", $_SERVER["PHP_SELF"], "s.tva_intra", "", $param, '', $sortfield, $sortorder);
  354. print_liste_field_titre("AccountAccountingSuggest", '', '', '', '', '', '', '', 'nowraponall ');
  355. print_liste_field_titre("IntoAccount", '', '', '', '', '', '', '', 'center ');
  356. $checkpicto = '';
  357. if ($massactionbutton) $checkpicto = $form->showCheckAddButtons('checkforselect', 1);
  358. print_liste_field_titre($checkpicto, '', '', '', '', '', '', '', 'center ');
  359. print "</tr>\n";
  360. $facture_static = new Facture($db);
  361. $product_static = new Product($db);
  362. $isSellerInEEC = isInEEC($mysoc);
  363. $accountingaccount_codetotid_cache = array();
  364. while ($i < min($num_lines, $limit)) {
  365. $objp = $db->fetch_object($result);
  366. $objp->code_sell_l = '';
  367. $objp->code_sell_p = '';
  368. $product_static->ref = $objp->product_ref;
  369. $product_static->id = $objp->product_id;
  370. $product_static->type = $objp->type;
  371. $product_static->label = $objp->product_label;
  372. $product_static->status = $objp->status;
  373. $product_static->status_buy = $objp->status_buy;
  374. $product_static->accountancy_code_sell = $objp->code_sell;
  375. $product_static->accountancy_code_sell_intra = $objp->code_sell_intra;
  376. $product_static->accountancy_code_sell_export = $objp->code_sell_export;
  377. $product_static->accountancy_code_buy = $objp->code_buy;
  378. $product_static->accountancy_code_buy_intra = $objp->code_buy_intra;
  379. $product_static->accountancy_code_buy_export = $objp->code_buy_export;
  380. $facture_static->ref = $objp->ref;
  381. $facture_static->id = $objp->facid;
  382. $facture_static->type = $objp->ftype;
  383. $code_sell_p_notset = '';
  384. $objp->aarowid_suggest = ''; // Will be set later
  385. $isBuyerInEEC = isInEEC($objp);
  386. // Search suggested default account for product/service
  387. $suggestedaccountingaccountbydefaultfor = '';
  388. if ($objp->type_l == 1) {
  389. if ($objp->country_code == $mysoc->country_code || empty($objp->country_code)) { // If buyer in same country than seller (if not defined, we assume it is same country)
  390. $objp->code_sell_l = (!empty($conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT : '');
  391. $suggestedaccountingaccountbydefaultfor = '';
  392. } else {
  393. if ($isSellerInEEC && $isBuyerInEEC && $objp->tva_tx_line != 0) { // European intravat sale, but with a VAT
  394. $objp->code_sell_l = (!empty($conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT : '');
  395. $suggestedaccountingaccountbydefaultfor = 'eecwithvat';
  396. } elseif ($isSellerInEEC && $isBuyerInEEC && empty($objp->tva_intra)) { // European intravat sale, without VAT intra community number
  397. $objp->code_sell_l = (!empty($conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT : '');
  398. $suggestedaccountingaccountbydefaultfor = 'eecwithoutvatnumber';
  399. } elseif ($isSellerInEEC && $isBuyerInEEC) { // European intravat sale
  400. $objp->code_sell_l = (!empty($conf->global->ACCOUNTING_SERVICE_SOLD_INTRA_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_SOLD_INTRA_ACCOUNT : '');
  401. $suggestedaccountingaccountbydefaultfor = 'eec';
  402. } else { // Foreign sale
  403. $objp->code_sell_l = (!empty($conf->global->ACCOUNTING_SERVICE_SOLD_EXPORT_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_SOLD_EXPORT_ACCOUNT : '');
  404. $suggestedaccountingaccountbydefaultfor = 'export';
  405. }
  406. }
  407. } elseif ($objp->type_l == 0) {
  408. if ($objp->country_code == $mysoc->country_code || empty($objp->country_code)) { // If buyer in same country than seller (if not defined, we assume it is same country)
  409. $objp->code_sell_l = (!empty($conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT : '');
  410. $suggestedaccountingaccountbydefaultfor = '';
  411. } else {
  412. if ($isSellerInEEC && $isBuyerInEEC && $objp->tva_tx_line != 0) { // European intravat sale, but with a VAT
  413. $objp->code_sell_l = (!empty($conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT : '');
  414. $suggestedaccountingaccountbydefaultfor = 'eecwithvat';
  415. } elseif ($isSellerInEEC && $isBuyerInEEC && empty($objp->tva_intra)) { // European intravat sale, without VAT intra community number
  416. $objp->code_sell_l = (!empty($conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT : '');
  417. $suggestedaccountingaccountbydefaultfor = 'eecwithoutvatnumber';
  418. } elseif ($isSellerInEEC && $isBuyerInEEC) { // European intravat sale
  419. $objp->code_sell_l = (!empty($conf->global->ACCOUNTING_PRODUCT_SOLD_INTRA_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_SOLD_INTRA_ACCOUNT : '');
  420. $suggestedaccountingaccountbydefaultfor = 'eec';
  421. } else {
  422. $objp->code_sell_l = (!empty($conf->global->ACCOUNTING_PRODUCT_SOLD_EXPORT_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_SOLD_EXPORT_ACCOUNT : '');
  423. $suggestedaccountingaccountbydefaultfor = 'export';
  424. }
  425. }
  426. }
  427. if ($objp->code_sell_l == -1) $objp->code_sell_l = '';
  428. // Search suggested account for product/service (similar code exists in page index.php to make automatic binding)
  429. $suggestedaccountingaccountfor = '';
  430. if (($objp->country_code == $mysoc->country_code) || empty($objp->country_code)) { // If buyer in same country than seller (if not defined, we assume it is same country)
  431. $objp->code_sell_p = $objp->code_sell;
  432. $objp->aarowid_suggest = $objp->aarowid;
  433. $suggestedaccountingaccountfor = '';
  434. } else {
  435. if ($isSellerInEEC && $isBuyerInEEC && $objp->tva_tx_line != 0) { // European intravat sale, but with VAT
  436. $objp->code_sell_p = $objp->code_sell;
  437. $objp->aarowid_suggest = $objp->aarowid;
  438. $suggestedaccountingaccountfor = 'eecwithvat';
  439. } elseif ($isSellerInEEC && $isBuyerInEEC && empty($objp->tva_intra)) { // European intravat sale, without VAT intra community number
  440. $objp->code_sell_p = $objp->code_sell;
  441. $objp->aarowid_suggest = $objp->aarowid; // There is a doubt for this case. Is it an error on vat or we just forgot to fill vat number ?
  442. $suggestedaccountingaccountfor = 'eecwithoutvatnumber';
  443. } elseif ($isSellerInEEC && $isBuyerInEEC) { // European intravat sale
  444. $objp->code_sell_p = $objp->code_sell_intra;
  445. $objp->aarowid_suggest = $objp->aarowid_intra;
  446. $suggestedaccountingaccountfor = 'eec';
  447. } else { // Foreign sale
  448. $objp->code_sell_p = $objp->code_sell_export;
  449. $objp->aarowid_suggest = $objp->aarowid_export;
  450. $suggestedaccountingaccountfor = 'export';
  451. }
  452. }
  453. if (!empty($objp->code_sell_p)) {
  454. // Value was defined previously
  455. } else {
  456. $code_sell_p_notset = 'color:orange';
  457. }
  458. if (empty($objp->code_sell_l) && empty($objp->code_sell_p)) $code_sell_p_notset = 'color:red';
  459. if ($suggestedaccountingaccountfor == 'eecwithoutvatnumber' && empty($code_sell_p_notset)) $code_sell_p_notset = 'color:orange';
  460. // $objp->code_sell_l is now default code of product/service
  461. // $objp->code_sell_p is now code of product/service
  462. print '<tr class="oddeven">';
  463. // Line id
  464. print '<td>'.$objp->rowid.'</td>';
  465. // Ref Invoice
  466. print '<td class="nowraponall">'.$facture_static->getNomUrl(1).'</td>';
  467. print '<td class="center">'.dol_print_date($db->jdate($objp->datef), 'day').'</td>';
  468. // Ref Product
  469. print '<td>';
  470. if ($product_static->id > 0) {
  471. print $product_static->getNomUrl(1);
  472. }
  473. if ($objp->product_label) print '<br><span class="opacitymedium">'.$objp->product_label.'</span>';
  474. print '</td>';
  475. print '<td class="tdoverflowonsmartphone">';
  476. $text = dolGetFirstLineOfText(dol_string_nohtmltag($objp->description));
  477. $trunclength = empty($conf->global->ACCOUNTING_LENGTH_DESCRIPTION) ? 32 : $conf->global->ACCOUNTING_LENGTH_DESCRIPTION;
  478. print $form->textwithtooltip(dol_trunc($text, $trunclength), $objp->description);
  479. print '</td>';
  480. print '<td class="nowrap right">';
  481. print price($objp->total_ht);
  482. print '</td>';
  483. // Vat rate
  484. if ($objp->vat_tx_l != $objp->vat_tx_p)
  485. $code_vat_differ = 'font-weight:bold; text-decoration:blink; color:red';
  486. print '<td style="'.$code_vat_differ.'" class="right">';
  487. print vatrate($objp->tva_tx_line.($objp->vat_src_code ? ' ('.$objp->vat_src_code.')' : ''));
  488. print '</td>';
  489. // Country
  490. print '<td>';
  491. $labelcountry = ($objp->country_code && ($langs->trans("Country".$objp->country_code) != "Country".$objp->country_code)) ? $langs->trans("Country".$objp->country_code) : $objp->country_label;
  492. print $labelcountry;
  493. print '</td>';
  494. print '<td>'.$objp->tva_intra.'</td>';
  495. // Found accounts
  496. print '<td style="'.$code_sell_p_notset.'">';
  497. $s = '<span class="small">'.(($objp->type_l == 1) ? $langs->trans("DefaultForService") : $langs->trans("DefaultForProduct")).': </span>';
  498. $shelp = '';
  499. if ($suggestedaccountingaccountbydefaultfor == 'eec') $shelp .= $langs->trans("SaleEEC");
  500. elseif ($suggestedaccountingaccountbydefaultfor == 'export') $shelp .= $langs->trans("SaleExport");
  501. $s .= ($objp->code_sell_l > 0 ? length_accountg($objp->code_sell_l) : $langs->trans("NotDefined"));
  502. print $form->textwithpicto($s, $shelp, 1, 'help', '', 0, 2, '', 1);
  503. if ($objp->product_id > 0)
  504. {
  505. print '<br>';
  506. $s = '<span class="small">'.(($objp->type_l == 1) ? $langs->trans("ThisService") : $langs->trans("ThisProduct")).': </span>';
  507. $shelp = '';
  508. if ($suggestedaccountingaccountfor == 'eec') $shelp = $langs->trans("SaleEEC");
  509. elseif ($suggestedaccountingaccountfor == 'eecwithvat') $shelp = $langs->trans("SaleEECWithVAT");
  510. elseif ($suggestedaccountingaccountfor == 'eecwithoutvatnumber') $shelp = $langs->trans("SaleEECWithoutVATNumber");
  511. elseif ($suggestedaccountingaccountfor == 'export') $shelp = $langs->trans("SaleExport");
  512. $s .= (empty($objp->code_sell_p) ? $langs->trans("NotDefined") : length_accountg($objp->code_sell_p));
  513. print $form->textwithpicto($s, $shelp, 1, 'help', '', 0, 2, '', 1);
  514. }
  515. print '</td>';
  516. // Suggested accounting account
  517. // $objp->code_sell_l = default (it takes the country into consideration), $objp->code_sell_p is value for product (it takes the country into consideration too)
  518. print '<td>';
  519. $suggestedid = $objp->aarowid_suggest;
  520. /*var_dump($suggestedid);
  521. var_dump($objp->code_sell_p);
  522. var_dump($objp->code_sell_l);*/
  523. if (empty($suggestedid) && empty($objp->code_sell_p) && !empty($objp->code_sell_l) && empty($conf->global->ACCOUNTANCY_DO_NOT_AUTOFILL_ACCOUNT_WITH_GENERIC))
  524. {
  525. if (empty($accountingaccount_codetotid_cache[$objp->code_sell_l]))
  526. {
  527. $tmpaccount = new AccountingAccount($db);
  528. $tmpaccount->fetch(0, $objp->code_sell_l, 1);
  529. if ($tmpaccount->id > 0) {
  530. $suggestedid = $tmpaccount->id;
  531. }
  532. $accountingaccount_codetotid_cache[$objp->code_sell_l] = $tmpaccount->id;
  533. }
  534. else {
  535. $suggestedid = $accountingaccount_codetotid_cache[$objp->code_sell_l];
  536. }
  537. }
  538. print $formaccounting->select_account($suggestedid, 'codeventil'.$objp->rowid, 1, array(), 0, 0, 'codeventil maxwidth200 maxwidthonsmartphone', 'cachewithshowemptyone');
  539. print '</td>';
  540. // Column with checkbox
  541. print '<td class="center">';
  542. //var_dump($objp->aarowid);var_dump($objp->aarowid_intra);var_dump($objp->aarowid_export);var_dump($objp->aarowid_suggest);
  543. $ischecked = $objp->aarowid_suggest;
  544. if ($suggestedaccountingaccountfor == 'eecwithoutvatnumber') $ischecked = 0;
  545. print '<input type="checkbox" class="flat checkforselect checkforselect'.$objp->rowid.'" name="toselect[]" value="'.$objp->rowid."_".$i.'"'.($ischecked ? "checked" : "").'/>';
  546. print '</td>';
  547. print '</tr>';
  548. $i++;
  549. }
  550. print '</table>';
  551. print "</div>";
  552. print '</form>';
  553. } else {
  554. print $db->error();
  555. }
  556. if ($db->type == 'mysqli') {
  557. $db->query("SET SQL_BIG_SELECTS=0"); // Enable MAX_JOIN_SIZE limitation
  558. }
  559. // Add code to auto check the box when we select an account
  560. print '<script type="text/javascript" language="javascript">
  561. jQuery(document).ready(function() {
  562. jQuery(".codeventil").change(function() {
  563. var s=$(this).attr("id").replace("codeventil", "")
  564. console.log(s+" "+$(this).val());
  565. if ($(this).val() == -1) jQuery(".checkforselect"+s).prop("checked", false);
  566. else jQuery(".checkforselect"+s).prop("checked", true);
  567. });
  568. });
  569. </script>';
  570. // End of page
  571. llxFooter();
  572. $db->close();