importaccounts.php 6.5 KB

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