importaccounts.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /* Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
  3. * Copyright (C) 2013-2017 Alexandre Spangaro <aspangaro@open-dsi.fr>
  4. * Copyright (C) 2014 Florian Henry <florian.henry@open-concept.pro>
  5. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/accountancy/admin/importaccounts.php
  22. * \ingroup Advanced accountancy
  23. * \brief Page import accounting account
  24. */
  25. require '../../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
  27. require_once DOL_DOCUMENT_ROOT . '/accountancy/class/accountingaccount.class.php';
  28. require_once DOL_DOCUMENT_ROOT . '/core/class/html.formaccounting.class.php';
  29. // Load translation files required by the page
  30. $langs->loadLangs(array("compta","bills","accountancy"));
  31. // Security check
  32. if (! $user->admin)
  33. accessforbidden();
  34. $limit = GETPOST('limit','int')?GETPOST('limit','int'):(empty($conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION)?$conf->liste_limit:$conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION);
  35. $sortfield = GETPOST("sortfield",'alpha');
  36. $sortorder = GETPOST("sortorder",'alpha');
  37. $page = GETPOST("page",'int');
  38. if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
  39. $offset = $limit * $page;
  40. $pageprev = $page - 1;
  41. $pagenext = $page + 1;
  42. /*
  43. * View
  44. */
  45. llxHeader('', $langs->trans("ImportAccount"));
  46. $to_import = GETPOST("mesCasesCochees");
  47. if ($_POST["action"] == 'import') {
  48. print '<div><font color="red">' . $langs->trans("Processing") . '...</font></div>';
  49. if (is_array($to_import) && count($to_import) > 0) {
  50. print '<div><font color="red">' . count($to_import) . ' ' . $langs->trans("SelectedLines") . '</font></div>';
  51. $sql = 'SELECT pcg_version FROM ' . MAIN_DB_PREFIX . 'accounting_system WHERE rowid=' . $conf->global->CHARTOFACCOUNTS;
  52. $result = $db->query($sql);
  53. if ($result && ($db->num_rows($result) > 0)) {
  54. $obj = $db->fetch_object($result);
  55. $cpt = 0;
  56. foreach ( $to_import as $maLigneCochee ) {
  57. $accounting = new AccountingAccount($db);
  58. $monLabel = (string) GETPOST('label' . $maLigneCochee);
  59. $monParentAccount = (string) GETPOST('AccountParent' . $maLigneCochee);
  60. $monType = (string) GETPOST('pcgType' . $maLigneCochee);
  61. $monSubType = (string) GETPOST('pcgSubType' . $maLigneCochee);
  62. $accounting->fk_pcg_version = $obj->pcg_version;
  63. $accounting->account_number = $maLigneCochee;
  64. $accounting->label = $monLabel;
  65. $accounting->account_parent = $monParentAccount;
  66. $accounting->pcg_type = $monType;
  67. $accounting->pcg_subtype = $monSubType;
  68. $accounting->active = 1;
  69. $result = $accounting->create($user);
  70. if ($result > 0) {
  71. setEventMessages($langs->trans("AccountingAccountAdd"), null, 'mesgs');
  72. } else {
  73. setEventMessages($accounting->error, $accounting->errors, 'errors');
  74. }
  75. $cpt ++;
  76. }
  77. } else {
  78. setEventMessages($langs->trans('AccountPlanNotFoundCheckSetting'), null, 'errors');
  79. }
  80. } else {
  81. print '<div><font color="red">' . $langs->trans("AnyLineImport") . '</font></div>';
  82. }
  83. print '<div><font color="red">' . $langs->trans("EndProcessing") . '</font></div>';
  84. }
  85. // list accounting account from product
  86. $sql = "(SELECT p.rowid as product_id, p.accountancy_code_sell as accounting ";
  87. $sql .= " FROM " . MAIN_DB_PREFIX . "product as p ";
  88. $sql .= " WHERE p.accountancy_code_sell >=0";
  89. $sql .= " GROUP BY accounting ";
  90. $sql .= ")";
  91. $sql .= "UNION ALL(SELECT p.rowid as product_id, p.accountancy_code_buy as accounting ";
  92. $sql .= " FROM " . MAIN_DB_PREFIX . "product as p ";
  93. $sql .= " WHERE p.accountancy_code_buy >=0";
  94. $sql .= " GROUP BY accounting ";
  95. $sql .= ") ";
  96. $sql .= " ORDER BY accounting DESC " . $db->plimit($limit + 1, $offset);
  97. dol_syslog('accountancy/admin/importaccounts.php:: $sql=' . $sql);
  98. $result = $db->query($sql);
  99. if ($result) {
  100. $num_lines = $db->num_rows($result);
  101. $i = 0;
  102. print_barre_liste($langs->trans("ImportAccount"), $page, $_SERVER["PHP_SELF"], "", $sortfield, $sortorder, '', $num_lines);
  103. print '<form action="' . $_SERVER["PHP_SELF"] . '" method="POST">' . "\n";
  104. print '<input type="hidden" name="action" value="import">';
  105. print '<table class="noborder" width="100%">';
  106. print '<tr class="liste_titre"><td>' . $langs->trans("AccountAccouting") . '</td>';
  107. print '<td>' . $langs->trans("label") . '</td>';
  108. print '<td>' . $langs->trans("Accountparent") . '</td>';
  109. print '<td>' . $langs->trans("Pcgtype") . '</td>';
  110. print '<td>' . $langs->trans("Pcgsubtype") . '</td>';
  111. print '<td align="center">' . $langs->trans("Import") . '</td>';
  112. print '</tr>';
  113. $form = new Form($db);
  114. $formaccounting = new FormAccounting($db);
  115. while ( $i < min($num_lines, $limit) ) {
  116. $objp = $db->fetch_object($result);
  117. print '<tr class="oddeven">';
  118. print '<td class="left">';
  119. print $objp->accounting;
  120. print '</td>';
  121. print '<td class="left">';
  122. print '<input name="label" size="30" value="">';
  123. print '</td>';
  124. // Colonne choix du compte
  125. print '<td>';
  126. print $formaccounting->select_account($accounting->account_parent, 'AccountParent');
  127. print '</td>';
  128. print '<td>';
  129. print '<input type="text" name="pcgType" value="'.dol_escape_htmltag(isset($_POST['pcg_subtype'])?GETPOST('pcg_subtype','alpha'):$accounting->pcg_type).'">';
  130. print '</td>';
  131. print '<td>';
  132. print '<input type="text" name="pcgSubType" value="'.dol_escape_htmltag(isset($_POST['pcg_subtype'])?GETPOST('pcg_subtype','alpha'):$accounting->pcg_subtype).'">';
  133. print '</td>';
  134. // Colonne choix ligne a ventiler
  135. $checked = ('label' == 'O') ? ' checked' : '';
  136. print '<td align="center">';
  137. print '<input type="checkbox" name="mesCasesCochees[]" ' . $checked . ' value="' . $objp->accounting . '"/>';
  138. print '</td>';
  139. print '</tr>';
  140. $i ++;
  141. }
  142. print '<tr><td colspan="8">&nbsp;</td></tr><tr><td colspan="8" align="center"><input type="submit" class="butAction" value="' . $langs->trans("Import") . '"></td></tr>';
  143. print '</table>';
  144. print '</form>';
  145. } else {
  146. print $db->error();
  147. }
  148. // End of page
  149. llxFooter();
  150. $db->close();