Show average price of a part in part info.

This commit is contained in:
Jan Böhmer 2019-08-02 15:10:29 +02:00
parent baa0466633
commit a40d4df818
3 changed files with 22 additions and 61 deletions

View file

@ -193,45 +193,32 @@ class Orderdetail extends DBElement
/**
* Get the price for a specific quantity.
*
* @param bool $as_money_string * if true, this method returns a money string incl. currency
* * if false, this method returns the price as float
* @param int $quantity this is the quantity to choose the correct pricedetails
* @param int|null $multiplier * This is the multiplier which will be applied to every single price
* * If you pass NULL, the number from $quantity will be used
*
* @return float|string|null float: the price as a float number (if "$as_money_string == false")
* * null: if there are no prices and "$as_money_string == false"
* * string: the price as a string incl. currency (if "$as_money_string == true")
* @return float float: the price as a float number (if "$as_money_string == false")
*
* @throws Exception if there are no pricedetails for the choosed quantity
* (for example, there are only one pricedetails with the minimum discount quantity '10',
* but the choosed quantity is '5' --> the price for 5 parts is not defined!)
* @throws Exception if there was an error
*
* @see floatToMoneyString()
*/
public function getPrice(bool $as_money_string = false, int $quantity = 1, $multiplier = null)
public function getPrice(int $quantity = 1, $multiplier = null) : ?float
{
/*
if (($quantity == 0) && ($multiplier === null)) {
if ($as_money_string) {
return floatToMoneyString(0);
} else {
return 0;
}
return 0.0;
}
$all_pricedetails = $this->getPricedetails();
if (count($all_pricedetails) == 0) {
if ($as_money_string) {
return floatToMoneyString(null);
} else {
return null;
}
}
$correct_pricedetails = null;
foreach ($all_pricedetails as $pricedetails) {
// choose the correct pricedetails for the choosed quantity ($quantity)
if ($quantity < $pricedetails->getMinDiscountQuantity()) {
@ -241,18 +228,15 @@ class Orderdetail extends DBElement
$correct_pricedetails = $pricedetails;
}
if (! isset($correct_pricedetails) || (! \is_object($correct_pricedetails))) {
throw new Exception(_('Es sind keine Preisinformationen für die angegebene Bestellmenge vorhanden!'));
if ($correct_pricedetails == null) {
return null;
}
if ($multiplier === null) {
$multiplier = $quantity;
}
return $correct_pricedetails->getPrice($as_money_string, $multiplier);
* */
//TODO
throw new \Exception('Not implemented yet...');
return $correct_pricedetails->getPricePerUnit($multiplier);
}
/********************************************************************************

View file

@ -626,40 +626,25 @@ class Part extends AttachmentContainingDBElement
*
* This method simply gets the prices of the orderdetails and prepare them.\n
* In the returned array/string there is a price for every supplier.
*
* @param bool $float_array * if true, the returned array is an array of floats
* * if false, the returned array is an array of strings
* @param string|null $delimeter if this is a string, this method returns a delimeted string
* instead of an array.
* @param int $quantity this is the quantity to choose the correct priceinformation
* @param int|null $multiplier * This is the multiplier which will be applied to every single price
* * If you pass NULL, the number from $quantity will be used
* @param bool $hide_obsolete If true, prices from obsolete orderdetails will NOT be returned
*
* @return array all prices as an array of floats (if "$delimeter == NULL" & "$float_array == true")
* @return array all prices as an array of strings (if "$delimeter == NULL" & "$float_array == false")
* @return string all prices as a string, delimeted by $delimeter (if $delimeter is a string)
* @return float[] all prices as an array of floats (if "$delimeter == NULL" & "$float_array == true")
*
* If there are orderdetails without prices, for these orderdetails there
* will be a "NULL" in the returned float array (or a "-" in the string array)!!
* (This is needed for the HTML output, if there are all orderdetails and prices listed.)
*
* @throws Exception if there was an error
* @throws \Exception if there was an error
*/
public function getPrices(bool $float_array = false, $delimeter = null, int $quantity = 1, $multiplier = null, bool $hide_obsolete = false)
public function getPrices(int $quantity = 1, $multiplier = null, bool $hide_obsolete = false) : array
{
$prices = array();
foreach ($this->getOrderdetails($hide_obsolete) as $details) {
$prices[] = $details->getPrice(!$float_array, $quantity, $multiplier);
$prices[] = $details->getPrice($quantity, $multiplier);
}
if (\is_string($delimeter)) {
return implode($delimeter, $prices);
} else {
return $prices;
}
}
/**
* Get the average price of all orderdetails.
@ -667,21 +652,17 @@ class Part extends AttachmentContainingDBElement
* With the $multiplier you're able to multiply the price before it will be returned.
* This is useful if you want to have the price as a string with currency, but multiplied with a factor.
*
* @param bool $as_money_string * if true, the retruned value will be a string incl. currency,
* ready to print it out. See float_to_money_string().
* * if false, the returned value is a float
* @param int $quantity this is the quantity to choose the correct priceinformations
* @param int|null $multiplier * This is the multiplier which will be applied to every single price
* * If you pass NULL, the number from $quantity will be used
*
* @return float price (if "$as_money_string == false")
* @return string price with currency (if "$as_money_string == true")
*
* @throws Exception if there was an error
* @throws \Exception if there was an error
*/
public function getAveragePrice(bool $as_money_string = false, int $quantity = 1, $multiplier = null)
public function getAveragePrice(int $quantity = 1, $multiplier = null) : float
{
$prices = $this->getPrices(true, null, $quantity, $multiplier, true);
$prices = $this->getPrices($quantity, $multiplier, true);
$average_price = null;
$count = 0;
@ -696,12 +677,8 @@ class Part extends AttachmentContainingDBElement
$average_price /= $count;
}
if ($as_money_string) {
return floatToMoneyString($average_price);
} else {
return $average_price;
}
}
/**
* Get the filename of the master picture (absolute path from filesystem root).

View file

@ -32,7 +32,7 @@
</h6>
<h6 title="{% trans %}part.avg_price.label{% endtrans %}">
<i class="fas fa-money-bill-alt fa-fw"></i>
<span class="text-muted">{{ "TODO" }}</span>
<span class="text-muted">{{ part.averagePrice | moneyFormat }}</span>
</h6>
{#
{% if part.comment != "" %}