<?php

namespace App\Modules\Libros_electronicos\Models;

use Illuminate\Database\Eloquent\Model;
use DB;
use App\Modules\Catalogo\Models\Plan_cuentas;
use App\Modules\Catalogo\Models\Tipdocs;
use App\Modules\Libros_electronicos\Models\Rphonorarios;
class Rphonorarios_linea extends Model
{
    public $timestamps = false;
    protected $table = 'rphonorarios_lineas';
 	protected $guarded = [];
	protected $fillable = [];
	protected $table_cuentas = 'rphonorarios_cuentas';

	//consulta relacionada para traer lineas de una compra
     public function relation_shop(){
		$show = $this::where('id',$this->rphonorarios_id)->get();
		$cuentas_contables = DB::table($this->table_cuentas)->where('rphonorario_id',$this->rphonorarios_id)->get();
		$results = array('lineas'=>$show,'cuentas'=>$cuentas_contables,'totales'=>$this->calculated_lines());
		return $results;
    }
    //funcion para obtener los calculos de las lineas
    public function calculated_lines(){
    	//variables para calculos necesarios
		$totales = $this::where('id',$this->rphonorarios_id)->first(); 

		$data = array(
			'total'=>$totales->honorarios
		);
		return $data;
    }
    public function save_linea(){
        DB::table($this->table)->insert(['codigo' => $this->codigo, 'concepto' => $this->concepto,'cantidad'=>$this->cantidad,'um'=>$this->um,'precio'=>$this->precio,'cuenta'=>$this->cuenta,'dolares'=>$this->dolares,'debe'=>$this->debe,'haber'=>$this->haber,'compras_id'=>$this->compras_id]);
    }
    public function rphonorarios(){
		return $this->belongsto('App\Modules\Libros_electronicos\Models\Rphonorario');
	}
	public function tipdocs(){
		return $this->belongsto('App\Modules\Catalogo\Models\Tipdoc');
	}
	public function plan_cuentas(){
		return $this->belongsto('App\Modules\Catalogo\Models\Plan_cuenta');
	}
	public function ccostos(){
		return $this->belongsto('App\Modules\Catalogo\Models\Ccosto');
	}
}
