productaccount.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. <?php
  2. /* Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
  3. * Copyright (C) 2013-2016 Alexandre Spangaro <aspangaro.dolibarr@gmail.com>
  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. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file htdocs/accountancy/admin/productaccount.php
  23. * \ingroup Advanced accountancy
  24. * \brief To define accounting account on product / service
  25. */
  26. require '../../main.inc.php';
  27. // Class
  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. // Langs
  36. $langs->load("companies");
  37. $langs->load("compta");
  38. $langs->load("main");
  39. $langs->load("accountancy");
  40. $langs->load("products");
  41. // Security check
  42. if (empty($conf->accounting->enabled)) {
  43. accessforbidden();
  44. }
  45. if (! $user->rights->accounting->bind->write)
  46. accessforbidden();
  47. // search & action GETPOST
  48. $action = GETPOST('action', 'alpha');
  49. $codeventil_buy = GETPOST('codeventil_buy', 'array');
  50. $codeventil_sell = GETPOST('codeventil_sell', 'array');
  51. $chk_prod = GETPOST('chk_prod', 'array');
  52. $account_number_buy = GETPOST('account_number_buy');
  53. $account_number_sell = GETPOST('account_number_sell');
  54. $changeaccount = GETPOST('changeaccount', 'array');
  55. $changeaccount_buy = GETPOST('changeaccount_buy', 'array');
  56. $changeaccount_sell = GETPOST('changeaccount_sell', 'array');
  57. $search_ref = GETPOST('search_ref', 'alpha');
  58. $search_label = GETPOST('search_label', 'alpha');
  59. $search_desc = GETPOST('search_desc', 'alpha');
  60. $search_current_account = GETPOST('search_current_account', 'alpha');
  61. $search_current_account_valid = GETPOST('search_current_account_valid', 'alpha');
  62. if ($search_current_account_valid == '') $search_current_account_valid='withoutvalidaccount';
  63. $accounting_product_mode = GETPOST('accounting_product_mode', 'alpha');
  64. $btn_changeaccount = GETPOST('changeaccount');
  65. $btn_changetype = GETPOST('changetype');
  66. $limit = GETPOST("limit")?GETPOST("limit","int"):(empty($conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION)?$conf->liste_limit:$conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION);
  67. $sortfield = GETPOST("sortfield",'alpha');
  68. $sortorder = GETPOST("sortorder",'alpha');
  69. $page = GETPOST("page",'int');
  70. if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
  71. $offset = $limit * $page;
  72. $pageprev = $page - 1;
  73. $pagenext = $page + 1;
  74. if (! $sortfield) $sortfield="p.ref";
  75. if (! $sortorder) $sortorder="ASC";
  76. if (empty($action)) $action='list';
  77. $arrayfields=array();
  78. /*
  79. * Actions
  80. */
  81. if (GETPOST('cancel')) { $action='list'; $massaction=''; }
  82. if (! GETPOST('confirmmassaction') && $massaction != 'presend' && $massaction != 'confirm_presend') { $massaction=''; }
  83. $parameters=array();
  84. $reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
  85. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  86. // Purge search criteria
  87. if (GETPOST("button_removefilter_x") || GETPOST("button_removefilter.x") || GETPOST("button_removefilter")) // All test are required to be compatible with all browsers
  88. {
  89. $search_ref = '';
  90. $search_label = '';
  91. $search_desc = '';
  92. $search_current_account = '';
  93. $search_current_account_valid = '-1';
  94. }
  95. // Sales or Purchase mode ?
  96. if ($action == 'update') {
  97. if (! empty($btn_changetype)) {
  98. $error = 0;
  99. $accounting_product_modes = array (
  100. 'ACCOUNTANCY_SELL',
  101. 'ACCOUNTANCY_BUY'
  102. );
  103. $accounting_product_mode = GETPOST('accounting_product_mode', 'alpha');
  104. if (in_array($accounting_product_mode, $accounting_product_modes)) {
  105. if (! dolibarr_set_const($db, 'ACCOUNTING_PRODUCT_MODE', $accounting_product_mode, 'chaine', 0, '', $conf->entity)) {
  106. $error ++;
  107. }
  108. } else {
  109. $error ++;
  110. }
  111. }
  112. if (! empty($btn_changeaccount)) {
  113. //$msg = '<div><span class="accountingprocessing">' . $langs->trans("Processing") . '...</span></div>';
  114. if (! empty($chk_prod)) {
  115. $accounting = new AccountingAccount($db);
  116. //$msg .= '<div><span class="accountingprocessing">' . count($chk_prod) . ' ' . $langs->trans("SelectedLines") . '</span></div>';
  117. $cpt = 0; $ok = 0; $ko = 0;
  118. foreach ( $chk_prod as $productid ) {
  119. $accounting_account_id = GETPOST('codeventil_' . $productid);
  120. $result = $accounting->fetch($accounting_account_id, null, 1);
  121. if ($result < 0) {
  122. // setEventMessages(null, $accounting->errors, 'errors');
  123. $msg .= '<div><font color="red">' . $langs->trans("ErrorDB") . ' : ' . $langs->trans("Product") . ' ' . $productid . ' ' . $langs->trans("NotVentilatedinAccount") . ' : id=' . $accounting_account_id . '<br/> <pre>' . $sql . '</pre></font></div>';
  124. } else {
  125. $sql = " UPDATE " . MAIN_DB_PREFIX . "product";
  126. if ($accounting_product_mode == 'ACCOUNTANCY_BUY') {
  127. $sql .= " SET accountancy_code_buy = " . $accounting->account_number;
  128. }
  129. if ($accounting_product_mode == 'ACCOUNTANCY_SELL') {
  130. $sql .= " SET accountancy_code_sell = " . $accounting->account_number;
  131. }
  132. $sql .= " WHERE rowid = " . $productid;
  133. dol_syslog("/accountancy/admin/productaccount.php sql=" . $sql, LOG_DEBUG);
  134. if ($db->query($sql)) {
  135. $ok++;
  136. //$msg .= '<div><font color="green">' . $langs->trans("Product") . ' ' . $productid . ' - ' . $langs->trans("VentilatedinAccount") . ' : ' . length_accountg($accounting->account_number) . '</font></div>';
  137. } else {
  138. $ko++;
  139. //$msg .= '<div><font color="red">' . $langs->trans("ErrorDB") . ' : ' . $langs->trans("Product") . ' ' . $productid . ' ' . $langs->trans("NotVentilatedinAccount") . ' : ' . length_accountg($accounting->account_number) . '<br/> <pre>' . $sql . '</pre></font></div>';
  140. }
  141. }
  142. $cpt ++;
  143. }
  144. } else {
  145. //$msg .= '<div><span class="accountingprocessing">' . $langs->trans("AnyLineVentilate") . '</span></div>';
  146. }
  147. if ($ko) setEventMessages($langs->trans("XLineFailedToBeBinded", $ko), null, 'errors');
  148. if ($ok) setEventMessages($langs->trans("XLineSuccessfullyBinded", $ok), null, 'mesgs');
  149. //$msg .= '<div><span class="accountingprocessing">' . $langs->trans("EndProcessing") . '</span></div>';
  150. }
  151. }
  152. /*
  153. * View
  154. */
  155. $form = new FormAccounting($db);
  156. // Defaut AccountingAccount RowId Product / Service
  157. // at this time ACCOUNTING_SERVICE_SOLD_ACCOUNT & ACCOUNTING_PRODUCT_SOLD_ACCOUNT are account number not accountingacount rowid
  158. // so we need to get those default value rowid first
  159. $accounting = new AccountingAccount($db);
  160. // TODO: we should need to check if result is a really exist accountaccount rowid.....
  161. $aarowid_servbuy = $accounting->fetch('', $conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT, 1);
  162. $aarowid_prodbuy = $accounting->fetch('', $conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT, 1);
  163. $aarowid_servsell = $accounting->fetch('', $conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT, 1);
  164. $aarowid_prodsell = $accounting->fetch('', $conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT, 1);
  165. $aacompta_servbuy = (! empty($conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT : $langs->trans("CodeNotDef"));
  166. $aacompta_prodbuy = (! empty($conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT : $langs->trans("CodeNotDef"));
  167. $aacompta_servsell = (! empty($conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT : $langs->trans("CodeNotDef"));
  168. $aacompta_prodsell = (! empty($conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT : $langs->trans("CodeNotDef"));
  169. llxHeader('', $langs->trans("ProductsBinding"));
  170. $pcgverid = $conf->global->CHARTOFACCOUNTS;
  171. $pcgvercode = dol_getIdFromCode($db, $pcgverid, 'accounting_system', 'rowid', 'pcg_version');
  172. if (empty($pcgvercode)) $pcgvercode=$pcgverid;
  173. $sql = "SELECT p.rowid, p.ref, p.label, p.description, p.tosell, p.tobuy, p.accountancy_code_sell, p.accountancy_code_buy, p.tms, p.fk_product_type as product_type,";
  174. $sql.= " aa.rowid as aaid";
  175. $sql.= " FROM " . MAIN_DB_PREFIX . "product as p";
  176. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa ON";
  177. if ($accounting_product_mode == 'ACCOUNTANCY_BUY') {
  178. $sql.=" p.accountancy_code_buy = aa.account_number AND aa.fk_pcg_version = '" . $pcgvercode . "'";
  179. }
  180. else
  181. {
  182. $sql.=" p.accountancy_code_sell = aa.account_number AND aa.fk_pcg_version = '" . $pcgvercode . "'";
  183. }
  184. $sql.= ' WHERE p.entity IN ('.getEntity('product').')';
  185. if ($accounting_product_mode == 'ACCOUNTANCY_BUY') {
  186. if (strlen(trim($search_current_account))) {
  187. $sql .= natural_search("p.accountancy_code_buy",$search_current_account);
  188. }
  189. } else {
  190. if (strlen(trim($search_current_account))) {
  191. $sql .= natural_search("p.accountancy_code_sell",$search_current_account);
  192. }
  193. }
  194. if ($search_current_account_valid == 'withoutvalidaccount')
  195. {
  196. $sql .= " AND aa.account_number IS NULL";
  197. }
  198. if ($search_current_account_valid == 'withvalidaccount')
  199. {
  200. $sql .= " AND aa.account_number IS NOT NULL";
  201. }
  202. // Add search filter like
  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("p.description",$search_desc);
  211. }
  212. $sql .= $db->order($sortfield, $sortorder);
  213. $nbtotalofrecords = '';
  214. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
  215. {
  216. $result = $db->query($sql);
  217. $nbtotalofrecords = $db->num_rows($result);
  218. }
  219. $sql .= $db->plimit($limit + 1, $offset);
  220. dol_syslog("/accountancy/admin/productaccount.php:: sql=" . $sql, LOG_DEBUG);
  221. $result = $db->query($sql);
  222. if ($result)
  223. {
  224. $num = $db->num_rows($result);
  225. $i = 0;
  226. $param='';
  227. if (! empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param.='&contextpage='.$contextpage;
  228. if ($limit > 0 && $limit != $conf->liste_limit) $param.='&limit='.$limit;
  229. if ($search_ref > 0) $param.="&search_desc=".urlencode($search_ref);
  230. if ($search_label > 0) $param.="&search_desc=".urlencode($search_label);
  231. if ($search_desc > 0) $param.="&search_desc=".urlencode($search_desc);
  232. if ($search_current_account > 0) $param.="&search_current_account=".urlencode($search_current_account);
  233. if ($search_current_account_valid && $search_current_account_valid != '-1') $param.="&search_current_account_valid=".urlencode($search_current_account_valid);
  234. print '<form action="' . $_SERVER["PHP_SELF"] . '" method="post">';
  235. if ($optioncss != '') print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  236. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  237. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  238. print '<input type="hidden" name="action" value="update">';
  239. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  240. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  241. print '<input type="hidden" name="page" value="'.$page.'">';
  242. print load_fiche_titre($langs->trans("ProductsBinding"), '', 'title_accountancy');
  243. print '<br>';
  244. print $langs->trans("InitAccountancyDesc") . '<br>';
  245. print '<br>';
  246. // Select mode
  247. print '<table class="noborder" width="100%">';
  248. print '<tr class="liste_titre">';
  249. print '<td>' . $langs->trans('Options') . '</td><td>' . $langs->trans('Description') . '</td>';
  250. print "</tr>\n";
  251. print '<tr ' . $bc[false] . '><td class="titlefield"><input type="radio" name="accounting_product_mode" value="ACCOUNTANCY_SELL"' . ($accounting_product_mode != 'ACCOUNTANCY_BUY' ? ' checked' : '') . '> ' . $langs->trans('OptionModeProductSell') . '</td>';
  252. print '<td>'.$langs->trans('OptionModeProductSellDesc');
  253. print "</td></tr>\n";
  254. print '<tr ' . $bc[true] . '><td class="titlefield"><input type="radio" name="accounting_product_mode" value="ACCOUNTANCY_BUY"' . ($accounting_product_mode == 'ACCOUNTANCY_BUY' ? ' checked' : '') . '> ' . $langs->trans('OptionModeProductBuy') . '</td>';
  255. print '<td>'.$langs->trans('OptionModeProductBuyDesc')."</td></tr>\n";
  256. print "</table>\n";
  257. print '<div align="center"><input type="submit" class="button" value="' . $langs->trans('Refresh') . '" name="changetype"></div>';
  258. print "<br>\n";
  259. // Filter on categories
  260. $moreforfilter='';
  261. $varpage=empty($contextpage)?$_SERVER["PHP_SELF"]:$contextpage;
  262. $selectedfields=$form->multiSelectArrayWithCheckbox('selectedfields', $arrayfields, $varpage); // This also change content of $arrayfields
  263. $texte=$langs->trans("ListOfProductsServices");
  264. print_barre_liste($texte, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, $nbtotalofrecords, '', 0, '', '', $limit);
  265. print '<table class="liste '.($moreforfilter?"listwithfilterbefore":"").'">';
  266. print '<tr class="liste_titre_filter">';
  267. print '<td class="liste_titre"><input type="text" class="flat" size="8" name="search_ref" value="' . dol_escape_htmltag($search_ref) . '"></td>';
  268. print '<td class="liste_titre"><input type="text" class="flat" size="10" name="search_label" value="' . dol_escape_htmltag($search_label) . '"></td>';
  269. if (! empty($conf->global->ACCOUNTANCY_SHOW_PROD_DESC)) print '<td class="liste_titre"><input type="text" class="flat" size="20" name="search_desc" value="' . dol_escape_htmltag($search_desc) . '"></td>';
  270. // On sell
  271. print '<td class="liste_titre"></td>';
  272. // On buy
  273. print '<td class="liste_titre"></td>';
  274. // Current account
  275. print '<td class="liste_titre">';
  276. print '<input type="text" class="flat" size="6" name="search_current_account" value="' . dol_escape_htmltag($search_current_account) . '">';
  277. $listofvals=array('withoutvalidaccount'=>$langs->trans("WithoutValidAccount"), 'withvalidaccount'=>$langs->trans("WithValidAccount"));
  278. print ' '.$langs->trans("or").' '.$form->selectarray('search_current_account_valid', $listofvals, $search_current_account_valid, 1);
  279. print '</td>';
  280. print '<td class="liste_titre">&nbsp;</td>';
  281. print '<td align="center" class="liste_titre">';
  282. $searchpicto=$form->showFilterButtons();
  283. print $searchpicto;
  284. print '</td>';
  285. print '</tr>';
  286. print '<tr class="liste_titre">';
  287. print_liste_field_titre($langs->trans("Ref"), $_SERVER["PHP_SELF"], "p.ref", "", $param, '', $sortfield, $sortorder);
  288. print_liste_field_titre($langs->trans("Label"), $_SERVER["PHP_SELF"], "p.label", "", $param, '', $sortfield, $sortorder);
  289. if (! empty($conf->global->ACCOUNTANCY_SHOW_PROD_DESC)) print_liste_field_titre($langs->trans("Description"), $_SERVER["PHP_SELF"], "p.description", "", $param, '', $sortfield, $sortorder);
  290. print_liste_field_titre($langs->trans("OnSell"), $_SERVER["PHP_SELF"], "p.tosell", "", $param, 'align="center"', $sortfield, $sortorder);
  291. print_liste_field_titre($langs->trans("OnBuy"), $_SERVER["PHP_SELF"], "p.tobuy", "", $param, 'align="center"', $sortfield, $sortorder);
  292. if ($accounting_product_mode == 'ACCOUNTANCY_BUY') {
  293. $fieldtosortaccount="p.accountancy_code_buy";
  294. }
  295. else $fieldtosortaccount="p.accountancy_code_sell";
  296. print_liste_field_titre($langs->trans("CurrentDedicatedAccountingAccount"), $_SERVER["PHP_SELF"], $fieldtosortaccount, "", $param, '', $sortfield, $sortorder);
  297. print_liste_field_titre($langs->trans("AssignDedicatedAccountingAccount"));
  298. $clickpitco=$form->showCheckAddButtons('checkforselect', 1);
  299. print_liste_field_titre($clickpitco, '', '', '', '', 'align="center"');
  300. print '</tr>';
  301. $product_static = new Product($db);
  302. $var = true;
  303. $i=0;
  304. while ($i < min($num,$limit))
  305. {
  306. $obj = $db->fetch_object($result);
  307. // Ref produit as link
  308. $product_static->ref = $obj->ref;
  309. $product_static->id = $obj->rowid;
  310. $product_static->type = $obj->product_type;
  311. $product_static->label = $obj->label;
  312. $product_static->description = $obj->description;
  313. $product_static->status = $obj->tosell;
  314. $product_static->status_buy = $obj->tobuy;
  315. if ($obj->product_type == 0) {
  316. $compta_prodsell = (! empty($conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT : $langs->trans("CodeNotDef"));
  317. $compta_prodsell_id = $aarowid_prodsell;
  318. } else {
  319. $compta_prodsell = (! empty($conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT : $langs->trans("CodeNotDef"));
  320. $compta_prodsell_id = $aarowid_servsell;
  321. }
  322. if ($obj->product_type == 0) {
  323. $compta_prodbuy = (! empty($conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT : $langs->trans("CodeNotDef"));
  324. $compta_prodbuy_id = $aarowid_prodbuy;
  325. } else {
  326. $compta_prodbuy = (! empty($conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT : $langs->trans("CodeNotDef"));
  327. $compta_prodbuy_id = $aarowid_servbuy;
  328. }
  329. print '<tr class="oddeven">';
  330. print '<td>';
  331. print $product_static->getNomUrl(1);
  332. print '</td>';
  333. print '<td align="left">'.$obj->label.'</td>';
  334. if (! empty($conf->global->ACCOUNTANCY_SHOW_PROD_DESC))
  335. {
  336. // TODO ADJUST DESCRIPTION SIZE
  337. // print '<td align="left">' . $obj->description . '</td>';
  338. // TODO: we shoul set a user defined value to adjust user square / wide screen size
  339. $trunclengh = defined('ACCOUNTING_LENGTH_DESCRIPTION') ? ACCOUNTING_LENGTH_DESCRIPTION : 64;
  340. print '<td style="' . $code_sell_p_l_differ . '">' . nl2br(dol_trunc($obj->description, $trunclengh)) . '</td>';
  341. }
  342. print '<td align="center">'.$product_static->getLibStatut(3, 0).'</td>';
  343. print '<td align="center">'.$product_static->getLibStatut(3, 1).'</td>';
  344. // Current accounting account
  345. print '<td align="left">';
  346. if ($accounting_product_mode == 'ACCOUNTANCY_BUY') {
  347. print length_accountg($obj->accountancy_code_buy);
  348. if ($obj->accountancy_code_buy && empty($obj->aaid)) print ' '.img_warning($langs->trans("ValueNotIntoChartOfAccount"));
  349. }
  350. else
  351. {
  352. print length_accountg($obj->accountancy_code_sell);
  353. if ($obj->accountancy_code_sell && empty($obj->aaid)) print ' '.img_warning($langs->trans("ValueNotIntoChartOfAccount"));
  354. }
  355. print '</td>';
  356. // Dedicated account
  357. $defaultvalue='';
  358. if ($accounting_product_mode == 'ACCOUNTANCY_BUY') {
  359. // Accounting account buy
  360. print '<td align="left">';
  361. //$defaultvalue=GETPOST('codeventil_' . $product_static->id,'alpha'); This is id and we need a code
  362. if (empty($defaultvalue)) $defaultvalue=$compta_prodbuy;
  363. $codesell=length_accountg($obj->accountancy_code_buy);
  364. if (! empty($obj->aaid)) $defaultvalue = ''; // Do not suggest default new value is code is already valid
  365. print $form->select_account($defaultvalue, 'codeventil_' . $product_static->id, 1, array(), 1);
  366. print '</td>';
  367. } else {
  368. // Accounting account sell
  369. print '<td align="left">';
  370. //$defaultvalue=GETPOST('codeventil_' . $product_static->id,'alpha'); This is id and we need a code
  371. if (empty($defaultvalue)) $defaultvalue=$compta_prodsell;
  372. $codesell=length_accountg($obj->accountancy_code_sell);
  373. //var_dump($defaultvalue.' - '.$codesell.' - '.$compta_prodsell);
  374. if (! empty($obj->aaid)) $defaultvalue = ''; // Do not suggest default new value is code is already valid
  375. print $form->select_account($defaultvalue, 'codeventil_' . $product_static->id, 1, array(), 1);
  376. print '</td>';
  377. }
  378. // Checkbox select
  379. print '<td align="center">';
  380. print '<input type="checkbox" class="checkforselect" name="chk_prod[]" value="' . $obj->rowid . '"/></td>';
  381. print "</tr>";
  382. $i ++;
  383. }
  384. print '</table>';
  385. // Example : Adding jquery code
  386. print '<script type="text/javascript" language="javascript">
  387. jQuery(document).ready(function() {
  388. function init_savebutton()
  389. {
  390. console.log("We check if at least one line is checked")
  391. atleastoneselected=0;
  392. jQuery(".checkforselect").each(function( index ) {
  393. /* console.log( index + ": " + $( this ).text() ); */
  394. if ($(this).is(\':checked\')) atleastoneselected++;
  395. });
  396. if (atleastoneselected) jQuery("#changeaccount").removeAttr(\'disabled\');
  397. else jQuery("#changeaccount").attr(\'disabled\',\'disabled\');
  398. if (atleastoneselected) jQuery("#changeaccount").attr(\'class\',\'butAction\');
  399. else jQuery("#changeaccount").attr(\'class\',\'butActionRefused\');
  400. }
  401. jQuery(".checkforselect, #checkallactions").click(function() {
  402. init_savebutton();
  403. });
  404. init_savebutton();
  405. });
  406. </script>';
  407. print '<br><div align="center"><input type="submit" class="butAction" id="changeaccount" name="changeaccount" value="' . $langs->trans("Save") . '"></div>';
  408. print '</form>';
  409. $db->free($result);
  410. } else {
  411. dol_print_error($db);
  412. }
  413. llxFooter();
  414. $db->close();