Files
opensourcepos/app/Models/Item_quantity.php
objecttothis 29a9b1a7e7 Bugfix: Resolve Race Condition in Rewards and Gift Card Spending (#4640)
* Implement atomic updates for gift card and reward point decrements, enhance error handling for insufficient balances, and add regression tests for concurrency safety.

* Add translations for insufficient gift card balance and reward points error messages across all supported languages.

* Reorder `clear_suspended_sale_detail` call to ensure transactional consistency.

* Reorder `clear_all` call to align with success and error handling logic.

* Ensure soft-deleted gift cards are excluded in balance updates.

* Refactor change_quantity logic with atomic upserts, improve error handling for insufficient stock, and update related tests and constants.

* Added check for NEW_ENTRY

* Added unit tests to test changes.

* Fix class name casing in ItemQuantityTest for consistency.

* Fix Bulgarian translations for insufficient balance error messages in Sales module.

* Fix Greek translations for insufficient balance error messages in Sales module.

* Fix Armenian translations for insufficient balance error messages in Sales module.

* Fix Tamil translations for insufficient balance error messages in Sales module.

* Implement race condition testing for database methods with concurrent process support.

* Fix class name casing in ItemTest for consistency.

* Improve concurrent process handling in race condition tests; add readiness and synchronization barriers.

* Improve handling of process I/O streams and timeout management in race condition tests.

* Add test for decrementing gift card value when marked as deleted

* Add `finally` block to ensure proper cleanup in async database race condition tests

* Improve error handling and timeout management in async database race condition tests.

* Refactor test utilities to use shared `EmployeeFixtureTrait` and `ItemFixtureTrait`.

* Track process exit codes explicitly in race condition tests for improved error detection and debugging.

* Improve error handling in `ConcurrentDbRaceTrait` by adding exceptions for `mysqli_poll` and `mysqli_reap_async_query`.

Signed-off-by: objecttothis <17935339+objecttothis@users.noreply.github.com>

---------

Signed-off-by: objecttothis <17935339+objecttothis@users.noreply.github.com>
2026-08-20 02:24:23 +04:00

128 lines
3.6 KiB
PHP

<?php
namespace App\Models;
use CodeIgniter\Database\RawSql;
use CodeIgniter\Model;
use stdClass;
/**
* Item_quantity class
*/
class Item_quantity extends Model
{
protected $table = 'item_quantities';
protected $primaryKey = 'item_id';
protected $useAutoIncrement = false;
protected $useSoftDeletes = false;
protected $allowedFields = [
'quantity'
];
protected $item_id;
protected $location_id;
protected $quantity;
/**
* @param int $item_id
* @param int $location_id
* @return bool
*/
public function exists(int $item_id, int $location_id): bool
{
$builder = $this->db->table('item_quantities');
$builder->where('item_id', $item_id);
$builder->where('location_id', $location_id);
return ($builder->get()->getNumRows() == 1); // TODO: ===
}
/**
* @param array $location_detail
* @param int $item_id
* @param int $location_id
* @return bool
*/
public function save_value(array $location_detail, int $item_id, int $location_id): bool
{
if (!$this->exists($item_id, $location_id)) {
$builder = $this->db->table('item_quantities');
return $builder->insert($location_detail);
}
$builder = $this->db->table('item_quantities');
$builder->where('item_id', $item_id);
$builder->where('location_id', $location_id);
return $builder->update($location_detail);
}
/**
* @param int $item_id
* @param int $location_id
* @return array|Item_quantity|stdClass|null
*/
public function get_item_quantity(int $item_id, int $location_id): array|Item_quantity|StdClass|null
{
$builder = $this->db->table('item_quantities');
$builder->where('item_id', $item_id);
$builder->where('location_id', $location_id);
$result = $builder->get()->getRow();
if (empty($result)) {
// Get empty base parent object, as $item_id is NOT an item
$result = model(Item_quantity::class);
// Get all the fields from items table (TODO: to be reviewed)
foreach ($this->db->getFieldNames('item_quantities') as $field) {
$result->$field = '';
}
$result->quantity = 0;
}
return $result;
}
/**
* Atomically changes an item's quantity at a location by a signed delta.
* Positive delta adds; negative delta subtracts. Creates the
* item_quantities row if it doesn't yet exist. Negative resulting
* quantity is allowed (no floor guard).
*/
public function changeQuantity(int $itemId, int $locationId, float $quantityChange): bool
{
$builder = $this->db->table('item_quantities');
$builder->set([
'item_id' => $itemId,
'location_id' => $locationId,
'quantity' => $quantityChange,
]);
$builder->updateFields(['quantity' => new RawSql('quantity + ' . $this->db->escape($quantityChange))]);
return $builder->upsert() !== false;
}
/**
* Set to 0 all quantity in the given item
*/
public function reset_quantity(int $item_id): bool
{
$builder = $this->db->table('item_quantities');
$builder->where('item_id', $item_id);
return $builder->update(['quantity' => 0]);
}
/**
* Set to 0 all quantity in the given list of items
*/
public function reset_quantity_list(array $item_ids): bool
{
$builder = $this->db->table('item_quantities');
$builder->whereIn('item_id', $item_ids);
return $builder->update(['quantity' => 0]);
}
}