Files
intranet/app/Controller/Propietarios.php

130 lines
3.6 KiB
PHP
Raw Normal View History

2020-12-01 17:23:13 -03:00
<?php
namespace App\Controller;
use App\Definition\Controller;
use Incoviba\old\Venta\Propietario;
use Incoviba\old\Common\Region;
use Incoviba\old\Common\Direccion;
use Incoviba\old\Venta\Venta;
class Propietarios
{
use Controller;
public static function edit()
{
$id = get('venta');
$venta = model(Venta::class)->findOne($id);
$propietario = $venta->propietario();
$regiones = model(Region::class)->orderByAsc('numeracion')->findMany();
return view('ventas.propietarios.edit', compact('venta', 'propietario', 'regiones'));
}
public static function editar()
{
$id = get('venta');
$venta = model(Venta::class)->findOne($id);
$info = post();
list($info['rut'], $info['dv']) = explode('-', str_replace('.', '', $info['rut']));
$propietario = model(Propietario::class)->findOne($info['rut']);
if (!$propietario) {
$propietario = model(Propietario::class)->create();
}
if ($propietario->direccion != 0) {
$direccion = $propietario->direccion();
} else {
$direccion = model(Direccion::class)
->where('calle', post('calle'))
->where('numero', post('numero'))
->where('extra', post('extra'))
->where('comuna', post('comuna'))
->findOne();
if (!$direccion) {
$data = [
'calle' => post('calle'),
'numero' => post('numero'),
'extra' => post('extra'),
'comuna' => post('comuna')
];
$direccion = model(Direccion::class)->create($data);
}
}
if (isset($info['empresa'])) {
$info['apellido_paterno'] = '';
$info['apellido_materno'] = '';
}
if ($propietario->representante != 0) {
list($info['rep_rut'], $info['rep_dv']) = explode('-', str_replace('.', '', $info['rep_rut']));
$representante = $propietario->representante();
} elseif (isset($info['rep_rut'])) {
list($info['rep_rut'], $info['rep_dv']) = explode('-', str_replace('.', '', $info['rep_rut']));
$representante= model(Propietario::class)->findOne($info['rep_rut']);
if (!$representante) {
$representante= model(Propietario::class)->create();
}
}
$fields = ['rut', 'dv', 'nombres', 'apellido_paterno', 'apellido_materno'];
$change = false;
foreach ($fields as $key) {
if ($propietario->$key != $info[$key]) {
$propietario->$key = $info[$key];
$change = true;
}
}
if ($direccion->isNew()) {
$direccion->save();
}
if ($propietario->direccion != $direccion->id) {
$propietario->direccion = $direccion->id;
$change = true;
}
if ($change) {
d($propietario);
$propietario->save();
}
if (isset($info['rep_rut'])) {
$change = false;
if ($representante->rut != $info['rep_rut']) {
$representante->rut = $info['rep_rut'];
$representante->dv = $info['rep_dv'];
$change = true;
}
if ($representante->nombres != $info['rep_nombres']) {
$representante->nombres = $info['rep_nombres'];
$change = true;
}
if ($representante->apellido_paterno != $info['rep_apaterno']) {
$representante->apellido_paterno = $info['rep_apaterno'];
$change = true;
}
if ($representante->apellido_materno != $info['rep_amaterno']) {
$representante->apellido_materno = $info['rep_amaterno'];
$change = true;
}
if ($representante->direccion != $direccion->id) {
$representante->direccion = $direccion->id;
$change = true;
}
if ($change) {
$representante->save();
}
if ($propietario->representante != $representante->rut) {
$propietario->representante = $representante->rut;
$propietario->save();
}
}
if ($venta->propietario != $propietario->rut) {
$venta->propietario = $propietario->rut;
$venta->save();
}
header('Location: ' . url('', ['p' => 'ventas', 'a' => 'show', 'venta' => $venta->id]));
}
}
?>