From 34a193ee4ae2ba9a0c848cc75e209c4ec8f1e4b9 Mon Sep 17 00:00:00 2001 From: sparkyx Date: Wed, 9 Aug 2023 13:09:31 +0200 Subject: [PATCH] Improve autonumbering accounting, to force accounting in parent class --- include/constant.php | 2 +- include/sql/patch/upgrade188.sql | 166 ++++++++++++++++++ ...c_account.Test.php => acc_accountTest.php} | 0 unit-test/include/class/card_propertyTest.php | 24 +++ unit-test/include/class/ficheTest.php | 72 +++++++- 5 files changed, 256 insertions(+), 8 deletions(-) create mode 100644 include/sql/patch/upgrade188.sql rename unit-test/include/class/{acc_account.Test.php => acc_accountTest.php} (100%) diff --git a/include/constant.php b/include/constant.php index 6e84eacfc..6e2607785 100644 --- a/include/constant.php +++ b/include/constant.php @@ -116,7 +116,7 @@ if (!defined("NOALYSS_PACKAGE_REPOSITORY")) { if (!defined("SYSINFO_DISPLAY")) { define("SYSINFO_DISPLAY", TRUE); } -define("DBVERSION", 188); +define("DBVERSION", 189); define("MONO_DATABASE", 25); define("DBVERSIONREPO", 20); define('NOTFOUND', '--not found--'); diff --git a/include/sql/patch/upgrade188.sql b/include/sql/patch/upgrade188.sql new file mode 100644 index 000000000..7deff64df --- /dev/null +++ b/include/sql/patch/upgrade188.sql @@ -0,0 +1,166 @@ +begin; +CREATE OR REPLACE FUNCTION comptaproc.account_compute(p_f_id integer) + RETURNS account_type +AS $function$ +declare + class_base fiche_def.fd_class_base%type; + maxcode numeric; + sResult account_type; + bAlphanum bool; + sName text; + nCount integer; + sNumber text; +begin + -- patch 189 + select fd_class_base into class_base + from + fiche_def join fiche using (fd_id) + where + f_id=p_f_id; + + bAlphanum := account_alphanum(); + if bAlphanum = false then + select max (pcm_val::numeric) into maxcode + from tmp_pcmn + where pcm_val_parent = class_base and pcm_val !~* '[[:alpha:]]' ; + if maxcode is null or length(maxcode::text) < length(class_base)+4 then + maxcode:=class_base::numeric*10000+1; + else + select max (pcm_val::numeric) into maxcode + from tmp_pcmn + where pcm_val !~* '[[:alpha:]]' + and pcm_val_parent = class_base + and substr(pcm_val::text,1,length(class_base))=class_base; + + sNumber := substr(maxcode::text,length(class_base)+1); + nCount := sNumber::numeric+1; + sNumber := lpad (nCount::text,4,'0'); + + maxcode:=class_base||sNumber; + end if; + sResult:=maxcode::account_type; + else + -- if alphanum, use name + select ad_value into sName from fiche_detail where f_id=p_f_id and ad_id=1; + if sName is null then + raise exception 'Cannot compute an accounting without the name of the card for %',p_f_id; + end if; + sResult := account_compute_alpha(class_base,sName); + end if; + return sResult; +end; +$function$ +LANGUAGE plpgsql; + + +CREATE OR REPLACE FUNCTION comptaproc.account_add(p_id account_type, p_name character varying) + RETURNS text +AS $function$ +declare + nParent tmp_pcmn.pcm_val_parent%type; + nCount integer; + sReturn text; +begin + -- patch 189 + sReturn:= format_account(p_id); + select count(*) into nCount from tmp_pcmn where pcm_val=sReturn; + if nCount = 0 then + nParent=account_parent(p_id); + insert into tmp_pcmn (pcm_val,pcm_lib,pcm_val_parent) + values (p_id, p_name,nParent) returning pcm_val into sReturn; + end if; + return sReturn; +end ; +$function$ +LANGUAGE plpgsql; + + +CREATE OR REPLACE FUNCTION comptaproc.account_insert(p_f_id integer, p_account text) + RETURNS text +AS $function$ +declare + nParent tmp_pcmn.pcm_val_parent%type; + sName varchar; + sNew tmp_pcmn.pcm_val%type; + bAuto bool; + nFd_id integer; + sClass_Base fiche_def.fd_class_base%TYPE; + nCount integer; + first text; + second text; + s_account text; +begin + -- patch 189 + -- accouting is given + if p_account is not null and length(trim(p_account)) != 0 then + -- if there is coma in p_account, treat normally + if position (',' in p_account) = 0 then + s_account := format_account(substr( p_account,1 , 40)::account_type); + select count(*) into nCount from tmp_pcmn where pcm_val=s_account::account_type; + if nCount !=0 then + perform attribut_insert(p_f_id,5,s_account); + else + -- account doesn't exist, create it + select ad_value into sName from + fiche_detail + where + ad_id=1 and f_id=p_f_id; + -- retrieve parent account from card + select fd_class_base::account_type into nParent from fiche_def where fd_id=(select fd_id from fiche where f_id=p_f_id); + if nParent = null or nParent = '' then + nParent:=account_parent(s_account::account_type); + end if; + insert into tmp_pcmn(pcm_val,pcm_lib,pcm_val_parent) values (s_account::account_type,sName,nParent); + perform attribut_insert(p_f_id,5,s_account); + + end if; + return s_account; + else + -- there is 2 accounts separated by a comma + first := split_part(p_account,',',1); + second := split_part(p_account,',',2); + -- check there is no other coma + + if position (',' in first) != 0 or position (',' in second) != 0 then + raise exception 'Too many comas, invalid account'; + end if; + perform attribut_insert(p_f_id,5,p_account); + + end if; + return s_account; + end if; + + select fd_id into nFd_id from fiche where f_id=p_f_id; + bAuto:= account_auto(nFd_id); + + select fd_class_base into sClass_base from fiche_def where fd_id=nFd_id; + if bAuto = true and sClass_base similar to '[[:digit:]]*' then + sNew:=account_compute(p_f_id); + select ad_value into sName from + fiche_detail + where + ad_id=1 and f_id=p_f_id; + nParent:=sClass_Base::account_type; + sNew := account_add (sNew,sName); + update tmp_pcmn set pcm_val_parent=nParent where pcm_val=sNew; + perform attribut_insert(p_f_id,5,sNew); + return sNew; + else + -- if there is an account_base then it is the default + if trim(coalesce(sClass_base::text,'')) = '' then + perform attribut_insert(p_f_id,5,null); + else + perform attribut_insert(p_f_id,5,sClass_base); + end if; + return sClass_base; + end if; + + raise notice 'ai89.account_insert nothing done : error'; + +end; +$function$ +LANGUAGE plpgsql; + + +insert into version (val,v_description) values (189,'Compute properly accounting'); +commit; \ No newline at end of file diff --git a/unit-test/include/class/acc_account.Test.php b/unit-test/include/class/acc_accountTest.php similarity index 100% rename from unit-test/include/class/acc_account.Test.php rename to unit-test/include/class/acc_accountTest.php diff --git a/unit-test/include/class/card_propertyTest.php b/unit-test/include/class/card_propertyTest.php index ebf5beeb4..5bdf47bb3 100644 --- a/unit-test/include/class/card_propertyTest.php +++ b/unit-test/include/class/card_propertyTest.php @@ -265,4 +265,28 @@ class Card_PropertyTest extends TestCase $this->assertEquals($expected,$result["input"]->value," Accounting incorrect "); } + /** + * @testdox test auto numbering of card for accounting. + * @return void + */ + public function testCardAutoNumbering() + { + $g_connection=Dossier::connect(); + $g_connection->start(); + $fiche_def=$this->getFicheDef(); + $fiche_def->save_class_base('620'); + + $g_connection->exec_sql("update fiche_def set fd_create_account=true where fd_id=25"); + + $fiche=$this->getFiche(); + $fiche->setAttribut(ATTR_DEF_ACCOUNT, null); + $aProperty=$fiche->to_array(); + Card_Property::update($fiche); + $g_connection->commit(); + + $fiche->load(); + + $this->assertTrue(!empty($fiche->strAttribut(ATTR_DEF_ACCOUNT)),' accounting not computed'); + + } } diff --git a/unit-test/include/class/ficheTest.php b/unit-test/include/class/ficheTest.php index 43b0da2f9..a02a1723b 100644 --- a/unit-test/include/class/ficheTest.php +++ b/unit-test/include/class/ficheTest.php @@ -529,7 +529,7 @@ where } } /** - * Create a card + * @brief Create a card * @param type $p_qcode * @returns \Fiche */ @@ -557,12 +557,36 @@ where $fiche_def->set_autocreate(true); $fiche_def->save_class_base('600'); $fiche=$this->build_fiche(2,'TESTACCOUNT'); - $start=$fiche->id; - $this->assertEquals('600001',$fiche->strAttribut(ATTR_DEF_ACCOUNT),'Account not properly created'); - for ( $i=600002; $i < 600999;$i++) { + // $start=$fiche->id; + $this->assertEquals('6000001',$fiche->strAttribut(ATTR_DEF_ACCOUNT),'Account not properly created'); + for ( $i=6000002; $i < 6000999;$i++) { $fiche->setAttribut(ATTR_DEF_ACCOUNT, ""); Card_Property::update($fiche); $fiche->load(); + + $this->assertEquals($i,$fiche->strAttribut(ATTR_DEF_ACCOUNT),'Account not properly created'); + + } + + } + /** + * @testdox testAutomaticAccountingUpdate test accounting automatic compute(update) + */ + public function testAutomaticAccounting620Update() + { + $this->g_connection->exec_sql("update public.parameter set pr_value = $1 where pr_id=$2", + array('N','MY_ALPHANUM')); + $fiche_def=new Fiche_Def($this->g_connection,2); + $fiche_def->set_autocreate(true); + $fiche_def->save_class_base('620'); + $fiche=$this->build_fiche(2,'TESTACCOUNT'); + // $start=$fiche->id; + $this->assertEquals('6200001',$fiche->strAttribut(ATTR_DEF_ACCOUNT),'Account not properly created'); + for ( $i=6200002; $i < 6200999;$i++) { + $fiche->setAttribut(ATTR_DEF_ACCOUNT, ""); + Card_Property::update($fiche); + $fiche->load(); + $this->assertEquals($i,$fiche->strAttribut(ATTR_DEF_ACCOUNT),'Account not properly created'); } @@ -579,7 +603,7 @@ where $fiche_def->set_autocreate(true); $fiche_def->save_class_base('600'); $first=true; - for ( $i=600001; $i < 601000;$i++) { + for ( $i=6000001; $i < 6000999;$i++) { $fiche=new Fiche($this->g_connection); $fiche->insert(2,['av_text1'=>'PHPUNIT '.__FUNCTION__]); if ( $first ) { @@ -617,7 +641,7 @@ where /** * @testdox testAutomaticAccountingUpdateAlpha test accounting automatic compute(update) with alphanumeric enable */ - public function testAutomaticAccountingUpdateAlpha() + public function testAlphaAutomaticAccountingUpdate() { $this->g_connection->exec_sql("update public.parameter set pr_value = $1 where pr_id=$2", @@ -647,7 +671,7 @@ where /** * @testdox testAutomaticAccountingInsertAlpha test accounting automatic compute (insert) with alphanumeric enable */ - public function testAutomaticAccountingInsertAlpha() + public function testAlphaAutomaticAccountingInsert() { $this->g_connection->exec_sql("update public.parameter set pr_value = $1 where pr_id=$2", @@ -772,4 +796,38 @@ where $fiche->delete(); } + + /** + * @brief test if it is possible to generate an accounting if there is a mix of alpha numeric and numeric + * @testdox test if it is possible to generate an accounting if there is a mix of alpha numeric and numeric + * accounting + * @return void + */ + public function testMixedAlphaAccounting() + { + + + // set category : enable automatic compute + base = 600 + $fiche_def=new Fiche_Def($this->g_connection,2); + $fiche_def->set_autocreate(true); + $fiche_def->save_class_base('600'); + + // insert a accounting 600TESTALPHA + $this->g_connection->exec_sql("insert into tmp_pcmn (pcm_val,pcm_lib,pcm_val_parent,pcm_type,pcm_direct_use) + values('600TESTALPHA','Test Alpha','600','CHA','Y')"); + + + $fiche=$this->build_fiche(2,'TESTACCOUNT'); + + // Try to generate an account + $fiche->setAttribut(ATTR_DEF_ACCOUNT, null); + $fiche->load(); + + Card_Property::update($fiche); + $fiche_updated=new Fiche($this->g_connection,$fiche->id); + $fiche_updated->load(); + $this->assertEquals('6000001',$fiche_updated->strAttribut(ATTR_DEF_ACCOUNT),'account not computed properly'); + $this->g_connection->exec_sql('delete from tmp_pcmn where pcm_val=$1',['600TESTALPHA']); + + } }