dolibarr 20.0.0
accountancyimport.class.php
Go to the documentation of this file.
1<?php
2/*
3 * Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
5 * Copyright (C) 2015 Florian Henry <florian.henry@open-concept.pro>
6 * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
7 * Copyright (C) 2016 Pierre-Henry Favre <phf@atm-consulting.fr>
8 * Copyright (C) 2016-2020 Alexandre Spangaro <aspangaro@open-dsi.fr>
9 * Copyright (C) 2013-2017 Olivier Geffroy <jeff@jeffinfo.com>
10 * Copyright (C) 2017 Elarifr. Ari Elbaz <github@accedinfo.com>
11 * Copyright (C) 2017-2019 Frédéric France <frederic.france@netlogic.fr>
12 * Copyright (C) 2017 André Schild <a.schild@aarboard.ch>
13 * Copyright (C) 2020 Guillaume Alexandre <guillaume@tag-info.fr>
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 3 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <https://www.gnu.org/licenses/>.
27 */
28
41{
45 public $db;
46
50 public $errors = array();
51
57 public function __construct(DoliDB $db)
58 {
59 $this->db = $db;
60 }
61
70 public function cleanAmount(&$arrayrecord, $listfields, $record_key)
71 {
72 $value_trim = trim($arrayrecord[$record_key]['val']);
73 return (float) price2num($value_trim);
74 }
75
84 public function cleanValue(&$arrayrecord, $listfields, $record_key)
85 {
86 return trim($arrayrecord[$record_key]['val']);
87 }
88
97 public function computeAmount(&$arrayrecord, $listfields, $record_key)
98 {
99 // get fields indexes
100 if (isset($listfields['b.debit']) && isset($listfields['b.credit'])) {
101 $debit_index = $listfields['b.debit'];
102
103 $debitFloat = (float) price2num($arrayrecord[$debit_index]['val']);
104 if (!empty($debitFloat)) {
105 $amount = $debitFloat;
106 } else {
107 $credit_index = $listfields['b.credit'];
108 $amount = (float) price2num($arrayrecord[$credit_index]['val']);
109 }
110
111 return "'" . $this->db->escape(abs($amount)) . "'";
112 }
113
114 return "''";
115 }
116
117
126 public function computeDirection(&$arrayrecord, $listfields, $record_key)
127 {
128 if (isset($listfields['b.debit'])) {
129 $debit_index = $listfields['b.debit'];
130
131 $debitFloat = (float) price2num($arrayrecord[$debit_index]['val']);
132 if (!empty($debitFloat)) {
133 $sens = 'D';
134 } else {
135 $sens = 'C';
136 }
137
138 return "'" . $this->db->escape($sens) . "'";
139 }
140
141 return "''";
142 }
143
152 public function computePieceNum(&$arrayrecord, $listfields, $record_key)
153 {
154 global $conf;
155
156 $pieceNum = trim($arrayrecord[$record_key]['val']);
157
158 // auto-determine the next value for piece number
159 if ($pieceNum == '') {
160 if (isset($listfields['b.code_journal']) && isset($listfields['b.doc_date'])) {
161 // define memory for last record values and keep next piece number
162 if (!isset($conf->cache['accounting'])) {
163 $conf->cache['accounting'] = array(
164 'lastRecordCompareValues' => array(),
165 'nextPieceNum' => 0,
166 );
167 }
168 $codeJournalIndex = $listfields['b.code_journal'];
169 $docDateIndex = $listfields['b.doc_date'];
170 $atLeastOneLastRecordChanged = false;
171 if (empty($conf->cache['accounting']['lastRecordCompareValues'])) {
172 $atLeastOneLastRecordChanged = true;
173 } else {
174 if ($arrayrecord[$codeJournalIndex]['val'] != $conf->cache['accounting']['lastRecordCompareValues']['b.code_journal']
175 || $arrayrecord[$docDateIndex]['val'] != $conf->cache['accounting']['lastRecordCompareValues']['b.doc_date']
176 ) {
177 $atLeastOneLastRecordChanged = true;
178 }
179 }
180
181 // at least one record value has changed, so we search for the next piece number from database or increment it
182 if ($atLeastOneLastRecordChanged === true) {
183 $lastPieceNum = 0;
184 if (empty($conf->cache['accounting']['nextPieceNum'])) {
185 // get last piece number from database
186 $sql = "SELECT MAX(piece_num) as last_piece_num";
187 $sql .= " FROM ".$this->db->prefix()."accounting_bookkeeping";
188 $sql .= " WHERE entity IN (".getEntity('accountingbookkeeping').")";
189 $res = $this->db->query($sql);
190 if (!$res) {
191 $this->errors[] = $this->db->lasterror();
192 return '';
193 }
194 if ($obj = $this->db->fetch_object($res)) {
195 $lastPieceNum = (int) $obj->last_piece_num;
196 }
197 $this->db->free($res);
198 }
199 // set next piece number in memory
200 if (empty($conf->cache['accounting']['nextPieceNum'])) {
201 $conf->cache['accounting']['nextPieceNum'] = $lastPieceNum;
202 }
203 $conf->cache['accounting']['nextPieceNum']++;
204
205 // set last records values in memory
206 $conf->cache['accounting']['lastRecordCompareValues'] = array(
207 'b.code_journal' => $arrayrecord[$codeJournalIndex]['val'],
208 'b.doc_date' => $arrayrecord[$docDateIndex]['val'],
209 );
210 }
211 $pieceNum = (string) $conf->cache['accounting']['nextPieceNum'];
212 }
213 }
214
215 return $pieceNum;
216 }
217}
Manage the different format accountancy import.
computeAmount(&$arrayrecord, $listfields, $record_key)
Compute amount.
cleanAmount(&$arrayrecord, $listfields, $record_key)
Clean amount.
cleanValue(&$arrayrecord, $listfields, $record_key)
Clean value with trim.
computeDirection(&$arrayrecord, $listfields, $record_key)
Compute direction.
computePieceNum(&$arrayrecord, $listfields, $record_key)
Compute piece number.
__construct(DoliDB $db)
Constructor.
Class to manage Dolibarr database access.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.