noalyss-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Noalyss-commit] [noalyss] 04/162: Currency : add test file + database f


From: Dany De Bontridder
Subject: [Noalyss-commit] [noalyss] 04/162: Currency : add test file + database file + mtable
Date: Sat, 11 Jul 2020 13:23:17 -0400 (EDT)

sparkyx pushed a commit to annotated tag E-4
in repository noalyss.

commit 943a4a9ae86966f1fd08dcb90459e3f1260d739e
Author: Dany De Bontridder <danydb@noalyss.eu>
AuthorDate: Sat Apr 21 20:34:48 2018 +0200

    Currency : add test file + database file + mtable
---
 include/class/currency_mtable.class.php            | 225 ++++++++++++++++++++-
 include/database/currency_history_sql.class.php    |   4 +-
 .../database/v_currency_last_value_sql.class.php   | 147 ++++++++++++++
 include/template/currency_mtable_input.php         |  87 ++++++++
 .../currency_mtable_input_new.php}                 |  50 ++++-
 scenario/test_currency_mtable.php                  |  90 +++++++++
 sql/upgrade.sql                                    |   9 +-
 7 files changed, 589 insertions(+), 23 deletions(-)

diff --git a/include/class/currency_mtable.class.php 
b/include/class/currency_mtable.class.php
index acfb7ae..ace6ed1 100644
--- a/include/class/currency_mtable.class.php
+++ b/include/class/currency_mtable.class.php
@@ -23,14 +23,227 @@
  * @file
  * @brief Currency thanks the view v_currency_last_value
  */
+require_once NOALYSS_INCLUDE.'/lib/manage_table_sql.class.php';
+require_once NOALYSS_INCLUDE.'/database/currency_sql.class.php';
+require_once NOALYSS_INCLUDE.'/database/currency_history_sql.class.php';
 
-class Current_MTable extends Manage_Table_SQL
+class Currency_MTable extends Manage_Table_SQL
 {
-    function __construct(Data $p_table)
+
+    /**
+     * constructor
+     * @param Data $p_table
+     * @example test_currency_mtable.php
+     */
+    function __construct(Data_SQL $p_table)
     {
-        $this->table=$p_table;
         parent::__construct($p_table);
-        // 
-                
+
+        // If currency , cannot be deleted
+        if ($this->is_currency_used()==TRUE)
+        {
+            $this->set_delete_row(FALSE);
+        }
+        else
+        {
+            $this->set_delete_row(TRUE);
+        }
+        $this->set_property_visible("currency_id", FALSE);
+        $this->set_property_visible("currency_history_id", FALSE);
+        $this->set_col_label("cr_code_iso", "ISO");
+        $this->set_col_label("cr_name", _("Nom"));
+        $this->set_col_label('ch_value', _("Valeur"));
+        $this->set_col_label('str_from', _("Date"));
+        $this->set_col_type("str_from", "date");
+        $this->a_order=array("cr_code_iso", "currency_id", 
"currency_history_id", "cr_name", "ch_value", "str_from");
+        $this->set_icon_mod("first");
+        $this->set_sort_column("cr_code_iso");
     }
-}
\ No newline at end of file
+
+    /**
+     * returns TRUE the currency is used otherwise FALSE. We cannot delete a 
currency which is used in a
+     * operation
+     * @returns boolean true if currency is used
+     */
+    function is_currency_used()
+    {
+        return FALSE;
+    }
+
+    function input()
+    {
+        $record=$this->get_table();
+        $cr_code_iso=new IText("cr_code_iso", $record->cr_code_iso);
+        $cr_code_iso->size=10;
+        $cr_name=new IText("cr_name", $record->cr_name);
+        $cr_name->size=50;
+        $a_histo=$record->cn->get_array("select to_char(ch_from,'DD.MM.YYYY') 
as str_from,ch_value 
+               from 
+                currency_history 
+               where 
+                currency_id=$1", array($record->currency_id));
+        $new_rate_date=new IDate("new_rate_date");
+        $new_rate_value=new INum("new_rate_value");
+        $new_rate_value->prec=4;
+        if ($record->currency_id!=-1)
+        {
+            require NOALYSS_TEMPLATE."/currency_mtable_input.php";
+        }
+        else
+        {
+            require NOALYSS_TEMPLATE."/currency_mtable_input_new.php";
+        }
+    }
+
+    /**
+     * 
+     */
+    function check()
+    {
+        global $cn;
+        $table=$this->get_table();
+        $is_error=0;
+        // ------ cr_code_iso can not be empty
+        if (trim($table->cr_code_iso)=="")
+        {
+            $is_error++;
+            $this->set_error("cr_code_iso", _("Code iso ne peut pas être 
vide"));
+        }
+        // ------ cr_name can not be empty
+        if (trim($table->cr_name)=="")
+        {
+            $is_error++;
+            $this->set_error("cr_name", _("Nom ne peut pas être vide"));
+        }
+        // ----- for new record than we need at leat a date + value
+        if ($table->currency_id==-1)
+        {
+            // -- for new record , we need a date + value
+            if (isDate($table->str_from)==0||trim($table->str_from)=="")
+            {
+                $is_error++;
+                $this->set_error("str_from", _("Date incorrecte, il faut au 
moins une valeur"));
+            }
+            if (isNumber($table->ch_value)==0||trim($table->ch_value)=="")
+            {
+                $is_error++;
+                $this->set_error("ch_value", _("Valeur incorrecte, il faut au 
moins une valeur"));
+            }
+        }
+        else
+        {
+            // -- for update, the date and value must be valid
+            if (trim($table->str_from)!=""&&trim($table->ch_value)!="")
+            {
+                if (isDate($table->str_from)==0)
+                {
+                    $is_error++;
+                    $this->set_error("str_from", _("Date incorrecte"));
+                }
+                if (isNumber($table->ch_value)==0)
+                {
+                    $is_error++;
+                    $this->set_error("ch_value", _("Valeur incorrecte"));
+                }
+                // Date must be the greatest
+                $count=$cn->get_value("select count(*) 
+                        from 
+                            currency_history 
+                        where 
+                            currency_id =$1 
+                            and ch_from >= to_date($2,'DD.MM.YYYY') ", 
array($table->currency_id, $table->str_from));
+                if ($count>0)
+                {
+                    $is_error++;
+                    $this->set_error("str_from", _("Date doit être après la 
dernière valeur"));
+                }
+            }
+            elseif (trim($table->str_from)!=""||trim($table->ch_value)!="")
+            {
+                $is_error++;
+                $this->set_error("str_from", _("Valeur manquante"));
+            }
+        }
+        //-- duplicate iso code
+        $cnt=$cn->get_value("select count(*) from currency where id != $1 and 
cr_code_iso=$2",
+                array($table->currency_id, $table->cr_code_iso));
+        if ($cnt>0)
+        {
+            $is_error++;
+            $this->set_error("cr_code_iso", _("Code ISO existe déjà"));
+        }
+
+
+        if ($is_error==0)
+        {
+            return TRUE;
+        }
+
+        return FALSE;
+    }
+
+    /**
+     * 
+     */
+    function save()
+    {
+        $cn=Dossier::connect();
+        try
+        {
+            $cn->start();
+            $record=$this->get_table();
+
+            if ($record->currency_id==-1)
+            {
+                // Append a new currency and a default value
+                $currency=new Currency_SQL($cn);
+                $currency->setp("cr_code_iso", 
mb_strtoupper($record->cr_code_iso));
+                $currency->setp("cr_name", $record->cr_name);
+                $currency->insert();
+
+                $currency_history=new Currency_history_SQL($cn);
+                $currency_history->setp("ch_value", $record->ch_value);
+                $currency_history->setp("ch_from", $record->str_from);
+                $currency_history->setp("currency_id", $currency->id);
+                $currency_history->insert();
+
+                $this->table->currency_id=$currency->id;
+                $this->table->load();
+            }
+            else
+            {
+                // append a value in currency_historique and update currency
+                $currency=new Currency_SQL($cn, $record->currency_id);
+                $currency->setp("cr_code_iso", 
mb_strtoupper($record->cr_code_iso));
+                $currency->setp("cr_name", $record->cr_name);
+                $currency->update();
+
+                if ($record->str_from!="")
+                {
+                    $currency_history=new Currency_history_SQL($cn);
+                    $currency_history->setp("ch_value", $record->ch_value);
+                    $currency_history->setp("ch_from", $record->str_from);
+                    $currency_history->setp("currency_id", $currency->id);
+                    $currency_history->insert();
+                }
+            }
+            $cn->commit();
+        }
+        catch (Exception $ex)
+        {
+            $cn->rollback();
+            throw ($ex);
+        }
+    }
+
+    function from_request()
+    {
+        $http=new HttpInput();
+        
$this->table->cr_code_iso=mb_strtoupper(strip_tags($http->request("cr_code_iso")));
+        $this->table->cr_name=strip_tags($http->request("cr_name"));
+        $this->table->currency_id=$http->request("p_id", "number");
+        $this->table->ch_value=$http->request("new_rate_value");
+        $this->table->str_from=$http->request("new_rate_date");
+    }
+
+}
diff --git a/include/database/currency_history_sql.class.php 
b/include/database/currency_history_sql.class.php
index 05f35a4..5bec732 100644
--- a/include/database/currency_history_sql.class.php
+++ b/include/database/currency_history_sql.class.php
@@ -49,7 +49,7 @@ class Currency_history_SQL extends Noalyss_SQL
         $this->type=array(
             "id"=>"numeric"
             , "ch_value"=>"numeric"
-            , "ch_from"=>"timestamp without time zone"
+            , "ch_from"=>"date"
             , "currency_id"=>"numeric"
         );
 
@@ -58,7 +58,7 @@ class Currency_history_SQL extends Noalyss_SQL
             "id"=>"auto"
         );
 
-        $this->date_format="YYYYMMDD";
+        $this->date_format="DD.MM.YYYY";
         parent::__construct($p_cn, $p_id);
     }
 
diff --git a/include/database/v_currency_last_value_sql.class.php 
b/include/database/v_currency_last_value_sql.class.php
new file mode 100644
index 0000000..7b6ecf8
--- /dev/null
+++ b/include/database/v_currency_last_value_sql.class.php
@@ -0,0 +1,147 @@
+<?php
+
+/*
+ *   This file is part of NOALYSS.
+ *
+ *   PhpCompta 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 2 of the License, or
+ *   (at your option) any later version.
+ *
+ *   PhpCompta 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 PhpCompta; if not, write to the Free Software
+ *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+// Copyright (2018) Author Dany De Bontridder <dany@alchimerys.be>
+
+require_once NOALYSS_INCLUDE.'/lib/noalyss_sql.class.php';
+require_once NOALYSS_INCLUDE.'/lib/database.class.php';
+
+/**
+ * class_currency_history_sql.php
+ *
+ * @file
+ * @brief abstract of the view public.v_currency_last_value
+ */
+
+/**
+ * class_currency_history_sql.php
+ *
+ * @class
+ * @brief abstract of the view public.v_currency_last_value
+ */
+class V_Currency_Last_Value_SQL extends Data_SQL
+{
+
+    function __construct(Database $p_cn, $p_id=-1)
+    {
+        $this->table="public.v_currency_last_value";
+        $this->primary_key="currency_id";
+        /*
+         * List of columns
+         */
+        $this->name=array(
+            "currency_id"=>"currency_id"
+            , "cr_name"=>"cr_name"
+            , "cr_code_iso"=>"cr_code_iso"
+            , "currency_history_id"=>"currency_history_id"
+            , "ch_value"=>"ch_value"
+            , "str_from"=>"str_from"
+        );
+        /*
+         * Type of columns
+         */
+        $this->type=array(
+            "currency_id"=>"numeric"
+            , "cr_name"=>"text"
+            , "cr_code_iso"=>"text"
+            , "currency_history_id"=>"numeric"
+            , "ch_value"=>"numeric"
+            , "str_from"=>"text"
+        );
+
+
+        $this->default=array(
+            "currency_id"=>"auto"
+        );
+
+        $this->date_format="DD.MM.YYYY";
+        parent::__construct($p_cn, $p_id);
+    }
+
+    public function count($p_where="", $p_array=null)
+    {
+        $count=$this->cn->get_value("select count(*) from $this->table 
".$p_where, $p_array);
+        return $count;
+    }
+
+    public function delete()
+    {
+        
+    }
+
+    public function exist()
+    {
+        $pk=$this->primary_key;
+        $count=$this->cn->get_value("select count(*) from ".$this->table." 
where ".$this->primary_key."=$1",
+                array($this->$pk));
+        return $count;
+    }
+
+    public function insert()
+    {
+        
+    }
+
+    public function load()
+    {
+        $sql=" select ";
+        $sep="";
+        foreach ($this->name as $key)
+        {
+            switch ($this->type[$key])
+            {
+                case "date":
+                    $sql.=$sep.'to_char('.$key.",'".$this->date_format."') as 
".$key;
+                    break;
+                default:
+                    $sql.=$sep.$key;
+            }
+            $sep=",";
+        }
+        $pk=$this->primary_key;
+        $sql.=" from ".$this->table;
+
+        $sql.=" where ".$this->primary_key." = $1";
+
+        $result=$this->cn->get_array($sql, array($this->$pk));
+        if ($this->cn->count()==0)
+        {
+            $this->$pk=-1;
+            return;
+        }
+
+        foreach ($result[0] as $key=> $value)
+        {
+            $this->$key=$value;
+        }
+    }
+
+    function seek($cond='', $p_array=null)
+    {
+        $sql="select * from ".$this->table."  $cond";
+        $ret=$this->cn->exec_sql($sql, $p_array);
+        return $ret;
+    }
+
+    public function update()
+    {
+        
+    }
+
+}
diff --git a/include/template/currency_mtable_input.php 
b/include/template/currency_mtable_input.php
new file mode 100644
index 0000000..dae160c
--- /dev/null
+++ b/include/template/currency_mtable_input.php
@@ -0,0 +1,87 @@
+<?php
+
+/*
+ *   This file is part of NOALYSS.
+ *
+ *   PhpCompta 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 2 of the License, or
+ *   (at your option) any later version.
+ *
+ *   PhpCompta 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 PhpCompta; if not, write to the Free Software
+ *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+// Copyright (2018) Author Dany De Bontridder <dany@alchimerys.be>
+
+if (!defined('ALLOWED'))     die('Appel direct ne sont pas permis');
+
+/**
+ * @file
+ * @brief input for currency_mtable
+ * @see Currency_MTable
+ */
+?>
+<table>
+    <tr>
+        <td>
+            ISO
+        </td>
+        <td>
+            <?php echo $cr_code_iso->input(); ?>
+        </td>
+    </tr>
+    <tr>
+        <td>
+            <?php echo _("Nom")?>
+        </td>
+        <td>
+            <?php echo $cr_name->input(); ?>
+        </td>
+    </tr>
+</table>    
+
+<table class="result">
+    <tr>
+    <th>
+        <?php echo _("Date");?>
+    </th>
+    <th>
+        <?php echo _("Valeur")?>
+    </th>
+    </tr>
+    <?php
+        $nb_histo=count($a_histo);
+        for ($i=0;$i<$nb_histo;$i++):
+    ?>
+    <tr>
+        <td>
+            <?php echo $a_histo[$i]['str_from']?>
+        </td>
+        <td>
+            <?php echo $a_histo[$i]['ch_value']?>
+        </td>
+    </tr>
+    <?php    endfor; ?>
+  <tr>
+        <td>
+            <?php echo _("Date"); ?>
+        </td>
+        <td>
+            <?php echo $new_rate_date->input();?>
+        </td>
+    </tr>
+    <tr>
+      <td>
+            <?php echo _("Valeur"); ?>
+        </td>
+        <td>
+            <?php echo $new_rate_value->input();?>
+        </td>
+    </tr>
+</table>
diff --git a/include/class/currency_mtable.class.php 
b/include/template/currency_mtable_input_new.php
similarity index 53%
copy from include/class/currency_mtable.class.php
copy to include/template/currency_mtable_input_new.php
index acfb7ae..88639b8 100644
--- a/include/class/currency_mtable.class.php
+++ b/include/template/currency_mtable_input_new.php
@@ -19,18 +19,46 @@
  */
 // Copyright (2018) Author Dany De Bontridder <dany@alchimerys.be>
 
+if (!defined('ALLOWED'))     die('Appel direct ne sont pas permis');
+
 /**
  * @file
- * @brief Currency thanks the view v_currency_last_value
+ * @brief input for currency_mtable
+ * @see Currency_MTable
  */
+?>
+<table>
+    <tr>
+        <td>
+            ISO
+        </td>
+        <td>
+            <?php echo $cr_code_iso->input(); ?>
+        </td>
+    </tr>
+    <tr>
+        <td>
+            <?php echo _("Nom")?>
+        </td>
+        <td>
+            <?php echo $cr_name->input(); ?>
+        </td>
+    </tr>
+    <tr>
+        <td>
+            <?php echo _("Date"); ?>
+        </td>
+        <td>
+            <?php echo $new_rate_date->input();?>
+        </td>
+    </tr>
+    <tr>
+      <td>
+            <?php echo _("Valeur"); ?>
+        </td>
+        <td>
+            <?php echo $new_rate_value->input();?>
+        </td>
+    </tr>
+</table>    
 
-class Current_MTable extends Manage_Table_SQL
-{
-    function __construct(Data $p_table)
-    {
-        $this->table=$p_table;
-        parent::__construct($p_table);
-        // 
-                
-    }
-}
\ No newline at end of file
diff --git a/scenario/test_currency_mtable.php 
b/scenario/test_currency_mtable.php
new file mode 100644
index 0000000..d46f390
--- /dev/null
+++ b/scenario/test_currency_mtable.php
@@ -0,0 +1,90 @@
+<?php
+
+/*
+ *   This file is part of NOALYSS.
+ *
+ *   PhpCompta 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 2 of the License, or
+ *   (at your option) any later version.
+ *
+ *   PhpCompta 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 PhpCompta; if not, write to the Free Software
+ *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+// Copyright (2018) Author Dany De Bontridder <dany@alchimerys.be>
+
+if (!defined('ALLOWED'))
+    die('Appel direct ne sont pas permis');
+
+/**
+ * @file
+ * @brief 
+ * @param type $name Descriptionara
+ */
+//@description:Test the class Currency_MTable , ajax and javascript
+
+
+$_POST['gDossier']=$gDossierLogInput;
+$_GET['gDossier']=$gDossierLogInput;
+$_REQUEST=array_merge($_GET, $_POST);
+require_once NOALYSS_INCLUDE."/class/currency_mtable.class.php";
+require_once NOALYSS_INCLUDE."/lib/manage_table_sql.class.php";
+require_once NOALYSS_INCLUDE.'/database/v_currency_last_value_sql.class.php';
+
+$is_ajax=$http->request("TestAjaxFile", "string", "-");
+
+$currency=new V_Currency_Last_Value_SQL($cn);
+
+if ($is_ajax!="-")
+{
+    $p_id=$http->request('p_id', "number");
+    $currency->set_pk_value($p_id);
+    $currency->load();
+}
+
+$currency_table=new Currency_MTable($currency);
+
+$currency_table->set_callback("ajax_test.php");
+$currency_table->add_json_param("TestAjaxFile", __FILE__);
+if ($is_ajax!="-")
+{
+    $table=$http->request('table');
+    $action=$http->request('action');
+    $p_id=$http->request('p_id', "number");
+    $ctl_id=$http->request('ctl');
+    /*
+     * we're in ajax part
+     */
+    if ($action=="input")
+    {
+        $currency_table->set_object_name($ctl_id);
+        header('Content-type: text/xml; charset=UTF-8');
+        echo $currency_table->ajax_input()->saveXML();
+        return;
+    }
+    elseif ($action=="save")
+    {
+        $currency_table->set_object_name($ctl_id);
+        header('Content-type: text/xml; charset=UTF-8');
+        echo $currency_table->ajax_save()->saveXML();
+        return;
+    }
+    elseif ($action=="delete")
+    {
+        $currency_table->set_object_name($ctl_id);
+        header('Content-type: text/xml; charset=UTF-8');
+        echo $currency_table->ajax_delete()->saveXML();
+    }
+    return;
+}
+
+$currency_table->create_js_script();
+$currency_table->display_table();
+
+
diff --git a/sql/upgrade.sql b/sql/upgrade.sql
index cdc851f..001b69c 100644
--- a/sql/upgrade.sql
+++ b/sql/upgrade.sql
@@ -15,11 +15,12 @@ CREATE TABLE public.currency (
 
 CREATE TABLE public.currency_history (
        id serial NOT NULL,
-       ch_value numeric(6) NOT NULL,
-       ch_from timestamp NOT NULL,
+       ch_value numeric(20,6) NOT NULL,
+       ch_from date NOT NULL,
        currency_id int4 NOT NULL,
        CONSTRAINT currency_history_pk PRIMARY KEY (id),
-       CONSTRAINT currency_history_currency_fk FOREIGN KEY (id) REFERENCES 
currency(id)
+       CONSTRAINT currency_history_currency_fk FOREIGN KEY (currency_id) 
REFERENCES currency(id) 
+        ON DELETE RESTRICT ON UPDATE CASCADE
 )
 ;
 
@@ -42,7 +43,7 @@ select
        cr1.cr_code_iso,
        ch1.id as currency_history_id,
        ch1.ch_value as ch_value,
-       rc_from 
+       to_char(rc_from,'DD.MM.YYYY') as str_from 
 from
 currency as cr1
 join recent_rate on (currency_id=cr1.id)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]