diff --git a/include/ac_common.php b/include/ac_common.php index 8e6b31e0f..d9b65ecd8 100644 --- a/include/ac_common.php +++ b/include/ac_common.php @@ -59,6 +59,27 @@ function cmpDate ($p_date,$p_date_oth) { // si $p_date > $p_date_oth return > 0 return $l_mkdate-$l2_mkdate; } +/* function IsNumber + * purpose : check if the argument is a number + * + * parm : $p_int + * + * gen nothing + * + * return : + * 1 it's a number + * 0 it is not + */ +function isNumber($p_int) { + if ( strlen (trim($p_int)) == 0 ) return 0; + if (! ereg ("^[0-9]+$",$p_int) ) { + return 0; + } else { + return 1; + }// !ereg + return $p_date; +} + /* function isDate * Purpose : Verifie qu'une date est bien formaté * en d.m.y et est valable diff --git a/include/constant.php b/include/constant.php index 2cc0ecc1c..fb3983c4b 100644 --- a/include/constant.php +++ b/include/constant.php @@ -77,4 +77,17 @@ define ("NOTCENTRALIZED",3); define ("MENU_FACT",1); define ("MENU_FICHE",2); define ("MENU_PARAM",3); + +// for the fiche_inc.GetSqlFiche function +define ("ALL_FICHE_DEF_REF", 1000); + +// fixed value for attr_def data +define ("ATTR_DEF_ACCOUNT",5); +define ("ATTR_DEF_NAME",1); +define ("ATTR_DEF_PRIX_ACHAT",6); +define ("ATTR_DEF_PRIX_VENTE",7); +define ("ATTR_DEF_TVA",2); +define ("FICHE_TYPE_CLIENT",9); +define ("FICHE_TYPE_VENTE",1); + ?> diff --git a/include/fiche_inc.php b/include/fiche_inc.php index aa5f38410..408231e0b 100644 --- a/include/fiche_inc.php +++ b/include/fiche_inc.php @@ -19,8 +19,29 @@ // Copyright Author Dany De Bontridder ddebontridder@yahoo.fr // $Revision$ +/* fucntion GetSqlFiche + *************************************************** + * purpose : return the sql string which match the p_type + * permit to have all the sql here + * param : $p_type what sql select command u want + * return : string + */ +function GetSqlFiche($p_type) { + switch ($p_type ) { + + // return all the existing fiche_def_ref + case ALL_FICHE_DEF_REF: + return "select frd_id, frd text from fiche_def_ref"; + + default: + echo_error("undefined type cannot return the corresponding select"); + return null; + } + +} /* function addFiche + *************************************************** * Purpose : ajoute une nouvelle fiche * * parm : @@ -35,41 +56,96 @@ */ function AddFiche($p_cn,$p_type,$p_array) { echo_debug(" AddFiche($p_cn,$p_type,$p_array) "); - + // Push the array in element starting with p + // foreach ($p_array as $key=>$element){ ${"p_$key"}=$element; echo_debug("p_$key=$element;"); } - $base=GetBaseFiche($p_cn,$p_type); - $num=GetNextFiche($p_cn,$base); - $sql=sprintf("insert into tmp_pcmn (pcm_val,pcm_lib,pcm_val_parent) + // First Get the attr of the fiche + $field=Get_attr_def($p_cn,$p_fd_id); + + + // Create the Fiche + $Sql="insert into fiche (fd_id) values (".$p_fd_id.")"; + $Res=ExecSql($p_cn,$Sql); + + // Get is f_id (sequence) + $l_f_id=GetSequence($p_cn,'s_fiche'); + // TODO If a class base if given, we should not create automatically + // The class + + // Should we Create accounts for each cards + $create=GetCreateAccount($p_cn,$p_fd_id); + + echo_debug ( " create = $create "); + if ( $create == 1) { + // We create an account for each + // Get The Class Base + $base=GetBaseFiche($p_cn,$p_type); + + // If a specific post account is needed created it + + if ( $base !=null ) { + // if the class base is not null, create it in the tmp_pcmn + $num=GetNextFiche($p_cn,$base); + $sql=sprintf("insert into tmp_pcmn (pcm_val,pcm_lib,pcm_val_parent) values (%s,'%s',%d)", - $num, - $p_nom0, - $base); - echo_debug($sql); - $Res=ExecSql($p_cn,$sql); - $sql=sprintf("insert into fiche ( f_id,f_label,f_fd_id) values (%d,'%s',%d)", - $num,$p_nom0,$p_type); - $Res=ExecSql($p_cn,$sql); - for ( $i = 1; $i < $p_inc;$i++) { - $sql=sprintf("insert into isupp (is_f_id,is_isd_id,is_value) values ( - %d,%d,'%s')", - $num, ${"p_isd_id$i"},${"p_nom$i"}); - echo_debug($sql); - $Res=ExecSql($p_cn,$sql); + $num, + $p_av_text0, + $base); + echo_debug($sql); + $Res=ExecSql($p_cn,$sql); + // Add it the jnt table + // 5 is always for the account post + $Sql=sprintf("insert into jnt_fic_att_value (f_id,ad_id) values (%d,%d)", + $l_f_id,ATTR_DEF_ACCOUNT); + $Res=ExecSql($p_cn,$Sql); + + // Get the jft_id sequence (s_jnt_fic_att_value) + $l_jft_id=GetSequence($p_cn,'s_jnt_fic_att_value'); + + + // Add it the attr_value table + $Sql=sprintf("insert into attr_value(jft_id,av_text) values (%d,'%s')", + $l_jft_id,$num); + $Res=ExecSql($p_cn,$Sql); + } + }// end create accounts for each cards + + + // Add the others attribut in jnt_fic_att_value and after in attr_value + for ( $i = 0; $i < $p_inc;$i++) { + //Except for the class base + if ( ${"p_ad_id$i"} == ATTR_DEF_ACCOUNT ) continue; + + // Add it the jnt table + $Sql=sprintf("insert into jnt_fic_att_value (f_id,ad_id) values (%d,%d)", + $l_f_id,${"p_ad_id$i"}); + $Res=ExecSql($p_cn,$Sql); + + // Get the jft_id sequence (s_jnt_fic_att_value) + $l_jft_id=GetSequence($p_cn,'s_jnt_fic_att_value'); + + + // Add it the attr_value table + $Sql=sprintf("insert into attr_value(jft_id,av_text) values (%d,'%s')", + $l_jft_id,${"p_av_text$i"}); + $Res=ExecSql($p_cn,$Sql); + } } /* function EncodeFiche + *************************************************** * Purpose : Affiche les détails d'une fiche et propose * de mettre à jour * ou si array est a null, permet d'ajouter une - * fiche + * fiche, to fill the attribute * * parm : * - p_cn connexion - * - p_type id du modele fichedef(fd_id) de la fiche SI array est null + * - p_type id du modele fiche_def(fd_id) de la fiche SI array est null * sinon correspond au id d'une fiche fiche(f_id) * gen : * - none @@ -87,21 +163,29 @@ function EncodeFiche($p_cn,$p_type,$p_array=null) { echo ""; if ($p_array == null) { - echo '

'.getFicheName($p_cn,$p_type).'

'; + // Array is null so we display a blank form + // echo '

'.getFicheName($p_cn,$p_type).'

'; + echo '

New

'; $p_f_id=""; echo_debug("Array is null"); - $sql="select isd_id,isd_label from isupp_def where isd_fd_id=".$p_type; + // Find all the attribute of the existing cards + // --> Get_attr_def + $sql="select frd_id,ad_id,ad_text from fiche_def join jnt_fic_attr using (fd_id) + join attr_def using (ad_id) where fd_id=".$p_type." order by ad_id"; $Res=ExecSql($p_cn,$sql); $Max=pg_NumRows($Res); - - echo ''; + // Put the card modele id (fiche_def.fd_id) + echo ''; for ($i=0;$i < $Max;$i++) { $l_line=pg_fetch_array($Res,$i); - $Hid=sprintf('', - $i,$l_line['isd_id']); - printf ('', - $l_line['isd_label'], $i,$Hid); + + // The number of the attribute + $Hid=sprintf('', + $i,$l_line['ad_id']); + // content of the attribute + printf ('', + $l_line['ad_text'], $i,$Hid); } echo ''; echo '
%s %s
%s %s
'; @@ -109,33 +193,45 @@ function EncodeFiche($p_cn,$p_type,$p_array=null) { echo ''; echo ''; }else { + // Array is not null so we have to find the card's data from the database + // and display them - $sql="select f_label,f_fd_id from fiche where f_id=".$p_type; - $Res=ExecSql($p_cn,$sql); - $Max=pg_NumRows($Res); - if ( $Max == 0 ) return; - $l_line=pg_fetch_array($Res,0); - echo '

'.getFicheName($p_cn,$l_line['f_fd_id']).'

'; - printf('
', - $l_line['f_label']); - echo ''; + // Display the card name + $label=getFicheName($p_cn,$p_type); + echo '

'.$label.'

'; + + // Find all the data related to the card + $sql="select av_text,ad_id,ad_text,jft_id from attr_value + natural join attr_def + natural join jnt_fic_att_value + natural join fiche where f_id=$p_type"; - $sql="select isd_fd_id,isd_id,isd_label,is_value,is_id,is_f_id from isupp left join isupp_def on isd_id=is_isd_id where is_f_id=".$p_type; - $Res=ExecSql($p_cn,$sql); $Max=pg_NumRows($Res); echo_debug("Max ==== $Max"); - echo ''; + echo ''; + echo ''; echo ''; + for ($i=0;$i < $Max;$i++) { + // fetch the data $l_line=pg_fetch_array($Res,$i); - - $Hid=sprintf('', - $i,$l_line['is_id']); - printf (' %s %s', - $l_line['isd_label'], $i, $l_line['is_value'],$Hid); + // Put also the class in a special variable + // useful when we want to update the PCMN + // TODO permit the update of TMP_PCMN + if ( $l_line['ad_id'] == ATTR_DEF_ACCOUNT ) { + printf('', + $l_line['av_text']); + + } + // in hidden we put the jft_id + $Hid=sprintf('', + $i,$l_line['jft_id']); + printf (' %s %s', + $l_line['ad_text'], $i, $l_line['av_text'],$Hid); + } echo ''; echo ''; @@ -145,6 +241,7 @@ function EncodeFiche($p_cn,$p_type,$p_array=null) { } /* function GetBaseFiche + *************************************************** * Purpose : donne la classe comptable de base d'une fiche * * parm : @@ -158,17 +255,40 @@ function EncodeFiche($p_cn,$p_type,$p_array=null) { */ function GetBaseFiche($p_cn,$p_type) { $base=null; - $Res=ExecSql($p_cn,"select fd_class_base from fichedef where fd_id=".$p_type); - if ( pg_NumRows($Res) == 0 ) return; + $Res=ExecSql($p_cn,"select fd_class_base from fiche_def where fd_id=".$p_type); + if ( pg_NumRows($Res) == 0 ) return null; $base=pg_fetch_array($Res,0); return $base['fd_class_base']; } + +/* function GetBaseFicheDefault + *************************************************** + * Purpose : Give the default accounts of fiche_def + * + * + * parm : + * - p_cn connexion + * - p_type fiche id + * gen : + * - none + * return: + * - return la classe id ou rien + * + */ +function GetBaseFicheDefault($p_cn,$p_type) { + $base=null; + $Res=ExecSql($p_cn,"select frd_class_base from fiche_def_ref where frd_id=".$p_type); + if ( pg_NumRows($Res) == 0 ) return null; + $base=pg_fetch_array($Res,0); + return $base['frd_class_base']; +} /* function ViewFiche + *************************************************** * Purpose : Montre les fiches d'une rubrique * * parm : * - p_cn connexion - * - $p_type f_id + * - $p_type fiche_def.fd_id catg of card * gen : * - none * return: @@ -176,8 +296,13 @@ function GetBaseFiche($p_cn,$p_type) { * */ function ViewFiche($p_cn,$p_type) { - echo '

'.getFicheName($p_cn,$p_type).'

'; - $Res=ExecSql($p_cn,"select f_id,f_label from fiche where f_fd_id='".$p_type."' order by f_id"); + echo '

'.getFicheDefName($p_cn,$p_type).'

'; + // Get all name the cards of the select category + // 1 for attr_def.ad_id is always the name + $Res=ExecSql($p_cn,"select f_id,av_text from + fiche join jnt_fic_att_value using (f_id) + join attr_value using (jft_id) + where fd_id='".$p_type."' and ad_id=".ATTR_DEF_NAME." order by f_id"); $Max=pg_NumRows($Res); for ( $i = 0; $i < $Max; $i++) { @@ -191,7 +316,7 @@ function ViewFiche($p_cn,$p_type) { if ( $i %2 == 0 ) $div='
'; echo $div.$span_del.' '.' '.' '.' '.' '.' '. - $span_mod." "." "." ".$span_id." "." "." ".$l_line['f_label']."
"; + $span_mod." "." "." ".$span_id." "." "." ".$l_line['av_text'].""; } echo '
'; echo ''; @@ -200,6 +325,7 @@ function ViewFiche($p_cn,$p_type) { } /* function GetNextFiche + *************************************************** * Purpose : Crée le poste suivant pour une fiche en fonction * de la classe de base (fichedef(fd_class_base)) * @@ -227,22 +353,8 @@ function GetNextFiche($p_cn,$p_base) { echo_debug("ret $ret"); return $ret+1; } -/* function - * Purpose : - * - * parm : - * - - * gen : - * - - * return: - * - - * - */ -function GetDataFiche($p_cn,$p_fiche_id) { - -} - /* function ViewFicheDetail + *************************************************** * Purpose : Montre le detail d'une fiche * et ajoute les lignes manquantes * dans le cas où le modèle à changer @@ -257,16 +369,30 @@ function GetDataFiche($p_cn,$p_fiche_id) { * */ function ViewFicheDetail($p_cn,$p_id) { -$sql="insert into isupp (is_f_id,is_isd_id) select". - " '".$p_id."',isd_id from isupp_def ". - " where isd_id not in ". - "(select is_isd_id from isupp where is_f_id=".$p_id.") ". - " and isd_fd_id=(select distinct f_fd_id from fiche ". - " where f_id=".$p_id.")"; + + // Retrieve the fiche_def.fd_id of the card + $fd_id=GetFicheDef($p_cn,$p_id); + + //Update the default attribute of a card if is model has changed + $sql="insert into jnt_fic_att_value (f_id,ad_id) + select $p_id, ad_id + from jnt_fic_attr + where fd_id=$fd_id and + ad_id not in (select ad_id from jnt_fic_att_value where + f_id=$p_id)"; + $Res=ExecSql($p_cn,$sql); + + // add a empty string to attr_value + $sql="insert into attr_value select jft_id,'' from jnt_fic_att_value + where f_id=$p_id and jft_id not in (select jft_id from attr_value natural join jnt_fic_att_value where + f_id = $p_id)"; + $Res=ExecSql($p_cn,$sql); + EncodeFiche ($p_cn,$p_id,1); } /* function UpdateFiche + *************************************************** * Purpose : Met a jour une fiche * change dans le plan comptable, fiche,et isupp * @@ -274,35 +400,91 @@ $sql="insert into isupp (is_f_id,is_isd_id) select". * - p_cn * - p_array * gen : - * - + * - none * return: - * - + * - nothing * */ function UpdateFiche($p_cn,$p_array) { foreach ( $p_array as $key=> $element) { echo_debug("$key => $element"); ${"$key"}=$element; + // Get the name + if ( $key == "ad_id".ATTR_DEF_NAME ) { + $label=$element; + } + // Get the class base + if ( $key=="ad_id".ATTR_DEF_ACCOUNT) { + $class=$element; + } } - $sql=sprintf("update tmp_pcmn set pcm_lib='%s' where pcm_val=%s", - $f_label, - $f_id); - echo_debug($sql); - $Res=ExecSql($p_cn,$sql); - $sql=sprintf("update fiche set f_label='%s' where f_id=%s", - $f_label, - $f_id); - echo_debug($sql); - $Res=ExecSql($p_cn,$sql); + // If each card has it own account must also update the tmp_pcm table + // + $fd_ref=GetFicheDefRef($p_cn,$fiche); + // Update all the others data for ( $i =0 ; $i < $max ; $i++) { - $sql=sprintf("update isupp set is_value='%s' where is_id=%d", - ${"nom$i"}, - ${"is_id$i"}); + $sql=sprintf("update attr_value set av_text='%s' where jft_id=%d", + ${"av_text$i"}, + ${"jft_id$i"}); echo_debug($sql); $Res=ExecSql($p_cn,$sql); + } + // Update the PCMN if needed + $sql="select av_text from attr_value natural join jnt_fic_att_value + where f_id=$fiche and ad_id=5"; + $Res=ExecSql($p_cn,$sql); + + if ( pg_NumRows($Res) != 0 ) { + // it means that a account exists + // Retrieve the name and the ad_id 5 + $f=pg_fetch_array($Res,0); + $class_old=$f['av_text']; + + // if the class changed + if ( $class != $class_old and $class != "" ) { + if ( CountSql($p_cn,"select * from jrnx where j_poste=$class") or + CountSql($p_cn,"select * from jrnx where j_poste=$class_old" )) + { + // No change if the account is already used + echo_error("Not possible to change the account, already used"); + } else { + if ( CountSql($p_cn,"select * from tmp_pcmn where pcm_val=".$class) ==1) { + $Res=ExecSql($p_cn,"update tmp_pcmn set pcm_val=$class_old where pcm_val=$class"); + } else // we have to insert + { + // First we must use a parent + $parent=GetParent($p_cn,$class); + $Res=ExecSql($p_cn, + "insert into tmp_pcmn (pcm_val,pcm_lib,pcm_val_parent) + values ($class,'$f_label',$parent)"); + } + } + $class=$class_old; + } else // $class="" + { + $class=$class_old; + // First we must use a parent + $parent=GetParent($p_cn,$class); + $Res=ExecSql($p_cn, + "insert into tmp_pcmn (pcm_val,pcm_lib,pcm_val_parent) + values ($class,'$f_label',$parent)"); + } + + + // Change the name in TMP_PCMN + // Get the new name + ExecSql($p_cn,"update tmp_pcmn set pcm_lib='".$f_label."' where pcm_val=".$class); + + + } + // Update of TMP_PCMN if a class base is given (ad_id=5) + + + } /* function EncodeModele + *************************************************** * Purpose : * * parm : @@ -324,142 +506,208 @@ function EncodeModele($p_js) echo '
'; echo '
'; } -/* function - * Purpose : +/* function DefModele + *************************************************** + * Purpose : Creation of a model of card or correction * * parm : - * - + * - $p_cn database connection + * - $p_ligne number of lines + * - $p_array array + * - $p_js class base (javascript code) * gen : - * - + * - none * return: - * - + * - nothing * */ -function DefModele ($p_js,$p_array=null,$p_ligne=1) +function DefModele ($p_cn,$p_js,$p_array=null,$p_ligne=1) { echo_debug("DefModele ($p_array,$p_ligne=1) "); - for ($i=0; $i < $p_ligne; $i++) { - ${"p_LABEL$i"}=""; - ${"p_inform$i"}=""; - ${"p_isd_id$i"}=""; - } - if ($p_array != null) { - foreach ( $p_array as $key => $element){ - ${"p_$key"}=$element; - echo_debug ("p_$key $element"); - }//foreach - }else { - $p_nom_mod=""; - $p_class_base=""; - } - echo '
'; - echo ''; - // echo ''; - echo '
Label'; - echo '
classe'.$p_js; + // Creating form + $display=''; - if ( isset($p_fd_id) ) { - echo ''; - } - for ( $i=0; $i < $p_ligne ; $i++ ) { - if ( isset($p_fd_id) ) { - echo ''; + // Number of line of the card + $display.=''; + + // Table of input + $display.=''; + + + //Name of the CARD (fiche_def.fd_label) + $display.=''; + $display.=''; + + // Class base fiche_def.fd_class_base (optional) + $display.=''; + $display.=''; + // Checkbox for the creation of a post + $display.=''; + + //display the different template + $ref=Get_fiche_def_ref($p_cn); + + // check if $ref is an array + // and display the choice + + // select by default the first choice + $check="CHECKED"; + if ( sizeof($ref) ) { + foreach ($ref as $i=>$v) { + $display.='"; + $check=""; } - printf( '
Libellé ', - $i, - ${"p_LABEL$i"} - ); - if ( ${"p_inform$i"} == "on" || ${"p_inform$i"}=='t' ) { - $val="checked"; - } else { - $val=""; - } - printf('Dans les formulaires ', - $i, - $val - ); + } - echo '
'; - if ( isset($p_fd_id) ){ - echo '
'; - }else { - echo '
'; - }//if isset p_fd_id else + // closing the form + $display.='
Catégorie de fiche '; + $display.='
Classe de base '.$p_js; + $display.='
Create accounts for each
'; + + $display.=''; + $display.=$v['text']; + // if a class base exist in fiche_def_ref, we display it + if ( sizeof ($v['class']) != 0 ) + $display.="  Class base = ".$v['class'].""; + $display.="
'; + $display.=''; + $display.='
'; + + // display it + echo $display; - echo '
'; } /* function AddModele - * Purpose : Add a modele into the database - * + *************************************************** + * Purpose : Add a modele of card into the database + * * parm : * - connection * - array * gen : * - none + * table : insert into fiche_def + * insert into attr_def * return: * - none * */ function AddModele($p_cn,$p_array) { echo_debug("AddModele"); - $p_ligne=$p_array['inc']; - for ($i=0; $i < $p_ligne; $i++) { - ${"p_inform$i"}=""; - } - + // Show what we receive for debug purpose only + // foreach ( $p_array as $key=>$element ) { echo_debug("p_$key $element"); ${"p_$key"}=$element; } + // Format correctly the name of the cat. of card $p_nom_mod=FormatString($p_nom_mod); - $p_class_base=FormatString($p_class_base); - $sql=sprintf("insert into fichedef(fd_label,fd_class_base) values ('%s','%s')", - $p_nom_mod,$p_class_base); - $Res=ExecSql($p_cn,$sql); + echo_debug("Adding $p_nom_mod"); + // Format the p_class_base + // must be an integer + if ( isNumber($p_class_base) == 0 && FormatString($p_class_base) != null ) { + echo_error ('p_class_base is NOT a number'); + } - $f_id=GetSequence($p_cn,"s_fdef"); - - for ( $i=0; $i < $p_inc; $i++) { - if ( ${"p_inform$i"} == "on" ) { - $valform="true"; - }else { - $valform="false"; + // $p_FICHE_REF cannot be null !!! (== fiche_def_ref.frd_id + if (! isset ($p_FICHE_REF) or strlen($p_FICHE_REF) == 0 ) { + echo_error ("AddModele : fiche_ref MUST NOT be null or empty"); + return; + } + // build the sql request for fiche_def + // and insert into fiche_def + // if p_class_base is null get the default class base from + // fiche_def_ref + if ( FormatString($p_class_base) == null ) + { // p_class is null + // So we take the default one + $p_class_base=GetBaseFicheDefault($p_cn,$p_FICHE_REF); } - ${"p_LABEL$i"}=FormatString(${"p_LABEL$i"}); - if ( ${"p_LABEL$i"} != null ) { - $sql=sprintf("insert into isupp_def(isd_label,isd_fd_id,isd_form) values ('%s',%d,%s)", - ${"p_LABEL$i"},$f_id,$valform); + // Set the value of fiche_def.fd_create_account + if ( isset($p_create)) + $p_create='true'; + else + $p_create='false'; + + // Class is valid ? + if ( FormatString($p_class_base) != null) { + // TODO verify that the class base exists + // p_class is a valid number + $sql=sprintf("insert into fiche_def(fd_label,fd_class_base,frd_id,fd_create_account) + values ('%s',%s,%d,'%s')", + $p_nom_mod,$p_class_base,$p_FICHE_REF,$p_create); + + $Res=ExecSql($p_cn,$sql); + + // Get the fd_id + $fd_id=GetSequence($p_cn,'s_fdef'); + + // Add the class_base if needed + // TODO replace 5 by the definition of class_base + if ( $p_create=='true' ) { + $sql=sprintf("insert into jnt_fic_attr(fd_id,ad_id) + values (%d,%d)",$fd_id,ATTR_DEF_ACCOUNT); $Res=ExecSql($p_cn,$sql); } + } else { + //There is no class base not even as default + $sql=sprintf("insert into fiche_def(fd_label,frd_id,fd_create_account) values ('%s',%d,'%s')", + $p_nom_mod,$p_FICHE_REF,$p_create); + + $Res=ExecSql($p_cn,$sql); + + // Get the fd_id + $fd_id=GetSequence($p_cn,'s_fdef'); + } + + // Get the default attr_def from attr_min + $def_attr=Get_attr_min($p_cn,$p_FICHE_REF); + + //if defaut attr not null + // build the sql insert for the table attr_def + if (sizeof($def_attr) != 0 ) { + // insert all the mandatory fields into jnt_fiche_attr + foreach ( $def_attr as $i=>$v) { + $sql=sprintf("insert into jnt_fic_Attr(fd_id,ad_id) + values (%d,%s)", + $fd_id,$v['ad_id']); + ExecSql($p_cn,$sql); + } + } + + // TODO show the details of the created catg of card } /* function UpdateModele - * Purpose : + *************************************************** + * Purpose : Modify the model of card + * change some attribute (fd_label) and permit + * to add line * * parm : * - p_cn connexion * - p_fiche fichedef(f_id) - * - p_js * gen : * - none * return: * - none * */ -function UpdateModele($p_cn,$p_fiche,$p_js) { +function UpdateModele($p_cn,$p_fiche) { $array=GetDataModele($p_cn,$p_fiche); if ($array==null) { echo_error ("fiche_inc:UpdateModele Fiche non trouvée"); return; } - echo '

'.getFicheName($p_cn,$p_fiche).'

'; + echo '

'.getFicheDefName($p_cn,$p_fiche).'

'; foreach ( $array as $key=>$element) echo_debug("$key => $element"); - DefModele($p_js,$array,$array['ligne']); + DisplayDetailModele($p_cn,$array,$array['ligne']); } /* function GetDataModele + *************************************************** * Purpose : Return the info of an fiche identified by * p_fiche * @@ -473,89 +721,38 @@ function UpdateModele($p_cn,$p_fiche,$p_js) { * */ function GetDataModele($p_cn,$p_fiche) { - $Res=ExecSql($p_cn,"select fd_id,fd_label,fd_class_base, - isd_id,isd_label,isd_form - from fichedef inner join isupp_def on isd_fd_id=fd_id - where fd_id=$p_fiche"); - $Max=pg_NumRows($Res); + $Res=ExecSql($p_cn,"select fd_class_base,ad_id,ad_text from + fiche_def + natural join jnt_fic_attr + natural join attr_def + where + fd_id=$p_fiche"); + $Max=pg_NumRows($Res) ; if ($Max==0) return null; + + // store the data in an array for ($i=0; $i < $Max;$i++) { $line=pg_fetch_array($Res,$i); if ($i == 0 ) { - $array["nom_mod"]=$line['fd_label']; - $array["class_base"]=$line['fd_class_base']; - $array["fd_id"]=$line['fd_id']; + $array['label']=GetFicheDefName($p_cn,$p_fiche); + $array['class_base']=$line['fd_class_base']; + $array['fd_id']=$p_fiche; }//if $i == 0 - //inform - $text=sprintf("inform%d",$i); - $array[$text]=$line['isd_form']; + //Label - $text=sprintf("LABEL%d",$i); - $array[$text]=$line['isd_label']; + $text=sprintf("ad_id%d",$i); + $array[$text]=$line['ad_id']; //isd_id - $text=sprintf("isd_id%d",$i); - $array[$text]=$line['isd_id']; + $text=sprintf("ad_text%d",$i); + $array[$text]=$line['ad_text']; }//for $array['ligne']=$Max; return $array; } - -/* function SaveModele - * Purpose : Sauve un modele de fiche - * - * parm : - * - connexion - * - tableau de valeurs - * gen : - * - none - * return: - * - none - * - */ -function SaveModele($p_cn,$p_array) { - for ($i=0; $i < $p_array['inc']; $i++) { - ${"inform$i"}=""; - } - - foreach ($p_array as $key=>$element){ - echo_debug("$key => $element"); - ${"$key"}=$element; - }//foreach - $nom_mod=FormatString($nom_mod); - $class_base=FormatString($class_base); - if ( $nom_mod != null ) { - $sql=sprintf("update fichedef set fd_label='%s', fd_class_base='%s' where fd_id=%d", - $nom_mod,$class_base,$fd_id); - $Res=ExecSql($p_cn,$sql); - } - for ($i=1;$i<$inc;$i++){ - if ( ${"inform$i"} != null) { - $valform='true'; - }else { - $valform='false'; - } //if ( ${"inform$d") != null) - - if ( ${"isd_id$i"} != null ) { - ${"LABEL$i"}=FormatString(${"LABEL$i"}); - if ( ${"LABEL$i"} != null ) { - $sql=sprintf("update isupp_def set isd_label='%s',isd_form=%s where isd_id=%d", - ${"LABEL$i"},$valform,${"isd_id$i"}); - } - } // if ( ${"p_isd_id$i"} != null ) - else { - ${"LABEL$i"}=FormatString(${"LABEL$i"}); - if ( ${"LABEL$i"} != null ) { - $sql=sprintf("insert into isupp_def( isd_label,isd_fd_id,isd_form) - values ('%s',%d,%s)", - ${"LABEL$i"},$fd_id,$valform); - }// if ( ${"LABEL$i"} != null ) - }//else if ( ${"p_isd_id$i"} != null ) - $Res=ExecSql($p_cn,$sql); - }//for -} /* function Remove - * Purpose : enleve une fiche dans isupp et fiche + *************************************************** + * Purpose : enleve une fiche dans attr_value, jnt_fic_att_value et fiche * a la condition que ce poste n'aie jamais * été utilisé * parm : @@ -572,14 +769,38 @@ function Remove ($p_cn, $p_fid) { echo_error ("Remove Missing Parameter p_cn = $p_cn p_fid=$p_fid"); return; } - if ( CountSql($p_cn,"select * from jrnx where j_poste=$p_fid") == 0 ) { - $Res=ExecSql($p_cn,"delete from isupp where is_f_id=$p_fid"); - $Res=ExecSql($p_cn,"delete from fiche where f_id=$p_fid"); - } else { - echo ""; + // if the card has its own account in PCMN + // Get the fiche_def.fd_id from fiche.f_id + $fd_id=GetFicheDef($p_cn,$p_fid); + if ( GetCreateAccount($p_cn,$fd_id) == 1 ) { + // Retrieve the class + $class=GetClass($p_cn,$p_fid); + + // if class is not NULL and if we use it before, we can't remove it + if (FormatString($class) != null && + CountSql($p_cn,"select * from jrnx where j_poste=$class") != 0 ) { + echo ""; + return; + } else { + // Remove in PCMN + ExecSql($p_cn,"delete from tmp_pcmn where pcm_val=".$class); + } + } + // Remove from attr_value + $Res=ExecSql($p_cn,"delete from attr_value + where jft_id in (select jft_id + from jnt_fic_att_value + natural join fiche where f_id=$p_fid)"); + // Remove from jnt_fic_att_value + $Res=ExecSql($p_cn,"delete from jnt_fic_att_value where f_id=$p_fid"); + + // Remove from fiche + $Res=ExecSql($p_cn,"delete from fiche where f_id=$p_fid"); + } /* function getFicheName + *************************************************** * Purpose : retourne le nom de la fiche * * parm : @@ -591,24 +812,54 @@ function Remove ($p_cn, $p_fid) { * - string avec nom fiche */ function getFicheName($p_cn,$p_id) { - - $Res=ExecSql($p_cn,"select fd_label from fichedef where fd_id='".$p_id."'"); + // Retrieve the attribute with the ad_id 1 + // 1 is always the name + // TODO replace absolute value by defined value + $Res=ExecSql($p_cn,"select av_text from + attr_value + natural join jnt_fic_att_value + natural join fiche + where + f_id=$p_id and + ad_id=".ATTR_DEF_NAME); + if ( pg_NumRows($Res) == 0 ) return "Unknown"; + $st=pg_fetch_array($Res,0); + return $st['av_text']; +} +/* function getFicheDefName + *************************************************** + * Purpose : retourne le nom de la cat. de fiches + * + * parm : + * - p_cn connexion + * - p_id fiche id fiche_def.fd_id + * gen : + * - none + * return: + * - string fiche_def.fd_label + */ +function getFicheDefName($p_cn,$p_id) { + $Res=ExecSql($p_cn,"select fd_label from fiche_def where fd_id=$p_id"); if ( pg_NumRows($Res) == 0 ) return "Unknown"; $st=pg_fetch_array($Res,0); return $st['fd_label']; } + /* function GetFicheJrn + *************************************************** * Purpose : Get all the fiche related to a "journal" * * parm : * - p_cn connextion * - j_jrn journal_id * - $p_type : deb or cred + * - $p_fiche_type : fiche_def_ref.frd_id to know what kind of card is asked + * -> Sell, Customer... * gen : * - none * return: array containing the fiche(f_id),fiche(f_label) */ -function GetFicheJrn($p_cn,$p_jrn,$p_type) +function GetFicheJrn($p_cn,$p_jrn,$p_type,$p_fiche_type) { $get=""; if ( $p_type == 'deb' ) { @@ -633,8 +884,14 @@ function GetFicheJrn($p_cn,$p_jrn,$p_type) echo_warning("No fiche"); return null; } - - $sql="select f_id, f_label from fiche where f_fd_id in (".$list['fiche'].") order by f_label"; + $sql="select f_id,av_text as f_label + from fiche natural join jnt_fic_att_value + natural join attr_def + natural join attr_value + natural join fiche_def_ref + where ad_id=1 and + fd_id in (".$list['fiche'].") and frd_id=$p_fiche_type order by f_label"; + $Res=ExecSql($p_cn,$sql); $Max=pg_NumRows($Res); if ($Max==0 ) return null; @@ -647,5 +904,362 @@ function GetFicheJrn($p_cn,$p_jrn,$p_type) } return $a; } +/* function Get_fiche_def_ref + *************************************************** + * Purpose : + * return an array containing all the + * data contained in the table fiche_def_ref + * parm : + * - $p_cn connection to the database + * gen : + * - none + * return: + * array if ok null otherwize + */ +function Get_fiche_def_ref($p_cn) +{ + // get all the data from fiche_def_ref + $Res=ExecSql($p_cn, "select frd_id,frd_text,frd_class_base + from fiche_def_ref + order by frd_text"); + + // check if the result is valid + $Max=pg_NumRows($Res); + if ( $Max == 0) return null; + + // store the result in a array + for ($i=0;$i<$Max;$i++) { + $f=pg_fetch_array($Res,$i); + $array[$i]['id']=$f['frd_id']; + $array[$i]['text']=$f['frd_text']; + $array[$i]['class']=$f['frd_class_base']; + } + + // return result + return $array; +} +/* function Get_attr_min + *************************************************** + * Purpose : retrieve the mandatory field of the card model + * + * parm : + * - $p_cn database connexion + * - $p_fiche_def_ref + * gen : + * - + * return: + * array of ad_id (attr_min.ad_id) and labels (attr_def.ad_text) + */ +function Get_attr_min($p_cn,$p_fiche_def_ref) { + // find the min attr for the fiche_def_ref + $Sql="select ad_id,ad_text from attr_min natural join attr_def + natural join fiche_def_ref + where + frd_id= $p_fiche_def_ref"; + $Res=ExecSql($p_cn,$Sql); + $Num=pg_NumRows($Res); + + // test the number of returned rows + if ($Num == 0 ) return null; + + // Get Results & Store them in a array + for ($i=0;$i<$Num;$i++) { + $f=pg_fetch_array($Res,$i); + $array[$i]['ad_id']=$f['ad_id']; + $array[$i]['ad_text']=$f['ad_text']; + } + return $array; +} +/* function Get_attr_def + *************************************************** + * Purpose : retrieve the fields of the card model + * + * parm : + * - $p_cn database connexion + * - $p_fiche_def_ref fiche_def.fd_id + * gen : + * - + * return: + * array of ad_id (attr_def.ad_id) and labels (attr_def.ad_text) + */ +function Get_attr_def($p_cn,$p_fiche_def) { + // find the min attr for the fiche_def_ref + $Sql="select ad_id,ad_text from attr_def + natural join jnt_fic_attr + natural join fiche_def + where + fd_id= $p_fiche_def order by ad_id"; + $Res=ExecSql($p_cn,$Sql); + $Num=pg_NumRows($Res); + + // test the number of returned rows + if ($Num == 0 ) return null; + + // Get Results & Store them in a array + for ($i=0;$i<$Num;$i++) { + $f=pg_fetch_array($Res,$i); + $array[$i]['ad_id']=$f['ad_id']; + $array[$i]['ad_text']=$f['ad_text']; + } + return $array; +} +/* function GetCreateAccount + *************************************************** + * Purpose : retrieve the fields of the card model + * which indicate if each cards needs its own account + * parm : + * - $p_cn database connexion + * - $p_fiche_def_ref fiche_def.fd_id + * gen : + * - + * return: + * not null if yes, each cards has its own account + */ +function GetCreateAccount($p_cn,$p_fiche_def) { + // find the min attr for the fiche_def_ref + $Sql="select fd_create_account + from fiche_def + where + fd_id= $p_fiche_def"; + $Res=ExecSql($p_cn,$Sql); + $Num=pg_NumRows($Res); + + // test the number of returned rows + if ($Num == 0 ) return 0; + + + for ($i=0;$i<$Num;$i++) { + $f=pg_fetch_array($Res,$i); + echo_debug ("fd_create_account == ".$f['fd_create_account']); + if ( $f['fd_create_account']=='t') { + echo_debug("fd_create_account return 1"); + return 1; + } + } + echo_debug("fd_create_account return 0"); + return 0; +} +/* function GetFicheDefRef + *************************************************** + * Purpose : retrieve the fiche_def.frd_id thanks the f_id + * of a card + * + * parm : + * - $p_cn connection + * - $p_f_id fiche.f_id + * gen : + * - none + * return: + * - the fiche_def.frd_id or null if nothing has been found + */ +function GetFicheDefRef($p_cn,$p_f_id) +{ + // Sql stmt + $sql="select frd_id from fiche_def join fiche using (fd_id) + where f_id=$p_f_id"; + // Execute it + $Res=ExecSql($p_cn,$sql); + + // nothing is found + if ( pg_NumRows($Res) == 0 ) return null; + + // Fetch the data + $f=pg_fetch_array($Res,0); + + // return the value + return $f['frd_id']; + +} +/* function GetFicheRef + *************************************************** + * Purpose : retrieve the fiche_def.fd_id thanks the f_id + * of a card + * + * parm : + * - $p_cn connection + * - $p_f_id fiche.f_id + * gen : + * - none + * return: + * - the fiche_def.fd_id or null if nothing has been found + */ +function GetFicheDef($p_cn,$p_f_id) +{ + // Sql stmt + $sql="select fd_id from fiche_def join fiche using (fd_id) + where f_id=$p_f_id"; + // Execute it + $Res=ExecSql($p_cn,$sql); + + // nothing is found + if ( pg_NumRows($Res) == 0 ) return null; + + // Fetch the data + $f=pg_fetch_array($Res,0); + + // return the value + return $f['fd_id']; + +} + +/* function GetClass + *************************************************** + * Purpose : Retrieve the account of a card + * + * parm : + * - connection + * - card id (fiche.f_id) + * gen : + * - none + * return: + * string if a class if found or null otherwise + */ +function GetClass($p_cn,$p_fid) { + echo_debug("GetClass($p_fid)"); + + // the account class is in the av_text with the ad_id=5 in + // attr_Def + $sql="select av_text from attr_value + natural join jnt_fic_att_value + natural join attr_def + where + ad_id=".ATTR_DEF_ACCOUNT." and f_id=$p_fid"; + // Exec + $Res=ExecSql($p_cn,$sql); + if (pg_NumRows($Res) == 0 ) return null; + + // Fetch the data and return it + $f=pg_fetch_array($Res,0); + return $f['av_text']; +} +/* function InsertModeleLine + ************************************************** + * Purpose : Insert a new row into jnt_fic_attr + * for adding a new attribute to the card model + * parm : + * - p_cn connection + * - the fiche_def.fd_id, id of the cat of the model + * - the ad_id is the attr_def.ad_id + * gen : + * - none + * return: none + */ +function InsertModeleLine($p_cn,$p_fid,$p_adid) +{ + // Insert a new attribute for the model + // it means insert a row in jnt_fic_attr + $sql=sprintf("insert into jnt_fic_attr (fd_id,ad_id) values (%d,%d)", + $p_fid,$p_adid); + $Res=ExecSql($p_cn,$sql); + +} +/* function SaveModeleName + ************************************************** + * Purpose : Update the model's name + * + * parm : + * - p_cn connection + * - the fiche_def.fd_id, id of the cat of the model + * - the label + * gen : + * - none + * return: none + */ +function SaveModeleName($p_cn,$p_fid,$p_label) +{ + // Insert a new attribute for the model + // it means insert a row in jnt_fic_attr + $sql=sprintf("update fiche_def set fd_label='%s' where + fd_id=%d", + $p_label,$p_fid); + $Res=ExecSql($p_cn,$sql); + +} + +/* function DisplayDetailModele + ************************************************** + * Purpose : Show the data contained in an array of + * a model of card (fiche_def) + * + * parm : + * - p_cn database connection + * - array containing + * label,class_base,fd_id,ad_textX,ad_idX + * - number of line + * gen : + * - none + * return: + * none + */ +function DisplayDetailModele($p_cn,$p_array,$MaxLine) +{ + echo_debug("DisplayDetailModele"); + + foreach ($p_array as $v=>$i) { + echo_debug("v == $v i==$i"); + ${"$v"}=$i; + } + echo ''; + echo ''; + + printf(""); + // Display each attribute + for ($i=0;$i<$MaxLine;$i++) { + echo ''; + } + + // Show the possible attribute which are not already attribute of the model + // of card + $Res=ExecSql($p_cn,"select ad_id,ad_text from attr_def + where ad_id not in (select ad_id from fiche_def natural join jnt_fic_attr + where fd_id=$fd_id)"); + $M=pg_NumRows($Res); + + // Show the unused attribute + echo ''; + printf("
'; + // Can change the name + if ( ${"ad_id$i"} == ATTR_DEF_NAME ) { + printf('Label', + $label); + printf(''); + } else { + // The attr. + printf('%s ', + ${"ad_text$i"}); + } + echo '
'; + echo ''; + echo '
"); + echo '
'; +} +/* function GetParent + ************************************************** + * Purpose : Get the parent in tmp_pcmn + * + * parm : + * - cn database connextion + * - the base we want insert + * gen : + * - + * return: the parent + * + */ +function GetParent($p_cn,$p_val) +{ + $len=strlen($p_val)-1; + for ($i=$len-1;$i>0;$i--) { + $a=substr($p_val,0,$i); + echo_debug ("parent == $a len =1"); + if (CountSql($p_cn,"select pcm_val from tmp_pcmn where pcm_val=$a") == 1) + return $a; + } +} ?> diff --git a/include/top_menu_compta.php b/include/top_menu_compta.php index 585e77c5f..af30946cf 100644 --- a/include/top_menu_compta.php +++ b/include/top_menu_compta.php @@ -116,7 +116,7 @@ function ShowMenuComptaLeft($p_dossier,$p_item) // A test must be added echo ' Creation'; - $Res=ExecSql($cn,"select fd_id,fd_label from fichedef order by fd_label"); + $Res=ExecSql($cn,"select fd_id,fd_label from fiche_def order by fd_label"); $Max=pg_NumRows($Res); for ( $i=0; $i < $Max;$i++) { $l_line=pg_fetch_array($Res,$i);