productaccount.php 22 KB

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