Completed
Branch develop (b928bb)
by
unknown
27:14
created

selectjournal_accountancy_bookkepping()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 20
nc 7
nop 4
dl 0
loc 29
rs 8.439
c 0
b 0
f 0
1
<?php
2
/* Copyright (C) 2013-2016 Florian Henry        <[email protected]>
3
 * Copyright (C) 2013-2014 Olivier Geffroy      <[email protected]>
4
 * Copyright (C) 2013-2016 Alexandre Spangaro   <[email protected]>
5
 * Copyright (C) 2015      Ari Elbaz (elarifr)  <[email protected]>
6
 * Copyright (C) 2016      Marcos García        <[email protected]>
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
/**
23
 * \file		htdocs/accountancy/class/html.formventilation.class.php
24
 * \ingroup		Advanced accountancy
25
 * \brief		File of class with all html predefined components
26
 */
27
28
/**
29
 * Class to manage generation of HTML components for bank module
30
 */
31
class FormVentilation extends Form
32
{
33
	/**
34
	 * Return select filter with date of transaction
35
	 *
36
	 * @param string $htmlname Name of select field
37
	 * @param string $selectedkey Value
38
	 * @return string HTML edit field
39
	 */
40
	function select_bookkeeping_importkey($htmlname = 'importkey', $selectedkey = '') {
41
		$options = array();
42
43
		$sql = 'SELECT DISTINCT import_key from ' . MAIN_DB_PREFIX . 'accounting_bookkeeping';
44
	    $sql .= " WHERE entity IN (" . getEntity("accountancy", 1) . ")";
45
		$sql .= ' ORDER BY import_key DESC';
46
47
		dol_syslog(get_class($this) . "::select_bookkeeping_importkey", LOG_DEBUG);
48
		$resql = $this->db->query($sql);
49
50
		if (!$resql) {
51
			$this->error = "Error " . $this->db->lasterror();
52
			dol_syslog(get_class($this) . "::select_bookkeeping_importkey " . $this->error, LOG_ERR);
53
			return - 1;
54
		}
55
56
		while ($obj = $this->db->fetch_object($resql)) {
57
			$options[$obj->import_key] = dol_print_date($obj->import_key, 'dayhourtext');
58
		}
59
60
		return Form::selectarray($htmlname, $options, $selectedkey);
61
	}
62
63
	/**
64
	 * Return list of accounts with label by chart of accounts
65
	 *
66
	 * @param string   $selectid           Preselected id or code of accounting accounts (depends on $select_in)
67
	 * @param string   $htmlname           Name of field in html form
68
	 * @param int      $showempty          Add an empty field
69
	 * @param array    $event              Event options
70
	 * @param int      $select_in          selectid value is a aa.rowid (0 default) or aa.account_number (1)
71
	 * @param int      $select_out         set value returned by select 0=rowid (default), 1=account_number
72
	 * @param string   $morecss            More css non HTML object
73
	 * @return string String with HTML select
74
	 */
75
	function select_account($selectid, $htmlname = 'account', $showempty = 0, $event = array(), $select_in = 0, $select_out = 0, $morecss='maxwidth300 maxwidthonsmartphone') {
76
		global $conf;
77
78
		require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
79
80
		$trunclength = defined('ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT') ? $conf->global->ACCOUNTING_LENGTH_DESCRIPTION_ACCOUNT : 50;
81
82
		$sql = "SELECT DISTINCT aa.account_number, aa.label, aa.rowid, aa.fk_pcg_version";
83
		$sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as aa";
84
		$sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
85
		$sql .= " AND asy.rowid = " . $conf->global->CHARTOFACCOUNTS;
86
		$sql .= " AND aa.active = 1";
87
		$sql .= " ORDER BY aa.account_number";
88
89
		dol_syslog(get_class($this) . "::select_account", LOG_DEBUG);
90
		$resql = $this->db->query($sql);
91
92
		if (!$resql) {
93
			$this->error = "Error " . $this->db->lasterror();
94
			dol_syslog(get_class($this) . "::select_account " . $this->error, LOG_ERR);
95
			return -1;
96
		}
97
98
		$out = ajax_combobox($htmlname, $event);
99
100
		// TODO Add $options in cache so next call will not execute the request
101
		$selected = 0;
102
		$options = array();
103
		while ($obj = $this->db->fetch_object($resql)) 
104
		{
105
			$label = length_accountg($obj->account_number) . ' - ' . $obj->label;
106
			$label = dol_trunc($label, $trunclength);
107
108
			$select_value_in = $obj->rowid;
109
			$select_value_out = $obj->rowid;
110
111
			// Try to guess if we have found default value
112
			if ($select_in == 1) {
113
				$select_value_in = $obj->account_number;
114
			}
115
			if ($select_out == 1) {
116
				$select_value_out = $obj->account_number;
117
			}
118
			// Remember guy's we store in database llx_facturedet the rowid of accounting_account and not the account_number
119
			// Because same account_number can be share between different accounting_system and do have the same meaning
120
			if ($selectid != '' && $selectid == $select_value_in) {
121
			    //var_dump("Found ".$selectid." ".$select_value_in);
122
				$selected = $select_value_out;
123
			}
124
125
			$options[$select_value_out] = $label;
126
		}
127
128
		$out .= Form::selectarray($htmlname, $options, $selected, $showempty, 0, 0, '', 0, 0, 0, '', $morecss, 1);
129
		$this->db->free($resql);
130
		
131
		return $out;
132
	}
133
134
	/**
135
	 * Return list of accounts with label by class of accounts
136
	 *
137
	 * @param string $selectid Preselected pcg_type
138
	 * @param string $htmlname Name of field in html form
139
	 * @param int $showempty Add an empty field
140
	 * @param array $event Event options
141
	 *
142
	 * @return string String with HTML select
143
	 */
144
	function select_pcgtype($selectid, $htmlname = 'pcg_type', $showempty = 0, $event = array()) {
145
		global $conf;
146
147
		$sql = "SELECT DISTINCT pcg_type ";
148
		$sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as aa";
149
		$sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
150
		$sql .= " AND asy.rowid = " . $conf->global->CHARTOFACCOUNTS;
151
		$sql .= " ORDER BY pcg_type";
152
153
		dol_syslog(get_class($this) . "::select_pcgtype", LOG_DEBUG);
154
		$resql = $this->db->query($sql);
155
156
		if (!$resql) {
157
			$this->error = "Error ".$this->db->lasterror();
158
			dol_syslog(get_class($this)."::select_pcgtype ".$this->error, LOG_ERR);
159
			return -1;
160
		}
161
162
		$options = array();
163
		$out = ajax_combobox($htmlname, $event);
164
165
		while ($obj = $this->db->fetch_object($resql)) {
166
			$options[$obj->pcg_type] = $obj->pcg_type;
167
		}
168
169
		$out .= Form::selectarray($htmlname, $options, $selectid, $showempty);
170
171
		$this->db->free($resql);
172
		return $out;
173
	}
174
175
	/**
176
	 * Return list of accounts with label by sub_class of accounts
177
	 *
178
	 * @param string $selectid Preselected pcg_type
179
	 * @param string $htmlname Name of field in html form
180
	 * @param int $showempty Add an empty field
181
	 * @param array $event Event options
182
	 *
183
	 * @return string String with HTML select
184
	 */
185
	function select_pcgsubtype($selectid, $htmlname = 'pcg_subtype', $showempty = 0, $event = array()) {
186
		global $conf;
187
188
		$sql = "SELECT DISTINCT pcg_subtype ";
189
		$sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as aa";
190
		$sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
191
		$sql .= " AND asy.rowid = " . $conf->global->CHARTOFACCOUNTS;
192
		$sql .= " ORDER BY pcg_subtype";
193
194
		dol_syslog(get_class($this) . "::select_pcgsubtype", LOG_DEBUG);
195
		$resql = $this->db->query($sql);
196
197
		if (!$resql) {
198
			$this->error = "Error ".$this->db->lasterror();
199
			dol_syslog(get_class($this)."::select_pcgsubtype ".$this->error, LOG_ERR);
200
			return -1;
201
		}
202
203
		$options = array();
204
		$out = ajax_combobox($htmlname, $event);
205
206
		while ($obj = $this->db->fetch_object($resql)) {
207
			$options[$obj->pcg_subtype] = $obj->pcg_subtype;
208
		}
209
210
		$out .= Form::selectarray($htmlname, $options, $selectid, $showempty);
211
212
		$this->db->free($resql);
213
		return $out;
214
	}
215
216
	/**
217
	 * Return list of auxilary thirdparty accounts
218
	 *
219
	 * @param string $selectid Preselected pcg_type
220
	 * @param string $htmlname Name of field in html form
221
	 * @param int $showempty Add an empty field
222
	 * @param array $event Event options
223
	 *
224
	 * @return string String with HTML select
225
	 */
226
	function select_auxaccount($selectid, $htmlname = 'account_num_aux', $showempty = 0, $event = array()) {
227
228
		$aux_account = array();
229
230
		// Auxiliary customer account
231
		$sql = "SELECT DISTINCT code_compta, nom ";
232
		$sql .= " FROM ".MAIN_DB_PREFIX."societe";
233
	    $sql .= " WHERE entity IN (" . getEntity("societe", 1) . ")";
234
		$sql .= " ORDER BY code_compta";
235
		dol_syslog(get_class($this)."::select_auxaccount", LOG_DEBUG);
236
		$resql = $this->db->query($sql);
237
		if ($resql) {
238
			while ($obj = $this->db->fetch_object($resql)) {
239
				if (!empty($obj->code_compta)) {
240
					$aux_account[$obj->code_compta] = $obj->code_compta.' ('.$obj->nom.')';
241
				}
242
			}
243
		} else {
244
			$this->error = "Error ".$this->db->lasterror();
245
			dol_syslog(get_class($this)."::select_pcgsubtype ".$this->error, LOG_ERR);
246
			return -1;
247
		}
248
		$this->db->free($resql);
249
250
		// Auxiliary supplier account
251
		$sql = "SELECT DISTINCT code_compta_fournisseur, nom ";
252
		$sql .= " FROM ".MAIN_DB_PREFIX."societe";
253
	    $sql .= " WHERE entity IN (" . getEntity("societe", 1) . ")";
254
		$sql .= " ORDER BY code_compta_fournisseur";
255
		dol_syslog(get_class($this)."::select_auxaccount", LOG_DEBUG);
256
		$resql = $this->db->query($sql);
257
		if ($resql) {
258
			while ($obj = $this->db->fetch_object($resql)) {
259
				if (!empty($obj->code_compta_fournisseur)) {
260
					$aux_account[$obj->code_compta_fournisseur] = $obj->code_compta_fournisseur.' ('.$obj->nom.')';
261
				}
262
			}
263
		} else {
264
			$this->error = "Error ".$this->db->lasterror();
265
			dol_syslog(get_class($this)."::select_pcgsubtype ".$this->error, LOG_ERR);
266
			return -1;
267
		}
268
		$this->db->free($resql);
269
270
		// Build select
271
		$out = ajax_combobox($htmlname, $event);
272
		$out .= Form::selectarray($htmlname, $aux_account, $selectid, $showempty, 0, 0, '', 0, 0, 0, '', 'maxwidth300');
273
274
		return $out;
275
	}
276
277
	/**
278
	 * Return HTML combo list of years existing into book keepping
279
	 *
280
	 * @param string $selected Preselected value
281
	 * @param string $htmlname Name of HTML select object
282
	 * @param int $useempty Affiche valeur vide dans liste
283
	 * @param string $output_format (html/opton (for option html only)/array (to return options arrays
284
	 * @return string/array
285
	 */
286
	function selectyear_accountancy_bookkepping($selected = '', $htmlname = 'yearid', $useempty = 0, $output_format = 'html')
287
	{
288
	    global $conf;
289
	    
290
		$out_array = array();
291
292
		$sql = "SELECT DISTINCT date_format(doc_date,'%Y') as dtyear";
293
		$sql .= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping";
294
	    $sql .= " WHERE entity IN (" . getEntity("accountancy", 1) . ")";
295
		$sql .= " ORDER BY date_format(doc_date,'%Y')";
296
		dol_syslog(get_class($this)."::".__METHOD__, LOG_DEBUG);
297
		$resql = $this->db->query($sql);
298
299
		if (!$resql) {
300
			$this->error = "Error ".$this->db->lasterror();
301
			dol_syslog(get_class($this)."::".__METHOD__.$this->error, LOG_ERR);
302
			return -1;
303
		}
304
		while ($obj = $this->db->fetch_object($resql)) {
305
			$out_array[$obj->dtyear] = $obj->dtyear;
306
		}
307
		$this->db->free($resql);
308
309
		if ($output_format == 'html') {
310
			return Form::selectarray($htmlname, $out_array, $selected, $useempty, 0, 0, 'placeholder="aa"');
311
		} else {
312
			return $out_array;
313
		}
314
	}
315
316
	/**
317
	 * Return HTML combo list of years existing into book keepping
318
	 *
319
	 * @param  string          $selected       Preselected value
320
	 * @param  string          $htmlname       Name of HTML select object
321
	 * @param  int             $useempty       Affiche valeur vide dans liste
322
	 * @param  string          $output_format  Html/option (for option html only)/array (to return options arrays
323
	 * @return string/array
324
	 */
325
	function selectjournal_accountancy_bookkepping($selected = '', $htmlname = 'journalid', $useempty = 0, $output_format = 'html')
326
	{
327
	    global $conf,$langs;
328
	    
329
		$out_array = array();
330
331
		$sql = "SELECT DISTINCT code_journal";
332
		$sql .= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping";
333
	    $sql .= " WHERE entity IN (" . getEntity("accountancy", 1) . ")";
334
		$sql .= " ORDER BY code_journal";
335
		dol_syslog(get_class($this)."::".__METHOD__, LOG_DEBUG);
336
		$resql = $this->db->query($sql);
337
338
		if (!$resql) {
339
			$this->error = "Error ".$this->db->lasterror();
340
			dol_syslog(get_class($this)."::".__METHOD__.$this->error, LOG_ERR);
341
			return -1;
342
		}
343
		while ($obj = $this->db->fetch_object($resql)) {
344
			$out_array[$obj->code_journal] = $obj->code_journal?$obj->code_journal:$langs->trans("NotDefined");  // TODO Not defined is accepted ? We should avoid this, shouldn't we ?
345
		}
346
		$this->db->free($resql);
347
348
		if ($output_format == 'html') {
349
			return Form::selectarray($htmlname, $out_array, $selected, $useempty, 0, 0, 'placeholder="aa"');
350
		} else {
351
			return $out_array;
352
		}
353
	}
354
}
355