dolibarr 18.0.6
api_accountancy.class.php
1<?php
2/* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
3 * Copyright (C) 2019 Cedric Ancelin <icedo.anc@gmail.com>
4 * Copyright (C) 2023 Lionel Vessiller <lvessiller@open-dsi.fr>
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 <https://www.gnu.org/licenses/>.
18 */
19
20use Luracast\Restler\RestException;
21
30{
35 public static $FIELDS = array();
36
40 public $bookkeeping;
41
45 public $accountancyexport;
46
50 public function __construct()
51 {
52 global $db, $langs;
53 $this->db = $db;
54
55 require_once DOL_DOCUMENT_ROOT.'/accountancy/class/bookkeeping.class.php';
56 require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountancyexport.class.php';
57
58 $langs->load('accountancy');
59
60 $this->bookkeeping = new BookKeeping($this->db);
61 $this->accountancyexport = new AccountancyExport($this->db);
62 }
63
90 public function exportData($period, $date_min = '', $date_max = '', $format = '', $lettering = 0, $alreadyexport = 0, $notnotifiedasexport = 0)
91 {
92 global $conf, $langs;
93
94 // check rights
95 if (!DolibarrApiAccess::$user->rights->accounting->mouvements->export) {
96 throw new RestException(401, 'No permission to export accounting');
97 }
98
99 // check parameters
100 $period_available_list = array('lastmonth', 'currentmonth', 'last3months', 'last6months', 'currentyear', 'lastyear', 'fiscalyear', 'lastfiscalyear', 'actualandlastfiscalyear', 'custom');
101 if (!in_array($period, $period_available_list)) {
102 throw new RestException(404, 'Accountancy export period not found');
103 }
104 if ($period == 'custom') {
105 if ($date_min == '' && $date_max == '') {
106 throw new RestException(404, 'Accountancy export start and end date for custom period not defined');
107 }
108 }
109 if ($format == '') {
110 $format = AccountancyExport::$EXPORT_TYPE_CONFIGURABLE; // uses default
111 }
112
113 // get objects
114 $bookkeeping = $this->bookkeeping;
115 $accountancyexport = $this->accountancyexport;
116
117 // find export format code from format number
118 $format_number_available_list = $accountancyexport->getType();
119 if (is_numeric($format)) {
120 $format_number = (int) $format;
121 } else {
122 $format_number = 0;
123 $format_label_available_list = array_flip($format_number_available_list);
124 if (isset($format_label_available_list[$format])) {
125 $format_number = $format_label_available_list[$format];
126 }
127 }
128
129 // get all format available and check if exists
130 if (!array_key_exists($format_number, $format_number_available_list)) {
131 throw new RestException(404, 'Accountancy export format not found');
132 }
133
134 $sortorder = 'ASC'; // by default
135 $sortfield = 't.piece_num, t.rowid'; // by default
136
137 // set filter for each period available
138 $filter = array();
139 $doc_date_start = null;
140 $doc_date_end= null;
141 $now = dol_now();
142 $now_arr = dol_getdate($now);
143 $now_month = $now_arr['mon'];
144 $now_year = $now_arr['year'];
145 if ($period == 'custom') {
146 if ($date_min != '') {
147 $time_min = strtotime($date_min);
148 if ($time_min !== false) {
149 $doc_date_start = $time_min;
150 }
151 }
152 if ($date_max != '') {
153 $time_max = strtotime($date_max);
154 if ($time_max !== false) {
155 $doc_date_end = $time_max;
156 }
157 }
158 } elseif ($period == 'lastmonth') {
159 $prev_date_arr = dol_get_prev_month($now_month, $now_year); // get previous month and year if month is january
160 $doc_date_start = dol_mktime(0, 0, 0, $prev_date_arr['month'], 1, $prev_date_arr['year']); // first day of previous month
161 $doc_date_end = dol_get_last_day($prev_date_arr['year'], $prev_date_arr['month']); // last day of previous month
162 } elseif ($period == 'currentmonth') {
163 $doc_date_start = dol_mktime(0, 0, 0, $now_month, 1, $now_year); // first day of current month
164 $doc_date_end = dol_get_last_day($now_year, $now_month); // last day of current month
165 } elseif ($period == 'last3months' || $period == 'last6months') {
166 if ($period == 'last3months') {
167 // last 3 months
168 $nb_prev_month = 3;
169 } else {
170 // last 6 months
171 $nb_prev_month = 6;
172 }
173 $prev_month_date_list = array();
174 $prev_month_date_list[] = dol_get_prev_month($now_month, $now_year); // get previous month for index = 0
175 for ($i = 1; $i < $nb_prev_month; $i++) {
176 $prev_month_date_list[] = dol_get_prev_month($prev_month_date_list[$i-1]['month'], $prev_month_date_list[$i-1]['year']); // get i+1 previous month for index=i
177 }
178 $doc_date_start = dol_mktime(0, 0, 0, $prev_month_date_list[$nb_prev_month-1]['month'], 1, $prev_month_date_list[$nb_prev_month-1]['year']); // first day of n previous month for index=n-1
179 $doc_date_end = dol_get_last_day($prev_month_date_list[0]['year'], $prev_month_date_list[0]['month']); // last day of previous month for index = 0
180 } elseif ($period == 'currentyear' || $period == 'lastyear') {
181 $period_year = $now_year;
182 if ($period == 'lastyear') {
183 $period_year--;
184 }
185 $doc_date_start = dol_mktime(0, 0, 0, 1, 1, $period_year); // first day of year
186 $doc_date_end = dol_mktime(23, 59, 59, 12, 31, $period_year); // last day of year
187 } elseif ($period == 'fiscalyear' || $period == 'lastfiscalyear' || $period == 'actualandlastfiscalyear') {
188 // find actual fiscal year
189 $cur_fiscal_period = getCurrentPeriodOfFiscalYear($this->db, $conf);
190 $cur_fiscal_date_start = $cur_fiscal_period['date_start'];
191 $cur_fiscal_date_end = $cur_fiscal_period['date_end'];
192
193 if ($period == 'fiscalyear') {
194 $doc_date_start = $cur_fiscal_date_start;
195 $doc_date_end = $cur_fiscal_date_end;
196 } else {
197 // get one day before current fiscal date start (to find previous fiscal period)
198 $prev_fiscal_date_search = dol_time_plus_duree($cur_fiscal_date_start, -1, 'd');
199
200 // find previous fiscal year from current fiscal year
201 $prev_fiscal_period = getCurrentPeriodOfFiscalYear($this->db, $conf, $prev_fiscal_date_search);
202 $prev_fiscal_date_start = $prev_fiscal_period['date_start'];
203 $prev_fiscal_date_end = $prev_fiscal_period['date_end'];
204
205 if ($period == 'lastfiscalyear') {
206 $doc_date_start = $prev_fiscal_date_start;
207 $doc_date_end = $prev_fiscal_date_end;
208 } else {
209 // period == 'actualandlastfiscalyear'
210 $doc_date_start = $prev_fiscal_date_start;
211 $doc_date_end = $cur_fiscal_date_end;
212 }
213 }
214 }
215 if (is_numeric($doc_date_start)) {
216 $filter['t.doc_date>='] = $doc_date_start;
217 }
218 if (is_numeric($doc_date_end)) {
219 $filter['t.doc_date<='] = $doc_date_end;
220 }
221
222 $result = $bookkeeping->fetchAll($sortorder, $sortfield, 0, 0, $filter, 'AND', $alreadyexport);
223
224 if ($result < 0) {
225 throw new RestException(500, 'Error bookkeeping fetch all : '.$bookkeeping->errorsToString());
226 } else {
227 // export files then exit
228 if (empty($lettering)) {
229 if (is_array($bookkeeping->lines)) {
230 foreach ($bookkeeping->lines as $k => $movement) {
231 unset($bookkeeping->lines[$k]->lettering_code);
232 unset($bookkeeping->lines[$k]->date_lettering);
233 }
234 }
235 }
236
237 $error = 0;
238 $this->db->begin();
239
240 if (empty($notnotifiedasexport)) {
241 if (is_array($bookkeeping->lines)) {
242 foreach ($bookkeeping->lines as $movement) {
243 $now = dol_now();
244
245 $sql = " UPDATE " . MAIN_DB_PREFIX . "accounting_bookkeeping";
246 $sql .= " SET date_export = '" . $this->db->idate($now) . "'";
247 $sql .= " WHERE rowid = " . ((int) $movement->id);
248
249 $result = $this->db->query($sql);
250 if (!$result) {
251 $accountancyexport->errors[] = $langs->trans('NotAllExportedMovementsCouldBeRecordedAsExportedOrValidated');
252 $error++;
253 break;
254 }
255 }
256 }
257 }
258
259 // export and only write file without downloading
260 if (!$error) {
261 $result = $accountancyexport->export($bookkeeping->lines, $format_number, 0, 1, 2);
262 if ($result < 0) {
263 $error++;
264 }
265 }
266
267 if ($error) {
268 $this->db->rollback();
269 throw new RestException(500, 'Error accountancy export : '.implode(',', $accountancyexport->errors));
270 } else {
271 $this->db->commit();
272 exit();
273 }
274 }
275 }
276}
getCurrentPeriodOfFiscalYear($db, $conf, $from_time=null)
Get current period of fiscal year.
Manage the different format accountancy export.
__construct()
Constructor.
exportData($period, $date_min='', $date_max='', $format='', $lettering=0, $alreadyexport=0, $notnotifiedasexport=0)
Accountancy export data.
Class to manage Ledger (General Ledger and Subledger)
Class for API REST v1.
Definition api.class.php:31
dol_get_prev_month($month, $year)
Return previous month.
Definition date.lib.php:496
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
Definition date.lib.php:122
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:596
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
dol_now($mode='auto')
Return date for now.
dol_getdate($timestamp, $fast=false, $forcetimezone='')
Return an array with locale date info.