48 lines
1.1 KiB
PHP
48 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace KupShop\UnitsBundle\Utils;
|
|
|
|
class PiecesRounder
|
|
{
|
|
protected $units;
|
|
|
|
public function roundPieces($product, $pieces)
|
|
{
|
|
$units = $this->loadUnits();
|
|
$unitID = $product->unit['id'] ?? null;
|
|
|
|
$pieces = str_replace(',', '.', $pieces);
|
|
|
|
if ($unitID && $units[$unitID]['pieces_precision']) {
|
|
switch ($units[$unitID]['pieces_precision']) {
|
|
case 0.0001: $round = 4;
|
|
break;
|
|
case 0.001: $round = 3;
|
|
break;
|
|
case 0.01: $round = 2;
|
|
break;
|
|
case 0.1: $round = 1;
|
|
break;
|
|
case 0:
|
|
default: $round = 0;
|
|
}
|
|
|
|
return round($pieces, $round);
|
|
}
|
|
|
|
return intval($pieces);
|
|
}
|
|
|
|
protected function loadUnits()
|
|
{
|
|
if (!$this->units) {
|
|
$this->units = sqlFetchAll(sqlQueryBuilder()
|
|
->select('*')
|
|
->from('products_units')
|
|
->execute(), 'id');
|
|
}
|
|
|
|
return $this->units;
|
|
}
|
|
}
|