<commit_before><?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of DRMAnnexesForm
 *
 * @author mathurin
 */
class DRMAnnexesForm extends acCouchdbObjectForm {

    private $drm = null;
    private $detailsSortiesVrac = null;
    private $detailsSortiesExport = null;
    private $docTypesList = array();

    public function __construct(acCouchdbJson $object, $options = array(), $CSRFSecret = null) {
        $this->drm = $object;
        $this->getDocTypes();
        parent::__construct($this->drm, $options, $CSRFSecret);
    }

    public function configure() {
        foreach ($this->docTypesList as $docType) {
            $keyDebut = $docType . '_debut';
            $keyFin = $docType . '_fin';
            $keyNb = $docType . '_nb';

            $this->setWidget($keyDebut, new sfWidgetFormInputText());
            $this->setWidget($keyFin, new sfWidgetFormInputText());
            $this->setWidget($keyNb, new sfWidgetFormInputText());

            $this->setValidator($keyDebut, new sfValidatorString(array('required' => false)));
            $this->setValidator($keyFin, new sfValidatorString(array('required' => false)));
            $this->setValidator($keyNb, new sfValidatorString(array('required' => false)));

            $this->widgetSchema->setLabel($keyDebut, DRMClient::$drm_documents_daccompagnement[$docType] . ' début');
            $this->widgetSchema->setLabel($keyFin, DRMClient::$drm_documents_daccompagnement[$docType] . ' fin');
            $this->widgetSchema->setLabel($keyNb, DRMClient::$drm_documents_daccompagnement[$docType] . ' nombre de documents');
        }

        $observations = new DRMObservationsCollectionForm($this->drm);
        $this->embedForm('observationsProduits', $observations);

        $this->setWidget('paiement_douane_frequence', new sfWidgetFormChoice(array('expanded' => true, 'multiple' => false, 'choices' => $this->getPaiementDouaneFrequence())));
        $this->setValidator('paiement_douane_frequence', new sfValidatorChoice(array('required' => true, 'choices' => array_keys($this->getPaiementDouaneFrequence())), array('required' => "Aucune fréquence de paiement des droits douane n'a été choisie")));
        $this->widgetSchema->setLabel('paiement_douane_frequence', 'Fréquence de paiement');

        $this->setWidget('quantite_sucre', new sfWidgetFormInputFloat(array('float_format' => "%01.04f")));
        $this->widgetSchema->setLabel('quantite_sucre', 'Quantités de sucre (en quintaux)');
        $this->setValidator('quantite_sucre', new sfValidatorNumber(array('required' => false)));

        $this->setWidget('statistiques_jus', new sfWidgetFormInputFloat(array('float_format' => "%01.04f")));
        $this->setWidget('statistiques_mcr', new sfWidgetFormInputFloat(array('float_format' => "%01.04f")));
        $this->setWidget('statistiques_vinaigre', new sfWidgetFormInputFloat(array('float_format' => "%01.04f")));
        $this->widgetSchema->setLabel('statistiques_jus', 'Quantités de moûts de raisin transformées en jus de raisin');
        $this->widgetSchema->setLabel('statistiques_mcr', 'Quantités de moûts de raisin transformées en MCR');
        $this->widgetSchema->setLabel('statistiques_vinaigre', 'Quantités de moûts de raisin transformées en vinaigre');


        $this->setValidator('statistiques_jus', new sfValidatorNumber(array('required' => false)));
        $this->setValidator('statistiques_mcr', new sfValidatorNumber(array('required' => false)));
        $this->setValidator('statistiques_vinaigre', new sfValidatorNumber(array('required' => false)));

        foreach ($this->drm->getProduits() as $produit) {
            $genre = $produit->getConfig()->getGenre();
            if($genre->getKey() == 'DEFAUT') { continue; }
            $droit = $genre->getDroitDouane($this->drm->getFirstDayOfPeriode());
            $genreKey = DRMDroits::$correspondanceGenreKey[$genre->getKey()];

            $this->setWidget('cumul_' . $genreKey, new sfWidgetFormInputFloat(array('float_format' => "%d")));
            $this->setValidator('cumul_' . $genreKey, new sfValidatorNumber(array('required' => false)));
            $this->widgetSchema->setLabel('cumul_' . $genreKey, $droit->libelle . ' (' . $droit->code . ') :');
        }

        $this->embedForm('releve_non_apurement', new DRMReleveNonApurementItemsForm($this->drm->getReleveNonApurement()));
        $this->widgetSchema->setNameFormat('drmAnnexesForm[%s]');
    }

    protected function doUpdateObject($values) {
        parent::doUpdateObject($values);
        foreach ($this->docTypesList as $docType) {
            $this->drm->getOrAdd('documents_annexes')->getOrAdd($docType)->debut = $values[$docType . '_debut'];
            $this->drm->getOrAdd('documents_annexes')->getOrAdd($docType)->fin = $values[$docType . '_fin'];
            $this->drm->getOrAdd('documents_annexes')->getOrAdd($docType)->nb = $values[$docType . '_nb'];
        }
        foreach ($this->getEmbeddedForms() as $key => $releveNonApurementForm) {
          if($key!="observationsProduits"){
            $releveNonApurementForm->updateObject($values[$key]);
            }
        }

        $paiement_douane_frequence = $values['paiement_douane_frequence'];

        $this->drm->getSociete()->add('paiement_douane_frequence', $paiement_douane_frequence);
        if ($paiement_douane_frequence == DRMPaiement::FREQUENCE_ANNUELLE) {
            foreach ($this->drm->getProduits() as $produit) {
                $genre = $produit->getConfig()->getGenre();
                $genreKey = DRMDroits::$correspondanceGenreKey[$genre->getKey()];
                $localCumul = $values['cumul_' . $genreKey];

                if ($localCumul && $localCumul > 0) {
                    $this->drm->getOrAdd('droits')->getOrAdd('douane')->getOrAdd($genreKey)->set('report', $localCumul);
                }
            }
        }

        $this->drm->declaratif->statistiques->jus = $values['statistiques_jus'];
        $this->drm->declaratif->statistiques->mcr = $values['statistiques_mcr'];
        $this->drm->declaratif->statistiques->vinaigre = $values['statistiques_vinaigre'];

        if (isset($values['quantite_sucre'])) {
          $this->drm->add('quantite_sucre', $values['quantite_sucre']);
        }

        if ($observations = $values['observationsProduits']) {
          foreach ($observations as $hash => $observation) {
            $this->drm->addObservationProduit($hash, $observation['observations']);
          }
        }

        $this->drm->etape = DRMClient::ETAPE_VALIDATION;
        $this->drm->save();

        $societe = $this->drm->getEtablissement()->getSociete();
        $societe->add('paiement_douane_frequence', $paiement_douane_frequence);
        $societe->save();
    }

    public function updateDefaultsFromObject() {
        parent::updateDefaultsFromObject();<commit_msg>Positif ou strictement égal à 0<commit_after><?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of DRMAnnexesForm
 *
 * @author mathurin
 */
class DRMAnnexesForm extends acCouchdbObjectForm {

    private $drm = null;
    private $detailsSortiesVrac = null;
    private $detailsSortiesExport = null;
    private $docTypesList = array();

    public function __construct(acCouchdbJson $object, $options = array(), $CSRFSecret = null) {
        $this->drm = $object;
        $this->getDocTypes();
        parent::__construct($this->drm, $options, $CSRFSecret);
    }

    public function configure() {
        foreach ($this->docTypesList as $docType) {
            $keyDebut = $docType . '_debut';
            $keyFin = $docType . '_fin';
            $keyNb = $docType . '_nb';

            $this->setWidget($keyDebut, new sfWidgetFormInputText());
            $this->setWidget($keyFin, new sfWidgetFormInputText());
            $this->setWidget($keyNb, new sfWidgetFormInputText());

            $this->setValidator($keyDebut, new sfValidatorString(array('required' => false)));
            $this->setValidator($keyFin, new sfValidatorString(array('required' => false)));
            $this->setValidator($keyNb, new sfValidatorString(array('required' => false)));

            $this->widgetSchema->setLabel($keyDebut, DRMClient::$drm_documents_daccompagnement[$docType] . ' début');
            $this->widgetSchema->setLabel($keyFin, DRMClient::$drm_documents_daccompagnement[$docType] . ' fin');
            $this->widgetSchema->setLabel($keyNb, DRMClient::$drm_documents_daccompagnement[$docType] . ' nombre de documents');
        }

        $observations = new DRMObservationsCollectionForm($this->drm);
        $this->embedForm('observationsProduits', $observations);

        $this->setWidget('paiement_douane_frequence', new sfWidgetFormChoice(array('expanded' => true, 'multiple' => false, 'choices' => $this->getPaiementDouaneFrequence())));
        $this->setValidator('paiement_douane_frequence', new sfValidatorChoice(array('required' => true, 'choices' => array_keys($this->getPaiementDouaneFrequence())), array('required' => "Aucune fréquence de paiement des droits douane n'a été choisie")));
        $this->widgetSchema->setLabel('paiement_douane_frequence', 'Fréquence de paiement');

        $this->setWidget('quantite_sucre', new sfWidgetFormInputFloat(array('float_format' => "%01.04f")));
        $this->widgetSchema->setLabel('quantite_sucre', 'Quantités de sucre (en quintaux)');
        $this->setValidator('quantite_sucre', new sfValidatorNumber(array('required' => false)));

        $this->setWidget('statistiques_jus', new sfWidgetFormInputFloat(array('float_format' => "%01.04f")));
        $this->setWidget('statistiques_mcr', new sfWidgetFormInputFloat(array('float_format' => "%01.04f")));
        $this->setWidget('statistiques_vinaigre', new sfWidgetFormInputFloat(array('float_format' => "%01.04f")));
        $this->widgetSchema->setLabel('statistiques_jus', 'Quantités de moûts de raisin transformées en jus de raisin');
        $this->widgetSchema->setLabel('statistiques_mcr', 'Quantités de moûts de raisin transformées en MCR');
        $this->widgetSchema->setLabel('statistiques_vinaigre', 'Quantités de moûts de raisin transformées en vinaigre');


        $this->setValidator('statistiques_jus', new sfValidatorNumber(array('required' => false)));
        $this->setValidator('statistiques_mcr', new sfValidatorNumber(array('required' => false)));
        $this->setValidator('statistiques_vinaigre', new sfValidatorNumber(array('required' => false)));

        foreach ($this->drm->getProduits() as $produit) {
            $genre = $produit->getConfig()->getGenre();
            if($genre->getKey() == 'DEFAUT') { continue; }
            $droit = $genre->getDroitDouane($this->drm->getFirstDayOfPeriode());
            $genreKey = DRMDroits::$correspondanceGenreKey[$genre->getKey()];

            $this->setWidget('cumul_' . $genreKey, new sfWidgetFormInputFloat(array('float_format' => "%d")));
            $this->setValidator('cumul_' . $genreKey, new sfValidatorNumber(array('required' => false)));
            $this->widgetSchema->setLabel('cumul_' . $genreKey, $droit->libelle . ' (' . $droit->code . ') :');
        }

        $this->embedForm('releve_non_apurement', new DRMReleveNonApurementItemsForm($this->drm->getReleveNonApurement()));
        $this->widgetSchema->setNameFormat('drmAnnexesForm[%s]');
    }

    protected function doUpdateObject($values) {
        parent::doUpdateObject($values);
        foreach ($this->docTypesList as $docType) {
            $this->drm->getOrAdd('documents_annexes')->getOrAdd($docType)->debut = $values[$docType . '_debut'];
            $this->drm->getOrAdd('documents_annexes')->getOrAdd($docType)->fin = $values[$docType . '_fin'];
            $this->drm->getOrAdd('documents_annexes')->getOrAdd($docType)->nb = $values[$docType . '_nb'];
        }
        foreach ($this->getEmbeddedForms() as $key => $releveNonApurementForm) {
          if($key!="observationsProduits"){
            $releveNonApurementForm->updateObject($values[$key]);
            }
        }

        $paiement_douane_frequence = $values['paiement_douane_frequence'];

        $this->drm->getSociete()->add('paiement_douane_frequence', $paiement_douane_frequence);
        if ($paiement_douane_frequence == DRMPaiement::FREQUENCE_ANNUELLE) {
            foreach ($this->drm->getProduits() as $produit) {
                $genre = $produit->getConfig()->getGenre();
                $genreKey = DRMDroits::$correspondanceGenreKey[$genre->getKey()];
                $localCumul = $values['cumul_' . $genreKey];
                if (!($localCumul < 0) && !is_null($localCumul)) {
                    $this->drm->getOrAdd('droits')->getOrAdd('douane')->getOrAdd($genreKey)->set('report', $localCumul);
                }
            }
        }

        $this->drm->declaratif->statistiques->jus = $values['statistiques_jus'];
        $this->drm->declaratif->statistiques->mcr = $values['statistiques_mcr'];
        $this->drm->declaratif->statistiques->vinaigre = $values['statistiques_vinaigre'];

        if (isset($values['quantite_sucre'])) {
          $this->drm->add('quantite_sucre', $values['quantite_sucre']);
        }

        if ($observations = $values['observationsProduits']) {
          foreach ($observations as $hash => $observation) {
            $this->drm->addObservationProduit($hash, $observation['observations']);
          }
        }

        $this->drm->etape = DRMClient::ETAPE_VALIDATION;
        $this->drm->save();

        $societe = $this->drm->getEtablissement()->getSociete();
        $societe->add('paiement_douane_frequence', $paiement_douane_frequence);
        $societe->save();
    }

    public function updateDefaultsFromObject() {
        parent::updateDefaultsFromObject();
{'fraction_non_alphanumeric': 0.1612352355201366, 'fraction_numerical': 0.0024903941938238224, 'mean_word_length': 3.6647859276468635, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 272, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6540635', 'n_tokens_mistral': 4499, 'n_tokens_neox': 4318, 'n_words': 648}