Task #0001992: Prévision, calcul avec comptabilité analytique

This commit is contained in:
sparkyx 2021-05-24 12:39:50 +02:00
parent 2838cc2f08
commit 1a632a2d1c
37 changed files with 1675 additions and 1189 deletions

View file

@ -23,8 +23,9 @@
require_once NOALYSS_INCLUDE.'/lib/itext.class.php';
require_once NOALYSS_INCLUDE.'/lib/ibutton.class.php';
require_once NOALYSS_INCLUDE.'/class/acc_report_row.class.php';
require_once NOALYSS_INCLUDE.'/lib/impress.class.php';
require_once NOALYSS_INCLUDE.'/database/form_detail_sql.class.php';
require_once NOALYSS_INCLUDE.'/database/form_definition_sql.class.php';
/*!
* \brief Class rapport Create, view, modify and parse report
@ -33,28 +34,29 @@ require_once NOALYSS_INCLUDE.'/lib/impress.class.php';
class Acc_Report
{
var $db; /*!< $db database connx */
var $id; /*!< $id formdef.fr_id */
var $name; /*!< $name report's name */
var $aAcc_Report_row; /*!< array of rapport_row */
private $form_definition; /*!< form_definition_sql
var $nb;
/*!\brief Constructor */
function __construct($p_cn,$p_id=0)
function __construct($p_cn,$p_id=-1)
{
$this->db=$p_cn;
$this->id=$p_id;
$this->name='Nouveau';
$this->aAcc_Report_row=null;
$this->form_definition=new Form_Definition_SQL($p_cn,$p_id);
}
/*!\brief Return the report's name
public function get_form_definition()
{
return $this->form_definition;
}
public function set_form_definition($form_definition): void
{
$this->form_definition=$form_definition;
}
/*!\brief Return the report's name
*/
function get_name()
{
$ret=$this->db->exec_sql("select fr_label from formdef where fr_id=".$this->id);
if (Database::num_row($ret) == 0) return $this->name;
$a=Database::fetch_array($ret,0);
$this->name=$a['fr_label'];
return $this->name;
return $this->form_definition->getp("fr_label");
}
/*!\brief return all the row and parse formula
* from a report
@ -65,15 +67,15 @@ class Acc_Report
function get_row($p_start,$p_end,$p_type_date)
{
$Res=$this->db->exec_sql("select fo_id ,
$Res=$this->form_definition->cn->exec_sql("select fo_id ,
fo_fr_id,
fo_pos,
fo_label,
fo_formula,
fr_label from form
inner join formdef on fr_id=fo_fr_id
where fr_id =".$this->id.
"order by fo_pos");
fr_label from form_detail
inner join form_definition on fr_id=fo_fr_id
where fr_id =$1
order by fo_pos",array($this->form_definition->getp("fr_id")));
$Max=Database::num_row($Res);
if ($Max==0)
{
@ -84,7 +86,7 @@ class Acc_Report
for ($i=0;$i<$Max;$i++)
{
$l_line=Database::fetch_array($Res,$i);
$col[]=Impress::parse_formula($this->db,
$col[]=Impress::parse_formula($this->form_definition->cn,
$l_line['fo_label'],
$l_line['fo_formula'],
$p_start,
@ -97,248 +99,40 @@ class Acc_Report
$this->row=$col;
return $col;
}
/*!
* \brief Display a form for encoding a new report or update one
*
* \param $p_line number of line
*
*/
function form($p_line=0)
{
$r="";
if ($p_line == 0 ) $p_line=count($this->aAcc_Report_row);
$r.= dossier::hidden();
$r.= HtmlInput::hidden('line',$p_line);
$r.= HtmlInput::hidden('fr_id',$this->id);
$wForm=new IText();
$r.=_("Nom du rapport")." : ";
$r.=$wForm->input('form_nom',$this->name);
$r.= '<TABLE id="rap1" width="100%">';
$r.= "<TR>";
$r.= "<TH> Position </TH>";
$r.= "<TH> Texte </TH>";
$r.= "<TH> Formule</TH>";
$r.= '</TR>';
$wName=new IText();
$wName->size=40;
$wPos=new IText();
$wPos->size=3;
$wForm=new IText();
$wForm->size=35;
for ( $i =0 ; $i < $p_line;$i++)
{
$r.= "<TR>";
$r.= "<TD>";
$wPos->value=( isset($this->aAcc_Report_row[$i]->fo_pos))?$this->aAcc_Report_row[$i]->fo_pos:$i+1;
$r.=$wPos->input("pos".$i);
$r.= '</TD>';
$r.= "<TD>";
$wName->value=( isset($this->aAcc_Report_row[$i]->fo_label))?$this->aAcc_Report_row[$i]->fo_label:"";
$r.=$wName->input("text".$i);
$r.= '</TD>';
$r.='<td>';
$search=new IPoste("form".$i);
$search->size=50;
$search->value=( isset($this->aAcc_Report_row[$i]->fo_formula))?$this->aAcc_Report_row[$i]->fo_formula:"";
$search->label=_("Recherche poste");
$search->set_attribute('gDossier',dossier::id());
$search->set_attribute('bracket',1);
$search->set_attribute('no_overwrite',1);
$search->set_attribute('noquery',1);
$search->set_attribute('account',$search->name);
$search->set_attribute('ipopup','ipop_card');
$r.=$search->input();
$r.='</td>';
$r.= "</TR>";
}
$r.= "</TABLE>";
$wButton=new IButton();
$wButton->javascript=" rapport_add_row('".dossier::id()."')";
$wButton->label="Ajout d'une ligne";
$r.=$wButton->input();
return $r;
}
/*!\brief save into form and form_def
*/
function save()
{
if ( strlen(trim($this->name)) == 0 )
return;
if ( $this->id == 0 )
$this->insert();
else
$this->update();
}
function insert()
{
try
{
$this->db->start();
$ret_sql=$this->db->exec_sql(
"insert into formdef (fr_label) values($1) returning fr_id",
array($this->name)
);
$this->id=Database::fetch_result($ret_sql,0,0);
$ix=1;
foreach ( $this->aAcc_Report_row as $row)
{
if ( strlen(trim($row->get_parameter("name"))) != 0 &&
strlen(trim($row->get_parameter("formula"))) != 0 )
{
$ix=($row->get_parameter("position")!="")?$row->get_parameter("position"):$ix;
$row->set_parameter("position",$ix);
$ret_sql=$this->db->exec_sql(
"insert into form (fo_fr_id,fo_pos,fo_label,fo_formula)".
" values($1,$2,$3,$4)",
array($this->id,
$row->fo_pos,
$row->fo_label,
$row->fo_formula)
);
}
}
}
catch (Exception $e)
{
record_log($e);
$this->db->rollback();
echo $e->getMessage();
}
$this->db->commit();
}
function update()
{
try
{
$this->db->start();
$ret_sql=$this->db->exec_sql(
"update formdef set fr_label=$1 where fr_id=$2",
array($this->name,$this->id));
$ret_sql=$this->db->exec_sql(
"delete from form where fo_fr_id=$1",
array($this->id));
$ix=0;
foreach ( $this->aAcc_Report_row as $row)
{
if ( strlen(trim($row->get_parameter("name"))) != 0 &&
strlen(trim($row->get_parameter("formula"))) != 0 )
{
$ix=($row->get_parameter("position")!="")?$row->get_parameter("position"):$ix;
$row->set_parameter("position",$ix);
$ret_sql=$this->db->exec_sql(
"insert into form (fo_fr_id,fo_pos,fo_label,fo_formula)".
" values($1,$2,$3,$4)",
array($this->id,
$row->fo_pos,
$row->fo_label,
$row->fo_formula)
);
}
}
}
catch (Exception $e)
{
record_log($e);
$this->db->rollback();
echo $e->getMessage();
}
$this->db->commit();
}
/*!\brief fill a form thanks an array, usually it is $_POST
*\param $p_array keys = fr_id, form_nom,textXX, formXX, posXX where
XX is an number
*/
function from_array($p_array)
{
$this->id=(isset($p_array['fr_id']))?$p_array['fr_id']:0;
$this->name=(isset($p_array['form_nom']))?$p_array['form_nom']:"";
$ix=0;
$rr=new Acc_Report_Row();
$rr->set_parameter("form_id",$this->id);
$rr->set_parameter('database',$this->db);
$this->aAcc_Report_row=$rr->from_array($p_array);
$this->form_definition->save();
}
/*!\brief the fr_id MUST be set before calling
*/
function load()
{
$sql=$this->db->exec_sql(
"select fr_label from formdef where fr_id=$1",
array($this->id));
if ( Database::num_row($sql) == 0 ) return;
$this->name=Database::fetch_result($sql,0,0);
$sql=$this->db->exec_sql(
"select fo_id,fo_pos,fo_label,fo_formula ".
" from form ".
" where fo_fr_id=$1 order by fo_pos",
array($this->id));
$f=Database::fetch_all($sql);
$array=array();
if ( ! empty($f) )
{
foreach ($f as $r)
{
$obj=new Acc_Report_Row();
$obj->set_parameter("name",$r['fo_label']);
$obj->set_parameter("id",$r['fo_id']);
$obj->set_parameter("position",$r['fo_pos']);
$obj->set_parameter("formula",$r['fo_formula']);
$obj->set_parameter('database',$this->db);
$obj->set_parameter('form_id',$this->id);
$array[]=clone $obj;
}
}
$this->aAcc_Report_row=$array;
$this->form_definition->load();
}
function delete()
{
$ret=$this->db->exec_sql(
"delete from formdef where fr_id=$1",
array($this->id)
);
$this->form_definition->delete();
}
/*!\brief get a list from formdef of all defined form
/*!\brief get a list from form_definition of all defined form
*
*\return array of object rapport
*
*/
function get_list()
{
$sql="select fr_id,fr_label from formdef order by fr_label";
$ret=$this->db->exec_sql($sql);
$sql="select fr_id,fr_label from form_definition order by fr_label";
$ret=$this->form_definition->cn->exec_sql($sql);
if ( Database::num_row($ret) == 0 ) return array();
$array=Database::fetch_all($ret);
$obj=array();
foreach ($array as $row)
{
$tmp=new Acc_Report($this->db);
$tmp=new Acc_Report($this->form_definition->cn);
$tmp->id=$row['fr_id'];
$tmp->name=$row['fr_label'];
$obj[]=clone $tmp;
@ -351,7 +145,7 @@ class Acc_Report
*/
function make_array()
{
$sql=$this->db->make_array("select fr_id,fr_label from formdef order by fr_label");
$sql=$this->form_definition->cn->make_array("select fr_id,fr_label from form_definition order by fr_label");
return $sql;
}
@ -362,14 +156,20 @@ class Acc_Report
function export_csv($p_file)
{
$this->load();
fputcsv($p_file,array($this->name));
foreach ($this->aAcc_Report_row as $row)
fputcsv($p_file,array($this->form_definition->getp("fr_label")));
$array=$this->form_definition->get_cn()->get_array("select fo_label,fo_pos,fo_formula
from form_detail
where fo_fr_id=$1
order by fo_pos"
,array($this->form_definition->getp("fr_id"))
);
foreach ($array as $row)
{
fputcsv($p_file,array($row->get_parameter("name"),
$row->get_parameter('position'),
$row->get_parameter('formula'))
fputcsv($p_file,array($row["fo_label"],
$row["fo_pos"],
$row["fo_formula"])
);
}
@ -392,21 +192,18 @@ class Acc_Report
$file=fopen($file_report,'r');
$data=fgetcsv($file);
if ( empty($data) ) return;
$this->name=$data[0];
$array=array();
$cn=$this->form_definition->cn;
$id=$this->form_definition->getp("fr_id");
while($data=fgetcsv($file))
{
$obj=new Acc_Report_Row();
$obj->set_parameter("name",$data[0]);
$obj->set_parameter("id",0);
$obj->set_parameter("position",$data[1]);
$obj->set_parameter("formula",$data[2]);
$obj->set_parameter('database',$this->db);
$obj->set_parameter('form_id',0);
$array[]=clone $obj;
$obj=new Form_Detail_SQL($cn);
$obj->setp("fo_label",$data[0]);
$obj->setp("fo_pos",$data[1]);
$obj->set("fo_formula",$data[2]);
$obj->setp("fo_fr_id",$id);
$obj->insert();
}
$this->aAcc_Report_row=$array;
$this->insert();
}
}
/**
@ -416,42 +213,35 @@ class Acc_Report
*/
function exist($p_id=0)
{
$c=$this->id;
$c=$this->form_definition->getp("fr_id");
if ( $p_id != 0 ) $c=$p_id;
$ret=$this->db->exec_sql("select fr_label from formdef where fr_id=$1",array($c));
$ret=$this->form_definition->cn->exec_sql("select fr_label from form_definition where fr_id=$1",array($c));
if (Database::num_row($ret) == 0) return false;
return true;
}
static function test_me()
/**
* @brief display a form for creating a new report by importing it or set manually
*/
function create()
{
$cn=Dossier::connect();
$a=new Acc_Report($cn);
print_r($a->get_list());
$array=array("text0"=>"test1",
"form0"=>"7%",
"text1"=>"test2",
"form1"=>"6%",
"fr_id"=>110,
"form_nom"=>"Tableau"
);
$a->from_array($array);
print_r($a);
echo '<form method="post">';
echo $a->form(10);
echo HtmlInput::submit('update','Enregistre');
/* Add a line should be a javascript see comptanalytic */
// $r.= '<INPUT TYPE="submit" value="Ajoute une ligne" name="add_line">';
echo HtmlInput::submit('del_form','Efface ce rapport');
echo HtmlInput::hidden('test_select',$_REQUEST['test_select']);
echo "</FORM>";
if ( isset ($_POST['update']))
{
$b=new Acc_Report($cn);
$b->from_array($_POST);
echo '<hr>';
print_r($b);
}
require_once NOALYSS_INCLUDE."/template/acc_report-create.php";
}
/**
* @brief
* @param IText $name
*/
function input_name($name)
{
$name=new IText("fr_name",$name);
$name->id="fr_name_inplace";
$iName=new Inplace_Edit($name);
$iName->set_callback("ajax_misc.php");
$iName->add_json_param("op", "report_definition");
$iName->add_json_param("sa", "change_name");
$iName->add_json_param("gDossier", Dossier::id());
$iName->add_json_param("p_id", $this->form_definition->getp("fr_id"));
return $iName;
}
}