<?php

/**
 * KataoSupplierInvoice form base class.
 *
 * @package    form
 * @subpackage katao_supplier_invoice
 * @version    SVN: $Id: sfPropelFormGeneratedTemplate.php 15484 2009-02-13 13:13:51Z fabien $
 */
class BaseKataoSupplierInvoiceForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                                => new sfWidgetFormInputHidden(),
      'katao_supplier_id'                 => new sfWidgetFormPropelSelect(array('model' => 'KataoSupplier', 'add_empty' => false)),
      'katao_period_id'                   => new sfWidgetFormPropelSelect(array('model' => 'KataoPeriod', 'add_empty' => false)),
      'status'                            => new sfWidgetFormInput(),
      'supplier_name'                     => new sfWidgetFormInput(),
      'supplier_email'                    => new sfWidgetFormInput(),
      'supplier_address1'                 => new sfWidgetFormInput(),
      'supplier_address2'                 => new sfWidgetFormInput(),
      'supplier_zip'                      => new sfWidgetFormInput(),
      'supplier_city'                     => new sfWidgetFormInput(),
      'katao_name'                        => new sfWidgetFormInput(),
      'katao_address'                     => new sfWidgetFormTextarea(),
      'katao_siret_number'                => new sfWidgetFormInput(),
      'katao_rcs_number'                  => new sfWidgetFormInput(),
      'katao_capital'                     => new sfWidgetFormInput(),
      'reference'                         => new sfWidgetFormInput(),
      'invoice_date'                      => new sfWidgetFormDateTime(),
      'due_date'                          => new sfWidgetFormDateTime(),
      'payment_date'                      => new sfWidgetFormDateTime(),
      'payment_mode'                      => new sfWidgetFormInput(),
      'payment_details'                   => new sfWidgetFormInput(),
      'payment_amount_euro'               => new sfWidgetFormInput(),
      'payment_amount_sol'                => new sfWidgetFormInput(),
      'taux0_bases_ht'                    => new sfWidgetFormInput(),
      'taux0_discount_ht'                 => new sfWidgetFormInput(),
      'taux0_amount'                      => new sfWidgetFormInput(),
      'taux1_bases_ht'                    => new sfWidgetFormInput(),
      'taux1_discount_ht'                 => new sfWidgetFormInput(),
      'taux1_amount'                      => new sfWidgetFormInput(),
      'taux2_bases_ht'                    => new sfWidgetFormInput(),
      'taux2_discount_ht'                 => new sfWidgetFormInput(),
      'taux2_amount'                      => new sfWidgetFormInput(),
      'total_ht'                          => new sfWidgetFormInput(),
      'total_tva'                         => new sfWidgetFormInput(),
      'comment'                           => new sfWidgetFormTextarea(),
      'fees_shipping_ht'                  => new sfWidgetFormInput(),
      'fees_shipping_rate'                => new sfWidgetFormInput(),
      'fees_shipping_tva'                 => new sfWidgetFormInput(),
      'fees_billing_ht'                   => new sfWidgetFormInput(),
      'fees_billing_rate'                 => new sfWidgetFormInput(),
      'fees_billing_tva'                  => new sfWidgetFormInput(),
      'created_at'                        => new sfWidgetFormDateTime(),
      'updated_at'                        => new sfWidgetFormDateTime(),
      'katao_supplier_invoice_order_list' => new sfWidgetFormPropelSelectMany(array('model' => 'KataoOrder')),
    ));

    $this->setValidators(array(
      'id'                                => new sfValidatorPropelChoice(array('model' => 'KataoSupplierInvoice', 'column' => 'id', 'required' => false)),
      'katao_supplier_id'                 => new sfValidatorPropelChoice(array('model' => 'KataoSupplier', 'column' => 'id')),
      'katao_period_id'                   => new sfValidatorPropelChoice(array('model' => 'KataoPeriod', 'column' => 'id')),
      'status'                            => new sfValidatorInteger(),
      'supplier_name'                     => new sfValidatorString(array('max_length' => 255)),
      'supplier_email'                    => new sfValidatorString(array('max_length' => 255)),
      'supplier_address1'                 => new sfValidatorString(array('max_length' => 255)),
      'supplier_address2'                 => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'supplier_zip'                      => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'supplier_city'                     => new sfValidatorString(array('max_length' => 255)),
      'katao_name'                        => new sfValidatorString(array('max_length' => 255)),
      'katao_address'                     => new sfValidatorString(array('required' => false)),
      'katao_siret_number'                => new sfValidatorString(array('max_length' => 14)),
      'katao_rcs_number'                  => new sfValidatorString(array('max_length' => 255)),
      'katao_capital'                     => new sfValidatorInteger(),
      'reference'                         => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'invoice_date'                      => new sfValidatorDateTime(),
      'due_date'                          => new sfValidatorDateTime(),
      'payment_date'                      => new sfValidatorDateTime(array('required' => false)),
      'payment_mode'                      => new sfValidatorInteger(),
      'payment_details'                   => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'payment_amount_euro'               => new sfValidatorNumber(array('required' => false)),
      'payment_amount_sol'                => new sfValidatorNumber(array('required' => false)),
      'taux0_bases_ht'                    => new sfValidatorNumber(array('required' => false)),
      'taux0_discount_ht'                 => new sfValidatorNumber(array('required' => false)),
      'taux0_amount'                      => new sfValidatorNumber(array('required' => false)),
      'taux1_bases_ht'                    => new sfValidatorNumber(array('required' => false)),
      'taux1_discount_ht'                 => new sfValidatorNumber(array('required' => false)),
      'taux1_amount'                      => new sfValidatorNumber(array('required' => false)),
      'taux2_bases_ht'                    => new sfValidatorNumber(array('required' => false)),
      'taux2_discount_ht'                 => new sfValidatorNumber(array('required' => false)),
      'taux2_amount'                      => new sfValidatorNumber(array('required' => false)),
      'total_ht'                          => new sfValidatorNumber(array('required' => false)),
      'total_tva'                         => new sfValidatorNumber(array('required' => false)),
      'comment'                           => new sfValidatorString(array('required' => false)),
      'fees_shipping_ht'                  => new sfValidatorNumber(array('required' => false)),
      'fees_shipping_rate'                => new sfValidatorNumber(array('required' => false)),
      'fees_shipping_tva'                 => new sfValidatorNumber(array('required' => false)),
      'fees_billing_ht'                   => new sfValidatorNumber(array('required' => false)),
      'fees_billing_rate'                 => new sfValidatorNumber(array('required' => false)),
      'fees_billing_tva'                  => new sfValidatorNumber(array('required' => false)),
      'created_at'                        => new sfValidatorDateTime(array('required' => false)),
      'updated_at'                        => new sfValidatorDateTime(array('required' => false)),
      'katao_supplier_invoice_order_list' => new sfValidatorPropelChoiceMany(array('model' => 'KataoOrder', 'required' => false)),
    ));

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

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

    parent::setup();
  }

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


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

    if (isset($this->widgetSchema['katao_supplier_invoice_order_list']))
    {
      $values = array();
      foreach ($this->object->getKataoSupplierInvoiceOrders() as $obj)
      {
        $values[] = $obj->getKataoOrderId();
      }

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

  }

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

    $this->saveKataoSupplierInvoiceOrderList($con);
  }

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

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

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

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

    $values = $this->getValue('katao_supplier_invoice_order_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new KataoSupplierInvoiceOrder();
        $obj->setKataoSupplierInvoiceId($this->object->getPrimaryKey());
        $obj->setKataoOrderId($value);
        $obj->save();
      }
    }
  }

}
