mirror of
https://github.com/opensourcepos/opensourcepos.git
synced 2026-10-08 10:32:09 -04:00
feat(items): optimize search, attribute filtering, and sort for items view (#4652) BREAKING CHANGE: none ## Search & Query Optimization - Split item search into two-phase query: Phase A resolves qualifying IDs, Phase B joins scoped display tables for better performance and readability - Sanitize `definition_ids` via `array_map` to prevent injection vulnerabilities - Introduce subquery for SUM aggregation to prevent over-counting across joins - Add validation requiring both start and end dates before applying date range filter ## Attribute Search (fixes #2919, #2722) - Add `SHOW_IN_SEARCH` flag (value 8) to Attribute model to separate searchability from table visibility - Add `parse_attribute_search()` to parse syntax like `color:blue AND size:large` - Add `applyNamedAttributeSearch()` supporting decimal and date types with locale-aware parsing - Support AND/OR logic for multi-attribute queries ## Sorting - Add `get_attribute_sort_definition_id()` to detect attribute column sorting - Join attribute tables dynamically when sorting by attribute columns - Use `MAX()` for consistent results when sorting by attribute values - Replace static sort column list with dynamic headers via `itemSortColumns()` - Add `sanitizeSortColumnAttribute()` to validate attribute definition IDs as sort columns ## Tax & Data Row - Streamline tax computation in `getItemDataRow()` ## Low Inventory Filter - Require valid `stock_location_id` before applying low inventory filter - Add conditional logic to sort by sum of quantities across all locations when `stock_location_id` is invalid ## Localization - Add `show_in_search` / `show_in_search_visibility` strings to all language files - Translated: de-DE, es-ES, fr, it; English placeholder for remaining locales - Unify single-quote style across all attribute language files ## Refactoring & Style - Adopt camelCase naming throughout (variables, helpers, methods) - Replace `sanitizeSortColumnAttribute` with reusable `sanitizeSortColumn` from `Secure_Controller` - Simplify column key extraction using `array_key_first` - Apply PSR-12 formatting ## Tests - Add tests for tax computation, quantity aggregation (single- and multi-location), named attribute search, free-text parsing, and date/decimal type handling - Add `ensureStockLocation` helper to auto-create missing stock locations in tests - Refactor tests to handle config cache issues Co-authored-by: Ollama <ollama@steganos.dev>
110 lines
2.6 KiB
PHP
110 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
/**
|
|
* Item_taxes class
|
|
*/
|
|
class Item_taxes extends Model
|
|
{
|
|
protected $table = 'item_taxes';
|
|
protected $primaryKey = 'item_id';
|
|
protected $useAutoIncrement = false;
|
|
protected $useSoftDeletes = false;
|
|
protected $allowedFields = [
|
|
'name',
|
|
'percent'
|
|
];
|
|
|
|
/**
|
|
* Gets tax info for a particular item
|
|
*/
|
|
public function get_info(int $item_id): array
|
|
{
|
|
$builder = $this->db->table('items_taxes');
|
|
$builder->where('item_id', $item_id);
|
|
|
|
// Return an array of taxes for an item
|
|
return $builder->get()->getResultArray();
|
|
}
|
|
|
|
/**
|
|
* @return array item_id => array of taxes for that item
|
|
*/
|
|
public function getInfoMultiple(array $itemIds): array
|
|
{
|
|
if (empty($itemIds)) {
|
|
return [];
|
|
}
|
|
|
|
$builder = $this->db->table('items_taxes');
|
|
$builder->whereIn('item_id', $itemIds);
|
|
|
|
$taxesByItemId = [];
|
|
foreach ($builder->get()->getResultArray() as $tax) {
|
|
$taxesByItemId[$tax['item_id']][] = $tax;
|
|
}
|
|
|
|
return $taxesByItemId;
|
|
}
|
|
|
|
/**
|
|
* Inserts or updates an item's taxes
|
|
*/
|
|
public function save_value(array &$items_taxes_data, int $item_id): bool
|
|
{
|
|
$success = true;
|
|
|
|
$this->delete($item_id);
|
|
|
|
$builder = $this->db->table('items_taxes');
|
|
|
|
foreach ($items_taxes_data as $row) {
|
|
$row['item_id'] = $item_id;
|
|
$success &= $builder->insert($row);
|
|
}
|
|
|
|
return $success;
|
|
}
|
|
|
|
/**
|
|
* Saves taxes for multiple items
|
|
*/
|
|
public function save_multiple(array &$items_taxes_data, string $item_ids): bool // TODO: investigate why this is sent as a : delimited string rather than an array.
|
|
{
|
|
$success = true;
|
|
|
|
// Run these queries as a transaction, we want to make sure we do all or nothing
|
|
$this->db->transStart();
|
|
|
|
foreach (explode(':', $item_ids) as $item_id) {
|
|
$this->delete($item_id);
|
|
|
|
$builder = $this->db->table('items_taxes');
|
|
|
|
foreach ($items_taxes_data as $row) {
|
|
$row['item_id'] = $item_id;
|
|
$success &= $builder->insert($row);
|
|
}
|
|
}
|
|
|
|
$this->db->transComplete();
|
|
|
|
$success &= $this->db->transStatus();
|
|
|
|
return $success;
|
|
}
|
|
|
|
/**
|
|
* Deletes taxes given an item
|
|
*/
|
|
public function delete($item_id = null, bool $purge = false): bool
|
|
{
|
|
$builder = $this->db->table('items_taxes');
|
|
|
|
return $builder->delete(['item_id' => $item_id]);
|
|
}
|
|
}
|