<?php
namespace App\Controller;

use App\Definition\Controller;
use Incoviba\old\Venta\Venta;
use Incoviba\old\Venta\EstadoPago;
use Carbon\Carbon;

class Reajustes
{
	use Controller;

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

		$valor = correctNumber(post('valor'));
		if ($valor == '') {
			$valor_uf = correctNumber(post('valor_uf'));
			$valor = $valor_uf * $uf->uf->value;
		}
		$pago = $venta->pie()->reajuste();
		if ($pago->valor != $valor) {
			$pago->valor = $valor;
		}
		if ($pago->fecha != $f->format('Y-m-d')) {
			$pago->fecha = $f->format('Y-m-d');
			$pago->uf = $uf->uf->value;
		}

		$pago->save();
		header('Location: ' . url('', ['p' => 'ventas', 'a' => 'show', 'venta' => $venta->id]));
	}
	public static function pagar()
	{
		$id = get('venta');
		$venta = model(Venta::class)->findOne($id);

		return view('ventas.pies.reajustes.pagar', compact('venta'));
	}
	public static function pagado()
	{
		$id = get('venta');
		$venta = model(Venta::class)->findOne($id);

		$f = Carbon::createFromDate(post('year'), post('month'), post('day'), config('app.timezone'));

		$data = [
			'pago' => $venta->pie()->reajuste()->id,
			'fecha' => $f->format('Y-m-d'),
			'estado' => 1
		];
		$estado = model(EstadoPago::class)->create($data);
		$estado->save();
		header('Location: ' . url('', ['p' => 'ventas', 'a' => 'show', 'venta' => $venta->id]));
	}
	public static function abonar()
	{
		$id = get('venta');
		$venta = model(Venta::class)->findOne($id);

		return view('ventas.pies.reajustes.abonar', compact('venta'));
	}
	public static function abonado()
	{
		$id = get('venta');
		$venta = model(Venta::class)->findOne($id);

		$f = Carbon::createFromDate(post('year'), post('month'), post('day'), config('app.timezone'));

		$data = [
			'pago' => $venta->pie()->reajuste()->id,
			'fecha' => $f->format('Y-m-d'),
			'estado' => 2
		];
		$estado = model(EstadoPago::class)->create($data);
		$estado->save();
		header('Location: ' . url('', ['p' => 'ventas', 'a' => 'show', 'venta' => $venta->id]));
	}
}
?>
