db=$p_cn;
$this->id=$p_id;
$this->date=date("d.m.Y");
$this->nMaxRow=10;
}
/*!\brief add several rows */
function add()
{
$amount=0;
try
{
$this->db->start();
foreach ($this->a_operation as $row)
{
$add=round($row->oa_amount,2);
$add=($row->oa_debit=='t')?$add:$add*(-1);
$amount+=round($add,2);
$row->add();
}
if ( $amount != 0 ) throw new Exception (_('Operation non equilibrée'));
}
catch (Exception $e)
{
record_log($e->getTraceAsString());
echo $e->getTrace();
$this->db->rollback();
throw new Exception($e);
}
$this->db->commit();
}
/*!\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 IDate("pdate",$this->date);
$wDate->table=1;
$wDate->size=10;
$wDate->readonly=$p_readonly;
$wDescription=new IText("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.="".td(_("Date")).td($wDate->input())."
";
$ret.='Description | '.
''.
$wDescription->input()." |
";
$Plan=new Anc_Plan($this->db);
$aPlan=$Plan->get_list();
$max=(count($this->a_operation)<$this->nMaxRow)?$this->nMaxRow:count($this->a_operation);
$ret.='
';
/* show 10 rows */
$ret.="";
$ret.=th(_("Fiche"),'style="text-align:left"').th("");
foreach ($aPlan as $d)
{
$idx=$d['id'];
/* array of possible value for the select */
$aPoste[$idx]=$this->db->make_array("select 0 as value,'-' as label "
. " union select po_id as value,".
" po_name||':'||coalesce(po_description,'-') as label ".
" from poste_analytique ".
" where pa_id = ".$idx.
" order by 2 ");
$ret.="".$d['name']." | ";
}
$ret.=" | ".
""._('Montant') ." | ".
""._("Débit")." | ".
"
";
for ($i = 0;$i < $max;$i++)
{
if ( $p_readonly == 1 && isset($this->a_operation[$i]) && $this->a_operation[$i]->po_id ==0 ) continue;
$ret.="";
$ret.="";
$card=new ICard("qcode$i");
$card->set_attribute("typecard", sprintf("[sql] fd_id in (select fd_id from jnt_fic_attr where ad_id=%s)",ATTR_DEF_ACCOUNT));
$card->set_attribute('label', "qcode{$i}_label");
$card->setReadOnly($p_readonly);
$card->value=(isset($this->a_operation[$i]->card))?$this->a_operation[$i]->card:"";
$ret.=$card->input();
$ret.=$card->search();
$ret.=' | ';
$ret.='';
$label=new ISpan("qcode{$i}_label");
$label->style="vertical-align:top";
$ret.=$label->input();
$ret.=' | ';
foreach ($aPlan as $d)
{
$idx=$d['id'];
// init variable
$wSelect=new ISelect("pop".$i."plan".$idx);
$wSelect->value=$aPoste[$idx];
$wSelect->size=12;
$wSelect->readOnly=$p_readonly;
if ( isset($this->a_operation[$i]))
{
$wSelect->selected=$this->a_operation[$i]->po_id;
}
$ret.=td($wSelect->input());
}
$wAmount=new INum("pamount$i",0.0);
$wAmount->size=12;
$wAmount->table=1;
$wAmount->javascript=" onChange=format_number(this);caod_checkTotal()";
$wAmount->readOnly=$p_readonly;
$wDebit=new ICheckBox("pdeb$i");
$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->input();
$ret.=td($wDebit->input());
$ret.="
";
}
$ret.="
";
if ( $p_readonly==false)
{
$add_row=new IButton('Ajouter');
$add_row->label=_('Ajouter une ligne');
$add_row->javascript='anc_add_row(\'ago\');';
$ret.=HtmlInput::hidden('nbrow',$max);
$ret.=$add_row->input();
}
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 <$p_array['nbrow'];$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_row=$i;
$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->card=(isset($p_array["qcode".$i]))?$p_array["qcode".$i]:"";
$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()
{
$this->db->start();
try
{
$oa_group=$this->db->get_next_seq('s_oa_group');
for ($i=0;$ia_operation);$i++)
{
if ( $this->a_operation[$i]->po_id == 0 ) continue;
$this->a_operation[$i]->oa_group=$oa_group;
$this->a_operation[$i]->add();
}
}
catch (Exception $ex)
{
record_log($e->getTraceAsString());
echo ''.
'Erreur dans l\'enregistrement '.
__FILE__.':'.__LINE__.' '.
$ex->getMessage();
$p_cn->rollback();
throw new Exception("Erreur ".$ex->getMessage());
}
$this->db->commit();
}
/*!\brief show the form */
function show()
{
return $this->form(1);
}
static function test_me()
{
$dossier=dossier::id();
$cn=Dossier::connect();
if ( isset($_POST['go']))
{
$b=new Anc_Group_Operation($cn);
$b->get_from_array($_POST);
return;
}
$a=new Anc_Group_Operation($cn);
echo '';
}
}