| Conditions | 49 |
| Paths | 4324 |
| Total Lines | 292 |
| Code Lines | 196 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 131 | private function modificar() |
||
| 132 | { |
||
| 133 | $this->albaran->observaciones = $_POST['observaciones']; |
||
| 134 | |||
| 135 | /// ¿Es editable o ya ha sido facturado? |
||
| 136 | if ($this->albaran->ptefactura) { |
||
| 137 | $eje0 = $this->ejercicio->get_by_fecha($_POST['fecha'], FALSE); |
||
| 138 | if ($eje0) { |
||
| 139 | $this->albaran->fecha = $_POST['fecha']; |
||
| 140 | $this->albaran->hora = $_POST['hora']; |
||
| 141 | |||
| 142 | if ($this->albaran->codejercicio != $eje0->codejercicio) { |
||
| 143 | $this->albaran->codejercicio = $eje0->codejercicio; |
||
| 144 | $this->albaran->new_codigo(); |
||
| 145 | } |
||
| 146 | } else { |
||
| 147 | $this->new_error_msg('Ningún ejercicio encontrado.'); |
||
| 148 | } |
||
| 149 | |||
| 150 | /// ¿cambiamos el cliente? |
||
| 151 | if ($_POST['cliente'] != $this->albaran->codcliente) { |
||
| 152 | $cliente = $this->cliente->get($_POST['cliente']); |
||
| 153 | if ($cliente) { |
||
| 154 | $this->albaran->codcliente = $cliente->codcliente; |
||
| 155 | $this->albaran->cifnif = $cliente->cifnif; |
||
| 156 | $this->albaran->nombrecliente = $cliente->razonsocial; |
||
| 157 | $this->albaran->apartado = NULL; |
||
| 158 | $this->albaran->ciudad = NULL; |
||
| 159 | $this->albaran->coddir = NULL; |
||
| 160 | $this->albaran->codpais = NULL; |
||
| 161 | $this->albaran->codpostal = NULL; |
||
| 162 | $this->albaran->direccion = NULL; |
||
| 163 | $this->albaran->provincia = NULL; |
||
| 164 | |||
| 165 | foreach ($cliente->get_direcciones() as $d) { |
||
| 166 | if ($d->domfacturacion) { |
||
| 167 | $this->albaran->apartado = $d->apartado; |
||
| 168 | $this->albaran->ciudad = $d->ciudad; |
||
| 169 | $this->albaran->coddir = $d->id; |
||
| 170 | $this->albaran->codpais = $d->codpais; |
||
| 171 | $this->albaran->codpostal = $d->codpostal; |
||
| 172 | $this->albaran->direccion = $d->direccion; |
||
| 173 | $this->albaran->provincia = $d->provincia; |
||
| 174 | break; |
||
| 175 | } |
||
| 176 | } |
||
| 177 | } else { |
||
| 178 | $this->albaran->codcliente = NULL; |
||
| 179 | $this->albaran->nombrecliente = $_POST['nombrecliente']; |
||
| 180 | $this->albaran->cifnif = $_POST['cifnif']; |
||
| 181 | $this->albaran->coddir = NULL; |
||
| 182 | } |
||
| 183 | } else { |
||
| 184 | $this->albaran->nombrecliente = $_POST['nombrecliente']; |
||
| 185 | $this->albaran->cifnif = $_POST['cifnif']; |
||
| 186 | $this->albaran->codpais = $_POST['codpais']; |
||
| 187 | $this->albaran->provincia = $_POST['provincia']; |
||
| 188 | $this->albaran->ciudad = $_POST['ciudad']; |
||
| 189 | $this->albaran->codpostal = $_POST['codpostal']; |
||
| 190 | $this->albaran->direccion = $_POST['direccion']; |
||
| 191 | $this->albaran->apartado = $_POST['apartado']; |
||
| 192 | |||
| 193 | $this->albaran->envio_nombre = $_POST['envio_nombre']; |
||
| 194 | $this->albaran->envio_apellidos = $_POST['envio_apellidos']; |
||
| 195 | $this->albaran->envio_codtrans = NULL; |
||
| 196 | if ($_POST['envio_codtrans'] != '') { |
||
| 197 | $this->albaran->envio_codtrans = $_POST['envio_codtrans']; |
||
| 198 | } |
||
| 199 | $this->albaran->envio_codigo = $_POST['envio_codigo']; |
||
| 200 | $this->albaran->envio_codpais = $_POST['envio_codpais']; |
||
| 201 | $this->albaran->envio_provincia = $_POST['envio_provincia']; |
||
| 202 | $this->albaran->envio_ciudad = $_POST['envio_ciudad']; |
||
| 203 | $this->albaran->envio_codpostal = $_POST['envio_codpostal']; |
||
| 204 | $this->albaran->envio_direccion = $_POST['envio_direccion']; |
||
| 205 | $this->albaran->envio_apartado = $_POST['envio_apartado']; |
||
| 206 | |||
| 207 | $cliente = $this->cliente->get($this->albaran->codcliente); |
||
| 208 | } |
||
| 209 | |||
| 210 | $serie = $this->serie->get($this->albaran->codserie); |
||
| 211 | |||
| 212 | /// ¿cambiamos la serie? |
||
| 213 | if ($_POST['serie'] != $this->albaran->codserie) { |
||
| 214 | $serie2 = $this->serie->get($_POST['serie']); |
||
| 215 | if ($serie2) { |
||
| 216 | $this->albaran->codserie = $serie2->codserie; |
||
| 217 | $this->albaran->new_codigo(); |
||
| 218 | |||
| 219 | $serie = $serie2; |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | $this->albaran->codpago = $_POST['forma_pago']; |
||
| 224 | |||
| 225 | /// ¿Cambiamos la divisa? |
||
| 226 | if ($_POST['divisa'] != $this->albaran->coddivisa) { |
||
| 227 | $divisa = $this->divisa->get($_POST['divisa']); |
||
| 228 | if ($divisa) { |
||
| 229 | $this->albaran->coddivisa = $divisa->coddivisa; |
||
| 230 | $this->albaran->tasaconv = $divisa->tasaconv; |
||
| 231 | } |
||
| 232 | } else if ($_POST['tasaconv'] != '') { |
||
| 233 | $this->albaran->tasaconv = floatval($_POST['tasaconv']); |
||
| 234 | } |
||
| 235 | |||
| 236 | if (isset($_POST['numlineas'])) { |
||
| 237 | $numlineas = intval($_POST['numlineas']); |
||
| 238 | |||
| 239 | $this->albaran->irpf = 0; |
||
| 240 | $this->albaran->netosindto = 0; |
||
| 241 | $this->albaran->neto = 0; |
||
| 242 | $this->albaran->totaliva = 0; |
||
| 243 | $this->albaran->totalirpf = 0; |
||
| 244 | $this->albaran->totalrecargo = 0; |
||
| 245 | $this->albaran->dtopor1 = floatval($_POST['adtopor1']); |
||
| 246 | $this->albaran->dtopor2 = floatval($_POST['adtopor2']); |
||
| 247 | $this->albaran->dtopor3 = floatval($_POST['adtopor3']); |
||
| 248 | $this->albaran->dtopor4 = floatval($_POST['adtopor4']); |
||
| 249 | $this->albaran->dtopor5 = floatval($_POST['adtopor5']); |
||
| 250 | |||
| 251 | $lineas = $this->albaran->get_lineas(); |
||
| 252 | $articulo = new articulo(); |
||
| 253 | |||
| 254 | /// eliminamos las líneas que no encontremos en el $_POST |
||
| 255 | foreach ($lineas as $l) { |
||
| 256 | $encontrada = FALSE; |
||
| 257 | for ($num = 0; $num <= $numlineas; $num++) { |
||
| 258 | if (isset($_POST['idlinea_' . $num]) && $l->idlinea == intval($_POST['idlinea_' . $num])) { |
||
| 259 | $encontrada = TRUE; |
||
| 260 | break; |
||
| 261 | } |
||
| 262 | } |
||
| 263 | if (!$encontrada) { |
||
| 264 | if ($l->delete()) { |
||
| 265 | /// actualizamos el stock |
||
| 266 | $art0 = $articulo->get($l->referencia); |
||
| 267 | if ($art0) { |
||
| 268 | $art0->sum_stock($this->albaran->codalmacen, $l->cantidad, FALSE, $l->codcombinacion); |
||
| 269 | } |
||
| 270 | } else { |
||
| 271 | $this->new_error_msg("¡Imposible eliminar la línea del artículo " . $l->referencia . "!"); |
||
| 272 | } |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | $regimeniva = 'general'; |
||
| 277 | if ($cliente) { |
||
| 278 | $regimeniva = $cliente->regimeniva; |
||
| 279 | } |
||
| 280 | |||
| 281 | /// modificamos y/o añadimos las demás líneas |
||
| 282 | for ($num = 0; $num <= $numlineas; $num++) { |
||
| 283 | $encontrada = FALSE; |
||
| 284 | if (isset($_POST['idlinea_' . $num])) { |
||
| 285 | foreach ($lineas as $k => $value) { |
||
| 286 | /// modificamos la línea |
||
| 287 | if ($value->idlinea == intval($_POST['idlinea_' . $num])) { |
||
| 288 | $encontrada = TRUE; |
||
| 289 | $cantidad_old = $value->cantidad; |
||
| 290 | $lineas[$k]->cantidad = floatval($_POST['cantidad_' . $num]); |
||
| 291 | $lineas[$k]->pvpunitario = floatval($_POST['pvp_' . $num]); |
||
| 292 | $lineas[$k]->dtopor = floatval(fs_filter_input_post('dto_' . $num, 0)); |
||
| 293 | $lineas[$k]->dtopor2 = floatval(fs_filter_input_post('dto2_' . $num, 0)); |
||
| 294 | $lineas[$k]->dtopor3 = floatval(fs_filter_input_post('dto3_' . $num, 0)); |
||
| 295 | $lineas[$k]->dtopor4 = floatval(fs_filter_input_post('dto4_' . $num, 0)); |
||
| 296 | $lineas[$k]->pvpsindto = $value->cantidad * $value->pvpunitario; |
||
| 297 | |||
| 298 | // Descuento Unificado Equivalente |
||
| 299 | $due_linea = $this->fbase_calc_due(array($lineas[$k]->dtopor, $lineas[$k]->dtopor2, $lineas[$k]->dtopor3, $lineas[$k]->dtopor4)); |
||
| 300 | $lineas[$k]->pvptotal = $lineas[$k]->cantidad * $lineas[$k]->pvpunitario * $due_linea; |
||
| 301 | |||
| 302 | $lineas[$k]->descripcion = $_POST['desc_' . $num]; |
||
| 303 | $lineas[$k]->codimpuesto = NULL; |
||
| 304 | $lineas[$k]->iva = 0; |
||
| 305 | $lineas[$k]->recargo = 0; |
||
| 306 | $lineas[$k]->irpf = floatval(fs_filter_input_post('irpf_' . $num, 0)); |
||
| 307 | if (!$serie->siniva && $regimeniva != 'Exento') { |
||
| 308 | $imp0 = $this->impuesto->get_by_iva($_POST['iva_' . $num]); |
||
| 309 | if ($imp0) { |
||
| 310 | $lineas[$k]->codimpuesto = $imp0->codimpuesto; |
||
| 311 | } |
||
| 312 | |||
| 313 | $lineas[$k]->iva = floatval($_POST['iva_' . $num]); |
||
| 314 | $lineas[$k]->recargo = floatval(fs_filter_input_post('recargo_' . $num, 0)); |
||
| 315 | } |
||
| 316 | |||
| 317 | if ($lineas[$k]->save()) { |
||
| 318 | if ($value->irpf > $this->albaran->irpf) { |
||
| 319 | $this->albaran->irpf = $value->irpf; |
||
| 320 | } |
||
| 321 | |||
| 322 | if ($lineas[$k]->cantidad != $cantidad_old) { |
||
| 323 | /// actualizamos el stock |
||
| 324 | $art0 = $articulo->get($value->referencia); |
||
| 325 | if ($art0) { |
||
| 326 | $art0->sum_stock($this->albaran->codalmacen, $cantidad_old - $lineas[$k]->cantidad, FALSE, $lineas[$k]->codcombinacion); |
||
| 327 | } |
||
| 328 | } |
||
| 329 | } else { |
||
| 330 | $this->new_error_msg("¡Imposible modificar la línea del artículo " . $value->referencia . "!"); |
||
| 331 | } |
||
| 332 | |||
| 333 | break; |
||
| 334 | } |
||
| 335 | } |
||
| 336 | |||
| 337 | /// añadimos la línea |
||
| 338 | if (!$encontrada && intval($_POST['idlinea_' . $num]) == -1 && isset($_POST['referencia_' . $num])) { |
||
| 339 | $linea = new linea_albaran_cliente(); |
||
| 340 | $linea->idalbaran = $this->albaran->idalbaran; |
||
| 341 | $linea->descripcion = $_POST['desc_' . $num]; |
||
| 342 | |||
| 343 | if (!$serie->siniva && $regimeniva != 'Exento') { |
||
| 344 | $imp0 = $this->impuesto->get_by_iva($_POST['iva_' . $num]); |
||
| 345 | if ($imp0) { |
||
| 346 | $linea->codimpuesto = $imp0->codimpuesto; |
||
| 347 | } |
||
| 348 | |||
| 349 | $linea->iva = floatval($_POST['iva_' . $num]); |
||
| 350 | $linea->recargo = floatval(fs_filter_input_post('recargo_' . $num, 0)); |
||
| 351 | } |
||
| 352 | |||
| 353 | $linea->irpf = floatval(fs_filter_input_post('irpf_' . $num, 0)); |
||
| 354 | $linea->cantidad = floatval($_POST['cantidad_' . $num]); |
||
| 355 | $linea->pvpunitario = floatval($_POST['pvp_' . $num]); |
||
| 356 | $linea->dtopor = floatval(fs_filter_input_post('dto_' . $num, 0)); |
||
| 357 | $linea->dtopor2 = floatval(fs_filter_input_post('dto2_' . $num, 0)); |
||
| 358 | $linea->dtopor3 = floatval(fs_filter_input_post('dto3_' . $num, 0)); |
||
| 359 | $linea->dtopor4 = floatval(fs_filter_input_post('dto4_' . $num, 0)); |
||
| 360 | $linea->pvpsindto = $linea->cantidad * $linea->pvpunitario; |
||
| 361 | |||
| 362 | // Descuento Unificado Equivalente |
||
| 363 | $due_linea = $this->fbase_calc_due(array($linea->dtopor, $linea->dtopor2, $linea->dtopor3, $linea->dtopor4)); |
||
| 364 | $linea->pvptotal = $linea->cantidad * $linea->pvpunitario * $due_linea; |
||
| 365 | |||
| 366 | $art0 = $articulo->get($_POST['referencia_' . $num]); |
||
| 367 | if ($art0) { |
||
| 368 | $linea->referencia = $art0->referencia; |
||
| 369 | if ($_POST['codcombinacion_' . $num]) { |
||
| 370 | $linea->codcombinacion = $_POST['codcombinacion_' . $num]; |
||
| 371 | } |
||
| 372 | } |
||
| 373 | |||
| 374 | if ($linea->save()) { |
||
| 375 | if ($art0) { |
||
| 376 | /// actualizamos el stock |
||
| 377 | $art0->sum_stock($this->albaran->codalmacen, 0 - $linea->cantidad, FALSE, $linea->codcombinacion); |
||
| 378 | } |
||
| 379 | |||
| 380 | if ($linea->irpf > $this->albaran->irpf) { |
||
| 381 | $this->albaran->irpf = $linea->irpf; |
||
| 382 | } |
||
| 383 | } else { |
||
| 384 | $this->new_error_msg("¡Imposible guardar la línea del artículo " . $linea->referencia . "!"); |
||
| 385 | } |
||
| 386 | } |
||
| 387 | } |
||
| 388 | } |
||
| 389 | |||
| 390 | /// obtenemos los subtotales por impuesto |
||
| 391 | $due_totales = $this->fbase_calc_due([$this->albaran->dtopor1, $this->albaran->dtopor2, $this->albaran->dtopor3, $this->albaran->dtopor4, $this->albaran->dtopor5]); |
||
| 392 | foreach ($this->fbase_get_subtotales_documento($this->albaran->get_lineas(), $due_totales) as $subt) { |
||
| 393 | $this->albaran->netosindto += $subt['netosindto']; |
||
| 394 | $this->albaran->neto += $subt['neto']; |
||
| 395 | $this->albaran->totaliva += $subt['iva']; |
||
| 396 | $this->albaran->totalirpf += $subt['irpf']; |
||
| 397 | $this->albaran->totalrecargo += $subt['recargo']; |
||
| 398 | } |
||
| 399 | |||
| 400 | $this->albaran->total = round($this->albaran->neto + $this->albaran->totaliva - $this->albaran->totalirpf + $this->albaran->totalrecargo, FS_NF0); |
||
| 401 | |||
| 402 | if (abs(floatval($_POST['atotal']) - $this->albaran->total) > .01) { |
||
| 403 | $this->new_error_msg("El total difiere entre el controlador y la vista (" . $this->albaran->total . |
||
| 404 | " frente a " . $_POST['atotal'] . "). Debes informar del error."); |
||
| 405 | } |
||
| 406 | } |
||
| 407 | } |
||
| 408 | |||
| 409 | /// función auxiliar para implementar en los plugins que lo necesiten |
||
| 410 | if (!fs_generar_numero2($this->albaran)) { |
||
| 411 | $this->albaran->numero2 = $_POST['numero2']; |
||
| 412 | } |
||
| 413 | |||
| 414 | if ($this->albaran->save()) { |
||
| 415 | /// Función de ejecución de tareas post guardado correcto del albarán |
||
| 416 | fs_documento_post_save($this->albaran); |
||
| 417 | |||
| 418 | $this->new_message(ucfirst(FS_ALBARAN) . " modificado correctamente."); |
||
| 419 | $this->propagar_cifnif(); |
||
| 420 | $this->new_change(ucfirst(FS_ALBARAN) . ' Cliente ' . $this->albaran->codigo, $this->albaran->url()); |
||
| 421 | } else { |
||
| 422 | $this->new_error_msg("¡Imposible modificar el " . FS_ALBARAN . "!"); |
||
| 423 | } |
||
| 459 |