Completed
Branch develop (4e4045)
by
unknown
25:10
created

AccountancySystem   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 105
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
C fetch() 0 39 7
B create() 0 28 3
1
<?php
2
/* Copyright (C) 2013-2014 Olivier Geffroy       <[email protected]>
3
 * Copyright (C) 2013-2014 Alexandre Spangaro    <[email protected]>
4
 * Copyright (C) 2013-2014 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/accountancysystem.class.php
22
 * \ingroup		Advanced accountancy
23
 * \brief		File of class to manage accountancy systems
24
 */
25
26
/**
27
 * Class to manage accountancy systems
28
 */
29
class AccountancySystem
30
{
31
	var $db;
32
	var $error;
33
	var $rowid;
34
	var $fk_pcg_version;
35
	var $pcg_type;
36
	var $pcg_subtype;
37
	var $label;
38
	var $account_number;
39
	var $account_parent;
40
	
41
	/**
42
	 * Constructor
43
	 *
44
	 * @param DoliDB $db handler
45
	 */
46
	function __construct($db) {
47
		$this->db = $db;
48
	}
49
	
50
	
51
	/**
52
	 * Load record in memory
53
	 *
54
	 * @param 	int 	$rowid 				   Id
55
	 * @param 	string 	$ref             	   ref
56
	 * @return 	int                            <0 if KO, Id of record if OK and found
57
	 */
58
	function fetch($rowid = 0, $ref = '') 
59
	{
60
	    global $conf;
61
	
62
	    if ($rowid > 0 || $ref) 
63
	    {
64
	        $sql  = "SELECT a.pcg_version, a.label, a.active";
65
	        $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_system as a";
66
	        $sql .= " WHERE";
67
	        if ($rowid) {
68
	            $sql .= " a.rowid = '" . $rowid . "'";
69
	        } elseif ($ref) {
70
	            $sql .= " a.pcg_version = '" . $ref . "'";
71
	        }
72
	
73
	        dol_syslog(get_class($this) . "::fetch sql=" . $sql, LOG_DEBUG);
74
	        $result = $this->db->query($sql);
75
	        if ($result) {
76
	            $obj = $this->db->fetch_object($result);
77
	
78
	            if ($obj) {
79
	                $this->id = $obj->rowid;
80
	                $this->rowid = $obj->rowid;
81
	                $this->pcg_version = $obj->pcg_version;
82
	                $this->ref = $obj->pcg_version;
83
	                $this->label = $obj->label;
84
	                $this->active = $obj->active;
85
	                	
86
	                return $this->id;
87
	            } else {
88
	                return 0;
89
	            }
90
	        } else {
91
	            $this->error = "Error " . $this->db->lasterror();
92
	            $this->errors[] = "Error " . $this->db->lasterror();
93
	        }
94
	    }
95
	    return - 1;
96
	}
97
	
98
	
99
	/**
100
	 * Insert accountancy system name into database
101
	 *
102
	 * @param User $user making insert
103
	 * @return int if KO, Id of line if OK
104
	 */
105
	function create($user) {
106
		$now = dol_now();
107
		
108
		$sql = "INSERT INTO " . MAIN_DB_PREFIX . "accounting_system";
109
		$sql .= " (date_creation, fk_user_author, numero, label)";
110
		$sql .= " VALUES (" . $this->db->idate($now) . "," . $user->id . ",'" . $this->numero . "','" . $this->label . "')";
111
		
112
		dol_syslog(get_class($this) . "::create sql=" . $sql, LOG_DEBUG);
113
		$resql = $this->db->query($sql);
114
		if ($resql) {
115
			$id = $this->db->last_insert_id(MAIN_DB_PREFIX . "accounting_system");
116
			
117
			if ($id > 0) {
118
				$this->rowid = $id;
119
				$result = $this->rowid;
120
			} else {
121
				$result = - 2;
122
				$this->error = "AccountancySystem::Create Erreur $result";
123
				dol_syslog($this->error, LOG_ERR);
124
			}
125
		} else {
126
			$result = - 1;
127
			$this->error = "AccountancySystem::Create Erreur $result";
128
			dol_syslog($this->error, LOG_ERR);
129
		}
130
		
131
		return $result;
132
	}
133
}