<?php

namespace Predial\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

use Predial\DetallePagoPredial;
use Predial\DetalleFacturaPredial;

class DetallePagoPredialController extends Controller{

    public static function store($valort, $pago, $factura, $valorX){
        try {
            $xxx = 0;
            $data = DB::table('gr_detalle_factura_predial as dtf')
                        ->select('dtf.id_unico', 'dtf.valor')
                        ->where('dtf.factura', '=', $factura)
                        ->get();
            foreach ($data as $row) {
                $valor = $row->valor;

                $new = new DetallePagoPredial();
                $new->valor          = $valor;
                $new->pago           = $pago;
                $new->detallefactura = $row->id_unico;
                $new->save();

            }

            $dtp = DetallePagoPredialController::getLastYearPay($pago);
            DetallePagoPredialController::saveLastYearPay($dtp->anno, $dtp->predio);
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }

    public static function guardarPagoF($pago, $factura){
        try {
            $xxx = 0;
            $data = DB::table('gr_detalle_factura_predial as dtf')
                        ->select('dtf.id_unico', 'dtf.valor', 'dtf.concepto', 'dtp.valor as dtpval', 'dtp.detallefactura')
                        ->leftJoin('gr_detalle_pago_predial as dtp', 'dtp.detallefactura', '=', 'dtf.id_unico')
                        ->where('dtf.factura', '=', $factura)
                        ->get();
            foreach ($data as $row) {
                $valor = $row->valor;

                $new = new DetallePagoPredial();
                $new->valor          = $valor;
                $new->pago           = $pago;
                $new->detallefactura = $row->id_unico;
                $new->save();
            }

            $dtp = DetallePagoPredialController::getLastYearPay($pago);
            DetallePagoPredialController::saveLastYearPay($dtp->anno, $dtp->predio);
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }

    public static function getLastYearPay($pay){
        try {
            $data = DB::table('gr_detalle_pago_predial as dtp')
                    ->select('fat.predio as predio', 'ava.anno as anno')
                    ->leftJoin('gr_detalle_factura_predial as dtf', 'dtp.detallefactura', '=', 'dtf.id_unico')
                    ->leftJoin('gr_factura_predial as fat', 'dtf.factura', '=', 'fat.id_unico')
                    ->leftJoin('gr_avaluo as ava', 'dtf.avaluo', '=', 'ava.id_unico')
                    ->where('dtp.pago', '=', $pay)
                    ->orderBy('ava.anno', 'desc')
                    ->first();
            return $data;
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }

    public static function saveLastYearPay($year, $predio){
        try {
            DB::table('gr_ultimo_ano_pago')
                ->insert(["anno" => $year, "predio" => $predio]);
        } catch (Exception $e) {
            return $e->getMessage;
        }
    }
}
