<?php
namespace App\Controller;

use Carbon\Carbon;
use App\Definition\Controller;
use Incoviba\old\Venta\Venta;
use Incoviba\old\Venta\Subsidio;
use Incoviba\old\Venta\Pago;
use Incoviba\old\Venta\EstadoPago;
use Incoviba\old\Venta\TipoEstadoPago;

class Subsidios
{
  use Controller;

  public static function add()
  {
    $id = get('venta');
    $venta = model(Venta::class)->findOne($id);
    echo view('ventas.subsidios.add', compact('venta'));
  }
  public static function do_add()
  {
    $id = get('venta');
    $venta = model(Venta::class)->findOne($id);
    $f = Carbon::createFromDate(post('year'), post('month'), post('day'), config('app.timezone'));
    $uf = uf($f);

    $pago1 = model(Pago::class)->create();
    $valor = post('ahorro_uf') * $uf->uf->value;
    if (post('ahorro') != null) {
      $valor = post('ahorro');
    }
    $pago1->valor = $valor;
    $pago1->fecha = $f->format('Y-m-d');
    $pago1->uf = $uf->uf->value;

    $pago2 = model(Pago::class)->create();
    $valor = post('subsidio_uf') * $uf->uf->value;
    if (post('subsidio') != null) {
      $valor = post('subsidio');
    }
    $pago2->valor = $valor;
    $pago2->fecha = $f->format('Y-m-d');
    $pago2->uf = $uf->uf->value;

    $pago1->new();
    $pago2->new();

    $subsidio = model(Subsidio::class)->create();
    $subsidio->pago = $pago1->id;
    $subsidio->subsidio = $pago2->id;

    $subsidio->save();
    $venta->subsidio = $subsidio->id();
    $venta->save();
    header('Location: ' . nUrl('ventas', 'show', ['venta' => $venta->id]));
  }
  public static function edit()
  {
    $id = get('venta');
    $venta = model(Venta::class)->findOne($id);
    echo view('ventas.subsidios.edit', compact('venta'));
  }
  public static function do_edit()
  {
    $id = get('venta');
    $venta = model(Venta::class)->findOne($id);
    $f = Carbon::createFromDate(post('year'), post('month'), post('day'), config('app.timezone'));
    $uf = uf($f);

    $pago1 = $venta->subsidio()->pago();
    $valor = post('ahorro_uf') * $uf->uf->value;
    if (post('ahorro') != null) {
      $valor = post('ahorro');
    }
    $pago1->valor = $valor;
    $pago1->fecha = $f->format('Y-m-d');
    $pago1->uf = $uf->uf->value;

    $pago2 = $venta->subsidio()->subsidio();
    $valor = post('subsidio_uf') * $uf->uf->value;
    if (post('subsidio') != null) {
      $valor = post('subsidio');
    }
    $pago2->valor = $valor;
    $pago2->fecha = $f->format('Y-m-d');
    $pago2->uf = $uf->uf->value;

    $pago1->save();
    $pago2->save();
    header('Location: ' . nUrl('ventas', 'show', ['venta' => $venta->id]));
  }
  public static function pagar()
  {
    $id = get('venta');
    $venta = model(Venta::class)->findOne($id);
    $tipo = get('tipo');
    switch($tipo) {
      case 'subsidio':
        $pago = $venta->subsidio()->subsidio();
        break;
      case 'pago':
        $pago = $venta->subsidio()->pago();
        break;
      default:
        $pago = null;
    }
    echo view('ventas.subsidios.pagar', compact('venta', 'pago'));
  }
  public static function do_pagar()
  {
    $id = get('venta');
    $venta = model(Venta::class)->findOne($id);
    $f = Carbon::createFromDate(post('year'), post('month'), post('day'), config('app.timezone'));
    $tipo = post('tipo');
    switch($tipo) {
      case 'subsidio':
        $pago = $venta->subsidio()->subsidio();
        break;
      case 'pago':
        $pago = $venta->subsidio()->pago();
        break;
      default:
        $pago = null;
    }
    $pago->valor = post('valor');
    $tipo = model(TipoEstadoPago::class)->where('descripcion', 'depositado')->findOne();
    $data = [
      'pago' => $pago->id,
      'fecha' => $f->format('Y-m-d'),
      'estado' => $tipo->id
    ];
    $estado = model(EstadoPago::class)->create($data);
    $pago->save();
    $estado->save();
    header('Location: ' . nUrl('ventas', 'show', ['venta' => $venta->id]));
  }
  public static function abonar()
  {
    $id = get('venta');
    $venta = model(Venta::class)->findOne($id);
    $tipo = get('tipo');
    switch($tipo) {
      case 'subsidio':
        $pago = $venta->subsidio()->subsidio();
        break;
      case 'pago':
        $pago = $venta->subsidio()->pago();
        break;
      default:
        $pago = null;
    }
    echo view('ventas.subsidios.abonar', compact('venta', 'pago'));
  }
  public static function do_abonar()
  {
    $id = get('venta');
    $venta = model(Venta::class)->findOne($id);
    $f = Carbon::createFromDate(post('year'), post('month'), post('day'), config('app.timezone'));
    $tipo = post('tipo');
    switch($tipo) {
      case 'subsidio':
        $pago = $venta->subsidio()->subsidio();
        break;
      case 'pago':
        $pago = $venta->subsidio()->pago();
        break;
      default:
        $pago = null;
    }
    $pago->valor = post('valor');
    $tipo = model(TipoEstadoPago::class)->where('descripcion', 'abonado')->findOne();
    $data = [
      'pago' => $pago->id,
      'fecha' => $f->format('Y-m-d'),
      'estado' => $tipo->id
    ];
    $estado = model(EstadoPago::class)->create($data);
    $pago->save();
    $estado->save();
    header('Location: ' . nUrl('ventas', 'show', ['venta' => $venta->id]));
  }
}
