<?php

namespace App\Http\Controllers;

use App\Models\CostoTotal;
use Illuminate\Http\Request;

class CostoTotalController extends Controller
{
    public function getCostoTotal(Request $request) {
        return CostoTotal::on('costos_principal')
                         ->where('con_fk_id', $request->input('con_fk_id'))
                         ->where('ins_fk_id', $request->input('ins_fk_id'))
                         ->where('cot_ano', $request->input('cot_ano'))
                         ->where('cot_mes', $request->input('cot_mes'))
                         ->get()->toArray();
    }

    public function getCostoTotalMeses(Request $request) {
        $sede = $request->input('sed_fk_id');

        if ($sede === "0") {
            return \DB::select("select *
                                from costos_principal.costo_total
                                where con_fk_id = ".$request->input('con_fk_id')." and
                                      ins_fk_id = '".$request->input('ins_fk_id')."' and
                                      cot_ano = ".$request->input('cot_ano')." and
                                      cot_mes in (".$request->input('meses').")
                                order by cot_mes asc");
        } else {
            return \DB::select("select *
                                from costos_principal.costo_total as cot
                                join costos_principal.centros_costo on (cco_fk_id = cco_pk_id)
                                where cot.con_fk_id = ".$request->input('con_fk_id')." and
                                      cot.ins_fk_id = '".$request->input('ins_fk_id')."' and
                                      cot_ano = ".$request->input('cot_ano')." and
                                      cot_mes in (".$request->input('meses').") and
                                      sed_fk_id = ".$sede."
                                order by cot_mes asc");
        }
    }
}
