Cleaning code : TVA rewritten

This commit is contained in:
sparkyx 2022-12-18 00:31:29 +01:00
parent 12a1f8a516
commit 9407c2ec7b
3 changed files with 113 additions and 111 deletions

View file

@ -35,29 +35,52 @@
*/ */
class Acc_Tva class Acc_Tva
{ {
private $cn; /*!< $cn database connection */
private static $variable=array("id"=>"tva_id", private static $variable=array("id"=>"tva_id",
"label"=>"tva_label", "label"=>"tva_label",
"rate"=>"tva_rate", "rate"=>"tva_rate",
"comment"=>"tva_comment", "comment"=>"tva_comment",
"account"=>"tva_poste", "account"=>"tva_poste",
"both_side"=>'tva_both_side'); "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; $this->cn=$p_init;
if ( isNumber($p_tva_id)==1) { $this->tva_rate_sql=new Tva_Rate_SQL($p_init,$p_tva_id);
$this->tva_id=$p_tva_id; $this->tva_id=$p_tva_id;
} else { $this->tva_label=&$this->tva_rate_sql->tva_label;
$this->tva_id=0; $this->tva_rate=&$this->tva_rate_sql->tva_rate;
} $this->tva_comment=&$this->tva_rate_sql->tva_comment;
$this->poste=""; $this->tva_poste=&$this->tva_rate_sql->tva_poste;
$this->tva_label=null; $this->tva_both_side=&$this->tva_rate_sql->tva_both_side;
$this->tva_rate=0;
$this->tva_comment=null;
$this->tva_poste=null;
$this->tva_both_side='f';
} }
/**
* @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) public function get_parameter($p_string)
{ {
if ( array_key_exists($p_string,self::$variable) ) 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, *Load the VAT,
*@note if the label is not found then we get an message error, so the best is probably *@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() public function load()
{ {
if ( trim($this->tva_id)=="" || isNumber($this->tva_id)==0) return -1; $this->tva_rate_sql->setp("tva_id",$this->tva_id);
$sql="select * from tva_rate where tva_id=$1";
$res=$this->cn->exec_sql(
$sql,
array($this->tva_id)
);
if ( $this->cn->size() == 0 ) return -1; if ( ! $this->tva_rate_sql->load() ) return -1;
$row=Database::fetch_array($res,0);
foreach (self::$variable as $idx)
{
$this->$idx=$row[$idx];
}
return 0; return 0;
} }
/*!\brief get the account of the side (debit or credit) /*!\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 ]")); 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(); */

View file

@ -31,7 +31,7 @@
* @class Tva_rate_SQL * @class Tva_rate_SQL
* @brief abstract of the table public.tva_rate * @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) function __construct(DatabaseCore $p_cn, $p_id=-1)

View file

@ -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");
}
}