215 public function create($user, $notrigger = 0)
220 if (isset($this->fk_element)) {
221 $this->fk_element = (int) $this->fk_element;
223 if (isset($this->fk_product)) {
224 $this->fk_product = (int) $this->fk_product;
226 if (isset($this->fk_elementdet)) {
227 $this->fk_elementdet = (int) $this->fk_elementdet;
229 if (isset($this->qty)) {
230 $this->qty = (float) $this->qty;
232 if (isset($this->fk_entrepot)) {
233 $this->fk_entrepot = (int) $this->fk_entrepot;
235 if (isset($this->fk_user)) {
236 $this->fk_user = (int) $this->fk_user;
238 if (isset($this->comment)) {
239 $this->comment = trim($this->comment);
241 if (isset($this->
status)) {
244 if (isset($this->batch)) {
245 $this->batch = trim($this->batch);
247 if (empty($this->datec)) {
252 if (empty($this->fk_product)) {
253 $this->error =
'Error, property ->fk_product must not be empty to create a line of reception';
256 if (empty($this->fk_reception)) {
257 $this->error =
'Error, property ->fk_reception must not be empty to create a line of reception';
262 if (empty($this->rang)) {
267 $ranktouse = $this->rang;
268 if ($ranktouse == -1) {
269 $rangmax = $this->
line_max($this->fk_reception);
270 $ranktouse = $rangmax + 1;
274 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.$this->table_element.
"(";
275 $sql .=
"fk_product,";
276 $sql .=
"fk_element,";
277 $sql .=
"fk_elementdet,";
278 $sql .=
"element_type,";
280 $sql .=
"fk_entrepot,";
289 $sql .=
"description,";
291 $sql .=
"fk_reception,";
292 $sql .=
"cost_price,";
294 $sql .=
") VALUES (";
295 $sql .=
" ".(!isset($this->fk_product) ?
'NULL' : (int) $this->fk_product).
",";
296 $sql .=
" ".(!isset($this->fk_element) ?
'NULL' : (int) $this->fk_element).
",";
297 $sql .=
" ".(!isset($this->fk_elementdet) ?
'NULL' : (int) $this->fk_elementdet).
",";
298 $sql .=
" '".(!isset($this->element_type) ?
"supplier_order" : $this->db->escape($this->element_type)).
"',";
299 $sql .=
" ".(!isset($this->qty) ?
'NULL' : (float) $this->qty).
",";
300 $sql .=
" ".(!isset($this->fk_entrepot) ?
'NULL' : (int) $this->fk_entrepot).
",";
301 $sql .=
" ".(!isset($this->fk_user) ?
'NULL' : (int) $this->fk_user).
",";
302 $sql .=
" ".(!isset($this->datec) ||
dol_strlen($this->datec) == 0 ?
'NULL' :
"'".$this->db->idate($this->datec).
"'").
",";
303 $sql .=
" ".(!isset($this->comment) ?
'NULL' :
"'".$this->db->escape($this->comment).
"'").
",";
304 $sql .=
" ".(!isset($this->
status) ?
'NULL' : (int) $this->
status).
",";
305 $sql .=
" ".(!isset($this->batch) ?
'NULL' :
"'".$this->db->escape($this->batch).
"'").
",";
306 $sql .=
" ".(!isset($this->eatby) ||
dol_strlen((
string) $this->eatby) == 0 ?
'NULL' :
"'".$this->db->idate($this->eatby).
"'").
",";
307 $sql .=
" ".(!isset($this->sellby) ||
dol_strlen((
string) $this->sellby) == 0 ?
'NULL' :
"'".$this->db->idate($this->sellby).
"'").
",";
308 $sql .=
" ".((int) $this->fk_unit);
310 $sql .=
", ".((int) $ranktouse).
",";
311 $sql .=
" ".((int) $this->fk_reception).
",";
312 $sql .=
" ".(!isset($this->cost_price) ?
'0' : (float) $this->cost_price).
",";
313 $sql .=
" ".(!isset($this->ref_fourn) ?
'NULL' :
"'".$this->db->escape($this->ref_fourn).
"'");
319 $resql = $this->db->query($sql);
322 $this->errors[] =
"Error ".$this->db->lasterror();
326 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
330 $result = $this->call_trigger(
'LINERECEPTION_CREATE', $user);
348 foreach ($this->errors as $errmsg) {
350 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
352 $this->db->rollback();
368 public function insert($user =
null, $notrigger = 0)
374 $origin_id = $this->origin_id;
375 if ($origin_id > 0) {
376 if ((empty($this->fk_reception)
377 || (empty($this->fk_elementdet))
378 || !is_numeric($this->qty))) {
379 $langs->load(
'errors');
380 $this->errors[] = $langs->trans(
'ErrorMandatoryParametersNotProvided');
384 if (empty($this->fk_reception) || !is_numeric($this->qty)) {
385 $langs->load(
'errors');
386 $this->errors[] = $langs->trans(
'ErrorMandatoryParametersNotProvided');
392 if (empty($this->rang)) {
397 $ranktouse = $this->rang;
398 if ($ranktouse == -1) {
399 $rangmax = $this->
line_max($this->fk_reception);
400 $ranktouse = $rangmax + 1;
403 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"receptiondet_batch (";
404 $sql .=
"fk_reception";
405 $sql .=
", fk_entrepot";
406 $sql .=
", fk_elementdet";
407 $sql .=
", fk_product";
408 $sql .=
", element_type";
411 $sql .=
", description";
413 $sql .=
") VALUES (";
414 $sql .= ((int) $this->fk_reception);
415 $sql .=
", ".(empty($this->fk_entrepot) ?
'NULL' : ((int) $this->fk_entrepot));
416 $sql .=
", ".(empty($this->fk_elementdet) ?
'NULL' : ((int) $this->fk_elementdet));
417 $sql .=
", ".(empty($this->fk_product) ?
'NULL' : ((int) $this->fk_product));
418 $sql .=
", '".(empty($this->element_type) ?
'order' : $this->db->escape($this->element_type)).
"'";
419 $sql .=
", ".price2num($this->qty,
'MS');
420 $sql .=
", ".((int) $this->fk_unit);
422 $sql .=
", ".((int) $ranktouse);
425 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
426 $resql = $this->db->query($sql);
428 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"receptiondet_batch");
437 if (!$error && !$notrigger) {
439 $result = $this->call_trigger(
'LINERECEPTION_INSERT', $user);
447 foreach ($this->errors as $errmsg) {
449 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
457 $this->db->rollback();
472 public function fetch($id, $ref =
'')
476 $sql .=
" t.fk_element,";
477 $sql .=
" t.fk_elementdet,";
478 $sql .=
" t.element_type,";
479 $sql .=
" t.fk_product,";
481 $sql .=
" t.fk_entrepot,";
482 $sql .=
" t.fk_user,";
484 $sql .=
" t.comment,";
485 $sql .=
" t.status,";
487 $sql .=
" t.batch, t.cost_price, t.ref_fourn,";
489 $sql .=
" t.sellby,";
490 $sql .=
" t.fk_unit,";
491 $sql .=
" t.description,";
493 $sql .=
" t.fk_reception";
494 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" as t";
496 $sql .=
" WHERE t.ref = '".$this->db->escape($ref).
"'";
498 $sql .=
" WHERE t.rowid = ".((int) $id);
502 $resql = $this->db->query($sql);
504 if ($this->db->num_rows($resql)) {
505 $obj = $this->db->fetch_object($resql);
507 $this->
id = $obj->rowid;
509 $this->fk_element = $obj->fk_element;
510 $this->origin_id = $obj->fk_element;
511 $this->fk_elementdet = $obj->fk_elementdet;
512 $this->origin_line_id = $obj->fk_elementdet;
513 $this->element_type = $obj->element_type;
514 $this->origin_type = $obj->element_type;
515 $this->fk_product = $obj->fk_product;
516 $this->qty = $obj->qty;
517 $this->fk_entrepot = $obj->fk_entrepot;
518 $this->fk_user = $obj->fk_user;
519 $this->datec = $this->db->jdate($obj->datec);
520 $this->comment = $obj->comment;
521 $this->
status = $obj->status;
522 $this->tms = $this->db->jdate($obj->tms);
523 $this->batch = $obj->batch;
524 $this->cost_price = $obj->cost_price;
525 $this->ref_fourn = $obj->ref_fourn;
526 $this->eatby = $this->db->jdate($obj->eatby);
527 $this->sellby = $this->db->jdate($obj->sellby);
529 $this->fk_unit = $obj->fk_unit;
530 $this->rang = $obj->rang;
531 $this->fk_reception = $obj->fk_reception;
535 $this->db->free($resql);
539 $this->error =
"Error ".$this->db->lasterror();
552 public function update($user, $notrigger = 0)
558 if (isset($this->fk_element)) {
559 $this->fk_element = (int) $this->fk_element;
561 if (isset($this->fk_product)) {
562 $this->fk_product = (int) $this->fk_product;
564 if (isset($this->fk_elementdet)) {
565 $this->fk_elementdet = (int) $this->fk_elementdet;
567 if (isset($this->qty)) {
568 $this->qty = (float) $this->qty;
570 if (isset($this->fk_entrepot)) {
571 $this->fk_entrepot = (int) $this->fk_entrepot;
573 if (isset($this->fk_user)) {
574 $this->fk_user = (int) $this->fk_user;
576 if (isset($this->comment)) {
577 $this->comment = trim($this->comment);
579 if (isset($this->
status)) {
582 if (isset($this->batch)) {
583 $this->batch = trim($this->batch);
591 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element.
" SET";
592 $sql .=
" fk_element=".(isset($this->fk_element) ? ((int) $this->fk_element) :
"null").
",";
593 $sql .=
" fk_product=".(isset($this->fk_product) ? ((int) $this->fk_product) :
"null").
",";
594 $sql .=
" fk_elementdet=".(isset($this->fk_elementdet) ? ((int) $this->fk_elementdet) :
"null").
",";
595 $sql .=
" qty=".(isset($this->qty) ? ((float) $this->qty) :
"null").
",";
596 $sql .=
" fk_entrepot=".(isset($this->fk_entrepot) ? ((int) $this->fk_entrepot) :
"null").
",";
597 $sql .=
" fk_user=".(isset($this->fk_user) ? ((int) $this->fk_user) :
"null").
",";
598 $sql .=
" datec=".(dol_strlen($this->datec) != 0 ?
"'".$this->db->idate($this->datec).
"'" :
'null').
",";
599 $sql .=
" comment=".(isset($this->comment) ?
"'".$this->db->escape($this->comment).
"'" :
"null").
",";
600 $sql .=
" status=".(isset($this->
status) ? ((int) $this->
status) :
"null").
",";
601 $sql .=
" tms=".(dol_strlen((
string) $this->tms) != 0 ?
"'".$this->db->idate($this->tms).
"'" :
'null').
",";
602 $sql .=
" batch=".(isset($this->batch) ?
"'".$this->db->escape($this->batch).
"'" :
"null").
",";
603 $sql .=
" cost_price=".(isset($this->cost_price) ? ((float) $this->cost_price) :
"0").
",";
604 $sql .=
" ref_fourn=".(isset($this->ref_fourn) ?
"'".$this->db->escape($this->ref_fourn).
"'" :
"null").
",";
605 $sql .=
" eatby=".(dol_strlen((
string) $this->eatby) != 0 ?
"'".$this->db->idate((
int) $this->eatby).
"'" :
'null').
",";
606 $sql .=
" sellby=".(dol_strlen((
string) $this->sellby) != 0 ?
"'".$this->db->idate((
int) $this->sellby).
"'" :
'null').
",";
607 $sql .=
" fk_unit = ".((int) $this->fk_unit);
608 $sql .=
" WHERE rowid=".((int) $this->
id);
613 $resql = $this->db->query($sql);
616 $this->errors[] =
"Error ".$this->db->lasterror();
620 if (empty($this->
id) && !empty($this->rowid)) {
621 $this->
id = $this->rowid;
630 $result = $this->call_trigger(
'LINERECEPTION_MODIFY', $user);
640 foreach ($this->errors as $errmsg) {
642 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
644 $this->db->rollback();
785 $langs->load(
'orders');
788 return $langs->trans($this->labelStatus[$status]);
789 } elseif ($mode == 1) {
790 return $langs->trans($this->labelStatusShort[$status]);
791 } elseif ($mode == 2) {
792 return $langs->trans($this->labelStatus[$status]);
793 } elseif ($mode == 3) {
795 return img_picto($langs->trans($this->labelStatus[$status]),
'statut0');
796 } elseif ($status == 1) {
797 return img_picto($langs->trans($this->labelStatus[$status]),
'statut4');
798 } elseif ($status == 2) {
799 return img_picto($langs->trans($this->labelStatus[$status]),
'statut8');
801 } elseif ($mode == 4) {
803 return img_picto($langs->trans($this->labelStatus[$status]),
'statut0').
' '.$langs->trans($this->labelStatus[$status]);
804 } elseif ($status == 1) {
805 return img_picto($langs->trans($this->labelStatus[$status]),
'statut4').
' '.$langs->trans($this->labelStatus[$status]);
806 } elseif ($status == 2) {
807 return img_picto($langs->trans($this->labelStatus[$status]),
'statut8').
' '.$langs->trans($this->labelStatus[$status]);
809 } elseif ($mode == 5) {
811 return '<span class="hideonsmartphone">'.$langs->trans($this->labelStatusShort[$status]).
' </span>'.img_picto($langs->trans($this->labelStatus[$status]),
'statut0');
812 } elseif ($status == 1) {
813 return '<span class="hideonsmartphone">'.$langs->trans($this->labelStatusShort[$status]).
' </span>'.img_picto($langs->trans($this->labelStatus[$status]),
'statut4');
814 } elseif ($status == 2) {
815 return '<span class="hideonsmartphone">'.$langs->trans($this->labelStatusShort[$status]).
' </span>'.img_picto($langs->trans($this->labelStatus[$status]),
'statut8');
861 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, $filter =
'', $filtermode =
'AND')
867 $sql .=
" t.fk_element,";
868 $sql .=
" t.fk_product,";
869 $sql .=
" t.fk_elementdet,";
871 $sql .=
" t.fk_entrepot,";
872 $sql .=
" t.fk_user,";
874 $sql .=
" t.comment,";
875 $sql .=
" t.status,";
879 $sql .=
" t.sellby,";
880 $sql .=
" t.fk_unit,";
881 $sql .=
" t.description,";
883 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" as t";
886 if (is_array($filter)) {
888 if (count($filter) > 0) {
889 foreach ($filter as $key => $value) {
890 if ($key ==
't.comment') {
891 $sqlwhere [] = $this->db->sanitize($key).
" LIKE '%".$this->db->escape($this->db->escapeforlike($value)).
"%'";
892 } elseif ($key ==
't.datec' || $key ==
't.tms' || $key ==
't.eatby' || $key ==
't.sellby' || $key ==
't.batch') {
893 $sqlwhere [] = $this->db->sanitize($key).
" = '".$this->db->escape($value).
"'";
894 } elseif ($key ==
'qty') {
895 $sqlwhere [] = $this->db->sanitize($key).
" = ".((float) $value);
897 $sqlwhere [] = $this->db->sanitize($key).
" = ".((int) $value);
901 if (count($sqlwhere) > 0) {
902 $sql .=
' WHERE '.implode(
' '.$this->db->sanitize($filtermode).
' ', $sqlwhere);
912 $this->errors[] = $errormessage;
913 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
917 if (!empty($sortfield)) {
918 $sql .= $this->db->order($sortfield, $sortorder);
920 if (!empty($limit)) {
921 $sql .= $this->db->plimit($limit, $offset);
923 $this->lines = array();
925 $resql = $this->db->query($sql);
927 $num = $this->db->num_rows($resql);
929 while ($obj = $this->db->fetch_object($resql)) {
930 $line =
new self($this->db);
932 $line->id = $obj->rowid;
934 $line->fk_element = $obj->fk_element;
935 $line->fk_product = $obj->fk_product;
936 $line->fk_elementdet = $obj->fk_elementdet;
937 $line->qty = $obj->qty;
938 $line->fk_entrepot = $obj->fk_entrepot;
939 $line->fk_user = $obj->fk_user;
940 $line->datec = $this->db->jdate($obj->datec);
941 $line->comment = $obj->comment;
942 $line->status = $obj->status;
943 $line->tms = $this->db->jdate($obj->tms);
944 $line->batch = $obj->batch;
945 $line->eatby = $this->db->jdate($obj->eatby);
946 $line->sellby = $this->db->jdate($obj->sellby);
947 $line->description = $obj->description;
948 $line->fk_unit = $obj->fk_unit;
949 $line->rang = $obj->rang;
950 $line->fetch_optionals();
952 $this->lines[$line->id] = $line;
954 $this->db->free($resql);
958 $this->errors[] =
'Error '.$this->db->lasterror();
959 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);