From 8ca0f9d568d76b70b0e81588e3ed2c0aaeb620dd Mon Sep 17 00:00:00 2001 From: sparkyx Date: Sun, 19 Oct 2025 12:15:49 +0200 Subject: [PATCH] Code rewrite : save Acc_Document without passing by Document --- include/class/acc_document.class.php | 32 ++++++++++++++++++++++++---- include/class/document.class.php | 18 +--------------- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/include/class/acc_document.class.php b/include/class/acc_document.class.php index 64d059399..f9ff6ab0d 100644 --- a/include/class/acc_document.class.php +++ b/include/class/acc_document.class.php @@ -51,7 +51,32 @@ class Acc_Document extends Document { $this->document_xml = $document_xml; return $this; } + /*! + * \brief insert the generated Document into the database, update the $this->d_id + * that is the PK of document. and load the PDF into the database. + * \param $p_file is the generated file (full path) + * \return 0 if no error otherwise 1 + */ + protected function saveGenerated($p_file) + { + $this->db->start(); + $this->d_filename= basename($p_file); + $this->d_mimetype= mime_content_type($p_file); + $this->d_lob=$this->db->lo_import($p_file); + if ($this->d_lob==false) + { + echo "ne peut pas importer [$p_file]"; + return 1; + } + $sql="update jrn set jr_pj=$1,jr_pj_name=$2,jr_pj_type=$3 where jr_id=$4 returning jr_id"; + $id=$this->db->get_value($sql, array($this->d_lob, $this->d_filename, $this->d_mimetype, $this->d_id)); + $this->db->commit(); + if ( $id == "") { + throw new Exception("AD99 FILE NOT SAVED INTO DB",99); + } + + } /** * @brief constructor * @param $cn \Database @@ -208,17 +233,16 @@ class Acc_Document extends Document { // var md_id (int) DOCUMENT_MODELE.MD_ID $this->md_id = $p_array['gen_doc']; // var ag_id == 0 fake follow-up - $this->ag_id = 0; + $this->ag_id = 0; // var e_pj (string) receipt nb $p_array['e_pj'] = $this->db->get_value("select jr_pj_number from jrn where jr_id=$1" , [$this->d_id]); $filename = ""; + // generate the document and set d_lob,d_mimetype, + // this function will call saveGenerated and save in DB $this->generate($p_array, $p_array['e_pj']); - // Move the document to accountancy (table JRN), - $this->moveDocumentACC($internal); - // Update the comment with invoice number, if the comment is empty if (!isset($p_array['e_comm']) || noalyss_strlentrim($p_array['e_comm']) == 0) { $sql = "update jrn set jr_comment=' document " . $this->d_number . "' where jr_internal=$1"; diff --git a/include/class/document.class.php b/include/class/document.class.php index 9c96b9479..6112d61eb 100644 --- a/include/class/document.class.php +++ b/include/class/document.class.php @@ -389,8 +389,7 @@ class Document * \param $p_file is the generated file * \return 0 if no error otherwise 1 */ - - function saveGenerated($p_file) + protected function saveGenerated($p_file) { // We save the generated file $doc=new Document($this->db); @@ -1819,21 +1818,6 @@ class Document $this->db->lo_unlink($d_lob); } - /*! - * \brief Move a document from the table document into the concerned operation - * the document is not copied : it is only a link - * \param $p_internal internal code - */ - function moveDocumentACC($p_internal) - { - $sql="update jrn set jr_pj=$1,jr_pj_name=$2,jr_pj_type=$3 where jr_internal=$4"; - - $this->db->exec_sql($sql, array($this->d_lob, $this->d_filename, $this->d_mimetype, $p_internal)); - // clean the table document - $sql='delete from document where d_id=$1'; - $this->db->exec_sql($sql,[$this->d_id]); - } - /** * @brief replace a special tag *TAGxxxx with the value from fiche_detail, the xxxx * is the ad_value