Completed
Branch develop (eec106)
by
unknown
20:48
created

BookKeeping::deleteByImportkey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 1
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/* Copyright (C) 2014-2017 Olivier Geffroy		<[email protected]>
3
 * Copyright (C) 2015-2017 Alexandre Spangaro	<[email protected]>
4
 * Copyright (C) 2015-2017 Florian Henry		<[email protected]>
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
/**
21
 *	\file		htdocs/accountancy/class/bookkeeping.class.php
22
 *	\ingroup	Advanced accountancy
23
 *	\brief		File of class to manage Ledger (General Ledger and Subledger)
24
 */
25
26
// Class
27
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php';
28
29
/**
30
 * Class to manage Ledger (General Ledger and Subledger)
31
 */
32
class BookKeeping extends CommonObject
33
{
34
	/**
35
	 *
36
	 * @var string Error code (or message)
37
	 * @deprecated
38
	 *
39
	 * @see Accountingbookkeeping::errors
40
	 */
41
	public $error;
42
	/**
43
	 *
44
	 * @var string[] Error codes (or messages)
45
	 */
46
	public $errors = array ();
47
	/**
48
	 *
49
	 * @var string Id to identify managed objects
50
	 */
51
	public $element = 'accountingbookkeeping';
52
	/**
53
	 *
54
	 * @var string Name of table without prefix where object is stored
55
	 */
56
	public $table_element = 'accounting_bookkeeping';
57
58
	public $entity = 1;
59
60
	/**
61
	 *
62
	 * @var BookKeepingLine[] Lines
63
	 */
64
	public $lines = array ();
65
66
	/**
67
	 *
68
	 * @var int ID
69
	 */
70
	public $id;
71
	/**
72
	 */
73
	public $doc_date;
74
	public $doc_type;
75
	public $doc_ref;
76
	public $fk_doc;
77
	public $fk_docdet;
78
	public $thirdparty_code;
79
	public $subledger_account;
80
	public $subledger_label;
81
	public $numero_compte;
82
	public $label_compte;
83
	public $label_operation;
84
	public $debit;
85
	public $credit;
86
	public $montant;
87
	public $sens;
88
	public $fk_user_author;
89
	public $import_key;
90
	public $code_journal;
91
	public $journal_label;
92
	public $piece_num;
93
94
	/**
95
	 * Constructor
96
	 *
97
	 * @param DoliDb $db Database handler
98
	 */
99
	public function __construct(DoliDB $db) {
100
		$this->db = $db;
101
	}
102
103
	/**
104
	 * Create object into database
105
	 *
106
	 * @param  User	$user		User that creates
107
	 * @param  bool	$notrigger	false=launch triggers after, true=disable triggers
108
	 * @return int				<0 if KO, Id of created object if OK
109
	 */
110
	public function create(User $user, $notrigger = false) {
111
		global $conf, $langs;
112
113
		dol_syslog(__METHOD__, LOG_DEBUG);
114
115
		$error = 0;
116
117
		// Clean parameters
118
		if (isset($this->doc_type)) {
119
			$this->doc_type = trim($this->doc_type);
120
		}
121
		if (isset($this->doc_ref)) {
122
			$this->doc_ref = trim($this->doc_ref);
123
		}
124
		if (isset($this->fk_doc)) {
125
			$this->fk_doc = trim($this->fk_doc);
126
		}
127
		if (isset($this->fk_docdet)) {
128
			$this->fk_docdet = trim($this->fk_docdet);
129
		}
130
		if (isset($this->thirdparty_code)) {
131
			$this->thirdparty_code = trim($this->thirdparty_code);
132
		}
133
		if (isset($this->subledger_account)) {
134
			$this->subledger_account = trim($this->subledger_account);
135
		}
136
		if (isset($this->subledger_label)) {
137
			$this->subledger_label = trim($this->subledger_label);
138
		}
139
		if (isset($this->numero_compte)) {
140
			$this->numero_compte = trim($this->numero_compte);
141
		}
142
		if (isset($this->label_compte)) {
143
			$this->label_compte = trim($this->label_compte);
144
		}
145
		if (isset($this->label_operation)) {
146
			$this->label_operation = trim($this->label_operation);
147
		}
148
		if (isset($this->debit)) {
149
			$this->debit = trim($this->debit);
150
		}
151
		if (isset($this->credit)) {
152
			$this->credit = trim($this->credit);
153
		}
154
		if (isset($this->montant)) {
155
			$this->montant = trim($this->montant);
156
		}
157
		if (isset($this->sens)) {
158
			$this->sens = trim($this->sens);
159
		}
160
		if (isset($this->fk_user_author)) {
161
			$this->fk_user_author = trim($this->fk_user_author);
162
		}
163
		if (isset($this->import_key)) {
164
			$this->import_key = trim($this->import_key);
165
		}
166
		if (isset($this->code_journal)) {
167
			$this->code_journal = trim($this->code_journal);
168
		}
169
		if (isset($this->journal_label)) {
170
			$this->journal_label = trim($this->journal_label);
171
		}
172
		if (isset($this->piece_num)) {
173
			$this->piece_num = trim($this->piece_num);
174
		}
175
		if (empty($this->debit)) $this->debit = 0;
176
		if (empty($this->credit)) $this->credit = 0;
177
178
		// Check parameters
179
		if (empty($this->numero_compte) || $this->numero_compte == '-1')
180
		{
181
			$langs->load("errors");
182
			if (in_array($this->doc_type, array('bank', 'expense_report')))
183
			{
184
				$this->errors[]=$langs->trans('ErrorFieldAccountNotDefinedForBankLine', $this->fk_docdet,  $this->doc_type);
185
			}
186
			else
187
			{
188
				//$this->errors[]=$langs->trans('ErrorFieldAccountNotDefinedForInvoiceLine', $this->doc_ref,  $this->label_compte);
189
				$mesg=$this->doc_ref.', '.$langs->trans("AccountAccounting").': '.$this->numero_compte;
190
				if ($this->subledger_account && $this->subledger_account != $this->numero_compte)
191
				{
192
					$mesg.=', '.$langs->trans("SubledgerAccount").': '.$this->subledger_account;
193
				}
194
				$this->errors[]=$langs->trans('ErrorFieldAccountNotDefinedForLine', $mesg);
195
			}
196
197
			return -1;
198
		}
199
200
		$this->db->begin();
201
202
		$this->piece_num = 0;
203
204
		// First check if line not yet already in bookkeeping
205
		$sql = "SELECT count(*) as nb";
206
		$sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element;
207
		$sql .= " WHERE doc_type = '" . $this->db->escape($this->doc_type) . "'";
208
		$sql .= " AND fk_doc = " . $this->fk_doc;
209
		$sql .= " AND fk_docdet = " . $this->fk_docdet;					// This field can be 0 is record is for several lines
210
		$sql .= " AND numero_compte = '" . $this->db->escape($this->numero_compte) . "'";
211
		$sql .= " AND entity IN (" . getEntity('accountancy') . ")";
212
213
		$resql = $this->db->query($sql);
214
215
		if ($resql) {
216
			$row = $this->db->fetch_object($resql);
217
			if ($row->nb == 0)
218
			{
219
				// Determine piece_num
220
				$sqlnum = "SELECT piece_num";
221
				$sqlnum .= " FROM " . MAIN_DB_PREFIX . $this->table_element;
222
				$sqlnum .= " WHERE doc_type = '" . $this->db->escape($this->doc_type) . "'";		// For example doc_type = 'bank'
223
				$sqlnum .= " AND fk_docdet = " . $this->db->escape($this->fk_docdet);				// fk_docdet is rowid into llx_bank or llx_facturedet or llx_facturefourndet, or ...
224
				$sqlnum .= " AND doc_ref = '" . $this->db->escape($this->doc_ref) . "'";			// ref of source object
225
				$sqlnum .= " AND entity IN (" . getEntity('accountancy') . ")";
226
227
				dol_syslog(get_class($this) . ":: create sqlnum=" . $sqlnum, LOG_DEBUG);
228
				$resqlnum = $this->db->query($sqlnum);
229
				if ($resqlnum) {
230
					$objnum = $this->db->fetch_object($resqlnum);
231
					$this->piece_num = $objnum->piece_num;
232
				}
233
				dol_syslog(get_class($this) . ":: create this->piece_num=" . $this->piece_num, LOG_DEBUG);
234
				if (empty($this->piece_num)) {
235
					$sqlnum = "SELECT MAX(piece_num)+1 as maxpiecenum";
236
					$sqlnum .= " FROM " . MAIN_DB_PREFIX . $this->table_element;
237
					$sqlnum .= " WHERE entity IN (" . getEntity('accountancy') . ")";
238
239
					dol_syslog(get_class($this) . ":: create sqlnum=" . $sqlnum, LOG_DEBUG);
240
					$resqlnum = $this->db->query($sqlnum);
241
					if ($resqlnum) {
242
						$objnum = $this->db->fetch_object($resqlnum);
243
						$this->piece_num = $objnum->maxpiecenum;
244
					}
245
				}
246
				dol_syslog(get_class($this) . ":: create this->piece_num=" . $this->piece_num, LOG_DEBUG);
247
				if (empty($this->piece_num)) {
248
					$this->piece_num = 1;
249
				}
250
251
				$now = dol_now();
252
				if (empty($this->date_create)) {
253
					$this->date_create = $now;
254
				}
255
256
				$sql = "INSERT INTO " . MAIN_DB_PREFIX . $this->table_element . " (";
257
				$sql .= "doc_date";
258
				$sql .= ", doc_type";
259
				$sql .= ", doc_ref";
260
				$sql .= ", fk_doc";
261
				$sql .= ", fk_docdet";
262
				$sql .= ", thirdparty_code";
263
				$sql .= ", subledger_account";
264
				$sql .= ", subledger_label";
265
				$sql .= ", numero_compte";
266
				$sql .= ", label_compte";
267
				$sql .= ", label_operation";
268
				$sql .= ", debit";
269
				$sql .= ", credit";
270
				$sql .= ", montant";
271
				$sql .= ", sens";
272
				$sql .= ", fk_user_author";
273
				$sql .= ", date_creation";
274
				$sql .= ", code_journal";
275
				$sql .= ", journal_label";
276
				$sql .= ", piece_num";
277
				$sql .= ', entity';
278
				$sql .= ") VALUES (";
279
				$sql .= "'" . $this->db->idate($this->doc_date) . "'";
280
				$sql .= ",'" . $this->db->escape($this->doc_type) . "'";
281
				$sql .= ",'" . $this->db->escape($this->doc_ref) . "'";
282
				$sql .= "," . $this->fk_doc;
283
				$sql .= "," . $this->fk_docdet;
284
				$sql .= ",'" . $this->db->escape($this->thirdparty_code) . "'";
285
				$sql .= ",'" . $this->db->escape($this->subledger_account) . "'";
286
				$sql .= ",'" . $this->db->escape($this->subledger_label) . "'";
287
				$sql .= ",'" . $this->db->escape($this->numero_compte) . "'";
288
				$sql .= ",'" . $this->db->escape($this->label_compte) . "'";
289
				$sql .= ",'" . $this->db->escape($this->label_operation) . "'";
290
				$sql .= "," . $this->debit;
291
				$sql .= "," . $this->credit;
292
				$sql .= "," . $this->montant;
293
				$sql .= ",'" . $this->db->escape($this->sens) . "'";
294
				$sql .= ",'" . $this->db->escape($this->fk_user_author) . "'";
295
				$sql .= ",'" . $this->db->idate($this->date_create). "'";
296
				$sql .= ",'" . $this->db->escape($this->code_journal) . "'";
297
				$sql .= ",'" . $this->db->escape($this->journal_label) . "'";
298
				$sql .= "," . $this->db->escape($this->piece_num);
299
				$sql .= ", " . (! isset($this->entity) ? '1' : $this->entity);
300
				$sql .= ")";
301
302
				dol_syslog(get_class($this) . ":: create sql=" . $sql, LOG_DEBUG);
303
				$resql = $this->db->query($sql);
304
				if ($resql) {
305
					$id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
306
307
					if ($id > 0) {
308
						$this->id = $id;
309
						$result = 0;
310
					} else {
311
						$result = -2;
312
						$error ++;
313
						$this->errors[] = 'Error Create Error ' . $result . ' lecture ID';
314
						dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
315
					}
316
				} else {
317
					$result = -1;
318
					$error ++;
319
					$this->errors[] = 'Error ' . $this->db->lasterror();
320
					dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
321
				}
322
			} else {	// Already exists
323
				$result = -3;
324
				$error++;
325
				$this->error='BookkeepingRecordAlreadyExists';
326
				dol_syslog(__METHOD__ . ' ' . $this->error, LOG_WARNING);
327
			}
328
		} else {
329
			$result = -5;
330
			$error ++;
331
			$this->errors[] = 'Error ' . $this->db->lasterror();
332
			dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
333
		}
334
335
		if (! $error) {
336
337
			if (! $notrigger) {
338
				// Uncomment this and change MYOBJECT to your own tag if you
339
				// want this action to call a trigger.
340
341
				// // Call triggers
342
				// $result=$this->call_trigger('MYOBJECT_CREATE',$user);
343
				// if ($result < 0) $error++;
344
				// // End call triggers
345
			}
346
		}
347
348
		// Commit or rollback
349
		if ($error) {
350
			$this->db->rollback();
351
			return -1 * $error;
352
		} else {
353
			$this->db->commit();
354
			return $result;
355
		}
356
	}
357
358
	/**
359
	 * Create object into database
360
	 *
361
	 * @param  User	$user	   User that creates
362
	 * @param  bool	$notrigger  false=launch triggers after, true=disable triggers
363
	 * @param  string  $mode 	   Mode
364
	 * @return int				 <0 if KO, Id of created object if OK
365
	 */
366
	public function createStd(User $user, $notrigger = false, $mode='') {
367
		dol_syslog(__METHOD__, LOG_DEBUG);
368
369
		$error = 0;
370
371
		// Clean parameters
372
373
		if (isset($this->doc_type)) {
374
			$this->doc_type = trim($this->doc_type);
375
		}
376
		if (isset($this->doc_ref)) {
377
			$this->doc_ref = trim($this->doc_ref);
378
		}
379
		if (isset($this->fk_doc)) {
380
			$this->fk_doc = trim($this->fk_doc);
381
		}
382
		if (isset($this->fk_docdet)) {
383
			$this->fk_docdet = trim($this->fk_docdet);
384
		}
385
		if (isset($this->thirdparty_code)) {
386
			$this->thirdparty_code = trim($this->thirdparty_code);
387
		}
388
		if (isset($this->subledger_account)) {
389
			$this->subledger_account = trim($this->subledger_account);
390
		}
391
		if (isset($this->subledger_label)) {
392
			$this->subledger_label = trim($this->subledger_label);
393
		}
394
		if (isset($this->numero_compte)) {
395
			$this->numero_compte = trim($this->numero_compte);
396
		}
397
		if (isset($this->label_compte)) {
398
			$this->label_compte = trim($this->label_compte);
399
		}
400
		if (isset($this->label_operation)) {
401
			$this->label_operation = trim($this->label_operation);
402
		}
403
		if (isset($this->debit)) {
404
			$this->debit = trim($this->debit);
405
		}
406
		if (isset($this->credit)) {
407
			$this->credit = trim($this->credit);
408
		}
409
		if (isset($this->montant)) {
410
			$this->montant = trim($this->montant);
411
		}
412
		if (isset($this->sens)) {
413
			$this->sens = trim($this->sens);
414
		}
415
		if (isset($this->fk_user_author)) {
416
			$this->fk_user_author = trim($this->fk_user_author);
417
		}
418
		if (isset($this->import_key)) {
419
			$this->import_key = trim($this->import_key);
420
		}
421
		if (isset($this->code_journal)) {
422
			$this->code_journal = trim($this->code_journal);
423
		}
424
		if (isset($this->journal_label)) {
425
			$this->journal_label = trim($this->journal_label);
426
		}
427
		if (isset($this->piece_num)) {
428
			$this->piece_num = trim($this->piece_num);
429
		}
430
		if (empty($this->debit)) $this->debit = 0;
431
		if (empty($this->credit)) $this->credit = 0;
432
433
		$this->debit = price2num($this->debit, 'MT');
434
		$this->credit = price2num($this->credit, 'MT');
435
436
		$now = dol_now();
437
		if (empty($this->date_create)) {
438
		    $this->date_create = $now;
439
		}
440
441
		// Check parameters
442
		// Put here code to add control on parameters values
443
444
		// Insert request
445
		$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this->table_element . $mode.'(';
446
		$sql .= 'doc_date,';
447
		$sql .= 'doc_type,';
448
		$sql .= 'doc_ref,';
449
		$sql .= 'fk_doc,';
450
		$sql .= 'fk_docdet,';
451
		$sql .= 'thirdparty_code,';
452
		$sql .= 'subledger_account,';
453
		$sql .= 'subledger_label,';
454
		$sql .= 'numero_compte,';
455
		$sql .= 'label_compte,';
456
		$sql .= 'label_operation,';
457
		$sql .= 'debit,';
458
		$sql .= 'credit,';
459
		$sql .= 'montant,';
460
		$sql .= 'sens,';
461
		$sql .= 'fk_user_author,';
462
		$sql .= 'date_creation,';
463
		$sql .= 'code_journal,';
464
		$sql .= 'journal_label,';
465
		$sql .= 'piece_num,';
466
		$sql .= 'entity';
467
		$sql .= ') VALUES (';
468
		$sql .= ' ' . (! isset($this->doc_date) || dol_strlen($this->doc_date) == 0 ? 'NULL' : "'" . $this->db->idate($this->doc_date) . "'") . ',';
469
		$sql .= ' ' . (! isset($this->doc_type) ? 'NULL' : "'" . $this->db->escape($this->doc_type) . "'") . ',';
470
		$sql .= ' ' . (! isset($this->doc_ref) ? 'NULL' : "'" . $this->db->escape($this->doc_ref) . "'") . ',';
471
		$sql .= ' ' . (empty($this->fk_doc) ? '0' : $this->fk_doc) . ',';
472
		$sql .= ' ' . (empty($this->fk_docdet) ? '0' : $this->fk_docdet) . ',';
473
		$sql .= ' ' . (! isset($this->thirdparty_code) ? 'NULL' : "'" . $this->db->escape($this->thirdparty_code) . "'") . ',';
474
		$sql .= ' ' . (! isset($this->subledger_account) ? 'NULL' : "'" . $this->db->escape($this->subledger_account) . "'") . ',';
475
		$sql .= ' ' . (! isset($this->subledger_label) ? 'NULL' : "'" . $this->db->escape($this->subledger_label) . "'") . ',';
476
		$sql .= ' ' . (! isset($this->numero_compte) ? 'NULL' : "'" . $this->db->escape($this->numero_compte) . "'") . ',';
477
		$sql .= ' ' . (! isset($this->label_compte) ? 'NULL' : "'" . $this->db->escape($this->label_compte) . "'") . ',';
478
		$sql .= ' ' . (! isset($this->label_operation) ? 'NULL' : "'" . $this->db->escape($this->label_operation) . "'") . ',';
479
		$sql .= ' ' . (! isset($this->debit) ? 'NULL' : $this->debit ). ',';
480
		$sql .= ' ' . (! isset($this->credit) ? 'NULL' : $this->credit ). ',';
481
		$sql .= ' ' . (! isset($this->montant) ? 'NULL' : $this->montant ). ',';
482
		$sql .= ' ' . (! isset($this->sens) ? 'NULL' : "'" . $this->db->escape($this->sens) . "'") . ',';
483
		$sql .= ' ' . $user->id . ',';
484
		$sql .= ' ' . "'" . $this->db->idate($this->date_create) . "',";
485
		$sql .= ' ' . (empty($this->code_journal) ? 'NULL' : "'" . $this->db->escape($this->code_journal) . "'") . ',';
486
		$sql .= ' ' . (empty($this->journal_label) ? 'NULL' : "'" . $this->db->escape($this->journal_label) . "'") . ',';
487
		$sql .= ' ' . (empty($this->piece_num) ? 'NULL' : $this->db->escape($this->piece_num)).',';
488
		$sql .= ' ' . (! isset($this->entity) ? '1' : $this->entity);
489
		$sql .= ')';
490
491
		$this->db->begin();
492
493
		$resql = $this->db->query($sql);
494
		if (! $resql) {
495
			$error ++;
496
			$this->errors[] = 'Error ' . $this->db->lasterror();
497
			dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
498
		}
499
500
		if (! $error) {
501
			$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
502
503
			if (! $notrigger) {
504
				// Uncomment this and change MYOBJECT to your own tag if you
505
				// want this action to call a trigger.
506
507
				// // Call triggers
508
				// $result=$this->call_trigger('MYOBJECT_CREATE',$user);
509
				// if ($result < 0) $error++;
510
				// // End call triggers
511
			}
512
		}
513
514
		// Commit or rollback
515
		if ($error) {
516
			$this->db->rollback();
517
518
			return - 1 * $error;
519
		} else {
520
			$this->db->commit();
521
522
			return $this->id;
523
		}
524
	}
525
526
	/**
527
	 * Load object in memory from the database
528
	 *
529
	 * @param int $id Id object
530
	 * @param string $ref Ref
531
	 * @param string $mode 	Mode
532
	 *
533
	 * @return int <0 if KO, 0 if not found, >0 if OK
534
	 */
535
	public function fetch($id, $ref = null, $mode='') {
536
		global $conf;
537
538
		dol_syslog(__METHOD__, LOG_DEBUG);
539
540
		$sql = 'SELECT';
541
		$sql .= ' t.rowid,';
542
		$sql .= " t.doc_date,";
543
		$sql .= " t.doc_type,";
544
		$sql .= " t.doc_ref,";
545
		$sql .= " t.fk_doc,";
546
		$sql .= " t.fk_docdet,";
547
		$sql .= " t.thirdparty_code,";
548
		$sql .= " t.subledger_account,";
549
		$sql .= " t.subledger_label,";
550
		$sql .= " t.numero_compte,";
551
		$sql .= " t.label_compte,";
552
		$sql .= " t.label_operation,";
553
		$sql .= " t.debit,";
554
		$sql .= " t.credit,";
555
		$sql .= " t.montant,";
556
		$sql .= " t.sens,";
557
		$sql .= " t.fk_user_author,";
558
		$sql .= " t.import_key,";
559
		$sql .= " t.code_journal,";
560
		$sql .= " t.journal_label,";
561
		$sql .= " t.piece_num";
562
		$sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element.$mode. ' as t';
563
		$sql .= ' WHERE 1 = 1';
564
		$sql .= " AND entity IN (" . getEntity('accountancy') . ")";
565
		if (null !== $ref) {
566
			$sql .= ' AND t.ref = ' . '\'' . $ref . '\'';
567
		} else {
568
			$sql .= ' AND t.rowid = ' . $id;
569
		}
570
571
		$resql = $this->db->query($sql);
572
		if ($resql) {
573
			$numrows = $this->db->num_rows($resql);
574
			if ($numrows) {
575
				$obj = $this->db->fetch_object($resql);
576
577
				$this->id = $obj->rowid;
578
579
				$this->doc_date = $this->db->jdate($obj->doc_date);
580
				$this->doc_type = $obj->doc_type;
581
				$this->doc_ref = $obj->doc_ref;
582
				$this->fk_doc = $obj->fk_doc;
583
				$this->fk_docdet = $obj->fk_docdet;
584
				$this->thirdparty_code = $obj->thirdparty_code;
585
				$this->subledger_account = $obj->subledger_account;
586
				$this->subledger_label = $obj->subledger_label;
587
				$this->numero_compte = $obj->numero_compte;
588
				$this->label_compte = $obj->label_compte;
589
				$this->label_operation = $obj->label_operation;
590
				$this->debit = $obj->debit;
591
				$this->credit = $obj->credit;
592
				$this->montant = $obj->montant;
593
				$this->sens = $obj->sens;
594
				$this->fk_user_author = $obj->fk_user_author;
595
				$this->import_key = $obj->import_key;
596
				$this->code_journal = $obj->code_journal;
597
				$this->journal_label = $obj->journal_label;
598
				$this->piece_num = $obj->piece_num;
599
			}
600
			$this->db->free($resql);
601
602
			if ($numrows) {
603
				return 1;
604
			} else {
605
				return 0;
606
			}
607
		} else {
608
			$this->errors[] = 'Error ' . $this->db->lasterror();
609
			dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
610
611
			return - 1;
612
		}
613
	}
614
615
616
	/**
617
	 * Load object in memory from the database
618
	 *
619
	 * @param string $sortorder Sort Order
620
	 * @param string $sortfield Sort field
621
	 * @param int $limit offset limit
622
	 * @param int $offset offset limit
623
	 * @param array $filter filter array
624
	 * @param string $filtermode filter mode (AND or OR)
625
	 *
626
	 * @return int <0 if KO, >0 if OK
627
	 */
628
	public function fetchAllByAccount($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND') {
629
		global $conf;
630
631
		dol_syslog(__METHOD__, LOG_DEBUG);
632
633
		$sql = 'SELECT';
634
		$sql .= ' t.rowid,';
635
		$sql .= " t.doc_date,";
636
		$sql .= " t.doc_type,";
637
		$sql .= " t.doc_ref,";
638
		$sql .= " t.fk_doc,";
639
		$sql .= " t.fk_docdet,";
640
		$sql .= " t.thirdparty_code,";
641
		$sql .= " t.subledger_account,";
642
		$sql .= " t.subledger_label,";
643
		$sql .= " t.numero_compte,";
644
		$sql .= " t.label_compte,";
645
		$sql .= " t.label_operation,";
646
		$sql .= " t.debit,";
647
		$sql .= " t.credit,";
648
		$sql .= " t.montant,";
649
		$sql .= " t.sens,";
650
		$sql .= " t.fk_user_author,";
651
		$sql .= " t.import_key,";
652
		$sql .= " t.code_journal,";
653
		$sql .= " t.journal_label,";
654
		$sql .= " t.piece_num";
655
		// Manage filter
656
		$sqlwhere = array ();
657
		if (count($filter) > 0) {
658
			foreach ( $filter as $key => $value ) {
659
				if ($key == 't.doc_date') {
660
					$sqlwhere[] = $key . '=\'' . $this->db->idate($value) . '\'';
661
				} elseif ($key == 't.doc_date>=' || $key == 't.doc_date<=') {
662
					$sqlwhere[] = $key . '\'' . $this->db->idate($value) . '\'';
663
				} elseif ($key == 't.numero_compte>=' || $key == 't.numero_compte<=' || $key == 't.subledger_account>=' || $key == 't.subledger_account<=') {
664
					$sqlwhere[] = $key . '\'' . $this->db->escape($value) . '\'';
665
				} elseif ($key == 't.fk_doc' || $key == 't.fk_docdet' || $key == 't.piece_num') {
666
					$sqlwhere[] = $key . '=' . $value;
667
				} elseif ($key == 't.subledger_account' || $key == 't.numero_compte') {
668
					$sqlwhere[] = $key . ' LIKE \'' . $this->db->escape($value) . '%\'';
669
				} elseif ($key == 't.label_operation') {
670
					$sqlwhere[] = $key . ' LIKE \'' . $this->db->escape($value) . '%\'';
671
				} else {
672
					$sqlwhere[] = $key . ' LIKE \'%' . $this->db->escape($value) . '%\'';
673
				}
674
			}
675
		}
676
		$sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
677
		$sql .= ' WHERE 1 = 1';
678
		$sql .= " AND entity IN (" . getEntity('accountancy') . ")";
679
		if (count($sqlwhere) > 0) {
680
			$sql .= ' AND ' . implode(' ' . $filtermode . ' ', $sqlwhere);
681
		}
682
		// Affichage par compte comptable
683
		$sql .= ' ORDER BY t.numero_compte ASC';
684
		if (! empty($sortfield)) {
685
			$sql .= ', ' . $sortfield . ' ' .$sortorder;
686
		}
687
		if (! empty($limit)) {
688
			$sql .= ' ' . $this->db->plimit($limit + 1, $offset);
689
		}
690
		$this->lines = array ();
691
692
		$resql = $this->db->query($sql);
693
		if ($resql) {
694
			$num = $this->db->num_rows($resql);
695
696
			while ( $obj = $this->db->fetch_object($resql) ) {
697
				$line = new BookKeepingLine();
698
699
				$line->id = $obj->rowid;
700
701
				$line->doc_date = $this->db->jdate($obj->doc_date);
702
				$line->doc_type = $obj->doc_type;
703
				$line->doc_ref = $obj->doc_ref;
704
				$line->fk_doc = $obj->fk_doc;
705
				$line->fk_docdet = $obj->fk_docdet;
706
				$line->thirdparty_code = $obj->thirdparty_code;
707
				$line->subledger_account = $obj->subledger_account;
708
				$line->subledger_label = $obj->subledger_label;
709
				$line->numero_compte = $obj->numero_compte;
710
				$line->label_compte = $obj->label_compte;
711
				$line->label_operation = $obj->label_operation;
712
				$line->debit = $obj->debit;
713
				$line->credit = $obj->credit;
714
				$line->montant = $obj->montant;
715
				$line->sens = $obj->sens;
716
				$line->fk_user_author = $obj->fk_user_author;
717
				$line->import_key = $obj->import_key;
718
				$line->code_journal = $obj->code_journal;
719
				$line->journal_label = $obj->journal_label;
720
				$line->piece_num = $obj->piece_num;
721
722
				$this->lines[] = $line;
723
			}
724
			$this->db->free($resql);
725
726
			return $num;
727
		} else {
728
			$this->errors[] = 'Error ' . $this->db->lasterror();
729
			dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
730
731
			return - 1;
732
		}
733
	}
734
735
	/**
736
	 * Load object in memory from the database
737
	 *
738
	 * @param string $sortorder Sort Order
739
	 * @param string $sortfield Sort field
740
	 * @param int $limit offset limit
741
	 * @param int $offset offset limit
742
	 * @param array $filter filter array
743
	 * @param string $filtermode filter mode (AND or OR)
744
	 *
745
	 * @return int <0 if KO, >0 if OK
746
	 */
747
	public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND') {
748
		global $conf;
749
750
		dol_syslog(__METHOD__, LOG_DEBUG);
751
752
		$sql = 'SELECT';
753
		$sql .= ' t.rowid,';
754
		$sql .= " t.doc_date,";
755
		$sql .= " t.doc_type,";
756
		$sql .= " t.doc_ref,";
757
		$sql .= " t.fk_doc,";
758
		$sql .= " t.fk_docdet,";
759
		$sql .= " t.thirdparty_code,";
760
		$sql .= " t.subledger_account,";
761
		$sql .= " t.subledger_label,";
762
		$sql .= " t.numero_compte,";
763
		$sql .= " t.label_compte,";
764
		$sql .= " t.label_operation,";
765
		$sql .= " t.debit,";
766
		$sql .= " t.credit,";
767
		$sql .= " t.montant,";
768
		$sql .= " t.sens,";
769
		$sql .= " t.fk_user_author,";
770
		$sql .= " t.import_key,";
771
		$sql .= " t.code_journal,";
772
		$sql .= " t.journal_label,";
773
		$sql .= " t.piece_num";
774
		$sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
775
		// Manage filter
776
		$sqlwhere = array ();
777
		if (count($filter) > 0) {
778
			foreach ( $filter as $key => $value ) {
779
				if ($key == 't.doc_date') {
780
					$sqlwhere[] = $key . '=\'' . $this->db->idate($value) . '\'';
781
				} elseif ($key == 't.doc_date>=' || $key == 't.doc_date<=') {
782
					$sqlwhere[] = $key . '\'' . $this->db->idate($value) . '\'';
783
				} elseif ($key == 't.numero_compte>=' || $key == 't.numero_compte<=' || $key == 't.subledger_account>=' || $key == 't.subledger_account<=') {
784
					$sqlwhere[] = $key . '\'' . $this->db->escape($value) . '\'';
785
				} elseif ($key == 't.fk_doc' || $key == 't.fk_docdet' || $key == 't.piece_num') {
786
					$sqlwhere[] = $key . '=' . $value;
787
				} elseif ($key == 't.subledger_account' || $key == 't.numero_compte') {
788
					$sqlwhere[] = $key . ' LIKE \'' . $this->db->escape($value) . '%\'';
789
				} else {
790
					$sqlwhere[] = $key . ' LIKE \'%' . $this->db->escape($value) . '%\'';
791
				}
792
			}
793
		}
794
		$sql.= ' WHERE 1 = 1';
795
		$sql .= " AND entity IN (" . getEntity('accountancy') . ")";
796
		if (count($sqlwhere) > 0) {
797
			$sql .= ' AND ' . implode(' ' . $filtermode . ' ', $sqlwhere);
798
		}
799
800
		if (! empty($sortfield)) {
801
			$sql .= $this->db->order($sortfield, $sortorder);
802
		}
803
		if (! empty($limit)) {
804
			$sql .= ' ' . $this->db->plimit($limit + 1, $offset);
805
		}
806
		$this->lines = array ();
807
808
		$resql = $this->db->query($sql);
809
		if ($resql) {
810
			$num = $this->db->num_rows($resql);
811
812
			while ( $obj = $this->db->fetch_object($resql) ) {
813
				$line = new BookKeepingLine();
814
815
				$line->id = $obj->rowid;
816
817
				$line->doc_date = $this->db->jdate($obj->doc_date);
818
				$line->doc_type = $obj->doc_type;
819
				$line->doc_ref = $obj->doc_ref;
820
				$line->fk_doc = $obj->fk_doc;
821
				$line->fk_docdet = $obj->fk_docdet;
822
				$line->thirdparty_code = $obj->thirdparty_code;
823
				$line->subledger_account = $obj->subledger_account;
824
				$line->subledger_label = $obj->subledger_label;
825
				$line->numero_compte = $obj->numero_compte;
826
				$line->label_compte = $obj->label_compte;
827
				$line->label_operation = $obj->label_operation;
828
				$line->debit = $obj->debit;
829
				$line->credit = $obj->credit;
830
				$line->montant = $obj->montant;
831
				$line->sens = $obj->sens;
832
				$line->fk_user_author = $obj->fk_user_author;
833
				$line->import_key = $obj->import_key;
834
				$line->code_journal = $obj->code_journal;
835
				$line->journal_label = $obj->journal_label;
836
				$line->piece_num = $obj->piece_num;
837
838
				$this->lines[] = $line;
839
			}
840
			$this->db->free($resql);
841
842
			return $num;
843
		} else {
844
			$this->errors[] = 'Error ' . $this->db->lasterror();
845
			dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
846
847
			return - 1;
848
		}
849
	}
850
851
	/**
852
	 * Load object in memory from the database
853
	 *
854
	 * @param string $sortorder Sort Order
855
	 * @param string $sortfield Sort field
856
	 * @param int $limit offset limit
857
	 * @param int $offset offset limit
858
	 * @param array $filter filter array
859
	 * @param string $filtermode filter mode (AND or OR)
860
	 *
861
	 * @return int <0 if KO, >0 if OK
862
	 */
863
	public function fetchAllBalance($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND') {
864
		global $conf;
865
866
		dol_syslog(__METHOD__, LOG_DEBUG);
867
868
		$sql = 'SELECT';
869
		$sql .= " t.numero_compte,";
870
		$sql .= " SUM(t.debit) as debit,";
871
		$sql .= " SUM(t.credit) as credit";
872
		$sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
873
		// Manage filter
874
		$sqlwhere = array ();
875
		if (count($filter) > 0) {
876
			foreach ( $filter as $key => $value ) {
877
				if ($key == 't.doc_date') {
878
					$sqlwhere[] = $key . '=\'' . $this->db->idate($value) . '\'';
879
				} elseif ($key == 't.doc_date>=' || $key == 't.doc_date<=') {
880
					$sqlwhere[] = $key . '\'' . $this->db->idate($value) . '\'';
881
				} elseif ($key == 't.numero_compte>=' || $key == 't.numero_compte<=' || $key == 't.subledger_account>=' || $key == 't.subledger_account<=') {
882
					$sqlwhere[] = $key . '\'' . $this->db->escape($value) . '\'';
883
				} elseif ($key == 't.fk_doc' || $key == 't.fk_docdet' || $key == 't.piece_num') {
884
					$sqlwhere[] = $key . '=' . $value;
885
				} elseif ($key == 't.subledger_account' || $key == 't.numero_compte') {
886
					$sqlwhere[] = $key . ' LIKE \'' . $this->db->escape($value) . '%\'';
887
				} elseif ($key == 't.subledger_label') {
888
					$sqlwhere[] = $key . ' LIKE \'' . $this->db->escape($value) . '%\'';
889
				} else {
890
					$sqlwhere[] = $key . ' LIKE \'%' . $this->db->escape($value) . '%\'';
891
				}
892
			}
893
		}
894
		$sql.= ' WHERE 1 = 1';
895
		$sql .= " AND entity IN (" . getEntity('accountancy') . ")";
896
		if (count($sqlwhere) > 0) {
897
			$sql .= ' AND ' . implode(' ' . $filtermode . ' ', $sqlwhere);
898
		}
899
900
		$sql .= ' GROUP BY t.numero_compte';
901
902
		if (! empty($sortfield)) {
903
			$sql .= $this->db->order($sortfield, $sortorder);
904
		}
905
		if (! empty($limit)) {
906
			$sql .= ' ' . $this->db->plimit($limit + 1, $offset);
907
		}
908
		$this->lines = array ();
909
910
		$resql = $this->db->query($sql);
911
		if ($resql) {
912
			$num = $this->db->num_rows($resql);
913
914
			while ( $obj = $this->db->fetch_object($resql) ) {
915
				$line = new BookKeepingLine();
916
917
				$line->numero_compte = $obj->numero_compte;
918
				$line->debit = $obj->debit;
919
				$line->credit = $obj->credit;
920
				$this->lines[] = $line;
921
			}
922
			$this->db->free($resql);
923
924
			return $num;
925
		} else {
926
			$this->errors[] = 'Error ' . $this->db->lasterror();
927
			dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
928
929
			return - 1;
930
		}
931
	}
932
933
	/**
934
	 * Update object into database
935
	 *
936
	 * @param  User    $user       User that modifies
937
	 * @param  bool    $notrigger  false=launch triggers after, true=disable triggers
938
	 * @param  string  $mode       Mode
939
	 * @return int                 <0 if KO, >0 if OK
940
	 */
941
	public function update(User $user, $notrigger = false, $mode='') {
942
		$error = 0;
943
944
		dol_syslog(__METHOD__, LOG_DEBUG);
945
946
		// Clean parameters
947
		if (isset($this->doc_type)) {
948
			$this->doc_type = trim($this->doc_type);
949
		}
950
		if (isset($this->doc_ref)) {
951
			$this->doc_ref = trim($this->doc_ref);
952
		}
953
		if (isset($this->fk_doc)) {
954
			$this->fk_doc = trim($this->fk_doc);
955
		}
956
		if (isset($this->fk_docdet)) {
957
			$this->fk_docdet = trim($this->fk_docdet);
958
		}
959
		if (isset($this->thirdparty_code)) {
960
			$this->thirdparty_code = trim($this->thirdparty_code);
961
		}
962
		if (isset($this->subledger_account)) {
963
			$this->subledger_account = trim($this->subledger_account);
964
		}
965
		if (isset($this->subledger_label)) {
966
			$this->subledger_label = trim($this->subledger_label);
967
		}
968
		if (isset($this->numero_compte)) {
969
			$this->numero_compte = trim($this->numero_compte);
970
		}
971
		if (isset($this->label_compte)) {
972
			$this->label_compte = trim($this->label_compte);
973
		}
974
		if (isset($this->label_operation)) {
975
			$this->label_operation = trim($this->label_operation);
976
		}
977
		if (isset($this->debit)) {
978
			$this->debit = trim($this->debit);
979
		}
980
		if (isset($this->credit)) {
981
			$this->credit = trim($this->credit);
982
		}
983
		if (isset($this->montant)) {
984
			$this->montant = trim($this->montant);
985
		}
986
		if (isset($this->sens)) {
987
			$this->sens = trim($this->sens);
988
		}
989
		if (isset($this->fk_user_author)) {
990
			$this->fk_user_author = trim($this->fk_user_author);
991
		}
992
		if (isset($this->import_key)) {
993
			$this->import_key = trim($this->import_key);
994
		}
995
		if (isset($this->code_journal)) {
996
			$this->code_journal = trim($this->code_journal);
997
		}
998
		if (isset($this->journal_label)) {
999
			$this->journal_label = trim($this->journal_label);
1000
		}
1001
		if (isset($this->piece_num)) {
1002
			$this->piece_num = trim($this->piece_num);
1003
		}
1004
1005
		$this->debit = price2num($this->debit, 'MT');
1006
		$this->credit = price2num($this->credit, 'MT');
1007
1008
		// Check parameters
1009
		// Put here code to add a control on parameters values
1010
1011
		// Update request
1012
		$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . $mode.' SET';
1013
		$sql .= ' doc_date = ' . (! isset($this->doc_date) || dol_strlen($this->doc_date) != 0 ? "'" . $this->db->idate($this->doc_date) . "'" : 'null') . ',';
1014
		$sql .= ' doc_type = ' . (isset($this->doc_type) ? "'" . $this->db->escape($this->doc_type) . "'" : "null") . ',';
1015
		$sql .= ' doc_ref = ' . (isset($this->doc_ref) ? "'" . $this->db->escape($this->doc_ref) . "'" : "null") . ',';
1016
		$sql .= ' fk_doc = ' . (isset($this->fk_doc) ? $this->fk_doc : "null") . ',';
1017
		$sql .= ' fk_docdet = ' . (isset($this->fk_docdet) ? $this->fk_docdet : "null") . ',';
1018
		$sql .= ' thirdparty_code = ' . (isset($this->thirdparty_code) ? "'" . $this->db->escape($this->thirdparty_code) . "'" : "null") . ',';
1019
		$sql .= ' subledger_account = ' . (isset($this->subledger_account) ? "'" . $this->db->escape($this->subledger_account) . "'" : "null") . ',';
1020
		$sql .= ' subledger_label = ' . (isset($this->subledger_label) ? "'" . $this->db->escape($this->subledger_label) . "'" : "null") . ',';
1021
		$sql .= ' numero_compte = ' . (isset($this->numero_compte) ? "'" . $this->db->escape($this->numero_compte) . "'" : "null") . ',';
1022
		$sql .= ' label_compte = ' . (isset($this->label_compte) ? "'" . $this->db->escape($this->label_compte) . "'" : "null") . ',';
1023
		$sql .= ' label_operation = ' . (isset($this->label_operation) ? "'" . $this->db->escape($this->label_operation) . "'" : "null") . ',';
1024
		$sql .= ' debit = ' . (isset($this->debit) ? $this->debit : "null") . ',';
1025
		$sql .= ' credit = ' . (isset($this->credit) ? $this->credit : "null") . ',';
1026
		$sql .= ' montant = ' . (isset($this->montant) ? $this->montant : "null") . ',';
1027
		$sql .= ' sens = ' . (isset($this->sens) ? "'" . $this->db->escape($this->sens) . "'" : "null") . ',';
1028
		$sql .= ' fk_user_author = ' . (isset($this->fk_user_author) ? $this->fk_user_author : "null") . ',';
1029
		$sql .= ' import_key = ' . (isset($this->import_key) ? "'" . $this->db->escape($this->import_key) . "'" : "null") . ',';
1030
		$sql .= ' code_journal = ' . (isset($this->code_journal) ? "'" . $this->db->escape($this->code_journal) . "'" : "null") . ',';
1031
		$sql .= ' journal_label = ' . (isset($this->journal_label) ? "'" . $this->db->escape($this->journal_label) . "'" : "null") . ',';
1032
		$sql .= ' piece_num = ' . (isset($this->piece_num) ? $this->piece_num : "null");
1033
		$sql .= ' WHERE rowid=' . $this->id;
1034
1035
		$this->db->begin();
1036
1037
		$resql = $this->db->query($sql);
1038
		if (! $resql) {
1039
			$error ++;
1040
			$this->errors[] = 'Error ' . $this->db->lasterror();
1041
			dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
1042
		}
1043
1044
		if (! $error && ! $notrigger) {
1045
			// Uncomment this and change MYOBJECT to your own tag if you
1046
			// want this action calls a trigger.
1047
1048
			// // Call triggers
1049
			// $result=$this->call_trigger('MYOBJECT_MODIFY',$user);
1050
			// if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
1051
			// // End call triggers
1052
		}
1053
1054
		// Commit or rollback
1055
		if ($error) {
1056
			$this->db->rollback();
1057
1058
			return - 1 * $error;
1059
		} else {
1060
			$this->db->commit();
1061
1062
			return 1;
1063
		}
1064
	}
1065
1066
	/**
1067
	 * Update movement
1068
	 *
1069
	 * @param  string  $piece_num      Piece num
1070
	 * @param  string  $field          Field
1071
	 * @param  string  $value          Value
1072
	 * @param  string  $mode           Mode
1073
	 * @return number                  <0 if KO, >0 if OK
1074
	 */
1075
	public function updateByMvt($piece_num='', $field='', $value='', $mode='') {
1076
		$this->db->begin();
1077
		$sql = "UPDATE " . MAIN_DB_PREFIX .  $this->table_element . $mode . " as ab";
1078
		$sql .= ' SET ab.' . $field . '=' . (is_numeric($value)?$value:"'".$value."'");
1079
		$sql .= ' WHERE ab.piece_num=' . $piece_num ;
1080
		$resql = $this->db->query($sql);
1081
1082
		if (! $resql) {
1083
			$error ++;
0 ignored issues
show
Bug introduced by
The variable $error does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
1084
			$this->errors[] = 'Error ' . $this->db->lasterror();
1085
			dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
1086
		}
1087
		if ($error) {
1088
			$this->db->rollback();
1089
1090
			return - 1 * $error;
1091
		} else {
1092
			$this->db->commit();
1093
1094
			return 1;
1095
		}
1096
	}
1097
1098
	/**
1099
	 * Delete object in database
1100
	 *
1101
	 * @param User $user User that deletes
1102
	 * @param bool $notrigger false=launch triggers after, true=disable triggers
1103
	 * @param string $mode Mode
1104
	 * @return int <0 if KO, >0 if OK
1105
	 */
1106
	public function delete(User $user, $notrigger = false, $mode='') {
1107
		dol_syslog(__METHOD__, LOG_DEBUG);
1108
1109
		$error = 0;
1110
1111
		$this->db->begin();
1112
1113
		if (! $error) {
1114
			if (! $notrigger) {
1115
				// Uncomment this and change MYOBJECT to your own tag if you
1116
				// want this action calls a trigger.
1117
1118
				// // Call triggers
1119
				// $result=$this->call_trigger('MYOBJECT_DELETE',$user);
1120
				// if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
1121
				// // End call triggers
1122
			}
1123
		}
1124
1125
		if (! $error) {
1126
			$sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this->table_element.$mode;
1127
			$sql .= ' WHERE rowid=' . $this->id;
1128
1129
			$resql = $this->db->query($sql);
1130
			if (! $resql) {
1131
				$error ++;
1132
				$this->errors[] = 'Error ' . $this->db->lasterror();
1133
				dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
1134
			}
1135
		}
1136
1137
		// Commit or rollback
1138
		if ($error) {
1139
			$this->db->rollback();
1140
1141
			return - 1 * $error;
1142
		} else {
1143
			$this->db->commit();
1144
1145
			return 1;
1146
		}
1147
	}
1148
1149
	/**
1150
	 * Delete bookkepping by importkey
1151
	 *
1152
	 * @param  string		$importkey		Import key
1153
	 * @return int Result
1154
	 */
1155
	function deleteByImportkey($importkey) {
1156
		$this->db->begin();
1157
1158
		// first check if line not yet in bookkeeping
1159
		$sql = "DELETE";
1160
		$sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element;
1161
		$sql .= " WHERE import_key = '" . $importkey . "'";
1162
1163
		$resql = $this->db->query($sql);
1164
1165
		if (! $resql) {
1166
			$this->errors[] = "Error " . $this->db->lasterror();
1167
			dol_syslog(get_class($this)."::delete Error " . $this->db->lasterror(), LOG_ERR);
1168
			$this->db->rollback();
1169
			return - 1;
1170
		}
1171
1172
		$this->db->commit();
1173
		return 1;
1174
	}
1175
1176
	/**
1177
	 * Delete bookkepping by year
1178
	 *
1179
	 * @param  string $delyear		Year to delete
1180
	 * @param  string $journal		Journal to delete
1181
	 * @param  string $mode 		Mode
1182
	 * @return int					<0 if KO, >0 if OK
1183
	 */
1184
	function deleteByYearAndJournal($delyear='', $journal='', $mode='') {
1185
		global $conf;
1186
1187
		if (empty($delyear) && empty($journal))
1188
		{
1189
			return -1;
1190
		}
1191
1192
		$this->db->begin();
1193
1194
		// first check if line not yet in bookkeeping
1195
		$sql = "DELETE";
1196
		$sql.= " FROM " . MAIN_DB_PREFIX . $this->table_element.$mode;
1197
		$sql.= " WHERE 1 = 1";
1198
		if (! empty($delyear)) $sql.= " AND YEAR(doc_date) = " . $delyear;		 // FIXME Must use between
1199
		if (! empty($journal)) $sql.= " AND code_journal = '".$journal."'";
1200
		$sql .= " AND entity IN (" . getEntity('accountancy') . ")";
1201
		$resql = $this->db->query($sql);
1202
1203
		if (! $resql) {
1204
			$this->errors[] = "Error " . $this->db->lasterror();
1205
			foreach ( $this->errors as $errmsg ) {
1206
				dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR);
1207
				$this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
1208
			}
1209
			$this->db->rollback();
1210
			return -1;
1211
		}
1212
1213
		$this->db->commit();
1214
		return 1;
1215
	}
1216
1217
	/**
1218
	 * Delete bookkepping by piece number
1219
	 *
1220
	 * @param int $piecenum peicenum to delete
1221
	 * @param string $mode Mode
1222
	 * @return int Result
1223
	 */
1224
	function deleteMvtNum($piecenum, $mode) {
1225
		global $conf;
1226
1227
		$this->db->begin();
1228
1229
		// first check if line not yet in bookkeeping
1230
		$sql = "DELETE";
1231
		$sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element. $mode;
1232
		$sql .= " WHERE piece_num = " . $piecenum;
1233
		$sql .= " AND entity IN (" . getEntity('accountancy') . ")";
1234
1235
		$resql = $this->db->query($sql);
1236
1237
		if (! $resql) {
1238
			$this->errors[] = "Error " . $this->db->lasterror();
1239
			foreach ( $this->errors as $errmsg ) {
1240
				dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR);
1241
				$this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
1242
			}
1243
			$this->db->rollback();
1244
			return - 1;
1245
		}
1246
1247
		$this->db->commit();
1248
		return 1;
1249
	}
1250
1251
	/**
1252
	 * Load an object from its id and create a new one in database
1253
	 *
1254
	 * @param int $fromid Id of object to clone
1255
	 *
1256
	 * @return int New id of clone
1257
	 */
1258
	public function createFromClone($fromid) {
1259
		dol_syslog(__METHOD__, LOG_DEBUG);
1260
1261
		global $user;
1262
		$error = 0;
1263
		$object = new Accountingbookkeeping($this->db);
1264
1265
		$this->db->begin();
1266
1267
		// Load source object
1268
		$object->fetch($fromid);
1269
		// Reset object
1270
		$object->id = 0;
1271
1272
		// Clear fields
1273
		// ...
1274
1275
		// Create clone
1276
		$result = $object->create($user);
1277
1278
		// Other options
1279
		if ($result < 0) {
1280
			$error ++;
1281
			$this->errors = $object->errors;
1282
			dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
1283
		}
1284
1285
		// End
1286
		if (! $error) {
1287
			$this->db->commit();
1288
1289
			return $object->id;
1290
		} else {
1291
			$this->db->rollback();
1292
1293
			return - 1;
1294
		}
1295
	}
1296
1297
	/**
1298
	 * Initialise object with example values
1299
	 * Id must be 0 if object instance is a specimen
1300
	 *
1301
	 * @return void
1302
	 */
1303
	public function initAsSpecimen() {
1304
		global $user;
1305
1306
		$now=dol_now();
1307
1308
		$this->id = 0;
1309
		$this->doc_date = $now;
1310
		$this->doc_type = '';
1311
		$this->doc_ref = '';
1312
		$this->fk_doc = '';
1313
		$this->fk_docdet = '';
1314
		$this->thirdparty_code = 'CU001';
1315
		$this->subledger_account = '41100001';
1316
		$this->subledger_label = 'My customer company';
1317
		$this->numero_compte = '411';
1318
		$this->label_compte = 'Customer';
1319
		$this->label_operation = 'Sales of pea';
1320
		$this->debit = 99.9;
1321
		$this->credit = '';
1322
		$this->montant = '';
1323
		$this->sens = 'D';
1324
		$this->fk_user_author = $user->id;
1325
		$this->import_key = '';
1326
		$this->code_journal = 'VT';
1327
		$this->journal_label = 'Journal de vente';
1328
		$this->piece_num = '';
1329
	}
1330
1331
	/**
1332
	 * Load an accounting document into memory from database
1333
	 *
1334
	 * @param int $piecenum Accounting document to get
1335
	 * @param string $mode Mode
1336
	 * @return int <0 if KO, >0 if OK
1337
	 */
1338
	public function fetchPerMvt($piecenum, $mode='') {
1339
		global $conf;
1340
1341
		$sql = "SELECT piece_num,doc_date,code_journal,journal_label,doc_ref,doc_type";
1342
		$sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element.$mode;
1343
		$sql .= " WHERE piece_num = " . $piecenum;
1344
		$sql .= " AND entity IN (" . getEntity('accountancy') . ")";
1345
1346
		dol_syslog(get_class($this) . "::" . __METHOD__, LOG_DEBUG);
1347
		$result = $this->db->query($sql);
1348
		if ($result) {
1349
			$obj = $this->db->fetch_object($result);
1350
1351
			$this->piece_num = $obj->piece_num;
1352
			$this->code_journal = $obj->code_journal;
1353
			$this->journal_label = $obj->journal_label;
1354
			$this->doc_date = $this->db->jdate($obj->doc_date);
1355
			$this->doc_ref = $obj->doc_ref;
1356
			$this->doc_type = $obj->doc_type;
1357
		} else {
1358
			$this->error = "Error " . $this->db->lasterror();
1359
			dol_syslog(get_class($this) . "::" . __METHOD__ . $this->error, LOG_ERR);
1360
			return - 1;
1361
		}
1362
1363
		return 1;
1364
	}
1365
1366
	/**
1367
	 * Return next number movement
1368
	 *
1369
	 * @param	string	$mode	Mode
1370
	 * @return	string			Next numero to use
1371
	 */
1372
	public function getNextNumMvt($mode='')
1373
	{
1374
		global $conf;
1375
1376
		$sql = "SELECT MAX(piece_num)+1 as max FROM " . MAIN_DB_PREFIX . $this->table_element.$mode;
1377
		$sql .= " WHERE entity IN (" . getEntity('accountancy') . ")";
1378
1379
		dol_syslog(get_class($this) . "getNextNumMvt sql=" . $sql, LOG_DEBUG);
1380
		$result = $this->db->query($sql);
1381
1382
		if ($result) {
1383
			$obj = $this->db->fetch_object($result);
1384
			if ($obj) $result = $obj->max;
1385
			if (empty($result)) $result = 1;
1386
			return $result;
1387
		} else {
1388
			$this->error = "Error " . $this->db->lasterror();
1389
			dol_syslog(get_class($this) . "::getNextNumMvt " . $this->error, LOG_ERR);
1390
			return - 1;
1391
		}
1392
	}
1393
1394
	/**
1395
	 * Load all informations of accountancy document
1396
	 *
1397
	 * @param  int     $piecenum   Id of line to get
1398
	 * @param  string  $mode       Mode
1399
	 * @return int                 <0 if KO, >0 if OK
1400
	 */
1401
	function fetchAllPerMvt($piecenum, $mode='') {
1402
		global $conf;
1403
1404
		$sql = "SELECT rowid, doc_date, doc_type,";
1405
		$sql .= " doc_ref, fk_doc, fk_docdet, thirdparty_code, subledger_account, subledger_label,";
1406
		$sql .= " numero_compte, label_compte, label_operation, debit, credit,";
1407
		$sql .= " montant, sens, fk_user_author, import_key, code_journal, journal_label, piece_num";
1408
		$sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element.$mode;
1409
		$sql .= " WHERE piece_num = " . $piecenum;
1410
		$sql .= " AND entity IN (" . getEntity('accountancy') . ")";
1411
1412
		dol_syslog(get_class($this) . "::" . __METHOD__, LOG_DEBUG);
1413
		$result = $this->db->query($sql);
1414
		if ($result) {
1415
1416
			while ( $obj = $this->db->fetch_object($result) ) {
1417
1418
				$line = new BookKeepingLine();
1419
1420
				$line->id = $obj->rowid;
1421
1422
				$line->doc_date = $this->db->jdate($obj->doc_date);
1423
				$line->doc_type = $obj->doc_type;
1424
				$line->doc_ref = $obj->doc_ref;
1425
				$line->fk_doc = $obj->fk_doc;
1426
				$line->fk_docdet = $obj->fk_docdet;
1427
				$line->thirdparty_code = $obj->thirdparty_code;
1428
				$line->subledger_account = $obj->subledger_account;
1429
				$line->subledger_label = $obj->subledger_label;
1430
				$line->numero_compte = $obj->numero_compte;
1431
				$line->label_compte = $obj->label_compte;
1432
				$line->label_operation = $obj->label_operation;
1433
				$line->debit = $obj->debit;
1434
				$line->credit = $obj->credit;
1435
				$line->montant = $obj->montant;
1436
				$line->sens = $obj->sens;
1437
				$line->code_journal = $obj->code_journal;
1438
				$line->journal_label = $obj->journal_label;
1439
				$line->piece_num = $obj->piece_num;
1440
1441
				$this->linesmvt[] = $line;
1442
			}
1443
		} else {
1444
			$this->error = "Error " . $this->db->lasterror();
1445
			dol_syslog(get_class($this) . "::" . __METHOD__ . $this->error, LOG_ERR);
1446
			return - 1;
1447
		}
1448
1449
		return 1;
1450
	}
1451
1452
	/**
1453
	 * Export bookkeping
1454
	 *
1455
	 * @param	string	$model	Model
1456
	 * @return	int				Result
1457
	 */
1458
	function export_bookkeping($model = 'ebp') {
1459
		global $conf;
1460
1461
		$sql = "SELECT rowid, doc_date, doc_type,";
1462
		$sql .= " doc_ref, fk_doc, fk_docdet, thirdparty_code, subledger_account, subledger_label,";
1463
		$sql .= " numero_compte, label_compte, label_operation, debit, credit,";
1464
		$sql .= " montant, sens, fk_user_author, import_key, code_journal, piece_num";
1465
		$sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element;
1466
		$sql .= " WHERE entity IN (" . getEntity('accountancy') . ")";
1467
1468
		dol_syslog(get_class($this) . "::export_bookkeping", LOG_DEBUG);
1469
1470
		$resql = $this->db->query($sql);
1471
1472
		if ($resql) {
1473
			$this->linesexport = array ();
1474
1475
			$num = $this->db->num_rows($resql);
1476
			while ( $obj = $this->db->fetch_object($resql) ) {
1477
				$line = new BookKeepingLine();
1478
1479
				$line->id = $obj->rowid;
1480
1481
				$line->doc_date = $this->db->jdate($obj->doc_date);
1482
				$line->doc_type = $obj->doc_type;
1483
				$line->doc_ref = $obj->doc_ref;
1484
				$line->fk_doc = $obj->fk_doc;
1485
				$line->fk_docdet = $obj->fk_docdet;
1486
				$line->thirdparty_code = $obj->thirdparty_code;
1487
				$line->subledger_account = $obj->subledger_account;
1488
				$line->subledger_label = $obj->subledger_label;
1489
				$line->numero_compte = $obj->numero_compte;
1490
				$line->label_compte = $obj->label_compte;
1491
				$line->label_operation = $obj->label_operation;
1492
				$line->debit = $obj->debit;
1493
				$line->credit = $obj->credit;
1494
				$line->montant = $obj->montant;
1495
				$line->sens = $obj->sens;
1496
				$line->code_journal = $obj->code_journal;
1497
				$line->piece_num = $obj->piece_num;
1498
1499
				$this->linesexport[] = $line;
1500
			}
1501
			$this->db->free($resql);
1502
1503
			return $num;
1504
		} else {
1505
			$this->error = "Error " . $this->db->lasterror();
1506
			dol_syslog(get_class($this) . "::export_bookkeping " . $this->error, LOG_ERR);
1507
			return - 1;
1508
		}
1509
	}
1510
1511
	/**
1512
	 * Transform transaction
1513
	 *
1514
	 * @param  number   $direction     If 0 tmp => real, if 1 real => tmp
1515
	 * @param  string   $piece_num     Piece num
1516
	 * @return void
1517
	 */
1518
	public function transformTransaction($direction=0,$piece_num='') {
1519
		$this->db->begin();
1520
		if ($direction==0) {
1521
			$next_piecenum=$this->getNextNumMvt();
1522
			if ($result < 0) {
0 ignored issues
show
Bug introduced by
The variable $result does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
1523
				$error++;
0 ignored issues
show
Bug introduced by
The variable $error does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
1524
			}
1525
			$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this->table_element.'(doc_date, doc_type,';
1526
			$sql .= ' doc_ref, fk_doc, fk_docdet, thirdparty_code, subledger_account, subledger_label,';
1527
			$sql .= ' numero_compte, label_compte, label_operation, debit, credit,';
1528
			$sql .= ' montant, sens, fk_user_author, import_key, code_journal, journal_label, piece_num)';
1529
			$sql .= 'SELECT doc_date, doc_type,';
1530
			$sql .= ' doc_ref, fk_doc, fk_docdet, thirdparty_code, subledger_account, subledger_label,';
1531
			$sql .= ' numero_compte, label_compte, label_operation, debit, credit,';
1532
			$sql .= ' montant, sens, fk_user_author, import_key, code_journal, journal_label, '.$next_piecenum.'';
1533
			$sql .= ' FROM '.MAIN_DB_PREFIX . $this->table_element.'_tmp WHERE piece_num = '.$piece_num;
1534
			$resql = $this->db->query($sql);
1535
			if (! $resql) {
1536
				$error ++;
1537
				$this->errors[] = 'Error ' . $this->db->lasterror();
1538
				dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
1539
			}
1540
			$sql = 'DELETE FROM '.MAIN_DB_PREFIX . $this->table_element.'_tmp WHERE piece_num = '.$piece_num;
1541
			$resql = $this->db->query($sql);
1542
			if (! $resql) {
1543
				$error ++;
1544
				$this->errors[] = 'Error ' . $this->db->lasterror();
1545
				dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
1546
			}
1547
		}
1548
		if ($direction==1) {
1549
			$sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this->table_element.'_tmp WHERE piece_num = '.$piece_num;
1550
			$resql = $this->db->query($sql);
1551
			if (! $resql) {
1552
				$error ++;
1553
				$this->errors[] = 'Error ' . $this->db->lasterror();
1554
				dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
1555
			}
1556
			$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this->table_element.'_tmp(doc_date, doc_type,';
1557
			$sql .= ' doc_ref, fk_doc, fk_docdet, thirdparty_code, subledger_account, subledger_label,';
1558
			$sql .= ' numero_compte, label_compte, label_operation, debit, credit,';
1559
			$sql .= ' montant, sens, fk_user_author, import_key, code_journal, journal_label, piece_num)';
1560
			$sql .= 'SELECT doc_date, doc_type,';
1561
			$sql .= ' doc_ref, fk_doc, fk_docdet, thirdparty_code, subledger_account, subledger_label,';
1562
			$sql .= ' numero_compte, label_compte, label_operation, debit, credit,';
1563
			$sql .= ' montant, sens, fk_user_author, import_key, code_journal, journal_label, piece_num';
1564
			$sql .= ' FROM '.MAIN_DB_PREFIX . $this->table_element.' WHERE piece_num = '.$piece_num;
1565
			$resql = $this->db->query($sql);
1566
			if (! $resql) {
1567
				$error ++;
1568
				$this->errors[] = 'Error ' . $this->db->lasterror();
1569
				dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
1570
			}
1571
			$sql = 'DELETE FROM '.MAIN_DB_PREFIX . $this->table_element.'_tmp WHERE piece_num = '.$piece_num;
1572
			$resql = $this->db->query($sql);
1573
			if (! $resql) {
1574
				$error ++;
1575
				$this->errors[] = 'Error ' . $this->db->lasterror();
1576
				dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
1577
			}
1578
		}
1579
		if (! $error) {
1580
			$this->db->commit();
1581
			return 1;
1582
		} else {
1583
			$this->db->rollback();
1584
			return - 1;
1585
		}
1586
		/*
1587
		$sql = "DELETE FROM ";
1588
		$sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as ab";
1589
		$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON aa.account_number = ab.numero_compte";
1590
		$sql .= " AND aa.active = 1";
1591
		$sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
1592
		$sql .= " AND asy.rowid = " . $pcgver;
1593
		$sql .= " AND ab.entity IN (" . getEntity('accountancy') . ")";
1594
		$sql .= " ORDER BY account_number ASC";
1595
		*/
1596
	}
1597
1598
	/**
1599
	* Return list of accounts with label by chart of accounts
1600
	*
1601
	* @param string		$selectid	Preselected chart of accounts
1602
	* @param string		$htmlname	Name of field in html form
1603
	* @param int		$showempty	Add an empty field
1604
	* @param array		$event		Event options
1605
	* @param int		$select_in	Value is a aa.rowid (0 default) or aa.account_number (1)
1606
	* @param int		$select_out	Set value returned by select 0=rowid (default), 1=account_number
1607
	* @param int		$aabase		Set accounting_account base class to display empty=all or from 1 to 8 will display only account beginning by this number
1608
	* @return string	String with HTML select
1609
	*/
1610
	function select_account($selectid, $htmlname = 'account', $showempty = 0, $event = array(), $select_in = 0, $select_out = 0, $aabase = '') {
1611
		global $conf;
1612
1613
		require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
1614
1615
		$pcgver = $conf->global->CHARTOFACCOUNTS;
1616
1617
		$sql = "SELECT DISTINCT ab.numero_compte as account_number, aa.label as label, aa.rowid as rowid, aa.fk_pcg_version";
1618
		$sql .= " FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as ab";
1619
		$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON aa.account_number = ab.numero_compte";
1620
		$sql .= " AND aa.active = 1";
1621
		$sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
1622
		$sql .= " AND asy.rowid = " . $pcgver;
1623
		$sql .= " AND ab.entity IN (" . getEntity('accountancy') . ")";
1624
		$sql .= " ORDER BY account_number ASC";
1625
1626
		dol_syslog(get_class($this) . "::select_account", LOG_DEBUG);
1627
		$resql = $this->db->query($sql);
1628
1629
		if (! $resql) {
1630
			$this->error = "Error " . $this->db->lasterror();
1631
			dol_syslog(get_class($this) . "::select_account " . $this->error, LOG_ERR);
1632
			return - 1;
1633
		}
1634
1635
		$out = ajax_combobox($htmlname, $event);
1636
1637
		$options = array();
1638
		$selected = null;
1639
1640
		while ($obj = $this->db->fetch_object($resql)) {
1641
			$label = length_accountg($obj->account_number) . ' - ' . $obj->label;
1642
1643
			$select_value_in = $obj->rowid;
1644
			$select_value_out = $obj->rowid;
1645
1646
			if ($select_in == 1) {
1647
				$select_value_in = $obj->account_number;
1648
			}
1649
			if ($select_out == 1) {
1650
				$select_value_out = $obj->account_number;
1651
			}
1652
1653
			// Remember guy's we store in database llx_facturedet the rowid of accounting_account and not the account_number
1654
			// Because same account_number can be share between different accounting_system and do have the same meaning
1655
			if (($selectid != '') && $selectid == $select_value_in) {
1656
				$selected = $select_value_out;
1657
			}
1658
1659
			$options[$select_value_out] = $label;
1660
		}
1661
1662
		$out .= Form::selectarray($htmlname, $options, $selected, $showempty, 0, 0, '', 0, 0, 0, '', 'maxwidth300');
1663
		$this->db->free($resql);
1664
		return $out;
1665
	}
1666
1667
	/**
1668
	 * Description of a root accounting account
1669
	 *
1670
	 * @param 	string 	$account	Accounting account
1671
	 * @return 	string 				Root account
1672
	 */
1673
	function get_compte_racine($account = null)
1674
	{
1675
		global $conf;
1676
		$pcgver = $conf->global->CHARTOFACCOUNTS;
1677
1678
		$sql  = "SELECT root.account_number, root.label as label";
1679
		$sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as aa";
1680
		$sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
1681
		$sql .= " AND asy.rowid = " . $pcgver;
1682
		$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as parent ON aa.account_parent = parent.rowid";
1683
		$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as root ON parent.account_parent = root.rowid";
1684
		$sql .= " WHERE aa.account_number = '" . $account . "'";
1685
		$sql .= " AND parent.active = 1";
1686
		$sql .= " AND root.active = 1";
1687
		$sql .= " AND aa.entity IN (" . getEntity('accountancy') . ")";
1688
1689
		dol_syslog(get_class($this) . "::select_account sql=" . $sql, LOG_DEBUG);
1690
		$resql = $this->db->query($sql);
1691
		if ($resql) {
1692
			$obj = '';
1693
			if ($this->db->num_rows($resql)) {
1694
				$obj = $this->db->fetch_object($resql);
1695
			}
1696
1697
			return $obj->label;
1698
1699
		} else {
1700
			$this->error = "Error " . $this->db->lasterror();
1701
			dol_syslog(__METHOD__ . " " . $this->error, LOG_ERR);
1702
1703
			return -1;
1704
		}
1705
	}
1706
1707
	/**
1708
	 * Description of accounting account
1709
	 *
1710
	 * @param	string	$account	Accounting account
1711
	 * @return	string				Account desc
1712
	 */
1713
	function get_compte_desc($account = null)
1714
	{
1715
		global $conf;
1716
1717
		$pcgver = $conf->global->CHARTOFACCOUNTS;
1718
		$sql  = "SELECT aa.account_number, aa.label, aa.rowid, aa.fk_pcg_version, cat.label as category";
1719
		$sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as aa ";
1720
		$sql .= " INNER JOIN " . MAIN_DB_PREFIX . "accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
1721
		$sql .= " AND aa.account_number = '" . $account . "'";
1722
		$sql .= " AND asy.rowid = " . $pcgver;
1723
		$sql .= " AND aa.active = 1";
1724
		$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_accounting_category as cat ON aa.fk_accounting_category = cat.rowid";
1725
		$sql .= " WHERE aa.entity IN (" . getEntity('accountancy') . ")";
1726
1727
		dol_syslog(get_class($this) . "::select_account sql=" . $sql, LOG_DEBUG);
1728
		$resql = $this->db->query($sql);
1729
		if ($resql) {
1730
			$obj = '';
1731
			if ($this->db->num_rows($resql)) {
1732
				$obj = $this->db->fetch_object($resql);
1733
			}
1734
			if(empty($obj->category)){
1735
				return $obj->label;
1736
			}else{
1737
				return $obj->label.' ('.$obj->category.')';
1738
			}
1739
		} else {
1740
			$this->error = "Error " . $this->db->lasterror();
1741
			dol_syslog(__METHOD__ . " " . $this->error, LOG_ERR);
1742
			return -1;
1743
		}
1744
	}
1745
}
1746
1747
/**
1748
 * Class BookKeepingLine
1749
 */
1750
class BookKeepingLine
1751
{
1752
	public $id;
1753
	public $doc_date = '';
1754
	public $doc_type;
1755
	public $doc_ref;
1756
	public $fk_doc;
1757
	public $fk_docdet;
1758
	public $thirdparty_code;
1759
	public $subledger_account;
1760
	public $subledger_label;
1761
	public $numero_compte;
1762
	public $label_compte;
1763
	public $label_operation;
1764
	public $debit;
1765
	public $credit;
1766
	public $montant;
1767
	public $sens;
1768
	public $fk_user_author;
1769
	public $import_key;
1770
	public $code_journal;
1771
	public $journal_label;
1772
	public $piece_num;
1773
}
1774