dolibarr 21.0.0-alpha
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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 3 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <https://www.gnu.org/licenses/>.
28 */
29
42{
46 public $db;
47
51 public $errors = array();
52
58 public function __construct(DoliDB $db)
59 {
60 $this->db = $db;
61 }
62
71 public function cleanAmount(&$arrayrecord, $listfields, $record_key)
72 {
73 $value_trim = trim($arrayrecord[$record_key]['val']);
74 return (float) price2num($value_trim);
75 }
76
85 public function cleanValue(&$arrayrecord, $listfields, $record_key)
86 {
87 return trim($arrayrecord[$record_key]['val']);
88 }
89
98 public function computeAmount(&$arrayrecord, $listfields, $record_key)
99 {
100 // get fields indexes
101 if (isset($listfields['b.debit']) && isset($listfields['b.credit'])) {
102 $debit_index = $listfields['b.debit'];
103
104 $debitFloat = (float) price2num($arrayrecord[$debit_index]['val']);
105 if (!empty($debitFloat)) {
106 $amount = $debitFloat;
107 } else {
108 $credit_index = $listfields['b.credit'];
109 $amount = (float) price2num($arrayrecord[$credit_index]['val']);
110 }
111
112 return "'" . $this->db->escape(abs($amount)) . "'";
113 }
114
115 return "''";
116 }
117
118
127 public function computeDirection(&$arrayrecord, $listfields, $record_key)
128 {
129 if (isset($listfields['b.debit'])) {
130 $debit_index = $listfields['b.debit'];
131
132 $debitFloat = (float) price2num($arrayrecord[$debit_index]['val']);
133 if (!empty($debitFloat)) {
134 $sens = 'D';
135 } else {
136 $sens = 'C';
137 }
138
139 return "'" . $this->db->escape($sens) . "'";
140 }
141
142 return "''";
143 }
144
153 public function computePieceNum(&$arrayrecord, $listfields, $record_key)
154 {
155 global $conf;
156
157 $pieceNum = trim($arrayrecord[$record_key]['val']);
158
159 // auto-determine the next value for piece number
160 if ($pieceNum == '') {
161 if (isset($listfields['b.code_journal']) && isset($listfields['b.doc_date'])) {
162 // define memory for last record values and keep next piece number
163 if (!isset($conf->cache['accounting'])) {
164 $conf->cache['accounting'] = array(
165 'lastRecordCompareValues' => array(),
166 'nextPieceNum' => 0,
167 );
168 }
169 $codeJournalIndex = $listfields['b.code_journal'];
170 $docDateIndex = $listfields['b.doc_date'];
171 $atLeastOneLastRecordChanged = false;
172 if (empty($conf->cache['accounting']['lastRecordCompareValues'])) {
173 $atLeastOneLastRecordChanged = true;
174 } else {
175 if ($arrayrecord[$codeJournalIndex]['val'] != $conf->cache['accounting']['lastRecordCompareValues']['b.code_journal']
176 || $arrayrecord[$docDateIndex]['val'] != $conf->cache['accounting']['lastRecordCompareValues']['b.doc_date']
177 ) {
178 $atLeastOneLastRecordChanged = true;
179 }
180 }
181
182 // at least one record value has changed, so we search for the next piece number from database or increment it
183 if ($atLeastOneLastRecordChanged) {
184 $lastPieceNum = 0;
185 if (empty($conf->cache['accounting']['nextPieceNum'])) {
186 // get last piece number from database
187 $sql = "SELECT MAX(piece_num) as last_piece_num";
188 $sql .= " FROM ".$this->db->prefix()."accounting_bookkeeping";
189 $sql .= " WHERE entity IN (".getEntity('accountingbookkeeping').")";
190 $res = $this->db->query($sql);
191 if (!$res) {
192 $this->errors[] = $this->db->lasterror();
193 return '';
194 }
195 if ($obj = $this->db->fetch_object($res)) {
196 $lastPieceNum = (int) $obj->last_piece_num;
197 }
198 $this->db->free($res);
199 }
200 // set next piece number in memory
201 if (empty($conf->cache['accounting']['nextPieceNum'])) {
202 $conf->cache['accounting']['nextPieceNum'] = $lastPieceNum;
203 }
204 $conf->cache['accounting']['nextPieceNum']++;
205
206 // set last records values in memory
207 $conf->cache['accounting']['lastRecordCompareValues'] = array(
208 'b.code_journal' => $arrayrecord[$codeJournalIndex]['val'],
209 'b.doc_date' => $arrayrecord[$docDateIndex]['val'],
210 );
211 }
212 $pieceNum = (string) $conf->cache['accounting']['nextPieceNum'];
213 }
214 }
215
216 return $pieceNum;
217 }
218}
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 '.