<?php

class Movimiento extends Utilidades
{
    public $comprobante;
    public $fechaOperacion;

    public function __construct($comprobante, $fechaOperacion)
    {
        $this->comprobante = $comprobante;
        $this->fechaOperacion = $fechaOperacion;
    }

    public function crear($cuenta, $debe = 0, $haber = 0)
    {
        $movimiento = new DetalleComprobante();
        $movimiento->descripcion = $this->comprobante->descripcion;
        $movimiento->fecha_referencia = $this->fechaOperacion;
        $movimiento->referencia = $this->comprobante->nro_documento;
        $movimiento->id_cuenta = $cuenta;
        $movimiento->clv_comprobante = $this->comprobante->id;
        $movimiento->monto_debe = $debe;
        $movimiento->monto_haber = $haber;
        $movimiento->estatus_contab = 1;

        if (! $movimiento->validate()) {
            throw new Exception('Imposible registrar movimiento: <strong>' . array_values($movimiento->getErrors())[0][0] . '</strong>', $this->codigo_error);
        }

        if (! $movimiento->save()) {
            throw new Exception('Error al contabilizar comprobante.', $this->codigo_error);
        }
    }
}
