Add model functions needed for plugins

- getDateAdded and getDatesAdded in Inventory.php
- GetDistinctCategories in Item.php
- GetBulkItemQuantities in Item_quantity.php
- GetBulkInfo in Item_taxes.php
- GetStockLocationsByItem in Stock_location.php

Signed-off-by: objec <objecttothis@gmail.com>
This commit is contained in:
objec
2026-05-18 16:15:24 +04:00
parent ad097adccd
commit 1c2112a78b
5 changed files with 105 additions and 0 deletions

View File

@@ -60,6 +60,39 @@ class Inventory extends Model
return $builder->get();
}
/**
* Retrieves the earliest transaction date (date added) for an item.
*
* @param int $itemId
* @return string|null The earliest trans_date in Y-m-d H:i:s format, or null if no transactions exist
*/
public function getDateAdded(int $itemId): ?string
{
$result = $this->db->table('inventory')
->selectMin('trans_date', 'date_added')
->where('trans_items', $itemId)
->get()
->getRowArray();
return $result['date_added'] ?? null;
}
public function getDatesAdded(array $itemIds): array
{
if (empty($itemIds)) {
return [];
}
$results = $this->db->table('inventory')
->select('trans_items, MIN(trans_date) AS date_added')
->whereIn('trans_items', $itemIds)
->groupBy('trans_items')
->get()
->getResultArray();
return array_column($results, 'date_added', 'trans_items');
}
/**
* @param int $item_id ID number for the item to have quantity reset.
* @return bool|int|string The row id of the inventory table on insert or false on failure

View File

@@ -303,6 +303,22 @@ class Item extends Model
return $builder->get();
}
public function getDistinctCategories(): array
{
$results = $this->db->table('items')
->select('category')
->where('deleted', 0)
->where('category !=', '')
->where('category IS NOT NULL')
->distinct()
->orderBy('category', 'ASC')
->get()
->getResultArray();
$categories = array_column($results, 'category');
return array_combine($categories, $categories);
}
/**
* Gets information about a particular item
*/

View File

@@ -83,6 +83,27 @@ class Item_quantity extends Model
return $result;
}
/**
* Get all the quantities of the given items. Used by plugins. Do not remove from code.
*
* @param array $itemIds
* @param bool $getDeleted
* @return array
*/
public function getBulkItemQuantities(array $itemIds, bool $getDeleted = false): array
{
$builder = $this->db->table('item_quantities');
$builder->whereIn('item_quantities.item_id', $itemIds);
if (!$getDeleted) {
$builder->join('stock_locations', 'stock_locations.location_id = item_quantities.location_id');
$builder->where('stock_locations.deleted', 0);
$builder->select('item_quantities.*');
}
return $builder->get()->getResultArray();
}
/**
* changes to quantity of an item according to the given amount.
* if $quantity_change is negative, it will be subtracted,

View File

@@ -30,6 +30,28 @@ class Item_taxes extends Model
return $builder->get()->getResultArray();
}
/**
* Get all the taxes of given items. Used by plugins. Do not remove from code.
*
* @param array $itemIds
* @param bool $getDeleted
* @return array
*/
public function getBulkInfo(array $itemIds, bool $getDeleted = false): array
{
$builder = $this->db->table($this->table);
$builder->whereIn('items_taxes.item_id', $itemIds);
if (!$getDeleted) {
$builder->join('items', 'items.item_id = items_taxes.item_id');
$builder->where('items.deleted', 0);
$builder->select('items_taxes.*');
}
return $builder->get()->getResultArray();
}
/**
* Inserts or updates an item's taxes
*/

View File

@@ -260,6 +260,19 @@ class Stock_location extends Model
}
}
public function getStockLocationsByItem(int $itemId): array
{
return $this->db->table('item_quantities')
->select('stock_locations.location_name, item_quantities.quantity')
->join('stock_locations', 'stock_locations.location_id = item_quantities.location_id')
->where('item_quantities.item_id', $itemId)
->where('stock_locations.deleted', 0)
->where('item_quantities.quantity >', 0)
->orderBy('item_quantities.quantity', 'DESC')
->get()
->getResultArray();
}
/**
* Deletes one item
* @param int|null $location_id