From 16415a1c92ec249c3ace39c28075aab00310feeb Mon Sep 17 00:00:00 2001 From: atm-lena Date: Wed, 6 Jul 2022 17:28:29 +0200 Subject: [PATCH] Clean Code --- htdocs/bom/ajax/ajax.php | 4 ++-- htdocs/bom/bom_card.php | 12 ++++++------ htdocs/bom/class/bom.class.php | 17 ++++++++++++----- htdocs/bom/tpl/objectline_create.tpl.php | 8 +++----- htdocs/bom/tpl/objectline_view.tpl.php | 11 ++++------- htdocs/product/card.php | 2 +- 6 files changed, 28 insertions(+), 26 deletions(-) diff --git a/htdocs/bom/ajax/ajax.php b/htdocs/bom/ajax/ajax.php index d8ea9650b09..ef99ab214d1 100644 --- a/htdocs/bom/ajax/ajax.php +++ b/htdocs/bom/ajax/ajax.php @@ -17,8 +17,8 @@ */ /** - * \file htdocs/public/ticket/ajax/ajax.php - * \brief Ajax component for Ticket. + * \file htdocs/bom/ajax/ajax.php + * \brief Ajax component for BOM. */ if (!defined('NOTOKENRENEWAL')) { diff --git a/htdocs/bom/bom_card.php b/htdocs/bom/bom_card.php index d448551c95e..0700bad4c2a 100644 --- a/htdocs/bom/bom_card.php +++ b/htdocs/bom/bom_card.php @@ -546,16 +546,16 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea * Lines */ - if (!empty($object->table_element_line)) { - print load_fiche_titre($langs->trans('BOMProductsList'), '', 'product'); - - $res = $object->fetchLinesbytype(0); + //Products + $res = $object->fetchLinesbytypeproduct(0); $object->calculateCosts(); if($res > 0) { + print load_fiche_titre($langs->trans('BOMProductsList'), '', 'product'); + print '
@@ -602,9 +602,9 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea } + //Services $filtertype = 1; - - $res = $object->fetchLinesbytype(1); + $res = $object->fetchLinesbytypeproduct(1); $object->calculateCosts(); if($res > 0) { diff --git a/htdocs/bom/class/bom.class.php b/htdocs/bom/class/bom.class.php index 3366ae1dda1..15d16c4c9fa 100644 --- a/htdocs/bom/class/bom.class.php +++ b/htdocs/bom/class/bom.class.php @@ -401,11 +401,13 @@ class BOM extends CommonObject } /** - * Load object lines in memory from the database + * Load object lines in memory from the database by type of product * + * @param int $typeproduct 0 type product, 1 type service + * @return int <0 if KO, 0 if not found, >0 if OK */ - public function fetchLinesbytype($type = 0) + public function fetchLinesbytypeproduct($typeproduct = 0) { $this->lines = array(); @@ -421,7 +423,7 @@ class BOM extends CommonObject $sql .= " FROM ".$this->db->prefix().$objectline->table_element." as l"; $sql .= " LEFT JOIN ".$this->db->prefix()."product as p ON p.rowid = l.fk_product"; $sql .= " WHERE l.fk_".$this->db->escape($this->element)." = ".((int) $this->id); - $sql .= " AND p.fk_product_type = ". $type; + $sql .= " AND p.fk_product_type = ". $typeproduct; if (isset($objectline->fields['position'])) { $sql .= $this->db->order('position', 'ASC'); } @@ -1141,6 +1143,7 @@ class BOM extends CommonObject } } else { + //Convert qty to hour if($line->duration_unit == 's') $qty = $line->qty / 3600; if($line->duration_unit == 'i') $qty = $line->qty / 60; if($line->duration_unit == 'd') $qty = $line->qty * 24; @@ -1151,9 +1154,13 @@ class BOM extends CommonObject if($conf->workstation->enabled){ if($tmpproduct->fk_default_workstation) { $workstation = new Workstation($this->db); - $workstation->fetch($tmpproduct->fk_default_workstation); + $res = $workstation->fetch($tmpproduct->fk_default_workstation); - $line->total_cost = price2num($qty * $workstation->thm_operator_estimated, 'MT'); + if($res > 0) $line->total_cost = price2num($qty * $workstation->thm_operator_estimated, 'MT'); + else { + $this->error = $workstation->error; + return -3; + } } } else { $line->total_cost = price2num($qty * $tmpproduct->cost_price, 'MT'); diff --git a/htdocs/bom/tpl/objectline_create.tpl.php b/htdocs/bom/tpl/objectline_create.tpl.php index 7a022c1172c..f2f660bc711 100644 --- a/htdocs/bom/tpl/objectline_create.tpl.php +++ b/htdocs/bom/tpl/objectline_create.tpl.php @@ -163,7 +163,7 @@ if($filtertype != 1) { } else { $coldisplay++; print ''; - print $formproduct->selectMeasuringUnits("duration_unit", "time", (GETPOSTISSET('duration_value') ? GETPOST('duration_value', 'alpha') : 'h'), 0, 1); + print $formproduct->selectMeasuringUnits("duration_unit", "time", 'h', 0, 1); print ''; $coldisplay++; @@ -196,8 +196,6 @@ jQuery(document).ready(function() { /* When changing predefined product, we reload list of supplier prices required for margin combo */ $("#idprod").change(function() { - console.log("#idprod change triggered"); - /* To set focus */ if (jQuery('#idprod').val() > 0) { @@ -212,11 +210,11 @@ jQuery(document).ready(function() { } }); + + //change unit selected if we change service selected $('#idprod:nth-child(2)').change(function(){ var idproduct = $(this).val(); - - console.log(idproduct); $.ajax({ url : "" ,type: 'POST' diff --git a/htdocs/bom/tpl/objectline_view.tpl.php b/htdocs/bom/tpl/objectline_view.tpl.php index 540146b14fa..1699c968794 100644 --- a/htdocs/bom/tpl/objectline_view.tpl.php +++ b/htdocs/bom/tpl/objectline_view.tpl.php @@ -129,32 +129,29 @@ if($filtertype != 1) { echo $line->efficiency; print ''; } else { - $product = new Product($object->db); - $res = $product->fetch($line->fk_product); - //Unité print ''; $coldisplay++; if ($line->qty > 1) { $dur = array("s"=>$langs->trans("Seconds"), "i"=>$langs->trans("Minutes"), "h"=>$langs->trans("Hours"), "d"=>$langs->trans("Days"), "w"=>$langs->trans("Weeks"), "m"=>$langs->trans("Months"), "y"=>$langs->trans("Years")); - } elseif ($product->duration_value > 0) { + } elseif ($tmpproduct->duration_value > 0) { $dur = array("s"=>$langs->trans("Second"), "i"=>$langs->trans("Minute"), "h"=>$langs->trans("Hour"), "d"=>$langs->trans("Day"), "w"=>$langs->trans("Week"), "m"=>$langs->trans("Month"), "y"=>$langs->trans("Year")); } if(!empty($line->duration_unit)){ print (isset($dur[$line->duration_unit]) ? " ".$langs->trans($dur[$line->duration_unit])." " : ''); } else { - print (!empty($product->duration_unit) && isset($dur[$product->duration_unit]) ? " " . $langs->trans($dur[$product->duration_unit]) . " " : ''); + print (!empty($tmpproduct->duration_unit) && isset($dur[$tmpproduct->duration_unit]) ? " " . $langs->trans($dur[$tmpproduct->duration_unit]) . " " : ''); } print ''; //Poste de travail if($conf->workstation->enabled) { $workstation = new Workstation($object->db); - $workstation->fetch($product->fk_default_workstation); + $res = $workstation->fetch($tmpproduct->fk_default_workstation); print ''; $coldisplay++; - echo $workstation->getNomUrl(); + if($res > 0) echo $workstation->getNomUrl(); print ''; } } diff --git a/htdocs/product/card.php b/htdocs/product/card.php index f9ac8da8d05..eb215839fb1 100644 --- a/htdocs/product/card.php +++ b/htdocs/product/card.php @@ -2496,7 +2496,7 @@ if (is_object($objcanvas) && $objcanvas->displayCanvasExists($action)) { if($object->isService() && $conf->workstation->enabled) { $workstation = new Workstation($db); - $workstation->fetch($object->fk_default_workstation); + $res = $workstation->fetch($object->fk_default_workstation); print ''.$langs->trans("DefaultWorkstation").''; print (!empty($workstation->id) ? $workstation->getNomUrl(1) : '');