db=$p_cn;
$this->id=$p_id;
$this->date=date("d.m.Y");
$this->nMaxRow=10;
}
/*!\brief add several rows */
function add() {
/*!\todo add a control to check that the amount are balanced if
not throw an exception
*/
foreach ($this->a_operation as $row) {
$row->add();
}
}
/*!\brief show a form for the operation (several rows)
* \return the string containing the form but without the form tag
*
*/
function form($p_readonly=0){
$wDate=new widget("js_date","Date : ","pdate",$this->date);
$wDate->table=1;
$wDate->size=10;
$wDate->readonly=$p_readonly;
$wDescription=new widget("text","Description","pdesc");
$wDescription->table=0;
$wDescription->size=80;
$wDescription->readonly=$p_readonly;
// Show an existing operation
//
if ( isset ($this->a_operation[0])) {
$wDate->value=$this->a_operation[0]->oa_date;
$wDescription->value=$this->a_operation[0]->oa_description;
}
$ret="";
$ret.='
';
$ret.="".$wDate->IOValue()."
";
$ret.='| Description | '.
''.
$wDescription->IOValue()." |
";
$Plan=new Anc_Plan($this->db);
$aPlan=$Plan->get_list();
$ret.='
';
/* show 10 rows */
$ret.="";
foreach ($aPlan as $d)
{
$idx=$d['id'];
/* array of possible value for the select */
$aPoste[$idx]=make_array($this->db,"select po_id as value,".
" html_quote(po_name||':'||po_description) as label ".
" from poste_analytique ".
" where pa_id = ".$idx.
" order by po_name ");
$ret.="| Poste | ";
}
$ret.=" | ".
" Montant | ".
"Débit | ".
"
";
for ($i = 0;$i < $this->nMaxRow;$i++)
{
$ret.="";
foreach ($aPlan as $d)
{
$idx=$d['id'];
// init variable
$wSelect=new widget("select","","pop".$i."plan".$idx);
$wSelect->value=$aPoste[$idx];
$wSelect->table=1;
$wSelect->size=12;
$wSelect->readonly=$p_readonly;
if ( isset($this->a_operation[$i])) {
$wSelect->selected=$this->a_operation[$i]->po_id;
}
$ret.=$wSelect->IOValue();
}
$wAmount=new widget("text","","pamount$i",0.0);
$wAmount->size=12;
$wAmount->table=1;
$wAmount->javascript=" onChange=caod_checkTotal()";
$wAmount->readonly=$p_readonly;
$wDebit=new widget("checkbox","","pdeb$i");
$wDebit->table=1;
$wDebit->readonly=$p_readonly;
$wDebit->javascript=" onChange=caod_checkTotal()";
if ( isset ($this->a_operation[$i])) {
$wSelect->selected=$this->a_operation[$i]->po_id;
$wAmount->value=$this->a_operation[$i]->oa_amount;
$wDebit->value=$this->a_operation[$i]->oa_debit;
if ( $wDebit->value=='t') { $wDebit->selected=true;}
}
// build the table
$ret.=" | ";
$ret.=$wAmount->IOValue();
$ret.=$wDebit->IOValue();
$ret.="
";
}
$ret.="
";
return $ret;
}
/*!\brief fill row from $_POST data
*
*/
function get_from_array($p_array) {
$Plan=new Anc_Plan($this->db);
$aPlan=$Plan->get_list();
for ( $i = 0;$i <$this->nMaxRow;$i++) {
foreach ($aPlan as $d)
{
$idx=$d['id'];
$p=new Anc_Operation($this->db);
$p->oa_amount=$p_array["pamount$i"];
$p->oa_description=$p_array["pdesc"];
$p->oa_date=$p_array['pdate'];
$p->j_id=0;
$p->oa_debit=(isset ($p_array["pdeb$i"]))?'t':'f';
$p->oa_group=0;
$p->po_id=$p_array["pop$i"."plan".$idx];
$p->pa_id=$idx;
$this->a_operation[]=clone $p;
}
}
}
/*!\brief save the group of operation but only if the amount is
balanced */
function save() {
StartSql($this->db);
try {
$oa_group=NextSequence($this->db,'s_oa_group');
for ($i=0;$ia_operation);$i++) {
$this->a_operation[$i]->oa_group=$oa_group;
$this->a_operation[$i]->add();
}
} catch (Exception $ex) {
echo ''.
'Erreur dans l\'enregistrement '.
__FILE__.':'.__LINE__.' '.
$ex->getMessage();
Rollback($p_cn);
exit();
}
Commit($this->db);
}
/*!\brief show the form */
function show() {
return $this->form(1);
}
static function test_me()
{
$dossier=dossier::id();
$cn=DbConnect($dossier);
if ( isset($_POST['go'])) {
$b=new Anc_Group_Operation($cn);
$b->get_from_array($_POST);
exit();
}
$a=new Anc_Group_Operation($cn);
echo '';
}
}