Subversion Repositories portal2

Rev

Rev 710 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
/*  emg - Energy Monitoring & Graphing
*  Copyright (C) 2006-2012 Romain Lievin
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software Foundation,
*  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

?>

<?php
/*
* List available variables to monito
*
* Called from: form_cost_add.php
*
* Input args (GET):
* - device_name
* - sensor_name
*
* Inputs args (POST): none
*
* Output args:
* - device_name
* - sensor_name
* - var_name
*
* Call: form_cost_list.php
*
*/

?>

<?php
include_once('auth_check.php');
?>

<div id="page_cost_add_submit">
        <h1>
                <?php echo _('Add Cost'); ?>
        </h1>
</div>

<?php
$ok = false;
$error = new Error();

if(!isset($_POST['form_token'], $_POST['form_datum_name'],
                $_POST['form_start_day'], $_POST['form_start_month'],
                $_POST['form_start_year'], $_POST['form_end_day'], $_POST['form_end_month'],
                $_POST['form_end_year'], $_POST['form_cost_subscription'], $_POST['form_cost_connsumption'],
                $_POST['form_cost_vat']))
{
        $error->push(_('One or more fields are missing.'));
}
else
if( $_POST['form_token'] != $_SESSION['form_token'])
{
        $error->push(_('Internal error: invalid token.'));
}
else
if(isset($_POST['form_cancel']))
{
        $error->push(_('Operation aborted.'));
}
else
if(filter_var($_POST['form_cost_subscription'], FILTER_VALIDATE_FLOAT) === false)
{
        //echo $_POST['form_cost_subscription'];
        $error->push(_('Incorrect numeric value.'));
}
else
if(filter_var($_POST['form_cost_connsumption'], FILTER_VALIDATE_FLOAT) === false)
{
        //echo $_POST['form_cost_subscription'];
        $error->push(_('Incorrect numeric value.'));
}
else
if(filter_var($_POST['form_cost_vat'], FILTER_VALIDATE_FLOAT) === false)
{
        //echo $_POST['form_cost_subscription'];
        $error->push(_('Incorrect numeric value.'));
}
else
if(checkdate($_POST['form_start_month'], $_POST['form_start_day'], $_POST['form_start_year']) == false)
{
        $error->push(_('Invalid start date.'));
}
else
if(checkdate($_POST['form_end_month'], $_POST['form_end_day'], $_POST['form_end_year']) == false)
{
        $error->push(_('Invalid end date.'));
}
else
{
        // Get args
        $form_datum_name = filter_var($_POST['form_datum_name'], FILTER_SANITIZE_STRING);
        $form_cost_subscription= $_POST['form_cost_subscription'];              //$form_cost_subscription = filter_var($_POST['form_cost_subscription'], FILTER_SANITIZE_NUMBER_FLOAT);
        $form_cost_connsumption   = $_POST['form_cost_connsumption'];                                           //$form_cost_connsumption = filter_var($_POST['form_cost_connsumption'], FILTER_SANITIZE_NUMBER_FLOAT);
        $form_cost_vat   = filter_var($_POST['form_cost_vat'], FILTER_SANITIZE_NUMBER_FLOAT);
        $form_start_year = filter_var($_POST['form_start_year'], FILTER_SANITIZE_NUMBER_INT);
        $form_start_month= filter_var($_POST['form_start_month'], FILTER_SANITIZE_NUMBER_INT);
        $form_start_day  = filter_var($_POST['form_start_day'], FILTER_SANITIZE_NUMBER_INT);
        $form_end_year   = filter_var($_POST['form_end_year'], FILTER_SANITIZE_NUMBER_INT);
        $form_end_month  = filter_var($_POST['form_end_month'], FILTER_SANITIZE_NUMBER_INT);
        $form_end_day    = filter_var($_POST['form_end_day'], FILTER_SANITIZE_NUMBER_INT);

        // Create cost object
        $date_start      = new DateTime("$form_start_year-$form_start_month-$form_start_day");
        $date_end        = new DateTime("$form_end_year-$form_end_month-$form_end_day");

        $d               = $date_start->diff($date_end, false);
        if($d->invert)
        {
                $error->push(_('Dates are not valid.'));
                $ok      = false;
                goto error;
        }
        else
        {
                // we should check for date overlap
        }
        $interval = new Interval($date_start, $date_end);
        $tag      = $interval->getTag();

        if(abs($form_cost_subscription ) < 0.00001 && abs($form_cost_connsumption ) < 0.00001)
        {
                $error->push(_('Subscription and consumption can\'t be null altogether!'));
                $ok      = false;
                goto error;
        }

        $type = Cost::BILL_NONE;
        if(abs($form_cost_subscription ) > 0.00001)
        $type |= Cost::BILL_SUBSCRIPTION;
        if(abs($form_cost_connsumption ) > 0.00001)
        $type |= Cost::BILL_CONSUMPTION;

        $cost      = new Cost($tag, $type);
        $cost->setInterval($interval);
        $cost->setSubscription($form_cost_subscription);
        $cost->setConsumption($form_cost_connsumption);
        $cost->setType($type);
        $cost->setVat($form_cost_vat);
        //var_dump($cost);

        // Retrieve tracker to attach cost object
        $data      = $_SESSION['data'];

        $datum_key = $data->searchItem('name', $form_datum_name);
        $datum     = $data->getItem($datum_key);

        $tracker   = $datum->getPath();
        $costs     = $datum->getCosts();

        // And add object
        //echo $cost->getName();
        $result    = $costs->addItem($tag, $cost);
        if($result == true)
        {
                $error->push(_('Cost added.'));
                $ok      = true;
        }
        else
        {
                $error->push(_('Cost already exists. Use another range.'));
        }

        /*** save configuration contents ***/
        config_save();
}
error:
        echo $error->display();

/* Redirection */
if(isset($_POST['form_cancel']) || ($ok === true))
{
        $id = link_get_id_from_filename("form_cost_list.php");
        $refresh = $_SESSION['refresh_redirection'];
        echo "<meta http-equiv=\"refresh\" content=\"$refresh; URL=index.php?id_page=$id\">";
}
else if($ok == false)
{
        $id = link_get_id_from_filename("form_cost_add.php");
        $refresh = $_SESSION['refresh_redirection'];
        echo "<meta http-equiv=\"refresh\" content=\"$refresh; URL=index.php?id_page=$id\">";
}

?>