91 public function exportData($period, $date_min =
'', $date_max =
'', $format =
'', $lettering = 0, $alreadyexport = 0, $notnotifiedasexport = 0)
96 if (!DolibarrApiAccess::$user->hasRight(
'accounting',
'mouvements',
'export')) {
97 throw new RestException(403,
'No permission to export accounting');
101 $period_available_list = array(
'lastmonth',
'currentmonth',
'last3months',
'last6months',
'currentyear',
'lastyear',
'fiscalyear',
'lastfiscalyear',
'actualandlastfiscalyear',
'custom');
102 if (!in_array($period, $period_available_list)) {
103 throw new RestException(404,
'Accountancy export period not found');
105 if ($period ==
'custom') {
106 if ($date_min ==
'' && $date_max ==
'') {
107 throw new RestException(404,
'Accountancy export start and end date for custom period not defined');
111 $format = AccountancyExport::$EXPORT_TYPE_CONFIGURABLE;
115 $bookkeeping = $this->bookkeeping;
116 $accountancyexport = $this->accountancyexport;
119 $format_number_available_list = $accountancyexport->getType();
120 if (is_numeric($format)) {
121 $format_number = (int) $format;
124 $format_label_available_list = array_flip($format_number_available_list);
125 if (isset($format_label_available_list[$format])) {
126 $format_number = $format_label_available_list[$format];
131 if (!array_key_exists($format_number, $format_number_available_list)) {
132 throw new RestException(404,
'Accountancy export format not found');
136 $sortfield =
't.piece_num, t.rowid';
140 $doc_date_start =
null;
141 $doc_date_end =
null;
144 $now_month = $now_arr[
'mon'];
145 $now_year = $now_arr[
'year'];
146 if ($period ==
'custom') {
147 if ($date_min !=
'') {
148 $time_min = strtotime($date_min);
149 if ($time_min !==
false) {
150 $doc_date_start = $time_min;
153 if ($date_max !=
'') {
154 $time_max = strtotime($date_max);
155 if ($time_max !==
false) {
156 $doc_date_end = $time_max;
159 } elseif ($period ==
'lastmonth') {
161 $doc_date_start =
dol_mktime(0, 0, 0, $prev_date_arr[
'month'], 1, $prev_date_arr[
'year']);
162 $doc_date_end =
dol_get_last_day($prev_date_arr[
'year'], $prev_date_arr[
'month']);
163 } elseif ($period ==
'currentmonth') {
164 $doc_date_start =
dol_mktime(0, 0, 0, $now_month, 1, $now_year);
166 } elseif ($period ==
'last3months' || $period ==
'last6months') {
167 if ($period ==
'last3months') {
174 $prev_month_date_list = array();
176 for ($i = 1; $i < $nb_prev_month; $i++) {
177 $prev_month_date_list[] =
dol_get_prev_month($prev_month_date_list[$i - 1][
'month'], $prev_month_date_list[$i - 1][
'year']);
179 $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']);
180 $doc_date_end =
dol_get_last_day($prev_month_date_list[0][
'year'], $prev_month_date_list[0][
'month']);
181 } elseif ($period ==
'currentyear' || $period ==
'lastyear') {
182 $period_year = $now_year;
183 if ($period ==
'lastyear') {
186 $doc_date_start =
dol_mktime(0, 0, 0, 1, 1, $period_year);
187 $doc_date_end =
dol_mktime(23, 59, 59, 12, 31, $period_year);
188 } elseif ($period ==
'fiscalyear' || $period ==
'lastfiscalyear' || $period ==
'actualandlastfiscalyear') {
191 $cur_fiscal_date_start = $cur_fiscal_period[
'date_start'];
192 $cur_fiscal_date_end = $cur_fiscal_period[
'date_end'];
194 if ($period ==
'fiscalyear') {
195 $doc_date_start = $cur_fiscal_date_start;
196 $doc_date_end = $cur_fiscal_date_end;
203 $prev_fiscal_date_start = $prev_fiscal_period[
'date_start'];
204 $prev_fiscal_date_end = $prev_fiscal_period[
'date_end'];
206 if ($period ==
'lastfiscalyear') {
207 $doc_date_start = $prev_fiscal_date_start;
208 $doc_date_end = $prev_fiscal_date_end;
211 $doc_date_start = $prev_fiscal_date_start;
212 $doc_date_end = $cur_fiscal_date_end;
216 if (is_numeric($doc_date_start)) {
217 $filter[
't.doc_date>='] = $doc_date_start;
219 if (is_numeric($doc_date_end)) {
220 $filter[
't.doc_date<='] = $doc_date_end;
224 $result = $bookkeeping->fetchAll($sortorder, $sortfield, 0, 0, $filter,
'AND', $alreadyexport);
227 throw new RestException(500,
'Error bookkeeping fetch all : '.$bookkeeping->errorsToString());
230 if (empty($lettering)) {
231 if (is_array($bookkeeping->lines)) {
232 foreach ($bookkeeping->lines as $k => $movement) {
233 unset($bookkeeping->lines[$k]->lettering_code);
234 unset($bookkeeping->lines[$k]->date_lettering);
242 if (empty($notnotifiedasexport)) {
243 if (is_array($bookkeeping->lines)) {
244 foreach ($bookkeeping->lines as $movement) {
247 $sql =
" UPDATE " . MAIN_DB_PREFIX .
"accounting_bookkeeping";
248 $sql .=
" SET date_export = '" . $this->db->idate($now) .
"'";
249 $sql .=
" WHERE rowid = " . ((int) $movement->id);
251 $result = $this->db->query($sql);
253 $accountancyexport->errors[] = $langs->trans(
'NotAllExportedMovementsCouldBeRecordedAsExportedOrValidated');
263 $result = $accountancyexport->export($bookkeeping->lines, $format_number, 0, 1, 2);
270 $this->db->rollback();
271 throw new RestException(500,
'Error accountancy export : '.implode(
',', $accountancyexport->errors));