<?php

/*
 * ROMAN DIGITAL - Autor: Américo Patetta
 */

/**
 * Description of Despacho
 *
 * @author pc
 */
include_once "server/lib/mod_dbm/package.dbm.php";

class Despacho {

    private $dao;
    private $idDespacho = null;
    private $machineId = null;
    private $fecha = null;
    private $idEESS = null;
    private $volDespachado = null;
    private $idCombustible = null;
    private $odometro = null;
    private $horometro = null;

    public function __construct() {
        $this->dao = new dbObject();
    }

    public function persist() {
        if(!$this->idDespacho){
            $statement = $this->dao->connection->prepare("INSERT INTO combustible_despachos (machine_id, fecha, id_ee_ss,vol_despachado,id_combustible,odometro,horometro) VALUES (?,?,?,?,?,?,?)");
            $statement->bind_param("siididd",$this->machineId,$this->fecha,$this->idEESS,$this->volDespachado,$this->idCombustible,$this->odometro,$this->horometro);
        } else {
            $statement = $this->dao->connection->prepare("UPDATE combustible_despachos set unit_id=?, fecha=?, id_ee_ss=?,vol_despachado=?,id_combustible=?,odometro=?,horometro=? WHERE id_despacho = ?"); 
            $statement->bind_param("siididdi",$this->machineId,$this->fecha,$this->idEESS,$this->volDespachado,$this->idCombustible,$this->odometro,$this->horometro,$this->idDespacho);            
        }
        $statement->execute();
    }

    public function retrieve($idDespacho = null) {
        $id = ($idDespacho) ? $idDespacho : $this->idDespacho;
        $statement = $this->dao->connection->prepare("SELECT id_despacho, unit_id, fecha, id_ee_ss,vol_despachado, id_combustible,odometro,horometro FROM combustible_despachos WHERE id_despacho = ?");
        $statement->bind_param("d", $id);
        $statement->execute();
        $result = $this->dao->getFirstResult($statement);
        //$statement->bind_result($this->idDespacho, $this->unitId, $this->fecha, $this->id_ee_ss, $this->volDespachado, $this->idCombustible, $this->odometro, $this->horometro);
        //$statement->store_result();
        if ($result) {
            $this->id = $result['id_despacho'];
            $this->machineId = $result['unit_id'];
            $this->fecha = $result['fecha'];
            $this->idEESS = $result['id_ee_ss'];
            $this->volDespachado = $result['vol_despachado'];
            $this->idCombustible = $result['id_combustible'];
            $this->odometro = $result['odometro'];
            $this->horometro = $result['horometro'];
        }
        return count($result);
    }

    function get_result($statement) {
        $result = array();
        $statement->store_result();
        for ($i = 0; $i < $statement->num_rows; $i++) {
            $metadata = $statement->result_metadata();
            $params = array();
            while ($field = $metadata->fetch_field()) {
                $params[] = &$result[$i][$field->name];
            }
            call_user_func_array(array($statement, 'bind_result'), $params);
            $statement->fetch();
        }
        return $result;
    }

    public function getIdDespacho() {
        return $this->idDespacho;
    }

    public function getUnitId() {
        return $this->machineId;
    }

    public function getFecha() {
        return $this->fecha;
    }

    public function getIdEESS() {
        return $this->idEESS;
    }

    public function getVolDespachado() {
        return $this->volDespachado;
    }

    public function getIdCombustible() {
        return $this->idCombustible;
    }

    public function getOdometro() {
        return $this->odometro;
    }

    public function getHorometro() {
        return $this->horometro;
    }

    public function setIdDespacho($idDespacho): void {
        $this->idDespacho = $idDespacho;
    }

    public function setUnitId($unitId): void {
        $this->machineId = $unitId;
    }

    public function setFecha($fecha): void {
        $this->fecha = $fecha;
    }

    public function setIdEESS($idEESS): void {
        $this->idEESS = $idEESS;
    }

    public function setVolDespachado($volDespachado): void {
        $this->volDespachado = $volDespachado;
    }

    public function setIdCombustible($idCombustible): void {
        $this->idCombustible = $idCombustible;
    }

    public function setOdometro($odometro): void {
        $this->odometro = $odometro;
    }

    public function setHorometro($horometro): void {
        $this->horometro = $horometro;
    }

}

?>
