<?php

/**
 * Comisiones form base class.
 *
 * @package    form
 * @subpackage comisiones
 * @version    SVN: $Id: sfPropelFormGeneratedTemplate.php 15484 2009-02-13 13:13:51Z fabien $
 */
class BaseComisionesForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                         => new sfWidgetFormInputHidden(),
      'codare'                     => new sfWidgetFormInput(),
      'tipcomi'                    => new sfWidgetFormPropelSelect(array('model' => 'TiposComision', 'add_empty' => true)),
      'codpro'                     => new sfWidgetFormInput(),
      'fecha'                      => new sfWidgetFormDate(),
      'objetivo'                   => new sfWidgetFormInput(),
      'actividad'                  => new sfWidgetFormInput(),
      'observa'                    => new sfWidgetFormInput(),
      'created_at'                 => new sfWidgetFormDate(),
      'update_at'                  => new sfWidgetFormDate(),
      'username'                   => new sfWidgetFormInput(),
      'estado'                     => new sfWidgetFormInput(),
      'activo'                     => new sfWidgetFormInput(),
      'vigencia'                   => new sfWidgetFormInputHidden(),
      'justificacion'              => new sfWidgetFormInput(),
      'interrupcion'               => new sfWidgetFormInput(),
      'id_original'                => new sfWidgetFormInput(),
      'comisionados_contrato_list' => new sfWidgetFormPropelSelectMany(array('model' => 'Terceros')),
      'destinos_comisionados_list' => new sfWidgetFormPropelSelectMany(array('model' => 'Terceros')),
      'comisionados_objs_list'     => new sfWidgetFormPropelSelectMany(array('model' => 'Terceros')),
      'tarifas_destino_list'       => new sfWidgetFormPropelSelectMany(array('model' => 'Terceros')),
      'comisionados_planta_list'   => new sfWidgetFormPropelSelectMany(array('model' => 'Terceros')),
    ));

    $this->setValidators(array(
      'id'                         => new sfValidatorPropelChoice(array('model' => 'Comisiones', 'column' => 'id', 'required' => false)),
      'codare'                     => new sfValidatorString(array('max_length' => 20, 'required' => false)),
      'tipcomi'                    => new sfValidatorPropelChoice(array('model' => 'TiposComision', 'column' => 'tipcomi', 'required' => false)),
      'codpro'                     => new sfValidatorString(array('max_length' => 20, 'required' => false)),
      'fecha'                      => new sfValidatorDate(array('required' => false)),
      'objetivo'                   => new sfValidatorString(array('max_length' => 600, 'required' => false)),
      'actividad'                  => new sfValidatorString(array('max_length' => 700, 'required' => false)),
      'observa'                    => new sfValidatorString(array('max_length' => 700, 'required' => false)),
      'created_at'                 => new sfValidatorDate(array('required' => false)),
      'update_at'                  => new sfValidatorDate(array('required' => false)),
      'username'                   => new sfValidatorNumber(array('required' => false)),
      'estado'                     => new sfValidatorNumber(array('required' => false)),
      'activo'                     => new sfValidatorNumber(array('required' => false)),
      'vigencia'                   => new sfValidatorPropelChoice(array('model' => 'Comisiones', 'column' => 'vigencia', 'required' => false)),
      'justificacion'              => new sfValidatorString(array('max_length' => 500, 'required' => false)),
      'interrupcion'               => new sfValidatorString(array('max_length' => 1, 'required' => false)),
      'id_original'                => new sfValidatorNumber(array('required' => false)),
      'comisionados_contrato_list' => new sfValidatorPropelChoiceMany(array('model' => 'Terceros', 'required' => false)),
      'destinos_comisionados_list' => new sfValidatorPropelChoiceMany(array('model' => 'Terceros', 'required' => false)),
      'comisionados_objs_list'     => new sfValidatorPropelChoiceMany(array('model' => 'Terceros', 'required' => false)),
      'tarifas_destino_list'       => new sfValidatorPropelChoiceMany(array('model' => 'Terceros', 'required' => false)),
      'comisionados_planta_list'   => new sfValidatorPropelChoiceMany(array('model' => 'Terceros', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('comisiones[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Comisiones';
  }


  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['comisionados_contrato_list']))
    {
      $values = array();
      foreach ($this->object->getComisionadosContratos() as $obj)
      {
        $values[] = $obj->getNitocc();
      }

      $this->setDefault('comisionados_contrato_list', $values);
    }

    if (isset($this->widgetSchema['destinos_comisionados_list']))
    {
      $values = array();
      foreach ($this->object->getDestinosComisionadoss() as $obj)
      {
        $values[] = $obj->getNitocc();
      }

      $this->setDefault('destinos_comisionados_list', $values);
    }

    if (isset($this->widgetSchema['comisionados_objs_list']))
    {
      $values = array();
      foreach ($this->object->getComisionadosObjss() as $obj)
      {
        $values[] = $obj->getNitocc();
      }

      $this->setDefault('comisionados_objs_list', $values);
    }

    if (isset($this->widgetSchema['tarifas_destino_list']))
    {
      $values = array();
      foreach ($this->object->getTarifasDestinos() as $obj)
      {
        $values[] = $obj->getNitocc();
      }

      $this->setDefault('tarifas_destino_list', $values);
    }

    if (isset($this->widgetSchema['comisionados_planta_list']))
    {
      $values = array();
      foreach ($this->object->getComisionadosPlantas() as $obj)
      {
        $values[] = $obj->getNitocc();
      }

      $this->setDefault('comisionados_planta_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveComisionadosContratoList($con);
    $this->saveDestinosComisionadosList($con);
    $this->saveComisionadosObjsList($con);
    $this->saveTarifasDestinoList($con);
    $this->saveComisionadosPlantaList($con);
  }

  public function saveComisionadosContratoList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['comisionados_contrato_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(ComisionadosContratoPeer::ID_COMISION, $this->object->getPrimaryKey());
    ComisionadosContratoPeer::doDelete($c, $con);

    $values = $this->getValue('comisionados_contrato_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new ComisionadosContrato();
        $obj->setIdComision($this->object->getPrimaryKey());
        $obj->setNitocc($value);
        $obj->save();
      }
    }
  }

  public function saveDestinosComisionadosList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['destinos_comisionados_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(DestinosComisionadosPeer::ID_COMISION, $this->object->getPrimaryKey());
    DestinosComisionadosPeer::doDelete($c, $con);

    $values = $this->getValue('destinos_comisionados_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new DestinosComisionados();
        $obj->setIdComision($this->object->getPrimaryKey());
        $obj->setNitocc($value);
        $obj->save();
      }
    }
  }

  public function saveComisionadosObjsList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['comisionados_objs_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(ComisionadosObjsPeer::ID_COMISION, $this->object->getPrimaryKey());
    ComisionadosObjsPeer::doDelete($c, $con);

    $values = $this->getValue('comisionados_objs_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new ComisionadosObjs();
        $obj->setIdComision($this->object->getPrimaryKey());
        $obj->setNitocc($value);
        $obj->save();
      }
    }
  }

  public function saveTarifasDestinoList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['tarifas_destino_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(TarifasDestinoPeer::ID_COMISION, $this->object->getPrimaryKey());
    TarifasDestinoPeer::doDelete($c, $con);

    $values = $this->getValue('tarifas_destino_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new TarifasDestino();
        $obj->setIdComision($this->object->getPrimaryKey());
        $obj->setNitocc($value);
        $obj->save();
      }
    }
  }

  public function saveComisionadosPlantaList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['comisionados_planta_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(ComisionadosPlantaPeer::ID_COMISION, $this->object->getPrimaryKey());
    ComisionadosPlantaPeer::doDelete($c, $con);

    $values = $this->getValue('comisionados_planta_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new ComisionadosPlanta();
        $obj->setIdComision($this->object->getPrimaryKey());
        $obj->setNitocc($value);
        $obj->save();
      }
    }
  }

}
