<?php
//$Id$ 
//gen openMairie le 28/01/2026 09:37

require_once "../obj/om_dbform.class.php";

class tarif_gen extends om_dbform {

    protected $_absolute_class_name = "tarif";

    var $table = "tarif";
    var $clePrimaire = "tarif";
    var $typeCle = "N";
    var $required_field = array(
        "annee",
        "monnaie",
        "montant",
        "om_collectivite",
        "origine",
        "tarif",
        "terme"
    );
    var $unique_key = array(
      array("annee","duree","nature","om_collectivite","origine","sepulture_type","terme"),
    );
    var $foreign_keys_extended = array(
        "om_collectivite" => array("om_collectivite", ),
        "sepulture_type" => array("sepulture_type", ),
    );
    
    /**
     *
     * @return string
     */
    function get_default_libelle() {
        return $this->getVal($this->clePrimaire)."&nbsp;".$this->getVal("annee");
    }

    /**
     *
     * @return array
     */
    function get_var_sql_forminc__champs() {
        return array(
            "tarif",
            "annee",
            "origine",
            "terme",
            "duree",
            "nature",
            "sepulture_type",
            "montant",
            "monnaie",
            "om_collectivite",
        );
    }

    /**
     *
     * @return string
     */
    function get_var_sql_forminc__sql_om_collectivite() {
        return "SELECT om_collectivite.om_collectivite, om_collectivite.libelle FROM ".DB_PREFIXE."om_collectivite ORDER BY om_collectivite.libelle ASC";
    }

    /**
     *
     * @return string
     */
    function get_var_sql_forminc__sql_om_collectivite_by_id() {
        return "SELECT om_collectivite.om_collectivite, om_collectivite.libelle FROM ".DB_PREFIXE."om_collectivite WHERE om_collectivite = <idx>";
    }

    /**
     *
     * @return string
     */
    function get_var_sql_forminc__sql_sepulture_type() {
        return "SELECT sepulture_type.sepulture_type, sepulture_type.libelle FROM ".DB_PREFIXE."sepulture_type WHERE ((sepulture_type.om_validite_debut IS NULL AND (sepulture_type.om_validite_fin IS NULL OR sepulture_type.om_validite_fin > CURRENT_DATE)) OR (sepulture_type.om_validite_debut <= CURRENT_DATE AND (sepulture_type.om_validite_fin IS NULL OR sepulture_type.om_validite_fin > CURRENT_DATE))) ORDER BY sepulture_type.libelle ASC";
    }

    /**
     *
     * @return string
     */
    function get_var_sql_forminc__sql_sepulture_type_by_id() {
        return "SELECT sepulture_type.sepulture_type, sepulture_type.libelle FROM ".DB_PREFIXE."sepulture_type WHERE sepulture_type = <idx>";
    }




    function setvalF($val = array()) {
        //affectation valeur formulaire
        if (!is_numeric($val['tarif'])) {
            $this->valF['tarif'] = ""; // -> requis
        } else {
            $this->valF['tarif'] = $val['tarif'];
        }
        if (!is_numeric($val['annee'])) {
            $this->valF['annee'] = ""; // -> requis
        } else {
            $this->valF['annee'] = $val['annee'];
        }
        $this->valF['origine'] = $val['origine'];
        $this->valF['terme'] = $val['terme'];
        if (!is_numeric($val['duree'])) {
            $this->valF['duree'] = NULL;
        } else {
            $this->valF['duree'] = $val['duree'];
        }
        if ($val['nature'] == "") {
            $this->valF['nature'] = NULL;
        } else {
            $this->valF['nature'] = $val['nature'];
        }
        if (!is_numeric($val['sepulture_type'])) {
            $this->valF['sepulture_type'] = NULL;
        } else {
            $this->valF['sepulture_type'] = $val['sepulture_type'];
        }
        if (!is_numeric($val['montant'])) {
            $this->valF['montant'] = ""; // -> requis
        } else {
            $this->valF['montant'] = $val['montant'];
        }
        $this->valF['monnaie'] = $val['monnaie'];
        if (!is_numeric($val['om_collectivite'])) {
            $this->valF['om_collectivite'] = ""; // -> requis
        } else {
            if($_SESSION['niveau']==1) {
                $this->valF['om_collectivite'] = $_SESSION['collectivite'];
            } else {
                $this->valF['om_collectivite'] = $val['om_collectivite'];
            }
        }
    }

    //=================================================
    //cle primaire automatique [automatic primary key]
    //==================================================

    function setId(&$dnu1 = null) {
    //numero automatique
        $qres = $this->f->get_next_id_from_db_sequence(DB_PREFIXE.$this->table, array("origin" => __METHOD__));
        $this->valF[$this->clePrimaire] = $qres['result'];
    }

    function setValFAjout($val = array()) {
    //numero automatique -> pas de controle ajout cle primaire
    }

    function verifierAjout($val = array(), &$dnu1 = null) {
    //numero automatique -> pas de verfication de cle primaire
    }

    //==========================
    // Formulaire  [form]
    //==========================
    /**
     *
     */
    function setType(&$form, $maj) {
        // Récupération du mode de l'action
        $crud = $this->get_action_crud($maj);

        // MODE AJOUTER
        if ($maj == 0 || $crud == 'create') {
            $form->setType("tarif", "hidden");
            $form->setType("annee", "text");
            $form->setType("origine", "text");
            $form->setType("terme", "text");
            $form->setType("duree", "text");
            $form->setType("nature", "text");
            if ($this->is_in_context_of_foreign_key("sepulture_type", $this->retourformulaire)) {
                $form->setType("sepulture_type", "selecthiddenstatic");
            } else {
                $form->setType("sepulture_type", "select");
            }
            $form->setType("montant", "text");
            $form->setType("monnaie", "text");
            if ($this->is_in_context_of_foreign_key("om_collectivite", $this->retourformulaire)) {
                if($_SESSION["niveau"] == 2) {
                    $form->setType("om_collectivite", "selecthiddenstatic");
                } else {
                    $form->setType("om_collectivite", "hidden");
                }
            } else {
                if($_SESSION["niveau"] == 2) {
                    $form->setType("om_collectivite", "select");
                } else {
                    $form->setType("om_collectivite", "hidden");
                }
            }
        }

        // MDOE MODIFIER
        if ($maj == 1 || $crud == 'update') {
            $form->setType("tarif", "hiddenstatic");
            $form->setType("annee", "text");
            $form->setType("origine", "text");
            $form->setType("terme", "text");
            $form->setType("duree", "text");
            $form->setType("nature", "text");
            if ($this->is_in_context_of_foreign_key("sepulture_type", $this->retourformulaire)) {
                $form->setType("sepulture_type", "selecthiddenstatic");
            } else {
                $form->setType("sepulture_type", "select");
            }
            $form->setType("montant", "text");
            $form->setType("monnaie", "text");
            if ($this->is_in_context_of_foreign_key("om_collectivite", $this->retourformulaire)) {
                if($_SESSION["niveau"] == 2) {
                    $form->setType("om_collectivite", "selecthiddenstatic");
                } else {
                    $form->setType("om_collectivite", "hidden");
                }
            } else {
                if($_SESSION["niveau"] == 2) {
                    $form->setType("om_collectivite", "select");
                } else {
                    $form->setType("om_collectivite", "hidden");
                }
            }
        }

        // MODE SUPPRIMER
        if ($maj == 2 || $crud == 'delete') {
            $form->setType("tarif", "hiddenstatic");
            $form->setType("annee", "hiddenstatic");
            $form->setType("origine", "hiddenstatic");
            $form->setType("terme", "hiddenstatic");
            $form->setType("duree", "hiddenstatic");
            $form->setType("nature", "hiddenstatic");
            $form->setType("sepulture_type", "selectstatic");
            $form->setType("montant", "hiddenstatic");
            $form->setType("monnaie", "hiddenstatic");
            if ($_SESSION["niveau"] == 2) {
                $form->setType("om_collectivite", "selectstatic");
            } else {
                $form->setType("om_collectivite", "hidden");
            }
        }

        // MODE CONSULTER
        if ($maj == 3 || $crud == 'read') {
            $form->setType("tarif", "static");
            $form->setType("annee", "static");
            $form->setType("origine", "static");
            $form->setType("terme", "static");
            $form->setType("duree", "static");
            $form->setType("nature", "static");
            $form->setType("sepulture_type", "selectstatic");
            $form->setType("montant", "static");
            $form->setType("monnaie", "static");
            if ($this->is_in_context_of_foreign_key("om_collectivite", $this->retourformulaire)) {
                if($_SESSION["niveau"] == 2) {
                    $form->setType("om_collectivite", "selectstatic");
                } else {
                    $form->setType("om_collectivite", "hidden");
                }
            } else {
                if($_SESSION["niveau"] == 2) {
                    $form->setType("om_collectivite", "selectstatic");
                } else {
                    $form->setType("om_collectivite", "hidden");
                }
            }
        }

    }


    function setOnchange(&$form, $maj) {
    //javascript controle client
        $form->setOnchange('tarif','VerifNum(this)');
        $form->setOnchange('annee','VerifNum(this)');
        $form->setOnchange('duree','VerifFloat(this)');
        $form->setOnchange('sepulture_type','VerifNum(this)');
        $form->setOnchange('montant','VerifFloat(this)');
        $form->setOnchange('om_collectivite','VerifNum(this)');
    }
    /**
     * Methode setTaille
     */
    function setTaille(&$form, $maj) {
        $form->setTaille("tarif", 11);
        $form->setTaille("annee", 11);
        $form->setTaille("origine", 30);
        $form->setTaille("terme", 15);
        $form->setTaille("duree", 20);
        $form->setTaille("nature", 15);
        $form->setTaille("sepulture_type", 11);
        $form->setTaille("montant", 30);
        $form->setTaille("monnaie", 30);
        $form->setTaille("om_collectivite", 11);
    }

    /**
     * Methode setMax
     */
    function setMax(&$form, $maj) {
        $form->setMax("tarif", 11);
        $form->setMax("annee", 11);
        $form->setMax("origine", 50);
        $form->setMax("terme", 15);
        $form->setMax("duree", 20);
        $form->setMax("nature", 15);
        $form->setMax("sepulture_type", 11);
        $form->setMax("montant", 720898);
        $form->setMax("monnaie", 30);
        $form->setMax("om_collectivite", 11);
    }


    function setLib(&$form, $maj) {
    //libelle des champs
        $form->setLib('tarif', __('tarif'));
        $form->setLib('annee', __('annee'));
        $form->setLib('origine', __('origine'));
        $form->setLib('terme', __('terme'));
        $form->setLib('duree', __('duree'));
        $form->setLib('nature', __('nature'));
        $form->setLib('sepulture_type', __('sepulture_type'));
        $form->setLib('montant', __('montant'));
        $form->setLib('monnaie', __('monnaie'));
        $form->setLib('om_collectivite', __('om_collectivite'));
    }
    /**
     *
     */
    function setSelect(&$form, $maj, &$dnu1 = null, $dnu2 = null) {

        // om_collectivite
        $this->init_select(
            $form, 
            $this->f->get_db_reference(),
            $maj,
            null,
            "om_collectivite",
            $this->get_var_sql_forminc__sql("om_collectivite"),
            $this->get_var_sql_forminc__sql("om_collectivite_by_id"),
            false
        );
        // sepulture_type
        $this->init_select(
            $form, 
            $this->f->get_db_reference(),
            $maj,
            null,
            "sepulture_type",
            $this->get_var_sql_forminc__sql("sepulture_type"),
            $this->get_var_sql_forminc__sql("sepulture_type_by_id"),
            true
        );
    }


    function setVal(&$form, $maj, $validation, &$dnu1 = null, $dnu2 = null) {
        if($validation==0 and $maj==0 and $_SESSION['niveau']==1) {
            $form->setVal('om_collectivite', $_SESSION['collectivite']);
        }// fin validation
        $this->set_form_default_values($form, $maj, $validation);
    }// fin setVal

    //==================================
    // sous Formulaire
    //==================================
    

    function setValsousformulaire(&$form, $maj, $validation, $idxformulaire, $retourformulaire, $typeformulaire, &$dnu1 = null, $dnu2 = null) {
        $this->retourformulaire = $retourformulaire;
        if($validation==0 and $maj==0 and $_SESSION['niveau']==1) {
            $form->setVal('om_collectivite', $_SESSION['collectivite']);
        }// fin validation
        if($validation == 0) {
            if($this->is_in_context_of_foreign_key('om_collectivite', $this->retourformulaire))
                $form->setVal('om_collectivite', $idxformulaire);
            if($this->is_in_context_of_foreign_key('sepulture_type', $this->retourformulaire))
                $form->setVal('sepulture_type', $idxformulaire);
        }// fin validation
        $this->set_form_default_values($form, $maj, $validation);
    }// fin setValsousformulaire

    //==================================
    // cle secondaire
    //==================================
    

}
