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. * 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 $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 * @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 * * 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") * @return float 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")
* *
* @throws Exception if there are no pricedetails for the choosed quantity * @throws Exception if there are no pricedetails for the choosed quantity
* (for example, there are only one pricedetails with the minimum discount quantity '10', * (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!) * but the choosed quantity is '5' --> the price for 5 parts is not defined!)
* @throws Exception if there was an error * @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 (($quantity == 0) && ($multiplier === null)) {
if ($as_money_string) { return 0.0;
return floatToMoneyString(0);
} else {
return 0;
}
} }
$all_pricedetails = $this->getPricedetails(); $all_pricedetails = $this->getPricedetails();
if (count($all_pricedetails) == 0) { if (count($all_pricedetails) == 0) {
if ($as_money_string) {
return floatToMoneyString(null);
} else {
return null; return null;
}
} }
$correct_pricedetails = null;
foreach ($all_pricedetails as $pricedetails) { foreach ($all_pricedetails as $pricedetails) {
// choose the correct pricedetails for the choosed quantity ($quantity) // choose the correct pricedetails for the choosed quantity ($quantity)
if ($quantity < $pricedetails->getMinDiscountQuantity()) { if ($quantity < $pricedetails->getMinDiscountQuantity()) {
@ -241,18 +228,15 @@ class Orderdetail extends DBElement
$correct_pricedetails = $pricedetails; $correct_pricedetails = $pricedetails;
} }
if (! isset($correct_pricedetails) || (! \is_object($correct_pricedetails))) { if ($correct_pricedetails == null) {
throw new Exception(_('Es sind keine Preisinformationen für die angegebene Bestellmenge vorhanden!')); return null;
} }
if ($multiplier === null) { if ($multiplier === null) {
$multiplier = $quantity; $multiplier = $quantity;
} }
return $correct_pricedetails->getPrice($as_money_string, $multiplier); return $correct_pricedetails->getPricePerUnit($multiplier);
* */
//TODO
throw new \Exception('Not implemented yet...');
} }
/******************************************************************************** /********************************************************************************

View file

@ -626,39 +626,24 @@ class Part extends AttachmentContainingDBElement
* *
* This method simply gets the prices of the orderdetails and prepare them.\n * 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. * In the returned array/string there is a price for every supplier.
* * @param int $quantity this is the quantity to choose the correct priceinformation
* @param bool $float_array * if true, the returned array is an array of floats * @param int|null $multiplier * This is the multiplier which will be applied to every single price
* * 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 * * 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 * @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 float[] 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)
* *
* If there are orderdetails without prices, for these orderdetails there * @throws \Exception if there was an error
* 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
*/ */
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(); $prices = array();
foreach ($this->getOrderdetails($hide_obsolete) as $details) { foreach ($this->getOrderdetails($hide_obsolete) as $details) {
$prices[] = $details->getPrice(!$float_array, $quantity, $multiplier); $prices[] = $details->getPrice($quantity, $multiplier);
} }
if (\is_string($delimeter)) { return $prices;
return implode($delimeter, $prices);
} else {
return $prices;
}
} }
/** /**
@ -667,21 +652,17 @@ class Part extends AttachmentContainingDBElement
* With the $multiplier you're able to multiply the price before it will be returned. * 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. * 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 $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 * @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 * * If you pass NULL, the number from $quantity will be used
* *
* @return float price (if "$as_money_string == false") * @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; $average_price = null;
$count = 0; $count = 0;
@ -696,11 +677,7 @@ class Part extends AttachmentContainingDBElement
$average_price /= $count; $average_price /= $count;
} }
if ($as_money_string) { return $average_price;
return floatToMoneyString($average_price);
} else {
return $average_price;
}
} }
/** /**

View file

@ -32,7 +32,7 @@
</h6> </h6>
<h6 title="{% trans %}part.avg_price.label{% endtrans %}"> <h6 title="{% trans %}part.avg_price.label{% endtrans %}">
<i class="fas fa-money-bill-alt fa-fw"></i> <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> </h6>
{# {#
{% if part.comment != "" %} {% if part.comment != "" %}