FORECAST: code rewrite
This commit is contained in:
parent
22010b2cd8
commit
92973831d4
4 changed files with 35 additions and 203 deletions
|
|
@ -290,7 +290,17 @@ EOF;
|
||||||
$this->cn->commit();
|
$this->cn->commit();
|
||||||
return $new;
|
return $new;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
*@brief load all the existing forecast
|
||||||
|
*@param $p_cn is an Database object
|
||||||
|
*@return array of f_id and f_name
|
||||||
|
*/
|
||||||
|
public static function load_all($p_cn)
|
||||||
|
{
|
||||||
|
$sql="select f_id, f_name,f_start_date,f_end_date from forecast order by 2 desc";
|
||||||
|
$ret=$p_cn->get_array($sql);
|
||||||
|
return $ret;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
|
||||||
|
|
@ -1,172 +0,0 @@
|
||||||
<?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
|
|
||||||
|
|
||||||
/*!\file
|
|
||||||
* \brief manage the table forecast
|
|
||||||
*/
|
|
||||||
/*!
|
|
||||||
* \brief manage the table forecast
|
|
||||||
*/
|
|
||||||
#[\AllowDynamicProperties]
|
|
||||||
class Forecast
|
|
||||||
{
|
|
||||||
private static $variable=array ("id"=>"f_id",
|
|
||||||
"name"=>"f_name","start_date"=>"f_start_date","end_date"=>"f_end_date"
|
|
||||||
);
|
|
||||||
private $cn;
|
|
||||||
/**
|
|
||||||
* @brief constructor
|
|
||||||
* @param $p_init Database object
|
|
||||||
*/
|
|
||||||
function __construct ($p_init,$p_id=0)
|
|
||||||
{
|
|
||||||
$this->cn=$p_init;
|
|
||||||
$this->f_id=$p_id;
|
|
||||||
}
|
|
||||||
public function get_parameter($p_string)
|
|
||||||
{
|
|
||||||
if ( array_key_exists($p_string,self::$variable) )
|
|
||||||
{
|
|
||||||
$idx=self::$variable[$p_string];
|
|
||||||
return $this->$idx;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
throw new Exception("Attribut inexistant $p_string");
|
|
||||||
}
|
|
||||||
public function set_parameter($p_string,$p_value)
|
|
||||||
{
|
|
||||||
if ( array_key_exists($p_string,self::$variable) )
|
|
||||||
{
|
|
||||||
$idx=self::$variable[$p_string];
|
|
||||||
$this->$idx=$p_value;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
throw new Exception("Attribut inexistant $p_string");
|
|
||||||
}
|
|
||||||
public function get_info()
|
|
||||||
{
|
|
||||||
return var_export(self::$variable,true);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function verify()
|
|
||||||
{
|
|
||||||
// Verify that the elt we want to add is correct
|
|
||||||
// the f_name must be unique (case insensitive)
|
|
||||||
if ( noalyss_strlentrim($this->f_name)==0) throw new Exception(_('Le nom ne peut pas être vide'));
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
public function save()
|
|
||||||
{
|
|
||||||
/* please adapt */
|
|
||||||
if ( $this->get_parameter("id") == 0 )
|
|
||||||
$this->insert();
|
|
||||||
else
|
|
||||||
$this->update();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function insert()
|
|
||||||
{
|
|
||||||
if ( $this->verify() != 0 ) return;
|
|
||||||
$sql="insert into forecast (f_name,f_start_date,f_end_date) ".
|
|
||||||
" values ($1,$2,$3) returning f_id";
|
|
||||||
$res=$this->cn->exec_sql(
|
|
||||||
$sql,
|
|
||||||
array($this->f_name,$this->f_start_date,$this->f_end_date)
|
|
||||||
);
|
|
||||||
$this->f_id=Database::fetch_result($res,0,0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*@brief update the forecast table
|
|
||||||
*/
|
|
||||||
public function update()
|
|
||||||
{
|
|
||||||
if ( $this->verify() != 0 ) return;
|
|
||||||
|
|
||||||
$sql="update forecast set f_name=$1,f_start_date=$2,f_end_date=$3 ".
|
|
||||||
" where f_id = $4";
|
|
||||||
$res=$this->cn->exec_sql(
|
|
||||||
$sql,
|
|
||||||
array($this->f_name,$this->f_start_date,$this->f_end_date, $this->f_id)
|
|
||||||
);
|
|
||||||
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
*@brief load all the existing forecast
|
|
||||||
*@param $p_cn is an Database object
|
|
||||||
*@return array of f_id and f_name
|
|
||||||
*/
|
|
||||||
public static function load_all($p_cn)
|
|
||||||
{
|
|
||||||
$sql="select f_id, f_name,f_start_date,f_end_date from forecast order by 2 desc";
|
|
||||||
$ret=$p_cn->get_array($sql);
|
|
||||||
return $ret;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* @brief load from db
|
|
||||||
* @return boolean true if found else false
|
|
||||||
*/
|
|
||||||
public function load():bool
|
|
||||||
{
|
|
||||||
$sql="select f_id, f_name,f_start_date ,f_end_date from forecast where f_id=$1";
|
|
||||||
$res=$this->cn->exec_sql(
|
|
||||||
$sql,
|
|
||||||
array($this->f_id)
|
|
||||||
);
|
|
||||||
if ( Database::num_row($res) == 0 ) return false;
|
|
||||||
$row=Database::fetch_array($res,0);
|
|
||||||
$a_index=array_values(self::$variable);
|
|
||||||
foreach ( $a_index as $idx)
|
|
||||||
{
|
|
||||||
$this->$idx=$row[$idx];
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
public function delete()
|
|
||||||
{
|
|
||||||
$sql="delete from forecast where f_id=$1";
|
|
||||||
$res=$this->cn->exec_sql($sql,array($this->f_id));
|
|
||||||
}
|
|
||||||
public function object_clone()
|
|
||||||
{
|
|
||||||
$this->load();
|
|
||||||
/* save into the table forecast */
|
|
||||||
$sql="insert into forecast(f_name,f_start_date,f_end_date) select 'clone '||f_name,f_start_date,f_end_date from forecast where f_id=$1 returning f_id";
|
|
||||||
$new=$this->cn->get_value($sql,array($this->f_id));
|
|
||||||
|
|
||||||
/* save into forecast_cat */
|
|
||||||
$sql="insert into forecast_cat(fc_desc,f_id,fc_order) select fc_desc,$1,fc_order from forecast_cat where f_id=$2 returning fc_id" ;
|
|
||||||
$array=$this->cn->get_array($sql,array($new,$this->f_id));
|
|
||||||
|
|
||||||
$old=$this->cn->get_array("select fc_id from forecast_cat where f_id=$1",array($this->f_id));
|
|
||||||
/* save into forecast_item */
|
|
||||||
for ($i=0;$i<count($array);$i++)
|
|
||||||
{
|
|
||||||
$this->cn->exec_sql("insert into forecast_item (fi_text,fi_account,fi_order,fc_id,fi_amount,fi_pid) ".
|
|
||||||
" select fi_text,fi_account,fi_order,$1,fi_amount,fi_pid ".
|
|
||||||
" from forecast_item where fc_id=$2",array($array[$i]['fc_id'],$old[$i]['fc_id']));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
|
|
@ -171,7 +171,7 @@ if ($sa=='list')
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
$aForecast=Forecast::load_all($cn);
|
$aForecast= Anticipation::load_all($cn);
|
||||||
$menu=array();
|
$menu=array();
|
||||||
$get_dossier=dossier::get();
|
$get_dossier=dossier::get();
|
||||||
require_once NOALYSS_TEMPLATE."/forecast-new.php";
|
require_once NOALYSS_TEMPLATE."/forecast-new.php";
|
||||||
|
|
|
||||||
|
|
@ -29,12 +29,12 @@
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @backupGlobals enabled
|
* @backupGlobals disabled
|
||||||
* @coversDefaultClass \Forecast
|
* @coversDefaultClass \Anticipation
|
||||||
*/
|
*/
|
||||||
require DIRTEST.'/global.php';
|
require DIRTEST.'/global.php';
|
||||||
|
|
||||||
class ForecastTest extends TestCase
|
class AnticipationTest extends TestCase
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -68,7 +68,7 @@ class ForecastTest extends TestCase
|
||||||
{
|
{
|
||||||
include 'global.php';
|
include 'global.php';
|
||||||
global $g_connection;
|
global $g_connection;
|
||||||
$g_connection->exec_sql('delete from forecast');
|
$g_connection->exec_sql("delete from forecast");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -87,17 +87,15 @@ class ForecastTest extends TestCase
|
||||||
* @testdox insert into forecast
|
* @testdox insert into forecast
|
||||||
* @covers ::insert ::delete ::load
|
* @covers ::insert ::delete ::load
|
||||||
*/
|
*/
|
||||||
public function testSQL_Insert()
|
public function testSQL_Anticipation()
|
||||||
{
|
{
|
||||||
global $g_connection;
|
global $g_connection;
|
||||||
$obj=new Forecast($g_connection);
|
$forecast_sql=new Forecast_SQL($g_connection);
|
||||||
$obj->set_parameter("start_date", 111);
|
$forecast_sql->setp("f_name","phpunit");
|
||||||
$obj->set_parameter("end_date", 116);
|
$forecast_sql->setp("f_start_date",92);
|
||||||
$obj->set_parameter("name","PhpUNIT 2014");
|
$forecast_sql->setp("f_end_date",103);
|
||||||
$obj->insert();
|
$forecast_sql->save();
|
||||||
$this->assertTrue($obj->get_parameter("id")>0,"Cannot insert");
|
$this->assertEquals (1,$g_connection->get_value("select count(*) from forecast")," forecast not created");
|
||||||
$obj->delete();
|
|
||||||
$this->assertFalse($obj->load(),"Cannot delete");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -105,24 +103,20 @@ class ForecastTest extends TestCase
|
||||||
* @testdox update into forecast
|
* @testdox update into forecast
|
||||||
* @covers ::insert ::delete ::load
|
* @covers ::insert ::delete ::load
|
||||||
*/
|
*/
|
||||||
public function testSQL_Update()
|
public function testClone()
|
||||||
{
|
{
|
||||||
global $g_connection;
|
global $g_connection;
|
||||||
$obj=new Forecast($g_connection);
|
$forecast_sql=new Forecast_SQL($g_connection);
|
||||||
$obj->set_parameter("start_date", 111);
|
$forecast_sql->setp("f_name","phpunit2");
|
||||||
$obj->set_parameter("end_date", 116);
|
$forecast_sql->setp("f_start_date",92);
|
||||||
$obj->set_parameter("name", "PhpUNIT 2014");
|
$forecast_sql->setp("f_end_date",103);
|
||||||
$obj->insert();
|
$forecast_sql->save();
|
||||||
|
$id=$forecast_sql->getp('f_id');
|
||||||
$this->assertTrue($obj->get_parameter("id")>0,"Cannot insert");
|
$anticipation = new \Anticipation($g_connection,$id);
|
||||||
$obj->set_parameter('name','TEST UPDATE');
|
$clone_id = $anticipation->object_clone();
|
||||||
$obj->save();
|
$this->assertTrue(isNumber($clone_id)==1 && $clone_id > $id,"clone not created id = {$id} clone_id = {$clone_id}");
|
||||||
$reload=new Forecast($g_connection,$obj->get_parameter("id"));
|
|
||||||
$reload->load();
|
|
||||||
$this->assertTrue($reload->get_parameter("name") == $obj->get_parameter("name"),"cannot update");
|
|
||||||
$reload->delete();
|
|
||||||
$this->assertFalse($reload->load(),"Cannot delete");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue