<?php

namespace App\Http\Controllers;

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

class CostoPrimarioController extends Controller
{
    public function getCostoPrimario(Request $request) {
        return CostoPrimario::on('costos_principal')
                            ->where('con_fk_id', $request->input('con_fk_id'))
                            ->where('ins_fk_id', $request->input('ins_fk_id'))
                            ->where('cpr_ano', $request->input('cpr_ano'))
                            ->where('cpr_mes', $request->input('cpr_mes'))
                            ->get()->toArray();
    }

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

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