90 public function exportData($period, $date_min =
'', $date_max =
'', $format =
'', $lettering = 0, $alreadyexport = 0, $notnotifiedasexport = 0)
95 if (!DolibarrApiAccess::$user->rights->accounting->mouvements->export) {
96 throw new RestException(401,
'No permission to export accounting');
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');
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');
110 $format = AccountancyExport::$EXPORT_TYPE_CONFIGURABLE;
114 $bookkeeping = $this->bookkeeping;
115 $accountancyexport = $this->accountancyexport;
118 $format_number_available_list = $accountancyexport->getType();
119 if (is_numeric($format)) {
120 $format_number = (int) $format;
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];
130 if (!array_key_exists($format_number, $format_number_available_list)) {
131 throw new RestException(404,
'Accountancy export format not found');
135 $sortfield =
't.piece_num, t.rowid';
139 $doc_date_start =
null;
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;
152 if ($date_max !=
'') {
153 $time_max = strtotime($date_max);
154 if ($time_max !==
false) {
155 $doc_date_end = $time_max;
158 } elseif ($period ==
'lastmonth') {
160 $doc_date_start =
dol_mktime(0, 0, 0, $prev_date_arr[
'month'], 1, $prev_date_arr[
'year']);
161 $doc_date_end =
dol_get_last_day($prev_date_arr[
'year'], $prev_date_arr[
'month']);
162 } elseif ($period ==
'currentmonth') {
163 $doc_date_start =
dol_mktime(0, 0, 0, $now_month, 1, $now_year);
165 } elseif ($period ==
'last3months' || $period ==
'last6months') {
166 if ($period ==
'last3months') {
173 $prev_month_date_list = array();
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']);
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']);
179 $doc_date_end =
dol_get_last_day($prev_month_date_list[0][
'year'], $prev_month_date_list[0][
'month']);
180 } elseif ($period ==
'currentyear' || $period ==
'lastyear') {
181 $period_year = $now_year;
182 if ($period ==
'lastyear') {
185 $doc_date_start =
dol_mktime(0, 0, 0, 1, 1, $period_year);
186 $doc_date_end =
dol_mktime(23, 59, 59, 12, 31, $period_year);
187 } elseif ($period ==
'fiscalyear' || $period ==
'lastfiscalyear' || $period ==
'actualandlastfiscalyear') {
190 $cur_fiscal_date_start = $cur_fiscal_period[
'date_start'];
191 $cur_fiscal_date_end = $cur_fiscal_period[
'date_end'];
193 if ($period ==
'fiscalyear') {
194 $doc_date_start = $cur_fiscal_date_start;
195 $doc_date_end = $cur_fiscal_date_end;
202 $prev_fiscal_date_start = $prev_fiscal_period[
'date_start'];
203 $prev_fiscal_date_end = $prev_fiscal_period[
'date_end'];
205 if ($period ==
'lastfiscalyear') {
206 $doc_date_start = $prev_fiscal_date_start;
207 $doc_date_end = $prev_fiscal_date_end;
210 $doc_date_start = $prev_fiscal_date_start;
211 $doc_date_end = $cur_fiscal_date_end;
215 if (is_numeric($doc_date_start)) {
216 $filter[
't.doc_date>='] = $doc_date_start;
218 if (is_numeric($doc_date_end)) {
219 $filter[
't.doc_date<='] = $doc_date_end;
222 $result = $bookkeeping->fetchAll($sortorder, $sortfield, 0, 0, $filter,
'AND', $alreadyexport);
225 throw new RestException(500,
'Error bookkeeping fetch all : '.$bookkeeping->errorsToString());
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);
240 if (empty($notnotifiedasexport)) {
241 if (is_array($bookkeeping->lines)) {
242 foreach ($bookkeeping->lines as $movement) {
245 $sql =
" UPDATE " . MAIN_DB_PREFIX .
"accounting_bookkeeping";
246 $sql .=
" SET date_export = '" . $this->db->idate($now) .
"'";
247 $sql .=
" WHERE rowid = " . ((int) $movement->id);
249 $result = $this->db->query($sql);
251 $accountancyexport->errors[] = $langs->trans(
'NotAllExportedMovementsCouldBeRecordedAsExportedOrValidated');
261 $result = $accountancyexport->export($bookkeeping->lines, $format_number, 0, 1, 2);
268 $this->db->rollback();
269 throw new RestException(500,
'Error accountancy export : '.implode(
',', $accountancyexport->errors));