Conditions | 13 |
Paths | 50 |
Total Lines | 65 |
Code Lines | 39 |
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 |
||
156 | public function computePieceNum(&$arrayrecord, $listfields, $record_key) |
||
157 | { |
||
158 | global $conf; |
||
159 | |||
160 | $pieceNum = trim($arrayrecord[$record_key]['val']); |
||
161 | |||
162 | // auto-determine the next value for piece number |
||
163 | if ($pieceNum == '') { |
||
164 | if (isset($listfields['b.code_journal']) && isset($listfields['b.doc_date'])) { |
||
165 | // define memory for last record values and keep next piece number |
||
166 | if (!isset($conf->cache['accounting'])) { |
||
167 | $conf->cache['accounting'] = array( |
||
168 | 'lastRecordCompareValues' => array(), |
||
169 | 'nextPieceNum' => 0, |
||
170 | ); |
||
171 | } |
||
172 | $codeJournalIndex = $listfields['b.code_journal']; |
||
173 | $docDateIndex = $listfields['b.doc_date']; |
||
174 | $atLeastOneLastRecordChanged = false; |
||
175 | if (empty($conf->cache['accounting']['lastRecordCompareValues'])) { |
||
176 | $atLeastOneLastRecordChanged = true; |
||
177 | } else { |
||
178 | if ( |
||
179 | $arrayrecord[$codeJournalIndex]['val'] != $conf->cache['accounting']['lastRecordCompareValues']['b.code_journal'] |
||
180 | || $arrayrecord[$docDateIndex]['val'] != $conf->cache['accounting']['lastRecordCompareValues']['b.doc_date'] |
||
181 | ) { |
||
182 | $atLeastOneLastRecordChanged = true; |
||
183 | } |
||
184 | } |
||
185 | |||
186 | // at least one record value has changed, so we search for the next piece number from database or increment it |
||
187 | if ($atLeastOneLastRecordChanged === true) { |
||
188 | $lastPieceNum = 0; |
||
189 | if (empty($conf->cache['accounting']['nextPieceNum'])) { |
||
190 | // get last piece number from database |
||
191 | $sql = "SELECT MAX(piece_num) as last_piece_num"; |
||
192 | $sql .= " FROM " . $this->db->prefix() . "accounting_bookkeeping"; |
||
193 | $sql .= " WHERE entity IN (" . getEntity('accountingbookkeeping') . ")"; |
||
194 | $res = $this->db->query($sql); |
||
195 | if (!$res) { |
||
196 | $this->errors[] = $this->db->lasterror(); |
||
197 | return ''; |
||
198 | } |
||
199 | if ($obj = $this->db->fetch_object($res)) { |
||
200 | $lastPieceNum = (int) $obj->last_piece_num; |
||
201 | } |
||
202 | $this->db->free($res); |
||
203 | } |
||
204 | // set next piece number in memory |
||
205 | if (empty($conf->cache['accounting']['nextPieceNum'])) { |
||
206 | $conf->cache['accounting']['nextPieceNum'] = $lastPieceNum; |
||
207 | } |
||
208 | $conf->cache['accounting']['nextPieceNum']++; |
||
209 | |||
210 | // set last records values in memory |
||
211 | $conf->cache['accounting']['lastRecordCompareValues'] = array( |
||
212 | 'b.code_journal' => $arrayrecord[$codeJournalIndex]['val'], |
||
213 | 'b.doc_date' => $arrayrecord[$docDateIndex]['val'], |
||
214 | ); |
||
215 | } |
||
216 | $pieceNum = (string) $conf->cache['accounting']['nextPieceNum']; |
||
217 | } |
||
218 | } |
||
219 | |||
220 | return $pieceNum; |
||
221 | } |
||
223 |