From 1c2112a78be615df3506aa4e3d9db005157ebe6d Mon Sep 17 00:00:00 2001 From: objec Date: Mon, 18 May 2026 16:15:24 +0400 Subject: [PATCH] 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 --- app/Models/Inventory.php | 33 +++++++++++++++++++++++++++++++++ app/Models/Item.php | 16 ++++++++++++++++ app/Models/Item_quantity.php | 21 +++++++++++++++++++++ app/Models/Item_taxes.php | 22 ++++++++++++++++++++++ app/Models/Stock_location.php | 13 +++++++++++++ 5 files changed, 105 insertions(+) diff --git a/app/Models/Inventory.php b/app/Models/Inventory.php index ff9c56ad6..f594babe6 100644 --- a/app/Models/Inventory.php +++ b/app/Models/Inventory.php @@ -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 diff --git a/app/Models/Item.php b/app/Models/Item.php index efb8531d0..1b1fd1aa4 100644 --- a/app/Models/Item.php +++ b/app/Models/Item.php @@ -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 */ diff --git a/app/Models/Item_quantity.php b/app/Models/Item_quantity.php index f7c95c6e5..24ee72442 100644 --- a/app/Models/Item_quantity.php +++ b/app/Models/Item_quantity.php @@ -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, diff --git a/app/Models/Item_taxes.php b/app/Models/Item_taxes.php index c32fd5525..0f0c8f38c 100644 --- a/app/Models/Item_taxes.php +++ b/app/Models/Item_taxes.php @@ -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 */ diff --git a/app/Models/Stock_location.php b/app/Models/Stock_location.php index 358120496..2f4a7ef9f 100644 --- a/app/Models/Stock_location.php +++ b/app/Models/Stock_location.php @@ -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