[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Noalyss-commit] [noalyss] 11/32: Cleaning code : TVA rewritten
From: |
Dany De Bontridder |
Subject: |
[Noalyss-commit] [noalyss] 11/32: Cleaning code : TVA rewritten |
Date: |
Thu, 5 Jan 2023 14:18:30 -0500 (EST) |
sparkyx pushed a commit to branch devel
in repository noalyss.
commit 9407c2ec7b744ee4639e6b71df9dd2b4820007e3
Author: sparkyx <danydb@noalyss.eu>
AuthorDate: Sun Dec 18 00:31:29 2022 +0100
Cleaning code : TVA rewritten
---
include/class/acc_tva.class.php | 148 ++++++++------------------------
include/database/tva_rate_sql.class.php | 2 +-
unit-test/include/class/Acc_TVATest.php | 74 ++++++++++++++++
3 files changed, 113 insertions(+), 111 deletions(-)
diff --git a/include/class/acc_tva.class.php b/include/class/acc_tva.class.php
index 419e9ad7e..109fc09a7 100644
--- a/include/class/acc_tva.class.php
+++ b/include/class/acc_tva.class.php
@@ -35,29 +35,52 @@
*/
class Acc_Tva
{
- private $cn; /*!< $cn database connection */
private static $variable=array("id"=>"tva_id",
"label"=>"tva_label",
"rate"=>"tva_rate",
"comment"=>"tva_comment",
"account"=>"tva_poste",
"both_side"=>'tva_both_side');
+ public $tva_id;
+ public $tva_label;
+ public $tva_poste;
+ public $tva_rate;
+ public $tva_comment;
+ public $tva_poste;
+ public $tva_both_side;
- function __construct ($p_init,$p_tva_id=0)
+ private Tva_Rate_SQL $tva_rate_sql;
+
+ function __construct ($p_init,$p_tva_id=-1)
{
$this->cn=$p_init;
- if ( isNumber($p_tva_id)==1) {
- $this->tva_id=$p_tva_id;
- } else {
- $this->tva_id=0;
- }
- $this->poste="";
- $this->tva_label=null;
- $this->tva_rate=0;
- $this->tva_comment=null;
- $this->tva_poste=null;
- $this->tva_both_side='f';
+ $this->tva_rate_sql=new Tva_Rate_SQL($p_init,$p_tva_id);
+ $this->tva_id=$p_tva_id;
+ $this->tva_label=&$this->tva_rate_sql->tva_label;
+ $this->tva_rate=&$this->tva_rate_sql->tva_rate;
+ $this->tva_comment=&$this->tva_rate_sql->tva_comment;
+ $this->tva_poste=&$this->tva_rate_sql->tva_poste;
+ $this->tva_both_side=&$this->tva_rate_sql->tva_both_side;
+
}
+
+ /**
+ * @return Tva_Rate_SQL
+ */
+ public function getTvaRateSql(): Tva_Rate_SQL
+ {
+ return $this->tva_rate_sql;
+ }
+
+ /**
+ * @param Tva_Rate_SQL $tva_rate_sql
+ */
+ public function setTvaRateSql(Tva_Rate_SQL $tva_rate_sql)
+ {
+ $this->tva_rate_sql = $tva_rate_sql;
+ return $this;
+ }
+
public function get_parameter($p_string)
{
if ( array_key_exists($p_string,self::$variable) )
@@ -80,57 +103,7 @@ class Acc_Tva
}
- public function get_info()
- {
- return var_export(self::$variable,true);
- }
-
- public function verify()
- {
- // Verify that the elt we want to add is correct
- }
- public function save()
- {
-
- if ( $this->tva_id == 0 )
- $this->insert();
- else
- $this->update();
- }
-
- public function insert()
- {
- if ( $this->verify() != 0 ) return;
- $sql="select tva_insert($1,$2,$3,$4,$5)";
-
- $res=$this->cn->exec_sql(
- $sql,
- array($this->tva_label,
- $this->tva_rate,
- $this->tva_comment,
- $this->tva_poste,
- $this->tva_both_side)
- );
- $this->tva_id=$this->cn->get_current_seq('s_tva');
- $err=Database::fetch_result($res);
- }
- public function update()
- {
- if ( $this->verify() != 0 ) return;
- $sql="update tva_rate set
tva_label=$1,tva_rate=$2,tva_comment=$3,tva_poste=$4,tva_both_side=$5 ".
- " where tva_id = $6";
- $res=$this->cn->exec_sql(
- $sql,
- array($this->tva_label,
- $this->tva_rate,
- $this->tva_comment,
- $this->tva_poste,
- $this->tva_both_side,
- $this->tva_id)
- );
-
- }
/**
*Load the VAT,
*@note if the label is not found then we get an message error, so the
best is probably
@@ -138,21 +111,9 @@ class Acc_Tva
*/
public function load()
{
- if ( trim($this->tva_id)=="" || isNumber($this->tva_id)==0) return -1;
-
- $sql="select * from tva_rate where tva_id=$1";
- $res=$this->cn->exec_sql(
- $sql,
- array($this->tva_id)
- );
+ $this->tva_rate_sql->setp("tva_id",$this->tva_id);
- if ( $this->cn->size() == 0 ) return -1;
-
- $row=Database::fetch_array($res,0);
- foreach (self::$variable as $idx)
- {
- $this->$idx=$row[$idx];
- }
+ if ( ! $this->tva_rate_sql->load() ) return -1;
return 0;
}
/*!\brief get the account of the side (debit or credit)
@@ -176,37 +137,4 @@ class Acc_Tva
throw (new Exception (__FILE__.':'.__LINE__." param est d ou c, on
a recu [ $p_side ]"));
}
}
- public function delete()
- {
- if ( trim($this->tva_id)=="" || isNumber($this->tva_id)==0) return -1;
- $sql="delete from tva_rate where tva_id=$1";
- $res=$this->cn->exec_sql($sql,array($this->tva_id));
- }
- /*!\brief
- * Test function
- */
- static function test_me()
- {
- $cn=Dossier::connect();
- $a=new Acc_Tva($cn);
- echo $a->get_info();
- $a->set_parameter("id",1);
- $a->load();
- $a->set_parameter("id",0);
- $a->set_parameter("rate","0.2222");
- $a->set_parameter("label","test");
- $a->save();
- $a->load();
- print_r($a);
-
- $a->set_parameter("comment","un cht'it test");
- $a->save();
- $a->load();
- print_r($a);
-
- $a->delete();
- }
-
}
-
-/* test::test_me(); */
diff --git a/include/database/tva_rate_sql.class.php
b/include/database/tva_rate_sql.class.php
index d79e9bfa9..2ff4ff48e 100644
--- a/include/database/tva_rate_sql.class.php
+++ b/include/database/tva_rate_sql.class.php
@@ -31,7 +31,7 @@
* @class Tva_rate_SQL
* @brief abstract of the table public.tva_rate
*/
-class Tva_rate_SQL extends Table_Data_SQL
+class Tva_Rate_SQL extends Table_Data_SQL
{
function __construct(DatabaseCore $p_cn, $p_id=-1)
diff --git a/unit-test/include/class/Acc_TVATest.php
b/unit-test/include/class/Acc_TVATest.php
new file mode 100644
index 000000000..e42b07153
--- /dev/null
+++ b/unit-test/include/class/Acc_TVATest.php
@@ -0,0 +1,74 @@
+<?php
+/*
+ * This file is part of NOALYSS.
+ *
+ * NOALYSS 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.
+ *
+ * NOALYSS 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 NOALYSS; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+// Copyright Author Dany De Bontridder danydb@aevalys.eu 17/12/22
+/*!
+ * \file
+ * \brief test Acc_TVA
+ */
+
+use PHPUnit\Framework\TestCase;
+
+/**
+ * @backupGlobals enabled
+ * @coversDefaultClass \Acc_Payment
+ */
+require DIRTEST.'/global.php';
+
+
+class Acc_TVATest extends TestCase
+{
+ protected $object;
+
+ protected function setUp():void
+ {
+ include 'global.php';
+ }
+
+ /**
+ * @testdox check set_parameter function
+ * @return void
+ */
+ function testSet_parameter()
+ {
+ $cn=\Dossier::connect();
+ $count=$cn->get_value("select count(*) from tva_rate");
+ $this->assertEquals(9,$count);
+ $tva=new Acc_Tva($cn,2);
+ $this->assertEquals(0.1200,$tva->get_parameter("rate"),"error tva rate
get_parameter ");
+ $this->assertEquals(0.1200,$tva->tva_rate,"error tva rate direct
access ");
+ $tva->set_parameter("id",1);
+ $tva->load();
+ $this->assertEquals(0.2100 , $tva->tva_rate,"Cannot get tva rate after
set_parameter");
+ }
+ function testConstructor()
+ {
+ $cn=\Dossier::connect();
+ $count=$cn->get_value("select count(*) from tva_rate");
+ $this->assertEquals(9,$count);
+ $tva=new Acc_Tva($cn,2);
+ $this->assertEquals(0.1200,$tva->tva_rate,"error tva rate direct
access ");
+ $tva->set_parameter("id",1);
+ $tva->load();
+ $this->assertEquals(0.2100 , $tva->tva_rate,"Cannot get tva rate after
set_parameter");
+ $tva->set_parameter("id",1);
+ $tva->load();
+ $this->assertEquals(0.2100 , $tva->tva_rate,"Cannot get tva rate after
set_parameter");
+ }
+
+}
\ No newline at end of file
- [Noalyss-commit] [noalyss] branch devel updated (f1d3862e1 -> f78ec1e40), Dany De Bontridder, 2023/01/05
- [Noalyss-commit] [noalyss] 01/32: Do no save empty comment or description, Dany De Bontridder, 2023/01/05
- [Noalyss-commit] [noalyss] 07/32: Compatibility PHP8.1 , node can be null, Dany De Bontridder, 2023/01/05
- [Noalyss-commit] [noalyss] 10/32: Esthetic, Dany De Bontridder, 2023/01/05
- [Noalyss-commit] [noalyss] 13/32: Correct return value of Table_Data_SQL->load : false if nothing found, Dany De Bontridder, 2023/01/05
- [Noalyss-commit] [noalyss] 11/32: Cleaning code : TVA rewritten,
Dany De Bontridder <=
- [Noalyss-commit] [noalyss] 23/32: New : Follow Up , the description modifiable is using NicEdit, Dany De Bontridder, 2023/01/05
- [Noalyss-commit] [noalyss] 32/32: Merge branch '9035-followup-description-mod-rtf', Dany De Bontridder, 2023/01/05
- [Noalyss-commit] [noalyss] 22/32: PHP 8.1 , option ASSERT_QUIET_EVAL is removed, Dany De Bontridder, 2023/01/05
- [Noalyss-commit] [noalyss] 27/32: Fix Bug TVA : cannot save VAT, Dany De Bontridder, 2023/01/05
- [Noalyss-commit] [noalyss] 20/32: New #0001327: Code TVA : utilisation des symboles plutôt que des codes, Dany De Bontridder, 2023/01/05
- [Noalyss-commit] [noalyss] 02/32: Bug : incorrect name, Dany De Bontridder, 2023/01/05
- [Noalyss-commit] [noalyss] 12/32: PHPUNIT improve test, Dany De Bontridder, 2023/01/05
- [Noalyss-commit] [noalyss] 28/32: Cosmetic, Dany De Bontridder, 2023/01/05
- [Noalyss-commit] [noalyss] 24/32: New : Follow Up , the description modifiable is using NicEdit, Dany De Bontridder, 2023/01/05
- [Noalyss-commit] [noalyss] 21/32: Merge branch 'dev-9035-tva-code', Dany De Bontridder, 2023/01/05