Forceu/barcodebuddy

Last price not being filled when purchasing

EnigmaticWraith opened this issue · 3 comments

For some reason, the last price is not filled when adding products; they show up as $0.00 on Grocy. I'm not sure if this is due to something on Grocy's end or Barcode Buddy's.

I can't find any errors on either side's logs, however, I did test some of the API responses from my Grocy install, and the value they return for "last_price" is correct. Also, sending an add request properly sets to whatever price I enter, and manually adding a purchase inside Grocy correctly autofills as well.

I did see there was an issue opened around a year and a half ago that seemed to confirm the expected behavior is for Barcode Buddy to use the last price.

I did some digging and was able to solve it at least for my own needs. It looks like there's support built in for barcodes that have prices attached to them? However, this causes a null to always be passed into the price field in Grocy's API which Grocy seems to interpret as 0. (It also interprets no value passed as null, and thus zero.)

I was able to fix this by adding a new variable at the end here:

$result->unit = sanitizeString($infoArray["quantity_unit_stock"]["name"]);
$result->barcodes = $infoArray["product_barcodes"];

$result->price = $infoArray["current_price "];

Then updating the price that's passed in these two lines to be $productInfo->price:

$db->insertActionRequiredBarcode($barcode, $bestBeforeInDays, $price);

API::purchaseProduct($productInfo->id, $amount, $bestBeforeInDays, $price, $fileLock, $productInfo->defaultBestBeforeDays);

Obviously, this wouldn't be the ideal solution and there should be a check to see if the price is null so it doesn't break the barcode price functionality. However, I don't plan on using that functionality (and I haven't ever really used PHP before), so this works for my purposes.

Hopefully, that points you in the direction of a somewhat simple fix though!

Thank you very much! I created an issue at the grocy tracker in this regard: grocy/grocy#2106