bc_id=$id; $this->db=$p_cn; } /*!\brief insert a record into the table bud_card *\return return the string Sauve or throw an exception */ function add () { if ( isNumber($this->bc_price_unit) == false ) $this->bc_price_unit=0; $array=array( $this->bc_code, $this->bc_description, $this->bc_price_unit, $this->bc_unit, $this->bh_id ); $sql="insert into bud_card (bc_code,bc_description,bc_price_unit,bc_unit,bh_id) ". " values (substr($1,1,10),$2,$3,substr($4,1,20),$5) returning bc_id "; try { $a=$this->db->exec_sql($sql,$array); $x=Database::fetch_array($a,0); $this->bc_id=$x['bc_id']; } catch (Exception $e) { if ( DEBUG == 'true' ) print_r($e); return 'Impossible de sauver'; } return 'Sauve'; } /*!\brief update the table bud_card *\return return the string Sauve or throw an exception */ function update() { if ( $this->bc_id == 0) return; if ( isNumber($this->bc_price_unit) == false ) $this->bc_price_unit=0; $sql="update bud_card set bc_code=substr($1,1,10),". "bc_description=$2,". "bc_price_unit=$3, ". "bc_unit=substr($4,1,20) ". " where bc_id=$5"; $array=array( $this->bc_code, $this->bc_description, $this->bc_price_unit, $this->bc_unit, $this->bc_id ); try { $this->db->exec_sql($sql,$array); }catch (Exception $e) { if ( DEBUG == 'true' ) print_r($e); return 'Impossible de sauver'; } return 'Sauve'; } /*! *\brief convert an array to an object, if a idx is not set then the * corresponding property will be null * \param $p_array array to convert */ function get_from_array($p_array) { if ( empty($p_array) ) return; foreach (array ('bh_id','bc_id','bc_code','bc_description','bc_price_unit','bc_unit') as $key ){ $this->$key=(isset($p_array[$key]))?$p_array[$key]:null; } /* if ( $this->bc_id == null ) */ /* throw new Exception(__FILE__.":".__LINE__." bc ne peut pas etre nul"); */ } /*!\brief delete from bud_card the bc_id must be set * *\return Efface */ function delete() { $this->db->exec_sql("delete from bud_card where bc_id=".$this->bc_id); return 'Efface'; } /*!\brief load a bud_card from the database * * */ function load() { if ( $this->bc_id == 0 ) return ; $sql="select bc_id,bc_code,bc_description, bc_price_unit,bc_unit,bh_id ". " from bud_card ". " where ". " bc_id =".$this->bc_id; $res=$this->db->exec_sql($sql); if ( Database::num_row($res) == 0 ) return null; $a=Database::fetch_array($res,0); $this->get_from_array($a); } /*!\brief return a list of all the record of all the budcard *\param $p_cn connection *\param $p_bh_id the hypothese id *\return an array of Bud_Card objects */ static function get_list($p_cn,$p_bh_id) { $sql="select * from bud_card where bh_id = $p_bh_id"; $r=$p_cn->exec_sql($sql); $get=Database::fetch_all($r); if (empty ($get)) return array(); $result=array(); foreach ($get as $row ) { $obj=new Bud_Card($p_cn); $obj->get_from_array($row); $result[]=clone $obj; } return $result; } /*!\brief return a string with the form * *\return string */ function form() { $wCode=new IText("Code","bc_code",$this->bc_code); $wDescription=new IText("Description","bc_description",$this->bc_description); $wPriceUnit=new IText("Prix/unit","bc_price_unit",$this->bc_price_unit); $wUnit=new IText("Unit","bc_unit",$this->bc_unit); $wCode->table=1; $wDescription->table=1; $wPriceUnit->table=1; $wUnit->table=1; $r=""; $r.="