Conditions | 42 |
Paths | > 20000 |
Total Lines | 184 |
Code Lines | 120 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
94 | public function exportData($period, $date_min = '', $date_max = '', $format = '', $lettering = 0, $alreadyexport = 0, $notnotifiedasexport = 0) |
||
95 | { |
||
96 | global $conf, $langs; |
||
97 | |||
98 | // check rights |
||
99 | if (!DolibarrApiAccess::$user->hasRight('accounting', 'mouvements', 'export')) { |
||
100 | throw new RestException(403, 'No permission to export accounting'); |
||
101 | } |
||
102 | |||
103 | // check parameters |
||
104 | $period_available_list = array('lastmonth', 'currentmonth', 'last3months', 'last6months', 'currentyear', 'lastyear', 'fiscalyear', 'lastfiscalyear', 'actualandlastfiscalyear', 'custom'); |
||
105 | if (!in_array($period, $period_available_list)) { |
||
106 | throw new RestException(404, 'Accountancy export period not found'); |
||
107 | } |
||
108 | if ($period == 'custom') { |
||
109 | if ($date_min == '' && $date_max == '') { |
||
110 | throw new RestException(404, 'Accountancy export start and end date for custom period not defined'); |
||
111 | } |
||
112 | } |
||
113 | if ($format == '') { |
||
114 | $format = AccountancyExport::$EXPORT_TYPE_CONFIGURABLE; // uses default |
||
115 | } |
||
116 | |||
117 | // get objects |
||
118 | $bookkeeping = $this->bookkeeping; |
||
119 | $accountancyexport = $this->accountancyexport; |
||
120 | |||
121 | // find export format code from format number |
||
122 | $format_number_available_list = $accountancyexport->getType(); |
||
123 | if (is_numeric($format)) { |
||
124 | $format_number = (int) $format; |
||
125 | } else { |
||
126 | $format_number = 0; |
||
127 | $format_label_available_list = array_flip($format_number_available_list); |
||
128 | if (isset($format_label_available_list[$format])) { |
||
129 | $format_number = $format_label_available_list[$format]; |
||
130 | } |
||
131 | } |
||
132 | |||
133 | // get all format available and check if exists |
||
134 | if (!array_key_exists($format_number, $format_number_available_list)) { |
||
135 | throw new RestException(404, 'Accountancy export format not found'); |
||
136 | } |
||
137 | |||
138 | $sortorder = 'ASC'; // by default |
||
139 | $sortfield = 't.piece_num, t.rowid'; // by default |
||
140 | |||
141 | // set filter for each period available |
||
142 | $filter = array(); |
||
143 | $doc_date_start = null; |
||
144 | $doc_date_end = null; |
||
145 | $now = dol_now(); |
||
146 | $now_arr = dol_getdate($now); |
||
147 | $now_month = $now_arr['mon']; |
||
148 | $now_year = $now_arr['year']; |
||
149 | if ($period == 'custom') { |
||
150 | if ($date_min != '') { |
||
151 | $time_min = strtotime($date_min); |
||
152 | if ($time_min !== false) { |
||
153 | $doc_date_start = $time_min; |
||
154 | } |
||
155 | } |
||
156 | if ($date_max != '') { |
||
157 | $time_max = strtotime($date_max); |
||
158 | if ($time_max !== false) { |
||
159 | $doc_date_end = $time_max; |
||
160 | } |
||
161 | } |
||
162 | } elseif ($period == 'lastmonth') { |
||
163 | $prev_date_arr = dol_get_prev_month($now_month, $now_year); // get previous month and year if month is january |
||
164 | $doc_date_start = dol_mktime(0, 0, 0, $prev_date_arr['month'], 1, $prev_date_arr['year']); // first day of previous month |
||
165 | $doc_date_end = dol_get_last_day($prev_date_arr['year'], $prev_date_arr['month']); // last day of previous month |
||
166 | } elseif ($period == 'currentmonth') { |
||
167 | $doc_date_start = dol_mktime(0, 0, 0, $now_month, 1, $now_year); // first day of current month |
||
168 | $doc_date_end = dol_get_last_day($now_year, $now_month); // last day of current month |
||
169 | } elseif ($period == 'last3months' || $period == 'last6months') { |
||
170 | if ($period == 'last3months') { |
||
171 | // last 3 months |
||
172 | $nb_prev_month = 3; |
||
173 | } else { |
||
174 | // last 6 months |
||
175 | $nb_prev_month = 6; |
||
176 | } |
||
177 | $prev_month_date_list = array(); |
||
178 | $prev_month_date_list[] = dol_get_prev_month($now_month, $now_year); // get previous month for index = 0 |
||
179 | for ($i = 1; $i < $nb_prev_month; $i++) { |
||
180 | $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 |
||
181 | } |
||
182 | $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 |
||
183 | $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 |
||
184 | } elseif ($period == 'currentyear' || $period == 'lastyear') { |
||
185 | $period_year = $now_year; |
||
186 | if ($period == 'lastyear') { |
||
187 | $period_year--; |
||
188 | } |
||
189 | $doc_date_start = dol_mktime(0, 0, 0, 1, 1, $period_year); // first day of year |
||
190 | $doc_date_end = dol_mktime(23, 59, 59, 12, 31, $period_year); // last day of year |
||
191 | } elseif ($period == 'fiscalyear' || $period == 'lastfiscalyear' || $period == 'actualandlastfiscalyear') { |
||
192 | // find actual fiscal year |
||
193 | $cur_fiscal_period = getCurrentPeriodOfFiscalYear($this->db, $conf); |
||
194 | $cur_fiscal_date_start = $cur_fiscal_period['date_start']; |
||
195 | $cur_fiscal_date_end = $cur_fiscal_period['date_end']; |
||
196 | |||
197 | if ($period == 'fiscalyear') { |
||
198 | $doc_date_start = $cur_fiscal_date_start; |
||
199 | $doc_date_end = $cur_fiscal_date_end; |
||
200 | } else { |
||
201 | // get one day before current fiscal date start (to find previous fiscal period) |
||
202 | $prev_fiscal_date_search = dol_time_plus_duree($cur_fiscal_date_start, -1, 'd'); |
||
203 | |||
204 | // find previous fiscal year from current fiscal year |
||
205 | $prev_fiscal_period = getCurrentPeriodOfFiscalYear($this->db, $conf, $prev_fiscal_date_search); |
||
206 | $prev_fiscal_date_start = $prev_fiscal_period['date_start']; |
||
207 | $prev_fiscal_date_end = $prev_fiscal_period['date_end']; |
||
208 | |||
209 | if ($period == 'lastfiscalyear') { |
||
210 | $doc_date_start = $prev_fiscal_date_start; |
||
211 | $doc_date_end = $prev_fiscal_date_end; |
||
212 | } else { |
||
213 | // period == 'actualandlastfiscalyear' |
||
214 | $doc_date_start = $prev_fiscal_date_start; |
||
215 | $doc_date_end = $cur_fiscal_date_end; |
||
216 | } |
||
217 | } |
||
218 | } |
||
219 | if (is_numeric($doc_date_start)) { |
||
220 | $filter['t.doc_date>='] = $doc_date_start; |
||
221 | } |
||
222 | if (is_numeric($doc_date_end)) { |
||
223 | $filter['t.doc_date<='] = $doc_date_end; |
||
224 | } |
||
225 | |||
226 | // @FIXME Critical bugged. Never use fetchAll without limit ! |
||
227 | $result = $bookkeeping->fetchAll($sortorder, $sortfield, 0, 0, $filter, 'AND', $alreadyexport); |
||
228 | |||
229 | if ($result < 0) { |
||
230 | throw new RestException(500, 'Error bookkeeping fetch all : ' . $bookkeeping->errorsToString()); |
||
231 | } else { |
||
232 | // export files then exit |
||
233 | if (empty($lettering)) { |
||
234 | if (is_array($bookkeeping->lines)) { |
||
235 | foreach ($bookkeeping->lines as $k => $movement) { |
||
236 | unset($bookkeeping->lines[$k]->lettering_code); |
||
237 | unset($bookkeeping->lines[$k]->date_lettering); |
||
238 | } |
||
239 | } |
||
240 | } |
||
241 | |||
242 | $error = 0; |
||
243 | $this->db->begin(); |
||
244 | |||
245 | if (empty($notnotifiedasexport)) { |
||
246 | if (is_array($bookkeeping->lines)) { |
||
247 | foreach ($bookkeeping->lines as $movement) { |
||
248 | $now = dol_now(); |
||
249 | |||
250 | $sql = " UPDATE " . MAIN_DB_PREFIX . "accounting_bookkeeping"; |
||
251 | $sql .= " SET date_export = '" . $this->db->idate($now) . "'"; |
||
252 | $sql .= " WHERE rowid = " . ((int) $movement->id); |
||
253 | |||
254 | $result = $this->db->query($sql); |
||
255 | if (!$result) { |
||
256 | $accountancyexport->errors[] = $langs->trans('NotAllExportedMovementsCouldBeRecordedAsExportedOrValidated'); |
||
257 | $error++; |
||
258 | break; |
||
259 | } |
||
260 | } |
||
261 | } |
||
262 | } |
||
263 | |||
264 | // export and only write file without downloading |
||
265 | if (!$error) { |
||
266 | $result = $accountancyexport->export($bookkeeping->lines, $format_number, 0, 1, 2); |
||
267 | if ($result < 0) { |
||
268 | $error++; |
||
269 | } |
||
270 | } |
||
271 | |||
272 | if ($error) { |
||
273 | $this->db->rollback(); |
||
274 | throw new RestException(500, 'Error accountancy export : ' . implode(',', $accountancyexport->errors)); |
||
275 | } else { |
||
276 | $this->db->commit(); |
||
277 | exit(); |
||
|
|||
278 | } |
||
282 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.