productaccount.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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 Florian Henry <florian.henry@open-concept.pro>
  5. * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
  6. * Copyright (C) 2015 Ari Elbaz (elarifr) <github@accedinfo.com>
  7. * Copyright (C) 2021 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
  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/admin/productaccount.php
  24. * \ingroup Accountancy (Double entries)
  25. * \brief To define accounting account on product / service
  26. */
  27. require '../../main.inc.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formaccounting.class.php';
  33. require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingaccount.class.php';
  34. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  35. if (!empty($conf->categorie->enabled)) {
  36. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  37. }
  38. // Load translation files required by the page
  39. $langs->loadLangs(array("companies", "compta", "accountancy", "products"));
  40. // Security check
  41. if (empty($conf->accounting->enabled)) {
  42. accessforbidden();
  43. }
  44. if (empty($user->rights->accounting->bind->write)) {
  45. accessforbidden();
  46. }
  47. // search & action GETPOST
  48. $action = GETPOST('action', 'aZ09');
  49. $massaction = GETPOST('massaction', 'alpha');
  50. $codeventil_buy = GETPOST('codeventil_buy', 'array');
  51. $codeventil_sell = GETPOST('codeventil_sell', 'array');
  52. $chk_prod = GETPOST('chk_prod', 'array');
  53. $default_account = GETPOST('default_account', 'int');
  54. $confirm = GETPOST('confirm', 'alpha');
  55. $account_number_buy = GETPOST('account_number_buy');
  56. $account_number_sell = GETPOST('account_number_sell');
  57. $changeaccount = GETPOST('changeaccount', 'array');
  58. $changeaccount_buy = GETPOST('changeaccount_buy', 'array');
  59. $changeaccount_sell = GETPOST('changeaccount_sell', 'array');
  60. $searchCategoryProductOperator = (GETPOST('search_category_product_operator', 'int') ? GETPOST('search_category_product_operator', 'int') : 0);
  61. $searchCategoryProductList = GETPOST('search_category_product_list', 'array');
  62. $search_ref = GETPOST('search_ref', 'alpha');
  63. $search_label = GETPOST('search_label', 'alpha');
  64. $search_desc = GETPOST('search_desc', 'alpha');
  65. $search_vat = GETPOST('search_vat', 'alpha');
  66. $search_current_account = GETPOST('search_current_account', 'alpha');
  67. $search_current_account_valid = GETPOST('search_current_account_valid', 'alpha');
  68. if ($search_current_account_valid == '') {
  69. $search_current_account_valid = 'withoutvalidaccount';
  70. }
  71. $search_onsell = GETPOST('search_onsell', 'alpha');
  72. $search_onpurchase = GETPOST('search_onpurchase', 'alpha');
  73. $accounting_product_mode = GETPOST('accounting_product_mode', 'alpha');
  74. $btn_changetype = GETPOST('changetype', 'alpha');
  75. $optioncss = GETPOST('optioncss', 'alpha');
  76. if (empty($accounting_product_mode)) {
  77. $accounting_product_mode = 'ACCOUNTANCY_SELL';
  78. }
  79. $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : (empty($conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION) ? $conf->liste_limit : $conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION);
  80. $sortfield = GETPOST('sortfield', 'aZ09comma');
  81. $sortorder = GETPOST('sortorder', 'aZ09comma');
  82. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  83. if (empty($page) || $page == -1) {
  84. $page = 0;
  85. } // If $page is not defined, or '' or -1
  86. $offset = $limit * $page;
  87. $pageprev = $page - 1;
  88. $pagenext = $page + 1;
  89. if (!$sortfield) {
  90. $sortfield = "p.ref";
  91. }
  92. if (!$sortorder) {
  93. $sortorder = "ASC";
  94. }
  95. if (empty($action)) {
  96. $action = 'list';
  97. }
  98. $arrayfields = array();
  99. $accounting_product_modes = array(
  100. 'ACCOUNTANCY_SELL',
  101. 'ACCOUNTANCY_SELL_INTRA',
  102. 'ACCOUNTANCY_SELL_EXPORT',
  103. 'ACCOUNTANCY_BUY',
  104. 'ACCOUNTANCY_BUY_INTRA',
  105. 'ACCOUNTANCY_BUY_EXPORT'
  106. );
  107. if ($accounting_product_mode == 'ACCOUNTANCY_BUY') {
  108. $accountancy_field_name = "accountancy_code_buy";
  109. } elseif ($accounting_product_mode == 'ACCOUNTANCY_BUY_INTRA') {
  110. $accountancy_field_name = "accountancy_code_buy_intra";
  111. } elseif ($accounting_product_mode == 'ACCOUNTANCY_BUY_EXPORT') {
  112. $accountancy_field_name = "accountancy_code_buy_export";
  113. } elseif ($accounting_product_mode == 'ACCOUNTANCY_SELL') {
  114. $accountancy_field_name = "accountancy_code_sell";
  115. } elseif ($accounting_product_mode == 'ACCOUNTANCY_SELL_INTRA') {
  116. $accountancy_field_name = "accountancy_code_sell_intra";
  117. } else { // $accounting_product_mode == 'ACCOUNTANCY_SELL_EXPORT'
  118. $accountancy_field_name = "accountancy_code_sell_export";
  119. }
  120. /*
  121. * Actions
  122. */
  123. if (GETPOST('cancel', 'alpha')) {
  124. $action = 'list'; $massaction = '';
  125. }
  126. if (!GETPOST('confirmmassaction', 'alpha') && $massaction != 'presend' && $massaction != 'confirm_presend') {
  127. $massaction = '';
  128. }
  129. $parameters = array();
  130. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  131. if ($reshook < 0) {
  132. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  133. }
  134. // Purge search criteria
  135. 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
  136. $searchCategoryProductOperator = 0;
  137. $searchCategoryProductList = array();
  138. $search_ref = '';
  139. $search_label = '';
  140. $search_desc = '';
  141. $search_vat = '';
  142. $search_onsell = '';
  143. $search_onpurchase = '';
  144. $search_current_account = '';
  145. $search_current_account_valid = '-1';
  146. }
  147. // Sales or Purchase mode ?
  148. if ($action == 'update') {
  149. if (!empty($btn_changetype)) {
  150. $error = 0;
  151. if (in_array($accounting_product_mode, $accounting_product_modes)) {
  152. if (!dolibarr_set_const($db, 'ACCOUNTING_PRODUCT_MODE', $accounting_product_mode, 'chaine', 0, '', $conf->entity)) {
  153. $error++;
  154. }
  155. } else {
  156. $error++;
  157. }
  158. }
  159. if (!empty($chk_prod) && $massaction === 'changeaccount') {
  160. //$msg = '<div><span class="accountingprocessing">' . $langs->trans("Processing") . '...</span></div>';
  161. if (!empty($chk_prod) && in_array($accounting_product_mode, $accounting_product_modes)) {
  162. $accounting = new AccountingAccount($db);
  163. //$msg .= '<div><span class="accountingprocessing">' . count($chk_prod) . ' ' . $langs->trans("SelectedLines") . '</span></div>';
  164. $arrayofdifferentselectedvalues = array();
  165. $cpt = 0; $ok = 0; $ko = 0;
  166. foreach ($chk_prod as $productid) {
  167. $accounting_account_id = GETPOST('codeventil_'.$productid);
  168. $result = 0;
  169. if ($accounting_account_id > 0) {
  170. $arrayofdifferentselectedvalues[$accounting_account_id] = $accounting_account_id;
  171. $result = $accounting->fetch($accounting_account_id, null, 1);
  172. }
  173. if ($result <= 0) {
  174. // setEventMessages(null, $accounting->errors, 'errors');
  175. $msg .= '<div><span style="color:red">'.$langs->trans("ErrorDB").' : '.$langs->trans("Product").' '.$productid.' '.$langs->trans("NotVentilatedinAccount").' : id='.$accounting_account_id.'<br> <pre>'.$sql.'</pre></span></div>';
  176. $ko++;
  177. } else {
  178. $sql = '';
  179. if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
  180. $sql_exists = "SELECT rowid FROM " . MAIN_DB_PREFIX . "product_perentity";
  181. $sql_exists .= " WHERE fk_product = " . ((int) $productid) . " AND entity = " . ((int) $conf->entity);
  182. $resql_exists = $db->query($sql_exists);
  183. if (!$resql_exists) {
  184. $msg .= '<div><span style="color:red">'.$langs->trans("ErrorDB").' : '.$langs->trans("Product").' '.$productid.' '.$langs->trans("NotVentilatedinAccount").' : id='.$accounting_account_id.'<br> <pre>'.$resql_exists.'</pre></span></div>';
  185. $ko++;
  186. } else {
  187. $nb_exists = $db->num_rows($resql_exists);
  188. if ($nb_exists <= 0) {
  189. // insert
  190. $sql = "INSERT INTO " . MAIN_DB_PREFIX . "product_perentity (fk_product, entity, " . $db->escape($accountancy_field_name) . ")";
  191. $sql .= " VALUES (" . ((int) $productid) . ", " . ((int) $conf->entity) . ", '" . $db->escape($accounting->account_number) . "')";
  192. } else {
  193. $obj_exists = $db->fetch_object($resql_exists);
  194. // update
  195. $sql = "UPDATE " . MAIN_DB_PREFIX . "product_perentity";
  196. $sql .= " SET " . $accountancy_field_name . " = '" . $db->escape($accounting->account_number) . "'";
  197. $sql .= " WHERE rowid = " . ((int) $obj_exists->rowid);
  198. }
  199. }
  200. } else {
  201. $sql = " UPDATE ".MAIN_DB_PREFIX."product";
  202. $sql .= " SET ".$accountancy_field_name." = '".$db->escape($accounting->account_number)."'";
  203. $sql .= " WHERE rowid = ".((int) $productid);
  204. }
  205. dol_syslog("/accountancy/admin/productaccount.php", LOG_DEBUG);
  206. $db->begin();
  207. if ($db->query($sql)) {
  208. $ok++;
  209. $db->commit();
  210. } else {
  211. $ko++;
  212. $db->rollback();
  213. }
  214. }
  215. $cpt++;
  216. }
  217. }
  218. if ($ko) {
  219. setEventMessages($langs->trans("XLineFailedToBeBinded", $ko), null, 'errors');
  220. }
  221. if ($ok) {
  222. setEventMessages($langs->trans("XLineSuccessfullyBinded", $ok), null, 'mesgs');
  223. }
  224. }
  225. }
  226. /*
  227. * View
  228. */
  229. $form = new FormAccounting($db);
  230. // Default AccountingAccount RowId Product / Service
  231. // at this time ACCOUNTING_SERVICE_SOLD_ACCOUNT & ACCOUNTING_PRODUCT_SOLD_ACCOUNT are account number not accountingacount rowid
  232. // so we need to get those the rowid of those default value first
  233. $accounting = new AccountingAccount($db);
  234. // TODO: we should need to check if result is already exists accountaccount rowid.....
  235. $aarowid_servbuy = $accounting->fetch('', getDolGlobalString('ACCOUNTING_SERVICE_BUY_ACCOUNT'), 1);
  236. $aarowid_servbuy_intra = $accounting->fetch('', getDolGlobalString('ACCOUNTING_SERVICE_BUY_INTRA_ACCOUNT'), 1);
  237. $aarowid_servbuy_export = $accounting->fetch('', getDolGlobalString('ACCOUNTING_SERVICE_BUY_EXPORT_ACCOUNT'), 1);
  238. $aarowid_prodbuy = $accounting->fetch('', getDolGlobalString('ACCOUNTING_PRODUCT_BUY_ACCOUNT'), 1);
  239. $aarowid_prodbuy_intra = $accounting->fetch('', getDolGlobalString('ACCOUNTING_PRODUCT_BUY_INTRA_ACCOUNT'), 1);
  240. $aarowid_prodbuy_export = $accounting->fetch('', getDolGlobalString('ACCOUNTING_PRODUCT_BUY_EXPORT_ACCOUNT'), 1);
  241. $aarowid_servsell = $accounting->fetch('', getDolGlobalString('ACCOUNTING_SERVICE_SOLD_ACCOUNT'), 1);
  242. $aarowid_servsell_intra = $accounting->fetch('', getDolGlobalString('ACCOUNTING_SERVICE_SOLD_INTRA_ACCOUNT'), 1);
  243. $aarowid_servsell_export = $accounting->fetch('', getDolGlobalString('ACCOUNTING_SERVICE_SOLD_EXPORT_ACCOUNT'), 1);
  244. $aarowid_prodsell = $accounting->fetch('', getDolGlobalString('ACCOUNTING_PRODUCT_SOLD_ACCOUNT'), 1);
  245. $aarowid_prodsell_intra = $accounting->fetch('', getDolGlobalString('ACCOUNTING_PRODUCT_SOLD_INTRA_ACCOUNT'), 1);
  246. $aarowid_prodsell_export = $accounting->fetch('', getDolGlobalString('ACCOUNTING_PRODUCT_SOLD_EXPORT_ACCOUNT'), 1);
  247. $aacompta_servbuy = getDolGlobalString('ACCOUNTING_SERVICE_BUY_ACCOUNT', $langs->trans("CodeNotDef"));
  248. $aacompta_servbuy_intra = getDolGlobalString('ACCOUNTING_SERVICE_BUY_INTRA_ACCOUNT', $langs->trans("CodeNotDef"));
  249. $aacompta_servbuy_export = getDolGlobalString('ACCOUNTING_SERVICE_BUY_EXPORT_ACCOUNT', $langs->trans("CodeNotDef"));
  250. $aacompta_prodbuy = getDolGlobalString('ACCOUNTING_PRODUCT_BUY_ACCOUNT', $langs->trans("CodeNotDef"));
  251. $aacompta_prodbuy_intra = getDolGlobalString('ACCOUNTING_PRODUCT_BUY_INTRA_ACCOUNT', $langs->trans("CodeNotDef"));
  252. $aacompta_prodbuy_export = getDolGlobalString('ACCOUNTING_PRODUCT_BUY_EXPORT_ACCOUNT', $langs->trans("CodeNotDef"));
  253. $aacompta_servsell = getDolGlobalString('ACCOUNTING_SERVICE_SOLD_ACCOUNT', $langs->trans("CodeNotDef"));
  254. $aacompta_servsell_intra = getDolGlobalString('ACCOUNTING_SERVICE_SOLD_INTRA_ACCOUNT', $langs->trans("CodeNotDef"));
  255. $aacompta_servsell_export = getDolGlobalString('ACCOUNTING_SERVICE_SOLD_EXPORT_ACCOUNT', $langs->trans("CodeNotDef"));
  256. $aacompta_prodsell = getDolGlobalString('ACCOUNTING_PRODUCT_SOLD_ACCOUNT', $langs->trans("CodeNotDef"));
  257. $aacompta_prodsell_intra = getDolGlobalString('ACCOUNTING_PRODUCT_SOLD_INTRA_ACCOUNT', $langs->trans("CodeNotDef"));
  258. $aacompta_prodsell_export = getDolGlobalString('ACCOUNTING_PRODUCT_SOLD_EXPORT_ACCOUNT', $langs->trans("CodeNotDef"));
  259. $title = $langs->trans("ProductsBinding");
  260. $helpurl = '';
  261. $paramsCat = '';
  262. foreach ($searchCategoryProductList as $searchCategoryProduct) {
  263. $paramsCat .= "&search_category_product_list[]=".urlencode($searchCategoryProduct);
  264. }
  265. llxHeader('', $title, $helpurl, '', 0, 0, array(), array(), $paramsCat, '');
  266. $pcgverid = getDolGlobalString('CHARTOFACCOUNTS');
  267. $pcgvercode = dol_getIdFromCode($db, $pcgverid, 'accounting_system', 'rowid', 'pcg_version');
  268. if (empty($pcgvercode)) {
  269. $pcgvercode = $pcgverid;
  270. }
  271. $sql = "SELECT p.rowid, p.ref, p.label, p.description, p.tosell, p.tobuy, p.tva_tx,";
  272. if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
  273. $sql .= " ppe.accountancy_code_sell, ppe.accountancy_code_sell_intra, ppe.accountancy_code_sell_export,";
  274. $sql .= " ppe.accountancy_code_buy, ppe.accountancy_code_buy_intra, ppe.accountancy_code_buy_export,";
  275. } else {
  276. $sql .= " p.accountancy_code_sell, p.accountancy_code_sell_intra, p.accountancy_code_sell_export,";
  277. $sql .= " p.accountancy_code_buy, p.accountancy_code_buy_intra, p.accountancy_code_buy_export,";
  278. }
  279. $sql .= " p.tms, p.fk_product_type as product_type,";
  280. $sql .= " aa.rowid as aaid";
  281. $sql .= " FROM ".MAIN_DB_PREFIX."product as p";
  282. if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
  283. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product_perentity as ppe ON ppe.fk_product = p.rowid AND ppe.entity = " . ((int) $conf->entity);
  284. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON aa.account_number = ppe." . $accountancy_field_name . " AND aa.fk_pcg_version = '" . $db->escape($pcgvercode) . "'";
  285. } else {
  286. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON aa.account_number = p." . $accountancy_field_name . " AND aa.fk_pcg_version = '" . $db->escape($pcgvercode) . "'";
  287. }
  288. if (!empty($searchCategoryProductList)) {
  289. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX."categorie_product as cp ON p.rowid = cp.fk_product"; // We'll need this table joined to the select in order to filter by categ
  290. }
  291. $sql .= ' WHERE p.entity IN ('.getEntity('product').')';
  292. if (strlen(trim($search_current_account))) {
  293. $sql .= natural_search((empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED) ? "p." : "ppe.") . $accountancy_field_name, $search_current_account);
  294. }
  295. if ($search_current_account_valid == 'withoutvalidaccount') {
  296. $sql .= " AND aa.account_number IS NULL";
  297. }
  298. if ($search_current_account_valid == 'withvalidaccount') {
  299. $sql .= " AND aa.account_number IS NOT NULL";
  300. }
  301. $searchCategoryProductSqlList = array();
  302. if ($searchCategoryProductOperator == 1) {
  303. foreach ($searchCategoryProductList as $searchCategoryProduct) {
  304. if (intval($searchCategoryProduct) == -2) {
  305. $searchCategoryProductSqlList[] = "cp.fk_categorie IS NULL";
  306. } elseif (intval($searchCategoryProduct) > 0) {
  307. $searchCategoryProductSqlList[] = "cp.fk_categorie = ".$db->escape($searchCategoryProduct);
  308. }
  309. }
  310. if (!empty($searchCategoryProductSqlList)) {
  311. $sql .= " AND (".implode(' OR ', $searchCategoryProductSqlList).")";
  312. }
  313. } else {
  314. foreach ($searchCategoryProductList as $searchCategoryProduct) {
  315. if (intval($searchCategoryProduct) == -2) {
  316. $searchCategoryProductSqlList[] = "cp.fk_categorie IS NULL";
  317. } elseif (intval($searchCategoryProduct) > 0) {
  318. $searchCategoryProductSqlList[] = "p.rowid IN (SELECT fk_product FROM ".MAIN_DB_PREFIX."categorie_product WHERE fk_categorie = ".((int) $searchCategoryProduct).")";
  319. }
  320. }
  321. if (!empty($searchCategoryProductSqlList)) {
  322. $sql .= " AND (".implode(' AND ', $searchCategoryProductSqlList).")";
  323. }
  324. }
  325. // Add search filter like
  326. if (strlen(trim($search_ref))) {
  327. $sql .= natural_search("p.ref", $search_ref);
  328. }
  329. if (strlen(trim($search_label))) {
  330. $sql .= natural_search("p.label", $search_label);
  331. }
  332. if (strlen(trim($search_desc))) {
  333. $sql .= natural_search("p.description", $search_desc);
  334. }
  335. if (strlen(trim($search_vat))) {
  336. $sql .= natural_search("p.tva_tx", price2num($search_vat), 1);
  337. }
  338. if ($search_onsell != '' && $search_onsell != '-1') {
  339. $sql .= natural_search('p.tosell', $search_onsell, 1);
  340. }
  341. if ($search_onpurchase != '' && $search_onpurchase != '-1') {
  342. $sql .= natural_search('p.tobuy', $search_onpurchase, 1);
  343. }
  344. $sql .= " GROUP BY p.rowid, p.ref, p.label, p.description, p.tosell, p.tobuy, p.tva_tx,";
  345. $sql .= " p.fk_product_type,";
  346. $sql .= ' p.tms,';
  347. $sql .= ' aa.rowid,';
  348. if (empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
  349. $sql .= " p.accountancy_code_sell, p.accountancy_code_sell_intra, p.accountancy_code_sell_export, p.accountancy_code_buy, p.accountancy_code_buy_intra, p.accountancy_code_buy_export";
  350. } else {
  351. $sql .= " ppe.accountancy_code_sell, ppe.accountancy_code_sell_intra, ppe.accountancy_code_sell_export, ppe.accountancy_code_buy, ppe.accountancy_code_buy_intra, ppe.accountancy_code_buy_export";
  352. }
  353. $sql .= $db->order($sortfield, $sortorder);
  354. $nbtotalofrecords = '';
  355. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
  356. $resql = $db->query($sql);
  357. $nbtotalofrecords = $db->num_rows($resql);
  358. if (($page * $limit) > $nbtotalofrecords) { // if total resultset is smaller then paging size (filtering), goto and load page 0
  359. $page = 0;
  360. $offset = 0;
  361. }
  362. }
  363. $sql .= $db->plimit($limit + 1, $offset);
  364. dol_syslog("/accountancy/admin/productaccount.php", LOG_DEBUG);
  365. $resql = $db->query($sql);
  366. if ($resql) {
  367. $num = $db->num_rows($resql);
  368. $i = 0;
  369. $param = '';
  370. if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) {
  371. $param .= '&contextpage='.urlencode($contextpage);
  372. }
  373. if ($limit > 0 && $limit != $conf->liste_limit) {
  374. $param .= '&limit='.urlencode($limit);
  375. }
  376. if ($searchCategoryProductOperator == 1) {
  377. $param .= "&search_category_product_operator=".urlencode($searchCategoryProductOperator);
  378. }
  379. foreach ($searchCategoryProductList as $searchCategoryProduct) {
  380. $param .= "&search_category_product_list[]=".urlencode($searchCategoryProduct);
  381. }
  382. if ($search_ref > 0) {
  383. $param .= "&search_ref=".urlencode($search_ref);
  384. }
  385. if ($search_label > 0) {
  386. $param .= "&search_label=".urlencode($search_label);
  387. }
  388. if ($search_desc > 0) {
  389. $param .= "&search_desc=".urlencode($search_desc);
  390. }
  391. if ($search_vat > 0) {
  392. $param .= '&search_vat='.urlencode($search_vat);
  393. }
  394. if ($search_current_account > 0) {
  395. $param .= "&search_current_account=".urlencode($search_current_account);
  396. }
  397. if ($search_current_account_valid && $search_current_account_valid != '-1') {
  398. $param .= "&search_current_account_valid=".urlencode($search_current_account_valid);
  399. }
  400. if ($accounting_product_mode) {
  401. $param .= '&accounting_product_mode='.urlencode($accounting_product_mode);
  402. }
  403. print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
  404. if ($optioncss != '') {
  405. print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  406. }
  407. print '<input type="hidden" name="token" value="'.newToken().'">';
  408. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  409. print '<input type="hidden" name="action" value="update">';
  410. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  411. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  412. print load_fiche_titre($langs->trans("ProductsBinding"), '', 'title_accountancy');
  413. print '<br>';
  414. print '<span class="opacitymedium">'.$langs->trans("InitAccountancyDesc").'</span><br>';
  415. print '<br>';
  416. // Select mode
  417. print '<table class="noborder centpercent">';
  418. print '<tr class="liste_titre">';
  419. print '<td>'.$langs->trans('Options').'</td><td>'.$langs->trans('Description').'</td>';
  420. print "</tr>\n";
  421. print '<tr class="oddeven"><td><input type="radio" name="accounting_product_mode" value="ACCOUNTANCY_SELL"'.($accounting_product_mode == 'ACCOUNTANCY_SELL' ? ' checked' : '').'> '.$langs->trans('OptionModeProductSell').'</td>';
  422. print '<td>'.$langs->trans('OptionModeProductSellDesc');
  423. print "</td></tr>\n";
  424. if ($mysoc->isInEEC()) {
  425. print '<tr class="oddeven"><td><input type="radio" name="accounting_product_mode" value="ACCOUNTANCY_SELL_INTRA"'.($accounting_product_mode == 'ACCOUNTANCY_SELL_INTRA' ? ' checked' : '').'> '.$langs->trans('OptionModeProductSellIntra').'</td>';
  426. print '<td>'.$langs->trans('OptionModeProductSellIntraDesc');
  427. print "</td></tr>\n";
  428. }
  429. print '<tr class="oddeven"><td><input type="radio" name="accounting_product_mode" value="ACCOUNTANCY_SELL_EXPORT"'.($accounting_product_mode == 'ACCOUNTANCY_SELL_EXPORT' ? ' checked' : '').'> '.$langs->trans('OptionModeProductSellExport').'</td>';
  430. print '<td>'.$langs->trans('OptionModeProductSellExportDesc');
  431. print "</td></tr>\n";
  432. print '<tr class="oddeven"><td><input type="radio" name="accounting_product_mode" value="ACCOUNTANCY_BUY"'.($accounting_product_mode == 'ACCOUNTANCY_BUY' ? ' checked' : '').'> '.$langs->trans('OptionModeProductBuy').'</td>';
  433. print '<td>'.$langs->trans('OptionModeProductBuyDesc')."</td></tr>\n";
  434. if ($mysoc->isInEEC()) {
  435. print '<tr class="oddeven"><td><input type="radio" name="accounting_product_mode" value="ACCOUNTANCY_BUY_INTRA"'.($accounting_product_mode == 'ACCOUNTANCY_BUY_INTRA' ? ' checked' : '').'> '.$langs->trans('OptionModeProductBuyIntra').'</td>';
  436. print '<td>'.$langs->trans('OptionModeProductBuyDesc')."</td></tr>\n";
  437. }
  438. print '<tr class="oddeven"><td><input type="radio" name="accounting_product_mode" value="ACCOUNTANCY_BUY_EXPORT"'.($accounting_product_mode == 'ACCOUNTANCY_BUY_EXPORT' ? ' checked' : '').'> '.$langs->trans('OptionModeProductBuyExport').'</td>';
  439. print '<td>'.$langs->trans('OptionModeProductBuyDesc')."</td></tr>\n";
  440. print "</table>\n";
  441. print '<div class="center"><input type="submit" class="button" value="'.$langs->trans('Refresh').'" name="changetype"></div>';
  442. print "<br>\n";
  443. // Filter on categories
  444. $moreforfilter = '';
  445. $varpage = empty($contextpage) ? $_SERVER["PHP_SELF"] : $contextpage;
  446. $selectedfields = $form->multiSelectArrayWithCheckbox('selectedfields', $arrayfields, $varpage); // This also change content of $arrayfields
  447. if ($massaction !== 'set_default_account') {
  448. $arrayofmassactions = array(
  449. 'changeaccount'=>img_picto('', 'check', 'class="pictofixedwidth"').$langs->trans("Save")
  450. ,'set_default_account'=>img_picto('', 'check', 'class="pictofixedwidth"').$langs->trans("ConfirmPreselectAccount")
  451. );
  452. $massactionbutton = $form->selectMassAction('', $arrayofmassactions, 1);
  453. }
  454. $buttonsave = '<input type="submit" class="button button-save" id="changeaccount" name="changeaccount" value="'.$langs->trans("Save").'">';
  455. //print '<br><div class="center">'.$buttonsave.'</div>';
  456. $texte = $langs->trans("ListOfProductsServices");
  457. print_barre_liste($texte, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, '', 0, '', '', $limit, 0, 0, 1);
  458. if ($massaction == 'set_default_account') {
  459. $formquestion[]=array('type' => 'other',
  460. 'name' => 'set_default_account',
  461. 'label' => $langs->trans("AccountancyCode"),
  462. 'value' => $form->select_account('', 'default_account', 1, array(), 0, 0, 'maxwidth200 maxwidthonsmartphone', 'cachewithshowemptyone'));
  463. print $form->formconfirm($_SERVER["PHP_SELF"], $langs->trans("ConfirmPreselectAccount"), $langs->trans("ConfirmPreselectAccountQuestion", count($chk_prod)), "confirm_set_default_account", $formquestion, 1, 0, 200, 500, 1);
  464. }
  465. // Filter on categories
  466. $moreforfilter = '';
  467. if (!empty($conf->categorie->enabled) && $user->rights->categorie->lire) {
  468. $moreforfilter .= '<div class="divsearchfield">';
  469. $moreforfilter .= img_picto($langs->trans('Categories'), 'category', 'class="pictofixedwidth"');
  470. $categoriesProductArr = $form->select_all_categories(Categorie::TYPE_PRODUCT, '', '', 64, 0, 1);
  471. $categoriesProductArr[-2] = '- '.$langs->trans('NotCategorized').' -';
  472. $moreforfilter .= Form::multiselectarray('search_category_product_list', $categoriesProductArr, $searchCategoryProductList, 0, 0, 'minwidth300');
  473. $moreforfilter .= ' <input type="checkbox" class="valignmiddle" name="search_category_product_operator" value="1"'.($searchCategoryProductOperator == 1 ? ' checked="checked"' : '').'/> <span class="none">'.$langs->trans('UseOrOperatorForCategories').'</span>';
  474. $moreforfilter .= '</div>';
  475. }
  476. //Show/hide child products. Hidden by default
  477. if (!empty($conf->variants->enabled) && !empty($conf->global->PRODUIT_ATTRIBUTES_HIDECHILD)) {
  478. $moreforfilter .= '<div class="divsearchfield">';
  479. $moreforfilter .= '<input type="checkbox" id="search_show_childproducts" name="search_show_childproducts"'.($show_childproducts ? 'checked="checked"' : '').'>';
  480. $moreforfilter .= ' <label for="search_show_childproducts">'.$langs->trans('ShowChildProducts').'</label>';
  481. $moreforfilter .= '</div>';
  482. }
  483. $parameters = array();
  484. $reshook = $hookmanager->executeHooks('printFieldPreListTitle', $parameters); // Note that $action and $object may have been modified by hook
  485. if (empty($reshook)) {
  486. $moreforfilter .= $hookmanager->resPrint;
  487. } else {
  488. $moreforfilter = $hookmanager->resPrint;
  489. }
  490. if ($moreforfilter) {
  491. print '<div class="liste_titre liste_titre_bydiv centpercent">';
  492. print $moreforfilter;
  493. print '</div>';
  494. }
  495. print '<div class="div-table-responsive">';
  496. print '<table class="liste '.($moreforfilter ? "listwithfilterbefore" : "").'">';
  497. print '<tr class="liste_titre_filter">';
  498. print '<td class="liste_titre"><input type="text" class="flat" size="8" name="search_ref" value="'.dol_escape_htmltag($search_ref).'"></td>';
  499. print '<td class="liste_titre"><input type="text" class="flat" size="10" name="search_label" value="'.dol_escape_htmltag($search_label).'"></td>';
  500. print '<td class="liste_titre right"><input type="text" class="flat maxwidth50 right" size="5" name="search_vat" placeholder="%" value="'.dol_escape_htmltag($search_vat).'"></td>';
  501. if (!empty($conf->global->ACCOUNTANCY_SHOW_PROD_DESC)) {
  502. print '<td class="liste_titre"><input type="text" class="flat" size="20" name="search_desc" value="'.dol_escape_htmltag($search_desc).'"></td>';
  503. }
  504. // On sell
  505. if ($accounting_product_mode == 'ACCOUNTANCY_SELL' || $accounting_product_mode == 'ACCOUNTANCY_SELL_INTRA' || $accounting_product_mode == 'ACCOUNTANCY_SELL_EXPORT') {
  506. print '<td class="liste_titre center">'.$form->selectyesno('search_onsell', $search_onsell, 1, false, 1).'</td>';
  507. } else {
  508. // On buy
  509. print '<td class="liste_titre center">'.$form->selectyesno('search_onpurchase', $search_onpurchase, 1, false, 1).'</td>';
  510. }
  511. // Current account
  512. print '<td class="liste_titre">';
  513. print '<input type="text" class="flat" size="6" name="search_current_account" id="search_current_account" value="'.dol_escape_htmltag($search_current_account).'">';
  514. $listofvals = array('withoutvalidaccount'=>$langs->trans("WithoutValidAccount"), 'withvalidaccount'=>$langs->trans("WithValidAccount"));
  515. print ' '.$langs->trans("or").' '.$form->selectarray('search_current_account_valid', $listofvals, $search_current_account_valid, 1);
  516. print '</td>';
  517. print '<td class="liste_titre">&nbsp;</td>';
  518. print '<td class="center liste_titre">';
  519. $searchpicto = $form->showFilterButtons();
  520. print $searchpicto;
  521. print '</td>';
  522. print '</tr>';
  523. print '<tr class="liste_titre">';
  524. print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "p.ref", "", $param, '', $sortfield, $sortorder);
  525. print_liste_field_titre("Label", $_SERVER["PHP_SELF"], "p.label", "", $param, '', $sortfield, $sortorder);
  526. if (!empty($conf->global->ACCOUNTANCY_SHOW_PROD_DESC)) {
  527. print_liste_field_titre("Description", $_SERVER["PHP_SELF"], "p.description", "", $param, '', $sortfield, $sortorder);
  528. }
  529. print_liste_field_titre("VATRate", $_SERVER["PHP_SELF"], "p.tva_tx", "", $param, '', $sortfield, $sortorder, 'right ');
  530. // On sell / On purchase
  531. if ($accounting_product_mode == 'ACCOUNTANCY_SELL' || $accounting_product_mode == 'ACCOUNTANCY_SELL_INTRA' || $accounting_product_mode == 'ACCOUNTANCY_SELL_EXPORT') {
  532. print_liste_field_titre("OnSell", $_SERVER["PHP_SELF"], "p.tosell", "", $param, '', $sortfield, $sortorder, 'center ');
  533. } else {
  534. print_liste_field_titre("OnBuy", $_SERVER["PHP_SELF"], "p.tobuy", "", $param, '', $sortfield, $sortorder, 'center ');
  535. }
  536. print_liste_field_titre("CurrentDedicatedAccountingAccount", $_SERVER["PHP_SELF"], (empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED) ? "p." : "ppe.") . $accountancy_field_name, "", $param, '', $sortfield, $sortorder);
  537. print_liste_field_titre("AssignDedicatedAccountingAccount");
  538. $clickpitco = $form->showCheckAddButtons('checkforselect', 1);
  539. print_liste_field_titre($clickpitco, '', '', '', '', '', '', '', 'center ');
  540. print '</tr>';
  541. $product_static = new Product($db);
  542. $i = 0;
  543. while ($i < min($num, $limit)) {
  544. $obj = $db->fetch_object($resql);
  545. // Ref produit as link
  546. $product_static->ref = $obj->ref;
  547. $product_static->id = $obj->rowid;
  548. $product_static->type = $obj->product_type;
  549. $product_static->label = $obj->label;
  550. $product_static->description = $obj->description;
  551. $product_static->status = $obj->tosell;
  552. $product_static->status_buy = $obj->tobuy;
  553. // Sales
  554. if ($obj->product_type == 0) {
  555. if ($accounting_product_mode == 'ACCOUNTANCY_SELL') {
  556. $compta_prodsell = (!empty($conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT : $langs->trans("CodeNotDef"));
  557. $compta_prodsell_id = $aarowid_prodsell;
  558. } elseif ($accounting_product_mode == 'ACCOUNTANCY_SELL_INTRA') {
  559. $compta_prodsell = (!empty($conf->global->ACCOUNTING_PRODUCT_SOLD_INTRA_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_SOLD_INTRA_ACCOUNT : $langs->trans("CodeNotDef"));
  560. $compta_prodsell_id = $aarowid_prodsell_intra;
  561. } elseif ($accounting_product_mode == 'ACCOUNTANCY_SELL_EXPORT') {
  562. $compta_prodsell = (!empty($conf->global->ACCOUNTING_PRODUCT_SOLD_EXPORT_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_SOLD_EXPORT_ACCOUNT : $langs->trans("CodeNotDef"));
  563. $compta_prodsell_id = $aarowid_prodsell_export;
  564. } else {
  565. $compta_prodsell = (!empty($conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT : $langs->trans("CodeNotDef"));
  566. $compta_prodsell_id = $aarowid_prodsell;
  567. }
  568. } else {
  569. if ($accounting_product_mode == 'ACCOUNTANCY_SELL') {
  570. $compta_prodsell = (!empty($conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT : $langs->trans("CodeNotDef"));
  571. $compta_prodsell_id = $aarowid_servsell;
  572. } elseif ($accounting_product_mode == 'ACCOUNTANCY_SELL_INTRA') {
  573. $compta_prodsell = (!empty($conf->global->ACCOUNTING_SERVICE_SOLD_INTRA_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_SOLD_INTRA_ACCOUNT : $langs->trans("CodeNotDef"));
  574. $compta_prodsell_id = $aarowid_servsell_intra;
  575. } elseif ($accounting_product_mode == 'ACCOUNTANCY_SELL_EXPORT') {
  576. $compta_prodsell = (!empty($conf->global->ACCOUNTING_SERVICE_SOLD_EXPORT_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_SOLD_EXPORT_ACCOUNT : $langs->trans("CodeNotDef"));
  577. $compta_prodsell_id = $aarowid_servsell_export;
  578. } else {
  579. $compta_prodsell = (!empty($conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT : $langs->trans("CodeNotDef"));
  580. $compta_prodsell_id = $aarowid_servsell;
  581. }
  582. }
  583. // Purchases
  584. if ($obj->product_type == 0) {
  585. if ($accounting_product_mode == 'ACCOUNTANCY_BUY') {
  586. $compta_prodbuy = (!empty($conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT : $langs->trans("CodeNotDef"));
  587. $compta_prodbuy_id = $aarowid_prodbuy;
  588. } elseif ($accounting_product_mode == 'ACCOUNTANCY_BUY_INTRA') {
  589. $compta_prodbuy = (!empty($conf->global->ACCOUNTING_PRODUCT_BUY_INTRA_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_BUY_INTRA_ACCOUNT : $langs->trans("CodeNotDef"));
  590. $compta_prodbuy_id = $aarowid_prodbuy_intra;
  591. } elseif ($accounting_product_mode == 'ACCOUNTANCY_BUY_EXPORT') {
  592. $compta_prodbuy = (!empty($conf->global->ACCOUNTING_PRODUCT_BUY_EXPORT_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_BUY_EXPORT_ACCOUNT : $langs->trans("CodeNotDef"));
  593. $compta_prodbuy_id = $aarowid_prodbuy_export;
  594. } else {
  595. $compta_prodbuy = (!empty($conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT : $langs->trans("CodeNotDef"));
  596. $compta_prodbuy_id = $aarowid_prodbuy;
  597. }
  598. } else {
  599. if ($accounting_product_mode == 'ACCOUNTANCY_BUY') {
  600. $compta_prodbuy = (!empty($conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT : $langs->trans("CodeNotDef"));
  601. $compta_prodbuy_id = $aarowid_servbuy;
  602. } elseif ($accounting_product_mode == 'ACCOUNTANCY_BUY_INTRA') {
  603. $compta_prodbuy = (!empty($conf->global->ACCOUNTING_SERVICE_BUY_INTRA_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_BUY_INTRA_ACCOUNT : $langs->trans("CodeNotDef"));
  604. $compta_prodbuy_id = $aarowid_servbuy_intra;
  605. } elseif ($accounting_product_mode == 'ACCOUNTANCY_BUY_EXPORT') {
  606. $compta_prodbuy = (!empty($conf->global->ACCOUNTING_SERVICE_BUY_EXPORT_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_BUY_EXPORT_ACCOUNT : $langs->trans("CodeNotDef"));
  607. $compta_prodbuy_id = $aarowid_servbuy_export;
  608. } else {
  609. $compta_prodbuy = (!empty($conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT : $langs->trans("CodeNotDef"));
  610. $compta_prodbuy_id = $aarowid_servbuy;
  611. }
  612. }
  613. print '<tr class="oddeven">';
  614. print '<td>';
  615. print $product_static->getNomUrl(1);
  616. print '</td>';
  617. print '<td class="left">'.$obj->label.'</td>';
  618. if (!empty($conf->global->ACCOUNTANCY_SHOW_PROD_DESC)) {
  619. // TODO ADJUST DESCRIPTION SIZE
  620. // print '<td class="left">' . $obj->description . '</td>';
  621. // TODO: we should set a user defined value to adjust user square / wide screen size
  622. $trunclength = empty($conf->global->ACCOUNTING_LENGTH_DESCRIPTION) ? 32 : $conf->global->ACCOUNTING_LENGTH_DESCRIPTION;
  623. print '<td>'.nl2br(dol_trunc($obj->description, $trunclength)).'</td>';
  624. }
  625. // VAT
  626. print '<td class="right">';
  627. print vatrate($obj->tva_tx);
  628. print '</td>';
  629. // On sell / On purchase
  630. if ($accounting_product_mode == 'ACCOUNTANCY_SELL' || $accounting_product_mode == 'ACCOUNTANCY_SELL_INTRA' || $accounting_product_mode == 'ACCOUNTANCY_SELL_EXPORT') {
  631. print '<td class="center">'.$product_static->getLibStatut(3, 0).'</td>';
  632. } else {
  633. print '<td class="center">'.$product_static->getLibStatut(3, 1).'</td>';
  634. }
  635. // Current accounting account
  636. print '<td class="left">';
  637. if ($accounting_product_mode == 'ACCOUNTANCY_BUY') {
  638. print length_accountg($obj->accountancy_code_buy);
  639. if ($obj->accountancy_code_buy && empty($obj->aaid)) {
  640. print ' '.img_warning($langs->trans("ValueNotIntoChartOfAccount"));
  641. }
  642. } elseif ($accounting_product_mode == 'ACCOUNTANCY_BUY_INTRA') {
  643. print length_accountg($obj->accountancy_code_buy_intra);
  644. if ($obj->accountancy_code_buy_intra && empty($obj->aaid)) {
  645. print ' '.img_warning($langs->trans("ValueNotIntoChartOfAccount"));
  646. }
  647. } elseif ($accounting_product_mode == 'ACCOUNTANCY_BUY_EXPORT') {
  648. print length_accountg($obj->accountancy_code_buy_export);
  649. if ($obj->accountancy_code_buy_export && empty($obj->aaid)) {
  650. print ' '.img_warning($langs->trans("ValueNotIntoChartOfAccount"));
  651. }
  652. } elseif ($accounting_product_mode == 'ACCOUNTANCY_SELL') {
  653. print length_accountg($obj->accountancy_code_sell);
  654. if ($obj->accountancy_code_sell && empty($obj->aaid)) {
  655. print ' '.img_warning($langs->trans("ValueNotIntoChartOfAccount"));
  656. }
  657. } elseif ($accounting_product_mode == 'ACCOUNTANCY_SELL_INTRA') {
  658. print length_accountg($obj->accountancy_code_sell_intra);
  659. if ($obj->accountancy_code_sell_intra && empty($obj->aaid)) {
  660. print ' '.img_warning($langs->trans("ValueNotIntoChartOfAccount"));
  661. }
  662. } else {
  663. print length_accountg($obj->accountancy_code_sell_export);
  664. if ($obj->accountancy_code_sell_export && empty($obj->aaid)) {
  665. print ' '.img_warning($langs->trans("ValueNotIntoChartOfAccount"));
  666. }
  667. }
  668. print '</td>';
  669. // New account to set
  670. $defaultvalue = '';
  671. if ($accounting_product_mode == 'ACCOUNTANCY_BUY') {
  672. // Accounting account buy
  673. print '<td class="left">';
  674. //$defaultvalue=GETPOST('codeventil_' . $product_static->id,'alpha'); This is id and we need a code
  675. if (empty($defaultvalue)) {
  676. $defaultvalue = $compta_prodbuy;
  677. }
  678. $codesell = length_accountg($obj->accountancy_code_buy);
  679. if (!empty($obj->aaid)) {
  680. $defaultvalue = ''; // Do not suggest default new value is code is already valid
  681. }
  682. print $form->select_account(($default_account > 0 && $confirm === 'yes' && in_array($product_static->id, $chk_prod)) ? $default_account : $defaultvalue, 'codeventil_'.$product_static->id, 1, array(), 1, 0, 'maxwidth300 maxwidthonsmartphone productforselect');
  683. print '</td>';
  684. } elseif ($accounting_product_mode == 'ACCOUNTANCY_BUY_INTRA') {
  685. // Accounting account buy intra (In EEC)
  686. print '<td class="left">';
  687. //$defaultvalue=GETPOST('codeventil_' . $product_static->id,'alpha'); This is id and we need a code
  688. if (empty($defaultvalue)) {
  689. $defaultvalue = $compta_prodbuy;
  690. }
  691. $codesell = length_accountg($obj->accountancy_code_buy_intra);
  692. //var_dump($defaultvalue.' - '.$codesell.' - '.$compta_prodsell);
  693. if (!empty($obj->aaid)) {
  694. $defaultvalue = ''; // Do not suggest default new value is code is already valid
  695. }
  696. print $form->select_account(($default_account > 0 && $confirm === 'yes' && in_array($product_static->id, $chk_prod)) ? $default_account : $defaultvalue, 'codeventil_'.$product_static->id, 1, array(), 1, 0, 'maxwidth300 maxwidthonsmartphone productforselect');
  697. print '</td>';
  698. } elseif ($accounting_product_mode == 'ACCOUNTANCY_BUY_EXPORT') {
  699. // Accounting account buy export (Out of EEC)
  700. print '<td class="left">';
  701. //$defaultvalue=GETPOST('codeventil_' . $product_static->id,'alpha'); This is id and we need a code
  702. if (empty($defaultvalue)) {
  703. $defaultvalue = $compta_prodbuy;
  704. }
  705. $codesell = length_accountg($obj->accountancy_code_buy_export);
  706. //var_dump($defaultvalue.' - '.$codesell.' - '.$compta_prodsell);
  707. if (!empty($obj->aaid)) {
  708. $defaultvalue = ''; // Do not suggest default new value is code is already valid
  709. }
  710. print $form->select_account(($default_account > 0 && $confirm === 'yes' && in_array($product_static->id, $chk_prod)) ? $default_account : $defaultvalue, 'codeventil_'.$product_static->id, 1, array(), 1, 0, 'maxwidth300 maxwidthonsmartphone productforselect');
  711. print '</td>';
  712. } elseif ($accounting_product_mode == 'ACCOUNTANCY_SELL') {
  713. // Accounting account sell
  714. print '<td class="left">';
  715. //$defaultvalue=GETPOST('codeventil_' . $product_static->id,'alpha'); This is id and we need a code
  716. if (empty($defaultvalue)) {
  717. $defaultvalue = $compta_prodsell;
  718. }
  719. $codesell = length_accountg($obj->accountancy_code_sell);
  720. //var_dump($defaultvalue.' - '.$codesell.' - '.$compta_prodsell);
  721. if (!empty($obj->aaid)) {
  722. $defaultvalue = ''; // Do not suggest default new value is code is already valid
  723. }
  724. print $form->select_account(($default_account > 0 && $confirm === 'yes' && in_array($product_static->id, $chk_prod)) ? $default_account : $defaultvalue, 'codeventil_'.$product_static->id, 1, array(), 1, 0, 'maxwidth300 maxwidthonsmartphone productforselect');
  725. print '</td>';
  726. } elseif ($accounting_product_mode == 'ACCOUNTANCY_SELL_INTRA') {
  727. // Accounting account sell intra (In EEC)
  728. print '<td class="left">';
  729. //$defaultvalue=GETPOST('codeventil_' . $product_static->id,'alpha'); This is id and we need a code
  730. if (empty($defaultvalue)) {
  731. $defaultvalue = $compta_prodsell;
  732. }
  733. $codesell = length_accountg($obj->accountancy_code_sell_intra);
  734. //var_dump($defaultvalue.' - '.$codesell.' - '.$compta_prodsell);
  735. if (!empty($obj->aaid)) {
  736. $defaultvalue = ''; // Do not suggest default new value is code is already valid
  737. }
  738. print $form->select_account(($default_account > 0 && $confirm === 'yes' && in_array($product_static->id, $chk_prod)) ? $default_account : $defaultvalue, 'codeventil_'.$product_static->id, 1, array(), 1, 0, 'maxwidth300 maxwidthonsmartphone productforselect');
  739. print '</td>';
  740. } else {
  741. // Accounting account sell export (Out of EEC)
  742. print '<td class="left">';
  743. //$defaultvalue=GETPOST('codeventil_' . $product_static->id,'alpha'); This is id and we need a code
  744. if (empty($defaultvalue)) {
  745. $defaultvalue = $compta_prodsell;
  746. }
  747. $codesell = length_accountg($obj->accountancy_code_sell_export);
  748. if (!empty($obj->aaid)) {
  749. $defaultvalue = ''; // Do not suggest default new value is code is already valid
  750. }
  751. print $form->select_account(($default_account > 0 && $confirm === 'yes' && in_array($product_static->id, $chk_prod)) ? $default_account : $defaultvalue, 'codeventil_'.$product_static->id, 1, array(), 1, 0, 'maxwidth300 maxwidthonsmartphone productforselect');
  752. print '</td>';
  753. }
  754. if (!empty($chk_prod)) {
  755. $ischecked = 0;
  756. if (in_array($product_static->id, $chk_prod)) {
  757. $ischecked=true;
  758. }
  759. }
  760. // Checkbox select
  761. print '<td class="center">';
  762. print '<input type="checkbox" class="checkforselect productforselectcodeventil_'.$product_static->id.'" name="chk_prod[]" '.($ischecked ? "checked" : "").' value="'.$obj->rowid.'"/></td>';
  763. print "</tr>";
  764. $i++;
  765. }
  766. print '</table>';
  767. print '</div>';
  768. print '<script type="text/javascript">
  769. jQuery(document).ready(function() {
  770. function init_savebutton()
  771. {
  772. console.log("We check if at least one line is checked")
  773. atleastoneselected=0;
  774. jQuery(".checkforselect").each(function( index ) {
  775. /* console.log( index + ": " + $( this ).text() ); */
  776. if ($(this).is(\':checked\')) atleastoneselected++;
  777. });
  778. if (atleastoneselected) jQuery("#changeaccount").removeAttr(\'disabled\');
  779. else jQuery("#changeaccount").attr(\'disabled\',\'disabled\');
  780. if (atleastoneselected) jQuery("#changeaccount").attr(\'class\',\'button\');
  781. else jQuery("#changeaccount").attr(\'class\',\'button\');
  782. }
  783. jQuery(".checkforselect").change(function() {
  784. init_savebutton();
  785. });
  786. jQuery(".productforselect").change(function() {
  787. console.log($(this).attr("id")+" "+$(this).val());
  788. if ($(this).val() && $(this).val() != -1) {
  789. $(".productforselect"+$(this).attr("id")).prop(\'checked\', true);
  790. } else {
  791. $(".productforselect"+$(this).attr("id")).prop(\'checked\', false);
  792. }
  793. init_savebutton();
  794. });
  795. init_savebutton();
  796. jQuery("#search_current_account").keyup(function() {
  797. if (jQuery("#search_current_account").val() != \'\')
  798. {
  799. console.log("We set a value of account to search "+jQuery("#search_current_account").val()+", so we disable the other search criteria on account");
  800. jQuery("#search_current_account_valid").val(-1);
  801. }
  802. });
  803. });
  804. </script>';
  805. print '</form>';
  806. $db->free($resql);
  807. } else {
  808. dol_print_error($db);
  809. }
  810. // End of page
  811. llxFooter();
  812. $db->close();