noalyss Version-10
NOALYSS : serveur de comptabilité et ERP (2002)
Loading...
Searching...
No Matches
acc_document.class.php
Go to the documentation of this file.
1<?php
2
3/*
4 * This file is part of NOALYSS.
5 *
6 * NOALYSS is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * NOALYSS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with NOALYSS; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20// Copyright Author Dany De Bontridder danydb@aevalys.eu 28/08/25
22
23/**
24 * @file
25 * @brief Document used in accountancy : invoice , credit note, ...It is
26 * a specialization of Document used in Follow-UP
27 */
28
29/**
30 * @class
31 * @brief Document used in accountancy : invoice , credit note, ... It is
32 * a specialization of Document used in Follow-UP.
33 * property :
34 * - d_id JRN.JR_ID
35 * - d_name name Receipt number
36 - d_description Comment of the operation
37 - d_mimetype mimetype of the document JRN.JR_PJ_TYPE
38 - d_filename filename JRN.JR_PJ_NAME
39 - document_xml oid of the XML invoice (including PDF) JRN.JR_DOCUMENT_XML
40 - d_lob = JRN.JR_PJ
41 *
42 */
43class Acc_Document extends Document {
44 private $document_xml; ///< $document_xml (oid) XML document e-invoice
45
46 public function get_document_xml() {
48 }
49
51 $this->document_xml = $document_xml;
52 return $this;
53 }
54 /*!
55 * \brief insert the generated Document into the database, update the $this->d_id
56 * that is the PK of document. and load the PDF into the database.
57 * \param $p_file is the generated file (full path)
58 * \return 0 if no error otherwise 1
59 */
60 protected function saveGenerated($p_file)
61 {
62
63 $this->db->start();
64 $this->d_filename= basename($p_file);
65 $this->d_mimetype= mime_content_type($p_file);
66 $this->d_lob=$this->db->lo_import($p_file);
67 if ($this->d_lob==false)
68 {
69 echo "ne peut pas importer [$p_file]";
70 return 1;
71 }
72 $sql="update jrn set jr_pj=$1,jr_pj_name=$2,jr_pj_type=$3 where jr_id=$4 returning jr_id";
73 $id=$this->db->get_value($sql, array($this->d_lob, $this->d_filename, $this->d_mimetype, $this->d_id));
74 $this->db->commit();
75 if ( $id == "") {
76 throw new Exception("AD99 FILE NOT SAVED INTO DB",99);
77 }
78
79 }
80 /**
81 * @brief constructor
82 * @param $cn \Database
83 * @param $jr_id (int) JRN.JRID will be in d_id
84 */
85 function __construct($cn, $jr_id=0)
86 {
87 $this->db=$cn;
88 $this->set_id($jr_id);
89 // counter for MARCH_NEXT
90 $this->counter=0;
91
92 }
93 /**
94 * @brief set_id fill up d_filename, d_mimetype,d_lob,d_description,jr_pj_number
95 */
96 function set_id($jr_id) {
97 $this->d_id=$jr_id;
98 if ( $jr_id == 0 ){
99 return $this;
100 }
101 $row = $this->db->get_row("select jr_comment
102 ,jr_pj
103 ,jr_pj_name
104 ,jr_pj_type
105 ,jr_pj_number
106 ,jr_document_xml
107 from jrn
108 where
109 jr_id=$1", [$this->d_id]);
110 if ( empty ($row)) {
111 return $this;
112 }
113 $this->d_name=$row['jr_pj_number'];
114 $this->d_description=$row['jr_comment'];
115 $this->d_mimetype=$row['jr_pj_type'];
116 $this->d_filename=$row['jr_pj_name'];
117 $this->d_lob=$row['jr_pj'];
118 $this->document_xml=$row['jr_document_xml'];
119 return $this;
120 }
121 /**
122 * @brief save the file into DB, will create a large object if there
123 * is no document to replace. It will change the d_filename, d_mimetype
124 *
125 * @param $d_filename (string) full path to the file to load into DB
126 *
127 * @returns false if d_id = 0 or the file doesn't exist, true for success
128 */
129 function update($filename) {
130 if ($this->d_id == 0) return false;
131 if ( ! file_exists($filename)) return false;
132 $this->db->start();
133 $this->d_mimetype= mime_content_type($filename);
134 $this->d_filename= basename($filename);
135 if ( $this->d_lob == "") {
136 $this->db->lo_unlink($this->d_lob);
137 }
138
139 $this->d_lob=$this->db->lo_import($filename);
140 $this->db->exec_sql(
141 "update jrn set jr_pj=$1,jr_pj_name=$2,jr_pj_type=$3
142 where jr_id=$4",
143 [$this->d_lob,$this->d_filename,$this->d_mimetype,$this->d_id]
144 );
145 $this->db->commit();
146 return true;
147 }
148 /**
149 * @brief for FACTURX we replace the PDF save in DB by this one, always
150 * a PDF (since it is a FACTURX document)
151 */
152 function replace_receipt($new_oid)
153 {
154 if ($this->d_lob != "")
155 {
156 $this->db->lo_unlink($this->d_lob);
157 }
158 $this->d_lob=$new_oid;
159 $this->db->exec_sql("
160 update jrn
161 set
162 jr_pj = $1
163 ,jr_pj_name =$2
164 ,jr_pj_type =$3
165 where jr_id=$4
166 ",
167 [$this->d_lob
168 ,$this->d_filename
169 ,$this->d_mimetype
170 ,$this->d_id]
171 );
172
173 }
174 /**
175 * @brief save the Large Object $oid in the column JRN.JR_DOCUMENT_XML
176 * @param $oid( OID) PostgreSQL Object ID
177 */
178 function update_document_xml($oid){
179 $this->db->exec_sql("update jrn set jr_document_xml=$1 where
180 jr_id=$2",[
181 $oid,
182 $this->d_id
183 ]);
184
185 }
186 /**
187 * @brief create the invoice and saved it as attachment to the
188 * operation,
189 * @param $p_array is normally the $_POST
190 @verbatim
191 Array
192 (
193 [ledger_type] => VEN
194 [gDossier] => x
195 [nb_item] => 1 (number of item used to numerate e_marchX, e_quantX ,...)
196 [p_jrn] => 2
197 [p_jrn_predef] => 2
198 [jrn_type] => VEN or ACH
199 [e_date] => 10.06.2025 (date)
200 [e_ech] => limite date
201 [e_client] => CLIENT
202 [e_pj] => 25.827
203 [e_pj_suggest] => 25.827
204 [e_comm] => E-INVOICE
205 [p_currency_code] => 0
206 [p_currency_rate] => 1 (rate if 1 for EURO)
207 [jrn_note_input] =>
208 [e_march0] => 7DVINV
209 [e_march0_label] => Label of the operation
210 [e_march0_price] => 10.0000
211 [e_quant0] => 1.0000
212 [htva_march0] => 10
213 [e_march0_tva_id] => 1
214 [e_march0_tva_amount] => 2.1
215 [tva_march0] => 2.1
216 [tvac_march0] => 12.1
217 ...
218 [mp_date] => (dd.mm.yyyy date of payment)
219 [acompte] => 0 (amount to deduce as advance payment)
220 [e_comm_paiement] => (string : comment of the payment)
221 [e_mp] => 0 (method of payment it is the XX in e_mp_qcode_XX)
222 [e_mp_qcode_16] => Banque 1
223 [e_mp_qcode_17] => Banque 2
224 [view_invoice] => Enregistrer
225 [gen_doc] => int DOCUMENT_MODELE.MD_ID , document template to use
226 )
227 * @endverbatim
228 * @todo rewrite code : remove extract and +SQL value
229 * @returns void
230 */
231 function create_document($internal, $p_array) {
232 $this->f_id = $p_array['e_client'];
233 // var md_id (int) DOCUMENT_MODELE.MD_ID
234 $this->md_id = $p_array['gen_doc'];
235 // var ag_id == 0 fake follow-up
236 $this->ag_id = 0;
237 // var e_pj (string) receipt nb
238 $p_array['e_pj'] = $this->db->get_value("select jr_pj_number from jrn where jr_id=$1"
239 , [$this->d_id]);
240
241 // $filename (string) compute filename based on the template
242 $filename=$this->db->get_value ("select md_filename from document_modele where md_id=$1",
243 [$this->md_id]);
244 $filename = $p_array['e_pj']."-".$filename;
245 // generate the document and set d_lob,d_mimetype,
246 // this function will call saveGenerated and save in DB
247 $this->generate($p_array, $filename);
248
249 // Update the comment with invoice number, if the comment is empty
250 if (!isset($p_array['e_comm']) || noalyss_strlentrim($p_array['e_comm']) == 0) {
251 $sql = "update jrn set jr_comment=' document " . $this->d_number . "' where jr_internal=$1";
252 $this->db->exec_sql($sql, [$internal]);
253 }
254 }
255
256 /**
257 * @brief export the file to the file system and complet $this->d_mimetype, d_filename and
258 * @param $destination_file (string) full path to document
259 * @return bool false for failure and string (the full path_name) for success
260 */
261 function export_file($destination_file) {
262
263 if (empty($this->d_filename)) {
264 return false;
265 }
266
267 $this->db->start();
268 if ($this->db->lo_export($this->d_lob, $destination_file) == false) {
269 record_log("ACD122. cannot export");
270 $this->db->commit();
271 return false;
272 }
273 $this->db->commit();
274
275 return $destination_file;
276 }
277 /**
278 * \brief Save a "piece justificative" , the name must be a receipt. If it
279 * is a XML document, split it into 2 parts : PDF and XML
280 * the PDF be stored in JR_PJ and the XML into JR_DOCUMENT_XML
281 *
282 *
283 * \return $oid of the lob file if success null if a error occurs
284 *
285 */
286 function save_receipt()
287 {
288 $this->db->start();
289 /**
290 * pj is the $_FILES key
291 */
292 if ( $_FILES['pj']['name']=="") {
293 return false;
294 }
295 $a_file= $this->db->upload('pj',only_oid:false);
296 if ($a_file == false) {
297 return false;
298 }
299 $oid=$a_file['oid'];
300 // Remove old document if any
301 $old_oid = $this->db->get_value("select jr_pj from jrn where jr_id=$1"
302 ,[$this->d_id]);
303
304 if ( $old_oid != "")
305 {
306 $this->db->lo_unlink( $old_oid);
307 }
308
309 // if there is a e-invoice in XML
310 if ( $_FILES['pj']['type'] == 'text/xml'
311 || $_FILES['pj']['type'] == 'application/xml'
312 )
313 {
314 // save the XML
315 $this->db->exec_sql("update jrn set jr_document_xml = $1
316 where
317 jr_id=$2",
318 [$oid,$this->d_id]);
319
320 $xmlreader= \Noalyss\XMLDocument\XML_Reader::build_from_file($a_file['filename']);
321
322 //@var $embedded_file (array) keys = filecontent: binary data
323 //,mimecode mimetype and filename (string)
324 try
325 {
326
327 // create a PDF with standard information
328 $pdf=$xmlreader->to_pdf();
329 $file_oid=$this->db->lo_write($pdf->Output("S"));
330
331 //@var $file_oid OID of the large object saved in DB
332 $this->d_name="invoice.pdf";
333 $this->d_description="Auto generated invoice";
334 $this->d_lob=$file_oid;
335 $this->d_mimetype="application/pdf";
336
337 // save extracted document into DB
338 $this->db->exec_sql("update jrn set jr_pj=$1 , jr_pj_name=$2,
339 jr_pj_type=$3 where jr_id=$4",
340 array(
341 $this->d_lob
342 , $this->d_name
343 , $this->d_description
344 , $this->d_id
345 )
346 );
347 // save all the documents into the DB
348 // @var embedded_file (array of Noalyss\XML\Document_Reference)
349 $embedded_file=$xmlreader->get_embedded_document();
350 $nb_file = count($embedded_file);
351 for ($i=0;$i <$nb_file ; $i++)
352 {
353 // other documents save in jrn_sup_document
354 // @var $file (Binary File from XML)
355 $binary=$embedded_file[$i]->getBinary_object();
356
357 //@var $sup_oid : oid of the supplemental
358 $sup_oid=$this->db->lo_write($binary->filecontent);
359
360 $jrn_sup_document=new Jrn_Sup_Document_SQL($this->db);
361 $jrn_sup_document->jr_id=$this->d_id;
362 $jrn_sup_document->js_mimetype=$binary->mimecode;
363 $jrn_sup_document->js_filename=$binary->filename;
364 $jrn_sup_document->js_lob=$sup_oid;
365 $jrn_sup_document->js_description=$embedded_file[$i]->getDescription();
366 $jrn_sup_document->js_cbc_id=$embedded_file[$i]->getId();
367 $jrn_sup_document->save();
368 }
369
370 } catch (\Exception $e ) {
372 throw new \Exception("X281 ",281,$e);
373 }
374 } else {
375 $this->d_lob=$oid;
376 $this->d_name=$_FILES['pj']['name'];
377 $this->d_description=$_FILES['pj']['type'];
378
379 // save extracted document into DB
380 $this->db->exec_sql("update jrn set jr_pj=$1 , jr_pj_name=$2,
381 jr_pj_type=$3 where jr_id=$4",
382 array(
383 $this->d_lob
384 , $this->d_name
385 , $this->d_description
386 , $this->d_id
387 )
388 );
389 }
390 $this->db->commit();
391
392 return $oid;
393 }
394 /**
395 * @brief return a string with a link download XML or an empty string
396 * if there is no XML to download
397 */
398 function link_download_xml():string
399 {
400 $xml_oid=$this->db->get_value("select jr_document_xml from jrn where jr_id=$1",
401 [$this->d_id]);
402 if ($xml_oid == "") { return "";}
403 $url= "export.php?".http_build_query(
404 [
405 "gDossier"=>\Dossier::id(),
406 "jr_id"=>$this->d_id,
407 "act"=>'RAW:xml-invoice'
408 ]);
409 $r = sprintf('<a class="mtitle line" href="%s">',$url);
410 $r .= _("XML")
411 .'<i class="icon-download">'
412 .'</i>'
413 .'</a>';
414 return $r;
415 }
417 {
418 $gDossier=\Dossier::id();
420 $a_row=$q->collect_objects(" where jr_id=$1 order by js_cbc_id", [$jr_id]);
421
422 if ( count($a_row) > 0)
423 {
424 foreach ($a_row as $item) {
425 $export="export.php?";
426 $script="Supplement_Document.delete_document('$gDossier','$div','{$item->js_id}','$jr_id')";
427 $rowid=sprintf("row_js_%s_%s",$div,$item->js_id);
428 // @var $download (url) to send file
429 $download="export.php?". http_build_query(
430 [
431 "act"=>"RAW:suppl-document"
432 ,"js_id"=>$item->js_id
433 ,"gDossier"=>$gDossier
434 ]);
435 ?>
436 <div class="row" id="<?=$rowid?>">
437 <div class="col">
438 <a href="<?=$download?>" download> <?=$item->js_filename?></a>
439 </div>
440 <div class="col">
441 <?=$item->js_description?>
442 </div>
443 <div class="col">
444 <?=\Icon_Action::trash(uniqid("sdd"),$script)?>
445 </div>
446 </div>
447 <?php
448 }// end foreach $a_row
449 }// end if count
450 //download ALL files from this operation
451
452
453 }
454}
noalyss_strlentrim($p_string)
record_log($p_message)
Record an error message into the log file of the server or in the log folder of NOALYSS Record also t...
catch(Exception $exc) if(! $g_user->can_write_action($ag_id)) $r
$jr_id
_("actif, passif,charge,...")
Document used in accountancy : invoice , credit note, ... It is a specialization of Document used in ...
set_id($jr_id)
set_id fill up d_filename, d_mimetype,d_lob,d_description,jr_pj_number
update($filename)
save the file into DB, will create a large object if there is no document to replace.
save_receipt()
Save a "piece justificative" , the name must be a receipt.
set_document_xml($document_xml)
replace_receipt($new_oid)
for FACTURX we replace the PDF save in DB by this one, always a PDF (since it is a FACTURX document)
update_document_xml($oid)
save the Large Object $oid in the column JRN.JR_DOCUMENT_XML
saveGenerated($p_file)
insert the generated Document into the database, update the $this->d_id that is the PK of document....
$document_xml
$document_xml (oid) XML document e-invoice
export_file($destination_file)
export the file to the file system and complet $this->d_mimetype, d_filename and
create_document($internal, $p_array)
create the invoice and saved it as attachment to the operation,
static display_supplementary_doc($cn, $div, $jr_id)
link_download_xml()
return a string with a link download XML or an empty string if there is no XML to download
__construct($cn, $jr_id=0)
constructor
Class Document corresponds to the table DOCUMENT.
download($aDocument)
Download all documents in a ZIP files.
static trash($p_id, $p_javascript)
Display the icon of a trashbin.
abstract of the table public.jrn_sup_document
Get information from an XML Exception code :
$SecUser db
$script
Definition popup.php:131