dolibarr  9.0.0
importaccounts.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
3  * Copyright (C) 2013-2017 Alexandre Spangaro <aspangaro@zendsi.com>
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 
26 require '../../main.inc.php';
27 
28 require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
29 require_once DOL_DOCUMENT_ROOT . '/accountancy/class/accountingaccount.class.php';
30 require_once DOL_DOCUMENT_ROOT . '/core/class/html.formaccounting.class.php';
31 
32 // Load translation files required by the page
33 $langs->loadLangs(array("compta","bills","accountancy"));
34 
35 // Security check
36 if (! $user->admin)
38 
39 $limit = GETPOST('limit','int')?GETPOST('limit','int'):(empty($conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION)?$conf->liste_limit:$conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION);
40 $sortfield = GETPOST("sortfield",'alpha');
41 $sortorder = GETPOST("sortorder",'alpha');
42 $page = GETPOST("page",'int');
43 if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
44 $offset = $limit * $page;
45 $pageprev = $page - 1;
46 $pagenext = $page + 1;
47 
48 
49 
50 
51 /*
52  * View
53  */
54 
55 llxHeader('', $langs->trans("ImportAccount"));
56 
57 $to_import = GETPOST("mesCasesCochees");
58 
59 if ($_POST["action"] == 'import') {
60  print '<div><font color="red">' . $langs->trans("Processing") . '...</font></div>';
61  if (is_array($to_import) && count($to_import) > 0) {
62  print '<div><font color="red">' . count($to_import) . ' ' . $langs->trans("SelectedLines") . '</font></div>';
63  $sql = 'SELECT pcg_version FROM ' . MAIN_DB_PREFIX . 'accounting_system WHERE rowid=' . $conf->global->CHARTOFACCOUNTS;
64 
65  $result = $db->query($sql);
66  if ($result && ($db->num_rows($result) > 0)) {
67 
68  $obj = $db->fetch_object($result);
69 
70  $cpt = 0;
71  foreach ( $to_import as $maLigneCochee ) {
72 
73  $accounting = new AccountingAccount($db);
74 
75  $monLabel = (string) GETPOST('label' . $maLigneCochee);
76  $monParentAccount = (string) GETPOST('AccountParent' . $maLigneCochee);
77  $monType = (string) GETPOST('pcgType' . $maLigneCochee);
78  $monSubType = (string) GETPOST('pcgSubType' . $maLigneCochee);
79 
80  $accounting->fk_pcg_version = $obj->pcg_version;
81  $accounting->account_number = $maLigneCochee;
82  $accounting->label = $monLabel;
83  $accounting->account_parent = $monParentAccount;
84  $accounting->pcg_type = $monType;
85  $accounting->pcg_subtype = $monSubType;
86  $accounting->active = 1;
87 
88  $result = $accounting->create($user);
89  if ($result > 0) {
90  setEventMessages($langs->trans("AccountingAccountAdd"), null, 'mesgs');
91  } else {
92  setEventMessages($accounting->error, $accounting->errors, 'errors');
93  }
94  $cpt ++;
95  }
96  } else {
97  setEventMessages($langs->trans('AccountPlanNotFoundCheckSetting'), null, 'errors');
98  }
99  } else {
100  print '<div><font color="red">' . $langs->trans("AnyLineImport") . '</font></div>';
101  }
102  print '<div><font color="red">' . $langs->trans("EndProcessing") . '</font></div>';
103 }
104 
105 // list accounting account from product
106 
107 $sql = "(SELECT p.rowid as product_id, p.accountancy_code_sell as accounting ";
108 $sql .= " FROM " . MAIN_DB_PREFIX . "product as p ";
109 $sql .= " WHERE p.accountancy_code_sell >=0";
110 $sql .= " GROUP BY accounting ";
111 $sql .= ")";
112 $sql .= "UNION ALL(SELECT p.rowid as product_id, p.accountancy_code_buy as accounting ";
113 $sql .= " FROM " . MAIN_DB_PREFIX . "product as p ";
114 $sql .= " WHERE p.accountancy_code_buy >=0";
115 $sql .= " GROUP BY accounting ";
116 $sql .= ") ";
117 $sql .= " ORDER BY accounting DESC " . $db->plimit($limit + 1, $offset);
118 
119 dol_syslog('accountancy/admin/importaccounts.php:: $sql=' . $sql);
120 $result = $db->query($sql);
121 if ($result) {
122  $num_lines = $db->num_rows($result);
123  $i = 0;
124  print_barre_liste($langs->trans("ImportAccount"), $page, $_SERVER["PHP_SELF"], "", $sortfield, $sortorder, '', $num_lines);
125 
126  print '<form action="' . $_SERVER["PHP_SELF"] . '" method="POST">' . "\n";
127  print '<input type="hidden" name="action" value="import">';
128 
129  print '<table class="noborder" width="100%">';
130  print '<tr class="liste_titre"><td>' . $langs->trans("AccountAccouting") . '</td>';
131  print '<td>' . $langs->trans("label") . '</td>';
132  print '<td>' . $langs->trans("Accountparent") . '</td>';
133  print '<td>' . $langs->trans("Pcgtype") . '</td>';
134  print '<td>' . $langs->trans("Pcgsubtype") . '</td>';
135  print '<td align="center">' . $langs->trans("Import") . '</td>';
136  print '</tr>';
137 
138  $form = new Form($db);
139  $formaccounting = new FormAccounting($db);
140 
141  while ( $i < min($num_lines, $limit) ) {
142  $objp = $db->fetch_object($result);
143  print '<tr class="oddeven">';
144 
145  print '<td align="left">';
146  print $objp->accounting;
147  print '</td>';
148 
149  print '<td align="left">';
150  print '<input name="label" size="30" value="">';
151  print '</td>';
152 
153  // Colonne choix du compte
154  print '<td>';
155  print $formaccounting->select_account($accounting->account_parent, 'AccountParent');
156  print '</td>';
157 
158  print '<td>';
159  print '<input type="text" name="pcgType" value="'.dol_escape_htmltag(isset($_POST['pcg_subtype'])?GETPOST('pcg_subtype','alpha'):$accounting->pcg_type).'">';
160  print '</td>';
161 
162  print '<td>';
163  print '<input type="text" name="pcgSubType" value="'.dol_escape_htmltag(isset($_POST['pcg_subtype'])?GETPOST('pcg_subtype','alpha'):$accounting->pcg_subtype).'">';
164  print '</td>';
165 
166  // Colonne choix ligne a ventiler
167  $checked = ('label' == 'O') ? ' checked' : '';
168 
169  print '<td align="center">';
170  print '<input type="checkbox" name="mesCasesCochees[]" ' . $checked . ' value="' . $objp->accounting . '"/>';
171  print '</td>';
172 
173  print '</tr>';
174  $i ++;
175  }
176 
177  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>';
178 
179  print '</table>';
180  print '</form>';
181 } else {
182  print $db->error();
183 }
184 
185 // End of page
186 llxFooter();
187 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:56
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
print
Draft customers invoices.
Definition: index.php:91
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Class to manage generation of HTML components Only common components must be here.
print_barre_liste($titre, $page, $file, $options='', $sortfield='', $sortorder='', $morehtmlcenter='', $num=-1, $totalnboflines='', $picto='title_generic.png', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limit=-1, $hideselectlimit=0, $hidenavigation=0)
Print a title with navigation controls for pagination.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
llxHeader()
Empty header.
Definition: wrapper.php:44
Class to manage generation of HTML components for accounting management.
Class to manage accounting accounts.