From c068eebc4361de2920005f2488c9512f235d36ce Mon Sep 17 00:00:00 2001 From: atm-lena Date: Tue, 12 Jul 2022 10:03:31 +0200 Subject: [PATCH] Add function "convertDurationtoHour" --- htdocs/bom/class/bom.class.php | 9 ++------- htdocs/core/lib/date.lib.php | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/htdocs/bom/class/bom.class.php b/htdocs/bom/class/bom.class.php index 1f3dd041aad..16c780a5751 100644 --- a/htdocs/bom/class/bom.class.php +++ b/htdocs/bom/class/bom.class.php @@ -24,6 +24,7 @@ // Put here all includes required by your class file require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php'; require_once DOL_DOCUMENT_ROOT.'/workstation/class/workstation.class.php'; +require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php'; //require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php'; //require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php'; @@ -1128,13 +1129,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 == 'h') $qty = $line->qty; - if($line->duration_unit == 'd') $qty = $line->qty * 24; - if($line->duration_unit == 'w') $qty = $line->qty * 24 * 7; - if($line->duration_unit == 'm') $qty = $line->qty * 730.484; - if($line->duration_unit == 'y') $qty = $line->qty * 365 * 24; + $qty = convertDurationtoHour($line->qty, $line->duration_unit); if($conf->workstation->enabled){ if($tmpproduct->fk_default_workstation) { diff --git a/htdocs/core/lib/date.lib.php b/htdocs/core/lib/date.lib.php index 558ba2aa322..98a17e062ee 100644 --- a/htdocs/core/lib/date.lib.php +++ b/htdocs/core/lib/date.lib.php @@ -318,6 +318,27 @@ function convertSecondToTime($iSecond, $format = 'all', $lengthOfDay = 86400, $l } +/** Convert duration to hour + * + * @param int $duration_value Duration value + * @param int $duration_unit Duration unit + * @return int $result + */ +function convertDurationtoHour($duration_value, $duration_unit) +{ + $result = 0; + + if($duration_unit == 's') $result = $duration_value / 3600; + if($duration_unit == 'i') $result = $duration_value / 60; + if($duration_unit == 'h') $result = $duration_value; + if($duration_unit == 'd') $result = $duration_value * 24; + if($duration_unit == 'w') $result = $duration_value * 24 * 7; + if($duration_unit == 'm') $result = $duration_value * 730.484; + if($duration_unit == 'y') $result = $duration_value * 365 * 24; + + return $result; +} + /** * Generate a SQL string to make a filter into a range (for second of date until last second of date). * This method allows to maje SQL request that will deal correctly the timezone of server.