31 require_once DOL_DOCUMENT_ROOT.
'/core/class/notify.class.php';
32 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
33 require_once DOL_DOCUMENT_ROOT.
'/fichinter/class/fichinter.class.php';
41 public $element =
'fichinterrec';
42 public $table_element =
'fichinter_rec';
43 public $table_element_line =
'fichinterdet_rec';
48 public $fk_element =
'fk_fichinter';
58 public $picto =
'intervention';
76 public $date_last_gen;
95 public $usenewprice = 0;
107 $this->statuts[0] =
'Draft';
108 $this->statuts[1] =
'Closed';
110 $this->statuts_short[0] =
'Draft';
111 $this->statuts_short[1] =
'Closed';
113 $this->statuts_logo[0] =
'statut0';
114 $this->statuts_logo[1] =
'statut1';
125 return $this->
LibStatut($this->statut, $mode);
136 public function create($user, $notrigger = 0)
144 $this->title = trim($this->title);
153 $result = $fichintsrc->fetch($this->id_origin);
154 $result = $fichintsrc->fetch_lines(1);
159 $this->brouillon = 1;
161 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"fichinter_rec (";
167 $sql .=
", description";
168 $sql .=
", note_private";
169 $sql .=
", note_public";
170 $sql .=
", fk_user_author";
171 $sql .=
", fk_projet";
172 $sql .=
", fk_contrat";
173 $sql .=
", modelpdf";
175 $sql .=
", frequency";
176 $sql .=
", unit_frequency";
177 $sql .=
", date_when";
178 $sql .=
", date_last_gen";
179 $sql .=
", nb_gen_done";
180 $sql .=
", nb_gen_max";
183 $sql .=
") VALUES (";
184 $sql .=
"'".$this->db->escape($this->title).
"'";
185 $sql .=
", ".($this->socid > 0 ? ((int) $this->socid) :
'null');
186 $sql .=
", ".((int) $conf->entity);
187 $sql .=
", '".$this->db->idate($now).
"'";
188 $sql .=
", ".(!empty($fichintsrc->duration) ? ((int) $fichintsrc->duration) :
'0');
190 $sql .=
", ".(!empty($fichintsrc->note_private) ? (
"'".$this->db->escape($fichintsrc->note_private).
"'") :
"null");
191 $sql .=
", ".(!empty($fichintsrc->note_public) ? (
"'".$this->db->escape($fichintsrc->note_public).
"'") :
"null");
192 $sql .=
", ".((int) $user->id);
194 if ($this->socid == $fichintsrc->socid) {
195 $sql .=
", ".(!empty($fichintsrc->fk_project) ? ((int) $fichintsrc->fk_project) :
"null");
196 $sql .=
", ".(!empty($fichintsrc->fk_contrat) ? ((int) $fichintsrc->fk_contrat) :
"null");
198 $sql .=
", null, null";
201 $sql .=
", ".(!empty($fichintsrc->model_pdf) ?
"'".$this->db->escape($fichintsrc->model_pdf).
"'" :
"''");
204 $sql .=
", ".(!empty($this->frequency) ? ((int) $this->frequency) :
"null");
205 $sql .=
", '".$this->db->escape($this->unit_frequency).
"'";
206 $sql .=
", ".(!empty($this->date_when) ?
"'".$this->db->idate($this->date_when).
"'" :
'null');
207 $sql .=
", ".(!empty($this->date_last_gen) ?
"'".$this->db->idate($this->date_last_gen).
"'" :
'null');
209 $sql .=
", ".((int) $this->nb_gen_max);
213 if ($this->
db->query($sql)) {
214 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
219 $num = count($fichintsrc->lines);
220 for ($i = 0; $i < $num; $i++) {
222 $result_insert = $this->
addline(
223 $fichintsrc->lines[$i]->desc,
224 $fichintsrc->lines[$i]->duration,
225 $fichintsrc->lines[$i]->datei,
226 $fichintsrc->lines[$i]->rang,
227 $fichintsrc->lines[$i]->subprice,
228 $fichintsrc->lines[$i]->qty,
229 $fichintsrc->lines[$i]->tva_tx,
230 $fichintsrc->lines[$i]->fk_product,
231 $fichintsrc->lines[$i]->remise_percent,
236 $fichintsrc->lines[$i]->product_type,
237 $fichintsrc->lines[$i]->special_code,
238 $fichintsrc->lines[$i]->label,
239 $fichintsrc->lines[$i]->fk_unit
242 if ($result_insert < 0) {
248 $this->
db->rollback();
254 $this->error = $this->
db->error().
' sql='.$sql;
255 $this->
db->rollback();
259 $this->
db->rollback();
273 public function fetch($rowid = 0, $ref =
'', $ref_ext =
'')
275 $sql =
'SELECT f.titre as title, f.fk_soc';
276 $sql .=
', f.datec, f.duree, f.fk_projet, f.fk_contrat, f.description';
277 $sql .=
', f.note_private, f.note_public, f.fk_user_author';
278 $sql .=
', f.frequency, f.unit_frequency, f.date_when, f.date_last_gen, f.nb_gen_done, f.nb_gen_max, f.auto_validate';
279 $sql .=
', f.note_private, f.note_public, f.fk_user_author';
280 $sql .=
' FROM '.MAIN_DB_PREFIX.
'fichinter_rec as f';
282 $sql .=
" WHERE f.rowid = ".((int) $rowid);
284 $sql .=
" WHERE f.titre = '".$this->db->escape($ref).
"'";
287 dol_syslog(get_class($this).
"::fetch rowid=".$rowid, LOG_DEBUG);
289 $result = $this->
db->query($sql);
291 if ($this->
db->num_rows($result)) {
292 $obj = $this->
db->fetch_object($result);
295 $this->titre = $obj->title;
296 $this->title = $obj->title;
297 $this->
ref = $obj->title;
299 $this->datec = $obj->datec;
300 $this->duration = $obj->duree;
301 $this->socid = $obj->fk_soc;
303 $this->fk_project = $obj->fk_projet;
304 $this->fk_contrat = $obj->fk_contrat;
305 $this->note_private = $obj->note_private;
306 $this->note_public = $obj->note_public;
307 $this->user_author = $obj->fk_user_author;
308 $this->model_pdf = $obj->model_pdf;
309 $this->modelpdf = $obj->model_pdf;
310 $this->rang = $obj->rang;
311 $this->special_code = $obj->special_code;
312 $this->frequency = $obj->frequency;
313 $this->unit_frequency = $obj->unit_frequency;
314 $this->date_when = $this->
db->jdate($obj->date_when);
315 $this->date_last_gen = $this->
db->jdate($obj->date_last_gen);
316 $this->nb_gen_done = $obj->nb_gen_done;
317 $this->nb_gen_max = $obj->nb_gen_max;
318 $this->auto_validate = $obj->auto_validate;
320 $this->brouillon = 1;
325 $this->error = $this->
db->error();
330 $this->error =
'Interventional with id '.$rowid.
' not found sql='.$sql;
331 dol_syslog(get_class($this).
'::Fetch Error '.$this->error, LOG_ERR);
335 $this->error = $this->
db->error();
351 $this->lines = array();
353 $sql =
'SELECT l.rowid, l.fk_product, l.product_type as product_type, l.label as custom_label, l.description, ';
354 $sql .=
' l.price, l.qty, l.tva_tx, l.remise_percent, l.subprice, l.duree, ';
355 $sql .=
' l.total_ht, l.total_tva, l.total_ttc,';
356 $sql .=
' l.rang, l.special_code,';
357 $sql .=
' l.fk_unit, p.ref as product_ref, p.fk_product_type as fk_product_type,';
358 $sql .=
' p.label as product_label, p.description as product_desc';
359 $sql .=
' FROM '.MAIN_DB_PREFIX.
'fichinterdet_rec as l';
360 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON l.fk_product = p.rowid';
361 $sql .=
' WHERE l.fk_fichinter = '.((int) $this->
id);
363 dol_syslog(
'FichinterRec::fetch_lines', LOG_DEBUG);
365 $result = $this->
db->query($sql);
367 $num = $this->
db->num_rows($result);
370 $objp = $this->
db->fetch_object($result);
373 $line->id = $objp->rowid;
374 $line->label = $objp->custom_label;
375 $line->desc = $objp->description;
376 $line->product_type = $objp->product_type;
377 $line->product_ref = $objp->product_ref;
378 $line->product_label = $objp->product_label;
379 $line->product_desc = $objp->product_desc;
380 $line->fk_product_type = $objp->fk_product_type;
381 $line->qty = $objp->qty;
382 $line->duree = $objp->duree;
383 $line->duration = $objp->duree;
384 $line->datei = $objp->date;
385 $line->subprice = $objp->subprice;
386 $line->tva_tx = $objp->tva_tx;
387 $line->remise_percent = $objp->remise_percent;
388 $line->fk_remise_except = $objp->fk_remise_except;
389 $line->fk_product = $objp->fk_product;
390 $line->date_start = $objp->date_start;
391 $line->date_end = $objp->date_end;
392 $line->info_bits = $objp->info_bits;
393 $line->total_ht = $objp->total_ht;
394 $line->total_tva = $objp->total_tva;
395 $line->total_ttc = $objp->total_ttc;
396 $line->code_ventilation = $objp->fk_code_ventilation;
397 $line->rang = $objp->rang;
398 $line->special_code = $objp->special_code;
399 $line->fk_unit = $objp->fk_unit;
401 $this->lines[$i] = $line;
406 $this->
db->free($result);
409 $this->error = $this->
db->error();
423 public function delete($rowid = 0, $notrigger = 0, $idwarehouse = -1)
429 dol_syslog(get_class($this).
"::delete rowid=".$rowid, LOG_DEBUG);
434 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"fichinterdet_rec WHERE fk_fichinter = ".((int) $rowid);
436 if ($this->
db->query($sql)) {
437 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"fichinter_rec WHERE rowid = ".((int) $rowid);
439 if (!$this->
db->query($sql)) {
440 $this->error = $this->
db->lasterror();
444 $this->error = $this->
db->lasterror();
452 $this->
db->rollback();
480 public function addline($desc, $duration, $datei,
$rang = -1, $pu_ht = 0, $qty = 0, $txtva = 0, $fk_product = 0, $remise_percent = 0, $price_base_type =
'HT', $info_bits = 0, $fk_remise_except =
'', $pu_ttc = 0, $type = 0, $special_code = 0, $label =
'', $fk_unit =
null)
484 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
491 if ($this->brouillon) {
493 $remise_percent =
price2num($remise_percent);
503 if (!preg_match(
'/\((.*)\)/', $txtva)) {
507 if ($price_base_type ==
'HT') {
517 $tabprice =
calcul_price_total($qty, $pu, $remise_percent, $txtva, 0, 0, 0, $price_base_type, $info_bits, $type, $mysoc);
519 $total_ht = $tabprice[0];
520 $total_tva = $tabprice[1];
521 $total_ttc = $tabprice[2];
523 $product_type = $type;
526 $result = $product->fetch($fk_product);
527 $product_type = $product->type;
530 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"fichinterdet_rec (";
531 $sql .=
"fk_fichinter";
533 $sql .=
", description";
539 $sql .=
", fk_product";
540 $sql .=
", product_type";
541 $sql .=
", remise_percent";
544 $sql .=
", total_ht";
545 $sql .=
", total_tva";
546 $sql .=
", total_ttc";
550 $sql .=
") VALUES (";
551 $sql .= (int) $this->
id;
552 $sql .=
", ".(!empty($label) ?
"'".$this->db->escape($label).
"'" :
"null");
553 $sql .=
", ".(!empty($desc) ?
"'".$this->db->escape($desc).
"'" :
"null");
554 $sql .=
", ".(!empty($datei) ?
"'".$this->db->idate($datei).
"'" :
"null");
555 $sql .=
", ".$duration;
559 $sql .=
", ".(!empty($fk_product) ? $fk_product :
"null");
560 $sql .=
", ".$product_type;
561 $sql .=
", ".(!empty($remise_percent) ? $remise_percent :
"null");
564 $sql .=
", '".price2num($total_ht).
"'";
565 $sql .=
", '".price2num($total_tva).
"'";
566 $sql .=
", '".price2num($total_ttc).
"'";
567 $sql .=
", ".(int)
$rang;
569 $sql .=
", ".(!empty($fk_unit) ? $fk_unit :
"null");
572 dol_syslog(get_class($this).
"::addline", LOG_DEBUG);
573 if ($this->
db->query($sql)) {
576 $this->error = $this->
db->lasterror();
595 if ($user->rights->fichinter->creer) {
596 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"fichinter_rec ";
597 $sql .=
" SET frequency='".$this->db->escape($freq).
"'";
598 $sql .=
", date_last_gen='".$this->db->escape($courant).
"'";
599 $sql .=
" WHERE rowid = ".((int) $this->
id);
604 $this->frequency = $freq;
605 $this->date_last_gen = $courant;
626 public function getNomUrl($withpicto = 0, $option =
'', $max = 0, $short = 0, $moretitle =
'')
628 global $langs, $hookmanager;
631 $label = $langs->trans(
"ShowInterventionModel").
': '.$this->ref;
633 $url = DOL_URL_ROOT.
'/fichinter/card-rec.php?id='.$this->id;
639 $picto =
'intervention';
641 $link =
'<a href="'.$url.
'" title="'.
dol_escape_htmltag($label, 1).
'" class="classfortooltip">';
645 $result .= $link.img_object($label, $picto,
'class="classfortooltip"').$linkend;
647 if ($withpicto && $withpicto != 2) {
650 if ($withpicto != 2) {
651 $result .= $link.$this->ref.$linkend;
654 $hookmanager->initHooks(array($this->element .
'dao'));
655 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
656 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
658 $result = $hookmanager->resPrint;
660 $result .= $hookmanager->resPrint;
676 global $user, $langs, $conf;
680 $nownotime =
dol_mktime(0, 0, 0, $arraynow[
'mon'], $arraynow[
'mday'], $arraynow[
'year']);
682 parent::initAsSpecimen($option);
684 $this->usenewprice = 1;
697 $tables = array(
'fichinter_rec');
728 if (!$this->table_element) {
729 dol_syslog(get_class($this).
"::setFrequencyAndUnit called with table_element not defined", LOG_ERR);
733 if (!empty($frequency) && empty($unit)) {
734 dol_syslog(get_class($this).
"::setFrequencyAndUnit called with frequency defined but unit not ", LOG_ERR);
738 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
739 $sql .=
' SET frequency = '.($frequency ? $this->
db->escape($frequency) :
'null');
741 $sql .=
', unit_frequency = "'.$this->db->escape($unit).
'"';
743 $sql .=
" WHERE rowid = ".((int) $this->
id);
745 dol_syslog(get_class($this).
"::setFrequencyAndUnit", LOG_DEBUG);
746 if ($this->
db->query($sql)) {
747 $this->frequency = $frequency;
749 $this->unit_frequency = $unit;
767 if (!$this->table_element) {
768 dol_syslog(get_class($this).
"::setNextDate was called on objet with property table_element not defined", LOG_ERR);
771 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
772 $sql .=
" SET date_when = ".($date ?
"'".$this->db->idate($date).
"'" :
"null");
773 if ($increment_nb_gen_done > 0) {
774 $sql .=
', nb_gen_done = nb_gen_done + 1';
776 $sql .=
" WHERE rowid = ".((int) $this->
id);
778 dol_syslog(get_class($this).
"::setNextDate", LOG_DEBUG);
779 if ($this->
db->query($sql)) {
780 $this->date_when = $date;
781 if ($increment_nb_gen_done > 0) {
782 $this->nb_gen_done++;
799 if (!$this->table_element) {
800 dol_syslog(get_class($this).
"::setMaxPeriod was called on objet with property table_element not defined", LOG_ERR);
808 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
809 $sql .=
' SET nb_gen_max = '.((int) $nb);
810 $sql .=
' WHERE rowid = '.((int) $this->
id);
812 dol_syslog(get_class($this).
"::setMaxPeriod", LOG_DEBUG);
813 if ($this->
db->query($sql)) {
814 $this->nb_gen_max = $nb;
830 if (!$this->table_element) {
831 dol_syslog(get_class($this).
"::setAutoValidate called with property table_element not defined", LOG_ERR);
835 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
836 $sql .=
' SET auto_validate = '.((int) $validate);
837 $sql .=
' WHERE rowid = '.((int) $this->
id);
839 dol_syslog(get_class($this).
"::setAutoValidate", LOG_DEBUG);
840 if ($this->
db->query($sql)) {
841 $this->auto_validate = $validate;
856 if (!$this->table_element) {
857 dol_syslog(get_class($this).
"::updateNbGenDone called with property table_element not defined", LOG_ERR);
861 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
862 $sql .=
' SET nb_gen_done = nb_gen_done + 1';
863 $sql .=
' , date_last_gen = now()';
865 if ($this->nb_gen_max == $this->nb_gen_done + 1) {
866 $sql .=
' , statut = 1';
869 $sql .=
" WHERE rowid = ".((int) $this->
id);
871 dol_syslog(get_class($this).
"::setAutoValidate", LOG_DEBUG);
872 if ($this->
db->query($sql)) {
873 $this->nb_gen_done = $this->nb_gen_done + 1;
874 $this->nb_gen_done =
dol_now();