Skip to content

A free open source IT asset/license management system

Eager load orderItem

snipe8bbff04ed4c6
diff --git a/app/Models/Order.php b/app/Models/Order.php
index fb5466f002..6e3292275b 100644
--- a/app/Models/Order.php
+++ b/app/Models/Order.php
@@ -8,21 +8,19 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
- * An acquisition record for inventory. One row per real-world order with
- * an optional order_number, supplier, purchase_date, and company scope.
- * Line items (the actual things acquired and their quantities / prices)
- * live on the OrderItem polymorphic pivot so a single Order can carry
+ * Acquisition record for inventory. One row per real-world order with
+ * an optional order_number, supplier, purchase_date, currency, and
+ * company scope. Line items (things acquired, quantities, prices) live
+ * on the OrderItem polymorphic pivot so a single Order can carry
* accessories, consumables, components, assets, and license seats
- * side-by-side.
+ * side by side.
*
- * Explicitly NOT a purchase-order-workflow model — no state machine,
- * approvals, or receiving flow. This is just the "what was acquired,
- * when, from whom" record that the adjust-quantity flow and importers
- * write to today. A richer workflow (or a dedicated PurchaseOrder model)
- * can layer on later without disrupting this data.
+ * Explicitly NOT a purchase-order workflow (no state machine,
+ * approvals, receiving). Just the "what was acquired, when, from
+ * whom" record that the adjust-quantity flow, the AssetObserver, and
+ * the CSV importers write to.
*
- * File attachments (receipt / invoice PDFs) are not modeled here yet —
- * follow-up if / when it's actually needed.
+ * File attachments (receipt / invoice PDFs) are not modeled here yet.
*/
class Order extends SnipeModel
{
@@ -64,4 +62,22 @@ class Order extends SnipeModel
{
return $this->belongsTo(User::class, 'created_by');
}
+
+ /**
+ * Currency to display for this order. Falls back to the system
+ * default when the row's own `currency` column is null, so display
+ * code doesn't have to reach for `$snipeSettings->default_currency`
+ * itself. Returns null when neither is set (rare, but possible on
+ * fresh installs with the default_currency setting unwritten).
+ */
+ public function displayCurrency(): ?string
+ {
+ if ($this->currency !== null && $this->currency !== '') {
+ return $this->currency;
+ }
+
+ $default = Setting::getSettings()?->default_currency;
+
+ return ($default !== null && $default !== '') ? $default : null;
+ }
}
diff --git a/app/Models/OrderItem.php b/app/Models/OrderItem.php
index 8e2aff5fbb..2090f3b45b 100644
--- a/app/Models/OrderItem.php
+++ b/app/Models/OrderItem.php
@@ -12,7 +12,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
* One line on an Order. Polymorphic to the inventory model that was
* acquired (Accessory / Consumable / Component / Asset / License) so a
* single Order can span multiple item types. qty is the number of units
- * on this line; price is the per-unit cost (matches the semantic that
+ * on this line. Price is the per-unit cost (matches the semantic that
* the existing purchase_cost columns use across the codebase).
*/
class OrderItem extends Model
diff --git a/app/Models/Traits/AdjustsQuantity.php b/app/Models/Traits/AdjustsQuantity.php
index ff9e0ebabe..467af7a422 100644
--- a/app/Models/Traits/AdjustsQuantity.php
+++ b/app/Models/Traits/AdjustsQuantity.php
@@ -20,9 +20,10 @@ use Illuminate\Support\Facades\DB;
* action_logs with:
* - quantity = signed delta (positive for replenish, negative for decrement)
* - note = the operator's reason (required by the controller)
- * - order_id = optional FK to the Order row this replenishment
- * belongs to. The caller creates / looks up the Order and passes
- * the id; the trait does not touch the Orders table itself.
+ * - order_item_id = optional FK to the specific OrderItem line this
+ * event produced. The parent Order is reachable via the OrderItem.
+ * The caller creates / looks up the OrderItem and passes the id;
+ * the trait does not touch the Orders / OrderItems tables itself.
*
* License overrides adjustQuantity() so that adding or removing
* seats also creates or destroys the matching LicenseSeat pivot rows.
@@ -91,7 +92,7 @@ trait AdjustsQuantity
* (which extends RuntimeException) as a
* below-floor violation.
*/
- public function adjustQuantity(int $delta, string $note, ?int $orderId = null, ?string $filename = null): void
+ public function adjustQuantity(int $delta, string $note, ?int $orderItemId = null, ?string $filename = null): void
{
$column = $this->getAdjustableQuantityColumn();
$current = (int) ($this->{$column} ?? 0);
@@ -104,7 +105,7 @@ trait AdjustsQuantity
);
}
- DB::transaction(function () use ($delta, $column, $current, $note, $orderId, $filename) {
+ DB::transaction(function () use ($delta, $column, $current, $note, $orderItemId, $filename) {
// Use the query builder directly (not $this->increment) so
// the model's `updated` event doesn't fire. Firing it would
// write a second "update" action_log entry (with log_meta of
@@ -128,7 +129,7 @@ trait AdjustsQuantity
$log->item_id = $this->id;
$log->created_by = auth()->id();
$log->note = $note;
- $log->order_id = $orderId;
+ $log->order_item_id = $orderItemId;
$log->quantity = $delta;
// Feed the history-tab's "changed" column so QuantityAdjust
// entries render "qty: 5 → 8" the same way an update-type
diff --git a/app/Models/Traits/HasOrders.php b/app/Models/Traits/HasOrders.php
index 46e1d3a929..8df2253970 100644
--- a/app/Models/Traits/HasOrders.php
+++ b/app/Models/Traits/HasOrders.php
@@ -31,7 +31,7 @@ trait HasOrders
* the acquisition ledger — the Order row and its (now-trashed)
* OrderItem lines remain queryable via `withTrashed()` for
* historical reports, while ordinary `->orderItems()` reads exclude
- * them. Soft-delete on the parent does not propagate; the Order
+ * them. Soft-delete on the parent does not propagate. The Order
* data model treats a soft-deleted inventory row as still-existing.
*/
protected static function bootHasOrders(): void
diff --git a/app/Models/Traits/Loggable.php b/app/Models/Traits/Loggable.php
index 02c008644a..cf5e8b03a1 100644
--- a/app/Models/Traits/Loggable.php
+++ b/app/Models/Traits/Loggable.php
@@ -74,7 +74,12 @@ trait Loggable
// Start with the polymorphic history relation so all filters and
// ordering are applied to the same query instance.
- $history = $this->history();
+ //
+ // 'orderItem.order' eager-loaded because ActionlogsTransformer
+ // renders order_number / purchase_order via the OrderItem line
+ // to its parent Order. Without this the transformer fires an
+ // N+1 subquery per row in the history tab.
+ $history = $this->history()->with('orderItem.order');
if ($request->filled('search')) {
$history = $history->TextSearch(e($request->input('search')));