dolibarr  13.0.2
propalmergepdfproduct.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2004-2015 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2015 Florian HENRY <florian.henry@open-concept.pro>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
25 require_once DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php";
26 
27 
28 
33 {
37  public $element = 'propal_merge_pdf_product';
38 
42  public $table_element = 'propal_merge_pdf_product';
43 
44  public $fk_product;
45  public $file_name;
46  public $fk_user_author;
47  public $fk_user_mod;
48  public $datec = '';
49  public $tms = '';
50  public $lang;
51 
52  public $lines = array();
53 
54 
55 
56 
62  public function __construct($db)
63  {
64  $this->db = $db;
65  }
66 
67 
75  public function create($user, $notrigger = 0)
76  {
77  global $conf, $langs;
78  $error = 0;
79 
80  // Clean parameters
81 
82  if (isset($this->fk_product)) $this->fk_product = trim($this->fk_product);
83  if (isset($this->file_name)) $this->file_name = trim($this->file_name);
84  if (isset($this->fk_user_author)) $this->fk_user_author = trim($this->fk_user_author);
85  if (isset($this->fk_user_mod)) $this->fk_user_mod = trim($this->fk_user_mod);
86  if (isset($this->lang)) $this->lang = trim($this->lang);
87  if (isset($this->import_key)) $this->import_key = trim($this->import_key);
88 
89 
90 
91  // Check parameters
92  // Put here code to add control on parameters values
93 
94  // Insert request
95  $sql = "INSERT INTO ".MAIN_DB_PREFIX."propal_merge_pdf_product(";
96 
97  $sql .= "fk_product,";
98  $sql .= "file_name,";
99  if ($conf->global->MAIN_MULTILANGS) {
100  $sql .= "lang,";
101  }
102  $sql .= "fk_user_author,";
103  $sql .= "fk_user_mod,";
104  $sql .= "datec";
105 
106 
107  $sql .= ") VALUES (";
108 
109  $sql .= " ".(!isset($this->fk_product) ? 'NULL' : "'".$this->db->escape($this->fk_product)."'").",";
110  $sql .= " ".(!isset($this->file_name) ? 'NULL' : "'".$this->db->escape($this->file_name)."'").",";
111  if ($conf->global->MAIN_MULTILANGS) {
112  $sql .= " ".(!isset($this->lang) ? 'NULL' : "'".$this->db->escape($this->lang)."'").",";
113  }
114  $sql .= " ".$user->id.",";
115  $sql .= " ".$user->id.",";
116  $sql .= " '".$this->db->idate(dol_now())."'";
117 
118 
119  $sql .= ")";
120 
121  $this->db->begin();
122 
123  dol_syslog(__METHOD__, LOG_DEBUG);
124  $resql = $this->db->query($sql);
125  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
126 
127  if (!$error)
128  {
129  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."propal_merge_pdf_product");
130  }
131 
132  // Commit or rollback
133  if ($error)
134  {
135  foreach ($this->errors as $errmsg)
136  {
137  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
138  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
139  }
140  $this->db->rollback();
141  return -1 * $error;
142  } else {
143  $this->db->commit();
144  return $this->id;
145  }
146  }
147 
148 
155  public function fetch($id)
156  {
157  global $langs, $conf;
158 
159  $sql = "SELECT";
160  $sql .= " t.rowid,";
161 
162  $sql .= " t.fk_product,";
163  $sql .= " t.file_name,";
164  $sql .= " t.lang,";
165  $sql .= " t.fk_user_author,";
166  $sql .= " t.fk_user_mod,";
167  $sql .= " t.datec,";
168  $sql .= " t.tms,";
169  $sql .= " t.import_key";
170 
171 
172  $sql .= " FROM ".MAIN_DB_PREFIX."propal_merge_pdf_product as t";
173  $sql .= " WHERE t.rowid = ".$id;
174 
175  dol_syslog(__METHOD__, LOG_DEBUG);
176  $resql = $this->db->query($sql);
177  if ($resql)
178  {
179  if ($this->db->num_rows($resql))
180  {
181  $obj = $this->db->fetch_object($resql);
182 
183  $this->id = $obj->rowid;
184 
185  $this->fk_product = $obj->fk_product;
186  $this->file_name = $obj->file_name;
187  if ($conf->global->MAIN_MULTILANGS) {
188  $this->lang = $obj->lang;
189  }
190  $this->fk_user_author = $obj->fk_user_author;
191  $this->fk_user_mod = $obj->fk_user_mod;
192  $this->datec = $this->db->jdate($obj->datec);
193  $this->tms = $this->db->jdate($obj->tms);
194  $this->import_key = $obj->import_key;
195  }
196  $this->db->free($resql);
197 
198  return 1;
199  } else {
200  $this->error = "Error ".$this->db->lasterror();
201  dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
202  return -1;
203  }
204  }
205 
206  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
214  public function fetch_by_product($product_id, $lang = '')
215  {
216  // phpcs:enable
217  global $langs, $conf;
218 
219  $sql = "SELECT";
220  $sql .= " t.rowid,";
221 
222  $sql .= " t.fk_product,";
223  $sql .= " t.file_name,";
224  $sql .= " t.lang,";
225  $sql .= " t.fk_user_author,";
226  $sql .= " t.fk_user_mod,";
227  $sql .= " t.datec,";
228  $sql .= " t.tms,";
229  $sql .= " t.import_key";
230 
231 
232  $sql .= " FROM ".MAIN_DB_PREFIX."propal_merge_pdf_product as t";
233  $sql .= " WHERE t.fk_product = ".$product_id;
234  if ($conf->global->MAIN_MULTILANGS && !empty($lang)) {
235  $sql .= " AND t.lang = '".$this->db->escape($lang)."'";
236  }
237 
238  dol_syslog(__METHOD__, LOG_DEBUG);
239  $resql = $this->db->query($sql);
240  if ($resql)
241  {
242  if ($this->db->num_rows($resql))
243  {
244  while ($obj = $this->db->fetch_object($resql)) {
245  $line = new PropalmergepdfproductLine();
246 
247  $line->id = $obj->rowid;
248 
249  $line->fk_product = $obj->fk_product;
250  $line->file_name = $obj->file_name;
251  if ($conf->global->MAIN_MULTILANGS) {
252  $line->lang = $obj->lang;
253  }
254  $line->fk_user_author = $obj->fk_user_author;
255  $line->fk_user_mod = $obj->fk_user_mod;
256  $line->datec = $this->db->jdate($obj->datec);
257  $line->tms = $this->db->jdate($obj->tms);
258  $line->import_key = $obj->import_key;
259 
260 
261  if ($conf->global->MAIN_MULTILANGS) {
262  $this->lines[$obj->file_name.'_'.$obj->lang] = $line;
263  } else {
264  $this->lines[$obj->file_name] = $line;
265  }
266  }
267  }
268  $this->db->free($resql);
269 
270  return 1;
271  } else {
272  $this->error = "Error ".$this->db->lasterror();
273  dol_syslog(get_class($this)."::fetch_by_product ".$this->error, LOG_ERR);
274  return -1;
275  }
276  }
277 
278 
286  public function update($user = 0, $notrigger = 0)
287  {
288  global $conf, $langs;
289  $error = 0;
290 
291  // Clean parameters
292 
293  if (isset($this->fk_product)) $this->fk_product = trim($this->fk_product);
294  if (isset($this->file_name)) $this->file_name = trim($this->file_name);
295  if (isset($this->fk_user_mod)) $this->fk_user_mod = trim($this->fk_user_mod);
296  if (isset($this->lang)) $this->lang = trim($this->lang);
297 
298  // Check parameters
299  // Put here code to add a control on parameters values
300 
301  // Update request
302  $sql = "UPDATE ".MAIN_DB_PREFIX."propal_merge_pdf_product SET";
303 
304  $sql .= " fk_product=".(isset($this->fk_product) ? $this->fk_product : "null").",";
305  $sql .= " file_name=".(isset($this->file_name) ? "'".$this->db->escape($this->file_name)."'" : "null").",";
306  if ($conf->global->MAIN_MULTILANGS) {
307  $sql .= " lang=".(isset($this->lang) ? "'".$this->db->escape($this->lang)."'" : "null").",";
308  }
309  $sql .= " fk_user_mod=".$user->id;
310 
311 
312  $sql .= " WHERE rowid=".$this->id;
313 
314  $this->db->begin();
315 
316  dol_syslog(__METHOD__, LOG_DEBUG);
317  $resql = $this->db->query($sql);
318  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
319 
320  // Commit or rollback
321  if ($error)
322  {
323  foreach ($this->errors as $errmsg)
324  {
325  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
326  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
327  }
328  $this->db->rollback();
329  return -1 * $error;
330  } else {
331  $this->db->commit();
332  return 1;
333  }
334  }
335 
336 
344  public function delete($user, $notrigger = 0)
345  {
346  global $conf, $langs;
347  $error = 0;
348 
349  $this->db->begin();
350 
351  if (!$error)
352  {
353  $sql = "DELETE FROM ".MAIN_DB_PREFIX."propal_merge_pdf_product";
354  $sql .= " WHERE rowid=".$this->id;
355 
356  dol_syslog(__METHOD__, LOG_DEBUG);
357  $resql = $this->db->query($sql);
358  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
359  }
360 
361  // Commit or rollback
362  if ($error)
363  {
364  foreach ($this->errors as $errmsg)
365  {
366  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
367  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
368  }
369  $this->db->rollback();
370  return -1 * $error;
371  } else {
372  $this->db->commit();
373  return 1;
374  }
375  }
376 
377  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
387  public function delete_by_product($user, $product_id, $lang_id = '', $notrigger = 0)
388  {
389  // phpcs:enable
390  global $conf, $langs;
391  $error = 0;
392 
393  $this->db->begin();
394 
395  if (!$error)
396  {
397  $sql = "DELETE FROM ".MAIN_DB_PREFIX."propal_merge_pdf_product";
398  $sql .= " WHERE fk_product=".$product_id;
399 
400  if ($conf->global->MAIN_MULTILANGS && !empty($lang_id)) {
401  $sql .= " AND lang='".$this->db->escape($lang_id)."'";
402  }
403 
404  dol_syslog(__METHOD__, LOG_DEBUG);
405  $resql = $this->db->query($sql);
406  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
407  }
408 
409  // Commit or rollback
410  if ($error)
411  {
412  foreach ($this->errors as $errmsg)
413  {
414  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
415  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
416  }
417  $this->db->rollback();
418  return -1 * $error;
419  } else {
420  $this->db->commit();
421  return 1;
422  }
423  }
424 
425  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
432  public function delete_by_file($user)
433  {
434  // phpcs:enable
435  global $conf, $langs;
436  $error = 0;
437 
438  $this->db->begin();
439 
440  if (!$error)
441  {
442  $sql = "DELETE FROM ".MAIN_DB_PREFIX."propal_merge_pdf_product";
443  $sql .= " WHERE fk_product=".$this->fk_product." AND file_name='".$this->db->escape($this->file_name)."'";
444 
445  dol_syslog(__METHOD__, LOG_DEBUG);
446  $resql = $this->db->query($sql);
447  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
448  }
449 
450  // Commit or rollback
451  if ($error)
452  {
453  foreach ($this->errors as $errmsg)
454  {
455  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
456  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
457  }
458  $this->db->rollback();
459  return -1 * $error;
460  } else {
461  $this->db->commit();
462  return 1;
463  }
464  }
465 
466 
467 
475  public function createFromClone(User $user, $fromid)
476  {
477  $error = 0;
478 
479  $object = new Propalmergepdfproduct($this->db);
480 
481  $this->db->begin();
482 
483  // Load source object
484  $object->fetch($fromid);
485  $object->id = 0;
486  $object->statut = 0;
487 
488  // Clear fields
489  // ...
490 
491  // Create clone
492  $object->context['createfromclone'] = 'createfromclone';
493  $result = $object->create($user);
494 
495  // Other options
496  if ($result < 0)
497  {
498  $this->error = $object->error;
499  $this->errors = array_merge($this->errors, $object->errors);
500  $error++;
501  }
502 
503  if (!$error)
504  {
505  }
506 
507  unset($object->context['createfromclone']);
508 
509  // End
510  if (!$error)
511  {
512  $this->db->commit();
513  return $object->id;
514  } else {
515  $this->db->rollback();
516  return -1;
517  }
518  }
519 
520 
527  public function initAsSpecimen()
528  {
529  $this->id = 0;
530 
531  $this->fk_product = '';
532  $this->file_name = '';
533  $this->fk_user_author = '';
534  $this->fk_user_mod = '';
535  $this->datec = '';
536  $this->tms = '';
537  $this->import_key = '';
538  }
539 }
540 
545 {
549  public $id;
550 
554  public $fk_product;
555 
556  public $file_name;
557  public $lang;
558 
562  public $fk_user_author;
563 
567  public $fk_user_mod;
568 
569  public $datec = '';
570  public $tms = '';
571  public $import_key;
572 
576  public function __construct()
577  {
578  return 1;
579  }
580 }
fetch($id)
Load object in memory from the database.
Put here description of your class.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
dol_now($mode= 'auto')
Return date for now.
Class to manage Dolibarr users.
Definition: user.class.php:44
$conf db
API class for accounts.
Definition: inc.php:54
update($user=0, $notrigger=0)
Update object into database.
Class to manage propal merge of product line.
fetch_by_product($product_id, $lang= '')
Load object in memory from the database.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
delete_by_file($user)
Delete object in database.
delete_by_product($user, $product_id, $lang_id= '', $notrigger=0)
Delete object in database.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
create($user, $notrigger=0)
Create object into database.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)