From be3b40ac9415db9f8602f48cdf5fcf9bbf69d665 Mon Sep 17 00:00:00 2001 From: sparkyx Date: Fri, 3 Oct 2025 19:28:42 +0200 Subject: [PATCH] E-INVOICE , when loading a XML , the file is splitted into a PDF and XML --- include/class/acc_document.class.php | 94 ++++++++++++++++++++++------ include/lib/database_core.class.php | 10 ++- 2 files changed, 83 insertions(+), 21 deletions(-) diff --git a/include/class/acc_document.class.php b/include/class/acc_document.class.php index 167a3f830..01dcee9aa 100644 --- a/include/class/acc_document.class.php +++ b/include/class/acc_document.class.php @@ -18,7 +18,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ // Copyright Author Dany De Bontridder danydb@aevalys.eu 28/08/25 - +use Noalyss\XMLDocument\XMLInvoice_Reader; /** * @file @@ -52,7 +52,7 @@ class Acc_Document extends Document { return $this; } - /** + /** * @brief constructor * @param $cn \Database * @param $jr_id (int) JRN.JRID will be in d_id @@ -236,10 +236,11 @@ class Acc_Document extends Document { /** * pj is the $_FILES key */ - $oid = $this->db->upload('pj'); - if ($oid == false) { + $a_file= $this->db->upload('pj',only_oid:false); + if ($a_file == false) { return false; } + $oid=$a_file['oid']; // Remove old document if any $old_oid = $this->db->get_value("select jr_pj from jrn where jr_id=$1" ,[$this->d_id]); @@ -248,8 +249,69 @@ class Acc_Document extends Document { { $this->lo_unlink( $old_oid); } - // save new document - $this->db->exec_sql("update jrn set jr_pj=$1 , jr_pj_name=$2, + + // if there is a e-invoice in XML + if ( $_FILES['pj']['type'] == 'text/xml' + || $_FILES['pj']['type'] == 'application/xml' + ) + { + // save the XML + $this->db->exec_sql("update jrn set jr_document_xml = $1 + where + jr_id=$2", + [$oid,$this->d_id]); + + $xmlreader= XMLInvoice_Reader::build_from_file($a_file['filename']); + + //@var $embedded_file (array) keys = filecontent: binary data + //,mimecode mimetype and filename (string) + try + { + $embedded_file=$xmlreader->get_embedded_document(); + if ($embedded_file == false) + { + //@todo create a PDF with standard information + + $this->commit(); + return false; + } + } catch (\Exception $e ) { + \record_log($e); + // if exception is not too many document or document corrupted + // then rethrow the exception + if ( !in_array(e->getCode(),[110,116]) ) + { + throw new \Exception("X281 ",281,$e); + } + } + + //@var $file_oid OID of the large object saved in DB + $file_oid=$this->db->lo_write($embedded_file['filecontent']); + if ( $file_oid == false ) + { + // create a PDF with standard information + $xmlreader->to_pdf($this->db); + $this->commit(); + return $oid; + } + + $this->d_name=$embedded_file['filename']; + $this->d_description=$embedded_file['filename']; + $this->d_lob=$file_oid; + $this->d_mimetype=$embedded_file['mimecode']; + // save extracted document into DB + $this->db->exec_sql("update jrn set jr_pj=$1 , jr_pj_name=$2, + jr_pj_type=$3 where jr_id=$4", + array( + $this->d_lob + , $this->d_name + , $this->d_description + , $this->d_id + ) + ); + } else{ + // save new document + $this->db->exec_sql("update jrn set jr_pj=$1 , jr_pj_name=$2, jr_pj_type=$3 where jr_id=$4", array( $oid @@ -258,19 +320,13 @@ class Acc_Document extends Document { , $this->d_id ) ); - $this->db->commit(); - // if there is a e-invoice in XML -// if ( $_FILES['pj']['type'] == 'text/xml' -// || $_FILES['pj']['type'] == 'application/xml' -// ) -// { -// $xmlreader= Noalyss\XMLDocument\XMLInvoice_Reader::build_from_file($_FILES['pj']['tmp_name']); -// // $xmlreader->save_xml -// } - $this->d_name=$_FILES['pj']['name']; - $this->d_description=$_FILES['pj']['name']; - $this->d_lob=$oid; - $this->d_mimetype=$_FILES['pj']['type']; + $this->d_name=$_FILES['pj']['name']; + $this->d_description=$_FILES['pj']['name']; + $this->d_lob=$oid; + $this->d_mimetype=$_FILES['pj']['type']; + } + $this->db->commit(); + return $oid; } /** diff --git a/include/lib/database_core.class.php b/include/lib/database_core.class.php index a04706c04..c6311d29d 100644 --- a/include/lib/database_core.class.php +++ b/include/lib/database_core.class.php @@ -723,12 +723,14 @@ class DatabaseCore * must be set in the calling function. * * \param name of the variable in $_FILES + * \param $only_oid (bool) (default :true) false : return filename and oid in an array , true only OID, * \return $oid of the lob file if success * false if a error occurs or if there is no file to upload + * array(oid, filename) if $only_oid is true * */ - function upload($p_name) + function upload($p_name,$only_oid = false) { //var $a : 0 we're in a transaction, 1 we are not in a transaction @@ -761,7 +763,11 @@ class DatabaseCore return false; } if ( $a == 1 ) { $this->commit(); } - return $oid; + if ($only_oid ){ + return $oid; + }else { + return ["oid"=>$oid,'filename'=>$new_name]; + } } else { \record_log("DC754: move_uploaded fails".var_export($_FILES, true)); $this->rollback();