<?php
namespace App\Controller;

use App\Definition\Controller;

class Comentarios
{
  use Controller;

  public static function add()
  {
    $venta = \Model::factory(\Incoviba\old\Venta\Venta::class)->findOne(get('venta'));
    echo view('ventas.comentarios.add', compact('venta'));
  }
  public static function agregar()
  {
    $venta = \Model::factory(\Incoviba\old\Venta\Venta::class)->findOne(get('venta'));
    if ($venta === false) {
      throw new Exception('Venta no existe.');
    }
    $data = [
      'venta' => $venta->id,
      'fecha' => \Carbon\Carbon::now(config('app.timezone'))->format('Y-m-d H:i:s'),
      'texto' => post('comentario')
    ];
    $comentario = \Model::factory(\Incoviba\old\Venta\Comentario::class)->create($data);
    $comentario->save();
    header('Location: ' . nUrl('ventas', 'show', ['venta' => $venta->id]));
  }
  public static function delete()
  {
    $comentario = \Model::factory(\Incoviba\old\Venta\Comentario::class)->findOne(get('comentario'));
    $venta = $comentario->venta();
    $comentario->estado = 0;
    $comentario->save();
    header('Location: ' . nUrl('ventas', 'show', ['venta' => $venta->id]));
  }
}
?>
