importaccounts.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /*
  3. * Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
  4. * Copyright (C) 2013-2014 Alexandre Spangaro <alexandre.spangaro@gmail.com>
  5. * Copyright (C) 2014 Florian Henry <florian.henry@open-concept.pro>
  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 Accounting Expert
  23. * \brief Page import accounting account
  24. */
  25. require '../../main.inc.php';
  26. // Class
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingaccount.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/accountancy/class/html.formventilation.class.php';
  30. // langs
  31. $langs->load("compta");
  32. $langs->load("bills");
  33. $langs->load("main");
  34. $langs->load("accountancy");
  35. // Security check
  36. if (!$user->admin)
  37. accessforbidden();
  38. llxHeader('', $langs->trans("ImportAccount"));
  39. $to_import = GETPOST("mesCasesCochees");
  40. if ($_POST["action"] == 'import') {
  41. print '<div><font color="red">' . $langs->trans("Processing") . '...</font></div>';
  42. if (is_array($to_import) && count($to_import) > 0) {
  43. print '<div><font color="red">' . count($to_import) . ' ' . $langs->trans("SelectedLines") . '</font></div>';
  44. $sql = 'SELECT pcg_version FROM ' . MAIN_DB_PREFIX . 'accounting_system WHERE rowid=' . $conf->global->CHARTOFACCOUNTS;
  45. $result = $db->query($sql);
  46. if ($result && ($db->num_rows($result) > 0)) {
  47. $obj = $db->fetch_object($result);
  48. $cpt = 0;
  49. foreach ( $to_import as $maLigneCochee ) {
  50. $accounting = new AccountingAccount($db);
  51. $monLabel = GETPOST('label' . $maLigneCochee);
  52. $monParentAccount = GETPOST('AccountParent' . $maLigneCochee);
  53. $monType = GETPOST('pcgType' . $maLigneCochee);
  54. $monSubType = GETPOST('pcgSubType' . $maLigneCochee);
  55. $accounting->fk_pcg_version = $obj->pcg_version;
  56. $accounting->account_number = $maLigneCochee;
  57. $accounting->label = $monLabel;
  58. $accounting->account_parent = $monParentAccount;
  59. $accounting->pcg_type = $monType;
  60. $accounting->pcg_subtype = $monSubType;
  61. $accounting->active = 1;
  62. $result = $accounting->create($user);
  63. if ($result > 0) {
  64. setEventMessage($langs->trans("AccountingAccountAdd"), 'mesgs');
  65. } else {
  66. setEventMessage($accounting->error, 'errors');
  67. }
  68. $cpt ++;
  69. }
  70. } else {
  71. setEventMessage($langs->trans('AccountPlanNotFoundCheckSetting'), 'errors');
  72. }
  73. } else {
  74. print '<div><font color="red">' . $langs->trans("AnyLineImport") . '</font></div>';
  75. }
  76. print '<div><font color="red">' . $langs->trans("EndProcessing") . '</font></div>';
  77. }
  78. /*
  79. * list accounting account from product
  80. *
  81. */
  82. $page = GETPOST("page");
  83. if ($page < 0)
  84. $page = 0;
  85. $limit = $conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION;
  86. $offset = $limit * $page;
  87. $sql = "(SELECT p.rowid as product_id, p.accountancy_code_sell as accounting ";
  88. $sql .= " FROM " . MAIN_DB_PREFIX . "product as p ";
  89. $sql .= " WHERE p.accountancy_code_sell >=0";
  90. $sql .= " GROUP BY accounting ";
  91. $sql .= ")";
  92. $sql .= "UNION ALL(SELECT p.rowid as product_id, p.accountancy_code_buy as accounting ";
  93. $sql .= " FROM " . MAIN_DB_PREFIX . "product as p ";
  94. $sql .= " WHERE p.accountancy_code_buy >=0";
  95. $sql .= " GROUP BY accounting ";
  96. $sql .= ") ";
  97. $sql .= " ORDER BY accounting DESC " . $db->plimit($limit + 1, $offset);
  98. dol_syslog('accountancy/admin/importaccounts.php:: $sql=' . $sql);
  99. $result = $db->query($sql);
  100. if ($result) {
  101. $num_lines = $db->num_rows($result);
  102. $i = 0;
  103. print_barre_liste($langs->trans("ImportAccount"), $page, $_SERVER["PHP_SELF"], "", $sortfield, $sortorder, '', $num_lines);
  104. print '<form action="' . $_SERVER["PHP_SELF"] . '" method="POST">' . "\n";
  105. print '<input type="hidden" name="action" value="import">';
  106. print '<table class="noborder" width="100%">';
  107. print '<tr class="liste_titre"><td>' . $langs->trans("accountingaccount") . '</td>';
  108. print '<td>' . $langs->trans("label") . '</td>';
  109. print '<td>' . $langs->trans("Accountparent") . '</td>';
  110. print '<td>' . $langs->trans("Pcgtype") . '</td>';
  111. print '<td>' . $langs->trans("Pcgsubtype") . '</td>';
  112. print '<td align="center">' . $langs->trans("Import") . '</td>';
  113. print '</tr>';
  114. $form = new Form($db);
  115. $htmlacc = new FormVentilation($db);
  116. $var = true;
  117. while ( $i < min($num_lines, $limit) ) {
  118. $objp = $db->fetch_object($result);
  119. $var = ! $var;
  120. print "<tr $bc[$var]>";
  121. print '<td align="left">';
  122. print $objp->accounting;
  123. print '</td>';
  124. print '<td align="left">';
  125. print '<input name="label" size="30" value="">';
  126. print '</td>';
  127. // Colonne choix du compte
  128. print '<td>';
  129. print $htmlacc->select_account($accounting->account_parent, 'AccountParent');
  130. print '</td>';
  131. print '<td>';
  132. print $htmlacc->select_pcgtype($accounting->pcg_type, 'pcgType');
  133. print '</td>';
  134. print '<td>';
  135. print $htmlacc->select_pcgsubtype($accounting->pcg_subtype, 'pcgSubType');
  136. print '</td>';
  137. // Colonne choix ligne a ventiler
  138. $checked = ('label' == 'O') ? ' checked' : '';
  139. print '<td align="center">';
  140. print '<input type="checkbox" name="mesCasesCochees[]" ' . $checked . ' value="' . $objp->accounting . '"/>';
  141. print '</td>';
  142. print '</tr>';
  143. $i ++;
  144. }
  145. 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>';
  146. print '</table>';
  147. print '</form>';
  148. } else {
  149. print $db->error();
  150. }
  151. llxFooter();
  152. $db->close();