Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Item prices does not work #500

Closed
mctomaszek opened this issue Nov 25, 2015 · 20 comments
Closed

Item prices does not work #500

mctomaszek opened this issue Nov 25, 2015 · 20 comments

Comments

@mctomaszek
Copy link

hi

Items does not show prices. Avg price is always 0. Current Statistics show incorrect description and values for Parts with price and Parts without price

regards

@Drachenkaetzchen
Copy link
Member

I can confirm that the average price is not displayed correctly. Can you elaborate what you mean with incorrect description and values for the statistics? Screenshot perhaps?

@mctomaszek
Copy link
Author

hi
partkeepr

please have a look:

I have only 1 item with price: parts without price should be 1535 and with price only 1
partkeepr is showing parts without price 0 and with price 1536.

regards

@mctomaszek
Copy link
Author

hi again

I have corrected two files: part.php and PartsGrid.js but avg price in part list windows is still 0

partkeepr1
I have to do something else?

regards

@Drachenkaetzchen
Copy link
Member

Have you re-ran setup?

@mctomaszek
Copy link
Author

hi

no ...I will do and I will let you know

thank U

@Drachenkaetzchen
Copy link
Member

You always need to re-run setup if you change JS files in order to be compiled and minified into a master .js file.

@mctomaszek
Copy link
Author

I have problem with re-run

Warning: Division by zero

500 Internal Server Error - ContextErrorException

Stack Trace

in src/PartKeepr/PartBundle/Entity/Part.php at line 724 -
}
$sum += $stockLevel->getStockLevel();
}
$this->setAveragePrice($price / $sum);
$this->setStockLevel($sum);
if ($sum < $this->getMinStockLevel()) {
$this->setLowStock(true);
at ErrorHandler ->handleError ('2', 'Division by zero', '/var/www/html/partkeepr-0.75/src/PartKeepr/PartBundle/Entity/Part.php', '724', array('sum' => '0', 'price' => '0', 'stockLevel' => object(StockEntry)))
in src/PartKeepr/PartBundle/Entity/Part.php at line 724 +
at Part ->recomputeStockLevels ()
in src/PartKeepr/CoreBundle/DoctrineMigrations/Version20151002183125.php at line 26 +
at Version20151002183125 ->up (object(Schema))
in vendor/doctrine/migrations/lib/Doctrine/DBAL/Migrations/Version.php at line 264 +
at Version ->execute ('up', false, false)
in vendor/doctrine/migrations/lib/Doctrine/DBAL/Migrations/Migration.php at line 175

@Drachenkaetzchen
Copy link
Member

I just added a fix, can you replace the Part.php file again with the latest from the repository?

@mctomaszek
Copy link
Author

hi

so far so good......

thank U so much :)

P.S any idea about current statistics problems?

@Drachenkaetzchen
Copy link
Member

Not yet, I will need to have a look tomorrow

@mctomaszek
Copy link
Author

hello

another problem with avg price. for example you are adding 10 items with price 1 euro. Avg price will be 1 euro.....(okay :)) ... later you are removing this stock to 0 Avg price will be 0 ( okay :))...but if you again are you going to add stock 10 with price 1 euro you will get avg price 2 euro ( this is not okay :( )

regards

@mctomaszek
Copy link
Author

if you will be add 10 items with price 1 euro and later remove to 0 and again add and remove ....avg price will be increase 2,3,4,5 .......depends on add/remove times.
Another example you are adding 1 item with price 100 euro. avg price will be 100 euro. Later removing this item and stock is 0 with avg price 0. then you are adding the same 1 item with new price 50 euro and you will get 1 item with avg price 150 euro.

best regards

@Drachenkaetzchen
Copy link
Member

This is probably related to #302

The average price is currently calculated by the sum of all parts divided by the sum of all prices.

@mctomaszek
Copy link
Author

I have found temporary fix for this problem. If you are adding item with minus price of avg price for 1 item then you can add new stock with correct avg price later one. For example 1 item with 1 euro ....avg 1....then remove 1 item avg price 0. After adding again 1 item with 1 euro ...avg price 2 euro....but if you wll be add 1 item with -1 euro avg price going to be 1 :)....

@mctomaszek
Copy link
Author

I hope is clear....

@mctomaszek
Copy link
Author

looks like when the stock is 0 after remove items. Avg price is keeping last value of item prices. If you try add new item with new price from scratch. The avg price will be calculate: previous price + new price;

@mctomaszek
Copy link
Author

when stock is 0 previous price should not be take for calculation only new price

@Drachenkaetzchen Drachenkaetzchen modified the milestones: 1.0, 0.76 Dec 1, 2015
@tinutac
Copy link
Contributor

tinutac commented Feb 2, 2016

Hi @FELICITUS ,
I took a look at the avg price calculation. My idea is to use the strategy below for updating the average price. Then you get a quite good approximation of a FIFO approach of stock use.
Basically:

  • positive stock entry -> update average price (computed as a (sum of added stock * individual price)/ total number of items).
  • negative stock entry:
    • if negative total, just set everything on zero (avg price, total items)
    • if positive total:
      • smaller than the last positive stock entry use the price of the last positive stock entry as avg price.
      • higher than the last positive stock just update the avg price using the same strategy as for a positive stock entry.

Probably the code below is somewhat more clear. I've commented the original code to make the comparing easier.
It might be interesting to be add an updateAveragePrice for an already existing database if this is an acceptable code change (a double check is always welcome).

public function recomputeStockLevels()
{
        $sum = 0;
        $price = 0;

        $totalPartStockPrice = 0;
        $lastPosEntryQuant = 0;
        $lastPosEntryPrice = 0;

        foreach ($this->getStockLevels() as $stockLevel) {

            $sum += $stockLevel->getStockLevel();

            if ($stockLevel->getStockLevel() > 0) {
                $lastPosEntryQuant = $stockLevel->getStockLevel();
                $lastPosEntryPrice = $stockLevel->getPrice();
                $totalPartStockPrice += $lastPosEntryPrice * $lastPosEntryQuant;
                $price = $totalPartStockPrice / $sum;
            }
            else {
                if  ($sum < 0) {
                    $price = 0;
                    $sum = 0;
                }
                else
                    if ($sum < $lastPosEntryQuant){
                        $totalPartStockPrice = $sum * $lastPosEntryPrice;
                        $price = $totalPartStockPrice / $sum;
                    }
                    else {
                        $totalPartStockPrice += $stockLevel->getStockLevel() * $price;
                        $price = $totalPartStockPrice / $sum;
                    }
            }
        }

/*
        foreach ($this->getStockLevels() as $stockLevel) {

            if ($stockLevel->getStockLevel() < 0) {
                $this->setRemovals(true);
            } else {
                $price += $stockLevel->getPrice() * $stockLevel->getStockLevel();
            }
            $sum += $stockLevel->getStockLevel();
        }

        if ($sum > 0) {
            $this->setAveragePrice($price / $sum);
        } else {
            $this->setAveragePrice(0);
        }

*/

        $this->setStockLevel($sum);
        $this->setAveragePrice($price);

        if ($sum < $this->getMinStockLevel()) {
            $this->setLowStock(true);
        } else {
            $this->setLowStock(false);
        }
}

Drachenkaetzchen pushed a commit that referenced this issue Mar 9, 2016
[Bugfix]: Item prices does not work #500
Drachenkaetzchen referenced this issue Mar 11, 2016
[Bugfix]: Item prices (negative total stock followed by positive entry) (Bugfix for #601)
@Drachenkaetzchen Drachenkaetzchen modified the milestones: 1.1, 1.0 Mar 14, 2016
@Drachenkaetzchen
Copy link
Member

I believe the issue should be fixed thanks to @tinutac, can you confirm?

@Drachenkaetzchen
Copy link
Member

No feedback given for an extended period of time, closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants