<?php

class PagarRetiro extends AprobarCapitalizacion
{
    public function __construct($aporte)
    {
        parent::__construct($aporte);
        Yii::import('application.modules.contable.models.*', true);
    }

    public function contabilizar()
    {
        if ($this->aporte->id_tipo_aporte_voluntario != 2) {
            return;
        }

        if (! ParamEscenarios::conectado($this->escenario)) {
            return;
        }

        $comprobante = $this->comprobante($this->aporte);

        $this->seguimientoComprobante(
            $comprobante->id,
            $this->aporte->id,
            $this->aporte->getProcesoPago(),
            $this->titulo_proceso
        );

        $this->movimientos($comprobante);
    }

    protected function movimientos($comprobante)
    {
        $movimiento = new Movimiento(
            $comprobante,
            $this->aporte->fecha_registro
        );

        $montos = $this->getMontos();

        $movimiento->crear(
            Parametros::getCuentaContable([
                'clave' => 'cta_aporte_voluntario',
                'escenario' => $this->escenario,
                'tipo_parametro' => $this->tipo_parametro,
                'proceso' => $this->titulo_proceso
            ]),
            $montos['cta_aporte_voluntario']
        );

        $movimiento->crear(
            BancoCuentaContable::get($this->aporte->id_banco_destino),
            0,
            $montos['cta_banco_aporte_voluntario']
        );
    }

    protected function comprobante($aporte)
    {
        $this->setProceso($aporte);

        $comprobante = new Comprobante('crea');
        $comprobante->fecha_comprobante = $this->fechaComprobante();
        $comprobante->descripcion = "Pago aporte voluntario {$this->aporte->idAsociado->nombreCompleto()}";
        $comprobante->status = 1;
        $comprobante->nro_documento = $this->aporte->idAsociado->cedula;

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

        return $comprobante;
    }
}
